tor-browser

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

Debugger-onNativeCall-01.js (2058B)


      1 // Test that the onNativeCall hook is called when expected.
      2 
      3 load(libdir + 'eqArrayHelper.js');
      4 
      5 var g = newGlobal({newCompartment: true});
      6 var dbg = Debugger(g);
      7 var gdbg = dbg.addDebuggee(g);
      8 
      9 g.eval(`
     10 const x = [];
     11 Object.defineProperty(x, "a", {
     12  get: print,
     13  set: print,
     14 });
     15 function f() {
     16  x.a++;
     17  x.push(4);
     18 }
     19 `);
     20 
     21 for (let i = 0; i < 5; i++) {
     22  g.f();
     23 }
     24 
     25 const rv = [];
     26 dbg.onNativeCall = (callee, reason) => { rv.push(callee.name, reason); };
     27 
     28 var dbg2 = Debugger(g);
     29 var gdbg2 = dbg2.addDebuggee(g);
     30 
     31 const fscript = gdbg.getOwnPropertyDescriptor('f').value.script;
     32 
     33 for (let i = 0; i < 5; i++) {
     34  // The onNativeCall hook is called when doing global evaluations.
     35  rv.length = 0;
     36  gdbg.executeInGlobal(`f()`);
     37  assertEqArray(rv, ["print", "get", "print", "set", "push", "call"]);
     38 
     39  // The onNativeCall hook is called when doing frame evaluations.
     40  let handlerCalled = false;
     41  const handler = {
     42    hit(frame) {
     43      fscript.clearBreakpoint(handler);
     44      rv.length = 0;
     45      frame.eval(`f()`);
     46      assertEqArray(rv, ["print", "get", "print", "set", "push", "call"]);
     47      handlerCalled = true;
     48    },
     49  };
     50  fscript.setBreakpoint(fscript.mainOffset, handler);
     51  g.f();
     52  assertEq(handlerCalled, true);
     53 
     54  // The onNativeCall hook is also called when not in a debugger evaluation.
     55  rv.length = 0;
     56  g.f();
     57  assertEqArray(rv, ["print", "get", "print", "set", "push", "call"]);
     58 
     59  // The onNativeCall hook is *not* called when in a debugger evaluation
     60  // associated with a different debugger using exclusiveDebuggerOnEval.
     61  rv.length = 0;
     62  dbg2.exclusiveDebuggerOnEval = true;
     63  assertEq(dbg2.exclusiveDebuggerOnEval, true);
     64  gdbg2.executeInGlobal(`f()`);
     65  assertEqArray(rv, []);
     66 
     67  // The onNativeCall hook is called when that same distinct debugger
     68  // doesn't have the exclusiveDebuggerOnEval flag set to true
     69  rv.length = 0;
     70  dbg2.exclusiveDebuggerOnEval = false;
     71  assertEq(dbg2.exclusiveDebuggerOnEval, false);
     72  gdbg2.executeInGlobal(`f()`);
     73  assertEqArray(rv, ["print", "get", "print", "set", "push", "call"]);
     74 }