@@ -362,7 +362,9 @@ impl<FR> SandboxInstance<FR> {
362
362
///
363
363
/// The `state` parameter can be used to provide custom data for
364
364
/// these syscall implementations.
365
- pub fn invoke < FE : SandboxCapabilities < SupervisorFuncRef =FR > > (
365
+ pub fn invoke
366
+ //<FE: SandboxCapabilities<SupervisorFuncRef=FR>>
367
+ (
366
368
& self ,
367
369
368
370
// function to call that is exported from the module
@@ -372,55 +374,58 @@ impl<FR> SandboxInstance<FR> {
372
374
args : & [ RuntimeValue ] ,
373
375
374
376
// supervisor environment provided to the module
375
- supervisor_externals : & mut FE ,
377
+ // supervisor_externals: &mut FE,
376
378
377
379
// arbitraty context data of the call
378
380
state : u32 ,
379
381
) -> std:: result:: Result < Option < wasmi:: RuntimeValue > , wasmi:: Error > {
380
- with_guest_externals (
381
- supervisor_externals,
382
- self ,
383
- state,
384
- |guest_externals| {
385
382
386
- let wasmi_result = self . wasmi_instance
387
- . invoke_export ( export_name, args, guest_externals) ?;
388
-
389
- let wasmtime_function = self
390
- . wasmtime_instance
391
- . get_func ( export_name)
392
- . ok_or ( wasmi:: Error :: Function ( "wasmtime function failed" . to_string ( ) ) ) ?;
393
-
394
- let args: Vec < Val > = args
395
- . iter ( )
396
- . map ( |v| match * v {
397
- RuntimeValue :: I32 ( val) => Val :: I32 ( val) ,
398
- RuntimeValue :: I64 ( val) => Val :: I64 ( val) ,
399
- RuntimeValue :: F32 ( val) => Val :: F32 ( val. into ( ) ) ,
400
- RuntimeValue :: F64 ( val) => Val :: F64 ( val. into ( ) ) ,
401
- } )
402
- . collect ( ) ;
403
-
404
- let wasmtime_result = wasmtime_function
405
- . call ( & args)
406
- . map_err ( |e| wasmi:: Error :: Function ( e. to_string ( ) ) ) ?;
407
-
408
- assert_eq ! ( wasmtime_result. len( ) , 1 , "multiple return types are not supported yet" ) ;
409
- if let Some ( wasmi_value) = wasmi_result {
410
- let wasmtime_value = match * wasmtime_result. first ( ) . unwrap ( ) {
411
- Val :: I32 ( val) => RuntimeValue :: I32 ( val) ,
412
- Val :: I64 ( val) => RuntimeValue :: I64 ( val) ,
413
- Val :: F32 ( val) => RuntimeValue :: F32 ( val. into ( ) ) ,
414
- Val :: F64 ( val) => RuntimeValue :: F64 ( val. into ( ) ) ,
415
- _ => unreachable ! ( ) ,
416
- } ;
417
-
418
- assert_eq ! ( wasmi_value, wasmtime_value, "return values do not match" ) ;
419
- }
420
-
421
- Ok ( wasmi_result)
422
- } ,
423
- )
383
+ SCH :: with_sandbox_capabilities ( |supervisor_externals| {
384
+ with_guest_externals (
385
+ supervisor_externals,
386
+ self ,
387
+ state,
388
+ |guest_externals| {
389
+
390
+ let wasmi_result = self . wasmi_instance
391
+ . invoke_export ( export_name, args, guest_externals) ?;
392
+
393
+ let wasmtime_function = self
394
+ . wasmtime_instance
395
+ . get_func ( export_name)
396
+ . ok_or ( wasmi:: Error :: Function ( "wasmtime function failed" . to_string ( ) ) ) ?;
397
+
398
+ let args: Vec < Val > = args
399
+ . iter ( )
400
+ . map ( |v| match * v {
401
+ RuntimeValue :: I32 ( val) => Val :: I32 ( val) ,
402
+ RuntimeValue :: I64 ( val) => Val :: I64 ( val) ,
403
+ RuntimeValue :: F32 ( val) => Val :: F32 ( val. into ( ) ) ,
404
+ RuntimeValue :: F64 ( val) => Val :: F64 ( val. into ( ) ) ,
405
+ } )
406
+ . collect ( ) ;
407
+
408
+ let wasmtime_result = wasmtime_function
409
+ . call ( & args)
410
+ . map_err ( |e| wasmi:: Error :: Function ( e. to_string ( ) ) ) ?;
411
+
412
+ assert_eq ! ( wasmtime_result. len( ) , 1 , "multiple return types are not supported yet" ) ;
413
+ if let Some ( wasmi_value) = wasmi_result {
414
+ let wasmtime_value = match * wasmtime_result. first ( ) . unwrap ( ) {
415
+ Val :: I32 ( val) => RuntimeValue :: I32 ( val) ,
416
+ Val :: I64 ( val) => RuntimeValue :: I64 ( val) ,
417
+ Val :: F32 ( val) => RuntimeValue :: F32 ( val. into ( ) ) ,
418
+ Val :: F64 ( val) => RuntimeValue :: F64 ( val. into ( ) ) ,
419
+ _ => unreachable ! ( ) ,
420
+ } ;
421
+
422
+ assert_eq ! ( wasmi_value, wasmtime_value, "return values do not match" ) ;
423
+ }
424
+
425
+ Ok ( wasmi_result)
426
+ } ,
427
+ )
428
+ } )
424
429
}
425
430
426
431
/// Get the value from a global with the given `name`.
@@ -547,6 +552,13 @@ impl<FR> UnregisteredInstance<FR> {
547
552
}
548
553
}
549
554
555
+ pub trait SandboxCapabiliesHolder < ' a > {
556
+ type SupervisorFuncRef ;
557
+ type SC : SandboxCapabilities < SupervisorFuncRef = Self :: SupervisorFuncRef > + ' a ;
558
+
559
+ fn with_sandbox_capabilities < R , F : FnOnce ( & mut Self :: SC ) -> R > ( f : F ) -> R ;
560
+ }
561
+
550
562
/// Instantiate a guest module and return it's index in the store.
551
563
///
552
564
/// The guest module's code is specified in `wasm`. Environment that will be available to
@@ -555,13 +567,17 @@ impl<FR> UnregisteredInstance<FR> {
555
567
/// normally created by `sp_sandbox::Instance` primitive.
556
568
///
557
569
/// Returns uninitialized sandboxed module instance or an instantiation error.
558
- pub fn instantiate < ' a , FE : SandboxCapabilities > (
559
- supervisor_externals : & mut FE ,
570
+ pub fn instantiate < ' a , FE , SCH > (
571
+ // supervisor_externals: &mut FE,
560
572
dispatch_thunk : FE :: SupervisorFuncRef ,
561
573
wasm : & [ u8 ] ,
562
574
guest_env : GuestEnvironment ,
563
575
state : u32 ,
564
- ) -> std:: result:: Result < UnregisteredInstance < FE :: SupervisorFuncRef > , InstantiationError > {
576
+ ) -> std:: result:: Result < UnregisteredInstance < FE :: SupervisorFuncRef > , InstantiationError >
577
+ where
578
+ FE : SandboxCapabilities + ' a ,
579
+ SCH : SandboxCapabiliesHolder < ' a , SupervisorFuncRef = FE :: SupervisorFuncRef , SC = FE > ,
580
+ {
565
581
let wasmi_module = Module :: from_buffer ( wasm) . map_err ( |_| InstantiationError :: ModuleDecoding ) ?;
566
582
let wasmi_instance = ModuleInstance :: new ( & wasmi_module, & guest_env. imports )
567
583
. map_err ( |_| InstantiationError :: Instantiation ) ?;
@@ -580,11 +596,19 @@ pub fn instantiate<'a, FE: SandboxCapabilities>(
580
596
. filter_map ( |import| {
581
597
if let wasmtime:: ExternType :: Func ( func_ty) = import. ty ( ) {
582
598
Some ( wasmtime:: Extern :: Func ( wasmtime:: Func :: new ( & wasmtime_store, func_ty,
583
- move |_, _, _| Err ( wasmtime:: Trap :: new ( format ! (
584
- "Sandbox function stub" ,
585
- // func_ty.to_string(),
586
- // func_ty.name().to_string()
587
- ) ) )
599
+ move |_, _, _| {
600
+ SCH :: with_sandbox_capabilities ( |sc| {
601
+ // sc.invoke();
602
+ } ) ;
603
+
604
+ Ok ( ( ) )
605
+ }
606
+
607
+ // Err(wasmtime::Trap::new(format!(
608
+ // "Sandbox function stub",
609
+ // // func_ty.to_string(),
610
+ // // func_ty.name().to_string()
611
+ // )))
588
612
) ) )
589
613
} else {
590
614
None
@@ -604,18 +628,20 @@ pub fn instantiate<'a, FE: SandboxCapabilities>(
604
628
guest_to_supervisor_mapping : guest_env. guest_to_supervisor_mapping ,
605
629
} ) ;
606
630
607
- with_guest_externals (
608
- supervisor_externals,
609
- & sandbox_instance,
610
- state,
611
- |guest_externals| {
612
- wasmi_instance
613
- . run_start ( guest_externals)
614
- . map_err ( |_| InstantiationError :: StartTrapped )
631
+ SCH :: with_sandbox_capabilities ( |supervisor_externals| {
632
+ with_guest_externals (
633
+ supervisor_externals,
634
+ & sandbox_instance,
635
+ state,
636
+ |guest_externals| {
637
+ wasmi_instance
638
+ . run_start ( guest_externals)
639
+ . map_err ( |_| InstantiationError :: StartTrapped )
615
640
616
- // Note: no need to run start on wasmtime instance, since it's done automatically
617
- } ,
618
- ) ?;
641
+ // Note: no need to run start on wasmtime instance, since it's done automatically
642
+ } ,
643
+ )
644
+ } ) ?;
619
645
620
646
Ok ( UnregisteredInstance { sandbox_instance } )
621
647
}
0 commit comments