@@ -17,7 +17,7 @@ limitations under the License.
17
17
use alloc:: string:: ToString ;
18
18
use alloc:: vec:: Vec ;
19
19
use alloc:: { format, vec} ;
20
- use core:: ops:: Deref ;
20
+ use core:: ops:: { Deref , DerefMut } ;
21
21
22
22
use hyperlight_common:: flatbuffer_wrappers:: function_call:: FunctionCall ;
23
23
use hyperlight_common:: flatbuffer_wrappers:: function_types:: {
@@ -34,57 +34,71 @@ use wasmtime::{Config, Engine, Linker, Module, Store, Val};
34
34
35
35
use crate :: { hostfuncs, marshal, platform, wasip1} ;
36
36
37
+ // Set by transition to WasmSandbox (by init_wasm_runtime)
37
38
static CUR_ENGINE : Mutex < Option < Engine > > = Mutex :: new ( None ) ;
38
39
static CUR_LINKER : Mutex < Option < Linker < ( ) > > > = Mutex :: new ( None ) ;
40
+ // Set by transition to LoadedWasmSandbox (by load_wasm_module/load_wasm_module_phys)
39
41
static CUR_MODULE : Mutex < Option < Module > > = Mutex :: new ( None ) ;
42
+ static CUR_STORE : Mutex < Option < Store < ( ) > > > = Mutex :: new ( None ) ;
43
+ static CUR_INSTANCE : Mutex < Option < wasmtime:: Instance > > = Mutex :: new ( None ) ;
40
44
41
45
#[ no_mangle]
42
46
pub fn guest_dispatch_function ( function_call : & FunctionCall ) -> Result < Vec < u8 > > {
43
- let engine = CUR_ENGINE . lock ( ) ;
44
- let engine = engine . deref ( ) . as_ref ( ) . ok_or ( HyperlightGuestError :: new (
47
+ let mut store = CUR_STORE . lock ( ) ;
48
+ let store = store . deref_mut ( ) . as_mut ( ) . ok_or ( HyperlightGuestError :: new (
45
49
ErrorCode :: GuestError ,
46
- "Wasm runtime is not initialized " . to_string ( ) ,
50
+ "No wasm store available " . to_string ( ) ,
47
51
) ) ?;
48
- let linker = CUR_LINKER . lock ( ) ;
49
- let linker = linker . deref ( ) . as_ref ( ) . ok_or ( HyperlightGuestError :: new (
52
+ let instance = CUR_INSTANCE . lock ( ) ;
53
+ let instance = instance . deref ( ) . as_ref ( ) . ok_or ( HyperlightGuestError :: new (
50
54
ErrorCode :: GuestError ,
51
- "impossible: wasm runtime has no valid linker " . to_string ( ) ,
55
+ "No wasm instance available " . to_string ( ) ,
52
56
) ) ?;
53
- let module = CUR_MODULE . lock ( ) ;
54
- let module = module. deref ( ) . as_ref ( ) . ok_or ( HyperlightGuestError :: new (
55
- ErrorCode :: GuestError ,
56
- "No wasm module loaded" . to_string ( ) ,
57
- ) ) ?;
58
- let mut store = Store :: new ( engine, ( ) ) ;
59
- let instance = linker. instantiate ( & mut store, module) ?;
57
+
60
58
let func = instance
61
- . get_func ( & mut store, & function_call. function_name )
59
+ . get_func ( & mut * store, & function_call. function_name )
62
60
. ok_or ( HyperlightGuestError :: new (
63
61
ErrorCode :: GuestError ,
64
62
"Function not found" . to_string ( ) ,
65
63
) ) ?;
64
+
66
65
let mut w_params = vec ! [ ] ;
66
+ let mut allocated_addrs = vec ! [ ] ;
67
67
for f_param in ( function_call. parameters )
68
68
. as_ref ( )
69
69
. unwrap_or ( & vec ! [ ] )
70
70
. iter ( )
71
71
{
72
- w_params. push ( marshal:: hl_param_to_val (
73
- & mut store,
72
+ w_params. push ( marshal:: hl_param_to_val_with_tracking (
73
+ & mut * store,
74
74
|ctx, name| instance. get_export ( ctx, name) ,
75
75
f_param,
76
+ & mut allocated_addrs,
76
77
) ?) ;
77
78
}
78
79
let is_void = ReturnType :: Void == function_call. expected_return_type ;
79
80
let n_results = if is_void { 0 } else { 1 } ;
80
81
let mut results = vec ! [ Val :: I32 ( 0 ) ; n_results] ;
81
- func. call ( & mut store, & w_params, & mut results) ?;
82
- marshal:: val_to_hl_result (
83
- & mut store,
82
+ func. call ( & mut * store, & w_params, & mut results) ?;
83
+ let result = marshal:: val_to_hl_result (
84
+ & mut * store,
84
85
|ctx, name| instance. get_export ( ctx, name) ,
85
86
function_call. expected_return_type ,
86
87
& results,
88
+ ) ;
89
+
90
+ marshal:: free_allocated_addrs (
91
+ & mut * store,
92
+ |ctx, name| instance. get_export ( ctx, name) ,
93
+ & allocated_addrs,
87
94
)
95
+ . map_err ( |e| {
96
+ HyperlightGuestError :: new (
97
+ ErrorCode :: GuestError ,
98
+ format ! ( "Failed to free memory allocated for params: {:?}" , e) ,
99
+ )
100
+ } ) ?;
101
+ result
88
102
}
89
103
90
104
fn init_wasm_runtime ( ) -> Result < Vec < u8 > > {
@@ -124,8 +138,19 @@ fn load_wasm_module(function_call: &FunctionCall) -> Result<Vec<u8>> {
124
138
& function_call. parameters . as_ref ( ) . unwrap ( ) [ 1 ] ,
125
139
& * CUR_ENGINE . lock ( ) ,
126
140
) {
141
+ let linker = CUR_LINKER . lock ( ) ;
142
+ let linker = linker. deref ( ) . as_ref ( ) . ok_or ( HyperlightGuestError :: new (
143
+ ErrorCode :: GuestError ,
144
+ "impossible: wasm runtime has no valid linker" . to_string ( ) ,
145
+ ) ) ?;
146
+
127
147
let module = unsafe { Module :: deserialize ( engine, wasm_bytes) ? } ;
148
+ let mut store = Store :: new ( engine, ( ) ) ;
149
+ let instance = linker. instantiate ( & mut store, & module) ?;
150
+
128
151
* CUR_MODULE . lock ( ) = Some ( module) ;
152
+ * CUR_STORE . lock ( ) = Some ( store) ;
153
+ * CUR_INSTANCE . lock ( ) = Some ( instance) ;
129
154
Ok ( get_flatbuffer_result :: < i32 > ( 0 ) )
130
155
} else {
131
156
Err ( HyperlightGuestError :: new (
@@ -141,8 +166,19 @@ fn load_wasm_module_phys(function_call: &FunctionCall) -> Result<Vec<u8>> {
141
166
& function_call. parameters . as_ref ( ) . unwrap ( ) [ 1 ] ,
142
167
& * CUR_ENGINE . lock ( ) ,
143
168
) {
169
+ let linker = CUR_LINKER . lock ( ) ;
170
+ let linker = linker. deref ( ) . as_ref ( ) . ok_or ( HyperlightGuestError :: new (
171
+ ErrorCode :: GuestError ,
172
+ "impossible: wasm runtime has no valid linker" . to_string ( ) ,
173
+ ) ) ?;
174
+
144
175
let module = unsafe { Module :: deserialize_raw ( engine, platform:: map_buffer ( * phys, * len) ) ? } ;
176
+ let mut store = Store :: new ( engine, ( ) ) ;
177
+ let instance = linker. instantiate ( & mut store, & module) ?;
178
+
145
179
* CUR_MODULE . lock ( ) = Some ( module) ;
180
+ * CUR_STORE . lock ( ) = Some ( store) ;
181
+ * CUR_INSTANCE . lock ( ) = Some ( instance) ;
146
182
Ok ( get_flatbuffer_result :: < ( ) > ( ( ) ) )
147
183
} else {
148
184
Err ( HyperlightGuestError :: new (
0 commit comments