re_behaviour: the emulator check for given functions of a worktree

This commit is contained in:
KeelrLeek committed 2026-10-05 16:54:58 -05:00
1 parent 0d764799a7
commit 5211cee9c4
3 files changed
+49 -3

No files matched your search

+28
View File
@@ -614,3 +614,31 @@ def match_program(config, targets=None, decomp=None):
"ret_pops", "ours_ret_pops", "relocation_problems", "note")
return {"counts": counts, "results": [{k: r[k] for k in keep if k in r} for r in rows] if wanted else
[{k: r[k] for k in keep if k in r} for r in rows if r.get("status") != "differs"][:200]}
def behaviour_check(config, targets, decomp=None, precision=24):
"""Run the original machine code and our compiled C of each function in an emulator from the same inputs and
compare results, memory writes and calls (diffrun.py). targets: 1 to 30 addresses."""
import subprocess
import tempfile
config = config or {}
command = config.get("diffrun_command")
if not command:
raise HarborError("this project has no diffrun_command in its [reverse] table")
wanted = [t.strip() for t in re.split(r"[,\s]+", str(targets)) if t.strip()]
if not wanted or len(wanted) > 30:
raise HarborError("give 1 to 30 addresses")
command = command if isinstance(command, list) else [command]
with tempfile.TemporaryDirectory(dir=config.get("debugger_temp")) as tmp:
out = Path(tmp) / "behaviour.json"
argv = [*command, *wanted, "--json", str(out), "--precision", str(int(precision)), "--jobs", "4"]
if decomp:
argv += ["--decomp", str(decomp)]
result = subprocess.run(argv, capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=1500,
cwd=str(Path(command[-1]).parent),
creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0))
if not out.exists():
raise HarborError("the emulator check failed: " + (result.stdout + result.stderr).strip()[-800:])
rows = json.loads(out.read_text(encoding="utf-8"))
keep = ("address", "name", "verdict", "reason", "weak", "counts", "notes")
return [{k: r[k] for k in keep if k in r} for r in rows]