Commit f7601238 authored by Deepti Gandluri's avatar Deepti Gandluri Committed by Commit Bot

[wasm] Allocate SAB when memory is shared

When shared memory is defined in the module bytes, and not imported/exported
underlying memory should be a SharedArrayBuffer. This was missing in the
allocate flow during instantiation. Fixed to use a SharedArrayBuffer.

BUG=v8:6532

Change-Id: Ic62ed3fd578a0e03124ee40b273e6a4ea474bba4
Reviewed-on: https://chromium-review.googlesource.com/835348Reviewed-by: 's avatarEric Holk <eholk@chromium.org>
Reviewed-by: 's avatarBen Smith <binji@chromium.org>
Commit-Queue: Deepti Gandluri <gdeepti@chromium.org>
Cr-Commit-Position: refs/heads/master@{#50255}
parent a10324c2
......@@ -3175,8 +3175,11 @@ Handle<JSArrayBuffer> InstanceBuilder::AllocateMemory(uint32_t num_pages) {
return Handle<JSArrayBuffer>::null();
}
const bool enable_guard_regions = use_trap_handler();
const bool is_shared_memory =
module_->has_shared_memory && i::FLAG_experimental_wasm_threads;
Handle<JSArrayBuffer> mem_buffer = NewArrayBuffer(
isolate_, num_pages * WasmModule::kPageSize, enable_guard_regions);
isolate_, num_pages * WasmModule::kPageSize, enable_guard_regions,
is_shared_memory ? i::SharedFlag::kShared : i::SharedFlag::kNotShared);
if (mem_buffer.is_null()) {
thrower_->RangeError("Out of memory: wasm memory");
......
......@@ -7,20 +7,23 @@
load("test/mjsunit/wasm/wasm-constants.js");
load("test/mjsunit/wasm/wasm-module-builder.js");
function assertMemoryIsValid(memory) {
function assertMemoryIsValid(memory, shared) {
assertSame(WebAssembly.Memory.prototype, memory.__proto__);
assertSame(WebAssembly.Memory, memory.constructor);
assertTrue(memory instanceof Object);
assertTrue(memory instanceof WebAssembly.Memory);
if (shared) {
assertTrue(memory.buffer instanceof SharedArrayBuffer);
// Assert that the buffer is frozen when memory is shared.
assertTrue(Object.isFrozen(memory.buffer));
}
}
(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));
assertMemoryIsValid(memory, true);
})();
(function TestConstructorWithUndefinedShared() {
......@@ -36,7 +39,7 @@ function assertMemoryIsValid(memory) {
// For numeric values, shared = true.
let memory = new WebAssembly.Memory({
initial: 0, maximum: 10, shared: 2098665});
assertMemoryIsValid(memory);
assertMemoryIsValid(memory, true);
})();
(function TestConstructorWithEmptyStringShared() {
......@@ -101,3 +104,29 @@ function assertMemoryIsValid(memory) {
assertThrows(() => new WebAssembly.Instance(module,
{m: {imported_mem: memory}}), WebAssembly.LinkError);
})();
(function TestInstantiateWithSharedDefined() {
print("TestInstantiateWithSharedDefined");
let builder = new WasmModuleBuilder();
builder.addMemory(2, 10, true, "shared");
let module = new WebAssembly.Module(builder.toBuffer());
let instance = new WebAssembly.Instance(module);
assertMemoryIsValid(instance.exports.memory, true);
})();
(function TestAtomicOpWithSharedMemoryDefined() {
print("TestAtomicOpWithSharedMemoryDefined");
let builder = new WasmModuleBuilder();
builder.addMemory(2, 10, false, "shared");
builder.addFunction("main", kSig_i_ii)
.addBody([
kExprGetLocal, 0,
kExprGetLocal, 1,
kAtomicPrefix,
kExprI32AtomicAdd, 2, 0])
.exportFunc();
let module = new WebAssembly.Module(builder.toBuffer());
let instance = new WebAssembly.Instance(module);
assertEquals(0, instance.exports.main(0, 0x11111111));
assertEquals(0x11111111, instance.exports.main(0, 0x11111111));
})();
......@@ -409,7 +409,6 @@ class WasmModuleBuilder {
}
section.emit_u32v(wasm.memory.min);
if (has_max) section.emit_u32v(wasm.memory.max);
if (wasm.memory.shared) section.emit_u8(1);
});
}
......
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