Skip to content

Commit

Permalink
Do not re-export casper-wasm anymore.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michał Papierski committed Oct 18, 2023
1 parent 664162c commit 6408aa7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 35 deletions.
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
65 changes: 35 additions & 30 deletions src/stack_height.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();

Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/stack_height/thunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6408aa7

Please sign in to comment.