2019-02-08 14:53:55 +01:00
|
|
|
//! Implementation of panics backed by libgcc/libunwind (in some form).
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 16:18:40 -07:00
|
|
|
//!
|
|
|
|
|
//! For background on exception handling and stack unwinding please see
|
|
|
|
|
//! "Exception Handling in LLVM" (llvm.org/docs/ExceptionHandling.html) and
|
|
|
|
|
//! documents linked from it.
|
|
|
|
|
//! These are also good reads:
|
2020-11-05 14:33:23 +01:00
|
|
|
//! * <https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html>
|
2024-11-29 11:46:34 +01:00
|
|
|
//! * <https://nicolasbrailo.github.io/blog/projects_texts/13exceptionsunderthehood.html>
|
2021-06-23 16:26:46 -04:00
|
|
|
//! * <https://www.airs.com/blog/index.php?s=exception+frames>
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 16:18:40 -07:00
|
|
|
//!
|
|
|
|
|
//! ## A brief summary
|
|
|
|
|
//!
|
|
|
|
|
//! Exception handling happens in two phases: a search phase and a cleanup
|
|
|
|
|
//! phase.
|
|
|
|
|
//!
|
|
|
|
|
//! In both phases the unwinder walks stack frames from top to bottom using
|
|
|
|
|
//! information from the stack frame unwind sections of the current process's
|
2018-11-27 02:59:49 +00:00
|
|
|
//! modules ("module" here refers to an OS module, i.e., an executable or a
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 16:18:40 -07:00
|
|
|
//! dynamic library).
|
|
|
|
|
//!
|
|
|
|
|
//! For each stack frame, it invokes the associated "personality routine", whose
|
|
|
|
|
//! address is also stored in the unwind info section.
|
|
|
|
|
//!
|
|
|
|
|
//! In the search phase, the job of a personality routine is to examine
|
|
|
|
|
//! exception object being thrown, and to decide whether it should be caught at
|
2019-02-08 14:53:55 +01:00
|
|
|
//! that stack frame. Once the handler frame has been identified, cleanup phase
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 16:18:40 -07:00
|
|
|
//! begins.
|
|
|
|
|
//!
|
|
|
|
|
//! In the cleanup phase, the unwinder invokes each personality routine again.
|
|
|
|
|
//! This time it decides which (if any) cleanup code needs to be run for
|
2019-02-08 14:53:55 +01:00
|
|
|
//! the current stack frame. If so, the control is transferred to a special
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 16:18:40 -07:00
|
|
|
//! branch in the function body, the "landing pad", which invokes destructors,
|
2019-02-08 14:53:55 +01:00
|
|
|
//! frees memory, etc. At the end of the landing pad, control is transferred
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 16:18:40 -07:00
|
|
|
//! back to the unwinder and unwinding resumes.
|
|
|
|
|
//!
|
|
|
|
|
//! Once stack has been unwound down to the handler frame level, unwinding stops
|
|
|
|
|
//! and the last personality routine transfers control to the catch block.
|
|
|
|
|
|
|
|
|
|
use alloc::boxed::Box;
|
|
|
|
|
use core::any::Any;
|
2022-10-05 19:59:45 +01:00
|
|
|
use core::ptr;
|
2015-05-11 21:09:07 -07:00
|
|
|
|
2019-02-02 19:00:02 +01:00
|
|
|
use unwind as uw;
|
2015-05-11 21:09:07 -07:00
|
|
|
|
2022-10-11 18:02:07 +01:00
|
|
|
// In case where multiple copies of std exist in a single process,
|
2022-10-05 19:59:45 +01:00
|
|
|
// we use address of this static variable to distinguish an exception raised by
|
|
|
|
|
// this copy and some other copy (which needs to be treated as foreign exception).
|
|
|
|
|
static CANARY: u8 = 0;
|
|
|
|
|
|
|
|
|
|
// NOTE(nbdd0121)
|
2022-12-23 01:01:30 +00:00
|
|
|
// There is an ABI stability requirement on this struct.
|
|
|
|
|
// The first two field must be `_Unwind_Exception` and `canary`,
|
2022-10-05 19:59:45 +01:00
|
|
|
// as it may be accessed by a different version of the std with a different compiler.
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 16:18:40 -07:00
|
|
|
#[repr(C)]
|
2015-05-11 21:09:07 -07:00
|
|
|
struct Exception {
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 16:18:40 -07:00
|
|
|
_uwe: uw::_Unwind_Exception,
|
2022-10-05 19:59:45 +01:00
|
|
|
canary: *const u8,
|
2019-12-26 10:26:53 +01:00
|
|
|
cause: Box<dyn Any + Send>,
|
2015-05-11 21:09:07 -07:00
|
|
|
}
|
|
|
|
|
|
2024-12-13 18:58:15 +01:00
|
|
|
pub(crate) unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 16:18:40 -07:00
|
|
|
let exception = Box::new(Exception {
|
|
|
|
|
_uwe: uw::_Unwind_Exception {
|
2024-09-14 20:24:40 -07:00
|
|
|
exception_class: RUST_EXCEPTION_CLASS,
|
2024-02-22 17:32:06 -06:00
|
|
|
exception_cleanup: Some(exception_cleanup),
|
2023-08-05 20:20:41 +02:00
|
|
|
private: [core::ptr::null(); uw::unwinder_private_data_size],
|
2015-05-11 21:09:07 -07:00
|
|
|
},
|
2022-10-05 19:59:45 +01:00
|
|
|
canary: &CANARY,
|
2019-12-26 10:26:53 +01:00
|
|
|
cause: data,
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 16:18:40 -07:00
|
|
|
});
|
2015-06-10 19:33:04 -07:00
|
|
|
let exception_param = Box::into_raw(exception) as *mut uw::_Unwind_Exception;
|
2025-02-13 12:44:32 -08:00
|
|
|
return unsafe { uw::_Unwind_RaiseException(exception_param) as u32 };
|
2015-05-11 21:09:07 -07:00
|
|
|
|
2016-05-29 16:06:29 +05:30
|
|
|
extern "C" fn exception_cleanup(
|
|
|
|
|
_unwind_code: uw::_Unwind_Reason_Code,
|
|
|
|
|
exception: *mut uw::_Unwind_Exception,
|
|
|
|
|
) {
|
2015-05-11 21:09:07 -07:00
|
|
|
unsafe {
|
|
|
|
|
let _: Box<Exception> = Box::from_raw(exception as *mut Exception);
|
2020-01-10 00:19:40 +00:00
|
|
|
super::__rust_drop_panic();
|
2015-05-11 21:09:07 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-13 18:58:15 +01:00
|
|
|
pub(crate) unsafe fn cleanup(ptr: *mut u8) -> Box<dyn Any + Send> {
|
2025-02-13 12:44:32 -08:00
|
|
|
unsafe {
|
|
|
|
|
let exception = ptr as *mut uw::_Unwind_Exception;
|
|
|
|
|
if (*exception).exception_class != RUST_EXCEPTION_CLASS {
|
|
|
|
|
uw::_Unwind_DeleteException(exception);
|
|
|
|
|
super::__rust_foreign_exception();
|
|
|
|
|
}
|
2022-10-05 19:59:45 +01:00
|
|
|
|
2025-02-13 12:44:32 -08:00
|
|
|
let exception = exception.cast::<Exception>();
|
|
|
|
|
// Just access the canary field, avoid accessing the entire `Exception` as
|
|
|
|
|
// it can be a foreign Rust exception.
|
|
|
|
|
let canary = (&raw const (*exception).canary).read();
|
|
|
|
|
if !ptr::eq(canary, &CANARY) {
|
|
|
|
|
// A foreign Rust exception, treat it slightly differently from other
|
|
|
|
|
// foreign exceptions, because call into `_Unwind_DeleteException` will
|
|
|
|
|
// call into `__rust_drop_panic` which produces a confusing
|
|
|
|
|
// "Rust panic must be rethrown" message.
|
|
|
|
|
super::__rust_foreign_exception();
|
|
|
|
|
}
|
2022-10-05 19:59:45 +01:00
|
|
|
|
2025-02-13 12:44:32 -08:00
|
|
|
let exception = Box::from_raw(exception as *mut Exception);
|
|
|
|
|
exception.cause
|
|
|
|
|
}
|
2015-05-11 21:09:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Rust's exception class identifier. This is used by personality routines to
|
|
|
|
|
// determine whether the exception was thrown by their own runtime.
|
2024-09-26 11:33:12 -07:00
|
|
|
const RUST_EXCEPTION_CLASS: uw::_Unwind_Exception_Class = u64::from_ne_bytes(*b"MOZ\0RUST");
|