std: use sigaltstack for default segfault handler #30801

Merged
mlugg merged 1 commit from stack-traces into master 2026-01-13 07:24:51 +01:00
Owner

This allows stack overflows to print stack traces. The size of the
sigaltstack (and whether it is actually set) can be configured by
setting std.Options.signal_stack_size.

The default value for the signal stack size was chosen experimentally by
doubling the value required to get stack traces on stack overflow with
the self-hosted x86_64 backend. While some targets may typically use
more stack space than x86_64-linux, the self-hosted x86_64 backend is
quite wasteful with stack at the moment, making it a fair benchmark.
Executables produced by the LLVM backend should have lower stack usage.


Example:

const use_thread = false;
pub fn main() !void {
    if (use_thread) {
        const t = try std.Thread.spawn(.{}, iEatStackForBreakfast, .{1_000_000});
        t.join();
    } else {
        iEatStackForBreakfast(1_000_000);
    }
    std.log.info("done", .{});
}
noinline fn iEatStackForBreakfast(n: u32) void {
    if (n != 0) iEatStackForBreakfast(n - 1);
}
const std = @import("std");
$ zig run stack_overflow.zig --zig-lib-dir master/lib
Segmentation fault         zig run stack_overflow.zig
$ zig run stack_overflow.zig --zig-lib-dir branch/lib
Segmentation fault at address 0x7ffc7e5e7ff8
/home/mlugg/zig/stack-traces/stack_overflow.zig:12:38: 0x11953dc in iEatStackForBreakfast (stack_overflow.zig)
    if (n != 0) iEatStackForBreakfast(n - 1);
                                     ^
/home/mlugg/zig/stack-traces/stack_overflow.zig:12:38: 0x11953e0 in iEatStackForBreakfast (stack_overflow.zig)
    if (n != 0) iEatStackForBreakfast(n - 1);
                                     ^
/home/mlugg/zig/stack-traces/stack_overflow.zig:12:38: 0x11953e0 in iEatStackForBreakfast (stack_overflow.zig)
    if (n != 0) iEatStackForBreakfast(n - 1);
                                     ^
/home/mlugg/zig/stack-traces/stack_overflow.zig:12:38: 0x11953e0 in iEatStackForBreakfast (stack_overflow.zig)
    if (n != 0) iEatStackForBreakfast(n - 1);
                                     ^

(for 10,000 frames). And same behavior with use_thread = true.

This allows stack overflows to print stack traces. The size of the sigaltstack (and whether it is actually set) can be configured by setting `std.Options.signal_stack_size`. The default value for the signal stack size was chosen experimentally by doubling the value required to get stack traces on stack overflow with the self-hosted x86_64 backend. While some targets may typically use more stack space than x86_64-linux, the self-hosted x86_64 backend is quite wasteful with stack at the moment, making it a fair benchmark. Executables produced by the LLVM backend should have lower stack usage. --- Example: ```zig const use_thread = false; pub fn main() !void { if (use_thread) { const t = try std.Thread.spawn(.{}, iEatStackForBreakfast, .{1_000_000}); t.join(); } else { iEatStackForBreakfast(1_000_000); } std.log.info("done", .{}); } noinline fn iEatStackForBreakfast(n: u32) void { if (n != 0) iEatStackForBreakfast(n - 1); } const std = @import("std"); ``` ``` $ zig run stack_overflow.zig --zig-lib-dir master/lib Segmentation fault zig run stack_overflow.zig $ zig run stack_overflow.zig --zig-lib-dir branch/lib Segmentation fault at address 0x7ffc7e5e7ff8 /home/mlugg/zig/stack-traces/stack_overflow.zig:12:38: 0x11953dc in iEatStackForBreakfast (stack_overflow.zig) if (n != 0) iEatStackForBreakfast(n - 1); ^ /home/mlugg/zig/stack-traces/stack_overflow.zig:12:38: 0x11953e0 in iEatStackForBreakfast (stack_overflow.zig) if (n != 0) iEatStackForBreakfast(n - 1); ^ /home/mlugg/zig/stack-traces/stack_overflow.zig:12:38: 0x11953e0 in iEatStackForBreakfast (stack_overflow.zig) if (n != 0) iEatStackForBreakfast(n - 1); ^ /home/mlugg/zig/stack-traces/stack_overflow.zig:12:38: 0x11953e0 in iEatStackForBreakfast (stack_overflow.zig) if (n != 0) iEatStackForBreakfast(n - 1); ^ ``` (for 10,000 frames). And same behavior with `use_thread = true`.
mlugg requested review from andrewrk 2026-01-12 13:49:46 +01:00
mlugg force-pushed stack-traces from 0fac8001dc
Some checks failed
ci / aarch64-linux-release (pull_request) Has been cancelled
ci / x86_64-freebsd-release (pull_request) Has been cancelled
ci / x86_64-freebsd-debug (pull_request) Has been cancelled
ci / aarch64-macos-release (pull_request) Has been cancelled
ci / aarch64-linux-debug (pull_request) Has been cancelled
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / aarch64-macos-debug (pull_request) Has been cancelled
ci / x86_64-openbsd-release (pull_request) Has been cancelled
ci / x86_64-openbsd-debug (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / powerpc64le-linux-debug (pull_request) Has been cancelled
ci / powerpc64le-linux-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
ci / x86_64-windows-release (pull_request) Has been cancelled
to dc937ec28f
Some checks failed
ci / aarch64-macos-release (pull_request) Successful in 28m41s
ci / x86_64-freebsd-release (pull_request) Successful in 35m34s
ci / x86_64-freebsd-debug (pull_request) Successful in 41m36s
ci / x86_64-windows-release (pull_request) Successful in 43m35s
ci / x86_64-openbsd-release (pull_request) Successful in 49m36s
ci / x86_64-windows-debug (pull_request) Successful in 52m21s
ci / x86_64-openbsd-debug (pull_request) Successful in 54m19s
ci / aarch64-macos-debug (pull_request) Successful in 1h0m7s
ci / x86_64-linux-debug (pull_request) Successful in 1h11m32s
ci / x86_64-linux-debug-llvm (pull_request) Successful in 1h25m34s
ci / aarch64-linux-release (pull_request) Successful in 1h25m45s
ci / aarch64-linux-debug (pull_request) Successful in 2h15m59s
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / powerpc64le-linux-debug (pull_request) Has been cancelled
ci / powerpc64le-linux-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Successful in 2h32m40s
2026-01-12 13:53:51 +01:00
Compare
andrewrk approved these changes 2026-01-13 06:10:04 +01:00
andrewrk left a comment

congrats, you closed #1616

congrats, you closed #1616
mlugg merged commit 85cac9e5b6 into master 2026-01-13 07:24:51 +01:00
mlugg deleted branch stack-traces 2026-01-13 07:24:52 +01:00
Sign in to join this conversation.
No reviewers
No milestone
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
ziglang/zig!30801
No description provided.