Skip to content

Stop backtracing if the stack pointer gets stuck #135804

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions library/std/src/sys/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,21 @@ unsafe fn _print_fmt(fmt: &mut fmt::Formatter<'_>, print_fmt: PrintFmt) -> fmt::
let mut res = Ok(());
let mut omitted_count: usize = 0;
let mut first_omit = true;
let mut last_sp = core::ptr::null_mut();
let mut sp_stuck = false;
// 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!

sp_stuck = true;
return false;
}

if print_fmt == PrintFmt::Short && idx > MAX_NB_FRAMES {
return false;
}
Expand Down Expand Up @@ -126,11 +135,15 @@ unsafe fn _print_fmt(fmt: &mut fmt::Formatter<'_>, print_fmt: PrintFmt) -> fmt::
}

idx += 1;
last_sp = frame.sp();
res.is_ok()
})
};
res?;
bt_fmt.finish()?;
if sp_stuck {
writeln!(fmt, "note: stack pointer stuck, further frames are omitted")?;
}
if print_fmt == PrintFmt::Short {
writeln!(
fmt,
Expand Down
Loading