A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Wasmtime Cranelift backend: properly save/restore fuel around `call_ref` and exception catches. (#14407)
Chris Fallin922a92d281d5
diff --git a/crates/cranelift/src/func_environ.rs b/crates/cranelift/src/func_environ.rsindex 91eb2fcadc..fa80f61474 100644--- a/crates/cranelift/src/func_environ.rs+++ b/crates/cranelift/src/func_environ.rs@@ -605,6 +605,7 @@ impl<'module_environment> FuncEnvironment<'module_environment> {| Operator::Return| Operator::CallIndirect { .. }| Operator::Call { .. }+ | Operator::CallRef { .. }| Operator::ReturnCall { .. }| Operator::ReturnCallRef { .. }| Operator::ReturnCallIndirect { .. }@@ -670,7 +671,7 @@ 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::Call { .. } | Operator::CallIndirect { .. } | Operator::CallRef { .. } => {self.fuel_load_into_var(builder);}_ => {}@@ -5278,6 +5279,15 @@ impl FuncEnvironment<'_> {Ok(())}+ /// Hook invoked at the start of a catch block for a `try_table`,+ /// i.e. the block that control lands in when a `try_call` returns+ /// along its exceptional edge.+ pub fn on_catch_block_entry(&mut self, builder: &mut FunctionBuilder) {+ if self.tunables.consume_fuel {+ self.fuel_load_into_var(builder);+ }+ }+pub fn before_unconditionally_trapping_memory_access(&mut self, builder: &mut FunctionBuilder) {if self.tunables.consume_fuel {self.fuel_increment_var(builder);diff --git a/crates/cranelift/src/translate/code_translator.rs b/crates/cranelift/src/translate/code_translator.rsindex 4ed39e1cab..52c067504a 100644--- a/crates/cranelift/src/translate/code_translator.rs+++ b/crates/cranelift/src/translate/code_translator.rs@@ -4631,6 +4631,7 @@ fn create_catch_block(let block = builder.create_block();let exn_ref = builder.append_block_param(block, exn_payload_ty);builder.switch_to_block(block);+ environ.on_catch_block_entry(builder);debug_assert!(exn_ref_ty.bits() <= exn_payload_ty.bits());let exn_ref = if exn_ref_ty.bits() < exn_payload_ty.bits() {builder.ins().ireduce(exn_ref_ty, exn_ref)diff --git a/tests/all/fuel.rs b/tests/all/fuel.rsindex 5b176aecca..5c7cc91c51 100644--- a/tests/all/fuel.rs+++ b/tests/all/fuel.rs@@ -24,7 +24,7 @@ impl<'a> Parse<'a> for FuelWast<'a> {}}-#[wasmtime_test(wasm_features(bulk_memory, reference_types, gc))]+#[wasmtime_test(wasm_features(bulk_memory, reference_types, gc, function_references, exceptions))]#[cfg_attr(miri, ignore)]fn run(config: &mut Config) -> Result<()> {config.consume_fuel(true);@@ -1173,3 +1173,123 @@ 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(())+}diff --git a/tests/all/fuel.wast b/tests/all/fuel.wastindex 1a3f2af3e3..9cc8da2f25 100644--- a/tests/all/fuel.wast+++ b/tests/all/fuel.wast@@ -182,6 +182,195 @@(table funcref (elem $other))(start $f)))+(assert_fuel 6+ (module+ (type $t (func))+ (func $f+ ref.func $other+ call_ref $t+ )+ (func $other)+ (elem declare func $other)+ (start $f)))++(assert_fuel 9+ (module+ (type $t (func))+ (func $f+ ref.func $other+ call_ref $t+ )+ (func $other+ i32.const 0+ i32.const 0+ i32.const 0+ drop+ drop+ drop+ )+ (elem declare func $other)+ (start $f)))++(assert_fuel 6+ (module+ (tag $e)+ (func $f+ block $catch+ try_table (catch $e $catch)+ call $other+ end+ end+ )+ (func $other)+ (start $f)))++(assert_fuel 9+ (module+ (tag $e)+ (func $f+ block $catch+ try_table (catch $e $catch)+ call $other+ end+ end+ )+ (func $other+ i32.const 0+ i32.const 0+ i32.const 0+ drop+ drop+ drop+ )+ (start $f)))++(assert_fuel 7+ (module+ (tag $e)+ (func $f+ block $catch+ try_table (catch $e $catch)+ call $other+ end+ end+ )+ (func $other+ throw $e+ )+ (start $f)))++(assert_fuel 10+ (module+ (tag $e)+ (func $f+ block $catch+ try_table (catch $e $catch)+ call $other+ end+ end+ )+ (func $other+ i32.const 0+ i32.const 0+ i32.const 0+ drop+ drop+ drop+ throw $e+ )+ (start $f)))++(assert_fuel 10+ (module+ (tag $e)+ (func $f+ block $catch+ try_table (catch_all $catch)+ call $other+ end+ end+ )+ (func $other+ i32.const 0+ i32.const 0+ i32.const 0+ drop+ drop+ drop+ throw $e+ )+ (start $f)))++(assert_fuel 10+ (module+ (tag $e)+ (func $f+ block $catch (result exnref)+ try_table (catch_ref $e $catch)+ call $other+ end+ unreachable+ end+ drop+ )+ (func $other+ i32.const 0+ i32.const 0+ i32.const 0+ drop+ drop+ drop+ throw $e+ )+ (start $f)))++(assert_fuel 12+ (module+ (tag $e (param i32))+ (func $f+ block $catch (result i32)+ try_table (result i32) (catch $e $catch)+ call $other+ end+ end+ i32.const 1+ i32.add+ drop+ )+ (func $other (result i32)+ i32.const 0+ i32.const 0+ i32.const 0+ drop+ drop+ throw $e+ )+ (start $f)))++(assert_fuel 11+ (module+ (type $t (func))+ (tag $e)+ (func $f+ block $catch+ try_table (catch $e $catch)+ ref.func $other+ call_ref $t+ end+ end+ )+ (func $other+ i32.const 0+ i32.const 0+ i32.const 0+ drop+ drop+ drop+ throw $e+ )+ (elem declare func $other)+ (start $f)))+;; loops!(assert_fuel 3(module