Skip to content

Commit

Permalink
Merge pull request #31 from multiversx/control_flow_helper_fn
Browse files Browse the repository at this point in the history
Add check for control flow operator
  • Loading branch information
ovstinga authored Oct 9, 2023
2 parents 852c923 + a98ce88 commit 46c2fba
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
9 changes: 4 additions & 5 deletions vm-executor-wasmer/src/wasmer_breakpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use wasmer::{
};
use wasmer_types::{GlobalIndex, ModuleInfo};

use crate::wasmer_helpers::{create_global_index, MiddlewareWithProtectedGlobals};
use crate::wasmer_helpers::{
create_global_index, is_control_flow_operator, MiddlewareWithProtectedGlobals,
};

const BREAKPOINT_VALUE: &str = "breakpoint_value";

Expand Down Expand Up @@ -132,10 +134,7 @@ impl FunctionMiddleware for FunctionBreakpoints {
operator: Operator<'b>,
state: &mut MiddlewareReaderState<'b>,
) -> Result<(), MiddlewareError> {
let must_add_breakpoint = matches!(
operator,
Operator::Call { .. } | Operator::CallIndirect { .. }
);
let must_add_breakpoint = is_control_flow_operator(&operator);

state.push_operator(operator);

Expand Down
20 changes: 19 additions & 1 deletion vm-executor-wasmer/src/wasmer_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use wasmer::{ExportIndex, GlobalInit, GlobalType, Mutability, Type};
use wasmer::{ExportIndex, GlobalInit, GlobalType, Mutability, Type, wasmparser::Operator};
use wasmer_types::{GlobalIndex, ModuleInfo};

pub trait MiddlewareWithProtectedGlobals {
Expand Down Expand Up @@ -30,3 +30,21 @@ pub(crate) fn create_global_index(

global_index
}

pub(crate) fn is_control_flow_operator(operator: &Operator) -> bool {
matches!(
operator,
Operator::Loop { .. }
| Operator::Block { .. }
| Operator::End
| Operator::If { .. }
| Operator::Else
| Operator::Unreachable
| Operator::Br { .. }
| Operator::BrTable { .. }
| Operator::BrIf { .. }
| Operator::Call { .. }
| Operator::CallIndirect { .. }
| Operator::Return
)
}
20 changes: 4 additions & 16 deletions vm-executor-wasmer/src/wasmer_metering.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::wasmer_breakpoints::{Breakpoints, BREAKPOINT_VALUE_OUT_OF_GAS};
use crate::wasmer_helpers::{create_global_index, MiddlewareWithProtectedGlobals};
use crate::wasmer_helpers::{
create_global_index, is_control_flow_operator, MiddlewareWithProtectedGlobals,
};
use crate::{get_local_cost, get_opcode_cost};
use loupe::{MemoryUsage, MemoryUsageTracker};
use multiversx_chain_vm_executor::OpcodeCost;
Expand Down Expand Up @@ -175,21 +177,7 @@ impl FunctionMiddleware for FunctionMetering {
}
}

if matches!(
operator,
Operator::Loop { .. }
| Operator::Block { .. }
| Operator::End
| Operator::If { .. }
| Operator::Else
| Operator::Unreachable
| Operator::Br { .. }
| Operator::BrTable { .. }
| Operator::BrIf { .. }
| Operator::Call { .. }
| Operator::CallIndirect { .. }
| Operator::Return
) {
if is_control_flow_operator(&operator) {
self.inject_points_used_increment(state);
self.inject_out_of_gas_check(state);

Expand Down

0 comments on commit 46c2fba

Please sign in to comment.