Skip to content

Fix backtraces with -C panic=abort on linux; emit unwind tables by default #143613

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

Conversation

Enselic
Copy link
Member

@Enselic Enselic commented Jul 7, 2025

The linux backtrace unwinder relies on unwind tables to work properly. The default panic hook prints backtraces. Backtraces with -C panic=abort used to work in Rust 1.22 but broke in Rust 1.23, because in 1.23 we stopped emitting unwind tables with -C panic=abort (see #45031 and #81902 (comment)).

In 1.45 (see #69984) a workaround in the form of -C force-unwind-tables=yes was added.

More history

-C panic=abort was added in Rust 1.10 and the motivation was binary size and compile time. But given how confusing that behavior has turned out to be, it is better to make binary size optimzation opt-in with -C force-unwind-tables=no rather than default since the current default breaks backtraces.

Besides, if binary size is a primary concern, there are many other tricks that can be used that has a higher impact.

TODO

  • check if all tests pass
  • more tests?
  • better commit message
  • Update comment in must_emit_unwind_tables()
  • Update comment in tests/run-make/panic-abort-eh_frame/rmake.rs
  • sufficient to change this in compiler/rustc_target/src/spec/base/linux.rs ?
  • figure out how to present this in release notes
  • FCP probably

Closes

Closes #81902 which is regression-from-stable-to-stable
Closes #94815

Otherwise backtraces (in e.g. the default panic hook) are incomplete.

More details

The linux backtrace unwinder relies on unwind tables to work properly.
The default panic hook prints backtraces. Backtraces with
`-C panic=abort` used to work in Rust 1.22 but broke in Rust 1.23, because
in 1.23 we stopped emitting unwind tables with `-C panic=abort` (see
24cc38e).

In 1.45 (see cda9946) a workaround in the form of
`-C force-unwind-tables=yes` was added.

More history

`-C panic=abort` was added in [Rust
1.10](https://blog.rust-lang.org/2016/07/07/Rust-1.10/#what-s-in-1-10-stable)
and the motivation was binary size and compile time. But given how
confusing that behavior has turned out to be, it is better to make
binary size optimzation opt-in with `-C force-unwind-tables=no` rather
than default since the current default breaks backtraces.

Besides, if binary size is a primary concern, there are many other
tricks that can be used that has a higher impact.

TODO:
- [ ] better commit message
- [ ] more tests?
- [ ] figure out how to present this in release notes
- [ ] check if all tests pass
@rustbot
Copy link
Collaborator

rustbot commented Jul 7, 2025

r? @petrochenkov

rustbot has assigned @petrochenkov.
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 A-run-make Area: port run-make Makefiles to rmake.rs S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 7, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 7, 2025

These commits modify compiler targets.
(See the Target Tier Policy.)

This PR modifies run-make tests.

cc @jieyouxu

|| self.opts.cg.force_unwind_tables.unwrap_or(
self.panic_strategy() == PanicStrategy::Unwind || self.target.default_uwtable,
)
|| self.opts.cg.force_unwind_tables.unwrap_or(self.target.default_uwtable)
Copy link
Member

Choose a reason for hiding this comment

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

Why did you remove the panic=unwind check? This will cause unwind tables to be disabled on some targets where they were previously enabled.

Copy link
Member Author

@Enselic Enselic Jul 7, 2025

Choose a reason for hiding this comment

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

Good catch, leftover from early prototyping and I didn't realize earlier it now is the wrong thing to do, will fix in the next iteration, thank you.

@Enselic Enselic changed the title Emit unwind tables by default with -Cpanic=abort on linux Fix backtraces with -C panic=abort on linux; emit unwind tables by default Jul 7, 2025
@hanna-kruppe
Copy link
Contributor

Note that it’s currently pretty tricky to change this setting from whatever the default is on a given target, since there’s no Cargo profile setting for it (unlike the panic strategy), cc rust-lang/cargo#15333

This observation can probably be used as argument for or against this change.

@bjorn3
Copy link
Member

bjorn3 commented Jul 7, 2025

If you don't want unwind tables to save space you probably want to strip it out after linking anyway. -Cpanic=abort still gives unwind tables for the standard library on targets supporting panic=unwind unless -Zbuild-std is used. Would be a good idea to document this stripping in the min-sized-rust guide. You have to strip .eh_frame, .eh_frame_hdr and .gcc_except_table. One complication however is that we do have to keep unwind tables for functions that call extern "C-unwind" functions for soundness though.

@hanna-kruppe
Copy link
Contributor

If there's a way to drop all unwind tables that can safely be dropped but no more, then that would be a nice new feature (-Cstrip variant?) or addition to min-sized-rust. But if you're going all out on minimizing binary size, then you'll already want -Zbuild-std for a dozen other reasons anyway. I mostly brought it up from the POV of: if the default is changed, how easily can an ordinary user of stable Rust get back to the current behavior?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-run-make Area: port run-make Makefiles to rmake.rs S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
5 participants