Skip to content

[lldb][swift] Fix unwinding of Q funclets by comparing PC to continuation ptr #9574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

felipepiovezan
Copy link

@felipepiovezan felipepiovezan commented Nov 11, 2024

Unwinding these funclets is tricky because they change, halfway through the function, the meaning of the async register. In particular, the meaning changes from:
a) "the async context to be freed" [of the async function that just finished executing]
To:
b) "the async context of the current async function" [which has just been resumed].

LLDB has no way of identifying the instruction in which this transition happened.

This patch improves the situation slightly by employing a heuristic: if the async register has a continuation pointer that is equal to the currently executing funclet, then the stop is before the transition point. This heuristic fails on some recursive async functions.

An alternative approach involves assuming we will never stop between the end of the prologue and the transition point, so that LLDB may always unwind assuming it is past the transition point if the stop is outside the prologue. This has the benefit that it should "always work", including in recursive funclets.

However, it is difficult to reason about where a debugger may stop; in fact, very often the transition point is the second instruction after the prologue, which will trigger the fail point in a common scenario. Breakpoints are often placed in the first instruction after the prologue, and any kind of step operation will first "instruction step" over the breakpoint location, placing the debugger exactly in the "incorrect unwinding" zone.

A correct implementation will likely require more guarantees from the compiler. See rdar://139676623

…tion ptr

Unwinding these funclets is tricky because they change, halfway through
the function, the meaning of the async register. In particular, the
meaning changes from:
a) "the async context to be freed" [of the async function that just
finished executing]
To:
b) "the async context of the current async function" [which has just
been resumed].

LLDB has no way of identifying the instruction in which this transition
happened.

This patch improves the situation slightly by employing a heuristic: if
the async register has a continuation pointer that is equal to the
currently executing funclet, then the stop is _before_ the transition
point. This heuristic fails on some recursive async functions.

An alternative approach involves assuming we will never stop between the
end of the prologue and the transition point, so that LLDB may always
unwind assuming it is past the transition point if the stop is outside
the prologue. This has the benefit that it should "always work",
including in recursive funclets.

However, it is difficult to reason about where a debugger may stop; in
fact, very often the transition point is the second instruction after
the prologue, which will trigger the fail point in a common scenario.
Breakpoints are often placed in the _first_ instruction after the
prologue, and any kind of step operation will first "instruction step"
over the breakpoint location, placing the debugger exactly in the
"incorrect unwinding" zone.

A correct implementation will likely require more guarantees from the
compiler. See rdar://139676623
@felipepiovezan felipepiovezan force-pushed the felipe/improve_q_funclet_unwinding branch from 62d943a to 2451d19 Compare November 11, 2024 22:51
@felipepiovezan
Copy link
Author

@swift-ci test

/// FIXME: this fails in some recursive async functions. See: rdar://139676623
static llvm::Expected<bool> IsIndirectContext(Process &process,
StringRef mangled_name,
addr_t async_reg,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of curiosity: what is this? Is it a DWARF register number stored in an addr_t?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is the contents of the async reg. I hesitated to call it "async context" because we don't know whose context this is (this is what the function is indirectly determining)

@felipepiovezan felipepiovezan merged commit 0f00a51 into swiftlang:stable/20240723 Nov 12, 2024
3 checks passed
@felipepiovezan felipepiovezan deleted the felipe/improve_q_funclet_unwinding branch November 12, 2024 04:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants