diff --git a/.envrc b/.envrc new file mode 100644 index 0000000000..5166a810c5 --- /dev/null +++ b/.envrc @@ -0,0 +1,16 @@ +# shellcheck shell=bash +if ! has nix_direnv_version || ! nix_direnv_version 3.0.4; then + source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.4/direnvrc" "sha256-DzlYZ33mWF/Gs8DDeyjr8mnVmQGx7ASYqA5WlxwvBG4=" +fi + +dotenv_if_exists + +# watch_file "$(find ./nix -name "*.nix" -type f)" + +OS_AND_SYSTEM=$(uname -a) + +if [[ "${NO_NIX:-}" == "1" || $OS_AND_SYSTEM == "Darwin"* ]]; then + source non-nix-build/env.sh +else + use flake +fi diff --git a/.gitignore b/.gitignore index 08f2b95e18..877f83c211 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,6 @@ spec/target **/fuzz/artifacts/ **/fuzz/crash-inputs/ **/fuzz/Cargo.lock + +*.wasm +.direnv/ diff --git a/Cargo.lock b/Cargo.lock index 9cbd7de74f..87a4fe31bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1712,10 +1712,6 @@ dependencies = [ "wasm-encoder 0.227.1", ] -[[package]] -name = "wasm-test" -version = "0.44.0" - [[package]] name = "wasmi" version = "0.31.2" diff --git a/Cargo.toml b/Cargo.toml index d5a4319355..f40f5442e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,8 @@ members = [ "crates/ir", "crates/fuzz", "crates/wast", - "fuzz", "wasm-test", "crates/tracer", + "fuzz", "crates/tracer", + # "wasm-test", ] exclude = [] resolver = "2" diff --git a/crates/tracer/src/lib.rs b/crates/tracer/src/lib.rs index ca02e3cbb8..02f8a37ca5 100644 --- a/crates/tracer/src/lib.rs +++ b/crates/tracer/src/lib.rs @@ -1,10 +1,6 @@ use std::path::{Path, PathBuf}; use std::println; -pub fn add(left: u64, right: u64) -> u64 { - left + right -} - // TODO #[derive(Debug, Clone)] struct DebugInfo { @@ -25,7 +21,10 @@ impl DebugInfo { #[derive(Debug, Clone)] pub struct WasmTracer { + pub tracing: bool, debug_info: DebugInfo, + info: Vec, + index: usize, // TODO: tracer: runtime_tracing.Tracer, // etc } @@ -33,14 +32,21 @@ pub struct WasmTracer { // just to make it build so we can branch-out impl WasmTracer { - pub fn new(wasm_exe_path: &Path) -> Self { + pub fn new(tracing: bool, wasm_exe_path: &Path) -> Self { WasmTracer { + tracing, debug_info: DebugInfo::new(wasm_exe_path), + info: vec![], + index: 0, } } + pub fn no_tracing() -> Self { + Self::new(false, &Path::new("")) + } + pub fn load_local_variables(&mut self, address: usize) { // -> ??? - println!("load_local_variables {address}"); + // println!("load_local_variables {address}"); // e.g. here we might call something like // some kind of check if we already have the info for the current context // let cached = TODO; @@ -49,7 +55,10 @@ impl WasmTracer { if !cached { // TODO etc // load debuginfo etc + self.info.push(format!("{}", self.index)); + self.index += 1; } + // println!("{:?}", self.info); } } @@ -68,7 +77,5 @@ mod tests { #[test] fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); } } diff --git a/crates/wasmi/src/engine/executor/instrs.rs b/crates/wasmi/src/engine/executor/instrs.rs index e7a448f962..145f0ee799 100644 --- a/crates/wasmi/src/engine/executor/instrs.rs +++ b/crates/wasmi/src/engine/executor/instrs.rs @@ -129,24 +129,29 @@ impl<'engine> Executor<'engine> { #[inline(always)] fn execute(mut self, store: &mut Store) -> Result<(), Error> { use Instruction as Instr; + let tracing = std::env::var("CODETRACER_WASMI_TRACING").unwrap_or(std::string::String::from("")) == "1"; + let env_wasm_path = std::env::var("CODETRACER_WASM_PATH").unwrap_or(std::string::String::from("")); + let wasm_path = std::path::Path::new(&env_wasm_path); + let mut tracer = WasmTracer::new(tracing, &wasm_path); loop { - // TODO: change that, just startting from somewehre - // Args: path : Path, instruction address(?) , step? - // TODO: good way to take address - //let address = usize::from(self.ip.get()); - let address = 0x000000010; // should be in the main subprogram >= low_pc - //std::println!("address of {:?}: {:?}", *self.ip.get(), address); - // TODO: make this be a field with correct lifetime - let mut tracer = WasmTracer::new(std::path::Path::new("/home/pesho/code/codetracer-wasmi-recorder/wasm_test.wasm")); // ": for now hardcoded TODO pass"); - tracer.load_local_variables(address); + if tracer.tracing { + // TODO: change that, just startting from somewehre + // Args: path : Path, instruction address(?) , step? + // TODO: good way to take address + //let address = usize::from(self.ip.get()); + let address = 0x000000010; // should be in the main subprogram >= low_pc + std::println!("address of {:?}: TODO", *self.ip.get()); + // TODO: make this be a field with correct lifetime + tracer.load_local_variables(address); - // load debuginfo from gimli <- for this ip; - // -> find out current scope and vars in it: - // some kind of mapping between them and locations - // -> load them with some general(for us) mechanism from wasmi's memory(?) - // e.g. load_expression(expr, location, ..) -> ValueRecord - // (to be iterated on, just an idea) - match *self.ip.get() { + // load debuginfo from gimli <- for this ip; + // -> find out current scope and vars in it: + // some kind of mapping between them and locations + // -> load them with some general(for us) mechanism from wasmi's memory(?) + // e.g. load_expression(expr, location, ..) -> ValueRecord + // (to be iterated on, just an idea) + } + match *self.ip.get() { Instr::Trap { trap_code } => self.execute_trap(trap_code)?, Instr::ConsumeFuel { block_fuel } => { self.execute_consume_fuel(&mut store.inner, block_fuel)? diff --git a/justfile b/justfile new file mode 100644 index 0000000000..bcbdeace9c --- /dev/null +++ b/justfile @@ -0,0 +1,13 @@ +build-wasm-test-c: + emcc -O0 -g3 -o wasm_test.wasm wasm_test.c + +dwarfdump wasm-path: + # from llvm + llvm-dwarfdump {{wasm-path}} + +dis wasm-path: + # from binaryen + wasm-dis {{wasm-path}} + +trace wasm-path: + env CODETRACER_WASMI_TRACING=1 CODETRACER_WASM_PATH=$(realpath {{wasm-path}}) target/debug/wasmi_cli wasm_test.wasm diff --git a/shell.nix b/shell.nix index 96f0a8ac44..d145d35563 100644 --- a/shell.nix +++ b/shell.nix @@ -26,7 +26,15 @@ mkShell { emscripten binaryen llvm + just rust-analyzer + + figlet ]; + shellHook = '' + export EM_CACHE=/tmp/emcc/ + + figlet "welcome to wasmi recorder" + ''; } diff --git a/wasm_test.c b/wasm_test.c new file mode 100644 index 0000000000..c4c0ce5401 --- /dev/null +++ b/wasm_test.c @@ -0,0 +1,4 @@ +int main() { + int x = 1; + int y = 2 + x; +}