Skip to content

Commit d1238c8

Browse files
micleo2meta-codesync[bot]
authored andcommitted
Graft "[SH] Fix debug info source location leakage of generator prologue"
Summary: Setting a breakpoint inside a generator and inspecting variables returned nothing -- hdb `exec` evaluated to `undefined` and the CDP Scope panel showed only the global scope -- while the identical code in a normal function worked. Root cause: LowerGeneratorFunction builds the generator's state machine resume prologue (parameter loads, generator_state checks, and the resume dispatch switch) using an IRBuilder whose source location had leaked from previously processed user instructions, namely the last `yield`. Those synthetic entry instructions sit at the lowest bytecode offsets and run on every resume, yet carry no scoping info. Because they claimed a real user line, a breakpoint on that line resolved to the scope-less function entry instead of the actual user code, so the debugger found no lexical scope and reported no variables. Only the line matching the last yield broke; every other line resolved to genuine user code. Fix: stamp the synthetic resume/dispatch scaffolding with the inner function's own declaration location (as a normal function's prologue already does) rather than the leaked user line, at the three entry/resume-path creation sites. Breakpoints on user lines now resolve to real user code with intact scoping info, restoring variable inspection in both hdb (`exec`, `info variables`) and CDP (the DevTools Scope panel). Grafted 6a9118a0b5cc1d630d1cdf4b3fceee60dda560ae (D114104972) - Grafted xplat/static_h to xplat/shermes/stable Reviewed By: lavenzg Differential Revision: D115460651 fbshipit-source-id: 0b79bce3c5efbb34996af53f8767937f5f94681d
1 parent 063b094 commit d1238c8

5 files changed

Lines changed: 85 additions & 21 deletions

File tree

‎lib/Optimizer/Scalar/LowerGeneratorFunction.cpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ void LowerToStateMachine::convert() {
200200
auto *valueParam =
201201
builder_.createJSDynamicParam(inner_, builder_.createIdentifier("value"));
202202
movePastFirstInBlock(builder_, &*inner_->begin());
203+
// Reset the leaked user source location so the synthetic resume prologue is
204+
// not mistaken for real user code (e.g. a breakpoint on that line resolving
205+
// here). Use the inner function's own declaration location rather than "no
206+
// location": it matches a normal function's prologue and the sibling
207+
// scaffolding (getParentOuterScope_), and gives the executing-generator
208+
// TypeError thrown from this entry dispatch a real line in stack traces
209+
// rather than a raw-address fallback.
210+
builder_.setLocation(inner_->getSourceRange().Start);
203211
auto *loadActionParam = builder_.createLoadParamInst(actionParam);
204212
auto *loadValueParam = builder_.createLoadParamInst(valueParam);
205213

@@ -614,6 +622,9 @@ void LowerToStateMachine::lowerToSwitch(
614622
auto *throwBecauseExecutingBB = builder_.createBasicBlock(inner_);
615623
auto *checkIfCompletedBB = builder_.createBasicBlock(inner_);
616624
builder_.setInsertionBlock(newBeginBB);
625+
// Don't leak a user source line onto the synthetic entry dispatch; this
626+
// location persists through the scaffolding created below.
627+
builder_.setLocation(inner_->getSourceRange().Start);
617628

618629
auto *loadState =
619630
builder_.createLoadFrameInst(getParentOuterScope_, genState);
@@ -851,6 +862,9 @@ void LowerToStateMachine::lowerToSwitch(
851862
}
852863

853864
builder_.setInsertionBlock(userCodeSwitchBB);
865+
// The user-block loop above left a user source line on the builder; reset it
866+
// so the synthetic dispatch/catch scaffolding isn't taken for user code.
867+
builder_.setLocation(inner_->getSourceRange().Start);
854868
builder_.createStoreFrameInst(
855869
getParentOuterScope_,
856870
builder_.getLiteralNumber((uint8_t)State::Executing),

‎test/BCGen/HBC/async-function.js‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ var simpleAsyncFE = async function () {
273273
// CHECK-NEXT:2: start = L3, end = L5, target = L5
274274

275275
// CHECK:Function<?anon_0_simpleAwait>(1 params, 21 registers, 0 numbers, 0 non-pointers):
276-
// CHECK-NEXT:Offset in debug table: source 0x003d
276+
// CHECK-NEXT:Offset in debug table: source 0x0045
277277
// CHECK-NEXT: LoadParam r3, 2
278278
// CHECK-NEXT: LoadParam r4, 1
279279
// CHECK-NEXT: GetParentEnvironment r2, 0
@@ -403,7 +403,7 @@ var simpleAsyncFE = async function () {
403403
// CHECK-NEXT:4: start = L6, end = L8, target = L8
404404

405405
// CHECK:Function<?anon_0_simpleAsyncFE>(1 params, 21 registers, 0 numbers, 0 non-pointers):
406-
// CHECK-NEXT:Offset in debug table: source 0x005c
406+
// CHECK-NEXT:Offset in debug table: source 0x0064
407407
// CHECK-NEXT: LoadParam r3, 2
408408
// CHECK-NEXT: LoadParam r4, 1
409409
// CHECK-NEXT: GetParentEnvironment r2, 0
@@ -553,23 +553,27 @@ var simpleAsyncFE = async function () {
553553
// CHECK-NEXT: 0x0028 function idx 3, starts at line 19 col 21
554554
// CHECK-NEXT: bc 33: line 19 col 21
555555
// CHECK-NEXT: 0x0030 function idx 7, starts at line 10 col 1
556+
// CHECK-NEXT: bc 43: line 10 col 1
556557
// CHECK-NEXT: bc 121: line 11 col 3
557-
// CHECK-NEXT: 0x003d function idx 8, starts at line 14 col 1
558-
// CHECK-NEXT: bc 43: line 15 col 11
558+
// CHECK-NEXT: bc 157: line 10 col 1
559+
// CHECK-NEXT: bc 178: line 10 col 1
560+
// CHECK-NEXT: bc 220: line 10 col 1
561+
// CHECK-NEXT: 0x0045 function idx 8, starts at line 14 col 1
562+
// CHECK-NEXT: bc 43: line 14 col 1
559563
// CHECK-NEXT: bc 131: line 16 col 3
560564
// CHECK-NEXT: bc 150: line 15 col 11
561565
// CHECK-NEXT: bc 169: line 15 col 11
562566
// CHECK-NEXT: bc 255: line 15 col 11
563-
// CHECK-NEXT: bc 291: line 15 col 11
564-
// CHECK-NEXT: bc 312: line 15 col 11
565-
// CHECK-NEXT: bc 354: line 15 col 11
566-
// CHECK-NEXT: 0x005c function idx 9, starts at line 19 col 21
567-
// CHECK-NEXT: bc 43: line 20 col 11
567+
// CHECK-NEXT: bc 291: line 14 col 1
568+
// CHECK-NEXT: bc 312: line 14 col 1
569+
// CHECK-NEXT: bc 354: line 14 col 1
570+
// CHECK-NEXT: 0x0064 function idx 9, starts at line 19 col 21
571+
// CHECK-NEXT: bc 43: line 19 col 21
568572
// CHECK-NEXT: bc 131: line 21 col 3
569573
// CHECK-NEXT: bc 150: line 20 col 11
570574
// CHECK-NEXT: bc 169: line 20 col 11
571575
// CHECK-NEXT: bc 255: line 20 col 11
572-
// CHECK-NEXT: bc 291: line 20 col 11
573-
// CHECK-NEXT: bc 312: line 20 col 11
574-
// CHECK-NEXT: bc 354: line 20 col 11
575-
// CHECK-NEXT: 0x007b end of debug source table
576+
// CHECK-NEXT: bc 291: line 19 col 21
577+
// CHECK-NEXT: bc 312: line 19 col 21
578+
// CHECK-NEXT: bc 354: line 19 col 21
579+
// CHECK-NEXT: 0x0083 end of debug source table

‎test/BCGen/HBC/es6/generator.js‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ function *args() {
410410
// CHECK-NEXT: bc 23: line 10 col 1
411411
// CHECK-NEXT: bc 36: line 10 col 1
412412
// CHECK-NEXT: 0x0011 function idx 3, starts at line 10 col 1
413-
// CHECK-NEXT: bc 43: line 13 col 5
413+
// CHECK-NEXT: bc 43: line 10 col 1
414414
// CHECK-NEXT: bc 114: line 12 col 10
415415
// CHECK-NEXT: bc 138: line 13 col 5
416416
// CHECK-NEXT: bc 157: line 13 col 5
@@ -419,17 +419,17 @@ function *args() {
419419
// CHECK-NEXT: bc 284: line 13 col 14
420420
// CHECK-NEXT: bc 294: line 13 col 12
421421
// CHECK-NEXT: bc 312: line 13 col 5
422-
// CHECK-NEXT: bc 352: line 13 col 5
423-
// CHECK-NEXT: bc 373: line 13 col 5
424-
// CHECK-NEXT: bc 415: line 13 col 5
422+
// CHECK-NEXT: bc 352: line 10 col 1
423+
// CHECK-NEXT: bc 373: line 10 col 1
424+
// CHECK-NEXT: bc 415: line 10 col 1
425425
// CHECK-NEXT: 0x003c function idx 4, starts at line 18 col 1
426-
// CHECK-NEXT: bc 43: line 19 col 3
426+
// CHECK-NEXT: bc 43: line 18 col 1
427427
// CHECK-NEXT: bc 119: line 20 col 1
428428
// CHECK-NEXT: bc 134: line 19 col 3
429429
// CHECK-NEXT: bc 153: line 19 col 3
430430
// CHECK-NEXT: bc 219: line 19 col 18
431431
// CHECK-NEXT: bc 237: line 19 col 3
432-
// CHECK-NEXT: bc 277: line 19 col 3
433-
// CHECK-NEXT: bc 298: line 19 col 3
434-
// CHECK-NEXT: bc 340: line 19 col 3
432+
// CHECK-NEXT: bc 277: line 18 col 1
433+
// CHECK-NEXT: bc 298: line 18 col 1
434+
// CHECK-NEXT: bc 340: line 18 col 1
435435
// CHECK-NEXT: 0x005e end of debug source table
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
// RUN: %hdb --Xes6-block-scoping %s < %s.debug | %FileCheck --match-full-lines %s
9+
// REQUIRES: debugger
10+
11+
// Regression test: a breakpoint on a yield line must resolve to real user
12+
// code, not the generator's synthetic resume prologue. The lowering pass used
13+
// to stamp the prologue (which runs at the lowest offsets on every resume) with
14+
// the source line of the last yield, so a breakpoint on that line paused at the
15+
// scope-less function entry and no variables were visible.
16+
//
17+
// Uses `info variables` (not `exec`) and sets no breakpoint after any eval, to
18+
// avoid an unrelated stale-eval-module issue in breakpoint resolution.
19+
(function () {
20+
function* gen() {
21+
let outer = 20;
22+
for (let i = 0; i < 2; i++) {
23+
let v3 = 30;
24+
yield i;
25+
yield v3 + outer; // Breakpoint set on this line (the last yield).
26+
}
27+
}
28+
let g = gen();
29+
debugger;
30+
g.next();
31+
g.next();
32+
g.next();
33+
})();
34+
// CHECK: Break on 'debugger' statement in {{.*}}
35+
// CHECK-NEXT: Set breakpoint {{[0-9]+}} at {{.*}}:25:7
36+
// CHECK-NEXT: Continuing execution
37+
// CHECK-NEXT: Break on breakpoint {{[0-9]+}} in gen: {{.*}}:25:7
38+
// The scope must be populated with the in-scope variables at the yield.
39+
// CHECK-DAG: {{.*}}v3 = 30
40+
// CHECK-DAG: {{.*}}outer = 20
41+
// CHECK-DAG: {{.*}}i = 0
42+
// CHECK: Continuing execution
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
break 25
2+
continue
3+
info variables
4+
continue

0 commit comments

Comments
 (0)