Skip to content

Commit 4f9d6a3

Browse files
committed
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.
1 parent 527ba9b commit 4f9d6a3

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

library/std/src/sys/personality/gcc.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,7 @@ unsafe fn find_eh_action(context: *mut uw::_Unwind_Context) -> Result<EHAction,
335335
// `ip = -1` has special meaning, so use wrapping sub to allow for that
336336
ip: if ip_before_instr != 0 { ip } else { ip.wrapping_sub(1) },
337337
func_start: uw::_Unwind_GetRegionStart(context),
338-
get_text_start: &|| uw::_Unwind_GetTextRelBase(context),
339-
get_data_start: &|| uw::_Unwind_GetDataRelBase(context),
338+
raw_context: context,
340339
};
341340
eh::find_eh_action(lsda, &eh_context)
342341
}

library/std/src/sys/personality/gcc/eh.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
use core::ptr;
1616

17+
use unwind as uw;
18+
1719
use super::dwarf::DwarfReader;
1820

1921
pub const DW_EH_PE_omit: u8 = 0xFF;
@@ -37,11 +39,10 @@ pub const DW_EH_PE_aligned: u8 = 0x50;
3739
pub const DW_EH_PE_indirect: u8 = 0x80;
3840

3941
#[derive(Copy, Clone)]
40-
pub struct EHContext<'a> {
41-
pub ip: *const u8, // Current instruction pointer
42-
pub func_start: *const u8, // Pointer to the current function
43-
pub get_text_start: &'a dyn Fn() -> *const u8, // Get pointer to the code section
44-
pub get_data_start: &'a dyn Fn() -> *const u8, // Get pointer to the data section
42+
pub struct EHContext {
43+
pub(crate) ip: *const u8, // Current instruction pointer
44+
pub(crate) func_start: *const u8, // Pointer to the current function
45+
pub(crate) raw_context: *mut uw::_Unwind_Context,
4546
}
4647

4748
/// Landing pad.
@@ -63,7 +64,7 @@ pub enum EHAction {
6364
pub const USING_SJLJ_EXCEPTIONS: bool =
6465
cfg!(all(target_vendor = "apple", not(target_os = "watchos"), target_arch = "arm"));
6566

66-
pub unsafe fn find_eh_action(lsda: *const u8, context: &EHContext<'_>) -> Result<EHAction, ()> {
67+
pub unsafe fn find_eh_action(lsda: *const u8, context: &EHContext) -> Result<EHAction, ()> {
6768
if lsda.is_null() {
6869
return Ok(EHAction::None);
6970
}
@@ -224,7 +225,7 @@ unsafe fn read_encoded_offset(reader: &mut DwarfReader, encoding: u8) -> Result<
224225
/// [LSB-dwarf-ext]: https://refspecs.linuxfoundation.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/dwarfext.html
225226
unsafe fn read_encoded_pointer(
226227
reader: &mut DwarfReader,
227-
context: &EHContext<'_>,
228+
context: &EHContext,
228229
encoding: u8,
229230
) -> Result<*const u8, ()> {
230231
if encoding == DW_EH_PE_omit {
@@ -241,8 +242,8 @@ unsafe fn read_encoded_pointer(
241242
}
242243
context.func_start
243244
}
244-
DW_EH_PE_textrel => (*context.get_text_start)(),
245-
DW_EH_PE_datarel => (*context.get_data_start)(),
245+
DW_EH_PE_textrel => unsafe { uw::_Unwind_GetTextRelBase(context.raw_context) },
246+
DW_EH_PE_datarel => unsafe { uw::_Unwind_GetDataRelBase(context.raw_context) },
246247
// aligned means the value is aligned to the size of a pointer
247248
DW_EH_PE_aligned => {
248249
reader.ptr = reader.ptr.with_addr(round_up(reader.ptr.addr(), size_of::<*const u8>())?);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![crate_type = "cdylib"]
2+
3+
pub extern "C" fn add(a: u64, b: u64) -> u64 {
4+
a + b
5+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// This ensures that a cdylib that uses std and panic=unwind but does not
2+
// have any panics itself will not have *any* panic-related code in the final
3+
// binary, at least when using fat LTO
4+
// (since all the necessary nounwind propagation requires fat LTO).
5+
//
6+
// This code used to be pulled in via a landing pad in the personality function,
7+
// (since that is `extern "C"` and therefore panics if something unwinds), so
8+
// if this failed because you modified the personality function, ensure it contains
9+
// no potentially unwinding calls.
10+
11+
use run_make_support::{llvm_nm, rustc};
12+
13+
fn main() {
14+
rustc().input("add.rs").edition("2024").lto("fat").opt_level("3").run();
15+
16+
llvm_nm()
17+
.input("libadd.so")
18+
.run()
19+
// a collection of panic-related strings. if this appears in the output
20+
// for other reasons than having panic symbols, I am sorry.
21+
.assert_stdout_not_contains("panic")
22+
.assert_stdout_not_contains("addr2line")
23+
.assert_stdout_not_contains("backtrace")
24+
.assert_stdout_not_contains("gimli");
25+
}

0 commit comments

Comments
 (0)