From 53137df89a859fca258f9c448dec5a4eba4e47c7 Mon Sep 17 00:00:00 2001 From: mtrofin Date: Thu, 12 Jan 2017 08:48:09 -0800 Subject: [PATCH] [wasm] JS APIs & memory - updated WebAssembly.Instance ctor uses in our tests to match spec - disallowing mem section *and* mem import, as per spec BUG=v8:5824 Review-Url: https://codereview.chromium.org/2627763002 Cr-Commit-Position: refs/heads/master@{#42278} --- src/wasm/module-decoder.cc | 16 +++++++---- .../wasm/compiled-module-serialization.js | 9 ++++-- test/mjsunit/wasm/import-memory.js | 1 - test/mjsunit/wasm/instantiate-module-basic.js | 28 +++++++++++-------- test/mjsunit/wasm/js-api.js | 8 ++++++ test/mjsunit/wasm/module-memory.js | 9 +++--- test/mjsunit/wasm/wasm-module-builder.js | 4 +-- 7 files changed, 48 insertions(+), 27 deletions(-) diff --git a/src/wasm/module-decoder.cc b/src/wasm/module-decoder.cc index 8f92c6e34c..d7c7954de2 100644 --- a/src/wasm/module-decoder.cc +++ b/src/wasm/module-decoder.cc @@ -324,11 +324,7 @@ class ModuleDecoder : public Decoder { &module->min_mem_pages, &has_max, kSpecMaxWasmMemoryPages, &module->max_mem_pages); - if (module->has_memory) { - error("At most one memory object is supported"); - } else { - module->has_memory = true; - } + SetHasMemory(module); break; } case kExternalGlobal: { @@ -403,7 +399,7 @@ class ModuleDecoder : public Decoder { "memory", "pages", kV8MaxWasmMemoryPages, &module->min_mem_pages, &has_max, kSpecMaxWasmMemoryPages, &module->max_mem_pages); } - module->has_memory = true; + SetHasMemory(module); section_iter.advance(); } @@ -684,6 +680,14 @@ class ModuleDecoder : public Decoder { uint32_t off(const byte* ptr) { return static_cast(ptr - start_); } + void SetHasMemory(WasmModule* module) { + if (module->has_memory) { + error("At most one memory object is supported"); + } else { + module->has_memory = true; + } + } + // Decodes a single global entry inside a module starting at {pc_}. void DecodeGlobalInModule(WasmModule* module, uint32_t index, WasmGlobal* global) { diff --git a/test/mjsunit/wasm/compiled-module-serialization.js b/test/mjsunit/wasm/compiled-module-serialization.js index 89611653fb..e5cc4632ce 100644 --- a/test/mjsunit/wasm/compiled-module-serialization.js +++ b/test/mjsunit/wasm/compiled-module-serialization.js @@ -9,7 +9,7 @@ load("test/mjsunit/wasm/wasm-module-builder.js"); (function SerializeAndDeserializeModule() { var builder = new WasmModuleBuilder(); - builder.addMemory(1,1, true); + builder.addImportedMemory("", "memory", 1,1); var kSig_v_i = makeSig([kWasmI32], []); var signature = builder.addType(kSig_v_i); builder.addImport("", "some_value", kSig_i_v); @@ -48,8 +48,11 @@ load("test/mjsunit/wasm/wasm-module-builder.js"); view_1[0] = 42; var outval_1; - var i1 = new WebAssembly.Instance(module, {"": {some_value: () => 1, - writer: (x)=>outval_1 = x }}, mem_1); + var i1 = new WebAssembly.Instance(module, {"": + {some_value: () => 1, + writer: (x) => outval_1 = x , + memory: mem_1} + }); assertEquals(43, i1.exports.main(0)); diff --git a/test/mjsunit/wasm/import-memory.js b/test/mjsunit/wasm/import-memory.js index 6c26ef4152..fd91dc0f72 100644 --- a/test/mjsunit/wasm/import-memory.js +++ b/test/mjsunit/wasm/import-memory.js @@ -58,7 +58,6 @@ load("test/mjsunit/wasm/wasm-module-builder.js"); var i2; { let builder = new WasmModuleBuilder(); - builder.addMemory(1, 1, false); builder.addImportedMemory("fil", "imported_mem"); builder.addFunction("bar", kSig_i_i) .addBody([ diff --git a/test/mjsunit/wasm/instantiate-module-basic.js b/test/mjsunit/wasm/instantiate-module-basic.js index 927851e868..db9917aa77 100644 --- a/test/mjsunit/wasm/instantiate-module-basic.js +++ b/test/mjsunit/wasm/instantiate-module-basic.js @@ -29,7 +29,7 @@ function CheckInstance(instance) { assertFalse(Object.isExtensible(instance.exports)); assertTrue(Object.isFrozen(instance.exports)); - // Check the memory is an ArrayBuffer. + // Check the memory is WebAssembly.Memory. var mem = instance.exports.memory; assertFalse(mem === undefined); assertFalse(mem === null); @@ -126,7 +126,7 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88))); (function InstancesAreIsolatedFromEachother() { print("InstancesAreIsolatedFromEachother..."); var builder = new WasmModuleBuilder(); - builder.addMemory(1,1, true); + builder.addImportedMemory("", "memory", 1,1); var kSig_v_i = makeSig([kWasmI32], []); var signature = builder.addType(kSig_v_i); builder.addImport("m", "some_value", kSig_i_v); @@ -165,11 +165,12 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88))); var outval_1; var outval_2; var i1 = new WebAssembly.Instance(module, {m: {some_value: () => 1, - writer: (x)=>outval_1 = x }}, - mem_1); + writer: (x)=>outval_1 = x }, + "": {memory: mem_1}}); + var i2 = new WebAssembly.Instance(module, {m: {some_value: () => 2, - writer: (x)=>outval_2 = x }}, - mem_2); + writer: (x)=>outval_2 = x }, + "": {memory: mem_2}}); assertEquals(43, i1.exports.main(0)); assertEquals(1002, i2.exports.main(0)); @@ -206,7 +207,7 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88))); (function InstanceMemoryIsIsolated() { print("InstanceMemoryIsIsolated..."); var builder = new WasmModuleBuilder(); - builder.addMemory(1,1, true); + builder.addImportedMemory("", "memory", 1,1); builder.addFunction("f", kSig_i_v) .addBody([ @@ -222,8 +223,8 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88))); view_2[0] = 1000; var module = new WebAssembly.Module(builder.toBuffer()); - var i1 = new WebAssembly.Instance(module, null, mem_1); - var i2 = new WebAssembly.Instance(module, null, mem_2); + var i1 = new WebAssembly.Instance(module, {"":{memory:mem_1}}); + var i2 = new WebAssembly.Instance(module, {"":{memory:mem_2}}); assertEquals(1, i1.exports.f()); assertEquals(1000, i2.exports.f()); @@ -232,6 +233,11 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88))); (function MustBeMemory() { print("MustBeMemory..."); var memory = new ArrayBuffer(65536); - var module = new WebAssembly.Module(buffer); - assertThrows(() => new WebAssembly.Instance(module, null, memory), TypeError); + let builder = new WasmModuleBuilder(); + builder.addImportedMemory("", "memory"); + + let module = new WebAssembly.Module(builder.toBuffer()); + + + assertThrows(() => new WebAssembly.Instance(module, {"":{memory:memory}}), WebAssembly.LinkError); })(); diff --git a/test/mjsunit/wasm/js-api.js b/test/mjsunit/wasm/js-api.js index 6844dbb9b0..fe299307f9 100644 --- a/test/mjsunit/wasm/js-api.js +++ b/test/mjsunit/wasm/js-api.js @@ -48,6 +48,13 @@ let moduleBinaryImporting2Memories = (() => { return new Int8Array(builder.toBuffer()); })(); +let moduleBinaryWithMemSectionAndMemImport = (() => { + var builder = new WasmModuleBuilder(); + builder.addMemory(1, 1, false); + builder.addImportedMemory("", "memory1"); + return new Int8Array(builder.toBuffer()); +})(); + // 'WebAssembly' data property on global object let wasmDesc = Object.getOwnPropertyDescriptor(this, 'WebAssembly'); assertEq(typeof wasmDesc.value, "object"); @@ -511,6 +518,7 @@ assertErrorMessage(() => WebAssembly.validate("hi"), TypeError); assertEq(WebAssembly.validate(emptyModuleBinary), true); // TODO: other ways for validate to return false. assertEq(WebAssembly.validate(moduleBinaryImporting2Memories), false); +assertEq(WebAssembly.validate(moduleBinaryWithMemSectionAndMemImport), false); // 'WebAssembly.compile' data property let compileDesc = Object.getOwnPropertyDescriptor(WebAssembly, 'compile'); diff --git a/test/mjsunit/wasm/module-memory.js b/test/mjsunit/wasm/module-memory.js index d7cc1297c9..ff11465fe8 100644 --- a/test/mjsunit/wasm/module-memory.js +++ b/test/mjsunit/wasm/module-memory.js @@ -12,7 +12,8 @@ var kMemSize = 65536; function genModule(memory) { var builder = new WasmModuleBuilder(); - builder.addMemory(1, 1, true); + builder.addImportedMemory("", "memory", 1, 1); + builder.exportMemoryAs("memory"); builder.addFunction("main", kSig_i_i) .addBody([ // main body: while(i) { if(mem[i]) return -1; i -= 4; } return 0; @@ -36,7 +37,7 @@ function genModule(memory) { /**/ kExprI32Const, 0 // -- ]) .exportFunc(); - var module = builder.instantiate(null, memory); + var module = builder.instantiate({"": {memory:memory}}); assertTrue(module.exports.memory instanceof WebAssembly.Memory); if (memory != null) assertEquals(memory.buffer, module.exports.memory.buffer); return module; @@ -44,7 +45,7 @@ function genModule(memory) { function testPokeMemory() { print("testPokeMemory"); - var module = genModule(null); + var module = genModule(new WebAssembly.Memory({initial: 1})); var buffer = module.exports.memory.buffer; var main = module.exports.main; assertEquals(kMemSize, buffer.byteLength); @@ -75,7 +76,7 @@ function genAndGetMain(buffer) { } function testSurvivalAcrossGc() { - var checker = genAndGetMain(null); + var checker = genAndGetMain(new WebAssembly.Memory({initial: 1})); for (var i = 0; i < 3; i++) { print("gc run ", i); assertEquals(0, checker(kMemSize - 4)); diff --git a/test/mjsunit/wasm/wasm-module-builder.js b/test/mjsunit/wasm/wasm-module-builder.js index 453e1ff90b..48ce48808c 100644 --- a/test/mjsunit/wasm/wasm-module-builder.js +++ b/test/mjsunit/wasm/wasm-module-builder.js @@ -547,9 +547,9 @@ class WasmModuleBuilder { return buffer; } - instantiate(...args) { + instantiate(ffi) { let module = new WebAssembly.Module(this.toBuffer()); - let instance = new WebAssembly.Instance(module, ...args); + let instance = new WebAssembly.Instance(module, ffi); return instance; } }