| 1 |
// Cloned from memory.js but kept separate because it may have to be disabled on |
| 2 |
// some devices until bugs are fixed. |
| 3 |
|
| 4 |
// Bug 1666747 - partially OOB unaligned stores are not handled correctly on ARM |
| 5 |
// and ARM64. The simulators don't implement the correct semantics anyhow, so |
| 6 |
// when the bug is fixed in the code generator they must remain excluded here. |
| 7 |
var excluded = getBuildConfiguration("arm64") || |
| 8 |
getBuildConfiguration("arm64-simulator") || |
| 9 |
getBuildConfiguration("arm") || |
| 10 |
getBuildConfiguration("arm-simulator"); |
| 11 |
var thirtytwobit = getBuildConfiguration("pointer-byte-size") == 4; |
| 12 |
|
| 13 |
const RuntimeError = WebAssembly.RuntimeError; |
| 14 |
|
| 15 |
function storeModuleSrc(type, ext, offset, align) { |
| 16 |
var load_ext = ext === '' ? '' : ext + '_s'; |
| 17 |
return `(module |
| 18 |
(memory (export "mem") 1) |
| 19 |
(data (i32.const 0) "\\00\\01\\02\\03\\04\\05\\06\\07\\08\\09\\0a\\0b\\0c\\0d\\0e\\0f") |
| 20 |
(data (i32.const 16) "\\f0\\f1\\f2\\f3\\f4\\f5\\f6\\f7\\f8\\f9\\fa\\fb\\fc\\fd\\fe\\ff") |
| 21 |
(func $store (param i32) (param ${type}) |
| 22 |
(${type}.store${ext} |
| 23 |
offset=${offset} |
| 24 |
${align != 0 ? 'align=' + align : ''} |
| 25 |
(local.get 0) |
| 26 |
(local.get 1) |
| 27 |
) |
| 28 |
) (export "store" (func 0)) |
| 29 |
(func $load (param i32) (result ${type}) |
| 30 |
(${type}.load${load_ext} |
| 31 |
offset=${offset} |
| 32 |
${align != 0 ? 'align=' + align : ''} |
| 33 |
(local.get 0) |
| 34 |
) |
| 35 |
) (export "load" (func 1)))`; |
| 36 |
} |
| 37 |
|
| 38 |
function storeModule(type, ext, offset, align, exportBox = null) { |
| 39 |
let exports = wasmEvalText(storeModuleSrc(type, ext, offset, align)).exports; |
| 40 |
if (exportBox !== null) |
| 41 |
exportBox.exports = exports; |
| 42 |
return exports; |
| 43 |
} |
| 44 |
|
| 45 |
function testStoreOOB(type, ext, base, offset, align, value) { |
| 46 |
let exportBox = {}; |
| 47 |
if (type === 'i64') { |
| 48 |
assertErrorMessage(() => wasmAssert( |
| 49 |
storeModuleSrc(type, ext, offset, align), |
| 50 |
[{type, func: '$store', args: [`i32.const ${base}`, `i64.const ${value}`]}], |
| 51 |
{}, |
| 52 |
exportBox |
| 53 |
), RuntimeError, /index out of bounds/); |
| 54 |
} else { |
| 55 |
assertErrorMessage(() => storeModule(type, ext, offset, align, exportBox).store(base, value), |
| 56 |
RuntimeError, |
| 57 |
/index out of bounds/); |
| 58 |
} |
| 59 |
|
| 60 |
// Check that there were no partial writes at the end of the memory. |
| 61 |
let buf = new Int8Array(exportBox.exports.mem.buffer); |
| 62 |
let len = buf.length; |
| 63 |
for ( let addr = base + offset ; addr < len; addr++ ) |
| 64 |
assertEq(buf[addr], 0); |
| 65 |
} |
| 66 |
|
| 67 |
// Test bounds checks and edge cases. |
| 68 |
|
| 69 |
for (let align of [0,1,2,4]) { |
| 70 |
|
| 71 |
for (let offset of [0, 1, 2, 3, 4, 8, 16, 41, 0xfff0, 0xfff8]) { |
| 72 |
// Accesses of 1 byte. |
| 73 |
let lastValidIndex = 0x10000 - 1 - offset; |
| 74 |
if (align < 2 && !excluded) { |
| 75 |
testStoreOOB('i32', '8', lastValidIndex + 1, offset, align, -42); |
| 76 |
} |
| 77 |
|
| 78 |
// Accesses of 2 bytes. |
| 79 |
lastValidIndex = 0x10000 - 2 - offset; |
| 80 |
if (align < 4 && !excluded) { |
| 81 |
testStoreOOB('i32', '16', lastValidIndex + 1, offset, align, -32768); |
| 82 |
} |
| 83 |
|
| 84 |
// Accesses of 4 bytes. |
| 85 |
lastValidIndex = 0x10000 - 4 - offset; |
| 86 |
if (!excluded) { |
| 87 |
testStoreOOB('i32', '', lastValidIndex + 1, offset, align, 1337); |
| 88 |
testStoreOOB('f32', '', lastValidIndex + 1, offset, align, Math.fround(13.37)); |
| 89 |
} |
| 90 |
|
| 91 |
// Accesses of 8 bytes. |
| 92 |
lastValidIndex = 0x10000 - 8 - offset; |
| 93 |
if (!excluded) { |
| 94 |
testStoreOOB('f64', '', lastValidIndex + 1, offset, align, 1.23456789); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
for (let offset of [0, 1, 2, 3, 4, 8, 16, 41, 0xfff0, 0xfff8]) { |
| 99 |
// Accesses of 1 byte. |
| 100 |
let lastValidIndex = 0x10000 - 1 - offset; |
| 101 |
if (align < 2 && !excluded) { |
| 102 |
testStoreOOB('i64', '8', lastValidIndex + 1, offset, align, -42); |
| 103 |
} |
| 104 |
|
| 105 |
// Accesses of 2 bytes. |
| 106 |
lastValidIndex = 0x10000 - 2 - offset; |
| 107 |
if (align < 4 && !excluded) { |
| 108 |
testStoreOOB('i64', '16', lastValidIndex + 1, offset, align, -32768); |
| 109 |
} |
| 110 |
|
| 111 |
// Accesses of 4 bytes. |
| 112 |
lastValidIndex = 0x10000 - 4 - offset; |
| 113 |
if (!excluded) { |
| 114 |
testStoreOOB('i64', '32', lastValidIndex + 1, offset, align, 0xf1231337 | 0); |
| 115 |
} |
| 116 |
|
| 117 |
// Accesses of 8 bytes. |
| 118 |
lastValidIndex = 0x10000 - 8 - offset; |
| 119 |
if (!excluded) { |
| 120 |
testStoreOOB('i64', '', lastValidIndex + 1, offset, align, '0x1234567887654321'); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
// On 32-bit platforms, a declared-aligned i64 store is implemented as two |
| 126 |
// presumed-aligned 32-bit stores. This tests that we don't store the low |
| 127 |
// word before the high word if the low word is in-bounds but the high word |
| 128 |
// is not. |
| 129 |
if (thirtytwobit) { |
| 130 |
testStoreOOB('i64', '', 0x10000 - 4, 0, 0, '0x0123456789abcdef'); |
| 131 |
} |