@@ -126,26 +126,33 @@ mod imp {
126
126
static MAIN_ALTSTACK : AtomicPtr < libc:: c_void > = AtomicPtr :: new ( ptr:: null_mut ( ) ) ;
127
127
static NEED_ALTSTACK : AtomicBool = AtomicBool :: new ( false ) ;
128
128
129
+ /// # Safety
130
+ /// Must be called only once
131
+ #[ forbid( unsafe_op_in_unsafe_fn) ]
129
132
pub unsafe fn init ( ) {
130
133
PAGE_SIZE . store ( os:: page_size ( ) , Ordering :: Relaxed ) ;
131
134
132
135
// 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 ) } ;
134
137
GUARD . set ( ( guard. start , guard. end ) ) ;
135
138
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 ( ) } ;
137
141
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) } ;
139
144
// Configure our signal handler if one is not already set.
140
145
if action. sa_sigaction == SIG_DFL {
141
146
action. sa_flags = SA_SIGINFO | SA_ONSTACK ;
142
147
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 ( ) ) } ;
144
150
NEED_ALTSTACK . store ( true , Ordering :: Relaxed ) ;
145
151
}
146
152
}
147
153
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 ) } ;
149
156
MAIN_ALTSTACK . store ( handler. data , Ordering :: Relaxed ) ;
150
157
mem:: forget ( handler) ;
151
158
}
0 commit comments