Skip to content

Commit 12257b9

Browse files
committed
implement call resumption after running out of fuel
1 parent 59b4c1e commit 12257b9

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,23 @@ pub fn init_wasm_func_call<'a, T>(
154154
})
155155
}
156156

157+
pub fn resume_wasm_func_call<'a, T>(
158+
store: &'a mut Store<T>,
159+
code: &'a CodeMap,
160+
stack: &'a mut Stack,
161+
) -> Result<WasmFuncCall<'a, T, state::Init>, Error> {
162+
let (callee_ip, callee_sp, instance) = stack.restore_frame();
163+
Ok(WasmFuncCall {
164+
store,
165+
stack,
166+
code,
167+
callee_ip,
168+
callee_sp,
169+
instance,
170+
state: PhantomData,
171+
})
172+
}
173+
157174
#[cfg(feature = "trampolines")]
158175
pub fn execute_until_done(
159176
mut state: VmState,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod state;
99
#[cfg(feature = "trampolines")]
1010
use self::dispatch::ControlContinue;
1111
pub use self::{
12-
dispatch::{init_wasm_func_call, op_code_to_handler, ExecutionOutcome},
12+
dispatch::{init_wasm_func_call, op_code_to_handler, resume_wasm_func_call, ExecutionOutcome},
1313
state::{Inst, Stack},
1414
};
1515
use self::{

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,14 @@ impl Stack {
285285
self.frames.sync_ip(ip);
286286
}
287287

288+
pub fn restore_frame(&mut self) -> (Ip, Sp, Inst) {
289+
let Some((ip, start, instance)) = self.frames.restore_frame() else {
290+
panic!("restore_frame: missing top-frame")
291+
};
292+
let sp = self.values.sp(start);
293+
(ip, sp, instance)
294+
}
295+
288296
pub fn return_prepare_host_frame<'a>(
289297
&'a mut self,
290298
callee_params: BoundedSlotSpan,
@@ -568,6 +576,12 @@ impl CallStack {
568576
top.ip = ip;
569577
}
570578

579+
fn restore_frame(&self) -> Option<(Ip, usize, Inst)> {
580+
let instance = self.instance?;
581+
let top = self.top()?;
582+
Some((top.ip, top.start, instance))
583+
}
584+
571585
fn prepare_host_frame(&mut self, caller_ip: Option<Ip>) -> usize {
572586
if let Some(caller_ip) = caller_ip {
573587
self.sync_ip(caller_ip);

crates/wasmi/src/engine/executor/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ pub use self::handler::{op_code_to_handler, Inst, Stack};
22
use super::code_map::CodeMap;
33
use crate::{
44
engine::{
5-
executor::handler::{init_wasm_func_call, ExecutionOutcome},
5+
executor::handler::{init_wasm_func_call, resume_wasm_func_call, ExecutionOutcome},
66
CallParams,
77
CallResults,
88
EngineInner,
@@ -284,16 +284,16 @@ impl<'engine> EngineExecutor<'engine> {
284284
/// - When encountering a Wasm or host trap during the execution of `func`.
285285
fn resume_func_out_of_fuel<T, Results>(
286286
&mut self,
287-
_store: &mut Store<T>,
288-
_results: Results,
287+
store: &mut Store<T>,
288+
results: Results,
289289
) -> Result<<Results as CallResults>::Results, ExecutionOutcome>
290290
where
291291
Results: CallResults,
292292
{
293-
// self.execute_func(store)?;
294-
// let results = self.write_results_back(results);
295-
// Ok(results)
296-
todo!()
293+
let results = resume_wasm_func_call(store, self.code_map, self.stack)?
294+
.execute()?
295+
.write_results(results);
296+
Ok(results)
297297
}
298298

299299
/// Convenience forwarder to dispatch host functions.

0 commit comments

Comments
 (0)