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

Stop backtracing if the stack pointer gets stuck #135804

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ChrisDenton
Copy link
Member

@ChrisDenton ChrisDenton commented Jan 21, 2025

If the stack pointer does not make progress when backtracing then something has gone wrong and we should just stop rather than potentially continuing forever.

Workaround for #135717

@rustbot
Copy link
Collaborator

rustbot commented Jan 21, 2025

r? @joboet

rustbot has assigned @joboet.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 21, 2025
@ChrisDenton ChrisDenton linked an issue Jan 21, 2025 that may be closed by this pull request
@bjorn3
Copy link
Member

bjorn3 commented Jan 21, 2025

Actually this wouldn't really work with something like stacker as the new stack segment may have a lower address than the old one. Maybe this check could be only done if the jump is less than say 1MB? Also there are architectures where the stack grows up rather than down. For those the check needs to be reversed.

@ChrisDenton
Copy link
Member Author

Hm, the implementation here doesn't really care which way the stack pointer moves so long as it does move. I.e. it just checks that the stack pointer is not equal the previous one.

@bjorn3
Copy link
Member

bjorn3 commented Jan 21, 2025

Right, in that case it should be fine.

Copy link
Member

@joboet joboet left a comment

Choose a reason for hiding this comment

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

This looks good, I just have one question and one nit.

r=me if you're satisfied

library/std/src/sys/backtrace.rs Show resolved Hide resolved
library/std/src/sys/backtrace.rs Outdated Show resolved Hide resolved
// If we're using a short backtrace, ignore all frames until we're told to start printing.
let mut print = print_fmt != PrintFmt::Short;
set_image_base();
// SAFETY: we roll our own locking in this town
unsafe {
backtrace_rs::trace_unsynchronized(|frame| {
// Break if the stack pointer does not move (see #135717).
// Make sure to skip the first frame to handle the case where the frame pointer is omitted.
if frame.sp() == last_sp && !frame.sp().is_null() && idx > 1 {
Copy link
Member

Choose a reason for hiding this comment

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

How do "inline frames" get reported? Eg, if fn a calls fn b and b is inlined into a, then I could see the stack pointer for the "inline frame" b having the same address as frame a's stack pointer if a panic were to happen inside b.

Copy link
Member Author

@ChrisDenton ChrisDenton Jan 21, 2025

Choose a reason for hiding this comment

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

This is only for the trace itself so inline frames shouldn't be handled until symbolization, no? (i.e. resolve_frame_unsynchronized below this code)

Copy link
Member

Choose a reason for hiding this comment

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

I could be totally wrong, but I seem to recall that dbghelp.dll would sometimes report inlined frames as if they were actual frames (perhaps only on i686 or something like that?)

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh hm the old StackWalk64 API could be a problem, yes. I'll investigate.

Copy link
Member Author

Choose a reason for hiding this comment

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

Looking at our backtrace code, it seems that we do expect to always handle inline frames in symbolization. There's even a fallback for StackWalk64's lack of InlineFrameContext https://github.com/rust-lang/backtrace-rs/blob/016f80ae2179fdd8479db179cf47ed16a1198422/src/symbolize/dbghelp.rs#L160). I would assume that would have very weird results were inline frames to be reported as actual frames. Though I've not yet been able to find anything conclusive.

Copy link
Member

Choose a reason for hiding this comment

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

I think I implemented part of that last year in rust-lang/backtrace-rs#569. Maybe I'm just remembering something I ran into during development of that patch. If your test case works ok on i686 with inlining happening, then I don't have any concerns with this change 🙂

Copy link
Member Author

Choose a reason for hiding this comment

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

In my testing I've not been able to provoke any problems but I'll see what the full CI says. It is entirely possible there's a situation I'm not accounting for.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, so I just realised I was testing either with full debug info or no debug info but not with line-tables-only. This does show an issue on i686. I'll investigate further.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah I need to track both AddrStack and InlineFrameContext and only stop if both are the same. Which will need me to come up with a backtrace API for this.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, that sounds correct to me!

@ChrisDenton
Copy link
Member Author

Given potential concerns, I'll mark this as not to be rolled up.

@bors r=joboet rollup=never

@bors
Copy link
Contributor

bors commented Jan 21, 2025

📌 Commit f39c4c8 has been approved by joboet

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 21, 2025
@ChrisDenton
Copy link
Member Author

@bors r-

I think I may have found an issue,

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Infinite/long backtrace on i686 under Wine
6 participants