futex.js 12.6 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2018 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: --allow-natives-syntax --harmony-sharedarraybuffer
// Flags: --experimental-wasm-threads

'use strict';

10
d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
11

12
function WasmAtomicNotify(memory, offset, index, num) {
13 14 15 16
  let builder = new WasmModuleBuilder();
  builder.addImportedMemory("m", "memory", 0, 20, "shared");
  builder.addFunction("main", kSig_i_ii)
    .addBody([
17 18
      kExprLocalGet, 0,
      kExprLocalGet, 1,
19
      kAtomicPrefix,
20
      kExprAtomicNotify, /* alignment */ 0, offset])
21 22 23 24 25 26 27 28
    .exportAs("main");

  // Instantiate module, get function exports
  let module = new WebAssembly.Module(builder.toBuffer());
  let instance = new WebAssembly.Instance(module, {m: {memory}});
  return instance.exports.main(index, num);
}

29
function WasmI32AtomicWait(memory, offset, index, val, timeout) {
30 31 32 33 34
  let builder = new WasmModuleBuilder();
  builder.addImportedMemory("m", "memory", 0, 20, "shared");
  builder.addFunction("main",
    makeSig([kWasmI32, kWasmI32, kWasmF64], [kWasmI32]))
    .addBody([
35 36 37
      kExprLocalGet, 0,
      kExprLocalGet, 1,
      kExprLocalGet, 2,
38 39 40 41 42 43 44 45 46 47 48
      kExprI64SConvertF64,
      kAtomicPrefix,
      kExprI32AtomicWait, /* alignment */ 0, offset])
      .exportAs("main");

  // Instantiate module, get function exports
  let module = new WebAssembly.Module(builder.toBuffer());
  let instance = new WebAssembly.Instance(module, {m: {memory}});
  return instance.exports.main(index, val, timeout);
}

49 50 51 52 53 54 55 56
function WasmI64AtomicWait(memory, offset, index, val_low,
                                   val_high, timeout) {
  let builder = new WasmModuleBuilder();
  builder.addImportedMemory("m", "memory", 0, 20, "shared");
  // Wrapper for I64AtomicWait that takes two I32 values and combines to into
  // I64 for the instruction parameter.
  builder.addFunction("main",
    makeSig([kWasmI32, kWasmI32, kWasmI32, kWasmF64], [kWasmI32]))
57
    .addLocals(kWasmI64, 1) // local that is passed as value param to wait
58
    .addBody([
59
      kExprLocalGet, 1,
60 61 62
      kExprI64UConvertI32,
      kExprI64Const, 32,
      kExprI64Shl,
63
      kExprLocalGet, 2,
64 65
      kExprI64UConvertI32,
      kExprI64Ior,
66 67 68 69
      kExprLocalSet, 4, // Store the created I64 value in local
      kExprLocalGet, 0,
      kExprLocalGet, 4,
      kExprLocalGet, 3,
70 71 72 73 74 75 76 77 78 79 80
      kExprI64SConvertF64,
      kAtomicPrefix,
      kExprI64AtomicWait, /* alignment */ 0, offset])
      .exportAs("main");

  // Instantiate module, get function exports
  let module = new WebAssembly.Module(builder.toBuffer());
  let instance = new WebAssembly.Instance(module, {m: {memory}});
  return instance.exports.main(index, val_high, val_low, timeout);
}

