Skip to content

Commit

Permalink
[monitors] Implement Wasm-R3 recording (#176 from thelongmarch-azx/wa…
Browse files Browse the repository at this point in the history
…sm-r3)
  • Loading branch information
titzer committed Aug 8, 2024
2 parents f3457c7 + 69e0c99 commit c5b4052
Show file tree
Hide file tree
Showing 198 changed files with 1,456 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/monitors/ModuleInstrumenter.v3
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,27 @@ class ModuleInstrumenter(module: Module) {
}
// Attach callback {f} to before every memory read.
def beforeMemRead(f: (DynamicLoc, Memory, u64, u64) -> Resumption) {
beforeMemOp(f, null, null);
beforeMemOp(null, f, null, null);
}
// Attach callback {f} to before every memory write.
def beforeMemWrite(f: (DynamicLoc, Memory, u64, u64) -> Resumption) {
beforeMemOp(null, f, null);
beforeMemOp(null, null, f, null);
}

// Attach callback {f} to before every memory grow.
def beforeMemGrow(f: (DynamicLoc, Memory, u32) -> Resumption) {
beforeMemOp(null, null, f);
beforeMemOp(null, null, null, f);
}
// Attach callback {f} to before every memory read for matched functions.
def beforeMemReadMatching(matcher: (Module, FuncDecl) -> bool, f: (DynamicLoc, Memory, u64, u64) -> Resumption) {
beforeMemOp(matcher, f, null, null);
}
// Attach callback {f} to before every memory write for matched functions.
def beforeMemWriteMatching(matcher: (Module, FuncDecl) -> bool, f: (DynamicLoc, Memory, u64, u64) -> Resumption) {
beforeMemOp(matcher, null, f, null);
}
// Attach callback {f} to before every memory grow for matched functions.
def beforeMemGrowMatching(matcher: (Module, FuncDecl) -> bool, f: (DynamicLoc, Memory, u32) -> Resumption) {
beforeMemOp(matcher, null, null, f);
}

// Helpers
Expand Down Expand Up @@ -129,15 +140,18 @@ class ModuleInstrumenter(module: Module) {
}
}
private def beforeMemOp(
matcher: (Module, FuncDecl) -> bool,
readFn: (DynamicLoc, Memory, u64, u64) -> Resumption,
writeFn: (DynamicLoc, Memory, u64, u64) -> Resumption,
growFn: (DynamicLoc, Memory, u32) -> Resumption
) {
var it = BytecodeIterator.new();
var visitor = MemoryVisitor.new(module, readFn, writeFn, growFn);
visitor.bi = it;

forEachFunc(ModuleInstrumenter.instrumentMemoryAccesses(_, _, visitor));
if (matcher != null)
forEachFuncMatching(matcher, ModuleInstrumenter.instrumentMemoryAccesses(_, _, visitor));
else
forEachFunc(ModuleInstrumenter.instrumentMemoryAccesses(_, _, visitor));
}
private def instrumentMemoryAccesses(func: FuncDecl, visitor: MemoryVisitor) {
for (bi = visitor.bi.reset(func); bi.more(); bi.next()) {
Expand Down
Loading

0 comments on commit c5b4052

Please sign in to comment.