Skip to content

Add libstd Cargo feature "panic_immediate_abort" #55011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/libcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ path = "../libcore/benches/lib.rs"

[dev-dependencies]
rand = "0.5"

[features]
# Make panics and failed asserts immediately abort without formatting any message
panic_immediate_abort = []
24 changes: 21 additions & 3 deletions src/libcore/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@
use fmt;
use panic::{Location, PanicInfo};

#[cold] #[inline(never)] // this is the slow path, always
#[cold]
// inline(never) is required even in panic_immediate_abort mode, lest linker error
#[inline(never)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense to me that #[inline] would cause a linker error, but does omitting the attribute entirely also cause a link error? (it's best to avoid inline(never) if we can in panic-immediate-abort mode)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, omitting #[inline] resolved the linker error. Updated the patch.

#[lang = "panic"]
pub fn panic(expr_file_line_col: &(&'static str, &'static str, u32, u32)) -> ! {
if cfg!(feature = "panic_immediate_abort") {
unsafe { super::intrinsics::abort() }
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stray semicolon?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about code style recommendations in case of ()-returning if when called function does not return (). Where shall the semicolon go? As the result of if itself (like in this snippet) or inside the function? Or both? Or none?

Rustfmt seems to preserve both semicolons in

pub fn main() {
    if false {
        ();
    };
}

, so I assumed they should stay to remind it's not a part of larger expression, just a side-effects-full call.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the semicolon can just be removed, right? Typically semicolon after if indicates an assignment, but there's no assignment here


// Use Arguments::new_v1 instead of format_args!("{}", expr) to potentially
// reduce size overhead. The format_args! macro uses str's Display trait to
// write expr, which calls Formatter::pad, which must accommodate string
Expand All @@ -52,16 +58,28 @@ pub fn panic(expr_file_line_col: &(&'static str, &'static str, u32, u32)) -> ! {
panic_fmt(fmt::Arguments::new_v1(&[expr], &[]), &(file, line, col))
}

#[cold] #[inline(never)]
#[cold]
// inline(never) is required even in panic_immediate_abort mode, lest linker error
#[inline(never)]
#[lang = "panic_bounds_check"]
fn panic_bounds_check(file_line_col: &(&'static str, u32, u32),
index: usize, len: usize) -> ! {
if cfg!(feature = "panic_immediate_abort") {
unsafe { super::intrinsics::abort() }
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

semicolon as above


panic_fmt(format_args!("index out of bounds: the len is {} but the index is {}",
len, index), file_line_col)
}

#[cold] #[inline(never)]
#[cold]
#[cfg_attr(not(feature="panic_immediate_abort"),inline(never))]
#[cfg_attr( feature="panic_immediate_abort" ,inline)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could a brief comment be added for what this pattern is doing?

pub fn panic_fmt(fmt: fmt::Arguments, file_line_col: &(&'static str, u32, u32)) -> ! {
if cfg!(feature = "panic_immediate_abort") {
unsafe { super::intrinsics::abort() }
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stray semicolon


// NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call
#[allow(improper_ctypes)] // PanicInfo contains a trait object which is not FFI safe
extern "Rust" {
Expand Down
3 changes: 3 additions & 0 deletions src/libstd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ backtrace = []
panic-unwind = ["panic_unwind"]
profiler = ["profiler_builtins"]

# Make panics and failed asserts immediately abort without formatting any message
panic_immediate_abort = ["core/panic_immediate_abort"]

# An off-by-default feature which enables a linux-syscall-like ABI for libstd to
# interoperate with the host environment. Currently not well documented and
# requires rebuilding the standard library to use it.
Expand Down
17 changes: 15 additions & 2 deletions src/libstd/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,15 @@ pub fn rust_begin_panic(info: &PanicInfo) -> ! {
#[unstable(feature = "libstd_sys_internals",
reason = "used by the panic! macro",
issue = "0")]
#[inline(never)] #[cold]
#[cold]
#[cfg_attr(not(feature="panic_immediate_abort"),inline(never))]
#[cfg_attr( feature="panic_immediate_abort" ,inline)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could a comment be added here pointing to libcore's comment?

pub fn begin_panic_fmt(msg: &fmt::Arguments,
file_line_col: &(&'static str, u32, u32)) -> ! {
if cfg!(feature = "panic_immediate_abort") {
unsafe { intrinsics::abort() }
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stray semicolon


let (file, line, col) = *file_line_col;
let info = PanicInfo::internal_constructor(
Some(msg),
Expand Down Expand Up @@ -398,8 +404,15 @@ fn continue_panic_fmt(info: &PanicInfo) -> ! {
reason = "used by the panic! macro",
issue = "0")]
#[cfg_attr(not(test), lang = "begin_panic")]
#[inline(never)] #[cold] // avoid code bloat at the call sites as much as possible
// avoid code bloat at the call sites as much as possible
// inline(never) is required even in panic_immediate_abort mode, lest linker error
#[inline(never)]
#[cold]
pub fn begin_panic<M: Any + Send>(msg: M, file_line_col: &(&'static str, u32, u32)) -> ! {
if cfg!(feature = "panic_immediate_abort") {
unsafe { intrinsics::abort() }
};

// Note that this should be the only allocation performed in this code path.
// Currently this means that panic!() on OOM will invoke this code path,
// but then again we're not really ready for panic on OOM anyway. If
Expand Down