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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
mod frames;
mod values;
pub use self::{
frames::{CallStack, FuncFrame},
values::ValueStack,
};
use super::{
code_map::{CodeMap, InstructionsRef},
exec_context::FunctionExecutor,
func_types::FuncTypeRegistry,
FuncParams,
};
use crate::{
core::UntypedValue,
func::{HostFuncEntity, WasmFuncEntity},
AsContext,
AsContextMut,
Instance,
};
use core::{
fmt::{self, Display},
mem::size_of,
};
use wasmi_core::{Trap, TrapCode};
const DEFAULT_MIN_VALUE_STACK_HEIGHT: usize = 1024;
const DEFAULT_MAX_VALUE_STACK_HEIGHT: usize = 1024 * DEFAULT_MIN_VALUE_STACK_HEIGHT;
const DEFAULT_MAX_RECURSION_DEPTH: usize = 1024;
#[cold]
fn err_stack_overflow() -> TrapCode {
TrapCode::StackOverflow
}
#[derive(Debug, Copy, Clone)]
pub struct StackLimits {
initial_value_stack_height: usize,
maximum_value_stack_height: usize,
maximum_recursion_depth: usize,
}
#[derive(Debug)]
pub enum LimitsError {
InitialValueStackExceedsMaximum,
}
impl Display for LimitsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
LimitsError::InitialValueStackExceedsMaximum => {
write!(f, "initial value stack heihgt exceeds maximum stack height")
}
}
}
}
impl StackLimits {
pub fn new(
initial_value_stack_height: usize,
maximum_value_stack_height: usize,
maximum_recursion_depth: usize,
) -> Result<Self, LimitsError> {
if initial_value_stack_height > maximum_value_stack_height {
return Err(LimitsError::InitialValueStackExceedsMaximum);
}
Ok(Self {
initial_value_stack_height,
maximum_value_stack_height,
maximum_recursion_depth,
})
}
}
impl Default for StackLimits {
fn default() -> Self {
let register_len = size_of::<UntypedValue>();
let initial_value_stack_height = DEFAULT_MIN_VALUE_STACK_HEIGHT / register_len;
let maximum_value_stack_height = DEFAULT_MAX_VALUE_STACK_HEIGHT / register_len;
Self {
initial_value_stack_height,
maximum_value_stack_height,
maximum_recursion_depth: DEFAULT_MAX_RECURSION_DEPTH,
}
}
}
#[derive(Debug, Default)]
pub struct Stack {
pub(crate) values: ValueStack,
frames: CallStack,
}
impl Stack {
pub fn new(limits: StackLimits) -> Self {
let frames = CallStack::new(limits.maximum_recursion_depth);
let values = ValueStack::new(
limits.initial_value_stack_height,
limits.maximum_value_stack_height,
);
Self { frames, values }
}
#[inline(always)]
pub fn executor<'engine>(
&'engine mut self,
frame: &'engine mut FuncFrame,
codemap: &'engine CodeMap,
) -> FunctionExecutor {
let insts = codemap.insts(frame.iref());
FunctionExecutor::new(frame, insts, &mut self.values)
}
pub(crate) fn call_wasm_root(
&mut self,
wasm_func: &WasmFuncEntity,
code_map: &CodeMap,
) -> Result<FuncFrame, TrapCode> {
let iref = self.call_wasm_impl(wasm_func, code_map)?;
let instance = wasm_func.instance();
Ok(self.frames.init(iref, instance))
}
pub(crate) fn call_wasm<'engine>(
&mut self,
caller: &mut FuncFrame,
wasm_func: &WasmFuncEntity,
code_map: &'engine CodeMap,
) -> Result<FuncFrame, TrapCode> {
let iref = self.call_wasm_impl(wasm_func, code_map)?;
let instance = wasm_func.instance();
let frame = self.frames.push(caller, iref, instance)?;
Ok(frame)
}
pub(crate) fn call_wasm_impl<'engine>(
&mut self,
wasm_func: &WasmFuncEntity,
code_map: &'engine CodeMap,
) -> Result<InstructionsRef, TrapCode> {
let header = code_map.header(wasm_func.func_body());
let max_stack_height = header.max_stack_height();
self.values.reserve(max_stack_height)?;
let len_locals = header.len_locals();
self.values
.extend_zeros(len_locals)
.expect("stack overflow is unexpected due to previous stack reserve");
let iref = header.iref();
Ok(iref)
}
pub fn return_wasm(&mut self) -> Option<FuncFrame> {
self.frames.pop()
}
pub(crate) fn call_host_root<C>(
&mut self,
ctx: C,
host_func: HostFuncEntity<<C as AsContext>::UserState>,
func_types: &FuncTypeRegistry,
) -> Result<(), Trap>
where
C: AsContextMut,
{
self.call_host_impl(ctx, host_func, None, func_types)
}
pub(crate) fn call_host<C>(
&mut self,
ctx: C,
caller: &FuncFrame,
host_func: HostFuncEntity<<C as AsContext>::UserState>,
func_types: &FuncTypeRegistry,
) -> Result<(), Trap>
where
C: AsContextMut,
{
let instance = caller.instance();
self.call_host_impl(ctx, host_func, Some(instance), func_types)
}
#[inline(never)]
fn call_host_impl<C>(
&mut self,
mut ctx: C,
host_func: HostFuncEntity<<C as AsContext>::UserState>,
instance: Option<Instance>,
func_types: &FuncTypeRegistry,
) -> Result<(), Trap>
where
C: AsContextMut,
{
let (input_types, output_types) = func_types
.resolve_func_type(host_func.signature())
.params_results();
let len_inputs = input_types.len();
let len_outputs = output_types.len();
let max_inout = len_inputs.max(len_outputs);
self.values.reserve(max_inout)?;
if len_outputs > len_inputs {
let delta = len_outputs - len_inputs;
self.values.extend_zeros(delta)?;
}
let params_results = FuncParams::new(
self.values.peek_as_slice_mut(max_inout),
len_inputs,
len_outputs,
);
host_func.call(&mut ctx, instance, params_results)?;
if len_outputs < len_inputs {
let delta = len_inputs - len_outputs;
self.values.drop(delta);
}
Ok(())
}
pub fn clear(&mut self) {
self.values.clear();
self.frames.clear();
}
}