fix(host): the samplers' stack walks stay on the walked thread's stack, so HOS_PROFILE=1 no longer crashes the game on Windows #1343

Open
dyst wants to merge 1 commit from dyst/Longshot:fix/sampler-stack-walk into main
Member

Type: fix · Risk: low · Tested on: Windows 11, RTX 3080 (NVIDIA 621.14) · Needs: none

Why

On Windows the profiling mode (HOS_PROFILE=1, #1279) can crash the game: its all-thread sampler walks the stack of each thread it stops, and when a thread is stopped where its stack can't be walked, the walk reads memory that isn't there and takes the game down. With the mode sampling from the boot and listing the threads again every 5 s, that happened about 2 minutes into the first Windows run. Fixes #383.

How to reproduce

Seen on: Windows 11, RTX 3080, NVIDIA 621.14 (main @ 8e9cfc3a); #383 saw it on an RX 6700 XT before #1279
Settings and switches: HOS_PROFILE=1 (or HOS_SAMPLE_ALL=<file> with <file>.go), headless, the kokiri-start save
Steps: play; on main @ 8e9cfc3a the game closed after 135 s of play, in Kokiri Forest
Expected: the run goes on, and the profile folder holds the whole session
Actual: CRASH: exception 0xc0000005 (read 0x438, ...) at ntdll.dll+0x21580, from longshot.exe+0x1c6fc27c, which is sample_all_thread +0xb2c

What changed

Every stack walk in host/mkw_glue.c goes one frame at a time through one helper that only reads the walked thread's own stack. A frame it can't walk ends that sample's stack there, and the game goes on.

  • host: unwind_step() in host/mkw_glue.c is the one call of RtlVirtualUnwind. Before it reads anything, it checks that the stack pointer is on the walked thread's committed stack, and the frame register too when the function's unwind data names one. After the step it checks the new stack pointer. A step outside ends the walk. A RUNTIME_FUNCTION entry chained to another is followed.

  • host: thread_stack() gets a suspended thread's stack limits (its TEB's StackLimit to StackBase) with NtQueryInformationThread, looked up from ntdll at run time, so the link line is unchanged. own_stack() gets the current thread's limits, for the crash report.

  • host: the three walks use it: report_crash's host backtrace, sample_thread (HOS_SAMPLE) and sample_all_thread (HOS_SAMPLE_ALL, the profiling mode). Their output format is unchanged.

  • Why checks and not a guard: the host is C, and this clang takes __try/__except in C only with -fms-extensions, which the host isn't built with (the renderer's __try is in C++). So the walk can't fault, rather than catching the fault.

  • tests: tests/test_sampler_stack_walk.py (new):

    • RtlVirtualUnwind and RtlLookupFunctionEntry are called only in unwind_step;
    • the stack pointer is checked before the unwind data is read, the frame register before the unwind, the result after, and chained entries are followed;
    • the three walks step through it on their thread's stack;
    • where the compiler builds for Windows, the helpers compile on their own (syntax only, nothing run).

    It fails against main's mkw_glue.c.

Measurements

Verdict: pass. With the fix, the same profiled session ran 454 s of play to a clean stop, where main crashed at 135 s. The stacks the sampler writes are as deep as before.

Hardware: Windows 11 Pro 22631, RTX 3080 (NVIDIA 621.14), Ryzen 9 3900X (12 cores, 24 threads), 64 GB
Scene: kokiri-start, Kokiri Forest; 1920x1080, no upscaler, no frame generation; headless
Method: one HOS_PROFILE=1 session each, started through scripts/longshot_ctl.py launch --headless with heart protection, then teleports with walks and camera swings. On main the session reached Kokiri and died. With the fix, a Kokiri cutscene kept refusing the teleports, so the session sampled Kokiri until stopped by hand. Stack depth is frames per sample over each run's first 100 s of play, from its all.txt.
How to reproduce: a scripts/dev_packs.py base of 8e9cfc3a, a pack of this branch over it, then the same launch with HOS_PROFILE=1 on each runtime

Before: main @ 8e9cfc3a After: this PR @ 41e601e0 (on main 544229a2)
Play under HOS_PROFILE=1 crashed at 135 s (15,347 frames) 454 s, stopped by hand (33,444 frames)
CRASH: lines 1 0
Samples written 1,902,180 5,976,744
Game threads: frames per sample (first 100 s) 12.80 12.67
Game threads: stacks at the walk's 16-frame cap 14.0 % 10.9 %
Game and renderer threads: stacks of 1 frame 0.1 % / 0.4 % 0.1 % / 0.4 %
Renderer threads: frames per sample 9.90 9.90
before: [hos host] CRASH: exception 0xc0000005 (read 0x438, guest 0xfffff00000000438) at ntdll.dll+0x21580
before: [hos host] CRASH: host backtrace: ntdll.dll+0x21580 ntdll.dll+0x20fba ntdll.dll+0x20d2b KERNEL32.DLL+0x13bf8 longshot.exe+0x1c6fc27c KERNEL32.DLL+0x1259d ntdll.dll+0x5af78
after:  profile-20261116-211517: 33444 frames, 454 s of play, 5976744 sample lines; no CRASH line in the run's log

Why main @ 8e9cfc3a, not the current 544229a2: it is the base the pack was built over, and the numbers still hold. Of the 6 commits since, only #1291 touches host/mkw_glue.c, in hos_alloc_failed, not a sampler or the crash report's walk, and none touches host/profiling.c or host/posix/prof.c.

The crash depends on where a thread is stopped (#383 saw it in 1 of 8 runs before the profiling mode). One run each shows that the walk survives what main's didn't, not a rate. The deep-stack share fell 3 points. That could be the frame-register check ending some walks, or simply the different scene (a cutscene against main's walk). The 1-frame share is unchanged, so no walk is cut at its first frame, and the stack limits are read right.

Flags and settings

None

Testing

Tested:

  • Windows 11, RTX 3080, NVIDIA 621.14, in the game: the profiled session above, 454 s of play, no crash, every profile file written. Built as a dev_packs.py pack of this branch (longshot.exe and longshot_vk.dll) over a base of main @ 8e9cfc3a.
  • Checks, on Windows:
    • tests/test_sampler_stack_walk.py --cc <llvm-mingw clang> passes, and fails against main's mkw_glue.c.
    • The checks that read host/mkw_glue.c, through run_all.py: test_host_alloc_wait, test_input_background, test_nvn_host_tables and test_commit_limit pass. test_crash_record, test_slow_frames, test_profiling_mode and test_renderer_new_handler fail with the same message on main @ 544229a2: void hos_prof_start(void) is in host/posix/prof.c twice, and TDM gcc fails test_crash_record's stubs. That's 8 checks, not the whole suite.
  • Linux: host/mkw_glue.c compiled with gcc 14 (Docker, -fsyntax-only -D_GNU_SOURCE). Every changed line is inside #ifdef _WIN32. The only error was line 967's poll field, which is newer than the lift headers I compiled against and outside this change.

Not tested: the HOS_SAMPLE sampler and the crash report's backtrace in the game (the same helper, and their checks only), and AMD and Intel GPUs (the walk doesn't depend on the GPU).

Must be tested: None

Risks

What could break: a sampled stack, or a crash report's host backtrace, ending earlier than before where a frame would have read outside its thread's stack. That only affects the profiling tools and the report's backtrace, and players never run the profilers. #1044 (WIP) changes the same sample_all_thread, for its samples' timestamps: whichever lands second resolves a small textual overlap.

Turning it off: revert the commit

Compatibility: Nothing changes

Issues

Fixes #383

Checklist

Everyone

  • One topic.
  • The title is a Conventional Commit (type(scope): description) of the status line's type.
  • Why gives the reason plainly and links the issues that fit (the tracker searched).
  • Measurements has a verdict, or one line saying why there is nothing to measure.
  • Testing names what was tested, what wasn't, and what must still be tested, where and how.
  • Rebased on current main.
  • python -I -B tests/run_all.py passes (any failure that also fails on main is named).
  • No game files, dumps, build output or generated files.
  • third_party/ untouched (a new folder with a PROVENANCE.md instead).
  • The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled.

When it applies

  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.
  • A game-free test added for the fix, failing on main and passing here.
  • Host code builds on both Windows (llvm-mingw) and Linux, with UTF-8 paths.

🤖 Generated with Claude Code

**Type:** fix · **Risk:** low · **Tested on:** Windows 11, RTX 3080 (NVIDIA 621.14) · **Needs:** none ## Why On Windows the profiling mode (`HOS_PROFILE=1`, #1279) can crash the game: its all-thread sampler walks the stack of each thread it stops, and when a thread is stopped where its stack can't be walked, the walk reads memory that isn't there and takes the game down. With the mode sampling from the boot and listing the threads again every 5 s, that happened about 2 minutes into the first Windows run. Fixes #383. ## How to reproduce **Seen on:** Windows 11, RTX 3080, NVIDIA 621.14 (main @ 8e9cfc3a); #383 saw it on an RX 6700 XT before #1279 **Settings and switches:** `HOS_PROFILE=1` (or `HOS_SAMPLE_ALL=<file>` with `<file>.go`), headless, the `kokiri-start` save **Steps:** play; on main @ 8e9cfc3a the game closed after 135 s of play, in Kokiri Forest **Expected:** the run goes on, and the profile folder holds the whole session **Actual:** `CRASH: exception 0xc0000005 (read 0x438, ...) at ntdll.dll+0x21580`, from `longshot.exe+0x1c6fc27c`, which is `sample_all_thread` +0xb2c ## What changed Every stack walk in `host/mkw_glue.c` goes one frame at a time through one helper that only reads the walked thread's own stack. A frame it can't walk ends that sample's stack there, and the game goes on. - host: `unwind_step()` in `host/mkw_glue.c` is the one call of `RtlVirtualUnwind`. Before it reads anything, it checks that the stack pointer is on the walked thread's committed stack, and the frame register too when the function's unwind data names one. After the step it checks the new stack pointer. A step outside ends the walk. A `RUNTIME_FUNCTION` entry chained to another is followed. - host: `thread_stack()` gets a suspended thread's stack limits (its TEB's `StackLimit` to `StackBase`) with `NtQueryInformationThread`, looked up from ntdll at run time, so the link line is unchanged. `own_stack()` gets the current thread's limits, for the crash report. - host: the three walks use it: `report_crash`'s host backtrace, `sample_thread` (`HOS_SAMPLE`) and `sample_all_thread` (`HOS_SAMPLE_ALL`, the profiling mode). Their output format is unchanged. - Why checks and not a guard: the host is C, and this clang takes `__try`/`__except` in C only with `-fms-extensions`, which the host isn't built with (the renderer's `__try` is in C++). So the walk can't fault, rather than catching the fault. - tests: `tests/test_sampler_stack_walk.py` (new): - `RtlVirtualUnwind` and `RtlLookupFunctionEntry` are called only in `unwind_step`; - the stack pointer is checked before the unwind data is read, the frame register before the unwind, the result after, and chained entries are followed; - the three walks step through it on their thread's stack; - where the compiler builds for Windows, the helpers compile on their own (syntax only, nothing run). It fails against main's `mkw_glue.c`. ## Measurements **Verdict:** pass. With the fix, the same profiled session ran 454 s of play to a clean stop, where main crashed at 135 s. The stacks the sampler writes are as deep as before. Hardware: Windows 11 Pro 22631, RTX 3080 (NVIDIA 621.14), Ryzen 9 3900X (12 cores, 24 threads), 64 GB Scene: `kokiri-start`, Kokiri Forest; 1920x1080, no upscaler, no frame generation; headless Method: one `HOS_PROFILE=1` session each, started through `scripts/longshot_ctl.py launch --headless` with heart protection, then teleports with walks and camera swings. On main the session reached Kokiri and died. With the fix, a Kokiri cutscene kept refusing the teleports, so the session sampled Kokiri until stopped by hand. Stack depth is frames per sample over each run's first 100 s of play, from its `all.txt`. How to reproduce: a `scripts/dev_packs.py` base of 8e9cfc3a, a pack of this branch over it, then the same launch with `HOS_PROFILE=1` on each runtime | | Before: main @ 8e9cfc3a | After: this PR @ 41e601e0 (on main 544229a2) | |---|---|---| | Play under `HOS_PROFILE=1` | crashed at 135 s (15,347 frames) | 454 s, stopped by hand (33,444 frames) | | `CRASH:` lines | 1 | 0 | | Samples written | 1,902,180 | 5,976,744 | | Game threads: frames per sample (first 100 s) | 12.80 | 12.67 | | Game threads: stacks at the walk's 16-frame cap | 14.0 % | 10.9 % | | Game and renderer threads: stacks of 1 frame | 0.1 % / 0.4 % | 0.1 % / 0.4 % | | Renderer threads: frames per sample | 9.90 | 9.90 | ``` before: [hos host] CRASH: exception 0xc0000005 (read 0x438, guest 0xfffff00000000438) at ntdll.dll+0x21580 before: [hos host] CRASH: host backtrace: ntdll.dll+0x21580 ntdll.dll+0x20fba ntdll.dll+0x20d2b KERNEL32.DLL+0x13bf8 longshot.exe+0x1c6fc27c KERNEL32.DLL+0x1259d ntdll.dll+0x5af78 after: profile-20261116-211517: 33444 frames, 454 s of play, 5976744 sample lines; no CRASH line in the run's log ``` Why main @ 8e9cfc3a, not the current 544229a2: it is the base the pack was built over, and the numbers still hold. Of the 6 commits since, only #1291 touches `host/mkw_glue.c`, in `hos_alloc_failed`, not a sampler or the crash report's walk, and none touches `host/profiling.c` or `host/posix/prof.c`. The crash depends on where a thread is stopped (#383 saw it in 1 of 8 runs before the profiling mode). One run each shows that the walk survives what main's didn't, not a rate. The deep-stack share fell 3 points. That could be the frame-register check ending some walks, or simply the different scene (a cutscene against main's walk). The 1-frame share is unchanged, so no walk is cut at its first frame, and the stack limits are read right. ### Flags and settings None ## Testing **Tested:** - Windows 11, RTX 3080, NVIDIA 621.14, in the game: the profiled session above, 454 s of play, no crash, every profile file written. Built as a `dev_packs.py` pack of this branch (`longshot.exe` and `longshot_vk.dll`) over a base of main @ 8e9cfc3a. - Checks, on Windows: - `tests/test_sampler_stack_walk.py --cc <llvm-mingw clang>` passes, and fails against main's `mkw_glue.c`. - The checks that read `host/mkw_glue.c`, through `run_all.py`: `test_host_alloc_wait`, `test_input_background`, `test_nvn_host_tables` and `test_commit_limit` pass. `test_crash_record`, `test_slow_frames`, `test_profiling_mode` and `test_renderer_new_handler` fail with the same message on main @ 544229a2: `void hos_prof_start(void)` is in `host/posix/prof.c` twice, and TDM gcc fails `test_crash_record`'s stubs. That's 8 checks, not the whole suite. - Linux: `host/mkw_glue.c` compiled with gcc 14 (Docker, `-fsyntax-only -D_GNU_SOURCE`). Every changed line is inside `#ifdef _WIN32`. The only error was line 967's `poll` field, which is newer than the lift headers I compiled against and outside this change. **Not tested:** the `HOS_SAMPLE` sampler and the crash report's backtrace in the game (the same helper, and their checks only), and AMD and Intel GPUs (the walk doesn't depend on the GPU). **Must be tested:** None ## Risks **What could break:** a sampled stack, or a crash report's host backtrace, ending earlier than before where a frame would have read outside its thread's stack. That only affects the profiling tools and the report's backtrace, and players never run the profilers. #1044 (WIP) changes the same `sample_all_thread`, for its samples' timestamps: whichever lands second resolves a small textual overlap. **Turning it off:** revert the commit **Compatibility:** Nothing changes ## Issues Fixes #383 ## Checklist ### Everyone - [x] One topic. - [x] The title is a Conventional Commit (`type(scope): description`) of the status line's type. - [x] Why gives the reason plainly and links the issues that fit (the tracker searched). - [x] Measurements has a verdict, or one line saying why there is nothing to measure. - [x] Testing names what was tested, what wasn't, and what must still be tested, where and how. - [x] Rebased on current `main`. - [x] `python -I -B tests/run_all.py` passes (any failure that also fails on `main` is named). - [x] No game files, dumps, build output or generated files. - [x] `third_party/` untouched (a new folder with a `PROVENANCE.md` instead). - [x] The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled. ### When it applies - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. - [x] A game-free test added for the fix, failing on `main` and passing here. - [x] Host code builds on both Windows (llvm-mingw) and Linux, with UTF-8 paths. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
fix(host): the samplers' stack walks stay on the walked thread's stack, so HOS_PROFILE=1 no longer crashes the game on Windows
All checks were successful
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been skipped
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been skipped
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been skipped
Third-party parts / The same as upstream (pull_request) Has been skipped
CI / Conventional Commit title (pull_request) Successful in 7s
Website / Website on Cloudflare (pull_request_target) Has been skipped
CI / Workflow lint (actionlint) (pull_request) Successful in 12s
Guide screenshots / Guide images and local UI source review (pull_request) Successful in 12s
Third-party parts / As their notes say (pull_request) Successful in 17s
PR description / Description follows the template (pull_request_target) Successful in 3s
41e601e096
The three x64 walks in host/mkw_glue.c (the crash report's, HOS_SAMPLE's and HOS_SAMPLE_ALL's) called
RtlVirtualUnwind on a suspended thread's context with nothing checked. A thread stopped where its unwind data does not
describe the frame (being made or ending, switching stacks) gave the unwinder a stack pointer that was not a stack, its
read faulted in the sampler's thread, and the game died (issue 383; with #1279's profiling mode, which samples from the
boot and lists the threads again every 5 s, about 2 minutes into a Windows run).

Each walk now goes one frame at a time through unwind_step: the stack pointer, and the frame register when the function
has one, are checked against the walked thread's committed stack (its TEB's StackLimit to StackBase, read with
NtQueryInformationThread) before the unwind reads through them, and the new stack pointer after; a step outside ends the
walk, so the sample is only cut short. The host is C without SEH (no -fms-extensions), so the walk is made unable to
fault rather than guarded. tests/test_sampler_stack_walk.py checks it.

Fixes #383

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Author
Member

Tested in game: Windows 11, RTX 3080 (NVIDIA 621.14): HOS_PROFILE=1 from kokiri-start, headless, 454 s of play with no crash and a clean stop (main @ 8e9cfc3a crashed at 135 s in sample_all_thread); the profile folder complete, stacks as deep as main's

Tested in game: Windows 11, RTX 3080 (NVIDIA 621.14): HOS_PROFILE=1 from kokiri-start, headless, 454 s of play with no crash and a clean stop (main @ 8e9cfc3a crashed at 135 s in sample_all_thread); the profile folder complete, stacks as deep as main's
dyst changed title from WIP: fix(host): the samplers' stack walks stay on the walked thread's stack, so HOS_PROFILE=1 no longer crashes the game on Windows to fix(host): the samplers' stack walks stay on the walked thread's stack, so HOS_PROFILE=1 no longer crashes the game on Windows 2026-11-17 04:26:17 +00:00
TrashPanda approved these changes 2026-11-17 05:08:23 +00:00
TrashPanda left a comment

Profiling on Windows no longer takes the game down when a stopped thread's stack cannot be walked. I read head 41e601e0. It merges, and I approve it.

unwind_step is the only call of RtlVirtualUnwind and RtlLookupFunctionEntry. It checks the stack pointer against the walked thread's committed stack before it reads unwind data, the frame register when the function has one, and the stack pointer again after the step. A step outside that range ends the sample there. The crash report, HOS_SAMPLE and HOS_SAMPLE_ALL all go through it, and both samplers suspend the thread before they ask for its limits. A thread whose limits cannot be read keeps one frame and goes on. The lines the profile writes are unchanged.

The stack limits come from the thread's TEB through NtQueryInformationThread, looked up at run time, so the link line stays as it is. The frame register is the same numbering as CONTEXT's Rax through R15. A chained unwind entry is followed once so that check sees the real frame register, and RtlVirtualUnwind still gets the entry the lookup returned.

Windows, RTX 3080: one profiled session ran 454 seconds and stopped cleanly, where the same session on main closed at 135 seconds inside sample_all_thread. The stacks stayed about as deep. Must be tested is None. The check reads the source and, with a Windows compiler, compiles the helpers on their own.

Work-in-progress #1044 also edits sample_all_thread. Take this one; that branch rebases onto it.

Profiling on Windows no longer takes the game down when a stopped thread's stack cannot be walked. I read head `41e601e0`. It merges, and I approve it. `unwind_step` is the only call of `RtlVirtualUnwind` and `RtlLookupFunctionEntry`. It checks the stack pointer against the walked thread's committed stack before it reads unwind data, the frame register when the function has one, and the stack pointer again after the step. A step outside that range ends the sample there. The crash report, `HOS_SAMPLE` and `HOS_SAMPLE_ALL` all go through it, and both samplers suspend the thread before they ask for its limits. A thread whose limits cannot be read keeps one frame and goes on. The lines the profile writes are unchanged. The stack limits come from the thread's TEB through `NtQueryInformationThread`, looked up at run time, so the link line stays as it is. The frame register is the same numbering as `CONTEXT`'s `Rax` through `R15`. A chained unwind entry is followed once so that check sees the real frame register, and `RtlVirtualUnwind` still gets the entry the lookup returned. Windows, RTX 3080: one profiled session ran 454 seconds and stopped cleanly, where the same session on main closed at 135 seconds inside `sample_all_thread`. The stacks stayed about as deep. Must be tested is None. The check reads the source and, with a Windows compiler, compiles the helpers on their own. Work-in-progress #1044 also edits `sample_all_thread`. Take this one; that branch rebases onto it.
All checks were successful
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been skipped
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been skipped
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been skipped
Third-party parts / The same as upstream (pull_request) Has been skipped
CI / Conventional Commit title (pull_request) Successful in 7s
Website / Website on Cloudflare (pull_request_target) Has been skipped
CI / Workflow lint (actionlint) (pull_request) Successful in 12s
Guide screenshots / Guide images and local UI source review (pull_request) Successful in 12s
Third-party parts / As their notes say (pull_request) Successful in 17s
PR description / Description follows the template (pull_request_target) Successful in 3s
This pull request doesn't have enough approvals yet. 0 of 1 approvals granted.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'fix/sampler-stack-walk:dyst-fix/sampler-stack-walk'
git switch 'dyst-fix/sampler-stack-walk'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Longshot!1343
No description provided.