Skip to content
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

Make backtrace creation more accurate to support GC #268

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

frank-emrich
Copy link

@frank-emrich frank-emrich commented Dec 11, 2024

When creating a backtrace while running some continuation c with parent p (another continuation or the main stack), the following happens at the moment:
After traversing the stack frames of c, we need to traverse the stack frames of p. To this end, we look at the StackLimits object of p.
The last_wasm_exit_{fp, pc} fields therein tell us the frame pointer and program counter at the point where we stopped execution in p. This must have been at a resume Wasm instruction that resumed c. This FP and PC allows us to travese the stack frames of p.
Note that the fields in StackLimits are updated by the various stack switching instructions. The last_wasm_exit_{fp,pc} fields are only ever used for creating backtraces, they do not influence control flow at all.

Since #223, the last_wasm_exit_pc field in StackLimits is set on resume by using the CLIF instruction get_instruction_pointer, which I introduced specifically for that use case. This CLIF instruction will give us a PC value that will be associated with the Wasm resume instruction under consideration. This ensures that the backtraces we create mention the right Wasm instructions.

That's good enough for our current use case, where backtraces are used mostly to show where things went wrong on a trap. However, in the future, when we want to support GC, we also need to use backtraces to obtain stack maps, do identify live objects on continuation stacks. At that point, the current approach becomes too coarse: Currently, the last_wasm_exit_pc value we create is associated with the right Wasm instruction (namely, a resume), but with the wrong CLIF instruction: The PC saved in last_wasm_exit_pc will be associated with the get_instruction_pointer CLIF instruction that created it. However, logically, it must be associated with the stack_switch CLIF instruction where execution actually switched from p to c, and where it would subsequently continue if we were to switch back to the parent p. Only the stack_switch instruction will have the most recent/correct stack map describing where live values are located in the stack frame that executed resume.

This PR rectifies this situation as follows: Instead of maintaining the fields last_wasm_exit_{fp, pc} in StackLimits, we now load the required PC and FP values directly from the control context of the corresponding Fiber (i.e., the memory area that saves PC, SP, FP used to actually switch stacks). In other words, to create backtraces, we get the necessary information about where execution continues in the parent stack directly from the source of truth (i.e., the control context), rather than duplicating this information in StackLimits just for backtrace creation.

Thus, the fields last_wasm_exit_{fp, pc} are removed from StackLimits and no longer need to be updated on resume. Further, the get_instruction_pointer CLIF instruction, and corresponding GetRip x64 MInst, which I introduced in #223, are removed.

@frank-emrich frank-emrich requested a review from dhil December 11, 2024 20:41
@frank-emrich frank-emrich force-pushed the get-backtrace-info-from-fiber branch from 45fe008 to 3438f5d Compare December 18, 2024 14:44
@frank-emrich
Copy link
Author

ping @dhil

Copy link
Member

@dhil dhil left a comment

Choose a reason for hiding this comment

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

LGTM. Nice.

@dhil
Copy link
Member

dhil commented Dec 30, 2024

ping @dhil

Apologies. I was offline during the past few weeks.

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