RT-GUEST-POKE-KNOB: LEONIDA_GUEST_POKE writes bytes into a mapped image #582

Closed
github-bot wants to merge 1 commit from gh-pr/582/head into gh-pr/582/base
Owner

@⁠fiestorra on GitHub · 2026-11-24 09:04 UTC · original
fiestorra:carry/rt-guest-poke-knob → main · +204 −0 · 3 files · 1 commits · closed without merging

Claimed in the coordination issue as: RT-GUEST-POKE-KNOB — comment #14

Windows only, no game data needed for the test, nothing online. The measurement that motivated it is in comments #14 and #14.

What this changes

LEONIDA_GUEST_POKE writes bytes into a mapped image while it is still writable. Entries are separated by ; and each one is <module>:<rva>:<hex bytes>: the module is its basename, matched case-insensitively with or without its extension; the rva is 1 to 8 hexadecimal digits with or without a 0x prefix; the bytes are 1 to 256 hexadecimal pairs.

write_guest_pokes (runtime/src/pe_loader.cpp) is called from map_image, after relocations and before initialize_tls and protect_sections. After relocations so an rva means what the caller wrote; before the TLS template is copied so a poke reads as the image's own value to everything that runs afterwards, per-thread TLS copies included; before the protection step so no VirtualProtect dance is needed. It is in map_image rather than in load() so a run-time LoadLibrary gets the same treatment. Every mapped image is offered the entries and only the ones naming it apply. An entry that does not parse, or that would run past the end of the image, is reported on the loader's log and changes nothing; with the variable unset nothing is read and nothing is written.

runtime/README.md gets a paragraph next to the existing LEONIDA_TLS_GA_PATCH one, which is the loader's only other image rewrite.

Why this exists

The byte at game_scarlett_master.exe+0xa9438f4 decides whether the title's window stage continues: it is 0 at the check in exe+0x24785cd, the routine returns 0, and the boot stops at the int 3 in exe+0x19f4f58. Writing 1 there with a debugger moved the boot to the title's own CreateCommittedResource3 and CreateRenderTargetView calls, and the stop was never reached. Repeating that, and anything like it, wants a way to put a byte into the image from outside without attaching a debugger and re-running under it.

This is a measuring tool, not a compatibility feature. It does not make the title run: the run with that byte at 1 reported 160 first-chance access violations, and what should have raised the byte is still unknown.

What the test found

test/pe_loader_test.cpp gains test_guest_poke, run from main after test_unwind. It takes the last eight bytes of the fixture image (SizeOfImage - 8, past the code and data the fixture runs), loads tiny_dll through the public Loader API with different values of the variable, and reads those bytes back from the mapped image. It checks that a poke naming <module>.dll and one naming the module without its extension, in upper case and with a 0x prefix, both write; that two entries separated by ; are both applied; that a poke naming another module, one that would run past the end of the image, and eight malformed entries each change nothing; and that with the variable unset the bytes are the fixture's own.

pe_loader_test: OK (102 checks)
1/6 Test #14: loader ........... Passed
2/6 Test #15: module_names ..... Passed
3/6 Test #17: pe_loader ........ Passed
4/6 Test #18: pe_loader_bounds . Passed
5/6 Test #19: pe_loader_fuzz ... Passed
6/6 Test #20: loader_tls ....... Passed

Built and run in this checkout at d42773d07, Windows 11, MSVC, CPU only: no GPU, no spirv-as/spirv-val, no game executable.

One thing the test caught on the first run, worth naming because it is easy to get wrong: getenv_s reports the length with the terminating NUL counted, so a first version that built the entry list from std::string(spec, length) took that NUL as the last byte of the last entry. A single-entry …:deadbeefcafebabe then looked like 17 hex characters and was rejected, while a multi-entry value applied its first entry and rejected its last. The parse now drops trailing NULs before splitting, and the test's positive cases are what showed it.

Written with an AI assistant.

