[pr-fix][memory] loader: a hooked store that crosses a block boundary marks both blocks #423

Open
qnjk wants to merge 1 commit from qnjk/Darner:heap-sites-cross-block into main
First-time contributor

Contributor role (optional)

  • Fluxer ID:

What it changes

  • The problem on main: heap_sites::hooked writes the write-watch notes for both ends of the title's store — for last in [0, store.size - 1] — but the block marks that follow run once from the first byte's offset, and the TOLD exchange (xchg) goes to that one cell:

    asm.put(&rr(MOV, a, d));          // d = the store's offset
    asm.put(&shift(SHR, d, 16));      // d = block index of the first byte
    ...
    mov byte [d], 1                   // mark that block only
    

    (crates/darner-loader/src/heap_sites.rs:1073-1085 before this change). The software path for the same journal marks every block a write touches (crates/darner-gpu/src/heap_hooks.rs:397-402), and take() skips every block whose mark is 0 while tell_watch() tells only blocks whose TOLD byte is set. So a store whose last byte lands in the next block — a 16-byte store at +0xFFF8, say — left its notes there unread and that block untold to the watch, until some later store happened to mark it. The host's copy of the record was a round stale and a page we wrote through our view was one the watch had not been told about.

  • This change tests for the crossing first (movzx the offset's low 16 bits, cmp against 0x10000 - (size - 1), jae), and only in the cold crossed path marks and exchanges the first block and then the block after it. A one-byte store cannot cross and emits no test; a store inside one block — the common case — pays movzx, cmp and a jump not taken, with no further memory access. A store is 32 bytes at most, so it crosses at most one boundary: no loop is needed, and the report says what the cold path costs (~118 bytes of stub area per hooked store).

How it was tested

  • Tested revision: 01c8e31 (one commit on f3840d7).
  • Commands and results:
    • cargo test --locked -p darner-loader --lib — 93 passed, 0 failed.
    • New test heap_sites::stub_tests::a_store_across_a_block_boundary_marks_both_blocks (#[cfg(all(test, windows))], the module the other stub tests live in): it makes a site whose span is vmovups [rcx-48h], xmm0 — sixteen bytes at +0xFFF8 of the test heap, eight of them in block 1 — runs it through the existing runs() harness, and asserts that the registers and ymm come out as the unhooked run's, that the title's memory is unchanged, that our view has what the title's would, that the sixteen bytes really crossed (+0xFFF8..+0x10008), that (blocks[0], told[0], blocks[LINE], told[LINE]) == (1, 1, 1, 1), and that slots 0x7FF and 0x800 are noted with cells.any == 1. No title, no adapter, no fixture file: only VirtualAlloc.
    • Fail-before, with only the old one-mark sequence restored and the test kept:
      thread 'heap_sites::stub_tests::a_store_across_a_block_boundary_marks_both_blocks' panicked at
      crates\darner-loader\src\heap_sites.rs:2942:9:
      
      (the assertion that both blocks are marked and told; the first block's marks were right, the second block's were zero).
    • cargo clippy --locked -p darner-loader --all-targets -- -D warnings passes.
  • Changes a default: no. Stores that stay inside one block emit the same marks as before plus one taken jump; the new work happens only on a crossing store.
  • A/B result: not applicable — no default changes and no speed claim. The cost added to the common path is stated above (three instructions, no extra memory access) and was not measured in the game.
  • Gates run on that revision:
    • Gate 1, cargo clippy --workspace --all-targets --locked -- -D warnings: passes.
    • Gate 2, cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: passes.
    • Gate 3, cargo test --workspace --locked: not green on this machine, and not because of this change. Three pre-existing failures, each covered by an open pull request: darner-gpu stands still in parallel on the test deadlock #224 describes, and serially finishes 1772 passed and 1 failed (pipeline::make::tests::a_capped_draw_waits_once_then_drops, which passes on its own) beside the order-dependent flakes of #150; darner-shader fails emit::waterfall_tests::sampled_marks_restore_early_depth_only_when_safe_in_pixel_shaders, whose fixture #227 fixes. darner-loader's own suite is green.
  • Gameplay, when relevant: no gameplay run. The change is in a generated stub on the title's hot path; the test exercises the generated code, and the added instructions are three with no memory access.
  • Shader wave sizes tested, when relevant: not applicable.
  • Hardware dependence: none.
  • Coverage gaps: only the 16-byte store that crosses a boundary is exercised; a 32-byte store at +0xFFF0 (which also crosses) and a store exactly at the block start are not, though they take the same cold path. The stub's cost was reasoned from the emitted bytes, not measured in the game.

Checklist

  • I ran this change in the game, or say below why it can't be (docs, tests only).
    • Not run in gameplay: the generated stub is exercised directly by the test, and the change adds three instructions to the hot path with no measured game figure to offer. No FPS or gameplay claim is made.
  • This PR contains one change based on main.
  • The diff follows docs/asset-policy.md. (cargo xtask policy: 555 files ok.)
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: #314 touches crates/darner-loader/src/heap_sites.rs — it adds missing // SAFETY: comments in that file and in callhook.rs, so a conflict in comment lines is possible but unlikely. No open or closed issue mentions hooked, stub_tests or a store crossing a block boundary.
  • Replaces or overlaps: none found
## Contributor role (optional) - Fluxer ID: ## What it changes - The problem on main: `heap_sites::hooked` writes the write-watch notes for both ends of the title's store — `for last in [0, store.size - 1]` — but the block marks that follow run once from the **first** byte's offset, and the `TOLD` exchange (`xchg`) goes to that one cell: ```rust asm.put(&rr(MOV, a, d)); // d = the store's offset asm.put(&shift(SHR, d, 16)); // d = block index of the first byte ... mov byte [d], 1 // mark that block only ``` (`crates/darner-loader/src/heap_sites.rs:1073-1085` before this change). The software path for the same journal marks every block a write touches (`crates/darner-gpu/src/heap_hooks.rs:397-402`), and `take()` skips every block whose mark is 0 while `tell_watch()` tells only blocks whose `TOLD` byte is set. So a store whose last byte lands in the next block — a 16-byte store at +0xFFF8, say — left its notes there unread and that block untold to the watch, until some later store happened to mark it. The host's copy of the record was a round stale and a page we wrote through our view was one the watch had not been told about. - This change tests for the crossing first (`movzx` the offset's low 16 bits, `cmp` against `0x10000 - (size - 1)`, `jae`), and only in the cold `crossed` path marks and exchanges the first block and then the block after it. A one-byte store cannot cross and emits no test; a store inside one block — the common case — pays `movzx`, `cmp` and a jump not taken, with no further memory access. A store is 32 bytes at most, so it crosses at most one boundary: no loop is needed, and the report says what the cold path costs (~118 bytes of stub area per hooked store). ## How it was tested - Tested revision: `01c8e31` (one commit on `f3840d7`). - Commands and results: - `cargo test --locked -p darner-loader --lib` — 93 passed, 0 failed. - New test `heap_sites::stub_tests::a_store_across_a_block_boundary_marks_both_blocks` (`#[cfg(all(test, windows))]`, the module the other stub tests live in): it makes a site whose span is `vmovups [rcx-48h], xmm0` — sixteen bytes at +0xFFF8 of the test heap, eight of them in block 1 — runs it through the existing `runs()` harness, and asserts that the registers and ymm come out as the unhooked run's, that the title's memory is unchanged, that our view has what the title's would, that the sixteen bytes really crossed (+0xFFF8..+0x10008), that `(blocks[0], told[0], blocks[LINE], told[LINE]) == (1, 1, 1, 1)`, and that slots 0x7FF and 0x800 are noted with `cells.any == 1`. No title, no adapter, no fixture file: only `VirtualAlloc`. - Fail-before, with only the old one-mark sequence restored and the test kept: ``` thread 'heap_sites::stub_tests::a_store_across_a_block_boundary_marks_both_blocks' panicked at crates\darner-loader\src\heap_sites.rs:2942:9: ``` (the assertion that both blocks are marked and told; the first block's marks were right, the second block's were zero). - `cargo clippy --locked -p darner-loader --all-targets -- -D warnings` passes. - Changes a default: no. Stores that stay inside one block emit the same marks as before plus one taken jump; the new work happens only on a crossing store. - A/B result: not applicable — no default changes and no speed claim. The cost added to the common path is stated above (three instructions, no extra memory access) and was not measured in the game. - Gates run on that revision: - Gate 1, `cargo clippy --workspace --all-targets --locked -- -D warnings`: passes. - Gate 2, `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: passes. - Gate 3, `cargo test --workspace --locked`: **not green on this machine, and not because of this change.** Three pre-existing failures, each covered by an open pull request: `darner-gpu` stands still in parallel on the test deadlock #224 describes, and serially finishes 1772 passed and 1 failed (`pipeline::make::tests::a_capped_draw_waits_once_then_drops`, which passes on its own) beside the order-dependent flakes of #150; `darner-shader` fails `emit::waterfall_tests::sampled_marks_restore_early_depth_only_when_safe_in_pixel_shaders`, whose fixture #227 fixes. `darner-loader`'s own suite is green. - Gameplay, when relevant: no gameplay run. The change is in a generated stub on the title's hot path; the test exercises the generated code, and the added instructions are three with no memory access. - Shader wave sizes tested, when relevant: not applicable. - Hardware dependence: none. - Coverage gaps: only the 16-byte store that crosses a boundary is exercised; a 32-byte store at +0xFFF0 (which also crosses) and a store exactly at the block start are not, though they take the same cold path. The stub's cost was reasoned from the emitted bytes, not measured in the game. ## Checklist - [x] I ran this change in the game, or say below why it can't be (docs, tests only). - Not run in gameplay: the generated stub is exercised directly by the test, and the change adds three instructions to the hot path with no measured game figure to offer. No FPS or gameplay claim is made. - [x] This PR contains one change based on `main`. - [x] The diff follows `docs/asset-policy.md`. (`cargo xtask policy`: 555 files ok.) - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: #314 touches `crates/darner-loader/src/heap_sites.rs` — it adds missing `// SAFETY:` comments in that file and in `callhook.rs`, so a conflict in comment lines is possible but unlikely. No open or closed issue mentions `hooked`, `stub_tests` or a store crossing a block boundary. - Replaces or overlaps: none found
A hooked store marked only the block its first byte is in: the block number
was taken from the first byte's offset and the two marks were written once.
darner-gpu's journal marks every block a write touches (`heap_hooks::note`),
so a store that ended in the block after left the notes there unread and that
block untold to the watch until a later store happened to mark it: the host's
copy of the record stayed a round stale, and a page we had written through our
view was one the watch had not been told about.

The stub now tests whether the store's last byte reaches past its block (the
offset's low 16 bits plus the store's length) and, only then, marks and tells
the block after as well. A store inside one block pays a compare, a jump not
taken and no further memory access; the second block's marks sit where only a
crossing store goes. A store is 32 bytes at most, so there is at most one
boundary.

The test runs a 16-byte store at +0xFFF8 of the heap through a stub made of
its own bytes, with no title and no adapter, and asserts both blocks' BLOCKS
and TOLD bytes and both slots' notes; on the old code the marks come out
(1, 1, 0, 0). The two executed tests take a lock, since they share the memory
the cells are made over and the answer `resolve` gives.
This pull request can be merged automatically.
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 'heap-sites-cross-block:qnjk-heap-sites-cross-block'
git switch 'qnjk-heap-sites-cross-block'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'qnjk-heap-sites-cross-block'
git switch 'qnjk-heap-sites-cross-block'
git rebase 'main'
git switch 'main'
git merge --ff-only 'qnjk-heap-sites-cross-block'
git switch 'qnjk-heap-sites-cross-block'
git rebase 'main'
git switch 'main'
git merge --no-ff 'qnjk-heap-sites-cross-block'
git switch 'main'
git merge --squash 'qnjk-heap-sites-cross-block'
git switch 'main'
git merge --ff-only 'qnjk-heap-sites-cross-block'
git switch 'main'
git merge 'qnjk-heap-sites-cross-block'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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
TrustNo1/Darner!423
No description provided.