summary refs log tree commit diff
path: root/library/std/src
diff options
context:
space:
mode:
authorJubilee Young <workingjubilee@gmail.com>2024-07-16 19:31:23 -0700
committerJubilee Young <workingjubilee@gmail.com>2024-07-18 15:12:18 -0700
commit9e354daf7b3ef64740fe2ecf3c5aae9563b68189 (patch)
tree0f90a104a657554faae1a640d823057684078610 /library/std/src
parent7d356ebde329df5f38186efe3e7c5e8dabac7cb7 (diff)
downloadrust-9e354daf7b3ef64740fe2ecf3c5aae9563b68189.tar.gz
rust-9e354daf7b3ef64740fe2ecf3c5aae9563b68189.zip
unix: Unsafe-wrap stack_overflow::signal_handler
sometimes a safety comment is a prayer.
avoid fuzzy provenance casts after deref.

Co-authored-by: Jonas Böttiger <jonasboettiger@icloud.com>
Diffstat (limited to 'library/std/src')
-rw-r--r--library/std/src/sys/pal/unix/stack_overflow.rs13
1 files changed, 10 insertions, 3 deletions
diff --git a/library/std/src/sys/pal/unix/stack_overflow.rs b/library/std/src/sys/pal/unix/stack_overflow.rs
index 2e5bd85327a..0886624160c 100644
--- a/library/std/src/sys/pal/unix/stack_overflow.rs
+++ b/library/std/src/sys/pal/unix/stack_overflow.rs
@@ -86,13 +86,18 @@ mod imp {
     // out many large systems and all implementations allow returning from a
     // signal handler to work. For a more detailed explanation see the
     // comments on #26458.
+    /// SIGSEGV/SIGBUS entry point
+    /// # Safety
+    /// Rust doesn't call this, it *gets called*.
+    #[forbid(unsafe_op_in_unsafe_fn)]
     unsafe extern "C" fn signal_handler(
         signum: libc::c_int,
         info: *mut libc::siginfo_t,
         _data: *mut libc::c_void,
     ) {
         let (start, end) = GUARD.get();
-        let addr = (*info).si_addr() as usize;
+        // SAFETY: this pointer is provided by the system and will always point to a valid `siginfo_t`.
+        let addr = unsafe { (*info).si_addr().addr() };
 
         // If the faulting address is within the guard page, then we print a
         // message saying so and abort.
@@ -104,9 +109,11 @@ mod imp {
             rtabort!("stack overflow");
         } else {
             // Unregister ourselves by reverting back to the default behavior.
-            let mut action: sigaction = mem::zeroed();
+            // SAFETY: assuming all platforms define struct sigaction as "zero-initializable"
+            let mut action: sigaction = unsafe { mem::zeroed() };
             action.sa_sigaction = SIG_DFL;
-            sigaction(signum, &action, ptr::null_mut());
+            // SAFETY: pray this is a well-behaved POSIX implementation of fn sigaction
+            unsafe { sigaction(signum, &action, ptr::null_mut()) };
 
             // See comment above for why this function returns.
         }