| 1 |
// Test that wasm atomic operations implement correct mutual exclusion. |
| 2 |
// |
| 3 |
// We have several agents that attempt to hammer on a shared location with rmw |
| 4 |
// operations in such a way that failed atomicity will lead to an incorrect |
| 5 |
// result. Each agent attempts to clear or set specific bits in a shared datum. |
| 6 |
|
| 7 |
// 1 for a little bit, 2 for a lot, 3 to quit before running tests |
| 8 |
const DEBUG = 0; |
| 9 |
|
| 10 |
// The longer we run, the better, really, but we don't want to time out. |
| 11 |
const ITERATIONS = 100000; |
| 12 |
|
| 13 |
// If you change NUMWORKERS you must also change the tables for INIT, VAL, and |
| 14 |
// RESULT for all the operations, below, by adding or removing bits. |
| 15 |
const NUMWORKERS = 2; |
| 16 |
const NUMAGENTS = NUMWORKERS + 1; |
| 17 |
|
| 18 |
// Need at least one thread per agent. |
| 19 |
|
| 20 |
if (!wasmThreadsEnabled() || helperThreadCount() < NUMWORKERS) { |
| 21 |
if (DEBUG > 0) |
| 22 |
print("Threads not supported"); |
| 23 |
quit(0); |
| 24 |
} |
| 25 |
|
| 26 |
// Unless there are enough actual cores the spinning threads will not interact |
| 27 |
// in the desired way (we'll be waiting on preemption to advance), and this |
| 28 |
// makes the test pointless and also will usually make it time out. So bail out |
| 29 |
// if we can't have one core per agent. |
| 30 |
|
| 31 |
if (getCoreCount() < NUMAGENTS) { |
| 32 |
if (DEBUG > 0) |
| 33 |
print("Fake or feeble hardware"); |
| 34 |
quit(0); |
| 35 |
} |
| 36 |
|
| 37 |
// Most of the simulators have poor support for mutual exclusion and are anyway |
| 38 |
// too slow; avoid intermittent failures and timeouts. |
| 39 |
|
| 40 |
if (getBuildConfiguration("arm-simulator") || getBuildConfiguration("arm64-simulator") || |
| 41 |
getBuildConfiguration("mips64-simulator")) |
| 42 |
{ |
| 43 |
if (DEBUG > 0) |
| 44 |
print("Atomicity test disabled on simulator"); |
| 45 |
quit(0); |
| 46 |
} |
| 47 |
|
| 48 |
//////////////////////////////////////////////////////////////////////// |
| 49 |
// |
| 50 |
// Coordination code for bootstrapping workers - use spawn() to create a worker, |
| 51 |
// send() to send an item to a worker. send() will send to anyone, so only one |
| 52 |
// worker should be receiving at a time. spawn() will block until the worker is |
| 53 |
// running; send() will block until the message has been received. |
| 54 |
|
| 55 |
var COORD_BUSY = 0; |
| 56 |
var COORD_NUMLOC = 1; |
| 57 |
|
| 58 |
var coord = new Int32Array(new SharedArrayBuffer(COORD_NUMLOC*4)); |
| 59 |
|
| 60 |
function spawn(text) { |
| 61 |
text = ` |
| 62 |
var _coord = new Int32Array(getSharedObject()); |
| 63 |
Atomics.store(_coord, ${COORD_BUSY}, 0); |
| 64 |
function receive() { |
| 65 |
while (!Atomics.load(_coord, ${COORD_BUSY})) |
| 66 |
; |
| 67 |
let x = getSharedObject(); |
| 68 |
Atomics.store(_coord, ${COORD_BUSY}, 0); |
| 69 |
return x; |
| 70 |
} |
| 71 |
` + text; |
| 72 |
setSharedObject(coord.buffer); |
| 73 |
Atomics.store(coord, COORD_BUSY, 1); |
| 74 |
evalInWorker(text); |
| 75 |
while (Atomics.load(coord, COORD_BUSY)) |
| 76 |
; |
| 77 |
} |
| 78 |
|
| 79 |
function send(x) { |
| 80 |
while(Atomics.load(coord, COORD_BUSY)) |
| 81 |
; |
| 82 |
setSharedObject(x); |
| 83 |
Atomics.store(coord, COORD_BUSY, 1); |
| 84 |
while(Atomics.load(coord, COORD_BUSY)) |
| 85 |
; |
| 86 |
} |
| 87 |
|
| 88 |
///////////////////////////////////////////////////////////////////////////////// |
| 89 |
// |
| 90 |
// The "agents" comprise one master and one or more additional workers. We make |
| 91 |
// a separate module for each agent so that test values can be inlined as |
| 92 |
// constants. |
| 93 |
// |
| 94 |
// The master initially sets a shared location LOC to a value START. |
| 95 |
// |
| 96 |
// Each agent then operates atomically on LOC with an operation OP and a value |
| 97 |
// VAL. The operation OP is the same for all agents but each agent `i` has a |
| 98 |
// different VAL_i. |
| 99 |
// |
| 100 |
// To make this more interesting, the value START is distributed as many times |
| 101 |
// through the value at LOC as there is space for, and we perform several |
| 102 |
// operations back-to-back, with the VAL_i appropriately shifted. |
| 103 |
// |
| 104 |
// Each agent then spin-waits for LOC to contain a particular RESULT, which is |
| 105 |
// always (START OP VAL_0 OP VAL_1 ... VAL_k), again repeated throughout the |
| 106 |
// RESULT as appropriate. |
| 107 |
// |
| 108 |
// The process then starts over, and we repeat the process many times. If we |
| 109 |
// fail to have atomicity at any point the program will hang (LOC will never |
| 110 |
// attain the desired value) and the test should therefore time out. |
| 111 |
// |
| 112 |
// (Barriers are needed to make this all work out.) |
| 113 |
// |
| 114 |
// The general principle for the values is that each VAL should add (or clear) a |
| 115 |
// bit of the stored value. |
| 116 |
// |
| 117 |
// OP START VAL0 VAL1 VAL2 RESULT |
| 118 |
// |
| 119 |
// ADD[*] 0 1 2 4 7 |
| 120 |
// SUB 7 1 2 4 0 |
| 121 |
// AND 7 3 6 5 0 |
| 122 |
// OR 0 1 2 4 7 |
| 123 |
// XOR 0 1 2 4 7 // or start with 7 and end with 0 |
| 124 |
// CMPXCHG 0 1 2 4 7 // use nonatomic "or" to add the bit |
| 125 |
// |
| 126 |
// [*] Running the tests actually assumes that ADD works reasonably well. |
| 127 |
// |
| 128 |
// TODO - more variants we could test: |
| 129 |
// |
| 130 |
// - tests that do not drop the values of the atomic ops but accumulate them: |
| 131 |
// uses different code generation on x86/x64 |
| 132 |
// |
| 133 |
// - Xchg needs a different method, since here the atomic thing is that we read |
| 134 |
// the "previous value" and set the next value atomically. How can we observe |
| 135 |
// that that fails? If we run three agents, which all set the value to X, |
| 136 |
// X+1, ..., X+n, with the initial value being (say) X-1, each can record the |
| 137 |
// value it observed in a table, and we should be able to predict the counts |
| 138 |
// in that table once postprocessed. eg, the counts should all be the same. |
| 139 |
// If atomicity fails then a value is read twice when it shouldn't be, and |
| 140 |
// some other value is not read at all, and the counts will be off. |
| 141 |
// |
| 142 |
// - the different rmw operations can usually be combined so that we can test |
| 143 |
// the atomicity of operations that may be implemented differently. |
| 144 |
// |
| 145 |
// - the same tests, with test values as variables instead of constants. |
| 146 |
|
| 147 |
function makeModule(id) { |
| 148 |
let isMaster = id == 0; |
| 149 |
let VALSHIFT = NUMAGENTS; // 1 bit per agent |
| 150 |
|
| 151 |
function makeLoop(bits, name, op, loc, initial, val, expected) { |
| 152 |
// Exclude high bit to avoid messing with the sign. |
| 153 |
let NUMVALS32 = Math.floor(31/VALSHIFT); |
| 154 |
let NUMVALS = bits == 64 ? 2 * NUMVALS32 : Math.floor(Math.min(bits,31)/VALSHIFT); |
| 155 |
let BARRIER = "(i32.const 0)"; |
| 156 |
let barrier = ` |
| 157 |
;; Barrier |
| 158 |
(local.set $barrierValue (i32.add (local.get $barrierValue) (i32.const ${NUMAGENTS}))) |
| 159 |
(drop (i32.atomic.rmw.add ${BARRIER} (i32.const 1))) |
| 160 |
(loop $c1 |
| 161 |
(if (i32.lt_s (i32.atomic.load ${BARRIER}) (local.get $barrierValue)) |
| 162 |
(then (br $c1)))) |
| 163 |
;; End barrier |
| 164 |
`; |
| 165 |
|
| 166 |
// Distribute a value `v` across a word NUMVALs times |
| 167 |
|
| 168 |
function distribute(v) { |
| 169 |
if (bits <= 32) |
| 170 |
return '0x' + dist32(v); |
| 171 |
return '0x' + dist32(v) + dist32(v); |
| 172 |
} |
| 173 |
|
| 174 |
function dist32(v) { |
| 175 |
let n = 0; |
| 176 |
for (let i=0; i < Math.min(NUMVALS, NUMVALS32); i++) |
| 177 |
n = n | (v << (i*VALSHIFT)); |
| 178 |
assertEq(n >= 0, true); |
| 179 |
return (n + 0x100000000).toString(16).substring(1); |
| 180 |
} |
| 181 |
|
| 182 |
// Position a value `v` at position `pos` in a word |
| 183 |
|
| 184 |
function format(val, pos) { |
| 185 |
if (bits <= 32) |
| 186 |
return '0x' + format32(val, pos); |
| 187 |
if (pos < NUMVALS32) |
| 188 |
return '0x' + '00000000' + format32(val, pos); |
| 189 |
return '0x' + format32(val, pos - NUMVALS32) + '00000000'; |
| 190 |
} |
| 191 |
|
| 192 |
function format32(val, pos) { |
| 193 |
return ((val << (pos * VALSHIFT)) + 0x100000000).toString(16).substring(1); |
| 194 |
} |
| 195 |
|
| 196 |
let width = bits < 32 ? '' + bits : ''; |
| 197 |
let view = bits < 32 ? '_u' : ''; |
| 198 |
let prefix = bits == 64 ? 'i64' : 'i32'; |
| 199 |
return ` |
| 200 |
(func ${name} (param $barrierValue i32) (result i32) |
| 201 |
(local $n i32) |
| 202 |
(local $tmp ${prefix}) |
| 203 |
(local.set $n (i32.const ${ITERATIONS})) |
| 204 |
(loop $outer |
| 205 |
(if (local.get $n) |
| 206 |
(then (block |
| 207 |
${isMaster ? `;; Init |
| 208 |
(${prefix}.atomic.store${width} ${loc} (${prefix}.const ${distribute(initial)}))` : ``} |
| 209 |
${barrier} |
| 210 |
|
| 211 |
${(() => { |
| 212 |
let s = `;; Do\n`; |
| 213 |
for (let i=0; i < NUMVALS; i++) { |
| 214 |
let bitval = `(${prefix}.const ${format(val, i)})` |
| 215 |
// The load must be atomic though it would be better if it were relaxed, |
| 216 |
// we would avoid fences in that case. |
| 217 |
if (op.match(/cmpxchg/)) { |
| 218 |
s += `(loop $doit |
| 219 |
(local.set $tmp (${prefix}.atomic.load${width}${view} ${loc})) |
| 220 |
(br_if $doit (i32.eqz |
| 221 |
(${prefix}.eq |
| 222 |
(local.get $tmp) |
| 223 |
(${op} ${loc} (local.get $tmp) (${prefix}.or (local.get $tmp) ${bitval})))))) |
| 224 |
`; |
| 225 |
} else { |
| 226 |
s += `(drop (${op} ${loc} ${bitval})) |
| 227 |
`; |
| 228 |
} |
| 229 |
} |
| 230 |
return s |
| 231 |
})()} |
| 232 |
(loop $wait_done |
| 233 |
(br_if $wait_done (${prefix}.ne (${prefix}.atomic.load${width}${view} ${loc}) (${prefix}.const ${distribute(expected)})))) |
| 234 |
${barrier} |
| 235 |
(local.set $n (i32.sub (local.get $n) (i32.const 1))) |
| 236 |
(br $outer))))) |
| 237 |
(local.get $barrierValue))`; |
| 238 |
} |
| 239 |
|
| 240 |
const ADDLOC = "(i32.const 256)"; |
| 241 |
const ADDINIT = 0; |
| 242 |
const ADDVAL = [1, 2, 4]; |
| 243 |
const ADDRESULT = 7; |
| 244 |
|
| 245 |
const SUBLOC = "(i32.const 512)"; |
| 246 |
const SUBINIT = 7; |
| 247 |
const SUBVAL = [1, 2, 4]; |
| 248 |
const SUBRESULT = 0; |
| 249 |
|
| 250 |
const ANDLOC = "(i32.const 768)"; |
| 251 |
const ANDINIT = 7; |
| 252 |
const ANDVAL = [3, 6, 5]; |
| 253 |
const ANDRESULT = 0; |
| 254 |
|
| 255 |
const ORLOC = "(i32.const 1024)"; |
| 256 |
const ORINIT = 0; |
| 257 |
const ORVAL = [1, 2, 4]; |
| 258 |
const ORRESULT = 7; |
| 259 |
|
| 260 |
const XORLOC = "(i32.const 1280)"; |
| 261 |
const XORINIT = 0; |
| 262 |
const XORVAL = [1, 2, 4]; |
| 263 |
const XORRESULT = 7; |
| 264 |
|
| 265 |
const CMPXCHGLOC = "(i32.const 1536)"; |
| 266 |
const CMPXCHGINIT = 0; |
| 267 |
const CMPXCHGVAL = [1, 2, 4]; |
| 268 |
const CMPXCHGRESULT = 7; |
| 269 |
|
| 270 |
return ` |
| 271 |
(module |
| 272 |
(import "" "memory" (memory 1 1 shared)) |
| 273 |
(import "" "print" (func $print (param i32))) |
| 274 |
|
| 275 |
${makeLoop(8, "$test_add8", "i32.atomic.rmw8.add_u", ADDLOC, ADDINIT, ADDVAL[id], ADDRESULT)} |
| 276 |
${makeLoop(8, "$test_sub8", "i32.atomic.rmw8.sub_u", SUBLOC, SUBINIT, SUBVAL[id], SUBRESULT)} |
| 277 |
${makeLoop(8, "$test_and8", "i32.atomic.rmw8.and_u", ANDLOC, ANDINIT, ANDVAL[id], ANDRESULT)} |
| 278 |
${makeLoop(8, "$test_or8", "i32.atomic.rmw8.or_u", ORLOC, ORINIT, ORVAL[id], ORRESULT)} |
| 279 |
${makeLoop(8, "$test_xor8", "i32.atomic.rmw8.xor_u", XORLOC, XORINIT, XORVAL[id], XORRESULT)} |
| 280 |
${makeLoop(8, "$test_cmpxchg8", "i32.atomic.rmw8.cmpxchg_u", CMPXCHGLOC, CMPXCHGINIT, CMPXCHGVAL[id], CMPXCHGRESULT)} |
| 281 |
|
| 282 |
${makeLoop(16, "$test_add16", "i32.atomic.rmw16.add_u", ADDLOC, ADDINIT, ADDVAL[id], ADDRESULT)} |
| 283 |
${makeLoop(16, "$test_sub16", "i32.atomic.rmw16.sub_u", SUBLOC, SUBINIT, SUBVAL[id], SUBRESULT)} |
| 284 |
${makeLoop(16, "$test_and16", "i32.atomic.rmw16.and_u", ANDLOC, ANDINIT, ANDVAL[id], ANDRESULT)} |
| 285 |
${makeLoop(16, "$test_or16", "i32.atomic.rmw16.or_u", ORLOC, ORINIT, ORVAL[id], ORRESULT)} |
| 286 |
${makeLoop(16, "$test_xor16", "i32.atomic.rmw16.xor_u", XORLOC, XORINIT, XORVAL[id], XORRESULT)} |
| 287 |
${makeLoop(16, "$test_cmpxchg16", "i32.atomic.rmw16.cmpxchg_u", CMPXCHGLOC, CMPXCHGINIT, CMPXCHGVAL[id], CMPXCHGRESULT)} |
| 288 |
|
| 289 |
${makeLoop(32, "$test_add", "i32.atomic.rmw.add", ADDLOC, ADDINIT, ADDVAL[id], ADDRESULT)} |
| 290 |
${makeLoop(32, "$test_sub", "i32.atomic.rmw.sub", SUBLOC, SUBINIT, SUBVAL[id], SUBRESULT)} |
| 291 |
${makeLoop(32, "$test_and", "i32.atomic.rmw.and", ANDLOC, ANDINIT, ANDVAL[id], ANDRESULT)} |
| 292 |
${makeLoop(32, "$test_or", "i32.atomic.rmw.or", ORLOC, ORINIT, ORVAL[id], ORRESULT)} |
| 293 |
${makeLoop(32, "$test_xor", "i32.atomic.rmw.xor", XORLOC, XORINIT, XORVAL[id], XORRESULT)} |
| 294 |
${makeLoop(32, "$test_cmpxchg", "i32.atomic.rmw.cmpxchg", CMPXCHGLOC, CMPXCHGINIT, CMPXCHGVAL[id], CMPXCHGRESULT)} |
| 295 |
|
| 296 |
${makeLoop(64, "$test_add64", "i64.atomic.rmw.add", ADDLOC, ADDINIT, ADDVAL[id], ADDRESULT)} |
| 297 |
${makeLoop(64, "$test_sub64", "i64.atomic.rmw.sub", SUBLOC, SUBINIT, SUBVAL[id], SUBRESULT)} |
| 298 |
${makeLoop(64, "$test_and64", "i64.atomic.rmw.and", ANDLOC, ANDINIT, ANDVAL[id], ANDRESULT)} |
| 299 |
${makeLoop(64, "$test_or64", "i64.atomic.rmw.or", ORLOC, ORINIT, ORVAL[id], ORRESULT)} |
| 300 |
${makeLoop(64, "$test_xor64", "i64.atomic.rmw.xor", XORLOC, XORINIT, XORVAL[id], XORRESULT)} |
| 301 |
${makeLoop(64, "$test_cmpxchg64", "i64.atomic.rmw.cmpxchg", CMPXCHGLOC, CMPXCHGINIT, CMPXCHGVAL[id], CMPXCHGRESULT)} |
| 302 |
|
| 303 |
(func (export "test") |
| 304 |
(local $barrierValue i32) |
| 305 |
(call $print (i32.const ${10 + id})) |
| 306 |
(local.set $barrierValue (call $test_add8 (local.get $barrierValue))) |
| 307 |
(local.set $barrierValue (call $test_sub8 (local.get $barrierValue))) |
| 308 |
(local.set $barrierValue (call $test_and8 (local.get $barrierValue))) |
| 309 |
(local.set $barrierValue (call $test_or8 (local.get $barrierValue))) |
| 310 |
(local.set $barrierValue (call $test_xor8 (local.get $barrierValue))) |
| 311 |
(local.set $barrierValue (call $test_cmpxchg8 (local.get $barrierValue))) |
| 312 |
(call $print (i32.const ${20 + id})) |
| 313 |
(local.set $barrierValue (call $test_add16 (local.get $barrierValue))) |
| 314 |
(local.set $barrierValue (call $test_sub16 (local.get $barrierValue))) |
| 315 |
(local.set $barrierValue (call $test_and16 (local.get $barrierValue))) |
| 316 |
(local.set $barrierValue (call $test_or16 (local.get $barrierValue))) |
| 317 |
(local.set $barrierValue (call $test_xor16 (local.get $barrierValue))) |
| 318 |
(local.set $barrierValue (call $test_cmpxchg16 (local.get $barrierValue))) |
| 319 |
(call $print (i32.const ${30 + id})) |
| 320 |
(local.set $barrierValue (call $test_add (local.get $barrierValue))) |
| 321 |
(local.set $barrierValue (call $test_sub (local.get $barrierValue))) |
| 322 |
(local.set $barrierValue (call $test_and (local.get $barrierValue))) |
| 323 |
(local.set $barrierValue (call $test_or (local.get $barrierValue))) |
| 324 |
(local.set $barrierValue (call $test_xor (local.get $barrierValue))) |
| 325 |
(local.set $barrierValue (call $test_cmpxchg (local.get $barrierValue))) |
| 326 |
(call $print (i32.const ${40 + id})) |
| 327 |
(local.set $barrierValue (call $test_add64 (local.get $barrierValue))) |
| 328 |
(local.set $barrierValue (call $test_sub64 (local.get $barrierValue))) |
| 329 |
(local.set $barrierValue (call $test_and64 (local.get $barrierValue))) |
| 330 |
(local.set $barrierValue (call $test_or64 (local.get $barrierValue))) |
| 331 |
(local.set $barrierValue (call $test_xor64 (local.get $barrierValue))) |
| 332 |
(local.set $barrierValue (call $test_cmpxchg64 (local.get $barrierValue))) |
| 333 |
)) |
| 334 |
`; |
| 335 |
} |
| 336 |
|
| 337 |
function makeModule2(id) { |
| 338 |
let text = makeModule(id); |
| 339 |
if (DEBUG > 1) |
| 340 |
print(text); |
| 341 |
return new WebAssembly.Module(wasmTextToBinary(text)); |
| 342 |
} |
| 343 |
|
| 344 |
var mods = []; |
| 345 |
mods.push(makeModule2(0)); |
| 346 |
for ( let i=0; i < NUMWORKERS; i++ ) |
| 347 |
mods.push(makeModule2(i+1)); |
| 348 |
if (DEBUG > 2) |
| 349 |
quit(0); |
| 350 |
var mem = new WebAssembly.Memory({initial: 1, maximum: 1, shared: true}); |
| 351 |
|
| 352 |
//////////////////////////////////////////////////////////////////////// |
| 353 |
// |
| 354 |
// Worker code |
| 355 |
|
| 356 |
function startWorkers() { |
| 357 |
for ( let i=0; i < NUMWORKERS; i++ ) { |
| 358 |
spawn(` |
| 359 |
var mem = receive(); |
| 360 |
var mod = receive(); |
| 361 |
function pr(n) { if (${DEBUG}) print(n); } |
| 362 |
var ins = new WebAssembly.Instance(mod, {"":{memory: mem, print:pr}}); |
| 363 |
if (${DEBUG} > 0) |
| 364 |
print("Running ${i}"); |
| 365 |
ins.exports.test(); |
| 366 |
`); |
| 367 |
send(mem); |
| 368 |
send(mods[i+1]); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
//////////////////////////////////////////////////////////////////////// |
| 373 |
// |
| 374 |
// Main thread code |
| 375 |
|
| 376 |
startWorkers(); |
| 377 |
function pr(n) { if (DEBUG) print(n); } |
| 378 |
var ins = new WebAssembly.Instance(mods[0], {"":{memory: mem, print:pr}}); |
| 379 |
if (DEBUG > 0) |
| 380 |
print("Running master"); |
| 381 |
ins.exports.test(); |