@@ -16,6 +16,7 @@ limitations under the License.
16
16
17
17
#[ cfg( target_os = "windows" ) ]
18
18
use core:: ffi:: c_void;
19
+ use std:: ops:: DerefMut ;
19
20
use std:: sync:: atomic:: { AtomicBool , Ordering } ;
20
21
use std:: sync:: { Arc , Mutex } ;
21
22
use std:: thread;
@@ -125,7 +126,7 @@ impl HvHandlerExecVars {
125
126
. thread_id
126
127
. try_lock ( )
127
128
. map_err ( |_| new_error ! ( "Failed to get_thread_id" ) ) ?)
128
- . ok_or_else ( || new_error ! ( "thread_id not set" ) )
129
+ . ok_or_else ( || new_error ! ( "thread_id not set" ) )
129
130
}
130
131
131
132
#[ cfg( target_os = "windows" ) ]
@@ -228,15 +229,11 @@ impl HypervisorHandler {
228
229
#[ instrument( err( Debug ) , skip_all, parent = Span :: current( ) , level = "Trace" ) ]
229
230
pub ( crate ) fn start_hypervisor_handler (
230
231
& mut self ,
231
- mut sandbox_memory_manager : SandboxMemoryManager < GuestSharedMemory > ,
232
+ sandbox_memory_manager : SandboxMemoryManager < GuestSharedMemory > ,
232
233
) -> Result < ( ) > {
233
234
let configuration = self . configuration . clone ( ) ;
234
- let mut hv = set_up_hypervisor_partition (
235
- & mut sandbox_memory_manager,
236
- configuration. outb_handler . clone ( ) ,
237
- ) ?;
238
235
#[ cfg( target_os = "windows" ) ]
239
- let in_process = sandbox_memory_manager. is_in_process ( ) ;
236
+ let in_process = sandbox_memory_manager. is_in_process ( ) ;
240
237
241
238
* self . execution_variables . shm . try_lock ( ) . unwrap ( ) = Some ( sandbox_memory_manager) ;
242
239
@@ -267,18 +264,8 @@ impl HypervisorHandler {
267
264
#[ cfg( target_os = "linux" ) ]
268
265
self . execution_variables . run_cancelled . store ( false ) ;
269
266
270
- #[ cfg( target_os = "windows" ) ]
271
- if !in_process {
272
- self . execution_variables
273
- . set_partition_handle ( hv. get_partition_handle ( ) ) ?;
274
- }
275
-
276
267
let to_handler_rx = self . communication_channels . to_handler_rx . clone ( ) ;
277
- #[ cfg( target_os = "windows" ) ]
278
- let execution_variables = self . execution_variables . clone ( ) ;
279
- #[ cfg( target_os = "linux" ) ]
280
268
let mut execution_variables = self . execution_variables . clone ( ) ;
281
- // ^^^ this needs to be mut on linux to set_thread_id
282
269
let from_handler_tx = self . communication_channels . from_handler_tx . clone ( ) ;
283
270
let hv_handler_clone = self . clone ( ) ;
284
271
@@ -295,9 +282,24 @@ impl HypervisorHandler {
295
282
thread:: Builder :: new ( )
296
283
. name ( "Hypervisor Handler" . to_string ( ) )
297
284
. spawn ( move || -> Result < ( ) > {
285
+ let mut hv: Option < Box < dyn Hypervisor > > = None ;
298
286
for action in to_handler_rx {
299
287
match action {
300
288
HypervisorHandlerAction :: Initialise => {
289
+ {
290
+ hv = Some ( set_up_hypervisor_partition (
291
+ execution_variables. shm . try_lock ( ) . unwrap ( ) . deref_mut ( ) . as_mut ( ) . unwrap ( ) ,
292
+ configuration. outb_handler . clone ( ) ,
293
+ ) ?) ;
294
+ }
295
+ let hv = hv. as_mut ( ) . unwrap ( ) ;
296
+
297
+ #[ cfg( target_os = "windows" ) ]
298
+ if !in_process {
299
+ execution_variables
300
+ . set_partition_handle ( hv. get_partition_handle ( ) ) ?;
301
+ }
302
+
301
303
#[ cfg( target_os = "linux" ) ]
302
304
{
303
305
// We cannot use the Killable trait, so we get the `pthread_t` via a libc
@@ -328,6 +330,7 @@ impl HypervisorHandler {
328
330
. shared_mem
329
331
. lock
330
332
. try_read ( ) ;
333
+
331
334
let res = hv. initialise (
332
335
configuration. peb_addr . clone ( ) ,
333
336
configuration. seed ,
@@ -362,6 +365,8 @@ impl HypervisorHandler {
362
365
}
363
366
}
364
367
HypervisorHandlerAction :: DispatchCallFromHost ( function_name) => {
368
+ let hv = hv. as_mut ( ) . unwrap ( ) ;
369
+
365
370
// Lock to indicate an action is being performed in the hypervisor
366
371
execution_variables. running . store ( true , Ordering :: SeqCst ) ;
367
372
@@ -735,7 +740,7 @@ impl HypervisorHandler {
735
740
0 ,
736
741
0 ,
737
742
)
738
- . map_err ( |e| new_error ! ( "Failed to cancel guest execution {:?}" , e) ) ?;
743
+ . map_err ( |e| new_error ! ( "Failed to cancel guest execution {:?}" , e) ) ?;
739
744
}
740
745
}
741
746
// if running in-process on windows, we currently have no way of cancelling the execution
@@ -911,7 +916,7 @@ mod tests {
911
916
None ,
912
917
None ,
913
918
)
914
- . unwrap ( ) ;
919
+ . unwrap ( ) ;
915
920
916
921
usbox. evolve ( Noop :: default ( ) ) . unwrap ( )
917
922
}
@@ -944,6 +949,13 @@ mod tests {
944
949
}
945
950
}
946
951
952
+ #[ test]
953
+ fn create_10_sandboxes ( ) {
954
+ for _ in 0 ..10 {
955
+ create_multi_use_sandbox ( ) ;
956
+ }
957
+ }
958
+
947
959
#[ test]
948
960
fn hello_world ( ) -> Result < ( ) > {
949
961
let mut sandbox = create_multi_use_sandbox ( ) ;
0 commit comments