Skip to content

Commit dc0b55b

Browse files
committed
Add wasmtime fuel metering for Rust WASM modules
1 parent da1f094 commit dc0b55b

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

runtime/wasm/src/mapping.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,12 @@ impl ValidModule {
338338
config.max_wasm_stack(ENV_VARS.mappings.max_stack_size);
339339
config.async_support(true);
340340

341+
// Enable wasmtime fuel metering for Rust modules.
342+
// AS modules use parity_wasm-injected gas() calls instead.
343+
if is_rust_module {
344+
config.consume_fuel(true);
345+
}
346+
341347
let engine = &wasmtime::Engine::new(&config)?;
342348
let module = wasmtime::Module::from_binary(engine, &raw_module)?;
343349

runtime/wasm/src/module/instance.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,17 @@ impl WasmInstance {
246246
.as_secs()
247247
))));
248248
}
249+
Err(trap)
250+
if trap
251+
.chain()
252+
.any(|e| e.downcast_ref::<Trap>() == Some(&Trap::OutOfFuel)) =>
253+
{
254+
// Fuel exhaustion is deterministic — same code always burns the same fuel.
255+
Some(anyhow::anyhow!(
256+
"Handler '{}' exceeded fuel limit (possible infinite loop)",
257+
handler
258+
))
259+
}
249260
Err(trap) => {
250261
let trap_is_deterministic = is_trap_deterministic(&trap)
251262
|| self.instance_ctx().as_ref().deterministic_host_trap;
@@ -851,6 +862,13 @@ impl WasmInstance {
851862
// See also: runtime-timeouts
852863
store.set_epoch_deadline(2);
853864

865+
// For Rust modules, set initial fuel for wasmtime's built-in fuel metering.
866+
// This replaces the parity_wasm gas injection which can't parse modern WASM features.
867+
// 10 billion fuel units is generous for any reasonable handler but catches infinite loops.
868+
if valid_module.language == crate::rust_abi::MappingLanguage::Rust {
869+
store.set_fuel(10_000_000_000)?;
870+
}
871+
854872
let instance = valid_module
855873
.instance_pre
856874
.instantiate_async(store.as_context_mut())

0 commit comments

Comments
 (0)