Snapshot for release 2026.10.31.4 (internal 34cea02)

This commit is contained in:
Tom Bohar 2026-11-01 18:47:28 -05:00
commit b1c182b05d
114 changed files with 8691 additions and 452 deletions

View file

@ -29,6 +29,9 @@
// init export returns (HooksFlush; on a first launch AC's main thread is still suspended
// then), on every reload, and ~20 ms after the last change otherwise (a background
// flusher). A hook's slot is set at once; its patch follows within that window.
// Each freeze runs with the process in HIGH priority class (BoostForFreeze): on a busy
// machine the freeze otherwise waited seconds on low-priority threads, with AC's main
// thread suspended all that time (2026-11-01).
// ============================================================================
#include "Loader.h"
@ -84,6 +87,8 @@ static HANDLE g_dirtyEvent;
static volatile DWORD g_lastChange;
static void FlushLocked(const char* why);
static DWORD BoostForFreeze(void);
static void RestoreAfterFreeze(DWORD cls);
static DWORD WINAPI Flusher(LPVOID unused)
{
@ -143,7 +148,9 @@ static void SetPatched(int i, int patched)
{
HookEntry* e = &g_hooks[i];
if (e->patched == patched) return;
DWORD cls = BoostForFreeze(); // each call is a freeze of its own
int st = patched ? p_enable(e->target) : p_disable(e->target);
RestoreAfterFreeze(cls);
if (st == MH_OK) e->patched = patched;
else LoaderLog("ERR", "Hooks: MinHook %s %p failed: %d", patched ? "enable" : "disable", e->target, st);
}
@ -162,6 +169,30 @@ static void Want(int i, int patched)
SetEvent(g_dirtyEvent);
}
// Raises the process to HIGH_PRIORITY_CLASS for one MinHook freeze; returns the class to put
// back (0 = left as it was). MinHook's freeze suspends the process's threads one by one and reads
// each one's context (GetThreadContext), and that read waits until the suspended thread itself
// gets a CPU to run the kernel's get-context APC. A LOWEST or BELOW_NORMAL thread (AC's d3d9
// workers, the engine's heartbeat) on a machine saturated by normal-priority work (every client
// hot reloading at once) waits seconds for that, while the threads already suspended, AC's main
// thread first among them, stay frozen: the 4-7 s "applied in one freeze" lines and main-thread
// hangs of the 2026-10-31/11-01 reloads. In HIGH class a LOWEST thread is at 11, above every
// normal process, so the reads come back at once (tools\LoaderFreezeBench measures both). Every
// other thread is suspended for most of the window, so the boost doesn't let this process take
// CPU from the other clients beyond the freeze itself.
static DWORD BoostForFreeze(void)
{
HANDLE self = GetCurrentProcess();
DWORD cls = GetPriorityClass(self);
if (cls == 0 || cls == HIGH_PRIORITY_CLASS || cls == REALTIME_PRIORITY_CLASS) return 0;
return SetPriorityClass(self, HIGH_PRIORITY_CLASS) ? cls : 0;
}
static void RestoreAfterFreeze(DWORD cls)
{
if (cls != 0) SetPriorityClass(GetCurrentProcess(), cls);
}
// Applies every pending patch/unpatch in one MinHook freeze. (Caller holds g_lock.)
static void FlushLocked(const char* why)
{
@ -176,7 +207,9 @@ static void FlushLocked(const char* why)
}
if (n == 0) return;
DWORD t0 = GetTickCount();
DWORD cls = BoostForFreeze();
int st = p_applyQueued();
RestoreAfterFreeze(cls);
if (st == MH_OK) {
for (int k = 0; k < n; k++) g_hooks[queued[k]].patched = g_hooks[queued[k]].want;
LoaderLog("INF", "Hooks: %d patch(es) applied in one freeze (%lu ms, %s).", n, GetTickCount() - t0, why);

View file

@ -0,0 +1,35 @@
/*
* DumpArgs.h - the command line of RC_DumpProcess (SehTrampoline.c): "<pid> <dump path>", the
* path being the rest of the line (spaces kept, trailing spaces and a closing quote dropped).
* The engine writes it (src\RynthCore.Engine\HangDumpCommand.cs). Pure: tests\DumpArgsTests.c.
*/
#pragma once
#include <wchar.h>
/* 1 when `cmd` holds a pid above 0 and a non-empty path that fits `cap` characters (with its 0). */
static int ParseDumpArgs(const wchar_t* cmd, unsigned long* pid, wchar_t* path, int cap)
{
unsigned long p = 0;
int n = 0;
if (!cmd || !pid || !path || cap < 2) return 0;
path[0] = 0;
while (*cmd == L' ' || *cmd == L'\t') cmd++;
if (*cmd < L'0' || *cmd > L'9') return 0;
while (*cmd >= L'0' && *cmd <= L'9') {
unsigned long d = (unsigned long)(*cmd - L'0');
if (p > (4294967295UL - d) / 10) return 0; /* would overflow */
p = p * 10 + d;
cmd++;
}
if (*cmd != L' ' && *cmd != L'\t') return 0;
while (*cmd == L' ' || *cmd == L'\t') cmd++;
if (*cmd == L'"') cmd++;
while (*cmd && *cmd != L'"') {
if (n >= cap - 1) return 0; /* too long: refuse rather than cut the name */
path[n++] = *cmd++;
}
while (n > 0 && (path[n - 1] == L' ' || path[n - 1] == L'\t')) n--;
path[n] = 0;
*pid = p;
return p != 0 && n > 0;
}

View file

@ -27,7 +27,9 @@
#include <windows.h>
/* File version of this native module (bump with every behavioural change). */
#define RC_SEH_TRAMPOLINE_VERSION 8 /* v8: noise repeats only at powers of two, capped relogs per address,
#define RC_SEH_TRAMPOLINE_VERSION 9 /* v9: RC_DumpProcess, the hang dump written from rundll32 (out of
the client's process);
v8: noise repeats only at powers of two, capped relogs per address,
a total byte budget for native-crash.log (CrashLogFilter.h);
v7: stack sweeps bounded by this thread's stack (TEB), never
reading at or past StackBase;
@ -1008,3 +1010,47 @@ SEH_GameTickThiscall(void* fn, void* this_ptr, int* out_result,
RC_SEH_LEAVE();
return 1;
}
/* ── Out-of-process minidump (rundll32 entry) ─────────────────────────────
* The engine's hang watchdog runs
* %windir%\SysWOW64\rundll32.exe "<this dll>",RC_DumpProcess <pid> <dump path>
* so the dump of a hung client is written from another process: MiniDumpWriteDump on the
* client itself suspends every other thread and then needs the process heap, which a
* suspended thread may hold (2026-10-31: a client deadlocked for good that way). dbghelp comes
* from the system folder, not AC's own 2003 copy. The process exit code says how it went:
* 0 written, 1 bad arguments, 2 OpenProcess, 3 CreateFile, 4 dbghelp, 5 MiniDumpWriteDump
* (the file is deleted). Nothing runs in the client's process. */
#include "DumpArgs.h"
typedef BOOL (WINAPI *Fn_MiniDumpWriteDump)(HANDLE, DWORD, HANDLE, DWORD, void*, void*, void*);
static UINT DumpProcessCore(const wchar_t* cmd)
{
unsigned long pid = 0;
wchar_t path[MAX_PATH];
if (!ParseDumpArgs(cmd, &pid, path, MAX_PATH)) return 1;
HANDLE proc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_DUP_HANDLE, FALSE, pid);
if (!proc) return 2;
HANDLE file = CreateFileW(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == INVALID_HANDLE_VALUE) { CloseHandle(proc); return 3; }
HMODULE dbghelp = LoadLibraryExW(L"dbghelp.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
Fn_MiniDumpWriteDump write = dbghelp ? (Fn_MiniDumpWriteDump)GetProcAddress(dbghelp, "MiniDumpWriteDump") : NULL;
UINT rc = 4;
if (write) {
/* MiniDumpNormal (every thread's stack) | WithHandleData | WithUnloadedModules | WithThreadInfo */
rc = write(proc, pid, file, 0x00000000 | 0x00000004 | 0x00000020 | 0x00001000, NULL, NULL, NULL) ? 0 : 5;
}
CloseHandle(file);
CloseHandle(proc);
if (rc != 0) DeleteFileW(path);
return rc;
}
__declspec(dllexport) void CALLBACK RC_DumpProcessW(HWND hwnd, HINSTANCE inst, LPWSTR cmdLine, int show)
{
(void)hwnd; (void)inst; (void)show;
ExitProcess(DumpProcessCore(cmdLine));
}
/* rundll32 looks the entry up by its plain name (it tries RC_DumpProcessW first). */
#pragma comment(linker, "/EXPORT:RC_DumpProcessW=_RC_DumpProcessW@16")

View file

@ -0,0 +1,47 @@
/*
* DumpArgsTests.c - offline checks for DumpArgs.h (RC_DumpProcess's command line, the hang
* dump written from rundll32). Build and run with Run-DumpArgsTests.ps1 (x86 MSVC). Exit 0 = pass.
*/
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include "../DumpArgs.h"
static int g_checks, g_failed;
static void check(int ok, const char* what)
{
g_checks++;
if (ok) return;
g_failed++;
printf("[FAIL] %s\n", what);
}
int main(void)
{
unsigned long pid;
wchar_t path[260];
/* What HangDumpCommand.Arguments leaves for the entry (rundll32 passes the text after the entry name). */
check(ParseDumpArgs(L"17132 C:\\Games\\RynthCore\\Logs\\dumps\\hang_17132_20261031_211714.dmp", &pid, path, 260)
&& pid == 17132 && wcscmp(path, L"C:\\Games\\RynthCore\\Logs\\dumps\\hang_17132_20261031_211714.dmp") == 0,
"pid and path");
check(ParseDumpArgs(L" 42 C:\\Users\\Some One's PC\\a b.dmp ", &pid, path, 260)
&& pid == 42 && wcscmp(path, L"C:\\Users\\Some One's PC\\a b.dmp") == 0,
"spaces and an apostrophe in the path are kept, the ends trimmed");
check(ParseDumpArgs(L"7 \"C:\\x y\\z.dmp\"", &pid, path, 260) && pid == 7 && wcscmp(path, L"C:\\x y\\z.dmp") == 0,
"a quoted path loses its quotes");
check(!ParseDumpArgs(L"", &pid, path, 260), "empty refused");
check(!ParseDumpArgs(NULL, &pid, path, 260), "null refused");
check(!ParseDumpArgs(L"123", &pid, path, 260), "no path refused");
check(!ParseDumpArgs(L"0 C:\\a.dmp", &pid, path, 260), "pid 0 refused");
check(!ParseDumpArgs(L"x C:\\a.dmp", &pid, path, 260), "no pid refused");
check(!ParseDumpArgs(L"12x C:\\a.dmp", &pid, path, 260), "a pid run into text refused");
check(!ParseDumpArgs(L"99999999999 C:\\a.dmp", &pid, path, 260), "an overflowing pid refused");
check(ParseDumpArgs(L"4294967295 C:\\a.dmp", &pid, path, 260) && pid == 4294967295UL, "the largest pid");
check(!ParseDumpArgs(L"5 C:\\abcdefgh.dmp", &pid, path, 8), "a path longer than the buffer refused, not cut");
check(ParseDumpArgs(L"5 C:\\a.dmp", &pid, path, 9) && wcscmp(path, L"C:\\a.dmp") == 0, "a path that just fits");
printf("%d check(s), %d failed.\n", g_checks, g_failed);
return g_failed == 0 ? 0 : 1;
}

View file

@ -0,0 +1,28 @@
# Run-DumpArgsTests.ps1
# Builds and runs DumpArgsTests.c (x86 MSVC, the same toolchain as the DLL).
# -OutDir: where the test exe goes (default: native\SehTrampoline\tests\bin). Exit 0 = pass.
param([string]$OutDir = (Join-Path $PSScriptRoot "bin"))
$ErrorActionPreference = "Stop"
$vsPath = & "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" `
-latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 `
-property installationPath 2>$null
if (-not $vsPath) { Write-Error "Visual Studio with C++ tools not found."; exit 1 }
$vcVer = (Get-ChildItem "$vsPath\VC\Tools\MSVC" | Sort-Object Name -Descending | Select-Object -First 1).Name
$vcBase = "$vsPath\VC\Tools\MSVC\$vcVer"
$cl = "$vcBase\bin\Hostx86\x86\cl.exe"
$sdkVer = (Get-ChildItem "C:\Program Files (x86)\Windows Kits\10\Include" | Sort-Object Name -Descending | Select-Object -First 1).Name
$sdkInc = "C:\Program Files (x86)\Windows Kits\10\Include\$sdkVer"
$sdkLib = "C:\Program Files (x86)\Windows Kits\10\Lib\$sdkVer"
$env:INCLUDE = "$vcBase\include;$sdkInc\ucrt;$sdkInc\um;$sdkInc\shared"
$env:LIB = "$vcBase\lib\x86;$sdkLib\ucrt\x86;$sdkLib\um\x86"
New-Item -ItemType Directory -Path $OutDir -Force | Out-Null
$exe = Join-Path $OutDir "DumpArgsTests.exe"
& $cl /nologo /W4 /WX "$PSScriptRoot\DumpArgsTests.c" /Fo"$OutDir\\" /Fe"$exe" /link /MACHINE:X86
if ($LASTEXITCODE -ne 0) { Write-Error "Compilation failed."; exit $LASTEXITCODE }
& $exe
exit $LASTEXITCODE