Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 5 additions & 3 deletions library/std/src/sys/personality/gcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@
//! and the last personality routine transfers control to the catch block.
#![forbid(unsafe_op_in_unsafe_fn)]

mod dwarf;
mod eh;

use unwind as uw;

use super::dwarf::eh::{self, EHAction, EHContext};
use self::eh::{EHAction, EHContext};
Copy link
Member

Choose a reason for hiding this comment

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

Does this not work?

Suggested change
use self::eh::{EHAction, EHContext};
use eh::{EHAction, EHContext};
Copy link
Member Author

Choose a reason for hiding this comment

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

It does, but I prefer the self style because it makes it explicit that it's not a crate but a module, and makes rustfmt group it as such

use crate::ffi::c_int;

// Register ids were lifted from LLVM's TargetLowering::getExceptionPointerRegister()
Expand Down Expand Up @@ -332,8 +335,7 @@ unsafe fn find_eh_action(context: *mut uw::_Unwind_Context) -> Result<EHAction,
// `ip = -1` has special meaning, so use wrapping sub to allow for that
ip: if ip_before_instr != 0 { ip } else { ip.wrapping_sub(1) },
func_start: uw::_Unwind_GetRegionStart(context),
get_text_start: &|| uw::_Unwind_GetTextRelBase(context),
get_data_start: &|| uw::_Unwind_GetDataRelBase(context),
raw_context: context,
};
eh::find_eh_action(lsda, &eh_context)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#[cfg(test)]
mod tests;

pub mod eh;

pub struct DwarfReader {
pub ptr: *const u8,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

use core::ptr;

use super::DwarfReader;
use unwind as uw;

use super::dwarf::DwarfReader;

pub const DW_EH_PE_omit: u8 = 0xFF;
pub const DW_EH_PE_absptr: u8 = 0x00;
Expand All @@ -37,11 +39,10 @@ pub const DW_EH_PE_aligned: u8 = 0x50;
pub const DW_EH_PE_indirect: u8 = 0x80;

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

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

pub unsafe fn find_eh_action(lsda: *const u8, context: &EHContext<'_>) -> Result<EHAction, ()> {
pub unsafe fn find_eh_action(lsda: *const u8, context: &EHContext) -> Result<EHAction, ()> {
if lsda.is_null() {
return Ok(EHAction::None);
}
Expand Down Expand Up @@ -224,7 +225,7 @@ unsafe fn read_encoded_offset(reader: &mut DwarfReader, encoding: u8) -> Result<
/// [LSB-dwarf-ext]: https://refspecs.linuxfoundation.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/dwarfext.html
unsafe fn read_encoded_pointer(
reader: &mut DwarfReader,
context: &EHContext<'_>,
context: &EHContext,
encoding: u8,
) -> Result<*const u8, ()> {
if encoding == DW_EH_PE_omit {
Expand All @@ -241,8 +242,8 @@ unsafe fn read_encoded_pointer(
}
context.func_start
}
DW_EH_PE_textrel => (*context.get_text_start)(),
DW_EH_PE_datarel => (*context.get_data_start)(),
DW_EH_PE_textrel => unsafe { uw::_Unwind_GetTextRelBase(context.raw_context) },
DW_EH_PE_datarel => unsafe { uw::_Unwind_GetDataRelBase(context.raw_context) },
// aligned means the value is aligned to the size of a pointer
DW_EH_PE_aligned => {
reader.ptr = reader.ptr.with_addr(round_up(reader.ptr.addr(), size_of::<*const u8>())?);
Expand Down
2 changes: 0 additions & 2 deletions library/std/src/sys/personality/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
//! Additionally, ARM EHABI uses the personality function when generating
//! backtraces.

mod dwarf;

#[cfg(not(any(test, doctest)))]
cfg_select! {
target_os = "emscripten" => {
Expand Down
11 changes: 11 additions & 0 deletions tests/run-make-cargo/panic-free-cdylib/Cargo.toml
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"
5 changes: 5 additions & 0 deletions tests/run-make-cargo/panic-free-cdylib/lib.rs
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
}
43 changes: 43 additions & 0 deletions tests/run-make-cargo/panic-free-cdylib/rmake.rs
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.
Comment on lines +37 to +38
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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?

.assert_stdout_not_contains("panic")
.assert_stdout_not_contains("addr2line")
.assert_stdout_not_contains("backtrace")
.assert_stdout_not_contains("gimli");
}
Loading