@@ -40,9 +40,11 @@ pub enum WaitOutcome {
40
40
Timeout ,
41
41
}
42
42
43
+ /// # Safety
44
+ /// `buf` must be valid, readable pointer
43
45
unsafe fn get_string ( buf : * const c_char ) -> OsString {
44
46
use std:: os:: unix:: ffi:: OsStrExt ;
45
- let buf = CStr :: from_ptr ( buf) ;
47
+ let buf = unsafe { CStr :: from_ptr ( buf) } ;
46
48
let buf = buf. to_bytes ( ) ;
47
49
let s = OsStr :: from_bytes ( buf) ;
48
50
s. to_os_string ( )
@@ -79,7 +81,7 @@ pub extern "C" fn minion_backend_create(out: &mut *mut Backend) -> ErrorCode {
79
81
#[ no_mangle]
80
82
#[ must_use]
81
83
pub unsafe extern "C" fn minion_backend_free ( b : * mut Backend ) -> ErrorCode {
82
- let b = Box :: from_raw ( b) ;
84
+ let b = unsafe { Box :: from_raw ( b) } ;
83
85
mem:: drop ( b) ;
84
86
ErrorCode :: Ok
85
87
}
@@ -112,7 +114,9 @@ pub unsafe extern "C" fn minion_dominion_check_cpu_tle(
112
114
) -> ErrorCode {
113
115
match dominion. 0 . check_cpu_tle ( ) {
114
116
Ok ( st) => {
115
- out. write ( st) ;
117
+ unsafe {
118
+ out. write ( st) ;
119
+ }
116
120
ErrorCode :: Ok
117
121
}
118
122
Err ( _) => ErrorCode :: Unknown ,
@@ -128,7 +132,9 @@ pub unsafe extern "C" fn minion_dominion_check_real_tle(
128
132
) -> ErrorCode {
129
133
match dominion. 0 . check_real_tle ( ) {
130
134
Ok ( st) => {
131
- out. write ( st) ;
135
+ unsafe {
136
+ out. write ( st) ;
137
+ }
132
138
ErrorCode :: Ok
133
139
}
134
140
Err ( _) => ErrorCode :: Unknown ,
@@ -153,7 +159,7 @@ pub unsafe extern "C" fn minion_dominion_create(
153
159
out : & mut * mut Dominion ,
154
160
) -> ErrorCode {
155
161
let mut exposed_paths = Vec :: new ( ) ;
156
- {
162
+ unsafe {
157
163
let mut p = options. shared_directories ;
158
164
while !( * p) . host_path . is_null ( ) {
159
165
let opt = minion:: PathExpositionOptions {
@@ -168,6 +174,7 @@ pub unsafe extern "C" fn minion_dominion_create(
168
174
p = p. offset ( 1 ) ;
169
175
}
170
176
}
177
+ let isolation_root = unsafe { get_string ( options. isolation_root ) } . into ( ) ;
171
178
let opts = minion:: DominionOptions {
172
179
max_alive_process_count : options. process_limit as _ ,
173
180
memory_limit : u64:: from ( options. memory_limit ) ,
@@ -179,7 +186,7 @@ pub unsafe extern "C" fn minion_dominion_create(
179
186
options. real_time_limit . seconds . into ( ) ,
180
187
options. real_time_limit . nanoseconds ,
181
188
) ,
182
- isolation_root : get_string ( options . isolation_root ) . into ( ) ,
189
+ isolation_root,
183
190
exposed_paths,
184
191
} ;
185
192
let d = backend. 0 . new_dominion ( opts) ;
@@ -195,7 +202,7 @@ pub unsafe extern "C" fn minion_dominion_create(
195
202
#[ no_mangle]
196
203
#[ must_use]
197
204
pub unsafe extern "C" fn minion_dominion_free ( dominion : * mut Dominion ) -> ErrorCode {
198
- let b = Box :: from_raw ( dominion) ;
205
+ let b = unsafe { Box :: from_raw ( dominion) } ;
199
206
mem:: drop ( b) ;
200
207
ErrorCode :: Ok
201
208
}
@@ -274,15 +281,15 @@ pub unsafe extern "C" fn minion_cp_spawn(
274
281
out : & mut * mut ChildProcess ,
275
282
) -> ErrorCode {
276
283
let mut arguments = Vec :: new ( ) ;
277
- {
284
+ unsafe {
278
285
let mut p = options. argv ;
279
286
while !( * p) . is_null ( ) {
280
287
arguments. push ( get_string ( * p) ) ;
281
288
p = p. offset ( 1 ) ;
282
289
}
283
290
}
284
291
let mut environment = Vec :: new ( ) ;
285
- {
292
+ unsafe {
286
293
let mut p = options. envp ;
287
294
while !( * p) . name . is_null ( ) {
288
295
let name = get_string ( ( * p) . name ) ;
@@ -295,18 +302,22 @@ pub unsafe extern "C" fn minion_cp_spawn(
295
302
p = p. offset ( 1 ) ;
296
303
}
297
304
}
298
- let stdio = minion:: StdioSpecification {
299
- stdin : minion:: InputSpecification :: handle ( options. stdio . stdin ) ,
300
- stdout : minion:: OutputSpecification :: handle ( options. stdio . stdout ) ,
301
- stderr : minion:: OutputSpecification :: handle ( options. stdio . stderr ) ,
305
+ let stdio = unsafe {
306
+ minion:: StdioSpecification {
307
+ stdin : minion:: InputSpecification :: handle ( options. stdio . stdin ) ,
308
+ stdout : minion:: OutputSpecification :: handle ( options. stdio . stdout ) ,
309
+ stderr : minion:: OutputSpecification :: handle ( options. stdio . stderr ) ,
310
+ }
302
311
} ;
303
- let options = minion:: ChildProcessOptions {
304
- path : get_string ( options. image_path ) . into ( ) ,
305
- arguments,
306
- environment,
307
- dominion : ( * options. dominion ) . 0 . clone ( ) ,
308
- stdio,
309
- pwd : get_string ( options. workdir ) . into ( ) ,
312
+ let options = unsafe {
313
+ minion:: ChildProcessOptions {
314
+ path : get_string ( options. image_path ) . into ( ) ,
315
+ arguments,
316
+ environment,
317
+ dominion : ( * options. dominion ) . 0 . clone ( ) ,
318
+ stdio,
319
+ pwd : get_string ( options. workdir ) . into ( ) ,
320
+ }
310
321
} ;
311
322
let cp = backend. 0 . spawn ( options) . unwrap ( ) ;
312
323
let cp = ChildProcess ( cp) ;
@@ -336,7 +347,9 @@ pub unsafe extern "C" fn minion_cp_wait(
336
347
minion:: WaitOutcome :: AlreadyFinished => WaitOutcome :: AlreadyFinished ,
337
348
minion:: WaitOutcome :: Timeout => WaitOutcome :: Timeout ,
338
349
} ;
339
- out. write ( outcome) ;
350
+ unsafe {
351
+ out. write ( outcome) ;
352
+ }
340
353
ErrorCode :: Ok
341
354
}
342
355
Result :: Err ( _) => ErrorCode :: Unknown ,
@@ -346,24 +359,33 @@ pub unsafe extern "C" fn minion_cp_wait(
346
359
#[ no_mangle]
347
360
pub static EXIT_CODE_STILL_RUNNING : i64 = 1234_4321 ;
348
361
362
+ /// Returns child process (pointed by `cp`) exit code.
363
+ ///
364
+ /// `out` will contain exit code. If child is still running,
365
+ /// `out` will not be written to.
366
+ ///
367
+ /// if `finish_flag` is non-null it will be written 0/1 flag:
368
+ /// has child finished.
349
369
/// # Safety
350
370
/// Provided pointers must be valid
351
371
#[ no_mangle]
352
372
#[ must_use]
353
373
pub unsafe extern "C" fn minion_cp_exitcode (
354
- cp : & mut ChildProcess ,
374
+ cp : & ChildProcess ,
355
375
out : * mut i64 ,
356
- finish_flag : * mut bool ,
376
+ finish_flag : * mut u8 ,
357
377
) -> ErrorCode {
358
378
match cp. 0 . get_exit_code ( ) {
359
379
Result :: Ok ( exit_code) => {
360
380
if let Some ( code) = exit_code {
361
- out. write ( code) ;
381
+ unsafe {
382
+ out. write ( code) ;
383
+ }
362
384
} else {
363
- out. write ( EXIT_CODE_STILL_RUNNING )
385
+ unsafe { out. write ( EXIT_CODE_STILL_RUNNING ) }
364
386
}
365
387
if !finish_flag. is_null ( ) {
366
- finish_flag. write ( exit_code. is_some ( ) ) ;
388
+ unsafe { finish_flag. write ( exit_code. is_some ( ) as u8 ) } ;
367
389
}
368
390
ErrorCode :: Ok
369
391
}
@@ -376,6 +398,6 @@ pub unsafe extern "C" fn minion_cp_exitcode(
376
398
#[ no_mangle]
377
399
#[ must_use]
378
400
pub unsafe extern "C" fn minion_cp_free ( cp : * mut ChildProcess ) -> ErrorCode {
379
- mem:: drop ( Box :: from_raw ( cp) ) ;
401
+ mem:: drop ( unsafe { Box :: from_raw ( cp) } ) ;
380
402
ErrorCode :: Ok
381
403
}
0 commit comments