401190baf0
When memory.grow was executed concurrently on multiple threads a data race could happen such that two memory.grow operations result in the same return value. With this CL the return value of memory.grow is unique, given that memory.grow actually grows the memory. As a concrete example, assume a shared WebAssembly memory initially has a size of 100. Assume two threads call memory.grow concurrently with a parameter `10`. Then with the existing code, memory would grow correctly to a size of 120, but the data race may cause both memory.grow operations to return 100. With the change in this CL one memory.grow operation would return 100, the other would return 110. R=gdeepti@chromium.org CC=rreverser@google.com Bug: chromium:1067621 Change-Id: Ib22b5135714a56799e0818ccb39e5dce327e5f8e Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2144113 Reviewed-by: Ben Smith <binji@chromium.org> Reviewed-by: Deepti Gandluri <gdeepti@chromium.org> Commit-Queue: Andreas Haas <ahaas@chromium.org> Cr-Commit-Position: refs/heads/master@{#67135}
83 lines
2.3 KiB
JavaScript
83 lines
2.3 KiB
JavaScript
// Copyright 2020 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-module-builder.js');
|
|
|
|
const kNumberOfWorker = 4;
|
|
|
|
const workerOnMessage = function(msg) {
|
|
if (msg.module) {
|
|
let module = msg.module;
|
|
let mem = msg.mem;
|
|
this.instance = new WebAssembly.Instance(module, {m: {memory: mem}});
|
|
postMessage({instantiated: true});
|
|
} else {
|
|
const kNumberOfRuns = 20;
|
|
let result = new Array(kNumberOfRuns);
|
|
for (let i = 0; i < kNumberOfRuns; ++i) {
|
|
result[i] = instance.exports.grow();
|
|
}
|
|
postMessage({result: result});
|
|
}
|
|
};
|
|
|
|
function spawnWorkers() {
|
|
let workers = [];
|
|
for (let i = 0; i < kNumberOfWorker; i++) {
|
|
let worker = new Worker(
|
|
'onmessage = ' + workerOnMessage.toString(), {type: 'string'});
|
|
workers.push(worker);
|
|
}
|
|
return workers;
|
|
}
|
|
|
|
function instantiateModuleInWorkers(workers, module, shared_memory) {
|
|
for (let worker of workers) {
|
|
worker.postMessage({module: module, mem: shared_memory});
|
|
let msg = worker.getMessage();
|
|
if (!msg.instantiated) throw 'Worker failed to instantiate';
|
|
}
|
|
}
|
|
|
|
function triggerWorkers(workers) {
|
|
for (i = 0; i < workers.length; i++) {
|
|
let worker = workers[i];
|
|
worker.postMessage({});
|
|
}
|
|
}
|
|
|
|
(function TestConcurrentGrowMemoryResult() {
|
|
let builder = new WasmModuleBuilder();
|
|
builder.addImportedMemory('m', 'memory', 1, 500, 'shared');
|
|
builder.addFunction('grow', kSig_i_v)
|
|
.addBody([kExprI32Const, 1, kExprMemoryGrow, kMemoryZero])
|
|
.exportFunc();
|
|
|
|
const module = builder.toModule();
|
|
const shared_memory =
|
|
new WebAssembly.Memory({initial: 1, maximum: 500, shared: true});
|
|
|
|
// Spawn off the workers and run the sequences.
|
|
let workers = spawnWorkers();
|
|
instantiateModuleInWorkers(workers, module, shared_memory);
|
|
triggerWorkers(workers);
|
|
let all_results = [];
|
|
for (let worker of workers) {
|
|
let msg = worker.getMessage();
|
|
all_results = all_results.concat(msg.result);
|
|
}
|
|
|
|
all_results.sort((a, b) => a - b);
|
|
for (let i = 1; i < all_results.length; ++i) {
|
|
assertEquals(all_results[i - 1] + 1, all_results[i]);
|
|
}
|
|
|
|
// Terminate all workers.
|
|
for (let worker of workers) {
|
|
worker.terminate();
|
|
}
|
|
})();
|