fix sigaction double panic

Fixes #8357
This commit is contained in:
zseri
2022-01-21 10:14:44 +01:00
committed by Veikka Tuominen
parent d62b1c932e
commit c6cf40a0c0
4 changed files with 19 additions and 15 deletions

View File

@@ -1689,6 +1689,12 @@ pub fn maybeEnableSegfaultHandler() void {
var windows_segfault_handle: ?windows.HANDLE = null;
pub fn updateSegfaultHandler(act: ?*const os.Sigaction) error{OperationNotSupported}!void {
try os.sigaction(os.SIG.SEGV, act, null);
try os.sigaction(os.SIG.ILL, act, null);
try os.sigaction(os.SIG.BUS, act, null);
}
/// Attaches a global SIGSEGV handler which calls @panic("segmentation fault");
pub fn attachSegfaultHandler() void {
if (!have_segfault_handling_support) {
@@ -1704,9 +1710,9 @@ pub fn attachSegfaultHandler() void {
.flags = (os.SA.SIGINFO | os.SA.RESTART | os.SA.RESETHAND),
};
os.sigaction(os.SIG.SEGV, &act, null);
os.sigaction(os.SIG.ILL, &act, null);
os.sigaction(os.SIG.BUS, &act, null);
updateSegfaultHandler(&act) catch {
@panic("unable to install segfault handler, maybe adjust have_segfault_handling_support in std/debug.zig");
};
}
fn resetSegfaultHandler() void {
@@ -1722,9 +1728,8 @@ fn resetSegfaultHandler() void {
.mask = os.empty_sigset,
.flags = 0,
};
os.sigaction(os.SIG.SEGV, &act, null);
os.sigaction(os.SIG.ILL, &act, null);
os.sigaction(os.SIG.BUS, &act, null);
// do nothing if an error happens to avoid a double-panic
updateSegfaultHandler(&act) catch {};
}
fn handleSegfaultPosix(sig: i32, info: *const os.siginfo_t, ctx_ptr: ?*const anyopaque) callconv(.C) noreturn {

View File

@@ -5521,11 +5521,10 @@ pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) SigaltstackError!void {
}
/// Examine and change a signal action.
pub fn sigaction(sig: u6, act: ?*const Sigaction, oact: ?*Sigaction) void {
pub fn sigaction(sig: u6, noalias act: ?*const Sigaction, noalias oact: ?*Sigaction) error{OperationNotSupported}!void {
switch (errno(system.sigaction(sig, act, oact))) {
.SUCCESS => return,
.FAULT => unreachable,
.INVAL => unreachable,
.INVAL, .NOSYS => return error.OperationNotSupported,
else => unreachable,
}
}

View File

@@ -772,16 +772,16 @@ test "sigaction" {
};
var old_sa: os.Sigaction = undefined;
// Install the new signal handler.
os.sigaction(os.SIG.USR1, &sa, null);
try os.sigaction(os.SIG.USR1, &sa, null);
// Check that we can read it back correctly.
os.sigaction(os.SIG.USR1, null, &old_sa);
try os.sigaction(os.SIG.USR1, null, &old_sa);
try testing.expectEqual(S.handler, old_sa.handler.sigaction.?);
try testing.expect((old_sa.flags & os.SA.SIGINFO) != 0);
// Invoke the handler.
try os.raise(os.SIG.USR1);
try testing.expect(signal_test_failed == false);
// Check if the handler has been correctly reset to SIG_DFL
os.sigaction(os.SIG.USR1, null, &old_sa);
try os.sigaction(os.SIG.USR1, null, &old_sa);
try testing.expectEqual(os.SIG.DFL, old_sa.handler.sigaction);
}

View File

@@ -175,9 +175,9 @@ pub fn attachSegfaultHandler() void {
.flags = (os.SA.SIGINFO | os.SA.RESTART | os.SA.RESETHAND),
};
os.sigaction(os.SIG.SEGV, &act, null);
os.sigaction(os.SIG.ILL, &act, null);
os.sigaction(os.SIG.BUS, &act, null);
debug.updateSegfaultHandler(&act) catch {
@panic("unable to install segfault handler, maybe adjust have_segfault_handling_support in std/debug.zig");
};
}
fn handleSegfaultPosix(sig: i32, info: *const os.siginfo_t, ctx_ptr: ?*const anyopaque) callconv(.C) noreturn {