@@ -49,11 +49,11 @@ impl Supervisor {
4949    )  -> ( std:: sync:: MutexGuard < ' static ,  Option < Supervisor > > ,  Option < * mut  [ u8 ;  CALLBACK_STACK_SIZE ] > ) 
5050    { 
5151        let  mut  sv_guard = SUPERVISOR . lock ( ) . unwrap ( ) ; 
52-         // If the supervisor is not initialised for whatever reason, fast-fail . 
52+         // If the supervisor is not initialised for whatever reason, fast-return . 
5353        // This might be desired behaviour, as even on platforms where ptracing 
5454        // is not implemented it enables us to enforce that only one FFI call 
5555        // happens at a time. 
56-         let  Some ( sv)  = sv_guard. take ( )  else  { 
56+         let  Some ( sv)  = sv_guard. as_mut ( )  else  { 
5757            return  ( sv_guard,  None ) ; 
5858        } ; 
5959
@@ -68,9 +68,9 @@ impl Supervisor {
6868        // SAFETY: We do not access machine memory past this point until the 
6969        // supervisor is ready to allow it. 
7070        unsafe  { 
71-             if  alloc. borrow_mut ( ) . prepare_ffi ( ) . is_err ( )  { 
71+             if  alloc. borrow_mut ( ) . start_ffi ( ) . is_err ( )  { 
7272                // Don't mess up unwinding by maybe leaving the memory partly protected 
73-                 alloc. borrow_mut ( ) . unprep_ffi ( ) ; 
73+                 alloc. borrow_mut ( ) . end_ffi ( ) ; 
7474                panic ! ( "Cannot protect memory for FFI call!" ) ; 
7575            } 
7676        } 
@@ -82,7 +82,6 @@ impl Supervisor {
8282        // enforce an ordering for these events. 
8383        sv. message_tx . send ( TraceRequest :: StartFfi ( start_info) ) . unwrap ( ) ; 
8484        sv. confirm_rx . recv ( ) . unwrap ( ) ; 
85-         * sv_guard = Some ( sv) ; 
8685        // We need to be stopped for the supervisor to be able to make certain 
8786        // modifications to our memory - simply waiting on the recv() doesn't 
8887        // count. 
@@ -113,20 +112,19 @@ impl Supervisor {
113112        signal:: raise ( signal:: SIGUSR1 ) . unwrap ( ) ; 
114113
115114        // This is safe! It just sets memory to normal expected permissions. 
116-         alloc. borrow_mut ( ) . unprep_ffi ( ) ; 
115+         alloc. borrow_mut ( ) . end_ffi ( ) ; 
117116
118117        // If this is `None`, then `raw_stack_ptr` is None and does not need to 
119118        // be deallocated (and there's no need to worry about the guard, since 
120119        // it contains nothing). 
121-         let  sv = sv_guard. take ( ) ?; 
120+         let  sv = sv_guard. as_mut ( ) ?; 
122121        // SAFETY: Caller upholds that this pointer was allocated as a box with 
123122        // this type. 
124123        unsafe  { 
125124            drop ( Box :: from_raw ( raw_stack_ptr. unwrap ( ) ) ) ; 
126125        } 
127126        // On the off-chance something really weird happens, don't block forever. 
128-         let  ret = sv
129-             . event_rx 
127+         sv. event_rx 
130128            . try_recv_timeout ( std:: time:: Duration :: from_secs ( 5 ) ) 
131129            . map_err ( |e| { 
132130                match  e { 
@@ -135,10 +133,7 @@ impl Supervisor {
135133                        eprintln ! ( "Waiting for accesses from supervisor timed out!" ) , 
136134                } 
137135            } ) 
138-             . ok ( ) ; 
139-         // Do *not* leave the supervisor empty, or else we might get another fork... 
140-         * sv_guard = Some ( sv) ; 
141-         ret
136+             . ok ( ) 
142137    } 
143138} 
144139
@@ -148,11 +143,19 @@ impl Supervisor {
148143/// receiving back events through `get_events`. 
149144/// 
150145/// # Safety 
151- /// The invariants for `fork()` must be upheld by the caller. 
146+ /// The invariants for `fork()` must be upheld by the caller, namely either: 
147+ /// - Other threads do not exist, or; 
148+ /// - If they do exist, either those threads or the resulting child process 
149+ ///   only ever act in [async-signal-safe](https://www.man7.org/linux/man-pages/man7/signal-safety.7.html) ways. 
152150pub  unsafe  fn  init_sv ( )  -> Result < ( ) ,  SvInitError >  { 
153151    // FIXME: Much of this could be reimplemented via the mitosis crate if we upstream the 
154152    // relevant missing bits. 
155153
154+     // Not on a properly supported architecture! 
155+     if  cfg ! ( not( any( target_arch = "x86" ,  target_arch = "x86_64" ,  target_arch = "aarch64" ) ) )  { 
156+         return  Err ( SvInitError ) ; 
157+     } 
158+ 
156159    // On Linux, this will check whether ptrace is fully disabled by the Yama module. 
157160    // If Yama isn't running or we're not on Linux, we'll still error later, but 
158161    // this saves a very expensive fork call. 
@@ -244,8 +247,7 @@ pub unsafe fn init_sv() -> Result<(), SvInitError> {
244247/// given as a 0 if this is not used. 
245248pub  fn  register_retcode_sv ( code :  i32 )  { 
246249    let  mut  sv_guard = SUPERVISOR . lock ( ) . unwrap ( ) ; 
247-     if  let  Some ( sv)  = sv_guard. take ( )  { 
250+     if  let  Some ( sv)  = sv_guard. as_mut ( )  { 
248251        sv. message_tx . send ( TraceRequest :: OverrideRetcode ( code) ) . unwrap ( ) ; 
249-         * sv_guard = Some ( sv) ; 
250252    } 
251253} 
0 commit comments