Guard pages: emulate the faulting access instead of taking the exception #1305

Open
opened 2026-11-26 13:36:52 +00:00 by github-bot · 4 comments
Owner

@⁠liuycsbxbxn on GitHub · 2026-11-26 13:31 UTC · original

Split out of #1273.

Suggested labels: performance / memory
Verification cost: [GPU] [TITLE]

What the meta-issue asks

[1] Avoiding guard pages by patching … the instruction which tried the write / read. Instead of the exception, code could itself call a handler. The page can remain locked, because another view of that same page could be used to finish the actual write.

Where it lands

  • crates/victory-gpu/src/watch.rs — writes_as (the fault classification), failed_asking (the diagnostic on a failed ask), guard / guard_at, protect / protect_here, open / open_here.
  • The exception path is the one that produces VICTORY_LOCK_STATS-visible cost today; see also #1238 (hot-block pinning) and #1169.

Acceptance

  1. A measured before/after on one machine: page-fault rate per frame and the submit-thread time, one switch changed per comparison.
  2. Falls back cleanly when the page is not the shape the handler expected (the existing failed_asking path must still fire).
  3. No change in title behaviour in a capture comparison.

Why not just take it

Trampolining needs instruction-length decoding on x86-64 (and not breaking a partially-executed instruction); that is the part worth discussing before anyone writes it.


