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

fix: make unwind2 more resilient #306

Merged
merged 3 commits into from
Feb 21, 2025
Merged

fix: make unwind2 more resilient #306

merged 3 commits into from
Feb 21, 2025

Conversation

JonasKruckenberg
Copy link
Owner

This PR makes the stack walking implementation of unwind2 more resilient and useful outside of panics. The big fix here is that apparently leaf functions do not spill their return address to the stack instead keeping it in register and EH_FRAME has no explicit instructions on how to recover that. At least according to LLVMs libunwind
https://github.com/llvm/llvm-project/blob/6de5d1e46d1812de2bbbbe8d8d2c811e4d16acbe/libunwind/src/DwarfInstructions.hpp#L295-L296
Kind of annoyed that this was so obscure and difficult to find :/

In the way we previously used stack walking this was fine since save_context always explicitly pushed the return address onto the stack:

add sp, sp, -0x210
.cfi_def_cfa_offset 0x210
sd ra, 0x200(sp)
.cfi_offset ra, -16

In essence when called through begin_unwind there never was a leaf function that would show this behavior.

But now with this fixed we can finally construct stack traces from inside the kernels teap handler too! (In the trap handler we don't control where the return address lives, so need to deal with it potentially not being on stack at all).

This change fixes the stack walking code which previously overly relied on the particular construction of `with_context`. It is now more correct and more versatile allowing consumers to constructs stack traces from arbitrary register+pc contexts.
@JonasKruckenberg JonasKruckenberg merged commit a5b6a92 into main Feb 21, 2025
16 checks passed
@JonasKruckenberg JonasKruckenberg deleted the jonas/fix/unwind2 branch February 22, 2025 09:26
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.

1 participant