Skip to content

[AUTOMATED] diffverify C1: run the machine code and the decompiled function from one state - #1829

Closed
sjkim1127 wants to merge 13 commits into
Noelo-Lab:mainfrom
sjkim1127:feat/diffverify-c1
Closed

sjkim1127 wants to merge 13 commits into
Noelo-Lab:mainfrom
sjkim1127:feat/diffverify-c1

Conversation

@sjkim1127

@sjkim1127 sjkim1127 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

[AUTOMATED]

The first slice (C1) of the diffverify proposal in #1714: a dev-only kuna-verify that runs a function's machine code and kuna's decompiled function from the same state and compares what they return. This branch sits on #1714's commits, so the proposal documents show up in the diff too.

On #794's fixture, the pre-fix behavior (returnpair single) differs from the source's prototype, and the fix agrees:

printf 'typedef unsigned long long u64;\nu64 ins16(unsigned a, unsigned short b) { return ((a + 1) << 16) | b; }\n' > ins16.c
clang --target=arm-linux-gnueabi -O2 -c ins16.c -o ins16.o
make binaries specs && (cd decompiler && cargo build --release -p kuna-verify)
decompiler/target/release/kuna-verify check ins16.o ins16 --contract ins16.c --option returnpair single
ins16  DIFFERS  (run 1 of 256)
  contract u64 ins16(unsigned a, unsigned short b)  (ins16.c)
  input    a0=0x00007fff (r0)  a1=0x0000007f (r1)
  machine  r1:r0 = 0x000000008000007f
  kuna     int4 ins16(int4 a0, uint4 a1) = 0x8000007f; as u64 0xffffffff8000007f

Without --option returnpair single the same command prints AGREES(256). Without --contract it also agrees, adding PROTOTYPE_SUSPECT because the machine sets r1 to 0 on every run. The binary alone cannot settle a prototype.

The change

  • decompiler/crates/kuna-verify is a new crate, not linked into the release binary. The machine side lifts each instruction with the program's SLEIGH translator and evaluates it with kuna's OpBehavior table. The decompiled side interprets the final Funcdata. Both start from the same random state, and argument registers are extended as the ABI documents (narrowext abi).
  • The scope is register-only leaf functions. Calls and memory beyond the frame and read-only data are SKIPPED. Floating point and unmodeled user ops are UNKNOWN.
  • kuna-verify corpus <dir> checks every function a datatest script decompiles, under that script's options.
  • In the engine, only kuna_narrowext::rules_for is new, so the documented rule can be asked for regardless of the run's option. rules is unchanged.

Results

Over tests/datatests and tests/stages (3,979 decompiled functions, 256 runs each):

  • 696 agree, 4 differ and 3 match a declared divergence; the rest are skipped or unknown.
  • 3 of the 4 differences are the narrowext off pass that kuna-narrowext.xml pins as the bug; its abi pass agrees. The fourth is a known limit with relocated immediates.
  • Checking takes 3.6 s; with every decompile, the corpus runs in 20.6 s on 14 worker processes (--jobs). --json and check --all print every verdict for a script or a whole object.

Details and triage: docs/features/diffverify/c1-results.md. make test and make test-stages are at parity.

Model: Claude Opus 5.5

🤖 Generated with Claude Code

sjkim1127 and others added 13 commits November 19, 2026 12:17
…nd kuna

[AUTOMATED] Design only. Adds docs/features/diffverify/{analysis.md,
proposal.md,record.json}: a dev-only kuna-verify crate that checks, per
function, that kuna's Band B p-code computes what the lifted p-code computes
(z3 over SMT-LIB 2), that the lift matches the machine (Unicorn/qemu-user),
and later that the printed C matches the binary. No engine or emitted-C change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…age honestly

[AUTOMATED] Reorders the tiers: concrete execution of the binary
(EmulatePcodeCache) against the finished Funcdata (an interpreter on
EmulatePcodeOp) comes first and needs no phase hooks; z3 proves only
functions that already agree. Classifies the 56 P3/P4 fixes since June
(~15 body rewrites, ~29 prototype/ABI claims, ~12 not visible) instead of
claiming all of them, spells out why equivalence is hard, and adds a
declared-divergence list keyed by option.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
[AUTOMATED] Adds how Noelo-Lab#1534's symbolic p-code emulator, the Noelo-Lab#1641/Noelo-Lab#1699
phase hooks, the Noelo-Lab#317 ghidra-sim harness, the per-PR compile-and-compare
oracles and DecBench byte_match relate to this proposal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… loop-free code

