Files
Neutron/tools/recon/original_startup_probe.c
T

117 lines
5.8 KiB
C

/* Diagnose the copied original's black startup under the isolated Wine wrapper.
Thread suspension perturbs timing: this is not a gameplay timing reference.
No code/actor memory writes; every successfully suspended thread is resumed. */
#define main observer_unused_main
#include "original_state_observer.c"
#undef main
#include <tlhelp32.h>
static void threads(HANDLE process, DWORD pid, unsigned elapsed) {
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
THREADENTRY32 entry = {0};
entry.dwSize = sizeof(entry);
if (snapshot == INVALID_HANDLE_VALUE) return;
if (Thread32First(snapshot, &entry)) do {
if (entry.th32OwnerProcessID != pid) continue;
HANDLE thread = OpenThread(THREAD_SUSPEND_RESUME|THREAD_GET_CONTEXT|THREAD_QUERY_INFORMATION,
FALSE, entry.th32ThreadID);
CONTEXT context = {0};
uint32_t stack[64] = {0};
int context_ok = 0, stack_ok = 0;
if (!thread) continue;
DWORD prior = SuspendThread(thread);
if (prior != (DWORD)-1) {
context.ContextFlags = CONTEXT_CONTROL;
context_ok = GetThreadContext(thread, &context);
if (context_ok) stack_ok = read_memory(process, context.Esp, stack, sizeof(stack));
/* Resume even when context/stack reads fail. Keep prior suspension count. */
DWORD resumed = ResumeThread(thread);
printf("{\"kind\":\"thread\",\"elapsed_ms\":%u,\"tid\":%lu,\"prior_suspend\":%lu,"
"\"resume_ok\":%s,\"context_ok\":%s,\"eip\":%lu,\"esp\":%lu,\"stack_ok\":%s,\"stack\":[",
elapsed, entry.th32ThreadID, prior, resumed!=(DWORD)-1?"true":"false",
context_ok?"true":"false", context.Eip, context.Esp, stack_ok?"true":"false");
for (unsigned i=0; stack_ok && i<sizeof(stack)/sizeof(stack[0]); ++i)
printf("%s%lu", i?",":"", (unsigned long)stack[i]);
puts("]}");
}
CloseHandle(thread);
} while (Thread32Next(snapshot, &entry));
CloseHandle(snapshot);
}
static void modules(DWORD pid) {
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid);
MODULEENTRY32 entry = {0};
entry.dwSize = sizeof(entry);
if (snapshot == INVALID_HANDLE_VALUE) return;
if (Module32First(snapshot, &entry)) do {
fputs("{\"kind\":\"module\",\"name\":", stdout); json_string(entry.szModule);
printf(",\"base\":%lu,\"size\":%lu}\n", (unsigned long)(uintptr_t)entry.modBaseAddr, entry.modBaseSize);
} while (Module32Next(snapshot, &entry));
CloseHandle(snapshot);
}
static BOOL CALLBACK close_window(HWND window, LPARAM pid) {
DWORD actual;
GetWindowThreadProcessId(window, &actual);
if (actual == (DWORD)pid && IsWindowVisible(window)) PostMessageA(window, WM_CLOSE, 0, 0);
return TRUE;
}
int main(int argc, char **argv) {
char cwd[MAX_PATH], digest[65], command[]="NeutronHDnoCD.exe";
STARTUPINFOA start = {0}; PROCESS_INFORMATION child = {0};
int acknowledge=0, standard=0;
unsigned duration=11;
for (int i=1; i<argc; ++i) {
if (!strcmp(argv[i],"--acknowledge-repaint")) acknowledge=1;
else if (!strcmp(argv[i],"--standard-resolution")) standard=1;
else if (!strncmp(argv[i],"--seconds=",10)) {
char *end; unsigned long n=strtoul(argv[i]+10,&end,10);
if (*end || n<3 || n>60) return 7;
duration=(unsigned)n;
} else if (strcmp(argv[i],"ASE\\jimstop.ase")) return 7;
}
const Profile *profile=&profiles[standard?3:2];
if (standard) strcpy(command,"NeutronNoCD.exe");
setbuf(stdout, NULL);
if (!GetCurrentDirectoryA(sizeof(cwd),cwd) || _stricmp(cwd,"Z:\\work\\game")) return 2;
HANDLE file = CreateFileA(command,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
if (file == INVALID_HANDLE_VALUE) return 3;
if (!sha256(file,digest) || strcmp(digest,profile->sha)) { CloseHandle(file); return 4; }
start.cb = sizeof(start);
if (!CreateProcessA(command,command,NULL,NULL,FALSE,0,NULL,cwd,&start,&child)) { CloseHandle(file); return 5; }
CloseHandle(file); CloseHandle(child.hThread);
printf("{\"kind\":\"diagnostic_subject\",\"sha256\":\"%s\",\"pid\":%lu,\"timing_perturbed\":true}\n",digest,child.dwProcessId);
DWORD began=GetTickCount(); int identified=0;
for (unsigned sample=0; GetTickCount()-began<duration*1000; ++sample) {
Sleep(1500);
identified=runtime_identity(child.hProcess,profile);
if (!identified) continue;
Registry state;
registry_snapshot(child.hProcess,profile,&state);
print_registry(&state,GetTickCount()-began);
print_sample(child.hProcess,profile,&state,GetTickCount()-began);
uint32_t code=0;
if (read_memory(child.hProcess,state.current+0x490,&code,4))
printf("{\"kind\":\"level_identity\",\"elapsed_ms\":%lu,\"code\":%lu}\n",GetTickCount()-began,(unsigned long)code);
modules(child.dwProcessId);
threads(child.hProcess,child.dwProcessId,GetTickCount()-began);
if (acknowledge && sample == 1) {
HWND window=find_window(child.dwProcessId);
RECT rect={0};
BOOL pending=window&&GetUpdateRect(window,&rect,FALSE);
BOOL acknowledged=window&&ValidateRect(window,NULL);
printf("{\"kind\":\"window_repaint_acknowledgement\",\"elapsed_ms\":%lu,\"window\":%lu,"
"\"pending\":%s,\"acknowledged\":%s,\"rect\":[%ld,%ld,%ld,%ld]}\n",
GetTickCount()-began,(unsigned long)(uintptr_t)window,
pending?"true":"false",acknowledged?"true":"false",rect.left,rect.top,rect.right,rect.bottom);
}
}
EnumWindows(close_window,child.dwProcessId);
CloseHandle(child.hProcess);
if (!identified) return 6;
puts("ORIGINAL_STARTUP_DIAGNOSTIC_COMPLETE");
return 0;
}