Normalize unlisted module paths with the game's pinned helper and release its FString output with the matching allocator. Keep engine and outside paths on original validation, log additional accepted modules, and test malformed paths and allocation cleanup through the production adapters. Update scope documentation and keep earlier gameplay results separate from a new DLL's pending game test. Native tests and offline verification pass.
153 lines
8.4 KiB
C++
153 lines
8.4 KiB
C++
#define WIN32_LEAN_AND_MEAN
|
|
#define NOMINMAX
|
|
#include <Windows.h>
|
|
#include <MinHook.h>
|
|
#include <array>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <stdexcept>
|
|
#include <cstdlib>
|
|
#include "compatibility.hpp"
|
|
#include "policy.hpp"
|
|
|
|
namespace compat {
|
|
using Lookup = const ScriptRecord* (*)(const void*);
|
|
void test_context(const ScriptRecord*, Lookup);
|
|
void test_normalizer(GameString* (*)(GameString*, const void*), void (*)(void*));
|
|
void* test_bytes_adapter();
|
|
void* test_text_adapter();
|
|
void** test_original_bytes();
|
|
void** test_original_text();
|
|
}
|
|
namespace {
|
|
std::array<compat::ScriptRecord, 241> records{};
|
|
volatile long bytes_calls{}, text_calls{};
|
|
const void* last_name{};
|
|
const void* last_content{};
|
|
long normalized_allocations{}, normalized_frees{};
|
|
int malformed{};
|
|
compat::GameString* normalize_fixture(compat::GameString* out, const void* input) {
|
|
const auto* path=static_cast<const wchar_t*>(input);
|
|
const auto size=wcslen(path)+1;
|
|
out->data=static_cast<wchar_t*>(std::malloc(size*sizeof(wchar_t)));
|
|
if (!out->data) throw std::bad_alloc();
|
|
std::memcpy(out->data,path,size*sizeof(wchar_t));
|
|
out->count=out->capacity=static_cast<std::int32_t>(size);
|
|
++normalized_allocations;
|
|
if (malformed==1) --out->count; // No terminating NUL at declared end.
|
|
if (malformed==2) out->count=-1;
|
|
if (malformed==3) out->capacity=0;
|
|
if (malformed==4 && size>8) out->data[7]=L'\0';
|
|
return out;
|
|
}
|
|
void free_fixture(void* value) { ++normalized_frees; std::free(value); }
|
|
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<const void*>(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<const void*>(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<const compat::ScriptRecord*>(
|
|
reinterpret_cast<const char*>(records.data())+1),records.data(),records.size()), "Unaligned row");
|
|
compat::test_context(records.data(),resolve);
|
|
hook_ok(MH_Initialize());
|
|
if (argc == 2) {
|
|
std::array<char,128> prologues{};
|
|
std::ifstream file(argv[1],std::ios::binary);
|
|
file.read(prologues.data(),prologues.size());
|
|
check(file.gcount()==static_cast<std::streamsize>(prologues.size()),"Cannot read validator prologues");
|
|
auto* memory=static_cast<char*>(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<void*>(&bytes_fixture),&trampoline));
|
|
hook_ok(MH_CreateHook(memory+128,reinterpret_cast<void*>(&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<void*>(&bytes_fixture),compat::test_bytes_adapter(),compat::test_original_bytes()));
|
|
hook_ok(MH_CreateHook(reinterpret_cast<void*>(&text_fixture),compat::test_text_adapter(),compat::test_original_text()));
|
|
hook_ok(MH_QueueEnableHook(reinterpret_cast<void*>(&bytes_fixture)));
|
|
hook_ok(MH_QueueEnableHook(reinterpret_cast<void*>(&text_fixture)));
|
|
hook_ok(MH_ApplyQueued());
|
|
const void* changed = reinterpret_cast<const void*>(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<const void*>(0x5678)), "Original true result lost");
|
|
check(last_name==unknown && last_content==reinterpret_cast<const void*>(0x5678), "Text arguments changed");
|
|
compat::test_normalizer(&normalize_fixture,&free_fixture);
|
|
for (const auto path : {L"project/mods/pbzcodexsilkcorsair.js",
|
|
L"project/mods/pbzcodexsilkcorsairbootstrap.js",L"project/extra/nested/new.js",
|
|
L"project/mods/settings.json"}) {
|
|
const auto before=bytes_calls+text_calls;
|
|
check(bytes(path,changed),"Additional project byte script rejected");
|
|
check(text(path,changed),"Additional project text script rejected");
|
|
check(bytes_calls+text_calls==before,"Additional project script reached original validators");
|
|
}
|
|
for (const auto path : {L"puerts/new.js", L"engine/new.js", L"project/../new.js",
|
|
L"project//new.js",L"project/./new.js",L"project/mods/new.js.map",
|
|
L"C:/outside/new.js"}) {
|
|
check(!bytes(path,changed),"Outside/invalid byte path bypassed");
|
|
check(last_name==path && last_content==changed,"Rejected byte arguments changed");
|
|
check(text(path,reinterpret_cast<const void*>(0x5678)),"Rejected text original result lost");
|
|
}
|
|
for (malformed=1; malformed<=4; ++malformed) {
|
|
check(!bytes(unknown,changed),"Malformed normalized FString accepted");
|
|
check(!text(unknown,changed),"Malformed normalized text FString accepted");
|
|
}
|
|
malformed=0;
|
|
check(normalized_allocations==normalized_frees,"Normalized strings leaked or used wrong allocator");
|
|
std::puts("PASS: additional JS/JSON modules, scope restrictions and paired game-allocation cleanup");
|
|
hook_ok(MH_QueueDisableHook(reinterpret_cast<void*>(&bytes_fixture)));
|
|
hook_ok(MH_QueueDisableHook(reinterpret_cast<void*>(&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;
|
|
}
|
|
}
|