v8/test/mjsunit/wasm/shared-memory.js
Deepti Gandluri 0202a040c9 [wasm] Module bytes can set shared attribute on memory
- Validate that atomic ops can only be called when shared memory is declared
- Throw Compile/Link erros on mismatch between declared, imported memory
- Test harness helpers for setting shared memory, tests

BUG=v8:6532

R=binji@chromium.org, bradnelson@chromium.org

Change-Id: I43fe3d04bb7e3e0a2cecca0528578f98844d2608
Reviewed-on: https://chromium-review.googlesource.com/665379
Commit-Queue: Brad Nelson <bradnelson@chromium.org>
Reviewed-by: Brad Nelson <bradnelson@chromium.org>
Cr-Commit-Position: refs/heads/master@{#48019}
2017-09-14 18:16:31 +00:00

104 lines
3.7 KiB
JavaScript

// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --experimental-wasm-threads
load("test/mjsunit/wasm/wasm-constants.js");
load("test/mjsunit/wasm/wasm-module-builder.js");
function assertMemoryIsValid(memory) {
assertSame(WebAssembly.Memory.prototype, memory.__proto__);
assertSame(WebAssembly.Memory, memory.constructor);
assertTrue(memory instanceof Object);
assertTrue(memory instanceof WebAssembly.Memory);
}
(function TestConstructorWithShared() {
print("TestConstructorWithShared");
let memory = new WebAssembly.Memory({
initial: 0, maximum: 10, shared: true});
assertMemoryIsValid(memory);
// Assert that the buffer is frozen when memory is shared.
assertTrue(Object.isFrozen(memory.buffer));
})();
(function TestConstructorWithUndefinedShared() {
print("TestConstructorWithUndefinedShared");
// Maximum = undefined, shared = undefined.
let memory = new WebAssembly.Memory({
initial: 0, maximum: undefined, shared: undefined});
assertMemoryIsValid(memory);
})();
(function TestConstructorWithNumericShared() {
print("TestConstructorWithNumericShared");
// For numeric values, shared = true.
let memory = new WebAssembly.Memory({
initial: 0, maximum: 10, shared: 2098665});
assertMemoryIsValid(memory);
})();
(function TestConstructorWithEmptyStringShared() {
print("TestConstructorWithEmptyStringShared");
// Maximum = undefined, shared = false.
let memory = new WebAssembly.Memory({
initial: 0, maximum: undefined, shared: ""});
assertMemoryIsValid(memory);
})();
(function TestConstructorWithUndefinedMaxShared() {
print("TestConstructorWithUndefinedMaxShared");
// New memory with Maximum = undefined, shared = true => TypeError.
assertThrows(() => new WebAssembly.Memory({initial: 0, shared: true}),
TypeError);
})();
(function TestCompileWithUndefinedShared() {
print("TestCompileWithUndefinedShared");
let memory = new WebAssembly.Memory({
initial: 0, maximum: 10, shared: true});
let builder = new WasmModuleBuilder();
builder.addImportedMemory("m", "imported_mem", 0, undefined, "shared");
assertThrows(() => new WebAssembly.Module(builder.toBuffer()),
WebAssembly.CompileError);
})();
(function TestCompileAtomicOpUndefinedShared() {
print("TestCompileAtomicOpUndefinedShared");
let memory = new WebAssembly.Memory({
initial: 0, maximum: 10, shared: true});
let builder = new WasmModuleBuilder();
builder.addFunction("main", kSig_i_ii)
.addBody([
kExprGetLocal, 0,
kExprGetLocal, 1,
kAtomicPrefix,
kExprI32AtomicAdd]);
builder.addImportedMemory("m", "imported_mem");
assertThrows(() => new WebAssembly.Module(builder.toBuffer()),
WebAssembly.CompileError);
})();
(function TestInstantiateWithUndefinedShared() {
print("TestInstantiateWithUndefinedShared");
let memory = new WebAssembly.Memory({
initial: 0, maximum: 10, shared: true});
let builder = new WasmModuleBuilder();
builder.addImportedMemory("m", "imported_mem");
let module = new WebAssembly.Module(builder.toBuffer());
assertThrows(() => new WebAssembly.Instance(module,
{m: {imported_mem: memory}}), WebAssembly.LinkError);
})();
(function TestInstantiateWithImportNotSharedDefined() {
print("TestInstantiateWithImportNotSharedDefined");
let memory = new WebAssembly.Memory({
initial: 0, maximum: 10, shared: false});
let builder = new WasmModuleBuilder();
builder.addImportedMemory("m", "imported_mem", 0, 10, "shared");
let module = new WebAssembly.Module(builder.toBuffer());
assertThrows(() => new WebAssembly.Instance(module,
{m: {imported_mem: memory}}), WebAssembly.LinkError);
})();