From 509af4ce4948838524fd27e68456c3bd9388b3c6 Mon Sep 17 00:00:00 2001 From: Leo-Besancon Date: Tue, 16 Jul 2024 08:22:17 +0200 Subject: [PATCH 1/4] add_recursion_counter --- src/as_execution/common.rs | 9 +++++++++ src/types.rs | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/src/as_execution/common.rs b/src/as_execution/common.rs index 945faff8..fb94f869 100644 --- a/src/as_execution/common.rs +++ b/src/as_execution/common.rs @@ -38,6 +38,8 @@ pub(crate) fn call_module( ))) })?; + interface.increment_recursion_counter()?; + let resp = crate::execution::run_function( &*interface, module, @@ -49,6 +51,8 @@ pub(crate) fn call_module( if cfg!(not(feature = "gas_calibration")) { set_remaining_points(&env, ctx, resp.remaining_gas)?; } + + interface.decrement_recursion_counter()?; env.get_interface().finish_call()?; Ok(resp) } @@ -71,6 +75,8 @@ pub(crate) fn local_call( } else { interface.get_module(bytecode, remaining_gas)? }; + + interface.increment_recursion_counter()?; let resp = crate::execution::run_function( &*interface, @@ -80,6 +86,9 @@ pub(crate) fn local_call( remaining_gas, gas_costs, )?; + + interface.decrement_recursion_counter()?; + if cfg!(not(feature = "gas_calibration")) { set_remaining_points(&env, ctx, resp.remaining_gas)?; } diff --git a/src/types.rs b/src/types.rs index 961e4de7..d5f90cf1 100644 --- a/src/types.rs +++ b/src/types.rs @@ -294,6 +294,11 @@ impl Default for GasCosts { #[allow(unused_variables)] pub trait Interface: Send + Sync + InterfaceClone { + + fn increment_recursion_counter(&self) -> Result<()>; + + fn decrement_recursion_counter(&self) -> Result<()>; + /// Prepare the execution of a module at the given address and transfer a /// given amount of coins fn init_call(&self, address: &str, raw_coins: u64) -> Result>; From 3cb1caf6b99c5e9a2eb594b63c76236e9cd926b7 Mon Sep 17 00:00:00 2001 From: Leo-Besancon Date: Tue, 16 Jul 2024 08:40:32 +0200 Subject: [PATCH 2/4] fmt --- src/as_execution/common.rs | 8 ++++---- src/types.rs | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/as_execution/common.rs b/src/as_execution/common.rs index fb94f869..4b042c8c 100644 --- a/src/as_execution/common.rs +++ b/src/as_execution/common.rs @@ -51,7 +51,7 @@ pub(crate) fn call_module( if cfg!(not(feature = "gas_calibration")) { set_remaining_points(&env, ctx, resp.remaining_gas)?; } - + interface.decrement_recursion_counter()?; env.get_interface().finish_call()?; Ok(resp) @@ -75,7 +75,7 @@ pub(crate) fn local_call( } else { interface.get_module(bytecode, remaining_gas)? }; - + interface.increment_recursion_counter()?; let resp = crate::execution::run_function( @@ -86,9 +86,9 @@ pub(crate) fn local_call( remaining_gas, gas_costs, )?; - + interface.decrement_recursion_counter()?; - + if cfg!(not(feature = "gas_calibration")) { set_remaining_points(&env, ctx, resp.remaining_gas)?; } diff --git a/src/types.rs b/src/types.rs index d5f90cf1..7c667492 100644 --- a/src/types.rs +++ b/src/types.rs @@ -294,7 +294,6 @@ impl Default for GasCosts { #[allow(unused_variables)] pub trait Interface: Send + Sync + InterfaceClone { - fn increment_recursion_counter(&self) -> Result<()>; fn decrement_recursion_counter(&self) -> Result<()>; From ce918921d792cf7b1cc6ec6ca48b8a3134c2baed Mon Sep 17 00:00:00 2001 From: Leo-Besancon Date: Tue, 16 Jul 2024 08:57:41 +0200 Subject: [PATCH 3/4] Add counter to wasmv1 and test interface --- src/tests/mod.rs | 10 ++++++++++ src/wasmv1_execution/abi/abis.rs | 22 +++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/tests/mod.rs b/src/tests/mod.rs index adebbbce..2a97071f 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -18,6 +18,16 @@ impl InterfaceClone for TestInterface { } impl Interface for TestInterface { + fn increment_recursion_counter(&self) -> Result<()> { + println!("Increment recursion counter"); + Ok(()) + } + + fn decrement_recursion_counter(&self) -> Result<()> { + println!("Decrement recursion counter"); + Ok(()) + } + fn init_call(&self, address: &str, raw_coins: u64) -> Result> { println!("Init call to {}, with {} coins", address, raw_coins); Ok(vec![]) diff --git a/src/wasmv1_execution/abi/abis.rs b/src/wasmv1_execution/abi/abis.rs index 82cc6df8..32ae483f 100644 --- a/src/wasmv1_execution/abi/abis.rs +++ b/src/wasmv1_execution/abi/abis.rs @@ -152,6 +152,9 @@ fn abi_call(store_env: FunctionEnvMut, arg_offset: i32) -> Result, arg_offset: i32) -> Result, arg_offset: i32) -> Result< let remaining_gas = handler.get_remaining_gas(); let interface = handler.exec_env.get_interface(); let module = helper_get_module(interface, bytecode.clone(), remaining_gas)?; - + interface.increment_recursion_counter().map_err(|e| { + WasmV1Error::RuntimeError(format!("Could not increment recursion counter: {}", e)) + })?; let response = crate::execution::run_function( interface, module, @@ -211,6 +219,9 @@ fn abi_local_call(store_env: FunctionEnvMut, arg_offset: i32) -> Result< handler.get_gas_costs().clone(), ) .map_err(|err| WasmV1Error::RuntimeError(format!("Could not run function: {}", err)))?; + interface.decrement_recursion_counter().map_err(|e| { + WasmV1Error::RuntimeError(format!("Could not decrement recursion counter: {}", e)) + })?; handler.set_remaining_gas(response.remaining_gas); #[cfg(feature = "execution-trace")] @@ -1022,6 +1033,9 @@ fn abi_local_execution( let module = helper_get_tmp_module(handler, req.bytecode.clone(), remaining_gas)?; let interface = handler.exec_env.get_interface(); + interface.increment_recursion_counter().map_err(|e| { + WasmV1Error::RuntimeError(format!("Could not increment recursion counter: {}", e)) + })?; match crate::execution::run_function( interface, module, @@ -1031,6 +1045,12 @@ fn abi_local_execution( handler.get_gas_costs().clone(), ) { Ok(response) => { + interface.decrement_recursion_counter().map_err(|e| { + WasmV1Error::RuntimeError(format!( + "Could not decrement recursion counter: {}", + e + )) + })?; handler.set_remaining_gas(response.remaining_gas); #[cfg(feature = "execution-trace")] From 8ad1dc93e43662e2338fe977251d50b497583f5f Mon Sep 17 00:00:00 2001 From: Leo-Besancon Date: Tue, 15 Oct 2024 14:35:58 +0200 Subject: [PATCH 4/4] fmt --- src/types.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types.rs b/src/types.rs index 41b49191..42da156a 100644 --- a/src/types.rs +++ b/src/types.rs @@ -455,7 +455,7 @@ pub trait Interface: Send + Sync + InterfaceClone { fn increment_recursion_counter(&self) -> Result<()>; fn decrement_recursion_counter(&self) -> Result<()>; - + fn get_interface_version(&self) -> Result; /// Prepare the execution of a module at the given address and transfer a