Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

cranelift: preserve fuel across stack-switching instructions (#14644)

Craig Tillerbe0ef6b1e308
diff --git a/crates/cranelift/src/func_environ.rs b/crates/cranelift/src/func_environ.rs
index 665cba437b..1fb1fe1781 100644
--- a/crates/cranelift/src/func_environ.rs
+++ b/crates/cranelift/src/func_environ.rs
@@ -651,7 +651,13 @@ impl<'module_environment> FuncEnvironment<'module_environment> {
| Operator::ReturnCall { .. }
| Operator::ReturnCallRef { .. }
| Operator::ReturnCallIndirect { .. }
- | Operator::Throw { .. } | Operator::ThrowRef => {
+ | Operator::Throw { .. }
+ | Operator::ThrowRef
+ | Operator::Resume { .. }
+ | Operator::ResumeThrow { .. }
+ | Operator::ResumeThrowRef { .. }
+ | Operator::Suspend { .. }
+ | Operator::Switch { .. } => {
self.fuel_increment_var(builder);
self.fuel_save_from_var(builder);
}
@@ -713,7 +719,14 @@ impl<'module_environment> FuncEnvironment<'module_environment> {
// After a function call we need to reload our fuel value since the
// function may have changed it.
match op {
- Operator::Call { .. } | Operator::CallIndirect { .. } | Operator::CallRef { .. } => {
+ Operator::Call { .. }
+ | Operator::CallIndirect { .. }
+ | Operator::CallRef { .. }
+ | Operator::Resume { .. }
+ | Operator::ResumeThrow { .. }
+ | Operator::ResumeThrowRef { .. }
+ | Operator::Suspend { .. }
+ | Operator::Switch { .. } => {
self.fuel_load_into_var(builder);
}
Operator::Else => {
diff --git a/crates/cranelift/src/func_environ/stack_switching/instructions.rs b/crates/cranelift/src/func_environ/stack_switching/instructions.rs
index 4a18503237..b814327908 100644
--- a/crates/cranelift/src/func_environ/stack_switching/instructions.rs
+++ b/crates/cranelift/src/func_environ/stack_switching/instructions.rs
@@ -1998,6 +1998,7 @@ fn translate_resume_impl<'a>(
let (handler_index, suspended_contref, suspended_contobj) = {
builder.switch_to_block(suspend_block);
builder.seal_block(suspend_block);
+ env.on_catch_block_entry(builder);
let suspended_continuation = new_stack_chain.unchecked_get_continuation();
let mut suspended_continuation = helpers::VMContRef::new(suspended_continuation);
diff --git a/tests/all/fuel.rs b/tests/all/fuel.rs
index 1891fc8861..042069234d 100644
--- a/tests/all/fuel.rs
+++ b/tests/all/fuel.rs
@@ -1394,3 +1394,75 @@ fn else_and_end_operator_cost_on_taken_paths(config: &mut Config) -> Result<()>
Ok(())
}
+
+#[cfg(all(
+ feature = "stack-switching",
+ target_os = "linux",
+ target_arch = "x86_64"
+))]
+#[test]
+#[cfg_attr(miri, ignore)]
+fn stack_switching_respects_fuel() -> Result<()> {
+ const WAT: &str = r#"
+ (module
+ (type $ft (func))
+ (type $ct (cont $ft))
+ (tag $t)
+
+ (func $burn
+ (local $i i32)
+ (local.set $i (i32.const 50))
+ (loop $l
+ (suspend $t)
+ (local.tee $i (i32.sub (local.get $i) (i32.const 1)))
+ (br_if $l)
+ )
+ )
+
+ (func (export "run")
+ (local $k (ref null $ct))
+ (local.set $k (cont.new $ct (ref.func $burn)))
+ (loop $l
+ (block $on_t (result (ref $ct))
+ (resume $ct (on $t $on_t) (local.get $k))
+ return
+ )
+ (local.set $k)
+ (br $l)
+ )
+ )
+
+ (elem declare func $burn)
+ )
+ "#;
+
+ let mut config = Config::new();
+ config.consume_fuel(true);
+ config.wasm_function_references(true);
+ config.wasm_exceptions(true);
+ config.wasm_stack_switching(true);
+ let engine = Engine::new(&config)?;
+ let module = Module::new(&engine, WAT)?;
+
+ // Measure exact fuel consumed by a full run with plenty of fuel.
+ let mut store = Store::new(&engine, ());
+ store.set_fuel(10_000)?;
+ let instance = Instance::new(&mut store, &module, &[])?;
+ let run = instance.get_typed_func::<(), ()>(&mut store, "run")?;
+ run.call(&mut store, ())?;
+ let total_consumed = 10_000 - store.get_fuel()?;
+ // 50 iterations inside `$burn` (4 ops per iteration + entry) plus 50
+ // iterations in `run` must both be accounted for (> 400 fuel).
+ assert!(
+ total_consumed > 400,
+ "expected continuation fuel to be accounted for, got {total_consumed}"
+ );
+
+ // Providing enough fuel for `run`'s outer loop alone (~250 units) but not
+ // enough for `$burn` + `run` combined must trap with `Trap::OutOfFuel`.
+ store.set_fuel(300)?;
+ let trap = run.call(&mut store, ()).unwrap_err().downcast::<Trap>()?;
+ assert_eq!(trap, Trap::OutOfFuel);
+
+ Ok(())
+}