feat(libavs-win32): reconstruct+verify wave 2 5 functions
Wave 2 on libavs-win32.dll: five more self-contained leaves (bounded string copy, memory fill, a 64-bit absolute-difference threshold, and the codepage->LCID map), each reconstructed as MinGW C++ and verified against the shipping binary via the T1 Unicorn differential plus a per-body injected-bug non-vacuity check. The copy/fill leaves are compared on EAX and the owned destination buffer; the scalar leaves on EAX. All are pure POD-in leaves with no module globals, callees, or foreign-ABI dispatch. fp_model=none. 0x10011ae0 node_strncpy length-bounded NUL-copy returning count 0x10011960 node_copybounded cap/count-bounded NUL-terminated copy 0x100110b0 node_memset byte fill of n bytes 0x10012d00 node_absdiff_ge |A64-B64| >= 15000 predicate 0x10002b40 node_cp_to_lcid codepage to LCID map (EAX-passed arg) 0x10002b40 takes its argument in EAX (SUB EAX,0x3a4 prologue); it is proved through the REGISTER calling convention. Proof: re/avs/prove_libavs-win32_wave2.py (27 cases, exit 0).
This commit is contained in:
1 parent
29bea39650
commit
6d7a947803
5 files changed
+528
-5
No files matched your search
+20
-5
@@ -192998,7 +192998,10 @@
|
||||
"owning_module_guess": "unknown",
|
||||
"rva": "0x00002b40",
|
||||
"signature": "int __cdecl CPtoLCID(int param_1)",
|
||||
"status": "orig"
|
||||
"status": "verified",
|
||||
"fp_model": "none",
|
||||
"portability": "abi",
|
||||
"evidence": "T1 Unicorn differential re/avs/prove_libavs-win32_wave2.py (node_cp_to_lcid): 6 cases (the four mapped codepages 0x3a4/0x3a8/0x3b5/0x3b6 plus two unmapped) original==recon exact EAX, plus injected-bug non-vacuity (0x3a4->0x412 caught as EAX DIFF). Arg arrives in EAX (SUB EAX,0x3a4 prologue), proved via REGISTER calling convention; bare RET, no stack cleanup. No callees/globals, globals-first gate trivial. Reconstruction src/avs/leaf_ops_w2.cpp node_cp_to_lcid. Semantic name node_cp_to_lcid (codepage to LCID map)."
|
||||
},
|
||||
{
|
||||
"callees": [
|
||||
@@ -199073,7 +199076,10 @@
|
||||
"owning_module_guess": "unknown",
|
||||
"rva": "0x000110b0",
|
||||
"signature": "uint __cdecl XCd229cc000087(uint param_1, undefined1 param_2, uint param_3)",
|
||||
"status": "orig"
|
||||
"status": "verified",
|
||||
"fp_model": "none",
|
||||
"portability": "port",
|
||||
"evidence": "T1 Unicorn differential re/avs/prove_libavs-win32_wave2.py (node_memset): 5 cases (1 byte, 16, 15, 33, 7 at varied fill values) original==recon on the owned destination buffer (return is its own input pointer, compared via the buffer channel), plus injected-bug non-vacuity (fill +1 caught as out_buffers DIFF). __cdecl (bare RET); no callees/globals, globals-first gate trivial. Reconstruction src/avs/leaf_ops_w2.cpp node_memset. Semantic name node_memset (byte fill of n bytes)."
|
||||
},
|
||||
{
|
||||
"callees": [],
|
||||
@@ -199491,7 +199497,10 @@
|
||||
"owning_module_guess": "unknown",
|
||||
"rva": "0x00011960",
|
||||
"signature": "int __cdecl XCd229cc000063(undefined1 * param_1, int param_2, int param_3, int param_4)",
|
||||
"status": "orig"
|
||||
"status": "verified",
|
||||
"fp_model": "none",
|
||||
"portability": "port",
|
||||
"evidence": "T1 Unicorn differential re/avs/prove_libavs-win32_wave2.py (node_copybounded): 5 cases (NUL, empty, cap-truncation, cnt-truncation, cnt=0) original==recon on EAX (count) and the owned destination buffer, plus injected-bug non-vacuity (dst byte +1 caught as out_buffers DIFF). __cdecl (bare RET); no callees/globals, globals-first gate trivial. Reconstruction src/avs/leaf_ops_w2.cpp node_copybounded. Semantic name node_copybounded (cap- and count-bounded NUL-terminated copy)."
|
||||
},
|
||||
{
|
||||
"callees": [
|
||||
@@ -199548,7 +199557,10 @@
|
||||
"owning_module_guess": "unknown",
|
||||
"rva": "0x00011ae0",
|
||||
"signature": "int __cdecl XCd229cc0000f6(int param_1, int param_2, int param_3)",
|
||||
"status": "orig"
|
||||
"status": "verified",
|
||||
"fp_model": "none",
|
||||
"portability": "port",
|
||||
"evidence": "T1 Unicorn differential re/avs/prove_libavs-win32_wave2.py (node_strncpy): 5 cases (early NUL, empty, truncation, exact-fit, bounded) original==recon on EAX (count) and the owned destination buffer, plus injected-bug non-vacuity (dst byte +1 caught as out_buffers DIFF). __cdecl (bare RET); no callees/globals, globals-first gate trivial. Reconstruction src/avs/leaf_ops_w2.cpp node_strncpy. Semantic name node_strncpy (length-bounded NUL-copy returning the copied count)."
|
||||
},
|
||||
{
|
||||
"callees": [
|
||||
@@ -200082,7 +200094,10 @@
|
||||
"owning_module_guess": "unknown",
|
||||
"rva": "0x00012d00",
|
||||
"signature": "undefined4 __cdecl FUN_10012d00(uint param_1, int param_2, uint param_3, int param_4)",
|
||||
"status": "orig"
|
||||
"status": "verified",
|
||||
"fp_model": "none",
|
||||
"portability": "port",
|
||||
"evidence": "T1 Unicorn differential re/avs/prove_libavs-win32_wave2.py (node_absdiff_ge): 6 cases straddling the 15000 threshold (0, 15000, 14999, -15000, high-word, 0xffffffff) original==recon exact EAX, plus injected-bug non-vacuity (threshold 15000->15001 caught as EAX DIFF at the boundary case). __cdecl (bare RET); no callees/globals, globals-first gate trivial. Reconstruction src/avs/leaf_ops_w2.cpp node_absdiff_ge. Semantic name node_absdiff_ge (|A64-B64| >= 15000 predicate)."
|
||||
},
|
||||
{
|
||||
"callees": [
|
||||
|
||||
@@ -0,0 +1,243 @@
|
||||
"""libavs-win32.dll wave-2 differential proof.
|
||||
|
||||
Five self-contained AVS runtime leaves (bounded string copy, memory fill, a
|
||||
64-bit absolute-difference threshold, and the codepage->LCID map) proved with
|
||||
the compiled-recon differential: each original is emulated inside the shipping
|
||||
libavs-win32.dll under Unicorn and the MinGW C++ reconstruction
|
||||
(src/avs/leaf_ops_w2.cpp) runs under wine on identical inputs. Channels
|
||||
compared: EAX plus an owned output buffer (node_strncpy, node_copybounded), the
|
||||
owned output buffer alone (node_memset, whose return is its own input pointer),
|
||||
and EAX (node_absdiff_ge, node_cp_to_lcid). A final pass rebuilds the
|
||||
reconstruction with a per-body injected bug and asserts every differential
|
||||
flags it.
|
||||
|
||||
Globals-first: every leaf is self-contained. The copy/fill leaves read one
|
||||
caller-supplied source and write only their one caller-supplied destination;
|
||||
the threshold and codepage leaves are pure scalar functions. None reads or
|
||||
writes a module global or calls another routine.
|
||||
|
||||
tools/unicorn_diff/.venv/bin/python re/avs/prove_libavs-win32_wave2.py
|
||||
"""
|
||||
# /// script
|
||||
# requires-python = ">=3.11"
|
||||
# dependencies = ["unicorn", "pefile", "capstone"]
|
||||
# ///
|
||||
from __future__ import annotations
|
||||
|
||||
import dataclasses
|
||||
import os
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
sys.path.insert(0, str(ROOT / "tools" / "unicorn_diff"))
|
||||
|
||||
from udiff import ( # noqa: E402
|
||||
Buffer, CallConv, CallSpec, PeImage, RegValue, ReturnKind,
|
||||
RunResult, StackValue, UnicornRunner, Verdict, diff,
|
||||
)
|
||||
from udiff.scaffold import resolve_oracle # noqa: E402
|
||||
|
||||
CDECL = CallConv.CDECL
|
||||
REGISTER = CallConv.REGISTER
|
||||
INT = ReturnKind.INT
|
||||
VOID = ReturnKind.VOID
|
||||
|
||||
MODULE = ROOT / "re" / "avs"
|
||||
HARNESS = MODULE / "recon_avs_w2_harness.cpp"
|
||||
SRC = ROOT / "src" / "avs" / "leaf_ops_w2.cpp"
|
||||
RECON = MODULE / "recon_avs_w2.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 (
|
||||
"/nix/store/g72bh3mrbh6gkh59axd2199js9kccxfs-mcfgthread-i686-w64-mingw32-2.5.1/lib"
|
||||
)
|
||||
|
||||
|
||||
def build(src: Path, exe: Path) -> None:
|
||||
subprocess.run(
|
||||
["i686-w64-mingw32-g++", "-std=c++20", "-O2",
|
||||
f"-I{ROOT}/include", f"-I{ROOT}/src",
|
||||
str(HARNESS), str(src),
|
||||
f"-L{MCFGTHREAD_LIB}", "-o", str(exe),
|
||||
"-static", "-static-libgcc", "-static-libstdc++"],
|
||||
check=True, timeout=120,
|
||||
)
|
||||
|
||||
|
||||
def run_recon(exe: Path, mode: str, payload: bytes) -> bytes:
|
||||
env = dict(os.environ)
|
||||
env["WINEDEBUG"] = "-all"
|
||||
out = subprocess.run(["wine", str(exe), mode], input=payload.hex().encode(),
|
||||
capture_output=True, env=env, check=True, timeout=60)
|
||||
return bytes.fromhex(out.stdout.decode().replace("\n", "").strip())
|
||||
|
||||
|
||||
def u32(value: int) -> bytes:
|
||||
return struct.pack("<I", value & 0xFFFFFFFF)
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True, slots=True)
|
||||
class Case:
|
||||
mode: str
|
||||
spec: CallSpec
|
||||
payload: bytes
|
||||
kind: str
|
||||
buf_tag: str = ""
|
||||
|
||||
|
||||
def strncpy_case(src: bytes, n: int) -> Case:
|
||||
spec = CallSpec(rva=0x11AE0, conv=CDECL, ret=INT,
|
||||
stack_args=(StackValue(buffer=Buffer("dst", b"\x00" * n, out_size=n)),
|
||||
StackValue(buffer=Buffer("src", src)),
|
||||
StackValue(immediate=n)))
|
||||
payload = u32(n) + u32(len(src)) + src
|
||||
return Case("strncpy", spec, payload, "eax_buf", "dst")
|
||||
|
||||
|
||||
def copybounded_case(src: bytes, cap: int, cnt: int) -> Case:
|
||||
spec = CallSpec(rva=0x11960, conv=CDECL, ret=INT,
|
||||
stack_args=(StackValue(buffer=Buffer("dst", b"\x00" * cap, out_size=cap)),
|
||||
StackValue(immediate=cap),
|
||||
StackValue(buffer=Buffer("src", src)),
|
||||
StackValue(immediate=cnt)))
|
||||
payload = u32(cap) + u32(cnt) + u32(len(src)) + src
|
||||
return Case("copybounded", spec, payload, "eax_buf", "dst")
|
||||
|
||||
|
||||
def memset_case(value: int, n: int) -> Case:
|
||||
spec = CallSpec(rva=0x110B0, conv=CDECL, ret=VOID,
|
||||
stack_args=(StackValue(buffer=Buffer("dst", b"\x00" * n, out_size=n)),
|
||||
StackValue(immediate=value),
|
||||
StackValue(immediate=n)))
|
||||
payload = bytes([value & 0xFF]) + u32(n)
|
||||
return Case("memset", spec, payload, "buf", "dst")
|
||||
|
||||
|
||||
def absdiff_case(lo_a: int, hi_a: int, lo_b: int, hi_b: int) -> Case:
|
||||
spec = CallSpec(rva=0x12D00, conv=CDECL, ret=INT,
|
||||
stack_args=(StackValue(immediate=lo_a), StackValue(immediate=hi_a),
|
||||
StackValue(immediate=lo_b), StackValue(immediate=hi_b)))
|
||||
payload = u32(lo_a) + u32(hi_a) + u32(lo_b) + u32(hi_b)
|
||||
return Case("absdiff", spec, payload, "eax")
|
||||
|
||||
|
||||
def cptolcid_case(cp: int) -> Case:
|
||||
spec = CallSpec(rva=0x2B40, conv=REGISTER, ret=INT,
|
||||
regs=(RegValue("eax", immediate=cp),))
|
||||
payload = u32(cp)
|
||||
return Case("cptolcid", spec, payload, "eax")
|
||||
|
||||
|
||||
def build_entries() -> dict[str, tuple[str, list[Case]]]:
|
||||
return {
|
||||
"strncpy": ("FUN_10011ae0 node_strncpy",
|
||||
[strncpy_case(b"hi\x00", 8), strncpy_case(b"\x00", 4),
|
||||
strncpy_case(b"abcdefgh", 4), strncpy_case(b"abc\x00", 4),
|
||||
strncpy_case(b"exact\x00", 6)]),
|
||||
"copybounded": ("FUN_10011960 node_copybounded",
|
||||
[copybounded_case(b"hi\x00", 8, 8), copybounded_case(b"\x00", 4, 4),
|
||||
copybounded_case(b"abcdefgh", 4, 8), copybounded_case(b"abc\x00", 8, 2),
|
||||
copybounded_case(b"xyz\x00", 8, 0)]),
|
||||
"memset": ("FUN_100110b0 node_memset",
|
||||
[memset_case(0xAB, 1), memset_case(0x00, 16), memset_case(0xFF, 15),
|
||||
memset_case(0x5A, 33), memset_case(0xCC, 7)]),
|
||||
"absdiff": ("FUN_10012d00 node_absdiff_ge",
|
||||
[absdiff_case(0, 0, 0, 0), absdiff_case(15000, 0, 0, 0),
|
||||
absdiff_case(14999, 0, 0, 0), absdiff_case(0, 0, 15000, 0),
|
||||
absdiff_case(0, 1, 0, 0), absdiff_case(0xFFFFFFFF, 0, 0, 0)]),
|
||||
"cptolcid": ("CPtoLCID node_cp_to_lcid",
|
||||
[cptolcid_case(0x3A4), cptolcid_case(0x3A8), cptolcid_case(0x3B5),
|
||||
cptolcid_case(0x3B6), cptolcid_case(0x0409), cptolcid_case(0)]),
|
||||
}
|
||||
|
||||
|
||||
def to_results(case: Case, original: RunResult, recon: bytes) -> tuple[RunResult, RunResult]:
|
||||
if case.kind == "eax":
|
||||
return original, RunResult(eax=int.from_bytes(recon, "little"))
|
||||
if case.kind == "eax_buf":
|
||||
return original, RunResult(eax=int.from_bytes(recon[:4], "little"),
|
||||
out_buffers=((case.buf_tag, recon[4:]),))
|
||||
if case.kind == "buf":
|
||||
return original, RunResult(out_buffers=((case.buf_tag, recon),))
|
||||
raise ValueError(f"unknown kind {case.kind}")
|
||||
|
||||
|
||||
MUTATIONS: list[tuple[str, str, str]] = [
|
||||
("// rva=0x10011ae0", "dst[i] = c;\n if (c == 0) {", "dst[i] = c + 1;\n if (c == 0) {"),
|
||||
("// rva=0x10011960", "const unsigned char c = src[i];\n dst[i] = c;",
|
||||
"const unsigned char c = src[i];\n dst[i] = c + 1;"),
|
||||
("// rva=0x100110b0", "d[i] = v;", "d[i] = v + 1;"),
|
||||
("// rva=0x10012d00", "(abs_low < 15000u)", "(abs_low < 15001u)"),
|
||||
("// rva=0x10002b40", "if (cp == 0x3A4) {\n return 0x411;",
|
||||
"if (cp == 0x3A4) {\n return 0x412;"),
|
||||
]
|
||||
|
||||
|
||||
def mutate(src: str, anchor: str, old: str, new: str) -> str:
|
||||
start = src.index(anchor)
|
||||
hit = src.index(old, start)
|
||||
return src[:hit] + new + src[hit + len(old):]
|
||||
|
||||
|
||||
def main() -> int:
|
||||
oracle = resolve_oracle("libavs-win32.dll")
|
||||
image = PeImage(oracle.path)
|
||||
runner = UnicornRunner(image)
|
||||
entries = build_entries()
|
||||
log = [f"DLL={oracle.path} sha256={oracle.sha256} base={image.image_base:#x} "
|
||||
"comparison=faithful (exact EAX/owned-buffer channels)"]
|
||||
|
||||
build(SRC, RECON)
|
||||
total_cases = 0
|
||||
for _, (title, cases) in entries.items():
|
||||
print(f"PROVING {title}", flush=True)
|
||||
for case in cases:
|
||||
original = runner.run(case.spec)
|
||||
recon = run_recon(RECON, case.mode, case.payload)
|
||||
left, right = to_results(case, original, recon)
|
||||
report = diff(left, right)
|
||||
assert report.verdict is Verdict.EQUAL, (title, case.mode, case.payload.hex(), report)
|
||||
log.append(f" {title}: PASS ({len(cases)} cases, original==reconstructed)")
|
||||
total_cases += len(cases)
|
||||
|
||||
bug_src = SRC.read_text()
|
||||
for anchor, old, new in MUTATIONS:
|
||||
bug_src = mutate(bug_src, anchor, old, new)
|
||||
assert bug_src != SRC.read_text(), "no mutation applied"
|
||||
bug_cpp = RECON.parent / "leaf_ops_w2_bug.cpp"
|
||||
bug_exe = RECON.parent / "recon_avs_w2_bug.exe"
|
||||
bug_cpp.write_text(bug_src)
|
||||
try:
|
||||
build(bug_cpp, bug_exe)
|
||||
for _, (title, cases) in entries.items():
|
||||
flagged = False
|
||||
for case in cases:
|
||||
original = runner.run(case.spec)
|
||||
recon = run_recon(bug_exe, case.mode, case.payload)
|
||||
left, right = to_results(case, original, recon)
|
||||
report = diff(left, right)
|
||||
if report.verdict is Verdict.DIFF:
|
||||
flagged = True
|
||||
log.append(f" BUG {title} (injected bug rejected: "
|
||||
f"{report.differences[0].split(':')[0]})")
|
||||
break
|
||||
assert flagged, (title, "injected bug not caught by any case")
|
||||
finally:
|
||||
bug_cpp.unlink(missing_ok=True)
|
||||
bug_exe.unlink(missing_ok=True)
|
||||
log.append(f"NON-VACUITY: PASS (per-body injected bug caught for all {len(entries)} bodies)")
|
||||
|
||||
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: copy/fill leaves read one "
|
||||
"caller-supplied source and write only their one owned destination; the "
|
||||
"threshold and codepage leaves are pure scalar functions; no module global, "
|
||||
"callee, or foreign-ABI dispatch)")
|
||||
print("\n".join(log))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
@@ -0,0 +1,136 @@
|
||||
#include <cctype>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "avs/leaf_ops_w2.h"
|
||||
|
||||
namespace {
|
||||
|
||||
std::vector<std::uint8_t> ReadHex() {
|
||||
std::vector<std::uint8_t> out;
|
||||
std::string digits;
|
||||
int c = 0;
|
||||
while ((c = std::getchar()) != EOF) {
|
||||
if (std::isxdigit(c) != 0) {
|
||||
digits.push_back(static_cast<char>(c));
|
||||
}
|
||||
}
|
||||
for (std::size_t i = 0; i + 1 < digits.size(); i += 2) {
|
||||
out.push_back(static_cast<std::uint8_t>(
|
||||
std::strtol(digits.substr(i, 2).c_str(), nullptr, 16)));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
void PutHex(const void *data, std::size_t size) {
|
||||
const auto *bytes = static_cast<const std::uint8_t *>(data);
|
||||
for (std::size_t i = 0; i < size; ++i) {
|
||||
std::printf("%02x", bytes[i]);
|
||||
}
|
||||
std::printf("\n");
|
||||
}
|
||||
|
||||
std::uint32_t Rd32(const std::vector<std::uint8_t> &in, std::size_t pos) {
|
||||
return static_cast<std::uint32_t>(in[pos]) |
|
||||
(static_cast<std::uint32_t>(in[pos + 1]) << 8) |
|
||||
(static_cast<std::uint32_t>(in[pos + 2]) << 16) |
|
||||
(static_cast<std::uint32_t>(in[pos + 3]) << 24);
|
||||
}
|
||||
|
||||
int RunStrncpy(const std::vector<std::uint8_t> &in) {
|
||||
std::size_t c = 0;
|
||||
const int n = static_cast<int>(Rd32(in, c));
|
||||
c += 4;
|
||||
const std::uint32_t ls = Rd32(in, c);
|
||||
c += 4;
|
||||
const unsigned char *src = in.data() + c;
|
||||
(void)ls;
|
||||
std::vector<unsigned char> dst(n > 0 ? static_cast<std::size_t>(n) : 0, 0);
|
||||
const std::uint32_t r =
|
||||
static_cast<std::uint32_t>(avs::node_strncpy(dst.data(), src, n));
|
||||
std::vector<std::uint8_t> out;
|
||||
out.insert(out.end(), reinterpret_cast<const std::uint8_t *>(&r),
|
||||
reinterpret_cast<const std::uint8_t *>(&r) + 4);
|
||||
out.insert(out.end(), dst.begin(), dst.end());
|
||||
PutHex(out.data(), out.size());
|
||||
return 0;
|
||||
}
|
||||
|
||||
int RunCopyBounded(const std::vector<std::uint8_t> &in) {
|
||||
std::size_t c = 0;
|
||||
const int cap = static_cast<int>(Rd32(in, c));
|
||||
c += 4;
|
||||
const int cnt = static_cast<int>(Rd32(in, c));
|
||||
c += 4;
|
||||
const std::uint32_t ls = Rd32(in, c);
|
||||
c += 4;
|
||||
const unsigned char *src = in.data() + c;
|
||||
(void)ls;
|
||||
std::vector<unsigned char> dst(cap > 0 ? static_cast<std::size_t>(cap) : 0, 0);
|
||||
const std::uint32_t r =
|
||||
static_cast<std::uint32_t>(avs::node_copybounded(dst.data(), cap, src, cnt));
|
||||
std::vector<std::uint8_t> out;
|
||||
out.insert(out.end(), reinterpret_cast<const std::uint8_t *>(&r),
|
||||
reinterpret_cast<const std::uint8_t *>(&r) + 4);
|
||||
out.insert(out.end(), dst.begin(), dst.end());
|
||||
PutHex(out.data(), out.size());
|
||||
return 0;
|
||||
}
|
||||
|
||||
int RunMemset(const std::vector<std::uint8_t> &in) {
|
||||
std::size_t c = 0;
|
||||
const unsigned char v = in[c++];
|
||||
const std::uint32_t n = Rd32(in, c);
|
||||
std::vector<unsigned char> dst(n, 0);
|
||||
avs::node_memset(dst.data(), v, n);
|
||||
PutHex(dst.data(), dst.size());
|
||||
return 0;
|
||||
}
|
||||
|
||||
int RunAbsDiff(const std::vector<std::uint8_t> &in) {
|
||||
const std::uint32_t lo_a = Rd32(in, 0);
|
||||
const int hi_a = static_cast<int>(Rd32(in, 4));
|
||||
const std::uint32_t lo_b = Rd32(in, 8);
|
||||
const int hi_b = static_cast<int>(Rd32(in, 12));
|
||||
const std::uint32_t r = avs::node_absdiff_ge(lo_a, hi_a, lo_b, hi_b);
|
||||
PutHex(&r, sizeof(r));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int RunCpToLcid(const std::vector<std::uint8_t> &in) {
|
||||
const int cp = static_cast<int>(Rd32(in, 0));
|
||||
const std::uint32_t r = static_cast<std::uint32_t>(avs::node_cp_to_lcid(cp));
|
||||
PutHex(&r, sizeof(r));
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc < 2) {
|
||||
std::fprintf(stderr, "usage: recon_avs_w2 <mode>\n");
|
||||
return 2;
|
||||
}
|
||||
const std::vector<std::uint8_t> in = ReadHex();
|
||||
const std::string mode = argv[1];
|
||||
if (mode == "strncpy") {
|
||||
return RunStrncpy(in);
|
||||
}
|
||||
if (mode == "copybounded") {
|
||||
return RunCopyBounded(in);
|
||||
}
|
||||
if (mode == "memset") {
|
||||
return RunMemset(in);
|
||||
}
|
||||
if (mode == "absdiff") {
|
||||
return RunAbsDiff(in);
|
||||
}
|
||||
if (mode == "cptolcid") {
|
||||
return RunCpToLcid(in);
|
||||
}
|
||||
std::fprintf(stderr, "unknown mode %s\n", argv[1]);
|
||||
return 2;
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
#include "avs/leaf_ops_w2.h"
|
||||
|
||||
namespace avs {
|
||||
|
||||
// rva=0x10011ae0 (orig XCd229cc0000f6)
|
||||
int GK_CDECL node_strncpy(unsigned char *dst, const unsigned char *src, int n) {
|
||||
if (dst == nullptr || src == nullptr || n < 1) {
|
||||
return 0;
|
||||
}
|
||||
int i = 0;
|
||||
do {
|
||||
const unsigned char c = src[i];
|
||||
dst[i] = c;
|
||||
if (c == 0) {
|
||||
return i;
|
||||
}
|
||||
i = i + 1;
|
||||
} while (i < n);
|
||||
dst[n - 1] = 0;
|
||||
return i;
|
||||
}
|
||||
|
||||
// rva=0x10011960 (orig XCd229cc000063)
|
||||
int GK_CDECL node_copybounded(unsigned char *dst, int cap, const unsigned char *src, int cnt) {
|
||||
if (dst == nullptr || src == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
if (cap < 1) {
|
||||
return 0;
|
||||
}
|
||||
if (cnt < 1) {
|
||||
dst[0] = 0;
|
||||
return 0;
|
||||
}
|
||||
int i = 0;
|
||||
int term = 0;
|
||||
while (true) {
|
||||
const unsigned char c = src[i];
|
||||
dst[i] = c;
|
||||
if (c == 0) {
|
||||
term = (cap <= i) ? cap - 1 : i;
|
||||
break;
|
||||
}
|
||||
i = i + 1;
|
||||
cnt = cnt - 1;
|
||||
if (cap <= i) {
|
||||
term = cap - 1;
|
||||
break;
|
||||
}
|
||||
term = i;
|
||||
if (cnt <= 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
dst[term] = 0;
|
||||
return i;
|
||||
}
|
||||
|
||||
// rva=0x100110b0 (orig XCd229cc000087)
|
||||
void *GK_CDECL node_memset(void *p, unsigned char v, std::uint32_t n) {
|
||||
if (p == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
if (static_cast<int>(n) > 0) {
|
||||
auto *d = static_cast<unsigned char *>(p);
|
||||
for (std::uint32_t i = 0; i < n; i = i + 1) {
|
||||
d[i] = v;
|
||||
}
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
// rva=0x10012d00 (orig FUN_10012d00)
|
||||
std::uint32_t GK_CDECL node_absdiff_ge(std::uint32_t lo_a, int hi_a, std::uint32_t lo_b, int hi_b) {
|
||||
const std::uint32_t borrow = (lo_a < lo_b) ? 1u : 0u;
|
||||
const std::uint32_t high = static_cast<std::uint32_t>(hi_a - hi_b) - borrow;
|
||||
const std::uint32_t sign = static_cast<std::uint32_t>(static_cast<int>(high) >> 31);
|
||||
const std::uint32_t low_xor = (lo_a - lo_b) ^ sign;
|
||||
const std::uint32_t abs_high = ((high ^ sign) - sign) - ((low_xor < sign) ? 1u : 0u);
|
||||
const std::uint32_t abs_low = low_xor - sign;
|
||||
const std::uint32_t below = (abs_low < 15000u) ? 1u : 0u;
|
||||
if (static_cast<int>(abs_high) < static_cast<int>(below)) {
|
||||
return 0u;
|
||||
}
|
||||
return 1u;
|
||||
}
|
||||
|
||||
// rva=0x10002b40 (orig CPtoLCID)
|
||||
int node_cp_to_lcid(int cp) {
|
||||
if (cp == 0x3A4) {
|
||||
return 0x411;
|
||||
}
|
||||
if (cp == 0x3A8) {
|
||||
return 0x804;
|
||||
}
|
||||
if (cp == 0x3B5) {
|
||||
return 0x412;
|
||||
}
|
||||
if (cp == 0x3B6) {
|
||||
return 0x404;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace avs
|
||||
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "gk/types.h"
|
||||
|
||||
namespace avs {
|
||||
|
||||
// rva=0x10011ae0 (orig XCd229cc0000f6) node_strncpy
|
||||
int GK_CDECL node_strncpy(unsigned char *dst, const unsigned char *src, int n);
|
||||
|
||||
// rva=0x10011960 (orig XCd229cc000063) node_copybounded
|
||||
int GK_CDECL node_copybounded(unsigned char *dst, int cap, const unsigned char *src, int cnt);
|
||||
|
||||
// rva=0x100110b0 (orig XCd229cc000087) node_memset
|
||||
void *GK_CDECL node_memset(void *p, unsigned char v, std::uint32_t n);
|
||||
|
||||
// rva=0x10012d00 (orig FUN_10012d00) node_absdiff_ge
|
||||
std::uint32_t GK_CDECL node_absdiff_ge(std::uint32_t lo_a, int hi_a, std::uint32_t lo_b, int hi_b);
|
||||
|
||||
// rva=0x10002b40 (orig CPtoLCID) node_cp_to_lcid
|
||||
int node_cp_to_lcid(int cp);
|
||||
|
||||
} // namespace avs
|
||||
Reference in new issue
Block a user