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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
use crate::{
DataSegmentEntity,
Func,
FuncEntity,
core::{
CoreElementSegment as ElementSegmentEntity,
CoreGlobal as GlobalEntity,
CoreMemory as MemoryEntity,
CoreTable as TableEntity,
},
engine::{
code_map::FuncEntry,
executor::handler::{
dispatch::{Break, Control},
state::{self, DoneReason, Freg32, Freg64, Inst, Ip, Ireg, Mem0Len, Mem0Ptr, Sp},
utils::{self, GetValue, IntoControl as _, LoadEntity, SetValue, get_value, set_value},
},
},
ir::{self, BoundedSlotSpan, BranchOffset},
store::PrunedStore,
};
use core::ptr::NonNull;
/// Utility type to store the arguments of an execution handler and provide a clean API.
#[derive(Debug, Copy, Clone)]
pub struct Args {
/// The instruction pointer.
pub ip: Ip,
/// The stack pointer of the top frame.
pub sp: Sp,
/// The pointer to the data of the default memory at index 0.
pub mem0_ptr: Mem0Ptr,
/// The number of bytes of the default memory at index 0.
pub mem0_len: Mem0Len,
/// A reference to instance related entities.
pub instance: Inst,
/// The general purpose (or integer) accumulator register.
pub ireg: Ireg,
/// The 32-bit float accumulator register.
pub freg32: Freg32,
/// The 64-bit float accumulator register.
pub freg64: Freg64,
}
impl Args {
/// Creates a new [`Args`] from its parts.
#[inline]
#[expect(clippy::too_many_arguments)]
pub fn from_parts(
ip: Ip,
sp: Sp,
mem0_ptr: Mem0Ptr,
mem0_len: Mem0Len,
instance: Inst,
ireg: Ireg,
freg32: Freg32,
freg64: Freg64,
) -> Self {
Self {
ip,
sp,
mem0_ptr,
mem0_len,
instance,
ireg,
freg32,
freg64,
}
}
/// Consume `self` to return its parts.
#[inline]
pub fn into_parts(self) -> (Ip, Sp, Mem0Ptr, Mem0Len, Inst, Ireg, Freg32, Freg64) {
(
self.ip,
self.sp,
self.mem0_ptr,
self.mem0_len,
self.instance,
self.ireg,
self.freg32,
self.freg64,
)
}
/// Decodes and returns an [`Op`] of type `T` using `self`.
///
/// Aligns `self.ip` to [`Op`] bounds if `indirect-dispatch` is disabled.
///
/// [`Op`]: crate::ir::Op
#[inline]
pub unsafe fn decode_op<T: ir::Decode>(&mut self) -> T {
let old_ip = self.ip;
let op = unsafe { self.decode::<T>() };
self.ip = self.ip.align_relative_to(old_ip);
op
}
/// Decodes and returns a value of type `T` using `self`.
#[inline]
pub unsafe fn decode<T: ir::Decode>(&mut self) -> T {
let ip = match cfg!(feature = "indirect-dispatch") {
true => unsafe { self.ip.skip::<ir::OpCode>() },
false => unsafe { self.ip.skip::<::core::primitive::usize>() },
};
let (new_ip, op) = unsafe { ip.decode() };
self.ip = new_ip;
op
}
/// Returns a value of type `Dst` from `src`.
#[inline]
pub fn get<Dst, Src>(&self, src: Src) -> Dst
where
Src: GetValue<Dst>,
{
get_value(src, self.sp, self.ireg, self.freg32, self.freg64)
}
/// Stores `src` of type `Src` in `dst`.
#[inline]
pub fn set<Dst, Src>(&mut self, dst: Dst, src: Src)
where
Dst: SetValue<Src>,
{
(self.ireg, self.freg32, self.freg64) =
set_value(dst, src, self.sp, self.ireg, self.freg32, self.freg64);
}
/// Updates the [`Ip`] of `self` with `new_ip`.
#[inline]
pub fn set_ip(&mut self, new_ip: Ip) {
self.ip = new_ip;
}
/// Offsets the [`Ip`] of `self` by `offset`.
#[inline]
pub fn offset_ip(&mut self, offset: BranchOffset) {
self.ip = unsafe { self.ip.offset(i32::from(offset) as isize) };
}
/// Returns the bytes of the default memory at index 0.
#[inline]
pub fn fetch_default_memory_bytes<'a>(&mut self, _store: &'a mut PrunedStore) -> &'a mut [u8] {
state::mem0_bytes::<'a>(self.mem0_ptr, self.mem0_len)
}
/// Returns an exclusive reference to the memory at `index`.
#[inline]
pub fn fetch_memory<'a, Addr>(
&mut self,
store: &'a mut PrunedStore,
addr: Addr,
) -> &'a mut MemoryEntity
where
Inst: LoadEntity<Addr, Entity = MemoryEntity>,
{
// SAFETY: `addr` stems from a Wasmi IR operator and thus addresses a memory entry of
// `self.instance` whose cache was warmed at instantiation. The `state` borrow
// scopes the returned reference.
unsafe { self.instance.load_entity_mut(store, addr) }
}
/// Returns an exclusive reference to the global at `index`.
#[inline]
pub fn fetch_global<'a, Addr>(
&mut self,
store: &'a mut PrunedStore,
addr: Addr,
) -> &'a mut GlobalEntity
where
Inst: LoadEntity<Addr, Entity = GlobalEntity>,
{
// SAFETY: `addr` stems from a Wasmi IR operator and thus addresses a global entry of
// `self.instance` whose cache was warmed at instantiation. The `state` borrow
// scopes the returned reference.
unsafe { self.instance.load_entity_mut(store, addr) }
}
/// Returns an exclusive reference to the table at `index`.
#[inline]
pub fn fetch_table<'a, Addr>(
&mut self,
store: &'a mut PrunedStore,
addr: Addr,
) -> &'a mut TableEntity
where
Inst: LoadEntity<Addr, Entity = TableEntity>,
{
// SAFETY: `addr` stems from a Wasmi IR operator and thus addresses a table entry of
// `self.instance` whose cache was warmed at instantiation. The `state` borrow
// scopes the returned reference.
unsafe { self.instance.load_entity_mut(store, addr) }
}
/// Returns an exclusive reference to the element segment at `index`.
#[inline]
pub fn fetch_elem<'a, Addr>(
&mut self,
store: &'a mut PrunedStore,
addr: Addr,
) -> &'a mut ElementSegmentEntity
where
Inst: LoadEntity<Addr, Entity = ElementSegmentEntity>,
{
// SAFETY: `addr` stems from a Wasmi IR operator and thus addresses an element segment
// entry of `self.instance` whose cache was warmed at instantiation. The `state`
// borrow scopes the returned reference.
unsafe { self.instance.load_entity_mut(store, addr) }
}
/// Returns an exclusive reference to the data segment at `index`.
#[inline]
pub fn fetch_data<'a, Addr>(
&mut self,
store: &'a mut PrunedStore,
addr: Addr,
) -> &'a mut DataSegmentEntity
where
Inst: LoadEntity<Addr, Entity = DataSegmentEntity>,
{
// SAFETY: `addr` stems from a Wasmi IR operator and thus addresses a data segment entry
// of `self.instance` whose cache was warmed at instantiation. The `state` borrow
// scopes the returned reference.
unsafe { self.instance.load_entity_mut(store, addr) }
}
/// Reloads the data pointer and length of the default memory at index 0 from `state`.
#[inline]
pub fn reload_mem0(&mut self) {
(self.mem0_ptr, self.mem0_len) = utils::extract_mem0(self.instance);
}
/// Calls `func` with `params` on `instance` with `state` using `self`.
#[inline(always)]
pub fn call_func_entry(
&mut self,
store: &mut PrunedStore,
func: &FuncEntry,
params: BoundedSlotSpan,
instance: Option<Inst>,
) -> Control<(), Break> {
(self.ip, self.sp) =
utils::call_func_entry(store, self.ip, self.sp, params, func, instance)?;
Control::Continue(())
}
/// Tail-calls `func` with `params` on `instance` with `state` using `self`.
#[inline(always)]
pub fn return_call_func_entry(
&mut self,
store: &mut PrunedStore,
func: &FuncEntry,
params: BoundedSlotSpan,
instance: Option<Inst>,
) -> Control<(), Break> {
(self.ip, self.sp) = utils::return_call_func_entry(store, self.sp, params, func, instance)?;
Control::Continue(())
}
/// Resolves the [`Func`] at `table[index]` of type `func_type` using `state`.
#[inline]
pub fn resolve_indirect_func<Idx, Table>(
&mut self,
store: &mut PrunedStore,
index: Idx,
table: Table,
func_type: ir::FuncType,
) -> Control<(Func, NonNull<FuncEntity>), Break>
where
Idx: GetValue<u64>,
Inst: LoadEntity<Table, Entity = TableEntity>,
{
utils::resolve_indirect_func(index, table, func_type, store, self).into_control()
}
/// Calls `func` with `params` with `state` using `self`.
#[inline]
pub fn call_wasm_or_host_func(
&mut self,
store: &mut PrunedStore,
func: Func,
func_entity: NonNull<FuncEntity>,
params: BoundedSlotSpan,
) -> Control<(), Break> {
(
self.ip,
self.sp,
self.mem0_ptr,
self.mem0_len,
self.instance,
) = utils::call_wasm_or_host(
store,
self.ip,
self.sp,
func,
func_entity,
params,
self.mem0_ptr,
self.mem0_len,
self.instance,
)?;
Control::Continue(())
}
/// Tail-calls `func` with `params` with `state` using `self`.
#[inline]
pub fn return_call_wasm_or_host_func(
&mut self,
store: &mut PrunedStore,
func: Func,
func_entity: NonNull<FuncEntity>,
params: BoundedSlotSpan,
) -> Control<(), Break> {
(
self.ip,
self.sp,
self.mem0_ptr,
self.mem0_len,
self.instance,
) = utils::return_call_wasm_or_host(
store,
self.sp,
func,
func_entity,
params,
self.mem0_ptr,
self.mem0_len,
self.instance,
)?;
Control::Continue(())
}
/// Pops the top-most frame from the call stack.
#[inline]
pub fn pop_frame(&mut self, store: &mut PrunedStore) -> Control<(), Break> {
// Note: copied out so that the `done!` closure below captures just this `Sp` instead
// of `self`, which would force the whole `Args` into memory on entry.
let caller_sp = self.sp;
let Some((ip, sp, mem0_ptr, mem0_len, instance)) =
store
.stack_mut()
.pop_frame(self.mem0_ptr, self.mem0_len, self.instance)
else {
// No more frames on the call stack -> break out of execution!
done!(store, DoneReason::Return(caller_sp))
};
self.ip = ip;
self.sp = sp;
self.mem0_ptr = mem0_ptr;
self.mem0_len = mem0_len;
self.instance = instance;
Control::Continue(())
}
}