81
(function TestInvalidIndex() {
82
  if (!%IsAtomicsWaitAllowed()) return;
83 84 85 86 87
  let memory = new WebAssembly.Memory({initial: 1, maximum: 1, shared: true});

  // Valid indexes are 0-65535 (1 page).
  [-2, 65536, 0xffffffff].forEach(function(invalidIndex) {
    assertThrows(function() {
88
      WasmAtomicNotify(memory, 0, invalidIndex, -1);
89 90 91 92 93 94
    }, Error);
    assertThrows(function() {
      WasmI32AtomicWait(memory, 0, invalidIndex, 0, -1);
    }, Error);
    assertThrows(function() {
      WasmI64AtomicWait(memory, 0, invalidIndex, 0, 0, -1);
95
    }, Error);
96
    assertThrows(function() {
97
      WasmAtomicNotify(memory, invalidIndex, 0, -1);
98
    }, Error);
99
    assertThrows(function() {
100
      WasmI32AtomicWait(memory, invalidIndex, 0, 0, -1);
101
    }, Error);
102
    assertThrows(function() {
103
      WasmI64AtomicWait(memory, invalidIndex, 0, 0, 0, -1);
104
    }, Error);
105
    assertThrows(function() {
106
      WasmAtomicNotify(memory, invalidIndex/2, invalidIndex/2, -1);
107
    }, Error);
108
    assertThrows(function() {
109 110 111 112
      WasmI32AtomicWait(memory, invalidIndex/2, invalidIndex/2, 0, -1);
    }, Error);
    assertThrows(function() {
      WasmI64AtomicWait(memory, invalidIndex/2, invalidIndex/2, 0, 0, -1);
113
    }, Error);
114 115 116
  });
})();

117
(function TestInvalidAlignment() {
118
  if (!%IsAtomicsWaitAllowed()) return;
119 120 121 122 123
  let memory = new WebAssembly.Memory({initial: 1, maximum: 1, shared: true});

  // Wait and wake must be 4 byte aligned.
  [1, 2, 3].forEach(function(invalid) {
    assertThrows(function() {
124
      WasmAtomicNotify(memory, invalid, 0, -1)
125 126
    }, Error);
    assertThrows(function() {
127
      WasmAtomicNotify(memory, 0, invalid, -1)
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    }, Error);
    assertThrows(function() {
      WasmI32AtomicWait(memory, invalid, 0, 0, -1)
    }, Error);
    assertThrows(function() {
      WasmI32AtomicWait(memory, 0, invalid, 0, -1)
    }, Error);
    assertThrows(function() {
      WasmI64AtomicWait(memory, invalid, 0, 0, 0, -1)
    }, Error);
    assertThrows(function() {
      WasmI64AtomicWait(memory, 0, invalid, 0, 0, -1)
    }, Error);
  });

  //WasmI64AtomicWait must be 8 byte aligned.
  [4, 5, 6, 7].forEach(function(invalid) {
    assertThrows(function() {
      WasmI64AtomicWait(memory, 0, invalid, 0, 0, -1)
    }, Error);
    assertThrows(function() {
      WasmI64AtomicWait(memory, invalid, 0, 0, 0, -1)
    }, Error);
  });
})();

154
(function TestI32WaitTimeout() {
155
  if (!%IsAtomicsWaitAllowed()) return;
156 157 158 159 160 161 162 163 164
  let memory = new WebAssembly.Memory({initial: 1, maximum: 1, shared: true});
  var waitMs = 100;
  var startTime = new Date();
  assertEquals(2, WasmI32AtomicWait(memory, 0, 0, 0, waitMs*1000000));
  var endTime = new Date();
  assertTrue(endTime - startTime >= waitMs);
})();

(function TestI64WaitTimeout() {
165
  if (!%IsAtomicsWaitAllowed()) return;
166 167 168
  let memory = new WebAssembly.Memory({initial: 1, maximum: 1, shared: true});
  var waitMs = 100;
  var startTime = new Date();
169
  assertEquals(2, WasmI64AtomicWait(memory, 0, 0, 0, 0, waitMs*1000000));
170 171 172 173
  var endTime = new Date();
  assertTrue(endTime - startTime >= waitMs);
})();

174
(function TestI32WaitNotEqual() {
175
  if (!%IsAtomicsWaitAllowed()) return;
176
  let memory = new WebAssembly.Memory({initial: 1, maximum: 1, shared: true});
177
  assertEquals(1, WasmI32AtomicWait(memory, 0, 0, 42, -1));
178

179
  assertEquals(2, WasmI32AtomicWait(memory, 0, 0, 0, 0));
180 181 182

  let i32a = new Int32Array(memory.buffer);
  i32a[0] = 1;
183 184 185 186 187
  assertEquals(1, WasmI32AtomicWait(memory, 0, 0, 0, -1));
  assertEquals(2, WasmI32AtomicWait(memory, 0, 0, 1, 0));
})();