**@⁠liuycsbxbxn** on GitHub · 2026-11-26 13:31 UTC · [original](https://github.com/fxceless/victory-fork/issues/1305) Split out of #1273. **Suggested labels:** performance / memory **Verification cost:** `[GPU]` `[TITLE]` ### What the meta-issue asks > [1] Avoiding guard pages by patching … the instruction which tried the write / read. Instead of the exception, code could itself call a handler. The page can remain locked, because another view of that same page could be used to finish the actual write. ### Where it lands - `crates/victory-gpu/src/watch.rs` — `writes_as` (the fault classification), `failed_asking` (the diagnostic on a failed ask), `guard` / `guard_at`, `protect` / `protect_here`, `open` / `open_here`. - The exception path is the one that produces `VICTORY_LOCK_STATS`-visible cost today; see also #1238 (hot-block pinning) and #1169. ### Acceptance 1. A measured before/after on one machine: page-fault rate per frame and the submit-thread time, one switch changed per comparison. 2. Falls back cleanly when the page is not the shape the handler expected (the existing `failed_asking` path must still fire). 3. No change in title behaviour in a capture comparison. ### Why not just take it Trampolining needs instruction-length decoding on x86-64 (and not breaking a partially-executed instruction); that is the part worth discussing before anyone writes it. ---
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 13:36:52 +00:00
Author
Owner

@⁠JayFoxRox on GitHub · 2026-11-26 17:04 UTC · original

More hints to communicate the idea:

  1. GPU uploaded resources are guard-page (= no read/write/exec allowed) copies in system RAM (+ a full-access view of the same in system RAM).
  2. The GPU "owns" the resource by default.
  3. CPU reads (#1306) /writes (#1307) to such pages / GPU-resources and triggers exceptions

Current state

  1. Unlock page (and potentially redownload page from GPU)
  2. Continue execution
  3. Eventually (reupload to GPU) re-lock page

This is very expensive due to exception handling (microsecond range) and page lock/unlock (microsecond range) which requires a kernel-round-trip.

Speculation: I assume the game uses the CPU readback to find which resources need to be streamed, so any delay here causes streaming latency.

Proposal

  1. Check what instruction caused the exception and write an x64 emulator for those instructions and some handler function for these instructions.
  2. To avoid further exceptions from the same location, modify/patch the game-code (in memory) to jump to the emulation handler which emulates the instruction and replaces the target address with the full-access view of the resource (for reads, potentially download GPU resource here, if deemed necessary).
  3. For writes, queue a re-upload of the GPU resource.

After the first encounter of such code locations (access to GPU resources) the page-exception would not longer happen and there'd be standard control flow (nanosecond operations); everything stays in user-code.

Additionally, it would help identify the root-cause for reads/writes by RIP or stack analysis, so by analyzing the game could, it could be identified and categorized how critical a GPU resource re-upload or download is (as in: why does the game try to access the GPU resource and is it important to be instant).

--

A potential improvement could build a cache of these locations so patching could be done upfront for known locations. Obviously this requires checks that the version of the game code is still the same.

**@⁠JayFoxRox** on GitHub · 2026-11-26 17:04 UTC · [original](https://github.com/fxceless/victory-fork/issues/1305#issuecomment-7198460484) More hints to communicate the idea: 1. GPU uploaded resources are guard-page (= no read/write/exec allowed) copies in system RAM (+ a full-access view of the same in system RAM). 2. The GPU "owns" the resource by default. 3. CPU reads (#1306) /writes (#1307) to such pages / GPU-resources and triggers exceptions ## Current state 4. Unlock page (and potentially redownload page from GPU) 5. Continue execution 6. Eventually (reupload to GPU) re-lock page This is very expensive due to exception handling (microsecond range) and page lock/unlock (microsecond range) which requires a kernel-round-trip. Speculation: I assume the game uses the CPU readback to find which resources need to be streamed, so any delay here causes streaming latency. ## Proposal 4. Check what instruction caused the exception and write an x64 emulator for those instructions and some handler function for these instructions. 5. To avoid further exceptions from the same location, modify/patch the game-code (in memory) to jump to the emulation handler which emulates the instruction and replaces the target address with the full-access view of the resource (for reads, potentially download GPU resource here, if deemed necessary). 6. For writes, queue a re-upload of the GPU resource. After the first encounter of such code locations (access to GPU resources) the page-exception would not longer happen and there'd be standard control flow (nanosecond operations); everything stays in user-code. Additionally, it would help identify the root-cause for reads/writes by RIP or stack analysis, so by analyzing the game could, it could be identified and categorized how critical a GPU resource re-upload or download is (as in: why does the game try to access the GPU resource and is it important to be instant). -- A potential improvement could build a cache of these locations so patching could be done upfront for known locations. Obviously this requires checks that the version of the game code is still the same.
Author
Owner

@⁠liuycsbxbxn on GitHub · 2026-11-26 17:04 UTC · original

Per your replies on #1306 and #1307, this is the prerequisite for both: the trampoline is what lets either side drop the fault + re-lock path instead of paying it 1000x over. I will scope this issue as that patch and keep the other two as follow-ups, so the dependency direction is explicit.

**@⁠liuycsbxbxn** on GitHub · 2026-11-26 17:04 UTC · [original](https://github.com/fxceless/victory-fork/issues/1305#issuecomment-7198470333) Per your replies on #1306 and #1307, this is the prerequisite for both: the trampoline is what lets either side drop the fault + re-lock path instead of paying it 1000x over. I will scope this issue as that patch and keep the other two as follow-ups, so the dependency direction is explicit.
Author
Owner

@⁠liuycsbxbxn on GitHub · 2026-11-26 17:10 UTC · original

Good - that matches the model in the issue body, and "patch once, then stay in user code" is the part that actually removes the cost; the fault path is not something we can make cheaper, only avoid.

Breaking it down, tagged with what can be built and tested without a card or a game dump:

1. Fault-site recorder (phase 0, [CPU] only). In the handler, log RIP (module + RVA), the surrounding bytes and the decoded instruction, and patch nothing yet. This is how we learn which instruction forms actually touch resources - the emulator subset should be driven by that data, not guessed.

2. Instruction length + operand decoder ([CPU] only). Exact x86-64 decoding for the subset found in (1). It has to be exact: a wrong length corrupts whatever follows when the instruction is copied into the trampoline. Unit-testable against a corpus; no card needed.

3. Trampoline patch + handler table ([CPU] only). VirtualProtect the code page, write the jump, flush the instruction cache; the per-site handler emulates the original instruction against the full-access view. Testable against a synthetic target (map a page no-access, place known bytes, assert the handler runs once and that the site behaves afterwards).

4. Read path (#1306) and write path (#1307), [GPU]. The handler decides download-on-read and queues the re-upload on write; this is the part that needs the card and the title to measure honestly.

5. Patch cache ([CPU] only). Keyed by (module build id, RVA, hash of the original bytes); verify the bytes still match before pre-patching, otherwise fall back to lazy patching for that site.

6. Attribution ([CPU] plumbing, [GPU][TITLE] for the content). RIP/stack -> which subsystem reads which resource; the report format and aggregation are CPU-only, the interesting conclusions are not.

Proposed order: 1 -> 2 -> 3, then 4 on hardware. (1)-(3) are all verifiable here without a card, and they are what #1306 and #1307 would sit on. If you want, I can start with (1)+(2) as a self-contained patch, so the instruction mix from a real capture decides how far the emulator has to go.

One question before I start: should the recorder be always-on but rate-limited, or opt-in per session? Always-on in a shipping path has a real cost, and a rate limit would bias the instruction mix we collect.

**@⁠liuycsbxbxn** on GitHub · 2026-11-26 17:10 UTC · [original](https://github.com/fxceless/victory-fork/issues/1305#issuecomment-7198604365) Good - that matches the model in the issue body, and "patch once, then stay in user code" is the part that actually removes the cost; the fault path is not something we can make cheaper, only avoid. Breaking it down, tagged with what can be built and tested without a card or a game dump: **1. Fault-site recorder (phase 0, `[CPU]` only).** In the handler, log RIP (module + RVA), the surrounding bytes and the decoded instruction, and patch nothing yet. This is how we learn which instruction forms actually touch resources - the emulator subset should be driven by that data, not guessed. **2. Instruction length + operand decoder (`[CPU]` only).** Exact x86-64 decoding for the subset found in (1). It has to be exact: a wrong length corrupts whatever follows when the instruction is copied into the trampoline. Unit-testable against a corpus; no card needed. **3. Trampoline patch + handler table (`[CPU]` only).** VirtualProtect the code page, write the jump, flush the instruction cache; the per-site handler emulates the original instruction against the full-access view. Testable against a synthetic target (map a page no-access, place known bytes, assert the handler runs once and that the site behaves afterwards). **4. Read path (#1306) and write path (#1307), `[GPU]`.** The handler decides download-on-read and queues the re-upload on write; this is the part that needs the card and the title to measure honestly. **5. Patch cache (`[CPU]` only).** Keyed by (module build id, RVA, hash of the original bytes); verify the bytes still match before pre-patching, otherwise fall back to lazy patching for that site. **6. Attribution (`[CPU]` plumbing, `[GPU][TITLE]` for the content).** RIP/stack -> which subsystem reads which resource; the report format and aggregation are CPU-only, the interesting conclusions are not. Proposed order: 1 -> 2 -> 3, then 4 on hardware. (1)-(3) are all verifiable here without a card, and they are what #1306 and #1307 would sit on. If you want, I can start with (1)+(2) as a self-contained patch, so the instruction mix from a real capture decides how far the emulator has to go. One question before I start: should the recorder be always-on but rate-limited, or opt-in per session? Always-on in a shipping path has a real cost, and a rate limit would bias the instruction mix we collect.
Author
Owner

@⁠liuycsbxbxn on GitHub · 2026-11-26 17:27 UTC · original

Small update so this is not only design talk: I built the parts that need no card, and they are green on a plain Windows box (26 tests, no warnings).

  • exact-length decoder - it refuses VEX/EVEX rather than guessing, because a wrong length corrupts whatever follows the copied instruction.
  • phase-0 recorder - aggregates per (module, RVA) and patches nothing; the rate limit caps new sites only, so a known site keeps counting and a hot one is never lost.
  • patcher + a live test on a synthetic PAGE_NOACCESS page: the first call faults exactly once, the handler rewrites the full 10-byte instruction in place (E9 rel32 + NOPs), and calls two and three return the right value and never fault again. Byte-exact restore is covered too.

I am deliberately stopping there and waiting for your call before writing the per-instruction emulator: which operand forms matter should come from the recorder's data, not from me guessing at the subset. It is a self-contained crate under prototypes/, not wired into the workspace, so it costs the normal build nothing.

And thank you for writing the design down in that much detail. Your "patch once, then stay in user code" framing is exactly what made me test the fault path instead of assuming it, and it is the reason the prototype was tractable at all. Happy to keep building in whatever direction you prefer - looking forward to it. 共勉.

**@⁠liuycsbxbxn** on GitHub · 2026-11-26 17:27 UTC · [original](https://github.com/fxceless/victory-fork/issues/1305#issuecomment-7199043816) Small update so this is not only design talk: I built the parts that need no card, and they are green on a plain Windows box (26 tests, no warnings). - exact-length decoder - it refuses VEX/EVEX rather than guessing, because a wrong length corrupts whatever follows the copied instruction. - phase-0 recorder - aggregates per (module, RVA) and patches nothing; the rate limit caps new sites only, so a known site keeps counting and a hot one is never lost. - patcher + a live test on a synthetic PAGE_NOACCESS page: the first call faults exactly once, the handler rewrites the full 10-byte instruction in place (E9 rel32 + NOPs), and calls two and three return the right value and never fault again. Byte-exact restore is covered too. I am deliberately stopping there and waiting for your call before writing the per-instruction emulator: which operand forms matter should come from the recorder's data, not from me guessing at the subset. It is a self-contained crate under prototypes/, not wired into the workspace, so it costs the normal build nothing. And thank you for writing the design down in that much detail. Your "patch once, then stay in user code" framing is exactly what made me test the fault path instead of assuming it, and it is the reason the prototype was tractable at all. Happy to keep building in whatever direction you prefer - looking forward to it. 共勉.
This discussion has been locked. Commenting is limited to contributors.
No milestone
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
github/fxceless--victory-fork#1305
No description provided.