From 7d6a7f513b0053be4e1629de492cbde9215c442c Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Thu, 11 Aug 2022 11:04:26 +0200 Subject: [PATCH 1/2] std: Don't pass undefined memory to the kernel in os.abort() --- lib/std/os.zig | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/std/os.zig b/lib/std/os.zig index 1192c726298e8919ab6dce959a1c26409ccb8494..b068a11229b54c93de90afed384f2321b0dbc041 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -476,9 +476,8 @@ pub fn abort() noreturn { // Install default handler so that the tkill below will terminate. const sigact = Sigaction{ .handler = .{ .sigaction = SIG.DFL }, - .mask = undefined, - .flags = undefined, - .restorer = undefined, + .mask = empty_sigset, + .flags = 0, }; sigaction(SIG.ABRT, &sigact, null) catch |err| switch (err) { error.OperationNotSupported => unreachable, -- 2.54.0 From b78f3bf1f17c7d67ae30d95047e40efc164bf8af Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Thu, 11 Aug 2022 10:56:21 +0200 Subject: [PATCH 2/2] std: fix definition of SIG_IGN, SIG_DFL, etc. POSIX specifies that the sa_handler field of the sigaction struct may be set to SIG_IGN or SIG_DFL. However, the current constants in the standard library use the function pointer signature corresponding to the sa_sigaction field instead. This may not cause issues in practice because the fields usually occupy the same memory in a union, but this isn't required by POSIX and there may be systems we do not yet support that do this differently. Fixing this also makes the Zig interface less confusing to use after reading the man page. --- lib/std/c/darwin.zig | 8 ++++---- lib/std/c/dragonfly.zig | 6 +++--- lib/std/c/freebsd.zig | 6 +++--- lib/std/c/netbsd.zig | 6 +++--- lib/std/c/openbsd.zig | 10 +++++----- lib/std/c/solaris.zig | 8 ++++---- lib/std/debug.zig | 2 +- lib/std/os.zig | 2 +- lib/std/os/linux.zig | 18 +++++++++--------- lib/std/os/test.zig | 2 +- 10 files changed, 34 insertions(+), 34 deletions(-) diff --git a/lib/std/c/darwin.zig b/lib/std/c/darwin.zig index e7fd3cde44e82d80518f0c66bbf37e0ba5ff520e..0a65fa52429ea9a6f23fef3e08e2d56b0cf4c9b7 100644 --- a/lib/std/c/darwin.zig +++ b/lib/std/c/darwin.zig @@ -814,10 +814,10 @@ pub const sigset_t = u32; pub const empty_sigset: sigset_t = 0; pub const SIG = struct { - pub const ERR = @intToPtr(?Sigaction.sigaction_fn, maxInt(usize)); - pub const DFL = @intToPtr(?Sigaction.sigaction_fn, 0); - pub const IGN = @intToPtr(?Sigaction.sigaction_fn, 1); - pub const HOLD = @intToPtr(?Sigaction.sigaction_fn, 5); + pub const ERR = @intToPtr(?Sigaction.handler_fn, maxInt(usize)); + pub const DFL = @intToPtr(?Sigaction.handler_fn, 0); + pub const IGN = @intToPtr(?Sigaction.handler_fn, 1); + pub const HOLD = @intToPtr(?Sigaction.handler_fn, 5); /// block specified signal set pub const _BLOCK = 1; diff --git a/lib/std/c/dragonfly.zig b/lib/std/c/dragonfly.zig index 1a60f94a1ec2a402343e3bb2fb3b0527573a221a..5d7822e8e9b925e12524ee7cbca4a786bf144a5c 100644 --- a/lib/std/c/dragonfly.zig +++ b/lib/std/c/dragonfly.zig @@ -609,9 +609,9 @@ pub const S = struct { pub const BADSIG = SIG.ERR; pub const SIG = struct { - pub const DFL = @intToPtr(?Sigaction.sigaction_fn, 0); - pub const IGN = @intToPtr(?Sigaction.sigaction_fn, 1); - pub const ERR = @intToPtr(?Sigaction.sigaction_fn, maxInt(usize)); + pub const DFL = @intToPtr(?Sigaction.handler_fn, 0); + pub const IGN = @intToPtr(?Sigaction.handler_fn, 1); + pub const ERR = @intToPtr(?Sigaction.handler_fn, maxInt(usize)); pub const BLOCK = 1; pub const UNBLOCK = 2; diff --git a/lib/std/c/freebsd.zig b/lib/std/c/freebsd.zig index dac239094e4a1a640f4a72a39d205be561df3c23..6f0188698662308f2f91093c48096495c252bc7d 100644 --- a/lib/std/c/freebsd.zig +++ b/lib/std/c/freebsd.zig @@ -670,9 +670,9 @@ pub const SIG = struct { pub const UNBLOCK = 2; pub const SETMASK = 3; - pub const DFL = @intToPtr(?Sigaction.sigaction_fn, 0); - pub const IGN = @intToPtr(?Sigaction.sigaction_fn, 1); - pub const ERR = @intToPtr(?Sigaction.sigaction_fn, maxInt(usize)); + pub const DFL = @intToPtr(?Sigaction.handler_fn, 0); + pub const IGN = @intToPtr(?Sigaction.handler_fn, 1); + pub const ERR = @intToPtr(?Sigaction.handler_fn, maxInt(usize)); pub const WORDS = 4; pub const MAXSIG = 128; diff --git a/lib/std/c/netbsd.zig b/lib/std/c/netbsd.zig index 3de14da7a26af0184e14aa898ca718f2307df8ac..a8287033d75e9fa4adce7231e6917632ff80cb48 100644 --- a/lib/std/c/netbsd.zig +++ b/lib/std/c/netbsd.zig @@ -910,9 +910,9 @@ pub const winsize = extern struct { const NSIG = 32; pub const SIG = struct { - pub const DFL = @intToPtr(?Sigaction.sigaction_fn, 0); - pub const IGN = @intToPtr(?Sigaction.sigaction_fn, 1); - pub const ERR = @intToPtr(?Sigaction.sigaction_fn, maxInt(usize)); + pub const DFL = @intToPtr(?Sigaction.handler_fn, 0); + pub const IGN = @intToPtr(?Sigaction.handler_fn, 1); + pub const ERR = @intToPtr(?Sigaction.handler_fn, maxInt(usize)); pub const WORDS = 4; pub const MAXSIG = 128; diff --git a/lib/std/c/openbsd.zig b/lib/std/c/openbsd.zig index 0863cc5a5e794819c09cf0aa7ec25c8339566a1a..5c259bc41ae3c929cda4d4f56c9591dca48f218c 100644 --- a/lib/std/c/openbsd.zig +++ b/lib/std/c/openbsd.zig @@ -982,11 +982,11 @@ pub const winsize = extern struct { const NSIG = 33; pub const SIG = struct { - pub const DFL = @intToPtr(?Sigaction.sigaction_fn, 0); - pub const IGN = @intToPtr(?Sigaction.sigaction_fn, 1); - pub const ERR = @intToPtr(?Sigaction.sigaction_fn, maxInt(usize)); - pub const CATCH = @intToPtr(?Sigaction.sigaction_fn, 2); - pub const HOLD = @intToPtr(?Sigaction.sigaction_fn, 3); + pub const DFL = @intToPtr(?Sigaction.handler_fn, 0); + pub const IGN = @intToPtr(?Sigaction.handler_fn, 1); + pub const ERR = @intToPtr(?Sigaction.handler_fn, maxInt(usize)); + pub const CATCH = @intToPtr(?Sigaction.handler_fn, 2); + pub const HOLD = @intToPtr(?Sigaction.handler_fn, 3); pub const HUP = 1; pub const INT = 2; diff --git a/lib/std/c/solaris.zig b/lib/std/c/solaris.zig index 61e52cfe301d24ed9064f808f05ca5eb78556310..1e726beb75c9f4a71333de44efa35b6e24883f29 100644 --- a/lib/std/c/solaris.zig +++ b/lib/std/c/solaris.zig @@ -879,10 +879,10 @@ pub const winsize = extern struct { const NSIG = 75; pub const SIG = struct { - pub const DFL = @intToPtr(?Sigaction.sigaction_fn, 0); - pub const ERR = @intToPtr(?Sigaction.sigaction_fn, maxInt(usize)); - pub const IGN = @intToPtr(?Sigaction.sigaction_fn, 1); - pub const HOLD = @intToPtr(?Sigaction.sigaction_fn, 2); + pub const DFL = @intToPtr(?Sigaction.handler_fn, 0); + pub const ERR = @intToPtr(?Sigaction.handler_fn, maxInt(usize)); + pub const IGN = @intToPtr(?Sigaction.handler_fn, 1); + pub const HOLD = @intToPtr(?Sigaction.handler_fn, 2); pub const WORDS = 4; pub const MAXSIG = 75; diff --git a/lib/std/debug.zig b/lib/std/debug.zig index 847298e54b5ab8459583586af79360d3df04db65..0738b5af0b63754bd20bc1efe653fa66cd49beb3 100644 --- a/lib/std/debug.zig +++ b/lib/std/debug.zig @@ -1787,7 +1787,7 @@ fn resetSegfaultHandler() void { return; } var act = os.Sigaction{ - .handler = .{ .sigaction = os.SIG.DFL }, + .handler = .{ .handler = os.SIG.DFL }, .mask = os.empty_sigset, .flags = 0, }; diff --git a/lib/std/os.zig b/lib/std/os.zig index b068a11229b54c93de90afed384f2321b0dbc041..46b1bed7bdc941cdb93f6a8f3582c9512236a9aa 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -475,7 +475,7 @@ pub fn abort() noreturn { // Install default handler so that the tkill below will terminate. const sigact = Sigaction{ - .handler = .{ .sigaction = SIG.DFL }, + .handler = .{ .handler = SIG.DFL }, .mask = empty_sigset, .flags = 0, }; diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index ae9b441b60ef78abb72aaae579291e9d1c2b98b4..539f3612f91878967433f182d2339d32978f9e23 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -1945,9 +1945,9 @@ pub const SIG = if (is_mips) struct { pub const SYS = 31; pub const UNUSED = SIG.SYS; - pub const ERR = @intToPtr(?Sigaction.sigaction_fn, maxInt(usize)); - pub const DFL = @intToPtr(?Sigaction.sigaction_fn, 0); - pub const IGN = @intToPtr(?Sigaction.sigaction_fn, 1); + pub const ERR = @intToPtr(?Sigaction.handler_fn, maxInt(usize)); + pub const DFL = @intToPtr(?Sigaction.handler_fn, 0); + pub const IGN = @intToPtr(?Sigaction.handler_fn, 1); } else if (is_sparc) struct { pub const BLOCK = 1; pub const UNBLOCK = 2; @@ -1989,9 +1989,9 @@ pub const SIG = if (is_mips) struct { pub const PWR = LOST; pub const IO = SIG.POLL; - pub const ERR = @intToPtr(?Sigaction.sigaction_fn, maxInt(usize)); - pub const DFL = @intToPtr(?Sigaction.sigaction_fn, 0); - pub const IGN = @intToPtr(?Sigaction.sigaction_fn, 1); + pub const ERR = @intToPtr(?Sigaction.handler_fn, maxInt(usize)); + pub const DFL = @intToPtr(?Sigaction.handler_fn, 0); + pub const IGN = @intToPtr(?Sigaction.handler_fn, 1); } else struct { pub const BLOCK = 0; pub const UNBLOCK = 1; @@ -2032,9 +2032,9 @@ pub const SIG = if (is_mips) struct { pub const SYS = 31; pub const UNUSED = SIG.SYS; - pub const ERR = @intToPtr(?Sigaction.sigaction_fn, maxInt(usize)); - pub const DFL = @intToPtr(?Sigaction.sigaction_fn, 0); - pub const IGN = @intToPtr(?Sigaction.sigaction_fn, 1); + pub const ERR = @intToPtr(?Sigaction.handler_fn, maxInt(usize)); + pub const DFL = @intToPtr(?Sigaction.handler_fn, 0); + pub const IGN = @intToPtr(?Sigaction.handler_fn, 1); }; pub const kernel_rwf = u32; diff --git a/lib/std/os/test.zig b/lib/std/os/test.zig index 44f8b16b9e0c97bcd8aa5f2e97bdb946dbf91ab0..a8497586f96999ec061dfbbf60cc88d394cb0fff 100644 --- a/lib/std/os/test.zig +++ b/lib/std/os/test.zig @@ -785,7 +785,7 @@ test "sigaction" { try testing.expect(signal_test_failed == false); // Check if the handler has been correctly reset to SIG_DFL try os.sigaction(os.SIG.USR1, null, &old_sa); - try testing.expectEqual(os.SIG.DFL, old_sa.handler.sigaction); + try testing.expectEqual(os.SIG.DFL, old_sa.handler.handler); } test "dup & dup2" { -- 2.54.0