Skip to content

Commit 50872bd

Browse files
committed
Auto merge of rust-lang#97033 - nbdd0121:unwind3, r=Amanieu
Remove libstd's calls to `C-unwind` foreign functions Remove all libstd and its dependencies' usage of `extern "C-unwind"`. This is a prerequiste of a WIP PR which will forbid libraries calling `extern "C-unwind"` functions to be compiled in `-Cpanic=unwind` and linked against `panic_abort` (this restriction is necessary to address soundness bug rust-lang#96926). Cargo will ensure all crates are compiled with the same `-Cpanic` but the std is only compiled `-Cpanic=unwind` but needs the ability to be linked into `-Cpanic=abort`. Currently there are two places where `C-unwind` is used in libstd: * `__rust_start_panic` is used for interfacing to the panic runtime. This could be `extern "Rust"` * `_{rdl,rg}_oom`: a shim `__rust_alloc_error_handler` will be generated by codegen to call into one of these; they can also be `extern "Rust"` (in fact, the generated shim is used as `extern "Rust"`, so I am not even sure why these are not, probably because they used to `extern "C"` and was changed to `extern "C-unwind"` when we allow alloc error hooks to unwind, but they really should just be using Rust ABI). For dependencies, there is only one `extern "C-unwind"` function call, in `unwind` crate. This can be expressed as a re-export. More dicussions can be seen in the Zulip thread: https://rust-lang.zulipchat.com/#narrow/stream/210922-project-ffi-unwind/topic/soundness.20in.20mixed.20panic.20mode `@rustbot` label: T-libs F-c_unwind
2 parents e6327bc + fbb3c19 commit 50872bd

File tree

5 files changed

+6
-9
lines changed

5 files changed

+6
-9
lines changed

library/alloc/src/alloc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -398,13 +398,13 @@ pub mod __alloc_error_handler {
398398

399399
// if there is no `#[alloc_error_handler]`
400400
#[rustc_std_internal_symbol]
401-
pub unsafe extern "C-unwind" fn __rdl_oom(size: usize, _align: usize) -> ! {
401+
pub unsafe fn __rdl_oom(size: usize, _align: usize) -> ! {
402402
panic!("memory allocation of {size} bytes failed")
403403
}
404404

405405
// if there is an `#[alloc_error_handler]`
406406
#[rustc_std_internal_symbol]
407-
pub unsafe extern "C-unwind" fn __rg_oom(size: usize, align: usize) -> ! {
407+
pub unsafe fn __rg_oom(size: usize, align: usize) -> ! {
408408
let layout = unsafe { Layout::from_size_align_unchecked(size, align) };
409409
extern "Rust" {
410410
#[lang = "oom"]

library/panic_abort/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub unsafe extern "C" fn __rust_panic_cleanup(_: *mut u8) -> *mut (dyn Any + Sen
3030

3131
// "Leak" the payload and shim to the relevant abort on the platform in question.
3232
#[rustc_std_internal_symbol]
33-
pub unsafe extern "C-unwind" fn __rust_start_panic(_payload: *mut &mut dyn BoxMeUp) -> u32 {
33+
pub unsafe fn __rust_start_panic(_payload: *mut &mut dyn BoxMeUp) -> u32 {
3434
// Android has the ability to attach a message as part of the abort.
3535
#[cfg(target_os = "android")]
3636
android::android_set_abort_message(_payload);

library/panic_unwind/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ pub unsafe extern "C" fn __rust_panic_cleanup(payload: *mut u8) -> *mut (dyn Any
104104
// Entry point for raising an exception, just delegates to the platform-specific
105105
// implementation.
106106
#[rustc_std_internal_symbol]
107-
pub unsafe extern "C-unwind" fn __rust_start_panic(payload: *mut &mut dyn BoxMeUp) -> u32 {
107+
pub unsafe fn __rust_start_panic(payload: *mut &mut dyn BoxMeUp) -> u32 {
108108
let payload = Box::from_raw((*payload).take_box());
109109

110110
imp::panic(payload)

library/std/src/panicking.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ extern "C" {
4747
}
4848

4949
#[allow(improper_ctypes)]
50-
extern "C-unwind" {
50+
extern "Rust" {
5151
/// `payload` is passed through another layer of raw pointers as `&mut dyn Trait` is not
5252
/// FFI-safe. `BoxMeUp` lazily performs allocation only when needed (this avoids allocations
5353
/// when using the "abort" panic runtime).

library/unwind/src/libunwind.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,7 @@ if #[cfg(not(all(target_os = "ios", target_arch = "arm")))] {
264264
pub fn _Unwind_SjLj_RaiseException(e: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
265265
}
266266

267-
#[inline]
268-
pub unsafe fn _Unwind_RaiseException(exc: *mut _Unwind_Exception) -> _Unwind_Reason_Code {
269-
_Unwind_SjLj_RaiseException(exc)
270-
}
267+
pub use _Unwind_SjLj_RaiseException as _Unwind_RaiseException;
271268
}
272269
} // cfg_if!
273270

0 commit comments

Comments
 (0)