gpu(native): no copy-back into a pool the view left, or into a freed range (#339) #384

Merged
AnastasisKon merged 3 commits from upstream-pr/384 into main 2026-11-28 00:38:42 +00:00
AnastasisKon commented 2026-11-27 15:57:03 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by AnastasisKon on 2026-11-27T15:57:03Z.


What this changes

Refs #339 (not Closes: the GPU-write case below is still open).

Before a GPU-written texture is written back into title memory, the copy now checks that the address still belongs where the view was placed:

  • if the title unmapped the range and remapped it into another pool since the view was placed (Title::placed against the pool's own resolve), the copy is skipped and logged once per texture, instead of writing over whatever holds the range now;
  • with VICTORY_TRACE_FREED=1, a copy-back into a range the title's allocator freed is logged through native::freed::check and skipped; native::freed::overlaps is the new silent predicate behind that test.

Either write would land on whatever occupies the range now, which is one way a freed block's link ends up with a garbage offset — the fault at image+0x4dac5bf described on #339. docs/native-host.md gains the section that explains the fault, names the two host-side suspects, and says what stays open: a GPU write landing in a range after the title freed and reused it, which the host cannot see because the title's allocator reports no frees.

How it was tested

  • cargo test --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory: all pass — victory-hle 321, native_probe 17, victory-loader 29, 0 failed.
  • cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings: no warnings on this tree (victory-gpu re-checked).
  • cargo xtask policy: 387 files ok.
  • Ran the game: not possible on this Mac, which has no copy of the title. What a run has to show: with VICTORY_TRACE_FREED=1, copy-backs skipped and logged, no new "not copied back" warning for a texture that is still alive, and no crash at image+0x4dac5bf on a machine that hit it before.

Checklist

  • No game files, game code, disassembly listings or SDK files are included
  • Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md: the section says which suspect this closes and which stays open
  • New diagnostics are behind a VICTORY_* variable and documented: no new variable; the new skip logs under the existing VICTORY_TRACE_FREED=1
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5529514026 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/384). Originally posted by [AnastasisKon](https://github.com/AnastasisKon) on 2026-11-27T15:57:03Z. --- ## What this changes Refs #339 (not `Closes`: the GPU-write case below is still open). Before a GPU-written texture is written back into title memory, the copy now checks that the address still belongs where the view was placed: - if the title unmapped the range and remapped it into another pool since the view was placed (`Title::placed` against the pool's own `resolve`), the copy is skipped and logged once per texture, instead of writing over whatever holds the range now; - with `VICTORY_TRACE_FREED=1`, a copy-back into a range the title's allocator freed is logged through `native::freed::check` and skipped; `native::freed::overlaps` is the new silent predicate behind that test. Either write would land on whatever occupies the range now, which is one way a freed block's link ends up with a garbage offset — the fault at `image+0x4dac5bf` described on #339. `docs/native-host.md` gains the section that explains the fault, names the two host-side suspects, and says what stays open: a GPU write landing in a range after the title freed and reused it, which the host cannot see because the title's allocator reports no frees. ## How it was tested - [x] `cargo test --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory`: all pass — victory-hle 321, native_probe 17, victory-loader 29, 0 failed. - [x] `cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings`: no warnings on this tree (`victory-gpu` re-checked). - [x] `cargo xtask policy`: 387 files ok. - [ ] Ran the game: not possible on this Mac, which has no copy of the title. What a run has to show: with `VICTORY_TRACE_FREED=1`, copy-backs skipped and logged, no new "not copied back" warning for a texture that is still alive, and no crash at `image+0x4dac5bf` on a machine that hit it before. ## Checklist - [x] No game files, game code, disassembly listings or SDK files are included - [x] Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md: the section says which suspect this closes and which stays open - [x] New diagnostics are behind a `VICTORY_*` variable and documented: no new variable; the new skip logs under the existing `VICTORY_TRACE_FREED=1`
AnastasisKon commented 2026-11-27 16:20:04 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by AnastasisKon on 2026-11-27T16:20:04Z.


What this changes
Refs #339

Two commits. (1) Title::write_back no longer copies a GPU-written texture back into whatever holds its title address now: when the address no longer resolves to the pool recorded at view placement (Title::placed vs PoolTable::resolve), the copy is skipped and logged once per texture — one way a freed block's link ends up with a garbage offset (the image+0x4dac5bf crashes). (2) Under VICTORY_TRACE_FREED=1 (main's native::freed from #378), a copy-back into a range the allocator freed is logged via freed::check("texture copy-back") and skipped; adds freed::overlaps. docs/native-host.md records the #339 analysis: crash shape (r8 = rcx + 0x0000ffff00000000, low 32 bits a garbage offset), the two host-side suspects, and why only these are checkable on native (zero-copy GPU writes can only be seen at resolve time; no block-level write-watch exists here).

How it was tested
cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings
cargo xtask policy (387 files ok)
cargo test --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory — not run in this environment (no Mac)
Ran the game — not run; a world run with VICTORY_TRACE_FREED=1 would exercise the new warnings
Checklist
No game files, game code, disassembly listings or SDK files included
Analysis/guesses marked in docs/native-host.md (#339 section)
New log lines are behind VICTORY_TRACE_FREED / the existing once-per-texture back.logged set

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7223135863 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/384#issuecomment-7223135863). Originally posted by [AnastasisKon](https://github.com/AnastasisKon) on 2026-11-27T16:20:04Z. --- What this changes Refs #339 Two commits. (1) Title::write_back no longer copies a GPU-written texture back into whatever holds its title address now: when the address no longer resolves to the pool recorded at view placement (Title::placed vs PoolTable::resolve), the copy is skipped and logged once per texture — one way a freed block's link ends up with a garbage offset (the image+0x4dac5bf crashes). (2) Under VICTORY_TRACE_FREED=1 (main's native::freed from #378), a copy-back into a range the allocator freed is logged via freed::check("texture copy-back") and skipped; adds freed::overlaps. docs/native-host.md records the #339 analysis: crash shape (r8 = rcx + 0x0000ffff00000000, low 32 bits a garbage offset), the two host-side suspects, and why only these are checkable on native (zero-copy GPU writes can only be seen at resolve time; no block-level write-watch exists here). How it was tested cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings cargo xtask policy (387 files ok) cargo test --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory — not run in this environment (no Mac) Ran the game — not run; a world run with VICTORY_TRACE_FREED=1 would exercise the new warnings Checklist No game files, game code, disassembly listings or SDK files included Analysis/guesses marked in docs/native-host.md (#339 section) New log lines are behind VICTORY_TRACE_FREED / the existing once-per-texture back.logged set
alicans commented 2026-11-27 16:28:21 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by alicans on 2026-11-27T16:28:21Z.


Thanks for working on this. Two data points from a 24 GB M4 Pro:

  • None of my runs that crashed at image+0x4dac5bf set VICTORY_TEXTURE_BACK=1, and their logs have no copy-back lines. As far as I can tell, Title::write_back only has work with that switch on (Title::new calls collect_written with it), so neither change here would have acted in those runs.
  • A world load on main 4cf84c83 with #359 and VICTORY_TRACE_FREED=1 logged no use of freed title memory in 81 s. It did not reach the crash either; my memory limit stopped it.

Would a run with VICTORY_TEXTURE_BACK=1 and VICTORY_TRACE_FREED=1 be useful for #339, or is there a path with copy-back off you'd like traced?

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7223300488 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/384#issuecomment-7223300488). Originally posted by [alicans](https://github.com/alicans) on 2026-11-27T16:28:21Z. --- Thanks for working on this. Two data points from a 24 GB M4 Pro: - None of my runs that crashed at `image+0x4dac5bf` set `VICTORY_TEXTURE_BACK=1`, and their logs have no copy-back lines. As far as I can tell, `Title::write_back` only has work with that switch on (`Title::new` calls `collect_written` with it), so neither change here would have acted in those runs. - A world load on main `4cf84c83` with #359 and `VICTORY_TRACE_FREED=1` logged no use of freed title memory in 81 s. It did not reach the crash either; my memory limit stopped it. Would a run with `VICTORY_TEXTURE_BACK=1` and `VICTORY_TRACE_FREED=1` be useful for #339, or is there a path with copy-back off you'd like traced?
LakshanSivakumar commented 2026-11-27 16:44:28 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T16:44:28Z.


Reviewed and merged locally with 384/385 together. Tests, Windows clippy, policy and the release build all pass. I haven't pushed it yet because it still needs a game run, and the machine's GPU memory was held by another run when I tried. I'll land it after that run.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7223615389 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/384#issuecomment-7223615389). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-27T16:44:28Z. --- Reviewed and merged locally with 384/385 together. Tests, Windows clippy, policy and the release build all pass. I haven't pushed it yet because it still needs a game run, and the machine's GPU memory was held by another run when I tried. I'll land it after that run.
AnastasisKon commented 2026-11-27 22:04:55 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by AnastasisKon on 2026-11-27T22:04:55Z.


@alicans — you are right, and the code says exactly why.

Title::write_back is reached only from the loop over take_written(), and collecting_written() is true only when VICTORY_TEXTURE_BACK=1 (title.rs:248 sets it, host.rs:895 reads it). With the switch off the host never copies a texture back into title memory at all — the one copy_nonoverlapping into a pool mapping in native/textures.rs is inside write_back — so neither skip here can run, never mind be what a crashing run needed. The freed-range half needs a second switch on top: freed::overlaps answers false unless VICTORY_TRACE_FREED=1, so it acts only with both set.

So the honest scope of this PR: it hardens the copy-back path for runs that turn VICTORY_TEXTURE_BACK on — the runs #46 and #449 want to become the default — and it is not an explanation of the crashes in your logs. I have added that sentence to the bullet in docs/native-host.md (f57aac7f) so nobody reads it as a fix for a default run.

What your two data points do settle, though, is worth having on #339: with the switch off, a copy-back cannot be the writer that tramples a freed block's link, which leaves the other suspect — GPU work still writing a range after the title freed and reused it. That is the case the docs list as still open, and catching it needs the title's allocator to report frees, which no interface today provides.

@LakshanSivakumar — the branch has one commit after the copy you merged locally: f57aac7f, the docs sentence above. Nothing else changed, so the merge you tested stands apart from that line; pull it before landing. Your point that this needs a game run still holds, and now the run worth doing is a VICTORY_TEXTURE_BACK=1 world load: with VICTORY_TRACE_FREED=1 too, the useful evidence is a "not copied back"/"skipped" line, or its absence for textures that are alive.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7229010806 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/384#issuecomment-7229010806). Originally posted by [AnastasisKon](https://github.com/AnastasisKon) on 2026-11-27T22:04:55Z. --- @alicans — you are right, and the code says exactly why. `Title::write_back` is reached only from the loop over `take_written()`, and `collecting_written()` is true only when `VICTORY_TEXTURE_BACK=1` (`title.rs:248` sets it, `host.rs:895` reads it). With the switch off the host never copies a texture back into title memory at all — the one `copy_nonoverlapping` into a pool mapping in `native/textures.rs` is inside `write_back` — so neither skip here can run, never mind be what a crashing run needed. The freed-range half needs a second switch on top: `freed::overlaps` answers false unless `VICTORY_TRACE_FREED=1`, so it acts only with both set. So the honest scope of this PR: it hardens the copy-back path for runs that turn `VICTORY_TEXTURE_BACK` on — the runs #46 and #449 want to become the default — and it is not an explanation of the crashes in your logs. I have added that sentence to the bullet in `docs/native-host.md` (`f57aac7f`) so nobody reads it as a fix for a default run. What your two data points do settle, though, is worth having on #339: with the switch off, a copy-back cannot be the writer that tramples a freed block's link, which leaves the other suspect — GPU work still writing a range after the title freed and reused it. That is the case the docs list as still open, and catching it needs the title's allocator to report frees, which no interface today provides. @LakshanSivakumar — the branch has one commit after the copy you merged locally: `f57aac7f`, the docs sentence above. Nothing else changed, so the merge you tested stands apart from that line; pull it before landing. Your point that this needs a game run still holds, and now the run worth doing is a `VICTORY_TEXTURE_BACK=1` world load: with `VICTORY_TRACE_FREED=1` too, the useful evidence is a "not copied back"/"skipped" line, or its absence for textures that are alive.
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
LakshanSivakumar/Native-Victory-macOS!384
No description provided.