Skip to content

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

Migrate more fuel-using tests to `*.wast` (#15146)

Alex Crichton945c1f03a08f
diff --git a/tests/all/fuel.rs b/tests/all/fuel.rs
index 9d6318951e..dd380d3585 100644
--- a/tests/all/fuel.rs
+++ b/tests/all/fuel.rs
@@ -1,412 +1,6 @@
use wasmtime::*;
use wasmtime_test_macros::wasmtime_test;
-#[wasmtime_test(wasm_features(gc, function_references, bulk_memory))]
-#[cfg_attr(miri, ignore)]
-fn iloop(config: &mut Config) -> Result<()> {
- let _ = env_logger::try_init();
-
- config.consume_fuel(true);
- iloop_aborts(
- &config,
- r#"
- (module
- (start 0)
- (func loop br 0 end)
- )
- "#,
- )?;
- iloop_aborts(
- &config,
- r#"
- (module
- (start 0)
- (func loop i32.const 1 br_if 0 end)
- )
- "#,
- )?;
- iloop_aborts(
- &config,
- r#"
- (module
- (start 0)
- (func loop i32.const 0 br_table 0 end)
- )
- "#,
- )?;
- iloop_aborts(
- &config,
- r#"
- (module
- (start 0)
- (func $f0 call $f1 call $f1)
- (func $f1 call $f2 call $f2)
- (func $f2 call $f3 call $f3)
- (func $f3 call $f4 call $f4)
- (func $f4 call $f5 call $f5)
- (func $f5 call $f6 call $f6)
- (func $f6 call $f7 call $f7)
- (func $f7 call $f8 call $f8)
- (func $f8 call $f9 call $f9)
- (func $f9 call $f10 call $f10)
- (func $f10 call $f11 call $f11)
- (func $f11 call $f12 call $f12)
- (func $f12 call $f13 call $f13)
- (func $f13 call $f14 call $f14)
- (func $f14 call $f15 call $f15)
- (func $f15 call $f16 call $f16)
- (func $f16)
- )
- "#,
- )?;
- iloop_aborts(
- &config,
- r#"
- (module
- (start 0)
- (func loop ref.null func br_on_null 0 drop end)
- )
- "#,
- )?;
- iloop_aborts(
- &config,
- r#"
- (module
- (start 0)
- (func
- ref.func 0
- loop (param (ref func))
- br_on_non_null 0
- unreachable
- end
- )
- (elem declare func 0)
- )
- "#,
- )?;
- iloop_aborts(
- &config,
- r#"
- (module
- (start 0)
- (func
- i32.const 0
- ref.i31
- loop (param (ref i31))
- br_on_cast 0 anyref (ref i31)
- unreachable
- end
- )
- (elem declare func 0)
- )
- "#,
- )?;
- iloop_aborts(
- &config,
- r#"
- (module
- (start 0)
- (func
- ref.null any
- loop (param anyref)
- br_on_cast_fail 0 anyref (ref i31)
- unreachable
- end
- )
- (elem declare func 0)
- )
- "#,
- )?;
-
- iloop_aborts(
- &config,
- r#"
- (module
- (memory 1)
- (start 0)
- (func
- i32.const 0
- i32.const 0
- i32.const 65536
- memory.copy
- )
- )
- "#,
- )?;
-
- iloop_aborts(
- &config,
- r#"
- (module
- (memory 1)
- (start 0)
- (func
- i32.const 0
- i32.const 0
- i32.const 65536
- memory.fill
- )
- )
- "#,
- )?;
-
- let data = "a".repeat(65536);
- iloop_aborts(
- &config,
- &format!(
- r#"
- (module
- (memory 1)
- (start 0)
- (func
- i32.const 0
- i32.const 0
- i32.const 65536
- memory.init $d
- )
-
- (data $d "{data}")
- )
- "#
- ),
- )?;
-
- iloop_aborts(
- &config,
- r#"
- (module
- (table 20000 funcref)
- (start 0)
- (func
- i32.const 0
- i32.const 0
- i32.const 20000
- table.copy
- )
- )
- "#,
- )?;
-
- iloop_aborts(
- &config,
- r#"
- (module
- (table 20000 funcref)
- (start 0)
- (func
- i32.const 0
- ref.null func
- i32.const 20000
- table.fill
- )
- )
- "#,
- )?;
-
- iloop_aborts(
- &config,
- r#"
- (module
- (table 0 funcref)
- (start 0)
- (func
- ref.null func
- i32.const 20000
- table.grow
- drop
- )
- )
- "#,
- )?;
-
- let elems = "$f ".repeat(20000);
- iloop_aborts(
- &config,
- &format!(
- r#"
- (module
- (table 20000 funcref)
- (start 0)
- (func
- i32.const 0
- i32.const 0
- i32.const 20000
- table.init $e
- )
- (func $f)
- (elem $e func {elems})
- )
- "#
- ),
- )?;
-
- iloop_aborts(
- &config,
- r#"
- (module
- (type $a (array i8))
- (start 0)
- (func
- i32.const 2_0000
- array.new_default $a
- drop
- )
- )
- "#,
- )?;
-
- iloop_aborts(
- &config,
- r#"
- (module
- (type $a (array (mut i8)))
- (start 0)
- (global $a (ref $a) i32.const 20000 array.new_default $a)
- (global $b (ref $a) i32.const 20000 array.new_default $a)
- (func
- global.get $a
- i32.const 0
- global.get $b
- i32.const 0
- i32.const 20000
- array.copy $a $a
- )
- )
- "#,
- )?;
-
- iloop_aborts(
- &config,
- r#"
- (module
- (type $a (array (mut i8)))
- (start 0)
- (global $a (ref $a) i32.const 20000 array.new_default $a)
- (func
- global.get $a
- i32.const 0
- i32.const 0
- i32.const 20000
- array.fill $a
- )
- )
- "#,
- )?;
-
- iloop_aborts(
- &config,
- &format!(
- r#"
- (module
- (type $a (array (mut i8)))
- (start 0)
- (func
- i32.const 0
- i32.const 65536
- array.new_data $a $d
- drop
- )
-
- (data $d "{data}")
- )
- "#
- ),
- )?;
-
- iloop_aborts(
- &config,
- &format!(
- r#"
- (module
- (type $a (array (mut i8)))
- (start 0)
- (global $a (ref $a) i32.const 20000 array.new_default $a)
- (func
- global.get $a
- i32.const 0
- i32.const 0
- i32.const 20000
- array.init_data $a $d
- )
-
- (data $d "{data}")
- )
- "#
- ),
- )?;
-
- iloop_aborts(
- &config,
- &format!(
- r#"
- (module
- (type $a (array (mut funcref)))
- (start 0)
- (func
- i32.const 0
- i32.const 20000
- array.new_elem $a $e
- drop
- )
- (func $f)
- (elem $e func {elems})
- )
- "#
- ),
- )?;
-
- iloop_aborts(
- &config,
- &format!(
- r#"
- (module
- (type $a (array (mut funcref)))
- (start 0)
- (global $a (ref $a) i32.const 20000 array.new_default $a)
- (func
- global.get $a
- i32.const 0
- i32.const 0
- i32.const 20000
- array.init_elem $a $e
- )
- (func $f)
- (elem $e func {elems})
- )
- "#
- ),
- )?;
-
- iloop_aborts(
- &config,
- r#"
- (module
- (type $a (array (mut i8)))
- (start 0)
- (func
- i32.const 0
- i32.const 20000
- array.new $a
- drop
- )
- )
- "#,
- )?;
-
- fn iloop_aborts(config: &Config, wat: &str) -> Result<()> {
- log::debug!("Testing infinite loop:\n{wat}");
- let engine = Engine::new(&config)?;
- let module = Module::new(&engine, wat)?;
- let mut store = Store::new(&engine, ());
- store.set_fuel(10_000)?;
- let error = Instance::new(&mut store, &module, &[]).err().unwrap();
- assert_eq!(error.downcast::<Trap>().unwrap(), Trap::OutOfFuel);
- Ok(())
- }
-
- Ok(())
-}
-
#[wasmtime_test]
fn manual_fuel(config: &mut Config) -> Result<()> {
config.consume_fuel(true);
@@ -419,39 +13,6 @@ fn manual_fuel(config: &mut Config) -> Result<()> {
Ok(())
}
-#[wasmtime_test]
-#[cfg_attr(miri, ignore)]
-fn host_function_consumes_all(config: &mut Config) -> Result<()> {
- const FUEL: u64 = 10_000;
- config.consume_fuel(true);
- let engine = Engine::new(&config)?;
- let module = Module::new(
- &engine,
- r#"
- (module
- (import "" "" (func))
- (func (export "")
- call 0
- call $other)
- (func $other))
- "#,
- )
- .unwrap();
- let mut store = Store::new(&engine, ());
- store.set_fuel(FUEL).unwrap();
- let func = Func::wrap(&mut store, |mut caller: Caller<'_, ()>| {
- let remaining = caller.get_fuel().unwrap();
- assert_eq!(remaining, FUEL - 2);
- assert!(caller.set_fuel(1).is_ok());
- });
-
- let instance = Instance::new(&mut store, &module, &[func.into()]).unwrap();
- let export = instance.get_typed_func::<(), ()>(&mut store, "").unwrap();
- let trap = export.call(&mut store, ()).unwrap_err();
- assert_eq!(trap.downcast::<Trap>().unwrap(), Trap::OutOfFuel);
- Ok(())
-}
-
#[wasmtime_test]
fn manual_edge_cases(config: &mut Config) -> Result<()> {
config.consume_fuel(true);
@@ -462,135 +23,6 @@ fn manual_edge_cases(config: &mut Config) -> Result<()> {
Ok(())
}
-#[wasmtime_test]
-#[cfg_attr(miri, ignore)]
-fn unconditionally_trapping_memory_accesses_save_fuel_before_trapping(
- config: &mut Config,
-) -> Result<()> {
- config.consume_fuel(true);
- config.memory_reservation(0x1_0000);
-
- let engine = Engine::new(&config)?;
-
- let module = Module::new(
- &engine,
- r#"
- (module
- (memory 1 1)
- (func (export "f") (param i32) (result i32)
- local.get 0
- local.get 0
- i32.add
- ;; This offset is larger than our memory max size and therefore
- ;; will unconditionally trap.
- i32.load8_s offset=0xffffffff))
- "#,
- )
- .unwrap();
-
- let mut store = Store::new(&engine, ());
- let init_fuel = 1_000;
- store.set_fuel(init_fuel).unwrap();
- assert_eq!(init_fuel, store.get_fuel().unwrap());
-
- let instance = Instance::new(&mut store, &module, &[]).unwrap();
- let f = instance
- .get_typed_func::<i32, i32>(&mut store, "f")
- .unwrap();
-
- let trap = f.call(&mut store, 0).unwrap_err();
- assert_eq!(trap.downcast::<Trap>().unwrap(), Trap::MemoryOutOfBounds);
-
- // The `i32.add` consumed some fuel before the unconditionally trapping
- // memory access.
- let consumed_fuel = init_fuel - store.get_fuel().unwrap();
- assert!(consumed_fuel > 0);
- Ok(())
-}
-
-#[wasmtime_test]
-#[cfg_attr(miri, ignore)]
-fn get_fuel_clamps_at_zero(config: &mut Config) -> Result<()> {
- config.consume_fuel(true);
- let engine = Engine::new(config)?;
- let mut store = Store::new(&engine, ());
- let module = Module::new(
- &engine,
- r#"
-(module
- (func $add2 (export "add2") (param $n i32) (result i32)
- (i32.add (local.get $n) (i32.const 2))
- )
-)
- "#,
- )?;
- let instance = Instance::new(&mut store, &module, &[])?;
-
- let add2 = instance.get_typed_func::<i32, i32>(&mut store, "add2")?;
-
- // Start with 6 fuel and one invocation of this function should cost 4 fuel
- store.set_fuel(6)?;
- assert_eq!(store.get_fuel()?, 6);
- add2.call(&mut store, 10)?;
- assert_eq!(store.get_fuel()?, 2);
-
- // One more invocation of the function would technically take us to -2 fuel,
- // but that's not representable, so the store should report 0 fuel after
- // this completes.
- add2.call(&mut store, 10)?;
- assert_eq!(store.get_fuel()?, 0);
-
- // Any further attempts should fail.
- assert!(add2.call(&mut store, 10).is_err());
-
- Ok(())
-}
-
-#[wasmtime_test]
-#[cfg_attr(miri, ignore)]
-fn immediate_trap_with_fuel1(config: &mut Config) -> Result<()> {
- config.consume_fuel(true);
- let engine = Engine::new(config)?;
- let mut store = Store::new(&engine, ());
-
- let module = Module::new(
- &engine,
- r#"
- (module
- (func (export "main"))
- )
- "#,
- )?;
-
- let instance = Instance::new(&mut store, &module, &[])?;
- let main = instance.get_typed_func::<(), ()>(&mut store, "main")?;
- store.set_fuel(1)?;
-
- assert!(main.call(&mut store, ()).is_err());
-
- Ok(())
-}
-
-#[wasmtime_test(strategies(only(Winch)))]
-#[cfg_attr(miri, ignore)]
-fn ensure_stack_alignment(config: &mut Config) -> Result<()> {
- config.consume_fuel(true);
- let engine = Engine::new(config)?;
- let mut store = Store::new(&engine, ());
- store.set_fuel(100000000)?;
-
- let bytes = include_bytes!("../misc_testsuite/winch/fuel_stack_alignment.wat");
- let module = Module::new(&engine, bytes)?;
- let instance = Instance::new(&mut store, &module, &[])?;
- let func = instance.get_typed_func::<f32, ()>(&mut store, "")?;
- let trap = func.call(&mut store, 50397184.0).unwrap_err();
- assert_eq!(
- trap.downcast::<Trap>().unwrap(),
- Trap::UnreachableCodeReached
- );
- Ok(())
-}
-
#[wasmtime_test]
#[cfg_attr(miri, ignore)]
fn custom_operator_cost(config: &mut Config) -> Result<()> {
@@ -712,81 +144,6 @@ fn unreachable_control_operator_cost(config: &mut Config) -> Result<()> {
Ok(())
}
-#[wasmtime_test(wasm_features(exceptions, reference_types))]
-#[cfg_attr(miri, ignore)]
-fn unreachable_try_table_fuel(config: &mut Config) -> Result<()> {
- config.consume_fuel(true);
- let engine = Engine::new(config)?;
- let module = Module::new(
- &engine,
- r#"(module
- (func (export "run") (result i32)
- i32.const 42
- return
- (try_table)))"#,
- )?;
- let mut store = Store::new(&engine, ());
- store.set_fuel(100)?;
- let instance = Instance::new(&mut store, &module, &[])?;
- let run = instance.get_typed_func::<(), i32>(&mut store, "run")?;
- assert_eq!(run.call(&mut store, ())?, 42);
- // Only function entry and i32.const consume fuel.
- assert_eq!(store.get_fuel()?, 98);
- Ok(())
-}
-
-#[wasmtime_test(wasm_features(exceptions, reference_types))]
-#[cfg_attr(miri, ignore)]
-fn exceptions_with_fuel(config: &mut Config) -> Result<()> {
- config.consume_fuel(true);
- let engine = Engine::new(config)?;
- let module = Module::new(
- &engine,
- r#"
- (module
- (tag $e (param i32))
-
- (func (export "throw") (param i32) (result i32)
- (block $catch (result i32)
- (try_table (result i32) (catch $e $catch)
- local.get 0
- if
- i32.const 42
- throw $e
- end
- i32.const 7)))
-
- (func (export "throw_ref") (result i32)
- (block $catch (result i32)
- (try_table (result i32) (catch $e $catch)
- (block $rethrow (result i32 exnref)
- (try_table (result i32 exnref) (catch_ref $e $rethrow)
- i32.const 42
- throw $e))
- throw_ref))))
- "#,
- )?;
- let mut store = Store::new(&engine, ());
- store.set_fuel(100)?;
- let instance = Instance::new(&mut store, &module, &[])?;
-
- let throw = instance.get_typed_func::<i32, i32>(&mut store, "throw")?;
- for (condition, result, consumed) in [(0, 7, 5), (1, 42, 6)] {
- store.set_fuel(100)?;
- assert_eq!(throw.call(&mut store, condition)?, result);
- // Function entry, try_table, local.get, if, and i32.const each
- // consume one fuel unit, as does throw when the branch is taken.
- assert_eq!(store.get_fuel()?, 100 - consumed);
- }
-
- let throw_ref = instance.get_typed_func::<(), i32>(&mut store, "throw_ref")?;
- store.set_fuel(100)?;
- assert_eq!(throw_ref.call(&mut store, ())?, 42);
- // Function entry, two try_tables, i32.const, throw, and throw_ref.
- assert_eq!(store.get_fuel()?, 94);
- Ok(())
-}
-
#[wasmtime_test(wasm_features(bulk_memory, reference_types, gc, function_references))]
#[cfg_attr(miri, ignore)]
fn custom_variable_operator_cost(config: &mut Config) -> Result<()> {
@@ -962,89 +319,6 @@ fn variable_operator_cost_follows_bounds_check(config: &mut Config) -> Result<()
Ok(())
}
-#[test]
-#[cfg_attr(miri, ignore)]
-fn huge_table64_grow_cannot_mint_fuel() -> Result<()> {
- huge_table64_grow_cannot_mint_fuel_impl(
- r#"
- (module
- (table $t i64 0 0x10000 (ref null func))
- (func (export "run") (param $delta i64)
- (loop $l
- (drop (table.grow $t (ref.null func) (local.get $delta)))
- (br $l))))
- "#,
- )
-}
-
-#[test]
-#[cfg_attr(miri, ignore)]
-fn huge_table64_grow_cannot_mint_fuel_const() -> Result<()> {
- huge_table64_grow_cannot_mint_fuel_impl(
- r#"
- (module
- (table $t i64 0 0x10000 (ref null func))
- (func (export "run") (param $delta i64)
- (loop $l
- (drop (table.grow $t (ref.null func) (i64.const -500)))
- (br $l))))
- "#,
- )
-}
-
-fn huge_table64_grow_cannot_mint_fuel_impl(wat: &str) -> Result<()> {
- let mut config = Config::new();
- config.consume_fuel(true);
- let engine = Engine::new(&config)?;
- let module = Module::new(&engine, wat)?;
-
- let mut store = Store::new(&engine, ());
- store.set_fuel(100_000)?;
- let instance = Instance::new(&mut store, &module, &[])?;
- let run = instance.get_typed_func::<i64, ()>(&mut store, "run")?;
-
- let trap = run.call(&mut store, -500).unwrap_err().downcast::<Trap>()?;
- assert_eq!(trap, Trap::OutOfFuel);
- assert_eq!(store.get_fuel()?, 0);
- Ok(())
-}
-
-#[test]
-#[cfg_attr(miri, ignore)]
-fn fuel_around_table_grow() -> Result<()> {
- let mut config = Config::new();
- config.consume_fuel(true);
- let engine = Engine::new(&config)?;
- let module = Module::new(
- &engine,
- r#"
- (module
- (type $ft (func))
- (func $f (type $ft))
- (table $t 1 10000000 (ref $ft) (ref.func $f))
- (func (export "grow") (result i32)
- (table.grow $t (ref.func $f) (i32.const 9999999)))
- (func (export "call") (param i32)
- (call_indirect $t (type $ft) (local.get 0))))
- "#,
- )?;
-
- let mut store = Store::new(&engine, ());
- store.set_fuel(2)?;
- let instance = Instance::new(&mut store, &module, &[])?;
- let grow = instance.get_typed_func::<(), i32>(&mut store, "grow")?;
- // The fuel check for a large bulk operation runs after the operation
- // completes so this call still traps with "out of fuel", but the grow
- // itself took effect.
- let trap = grow.call(&mut store, ()).unwrap_err().downcast::<Trap>()?;
- assert_eq!(trap, Trap::OutOfFuel);
-
- store.set_fuel(u64::MAX)?;
- let call = instance.get_typed_func::<i32, ()>(&mut store, "call")?;
- call.call(&mut store, 9999999)?;
- Ok(())
-}
-
#[wasmtime_test(wasm_features(extended_const))]
#[cfg_attr(miri, ignore)]
fn const_expr_honors_operator_cost(config: &mut Config) -> Result<()> {
@@ -1118,160 +392,6 @@ fn module_start_call_honors_operator_cost(config: &mut Config) -> Result<()> {
Ok(())
}
-#[wasmtime_test(strategies(not(Winch)))]
-#[cfg_attr(miri, ignore)]
-fn call_ref_respects_fuel(config: &mut Config) -> Result<()> {
- const WAT: &str = r#"
- (module
- (type $t (func (param i32 i32)))
- (func $run (export "run") (param $cnt i32) (param $depth i32)
- (local $i i32)
- (local.set $i (local.get $cnt))
- local.get $depth
- if
- (local.set $depth (i32.sub (local.get $depth) (i32.const 1)))
- loop $l
- (call_ref $t
- (local.get $cnt)
- (local.get $depth)
- (ref.func $run))
- (local.tee $i (i32.sub (local.get $i) (i32.const 1)))
- if br $l end
- end
- end
- )
- )
- "#;
-
- config.consume_fuel(true);
- config.wasm_reference_types(true);
- config.wasm_function_references(true);
- let engine = Engine::new(config)?;
- let module = Module::new(&engine, WAT)?;
- let mut store = Store::new(&engine, ());
- store.set_fuel(100_000)?;
- let instance = Instance::new(&mut store, &module, &[])?;
- let func = instance.get_func(&mut store, "run").unwrap();
- let result = func.call(&mut store, &[Val::I32(10), Val::I32(10)], &mut []);
- assert!(result.is_err());
- assert!(format!("{result:?}").contains("all fuel consumed by WebAssembly"));
- Ok(())
-}
-
-#[wasmtime_test(wasm_features(exceptions), strategies(not(Winch)))]
-#[cfg_attr(miri, ignore)]
-fn try_call_normal_return_respects_fuel(config: &mut Config) -> Result<()> {
- const WAT: &str = r#"
- (module
- (tag $e)
- (func $run (export "run") (param $cnt i32) (param $depth i32)
- (local $i i32)
- (local.set $i (local.get $cnt))
- local.get $depth
- if
- (local.set $depth (i32.sub (local.get $depth) (i32.const 1)))
- loop $l
- (block $catch
- (try_table (catch $e $catch)
- (call $run (local.get $cnt) (local.get $depth))))
- (local.tee $i (i32.sub (local.get $i) (i32.const 1)))
- if br $l end
- end
- end
- )
- )
- "#;
-
- config.consume_fuel(true);
- let engine = Engine::new(config)?;
- let module = Module::new(&engine, WAT)?;
- let mut store = Store::new(&engine, ());
- store.set_fuel(100_000)?;
- let instance = Instance::new(&mut store, &module, &[])?;
- let func = instance.get_func(&mut store, "run").unwrap();
- let result = func.call(&mut store, &[Val::I32(10), Val::I32(10)], &mut []);
- assert!(result.is_err());
- assert!(format!("{result:?}").contains("all fuel consumed by WebAssembly"));
- Ok(())
-}
-
-#[wasmtime_test(wasm_features(exceptions), strategies(not(Winch)))]
-#[cfg_attr(miri, ignore)]
-fn try_call_exceptional_return_respects_fuel(config: &mut Config) -> Result<()> {
- const WAT: &str = r#"
- (module
- (tag $e)
- (func $run (param $cnt i32) (param $depth i32)
- (local $i i32)
- (local.set $i (local.get $cnt))
- local.get $depth
- if
- (local.set $depth (i32.sub (local.get $depth) (i32.const 1)))
- loop $l
- (block $catch
- (try_table (catch $e $catch)
- (call $run (local.get $cnt) (local.get $depth))))
- (local.tee $i (i32.sub (local.get $i) (i32.const 1)))
- if br $l end
- end
- end
- throw $e
- )
- (func (export "run") (param $cnt i32) (param $depth i32)
- (block $catch
- (try_table (catch $e $catch)
- (call $run (local.get $cnt) (local.get $depth))))
- )
- )
- "#;
-
- config.consume_fuel(true);
- let engine = Engine::new(config)?;
- let module = Module::new(&engine, WAT)?;
- let mut store = Store::new(&engine, ());
- store.set_fuel(100_000)?;
- let instance = Instance::new(&mut store, &module, &[])?;
- let func = instance.get_func(&mut store, "run").unwrap();
- let result = func.call(&mut store, &[Val::I32(10), Val::I32(10)], &mut []);
- assert!(result.is_err());
- assert!(format!("{result:?}").contains("all fuel consumed by WebAssembly"));
- Ok(())
-}
-#[wasmtime_test(wasm_features(extended_const))]
-#[cfg_attr(miri, ignore)]
-fn const_expr_fuel_is_accounted_without_start(config: &mut Config) -> Result<()> {
- // Same module as `const_expr_honors_operator_cost` above, but with no
- // `start` function. The synthesized module startup function is the only
- // place the const-expr runs, and it has no trailing `end` operator to fold
- // the buffered charges into the fuel counter, so it must do that itself on
- // the way out.
- const WAT: &str = r#"
- (module
- (global $g i32 (i32.add (i32.const 1) (i32.const 2)))
- (export "g" (global $g)))
- "#;
-
- config.consume_fuel(true);
- let engine = Engine::new(config)?;
- let module = Module::new(&engine, WAT)?;
-
- let mut store = Store::new(&engine, ());
- store.set_fuel(10_000)?;
- let instance = Instance::new(&mut store, &module, &[])?;
-
- let g = instance
- .get_global(&mut store, "g")
- .unwrap()
- .get(&mut store);
- assert_eq!(g.i32(), Some(3), "global initializer did not run");
-
- // One unit for the startup function's entry charge, plus one for each
- // const-expr operator: `i32.const`, `i32.const`, `i32.add`.
- assert_eq!(10_000 - store.get_fuel()?, 4);
-
- Ok(())
-}
-
#[wasmtime_test(strategies(not(Winch)))]
#[cfg_attr(miri, ignore)]
fn else_and_end_operator_cost_on_taken_paths(config: &mut Config) -> Result<()> {
@@ -1338,75 +458,3 @@ 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(())
-}
diff --git a/tests/misc_testsuite/fuel/bulk-ops.wast b/tests/misc_testsuite/fuel/bulk-ops.wast
index 7d6df8c845..ae9afd07b1 100644
--- a/tests/misc_testsuite/fuel/bulk-ops.wast
+++ b/tests/misc_testsuite/fuel/bulk-ops.wast
@@ -1,5 +1,6 @@
;;! bulk_memory = true
;;! reference_types = true
+;;! memory64 = true
;;! consume_fuel = true
;; Fuel charged by bulk memory and table operations, on top of the usual cost
@@ -393,3 +394,126 @@
)
(start $f))
(assert_return (invoke $Fuel "get-fuel") (i64.const 995))
+
+;; A bulk op whose size-proportional cost exceeds the remaining fuel runs out
+;; of fuel.
+(invoke $Fuel "set-fuel" (i64.const 100))
+(assert_trap
+ (module
+ (memory 1)
+ (start 0)
+ (func
+ i32.const 0
+ i32.const 0
+ i32.const 200
+ memory.copy))
+ "all fuel consumed")
+
+(invoke $Fuel "set-fuel" (i64.const 100))
+(assert_trap
+ (module
+ (memory 1)
+ (start 0)
+ (func
+ i32.const 0
+ i32.const 0
+ i32.const 200
+ memory.fill))
+ "all fuel consumed")
+
+(invoke $Fuel "set-fuel" (i64.const 100))
+(assert_trap
+ (module
+ (memory 1)
+ (start 0)
+ (func
+ i32.const 0
+ i32.const 0
+ i32.const 200
+ memory.init $d)
+ (data $d
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
+ "all fuel consumed")
+
+(invoke $Fuel "set-fuel" (i64.const 100))
+(assert_trap
+ (module
+ (table 200 funcref)
+ (start 0)
+ (func
+ i32.const 0
+ i32.const 0
+ i32.const 200
+ table.copy))
+ "all fuel consumed")
+
+(invoke $Fuel "set-fuel" (i64.const 100))
+(assert_trap
+ (module
+ (table 200 funcref)
+ (start 0)
+ (func
+ i32.const 0
+ ref.null func
+ i32.const 200
+ table.fill))
+ "all fuel consumed")
+
+(invoke $Fuel "set-fuel" (i64.const 100))
+(assert_trap
+ (module
+ (table 0 funcref)
+ (start 0)
+ (func
+ ref.null func
+ i32.const 200
+ table.grow
+ drop))
+ "all fuel consumed")
+
+(invoke $Fuel "set-fuel" (i64.const 100))
+(assert_trap
+ (module
+ (table 200 funcref)
+ (start 0)
+ (func
+ i32.const 0
+ i32.const 0
+ i32.const 200
+ table.init $e)
+ (func $f)
+ (elem $e func
+ $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f
+ $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f
+ $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f
+ $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f
+ $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f
+ $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f
+ $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f
+ $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f))
+ "all fuel consumed")
+
+;; A failed `table.grow` by a huge 64-bit delta must not add fuel, so these
+;; loops still run out of fuel and leave none behind.
+(module
+ (table $t i64 0 0x10000 (ref null func))
+ (func (export "run") (param $delta i64)
+ (loop $l
+ (drop (table.grow $t (ref.null func) (local.get $delta)))
+ (br $l))))
+(invoke $Fuel "set-fuel" (i64.const 100000))
+(assert_trap (invoke "run" (i64.const -500)) "all fuel consumed")
+(assert_return (invoke $Fuel "get-fuel") (i64.const 0))
+
+(module
+ (table $t i64 0 0x10000 (ref null func))
+ (func (export "run")
+ (loop $l
+ (drop (table.grow $t (ref.null func) (i64.const -500)))
+ (br $l))))
+(invoke $Fuel "set-fuel" (i64.const 100000))
+(assert_trap (invoke "run") "all fuel consumed")
+(assert_return (invoke $Fuel "get-fuel") (i64.const 0))
diff --git a/tests/misc_testsuite/fuel/execution.wast b/tests/misc_testsuite/fuel/execution.wast
new file mode 100644
index 0000000000..7116292ff8
--- /dev/null
+++ b/tests/misc_testsuite/fuel/execution.wast
@@ -0,0 +1,155 @@
+;;! reference_types = true
+;;! exceptions = true
+;;! extended_const = true
+;;! consume_fuel = true
+
+(module $Fuel
+ (import "wasmtime" "set-fuel" (func $set (param i64)))
+ (import "wasmtime" "get-fuel" (func $get (result i64)))
+ (export "set-fuel" (func $set))
+ (export "get-fuel" (func $get)))
+(register "fuel" $Fuel)
+
+;; The store's fuel can't go negative: it reads as 0 after overshooting, and
+;; the next call runs out of fuel.
+(module
+ (func (export "add2") (param $n i32) (result i32)
+ (i32.add (local.get $n) (i32.const 2))))
+(invoke $Fuel "set-fuel" (i64.const 6))
+(assert_return (invoke "add2" (i32.const 10)) (i32.const 12))
+(assert_return (invoke $Fuel "get-fuel") (i64.const 2))
+(assert_return (invoke "add2" (i32.const 10)) (i32.const 12))
+(assert_return (invoke $Fuel "get-fuel") (i64.const 0))
+(assert_trap (invoke "add2" (i32.const 10)) "all fuel consumed")
+
+;; Function entry alone uses up a single unit of fuel.
+(module
+ (func (export "main")))
+(invoke $Fuel "set-fuel" (i64.const 1))
+(assert_trap (invoke "main") "all fuel consumed")
+
+;; A host call sees the fuel wasm has used so far, and wasm picks up fuel that
+;; the host changes.
+(module
+ (import "fuel" "get-fuel" (func $get (result i64)))
+ (import "fuel" "set-fuel" (func $set (param i64)))
+ (func (export "run")
+ (if (i64.ne (call $get) (i64.const 9998)) (then unreachable))
+ (call $set (i64.const 1))
+ (call $other))
+ (func $other))
+(invoke $Fuel "set-fuel" (i64.const 10000))
+(assert_trap (invoke "run") "all fuel consumed")
+
+;; Fuel used before an access that always traps is saved before the trap.
+(module
+ (memory 1 1)
+ (func (export "f") (param i32) (result i32)
+ local.get 0
+ local.get 0
+ i32.add
+ ;; This offset is larger than the memory's maximum size, so the access
+ ;; unconditionally traps.
+ i32.load8_s offset=0xffffffff))
+(invoke $Fuel "set-fuel" (i64.const 1000))
+(assert_trap (invoke "f" (i32.const 0)) "out of bounds memory access")
+(assert_return (invoke $Fuel "get-fuel") (i64.const 995))
+
+;; Unreachable code after `return`, including a `try_table`, uses no fuel:
+;; only function entry and `i32.const` do.
+(module
+ (func (export "run") (result i32)
+ i32.const 42
+ return
+ (try_table)))
+(invoke $Fuel "set-fuel" (i64.const 100))
+(assert_return (invoke "run") (i32.const 42))
+(assert_return (invoke $Fuel "get-fuel") (i64.const 98))
+
+;; Function entry, `try_table`, `local.get`, `if` and `i32.const` each use one
+;; unit of fuel, as do `throw` and `throw_ref` when they run.
+(module
+ (tag $e (param i32))
+
+ (func (export "throw") (param i32) (result i32)
+ (block $catch (result i32)
+ (try_table (result i32) (catch $e $catch)
+ local.get 0
+ if
+ i32.const 42
+ throw $e
+ end
+ i32.const 7)))
+
+ (func (export "throw_ref") (result i32)
+ (block $catch (result i32)
+ (try_table (result i32) (catch $e $catch)
+ (block $rethrow (result i32 exnref)
+ (try_table (result i32 exnref) (catch_ref $e $rethrow)
+ i32.const 42
+ throw $e))
+ throw_ref))))
+(invoke $Fuel "set-fuel" (i64.const 100))
+(assert_return (invoke "throw" (i32.const 0)) (i32.const 7))
+(assert_return (invoke $Fuel "get-fuel") (i64.const 95))
+(invoke $Fuel "set-fuel" (i64.const 100))
+(assert_return (invoke "throw" (i32.const 1)) (i32.const 42))
+(assert_return (invoke $Fuel "get-fuel") (i64.const 94))
+(invoke $Fuel "set-fuel" (i64.const 100))
+(assert_return (invoke "throw_ref") (i32.const 42))
+(assert_return (invoke $Fuel "get-fuel") (i64.const 94))
+
+;; Fuel is saved and reloaded around calls inside `try_table`, both when they
+;; return normally and when they throw, so deep recursion through them still
+;; runs out of fuel (#14407).
+(module
+ (tag $e)
+ (func $run (export "run") (param $cnt i32) (param $depth i32)
+ (local $i i32)
+ (local.set $i (local.get $cnt))
+ local.get $depth
+ if
+ (local.set $depth (i32.sub (local.get $depth) (i32.const 1)))
+ loop $l
+ (block $catch
+ (try_table (catch $e $catch)
+ (call $run (local.get $cnt) (local.get $depth))))
+ (local.tee $i (i32.sub (local.get $i) (i32.const 1)))
+ if br $l end
+ end
+ end))
+(invoke $Fuel "set-fuel" (i64.const 100000))
+(assert_trap (invoke "run" (i32.const 10) (i32.const 10)) "all fuel consumed")
+
+(module
+ (tag $e)
+ (func $run (param $cnt i32) (param $depth i32)
+ (local $i i32)
+ (local.set $i (local.get $cnt))
+ local.get $depth
+ if
+ (local.set $depth (i32.sub (local.get $depth) (i32.const 1)))
+ loop $l
+ (block $catch
+ (try_table (catch $e $catch)
+ (call $run (local.get $cnt) (local.get $depth))))
+ (local.tee $i (i32.sub (local.get $i) (i32.const 1)))
+ if br $l end
+ end
+ end
+ throw $e)
+ (func (export "run") (param $cnt i32) (param $depth i32)
+ (block $catch
+ (try_table (catch $e $catch)
+ (call $run (local.get $cnt) (local.get $depth))))))
+(invoke $Fuel "set-fuel" (i64.const 100000))
+(assert_trap (invoke "run" (i32.const 10) (i32.const 10)) "all fuel consumed")
+
+;; Without a start function, the module's startup function is the only place
+;; a const expr runs, and it must still flush the const expr's fuel: one unit
+;; for entry, plus `i32.const`, `i32.const` and `i32.add`.
+(invoke $Fuel "set-fuel" (i64.const 10000))
+(module
+ (global $g (export "g") i32 (i32.add (i32.const 1) (i32.const 2))))
+(assert_return (invoke $Fuel "get-fuel") (i64.const 9996))
+(assert_return (get "g") (i32.const 3))
diff --git a/tests/misc_testsuite/fuel/gc.wast b/tests/misc_testsuite/fuel/gc.wast
new file mode 100644
index 0000000000..9faf17b2cc
--- /dev/null
+++ b/tests/misc_testsuite/fuel/gc.wast
@@ -0,0 +1,242 @@
+;;! bulk_memory = true
+;;! gc = true
+;;! consume_fuel = true
+
+(module $Fuel
+ (import "wasmtime" "set-fuel" (func $set (param i64)))
+ (export "set-fuel" (func $set)))
+
+;; Infinite loops through the typed-reference branch instructions run out of
+;; fuel.
+(invoke $Fuel "set-fuel" (i64.const 10000))
+(assert_trap
+ (module
+ (start 0)
+ (func loop ref.null func br_on_null 0 drop end))
+ "all fuel consumed")
+
+(invoke $Fuel "set-fuel" (i64.const 10000))
+(assert_trap
+ (module
+ (start 0)
+ (func
+ ref.func 0
+ loop (param (ref func))
+ br_on_non_null 0
+ unreachable
+ end)
+ (elem declare func 0))
+ "all fuel consumed")
+
+(invoke $Fuel "set-fuel" (i64.const 10000))
+(assert_trap
+ (module
+ (start 0)
+ (func
+ i32.const 0
+ ref.i31
+ loop (param (ref i31))
+ br_on_cast 0 anyref (ref i31)
+ unreachable
+ end))
+ "all fuel consumed")
+
+(invoke $Fuel "set-fuel" (i64.const 10000))
+(assert_trap
+ (module
+ (start 0)
+ (func
+ ref.null any
+ loop (param anyref)
+ br_on_cast_fail 0 anyref (ref i31)
+ unreachable
+ end))
+ "all fuel consumed")
+
+;; An array bulk op whose size-proportional cost exceeds the remaining fuel
+;; runs out of fuel.
+(invoke $Fuel "set-fuel" (i64.const 100))
+(assert_trap
+ (module
+ (type $a (array i8))
+ (start 0)
+ (func
+ i32.const 200
+ array.new_default $a
+ drop))
+ "all fuel consumed")
+
+(invoke $Fuel "set-fuel" (i64.const 100))
+(assert_trap
+ (module
+ (type $a (array (mut i8)))
+ (start 0)
+ (func
+ i32.const 0
+ i32.const 200
+ array.new $a
+ drop))
+ "all fuel consumed")
+
+(invoke $Fuel "set-fuel" (i64.const 10000))
+(module $Arrays
+ (type $a (array (mut i8)))
+ (global $a (export "a") (ref $a) (array.new_default $a (i32.const 200)))
+ (global $b (export "b") (ref $a) (array.new_default $a (i32.const 200))))
+(register "arrays" $Arrays)
+
+(invoke $Fuel "set-fuel" (i64.const 100))
+(assert_trap
+ (module
+ (type $a (array (mut i8)))
+ (import "arrays" "a" (global $a (ref $a)))
+ (import "arrays" "b" (global $b (ref $a)))
+ (start 0)
+ (func
+ global.get $a
+ i32.const 0
+ global.get $b
+ i32.const 0
+ i32.const 200
+ array.copy $a $a))
+ "all fuel consumed")
+
+(invoke $Fuel "set-fuel" (i64.const 100))
+(assert_trap
+ (module
+ (type $a (array (mut i8)))
+ (import "arrays" "a" (global $a (ref $a)))
+ (start 0)
+ (func
+ global.get $a
+ i32.const 0
+ i32.const 0
+ i32.const 200
+ array.fill $a))
+ "all fuel consumed")
+
+(invoke $Fuel "set-fuel" (i64.const 100))
+(assert_trap
+ (module
+ (type $a (array (mut i8)))
+ (start 0)
+ (func
+ i32.const 0
+ i32.const 200
+ array.new_data $a $d
+ drop)
+ (data $d
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
+ "all fuel consumed")
+
+(invoke $Fuel "set-fuel" (i64.const 100))
+(assert_trap
+ (module
+ (type $a (array (mut i8)))
+ (import "arrays" "a" (global $a (ref $a)))
+ (start 0)
+ (func
+ global.get $a
+ i32.const 0
+ i32.const 0
+ i32.const 200
+ array.init_data $a $d)
+ (data $d
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
+ "all fuel consumed")
+
+(invoke $Fuel "set-fuel" (i64.const 10000))
+(module $FuncArray
+ (type $a (array (mut funcref)))
+ (global (export "a") (ref $a) (array.new_default $a (i32.const 200))))
+(register "func-array" $FuncArray)
+
+(invoke $Fuel "set-fuel" (i64.const 100))
+(assert_trap
+ (module
+ (type $a (array (mut funcref)))
+ (start 0)
+ (func
+ i32.const 0
+ i32.const 200
+ array.new_elem $a $e
+ drop)
+ (func $f)
+ (elem $e func
+ $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f
+ $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f
+ $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f
+ $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f
+ $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f
+ $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f
+ $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f
+ $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f))
+ "all fuel consumed")
+
+(invoke $Fuel "set-fuel" (i64.const 100))
+(assert_trap
+ (module
+ (type $a (array (mut funcref)))
+ (import "func-array" "a" (global $a (ref $a)))
+ (start 0)
+ (func
+ global.get $a
+ i32.const 0
+ i32.const 0
+ i32.const 200
+ array.init_elem $a $e)
+ (func $f)
+ (elem $e func
+ $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f
+ $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f
+ $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f
+ $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f
+ $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f
+ $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f
+ $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f
+ $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f $f))
+ "all fuel consumed")
+
+;; Fuel is saved and reloaded around `call_ref`, so a deep recursion through
+;; it still runs out of fuel (#14407).
+(module
+ (type $t (func (param i32 i32)))
+ (func $run (export "run") (param $cnt i32) (param $depth i32)
+ (local $i i32)
+ (local.set $i (local.get $cnt))
+ local.get $depth
+ if
+ (local.set $depth (i32.sub (local.get $depth) (i32.const 1)))
+ loop $l
+ (call_ref $t
+ (local.get $cnt)
+ (local.get $depth)
+ (ref.func $run))
+ (local.tee $i (i32.sub (local.get $i) (i32.const 1)))
+ if br $l end
+ end
+ end))
+(invoke $Fuel "set-fuel" (i64.const 100000))
+(assert_trap (invoke "run" (i32.const 10) (i32.const 10)) "all fuel consumed")
+
+;; The fuel for a large `table.grow` is charged after the grow completes, so
+;; the call runs out of fuel but the table still grew.
+(invoke $Fuel "set-fuel" (i64.const 10000))
+(module
+ (type $ft (func))
+ (func $f (type $ft))
+ (table $t 1 1000 (ref $ft) (ref.func $f))
+ (func (export "grow") (result i32)
+ (table.grow $t (ref.func $f) (i32.const 999)))
+ (func (export "call") (param i32)
+ (call_indirect $t (type $ft) (local.get 0))))
+(invoke $Fuel "set-fuel" (i64.const 2))
+(assert_trap (invoke "grow") "all fuel consumed")
+(invoke $Fuel "set-fuel" (i64.const 10000))
+(assert_return (invoke "call" (i32.const 999)))
diff --git a/tests/misc_testsuite/fuel/iloop.wast b/tests/misc_testsuite/fuel/iloop.wast
new file mode 100644
index 0000000000..7956c41dc0
--- /dev/null
+++ b/tests/misc_testsuite/fuel/iloop.wast
@@ -0,0 +1,50 @@
+;;! consume_fuel = true
+
+;; Infinite loops and exponential call trees run out of fuel.
+(module $Fuel
+ (import "wasmtime" "set-fuel" (func $set (param i64)))
+ (export "set-fuel" (func $set)))
+
+(invoke $Fuel "set-fuel" (i64.const 10000))
+(assert_trap
+ (module
+ (start 0)
+ (func loop br 0 end))
+ "all fuel consumed")
+
+(invoke $Fuel "set-fuel" (i64.const 10000))
+(assert_trap
+ (module
+ (start 0)
+ (func loop i32.const 1 br_if 0 end))
+ "all fuel consumed")
+
+(invoke $Fuel "set-fuel" (i64.const 10000))
+(assert_trap
+ (module
+ (start 0)
+ (func loop i32.const 0 br_table 0 end))
+ "all fuel consumed")
+
+(invoke $Fuel "set-fuel" (i64.const 10000))
+(assert_trap
+ (module
+ (start 0)
+ (func $f0 call $f1 call $f1)
+ (func $f1 call $f2 call $f2)
+ (func $f2 call $f3 call $f3)
+ (func $f3 call $f4 call $f4)
+ (func $f4 call $f5 call $f5)
+ (func $f5 call $f6 call $f6)
+ (func $f6 call $f7 call $f7)
+ (func $f7 call $f8 call $f8)
+ (func $f8 call $f9 call $f9)
+ (func $f9 call $f10 call $f10)
+ (func $f10 call $f11 call $f11)
+ (func $f11 call $f12 call $f12)
+ (func $f12 call $f13 call $f13)
+ (func $f13 call $f14 call $f14)
+ (func $f14 call $f15 call $f15)
+ (func $f15 call $f16 call $f16)
+ (func $f16))
+ "all fuel consumed")
diff --git a/tests/misc_testsuite/stack-switching/fuel.wast b/tests/misc_testsuite/stack-switching/fuel.wast
new file mode 100644
index 0000000000..ca5c350894
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/fuel.wast
@@ -0,0 +1,45 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+;;! consume_fuel = true
+
+;; Fuel used inside a continuation is accounted for.
+(module $Fuel
+ (import "wasmtime" "set-fuel" (func $set (param i64)))
+ (import "wasmtime" "get-fuel" (func $get (result i64)))
+ (export "set-fuel" (func $set))
+ (export "get-fuel" (func $get)))
+
+(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))
+
+;; Both the 50 iterations in `$burn` and the 50 in `run` use fuel.
+(invoke $Fuel "set-fuel" (i64.const 10000))
+(assert_return (invoke "run"))
+(assert_return (invoke $Fuel "get-fuel") (i64.const 9491))
+
+;; Enough fuel for `run`'s loop alone, but not for `$burn` as well.
+(invoke $Fuel "set-fuel" (i64.const 300))
+(assert_trap (invoke "run") "all fuel consumed")
diff --git a/tests/misc_testsuite/winch/fuel_stack_alignment.wat b/tests/misc_testsuite/winch/fuel_stack_alignment.wast
similarity index 98%
rename from tests/misc_testsuite/winch/fuel_stack_alignment.wat
rename to tests/misc_testsuite/winch/fuel_stack_alignment.wast
index 75d1765e8c..72bec44922 100644
--- a/tests/misc_testsuite/winch/fuel_stack_alignment.wat
+++ b/tests/misc_testsuite/winch/fuel_stack_alignment.wast
@@ -1,3 +1,7 @@
+;;! consume_fuel = true
+
+;; Regression test for #9554: a fuel check must keep the stack aligned when it
+;; calls the out-of-fuel builtin. A fuzz-generated module.
(module
(type (;0;) (func))
(type (;1;) (func (param f32)))
@@ -379,3 +383,5 @@
(export "" (func 0))
(export "1" (table 0))
)
+
+(assert_trap (invoke "" (f32.const 50397184)) "unreachable")