tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

decoding.js (3424B)


      1 // |jit-test| skip-if: !wasmCustomPageSizesEnabled()
      2 
      3 load(libdir + "wasm-binary.js");
      4 
      5 wasmValidateText(`(module
      6    (memory 0 1 (pagesize 1))
      7  )
      8 `);
      9 
     10 wasmValidateText(`(module
     11    (memory 0 1 (pagesize 65536))
     12  )
     13 `);
     14 
     15 // Missing page size.
     16 assertErrorMessage(() => wasmEvalBinary(moduleWithSections([ {name:memoryId, body:[0x01, 0x08, 0x00]} ])), WebAssembly.CompileError, /failed to decode custom page size/);
     17 
     18 // Too many bytes after page size.
     19 assertErrorMessage(() => wasmEvalBinary(moduleWithSections([ {name:memoryId, body:[0x01, 0x08, 0x00, 0x10, 0x01]} ])), WebAssembly.CompileError, /byte size mismatch in memory section/);
     20 
     21 // Tables shouldn't have page sizes.
     22 assertErrorMessage(() => wasmEvalBinary(moduleWithSections([ {name:tableId, body:[0x01, FuncRefCode, 0x08, 0x00, 0x10]} ])), WebAssembly.CompileError, /unexpected bits set in flags: 8/);
     23 
     24 function checkInvalidPageSize(pageSize) {
     25    assertErrorMessage(() => wasmValidateText(`(module
     26          (memory 0 1 (pagesize ${pageSize}))
     27        )`),
     28        WebAssembly.CompileError, /bad custom page size/);
     29 }
     30 
     31 checkInvalidPageSize(8);
     32 checkInvalidPageSize(128);
     33 checkInvalidPageSize(131072);
     34 
     35 wasmValidateText(`(module
     36    (memory 0 65536 (pagesize 1))
     37  )
     38 `);
     39 
     40 wasmValidateText(`(module
     41    (memory i32 0 65536 (pagesize 65536))
     42  )
     43 `);
     44 
     45 wasmValidateText(`(module
     46    (memory i32 0 4_294_967_295 (pagesize 1))
     47  )
     48 `);
     49 
     50 wasmValidateText(`(module
     51    (memory i64 0 4_294_967_296 (pagesize 1))
     52  )
     53 `);
     54 
     55 wasmValidateText(`(module
     56    (memory i64 0 9_007_199_254_740_991 (pagesize 1))
     57  )
     58 `);
     59 
     60 function maxPageCount(pageSize) {
     61    if (pageSize === 1)
     62      return 0xffffffff;
     63    return 1 << (32 - Math.log2(pageSize));
     64 }
     65 
     66 function maxPageCount64(pageSize) {
     67    if (pageSize === 1)
     68      return BigInt(Number.MAX_SAFE_INTEGER);
     69    return (1n << (53n - BigInt(Math.log2(pageSize)))) - 1n;
     70 }
     71 
     72 function checkPageCount(pageSize, pageCount) {
     73    function check() {
     74        return wasmValidateText(`(module
     75            (memory 0 ${pageCount} (pagesize ${pageSize}))
     76        )`);
     77    }
     78    if (pageCount <= maxPageCount(pageSize))
     79        check();
     80    else if (pageCount > 0xffffffff) {
     81        // In this case, a u64 is encoded instead of a u32 and a decoding
     82        // error raises this exception.
     83        assertErrorMessage(check, WebAssembly.CompileError,
     84                           /expected maximum length/);
     85    }
     86    else
     87        assertErrorMessage(check, WebAssembly.CompileError,
     88                           /maximum memory size too big/);
     89 }
     90 
     91 function checkPageCount64(pageSize, pageCount) {
     92    function check() {
     93        return wasmValidateText(`(module
     94            (memory i64 0 ${pageCount} (pagesize ${pageSize}))
     95        )`);
     96    }
     97    if (pageCount <= maxPageCount64(pageSize))
     98        check();
     99    else {
    100        assertErrorMessage(check, WebAssembly.CompileError,
    101                           /maximum memory size too big/);
    102    }
    103 }
    104 
    105 for (pageSize of [1, 65536])
    106    for (pageCount of [0, 1, 42, maxPageCount(pageSize)-1,
    107                       maxPageCount(pageSize),
    108                       maxPageCount(pageSize)+1,
    109                       0xffffffff])
    110        checkPageCount(pageSize, pageCount);
    111 
    112 for (pageSize of [1, 65536])
    113    for (pageCount of [0n, 1n, 42n, maxPageCount64(pageSize)-1n,
    114                       maxPageCount64(pageSize),
    115                       maxPageCount64(pageSize)+1n])
    116        checkPageCount64(pageSize, pageCount);