Skip to content

Commit e5e0a47

Browse files
committed
std: limit the length of the stack overflow backtrace
1 parent 7c5af8e commit e5e0a47

File tree

1 file changed

+7
-17
lines changed

1 file changed

+7
-17
lines changed

library/std/src/sys/pal/unix/stack_overflow/backtrace.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,9 @@ use crate::ptr;
99
/// even though both of them are not guaranteed to be async-signal-safe, strictly
1010
/// speaking. However, at least LLVM's libunwind (used by macOS) has a [test] for
1111
/// unwinding in signal handlers, and `dladdr` is used by `backtrace_symbols_fd`
12-
/// in glibc, which it [documents] as async-signal-safe.
13-
///
14-
/// In practice, this hack works well enough on GNU/Linux and macOS (and perhaps
15-
/// some other platforms). Realistically, the worst thing that can happen is that
16-
/// the stack overflow occurred inside the dynamic loaded while it holds some sort
17-
/// of lock, which could result in a deadlock if that happens in just the right
18-
/// moment. That's unlikely enough and not the *worst* thing to happen considering
19-
/// that a stack overflow is already an unrecoverable error and most likely
20-
/// indicates a bug.
12+
/// in glibc, which it [documents] as async-signal-safe. In practice, this hack
13+
/// works well enough on GNU/Linux and macOS (and perhaps some other platforms,
14+
/// but we haven't enabled those yet).
2115
///
2216
/// [test]: https://github.com/llvm/llvm-project/blob/a6385a3fc8a88f092d07672210a1e773481c2919/libunwind/test/signal_unwind.pass.cpp
2317
/// [documents]: https://www.gnu.org/software/libc/manual/html_node/Backtraces.html#index-backtrace_005fsymbols_005ffd
@@ -29,8 +23,8 @@ pub fn print() {
2923
let count = unsafe { &mut *(arg as *mut usize) };
3024
let depth = *count;
3125
*count += 1;
32-
if depth > 128 {
33-
return unwind::_URC_NO_REASON;
26+
if depth >= 128 {
27+
return unwind::_URC_NORMAL_STOP;
3428
}
3529

3630
let ip = unsafe { unwind::_Unwind_GetIP(ctx) };
@@ -53,11 +47,7 @@ pub fn print() {
5347

5448
let mut count = 0usize;
5549
unsafe { unwind::_Unwind_Backtrace(frame, ptr::from_mut(&mut count).cast()) };
56-
if count > 128 {
57-
rtprintpanic!(
58-
"[... omitted {} frame{} ...]\n",
59-
count - 128,
60-
if count - 128 > 1 { "s" } else { "" }
61-
);
50+
if count >= 128 {
51+
rtprintpanic!("[... some frames omitted ...]\n");
6252
}
6353
}

0 commit comments

Comments
 (0)