**@⁠fiestorra** on GitHub · 2026-11-24 09:04 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/582) `fiestorra:carry/rt-guest-poke-knob` → `main` · +204 −0 · 3 files · 1 commits · closed without merging Claimed in the coordination issue as: RT-GUEST-POKE-KNOB — comment https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14 Windows only, no game data needed for the test, nothing online. The measurement that motivated it is in comments https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14 and https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14. ## What this changes `LEONIDA_GUEST_POKE` writes bytes into a mapped image while it is still writable. Entries are separated by `;` and each one is `<module>:<rva>:<hex bytes>`: the module is its basename, matched case-insensitively with or without its extension; the rva is 1 to 8 hexadecimal digits with or without a `0x` prefix; the bytes are 1 to 256 hexadecimal pairs. `write_guest_pokes` (`runtime/src/pe_loader.cpp`) is called from `map_image`, after relocations and before `initialize_tls` and `protect_sections`. After relocations so an rva means what the caller wrote; before the TLS template is copied so a poke reads as the image's own value to everything that runs afterwards, per-thread TLS copies included; before the protection step so no `VirtualProtect` dance is needed. It is in `map_image` rather than in `load()` so a run-time `LoadLibrary` gets the same treatment. Every mapped image is offered the entries and only the ones naming it apply. An entry that does not parse, or that would run past the end of the image, is reported on the loader's log and changes nothing; with the variable unset nothing is read and nothing is written. `runtime/README.md` gets a paragraph next to the existing `LEONIDA_TLS_GA_PATCH` one, which is the loader's only other image rewrite. ## Why this exists The byte at `game_scarlett_master.exe+0xa9438f4` decides whether the title's window stage continues: it is 0 at the check in `exe+0x24785cd`, the routine returns 0, and the boot stops at the `int 3` in `exe+0x19f4f58`. Writing 1 there with a debugger moved the boot to the title's own `CreateCommittedResource3` and `CreateRenderTargetView` calls, and the stop was never reached. Repeating that, and anything like it, wants a way to put a byte into the image from outside without attaching a debugger and re-running under it. This is a measuring tool, not a compatibility feature. It does not make the title run: the run with that byte at 1 reported 160 first-chance access violations, and what should have raised the byte is still unknown. ## What the test found `test/pe_loader_test.cpp` gains `test_guest_poke`, run from `main` after `test_unwind`. It takes the last eight bytes of the fixture image (`SizeOfImage - 8`, past the code and data the fixture runs), loads `tiny_dll` through the public `Loader` API with different values of the variable, and reads those bytes back from the mapped image. It checks that a poke naming `<module>.dll` and one naming the module without its extension, in upper case and with a `0x` prefix, both write; that two entries separated by `;` are both applied; that a poke naming another module, one that would run past the end of the image, and eight malformed entries each change nothing; and that with the variable unset the bytes are the fixture's own. ``` pe_loader_test: OK (102 checks) 1/6 Test #14: loader ........... Passed 2/6 Test #15: module_names ..... Passed 3/6 Test #17: pe_loader ........ Passed 4/6 Test #18: pe_loader_bounds . Passed 5/6 Test #19: pe_loader_fuzz ... Passed 6/6 Test #20: loader_tls ....... Passed ``` Built and run in this checkout at `d42773d07`, Windows 11, MSVC, CPU only: no GPU, no `spirv-as`/`spirv-val`, no game executable. One thing the test caught on the first run, worth naming because it is easy to get wrong: `getenv_s` reports the length **with the terminating NUL counted**, so a first version that built the entry list from `std::string(spec, length)` took that NUL as the last byte of the last entry. A single-entry `…:deadbeefcafebabe` then looked like 17 hex characters and was rejected, while a multi-entry value applied its first entry and rejected its last. The parse now drops trailing NULs before splitting, and the test's positive cases are what showed it. Written with an AI assistant.
Entries are <module>:<rva>:<hex bytes>, separated by ';', applied after relocations and before the TLS template is copied and the sections are protected. Applied in map_image, so a run-time LoadLibrary gets them too. A malformed entry, or one that would run past the end of the image, is reported and changes nothing.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:09:53 +00:00
github-bot closed this pull request 2026-11-26 09:09:53 +00:00
Author
Owner

@⁠fiestorra on GitHub · 2026-11-24 10:10 UTC · original · reviewed

