[PR #582] [CLOSED] RT-GUEST-POKE-KNOB: LEONIDA_GUEST_POKE writes bytes into a mapped image #625

Closed
opened 2026-11-26 20:18:30 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/582
Author: @fiestorra
Created: 11/24/2026
Status: ❌ Closed

Base: main ← Head: carry/rt-guest-poke-knob


📝 Commits (1)

  • 9cbc75f RT-GUEST-POKE-KNOB: LEONIDA_GUEST_POKE writes bytes into a mapped image

📊 Changes

3 files changed (+204 additions, -0 deletions)

View changed files

📝 runtime/README.md (+13 -0)
📝 runtime/src/pe_loader.cpp (+123 -0)
📝 runtime/test/pe_loader_test.cpp (+68 -0)

📄 Description

Claimed in the coordination issue as: RT-GUEST-POKE-KNOB — comment https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7126729056

Windows only, no game data needed for the test, nothing online. The measurement that motivated it is in comments https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7126682372 and https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7126728672.

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.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/582 **Author:** [@fiestorra](https://github.com/fiestorra) **Created:** 11/24/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `carry/rt-guest-poke-knob` --- ### 📝 Commits (1) - [`9cbc75f`](https://github.com/ubiquitarianism/leonida/commit/9cbc75f78f57c0fc127ee0050f6ec2efb6b17a6b) RT-GUEST-POKE-KNOB: LEONIDA_GUEST_POKE writes bytes into a mapped image ### 📊 Changes **3 files changed** (+204 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `runtime/README.md` (+13 -0) 📝 `runtime/src/pe_loader.cpp` (+123 -0) 📝 `runtime/test/pe_loader_test.cpp` (+68 -0) </details> ### 📄 Description Claimed in the coordination issue as: RT-GUEST-POKE-KNOB — comment https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7126729056 Windows only, no game data needed for the test, nothing online. The measurement that motivated it is in comments https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7126682372 and https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7126728672. ## 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:18:30 +01:00
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
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
starred/leonida#625
No description provided.