Commit fa268032 authored by Brad Nelson's avatar Brad Nelson Committed by Commit Bot

[wasm] Disallow calling wasm constructors without new.

BUG=chromium:786021
R=titzer@chromium.org

Change-Id: I188ea4d639ef9d5ceeab5052e043ec1c9150bd77
Reviewed-on: https://chromium-review.googlesource.com/778282Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Commit-Queue: Brad Nelson <bradnelson@chromium.org>
Cr-Commit-Position: refs/heads/master@{#49506}
parent d8981833
...@@ -192,6 +192,10 @@ void WebAssemblyModule(const v8::FunctionCallbackInfo<v8::Value>& args) { ...@@ -192,6 +192,10 @@ void WebAssemblyModule(const v8::FunctionCallbackInfo<v8::Value>& args) {
HandleScope scope(isolate); HandleScope scope(isolate);
i::wasm::ScheduledErrorThrower thrower(i_isolate, "WebAssembly.Module()"); i::wasm::ScheduledErrorThrower thrower(i_isolate, "WebAssembly.Module()");
if (!args.IsConstructCall()) {
thrower.TypeError("WebAssembly.Module must be invoked with 'new'");
return;
}
if (!i::wasm::IsWasmCodegenAllowed(i_isolate, i_isolate->native_context())) { if (!i::wasm::IsWasmCodegenAllowed(i_isolate, i_isolate->native_context())) {
thrower.CompileError("Wasm code generation disallowed by embedder"); thrower.CompileError("Wasm code generation disallowed by embedder");
return; return;
...@@ -352,6 +356,10 @@ void WebAssemblyInstance(const v8::FunctionCallbackInfo<v8::Value>& args) { ...@@ -352,6 +356,10 @@ void WebAssemblyInstance(const v8::FunctionCallbackInfo<v8::Value>& args) {
if (i_isolate->wasm_instance_callback()(args)) return; if (i_isolate->wasm_instance_callback()(args)) return;
i::wasm::ScheduledErrorThrower thrower(i_isolate, "WebAssembly.Instance()"); i::wasm::ScheduledErrorThrower thrower(i_isolate, "WebAssembly.Instance()");
if (!args.IsConstructCall()) {
thrower.TypeError("WebAssembly.Instance must be invoked with 'new'");
return;
}
GetFirstArgumentAsModule(args, &thrower); GetFirstArgumentAsModule(args, &thrower);
if (thrower.error()) return; if (thrower.error()) return;
...@@ -483,6 +491,10 @@ void WebAssemblyTable(const v8::FunctionCallbackInfo<v8::Value>& args) { ...@@ -483,6 +491,10 @@ void WebAssemblyTable(const v8::FunctionCallbackInfo<v8::Value>& args) {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
HandleScope scope(isolate); HandleScope scope(isolate);
i::wasm::ScheduledErrorThrower thrower(i_isolate, "WebAssembly.Module()"); i::wasm::ScheduledErrorThrower thrower(i_isolate, "WebAssembly.Module()");
if (!args.IsConstructCall()) {
thrower.TypeError("WebAssembly.Table must be invoked with 'new'");
return;
}
if (!args[0]->IsObject()) { if (!args[0]->IsObject()) {
thrower.TypeError("Argument 0 must be a table descriptor"); thrower.TypeError("Argument 0 must be a table descriptor");
return; return;
...@@ -536,6 +548,10 @@ void WebAssemblyMemory(const v8::FunctionCallbackInfo<v8::Value>& args) { ...@@ -536,6 +548,10 @@ void WebAssemblyMemory(const v8::FunctionCallbackInfo<v8::Value>& args) {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
HandleScope scope(isolate); HandleScope scope(isolate);
i::wasm::ScheduledErrorThrower thrower(i_isolate, "WebAssembly.Memory()"); i::wasm::ScheduledErrorThrower thrower(i_isolate, "WebAssembly.Memory()");
if (!args.IsConstructCall()) {
thrower.TypeError("WebAssembly.Memory must be invoked with 'new'");
return;
}
if (!args[0]->IsObject()) { if (!args[0]->IsObject()) {
thrower.TypeError("Argument 0 must be a memory descriptor"); thrower.TypeError("Argument 0 must be a memory descriptor");
return; return;
...@@ -623,8 +639,8 @@ void WebAssemblyInstanceGetExports( ...@@ -623,8 +639,8 @@ void WebAssemblyInstanceGetExports(
v8::Isolate* isolate = args.GetIsolate(); v8::Isolate* isolate = args.GetIsolate();
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
HandleScope scope(isolate); HandleScope scope(isolate);
i::wasm::ScheduledErrorThrower thrower(i_isolate, i::wasm::ScheduledErrorThrower thrower(i_isolate,
"WebAssembly.Instance.exports()"); "WebAssembly.Instance.exports()");
EXTRACT_THIS(receiver, WasmInstanceObject); EXTRACT_THIS(receiver, WasmInstanceObject);
i::Handle<i::JSObject> exports_object(receiver->exports_object()); i::Handle<i::JSObject> exports_object(receiver->exports_object());
args.GetReturnValue().Set(Utils::ToLocal(exports_object)); args.GetReturnValue().Set(Utils::ToLocal(exports_object));
......
...@@ -907,3 +907,21 @@ assertInstantiateSuccess( ...@@ -907,3 +907,21 @@ assertInstantiateSuccess(
class Y extends WebAssembly.Memory { } class Y extends WebAssembly.Memory { }
assertThrows(() => new Y()); assertThrows(() => new Y());
})(); })();
(function TestCallWithoutNew() {
var bytes = Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x1, 0x00, 0x00, 0x00);
assertThrows(() => WebAssembly.Module(bytes), TypeError);
assertThrows(() => WebAssembly.Instance(new WebAssembly.Module(bytes)),
TypeError);
assertThrows(() => WebAssembly.Table({size: 10, element: 'anyfunc'}),
TypeError);
assertThrows(() => WebAssembly.Memory({size: 10}), TypeError);
})();
(function TestTinyModule() {
var bytes = Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x1, 0x00, 0x00, 0x00);
var module = new WebAssembly.Module(bytes);
assertTrue(module instanceof Module);
var instance = new WebAssembly.Instance(module);
assertTrue(instance instanceof Instance);
})();
...@@ -64,7 +64,7 @@ let id = (() => { // identity exported function ...@@ -64,7 +64,7 @@ let id = (() => { // identity exported function
print("TableGrowBoundsCheck"); print("TableGrowBoundsCheck");
let builder = new WasmModuleBuilder(); let builder = new WasmModuleBuilder();
addMain(builder); addMain(builder);
let module = WebAssembly.Module(builder.toBuffer()); let module = new WebAssembly.Module(builder.toBuffer());
let table = new WebAssembly.Table({element: "anyfunc", let table = new WebAssembly.Table({element: "anyfunc",
initial: 1, maximum:kMaxTableSize}); initial: 1, maximum:kMaxTableSize});
function fillTable() { function fillTable() {
...@@ -95,7 +95,7 @@ let id = (() => { // identity exported function ...@@ -95,7 +95,7 @@ let id = (() => { // identity exported function
print("TableGrowBoundsZeroInitial"); print("TableGrowBoundsZeroInitial");
let builder = new WasmModuleBuilder(); let builder = new WasmModuleBuilder();
addMain(builder); addMain(builder);
let module = WebAssembly.Module(builder.toBuffer()); let module = new WebAssembly.Module(builder.toBuffer());
var table = new WebAssembly.Table({element: "anyfunc", var table = new WebAssembly.Table({element: "anyfunc",
initial: 0, maximum:kMaxTableSize}); initial: 0, maximum:kMaxTableSize});
function growTableByOne() { function growTableByOne() {
......
...@@ -10,7 +10,7 @@ load('test/mjsunit/wasm/wasm-module-builder.js'); ...@@ -10,7 +10,7 @@ load('test/mjsunit/wasm/wasm-module-builder.js');
var debug = true; var debug = true;
function instantiate(buffer, ffi) { function instantiate(buffer, ffi) {
return new WebAssembly.Instance(WebAssembly.Module(buffer), ffi); return new WebAssembly.Instance(new WebAssembly.Module(buffer), ffi);
} }
(function BasicTest() { (function BasicTest() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment