From af5ebe9a87bb3c1b1b5739eab953c6d37bc7450d Mon Sep 17 00:00:00 2001 From: R4 Cheng Date: Sun, 8 Mar 2026 21:13:42 -0700 Subject: [PATCH] apple/bsd: replace `sighandler_t` with `sig_t` Replace all uses of `sighandler_t` with `sig_t` in Apple and BSD targets. These platforms don't use the GNU-specific extension that `sighandler_t` is. See the following list for details on each system. Per Rust Opsem, using `Option` is sound even when holding non-dereferenceable values (quote @tgross35) - Apple: - FreeBSD: - DragonFlyBSD: - NetBSD: - OpenBSD: Note this patch doesn't try to fix the `sigaction` situation where most systems use a specific field of the upstream `union` in its first field. That is work for another patchset (and further discussion.) ref: https://rust-lang.zulipchat.com/#narrow/channel/219381-t-libs/topic/type.20representing.20raw.20function.20pointers/with/606804857 and rust-lang/libc#4998 Co-authored-by: Adam Martinez <149513579+dybucc@users.noreply.github.com> --- libc-test/semver/apple.txt | 1 + libc-test/semver/dragonfly.txt | 1 + libc-test/semver/freebsd.txt | 1 + libc-test/semver/netbsd.txt | 1 + libc-test/semver/openbsd.txt | 1 + src/unix/bsd/apple/mod.rs | 4 +++- src/unix/bsd/freebsdlike/mod.rs | 4 +++- src/unix/bsd/mod.rs | 1 + src/unix/bsd/netbsdlike/mod.rs | 4 +++- src/unix/mod.rs | 16 ++++++++++++++++ 10 files changed, 31 insertions(+), 3 deletions(-) diff --git a/libc-test/semver/apple.txt b/libc-test/semver/apple.txt index 90de2724456a8..bd18afc5822fa 100644 --- a/libc-test/semver/apple.txt +++ b/libc-test/semver/apple.txt @@ -2155,6 +2155,7 @@ shmctl shmdt shmget shmid_ds +sig_t sigevent siginfo_t sigsuspend diff --git a/libc-test/semver/dragonfly.txt b/libc-test/semver/dragonfly.txt index 45e67dc8541e1..6a14a615b8bb5 100644 --- a/libc-test/semver/dragonfly.txt +++ b/libc-test/semver/dragonfly.txt @@ -1667,6 +1667,7 @@ shmctl shmdt shmget shmid_ds +sig_t sigaltstack sigevent siginfo_t diff --git a/libc-test/semver/freebsd.txt b/libc-test/semver/freebsd.txt index 7edf6575d00b9..252f7fff46866 100644 --- a/libc-test/semver/freebsd.txt +++ b/libc-test/semver/freebsd.txt @@ -2430,6 +2430,7 @@ shmctl shmdt shmget shmid_ds +sig_t sigaltstack sigevent siginfo_t diff --git a/libc-test/semver/netbsd.txt b/libc-test/semver/netbsd.txt index c70af78952e06..1169ec12b0128 100644 --- a/libc-test/semver/netbsd.txt +++ b/libc-test/semver/netbsd.txt @@ -1598,6 +1598,7 @@ shmctl shmdt shmget shmid_ds +sig_t sigaltstack sigevent siginfo_t diff --git a/libc-test/semver/openbsd.txt b/libc-test/semver/openbsd.txt index bdbf6fb07d463..f7933a856d640 100644 --- a/libc-test/semver/openbsd.txt +++ b/libc-test/semver/openbsd.txt @@ -1385,6 +1385,7 @@ shmctl shmdt shmget shmid_ds +sig_t sigaltstack siginfo_t sigsuspend diff --git a/src/unix/bsd/apple/mod.rs b/src/unix/bsd/apple/mod.rs index 8aa5085ad311a..376319109ef84 100644 --- a/src/unix/bsd/apple/mod.rs +++ b/src/unix/bsd/apple/mod.rs @@ -326,9 +326,11 @@ s! { _pad: Padding<[usize; 9]>, } + // FIXME(1.0): This should not implement `PartialEq` + #[allow(unpredictable_function_pointer_comparisons)] pub struct sigaction { // FIXME(union): this field is actually a union - pub sa_sigaction: crate::sighandler_t, + pub sa_sigaction: crate::sig_t, pub sa_mask: sigset_t, pub sa_flags: c_int, } diff --git a/src/unix/bsd/freebsdlike/mod.rs b/src/unix/bsd/freebsdlike/mod.rs index 7d963eabf071f..aa680abd097ba 100644 --- a/src/unix/bsd/freebsdlike/mod.rs +++ b/src/unix/bsd/freebsdlike/mod.rs @@ -156,8 +156,10 @@ s! { _pad2: Padding<[c_int; 7]>, } + // FIXME(1.0): This should not implement `PartialEq` + #[allow(unpredictable_function_pointer_comparisons)] pub struct sigaction { - pub sa_sigaction: crate::sighandler_t, + pub sa_sigaction: crate::sig_t, pub sa_flags: c_int, pub sa_mask: sigset_t, } diff --git a/src/unix/bsd/mod.rs b/src/unix/bsd/mod.rs index 09b6484d36225..e925289796e55 100644 --- a/src/unix/bsd/mod.rs +++ b/src/unix/bsd/mod.rs @@ -11,6 +11,7 @@ pub type nfds_t = c_uint; pub type regoff_t = c_int; #[cfg(not(target_os = "dragonfly"))] pub type regoff_t = off_t; +pub type sig_t = Option; s! { pub struct sockaddr { diff --git a/src/unix/bsd/netbsdlike/mod.rs b/src/unix/bsd/netbsdlike/mod.rs index 522039f63b02f..1d0db57950528 100644 --- a/src/unix/bsd/netbsdlike/mod.rs +++ b/src/unix/bsd/netbsdlike/mod.rs @@ -26,8 +26,10 @@ s! { pub sched_priority: c_int, } + // FIXME(1.0): This should not implement `PartialEq` + #[allow(unpredictable_function_pointer_comparisons)] pub struct sigaction { - pub sa_sigaction: crate::sighandler_t, + pub sa_sigaction: crate::sig_t, pub sa_mask: crate::sigset_t, pub sa_flags: c_int, } diff --git a/src/unix/mod.rs b/src/unix/mod.rs index 75f7a965ab3a6..19ac0bf33bb1f 100644 --- a/src/unix/mod.rs +++ b/src/unix/mod.rs @@ -1336,8 +1336,24 @@ extern "C" { #[cfg_attr(gnu_file_offset_bits64, link_name = "ftruncate64")] pub fn ftruncate(fd: c_int, length: off_t) -> c_int; + #[cfg(not(any( + target_vendor = "apple", + target_os = "freebsd", + target_os = "dragonfly", + target_os = "netbsd", + target_os = "openbsd" + )))] pub fn signal(signum: c_int, handler: sighandler_t) -> sighandler_t; + #[cfg(any( + target_vendor = "apple", + target_os = "freebsd", + target_os = "dragonfly", + target_os = "netbsd", + target_os = "openbsd" + ))] + pub fn signal(signum: c_int, handler: sig_t) -> sig_t; + #[cfg_attr(target_os = "netbsd", link_name = "__getrusage50")] #[cfg_attr(gnu_time_bits64, link_name = "__getrusage64")] #[cfg_attr(musl_redir_time64, link_name = "__getrusage_time64")]