From 6408aa789a9517452d0eb2f46dcedb3cf6fa0b24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Papierski?= Date: Wed, 18 Oct 2023 16:19:04 +0200 Subject: [PATCH] Do not re-export casper-wasm anymore. --- src/lib.rs | 1 - src/stack_height.rs | 65 +++++++++++++++++++++------------------ src/stack_height/thunk.rs | 8 ++--- 3 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c3cad8a..6e155f1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,7 +15,6 @@ mod symbols; pub mod stack_height; -pub use casper_wasm; pub use ext::{ externalize, externalize_mem, shrink_unknown_stack, underscore_funcs, ununderscore_funcs, }; diff --git a/src/stack_height.rs b/src/stack_height.rs index d5ece25..76f7ca3 100644 --- a/src/stack_height.rs +++ b/src/stack_height.rs @@ -56,34 +56,39 @@ use casper_wasm::{ elements::{self, Instruction, Instructions, Type}, }; -/// Macro to generate preamble and postamble. -macro_rules! instrument_call { - ($callee_idx: expr, $callee_stack_cost: expr, $stack_height_global_idx: expr, $stack_limit: expr) => {{ - use $crate::casper_wasm::elements::Instruction::*; - [ - // stack_height += stack_cost(F) - GetGlobal($stack_height_global_idx), - I32Const($callee_stack_cost), - I32Add, - SetGlobal($stack_height_global_idx), - // if stack_counter > LIMIT: unreachable - GetGlobal($stack_height_global_idx), - I32Const($stack_limit as i32), - I32GtU, - If(elements::BlockType::NoResult), - Unreachable, - End, - // Original call - Call($callee_idx), - // stack_height -= stack_cost(F) - GetGlobal($stack_height_global_idx), - I32Const($callee_stack_cost), - I32Sub, - SetGlobal($stack_height_global_idx), - ] - }}; +/// Const function to generate preamble and postamble. +const fn instrument_call( + callee_idx: u32, + callee_stack_cost: u32, + stack_height_global_idx: u32, + stack_limit: u32, +) -> [Instruction; INSTRUMENT_CALL_LENGTH] { + use casper_wasm::elements::Instruction::*; + [ + // stack_height += stack_cost(F) + GetGlobal(stack_height_global_idx), + I32Const(callee_stack_cost as i32), + I32Add, + SetGlobal(stack_height_global_idx), + // if stack_counter > LIMIT: unreachable + GetGlobal(stack_height_global_idx), + I32Const(stack_limit as i32), + I32GtU, + If(elements::BlockType::NoResult), + Unreachable, + End, + // Original call + Call(callee_idx), + // stack_height -= stack_cost(F) + GetGlobal(stack_height_global_idx), + I32Const(callee_stack_cost as i32), + I32Sub, + SetGlobal(stack_height_global_idx), + ] } +const INSTRUMENT_CALL_LENGTH: usize = 15; + mod max_height; mod thunk; @@ -282,7 +287,7 @@ fn instrument_function(ctx: &mut Context, func: &mut Instructions) -> Result<(), .collect(); // The `instrumented_call!` contains the call itself. This is why we need to subtract one. - let len = func.elements().len() + calls.len() * (instrument_call!(0, 0, 0, 0).len() - 1); + let len = func.elements().len() + calls.len() * (INSTRUMENT_CALL_LENGTH - 1); let original_instrs = mem::replace(func.elements_mut(), Vec::with_capacity(len)); let new_instrs = func.elements_mut(); @@ -291,11 +296,11 @@ fn instrument_function(ctx: &mut Context, func: &mut Instructions) -> Result<(), // whether there is some call instruction at this position that needs to be instrumented let did_instrument = if let Some(call) = calls.peek() { if call.offset == original_pos { - let new_seq = instrument_call!( + let new_seq = instrument_call( call.callee, - call.cost as i32, + call.cost, ctx.stack_height_global_idx(), - ctx.stack_limit() + ctx.stack_limit(), ); new_instrs.extend(new_seq); true diff --git a/src/stack_height/thunk.rs b/src/stack_height/thunk.rs index f86dd6b..8927009 100644 --- a/src/stack_height/thunk.rs +++ b/src/stack_height/thunk.rs @@ -6,7 +6,7 @@ use casper_wasm::{ elements::{self, FunctionType, Internal}, }; -use super::{resolve_func_type, Context, Error}; +use super::{instrument_call, resolve_func_type, Context, Error}; struct Thunk { signature: FunctionType, @@ -77,11 +77,11 @@ pub(crate) fn generate_thunks( let mut mbuilder = builder::from_module(module); for (func_idx, thunk) in replacement_map.iter_mut() { - let instrumented_call = instrument_call!( + let instrumented_call = instrument_call( *func_idx, - thunk.callee_stack_cost as i32, + thunk.callee_stack_cost, ctx.stack_height_global_idx(), - ctx.stack_limit() + ctx.stack_limit(), ); // Thunk body consist of: // - argument pushing