tor-browser

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

wasm-breakpoint.js (5710B)


      1 // |jit-test| test-also=--wasm-compiler=optimizing; skip-if: !wasmDebuggingEnabled()
      2 // Tests that wasm module scripts handles basic breakpoint operations.
      3 
      4 load(libdir + "wasm.js");
      5 
      6 function runTest(wast, initFunc, doneFunc) {
      7    let g = newGlobal({newCompartment: true});
      8    let dbg = new Debugger(g);
      9 
     10    g.eval(`
     11 var b = wasmTextToBinary('${wast}');
     12 var m = new WebAssembly.Instance(new WebAssembly.Module(b));
     13 `);
     14 
     15    var wasmScript = dbg.findScripts().filter(s => s.format == 'wasm')[0];
     16    var breakpoints = wasmScript.getPossibleBreakpointOffsets();
     17 
     18    initFunc({
     19        dbg,
     20        wasmScript,
     21        g,
     22        breakpoints,
     23    });
     24 
     25    let result, error;
     26    try {
     27        result = g.eval("m.exports.test()");
     28    } catch (ex) {
     29        error = ex;
     30    }
     31 
     32    doneFunc({
     33        dbg,
     34        result,
     35        error,
     36        wasmScript,
     37        g
     38    });
     39 }
     40 
     41 
     42 var onBreakpointCalled;
     43 
     44 // Checking if we can stop at specified breakpoint.
     45 runTest(
     46    '(module (func (nop) (nop)) (export "test" (func 0)))',
     47    function ({wasmScript, breakpoints}) {
     48        print(`${JSON.stringify(breakpoints)}`);
     49        assertEq(breakpoints.length, 2);
     50        assertEq(breakpoints[0] > 0, true);
     51        // Checking if breakpoints offsets are in ascending order.
     52        assertEq(breakpoints[0] < breakpoints[1], true);
     53        onBreakpointCalled = 0;
     54        breakpoints.forEach(function (offset) {
     55            wasmScript.setBreakpoint(offset, {
     56                hit: (frame) => {
     57                    assertEq(frame.offset, offset);
     58                    onBreakpointCalled++;
     59                }
     60            });
     61        });
     62    },
     63    function ({dbg, error}) {
     64        assertEq(error, undefined);
     65        assertEq(onBreakpointCalled, 2);
     66    }
     67 );
     68 
     69 // Checking if we can remove breakpoint one by one.
     70 runTest(
     71    '(module (func (nop) (nop)) (export "test" (func 0)))',
     72    function ({wasmScript, breakpoints}) {
     73        onBreakpointCalled = 0;
     74        var handlers = [];
     75        breakpoints.forEach(function (offset, i) {
     76            wasmScript.setBreakpoint(offset, handlers[i] = {
     77                hit: (frame) => {
     78                    assertEq(frame.offset, breakpoints[0]);
     79                    onBreakpointCalled++;
     80                    // Removing all handlers.
     81                    handlers.forEach(h => wasmScript.clearBreakpoint(h));
     82                }
     83            });
     84        });
     85    },
     86    function ({error}) {
     87        assertEq(error, undefined);
     88        assertEq(onBreakpointCalled, 1);
     89    }
     90 );
     91 
     92 // Checking if we can remove breakpoint one by one from a breakpoint handler.
     93 runTest(
     94    '(module (func (nop) (nop)) (export "test" (func 0)))',
     95    function ({wasmScript, breakpoints}) {
     96        onBreakpointCalled = 0;
     97        var handlers = [];
     98        breakpoints.forEach(function (offset, i) {
     99            wasmScript.setBreakpoint(offset, handlers[i] = {
    100                hit: (frame) => {
    101                    assertEq(frame.offset, breakpoints[0]);
    102                    onBreakpointCalled++;
    103                    // Removing all handlers.
    104                    handlers.forEach(h => wasmScript.clearBreakpoint(h));
    105                }
    106            });
    107        });
    108    },
    109    function ({error}) {
    110        assertEq(error, undefined);
    111        assertEq(onBreakpointCalled, 1);
    112    }
    113 );
    114 
    115 // Checking if we can remove breakpoint one by one from onEnterFrame,
    116 // but onStep will still work.
    117 var onStepCalled;
    118 runTest(
    119    '(module (func (nop) (nop)) (export "test" (func 0)))',
    120    function ({dbg, wasmScript, breakpoints}) {
    121        onBreakpointCalled = 0;
    122        onStepCalled = [];
    123        var handlers = [];
    124        breakpoints.forEach(function (offset, i) {
    125            wasmScript.setBreakpoint(offset, handlers[i] = {
    126                hit: (frame) => {
    127                    assertEq(false, true);
    128                    onBreakpointCalled++;
    129                }
    130            });
    131        });
    132        dbg.onEnterFrame = function (frame) {
    133            if (frame.type != 'wasmcall') return;
    134            frame.onStep = function () {
    135                onStepCalled.push(frame.offset);
    136            };
    137 
    138            // Removing all handlers.
    139            handlers.forEach(h => wasmScript.clearBreakpoint(h));
    140        };
    141    },
    142    function ({error}) {
    143        assertEq(error, undefined);
    144        assertEq(onBreakpointCalled, 0);
    145        assertEq(onStepCalled.length, 2);
    146    }
    147 );
    148 
    149 // Checking if we can remove all breakpoints.
    150 runTest(
    151    '(module (func (nop) (nop)) (export "test" (func 0)))',
    152    function ({wasmScript, breakpoints}) {
    153        onBreakpointCalled = 0;
    154        breakpoints.forEach(function (offset, i) {
    155            wasmScript.setBreakpoint(offset, {
    156                hit: (frame) => {
    157                    assertEq(frame.offset, breakpoints[0]);
    158                    onBreakpointCalled++;
    159                    // Removing all handlers.
    160                    wasmScript.clearAllBreakpoints();
    161                }
    162            });
    163        });
    164    },
    165    function ({error}) {
    166        assertEq(error, undefined);
    167        assertEq(onBreakpointCalled, 1);
    168    }
    169 );
    170 
    171 // Checking if breakpoints are removed after debugger has been detached.
    172 runTest(
    173    '(module (func (nop) (nop)) (export "test" (func 0)))',
    174    function ({dbg, wasmScript, g, breakpoints}) {
    175        onBreakpointCalled = 0;
    176        breakpoints.forEach(function (offset, i) {
    177            wasmScript.setBreakpoint(offset, {
    178                hit: (frame) => {
    179                    onBreakpointCalled++;
    180                }
    181            });
    182        });
    183        dbg.onEnterFrame = function (frame) {
    184            dbg.removeDebuggee(g);
    185        };
    186    },
    187    function ({error}) {
    188        assertEq(error, undefined);
    189        assertEq(onBreakpointCalled, 0);
    190    }
    191 );