WIP: fix(nvn): the array binds check the count and the whole guest array before reading it #2018

Draft
siantm wants to merge 4 commits from siantm/Longshot:fix/nvn-bind-array-span into main
Contributor

Summary

The native NVN layer's array binds read the game's array before checking that all of it lies in the guest window.
BindTextures, BindImages, BindSeparateTextures, BindSeparateSamplers, their Deferred forms (8-byte handles)
and BindVertexBuffers (16-byte buffer ranges) passed the array as P(4) / P(3), which checks one byte of it
(NVN_OBJ_MIN). BindTextures and BindTexturesDeferred also let HOS_NVN_WATCH_TEX's watch read the handles before
bind_handles checked their count. So an array that starts inside the window and runs past its end was read past it:
host memory, or a crash, instead of the stop with the window's line that every other array argument gets (and that
the Deferred array's second read at submit already gets). Two more holes, found in review of this PR: a positive count
at guest address 0 (P/PN give NULL for 0) reached the watch or the record's memcpy, and BindVertexBuffers and
BindVertexBuffer checked their first and count only as first + count > 16, an int sum the game's values can
overflow (first 1 with a count of INT_MAX passed it).

This checks the count first (0 to 32 handles, as bind_handles records, in bind_array; for vertex buffers, in
vbufs_check, first 0 to 16 and the count 0 to 16 - first, each compared alone, which both vertex-buffer entry points
go through) and then the whole array in the window (bind_span: all 8 x count or 16 x count bytes, and guest address 0
only with a count of 0), before the watch or the record reads an element. A count of 0 at address 0 is recorded empty,
and nothing is copied from NULL. The result: such an array stops the game with its line ("guest memory ... is outside
the guest window", "bind of 2 array elements at guest address 0", "BindVertexBuffers 1+2147483647"); arrays inside the
window are recorded as before.

How to see it on main: not in normal play (the game passes arrays inside the window). Game-free:
python -I -B tests/test_nvn_bind_span.py with main's host/nvn_native.c makes these binds read past the window's
end, copy from a null array or accept an overflowing first + count (50 of 104 cases fail, see Measurements).

Found by BeanBot's review of #1954 (point 2, "count and span before the read")
and named inherited in futondestroyer's review of #1954
("The missing full-span check in the bind-array wrappers is inherited"); the null array and the overflowing sum by
BeanBot's review of this PR.

Read first: bind_span, vbufs_check, bind_array and the nvnCommandBufferBind* entry points in
host/nvn_native.c; tests/test_nvn_bind_span.py.

Overlap: #1954 (perf/nvn-bind-fields @ fe1dd999) rewrites bind_handles and the watch just above these lines; the two
merge without a conflict (checked with git merge-tree at this head), in either order. Proposed order: this first;
#1954 is rebased when it lands (its watch's own 0 to 32 check then only repeats this one). The watch's own races and its
frame walk (BeanBot's points L3 and L4 here) are #1954's: its 519f5c7e and fe1dd999 fix them; this PR does not touch
the watch. Left alone, pre-existing on main: resolve_deferred writes each submit's resolved handles into the
shared recording (L5), for a separate change.

Testing

Tested: Windows 11, i7-9700, RTX 4060 (NVIDIA 610.88), llvm-mingw clang 23.1.5; game-free checks only. This has
NOT run in the game on this PC (Still to test). Each check ran in a hidden console of its own, in build jobs of the PC's
job queue (fb2-2018-1, and fb2-2018-2 for main's column), on main @ b62932a9, head 555b5160:

  • python -I -B tests/test_nvn_bind_span.py (no --cc: LONGSHOT_MINGW's clang; and again with CC a command
    with a flag, clang.exe --target=x86_64-w64-mingw32): "PASS: every array bind checks its count and its whole span
    before it reads the array (104 cases)": the 9 array entry points x edge, zero, zero-null, null, past, order, over,
    huge, negative (BindVertexBuffers also far), BindVertexBuffer's edge, over, far and negative (first 15, 16, INT_MAX,
    -1), and BindTextures(Deferred) again with HOS_NVN_WATCH_TEX=beef. A bind kept is checked whole (opcode, size, stage/first, count, a Deferred bind's source,
    then the elements); a stop must give its own line and leave no record.
  • The same test at its commit 45d430a8 (the PR's first fix, without 555b5160): 24 of 104 fail: a count of 2 at guest
    address 0 and a count of 0 there, for every entry point and watch mode (22: memcpy given NULL, or the watch's read
    of address 0), and BindVertexBuffers with first 1 and a count of INT_MAX (an access fault: first + count
    overflowed and passed) and with first INT_MAX (the array read before a stop). BindVertexBuffer's own first
    INT_MAX passed there too (the compiler folded its first + 1 > 16); it is checked now without relying on that.
  • Changes of the fix that the test must catch, each run in a copy of the tree: a Deferred bind recording its source as 0
    (BindImagesDeferred): 2 fail (edge, zero: "source 0, not ..."); the span checked before the count: 30 fail (over,
    huge, negative stop with the window's line, not the count's); the first handle read before the span check: 10 fail
    (order: "read the array ... before the bind checked it"; past still passes, as the review said it would).
  • host/nvn_native.c compiled (-fsyntax-only) with the host build's own flags, with and without Tracy: no error.
  • python -I -B tests/run_all.py --strict --changed origin/main (RX_CPM set): 21 passed, 2 failed, 1 skipped
    (test_tracy_funcs.py: Linux only). The 2 failing (test_dev_packs.py, test_host_guest_writes.py) fail on
    main @ b62932a9 too, run by name from a main worktree in the same job, with the same messages; none of the 24 failed
    for want of the Vulkan headers.
  • Earlier, the PR's first two commits (on main @ f1f2dc5b, jobs fb3-sep-1 and fb3-sep-2): their first test passed at
    the fix and failed 13 of 55 at the test commit.

Still to test:

  • Windows 11, this PC (i7-9700, RTX 4060 (NVIDIA 610.88)): Castle Town's market and a load into Hyrule Field, two minutes,
    this PR against main, expecting no stop, the same picture and frame times within the runs' spread (by hand)
  • Linux: build the host and run python3 -I -B tests/test_nvn_bind_span.py (and with CC='ccache clang'), expecting
    it to pass (by hand)
  • Linux with NVIDIA and with AMD (Mesa RADV), Windows with an AMD GPU, and the Steam Deck: Castle Town's market and
    Hyrule Field, two minutes each, this PR against main, expecting no stop and the same picture (the Test Farm: play
    Castle Town's market and Hyrule Field on Linux, Windows with an AMD GPU and the Steam Deck, this PR against main,
    looking for a stop with "outside the guest window", "at guest address 0" or "BindVertexBuffers", or a changed picture)
  • mise run lint (ruff, pre-commit) and mise run test on a PC with ruff and pre-commit (none here), expecting no
    failure but main's (by hand; CI runs them on the head too)

Measurements

Verdict: the bug is gone in the game-free fixture: with main's host/nvn_native.c 50 of the 104 cases of this
PR's tests/test_nvn_bind_span.py read past the window's end, read the array before checking it, copy from a null
array or accept an overflowing first + count; with this PR none (104 pass). Nothing about speed is claimed or measured.

Setup: Windows 11, i7-9700, llvm-mingw clang 23.1.5; python -I -B tests/test_nvn_bind_span.py (the head's test),
one run each, game-free, in build jobs of the PC's job queue: Before, main's host/nvn_native.c and host/hos.h
with the head's test (fb2-2018-2); After, the head (fb2-2018-1). The fixture compiles the real entry points over a
guest window followed by no-access pages; each case is a process of its own.

cases failing, of 104 Before: main @ b62932a9 After: this PR @ 555b5160
past (read past the window's end) 11 0
order (array read before its span check) 11 0
over, huge (watch read before the count check; vertex-buffer count overflow) 5 0
far (vertex-buffer first + count overflow) 1 0
null (a count of 2 at address 0) 11 0
zero-null (a count of 0 at address 0: memcpy given NULL) 11 0
all 50 0
Before: test_nvn_bind_span: FAILED 50 of 104: BindTextures zero-null, BindTextures null, BindTextures past, ...
After:  PASS: every array bind checks its count and its whole span before it reads the array (104 cases)

Risks

An array the game passes that runs past the guest window's end, which main read past, now stops the game with the
window's line; if the game ever did that on purpose, it would stop here where main read host memory (and could crash
or draw with garbage handles). So does a positive count with a null array (main read address 0 of the host: a crash),
and a vertex-buffer first and count beyond 16 that main let through when first + count overflowed. Arrays inside the
window, a count of 0, and the count's own stop (more than 32 handles, more than 16 ranges) are unchanged.
No switch: a revert gives back main's code. Players notice nothing after updating: no settings, saves or caches change.

Checklist

Ready for review

  • One topic, on current main; the title is a Conventional Commit.
  • Tested on my own PC, in the game for a change the game runs, as Testing says.
  • mise run test passes (any failure that also fails on main is named).
  • Measured against main, or Measurements says why there is nothing to measure.
  • No game files, build output or hand-edited generated files; third_party/ untouched; nothing GPL-incompatible.

When it applies

  • A fix: a game-free test that fails on main and passes here.
  • Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths.
  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.

Ready to merge

  • Everything in Still to test has run, its results in Tested.
  • Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers").
  • Approved in review, every comment answered.
  • CI passes on the head; on current main, measured again or a comment says why the numbers still hold.
## Summary The native NVN layer's array binds read the game's array before checking that all of it lies in the guest window. `BindTextures`, `BindImages`, `BindSeparateTextures`, `BindSeparateSamplers`, their `Deferred` forms (8-byte handles) and `BindVertexBuffers` (16-byte buffer ranges) passed the array as `P(4)` / `P(3)`, which checks one byte of it (`NVN_OBJ_MIN`). `BindTextures` and `BindTexturesDeferred` also let `HOS_NVN_WATCH_TEX`'s watch read the handles before `bind_handles` checked their count. So an array that starts inside the window and runs past its end was read past it: host memory, or a crash, instead of the stop with the window's line that every other array argument gets (and that the Deferred array's second read at submit already gets). Two more holes, found in review of this PR: a positive count at guest address 0 (`P`/`PN` give NULL for 0) reached the watch or the record's `memcpy`, and `BindVertexBuffers` and `BindVertexBuffer` checked their first and count only as `first + count > 16`, an `int` sum the game's values can overflow (first 1 with a count of `INT_MAX` passed it). This checks the count first (0 to 32 handles, as `bind_handles` records, in `bind_array`; for vertex buffers, in `vbufs_check`, first 0 to 16 and the count 0 to 16 - first, each compared alone, which both vertex-buffer entry points go through) and then the whole array in the window (`bind_span`: all 8 x count or 16 x count bytes, and guest address 0 only with a count of 0), before the watch or the record reads an element. A count of 0 at address 0 is recorded empty, and nothing is copied from NULL. The result: such an array stops the game with its line ("guest memory ... is outside the guest window", "bind of 2 array elements at guest address 0", "BindVertexBuffers 1+2147483647"); arrays inside the window are recorded as before. How to see it on `main`: not in normal play (the game passes arrays inside the window). Game-free: `python -I -B tests/test_nvn_bind_span.py` with `main`'s `host/nvn_native.c` makes these binds read past the window's end, copy from a null array or accept an overflowing first + count (50 of 104 cases fail, see Measurements). Found by BeanBot's review of #1954 ([point 2, "count and span before the read"](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1954#issuecomment-61943)) and named inherited in futondestroyer's [review of #1954](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1954#issuecomment-62883) ("The missing full-span check in the bind-array wrappers is inherited"); the null array and the overflowing sum by BeanBot's [review of this PR](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2018#issuecomment-64182). Read first: `bind_span`, `vbufs_check`, `bind_array` and the `nvnCommandBufferBind*` entry points in `host/nvn_native.c`; `tests/test_nvn_bind_span.py`. Overlap: #1954 (`perf/nvn-bind-fields` @ fe1dd999) rewrites `bind_handles` and the watch just above these lines; the two merge without a conflict (checked with `git merge-tree` at this head), in either order. Proposed order: this first; #1954 is rebased when it lands (its watch's own 0 to 32 check then only repeats this one). The watch's own races and its frame walk (BeanBot's points L3 and L4 here) are #1954's: its 519f5c7e and fe1dd999 fix them; this PR does not touch the watch. Left alone, pre-existing on `main`: `resolve_deferred` writes each submit's resolved handles into the shared recording (L5), for a separate change. ## Testing **Tested:** Windows 11, i7-9700, RTX 4060 (NVIDIA 610.88), llvm-mingw clang 23.1.5; game-free checks only. This has NOT run in the game on this PC (Still to test). Each check ran in a hidden console of its own, in build jobs of the PC's job queue (fb2-2018-1, and fb2-2018-2 for `main`'s column), on `main` @ b62932a9, head 555b5160: - `python -I -B tests/test_nvn_bind_span.py` (no `--cc`: `LONGSHOT_MINGW`'s clang; and again with `CC` a command with a flag, `clang.exe --target=x86_64-w64-mingw32`): "PASS: every array bind checks its count and its whole span before it reads the array (104 cases)": the 9 array entry points x edge, zero, zero-null, null, past, order, over, huge, negative (BindVertexBuffers also far), BindVertexBuffer's edge, over, far and negative (first 15, 16, `INT_MAX`, -1), and `BindTextures(Deferred)` again with `HOS_NVN_WATCH_TEX=beef`. A bind kept is checked whole (opcode, size, stage/first, count, a Deferred bind's source, then the elements); a stop must give its own line and leave no record. - The same test at its commit 45d430a8 (the PR's first fix, without 555b5160): 24 of 104 fail: a count of 2 at guest address 0 and a count of 0 there, for every entry point and watch mode (22: `memcpy` given NULL, or the watch's read of address 0), and BindVertexBuffers with first 1 and a count of `INT_MAX` (an access fault: `first + count` overflowed and passed) and with first `INT_MAX` (the array read before a stop). BindVertexBuffer's own first `INT_MAX` passed there too (the compiler folded its `first + 1 > 16`); it is checked now without relying on that. - Changes of the fix that the test must catch, each run in a copy of the tree: a Deferred bind recording its source as 0 (`BindImagesDeferred`): 2 fail (edge, zero: "source 0, not ..."); the span checked before the count: 30 fail (over, huge, negative stop with the window's line, not the count's); the first handle read before the span check: 10 fail (order: "read the array ... before the bind checked it"; past still passes, as the review said it would). - `host/nvn_native.c` compiled (`-fsyntax-only`) with the host build's own flags, with and without Tracy: no error. - `python -I -B tests/run_all.py --strict --changed origin/main` (`RX_CPM` set): 21 passed, 2 failed, 1 skipped (`test_tracy_funcs.py`: Linux only). The 2 failing (`test_dev_packs.py`, `test_host_guest_writes.py`) fail on `main` @ b62932a9 too, run by name from a `main` worktree in the same job, with the same messages; none of the 24 failed for want of the Vulkan headers. - Earlier, the PR's first two commits (on `main` @ f1f2dc5b, jobs fb3-sep-1 and fb3-sep-2): their first test passed at the fix and failed 13 of 55 at the test commit. **Still to test:** - Windows 11, this PC (i7-9700, RTX 4060 (NVIDIA 610.88)): Castle Town's market and a load into Hyrule Field, two minutes, this PR against `main`, expecting no stop, the same picture and frame times within the runs' spread (by hand) - Linux: build the host and run `python3 -I -B tests/test_nvn_bind_span.py` (and with `CC='ccache clang'`), expecting it to pass (by hand) - Linux with NVIDIA and with AMD (Mesa RADV), Windows with an AMD GPU, and the Steam Deck: Castle Town's market and Hyrule Field, two minutes each, this PR against `main`, expecting no stop and the same picture (the Test Farm: play Castle Town's market and Hyrule Field on Linux, Windows with an AMD GPU and the Steam Deck, this PR against main, looking for a stop with "outside the guest window", "at guest address 0" or "BindVertexBuffers", or a changed picture) - `mise run lint` (ruff, pre-commit) and `mise run test` on a PC with ruff and pre-commit (none here), expecting no failure but `main`'s (by hand; CI runs them on the head too) ## Measurements **Verdict:** the bug is gone in the game-free fixture: with `main`'s `host/nvn_native.c` 50 of the 104 cases of this PR's `tests/test_nvn_bind_span.py` read past the window's end, read the array before checking it, copy from a null array or accept an overflowing first + count; with this PR none (104 pass). Nothing about speed is claimed or measured. Setup: Windows 11, i7-9700, llvm-mingw clang 23.1.5; `python -I -B tests/test_nvn_bind_span.py` (the head's test), one run each, game-free, in build jobs of the PC's job queue: Before, `main`'s `host/nvn_native.c` and `host/hos.h` with the head's test (fb2-2018-2); After, the head (fb2-2018-1). The fixture compiles the real entry points over a guest window followed by no-access pages; each case is a process of its own. | cases failing, of 104 | Before: main @ b62932a9 | After: this PR @ 555b5160 | |---|---|---| | past (read past the window's end) | 11 | 0 | | order (array read before its span check) | 11 | 0 | | over, huge (watch read before the count check; vertex-buffer count overflow) | 5 | 0 | | far (vertex-buffer first + count overflow) | 1 | 0 | | null (a count of 2 at address 0) | 11 | 0 | | zero-null (a count of 0 at address 0: `memcpy` given NULL) | 11 | 0 | | all | 50 | 0 | ``` Before: test_nvn_bind_span: FAILED 50 of 104: BindTextures zero-null, BindTextures null, BindTextures past, ... After: PASS: every array bind checks its count and its whole span before it reads the array (104 cases) ``` ## Risks An array the game passes that runs past the guest window's end, which `main` read past, now stops the game with the window's line; if the game ever did that on purpose, it would stop here where `main` read host memory (and could crash or draw with garbage handles). So does a positive count with a null array (`main` read address 0 of the host: a crash), and a vertex-buffer first and count beyond 16 that `main` let through when first + count overflowed. Arrays inside the window, a count of 0, and the count's own stop (more than 32 handles, more than 16 ranges) are unchanged. No switch: a revert gives back `main`'s code. Players notice nothing after updating: no settings, saves or caches change. ## Checklist ### Ready for review - [x] One topic, on current `main`; the title is a Conventional Commit. - [ ] Tested on my own PC, in the game for a change the game runs, as Testing says. - [ ] `mise run test` passes (any failure that also fails on `main` is named). - [x] Measured against `main`, or Measurements says why there is nothing to measure. - [x] No game files, build output or hand-edited generated files; `third_party/` untouched; nothing GPL-incompatible. ### When it applies - [x] A fix: a game-free test that fails on `main` and passes here. - [ ] Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths. - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. ### Ready to merge - [ ] Everything in Still to test has run, its results in Tested. - [ ] Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers"). - [ ] Approved in review, every comment answered. - [ ] CI passes on the head; on current `main`, measured again or a comment says why the numbers still hold.
tests/test_nvn_bind_span.py runs host/nvn_native.c's own array bind entry points (BindTextures, BindImages,
BindSeparateTextures, BindSeparateSamplers, their Deferred forms, BindVertexBuffers), with the real gw, P/PN, allocator,
bind_handles, bind_vbufs and HOS_NVN_WATCH_TEX's watch, over a guest window followed by pages no access is allowed
to. An array whose second element lies past the window's end, and a count of 33 with the watch on, must stop the game
before anything reads past the end. On main each entry checks only the array's first byte (P), so both fault instead.

Assisted-by: Claude Code
fix(nvn): the array binds check the count and the whole guest array before reading it
Some checks failed
Website / Website on Cloudflare (pull_request_target) Has been skipped
PR description / Description follows the template (pull_request_target) Successful in 19s
CI / Conventional Commit title (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been cancelled
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been cancelled
CI / Workflow lint (actionlint) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
Guide screenshots / Guide images and local UI source review (pull_request) Has been cancelled
Third-party parts / As their notes say (pull_request) Has been cancelled
Third-party parts / The same as upstream (pull_request) Has been cancelled
cfd24dda9b
The handle-array entry points (BindTextures, BindImages, BindSeparateTextures, BindSeparateSamplers and their
Deferred forms) passed P(4), which checks one byte of the game's array, and BindTextures(Deferred) let the
HOS_NVN_WATCH_TEX watch read the handles before bind_handles checked their count. bind_array now checks the count
(0 to 32) first and then all 8 x count bytes in the guest window (PN), before the watch or the record reads any.
BindVertexBuffers passed P(3) the same way; its 16-byte ranges are now checked whole too (the count is still checked
by bind_vbufs before it reads). An array that runs off the window's end now stops the game with the window's line,
as the submit-time read of a Deferred array already did, instead of reading past it.

Assisted-by: Claude Code
BeanBot left a comment

Recommendation: Request changes. The new checks catch non-null arrays that cross the guest window, but a null array still reaches a host read and the vertex count check can overflow. Both need to reject the input before recording or reading it.

Adds guest-window checks to nine NVN array binds so malformed calls can stop with a diagnostic instead of faulting. It reuses the existing range helper and preserves the command format, but the vertex wrapper uses a zero-byte span for invalid counts and relies on a separate, unsafe count check.

@fix will do (10)

  1. L1 host/nvn_native.c:2183: PN returns NULL without validating guest address zero. (more on the line)
  2. L2 host/nvn_native.c:2119: The wrapper checks zero bytes when the count exceeds 16, then depends on the inherited signed addition in bind_vbufs. (more on the line)
  3. L3 (minor) host/nvn_native.c:2158: The inherited watch still races when independent command-buffer threads initialize want on their first texture binds, even with the switch off. With the watch enabled, two matching chains can also race on seen and nseen, including at the table's last slot. Initialize the setting once with synchronized publication and protect the lookup and insertion together.
  4. L4 (minor) host/nvn_native.c:2163: The inherited watch checks the frame pointer's start before reading two words. If a handle matches and the frame pointer is eight bytes below the guest-window end, the saved return address is read outside the window. Validate the full frame pair against the runtime window and stop walking on an invalid frame.
  5. L5 (minor) host/nvn_native.c:2511: The inherited deferred resolver writes new handles into the shared recording. With asynchronous replay, submitting the same recording again before the first submit drains can overwrite the first submit's handles while replay reads them. Give each queued submit its own resolved payload and retain it until replay finishes.
  6. L6 (minor) tests/test_nvn_bind_span.py:210: The over and negative cases accept any fatal diagnostic because the expected string is empty. (more on the line)
  7. L7 (minor) tests/test_nvn_bind_span.py:201: The success check only compares the trailing bytes. (more on the line)
  8. L8 (minor) tests/test_nvn_bind_span.py:44: CC works for a single executable, but a common setting such as CC='ccache clang' is passed to subprocess as one nonexistent executable. (more on the line)
  9. L9 (minor) tests/test_nvn_bind_span.py:94: The POSIX signal handler calls snprintf, which is unsafe in a signal handler, and sends its unchecked return value to write. (more on the line)
  10. L10 (minor): Measurements says there is nothing to measure, but the standards ask a fix to put its before-and-after proof there. Move or copy the fixture comparison into Measurements with the tested commits and setup, update it for the final head when rerun, and remove the unverified claim of one extra compare per bind.

For the author (1) (a run, a measurement or a decision)

  • L11 (minor): The changed host path has only pre-rebase game-free evidence. The rules require an author gameplay run before opening unless an override was authorized, and Tier 1 coverage before merge. Record the author run and the planned Tier 1 Windows, Linux and Steam Deck results on the revised head, along with the Linux fixture and host builds, UTF-8 path coverage and the full checks. The current description reports a filtered suite and no lint result.

Checks: Still running: CI / Conventional Commit title (pull_request), CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request), CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request), CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request), CI / Workflow lint (actionlint) (pull_request), CI / Website build and checks (pull_request), Guide screenshots / Guide images and local UI source review (pull_request), Third-party parts / As their notes say (pull_request), Third-party parts / The same as upstream (pull_request).

More: does it help, other systems, switches, what was read

Does it help: Yes for the tested end-of-window cases. The PR reports 13 faults in 55 fixture cases on the test-only commit and 55 of 55 passing with the fix on the same Windows PC, before the rebase. The current head, Linux, and normal gameplay remain unverified. No performance gain is claimed.

Other systems: The host bind path runs before Vulkan replay on Windows, Linux and Steam Deck, regardless of GPU vendor. Valid inputs take the same recording path, with the same payload layouts and submit-time deferred reads. The fixture does not cover rendering, frame pacing or repeated submissions. Windows still commits reserved guest pages on first touch, and Linux maps the guest window read/write. The new check verifies the window boundary, with no change to those memory policies. The diff has no path into saves, caches, audio, input or the Launcher.

Switches: None added, changed or removed. HOS_NVN_WATCH_TEX stays off by default in gpu/vk/switches.def. The fixture explicitly clears it and enables it for the two texture entry points that use it.

Reviewed: cfd24dda9b against 40bfceb055: Read tests/test_nvn_bind_span.py in full, the complete diff and both commits, AGENTS.md, CONTRIBUTING.md, the Forgejo template and docs/dev/standards.md. No docs/review-checklist.md exists in the checkout. Read the changed host/nvn_native.c blocks and traced the guest-window helpers, command allocation, dispatch, deferred submit and replay consumers. Skimmed nearby unchanged NVN code, existing source checks and CI configuration. Unrelated portions of host/nvn_native.c were not read line by line. No generated files or translations changed. The run evidence is from the PR description, with no comments or reviews supplied.; nothing run (a code review).

Checked and fine:

  • For non-null handle arrays, the count cap precedes the byte multiplication and the watch. The shared range helper uses subtraction and accepts arrays ending exactly at the boundary.
  • The fixture compiles the real bind bodies with synthetic data and exercises the original guard-page failure with the watch both off and on. The compiler and child processes are expected for this test, and the diff adds no downloads.
  • One topic, a Conventional Commit title after the WIP prefix, and Assisted-by trailers on both commits. The diff adds no game-derived data, build outputs, generated edits, third-party changes or renderer API changes.

Testing: Read-only review. No build, fixture, project script or game run was performed. The author reports two filtered-suite failures also present on the older base and one Linux-only skip. Head CI is still running, with no head-only failure reported.

Overlaps and merge order: #1954 rewrites bind_handles and the texture watch. Use #2018 first, then rebase #1954 and rerun the combined fixture, including both watch modes. The PR reports a conflict-free merge in either order. The sibling revision was unavailable in this checkout.


Automated code review by BeanBot (gpt-6.2-sol), on the pull request's opening. Nothing was run; a maintainer decides.

**Recommendation: Request changes.** The new checks catch non-null arrays that cross the guest window, but a null array still reaches a host read and the vertex count check can overflow. Both need to reject the input before recording or reading it. Adds guest-window checks to nine NVN array binds so malformed calls can stop with a diagnostic instead of faulting. It reuses the existing range helper and preserves the command format, but the vertex wrapper uses a zero-byte span for invalid counts and relies on a separate, unsafe count check. **`@fix` will do (10)** 1. **L1** `host/nvn_native.c:2183`: PN returns NULL without validating guest address zero. (more on the line) 2. **L2** `host/nvn_native.c:2119`: The wrapper checks zero bytes when the count exceeds 16, then depends on the inherited signed addition in bind_vbufs. (more on the line) 3. **L3** (minor) `host/nvn_native.c:2158`: The inherited watch still races when independent command-buffer threads initialize want on their first texture binds, even with the switch off. With the watch enabled, two matching chains can also race on seen and nseen, including at the table's last slot. Initialize the setting once with synchronized publication and protect the lookup and insertion together. 4. **L4** (minor) `host/nvn_native.c:2163`: The inherited watch checks the frame pointer's start before reading two words. If a handle matches and the frame pointer is eight bytes below the guest-window end, the saved return address is read outside the window. Validate the full frame pair against the runtime window and stop walking on an invalid frame. 5. **L5** (minor) `host/nvn_native.c:2511`: The inherited deferred resolver writes new handles into the shared recording. With asynchronous replay, submitting the same recording again before the first submit drains can overwrite the first submit's handles while replay reads them. Give each queued submit its own resolved payload and retain it until replay finishes. 6. **L6** (minor) `tests/test_nvn_bind_span.py:210`: The over and negative cases accept any fatal diagnostic because the expected string is empty. (more on the line) 7. **L7** (minor) `tests/test_nvn_bind_span.py:201`: The success check only compares the trailing bytes. (more on the line) 8. **L8** (minor) `tests/test_nvn_bind_span.py:44`: CC works for a single executable, but a common setting such as CC='ccache clang' is passed to subprocess as one nonexistent executable. (more on the line) 9. **L9** (minor) `tests/test_nvn_bind_span.py:94`: The POSIX signal handler calls snprintf, which is unsafe in a signal handler, and sends its unchecked return value to write. (more on the line) 10. **L10** (minor): Measurements says there is nothing to measure, but the standards ask a fix to put its before-and-after proof there. Move or copy the fixture comparison into Measurements with the tested commits and setup, update it for the final head when rerun, and remove the unverified claim of one extra compare per bind. **For the author (1)** (a run, a measurement or a decision) - **L11** (minor): The changed host path has only pre-rebase game-free evidence. The rules require an author gameplay run before opening unless an override was authorized, and Tier 1 coverage before merge. Record the author run and the planned Tier 1 Windows, Linux and Steam Deck results on the revised head, along with the Linux fixture and host builds, UTF-8 path coverage and the full checks. The current description reports a filtered suite and no lint result. **Checks:** Still running: `CI / Conventional Commit title (pull_request)`, `CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request)`, `CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request)`, `CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request)`, `CI / Workflow lint (actionlint) (pull_request)`, `CI / Website build and checks (pull_request)`, `Guide screenshots / Guide images and local UI source review (pull_request)`, `Third-party parts / As their notes say (pull_request)`, `Third-party parts / The same as upstream (pull_request)`. <details> <summary>More: does it help, other systems, switches, what was read</summary> **Does it help:** Yes for the tested end-of-window cases. The PR reports 13 faults in 55 fixture cases on the test-only commit and 55 of 55 passing with the fix on the same Windows PC, before the rebase. The current head, Linux, and normal gameplay remain unverified. No performance gain is claimed. **Other systems:** The host bind path runs before Vulkan replay on Windows, Linux and Steam Deck, regardless of GPU vendor. Valid inputs take the same recording path, with the same payload layouts and submit-time deferred reads. The fixture does not cover rendering, frame pacing or repeated submissions. Windows still commits reserved guest pages on first touch, and Linux maps the guest window read/write. The new check verifies the window boundary, with no change to those memory policies. The diff has no path into saves, caches, audio, input or the Launcher. **Switches:** None added, changed or removed. HOS_NVN_WATCH_TEX stays off by default in gpu/vk/switches.def. The fixture explicitly clears it and enables it for the two texture entry points that use it. **Reviewed:** cfd24dda9b4b against 40bfceb0552e: Read tests/test_nvn_bind_span.py in full, the complete diff and both commits, AGENTS.md, CONTRIBUTING.md, the Forgejo template and docs/dev/standards.md. No docs/review-checklist.md exists in the checkout. Read the changed host/nvn_native.c blocks and traced the guest-window helpers, command allocation, dispatch, deferred submit and replay consumers. Skimmed nearby unchanged NVN code, existing source checks and CI configuration. Unrelated portions of host/nvn_native.c were not read line by line. No generated files or translations changed. The run evidence is from the PR description, with no comments or reviews supplied.; nothing run (a code review). **Checked and fine:** - For non-null handle arrays, the count cap precedes the byte multiplication and the watch. The shared range helper uses subtraction and accepts arrays ending exactly at the boundary. - The fixture compiles the real bind bodies with synthetic data and exercises the original guard-page failure with the watch both off and on. The compiler and child processes are expected for this test, and the diff adds no downloads. - One topic, a Conventional Commit title after the WIP prefix, and Assisted-by trailers on both commits. The diff adds no game-derived data, build outputs, generated edits, third-party changes or renderer API changes. **Testing:** Read-only review. No build, fixture, project script or game run was performed. The author reports two filtered-suite failures also present on the older base and one Linux-only skip. Head CI is still running, with no head-only failure reported. **Overlaps and merge order:** #1954 rewrites bind_handles and the texture watch. Use #2018 first, then rebase #1954 and rerun the combined fixture, including both watch modes. The PR reports a conflict-free merge in either order. The sibling revision was unavailable in this checkout. </details> --- <sub>Automated code review by BeanBot (gpt-6.2-sol), on the pull request's opening. Nothing was run; a maintainer decides.</sub>
@ -2117,1 +2117,3 @@
NV(nvnCommandBufferBindVertexBuffers) { bind_vbufs(CB(P(0)), I(1), I(2), (const NVNbufferRange *)P(3)); }
NV(nvnCommandBufferBindVertexBuffers) { /* (the whole array in the guest window: P checks its first byte only) */
const int n = I(2); /* (out of 0 to 16: bind_vbufs stops the game before it reads any) */
bind_vbufs(CB(P(0)), I(1), n, (const NVNbufferRange *)PN(3, n > 0 && n <= 16 ? sizeof(NVNbufferRange) * (uint64_t)n : 0));
Member

L2: The wrapper checks zero bytes when the count exceeds 16, then depends on the inherited signed addition in bind_vbufs. With first = 1 and count = INT_MAX, first + count overflows before the comparison, so the guard cannot safely reject the call. Validate first and count before translating the pointer, using first between 0 and 16 and count between 0 and 16 - first. Use the same rule for the single-buffer entry point and add an extreme-value case.

**L2**: The wrapper checks zero bytes when the count exceeds 16, then depends on the inherited signed addition in bind_vbufs. With first = 1 and count = INT_MAX, first + count overflows before the comparison, so the guard cannot safely reject the call. Validate first and count before translating the pointer, using first between 0 and 16 and count between 0 and 16 - first. Use the same rule for the single-buffer entry point and add an extreme-value case.
@ -2174,0 +2180,4 @@
static const uint64_t *bind_array(Ctx *c) {
const int n = I(3);
if (n < 0 || n > 32) die("bind of %d handles", n);
return (const uint64_t *)PN(4, 8 * (uint64_t)n);
Member

L1: PN returns NULL without validating guest address zero. A positive count with a null array reaches watch_tex or memcpy, including the vertex-buffer call at line 2119. Reject null for positive counts before the watch or recorder. For a zero count, preserve the empty record and skip memcpy so a null array never reaches it. Cover both cases in the fixture.

**L1**: PN returns NULL without validating guest address zero. A positive count with a null array reaches watch_tex or memcpy, including the vertex-buffer call at line 2119. Reject null for positive counts before the watch or recorder. For a zero count, preserve the empty record and skip memcpy so a null array never reaches it. Cover both cases in the fixture.
@ -0,0 +41,4 @@
clone's folder), else clang, gcc or cc on PATH"""
for c in (given, os.environ.get("CC")):
if c:
return shutil.which(c) or c
Member

L8 (minor): CC works for a single executable, but a common setting such as CC='ccache clang' is passed to subprocess as one nonexistent executable. Accept a compiler command from CC, preserving existing executable paths with spaces, and append the fixture arguments to that command.

**L8** (minor): CC works for a single executable, but a common setting such as CC='ccache clang' is passed to subprocess as one nonexistent executable. Accept a compiler command from CC, preserving existing executable paths with spaces, and append the fixture arguments to that command.
@ -0,0 +91,4 @@
static void fault(int sig, siginfo_t *si, void *u) {
(void)sig, (void)u;
char b[256];
const int n = snprintf(b, sizeof b, "FAIL: %s, %s: read guest %#llx, past the window's end (%#llx), before any stop\n",
Member

L9 (minor): The POSIX signal handler calls snprintf, which is unsafe in a signal handler, and sends its unchecked return value to write. Both platform handlers label every access fault as a read past the guest window, including a null dereference. Check handler registration and distinguish faults in the guard from unexpected faults using fixed messages and signal-safe calls on POSIX.

**L9** (minor): The POSIX signal handler calls snprintf, which is unsafe in a signal handler, and sends its unchecked return value to write. Both platform handlers label every access fault as a read past the guest window, including a null dereference. Check handler registration and distinguish faults in the guard from unexpected faults using fixed messages and signal-safe calls on POSIX.
@ -0,0 +198,4 @@
const uint64_t array = WINDOW - (uint64_t)elem * (n ? n : 1);
const uint8_t *cur0 = host_rec.cur;
if (bind_case(e, n, array)) { fprintf(stderr, "FAIL: %s, %s: stopped: %s\n", entry_name, k, died_line); return 1; }
if (host_rec.cur == cur0 || (n && memcmp(host_rec.cur - (size_t)elem * n, GP(array), (size_t)elem * n))) {
Member

L7 (minor): The success check only compares the trailing bytes. A deferred wrapper that records src as zero still passes and then replays the stale record-time handles. The zero case accepts any record size or count. Check the opcode, size, count and deferred source before comparing the payload within the verified record bounds.

**L7** (minor): The success check only compares the trailing bytes. A deferred wrapper that records src as zero still passes and then replays the stale record-time handles. The zero case accepts any record size or count. Check the opcode, size, count and deferred source before comparing the payload within the verified record bounds.
@ -0,0 +207,4 @@
}
int n;
uint64_t array;
const char *want = "";
Member

L6 (minor): The over and negative cases accept any fatal diagnostic because the expected string is empty. A wrapper that checks the full span first and stops with a window error for an excessive count would pass. Also, the past case leaves the first element readable, so it cannot prove that validation precedes every read. Require the count diagnostic for invalid counts and protect the readable prefix in a separate ordering case.

**L6** (minor): The over and negative cases accept any fatal diagnostic because the expected string is empty. A wrapper that checks the full span first and stops with a window error for an excessive count would pass. Also, the past case leaves the first element readable, so it cannot prove that validation precedes every read. Require the count diagnostic for invalid counts and protect the readable prefix in a separate ordering case.
siantm force-pushed fix/nvn-bind-array-span from cfd24dda9b
Some checks failed
Website / Website on Cloudflare (pull_request_target) Has been skipped
PR description / Description follows the template (pull_request_target) Successful in 19s
CI / Conventional Commit title (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been cancelled
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been cancelled
CI / Workflow lint (actionlint) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
Guide screenshots / Guide images and local UI source review (pull_request) Has been cancelled
Third-party parts / As their notes say (pull_request) Has been cancelled
Third-party parts / The same as upstream (pull_request) Has been cancelled
to 555b5160d3
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
CI / Website build and checks (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request) Blocked by required conditions
PR description / Description follows the template (pull_request_target) Successful in 5s
beanstalk/approval waiting for a BigBean's 👍 on the request
Website / Website on Cloudflare (pull_request_target) Has been skipped
2026-11-21 08:20:36 +00:00
Compare
Author
Contributor

Pushed 555b5160 (with lease over cfd24dda), on main @ b62932a9, for BeanBot's review:

  • Rebased onto main @ b62932a9 without a conflict; the two earlier commits are unchanged (git range-diff: 405eb181 = 033c238e, cfd24dda = b398ced3).
  • 45d430a8 test(nvn): null-array, zero-count-at-address-0, overflowing first + count and ordering cases; invalid counts must stop with the count's own line; a kept bind is checked whole; fault handlers with fixed lines that tell the guard, the no-access page and any other fault apart; CC/--cc take a compiler command with flags (L1, L2's test, L6-L9).
  • 555b5160 fix(nvn): bind_span stops a positive count at guest address 0 before the watch or the record, and a count of 0 there copies nothing from NULL (L1); vbufs_check takes first 0 to 16 and the count 0 to 16 - first, each alone, before the array is translated, for both vertex-buffer entry points (L2).
  • Description: Summary says what the two commits add and where L3-L5 stand; Tested is for this head (job fb2-2018-1: the test passes at 555b5160, 24 of 104 cases fail at 45d430a8, three changes of the fix are caught, syntax with and without Tracy, --changed); Measurements has the fixture's main against this PR (50 of 104 fail on main, 0 here, job fb2-2018-2) and no longer the unmeasured "one compare more" (L10). The game run, Linux and the lint are still to test (L11).
  • #1954 still merges with this head without a conflict (git merge-tree against fe1dd999); this PR is still proposed to land first.
Pushed 555b5160 (with lease over cfd24dda), on `main` @ b62932a9, for BeanBot's [review](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2018#issuecomment-64182): - Rebased onto `main` @ b62932a9 without a conflict; the two earlier commits are unchanged (`git range-diff`: 405eb181 = 033c238e, cfd24dda = b398ced3). - 45d430a8 `test(nvn)`: null-array, zero-count-at-address-0, overflowing first + count and ordering cases; invalid counts must stop with the count's own line; a kept bind is checked whole; fault handlers with fixed lines that tell the guard, the no-access page and any other fault apart; `CC`/`--cc` take a compiler command with flags (L1, L2's test, L6-L9). - 555b5160 `fix(nvn)`: `bind_span` stops a positive count at guest address 0 before the watch or the record, and a count of 0 there copies nothing from NULL (L1); `vbufs_check` takes first 0 to 16 and the count 0 to 16 - first, each alone, before the array is translated, for both vertex-buffer entry points (L2). - Description: Summary says what the two commits add and where L3-L5 stand; Tested is for this head (job fb2-2018-1: the test passes at 555b5160, 24 of 104 cases fail at 45d430a8, three changes of the fix are caught, syntax with and without Tracy, `--changed`); Measurements has the fixture's `main` against this PR (50 of 104 fail on `main`, 0 here, job fb2-2018-2) and no longer the unmeasured "one compare more" (L10). The game run, Linux and the lint are still to test (L11). - #1954 still merges with this head without a conflict (`git merge-tree` against fe1dd999); this PR is still proposed to land first.
Author
Contributor

Re #2018 (comment) (L1): fixed in 555b5160, test first in 45d430a8. bind_span (used by bind_array and BindVertexBuffers) stops the game with "bind of N array elements at guest address 0" for a positive count at address 0, before the watch or the record; for a count of 0 the record is kept empty, bind_vbufs skips its copy and bind_array hands bind_handles an empty non-NULL array (bind_handles itself is left to #1954, whose rewrite already skips the copy for 0). The fixture's new null and zero-null cases (every entry point, both watch modes; the fixture's memcpy fails on a NULL pointer even for 0 bytes) fail 22 times at 45d430a8 and pass at 555b5160, in the same queue job.

Re https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2018#issuecomment-64174 (L1): fixed in 555b5160, test first in 45d430a8. `bind_span` (used by `bind_array` and `BindVertexBuffers`) stops the game with "bind of N array elements at guest address 0" for a positive count at address 0, before the watch or the record; for a count of 0 the record is kept empty, `bind_vbufs` skips its copy and `bind_array` hands `bind_handles` an empty non-NULL array (`bind_handles` itself is left to #1954, whose rewrite already skips the copy for 0). The fixture's new `null` and `zero-null` cases (every entry point, both watch modes; the fixture's `memcpy` fails on a NULL pointer even for 0 bytes) fail 22 times at 45d430a8 and pass at 555b5160, in the same queue job.
Author
Contributor

Re #2018 (comment) (L2): fixed in 555b5160, test first in 45d430a8. vbufs_check takes first 0 to 16 and the count 0 to 16 - first, each compared alone, and BindVertexBuffers calls it before the array is translated; bind_vbufs (which BindVertexBuffer also goes through) checks the same way. New cases: first 1 with a count of INT_MAX and first INT_MAX with a count of 1 for BindVertexBuffers, and first 15, 16, INT_MAX and -1 for BindVertexBuffer. At 45d430a8 the first two fail (an access fault, and the array read before any stop: the sum wrapped and passed); BindVertexBuffer's INT_MAX already stopped there (its compiled first + 1 > 16 did not wrap, though the sum is undefined in C), which the new check no longer relies on. All pass at 555b5160.

Re https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2018#issuecomment-64175 (L2): fixed in 555b5160, test first in 45d430a8. `vbufs_check` takes first 0 to 16 and the count 0 to 16 - first, each compared alone, and `BindVertexBuffers` calls it before the array is translated; `bind_vbufs` (which `BindVertexBuffer` also goes through) checks the same way. New cases: first 1 with a count of `INT_MAX` and first `INT_MAX` with a count of 1 for `BindVertexBuffers`, and first 15, 16, `INT_MAX` and -1 for `BindVertexBuffer`. At 45d430a8 the first two fail (an access fault, and the array read before any stop: the sum wrapped and passed); `BindVertexBuffer`'s `INT_MAX` already stopped there (its compiled `first + 1 > 16` did not wrap, though the sum is undefined in C), which the new check no longer relies on. All pass at 555b5160.
Author
Contributor

Re #2018 (comment) (L3): right, and inherited: this PR does not change watch_tex, and the race is in main's version (the function-static want set on the first bind, seen/nseen unlocked). #1954, which rewrites the watch, fixes both in its 519f5c7e: the switch is read once under watch_tex_lock and published with an atomic store (the inline test is an atomic load), and the chains are looked up and added under the same lock, the table's last slot included. This PR lands first and #1954 is rebased onto it (the two merge without a conflict), so the fix is not repeated here.

Re https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2018#issuecomment-64182 (L3): right, and inherited: this PR does not change `watch_tex`, and the race is in `main`'s version (the function-`static` `want` set on the first bind, `seen`/`nseen` unlocked). #1954, which rewrites the watch, fixes both in its 519f5c7e: the switch is read once under `watch_tex_lock` and published with an atomic store (the inline test is an atomic load), and the chains are looked up and added under the same lock, the table's last slot included. This PR lands first and #1954 is rebased onto it (the two merge without a conflict), so the fix is not repeated here.
Author
Contributor

Re #2018 (comment) (L4): right, and inherited from main's watch_tex, which this PR does not change. #1954 fixes the walk: its 519f5c7e reads a frame only when all 16 bytes (saved frame pointer and return address) are mapped guest memory and stops at a link that does not go up the stack, and its fe1dd999 (its head now) also keeps the walk inside the recording thread's own guest stack (fp - stk <= stk_n - 16). So the pair is checked whole before either word is read, and the walk ends at the first frame that fails.

Re https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2018#issuecomment-64182 (L4): right, and inherited from `main`'s `watch_tex`, which this PR does not change. #1954 fixes the walk: its 519f5c7e reads a frame only when all 16 bytes (saved frame pointer and return address) are mapped guest memory and stops at a link that does not go up the stack, and its fe1dd999 (its head now) also keeps the walk inside the recording thread's own guest stack (`fp - stk <= stk_n - 16`). So the pair is checked whole before either word is read, and the walk ends at the first frame that fails.
Author
Contributor

Re #2018 (comment) (L5): right as far as the code shows, and pre-existing on main, which this PR does not change. On main @ b62932a9, resolve_deferred copies each Deferred bind's handles (host/nvn_native.c:2504) and buffer ranges (:2508) into the shared recording at every submit (:2534), while the replay reads that recording later in three paths: the render thread (gpu/vk/vk_replay.cpp:7781 queues only the handles; Threaded rendering On, or Auto from 4 cores, or RX_VK_ASYNC=1), a queue stopped at an event wait on the submitting thread (submit_job, vk_replay.cpp:3027, with the render thread off), and HOS_NVN_HOLD=1 (nvn_native.c:2552). In each, a second submit of the same command buffer before the first is replayed overwrites the handles the first will read (on the render thread possibly while it reads them); whether the game resubmits a recording with Deferred binds inside that window is not determined here (it needs a game run). The fix would give each queued submit its own resolved copy, kept until its replay ends: a change to submit and replay, left for a separate change, not this bounds fix.

Re https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2018#issuecomment-64182 (L5): right as far as the code shows, and pre-existing on `main`, which this PR does not change. On `main` @ b62932a9, `resolve_deferred` copies each Deferred bind's handles (`host/nvn_native.c:2504`) and buffer ranges (`:2508`) into the shared recording at every submit (`:2534`), while the replay reads that recording later in three paths: the render thread (`gpu/vk/vk_replay.cpp:7781` queues only the handles; Threaded rendering On, or Auto from 4 cores, or `RX_VK_ASYNC=1`), a queue stopped at an event wait on the submitting thread (`submit_job`, `vk_replay.cpp:3027`, with the render thread off), and `HOS_NVN_HOLD=1` (`nvn_native.c:2552`). In each, a second submit of the same command buffer before the first is replayed overwrites the handles the first will read (on the render thread possibly while it reads them); whether the game resubmits a recording with Deferred binds inside that window is not determined here (it needs a game run). The fix would give each queued submit its own resolved copy, kept until its replay ends: a change to submit and replay, left for a separate change, not this bounds fix.
Author
Contributor

Re #2018 (comment) (L6): fixed in 45d430a8. over, huge and negative (and far) now require the count's own line exactly ("bind of 33 handles", "BindVertexBuffers 1+2147483647"), with the window's last page made no-access, and a new order case repeats past with that page no-access, so any read of the array before the span check faults. Both were checked against changes of the fix in the same queue job: checking the span before the count fails 30 cases (the window's line instead of the count's), and reading the first handle before the span check fails the 10 order cases while past still passes, as you said it would.

Re https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2018#issuecomment-64176 (L6): fixed in 45d430a8. `over`, `huge` and `negative` (and `far`) now require the count's own line exactly ("bind of 33 handles", "BindVertexBuffers 1+2147483647"), with the window's last page made no-access, and a new `order` case repeats `past` with that page no-access, so any read of the array before the span check faults. Both were checked against changes of the fix in the same queue job: checking the span before the count fails 30 cases (the window's line instead of the count's), and reading the first handle before the span check fails the 10 `order` cases while `past` still passes, as you said it would.
Author
Contributor

Re #2018 (comment) (L7): fixed in 45d430a8. A kept bind (edge, zero, zero-null) is checked whole: the record's size must equal the bytes the allocator moved on by and rec's rounded size, then its opcode, stage/first, count and a Deferred bind's source (the array's address, 0 for the others), and only then the elements within that size. With BindImagesDeferred changed to record its source as 0, its edge and zero cases fail ("source 0, not 3, 5, 32, 0x3ff00"), in the same queue job.

Re https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2018#issuecomment-64177 (L7): fixed in 45d430a8. A kept bind (edge, zero, zero-null) is checked whole: the record's size must equal the bytes the allocator moved on by and rec's rounded size, then its opcode, stage/first, count and a Deferred bind's source (the array's address, 0 for the others), and only then the elements within that size. With `BindImagesDeferred` changed to record its source as 0, its edge and zero cases fail ("source 0, not 3, 5, 32, 0x3ff00"), in the same queue job.
Author
Contributor

Re #2018 (comment) (L8): fixed in 45d430a8. --cc and CC now go through tests/compiler_command.py's parse_compiler (as run_all.py and the input checks do): an existing path, spaces included, is kept as one executable, anything else is split as a command (Windows rules there, shlex elsewhere), and the fixture's arguments are appended to it. It ran with CC set to clang.exe --target=x86_64-w64-mingw32 and passed; a ccache clang run on Linux is in Still to test.

Re https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2018#issuecomment-64178 (L8): fixed in 45d430a8. `--cc` and `CC` now go through `tests/compiler_command.py`'s `parse_compiler` (as `run_all.py` and the input checks do): an existing path, spaces included, is kept as one executable, anything else is split as a command (Windows rules there, `shlex` elsewhere), and the fixture's arguments are appended to it. It ran with `CC` set to `clang.exe --target=x86_64-w64-mingw32` and passed; a `ccache clang` run on Linux is in Still to test.
Author
Contributor

Re #2018 (comment) (L9): fixed in 45d430a8. Both handlers now write fixed lines only (POSIX: write in a loop, no snprintf; Windows: WriteFile) and tell three faults apart by address: in the guard pages ("read past the window's end", exit 5), in the window's no-access last page ("read the array ... before the bind checked it", exit 6), and anything else ("an access fault outside the guard and the no-access page (a null or stray pointer)", exit 7). AddVectoredExceptionHandler, sigaction, VirtualProtect/mprotect and the page size are checked, and the fixture stops with "fixture: no ..." if one fails; the Linux side is not run here (Still to test).

Re https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2018#issuecomment-64180 (L9): fixed in 45d430a8. Both handlers now write fixed lines only (POSIX: `write` in a loop, no `snprintf`; Windows: `WriteFile`) and tell three faults apart by address: in the guard pages ("read past the window's end", exit 5), in the window's no-access last page ("read the array ... before the bind checked it", exit 6), and anything else ("an access fault outside the guard and the no-access page (a null or stray pointer)", exit 7). `AddVectoredExceptionHandler`, `sigaction`, `VirtualProtect`/`mprotect` and the page size are checked, and the fixture stops with "fixture: no ..." if one fails; the Linux side is not run here (Still to test).
Author
Contributor

Re #2018 (comment) (L10): fixed in the description. Measurements now has a Verdict, the Setup and the fixture's table, main @ b62932a9 (its host/nvn_native.c with this PR's test, job fb2-2018-2) against this PR @ 555b5160 (job fb2-2018-1): 50 of 104 cases fail on main, by kind, and none here. The unmeasured "one compare more per array bind" is gone; no speed claim is made.

Re https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2018#issuecomment-64182 (L10): fixed in the description. Measurements now has a Verdict, the Setup and the fixture's table, `main` @ b62932a9 (its `host/nvn_native.c` with this PR's test, job fb2-2018-2) against this PR @ 555b5160 (job fb2-2018-1): 50 of 104 cases fail on `main`, by kind, and none here. The unmeasured "one compare more per array bind" is gone; no speed claim is made.
Author
Contributor

Re #2018 (comment) (L11): the game-free part is redone on this head (Tested: the fixture at 555b5160 and at 45d430a8, with CC as a command, the host file's syntax with and without Tracy, run_all.py --strict --changed with its 2 failures shown on main @ b62932a9 too). The author's game run on this PC (Castle Town's market and Hyrule Field against main) is pending and has been asked for; the Linux fixture and host build, CC='ccache clang', UTF-8 paths, the Tier 1 runs (Linux NVIDIA/AMD, Windows AMD, Steam Deck), the lint and the whole suite are in Still to test with who runs them. The PR stays WIP until those are in Tested.

Re https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2018#issuecomment-64182 (L11): the game-free part is redone on this head (Tested: the fixture at 555b5160 and at 45d430a8, with `CC` as a command, the host file's syntax with and without Tracy, `run_all.py --strict --changed` with its 2 failures shown on `main` @ b62932a9 too). The author's game run on this PC (Castle Town's market and Hyrule Field against `main`) is pending and has been asked for; the Linux fixture and host build, `CC='ccache clang'`, UTF-8 paths, the Tier 1 runs (Linux NVIDIA/AMD, Windows AMD, Steam Deck), the lint and the whole suite are in Still to test with who runs them. The PR stays WIP until those are in Tested.
Author
Contributor

@Beanstalk This fixes the native NVN array binds (Bind*Textures, Images and Samplers, deferred or not, and BindVertexBuffer(s); host/nvn_native.c): each checks the count, the first index and the game's whole array before reading it, and stops on a null array. No switch: a revert gives back main's code; play should not change. It has not run in the game on these PCs yet. Please start from the market-entrance save and play Castle Town Market for two minutes, then Hyrule Field for two minutes, the PR against main on the same PC, with a picture in each compared with main's, on Linux with NVIDIA, Linux with AMD (RADV), Windows with Intel and the Steam Deck. The same picture as main, no crash and no stop with "outside the guest window", "at guest address 0" or "BindVertexBuffers" in the log; frame time p99 equal to main's, within the run-to-run spread.

@Beanstalk This fixes the native NVN array binds (Bind*Textures, Images and Samplers, deferred or not, and BindVertexBuffer(s); host/nvn_native.c): each checks the count, the first index and the game's whole array before reading it, and stops on a null array. No switch: a revert gives back main's code; play should not change. It has not run in the game on these PCs yet. Please start from the market-entrance save and play Castle Town Market for two minutes, then Hyrule Field for two minutes, the PR against main on the same PC, with a picture in each compared with main's, on Linux with NVIDIA, Linux with AMD (RADV), Windows with Intel and the Steam Deck. The same picture as main, no crash and no stop with "outside the guest window", "at guest address 0" or "BindVertexBuffers" in the log; frame time p99 equal to main's, within the run-to-run spread.
Member

@siantm only members of BigBeans can start the farm. One of them can approve this request by reacting 👍 to it or to this comment.

@siantm only members of `BigBeans` can start the farm. One of them can approve this request by reacting 👍 to it or to this comment.
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
CI / Website build and checks (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request) Blocked by required conditions
PR description / Description follows the template (pull_request_target) Successful in 5s
beanstalk/approval waiting for a BigBean's 👍 on the request
Website / Website on Cloudflare (pull_request_target) Has been skipped
This pull request is marked as a work in progress.
Some workflows are waiting to be reviewed.
This branch is out-of-date with the base branch
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'fix/nvn-bind-array-span:siantm-fix/nvn-bind-array-span'
git switch 'siantm-fix/nvn-bind-array-span'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
3 participants
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
MagicBeanDev/Longshot!2018
No description provided.