Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
176 changes: 24 additions & 152 deletions src/crash_handler/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,9 +616,9 @@ mod draft {

/// This structure and formatter must be kept in sync with `bun.report`'s decoder implementation.
#[derive(Clone, Copy)]
pub enum CrashReason {
pub enum CrashReason<'a> {
/// From @panic()
Panic(&'static [u8]),
Panic(&'a [u8]),
/// "reached unreachable code"
Unreachable,

Expand All @@ -639,12 +639,12 @@ mod draft {
StackOverflow,

/// Either `main` returned an error, or somewhere else in the code a trace string is printed.
ZigError(&'static [u8]),
ZigError(&'a [u8]),

OutOfMemory,
}

impl CrashReason {
impl CrashReason<'_> {
/// Signal to terminate the process with after the crash report has
/// been printed. Signal-originated crashes re-raise the original
/// fault so the parent process (and core-dump analyzers) see the
Expand All @@ -669,7 +669,7 @@ mod draft {
}
}

impl fmt::Display for CrashReason {
impl fmt::Display for CrashReason<'_> {
fn fmt(&self, writer: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CrashReason::Panic(message) => write!(writer, "{}", bstr::BStr::new(message)),
Expand Down Expand Up @@ -788,7 +788,7 @@ mod draft {

/// This function is invoked when a crash happens. A crash is classified in `CrashReason`.
#[cold]
pub fn crash_handler(reason: CrashReason, seed: TraceSeed<'_>) -> ! {
pub fn crash_handler(reason: CrashReason<'_>, seed: TraceSeed<'_>) -> ! {
if cfg!(debug_assertions) {
Output::disable_scoped_debug_writer();
}
Expand Down Expand Up @@ -1471,8 +1471,7 @@ mod draft {
if msg == b"reached unreachable code" {
CrashReason::Unreachable
} else {
// SAFETY: process is about to abort; the borrow is never invalidated.
CrashReason::Panic(unsafe { bun_collections::detach_lifetime(msg) })
CrashReason::Panic(msg)
},
match error_return_trace {
Some(ert) if ert.index > 0 => TraceSeed::ErrorReturn(ert),
Expand Down Expand Up @@ -1733,144 +1732,21 @@ mod draft {
std::panic::set_hook(Box::new(rust_panic_hook));
}

/// `std::panic` hook: emit the same trace-string + auto-report as the fatal
/// `crash_handler()` path, then **abort**.
/// With `panic = "abort"` no unwind starts after this hook returns, so there
/// are no `catch_unwind` boundaries to reach.
/// Omits `info.location()`: release builds use `-Zlocation-detail=none`; the trace has the site.
#[cold]
#[inline(never)]
fn rust_panic_hook(info: &std::panic::PanicHookInfo<'_>) {
// Re-entry guard: if the hook itself panics (formatter, write, …), the
// recursive entry sees stage>0, prints a one-liner, and returns so the
// inner unwind tears the process down rather than looping.
let stage = PANIC_STAGE.with(|s| s.get());
if stage != 0 {
PANIC_STAGE.with(|s| s.set(stage + 1));
let stderr = &mut stderr_writer();
let _ = write!(
stderr,
"\npanic: {info}\npanicked during a panic. Aborting.\n"
);
return;
}
PANIC_STAGE.with(|s| s.set(1));
/// The trace string must fit the compressed message in a fixed buffer (`encode_trace_string`).
const MAX_MESSAGE_BYTES: usize = 1024;

// Just the panic message — no `(file:line:col)` suffix. The call site is
// captured in the backtrace and symbolized there. With `-Zlocation-detail=none`
// in release the location would be `<redacted>:0:0` anyway.
let mut msg_buf = BoundedArray::<u8, 1024>::default();
{
let payload = info.payload();
let msg: &str = if let Some(s) = payload.downcast_ref::<&'static str>() {
s
} else if let Some(s) = payload.downcast_ref::<std::string::String>() {
s.as_str()
} else {
"<non-string panic payload>"
};
let _ = write!(msg_buf.writer(), "{msg}");
}
// SAFETY: `CrashReason::Panic` stores `&'static [u8]` (it was designed for
// the `-> !` path). `msg_buf` outlives every read of `reason` below — the
// borrow is fully consumed by the `Display`/`TraceString` writes inside
// this frame and never escapes.
let reason =
CrashReason::Panic(unsafe { bun_collections::detach_lifetime(msg_buf.const_slice()) });

let mut trace_str_buf = BoundedArray::<u8, 1024>::default();
{
let _panic_guard = PANIC_MUTEX.lock();
let writer = &mut stderr_writer();

let debug_trace = Environment::SHOW_CRASH_TRACE
&& 'check_flag: {
for arg in bun_core::argv() {
if arg == &b"--debug-crash-handler-use-trace-string"[..] {
break 'check_flag false;
}
}
if is_reporting_enabled() {
break 'check_flag false;
}
true
};

Output::flush();
let _ =
writer.write_all(b"============================================================\n");
let _ = print_metadata(writer);

if enable_ansi_colors_stderr() {
let _ = writer.write_all(&Output::pretty_fmt::<true>("<red>"));
}
let _ = writer.write_all(b"panic");
if enable_ansi_colors_stderr() {
let _ = writer.write_all(&Output::pretty_fmt::<true>("<r>"));
}
let _ = writeln!(writer, ": {}", reason);

if let Some(action) = CURRENT_ACTION.with(|c| c.get()) {
let _ = writeln!(writer, "Crashed while {}", action);
}

let mut addr_buf: [usize; 20] = [0; 20];
let idx = debug::capture_stack_trace(debug::return_address(), &mut addr_buf);
let trace = StackTrace {
index: idx,
instruction_addresses: &addr_buf,
};

if debug_trace {
dump_stack_trace(&trace, WriteStackTraceLimits::default());
let _ = write!(
trace_str_buf.writer(),
"{}",
TraceString {
trace: &trace,
reason,
action: TraceStringAction::ViewTrace,
}
);
} else {
let _ = writer.write_all(b"oh no");
if enable_ansi_colors_stderr() {
let _ = writer.write_all(&Output::pretty_fmt::<true>("<r><d>:<r> "));
} else {
let _ = writer.write_all(b": ");
}
let _ = writer.write_all(
b"Bun has crashed. This indicates a bug in Bun, not your code.\n\n\
To send a redacted crash report to Bun's team,\n\
please file a GitHub issue using the link below:\n\n ",
);
if enable_ansi_colors_stderr() {
let _ = writer.write_all(&Output::pretty_fmt::<true>("<cyan>"));
}
let _ = write!(
trace_str_buf.writer(),
"{}",
TraceString {
trace: &trace,
reason,
action: TraceStringAction::OpenIssue,
}
);
let _ = writer.write_all(trace_str_buf.const_slice());
let _ = writer.write_all(b"\n");
}
if enable_ansi_colors_stderr() {
let _ = writer.write_all(&Output::pretty_fmt::<true>("<r>\n"));
} else {
let _ = writer.write_all(b"\n");
}
}

report(trace_str_buf.const_slice());

// A Rust `panic!` is a bug. The process must not continue — with
// `panic = "abort"` no unwind starts, so `catch_unwind` boundaries are
// unreachable for Rust panics.
crash(reason);
let msg = info
.payload_as_str()
.unwrap_or("<non-string panic payload>");
let msg = &msg[..msg.floor_char_boundary(MAX_MESSAGE_BYTES)];
crash_handler(
CrashReason::Panic(msg.as_bytes()),
TraceSeed::BeginAddr(debug::return_address()),
);
}

/// Adapter for non-fatal `bun_core::dump_current_stack_trace` callers
Expand Down Expand Up @@ -1971,7 +1847,7 @@ mod draft {
#[cfg(windows)]
fn classify_exception_windows(
record: &bun_sys::windows::EXCEPTION_RECORD,
) -> Option<CrashReason> {
) -> Option<CrashReason<'static>> {
Some(match record.ExceptionCode {
bun_sys::windows::EXCEPTION_DATATYPE_MISALIGNMENT => CrashReason::DatatypeMisalignment,
bun_sys::windows::EXCEPTION_ACCESS_VIOLATION => {
Expand Down Expand Up @@ -2595,7 +2471,7 @@ mod draft {

struct TraceString<'a> {
trace: &'a StackTrace<'a>,
reason: CrashReason,
reason: CrashReason<'a>,
action: TraceStringAction,
}

Expand Down Expand Up @@ -2961,7 +2837,7 @@ mod draft {
/// On POSIX this re-raises the signal that caused the crash (or SIGABRT
/// for panics) so the parent sees the real fault and core dumps are
/// attributed correctly.
fn crash(reason: CrashReason) -> ! {
fn crash(reason: CrashReason<'_>) -> ! {
#[cfg(not(windows))]
{
let sig = reason.terminal_signal();
Expand Down Expand Up @@ -3065,12 +2941,9 @@ mod draft {
Output::flush();
dump_stack_trace(trace, WriteStackTraceLimits::default());
} else {
// SAFETY: `err_name` outlives the local `TraceString` it is formatted through.
let reason =
CrashReason::ZigError(unsafe { bun_collections::detach_lifetime(err_name) });
let ts = TraceString {
trace,
reason,
reason: CrashReason::ZigError(err_name),
action: TraceStringAction::ViewTrace,
};
if IS_ROOT {
Expand Down Expand Up @@ -3740,7 +3613,7 @@ mod draft {
// SAFETY: per the caller contract above, `name` is a valid NUL-terminated C string (non-null).
let name_bytes = unsafe { bun_core::ffi::cstr(name) }.to_bytes();
// PORTING.md §Forbidden: no Box::leak. We're on the noreturn path, so a stack
// buffer suffices — `panic_impl` erases to &'static for the abort path.
// buffer suffices.
let mut msg = BoundedArray::<u8, 256>::default();
let _ = write!(
msg.writer(),
Expand All @@ -3757,8 +3630,7 @@ mod draft {
// SAFETY: caller passes a valid (ptr, len) byte slice
let msg = unsafe { core::slice::from_raw_parts(message_ptr, message_len) };
crash_handler(
// SAFETY: noreturn — see panic_impl note
CrashReason::Panic(unsafe { bun_collections::detach_lifetime(msg) }),
CrashReason::Panic(msg),
TraceSeed::BeginAddr(debug::return_address()),
);
}
Expand Down
4 changes: 4 additions & 0 deletions src/js/internal-for-testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ export const crash_handler = $rust("crash_handler.rs", "js_bindings.generate") a
getMachOImageZeroOffset: () => number;
segfault: () => void;
panic: () => void;
/** A real Rust `panic!` (goes through the `std::panic` hook, unlike `panic()` above), with `message` if given. */
rustPanic: (message?: string) => void;
/** A real `Result::unwrap()` on an `Err`, also reported through the `std::panic` hook. */
rustUnwrap: () => void;
rootError: () => void;
outOfMemory: () => void;
abort: () => void;
Expand Down
24 changes: 24 additions & 0 deletions src/runtime/api/crash_handler_jsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub(crate) mod js_bindings {
("segfault", __jsc_host_js_segfault),
("segfaultInDll", __jsc_host_js_segfault_in_dll),
("panic", __jsc_host_js_panic),
("rustPanic", __jsc_host_js_rust_panic),
("rustUnwrap", __jsc_host_js_rust_unwrap),
("rootError", __jsc_host_js_root_error),
("outOfMemory", __jsc_host_js_out_of_memory),
("abort", __jsc_host_js_abort),
Expand Down Expand Up @@ -132,6 +134,28 @@ pub(crate) mod js_bindings {
crash_handler::panic_impl(b"invoked crashByPanic() handler", None, None);
}

/// A real `panic!` (reaches the crash handler through the std panic hook, unlike `js_panic`).
#[bun_jsc::host_fn]
fn js_rust_panic(global: &JSGlobalObject, frame: &CallFrame) -> JsResult<JSValue> {
crash_handler::suppress_core_dumps_if_necessary();
let message = frame.argument(0);
if message.is_undefined() {
panic!("invoked crashByRustPanic() handler");
}
let message = message.to_slice(global)?;
panic!("{}", bstr::BStr::new(message.slice()));
}

/// A real `Result::unwrap()` on an `Err`; std formats the message, so the hook sees a `String` payload.
#[bun_jsc::host_fn]
fn js_rust_unwrap(_global: &JSGlobalObject, _frame: &CallFrame) -> JsResult<JSValue> {
crash_handler::suppress_core_dumps_if_necessary();
let result: Result<(), &str> =
core::hint::black_box(Err("invoked crashByRustUnwrap() handler"));
result.unwrap();
Ok(JSValue::UNDEFINED)
}

#[bun_jsc::host_fn]
fn js_abort(_global: &JSGlobalObject, _frame: &CallFrame) -> JsResult<JSValue> {
crash_handler::suppress_core_dumps_if_necessary();
Expand Down
4 changes: 1 addition & 3 deletions test/cli/run/crash-report-command-char.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { describe, expect, test } from "bun:test";
import { bunEnv, bunExe, mergeWindowEnvs, tempDir } from "harness";
import path from "path";

// NOTE: kept separate from run-crash-handler.test.ts on purpose — that file
// is skip-listed in test/expectations.txt (its segfault-reporter test is
// broken), so anything added there never runs in CI.
// The rest of the crash handler's coverage lives in run-crash-handler.test.ts.
describe.concurrent("crash report command character", () => {
// Crash while a given subcommand is running and return the command
// character from the trace string printed to stderr:
Expand Down
3 changes: 2 additions & 1 deletion test/cli/run/fixture-crash.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ if (approach in crash_handler) {
crash_handler[approach]();
} else {
console.error(
"usage: bun fixture-crash.js <segfault|segfaultInDll|panic|rootError|outOfMemory|abort|trap|raiseIgnoringPanicHandler>",
"usage: bun fixture-crash.js <segfault|segfaultInDll|panic|rustPanic|rustUnwrap|rootError|outOfMemory|abort|trap|raiseIgnoringPanicHandler>",
);
process.exit(1);
}
Loading
Loading