|
| 1 | +// Concurrent function validation should report the CompileError for the lowest |
| 2 | +// failing function index, not whichever worker finishes first. |
| 3 | +// https://bugs.webkit.org/show_bug.cgi?id=283476 |
| 4 | +//@ requireOptions("--useConcurrentJIT=true") |
| 5 | + |
| 6 | +import * as assert from "../assert.js"; |
| 7 | + |
| 8 | +function leb(value) { |
| 9 | + let bytes = []; |
| 10 | + do { |
| 11 | + let byte = value & 0x7f; |
| 12 | + value >>>= 7; |
| 13 | + if (value) |
| 14 | + byte |= 0x80; |
| 15 | + bytes.push(byte); |
| 16 | + } while (value); |
| 17 | + return bytes; |
| 18 | +} |
| 19 | + |
| 20 | +function section(id, payload) { |
| 21 | + return [id, ...leb(payload.length), ...payload]; |
| 22 | +} |
| 23 | + |
| 24 | +// Many invalid functions. Function 0 is large (slow to validate) so a higher-index |
| 25 | +// worker is likely to finish first under concurrent compilation. The reported |
| 26 | +// error must still be for function 0. |
| 27 | +function invalidModuleBytes() { |
| 28 | + const types = [0x01, 0x60, 0x00, 0x00]; |
| 29 | + const functionCount = 16; |
| 30 | + const functions = [functionCount, ...Array(functionCount).fill(0x00)]; |
| 31 | + |
| 32 | + // Function 0: many nops then empty-stack i32.add (fails late). |
| 33 | + const body0 = [0x00]; |
| 34 | + for (let i = 0; i < 8000; ++i) |
| 35 | + body0.push(0x01); // nop |
| 36 | + body0.push(0x6a, 0x0b); // i32.add end |
| 37 | + |
| 38 | + // Functions 1..N: immediate empty-stack f32.add (fails quickly). |
| 39 | + const bodyFast = [0x00, 0x92, 0x0b]; |
| 40 | + |
| 41 | + const codePayload = [functionCount, ...leb(body0.length), ...body0]; |
| 42 | + for (let i = 1; i < functionCount; ++i) |
| 43 | + codePayload.push(...leb(bodyFast.length), ...bodyFast); |
| 44 | + |
| 45 | + return new Uint8Array([ |
| 46 | + 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, |
| 47 | + ...section(1, types), |
| 48 | + ...section(3, functions), |
| 49 | + ...section(10, codePayload), |
| 50 | + ]); |
| 51 | +} |
| 52 | + |
| 53 | +const bytes = invalidModuleBytes(); |
| 54 | +const iterations = 80; |
| 55 | + |
| 56 | +for (let i = 0; i < iterations; ++i) { |
| 57 | + let message; |
| 58 | + try { |
| 59 | + new WebAssembly.Module(bytes); |
| 60 | + throw new Error("expected CompileError"); |
| 61 | + } catch (error) { |
| 62 | + assert.truthy(error instanceof WebAssembly.CompileError, `expected CompileError, got ${error}`); |
| 63 | + message = String(error); |
| 64 | + } |
| 65 | + assert.truthy( |
| 66 | + message.includes("function at index 0"), |
| 67 | + `iteration ${i}: expected error for function 0, got: ${message}` |
| 68 | + ); |
| 69 | + for (let j = 1; j < 16; ++j) { |
| 70 | + assert.truthy( |
| 71 | + !message.includes(`function at index ${j}`), |
| 72 | + `iteration ${i}: should not report function ${j} when function 0 also fails: ${message}` |
| 73 | + ); |
| 74 | + } |
| 75 | +} |
0 commit comments