Tier S now runs every proof under a deterministic budget (z3 rlimit, unroll
and query caps; wall clock only as a safety net) and unrolls loops to a bound,
reporting EQUIVALENT(<=k) for bounded proofs. The spent budget is recorded in
the baseline, so a change that makes a function exhaust it is flagged as a
regression. angr or binsec may encode the machine side.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
One p-code-to-SMT encoder serves both sides, a sat model replays exactly in
Tier C, and no second lifter toolchain is needed. Independence from SLEIGH
stays with Tier L. Closes the open question on angr or binsec.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
D-Helix (USENIX Security 2024) is the closest prior design. The proposal now
cites it and says where it differs: no recompilation for Tier C, stores and
calls observed besides the return value, prototype read through the ABI
(the cause of all D-Helix false negatives), concrete call replay, globals
from the load image, and culprit search over fired rules only.

Also: identical scratch addresses on both sides; localization rejects
subsets that add a difference, reports when no rule set explains one, and
tries past culprits first; a Cost section; C1-C3 now measure time,
bisection cost, precision and recall (reverted fixes); Tier P expectations
from D-Helix's recompile rates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… honest bounds

- Return values: compare only the bytes the physical ABI defines for the
  claimed type; a width or signedness verdict needs a typed contract (fixture
  source, DWARF). Noelo-Lab#794 is a DIFFERS against its source's u64 contract.
- Inputs: both sides start from the same raw state; an undeclared live-in is
  PROTOTYPE_SUSPECT, not a poison-induced DIFFERS.
- Calls: stubs write reachable memory and the decompiled side replays it;
  Tier S models calls as (regs, memory) -> (regs, memory) and never encodes a
  function Tier C could not fully execute.
- The Funcdata interpreter is new work; EmulateFunction's stores are no-ops.
  Unsupported ops give UNKNOWN(unsupported-op).
- EQUIVALENT(<=k) covers inputs on which both sides finish within the bound;
  one-sided overruns are replayed, not dropped. Unreplayable sat models are
  separated from encoder bugs.
- Localization reports a 1-minimal candidate set; log2 k only for a single
  culprit.
- Declared divergences are predicates (option, arch, observable, pattern).
- Slices: C1 pure integer, C2 memory and calls, C3 localization, C4 corpus.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ts, register-only C1

- Tier S admission no longer rests on Tier C's sampled runs: every op
  reachable in both CFGs must have complete SMT semantics and every call's
  reachable memory must be bounded, else SKIPPED(unencodable).
- A call stub's reachable memory includes every writable global, not only
  pointer arguments and escaped memory.
- C1 is register-only leaf functions (no memory access except read-only
  image loads); C2 adds stack, pointer-argument and global memory.
- record.json measurements follow the C1-C4 slices.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Unresolved indirect branches and calls, jump tables without a proved bound,
and unproved no-return calls make a function SKIPPED(incomplete-cfg).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…one state

Add the dev-only kuna-verify crate. The machine side lifts each instruction
with the program's SLEIGH translator and evaluates it with the OpBehavior
table; the decompiled side interprets the final Funcdata. Both start from the
same raw state, with argument registers extended as the ABI documents. Return
values are compared under the physical ABI and, given C source, against a
typed contract. Calls and memory beyond the frame are SKIPPED (slice C2).

Reverting Noelo-Lab#794 (returnpair single) gives DIFFERS against its source contract.
Over tests/datatests and tests/stages: 3,979 functions, 4 DIFFERS (3 are the
narrowext bug its stage test pins, 1 is a relocation harness limit), 3
DIVERGES (switchrunon-cut-row). Results: docs/features/diffverify/c1-results.md.

kuna_narrowext::rules_for exposes the rules for a given mode; rules() is
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Random inputs rarely reach a branch on one value. A small symbolic executor
records branch conditions over the inputs on both sides, and z3 supplies
inputs that take the other side; verdicts still come only from concrete runs.
C5 follows C2 and precedes S1, which reuses its expressions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

# Conflicts:
#	docs/features/diffverify/proposal.md
The decompiled function is compiled into a flat program every run reuses,
registers live in a dense memoized file, and a function stops after four runs
that exhaust the step budget. `corpus` runs one worker process per core and
prints in file order; `--json` and `check --all` are new. Stage tests pin the
harness fixes found while building C1.

Every verdict over tests/datatests and tests/stages is unchanged, sequential
and parallel. Check time 49.8 s -> 3.6 s; wall time 111 s -> 20.6 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sjkim1127

Copy link
Copy Markdown
Contributor Author

[AUTOMATED] Folded into #1830, which carries this branch's commits plus slice C2 and a fix for switch rows that this branch maps to the wrong out-edge. The later slices will land there too.

@sjkim1127 sjkim1127 closed this Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant