mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-16 15:59:59 -04:00
Profiler: unwind through vendor DLLs only while they are pinned, so slow-frame captures no longer crash at start.
NX2NAVI_PROFILE_SLOW unwound threads in any module, and one being unloaded while the game started (a driver or vendor DLL) had its unwind tables freed under the sampler. The exe and the system DLLs are walked freely; any other module is held with GetModuleHandleEx for the step, and one that is unloading ends the chain. The block-linear copy probe gains two cases of 256 KiB and more. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -108,6 +108,31 @@ static void count_stack(const uint64_t *rip, uint32_t depth) {
|
||||
stacks_dropped++;
|
||||
}
|
||||
|
||||
// Modules the unwinder walks through freely: the exe and system DLLs that stay loaded for the process's life. Any other
|
||||
// module (a driver or vendor DLL, which may load and unload while the game starts) is pinned while its frame unwinds,
|
||||
// and a module being unloaded ends the chain: unwinding one read its freed unwind tables and crashed the sampler
|
||||
// (NX2NAVI_PROFILE_SLOW at start). The unwind runs after ResumeThread, so taking the loader lock here is safe.
|
||||
static struct { uintptr_t lo, hi; } walk_mods[8];
|
||||
static int n_walk_mods;
|
||||
|
||||
static void walk_mods_init(void) {
|
||||
static const wchar_t *const sys[] = {L"ntdll.dll", L"KERNELBASE.dll", L"kernel32.dll", L"win32u.dll", L"ucrtbase.dll"};
|
||||
HMODULE h[6] = {GetModuleHandleW(NULL)};
|
||||
for (int i = 0; i < 5; i++) h[i + 1] = GetModuleHandleW(sys[i]);
|
||||
for (int i = 0; i < 6; i++) {
|
||||
MODULEINFO mi;
|
||||
if (h[i] && GetModuleInformation(GetCurrentProcess(), h[i], &mi, sizeof mi))
|
||||
walk_mods[n_walk_mods].lo = (uintptr_t)mi.lpBaseOfDll,
|
||||
walk_mods[n_walk_mods++].hi = (uintptr_t)mi.lpBaseOfDll + mi.SizeOfImage;
|
||||
}
|
||||
}
|
||||
|
||||
static int walkable(uint64_t rip) {
|
||||
for (int i = 0; i < n_walk_mods; i++)
|
||||
if (rip >= walk_mods[i].lo && rip < walk_mods[i].hi) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Unwinds the stack copied to stk_buf from real address lo (n bytes). Registers that point into the copied range are
|
||||
// moved into the copy, so frame-pointer frames read the copy too. Returns the number of frames.
|
||||
static uint32_t unwind_copy(CONTEXT *c, uintptr_t lo, size_t n, uint64_t *rip) {
|
||||
@@ -119,6 +144,9 @@ static uint32_t unwind_copy(CONTEXT *c, uintptr_t lo, size_t n, uint64_t *rip) {
|
||||
uint32_t d = 0;
|
||||
while (d < PROF_DEPTH && c->Rip && c->Rsp >= buf && c->Rsp + 8 <= end) {
|
||||
rip[d++] = c->Rip;
|
||||
HMODULE pin = NULL;
|
||||
if (!walkable(c->Rip) && !GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)(uintptr_t)c->Rip, &pin))
|
||||
break; // in no loaded module (or one being unloaded): the frame is kept, its callers are not
|
||||
DWORD64 base;
|
||||
PRUNTIME_FUNCTION f = RtlLookupFunctionEntry(c->Rip, &base, NULL);
|
||||
if (f) {
|
||||
@@ -129,6 +157,7 @@ static uint32_t unwind_copy(CONTEXT *c, uintptr_t lo, size_t n, uint64_t *rip) {
|
||||
c->Rip = *(DWORD64 *)c->Rsp;
|
||||
c->Rsp += 8;
|
||||
}
|
||||
if (pin) FreeLibrary(pin);
|
||||
}
|
||||
return d;
|
||||
}
|
||||
@@ -350,6 +379,7 @@ int prof_sampler_start(void) {
|
||||
exe_lo = (uintptr_t)mi.lpBaseOfDll;
|
||||
exe_hi = exe_lo + mi.SizeOfImage;
|
||||
}
|
||||
walk_mods_init();
|
||||
sampler = CreateThread(NULL, 0, sampler_main, NULL, 0, NULL);
|
||||
if (!sampler) {
|
||||
rt_log("prof: cannot start the sampler (error %lu)", GetLastError());
|
||||
|
||||
@@ -86,6 +86,9 @@ static int test_deswizzle(void) {
|
||||
for (unsigned td = 1; td <= 3; td++) // 3D: depth 5 against block depths 2, 4, 8
|
||||
for (unsigned th = 0; th <= 2; th++)
|
||||
if (check_swizzle(23 * 4, 19, 5, th, td, 4)) return 1;
|
||||
// levels of 256 KiB and more
|
||||
if (check_swizzle(1000 * 4, 300, 1, 4, 0, 4)) return 1;
|
||||
if (check_swizzle(97 * 16, 70, 9, 2, 2, 16)) return 1;
|
||||
|
||||
// Through a descriptor: RGBA8 2D with mip levels, level 1 and 2, layer 0, against the reference on the same geometry.
|
||||
nvn_texdesc d = {.target = 1, .format = 0x25, .width = 200, .height = 150, .depth = 1, .levels = 4};
|
||||
|
||||
Reference in New Issue
Block a user