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
|
|
|
#![no_std]
|
|
|
|
#![unstable(feature = "panic_unwind", issue = "32837")]
|
2017-08-22 16:24:29 -05:00
|
|
|
#![feature(link_cfg)]
|
2019-02-10 16:13:30 +09:00
|
|
|
#![feature(nll)]
|
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
|
|
|
#![feature(staged_api)]
|
|
|
|
#![feature(unwind_attributes)]
|
2017-03-24 11:01:45 -07:00
|
|
|
#![feature(static_nobundle)]
|
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
|
|
|
#![cfg_attr(not(target_env = "msvc"), feature(libc))]
|
|
|
|
|
2019-06-10 08:12:14 -07:00
|
|
|
cfg_if::cfg_if! {
|
2017-10-22 20:01:00 -07:00
|
|
|
if #[cfg(target_env = "msvc")] {
|
2020-07-06 20:31:12 -07:00
|
|
|
// Windows MSVC no extra unwinder support needed
|
|
|
|
} else if #[cfg(any(
|
|
|
|
target_os = "l4re",
|
|
|
|
target_os = "none",
|
|
|
|
))] {
|
|
|
|
// These "unix" family members do not have unwinder.
|
|
|
|
// Note this also matches x86_64-linux-kernel.
|
|
|
|
} else if #[cfg(any(
|
|
|
|
unix,
|
|
|
|
windows,
|
2020-08-08 02:14:40 -04:00
|
|
|
target_os = "psp",
|
2020-07-06 20:31:12 -07:00
|
|
|
all(target_vendor = "fortanix", target_env = "sgx"),
|
|
|
|
))] {
|
2017-10-22 20:01:00 -07:00
|
|
|
mod libunwind;
|
|
|
|
pub use libunwind::*;
|
2020-07-06 20:31:12 -07:00
|
|
|
} else {
|
|
|
|
// no unwinder on the system!
|
|
|
|
// - wasm32 (not emscripten, which is "unix" family)
|
|
|
|
// - os=none ("bare metal" targets)
|
|
|
|
// - os=hermit
|
|
|
|
// - os=uefi
|
|
|
|
// - os=cuda
|
|
|
|
// - nvptx64-nvidia-cuda
|
|
|
|
// - Any new targets not listed above.
|
2017-10-22 20:01:00 -07:00
|
|
|
}
|
|
|
|
}
|
2017-08-22 16:24:29 -05:00
|
|
|
|
2018-01-12 21:22:06 -02:00
|
|
|
#[cfg(target_env = "musl")]
|
2017-08-24 08:37:06 -05:00
|
|
|
#[link(name = "unwind", kind = "static", cfg(target_feature = "crt-static"))]
|
2017-08-22 16:24:29 -05:00
|
|
|
#[link(name = "gcc_s", cfg(not(target_feature = "crt-static")))]
|
2019-12-22 17:42:04 -05:00
|
|
|
extern "C" {}
|
2019-04-07 08:39:54 -06:00
|
|
|
|
2020-10-01 11:13:13 -07:00
|
|
|
// When building with crt-static, we get `gcc_eh` from the `libc` crate, since
|
|
|
|
// glibc needs it, and needs it listed later on the linker command line. We
|
|
|
|
// don't want to duplicate it here.
|
2020-10-08 15:05:31 +02:00
|
|
|
#[cfg(all(
|
|
|
|
target_os = "linux",
|
|
|
|
target_env = "gnu",
|
|
|
|
not(feature = "llvm-libunwind"),
|
|
|
|
not(feature = "system-llvm-libunwind")
|
|
|
|
))]
|
2020-09-20 21:19:34 -07:00
|
|
|
#[link(name = "gcc_s", cfg(not(target_feature = "crt-static")))]
|
|
|
|
extern "C" {}
|
|
|
|
|
2020-10-08 15:05:31 +02:00
|
|
|
#[cfg(all(
|
|
|
|
target_os = "linux",
|
|
|
|
target_env = "gnu",
|
|
|
|
not(feature = "llvm-libunwind"),
|
|
|
|
feature = "system-llvm-libunwind"
|
|
|
|
))]
|
|
|
|
#[link(name = "unwind", cfg(not(target_feature = "crt-static")))]
|
|
|
|
extern "C" {}
|
|
|
|
|
2019-04-07 08:39:54 -06:00
|
|
|
#[cfg(target_os = "redox")]
|
|
|
|
#[link(name = "gcc_eh", kind = "static-nobundle", cfg(target_feature = "crt-static"))]
|
|
|
|
#[link(name = "gcc_s", cfg(not(target_feature = "crt-static")))]
|
2019-12-22 17:42:04 -05:00
|
|
|
extern "C" {}
|
2020-05-17 21:08:23 +03:00
|
|
|
|
|
|
|
#[cfg(all(target_vendor = "fortanix", target_env = "sgx"))]
|
|
|
|
#[link(name = "unwind", kind = "static-nobundle")]
|
|
|
|
extern "C" {}
|