A CLI tool for tracing and analyzing memory access patterns in WebAssembly binaries through instrumentation. This tool identifies three types of memory performance defects: dead store, silent store, and silent read.
- Binary Instrumentation: Automatically instruments WebAssembly binaries to trace all memory operations
- Memory Access Analysis: Identifies performance bottlenecks in memory access patterns
- Defect Detection: Detects three types of memory performance issues:
- Dead Store: A store operation that is overwritten before the stored value is ever read
- Silent Store: A store operation that writes the same value that already exists in memory
- Silent Read: A read operation that immediately follows a store of the same value (redundant load)
- Detailed Reporting: Provides multiple levels of output detail from summary to full trace
All done by cargo:
cargo build --releasemem-tracer [OPTIONS] <INPUT_WASM> [-- <WASM_ARGS>...]<INPUT_WASM>: Input.wasmfile (must end with.wasm)-o, --out <OUTPUT_WASM>: Set the output file name (default:<input>_instrumented.wasm)-r, --run: Execute the instrumented WebAssembly and analyze the trace-f, --full: Set verbosity level for output reporting:-f: Show deduplicated memory defects hashed by wasm binary address-ff: Show all memory defects records by index-fff: Show full memory access trace
-- <WASM_ARGS>...: Arguments forwarded to the wasmtime engine
-
Instrument a WebAssembly binary:
mem-tracer program.wasm # Creates: program_instrumented.wasm -
Instrument and run with analysis:
mem-tracer program.wasm --run
-
Get detailed defect reports:
mem-tracer program.wasm --run -ff
-
Full trace output (for debugging):
mem-tracer program.wasm --run -fff
-
Pass arguments to the WebAssembly program:
mem-tracer program.wasm --run -f -- arg1 arg2
The tool uses the rWABIDB library to instrument WebAssembly binaries by:
- Memory Tracer Setup: Adds auxiliary memory and global variables for trace recording
- Instruction Analysis: Scans the binary to identify all load and store instruction types
- Hook Function Generation: Creates specialized hook functions for each memory operation type
- Instrumentation Insertion: Replaces original memory instructions with calls to hook functions
- Trace Recording: Each hook records operation details (opcode, address, value, instruction location)
The recorded trace is analyzed to detect performance defects:
- Dead Store Detection: Identifies stores that are overwritten before being read
- Silent Store Detection: Finds stores that write unchanged values to memory
- Silent Read Detection: Detects redundant loads after stores of the same value
The tool provides three levels of output detail:
DedupDefects (by wasm binary addr) {
dead_store_pairs (2 entries):
(A1B2, C3D4)
(E5F6, G7H8)
silent_store_pairs (1 entries):
(I9J0, K1L2)
silent_load_pairs (0 entries):
}
DefectResults (by index) {
dead_store_pairs (2 entries):
(15, 42)
(73, 128)
silent_store_pairs (1 entries):
(24, 89)
silent_load_pairs (0 entries):
}
Complete memory access trace with all operations, addresses, and values.
The tool generates custom hook functions for each type of memory instruction found in the binary. Each hook:
- Records the instruction opcode
- Captures instruction parameters (address, value, instruction location)
- Records the memory offset of the operation
- Updates the trace pointer
- Executes the original memory instruction
Each trace record contains:
- Instruction opcode (identifying the type of memory operation)
- Memory address
- Stored/loaded value
- Instruction location (binary address)
- Memory operation offset
The instrumentation adds:
_trace_memory: Auxiliary memory for storing trace records_trace_mem_pointer: Global pointer to current trace position
rWABIDB: My WebAssembly binary instrumentation framework @[https://github.com/qilinO-o/rWABIDB]walrus: WebAssembly parser and manipulation librarywasmtime: WebAssembly runtime for executionwasmtime-wasi: WASI support for wasmtimeanyhow: Error handlingclap: Command line argument parsing
This tool can be integrated into WebAssembly development workflows to:
- Performance Optimization: Identify memory access patterns that may impact performance
- Debugging: Understand memory behavior during program execution
- Code Quality: Detect potential memory-related issues during development
- Benchmarking: Analyze memory access patterns for optimization opportunities
When analyzing a WebAssembly binary, the tool might report:
DedupDefects (by wasm binary addr) {
dead_store_pairs (3 entries):
(1234, 5678) // Store at 0x1234 overwritten before read at 0x5678
(9ABC, DEF0) // Another dead store pair
(2468, 1357) // Third dead store instance
silent_store_pairs (2 entries):
(AAAA, BBBB) // Store writes same value as existing memory
(CCCC, DDDD) // Redundant store operation
silent_load_pairs (1 entries):
(EEEE, FFFF) // Load immediately after store of same value
}
This information helps developers optimize memory access patterns and eliminate redundant operations.