Skip to content

Commit

Permalink
Bug 1721997 - wasm: Check CompileArgs::build() and correctly report O…
Browse files Browse the repository at this point in the history
…OM's. r=yury

Differential Revision: https://phabricator.services.mozilla.com/D121386
  • Loading branch information
eqrion committed Aug 9, 2021
1 parent af10111 commit 1475b3b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 0 additions & 1 deletion js/src/builtin/TestingFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1967,7 +1967,6 @@ static bool WasmIntrinsicI8VecMul(JSContext* cx, unsigned argc, Value* vp) {
wasm::IntrinsicOp ops[] = {wasm::IntrinsicOp::I8VecMul};
RootedWasmModuleObject module(cx);
if (!wasm::CompileIntrinsicModule(cx, ops, wasm::Shareable::False, &module)) {
ReportOutOfMemory(cx);
return false;
}
args.rval().set(ObjectValue(*module.get()));
Expand Down
1 change: 1 addition & 0 deletions js/src/wasm/WasmCompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ SharedCompileArgs CompileArgs::build(JSContext* cx,

CompileArgs* target = cx->new_<CompileArgs>(std::move(scriptedCaller));
if (!target) {
ReportOutOfMemory(cx);
return nullptr;
}

Expand Down
13 changes: 13 additions & 0 deletions js/src/wasm/WasmIntrinsic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ bool wasm::CompileIntrinsicModule(JSContext* cx,
// Initialize the compiler environment, choosing the best tier possible
SharedCompileArgs compileArgs =
CompileArgs::build(cx, ScriptedCaller(), featureOptions);
if (!compileArgs) {
return false;
}
CompilerEnvironment compilerEnv(
CompileMode::Once, IonAvailable(cx) ? Tier::Optimized : Tier::Baseline,
OptimizedBackend::Ion, DebugEnabled::False);
Expand All @@ -103,6 +106,7 @@ bool wasm::CompileIntrinsicModule(JSContext* cx,
!moduleEnv.imports.append(Import(std::move(emptyString),
std::move(memoryString),
DefinitionKind::Memory))) {
ReportOutOfMemory(cx);
return false;
}
moduleEnv.memory = Some(MemoryDesc(Limits(0, Nothing(), sharedMemory)));
Expand All @@ -116,6 +120,7 @@ bool wasm::CompileIntrinsicModule(JSContext* cx,
if (!intrinsic.funcType(&type) ||
!moduleEnv.types.append(TypeDef(std::move(type))) ||
!moduleEnv.typeIds.append(TypeIdDesc())) {
ReportOutOfMemory(cx);
return false;
}
}
Expand All @@ -127,6 +132,7 @@ bool wasm::CompileIntrinsicModule(JSContext* cx,
FuncDesc decl(&moduleEnv.types[funcIndex].funcType(),
&moduleEnv.typeIds[funcIndex], funcIndex);
if (!moduleEnv.funcs.append(decl)) {
ReportOutOfMemory(cx);
return false;
}
moduleEnv.declareFuncExported(funcIndex, true, false);
Expand All @@ -140,6 +146,7 @@ bool wasm::CompileIntrinsicModule(JSContext* cx,
if (!exportString ||
!moduleEnv.exports.append(Export(std::move(exportString), funcIndex,
DefinitionKind::Function))) {
ReportOutOfMemory(cx);
return false;
}
}
Expand All @@ -148,12 +155,14 @@ bool wasm::CompileIntrinsicModule(JSContext* cx,
UniqueChars error;
ModuleGenerator mg(*compileArgs, &moduleEnv, &compilerEnv, nullptr, &error);
if (!mg.init(nullptr)) {
ReportOutOfMemory(cx);
return false;
}

// Prepare and compile function bodies
Vector<Bytes, 1, SystemAllocPolicy> bodies;
if (!bodies.reserve(ops.size())) {
ReportOutOfMemory(cx);
return false;
}
for (uint32_t funcIndex = 0; funcIndex < ops.size(); funcIndex++) {
Expand All @@ -172,6 +181,7 @@ bool wasm::CompileIntrinsicModule(JSContext* cx,
bytecode.begin() + bytecode.length())) {
// This must be an OOM and will be reported by the caller
MOZ_ASSERT(!error);
ReportOutOfMemory(cx);
return false;
}
}
Expand All @@ -180,20 +190,23 @@ bool wasm::CompileIntrinsicModule(JSContext* cx,
if (!mg.finishFuncDefs()) {
// This must be an OOM and will be reported by the caller
MOZ_ASSERT(!error);
ReportOutOfMemory(cx);
return false;
}

// Finish the module
SharedBytes bytecode = js_new<ShareableBytes>();
SharedModule module = mg.finishModule(*bytecode, nullptr);
if (!module) {
ReportOutOfMemory(cx);
return false;
}

// Create a WasmModuleObject for the module, and return it
RootedObject proto(
cx, GlobalObject::getOrCreatePrototype(cx, JSProto_WasmModule));
if (!proto) {
ReportOutOfMemory(cx);
return false;
}
result.set(WasmModuleObject::create(cx, *module, proto));
Expand Down

0 comments on commit 1475b3b

Please sign in to comment.