From: Ralf Jung Date: Tue, 26 Nov 2019 09:08:11 +0000 (+0100) Subject: also mention panic strategy X-Git-Url: https://git.ralfj.de/web.git/commitdiff_plain/fd23296e257f135236fc3e3e3d903bf22050b7ba?ds=inline;hp=66fabeaabd2bdc0b2e81153084dc18ad21c1085d also mention panic strategy --- diff --git a/personal/_posts/2019-11-25-how-to-panic-in-rust.md b/personal/_posts/2019-11-25-how-to-panic-in-rust.md index f9ac9ed..0a9d2ba 100644 --- a/personal/_posts/2019-11-25-how-to-panic-in-rust.md +++ b/personal/_posts/2019-11-25-how-to-panic-in-rust.md @@ -22,13 +22,14 @@ The actual mechanism of unwinding is a totally different matter (and one that I When trying to figure out how panicking works by reading the code in libstd, one can easily get lost in the maze. There are multiple layers of indirection that are only put together by the linker, -there is the [`#[panic_handler]` attribute](https://doc.rust-lang.org/nomicon/panic-handler.html) and the ["panic runtime"](https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md) (controlled by `-C panic`) and ["panic hooks"](https://doc.rust-lang.org/std/panic/fn.set_hook.html), +there is the [`#[panic_handler]` attribute](https://doc.rust-lang.org/nomicon/panic-handler.html) and the ["panic runtime"](https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md) (controlled by the panic *strategy*, which is set via `-C panic`) and ["panic hooks"](https://doc.rust-lang.org/std/panic/fn.set_hook.html), and it turns out panicking in `#[no_std]` context takes an entirely different code path... there is just a lot going on. To make things worse, [the RFC describing panic hooks](https://github.com/rust-lang/rfcs/blob/master/text/1328-global-panic-handler.md) calls them "panic handler", but that term has since been re-purposed. I think the best place to start are the interfaces controlling the two indirections: -* The *panic runtime* is used by libstd to control what happens after the panic information has been printed to stderr: either we abort (`-C panic=abort`) or we unwind (`-C panic=unwind`). +* The *panic runtime* is used by libstd to control what happens after the panic information has been printed to stderr. + It is determined by the panic *strategy*: either we abort (`-C panic=abort`) or we unwind (`-C panic=unwind`). (The panic runtime also provides the implementation for [`catch_unwind`](https://doc.rust-lang.org/std/panic/fn.catch_unwind.html) but we are not concerned with that here.) * The *panic handler* is used by libcore to implement (a) panics inserted by code generation (such as panics caused by arithmetic overflow or out-of-bounds array/slice indexing) and (b) the `core::panic!` macro (this is the `panic!` macro in libcore itself and in `#[no_std]` context in general). @@ -102,8 +103,10 @@ The `'static` bound is quite well hidden there, but after a while I realized tha Curiously, even though the components of `PanicInfo` and the arguments of `rust_panic_with_hook` match up pretty well and seem like they could just be forwarded, that is *not* what happens. Instead, libstd entirely *ignores* the `payload` component of the `PanicInfo`, and sets up the actual payload (passed to `rust_panic_with_hook`) such that it contains [the formatted `message`](https://github.com/rust-lang/rust/blob/7d761fe0462ba0f671a237d0bb35e3579b8ba0e8/src/libstd/panicking.rs#L348). - One consequence of this setup is that, as far as I can tell, the `-C panic` option has no effect at all for `no_std` applications! + In particular, this means that the panic *runtime* is irrelevant for `no_std` applications. It only comes into play when libstd's panic handler implementation is used. + (The panic *strategy* selected via `-C panic` still has an effect as it also influences code generation. + For example, with `-C panic=abort` code can become simpler as it does not need to support unwinding.) * [`begin_panic_fmt`](https://github.com/rust-lang/rust/blob/7d761fe0462ba0f671a237d0bb35e3579b8ba0e8/src/libstd/panicking.rs#L319), backing the [format string version of `std::panic!`](https://github.com/rust-lang/rust/blob/7d761fe0462ba0f671a237d0bb35e3579b8ba0e8/src/libstd/macros.rs#L22-L23) (i.e., this is used when you pass multiple arguments to the macro). This basically just packages the format string arguments into a `PanicInfo` (with a [dummy payload](https://github.com/rust-lang/rust/blob/7d761fe0462ba0f671a237d0bb35e3579b8ba0e8/src/libcore/panic.rs#L56)) and calls the default panic handler that we just discussed.