feat(libavs-ea3): reconstruct+verify wave 11 5

This commit is contained in:
ulw-execute committed 2026-11-29 10:50:59 -05:00
1 parent 0a836c89c8
commit 19955cc67d
5 files changed
+351 -10

No files matched your search

@@ -187616,7 +187616,7 @@
"owning_module_guess": "unknown",
"rva": "0x0002d820",
"signature": "undefined4 __cdecl FUN_1002d820(uint * param_1)",
"status": "orig"
"status": "verified"
},
{
"callees": [
@@ -187633,7 +187633,7 @@
"owning_module_guess": "unknown",
"rva": "0x0002d950",
"signature": "int __cdecl FUN_1002d950(uint * param_1, int param_2, uint param_3)",
"status": "orig"
"status": "verified"
},
{
"callees": [
@@ -187650,7 +187650,7 @@
"owning_module_guess": "unknown",
"rva": "0x0002db50",
"signature": "undefined4 __cdecl FUN_1002db50(uint * param_1, int param_2)",
"status": "orig"
"status": "verified"
},
{
"callees": [],
@@ -44,10 +44,16 @@ from udiff.scaffold import resolve_oracle # noqa: E402
CDECL = CallConv.CDECL
REGISTER = CallConv.REGISTER
VOID = ReturnKind.VOID
INT = ReturnKind.INT
MODULE = ROOT / "re" / "libavs-win32-ea3"
HARNESS = MODULE / "recon_libavs-win32-ea3_wave11_harness.cpp"
SRC = ROOT / "src" / "libavs-win32-ea3" / "leaf_ops_w11.cpp"
W10_SRC = ROOT / "src" / "libavs-win32-ea3" / "leaf_ops_w10.cpp"
DIGIT_TBL_RVA = 0x534A0
DIGIT_TBL_SZ = 0x40
CLASS_TBL_RVA = 0x534E0
CLASS_TBL_SZ = 0x100
RECON = MODULE / "recon_libavs-win32-ea3_wave11.exe"
_MCF = sorted(Path("/nix/store").glob("*mcfgthread-i686-w64-mingw32-2.5.1/lib/libmcfgthread.a"))
MCFGTHREAD_LIB = str(_MCF[0].parent) if _MCF else (
@@ -59,7 +65,7 @@ def build(src: Path, exe: Path) -> None:
subprocess.run(
["i686-w64-mingw32-g++", "-std=c++20", "-O2", "-mfpmath=sse", "-msse2",
"-ffp-contract=off", "-fno-fast-math", f"-I{ROOT}/include", f"-I{ROOT}/src",
str(HARNESS), str(src),
str(HARNESS), str(src), str(W10_SRC),
f"-L{MCFGTHREAD_LIB}", "-o", str(exe),
"-static", "-static-libgcc", "-static-libstdc++"],
check=True, timeout=60,
@@ -127,15 +133,52 @@ def clear_case(gate_true: bool, subs: list[tuple[bool, int, int]]) -> Case:
return Case("clear", spec, payload, "")
def validate_case(words: list[int], dtbl: bytes) -> Case:
body = b"".join(u32(w) for w in words)
spec = CallSpec(rva=0x2D820, conv=CDECL, ret=INT,
stack_args=(StackValue(buffer=Buffer("pid", body)),))
return Case("validate", spec, body + dtbl, "")
def format_case(words: list[int], cap: int, dtbl: bytes) -> Case:
body = b"".join(u32(w) for w in words)
spec = CallSpec(rva=0x2D950, conv=CDECL, ret=INT,
stack_args=(StackValue(buffer=Buffer("p", body)),
StackValue(buffer=Buffer("out", b"\x00" * cap, out_size=cap)),
StackValue(immediate=cap)))
return Case("format", spec, u32(cap) + body + dtbl, "out")
def parse_case(text: str, ctbl: bytes) -> Case:
sb = text.encode() + b"\x00"
spec = CallSpec(rva=0x2DB50, conv=CDECL, ret=INT,
stack_args=(StackValue(buffer=Buffer("w", b"\x00" * 16, out_size=16)),
StackValue(buffer=Buffer("s", sb))))
return Case("parse", spec, u32(len(sb)) + sb + ctbl, "w")
def to_results(case: Case, original: RunResult, recon: bytes) -> tuple[RunResult, RunResult]:
if case.mode == "clear":
chunks = tuple(
(SUB_TAGS[i], recon[i * SUB_SIZE:(i + 1) * SUB_SIZE]) for i in range(6))
return original, RunResult(out_buffers=chunks)
if case.mode == "validate":
return original, RunResult(eax=int.from_bytes(recon, "little"))
if case.mode == "format":
return original, RunResult(eax=int.from_bytes(recon[:4], "little"),
out_buffers=(("out", recon[4:]),))
if case.mode == "parse":
return original, RunResult(eax=int.from_bytes(recon[:4], "little"),
out_buffers=(("w", recon[4:20]),))
return original, RunResult(out_buffers=((case.buf_tag, recon),))
def build_entries() -> dict[str, tuple[str, list[Case]]]:
def build_entries(image: PeImage) -> dict[str, tuple[str, list[Case]]]:
dtbl = image.read(DIGIT_TBL_RVA, DIGIT_TBL_SZ)
ctbl = image.read(CLASS_TBL_RVA, CLASS_TBL_SZ)
fmt_ok = [[0x300C0A81, 0xEA67341A, 0xD6C1F578, 0x29471B11],
[0x64D8008A, 0x7E04B85E, 0x6FBBD218, 0x4CA0C258],
[0x49200DCE, 0x0064B6A3, 0xD997BF3B, 0x21304DE1]]
return {
"mtold12": ("___mtold12 MtoLd12",
[mtold12_case([1, 2, 3]),
@@ -143,6 +186,23 @@ def build_entries() -> dict[str, tuple[str, list[Case]]]:
mtold12_case([5]),
mtold12_case([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]),
mtold12_case([1, 0, 0, 0, 0, 0, 0, 0])]),
"validate": ("FUN_1002d820 ValidatePackedId",
[validate_case([0, 0, 0, 0], dtbl),
validate_case([0x12345678, 0x9ABCDEF0, 0x11112222, 0x33334444], dtbl),
validate_case([0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF], dtbl),
validate_case(fmt_ok[0], dtbl)]),
"format": ("FUN_1002d950 FormatPackedId",
[format_case(fmt_ok[0], 80, dtbl),
format_case(fmt_ok[1], 80, dtbl),
format_case(fmt_ok[2], 80, dtbl),
format_case(fmt_ok[0], 4, dtbl),
format_case([0, 0, 0, 0], 80, dtbl)]),
"parse": ("FUN_1002db50 ParsePackedId",
[parse_case("ABC:D:E:F:01234567:A", ctbl),
parse_case("ABC:D:E:F:0123456789:A", ctbl),
parse_case("ZZZ:Z:Z:Z:99999999X:._", ctbl),
parse_case("bad", ctbl),
parse_case("ABC:D:E:F:01234567:ABCDE:12", ctbl)]),
"clear": ("FUN_10025460 ClearPairFields",
[clear_case(True, [(True, 1, 0x11111111), (True, 1, 0x22222222),
(True, 1, 0x33333333), (True, 1, 0x44444444),
@@ -162,6 +222,9 @@ def build_entries() -> dict[str, tuple[str, list[Case]]]:
MUTATIONS: list[tuple[str, str, str]] = [
("// rva=0x100405c0", "std::int16_t exponent = 0x404e;", "std::int16_t exponent = 0x404f;"),
("// rva=0x10025460", "== obj[1]", "!= obj[1]"),
("// rva=0x1002d820", "return 1;", "return 0;"),
("// rva=0x1002d950", "out[n] = ':';", "out[n] = ';';"),
("// rva=0x1002db50", "words[2] = l24;", "words[2] = l24 + 1;"),
]
@@ -175,7 +238,7 @@ def main() -> int:
oracle = resolve_oracle("libavs-win32-ea3.dll")
image = PeImage(oracle.path)
runner = UnicornRunner(image)
entries = build_entries()
entries = build_entries(image)
log = [f"DLL={oracle.path} sha256={oracle.sha256} base={image.image_base:#x} "
"comparison=faithful (exact owned-buffer channels)"]
@@ -217,8 +280,10 @@ def main() -> int:
log.append(f"ALL DIFFERENTIALS PASS ({total_cases} cases across {len(entries)} bodies, "
f"{len(entries)} verified functions)")
log.append("GLOBALS-FIRST: PASS (self-contained leaves: the CRT mantissa converter writes only "
"its caller-supplied output block; the sub-object clearer reads and writes only the "
"caller-supplied object and sub-object blocks; no module global is touched)")
"its caller-supplied output block; the sub-object clearer touches only the "
"caller-supplied object and sub-object blocks; the packed-id validator/formatter/"
"parser compose the verified wave-10 digit/class primitives over caller-supplied "
"input, output and read-only table blocks; no module global is written)")
print("\n".join(log))
return 0
@@ -2,8 +2,11 @@
// payload on stdin, runs the reconstruction named by argv[1] on the same inputs
// the Unicorn side feeds the original, and writes the hex result the T8 differ
// compares.
// mtold12 -> the 12-byte _LDBL12 output block (mantissa + exponent);
// clear -> the six 0x2c-byte sub-objects after the +0x24/+0x28 clears.
// mtold12 -> the 12-byte _LDBL12 output block (mantissa + exponent);
// clear -> the six 0x2c-byte sub-objects after the +0x24/+0x28 clears;
// validate -> the 4-byte EAX from the packed-id validator;
// format -> 4-byte EAX length then the cap-byte formatted buffer;
// parse -> 4-byte EAX then the 16-byte parsed word array.
#include <cstdint>
#include <cstdio>
#include <cstdlib>
@@ -47,6 +50,8 @@ std::uint32_t U32(const std::uint8_t* p) {
return v;
}
void PutU32(std::uint32_t value) { PutHex(&value, 4); }
int RunMtoLd12(const std::vector<std::uint8_t>& in) {
std::int32_t count = static_cast<std::int32_t>(U32(in.data()));
const char* digits = reinterpret_cast<const char*>(in.data() + 4);
@@ -84,6 +89,43 @@ int RunClear(const std::vector<std::uint8_t>& in) {
return 0;
}
int RunValidate(const std::vector<std::uint8_t>& in) {
std::uint32_t words[4];
std::memcpy(words, in.data(), 16);
const std::uint8_t* table = in.data() + 16;
PutU32(ValidatePackedId(words, table));
return 0;
}
int RunFormat(const std::vector<std::uint8_t>& in) {
std::uint32_t cap = U32(in.data());
std::uint32_t words[4];
std::memcpy(words, in.data() + 4, 16);
const std::uint8_t* table = in.data() + 20;
std::vector<char> out(cap == 0 ? 1 : cap, 0);
std::int32_t ret = FormatPackedId(words, out.data(), cap, table);
std::vector<std::uint8_t> blob(4 + cap, 0);
std::memcpy(blob.data(), &ret, 4);
if (cap != 0) {
std::memcpy(blob.data() + 4, out.data(), cap);
}
PutHex(blob.data(), blob.size());
return 0;
}
int RunParse(const std::vector<std::uint8_t>& in) {
std::uint32_t len = U32(in.data());
const char* text = reinterpret_cast<const char*>(in.data() + 4);
const std::uint8_t* table = in.data() + 4 + len;
std::uint32_t words[4] = {0, 0, 0, 0};
std::uint32_t ret = ParsePackedId(words, text, table);
std::uint8_t blob[20];
std::memcpy(blob, &ret, 4);
std::memcpy(blob + 4, words, 16);
PutHex(blob, sizeof(blob));
return 0;
}
} // namespace
int main(int argc, char** argv) {
@@ -95,6 +137,9 @@ int main(int argc, char** argv) {
const std::string mode = argv[1];
if (mode == "mtold12") return RunMtoLd12(in);
if (mode == "clear") return RunClear(in);
if (mode == "validate") return RunValidate(in);
if (mode == "format") return RunFormat(in);
if (mode == "parse") return RunParse(in);
std::fprintf(stderr, "unknown mode %s\n", argv[1]);
return 2;
}
+204
View File
@@ -3,6 +3,8 @@
#include <cstdint>
#include <cstring>
#include "libavs-win32-ea3/leaf_ops_w10.h"
namespace gk {
namespace avsea3 {
@@ -154,5 +156,207 @@ void GK_CDECL ClearPairFields(void* object) {
ClearIfMarked(obj[0x25]);
}
// rva=0x1002d820 (orig FUN_1002d820)
// Validate the six packed fields of a four-dword id through the verified digit
// scanner; return 1 unless a field fails to scan (the scanner never reports a
// negative result for the shipping digit table, so the error returns mirror the
// original's dead branches).
std::uint32_t GK_CDECL ValidatePackedId(const std::uint32_t* words,
const void* digit_table) {
std::uint32_t w0 = words[0];
std::uint32_t w1 = words[1];
std::uint32_t w2 = words[2];
std::uint32_t w3 = words[3];
if (static_cast<std::int32_t>(ScanPackedDigits(w3 >> 8, 6, 4, 4, digit_table)) < 0) {
return 0xff;
}
if (static_cast<std::int32_t>(ScanPackedDigits(w1 & 0xfff, 6, 2, 2, digit_table)) < 0) {
return 0xff;
}
if (static_cast<std::int32_t>(ScanPackedDigits(w2, 4, 8, 8, digit_table)) < 0) {
return 0xff;
}
std::uint32_t lo = w3 & 0xff;
if (lo != 0 &&
static_cast<std::int32_t>(ScanPackedDigits(lo, 4, 2, 2, digit_table)) < 0) {
return 0xff;
}
std::int32_t last = static_cast<std::int32_t>(ScanPackedDigits(w0 >> 2, 6, 1, 5, digit_table));
if (last < 0) {
return 0xff;
}
if (last == 5 &&
static_cast<std::int32_t>(
ScanPackedDigits((w0 & 3) << 0x10 | w1 >> 0x10, 6, 0, 3, digit_table)) < 0) {
return 0xff;
}
return 1;
}
// rva=0x1002d950 (orig FUN_1002d950)
// Format the four-dword id into colon-separated digit groups through the verified
// digit decoder, with an optional 'X' marker and a trailing version group.
std::int32_t GK_CDECL FormatPackedId(const std::uint32_t* words, char* out,
std::uint32_t cap, const void* digit_table) {
std::uint32_t w0 = words[0];
std::uint32_t w1 = words[1];
std::uint32_t w2 = words[2];
std::uint32_t w3 = words[3];
std::uint32_t n = DecodePackedDigits(out, cap, w3 >> 0xe, 6, 3, 3, digit_table);
if (static_cast<std::int32_t>(n) < 0 || cap - n == 0) {
return -1;
}
char* p = out + n + 1;
out[n] = ':';
std::uint32_t avail = cap - n - 1;
n = DecodePackedDigits(p, avail, w3 >> 8, 6, 1, 1, digit_table);
if (static_cast<std::int32_t>(n) < 0 || avail - n == 0) {
return -1;
}
avail = avail - n - 1;
p[n] = ':';
p = p + n + 1;
n = DecodePackedDigits(p, avail, w1 >> 6, 6, 1, 1, digit_table);
if (static_cast<std::int32_t>(n) < 0 || avail - n == 0) {
return -1;
}
avail = avail - n - 1;
p[n] = ':';
p = p + n + 1;
n = DecodePackedDigits(p, avail, w1, 6, 1, 1, digit_table);
if (static_cast<std::int32_t>(n) < 0 || avail - n == 0) {
return -1;
}
p[n] = ':';
p = p + n + 1;
avail = avail - n - 1;
n = DecodePackedDigits(p, avail, w2, 4, 8, 8, digit_table);
if (static_cast<std::int32_t>(n) < 0) {
return -1;
}
char* q = p + n;
avail = avail - n;
if ((w3 & 0xff) != 0) {
n = DecodePackedDigits(q, avail, w3 & 0xff, 4, 2, 2, digit_table);
if (static_cast<std::int32_t>(n) < 0) {
return -1;
}
q = q + n;
avail = avail - n;
}
if ((w1 & 0xf000) != 0) {
if (avail == 0) {
return -1;
}
*q = 'X';
q = q + 1;
avail = avail - 1;
}
if (avail == 0) {
return -1;
}
*q = ':';
n = DecodePackedDigits(q + 1, avail - 1, w0 >> 2, 6, 1, 5, digit_table);
if (static_cast<std::int32_t>(n) < 0) {
return -1;
}
std::uint32_t left = (avail - 1) - n;
q = q + n + 1;
if (n == 5) {
n = DecodePackedDigits(q, left, (w0 & 3) << 0x10 | w1 >> 0x10, 6, 0, 3, digit_table);
if (static_cast<std::int32_t>(n) < 0) {
return -1;
}
q = q + n;
left = left - n;
}
if (left == 0) {
return -1;
}
*q = 0;
return static_cast<std::int32_t>(q - out);
}
// rva=0x1002db50 (orig FUN_1002db50)
// Parse a colon-separated digit-group string back into the four-dword id through
// the verified class packer and class predicate.
std::uint32_t GK_CDECL ParsePackedId(std::uint32_t* words, const char* text,
const void* class_table) {
std::uint32_t l34 = 0;
std::uint32_t l30 = 0;
std::uint32_t l2c = 0;
std::uint32_t l28 = 0;
std::uint32_t l24 = 0;
std::uint32_t l20 = 0;
std::uint32_t l1c = 0;
std::uint32_t l18 = 0;
std::uint32_t l14 = 0;
const char* p = text;
std::uint32_t n = PackClassDigits(&l34, p, 6, 6, 3, 3, class_table);
if (static_cast<std::int32_t>(n) < 0 || p[n] != ':') {
return 0xffffffff;
}
p = p + n + 1;
n = PackClassDigits(&l30, p, 6, 2, 1, 1, class_table);
if (static_cast<std::int32_t>(n) < 0 || p[n] != ':') {
return 0xffffffff;
}
p = p + n + 1;
n = PackClassDigits(&l2c, p, 6, 2, 1, 1, class_table);
if (static_cast<std::int32_t>(n) < 0 || p[n] != ':') {
return 0xffffffff;
}
p = p + n + 1;
n = PackClassDigits(&l28, p, 6, 2, 1, 1, class_table);
if (static_cast<std::int32_t>(n) < 0 || p[n] != ':') {
return 0xffffffff;
}
p = p + n + 1;
n = PackClassDigits(&l24, p, 4, 4, 8, 8, class_table);
if (static_cast<std::int32_t>(n) < 0) {
return 0xffffffff;
}
p = p + n;
if (CharClassBitMember(p, 4, class_table) != 0) {
n = PackClassDigits(&l20, p, 4, 4, 2, 2, class_table);
if (static_cast<std::int32_t>(n) < 0) {
return 0xffffffff;
}
p = p + n;
} else {
l20 = 0;
}
std::uint8_t c = static_cast<std::uint8_t>(*p);
if (c == 0x58) {
p = p + 1;
}
l1c = (c == 0x58) ? 1u : 0u;
if (static_cast<std::uint8_t>(*p) != 0x3a) {
return 0xffffffff;
}
n = PackClassDigits(&l18, p + 1, 6, 0xe, 1, 5, class_table);
if (static_cast<std::int32_t>(n) < 0) {
return 0xffffffff;
}
p = p + n + 1;
if (n == 5) {
n = PackClassDigits(&l14, p, 6, 0xe, 0, 3, class_table);
if (static_cast<std::int32_t>(n) < 0) {
return 0xffffffff;
}
p = p + n;
} else {
l14 = 0;
}
if (static_cast<std::uint8_t>(*p) != 0) {
return 0xffffffff;
}
words[1] = l14 << 0x10 | l1c << 0xc | l2c << 6 | l28;
words[0] = l18 << 2 | (l14 >> 0x10 & 3);
words[3] = l34 << 0xe | l30 << 8 | l20;
words[2] = l24;
return 0;
}
} // namespace avsea3
} // namespace gk
+27
View File
@@ -32,5 +32,32 @@ void GK_CDECL MtoLd12(const char* digits, std::int32_t count, void* out);
// caller-supplied sub-object blocks reached through the caller-supplied object.
void GK_CDECL ClearPairFields(void* object);
// rva=0x1002d820 (orig FUN_1002d820) __cdecl: validate a four-dword packed id by
// scanning each packed field through the verified digit scanner (0x36b00): the
// high 24 bits of word[3] (6 bits x4), word[1]&0xfff (6x2), word[2] (4x8), the
// low byte of word[3] when nonzero (4x2), word[0]>>2 (6x1..5) and, when that
// yields five digits, the spillover field (6x3). Returns 1 on success or 0xff on
// the first field that fails to scan. The digit table (0x534a0) is modelled as a
// caller-supplied block.
std::uint32_t GK_CDECL ValidatePackedId(const std::uint32_t* words,
const void* digit_table);
// rva=0x1002d950 (orig FUN_1002d950) __cdecl: format a four-dword packed id into
// the caller buffer as colon-separated digit groups through the verified digit
// decoder (0x36b50), emitting an optional 'X' marker (word[1]&0xf000) and the
// version spillover group. Returns the written length (excluding the NUL) or -1
// on overflow / decode failure. The digit table (0x534a0) is modelled as a
// caller-supplied block.
std::int32_t GK_CDECL FormatPackedId(const std::uint32_t* words, char* out,
std::uint32_t cap, const void* digit_table);
// rva=0x1002db50 (orig FUN_1002db50) __cdecl: parse a colon-separated digit-group
// string into a four-dword packed id through the verified class packer (0x36c40)
// and class predicate (0x36c10). Returns 0 on success (words filled) or 0xffffffff
// on any malformed field. The class table (0x534e0) is modelled as a
// caller-supplied block.
std::uint32_t GK_CDECL ParsePackedId(std::uint32_t* words, const char* text,
const void* class_table);
} // namespace avsea3
} // namespace gk