Console eval while paused in a debugger frame mutates the outer lexical binding instead of shadowing it
Categories
(DevTools :: Console, defect, P3)
Tracking
(firefox161 fixed)
| 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 ondebugger;. - 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.
| Assignee | ||
Comment 1•2 months ago
|
||
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.
| Assignee | ||
Comment 2•2 months ago
|
||
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.
| Assignee | ||
Comment 3•2 months ago
|
||
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
);
}
| Assignee | ||
Comment 4•2 months ago
|
||
| Assignee | ||
Comment 5•2 months ago
|
||
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.
Updated•2 months ago
|
Updated•2 months ago
|
Updated•1 month ago
|
Backed out for causing dt failures
there s also this tv failure -> https://treeherder.mozilla.org/logviewer?job_id=598495620&repo=autoland&task=CFLJRN8wReCzOj1JYEEjow.0
| Assignee | ||
Comment 10•1 month ago
|
||
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)
Comment 11•1 month ago
|
||
Comment 12•1 month ago
|
||
| bugherder | ||
Updated•25 days ago
|
Description
•