Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_recursion_counter #333

Open
wants to merge 3 commits into
base: next_breaking_update
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/as_execution/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub(crate) fn call_module(
)))
})?;

interface.increment_recursion_counter()?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't that double-count with the increment that happens also in init_call ?


let resp = crate::execution::run_function(
&*interface,
module,
Expand All @@ -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()?;
Copy link
Member

@damip damip Aug 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't this double-decrement with the decrement in finish_call ?

env.get_interface().finish_call()?;
Ok(resp)
}
Expand All @@ -72,6 +76,8 @@ pub(crate) fn local_call(
interface.get_module(bytecode, remaining_gas)?
};

interface.increment_recursion_counter()?;

let resp = crate::execution::run_function(
&*interface,
module,
Expand All @@ -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)?;
}
Expand Down
10 changes: 10 additions & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<u8>> {
println!("Init call to {}, with {} coins", address, raw_coins);
Ok(vec![])
Expand Down
4 changes: 4 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ 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<Vec<u8>>;
Expand Down
22 changes: 21 additions & 1 deletion src/wasmv1_execution/abi/abis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ fn abi_call(store_env: FunctionEnvMut<ABIEnv>, arg_offset: i32) -> Result<i32, W
let remaining_gas = handler.get_remaining_gas();
let interface = handler.exec_env.get_interface();
let module = helper_get_module(interface, bytecode, remaining_gas)?;
interface.increment_recursion_counter().map_err(|e| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double-counts with the init_call_wasmv1

WasmV1Error::RuntimeError(format!("Could not increment recursion counter: {}", e))
})?;
let response = crate::execution::run_function(
interface,
module,
Expand All @@ -161,6 +164,9 @@ fn abi_call(store_env: FunctionEnvMut<ABIEnv>, arg_offset: i32) -> Result<i32, W
handler.get_gas_costs().clone(),
)
.map_err(|err| WasmV1Error::RuntimeError(format!("Could not run function: {}", err)))?;
interface.decrement_recursion_counter().map_err(|e| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double-decrements with the finish_call below

WasmV1Error::RuntimeError(format!("Could not decrement recursion counter: {}", e))
})?;
handler.set_remaining_gas(response.remaining_gas);
let interface = handler.exec_env.get_interface();
interface.finish_call().map_err(|err| {
Expand Down Expand Up @@ -201,7 +207,9 @@ fn abi_local_call(store_env: FunctionEnvMut<ABIEnv>, 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,
Expand All @@ -211,6 +219,9 @@ fn abi_local_call(store_env: FunctionEnvMut<ABIEnv>, 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")]
Expand Down Expand Up @@ -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,
Expand All @@ -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")]
Expand Down
Loading