summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/wasm/memory64/memory-init.js
blob: 246fc3f2d40356b8de8e0ad49c5558e9c001b0b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// |jit-test| allow-oom; skip-if: !canRunHugeMemoryTests()

var S = (function () {
    let s = "";
    for ( let i=0; i < 16; i++ )
        s += "0123456789abcdef"
    return s;
})();

for (let shared of ['', 'shared']) {
    try {
        var ins = new WebAssembly.Instance(new WebAssembly.Module(wasmTextToBinary(`
(module
  (memory (export "mem") i64 65537 65537 ${shared})
  (data $d "${S}")
  (func (export "f") (param $p i64) (param $o i32) (param $n i32)
    (memory.init $d (local.get $p) (local.get $o) (local.get $n))))`)));
    } catch (e) {
        if (e instanceof WebAssembly.RuntimeError && String(e).match(/too many memory pages/)) {
            quit(0);
        }
        throw e;
    }

    var mem = new Uint8Array(ins.exports.mem.buffer);

    // Init above 4GB
    doit(mem, 0x1_0000_1000, 1, S.length-1);

    // Init above 4GB with OOM
    assertErrorMessage(() => ins.exports.f(0x1_0000_ff80n, 0, 256),
                       WebAssembly.RuntimeError,
                       /out of bounds/);

    // Init across 4GB
    doit(mem, 0xffff_ff80, 3, 200);
}

function doit(mem, addr, offs, n) {
    ins.exports.f(BigInt(addr), offs, n);
    for (let i=0; i < n; i++) {
        assertEq(mem[addr+i], S.charCodeAt(offs+i));
    }
}