82 lines
3.8 KiB
Python
82 lines
3.8 KiB
Python
"""Calibrate real FUN_101ab6d0 x87 returns and differential detection.
|
|
|
|
Run from the worktree:
|
|
tools/unicorn_diff/.venv/bin/python tools/unicorn_diff/calibrate.py
|
|
"""
|
|
from dataclasses import replace
|
|
from pathlib import Path
|
|
import struct
|
|
import sys
|
|
|
|
from udiff import (
|
|
Buffer, CallSpec, FloatTolerance, PeImage, RegValue,
|
|
ReturnKind, RunResult, UnicornRunner, Verdict, detect_fp, diff,
|
|
)
|
|
from udiff.abi import f32
|
|
from udiff.metadata import RVADB_PATH, load_calling_convention
|
|
|
|
|
|
def calibrate(dll: Path) -> None:
|
|
"""Compare real register-ABI calls with independently computed distances."""
|
|
image = PeImage(dll)
|
|
runner = UnicornRunner(image)
|
|
convention = load_calling_convention(0x1AB6D0)
|
|
print(f"metadata={RVADB_PATH} RVA=0x1ab6d0 calling_convention={convention.value}")
|
|
model = detect_fp(image.read(0x1AB6D0, 0xDA), image.va(0x1AB6D0))
|
|
assert model.name == "x87", model
|
|
print(f"DLL={dll} base={image.image_base:#x} VA={image.va(0x1AB6D0):#x}")
|
|
print(f"FP={model.name} flags={' '.join(model.compiler_flags)}")
|
|
tolerance = FloatTolerance(absolute=1e-6, relative=1e-6)
|
|
print(f"tolerance: absolute={tolerance.absolute} relative={tolerance.relative}")
|
|
center = (1.0, 2.0, 3.0)
|
|
extent = (2.0, 3.0, 4.0)
|
|
box = Buffer("box", struct.pack("<6f", *center, *extent))
|
|
for point in ((6.0, 9.0, 7.0), (-4.0, -5.0, 3.0), center):
|
|
original = runner.run(CallSpec(
|
|
rva=0x1AB6D0,
|
|
conv=convention,
|
|
ret=ReturnKind.FLOAT_ST0,
|
|
regs=(
|
|
RegValue("eax", buffer=box),
|
|
RegValue("edi", buffer=Buffer("point", struct.pack("<3f", *point))),
|
|
),
|
|
))
|
|
# Binary: center +/- extent spill to float; max(lower, point),
|
|
# min(upper, result); subtract point; sum squares; final float spill.
|
|
# These integral fixtures keep intermediate products exact in both
|
|
# Python binary64 and the original x87 extended precision.
|
|
offsets = tuple(
|
|
f32(p - min(f32(c + e), max(f32(c - e), p)))
|
|
for p, c, e in zip(point, center, extent, strict=True)
|
|
)
|
|
expected = f32(sum(value * value for value in offsets))
|
|
reference = RunResult(eax=0, st0=expected)
|
|
matching = diff(original, reference, tolerance)
|
|
assert original.st0_raw is not None, "real x87 register capture missing"
|
|
assert matching.verdict is Verdict.EQUAL, matching
|
|
print(
|
|
f"point={point} ST(0)={original.st0} raw={original.st0_raw} "
|
|
f"steps={original.steps} reference={expected} match={matching.verdict.value}"
|
|
)
|
|
# Inject a wrong additive constant into the reference calculation.
|
|
wrong_reference = replace(reference, st0=f32(expected + 1.0))
|
|
injected = diff(original, wrong_reference, tolerance)
|
|
assert injected.verdict is Verdict.DIFF, injected
|
|
assert len(injected.differences) == 1, injected
|
|
assert injected.differences[0].startswith("ST(0):"), injected
|
|
print(f"injected-bug={injected.verdict.value} {injected.differences[0]}")
|
|
assert original.st0 is not None
|
|
# All fields, including EAX and raw capture, remain identical except
|
|
# the decoded ST(0). This isolates the comparator's floating channel.
|
|
mutated = replace(original, st0=original.st0 + 2.0)
|
|
mutation = diff(original, mutated, tolerance)
|
|
assert mutation.verdict is Verdict.DIFF, mutation
|
|
assert len(mutation.differences) == 1, mutation
|
|
assert mutation.differences[0].startswith("ST(0):"), mutation
|
|
print(f"ST(0)-mutation={mutation.verdict.value} {mutation.differences[0]}")
|
|
print("PASS: real x87 capture, reference match, injected bug, ST(0)-only mutation")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
calibrate(Path(sys.argv[1]) if len(sys.argv) > 1 else Path("/home/athapa/ghidra/gamekdm.dll"))
|