Commit 17e01bc7 authored by Deepti Gandluri's avatar Deepti Gandluri Committed by Commit Bot

[wasm] Allow atomic operations on non-shared WebAssembly.memory

Currently atomic operations are only allowed on shared WebAssembly.memory.
An attempt to use atomic operations otherwise is a validation failure, there
is an ongoing attempt to allow Wasm atomic operations on any memory object.

https://github.com/WebAssembly/threads/issues/144

This CL adds experimental support for allowing atomic operations on all
memory objects behind the --wasm-atomics-on-non-shared-memory flag. Note
that Wait/Notify may not work as expected as they have additional checks
to ensure that the memory is a SAB.

Bug: v8:9921
Change-Id: Ia65b1a4a96ec026430fcce028465423f600adacd
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1895703
Commit-Queue: Deepti Gandluri <gdeepti@chromium.org>
Reviewed-by: 's avatarAdam Klein <adamk@chromium.org>
Reviewed-by: 's avatarBill Budge <bbudge@chromium.org>
Cr-Commit-Position: refs/heads/master@{#64716}
parent 7258185a
......@@ -750,6 +750,8 @@ DEFINE_DEBUG_BOOL(trace_wasm_lazy_compilation, false,
"trace lazy compilation of wasm functions")
DEFINE_BOOL(wasm_grow_shared_memory, true,
"allow growing shared WebAssembly memory objects")
DEFINE_BOOL(wasm_atomics_on_non_shared_memory, false,
"allow atomic operations on non-shared WebAssembly memory")
DEFINE_BOOL(wasm_lazy_validation, false,
"enable lazy validation for lazily compiled wasm functions")
// wasm-interpret-all resets {asm-,}wasm-lazy-compilation.
......
......@@ -1695,7 +1695,8 @@ class WasmFullDecoder : public WasmDecoder<validate> {
return true;
}
bool CheckHasSharedMemory() {
bool CheckHasMemoryForAtomics() {
if (FLAG_wasm_atomics_on_non_shared_memory && CheckHasMemory()) return true;
if (!VALIDATE(this->module_->has_shared_memory)) {
this->error(this->pc_ - 1, "Atomic opcodes used without shared memory");
return false;
......@@ -2791,7 +2792,7 @@ class WasmFullDecoder : public WasmDecoder<validate> {
this->error("invalid atomic opcode");
return 0;
}
if (!CheckHasSharedMemory()) return 0;
if (!CheckHasMemoryForAtomics()) return 0;
MemoryAccessImmediate<validate> imm(
this, this->pc_ + 1, ElementSizeLog2Of(memtype.representation()));
len += imm.length;
......
// Copyright 2019 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 --wasm-atomics-on-non-shared-memory
load("test/mjsunit/wasm/wasm-module-builder.js");
// TODO(gdeepti): If non-shared atomics are moving forward, ensure that
// the tests here are more comprehensive -i.e. reuse atomics.js/atomics64.js
// and cctests to run on both shared/non-shared memory.
(function TestCompileGenericAtomicOp() {
print(arguments.callee.name);
let memory = new WebAssembly.Memory({initial: 0, maximum: 10});
let builder = new WasmModuleBuilder();
builder.addFunction("main", kSig_i_ii)
.addBody([
kExprLocalGet, 0,
kExprLocalGet, 1,
kAtomicPrefix,
kExprI32AtomicAdd, 2, 0]);
builder.addImportedMemory("m", "imported_mem");
let module = new WebAssembly.Module(builder.toBuffer());
})();
(function TestCompileWasmAtomicNotify() {
print(arguments.callee.name);
let memory = new WebAssembly.Memory({initial: 0, maximum: 10});
let builder = new WasmModuleBuilder();
builder.addImportedMemory("m", "memory", 0, 20);
builder.addFunction("main", kSig_i_ii)
.addBody([
kExprLocalGet, 0,
kExprLocalGet, 1,
kAtomicPrefix,
kExprAtomicNotify, 0, 0])
.exportAs("main");
let module = new WebAssembly.Module(builder.toBuffer());
let instance = new WebAssembly.Instance(module, {m: {memory}});
})();
(function TestCompileWasmI32AtomicWait() {
print(arguments.callee.name);
let memory = new WebAssembly.Memory({initial: 0, maximum: 10});
let builder = new WasmModuleBuilder();
builder.addImportedMemory("m", "memory", 0, 20);
builder.addFunction("main",
makeSig([kWasmI32, kWasmI32, kWasmF64], [kWasmI32]))
.addBody([
kExprLocalGet, 0,
kExprLocalGet, 1,
kExprLocalGet, 2,
kExprI64SConvertF64,
kAtomicPrefix,
kExprI32AtomicWait, 0, 0])
.exportAs("main");
let module = new WebAssembly.Module(builder.toBuffer());
let instance = new WebAssembly.Instance(module, {m: {memory}});
})();
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