@@ -44,33 +44,44 @@ impl From<SupervisorFuncIndex> for usize {
44
44
45
45
/// Index of a function within guest index space.
46
46
///
47
- /// This index is supposed to be used with as index for `Externals`.
47
+ /// This index is supposed to be used as index for `Externals`.
48
48
#[ derive( Copy , Clone , Debug , PartialEq ) ]
49
49
struct GuestFuncIndex ( usize ) ;
50
50
51
51
/// This struct holds a mapping from guest index space to supervisor.
52
52
struct GuestToSupervisorFunctionMapping {
53
+ /// Position of elements in this vector are interpreted
54
+ /// as indices of guest functions and are mappeed to
55
+ /// corresponding supervisor function indices.
53
56
funcs : Vec < SupervisorFuncIndex > ,
54
57
}
55
58
56
59
impl GuestToSupervisorFunctionMapping {
60
+ /// Create an empty function mapping
57
61
fn new ( ) -> GuestToSupervisorFunctionMapping {
58
62
GuestToSupervisorFunctionMapping { funcs : Vec :: new ( ) }
59
63
}
60
64
65
+ /// Add a new supervisor function to the mapping.
66
+ /// Returns a newly assigned guest function index.
61
67
fn define ( & mut self , supervisor_func : SupervisorFuncIndex ) -> GuestFuncIndex {
62
68
let idx = self . funcs . len ( ) ;
63
69
self . funcs . push ( supervisor_func) ;
64
70
GuestFuncIndex ( idx)
65
71
}
66
72
73
+ /// Find supervisor function index by its corresponding guest function index
67
74
fn func_by_guest_index ( & self , guest_func_idx : GuestFuncIndex ) -> Option < SupervisorFuncIndex > {
68
75
self . funcs . get ( guest_func_idx. 0 ) . cloned ( )
69
76
}
70
77
}
71
78
79
+ /// Holds sandbox function and memory imports and performs name resolution
72
80
struct Imports {
81
+ /// Maps qualified function name to its guest function index
73
82
func_map : HashMap < ( Vec < u8 > , Vec < u8 > ) , GuestFuncIndex > ,
83
+
84
+ /// Maps qualified field name to its memory reference
74
85
memories_map : HashMap < ( Vec < u8 > , Vec < u8 > ) , MemoryRef > ,
75
86
}
76
87
@@ -146,14 +157,15 @@ impl ImportResolver for Imports {
146
157
/// Note that this functions are only called in the `supervisor` context.
147
158
pub trait SandboxCapabilities : FunctionContext {
148
159
/// Represents a function reference into the supervisor environment.
160
+ /// Provides an abstraction over execution environment.
149
161
type SupervisorFuncRef ;
150
162
151
163
/// Invoke a function in the supervisor environment.
152
164
///
153
165
/// This first invokes the dispatch_thunk function, passing in the function index of the
154
166
/// desired function to call and serialized arguments. The thunk calls the desired function
155
167
/// with the deserialized arguments, then serializes the result into memory and returns
156
- /// reference. The pointer to and length of the result in linear memory is encoded into an i64,
168
+ /// reference. The pointer to and length of the result in linear memory is encoded into an ` i64` ,
157
169
/// with the upper 32 bits representing the pointer and the lower 32 bits representing the
158
170
/// length.
159
171
///
@@ -176,15 +188,22 @@ pub trait SandboxCapabilities: FunctionContext {
176
188
///
177
189
/// [`Externals`]: ../wasmi/trait.Externals.html
178
190
pub struct GuestExternals < ' a , FE : SandboxCapabilities + ' a > {
191
+ /// Supervisor function environment
179
192
supervisor_externals : & ' a mut FE ,
193
+
194
+ /// Instance of sandboxed module to be dispatched
180
195
sandbox_instance : & ' a SandboxInstance < FE :: SupervisorFuncRef > ,
196
+
197
+ /// Opaque pointer to outer context, see the `instantiate` function
181
198
state : u32 ,
182
199
}
183
200
201
+ /// Construct trap error from specified message
184
202
fn trap ( msg : & ' static str ) -> Trap {
185
203
TrapKind :: Host ( Box :: new ( Error :: Other ( msg. into ( ) ) ) ) . into ( )
186
204
}
187
205
206
+ /// Deserialize bytes into `Result`
188
207
fn deserialize_result ( serialized_result : & [ u8 ] ) -> std:: result:: Result < Option < RuntimeValue > , Trap > {
189
208
use self :: sandbox_primitives:: HostError ;
190
209
use sp_wasm_interface:: ReturnValue ;
@@ -209,6 +228,7 @@ impl<'a, FE: SandboxCapabilities + 'a> Externals for GuestExternals<'a, FE> {
209
228
// Make `index` typesafe again.
210
229
let index = GuestFuncIndex ( index) ;
211
230
231
+ // Convert function index from guest to supervisor space
212
232
let func_idx = self . sandbox_instance
213
233
. guest_to_supervisor_mapping
214
234
. func_by_guest_index ( index)
@@ -229,7 +249,7 @@ impl<'a, FE: SandboxCapabilities + 'a> Externals for GuestExternals<'a, FE> {
229
249
230
250
let state = self . state ;
231
251
232
- // Move serialized arguments inside the memory and invoke dispatch thunk and
252
+ // Move serialized arguments inside the memory, invoke dispatch thunk and
233
253
// then free allocated memory.
234
254
let invoke_args_len = invoke_args_data. len ( ) as WordSize ;
235
255
let invoke_args_ptr = self
@@ -408,7 +428,10 @@ fn decode_environment_definition(
408
428
409
429
/// An environment in which the guest module is instantiated.
410
430
pub struct GuestEnvironment {
431
+ /// Function and memory imports of the guest module
411
432
imports : Imports ,
433
+
434
+ /// Supervisor functinons mapped to guest index space
412
435
guest_to_supervisor_mapping : GuestToSupervisorFunctionMapping ,
413
436
}
414
437
@@ -449,26 +472,20 @@ impl<FR> UnregisteredInstance<FR> {
449
472
/// Instantiate a guest module and return it's index in the store.
450
473
///
451
474
/// The guest module's code is specified in `wasm`. Environment that will be available to
452
- /// guest module is specified in `raw_env_def` (serialized version of [`EnvironmentDefinition`]).
453
- /// `dispatch_thunk` is used as function that handle calls from guests.
454
- ///
455
- /// # Errors
456
- ///
457
- /// Returns `Err` if any of the following conditions happens:
458
- ///
459
- /// - `raw_env_def` can't be deserialized as a [`EnvironmentDefinition`].
460
- /// - Module in `wasm` is invalid or couldn't be instantiated.
475
+ /// guest module is specified in `guest_env`, `dispatch_thunk` is used as function that
476
+ /// handle calls from guests. `state` is an opaque pointer to caller's arbitrary context
477
+ /// normally created by `sp_sandbox::Instance` primitive.
461
478
///
462
- /// [`EnvironmentDefinition`]: ../sandbox/struct.EnvironmentDefinition.html
479
+ /// Returns uninitialized sandboxed module instance or an instantiation error.
463
480
pub fn instantiate < ' a , FE : SandboxCapabilities > (
464
481
supervisor_externals : & mut FE ,
465
482
dispatch_thunk : FE :: SupervisorFuncRef ,
466
483
wasm : & [ u8 ] ,
467
- host_env : GuestEnvironment ,
484
+ guest_env : GuestEnvironment ,
468
485
state : u32 ,
469
486
) -> std:: result:: Result < UnregisteredInstance < FE :: SupervisorFuncRef > , InstantiationError > {
470
487
let module = Module :: from_buffer ( wasm) . map_err ( |_| InstantiationError :: ModuleDecoding ) ?;
471
- let instance = ModuleInstance :: new ( & module, & host_env . imports )
488
+ let instance = ModuleInstance :: new ( & module, & guest_env . imports )
472
489
. map_err ( |_| InstantiationError :: Instantiation ) ?;
473
490
474
491
let sandbox_instance = Rc :: new ( SandboxInstance {
@@ -477,7 +494,7 @@ pub fn instantiate<'a, FE: SandboxCapabilities>(
477
494
// for the purpose of running `start` function which should be ok.
478
495
instance : instance. not_started_instance ( ) . clone ( ) ,
479
496
dispatch_thunk,
480
- guest_to_supervisor_mapping : host_env . guest_to_supervisor_mapping ,
497
+ guest_to_supervisor_mapping : guest_env . guest_to_supervisor_mapping ,
481
498
} ) ;
482
499
483
500
with_guest_externals (
0 commit comments