|
| 1 | +//@ requireOptions("--useWasmFastMemory=true", "--useConcurrentJIT=false", "--thresholdForBBQOptimizeAfterWarmUp=1", "--thresholdForOMGOptimizeAfterWarmUp=10", "--thresholdForOMGOptimizeSoon=10") |
| 2 | + |
| 3 | +import * as assert from "../assert.js"; |
| 4 | + |
| 5 | +// A cross-instance call_indirect out of an OMG function whose own memory is in Signaling mode must |
| 6 | +// hand the callee its own bounds checking size in wasmBoundsCheckingSizeRegister. That register is |
| 7 | +// not pinned in Signaling mode, so if the context switch that installs it is emitted as a separate |
| 8 | +// B3 operation from the call, Air is free to allocate the register to an unrelated Tmp in between |
| 9 | +// and the BoundsChecking callee ends up checking its accesses against that Tmp's value. |
| 10 | +// |
| 11 | +// The caller below is shaped to create the register pressure that makes the allocator do exactly |
| 12 | +// that: 24 i64 locals kept live across a loop nest and then passed three times each as call |
| 13 | +// arguments, so the poison value sits in a spilled Tmp whose fixup load lands between the context |
| 14 | +// switch and the call. |
| 15 | +// |
| 16 | +// Both modules import their memory so the two modes this needs can be forced and asserted: a |
| 17 | +// Signaling caller is what leaves the register unpinned, and a BoundsChecking callee is what |
| 18 | +// actually reads it. Getting either mode wrong makes the test pass without exercising the bug. |
| 19 | +// |
| 20 | +// The modules are assembled by hand rather than through wabt-wrapper.js because the exact shape |
| 21 | +// here was tuned against the register allocator; keeping it as explicit bytes keeps that shape |
| 22 | +// from shifting under a wat round trip. |
| 23 | + |
| 24 | +function leb(n) { let r = []; do { let b = n & 0x7f; n >>>= 7; if (n) b |= 0x80; r.push(b); } while (n); return r; } |
| 25 | +function section(id, body) { return [id, ...leb(body.length), ...body]; } |
| 26 | +function vec(items) { return [...leb(items.length), ...items.flat()]; } |
| 27 | +function str(s) { return [...leb(s.length), ...[...s].map(c => c.charCodeAt(0))]; } |
| 28 | + |
| 29 | +const N_I64 = 24; |
| 30 | +const REPEAT = 3; |
| 31 | +const LOOP_DEPTH = 6; |
| 32 | +const PAGE_SIZE = 0x10000; |
| 33 | + |
| 34 | +// memory import descriptor: "e"."mem", kind 0x02, limits {min: 1, max: 1} |
| 35 | +const memoryImport = [...str("e"), ...str("mem"), 0x02, 0x01, 0x01, 0x01]; |
| 36 | + |
| 37 | +// callee: (i32 offset, i64 x N_I64 * REPEAT) -> () |
| 38 | +// caller: (i32 loopCond, i32 doCall, i32 offset, i64 x N_I64) -> () |
| 39 | +function buildCaller() { |
| 40 | + const sigParams = [0x7f, ...Array(N_I64 * REPEAT).fill(0x7e)]; |
| 41 | + const fsigParams = [0x7f, 0x7f, 0x7f, ...Array(N_I64).fill(0x7e)]; |
| 42 | + const types = section(1, vec([ |
| 43 | + [0x60, ...vec(sigParams), ...vec([])], |
| 44 | + [0x60, ...vec(fsigParams), ...vec([])], |
| 45 | + ])); |
| 46 | + const imports = section(2, vec([memoryImport])); |
| 47 | + const funcs = section(3, vec([[0x01]])); |
| 48 | + const tables = section(4, vec([[0x70, 0x01, 0x01, 0x01]])); |
| 49 | + const exports = section(7, vec([ |
| 50 | + [...str("f"), 0x00, 0x00], |
| 51 | + [...str("tbl"), 0x01, 0x00], |
| 52 | + ])); |
| 53 | + let body = []; |
| 54 | + for (let d = 0; d < LOOP_DEPTH; d++) |
| 55 | + body.push(0x03, 0x40); |
| 56 | + // Keep every i64 local live across the loop nest. |
| 57 | + for (let k = 0; k < N_I64; k++) |
| 58 | + body.push(0x41, 0x00, 0x20, ...leb(3 + k), 0x37, 0x03, ...leb(8 * k)); |
| 59 | + for (let d = 0; d < LOOP_DEPTH; d++) |
| 60 | + body.push(0x20, 0x00, 0x0d, 0x00, 0x0b); |
| 61 | + body.push(0x20, 0x01); |
| 62 | + body.push(0x04, 0x40); |
| 63 | + body.push(0x20, 0x02); |
| 64 | + for (let r = 0; r < REPEAT; r++) { |
| 65 | + for (let k = 0; k < N_I64; k++) |
| 66 | + body.push(0x20, ...leb(3 + k)); |
| 67 | + } |
| 68 | + body.push(0x41, 0x00); |
| 69 | + body.push(0x11, 0x00, 0x00); // call_indirect type=0 table=0 |
| 70 | + body.push(0x0b); |
| 71 | + body.push(0x0b); |
| 72 | + const code = section(10, vec([[...leb(body.length + 1), 0x00, ...body]])); |
| 73 | + return new Uint8Array([ |
| 74 | + 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, |
| 75 | + ...types, ...imports, ...funcs, ...tables, ...exports, ...code, |
| 76 | + ]); |
| 77 | +} |
| 78 | + |
| 79 | +function buildCallee() { |
| 80 | + const sigParams = [0x7f, ...Array(N_I64 * REPEAT).fill(0x7e)]; |
| 81 | + const types = section(1, vec([[0x60, ...vec(sigParams), ...vec([])]])); |
| 82 | + const imports = section(2, vec([memoryImport])); |
| 83 | + const funcs = section(3, vec([[0x00]])); |
| 84 | + const exports = section(7, vec([[...str("g"), 0x00, 0x00]])); |
| 85 | + // local.get 0; local.get 1; i64.store align=3 offset=0 |
| 86 | + const body = [0x20, 0x00, 0x20, 0x01, 0x37, 0x03, 0x00, 0x0b]; |
| 87 | + const code = section(10, vec([[...leb(body.length + 1), 0x00, ...body]])); |
| 88 | + return new Uint8Array([ |
| 89 | + 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, |
| 90 | + ...types, ...imports, ...funcs, ...exports, ...code, |
| 91 | + ]); |
| 92 | +} |
| 93 | + |
| 94 | +const callerMemory = createWebAssemblyMemoryWithMode({ initial: 1, maximum: 1 }, "Signaling"); |
| 95 | +const calleeMemory = createWebAssemblyMemoryWithMode({ initial: 1, maximum: 1 }, "BoundsChecking"); |
| 96 | +assert.eq(WebAssemblyMemoryMode(callerMemory), "Signaling"); |
| 97 | +assert.eq(WebAssemblyMemoryMode(calleeMemory), "BoundsChecking"); |
| 98 | + |
| 99 | +const callerInstance = new WebAssembly.Instance(new WebAssembly.Module(buildCaller()), { e: { mem: callerMemory } }); |
| 100 | +const f = callerInstance.exports.f; |
| 101 | +const tbl = callerInstance.exports.tbl; |
| 102 | + |
| 103 | +// The generator's m_mode comes from the instance, so assert it there too and not just on the memory. |
| 104 | +assert.eq(WebAssemblyMemoryMode(callerInstance), "Signaling"); |
| 105 | + |
| 106 | +const calleeInstance = new WebAssembly.Instance(new WebAssembly.Module(buildCallee()), { e: { mem: calleeMemory } }); |
| 107 | +const g = calleeInstance.exports.g; |
| 108 | +assert.eq(WebAssemblyMemoryMode(calleeInstance), "BoundsChecking"); |
| 109 | + |
| 110 | +const POISON = 0x4141414141414141n; |
| 111 | +const poison = Array(N_I64).fill(POISON); |
| 112 | +const zeros = Array(N_I64).fill(0n); |
| 113 | +const calleeZeros = Array(N_I64 * REPEAT).fill(0n); |
| 114 | +const calleePoison = Array(N_I64 * REPEAT).fill(POISON); |
| 115 | + |
| 116 | +// The callee traps on this access when it is handed its own bounds checking size. Checking it up |
| 117 | +// front catches a test whose own offset is no longer out of bounds, which would otherwise pass |
| 118 | +// vacuously. |
| 119 | +assert.throws(() => g(PAGE_SIZE, ...calleePoison), WebAssembly.RuntimeError, "Out of bounds memory access"); |
| 120 | + |
| 121 | +// Tier g past IPInt. IPInt's prologue reloads the bounds checking size register on entry, which |
| 122 | +// would mask the bug; BBQ and OMG do not. |
| 123 | +for (let i = 0; i < wasmTestLoopCount; i++) |
| 124 | + g(0, ...calleeZeros); |
| 125 | +// Tier f to OMG without ever taking the call_indirect branch: doCall is 0 and the table is still |
| 126 | +// empty, so the cross-instance call is first executed below, against OMG code. |
| 127 | +for (let i = 0; i < wasmTestLoopCount; i++) |
| 128 | + f(0, 0, 0, ...zeros); |
| 129 | + |
| 130 | +tbl.set(0, g); |
| 131 | + |
| 132 | +// Repeat rather than call once: which register Air parks the poison in is an allocation decision, |
| 133 | +// and f may be recompiled (e.g. under wasm-eager-jettison) between iterations. |
| 134 | +for (let i = 0; i < wasmTestLoopCount; i++) |
| 135 | + assert.throws(() => f(0, 1, PAGE_SIZE, ...poison), WebAssembly.RuntimeError, "Out of bounds memory access"); |
0 commit comments