| 1 |
// Test bounds checking at the end of memory with a constant base pointer. |
| 2 |
// This is intended to verify a bounds check elimination optimization in |
| 3 |
// the baseline compiler. |
| 4 |
|
| 5 |
// This can be only a functional test, we can't really see whether |
| 6 |
// the optimization is being applied. However, manual inspection |
| 7 |
// of the generated code has verified that the optimization is |
| 8 |
// being applied. |
| 9 |
|
| 10 |
// Memory is one page and minimum memory is one page, so accesses across |
| 11 |
// the end of the first page should fail. |
| 12 |
|
| 13 |
function gen(base, offset) { |
| 14 |
return wasmEvalText(`(module |
| 15 |
(memory 1) |
| 16 |
(data (i32.const 65528) "aaaaaaaa") |
| 17 |
(func (result i32) |
| 18 |
(i32.load offset=${offset} (i32.const ${base}))) |
| 19 |
(export "f" (func 0)))`).exports["f"]; |
| 20 |
} |
| 21 |
|
| 22 |
// Memory is two pages but minimum memory is one page, so accesses across |
| 23 |
// the end of the first page should succeed. |
| 24 |
|
| 25 |
function gen2(base, offset) { |
| 26 |
return wasmEvalText(`(module |
| 27 |
(memory 1) |
| 28 |
(data (i32.const 65528) "aaaaaaaa") |
| 29 |
(func (result i32) |
| 30 |
(drop (memory.grow (i32.const 1))) |
| 31 |
(i32.store (i32.const 65536) (i32.const 0x61616161)) |
| 32 |
(i32.store (i32.const 65540) (i32.const 0x61616161)) |
| 33 |
(i32.store (i32.const 80000) (i32.const 0x61616161)) |
| 34 |
(i32.store (i32.const 120000) (i32.const 0x61616161)) |
| 35 |
(i32.load offset=${offset} (i32.const ${base}))) |
| 36 |
(export "f" (func 0)))`).exports["f"]; |
| 37 |
} |
| 38 |
|
| 39 |
// Access the first page. |
| 40 |
// None of these should have an explicit bounds check generated for them as the |
| 41 |
// effective address is less than the sum of the minimum heap length and the |
| 42 |
// offset guard length. |
| 43 |
|
| 44 |
let onfirst = [[4,65528], [3,65528], [2,65528], [1,65528], [0,65528], |
| 45 |
[3,65529], [2,65529], [1,65529], [0,65529], |
| 46 |
[2,65530], [1,65530], [0,65530], |
| 47 |
[1,65531], [0,65531], |
| 48 |
[0,65532], |
| 49 |
[256, 65536-256-4], |
| 50 |
[2048, 65536-2048-4], |
| 51 |
[30000, 65536-30000-4]]; |
| 52 |
|
| 53 |
for ( let [x,y] of onfirst ) { |
| 54 |
assertEq(gen(x,y)(), 0x61616161|0); |
| 55 |
assertEq(gen(y,x)(), 0x61616161|0); |
| 56 |
} |
| 57 |
|
| 58 |
// Access the second page. |
| 59 |
// Again, no explicit bounds checks should be generated here. |
| 60 |
|
| 61 |
let onsecond = [[65532,1], [65532,2], [65532,3], [65532,4], [65532,5], [65532,6], [65532,7], [65532,8], |
| 62 |
[65531,2], [65531,3], [65531,4], [65531,5], [65531,6], [65531,7], [65531,8], [65531,9], |
| 63 |
[65530,3], [65530,4], [65530,5], [65530,6], [65530,7], [65530,8], [65530,9], [65530,10], |
| 64 |
[65529,4], [65529,5], [65529,6], [65529,7], [65529,8], [65529,9], [65529,10], [65529,11], |
| 65 |
[65528,5], [65528,6], [65528,7], [65528,8], [65528,9], [65528,10], [65528,11], [65528,12], |
| 66 |
[65527,6], [65527,7], [65527,8], [65527,9], [65527,10], [65527,11], [65527,12], [65527,13], |
| 67 |
[65526,7], [65526,8], [65526,9], [65526,10], [65526,11], [65526,12], [65526,13], [65526,14], |
| 68 |
[65525,8], [65525,9], [65525,10], [65525,11], [65525,12], [65525,13], [65525,14], [65525,15], |
| 69 |
[256, 65536-256], |
| 70 |
[2048, 65536-2048], |
| 71 |
[30000, 50000], |
| 72 |
[30000, 90000]]; |
| 73 |
|
| 74 |
for ( let [x,y] of onsecond ) { |
| 75 |
assertErrorMessage(() => { gen(x,y)() }, WebAssembly.RuntimeError, /index out of bounds/); |
| 76 |
assertErrorMessage(() => { gen(y,x)() }, WebAssembly.RuntimeError, /index out of bounds/); |
| 77 |
|
| 78 |
assertEq(gen2(x,y)(), 0x61616161|0); |
| 79 |
assertEq(gen2(y,x)(), 0x61616161|0); |
| 80 |
} |
| 81 |
|
| 82 |
// Access the third page. |
| 83 |
// Here the explicit bounds check cannot be omitted, as the access is |
| 84 |
// beyond min + offset guard. |
| 85 |
|
| 86 |
let onthird = [[60000, 90000]]; |
| 87 |
|
| 88 |
for ( let [x,y] of onthird ) { |
| 89 |
assertErrorMessage(() => { gen(x,y)() }, WebAssembly.RuntimeError, /index out of bounds/); |
| 90 |
assertErrorMessage(() => { gen(y,x)() }, WebAssembly.RuntimeError, /index out of bounds/); |
| 91 |
|
| 92 |
assertErrorMessage(() => { gen2(x,y)() }, WebAssembly.RuntimeError, /index out of bounds/); |
| 93 |
assertErrorMessage(() => { gen2(y,x)() }, WebAssembly.RuntimeError, /index out of bounds/); |
| 94 |
} |