Self-review by the author, at the request of the person running this work (they cannot merge either way - see the note at the end).

What I re-checked in the diff.

  • Input validation is complete for the documented grammar: rva 1 to 8 hex digits with optional 0x (runtime/src/pe_loader.cpp:566-581), bytes 1 to 256 hex pairs (:584-591), per-entry failure reported with the reason and the entry skipped (:630), a variable longer than the buffer reported rather than silently truncated (:613), and an entry whose bytes would run past the end of the image reported and skipped (:635-640). Nothing is written unless the entry parses and fits.
  • Application point is where it has to be: after relocations, so an rva means what the caller wrote, and before the sections are protected (:1619, with the reasoning in the comment at :1617-1618). That is what makes it usable on .text, not only on .data.

Evidence, all local. cmake --build clean, and the test for it: pe_loader_test with 102 checks passes; it is part of the 16/16 in ctest -R "list|proxy|xs_|command|pe_loader". It has also been used for real measurements on the title, two of them published on #14 (widening one cmp immediate of the title's own size check, and replacing one 7-byte instruction with int 3 + six nops to get a break at an address of my choosing without a debugger breakpoint). Reproduce with:

LEONIDA_GUEST_POKE=<basename>:<rva>:<hex bytes> in the environment of gta6rt.exe; the loader logs wrote N bytes at rva for each applied entry, so an ignored entry is never silent.

One limitation I would like recorded, not hidden. write_guest_pokes runs immediately before initialize_tls (:1619 then :1620), so a poke whose target overlaps the TLS template range is overwritten by the template copy. The comment at :1617 states the ordering but reads as if every poke survives it; the README documents the ordering and not this consequence. Say the word and I will add one sentence to the README and to that comment rather than leave it to be discovered the hard way.

