#define WIN32_LEAN_AND_MEAN #define NOMINMAX #include #include #include #include #include #include #include #include #include "compatibility.hpp" #include "policy.hpp" namespace compat { using Lookup = const ScriptRecord* (*)(const void*); void test_context(const ScriptRecord*, Lookup); void* test_bytes_adapter(); void* test_text_adapter(); void** test_original_bytes(); void** test_original_text(); } namespace { std::array records{}; volatile long bytes_calls{}, text_calls{}; const void* last_name{}; const void* last_content{}; const compat::ScriptRecord* resolve(const void* name) { if (name == records[0].path) return &records[0]; if (name == records[1].path) return &records[1]; return nullptr; } __declspec(noinline) bool bytes_fixture(const void* name, const void* content) { InterlockedIncrement(&bytes_calls); last_name=name; last_content=content; return content == reinterpret_cast(0x1234); } __declspec(noinline) bool text_fixture(const void* name, const void* content) { InterlockedIncrement(&text_calls); last_name=name; last_content=content; return content == reinterpret_cast(0x5678); } void check(bool good, const char* message) { if (!good) throw std::runtime_error(message); } void hook_ok(MH_STATUS status) { check(status == MH_OK, MH_StatusToString(status)); } } int main(int argc, char** argv) { try { using Fn = bool (*)(const void*, const void*); Fn volatile bytes = &bytes_fixture; Fn volatile text = &text_fixture; records[0].path=L"project/system/test.js"; records[1].path=L"puerts/test.js"; check(compat::is_project_script(L"project/settings.json"), "JSON allowed"); for (auto path : {L"project/../test.js", L"project/./test.js", L"project//test.js", L"project/test.js.map", L"project\\test.js", L"other/test.js", L"project/c:/test.js"}) check(!compat::is_project_script(path), "Invalid/out-of-scope path accepted"); check(!compat::contains_record(nullptr,records.data(),records.size()), "Null row"); check(!compat::contains_record(records.data()+records.size(),records.data(),records.size()), "Row beyond table"); check(!compat::contains_record(reinterpret_cast( reinterpret_cast(records.data())+1),records.data(),records.size()), "Unaligned row"); compat::test_context(records.data(),resolve); hook_ok(MH_Initialize()); if (argc == 2) { std::array prologues{}; std::ifstream file(argv[1],std::ios::binary); file.read(prologues.data(),prologues.size()); check(file.gcount()==static_cast(prologues.size()),"Cannot read validator prologues"); auto* memory=static_cast(VirtualAlloc(nullptr,4096,MEM_RESERVE|MEM_COMMIT,PAGE_EXECUTE_READWRITE)); check(memory!=nullptr,"Cannot allocate prologue fixture"); std::memcpy(memory,prologues.data(),64); std::memcpy(memory+128,prologues.data()+64,64); void* trampoline{}; // Decoder/relocation check only: game instructions are never executed. hook_ok(MH_CreateHook(memory,reinterpret_cast(&bytes_fixture),&trampoline)); hook_ok(MH_CreateHook(memory+128,reinterpret_cast(&text_fixture),&trampoline)); hook_ok(MH_RemoveHook(memory)); hook_ok(MH_RemoveHook(memory+128)); VirtualFree(memory,0,MEM_RELEASE); std::puts("PASS: MinHook accepts both actual game-validator prologues (not executed)"); } hook_ok(MH_CreateHook(reinterpret_cast(&bytes_fixture),compat::test_bytes_adapter(),compat::test_original_bytes())); hook_ok(MH_CreateHook(reinterpret_cast(&text_fixture),compat::test_text_adapter(),compat::test_original_text())); hook_ok(MH_QueueEnableHook(reinterpret_cast(&bytes_fixture))); hook_ok(MH_QueueEnableHook(reinterpret_cast(&text_fixture))); hook_ok(MH_ApplyQueued()); const void* changed = reinterpret_cast(0x9999); check(bytes(records[0].path,changed), "Changed project bytes rejected"); check(text(records[0].path,changed), "Changed project text rejected"); check(bytes_calls==0 && text_calls==0, "Project scripts reached original validators"); check(!bytes(records[1].path,changed), "Engine bytes bypassed"); check(!text(records[1].path,changed), "Engine text bypassed"); const wchar_t* unknown=L"project/not-listed.js"; check(!bytes(unknown,changed), "Unknown project bytes bypassed"); check(last_name==unknown && last_content==changed, "Byte arguments changed"); check(text(unknown,reinterpret_cast(0x5678)), "Original true result lost"); check(last_name==unknown && last_content==reinterpret_cast(0x5678), "Text arguments changed"); hook_ok(MH_QueueDisableHook(reinterpret_cast(&bytes_fixture))); hook_ok(MH_QueueDisableHook(reinterpret_cast(&text_fixture))); hook_ok(MH_ApplyQueued()); check(!bytes(records[0].path,changed), "Byte hook not removed"); check(!text(records[0].path,changed), "Text hook not removed"); hook_ok(MH_Uninitialize()); const auto log_directory=std::filesystem::current_path()/L"build"; check(!compat::install(log_directory),"Unsupported executable patched"); check(!bytes(records[0].path,changed),"Refused installation changed byte validator"); std::puts("PASS: unsupported executable refused without patching"); std::puts("PASS: native byte/text hooks, original forwarding, scope and hook removal"); return 0; } catch (const std::exception& error) { std::fprintf(stderr,"FAIL: %s\n",error.what()); return 1; } }