about summary refs log tree commit diff
path: root/library/std/src/panicking.rs
AgeCommit message (Collapse)AuthorLines
2023-03-26Remove unnecessary raw pointer in __rust_start_panic argbjorn3-10/+5
It is no longer necessary as __rust_start_panic switched to the Rust abi.
2023-02-20Explain the default panic hook betterMatt Harding-6/+10
This changes the documentation of `std::panic::set_hook` and `take_hook` to better explain how the default panic hook works. In particular the fact that `take_hook` registers the default hook, rather than no hook at all, was missing from the docs.
2023-01-14Remove various double spaces in source comments.André Vennberg-2/+2
2022-12-30Replace libstd, libcore, liballoc in docs.jonathanCogan-1/+1
2022-12-28adjust message on non-unwinding panicRalf Jung-1/+5
2022-11-29Adjust inlining attributes around panic_immediate_abortBen Kimock-2/+2
2022-10-06Fix whitespaceFlorian Bartels-2/+2
2022-10-06Prevent UB in child process after calling libc::forkFlorian Bartels-4/+23
After calling libc::fork, the child process tried to access a TLS variable when processing a panic. This caused a memory allocation which is UB in the child. To prevent this from happening, the panic handler will not access the TLS variable in case `panic::always_abort` was called before.
2022-09-23Update doc after renaming fn is_zeroFlorian Bartels-2/+2
`fn is_zero` has been renamed to `fn count_is_zero` in 1b1bf2463619e23eba1b36b6d7df276ce73563dd. This patch updates the documentation accordingly.
2022-09-19std: use `sync::RwLock` for internal staticsjoboet-79/+50
2022-05-23Auto merge of #92461 - rust-lang:const_tls_local_panic_count, r=Mark-Simulacrumbors-1/+1
Use const initializer for LOCAL_PANIC_COUNT This reduces the size of the __getit function for LOCAL_PANIC_COUNT and should speed up accesses of LOCAL_PANIC_COUNT a bit.
2022-05-14Use Rust ABI for `__rust_start_panic` and `_{rdl,rg}_oom`Gary Guo-1/+1
2022-04-23Use const initializer for LOCAL_PANIC_COUNTbjorn3-1/+1
This reduces the size of the __getit function for LOCAL_PANIC_COUNT and should speed up accesses of LOCAL_PANIC_COUNT a bit.
2022-04-06Rename RWLock to RwLock in std::sys.Mara Bos-2/+2
2022-03-10Use implicit capture syntax in format_argsT-O-R-U-S-3/+3
This updates the standard library's documentation to use the new syntax. The documentation is worthwhile to update as it should be more idiomatic (particularly for features like this, which are nice for users to get acquainted with). The general codebase is likely more hassle than benefit to update: it'll hurt git blame, and generally updates can be done by folks updating the code if (and when) that makes things more readable with the new format. A few places in the compiler and library code are updated (mostly just due to already having been done when this commit was first authored).
2022-02-02Configure panic hook backtrace behaviorMark Rousskov-8/+15
2022-01-22Rollup merge of #92828 - Amanieu:unwind-abort, r=dtolnayMatthias Krüger-8/+14
Print a helpful message if unwinding aborts when it reaches a nounwind function This is implemented by routing `TerminatorKind::Abort` back through the panic handler, but with a special flag in the `PanicInfo` which indicates that the panic handler should *not* attempt to unwind the stack and should instead abort immediately. This is useful for the planned change in https://github.com/rust-lang/lang-team/issues/97 which would make `Drop` impls `nounwind` by default. ### Code ```rust #![feature(c_unwind)] fn panic() { panic!() } extern "C" fn nounwind() { panic(); } fn main() { nounwind(); } ``` ### Before ``` $ ./test thread 'main' panicked at 'explicit panic', test.rs:4:5 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace Illegal instruction (core dumped) ``` ### After ``` $ ./test thread 'main' panicked at 'explicit panic', test.rs:4:5 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace thread 'main' panicked at 'panic in a function that cannot unwind', test.rs:7:1 stack backtrace: 0: 0x556f8f86ec9b - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hdccefe11a6ac4396 1: 0x556f8f88ac6c - core::fmt::write::he152b28c41466ebb 2: 0x556f8f85d6e2 - std::io::Write::write_fmt::h0c261480ab86f3d3 3: 0x556f8f8654fa - std::panicking::default_hook::{{closure}}::h5d7346f3ff7f6c1b 4: 0x556f8f86512b - std::panicking::default_hook::hd85803a1376cac7f 5: 0x556f8f865a91 - std::panicking::rust_panic_with_hook::h4dc1c5a3036257ac 6: 0x556f8f86f079 - std::panicking::begin_panic_handler::{{closure}}::hdda1d83c7a9d34d2 7: 0x556f8f86edc4 - std::sys_common::backtrace::__rust_end_short_backtrace::h5b70ed0cce71e95f 8: 0x556f8f865592 - rust_begin_unwind 9: 0x556f8f85a764 - core::panicking::panic_no_unwind::h2606ab3d78c87899 10: 0x556f8f85b910 - test::nounwind::hade6c7ee65050347 11: 0x556f8f85b936 - test::main::hdc6e02cb36343525 12: 0x556f8f85b7e3 - core::ops::function::FnOnce::call_once::h4d02663acfc7597f 13: 0x556f8f85b739 - std::sys_common::backtrace::__rust_begin_short_backtrace::h071d40135adb0101 14: 0x556f8f85c149 - std::rt::lang_start::{{closure}}::h70dbfbf38b685e93 15: 0x556f8f85c791 - std::rt::lang_start_internal::h798f1c0268d525aa 16: 0x556f8f85c131 - std::rt::lang_start::h476a7ee0a0bb663f 17: 0x556f8f85b963 - main 18: 0x7f64c0822b25 - __libc_start_main 19: 0x556f8f85ae8e - _start 20: 0x0 - <unknown> thread panicked while panicking. aborting. Aborted (core dumped) ```
2022-01-17Help optimize out backtraces when disabledKornel-1/+1
2022-01-17Add PanicInfo::can_unwind which indicates whether a panic handler isAmanieu d'Antras-8/+14
allowed to trigger unwinding.
2022-01-08Add safety comments to panic::(set/take/update)_hookBadel2-0/+15
2022-01-08Change panic::update_hook to simplify usageBadel2-22/+23
And to remove possibility of panics while changing the panic handler, because that resulted in a double panic.
2022-01-07Implement panic::update_hookBadel2-0/+63
2021-12-14Fix a bunch of typosFrank Steffahn-3/+3
2021-10-30Add #[must_use] to remaining std functions (O-Z)John Kugelman-0/+3
2021-10-25Clean up special function const checksGary Guo-1/+2
Mark them as const and `#[rustc_do_not_const_check]` instead of hard-coding them in const-eval checks.
2021-10-19Deduplicate panic_fmtGary Guo-24/+2
std's begin_panic_fmt and core's panic_fmt are duplicates. Merge them to declutter code and remove a lang item.
2021-09-08Bump stage0 compiler to 1.56Mark Rousskov-1/+1
2021-08-03rustc: Fill out remaining parts of C-unwind ABIAlex Crichton-2/+3
This commit intends to fill out some of the remaining pieces of the C-unwind ABI. This has a number of other changes with it though to move this design space forward a bit. Notably contained within here is: * On `panic=unwind`, the `extern "C"` ABI is now considered as "may unwind". This fixes a longstanding soundness issue where if you `panic!()` in an `extern "C"` function defined in Rust that's actually UB because the LLVM representation for the function has the `nounwind` attribute, but then you unwind. * Whether or not a function unwinds now mainly considers the ABI of the function instead of first checking the panic strategy. This fixes a miscompile of `extern "C-unwind"` with `panic=abort` because that ABI can still unwind. * The aborting stub for non-unwinding ABIs with `panic=unwind` has been reimplemented. Previously this was done as a small tweak during MIR generation, but this has been moved to a separate and dedicated MIR pass. This new pass will, for appropriate functions and function calls, insert a `cleanup` landing pad for any function call that may unwind within a function that is itself not allowed to unwind. Note that this subtly changes some behavior from before where previously on an unwind which was caught-to-abort it would run active destructors in the function, and now it simply immediately aborts the process. * The `#[unwind]` attribute has been removed and all users in tests and such are now using `C-unwind` and `#![feature(c_unwind)]`. I think this is largely the last piece of the RFC to implement. Unfortunately I believe this is still not stabilizable as-is because activating the feature gate changes the behavior of the existing `extern "C"` ABI in a way that has no replacement. My thinking for how to enable this is that we add support for the `C-unwind` ABI on stable Rust first, and then after it hits stable we change the behavior of the `C` ABI. That way anyone straddling stable/beta/nightly can switch to `C-unwind` safely.
2021-07-28Make const panic!("..") work in Rust 2021.Mara Bos-0/+1
During const eval, this replaces calls to core::panicking::panic_fmt and std::panicking::being_panic_fmt with a call to a new const fn: core::panicking::const_panic_fmt. That function uses fmt::Arguments::as_str() to get the str and calls panic_str with that instead. panic!() invocations with formatting arguments are still not accepted, as the creation of such a fmt::Arguments cannot be done in constant functions right now.
2021-06-10Rollup merge of #84687 - a1phyr:improve_rwlock, r=m-ou-seYuki Okushi-8/+7
Multiple improvements to RwLocks This PR replicates #77147, #77380 and #84650 on RWLocks : - Split `sys_common::RWLock` in `StaticRWLock` and `MovableRWLock` - Unbox rwlocks on some platforms (Windows, Wasm and unsupported) - Simplify `RwLock::into_inner` Notes to reviewers : - For each target, I copied `MovableMutex` to guess if `MovableRWLock` should be boxed. - ~A comment says that `StaticMutex` is not re-entrant, I don't understand why and I don't know whether it applies to `StaticRWLock`.~ r? `@m-ou-se`
2021-06-06Default panic message should print Box<dyn Any>Reagan McFarland-1/+1
Prior to this patch, the default panic message (resulting from calling `panic_any(42);` for example), would print the following error message: ``` thread 'main' panicked at 'Box<Any>', ... ``` However, this should be `Box<dyn Any>` instead.
2021-06-01Multiple improvements to RwLocksBenoît du Garreau-8/+7
- Split `sys_common::RWLock` between `StaticRWLock` and `MovableRWLock` - Unbox `RwLock` on some platforms (Windows, Wasm and unsupported) - Simplify `RwLock::into_inner`
2021-05-19Rename `rterr` to `rtprintpanic`Christiaan Dirkx-6/+3
2021-05-19Replace `sys_common::util::dumb_print` with `rterr!`Christiaan Dirkx-5/+5
2021-05-07std panicking: ALWAYS_ABORT: use Relaxed memory orderingIan Jackson-2/+2
As per https://github.com/rust-lang/rust/pull/81858#discussion_r626507810 Suggested-by: Mara Bos <m-ou.se@m-ou.se> Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
2021-05-07std panicking: Provide panic::always_abortIan Jackson-15/+49
We must change the atomic read on panic entry to `Acquire`, to pick up a possible an `always_panic` on another thread. We add `count` to the names of panic_count::get and ::is_zaero, because now there is another reason why panic ought to maybe abort. Renaming these ensures that we have checked every call site to ensure that they don't need further adjustment. Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk> Co-authored-by: Mara Bos <m-ou.se@m-ou.se>
2021-05-07std panicking: Make decrease() return ()Ian Jackson-2/+2
Nothing uses the return value. This will make the next changes easier. Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
2020-12-25fix another comment, and make __rust_start_panic code a bit more ↵Ralf Jung-2/+3
semantically clear
2020-12-22update a seemingly outdated commentRalf Jung-3/+2
2020-12-21slightly more typed interface to panic implementationRalf Jung-2/+2
2020-11-10Merge set_panic and set_print into set_output_capture.Mara Bos-4/+4
There were no use cases for setting them separately. Merging them simplifies some things.
2020-11-10Use Vec<u8> for LOCAL_STD{OUT,ERR} instead of dyn Write.Mara Bos-22/+1
It was only ever used with Vec<u8> anyway. This simplifies some things. - It no longer needs to be flushed, because that's a no-op anyway for a Vec<u8>. - Writing to a Vec<u8> never fails. - No #[cfg(test)] code is needed anymore to use `realstd` instead of `std`, because Vec comes from alloc, not std (like Write).
2020-11-10Remove io::LocalOutput and use Arc<Mutex<dyn>> for local streams.Mara Bos-4/+23
2020-10-27Auto merge of #78227 - SergioBenitez:test-stdout-threading, r=m-ou-sebors-1/+1
Capture output from threads spawned in tests This is revival of #75172. Original text: > Fixes #42474. > > r? `@​dtolnay` since you expressed interest in this, but feel free to redirect if you aren't the right person anymore. --- Closes #75172.
2020-10-22Capture output from threads spawned in testsTyler Mandry-1/+1
Fixes #42474.
2020-10-19Throw core::panic!("message") as &str instead of String.Mara Bos-1/+17
This makes it consistent with std::panic!("message"), which also throws a &str, not a String.
2020-08-27Abort when catch_unwind catches a foreign exceptionAmanieu d'Antras-0/+8
2020-08-10Rollup merge of #74200 - ↵Yuki Okushi-6/+44
poliorcetics:std-panicking-unsafe-block-in-unsafe-fn, r=Mark-Simulacrum Std panicking unsafe block in unsafe fn Partial fix of #73904. This encloses `unsafe` operations in `unsafe fn` in `libstd/ffi/panicking.rs`. I also made a two lines change to `libstd/thread/local.rs` to add the necessary `unsafe` block without breaking everything else. @rustbot modify labels: F-unsafe-block-in-unsafe-fn
2020-08-07Prevent `__rust_begin_short_backtrace` frames from being tail-call optimised ↵Alan Egerton-2/+7
away
2020-07-29Disallow missing unsafe blocks in unsafe fn in panicking.rsAlexis Bourget-6/+44
This adds SAFETY comments where necessary, explaining the preconditions and how they are respected.