Skip to content

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

Fold buffered fuel charges when a function exits (#14391)

Xia Chao9a5fb82e53db
diff --git a/crates/cranelift/src/func_environ.rs b/crates/cranelift/src/func_environ.rs
index 421211aa0b..b8f0f6a943 100644
--- a/crates/cranelift/src/func_environ.rs
+++ b/crates/cranelift/src/func_environ.rs
@@ -575,6 +575,18 @@ impl<'module_environment> FuncEnvironment<'module_environment> {
self.fuel_save_from_var(builder);
}
+ /// Folds any fuel buffered in `self.fuel_consumed` into `self.fuel_var`.
+ ///
+ /// Functions translated from wasm do this as part of their trailing `end`
+ /// operator. A synthesized function has no `end` operator, so it has to call
+ /// this itself before returning; otherwise the charges it buffered would be
+ /// dropped when `fuel_function_exit` saves `self.fuel_var`.
+ pub fn fuel_flush_consumed(&mut self, builder: &mut FunctionBuilder<'_>) {
+ if self.tunables.consume_fuel {
+ self.fuel_increment_var(builder);
+ }
+ }
+
fn fuel_before_op(
&mut self,
op: &Operator<'_>,
diff --git a/crates/cranelift/src/translate/func_translator.rs b/crates/cranelift/src/translate/func_translator.rs
index f220d1195d..d1b0857716 100644
--- a/crates/cranelift/src/translate/func_translator.rs
+++ b/crates/cranelift/src/translate/func_translator.rs
@@ -138,6 +138,11 @@ impl FuncTranslator {
builder.ensure_inserted_block();
environ.before_translate_function(&mut builder)?;
environ.translate_module_startup(&mut builder)?;
+ // This function is synthesized rather than translated, so it has no
+ // trailing `end` operator to fold buffered fuel charges into
+ // `self.fuel_var`. Flush them here so they are not dropped when
+ // `fuel_function_exit` saves the fuel var below.
+ environ.fuel_flush_consumed(&mut builder);
environ.after_translate_function(&mut builder)?;
builder.ins().return_(&[]);
builder.finalize(environ.target_config());
diff --git a/tests/all/fuel.rs b/tests/all/fuel.rs
index 5c7cc91c51..983c395206 100644
--- a/tests/all/fuel.rs
+++ b/tests/all/fuel.rs
@@ -1293,3 +1293,37 @@ fn try_call_exceptional_return_respects_fuel(config: &mut Config) -> Result<()>
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(())
+}