@@ -4,6 +4,7 @@ use crate::{
4
4
runtime:: { BlockType , CallFrame , LabelArgs , LabelFrame } ,
5
5
Error , FuncContext , ModuleInstance , Result , Store , Trap ,
6
6
} ;
7
+ use alloc:: format;
7
8
use alloc:: { string:: ToString , vec:: Vec } ;
8
9
use core:: ops:: { BitAnd , BitOr , BitXor , Neg } ;
9
10
use tinywasm_types:: { ElementKind , Instruction , ValType } ;
@@ -32,28 +33,33 @@ impl InterpreterRuntime {
32
33
33
34
// The function to execute, gets updated from ExecResult::Call
34
35
let mut instrs = & wasm_func. instructions ;
36
+ let mut instr_count = instrs. len ( ) ;
35
37
36
38
let mut current_module = store. get_module_instance ( func_inst. owner ) . unwrap ( ) . clone ( ) ;
37
39
38
- while let Some ( instr) = instrs. get ( cf. instr_ptr ) {
40
+ loop {
41
+ if unlikely ( cf. instr_ptr >= instr_count) {
42
+ cold ( ) ;
43
+ log:: error!( "instr_ptr out of bounds: {} >= {}" , cf. instr_ptr, instr_count) ;
44
+ return Err ( Error :: Other ( format ! ( "instr_ptr out of bounds: {} >= {}" , cf. instr_ptr, instr_count) ) ) ;
45
+ }
46
+ let instr = & instrs[ cf. instr_ptr ] ;
47
+
39
48
match exec_one ( & mut cf, instr, instrs, stack, store, & current_module) ? {
40
49
// Continue execution at the new top of the call stack
41
50
ExecResult :: Call => {
42
51
cf = stack. call_stack . pop ( ) ?;
43
52
func_inst = cf. func_instance . clone ( ) ;
44
- wasm_func = func_inst. assert_wasm ( ) . expect ( "exec expected wasm function" ) ;
53
+ wasm_func =
54
+ func_inst. assert_wasm ( ) . map_err ( |_| Error :: Other ( "call expected wasm function" . to_string ( ) ) ) ?;
45
55
instrs = & wasm_func. instructions ;
56
+ instr_count = instrs. len ( ) ;
46
57
47
58
if cf. func_instance . owner != current_module. id ( ) {
48
59
current_module. swap (
49
60
store
50
61
. get_module_instance ( cf. func_instance . owner )
51
- . unwrap_or_else ( || {
52
- panic ! (
53
- "exec expected module instance {} to exist for function" ,
54
- cf. func_instance. owner
55
- )
56
- } )
62
+ . ok_or_else ( || Error :: Other ( "call expected module instance" . to_string ( ) ) ) ?
57
63
. clone ( ) ,
58
64
) ;
59
65
}
@@ -79,12 +85,6 @@ impl InterpreterRuntime {
79
85
}
80
86
}
81
87
}
82
-
83
- log:: debug!( "end of exec" ) ;
84
- log:: debug!( "stack: {:?}" , stack. values) ;
85
- log:: debug!( "insts: {:?}" , instrs) ;
86
- log:: debug!( "instr_ptr: {}" , cf. instr_ptr) ;
87
- Err ( Error :: FuncDidNotReturn )
88
88
}
89
89
}
90
90
0 commit comments