Skip to content

Commit 3b2f7f2

Browse files
committed
add Stack::prepare_host_frame API
1 parent 0c63604 commit 3b2f7f2

File tree

1 file changed

+40
-1
lines changed
  • crates/wasmi/src/engine/executor/handler

1 file changed

+40
-1
lines changed

crates/wasmi/src/engine/executor/handler/state.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
ResumableOutOfFuelError,
88
StackConfig,
99
},
10-
errors::HostError,
10+
func::FuncInOut,
1111
instance::InstanceEntity,
1212
ir::{self, BoundedSlotSpan, Slot},
1313
store::PrunedStore,
@@ -239,6 +239,17 @@ impl Stack {
239239
self.values.capacity()
240240
}
241241

242+
pub fn prepare_host_frame<'a>(
243+
&'a mut self,
244+
caller_ip: Ip,
245+
callee_params: BoundedSlotSpan,
246+
results_len: u16,
247+
) -> Result<(Sp, FuncInOut<'a>), TrapCode> {
248+
let caller_start = self.frames.prepare_host_frame(caller_ip);
249+
self.values
250+
.prepare_host_frame(caller_start, callee_params, results_len)
251+
}
252+
242253
#[inline(always)]
243254
pub fn push_frame(
244255
&mut self,
@@ -317,6 +328,29 @@ impl ValueStack {
317328
Sp::new(&mut self.cells, start)
318329
}
319330

331+
fn prepare_host_frame<'a>(
332+
&'a mut self,
333+
caller_start: usize,
334+
callee_params: BoundedSlotSpan,
335+
results_len: u16,
336+
) -> Result<(Sp, FuncInOut<'a>), TrapCode> {
337+
let params_offset = usize::from(u16::from(callee_params.span().head()));
338+
let params_len = usize::from(callee_params.len());
339+
let results_len = usize::from(results_len);
340+
let callee_size = params_len.max(results_len);
341+
let Some(callee_start) = caller_start.checked_add(params_offset) else {
342+
return Err(TrapCode::StackOverflow);
343+
};
344+
let Some(callee_end) = callee_start.checked_add(callee_size) else {
345+
return Err(TrapCode::StackOverflow);
346+
};
347+
self.cells.resize(callee_end, UntypedVal::default());
348+
let sp = self.sp(caller_start);
349+
let cells = &mut self.cells[callee_start..];
350+
let inout = FuncInOut::new(cells, params_len, results_len);
351+
Ok((sp, inout))
352+
}
353+
320354
#[inline(always)]
321355
fn push(&mut self, start: usize, len_slots: usize, len_params: u16) -> Result<Sp, TrapCode> {
322356
debug_assert!(usize::from(len_params) <= len_slots);
@@ -410,6 +444,11 @@ impl CallStack {
410444
top.ip = ip;
411445
}
412446

447+
fn prepare_host_frame(&mut self, caller_ip: Ip) -> usize {
448+
self.sync_ip(caller_ip);
449+
self.top_start()
450+
}
451+
413452
#[inline(always)]
414453
fn push(
415454
&mut self,

0 commit comments

Comments
 (0)