Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit ab3924b

Browse files
committed
Auto merge of rust-lang#122565 - Zoxc:atomic-panic-msg, r=the8472
Try to write the panic message with a single `write_all` call This writes the panic message to a buffer before writing to stderr. This allows it to be printed with a single `write_all` call, preventing it from being interleaved with other outputs. It also adds newlines before and after the message ensuring that only the panic message will have its own lines. Before: ``` thread 'thread 'thread 'thread 'thread '<unnamed>thread 'thread 'thread 'thread '<unnamed><unnamed>thread '<unnamed>' panicked at ' panicked at <unnamed><unnamed><unnamed><unnamed><unnamed>' panicked at <unnamed>' panicked at src\heap.rssrc\heap.rs' panicked at ' panicked at ' panicked at ' panicked at ' panicked at src\heap.rs' panicked at src\heap.rs::src\heap.rssrc\heap.rssrc\heap.rssrc\heap.rssrc\heap.rs:src\heap.rs:455455:::::455:455::455455455455455:455:99:::::9:9: : 999: 999: assertion failed: size <= (*queue).block_size: : assertion failed: size <= (*queue).block_size: assertion failed: size <= (*queue).block_size: : : assertion failed: size <= (*queue).block_sizeassertion failed: size <= (*queue).block_sizeassertion failed: size <= (*queue).block_size assertion failed: size <= (*queue).block_size assertion failed: size <= (*queue).block_sizeassertion failed: size <= (*queue).block_sizeerror: process didn't exit successfully: `target\debug\direct_test.exe` (exit code: 0xc0000409, STATUS_STACK_BUFFER_OVERRUN) ``` After: ``` thread '<unnamed>' panicked at src\heap.rs:455:9: assertion failed: size <= (*queue).block_size thread '<unnamed>' panicked at src\heap.rs:455:9: assertion failed: size <= (*queue).block_size thread '<unnamed>' panicked at src\heap.rs:455:9: assertion failed: size <= (*queue).block_size error: process didn't exit successfully: `target\debug\direct_test.exe` (exit code: 0xc0000409, STATUS_STACK_BUFFER_OVERRUN) ``` --- try-jobs: x86_64-gnu-llvm-18
2 parents 4363f9b + 4bf85c2 commit ab3924b

File tree

85 files changed

+153
-20
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+153
-20
lines changed

library/std/src/panicking.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,23 @@ fn default_hook(info: &PanicHookInfo<'_>) {
266266
// Use a lock to prevent mixed output in multithreading context.
267267
// Some platforms also require it when printing a backtrace, like `SymFromAddr` on Windows.
268268
let mut lock = backtrace::lock();
269-
let _ = writeln!(err, "thread '{name}' panicked at {location}:\n{msg}");
269+
// Try to write the panic message to a buffer first to prevent other concurrent outputs
270+
// interleaving with it.
271+
let mut buffer = [0u8; 512];
272+
let mut cursor = crate::io::Cursor::new(&mut buffer[..]);
273+
274+
let write_msg = |dst: &mut dyn crate::io::Write| {
275+
// We add a newline to ensure the panic message appears at the start of a line.
276+
writeln!(dst, "\nthread '{name}' panicked at {location}:\n{msg}")
277+
};
278+
279+
if write_msg(&mut cursor).is_ok() {
280+
let pos = cursor.position() as usize;
281+
let _ = err.write_all(&buffer[0..pos]);
282+
} else {
283+
// The message did not fit into the buffer, write it directly instead.
284+
let _ = write_msg(err);
285+
};
270286

271287
static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
272288

src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind1.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at tests/fail/function_calls/exported_symbol_bad_unwind1.rs:LL:CC:
23
explicit panic
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind2.both.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
12
thread 'main' panicked at tests/fail/function_calls/exported_symbol_bad_unwind2.rs:LL:CC:
23
explicit panic
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
45
note: in Miri, you may have to set `MIRIFLAGS=-Zmiri-env-forward=RUST_BACKTRACE` for the environment variable to have an effect
6+
57
thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
68
panic in a function that cannot unwind
79
stack backtrace:

src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind2.definition.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
12
thread 'main' panicked at tests/fail/function_calls/exported_symbol_bad_unwind2.rs:LL:CC:
23
explicit panic
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
45
note: in Miri, you may have to set `MIRIFLAGS=-Zmiri-env-forward=RUST_BACKTRACE` for the environment variable to have an effect
6+
57
thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
68
panic in a function that cannot unwind
79
stack backtrace:

src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind2.extern_block.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at tests/fail/function_calls/exported_symbol_bad_unwind2.rs:LL:CC:
23
explicit panic
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

src/tools/miri/tests/fail/function_calls/return_pointer_on_unwind.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at tests/fail/function_calls/return_pointer_on_unwind.rs:LL:CC:
23
explicit panic
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

src/tools/miri/tests/fail/intrinsics/uninit_uninhabited_type.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
23
aborted execution: attempted to instantiate uninhabited type `!`
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

src/tools/miri/tests/fail/intrinsics/zero_fn_ptr.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
23
aborted execution: attempted to zero-initialize type `fn()`, which is invalid
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

src/tools/miri/tests/fail/panic/abort_unwind.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
12
thread 'main' panicked at tests/fail/panic/abort_unwind.rs:LL:CC:
23
PANIC!!!
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
45
note: in Miri, you may have to set `MIRIFLAGS=-Zmiri-env-forward=RUST_BACKTRACE` for the environment variable to have an effect
6+
57
thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
68
panic in a function that cannot unwind
79
stack backtrace:

src/tools/miri/tests/fail/panic/bad_unwind.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at tests/fail/panic/bad_unwind.rs:LL:CC:
23
explicit panic
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

0 commit comments

Comments
 (0)