debugger icon indicating copy to clipboard operation
debugger copied to clipboard

`this` gets global object instead of thisValue

Open Loirooriol opened this issue 9 years ago • 12 comments

  1. Open browser console
  2. Open browser toolbox's debugger
  3. Search /server/actors/object.js file (resource://devtools/server/actors/object.js)
  4. Add a breakpoint just inside ObjectActor.prototype.grip (e.g. line 76)
  5. In the browser console, enter []
  6. The debugger pauses at the breakpoint.
  7. Add a new watch expression: this
  8. Switch the browser toolbox to the console panel
  9. Enter this.

Result: the global Sandbox object. Expected: the this value of that function.

Loirooriol avatar Oct 07 '17 21:10 Loirooriol

/claim

bomsy avatar Oct 24 '17 08:10 bomsy

Thanks for claiming the issue! :wave:

Here are some links for getting setup, contributing, and developing. We're always happy to answer questions in slack! If you become busy, feel free to /unclaim it.

:fox_face: Debugger team!

claim avatar Oct 24 '17 08:10 claim

A good STR

  • Go to https://satin-fender.glitch.me/
  • In the debugger open client.js
  • Add a breakpoint to line 10
  • refresh the browser
  • After the breakpoint hits
  • Add this to the watch expression list

AR:this is the global Window object ER: this should be the instance object a

bomsy avatar Oct 30 '17 21:10 bomsy

This looks like a bug in the Debugger API to me. We're definitely evaluating try { this } catch (e) { e } in the frame for Test::run, and getting the global window.

I changed the code around the call to evalWithBindings in devtools/server/actors/webconsole.js` to read:

    if (frame) {
      dump("JIMB: frame.evalwithBindings(" + uneval(string) + ")\n");
      dump("JIMB: frame: " + describeDF(frame) + "\n");
      result = frame.evalWithBindings(string, bindings, evalOptions);
      if (result.return) {
        dump("JIMB: result.return: " + describeDV(result.return));
      } else if (result.throw) {
        dump("JIMB: result.throw: " + describeDV(result.throw));
      }
    } else {
      dump("JIMB: dbgWindow.executeInGlobalWithBindings(" + uneval(string) + ")\n");
      result = dbgWindow.executeInGlobalWithBindings(string, bindings, evalOptions);

(imagine appropriate definitions for describeDV and describeDF) and got the following output:

DBG-SERVER: Received packet 105: {
  "to": "server1.conn0.child1/consoleActor2",
  "type": "evaluateJS",
  "text": "try {\n this\n} catch (e) {\n e\n}",
  "frameActor": "server1.conn0.child1/frame38"
}
JIMB: frame.evalwithBindings("try {\n this\n} catch (e) {\n e\n}")
JIMB: frame: [Debugger.Frame callee=[Debugger.Object class=Function displayName=run]
              this=[Debugger.Object class=Object]]
JIMB: result.return: [Debugger.Object class=Window]
DBG-SERVER: Received packet 107: {
  "from": "server1.conn0.child1/consoleActor2",
  "input": "try {\n this\n} catch (e) {\n e\n}",
  "result": {
    "type": "object",
    "actor": "server1.conn0.child1/obj1049",
    "class": "Window",
    "extensible": true,
    "frozen": false,
    "sealed": false,
    "ownPropertyLength": 816,
    "preview": {
      "kind": "ObjectWithURL",
      "url": "https://satin-fender.glitch.me/"
    }
  },
  "timestamp": 1509743672917,
  "exception": null,
  "frame": null,
  "helperResult": null,
  "notes": null
}

Note that the this value fetched directly off the frame is correct, while the result of the eval is wrong.

jimblandy avatar Nov 03 '17 21:11 jimblandy

it would be nice to reproduce this issue in the console as well. as in, when paused does the same behavior occur there too. I would bet 💸 that it does

jasonLaster avatar Nov 04 '17 00:11 jasonLaster

Yeah I tested in the console, when paused. We get the same thing.

bomsy avatar Nov 04 '17 00:11 bomsy

/unclaim

bomsy avatar Nov 07 '17 02:11 bomsy

Done :+1:

claim avatar Nov 07 '17 02:11 claim

I had a chat about #4324 (this gets global object instead of thisValue) with Ted Campbell, who works on the JavaScript engine.

Summary: it seems likely that js::IsExtensibleLexicalEnvironment might be mishandling debugger environments. https://searchfox.org/mozilla-central/source/js/src/vm/EnvironmentObject.h#1073-1078

However, this is one instance of a larger class of bugs that result from SpiderMonkey using DebugEnvironmentProxy objects as environment objects, even though they're not subclasses of DebuggerEnvironment: https://searchfox.org/mozilla-central/rev/bab833ebeef6b2202e71f81d225b968283521fd6/js/src/vm/EnvironmentObject.h#893

In other words, most code in SpiderMonkey really wishes it were talking to some common superclass of debug environments and real environments, but there's no such type, so everyplace needs to have an if to handle both cases - and that if is often just missing.

So there's a short-term fix: change the behavior of IsExtensibleLexicalEnvironment, and maybe some related functions.

And then there's the longer-term fix: actually introduce a class which can serve as the common supertype of all the environments SpiderMonkey ought to deal with. In other words, make the types reflect reality, so you can trust them.

That's probably going to entail changing DebugEnvironmentProxy to no longer be a proxy object - which I think will be okay, and perhaps even good.

jasonLaster avatar Nov 14 '17 21:11 jasonLaster

Reduced test case, usable in JS shell:

let g = newGlobal();
g.target = this;

g.evaluate(`
    let d = new Debugger;
    let gw = d.addDebuggee(target);

    d.onDebuggerStatement = function(frame)
    {
        frame = frame.older;

        let res = frame.eval("this");
        print(res.return === frame.this);
    }
    `);

function brk() { debugger; }

function bar() {
    let reversed = "stringstring";
    brk();
}

bar.call({});

jimblandy avatar Nov 16 '17 21:11 jimblandy

JS engine work is occurring in bug 1397049.

jimblandy avatar Nov 16 '17 21:11 jimblandy

@jimblandy closed this issue as it's being tracked elsewhere

jasonLaster avatar Nov 16 '17 23:11 jasonLaster