- Notifications
You must be signed in to change notification settings - Fork 13.9k
Ensure the personality does not panic #148105
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
Noratrieb wants to merge 2 commits into rust-lang:master Choose a base branch from Noratrieb:personality-panicking
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits Select commit Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Ensure the personality does not panic
In a cdylib that uses std and is free from panics in the code, the panic machinery will *still* be pulled in because of the personality function when using LLD. The personality function is used by unwinding to figure out what to do when unwinding through a function. Each function that participates in unwind has an associated FDE (frame descriptor entries) in `.eh_frame`. This FDE points to a CIE (common information entry), which can reference a language-specific personality function, like `rust_eh_personality` in our case. As long as there is a CIE that references the personality, the function cannot be removed. If all references to a CIE get removed (because the functions and their associated FDEs have been removed), LLD will not remove the CIE, likely due to the ordering of its passes). Binutils ld will remove it. In the case where the CIE is still around (despite not being used), it will still reference the personality function, so that will still be around. This is not great since it's a bunch of code, but also not _that_ much. But this is where panicking comes in. Before this change, the personality function internally made use of `dyn Fn`. This caused an indirect call that LLVM was not able to analyze as guaranteed free of unwinding, even during fat LTO. This meant that an `invoke` was used, with a landing pad. In an `extern "C"` function, which the personality function is, all landing pads call `panic_cannot_unwind`, which is a `panic_nounwind`, which is, obviously, a panic. And as a panic, it pulls in *all* the panic machinery, which is very big and sad. It is also completely unnecessary, because these indirect functions do not panic, as they are just a convenient abstraction provided from the outside. By restructuring the code to remove these indirect calls, LLVM is able to fully analyze everything and see that rust_eh_personality cannot panic, and therefore remove its landing pad. With this change, exporting a panic-free function from a cdylib will only contain the function and the personality (when linked with LLD at least, with binutils ld it will only contain the function), with no panic code being present at all, which is great.
- Loading branch information
commit 6b33cc6438ca4420bb9d51a1bd63a9b3d0728a3f
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| [package] | ||
| name = "add" | ||
| version = "0.1.0" | ||
| edition = "2024" | ||
| | ||
| [lib] | ||
| path = "lib.rs" | ||
| crate-type = ["cdylib"] | ||
| | ||
| [profile.release] | ||
| lto = "fat" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| #![crate_type = "cdylib"] | ||
| | ||
| pub extern "C" fn add(a: u64, b: u64) -> u64 { | ||
| a + b | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // This ensures that a cdylib that uses std and panic=unwind but does not | ||
| // have any panics itself will not have *any* panic-related code in the final | ||
| // binary, at least when using fat LTO | ||
| // (since all the necessary nounwind propagation requires fat LTO). | ||
| // | ||
| // This code used to be pulled in via a landing pad in the personality function, | ||
| // (since that is `extern "C"` and therefore panics if something unwinds), so | ||
| // if this failed because you modified the personality function, ensure it contains | ||
| // no potentially unwinding calls. | ||
| | ||
| use run_make_support::{cargo, dynamic_lib_name, llvm_nm, path, rustc, target}; | ||
| | ||
| fn main() { | ||
| let target_dir = path("target"); | ||
| | ||
| // We use build-std to ensure that the sysroot does not have debug assertions, | ||
| // as this doesn't work with debug assertions. | ||
| cargo() | ||
| .args(&[ | ||
| "build", | ||
| "--manifest-path", | ||
| "Cargo.toml", | ||
| "--release", | ||
| "-Zbuild-std=std", | ||
| "--target", | ||
| &target(), | ||
| ]) | ||
| .env("CARGO_TARGET_DIR", &target_dir) | ||
| .env("RUSTC_BOOTSTRAP", "1") | ||
| .run(); | ||
| | ||
| let output_path = target_dir.join(target()).join("release").join(dynamic_lib_name("add")); | ||
| | ||
| llvm_nm() | ||
| .input(output_path) | ||
| .run() | ||
| // a collection of panic-related strings. if this appears in the output | ||
| // for other reasons than having panic symbols, I am sorry. | ||
| .assert_stdout_not_contains("panic") | ||
| .assert_stdout_not_contains("addr2line") | ||
| .assert_stdout_not_contains("backtrace") | ||
| .assert_stdout_not_contains("gimli"); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, but what if these symbols aren't found for reasons other than not having panic symbols? E.g. this test gets subtly broken after a refactor or something else changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems unlikely to me that all of these symbols would disappear for a reason other than panic machinery being gone, especially the "panic" one. But I'd be open to better suggestions for how to write it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put a panic behind a cfg switch and compile it twice? Then check for presence and absence respectively?