@@ -52,21 +52,21 @@ impl Supervisor {
52
52
// If the supervisor is not initialised for whatever reason, fast-fail.
53
53
// This might be desired behaviour, as even on platforms where ptracing
54
54
// is not implemented it enables us to enforce that only one FFI call
55
- // happens at a time
55
+ // happens at a time.
56
56
let Some ( sv) = sv_guard. take ( ) else {
57
57
return ( sv_guard, None ) ;
58
58
} ;
59
59
60
60
// Get pointers to all the pages the supervisor must allow accesses in
61
- // and prepare the fake stack
61
+ // and prepare the fake stack.
62
62
let page_ptrs = alloc. borrow ( ) . pages ( ) ;
63
63
let raw_stack_ptr: * mut [ u8 ; FAKE_STACK_SIZE ] =
64
64
Box :: leak ( Box :: new ( [ 0u8 ; FAKE_STACK_SIZE ] ) ) . as_mut_ptr ( ) . cast ( ) ;
65
65
let stack_ptr = raw_stack_ptr. expose_provenance ( ) ;
66
66
let start_info = StartFfiInfo { page_ptrs, stack_ptr } ;
67
67
68
68
// SAFETY: We do not access machine memory past this point until the
69
- // supervisor is ready to allow it
69
+ // supervisor is ready to allow it.
70
70
unsafe {
71
71
if alloc. borrow_mut ( ) . prepare_ffi ( ) . is_err ( ) {
72
72
// Don't mess up unwinding by maybe leaving the memory partly protected
@@ -79,13 +79,13 @@ impl Supervisor {
79
79
// NB: if we do not wait to receive a blank confirmation response, it is
80
80
// possible that the supervisor is alerted of the SIGSTOP *before* it has
81
81
// actually received the start_info, thus deadlocking! This way, we can
82
- // enforce an ordering for these events
82
+ // enforce an ordering for these events.
83
83
sv. message_tx . send ( TraceRequest :: StartFfi ( start_info) ) . unwrap ( ) ;
84
84
sv. confirm_rx . recv ( ) . unwrap ( ) ;
85
85
* sv_guard = Some ( sv) ;
86
86
// We need to be stopped for the supervisor to be able to make certain
87
87
// modifications to our memory - simply waiting on the recv() doesn't
88
- // count
88
+ // count.
89
89
signal:: raise ( signal:: SIGSTOP ) . unwrap ( ) ;
90
90
( sv_guard, Some ( raw_stack_ptr) )
91
91
}
@@ -109,22 +109,22 @@ impl Supervisor {
109
109
// simpler and more robust to simply use the signals which are left for
110
110
// arbitrary usage. Since this will block until we are continued by the
111
111
// supervisor, we can assume past this point that everything is back to
112
- // normal
112
+ // normal.
113
113
signal:: raise ( signal:: SIGUSR1 ) . unwrap ( ) ;
114
114
115
- // This is safe! It just sets memory to normal expected permissions
115
+ // This is safe! It just sets memory to normal expected permissions.
116
116
alloc. borrow_mut ( ) . unprep_ffi ( ) ;
117
117
118
118
// If this is `None`, then `raw_stack_ptr` is None and does not need to
119
119
// be deallocated (and there's no need to worry about the guard, since
120
- // it contains nothing)
120
+ // it contains nothing).
121
121
let sv = sv_guard. take ( ) ?;
122
122
// SAFETY: Caller upholds that this pointer was allocated as a box with
123
- // this type
123
+ // this type.
124
124
unsafe {
125
125
drop ( Box :: from_raw ( raw_stack_ptr. unwrap ( ) ) ) ;
126
126
}
127
- // On the off-chance something really weird happens, don't block forever
127
+ // On the off-chance something really weird happens, don't block forever.
128
128
let ret = sv
129
129
. event_rx
130
130
. try_recv_timeout ( std:: time:: Duration :: from_secs ( 5 ) )
@@ -151,32 +151,32 @@ impl Supervisor {
151
151
/// The invariants for `fork()` must be upheld by the caller.
152
152
pub unsafe fn init_sv ( ) -> Result < ( ) , SvInitError > {
153
153
// FIXME: Much of this could be reimplemented via the mitosis crate if we upstream the
154
- // relevant missing bits
154
+ // relevant missing bits.
155
155
156
156
// On Linux, this will check whether ptrace is fully disabled by the Yama module.
157
157
// If Yama isn't running or we're not on Linux, we'll still error later, but
158
- // this saves a very expensive fork call
158
+ // this saves a very expensive fork call.
159
159
let ptrace_status = std:: fs:: read_to_string ( "/proc/sys/kernel/yama/ptrace_scope" ) ;
160
160
if let Ok ( stat) = ptrace_status {
161
161
if let Some ( stat) = stat. chars ( ) . next ( ) {
162
- // Fast-error if ptrace is fully disabled on the system
162
+ // Fast-error if ptrace is fully disabled on the system.
163
163
if stat == '3' {
164
164
return Err ( SvInitError ) ;
165
165
}
166
166
}
167
167
}
168
168
169
- // Initialise the supervisor if it isn't already, placing it into SUPERVISOR
169
+ // Initialise the supervisor if it isn't already, placing it into SUPERVISOR.
170
170
let mut lock = SUPERVISOR . lock ( ) . unwrap ( ) ;
171
171
if lock. is_some ( ) {
172
172
return Ok ( ( ) ) ;
173
173
}
174
174
175
- // Prepare the IPC channels we need
175
+ // Prepare the IPC channels we need.
176
176
let ( message_tx, message_rx) = ipc:: channel ( ) . unwrap ( ) ;
177
177
let ( confirm_tx, confirm_rx) = ipc:: channel ( ) . unwrap ( ) ;
178
178
let ( event_tx, event_rx) = ipc:: channel ( ) . unwrap ( ) ;
179
- // SAFETY: Calling sysconf(_SC_PAGESIZE) is always safe and cannot error
179
+ // SAFETY: Calling sysconf(_SC_PAGESIZE) is always safe and cannot error.
180
180
let page_size = unsafe { libc:: sysconf ( libc:: _SC_PAGESIZE) } . try_into ( ) . unwrap ( ) ;
181
181
182
182
unsafe {
@@ -185,37 +185,37 @@ pub unsafe fn init_sv() -> Result<(), SvInitError> {
185
185
match unistd:: fork ( ) . unwrap ( ) {
186
186
unistd:: ForkResult :: Parent { child } => {
187
187
// If somehow another thread does exist, prevent it from accessing the lock
188
- // and thus breaking our safety invariants
188
+ // and thus breaking our safety invariants.
189
189
std:: mem:: forget ( lock) ;
190
190
// The child process is free to unwind, so we won't to avoid doubly freeing
191
- // system resources
191
+ // system resources.
192
192
let init = std:: panic:: catch_unwind ( || {
193
193
let listener =
194
194
ChildListener { message_rx, attached : false , override_retcode : None } ;
195
- // Trace as many things as possible, to be able to handle them as needed
195
+ // Trace as many things as possible, to be able to handle them as needed.
196
196
let options = ptrace:: Options :: PTRACE_O_TRACESYSGOOD
197
197
| ptrace:: Options :: PTRACE_O_TRACECLONE
198
198
| ptrace:: Options :: PTRACE_O_TRACEFORK ;
199
- // Attach to the child process without stopping it
199
+ // Attach to the child process without stopping it.
200
200
match ptrace:: seize ( child, options) {
201
201
// Ptrace works :D
202
202
Ok ( _) => {
203
203
let code = sv_loop ( listener, child, event_tx, confirm_tx, page_size)
204
204
. unwrap_err ( ) ;
205
205
// If a return code of 0 is not explicitly given, assume something went
206
- // wrong and return 1
206
+ // wrong and return 1.
207
207
std:: process:: exit ( code. unwrap_or ( 1 ) )
208
208
}
209
- // Ptrace does not work and we failed to catch that
209
+ // Ptrace does not work and we failed to catch that.
210
210
Err ( _) => {
211
- // If we can't ptrace, Miri continues being the parent
211
+ // If we can't ptrace, Miri continues being the parent.
212
212
signal:: kill ( child, signal:: SIGKILL ) . unwrap ( ) ;
213
213
SvInitError
214
214
}
215
215
}
216
216
} ) ;
217
217
match init {
218
- // The "Ok" case means that we couldn't ptrace
218
+ // The "Ok" case means that we couldn't ptrace.
219
219
Ok ( e) => return Err ( e) ,
220
220
Err ( p) => {
221
221
eprintln ! ( "Supervisor process panicked!\n {p:?}" ) ;
@@ -225,12 +225,12 @@ pub unsafe fn init_sv() -> Result<(), SvInitError> {
225
225
}
226
226
unistd:: ForkResult :: Child => {
227
227
// Make sure we never get orphaned and stuck in SIGSTOP or similar
228
- // SAFETY: prctl PR_SET_PDEATHSIG is always safe to call
228
+ // SAFETY: prctl PR_SET_PDEATHSIG is always safe to call.
229
229
let ret = libc:: prctl ( libc:: PR_SET_PDEATHSIG , libc:: SIGTERM ) ;
230
230
assert_eq ! ( ret, 0 ) ;
231
231
// First make sure the parent succeeded with ptracing us!
232
232
signal:: raise ( signal:: SIGSTOP ) . unwrap ( ) ;
233
- // If we're the child process, save the supervisor info
233
+ // If we're the child process, save the supervisor info.
234
234
* lock = Some ( Supervisor { message_tx, confirm_rx, event_rx } ) ;
235
235
}
236
236
}
0 commit comments