Skip to content

Commit 895c52d

Browse files
dtigCommit Bot
authored and
Commit Bot
committed
[wasm] Adjust atomics wait/notify semantics
Adjust atomics.wait, atomics.notify semantics for when they are used with non-shared Wasm memory to mirror the spec change introduced in: WebAssembly/threads#147. This does not need to be gated by the flag here, as this will only decode if the flag is enabled. Bug: v8:9921 Change-Id: I7f2e018fed6bd131ad4c386def1e838626c28a4d Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2256863 Reviewed-by: Ben Smith <[email protected]> Commit-Queue: Deepti Gandluri <[email protected]> Cr-Commit-Position: refs/heads/master@{#68468}
1 parent 7c429d9 commit 895c52d

File tree

2 files changed

+84
-9
lines changed

2 files changed

+84
-9
lines changed

src/runtime/runtime-wasm.cc

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,12 @@ RUNTIME_FUNCTION(Runtime_WasmCompileLazy) {
209209
}
210210

211211
// Should be called from within a handle scope
212-
Handle<JSArrayBuffer> GetSharedArrayBuffer(Handle<WasmInstanceObject> instance,
213-
Isolate* isolate, uint32_t address) {
212+
Handle<JSArrayBuffer> GetArrayBuffer(Handle<WasmInstanceObject> instance,
213+
Isolate* isolate, uint32_t address) {
214214
DCHECK(instance->has_memory_object());
215215
Handle<JSArrayBuffer> array_buffer(instance->memory_object().array_buffer(),
216216
isolate);
217217

218-
// Validation should have failed if the memory was not shared.
219-
DCHECK(array_buffer->is_shared());
220-
221218
// Should have trapped if address was OOB
222219
DCHECK_LT(address, array_buffer->byte_length());
223220
return array_buffer;
@@ -231,8 +228,12 @@ RUNTIME_FUNCTION(Runtime_WasmAtomicNotify) {
231228
CONVERT_NUMBER_CHECKED(uint32_t, address, Uint32, args[1]);
232229
CONVERT_NUMBER_CHECKED(uint32_t, count, Uint32, args[2]);
233230
Handle<JSArrayBuffer> array_buffer =
234-
GetSharedArrayBuffer(instance, isolate, address);
235-
return FutexEmulation::Wake(array_buffer, address, count);
231+
GetArrayBuffer(instance, isolate, address);
232+
if (array_buffer->is_shared()) {
233+
return FutexEmulation::Wake(array_buffer, address, count);
234+
} else {
235+
return Smi::FromInt(0);
236+
}
236237
}
237238

238239
RUNTIME_FUNCTION(Runtime_WasmI32AtomicWait) {
@@ -245,7 +246,12 @@ RUNTIME_FUNCTION(Runtime_WasmI32AtomicWait) {
245246
CONVERT_ARG_HANDLE_CHECKED(BigInt, timeout_ns, 3);
246247

247248
Handle<JSArrayBuffer> array_buffer =
248-
GetSharedArrayBuffer(instance, isolate, address);
249+
GetArrayBuffer(instance, isolate, address);
250+
251+
// Trap if memory is not shared
252+
if (!array_buffer->is_shared()) {
253+
return ThrowWasmError(isolate, MessageTemplate::kAtomicsWaitNotAllowed);
254+
}
249255
return FutexEmulation::WaitWasm32(isolate, array_buffer, address,
250256
expected_value, timeout_ns->AsInt64());
251257
}
@@ -260,7 +266,12 @@ RUNTIME_FUNCTION(Runtime_WasmI64AtomicWait) {
260266
CONVERT_ARG_HANDLE_CHECKED(BigInt, timeout_ns, 3);
261267

262268
Handle<JSArrayBuffer> array_buffer =
263-
GetSharedArrayBuffer(instance, isolate, address);
269+
GetArrayBuffer(instance, isolate, address);
270+
271+
// Trap if memory is not shared
272+
if (!array_buffer->is_shared()) {
273+
return ThrowWasmError(isolate, MessageTemplate::kAtomicsWaitNotAllowed);
274+
}
264275
return FutexEmulation::WaitWasm64(isolate, array_buffer, address,
265276
expected_value->AsInt64(),
266277
timeout_ns->AsInt64());

test/mjsunit/wasm/atomics-non-shared.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,67 @@ load("test/mjsunit/wasm/wasm-module-builder.js");
5858
let module = new WebAssembly.Module(builder.toBuffer());
5959
let instance = new WebAssembly.Instance(module, {m: {memory}});
6060
})();
61+
62+
(function TestWasmAtomicNotifyResult() {
63+
print(arguments.callee.name);
64+
let builder = new WasmModuleBuilder();
65+
builder.addImportedMemory("m", "memory", 0, 20);
66+
builder.addFunction("main", kSig_i_ii)
67+
.addBody([
68+
kExprLocalGet, 0,
69+
kExprLocalGet, 1,
70+
kAtomicPrefix,
71+
kExprAtomicNotify, 0, 0])
72+
.exportAs("main");
73+
74+
// Instantiate module, get function exports
75+
let module = new WebAssembly.Module(builder.toBuffer());
76+
let memory = new WebAssembly.Memory({initial: 1, maximum: 1});
77+
let instance = new WebAssembly.Instance(module, {m: {memory}});
78+
assertEquals(0, instance.exports.main(0, 100));
79+
})();
80+
81+
(function TestWasmI32AtomicWaitTraps() {
82+
print(arguments.callee.name);
83+
let builder = new WasmModuleBuilder();
84+
builder.addImportedMemory("m", "memory", 0, 20);
85+
builder.addFunction("main",
86+
makeSig([kWasmI32, kWasmI32, kWasmF64], [kWasmI32]))
87+
.addBody([
88+
kExprLocalGet, 0,
89+
kExprLocalGet, 1,
90+
kExprLocalGet, 2,
91+
kExprI64SConvertF64,
92+
kAtomicPrefix,
93+
kExprI32AtomicWait, 0, 0])
94+
.exportAs("main");
95+
96+
// Instantiate module, get function exports
97+
let module = new WebAssembly.Module(builder.toBuffer());
98+
let memory = new WebAssembly.Memory({initial: 1, maximum: 1});
99+
let instance = new WebAssembly.Instance(module, {m: {memory}});
100+
assertThrows(() => instance.exports.main(0, 5, 0), WebAssembly.RuntimeError);
101+
})();
102+
103+
(function TestWasmI64AtomicWaitTraps() {
104+
print(arguments.callee.name);
105+
let builder = new WasmModuleBuilder();
106+
builder.addImportedMemory("m", "memory", 0, 20);
107+
builder.addFunction("main",
108+
makeSig([kWasmI32, kWasmI32, kWasmF64], [kWasmI32]))
109+
.addBody([
110+
kExprLocalGet, 0,
111+
kExprLocalGet, 1,
112+
kExprI64UConvertI32,
113+
kExprLocalGet, 2,
114+
kExprI64SConvertF64,
115+
kAtomicPrefix,
116+
kExprI64AtomicWait, 0, 0])
117+
.exportAs("main");
118+
119+
// Instantiate module, get function exports
120+
let module = new WebAssembly.Module(builder.toBuffer());
121+
let memory = new WebAssembly.Memory({initial: 1, maximum: 1});
122+
let instance = new WebAssembly.Instance(module, {m: {memory}});
123+
assertThrows(() => instance.exports.main(0, 5, 0), WebAssembly.RuntimeError);
124+
})();

0 commit comments

Comments
 (0)