Skip to content

Commit

Permalink
Fix fetching execution traces after wasm execution
Browse files Browse the repository at this point in the history
  • Loading branch information
sydhds committed Feb 16, 2024
1 parent de63d1b commit 75443e6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
10 changes: 6 additions & 4 deletions src/as_execution/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl ASContext {
pub(crate) fn create_vm_instance_and_init_env(
&mut self,
store: &mut Store,
) -> Result<(Instance, u64)> {
) -> Result<(Instance, FunctionEnv<ASEnv>, u64)> {
let (imports, mut fenv) = self.resolver(store);
match Instance::new(store, &self.module, &imports) {
Ok(instance) => {
Expand All @@ -56,7 +56,7 @@ impl ASContext {
self.env
.abi_enabled
.store(true, std::sync::atomic::Ordering::Relaxed);
Ok((instance, post_init_points))
Ok((instance, fenv, post_init_points))
}
Err(err) => {
// Filter the error created by the metering middleware when
Expand Down Expand Up @@ -110,12 +110,14 @@ impl ASContext {
get_remaining_points(&self.env, store)
};

// println!("self.env.trace: {:p} {:?}", &self.env, self.env.trace);

return Ok(Response {
ret: Vec::new(), // main return empty vec
remaining_gas: remaining_gas?,
init_gas_cost: 0,
#[cfg(feature = "execution-trace")]
trace: self.env.trace.clone(),
trace: Default::default(),
});
}
let ret = if let Some(offset) = value.first() {
Expand All @@ -139,7 +141,7 @@ impl ASContext {
remaining_gas: remaining_gas?,
init_gas_cost: 0,
#[cfg(feature = "execution-trace")]
trace: self.env.trace.clone(),
trace: Default::default(),
})
}
Err(error) => bail!(error),
Expand Down
10 changes: 8 additions & 2 deletions src/as_execution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ pub(crate) fn exec_as_module(
let mut store = Store::new(engine);
let mut context = ASContext::new(interface, as_module.binary_module, gas_costs);

// save the gas remaining before subexecution: used by readonly execution
// save the gas remaining before sub-execution: used by readonly execution
interface.save_gas_remaining_before_subexecution(limit);

let (instance, init_rem_points) = context.create_vm_instance_and_init_env(&mut store)?;
let (instance, _fenv, init_rem_points) = context.create_vm_instance_and_init_env(&mut store)?;
let init_cost = as_module.initial_limit.saturating_sub(init_rem_points);

if cfg!(not(feature = "gas_calibration")) {
Expand All @@ -229,6 +229,12 @@ pub(crate) fn exec_as_module(
None
};
response.init_gas_cost = init_cost;

#[cfg(feature = "execution-trace")]
{
response.trace = _fenv.as_ref(&store).trace.clone();
}

Ok((response, gc_result))
}
Err(err) => {
Expand Down
28 changes: 27 additions & 1 deletion src/tests/tests_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use crate::{
types::{GasCosts, Interface},
RuntimeModule,
};
#[cfg(feature = "execution-trace")]
use crate::{AbiTrace, AbiTraceType};
use rand::Rng;
use serial_test::serial;
use wasmer::Store;
Expand Down Expand Up @@ -132,6 +134,30 @@ fn test_run_main() {
run_main(&*interface, runtime_module, 100_000, gas_costs).unwrap();
}

#[cfg(feature = "execution-trace")]
#[test]
#[serial]
/// Test basic main-only SC execution
fn test_run_main_get_execution_traces() {
let gas_costs = GasCosts::default();
let interface: Box<dyn Interface> = Box::new(TestInterface);
let module = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/wasm/basic_main.wasm"));

let runtime_module = RuntimeModule::new(module, gas_costs.clone(), Compiler::SP).unwrap();
let resp = run_main(&*interface, runtime_module, 100_000, gas_costs).unwrap();

assert_eq!(resp.trace.is_empty(), false);
assert_eq!(
resp.trace,
vec![AbiTrace {
name: "assembly_script_generate_event".to_string(),
params: vec![AbiTraceType::String("hello world!".to_string())],
return_value: AbiTraceType::None,
sub_calls: None
}]
)
}

#[test]
#[serial]
fn test_run_register_wasmv1() {
Expand Down Expand Up @@ -809,7 +835,7 @@ fn test_class_id() {
let module = ASModule::new(bytecode, 100_000, GasCosts::default(), Compiler::SP).unwrap();
let mut store = Store::new(module._engine);
let mut context = ASContext::new(&*interface, module.binary_module, GasCosts::default());
let (instance, _) = context.create_vm_instance_and_init_env(&mut store).unwrap();
let (instance, _function_env, _) = context.create_vm_instance_and_init_env(&mut store).unwrap();

// setup test specific context
let (_, fenv) = context.resolver(&mut store);
Expand Down
4 changes: 2 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{
use crate::execution::RuntimeModule;

#[cfg(feature = "execution-trace")]
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum AbiTraceType {
None,
Bool(bool),
Expand All @@ -28,7 +28,7 @@ pub enum AbiTraceType {
}

#[cfg(feature = "execution-trace")]
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct AbiTrace {
pub name: String,
pub params: Vec<AbiTraceType>,
Expand Down

0 comments on commit 75443e6

Please sign in to comment.