| 1 |
// |jit-test| skip-if: !wasmMemoryControlEnabled(); test-also=--setpref=wasm_memory64=true; test-also=--setpref=wasm_memory64=false |
| 2 |
|
| 3 |
// This tests memory.discard and WebAssembly.Memory.discard() by placing data |
| 4 |
// (the alphabet) halfway across a page boundary, then discarding the first |
| 5 |
// page. The first half of the alphabet should be zeroed; the second should |
| 6 |
// not. The memory should remain readable and writable. |
| 7 |
// |
| 8 |
// The ultimate goal is to release physical pages of memory back to the |
| 9 |
// operating system, but we can't really observe memory metrics here. Oh well. |
| 10 |
|
| 11 |
function initModule(discardOffset, discardLen, discardViaJS, shared, memType = 'i32') { |
| 12 |
const memProps = shared ? '4 4 shared' : '4'; // 4 pages |
| 13 |
const text = `(module |
| 14 |
(memory (export "memory") ${memType} ${memProps}) |
| 15 |
(data "abcdefghijklmnopqrstuvwxyz") |
| 16 |
(func (export "init") |
| 17 |
;; splat alphabet halfway across the 3/4 page boundary. |
| 18 |
;; 196595 = (65536 * 3) - (26 / 2) |
| 19 |
(memory.init 0 (${memType}.const 196595) (i32.const 0) (i32.const 26)) |
| 20 |
) |
| 21 |
(func (export "letter") (param i32) (result i32) |
| 22 |
(i32.load8_u (${memType}.add (${memType}.const 196595) ${memType == 'i64' ? '(i64.extend_i32_u (local.get 0))' : '(local.get 0)'})) |
| 23 |
) |
| 24 |
(func (export "discard") |
| 25 |
(memory.discard (${memType}.const ${discardOffset}) (${memType}.const ${discardLen})) |
| 26 |
) |
| 27 |
)`; |
| 28 |
const exp = wasmEvalText(text).exports; |
| 29 |
|
| 30 |
return [exp, discardViaJS ? () => exp.memory.discard(discardOffset, discardLen) : exp.discard]; |
| 31 |
} |
| 32 |
|
| 33 |
function checkRegion(exp, min, max, expectLetters) { |
| 34 |
for (let i = min; i < max; i++) { |
| 35 |
const c = "a".charCodeAt(0) + i; |
| 36 |
const expected = expectLetters ? c : 0; |
| 37 |
assertEq(exp.letter(i), expected, `checking position of letter ${String.fromCharCode(c)}`); |
| 38 |
} |
| 39 |
} |
| 40 |
function checkFirstHalf(exp, expectLetters) { return checkRegion(exp, 0, 13, expectLetters) } |
| 41 |
function checkSecondHalf(exp, expectLetters) { return checkRegion(exp, 13, 26, expectLetters) } |
| 42 |
function checkWholeAlphabet(exp, expectLetters) { return checkRegion(exp, 0, 26, expectLetters) } |
| 43 |
|
| 44 |
function testAll(func) { |
| 45 |
func(false, false, 'i32'); |
| 46 |
func(false, true, 'i32'); |
| 47 |
func(true, false, 'i32'); |
| 48 |
func(true, true, 'i32'); |
| 49 |
if (wasmMemory64Enabled()) { |
| 50 |
func(false, false, 'i64'); |
| 51 |
func(false, true, 'i64'); |
| 52 |
func(true, false, 'i64'); |
| 53 |
func(true, true, 'i64'); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
testAll(function testHappyPath(discardViaJS, shared, memType) { |
| 58 |
// Only page 3 of memory, half the alphabet |
| 59 |
const [exp, discard] = initModule(65536 * 2, 65536, discardViaJS, shared, memType); |
| 60 |
|
| 61 |
// All zero to start |
| 62 |
checkWholeAlphabet(exp, false); |
| 63 |
|
| 64 |
// Then all alphabet |
| 65 |
exp.init(); |
| 66 |
checkWholeAlphabet(exp, true); |
| 67 |
|
| 68 |
// Discarding the first page clears the first half of the alphabet |
| 69 |
discard(); |
| 70 |
checkFirstHalf(exp, false); |
| 71 |
checkSecondHalf(exp, true); |
| 72 |
|
| 73 |
// We should be able to write back to a discarded region of memory |
| 74 |
exp.init(); |
| 75 |
checkWholeAlphabet(exp, true); |
| 76 |
|
| 77 |
// ...and then discard again |
| 78 |
discard(); |
| 79 |
checkFirstHalf(exp, false); |
| 80 |
checkSecondHalf(exp, true); |
| 81 |
}); |
| 82 |
|
| 83 |
testAll(function testZeroLen(discardViaJS, shared) { |
| 84 |
// Discard zero bytes |
| 85 |
const [exp, discard] = initModule(PageSizeInBytes * 2, 0, discardViaJS, shared); |
| 86 |
|
| 87 |
// Init the stuff |
| 88 |
exp.init(); |
| 89 |
checkWholeAlphabet(exp, true); |
| 90 |
|
| 91 |
// Discarding zero bytes should be valid... |
| 92 |
discard(); |
| 93 |
|
| 94 |
// ...but should have no effect. |
| 95 |
checkWholeAlphabet(exp, true); |
| 96 |
}); |
| 97 |
|
| 98 |
testAll(function testWithGrow(discardViaJS, shared, memType) { |
| 99 |
if (shared) { |
| 100 |
return; // shared memories cannot grow |
| 101 |
} |
| 102 |
|
| 103 |
// Only page 3 of memory, half the alphabet. There is no max size on the |
| 104 |
// memory, so it will be subject to moving grows in 32-bit mode. |
| 105 |
const [exp, discard] = initModule(65536 * 2, 65536, discardViaJS, false, memType); |
| 106 |
|
| 107 |
// Start with the whole alphabet |
| 108 |
exp.init(); |
| 109 |
checkWholeAlphabet(exp, true); |
| 110 |
|
| 111 |
// Discarding the first page clears the first half of the alphabet |
| 112 |
discard(); |
| 113 |
checkFirstHalf(exp, false); |
| 114 |
checkSecondHalf(exp, true); |
| 115 |
|
| 116 |
// Oops we just grew by a preposterous amount, time to move |
| 117 |
exp.memory.grow(memType === "i64" ? 200n : 200); |
| 118 |
|
| 119 |
// The memory should still be readable |
| 120 |
checkFirstHalf(exp, false); |
| 121 |
checkSecondHalf(exp, true); |
| 122 |
|
| 123 |
// We should be able to write back to a discarded region of memory |
| 124 |
exp.init(); |
| 125 |
checkWholeAlphabet(exp, true); |
| 126 |
|
| 127 |
// ...and then discard again |
| 128 |
discard(); |
| 129 |
checkFirstHalf(exp, false); |
| 130 |
checkSecondHalf(exp, true); |
| 131 |
}); |
| 132 |
|
| 133 |
testAll(function testOOB(discardViaJS, shared) { |
| 134 |
// Discard two pages where there is only one |
| 135 |
const [exp, discard] = initModule(PageSizeInBytes * 3, PageSizeInBytes * 2, discardViaJS, shared); |
| 136 |
|
| 137 |
exp.init(); |
| 138 |
checkWholeAlphabet(exp, true); |
| 139 |
assertErrorMessage(() => discard(), WebAssembly.RuntimeError, /out of bounds/); |
| 140 |
|
| 141 |
// Ensure nothing was discarded |
| 142 |
checkWholeAlphabet(exp, true); |
| 143 |
}); |
| 144 |
|
| 145 |
testAll(function testOOB2(discardViaJS, shared) { |
| 146 |
// Discard two pages starting near the end of 32-bit address space |
| 147 |
// (would trigger an overflow in 32-bit world) |
| 148 |
const [exp, discard] = initModule(2 ** 32 - PageSizeInBytes, PageSizeInBytes * 2, discardViaJS, shared); |
| 149 |
|
| 150 |
exp.init(); |
| 151 |
checkWholeAlphabet(exp, true); |
| 152 |
assertErrorMessage(() => discard(), WebAssembly.RuntimeError, /out of bounds/); |
| 153 |
|
| 154 |
// Ensure nothing was discarded |
| 155 |
checkWholeAlphabet(exp, true); |
| 156 |
}); |
| 157 |
|
| 158 |
testAll(function testOOB3(discardViaJS, shared) { |
| 159 |
// Discard nearly an entire 32-bit address space's worth of pages. Very exciting! |
| 160 |
const [exp, discard] = initModule(0, 2 ** 32 - PageSizeInBytes, discardViaJS, shared); |
| 161 |
|
| 162 |
exp.init(); |
| 163 |
checkWholeAlphabet(exp, true); |
| 164 |
assertErrorMessage(() => discard(), WebAssembly.RuntimeError, /out of bounds/); |
| 165 |
|
| 166 |
// Ensure nothing was discarded |
| 167 |
checkWholeAlphabet(exp, true); |
| 168 |
}); |
| 169 |
|
| 170 |
(function testOOB4() { |
| 171 |
// Discard an entire 32-bit address space's worth of pages. JS can do this |
| 172 |
// even to 32-bit memories because it can handle numbers bigger |
| 173 |
// than 2^32 - 1. |
| 174 |
const [exp, _] = initModule(0, 0, false); // pass zero to allow the wasm module to validate |
| 175 |
const discard = () => exp.memory.discard(0, 2 ** 32); |
| 176 |
|
| 177 |
exp.init(); |
| 178 |
checkWholeAlphabet(exp, true); |
| 179 |
assertErrorMessage(() => discard(), WebAssembly.RuntimeError, /out of bounds/); |
| 180 |
|
| 181 |
// Ensure nothing was discarded |
| 182 |
checkWholeAlphabet(exp, true); |
| 183 |
})(); |
| 184 |
|
| 185 |
if (wasmMemory64Enabled()) { |
| 186 |
(function testOverflow() { |
| 187 |
// Discard UINT64_MAX - (2 pages), starting from page 4. This overflows but puts both start and end in bounds. |
| 188 |
// This cannot be done with a JS discard because JS can't actually represent big enough integers. |
| 189 |
|
| 190 |
// The big ol' number here is 2^64 - (65536 * 2) |
| 191 |
const [exp, discard] = initModule(65536 * 3, `18_446_744_073_709_420_544`, false, false, 'i64'); |
| 192 |
|
| 193 |
// Init the stuff |
| 194 |
exp.init(); |
| 195 |
checkWholeAlphabet(exp, true); |
| 196 |
|
| 197 |
// Discarding should not be valid when it goes out of bounds |
| 198 |
assertErrorMessage(() => discard(), WebAssembly.RuntimeError, /out of bounds/); |
| 199 |
|
| 200 |
// Ensure nothing was discarded |
| 201 |
checkWholeAlphabet(exp, true); |
| 202 |
})(); |
| 203 |
} |
| 204 |
|
| 205 |
testAll(function testMisalignedStart(discardViaJS, shared) { |
| 206 |
// Discard only the first half of the alphabet (this misaligns the start) |
| 207 |
const [exp, discard] = initModule(PageSizeInBytes * 3 - 13, 13, discardViaJS, shared); |
| 208 |
|
| 209 |
exp.init(); |
| 210 |
checkWholeAlphabet(exp, true); |
| 211 |
assertErrorMessage(() => discard(), WebAssembly.RuntimeError, /unaligned/); |
| 212 |
|
| 213 |
// Ensure nothing was discarded |
| 214 |
checkWholeAlphabet(exp, true); |
| 215 |
}); |
| 216 |
|
| 217 |
testAll(function testMisalignedEnd(discardViaJS, shared) { |
| 218 |
// Discard only the second half of the alphabet (this misaligns the end) |
| 219 |
const [exp, discard] = initModule(PageSizeInBytes * 3, 13, discardViaJS, shared); |
| 220 |
|
| 221 |
exp.init(); |
| 222 |
checkWholeAlphabet(exp, true); |
| 223 |
assertErrorMessage(() => discard(), WebAssembly.RuntimeError, /unaligned/); |
| 224 |
|
| 225 |
// Ensure nothing was discarded |
| 226 |
checkWholeAlphabet(exp, true); |
| 227 |
}); |