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, and generating and printing a backtrace is done by for example the default panic hook.

Begin emitting unwind tables by default again with -C panic=abort (see history below) so that backtraces work.

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

History

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 a workaround in the form of -C force-unwind-tables=yes was added (see #69984).

-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 optimization 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.

Release Note Entry Draft:

Compatibility Notes

@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

@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?

…default

The linux backtrace unwinder relies on unwind tables to work properly,
and generating and printing a backtrace is done by for example the
default panic hook.

Begin emitting unwind tables by default again with `-C panic=abort` (see
history below) so that backtraces work.

History
=======

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.

`-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 optimization 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.
@Enselic Enselic force-pushed the panic-abort-uwtables branch from d113be9 to 826aa60 Compare July 8, 2025 14:47
@Enselic
Copy link
Member Author

Enselic commented Jul 8, 2025

Changes since the initial version d113be9:

if the default is changed, how easily can an ordinary user of stable Rust get back to the current behavior?

I don't think ordinary users want to go back to the regression introduced in Rust 1.23, because I expect ordinary users to want backtraces to work.

For advanced users I don't think it's too much to ask to require building with -C force-unwind-tables=no to opt back in to the current (arguably broken) behavior.

@nbdd0121
Copy link
Contributor

nbdd0121 commented Jul 8, 2025

If you don't want unwind tables to save space you probably want to strip it out after linking anyway.

Stripping unwind tables is going to change the behaviour if you link in some foreign static library (e.g. a C++ library) that depends on unwinding.

One complication however is that we do have to keep unwind tables for functions that call extern "C-unwind" functions for soundness though.

I think there is no soundness concern, it's just correctness? Unwinding will stop with error if it cannot find unwind tables.

Copy link
Contributor

Choose a reason for hiding this comment

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

This change looks correct to me.

@bjorn3
Copy link
Member

bjorn3 commented Jul 9, 2025

I think there is no soundness concern, it's just correctness? Unwinding will stop with error if it cannot find unwind tables.

Right, only keeping .eh_frame without the LSDA would cause soundness issues I think.

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
6 participants