Skip to content
Merged
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
50 changes: 50 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,3 +452,53 @@ fn test_smart_contract_audit() {
let wasm_ctx = WASMCtx::new(wasm_args);
test_wasm_snark_with(wasm_ctx, step_size).unwrap();
}

#[test]
fn test_memory_truncation_with_host_call() {
pub struct CustomWasmCtx {
wasm_args: crate::wasm_ctx::WASMArgs,
}

impl ZKWASMCtx for CustomWasmCtx {
type T = ();

fn create_store(&self, engine: &wasmi::Engine) -> wasmi::Store<Self::T> {
wasmi::Store::new(engine, ())
}

fn create_linker(
&self,
engine: &wasmi::Engine,
_module: &wasmi::Module,
) -> Result<wasmi::Linker<Self::T>, ZKWASMError> {
let mut linker = wasmi::Linker::<Self::T>::new(engine);

linker
.func_wrap(
"host",
"call",
|_caller: wasmi::Caller<()>, i: u32| -> u32 { i },
)
.unwrap();

Ok(linker)
}

fn args(&self) -> &crate::wasm_ctx::WASMArgs {
&self.wasm_args
}
}

init_logger();
let step_size = StepSize::new(1000).set_memory_step_size(1000);

let wasm_args = WASMArgsBuilder::default()
.file_path(PathBuf::from("wasm/host_call_test.wat"))
.unwrap()
.func_args(vec![])
.invoke("test")
.build();

let wasm_ctx = CustomWasmCtx { wasm_args };
test_wasm_snark_with(wasm_ctx, step_size).unwrap();
}
4 changes: 4 additions & 0 deletions third-party/wasmi/crates/wasmi/src/tracer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ impl Tracer {

// Truncate to the largest 8-byte chunk that can accommodate the used highest address.
let new_len = highest_address.map_or(0, |addr| (addr / 7) + 1);

self.IS_mem.truncate(new_len as usize);

self.execution_trace
.retain(|vm| vm.instr != Instruction::HostCallStep || vm.Y < new_len);
}

/// Push initial heap/linear WASM memory to tracer for MCC
Expand Down
10 changes: 10 additions & 0 deletions wasm/host_call_test.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(module
(import "host" "call" (func $f (param i32) (result i32)))
(memory 1 2)
(func $test
(i32.const 42)
(call $f)
(drop)
)
(export "test" (func $test))
)
Loading