(function TestI64WaitNotEqual() {
188
  if (!%IsAtomicsWaitAllowed()) return;
189 190 191 192 193 194 195 196 197 198
  let memory = new WebAssembly.Memory({initial: 1, maximum: 1, shared: true});
  assertEquals(1, WasmI64AtomicWait(memory, 0, 0, 42, 0, -1));

  assertEquals(2, WasmI64AtomicWait(memory, 0, 0, 0, 0, 0));

  let i32a = new Int32Array(memory.buffer);
  i32a[0] = 1;
  i32a[1] = 2;
  assertEquals(1, WasmI64AtomicWait(memory, 0, 0, 0, 0, -1));
  assertEquals(2, WasmI64AtomicWait(memory, 0, 0, 1, 2, 0));
199 200
})();

201 202 203 204
(function TestWakeCounts() {
  let memory = new WebAssembly.Memory({initial: 1, maximum: 1, shared: true});

  [-1, 0, 4, 100, 0xffffffff].forEach(function(count) {
205
    WasmAtomicNotify(memory, 0, 0, count);
206 207 208 209 210 211 212
  });
})();

//// WORKER ONLY TESTS

if (this.Worker) {

213 214 215 216 217 218 219 220 221 222
  // This test creates 4 workers that wait on consecutive (8 byte separated to
  // satisfy alignments for all kinds of wait) memory locations to test various
  // wait/wake combinations. For each combination, each thread waits 3 times
  // expecting all 4 threads to be woken with wake(4) in first iteration, all 4
  // to be woken with wake(5) in second iteration and, 3 and 1 to be woken in
  // third iteration.

  let memory = new WebAssembly.Memory({initial: 1, maximum: 1, shared: true});
  let i32a = new Int32Array(memory.buffer);
  const numWorkers = 4;
223

224
  let workerScript = `onmessage = function(msg) {
225
    d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
226 227 228 229
    ${WasmI32AtomicWait.toString()}
    ${WasmI64AtomicWait.toString()}
    let id = msg.id;
    let memory = msg.memory;
230
    let i32a = new Int32Array(memory.buffer);
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    // indices are right shifted by 2 for Atomics.wait to convert them to index
    // for Int32Array
    // for wasm-wake numWorkers threads
    let result = Atomics.wait(i32a, 0>>>2, 0);
    postMessage(result);
    // for wasm-wake numWorkers + 1 threads
    result = Atomics.wait(i32a, 8>>>2, 0);
    postMessage(result);
    // for wasm-wake numWorkers - 1 threads
    result = Atomics.wait(i32a, 16>>>2, 0);
    postMessage(result);
    // for js-wake numWorkers threads
    result = WasmI32AtomicWait(memory, 0, 24, 0, -1);
    postMessage(result);
    // for js-wake numWorkers + 1 threads
    result = WasmI32AtomicWait(memory, 0, 32, 0, -1);
    postMessage(result);
    // for js-wake numWorkers - 1 threads
    result = WasmI32AtomicWait(memory, 0, 40, 0, -1);
    postMessage(result);
    // for wasm-wake numWorkers threads
    result = WasmI32AtomicWait(memory, 0, 48, 0, -1);
    postMessage(result);
    // for wasm-wake numWorkers + 1 threads
    result = WasmI32AtomicWait(memory, 0, 56, 0, -1);
    postMessage(result);
    // for wasm-wake numWorkers - 1 threads
    result = WasmI32AtomicWait(memory, 0, 64, 0, -1);
    postMessage(result);
    // for js-wake numWorkers threads
    result = WasmI64AtomicWait(memory, 0, 72, 0, 0, -1);
    postMessage(result);
    // for js-wake numWorkers + 1 threads
    result = WasmI64AtomicWait(memory, 0, 80, 0, 0, -1);
    postMessage(result);
    // for js-wake numWorkers - 1 threads
    result = WasmI64AtomicWait(memory, 0, 88, 0, 0, -1);
    postMessage(result);
    // for wasm-wake numWorkers threads
    result = WasmI64AtomicWait(memory, 0, 96, 0, 0, -1);
    postMessage(result);
    // for wasm-wake numWorkers + 1 threads
    result = WasmI64AtomicWait(memory, 0, 104, 0, 0, -1);
    postMessage(result);
    // for wasm-wake numWorkers - 1 threads
    result = WasmI64AtomicWait(memory, 0, 112, 0, 0, -1);
    postMessage(result);
  };`;
279

280 281 282 283
  let waitForAllWorkers = function(index) {
    // index is right shifted by 2 to convert to index in Int32Array
    while (%AtomicsNumWaitersForTesting(i32a, index>>>2) != numWorkers) {}
  }
284

285 286 287 288 289 290
  let jsWakeCheck = function(index, num, workers, msg) {
    waitForAllWorkers(index);
    let indexJs = index>>>2; // convert to index in Int32Array
    if (num >= numWorkers) {
      // if numWorkers or more is passed to wake, numWorkers workers should be
      // woken.
291
      assertEquals(numWorkers, Atomics.notify(i32a, indexJs, num));
292
    } else {
293 294
      // if num < numWorkers is passed to wake, num workers should be woken.
      // Then the remaining workers are woken for the next part
295 296
      assertEquals(num, Atomics.notify(i32a, indexJs, num));
      assertEquals(numWorkers-num, Atomics.notify(i32a, indexJs, numWorkers));
297
    }
298 299
    for (let id = 0; id < numWorkers; id++) {
      assertEquals(msg, workers[id].getMessage());
300
    }
301
  };
302

303 304 305 306 307
  let wasmWakeCheck = function(index, num, workers, msg) {
    waitForAllWorkers(index);
    if (num >= numWorkers) {
      // if numWorkers or more is passed to wake, numWorkers workers should be
      // woken.
308
      assertEquals(numWorkers, WasmAtomicNotify(memory, 0, index, num));
309 310 311
    } else {
      // if num < numWorkers is passed to wake, num workers should be woken.
      // Then the remaining workers are woken for the next part
312
      assertEquals(num, WasmAtomicNotify(memory, 0, index, num));
313
      assertEquals(numWorkers-num,
314
                   WasmAtomicNotify(memory, 0, index, numWorkers));
315 316 317
    }
    for (let id = 0; id < numWorkers; id++) {
      assertEquals(msg, workers[id].getMessage());
318 319 320
    }
  };

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
  let workers = [];
  for (let id = 0; id < numWorkers; id++) {
    workers[id] = new Worker(workerScript, {type: 'string'});
    workers[id].postMessage({id, memory});
  }

  wasmWakeCheck(0, numWorkers, workers, "ok");
  wasmWakeCheck(8, numWorkers + 1, workers, "ok");
  wasmWakeCheck(16, numWorkers - 1, workers, "ok");

  jsWakeCheck(24, numWorkers, workers, 0);
  jsWakeCheck(32, numWorkers + 1, workers, 0);
  jsWakeCheck(40, numWorkers - 1, workers, 0);

  wasmWakeCheck(48, numWorkers, workers, 0);
  wasmWakeCheck(56, numWorkers + 1, workers, 0);
  wasmWakeCheck(64, numWorkers - 1, workers, 0);

  jsWakeCheck(72, numWorkers, workers, 0);
  jsWakeCheck(80, numWorkers + 1, workers, 0);
  jsWakeCheck(88, numWorkers - 1, workers, 0);
342

343 344 345
  wasmWakeCheck(96, numWorkers, workers, 0);
  wasmWakeCheck(104, numWorkers + 1, workers, 0);
  wasmWakeCheck(112, numWorkers - 1, workers, 0);
346

347 348 349
  for (let id = 0; id < numWorkers; id++) {
    workers[id].terminate();
  }
350
}
351 352 353 354 355 356 357 358 359 360

(function TestWaitTrapsOnDisallowedIsolate() {
  let memory = new WebAssembly.Memory({initial: 1, maximum: 1, shared: true});
  var waitMs = 100;
  %SetAllowAtomicsWait(false)
  assertThrows(function() {
    WasmI32AtomicWait(memory, 0, 0, 0, waitMs*1000000)}, WebAssembly.RuntimeError);
  assertThrows(function() {
    WasmI64AtomicWait(memory, 0, 0, 0, waitMs*1000000)}, WebAssembly.RuntimeError);
})();