-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathconst_interpreter_loop.rs
More file actions
161 lines (145 loc) · 5.3 KB
/
const_interpreter_loop.rs
File metadata and controls
161 lines (145 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use alloc::vec::Vec;
use crate::{
addrs::ModuleAddr,
assert_validated::UnwrapValidatedExt,
config::Config,
core::{
reader::{
span::Span,
types::{FuncType, ResultType},
WasmReader,
},
utils::ToUsizeExt,
},
unreachable_validated,
value::{self, Ref},
value_stack::Stack,
RefType, RuntimeError, Store, Value,
};
// TODO update this documentation
/// Execute a previosly-validated constant expression. These type of expressions are used for initializing global
/// variables, data and element segments.
///
/// # Arguments
/// TODO
///
/// # Safety
/// This function assumes that the expression has been validated. Passing unvalidated code will likely result in a
/// panic, or undefined behaviour.
// TODO this signature might change to support hooks or match the spec better
pub(crate) fn run_const<T: Config>(
wasm: &mut WasmReader,
stack: &mut Stack,
module: ModuleAddr,
store: &Store<T>,
) -> Result<(), RuntimeError> {
use crate::core::reader::types::opcode::*;
loop {
let first_instr_byte = wasm.read_u8().unwrap_validated();
#[cfg(debug_assertions)]
crate::core::utils::print_beautiful_instruction_name_1_byte(first_instr_byte, wasm.pc);
#[cfg(not(debug_assertions))]
trace!("Read instruction byte {first_instr_byte:#04X?} ({first_instr_byte}) at wasm_binary[{}]", wasm.pc);
match first_instr_byte {
END => {
trace!("Constant instruction: END");
break;
}
GLOBAL_GET => {
let global_idx = wasm.read_var_u32().unwrap_validated().into_usize();
//TODO replace double indirection
let global = store
.globals
.get(store.modules.get(module).global_addrs[global_idx]);
trace!(
"Constant instruction: global.get [{global_idx}] -> [{:?}]",
global
);
stack.push_value::<T>(global.value)?;
}
I32_CONST => {
let constant = wasm.read_var_i32().unwrap_validated();
trace!("Constant instruction: i32.const [] -> [{constant}]");
stack.push_value::<T>(constant.into())?;
}
F32_CONST => {
let constant = value::F32::from_bits(wasm.read_f32().unwrap_validated());
trace!("Constanting instruction: f32.const [] -> [{constant}]");
stack.push_value::<T>(constant.into())?;
}
F64_CONST => {
let constant = value::F64::from_bits(wasm.read_f64().unwrap_validated());
trace!("Constanting instruction: f64.const [] -> [{constant}]");
stack.push_value::<T>(constant.into())?;
}
I64_CONST => {
let constant = wasm.read_var_i64().unwrap_validated();
trace!("Constant instruction: i64.const [] -> [{constant}]");
stack.push_value::<T>(constant.into())?;
}
REF_NULL => {
let reftype = RefType::read(wasm).unwrap_validated();
stack.push_value::<T>(Value::Ref(Ref::Null(reftype)))?;
trace!("Instruction: ref.null '{:?}' -> [{:?}]", reftype, reftype);
}
REF_FUNC => {
// we already checked for the func_idx to be in bounds during validation
let func_idx = wasm.read_var_u32().unwrap_validated().into_usize();
let func_addr = *store
.modules
.get(module)
.func_addrs
.get(func_idx)
.unwrap_validated();
stack.push_value::<T>(Value::Ref(Ref::Func(func_addr)))?;
}
FD_EXTENSIONS => {
use crate::core::reader::types::opcode::fd_extensions::*;
match wasm.read_var_u32().unwrap_validated() {
V128_CONST => {
let mut data = [0; 16];
for byte_ref in &mut data {
*byte_ref = wasm.read_u8().unwrap_validated();
}
stack.push_value::<T>(Value::V128(data))?;
}
0x00..=0x0B | 0x0D.. => unreachable_validated!(),
}
}
0x00..=0x0A
| 0x0C..=0x22
| 0x24..=0x40
| 0x45..=0xBF
| 0xC0..=0xCF
| 0xD1
| 0xD3..=0xFC
| 0xFE..=0xFF => {
unreachable_validated!();
}
}
}
Ok(())
}
pub(crate) fn run_const_span<T: Config>(
wasm: &[u8],
span: &Span,
module: ModuleAddr,
store: &Store<T>,
) -> Result<Option<Value>, RuntimeError> {
let mut wasm = WasmReader::new(wasm);
wasm.move_start_to(*span).unwrap_validated();
let mut stack = Stack::new::<T>(
Vec::new(),
&FuncType {
params: ResultType {
valtypes: Vec::new(),
},
returns: ResultType {
valtypes: Vec::new(),
},
},
&[],
)?;
run_const(&mut wasm, &mut stack, module, store)?;
Ok(stack.peek_value())
}