Files
kdm-re/tools/unicorn_diff/recon_arkkdm_harness.cpp
T

317 lines
11 KiB
C++

// Differential harness for arkkdm.dll pure-leaf reconstructions. Reads a hex
// 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. One mode per reconstructed rva.
#include <cctype>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <unordered_map>
#include <vector>
#include "arkkdm/leaf_ops.h"
#include "arkkdm/dispatch_ops.h"
#include "arkkdm/platform/dispatch_target.h"
using namespace gk::arkkdm;
namespace {
constexpr std::size_t kObjSize = 0x300;
void PutHex(const void* data, std::size_t size);
std::uint32_t U32(const std::uint8_t* p);
// Trace offset for a dispatched slot index; mirrors prove_arkkdm.py TRACE so an
// injected-vtable stub (original side) and this test double write the same byte.
std::uint32_t TraceOff(int slot) {
switch (slot) {
case 0: return 0x380;
case 2: return 0x384;
case 4: return 0x388;
case 6: return 0x38c;
case 7: return 0x390;
case 12: return 0x394;
case 14: return 0x398;
case 41: return 0x39c;
default: return 0x3a0;
}
}
// Injected vtable test double: overrides only the eight recovered slots. Each
// records its dispatch by writing 0x51070000|slot into this object at TraceOff
// and returns a per-slot value the case configured, so a mis-dispatch changes a
// distinct observable byte (the differential's non-vacuity on the slot index).
struct TracingTarget : DispatchTarget {
std::uint32_t ret[42]{};
void Mark(int slot) {
std::uint32_t value = 0x51070000u | static_cast<std::uint32_t>(slot);
std::memcpy(reinterpret_cast<std::uint8_t*>(this) + TraceOff(slot), &value, 4);
}
std::uint32_t GK_THISCALL Slot00(std::uint32_t) override { Mark(0); return ret[0]; }
std::uint32_t GK_THISCALL Slot02(std::uint32_t) override { Mark(2); return ret[2]; }
std::uint32_t GK_THISCALL Slot04() override { Mark(4); return ret[4]; }
std::uint32_t GK_THISCALL Slot06() override { Mark(6); return ret[6]; }
std::uint32_t GK_THISCALL Slot07() override { Mark(7); return ret[7]; }
std::uint32_t GK_THISCALL Slot12() override { Mark(12); return ret[12]; }
std::uint32_t GK_THISCALL Slot14() override { Mark(14); return ret[14]; }
std::uint32_t GK_THISCALL Slot41() override { Mark(41); return ret[41]; }
};
std::uint32_t Field32(const void* obj, std::uint32_t off) {
std::uint32_t value = 0;
std::memcpy(&value, static_cast<const std::uint8_t*>(obj) + off, 4);
return value;
}
void SetPtr(TracingTarget* obj, std::uint32_t off, DispatchTarget* value) {
std::memcpy(reinterpret_cast<std::uint8_t*>(obj) + off, &value, sizeof(value));
}
int DispatchVtable(const std::string& mode, const std::vector<std::uint8_t>& in) {
if (mode == "dsp3770" || mode == "dsp37b0") {
TracingTarget self, sub;
const int slot = mode == "dsp3770" ? 4 : 6;
sub.ret[slot] = U32(in.data() + 1);
if (in.at(0)) SetPtr(&self, 0x300, &sub);
std::uint32_t r = mode == "dsp3770" ? CallVia300Slot10(&self)
: CallVia300Slot18(&self);
PutHex(&r, 4);
return 0;
}
if (mode == "dsp37d0") {
TracingTarget self, sub;
if (in.at(0)) SetPtr(&self, 0x300, &sub);
Release300(&self);
std::uint32_t out[3] = {Field32(&self, 0x300), Field32(&sub, 0x380),
Field32(&sub, 0x390)};
PutHex(out, sizeof(out));
return 0;
}
if (mode == "dsp3db0") {
TracingTarget self, sub;
if (in.at(0)) SetPtr(&self, 0x38, &sub);
CallVia38Slot18(&self);
std::uint32_t t6 = Field32(&sub, 0x38c);
PutHex(&t6, 4);
return 0;
}
if (mode == "dsp3dd0") {
TracingTarget self, sub;
if (in.at(0)) SetPtr(&self, 0x38, &sub);
Release38(&self);
std::uint32_t out[3] = {Field32(&self, 0x38), Field32(&sub, 0x390),
Field32(&sub, 0x384)};
PutHex(out, sizeof(out));
return 0;
}
if (mode == "dsp50c0") {
TracingTarget self;
reinterpret_cast<std::uint8_t*>(&self)[0x2f5] = in.at(0);
self.ret[12] = U32(in.data() + 10);
auto a = static_cast<std::int32_t>(U32(in.data() + 2));
auto b = static_cast<std::int32_t>(U32(in.data() + 6));
Activate(&self, in.at(1), &a, &b);
std::uint8_t out[17];
out[0] = reinterpret_cast<std::uint8_t*>(&self)[0x2f5];
std::uint32_t f8 = Field32(&self, 0x2f8), fc = Field32(&self, 0x2fc);
std::uint32_t t12 = Field32(&self, 0x394), t14 = Field32(&self, 0x398);
std::memcpy(out + 1, &f8, 4);
std::memcpy(out + 5, &fc, 4);
std::memcpy(out + 9, &t12, 4);
std::memcpy(out + 13, &t14, 4);
PutHex(out, sizeof(out));
return 0;
}
if (mode == "dsp5120") {
TracingTarget self;
reinterpret_cast<std::uint8_t*>(&self)[0x2f5] = in.at(0);
SetFlagF6Notify(&self, in.at(1));
std::uint8_t out[5];
out[0] = reinterpret_cast<std::uint8_t*>(&self)[0x2f6];
std::uint32_t t14 = Field32(&self, 0x398);
std::memcpy(out + 1, &t14, 4);
PutHex(out, sizeof(out));
return 0;
}
if (mode == "dsp5350") {
TracingTarget self;
self.ret[41] = U32(in.data());
std::int32_t out = 0;
QuerySlotA4(&self, &out);
std::uint8_t res[8];
std::memcpy(res, &out, 4);
std::uint32_t t41 = Field32(&self, 0x39c);
std::memcpy(res + 4, &t41, 4);
PutHex(res, sizeof(res));
return 0;
}
std::fprintf(stderr, "unknown dispatch mode %s\n", mode.c_str());
return 2;
}
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 U32(const std::uint8_t* p) {
std::uint32_t v = 0;
std::memcpy(&v, p, 4);
return v;
}
std::vector<std::uint8_t> Object(const std::vector<std::uint8_t>& in) {
std::vector<std::uint8_t> obj(kObjSize, 0);
std::memcpy(obj.data(), in.data(), kObjSize);
return obj;
}
int Dispatch(const std::string& mode, const std::vector<std::uint8_t>& in) {
if (mode == "clearfield30" || mode == "resetheader" || mode == "setflagf4") {
std::vector<std::uint8_t> obj = Object(in);
if (mode == "clearfield30") ClearField30(obj.data());
else if (mode == "resetheader") ResetHeader(obj.data());
else SetFlagF4(obj.data());
PutHex(obj.data(), kObjSize);
return 0;
}
if (mode == "getflagf4" || mode == "getflagf5" || mode == "getflagf6") {
std::vector<std::uint8_t> obj = Object(in);
std::uint8_t result = mode == "getflagf4" ? GetFlagF4(obj.data())
: mode == "getflagf5" ? GetFlagF5(obj.data())
: GetFlagF6(obj.data());
PutHex(&result, 1);
return 0;
}
if (mode.rfind("getfield", 0) == 0) {
using GetFieldFn = std::uint32_t(GK_THISCALL*)(const void*);
static const std::unordered_map<std::string, GetFieldFn> kGetField = {
{"getfield184", GetField184}, {"getfield190", GetField190},
{"getfield188", GetField188}, {"getfield18c", GetField18c},
{"getfield198", GetField198}, {"getfield21c", GetField21c},
{"getfield220", GetField220}, {"getfield224", GetField224},
{"getfield228", GetField228}, {"getfield22c", GetField22c},
{"getfield230", GetField230}, {"getfield234", GetField234},
{"getfield23c", GetField23c}, {"getfield240", GetField240},
{"getfield248", GetField248}, {"getfield24c", GetField24c},
{"getfield2d4", GetField2d4}, {"getfield2d8", GetField2d8},
{"getfield2dc", GetField2dc}, {"getfield2e0", GetField2e0},
};
const auto it = kGetField.find(mode);
if (it == kGetField.end()) {
std::fprintf(stderr, "unknown getfield mode %s\n", mode.c_str());
return 2;
}
std::vector<std::uint8_t> obj = Object(in);
std::uint32_t result = it->second(obj.data());
PutHex(&result, 4);
return 0;
}
if (mode == "getslot19c" || mode == "getslot254") {
std::vector<std::uint8_t> obj = Object(in);
auto index = static_cast<std::int32_t>(U32(in.data() + kObjSize));
std::uint32_t result = mode == "getslot19c" ? GetSlot19c(obj.data(), index)
: GetSlot254(obj.data(), index);
PutHex(&result, 4);
return 0;
}
if (mode == "setbytetrue" || mode == "setbytefalse") {
std::uint8_t flag = in.at(0);
if (mode == "setbytetrue") SetByteTrue(&flag);
else SetByteFalse(&flag);
PutHex(&flag, 1);
return 0;
}
if (mode == "netwait") {
std::uint32_t a = U32(in.data());
std::uint32_t b = U32(in.data() + 4);
std::uint32_t ret = arkNetworkGetPropertyWait(&a, &b);
std::uint8_t result[12];
std::memcpy(result, &a, 4);
std::memcpy(result + 4, &b, 4);
std::memcpy(result + 8, &ret, 4);
PutHex(result, sizeof(result));
return 0;
}
if (mode == "pausebg") {
std::uint32_t ret = arkPauseBackgroundProcess(U32(in.data()));
PutHex(&ret, 4);
return 0;
}
if (mode == "retzero2") {
std::uint32_t ret = ReturnZeroTwoArgs(U32(in.data()), U32(in.data() + 4));
PutHex(&ret, 4);
return 0;
}
if (mode == "clearsix") {
std::uint32_t dst[6];
std::memcpy(dst, in.data(), sizeof(dst));
ClearSixWords(0, dst);
PutHex(dst, sizeof(dst));
return 0;
}
if (mode == "testslot") {
std::vector<std::uint8_t> obj = Object(in);
auto index = static_cast<std::int32_t>(U32(in.data() + kObjSize));
std::uint8_t out = 0xAA;
std::uint32_t ret = TestObjectSlot(obj.data(), index, &out);
std::uint8_t result[5];
result[0] = out;
std::memcpy(result + 1, &ret, 4);
PutHex(result, sizeof(result));
return 0;
}
if (mode == "copyout") {
std::vector<std::uint8_t> obj = Object(in);
std::uint32_t dst[0x11];
CopyRecordOut(obj.data(), dst);
PutHex(dst, sizeof(dst));
return 0;
}
if (mode == "copyin") {
std::vector<std::uint8_t> obj = Object(in);
std::uint32_t src[0x11];
std::memcpy(src, in.data() + kObjSize, sizeof(src));
CopyRecordIn(obj.data(), src);
PutHex(obj.data() + 0x74, sizeof(src));
return 0;
}
if (mode.rfind("dsp", 0) == 0) {
return DispatchVtable(mode, in);
}
std::fprintf(stderr, "unknown mode %s\n", mode.c_str());
return 2;
}
} // namespace
int main(int argc, char** argv) {
if (argc < 2) {
std::fprintf(stderr, "usage: recon_arkkdm <mode>\n");
return 2;
}
return Dispatch(argv[1], ReadHex());
}