Skip to content

Commit 9ad696a

Browse files
unix: Unsafe-wrap stack_overflow::init
1 parent d314219 commit 9ad696a

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

library/std/src/sys/pal/unix/stack_overflow.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -126,26 +126,33 @@ mod imp {
126126
static MAIN_ALTSTACK: AtomicPtr<libc::c_void> = AtomicPtr::new(ptr::null_mut());
127127
static NEED_ALTSTACK: AtomicBool = AtomicBool::new(false);
128128

129+
/// # Safety
130+
/// Must be called only once
131+
#[forbid(unsafe_op_in_unsafe_fn)]
129132
pub unsafe fn init() {
130133
PAGE_SIZE.store(os::page_size(), Ordering::Relaxed);
131134

132135
// Always write to GUARD to ensure the TLS variable is allocated.
133-
let guard = install_main_guard().unwrap_or(0..0);
136+
let guard = unsafe { install_main_guard().unwrap_or(0..0) };
134137
GUARD.set((guard.start, guard.end));
135138

136-
let mut action: sigaction = mem::zeroed();
139+
// SAFETY: assuming all platforms define struct sigaction as "zero-initializable"
140+
let mut action: sigaction = unsafe { mem::zeroed() };
137141
for &signal in &[SIGSEGV, SIGBUS] {
138-
sigaction(signal, ptr::null_mut(), &mut action);
142+
// SAFETY: just fetches the current signal handler into action
143+
unsafe { sigaction(signal, ptr::null_mut(), &mut action) };
139144
// Configure our signal handler if one is not already set.
140145
if action.sa_sigaction == SIG_DFL {
141146
action.sa_flags = SA_SIGINFO | SA_ONSTACK;
142147
action.sa_sigaction = signal_handler as sighandler_t;
143-
sigaction(signal, &action, ptr::null_mut());
148+
// SAFETY: only overriding signals if the default is set
149+
unsafe { sigaction(signal, &action, ptr::null_mut()) };
144150
NEED_ALTSTACK.store(true, Ordering::Relaxed);
145151
}
146152
}
147153

148-
let handler = make_handler(true);
154+
// SAFETY: mutates our signal stack. shouldn't we install this first?
155+
let handler = unsafe { make_handler(true) };
149156
MAIN_ALTSTACK.store(handler.data, Ordering::Relaxed);
150157
mem::forget(handler);
151158
}

0 commit comments

Comments
 (0)