Closed Bug 2075356 Opened 2 months ago Closed 1 month ago

Console eval while paused in a debugger frame mutates the outer lexical binding instead of shadowing it

Categories

(DevTools :: Console, defect, P3)

Firefox 158
defect

Tracking

(firefox161 fixed)

RESOLVED FIXED
161 Branch
Tracking Status
firefox161 --- fixed

People

(Reporter: cemase, Assigned: cemase)

References

Details

Attachments

(1 external file, 1 obsolete file)

User Agent: Mozilla/5.0 (X11; Linux x86_64; rv:158.0) Gecko/20100101 Firefox/158.0

Steps to reproduce

I frist loaded a page with:

   <!DOCTYPE html>
   <html>
   <body>
   <script>
   let outerX = 2;
   function readOuterX() { return outerX; }
   function inner() {
     var frameLocal = 42;
     debugger;
   }
   </script>
   </body>
   </html>
  • then in the Console, ran inner();, and execution paused on debugger;.
  • In the Debugger panel, confirmed the call stack shows inner.
  • then back in the Console, while still paused, ran this below in order:
   frameLocal                -> 42
   readOuterX()               -> 2
   let outerX = 3; outerX;    -> 3
   readOuterX()
   outerX

Actual results

readOuterX() returned 3, and outerX returned 3. The let outerX = 3
evaluated in the console reached the global lexical environment and
overwrote the existing binding, rather than creating a new binding scoped
to the frame.

Reproduces identically in:

  • unpatched Nightly (158.0a1)
  • Nightly with the patch from bug 1257088 / D326431 applied

So this is pre-existing, not introduced by that patch.

Expected results

readOuterX() should have returned 2 (unchanged), because the let outerX = 3 in the console should shadow the global outerX for the
duration of the paused frame, not replace it. Chrome DevTools shows this
correct behavior: readOuterX() returns 2 after the same steps, since it
layers a fresh lexical scope over the frame instead of writing through to
the outer binding.

I've just confirmed the engine shadows this correctly

var g = newGlobal({ newCompartment: true });
var dbg = new Debugger;
var gw = dbg.addDebuggee(g);

gw.executeInGlobal(`
  let outerX = 2;
  function readOuterX() { return outerX; }
  function inner() { var frameLocal = 42; debugger; }
`);

dbg.onDebuggerStatement = function (frame) {
  print("r1 =", frame.eval(`let outerX = 3; outerX;`).return);
  print("r2 =", frame.eval(`readOuterX();`).return);
  print("r3 =", frame.eval(`frameLocal`).return);
};
g.eval(`inner();`);

results

lochipi/firefox$ ./obj-gecko/dist/bin/js /tmp/repro.js
r1 = 3
r2 = 2
r3 = 42

Looking into the web console.

the web console is also working correctly;
for reference, I added dumps to trace how that flows:

// devtools/server/actors/webconsole/eval-with-debugger.js

function getFrameDbg(options, webConsole) {
  dump(`getFrameDbg: frameActor=${options.frameActor}\n`);
  ...
}

// in evalWithDebugger, after getFrameDbg():
dump(`evalWithDebugger: frame=${!!frame}\n`);

// in evalWithDebugger, before getEvalResult():
dump(
  `evalWithDebugger before getEvalResult: frame=${!!frame} ` +
    `allowRedeclare=${!!evalOptions.allowRedeclaringExistingLexicalBinding} ` +
    `useInnerBindings=${!!evalOptions.useInnerBindings}\n`
);

// at the top of getEvalResult():
dump(
  `getEvalResult ENTER: frame=${!!frame} bindings=${!!bindings} ` +
    `allowRedeclare=${!!evalOptions.allowRedeclaringExistingLexicalBinding} ` +
    `useInnerBindings=${!!evalOptions.useInnerBindings}\n`
);

and the output:

getFrameDbg: frameActor=server0.conn0.watcher2.process5//frame21
evalWithDebugger: frame=true
evalWithDebugger before getEvalResult: frame=true allowRedeclare=false useInnerBindings=false
getEvalResult ENTER: frame=true bindings=true allowRedeclare=false useInnerBindings=false

digging more into it.

I think I found the problem: there's a client-side transformation in the debugger's getMappedExpression that rewrites top-level let declarations into plain assignments before the eval reaches the server, so let outerX = 3 becomes outerX = 3 and writes through to the outer binding.

I'm pushing the fix in a few once I've verified the test pass, and it doesn't affect other parts.

/**
 * Gets information about original variable names from the source map
 * and replaces all possible generated names.
 */
export function getMappedExpression(expression, thread, thunkArgs) {
  const { getState, parserWorker } = thunkArgs;
  const mappings = getSelectedScopeMappings(getState(), thread);
  const bindings = getSelectedFrameBindings(getState(), thread);

  // We bail early if we do not need to map the expression. This is important
  // because mapping an expression can be slow if the parserWorker
  // worker is busy doing other work.
  //
  // 1. there are no mappings - we do not need to map original expressions
  // 2. does not contain `await` - we do not need to map top level awaits
  // 3. does not contain `=` - we do not need to map assignments
  const shouldMapScopes = isMapScopesEnabled(getState()) && mappings;
  if (!shouldMapScopes && !expression.match(/(await|=)/)) {
    return null;
  }

  return parserWorker.mapExpression(
    expression,
    mappings,
    bindings || [],
    features.mapExpressionBindings && getIsPaused(getState(), thread),
    features.mapAwaitExpression
  );
}

getMappedExpression runs the binding mapper whenever the expression
contains =, even without scope mappings. That will always turn
let x = 1 into x = 1, and declaration in a paused frame mutates the
outer binding instead of shadowing it. I've redirected the guard to
await only.

Adds a test that pauses in a frame and checks that a let/const declaration
shadows the outer binding instead of mutating it.

Assignee: nobody → cemase
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true
Attachment #9651101 - Attachment is obsolete: true
Severity: -- → S3
Priority: -- → P3
Pushed by nchevobbe@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/d1012b82661e https://hg.mozilla.org/integration/autoland/rev/28495510ab51 Don't rewrite let/const to assignments for console eval in frames r=devtools-reviewers,nchevobbe
Regressions: 2079848
Pushed by amarc@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/e51ef44b0b15 https://hg.mozilla.org/integration/autoland/rev/5851c9ac6516 Revert "Bug 2075356 - Don't rewrite let/const to assignments for console eval in frames r=devtools-reviewers,nchevobbe" for causing dt failures @ rowser_dbg-console-map-bindings.js

Backed out for causing dt failures

Flags: needinfo?(cemase)

thanks for the context! the patch scoped this too broad, and I missed the var rewrite.

I'll pick it up tomorrow and also check what's wrong with the timeout for redeclaration(bug 1257088)
(this seems to be intermittent, as it was passing, and I've just confirmed this locally - but I'll still look into it)

Flags: needinfo?(cemase)
Pushed by nchevobbe@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/16d68d4b3415 https://hg.mozilla.org/integration/autoland/rev/d3bd9bab5478 Don't rewrite let/const to assignments for console eval in frames r=devtools-reviewers,nchevobbe
Regressions: 2088782
Status: ASSIGNED → RESOLVED
Closed: 1 month ago
Resolution: --- → FIXED
Target Milestone: --- → 161 Branch
QA Whiteboard: [qa-triage-done-c162/b161]
Regressions: 2091640
No longer regressions: 2091640
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: