authorgravatar for zseri.devel@ytrizja.dezseri <zseri.devel@ytrizja.de> 2022-01-21 10:14:44+01:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-03-19 19:13:31+02:00
logc6cf40a0c03822cac3112be58c61ca55d436b5d0
treebbdfd198a258464b23ed84b1534d7f193d646e07
parentd62b1c932e5c5ff9cd3e14b4f3f8c078a0fb43f9

fix sigaction double panic

Fixes #8357

4 files changed, 19 insertions(+), 15 deletions(-)

lib/std/debug.zig+11-6
......@@ -1689,6 +1689,12 @@ pub fn maybeEnableSegfaultHandler() void {
16891689
16901690var windows_segfault_handle: ?windows.HANDLE = null;
16911691
1692pub fn updateSegfaultHandler(act: ?*const os.Sigaction) error{OperationNotSupported}!void {
1693 try os.sigaction(os.SIG.SEGV, act, null);
1694 try os.sigaction(os.SIG.ILL, act, null);
1695 try os.sigaction(os.SIG.BUS, act, null);
1696}
1697
16921698/// Attaches a global SIGSEGV handler which calls @panic("segmentation fault");
16931699pub fn attachSegfaultHandler() void {
16941700 if (!have_segfault_handling_support) {
......@@ -1704,9 +1710,9 @@ pub fn attachSegfaultHandler() void {
17041710 .flags = (os.SA.SIGINFO | os.SA.RESTART | os.SA.RESETHAND),
17051711 };
17061712
1707 os.sigaction(os.SIG.SEGV, &act, null);
1708 os.sigaction(os.SIG.ILL, &act, null);
1709 os.sigaction(os.SIG.BUS, &act, null);
1713 updateSegfaultHandler(&act) catch {
1714 @panic("unable to install segfault handler, maybe adjust have_segfault_handling_support in std/debug.zig");
1715 };
17101716}
17111717
17121718fn resetSegfaultHandler() void {
......@@ -1722,9 +1728,8 @@ fn resetSegfaultHandler() void {
17221728 .mask = os.empty_sigset,
17231729 .flags = 0,
17241730 };
1725 os.sigaction(os.SIG.SEGV, &act, null);
1726 os.sigaction(os.SIG.ILL, &act, null);
1727 os.sigaction(os.SIG.BUS, &act, null);
1731 // do nothing if an error happens to avoid a double-panic
1732 updateSegfaultHandler(&act) catch {};
17281733}
17291734
17301735fn handleSegfaultPosix(sig: i32, info: *const os.siginfo_t, ctx_ptr: ?*const anyopaque) callconv(.C) noreturn {
lib/std/os.zig+2-3
......@@ -5521,11 +5521,10 @@ pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) SigaltstackError!void {
55215521}
55225522
55235523/// Examine and change a signal action.
5524pub fn sigaction(sig: u6, act: ?*const Sigaction, oact: ?*Sigaction) void {
5524pub fn sigaction(sig: u6, noalias act: ?*const Sigaction, noalias oact: ?*Sigaction) error{OperationNotSupported}!void {
55255525 switch (errno(system.sigaction(sig, act, oact))) {
55265526 .SUCCESS => return,
5527 .FAULT => unreachable,
5528 .INVAL => unreachable,
5527 .INVAL, .NOSYS => return error.OperationNotSupported,
55295528 else => unreachable,
55305529 }
55315530}
lib/std/os/test.zig+3-3
......@@ -772,16 +772,16 @@ test "sigaction" {
772772 };
773773 var old_sa: os.Sigaction = undefined;
774774 // Install the new signal handler.
775 os.sigaction(os.SIG.USR1, &sa, null);
775 try os.sigaction(os.SIG.USR1, &sa, null);
776776 // Check that we can read it back correctly.
777 os.sigaction(os.SIG.USR1, null, &old_sa);
777 try os.sigaction(os.SIG.USR1, null, &old_sa);
778778 try testing.expectEqual(S.handler, old_sa.handler.sigaction.?);
779779 try testing.expect((old_sa.flags & os.SA.SIGINFO) != 0);
780780 // Invoke the handler.
781781 try os.raise(os.SIG.USR1);
782782 try testing.expect(signal_test_failed == false);
783783 // Check if the handler has been correctly reset to SIG_DFL
784 os.sigaction(os.SIG.USR1, null, &old_sa);
784 try os.sigaction(os.SIG.USR1, null, &old_sa);
785785 try testing.expectEqual(os.SIG.DFL, old_sa.handler.sigaction);
786786}
787787
src/crash_report.zig+3-3
......@@ -175,9 +175,9 @@ pub fn attachSegfaultHandler() void {
175175 .flags = (os.SA.SIGINFO | os.SA.RESTART | os.SA.RESETHAND),
176176 };
177177
178 os.sigaction(os.SIG.SEGV, &act, null);
179 os.sigaction(os.SIG.ILL, &act, null);
180 os.sigaction(os.SIG.BUS, &act, null);
178 debug.updateSegfaultHandler(&act) catch {
179 @panic("unable to install segfault handler, maybe adjust have_segfault_handling_support in std/debug.zig");
180 };
181181}
182182
183183fn handleSegfaultPosix(sig: i32, info: *const os.siginfo_t, ctx_ptr: ?*const anyopaque) callconv(.C) noreturn {