Merge status. I cannot merge this myself: MergePullRequest is denied for fiestorra on this repository, and required checks are still queued repo-wide (the CI queue condition noted on #14 - not something my branches can fix). The merge is otherwise clean: mergeable = true after rebasing on current main.

Written with an AI assistant.

**@⁠fiestorra** on GitHub · 2026-11-24 10:10 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/582#pullrequestreview-6037299276) · **reviewed** Self-review by the author, at the request of the person running this work (they cannot merge either way - see the note at the end). **What I re-checked in the diff.** - Input validation is complete for the documented grammar: rva 1 to 8 hex digits with optional `0x` (`runtime/src/pe_loader.cpp:566-581`), bytes 1 to 256 hex pairs (`:584-591`), per-entry failure reported with the reason and the entry skipped (`:630`), a variable longer than the buffer reported rather than silently truncated (`:613`), and an entry whose bytes would run past the end of the image reported and skipped (`:635-640`). Nothing is written unless the entry parses and fits. - Application point is where it has to be: after relocations, so an rva means what the caller wrote, and before the sections are protected (`:1619`, with the reasoning in the comment at `:1617-1618`). That is what makes it usable on `.text`, not only on `.data`. **Evidence, all local.** `cmake --build` clean, and the test for it: `pe_loader_test` with 102 checks passes; it is part of the 16/16 in `ctest -R "list|proxy|xs_|command|pe_loader"`. It has also been used for real measurements on the title, two of them published on #14 (widening one `cmp` immediate of the title's own size check, and replacing one 7-byte instruction with `int 3` + six nops to get a break at an address of my choosing without a debugger breakpoint). Reproduce with: `LEONIDA_GUEST_POKE=<basename>:<rva>:<hex bytes>` in the environment of `gta6rt.exe`; the loader logs `wrote N bytes at rva` for each applied entry, so an ignored entry is never silent. **One limitation I would like recorded, not hidden.** `write_guest_pokes` runs immediately before `initialize_tls` (`:1619` then `:1620`), so a poke whose target overlaps the TLS template range is overwritten by the template copy. The comment at `:1617` states the ordering but reads as if every poke survives it; the README documents the ordering and not this consequence. Say the word and I will add one sentence to the README and to that comment rather than leave it to be discovered the hard way. **Merge status.** I cannot merge this myself: `MergePullRequest` is denied for `fiestorra` on this repository, and required checks are still queued repo-wide (the CI queue condition noted on #14 - not something my branches can fix). The merge is otherwise clean: `mergeable = true` after rebasing on current main. Written with an AI assistant.
Author
Owner

@⁠fiestorra on GitHub · 2026-11-24 10:17 UTC · original

Follow-up on my own review: I acted on the one inaccuracy it found, and the branch is current.

  • Rebased carry/rt-guest-poke-knob onto current main (21625bca8); the single commit is now 9cbc75f78, still 3 files: runtime/README.md, runtime/src/pe_loader.cpp, runtime/test/pe_loader_test.cpp (+204).
  • Added the limitation my review raised, in both places where a reader would look for it: the README paragraph now says a poke on the image's own TLS index slot is overwritten a few lines later when the loader writes the index there, and the comment above write_guest_pokes in pe_loader.cpp says the same and notes that poking the TLS template range is deliberate - the per-thread copies carry the poked bytes. That matches the code: initialize_tls writes the index at pe_loader.cpp:1697, after write_guest_pokes at :1619.
  • Re-verified after the rebase: build clean, ctest -R "pe_loader" = 3/3 (pe_loader, pe_loader_bounds, pe_loader_fuzz).

Written with an AI assistant.

**@⁠fiestorra** on GitHub · 2026-11-24 10:17 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/582#issuecomment-7128568885) Follow-up on my own review: I acted on the one inaccuracy it found, and the branch is current. - Rebased `carry/rt-guest-poke-knob` onto current main (21625bca8); the single commit is now 9cbc75f78, still 3 files: `runtime/README.md`, `runtime/src/pe_loader.cpp`, `runtime/test/pe_loader_test.cpp` (+204). - Added the limitation my review raised, in both places where a reader would look for it: the README paragraph now says a poke on the image's own TLS index slot is overwritten a few lines later when the loader writes the index there, and the comment above `write_guest_pokes` in `pe_loader.cpp` says the same and notes that poking the TLS *template* range is deliberate - the per-thread copies carry the poked bytes. That matches the code: `initialize_tls` writes the index at `pe_loader.cpp:1697`, after `write_guest_pokes` at `:1619`. - Re-verified after the rebase: build clean, `ctest -R "pe_loader"` = 3/3 (`pe_loader`, `pe_loader_bounds`, `pe_loader_fuzz`). Written with an AI assistant.
Author
Owner

@⁠fiestorra on GitHub · 2026-11-24 11:44 UTC · original

Closing this myself. I have moved my work on this title to another repository and I am not going to be here to follow a review of it.

What the change is, so that someone can rebuild it without reading the diff: an environment value of the form <module basename>:<rva in hex>:<bytes in hex>, several separated by ;, applied to the mapped guest image after relocations and before the thread-local storage copy and before sections are made read-only. It is enough to replace an int 3 with a nop, or to change a byte a gate reads, which is the cheapest way I found to tell whether a stop is the title's own check or the layer's. Both examples I reported (the two traps in the title's own start-up path, in result 7129079465) were measured with it and nothing else.

The branch carry/rt-guest-poke-knob will be deleted with the rest of my branches there. If anyone wants the patch itself, say so and I will hand it over rather than leave it on a branch nobody can reach.

Written with an AI assistant.

**@⁠fiestorra** on GitHub · 2026-11-24 11:44 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/582#issuecomment-7130479055) Closing this myself. I have moved my work on this title to another repository and I am not going to be here to follow a review of it. What the change is, so that someone can rebuild it without reading the diff: an environment value of the form `<module basename>:<rva in hex>:<bytes in hex>`, several separated by `;`, applied to the mapped guest image after relocations and before the thread-local storage copy and before sections are made read-only. It is enough to replace an `int 3` with a `nop`, or to change a byte a gate reads, which is the cheapest way I found to tell whether a stop is the title's own check or the layer's. Both examples I reported (the two traps in the title's own start-up path, in result 7129079465) were measured with it and nothing else. The branch `carry/rt-guest-poke-knob` will be deleted with the rest of my branches there. If anyone wants the patch itself, say so and I will hand it over rather than leave it on a branch nobody can reach. Written with an AI assistant.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.