@@ -14,7 +14,7 @@ use crate::execution::store::{FuncInst, GlobalInst, MemInst, Store};
14
14
use crate :: execution:: value:: Value ;
15
15
use crate :: validation:: code:: read_declared_locals;
16
16
use crate :: value:: InteropValueList ;
17
- use crate :: Error :: RuntimeError ;
17
+ use crate :: Error :: { self , RuntimeError } ;
18
18
use crate :: RuntimeError :: { DivideBy0 , FunctionNotFound , UnrepresentableResult } ;
19
19
use crate :: { Result , ValidationInfo } ;
20
20
51
51
pub fn new_with_hooks ( validation_info : & ' _ ValidationInfo < ' b > , hook_set : H ) -> Result < Self > {
52
52
trace ! ( "Starting instantiation of bytecode" ) ;
53
53
54
- let store = Self :: init_store ( validation_info) ;
54
+ let store = Self :: init_store ( validation_info) ? ;
55
55
56
56
let mut instance = RuntimeInstance {
57
57
wasm_bytecode : validation_info. wasm ,
@@ -100,15 +100,16 @@ where
100
100
params : Param ,
101
101
) -> Result < Returns > {
102
102
// -=-= Verification =-=-
103
- let func_inst = self . store . funcs . get ( func_idx) . expect ( "valid FuncIdx" ) ;
103
+ // TODO(george-cosma): Do we want this to be a RuntimeError(FunctionNotFound)?
104
+ let func_inst = self . store . funcs . get ( func_idx) . ok_or ( Error :: InvalidFunctionIdx ( func_idx) ) ?;
104
105
let func_ty = self . types . get ( func_inst. ty ) . unwrap_validated ( ) ;
105
106
106
107
// Check correct function parameters and return types
107
108
if func_ty. params . valtypes != Param :: TYS {
108
- panic ! ( "Invalid `Param` generics" ) ;
109
+ return Err ( Error :: InvalidParameterTypes ) ;
109
110
}
110
111
if func_ty. returns . valtypes != Returns :: TYS {
111
- panic ! ( "Invalid `Returns` generics" ) ;
112
+ return Err ( Error :: InvalidResultTypes ) ;
112
113
}
113
114
114
115
// -=-= Invoke the function =-=-
@@ -143,19 +144,20 @@ where
143
144
ret_types : & [ ValType ] ,
144
145
) -> Result < Vec < Value > > {
145
146
// -=-= Verification =-=-
146
- let func_inst = self . store . funcs . get ( func_idx) . expect ( "valid FuncIdx" ) ;
147
+ // TODO(george-cosma): Do we want this to be a RuntimeError(FunctionNotFound)?
148
+ let func_inst = self . store . funcs . get ( func_idx) . ok_or ( Error :: InvalidFunctionIdx ( func_idx) ) ?;
147
149
let func_ty = self . types . get ( func_inst. ty ) . unwrap_validated ( ) ;
148
150
149
151
// Verify that the given parameters match the function parameters
150
152
let param_types = params. iter ( ) . map ( |v| v. to_ty ( ) ) . collect :: < Vec < _ > > ( ) ;
151
153
152
154
if func_ty. params . valtypes != param_types {
153
- panic ! ( "Invalid parameters for function" ) ;
155
+ return Err ( Error :: InvalidParameterTypes ) ;
154
156
}
155
157
156
158
// Verify that the given return types match the function return types
157
159
if func_ty. returns . valtypes != ret_types {
158
- panic ! ( "Invalid return types for function" ) ;
160
+ return Err ( Error :: InvalidResultTypes ) ;
159
161
}
160
162
161
163
// -=-= Invoke the function =-=-
@@ -169,9 +171,10 @@ where
169
171
let error = self . function ( func_idx, & mut stack) ;
170
172
error?;
171
173
172
- let func_inst = self . store . funcs . get ( func_idx) . expect ( "valid FuncIdx" ) ;
174
+ // TODO(george-cosma): Do we want this to be a RuntimeError(FunctionNotFound)?
175
+ let func_inst = self . store . funcs . get ( func_idx) . ok_or ( Error :: InvalidFunctionIdx ( func_idx) ) ?;
173
176
let func_ty = self . types . get ( func_inst. ty ) . unwrap_validated ( ) ;
174
-
177
+
175
178
// Pop return values from stack
176
179
let return_values = func_ty
177
180
. returns
@@ -263,9 +266,9 @@ where
263
266
let address = address as usize ;
264
267
mem. data . get ( address..( address + 4 ) )
265
268
} )
266
- . expect ( "TODO trap here" ) ;
269
+ . expect ( "TODO trap here" ) ; // ???
267
270
268
- let data: [ u8 ; 4 ] = data. try_into ( ) . expect ( "this to be exactly 4 bytes" ) ;
271
+ let data: [ u8 ; 4 ] = data. try_into ( ) . expect ( "this to be exactly 4 bytes" ) ; // ???
269
272
u32:: from_le_bytes ( data)
270
273
} ;
271
274
@@ -289,7 +292,7 @@ where
289
292
let address = address as usize ;
290
293
mem. data . get_mut ( address..( address + 4 ) )
291
294
} )
292
- . expect ( "TODO trap here" ) ;
295
+ . expect ( "TODO trap here" ) ; // ???
293
296
294
297
memory_location. copy_from_slice ( & data_to_store. to_le_bytes ( ) ) ;
295
298
trace ! ( "Instruction: i32.store [{relative_address} {data_to_store}] -> []" ) ;
@@ -663,7 +666,7 @@ where
663
666
Ok ( ( ) )
664
667
}
665
668
666
- fn init_store ( validation_info : & ValidationInfo ) -> Store {
669
+ fn init_store ( validation_info : & ValidationInfo ) -> Result < Store > {
667
670
let function_instances: Vec < FuncInst > = {
668
671
let mut wasm_reader = WasmReader :: new ( validation_info. wasm ) ;
669
672
@@ -674,24 +677,22 @@ where
674
677
. zip ( func_blocks)
675
678
. map ( |( ty, func) | {
676
679
wasm_reader
677
- . move_start_to ( * func)
678
- . expect ( "function index to be in the bounds of the WASM binary" ) ;
680
+ . move_start_to ( * func) ?;
679
681
680
682
let ( locals, bytes_read) = wasm_reader
681
683
. measure_num_read_bytes ( read_declared_locals)
682
684
. unwrap_validated ( ) ;
683
685
684
686
let code_expr = wasm_reader
685
- . make_span ( func. len ( ) - bytes_read)
686
- . expect ( "TODO remove this expect" ) ;
687
+ . make_span ( func. len ( ) - bytes_read) ?;
687
688
688
- FuncInst {
689
+ Ok ( FuncInst {
689
690
ty : * ty,
690
691
locals,
691
692
code_expr,
692
- }
693
+ } )
693
694
} )
694
- . collect ( )
695
+ . collect :: < Result < Vec < FuncInst > > > ( ) ?
695
696
} ;
696
697
697
698
let memory_instances: Vec < MemInst > = validation_info
@@ -714,10 +715,10 @@ where
714
715
} )
715
716
. collect ( ) ;
716
717
717
- Store {
718
+ Ok ( Store {
718
719
funcs : function_instances,
719
720
mems : memory_instances,
720
721
globals : global_instances,
721
- }
722
+ } )
722
723
}
723
724
}
0 commit comments