Skip to content

Instantly share code, notes, and snippets.

@nektro
Created October 20, 2022 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nektro/c7a3a3232e6dcb409664ed784be73529 to your computer and use it in GitHub Desktop.
Save nektro/c7a3a3232e6dcb409664ed784be73529 to your computer and use it in GitHub Desktop.
const std = @import("std");
extern fn sigaltstack(ss: *const stack_t, oss: *stack_t) c_int;
const stack_t = extern struct {
sp: ?*anyopaque,
flags: c_int,
size: usize,
};
var newstack: [4096]usize = undefined;
pub fn enable() void {
const new = stack_t{ .sp = &newstack, .flags = 0, .size = @sizeOf(@TypeOf(newstack)) };
var old = stack_t{ .sp = null, .flags = 0, .size = 0 };
std.debug.assert(sigaltstack(&new, &old) == 0);
_ = std.os.linux.sigaction(std.os.linux.SIG.SEGV, &.{
.handler = .{ .handler = handle },
.mask = std.mem.zeroes(std.os.linux.sigset_t),
.flags = std.os.linux.SA.NODEFER | std.os.linux.SA.ONSTACK,
}, null);
}
fn handle(s: c_int) callconv(.C) void {
std.debug.print("\n", .{});
_ = s;
std.log.err("caught stack overflow", .{});
std.debug.dumpCurrentStackTrace(@returnAddress());
std.os.abort();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment