Diagnostics: the return-address scan tests an image's code sections before VirtualQuery #51

Manually merged
github merged 1 commit from gh-pr/51/head into gh-pr/51/base 2026-11-27 03:15:38 +00:00
Owner

@⁠Sr-0w on GitHub · 2026-11-25 23:05 UTC · original
Sr-0w:diagnostics/wait-history-cheap → main · +69 −2 · 4 files · 1 commits · merged 2026-11-25 23:15 UTC

What

ReturnAddresses.Scan, used by the --watch-hang wait-object history (#47) on every wait of the game, now drops a stack value unless its six call bytes lie in an executable section of the image. The test uses the section headers (LoadedImage.InCode) and runs before the VirtualQuery readable-code check. Pointers into an image's data are rejected without a system call. Return addresses still get the same checks as before.

Why

The scan walks up to 64 KB of stack and called VirtualQuery for each value inside an image's range. Game stacks hold many pointers into the image's data sections.

In a managed profile (--profile-managed, 30 s during a world load), WaitObjects.Waiting → ReturnAddresses.Scan → GameHangWatch.IsReadable was 66% of the shims' samples on the game's threads.

In game

All runs below are on an RX 7800 XT and were recorded:

  • With --watch-hang --sample-threads 10: the short animation right after the "press A" title screen stutters, with frame drops and visible tearing in every recorded run.
  • The same build without those two options: no stutter on that animation.

This change removes the scan's system calls on the wait path. The periodic stack captures of --sample-threads (ThreadSnapshot, "stack capture stuck" about every 10 s) are a separate cost and are not touched here.

Tests

  • New Only_a_value_after_six_bytes_of_an_executable_section_is_checked_as_code test, built on a two-section image.
  • Loader tests: 398 passed.

🤖 Generated with Claude Code

**@⁠Sr-0w** on GitHub · 2026-11-25 23:05 UTC · [original](https://github.com/ThatRecompDev/five-star-recomp/pull/51) `Sr-0w:diagnostics/wait-history-cheap` → `main` · +69 −2 · 4 files · 1 commits · merged 2026-11-25 23:15 UTC ## What `ReturnAddresses.Scan`, used by the `--watch-hang` wait-object history (#47) on every wait of the game, now drops a stack value unless its six call bytes lie in an executable section of the image. The test uses the section headers (`LoadedImage.InCode`) and runs before the `VirtualQuery` readable-code check. Pointers into an image's data are rejected without a system call. Return addresses still get the same checks as before. ## Why The scan walks up to 64 KB of stack and called `VirtualQuery` for each value inside an image's range. Game stacks hold many pointers into the image's data sections. In a managed profile (`--profile-managed`, 30 s during a world load), `WaitObjects.Waiting` → `ReturnAddresses.Scan` → `GameHangWatch.IsReadable` was 66% of the shims' samples on the game's threads. ## In game All runs below are on an RX 7800 XT and were recorded: - With `--watch-hang --sample-threads 10`: the short animation right after the "press A" title screen stutters, with frame drops and visible tearing in every recorded run. - The same build without those two options: no stutter on that animation. This change removes the scan's system calls on the wait path. The periodic stack captures of `--sample-threads` (`ThreadSnapshot`, "stack capture stuck" about every 10 s) are a separate cost and are not touched here. ## Tests - New `Only_a_value_after_six_bytes_of_an_executable_section_is_checked_as_code` test, built on a two-section image. - Loader tests: 398 passed. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
With --watch-hang, every wait of the game records its caller frames. The scan behind that walks 64 KB of stack and
called VirtualQuery for each value inside an image's range, and stacks hold many pointers into an image's data. In a
managed profile of a world load, that was 66% of the shims' time on the game's threads, and the animation after the
title screen stuttered with --watch-hang and not without it. A value now needs its six call bytes inside an
executable section of the image, per its headers, before the readable-code check: data pointers are dropped without
a system call.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 03:15:37 +00:00
github manually merged commit 0a2db0b53d into gh-pr/51/base 2026-11-27 03:15:38 +00:00
Author
Owner

@⁠ThatRecompDev on GitHub · 2026-11-25 23:15 UTC · original

nice find, 66% of the shims' time on the game's threads is a lot to lose to VirtualQuery on data pointers.

checking the section headers first is the right order, and the readable-code check still runs after it, so nothing gets less safe. the test with a two-section image covers the boundaries nicely.

merged into main (0a2db0b), thanks!

the --sample-threads stack captures you mention are on our list too, they're a separate cost from this

**@⁠ThatRecompDev** on GitHub · 2026-11-25 23:15 UTC · [original](https://github.com/ThatRecompDev/five-star-recomp/pull/51#issuecomment-7177318313) nice find, 66% of the shims' time on the game's threads is a lot to lose to VirtualQuery on data pointers. checking the section headers first is the right order, and the readable-code check still runs after it, so nothing gets less safe. the test with a two-section image covers the boundaries nicely. merged into main (0a2db0b), thanks! the --sample-threads stack captures you mention are on our list too, they're a separate cost from this
This discussion has been locked. Commenting is limited to contributors.
No description provided.