fix(shaders): read a blob's words from the file, not stdio's stale buffer #2153
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/mods
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
4 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!2153
Loading…
Reference in a new issue
No description provided.
Delete branch "dazemc/Longshot:fix/spirv-pack-stale-read"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Store::readis meant to refuse a SPIR-V blob whose words changed on disk: it verifies a content hash after reading. On Linux that check is defeated, because the store reads through a per-threadFILE*opened once: glibc'sfseekohas a fast path that moves the pointer inside the stdio buffer instead of callinglseek/read, so a blob overwritten behind the handle is served from the old buffered bytes — which are exactly the bytes whose hash is in the head. A minimal reproduction understraceshows the second read issuing noread()syscall at all (only seeks) and returning the old word.Windows passes because its
fseekpath re-reads.What changed (
gpu/vk/spirv_pack.h): on POSIX the pack handles used byread()and byhead_is()are unbuffered (setvbuf(..., _IONBF, 0)), so every read goes to the file; Windows keeps its existing buffered path. The hash check stays as it was. Rejected alternatives: re-hashing after the read (already present — stale bytes hash to the head),posix_fadvise(kernel page cache, not stdio's buffer),O_DIRECT(alignment, slower), reopening per read (hot path).Rebase status (2026-11-25, current):
4e4e69396→7cfe29d0contomain @ 8f7300756701b7c9a278e576bb075ac20e622a32. Stable patch-id5d342c944abd9338c6ccb4ab58200a974257909cis unchanged and main's old-base→new-main file intersection with this PR is empty. The evidence already named below (suite, platform, in-game and measurement, as applicable) therefore still stands at the historical heads that produced it. Earlier rebase notes are history; where they say "this head", they mean the evidence commit named beside that run, not7cfe29d0c.Testing
Rebase (2026-11-25). Rebased onto
origin/mainbe663a08a30fa953420104d3cef6877bed6674a2. Old headc118f45949c104d0e7f70aa446b6eef9c20465e8→ new head4e4e6939698ee0a50f3b041fe466a9f65256ebf6.git diff -U0 8aa5ab87..c118f459andgit diff -U0 be663a08a3..4e4e6939hold the identical changed-line set (42 added/removed lines, 0 differing), so the rebase changed none of this PR's content. Main's new commits do touch both of this PR's files —gpu/vk/spirv_pack.h(+55) andtests/spirv_pack_test.cpp(+53) from #2286 — so the suite below was taken on the pre-rebase head and is not re-established at this head. Themise run testbox is unticked pending a suite at4e4e693969after the quiet window.Tested:
tests/spirv_pack_test.cppextended. The test writes a blob, reads it (filling the handle's buffer), overwrites a word in the file through a separatefopen(..., "r+b"), then requires the read to be refused and returns the changed word; a second block overwrites a head afterhead_isfirst read it and requiresputto write the words again. After the rebase ontoa2dc5385, #2171's version of theflipblock read the blob back on a thread of its own, so the calling thread's stdio handle held nothing when the disk changed, and the newCHECK(got[10] == 0xDEADBEEF)passed onmainwithout exercising the bug. The block now reads back on the same thread that reads after the change — the handle whose buffer glibc's stalefseekoserves — which is the condition the bug needs and the way the test read it before #2171; the assertion is not weakened. Run againsta2dc5385's owngpu/vk/spirv_pack.h(onlytests/spirv_pack_test.cpptaken from this head), it fails for the bug at lines 138, 142 and 144 (theflipcase) and 169 (the head case):With the fix:
In the game: a pack over
1885234b, from this branch at59ce70bb, Kokiri Forest (child), 20 s after warm-up: 1833 frames, 91.7 fps, median 10.93 ms, 0 cold frames (base: 1821 frames, 91.1 fps, 11.08 ms).Game run at
59ce70bb; the headc118f459differs only intests/spirv_pack_test.cpp(the rework after #2171's change to it) and the rebase onto8aa5ab87(upstreammaincommits, not part of this change), andgpu/vk/spirv_pack.his byte-identical between the two, so the run covers this change's game code.Still to test:
Host code: not applicable. The change is
gpu/vk/spirv_pack.h(the renderer's shader store) and its C++ test. Theheader is included only by the renderer's
gpu/vk/vk_progcache.cppand five C++ tests —tests/spirv_pack_test.cpp(this PR's),
tests/linux_pressure_test.cpp,tests/loaded_modules_drop_test.cpp,tests/program_cache_cleanup_test.cppandtests/program_cache_one_variant_test.cpp(checked withgrep -rn '#include.*spirv_pack\.h'at1a8a053b) — so it is renderer/test-only: no file underhost/includes it,the host build and its Windows (llvm-mingw) counterpart are untouched by this diff, and the "Host code builds on Windows
(llvm-mingw) and Linux" row does not apply. The renderer-side build it does affect is CI's, and the test compiles and
passes on Linux (
tests/test_spirv_pack.py, 2.1 s).Measurements
Verdict: no win in this run: 4 KB sequential is 3.0 → 3.4 µs/read (+0.4 µs, ~13 %), 64 KB sequential is 30.3 → 32.2 µs (+1.9 µs, ~6 %), 4 KB scattered is equal, and 64 KB scattered is 33.2 → 32.3 µs. The description records medians over several runs and no spread, so none of the differences is called noise; the 4 KB sequential cost is the worst case and is stated in Risks.
Setup: Linux on this PC, Ryzen 9 5950X, page-cached synthetic blobs, hashes precomputed, 2000 blobs, several runs, median of runs; a game-free microbenchmark of
Store::read.1885234b1a8a053bBaseline: the numbers were measured on
bc44f697; the changed lines ingpu/vk/spirv_pack.hare byte-identical on1a8a053b, and onlytests/spirv_pack_test.cppchanged in the rebase. No commit in344deb6c..a2dc5385touches the header, so the numbers still hold.The largest relative cost is the tiny 4 KB sequential read (3.0 → 3.4 µs, ~13 %); at 64 KB — the size real SPIR-V stages are — sequential is 30.3 → 32.2 µs, while scattered is equal at 4 KB and slightly lower at 64 KB. The setup records medians only, so these are stated as measured rather than as noise.
read()always reads the whole blob at a fresh offset, so stdio's buffer mostly missed anyway and only added a copy; unbufferedfreadreads straight intoout.Risks
A store written concurrently by another process while a blob is read now returns the changed words (and the hash refuses them) instead of stale ones. No switch; a revert is enough. Nothing changes for saves,
longshot_options.inior the game folder's caches; the shader store is read-only from the game's side.The measured cost is the 4 KB sequential read: 3.0 → 3.4 µs/read (+0.4 µs, ~13 %), the largest relative regression in the table; 64 KB sequential is 30.3 → 32.2 µs (+1.9 µs, ~6 %), 4 KB scattered is unchanged, and 64 KB scattered is slightly lower. The description records medians over several runs and no spread, so this is not dismissed as noise. The change is a correctness fix, and this is its measured price.
Checklist
Ready for review
main; the title is a Conventional Commit. — Rebased head7cfe29d0cis directly onmain @ 8f7300756701b7c9a278e576bb075ac20e622a32; the preserved diff is the single topic described in Summary.mise run testpasses (any failure that also fails onmainis named). Suite at4e4e6939698ee0a50f3b041fe466a9f65256ebf6: 833 passed, 2 failed, 8 skipped in 491 s (linux, 5 at once), exit 1; logprrefresh-logs/suite-2153-4e4e6939.log, sha25670f95d90649ef61033fce3182933fe96bdc2d3c492d1eb50e781c06d6deff3b0,.head= that sha,.start/.end/.exitand JUnit beside it. Passes against Amendment 1's CORE ∪ FLAKY plus the base failures of Amendment 2 — CORE/FLAKY: none; base-exempt:tests/test_display_startup_failure.py,tests/test_title_card_strips.py; same 8 skips. base failuretests/test_display_startup_failure.pyexempt: fails identically at the merge-basebe663a08a—display_startup_actual.inc:82:5: error: use of undeclared identifier 'bc_claim'; reproductionprrefresh-logs/base-be663a08a-two-ids.log, sha256a26e89fa21110e64aa11987f7acae1a9d166b3c6e1877e8335aa3c9b0b9ac257. base failuretests/test_title_card_strips.pyexempt: fails identically at the merge-basebe663a08a—ModuleNotFoundError: No module named 'oot_patches'; same reproduction, same sha256.main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
mainand passes here.tests/that reads changed source is updated, not weakened (tests/spirv_pack_test.cppgains the two cases; no existing assertion removed).Ready to merge
@Beanstalkrequest was posted on 2026-11-22 00:02, BeanBot answered that only a member ofBigBeanscan start it, and no approval has come yet. The by-handfliprework has run (in Tested). Ticks when that result is in Tested.pr status: Reviews: none); the Beanstalk approval is waiting for a BigBean.main, measured again or a comment says why the numbers still hold. On head1a8a053bafter the rebase: the description check passed, 11 checks still running, every build and test jobBlocked by required conditions. The measurements namemain @ 1885234b; the production lines ingpu/vk/spirv_pack.hare identical on this head, and onlytests/spirv_pack_test.cppchanged in the rebase, so they still hold. Re-confirm CI when it reports.Tested in game: Kokiri Forest (child), pack over
1885234b, 20 s: 91.7 fps median 10.93 ms, 0 cold frames; the shader store loads and plays. Arch Linux/Hyprland, RTX 4070 Ti, NVIDIA 620.70.06.Possibly related, for context:
b531da874abc44f69726Rebased onto
344deb6c(headbc44f697); gpu/vk/spirv_pack.h is untouched by the commits since, so the benchmark and the run hold.Measurements were taken on
1885234b. The head has since moved (bc44f697) and is based on344deb6c, which is now 3 commits behind current maindd5ffe6a, so here is why the numbers still hold rather than a fresh run.gpu/vk/spirv_pack.his untouched by everything that landed in between — #2161, #2140 and #2163 — and nothing in the change depends on those commits;merge-treeagainstdd5ffe6ais clean. A local check agrees:git log 344deb6c..dd5ffe6a -- gpu/vk/spirv_pack.his empty.@Beanstalk Windows: start the game on this branch on a pack and confirm the shader store loads and programs serve — no shader errors in the log, the scene renders. Windows keeps the buffered path, which is unchanged here, so this is a no-regression check.
Ready otherwise.
@dazemc only members of
BigBeanscan start the farm.One of them can approve this request by reacting 👍 to it or to this comment.This request was forbc44f69, and the PR has changed since (1a8a053), so a 👍 no longer approves it. Comment@Beanstalkagain to ask for the new commit.fix(shaders): read a blob's words from the file, not stdio's stale bufferto WIP: fix(shaders): read a blob's words from the file, not stdio's stale bufferBack to WIP for two things:
344deb6c, andmainhas since moved toa2dc5385with #2171 (9e10f6f7) fixing the game-free checks (~199 test files). The suite numbers here were measured against the oldmain(813/116/18 there, now 911/22/18), so 'mise run test' is unticked until a rebase ontoa2dc5385and a re-run.flipcase intests/spirv_pack_test.cppneeds reworking: #2171 moved that block's read-back onto a thread of its own, so the calling thread's handle holds no old buffered words and the newCHECK(got[10] == 0xDEADBEEF)passes onmainalready. The new head case (a head overwritten underput) still fails there, so the test still catches the bug — but thefliphalf should fail onmaintoo. Listed in Still to test.The in-game smoke stands: its record (
59ce70bb, 1833 frames, 91.7 fps, 0 cold frames) matches, andgpu/vk/spirv_pack.h's changed lines are identical to the head's. Measurements still namemain @ 1885234b; the comment saying why they hold was written againstdd5ffe6a, so re-confirm after the rebase.The Ready to merge boxes stay unticked, each with its reason beside it: Windows is with the Test Farm, waiting for a BigBean; no review yet; CI's build/test jobs are still pending.
bc44f697261a8a053bc0Rebased onto
a2dc5385: head is now1a8a053b. The rebase was clean, and theflipcase was reworked on top (test-only).Full game-free suite, run in the same shape as a clean
a2dc5385baseline (tests/run_all.py --strict --jobs 3,systemd-run --user --scope -p MemoryMax=8G -p MemorySwapMax=0, the pinned headers):a2dc5385: 911 passed / 23 failed / 18 skipped in 635 s1a8a053b: 911 / 23 / 18 in 619 sFailing names identical, compared name by name (23). The one name new on
a2dc5385since9e10f6f7istests/test_sim_turf_pools.py: #2174 changed the simulated-turf floor while that old check still asserts a zero floor, so it is stale onmainand red identically here.test_spirv_packpasses (2.1 s), both new cases included.The rework is test-only and honest. #2171 moved the
flipblock's read-back onto a thread of its own, so the calling thread's stdio handle held nothing andCHECK(got[10] == 0xDEADBEEF)passed onmainwithout exercising the bug. The block now reads back on the same thread that reads after the disk changes — the handle whose buffer glibc's stalefseekoserves — which is the condition the bug needs, as the test read it before #2171. The assertion is not weakened. Run againsta2dc5385's owngpu/vk/spirv_pack.h(only this head'stests/spirv_pack_test.cpp), it fails for the bug:The production hunk in
gpu/vk/spirv_pack.his byte-identical to the pre-rebase head; the rework changed no production code. Jev on the test change: 0.88, likely honest. Diff stat fora2dc5385..1a8a053bisgpu/vk/spirv_pack.h | 11 +andtests/spirv_pack_test.cpp | 33 +-; outsidetests/the only file is the original runtime fix, unchanged frombc44f697.In-game evidence (Jev: 0.68, likely valid): the smoked
59ce70bbrecord matches, the changed lines ingpu/vk/spirv_pack.hare identical to this head, and no commit in344deb6c..a2dc5385touches it. No new in-game run was made.Measurements column corrected to name the rebased head, with (where the numbers were taken) in a Baseline line: the production hunk in gpu/vk/spirv_pack.h is byte-identical, only the test changed in the rebase.
WIP: fix(shaders): read a blob's words from the file, not stdio's stale bufferto fix(shaders): read a blob's words from the file, not stdio's stale bufferCorrection to the comment above: the measurement commit is
bc44f697; the rebased head is1a8a053b.@Beanstalk Windows: start the game on this branch on a pack and confirm the shader store loads and programs serve — no shader errors in the log, the scene renders. Windows keeps the buffered path, which is unchanged here, so this is a no-regression check.
Head:
1a8a053b(rebased ontoa2dc5385).@dazemc only members of
BigBeanscan start the farm.One of them can approve this request by reacting 👍 to it or to this comment.This request was for1a8a053, and the PR has changed since (c118f45), so a 👍 no longer approves it. Comment@Beanstalkagain to ask for the new commit.Testing now names the run commit and what separates it from the head: on the branch side only
tests/spirv_pack_test.cppdiffers between59ce70bband1a8a053b(the rework after #2171's change), plus the rebase ontoa2dc5385, andgpu/vk/spirv_pack.his byte-identical, so the in-game run covers this change's game code. No code change; stays ready.Same shape point:
gpu/vk/spirv_pack.his host code, so the template's "Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths" belongs under When it applies, unticked until the Windows build/start in Still to test is in Tested. Everything else in Ready for review verifies at1a8a053b: the59ce70bbrun is the pre-rebase head, the header is byte-identical, the reworked test fails onmain's header and passes here, the suite's 23 failures are the same names asmain, and the Measurements' Baseline paragraph answers the "measured on1885234b, now ona2dc5385" warning.Quality-sweep follow-up on the host-build hint: it does not apply. The diff is
gpu/vk/spirv_pack.h(the renderer's shader store) and its C++ test — no file underhost/includes the header (included bygpu/vk/vk_progcache.cppandtests/linux_pressure_test.cpp), so the Windows llvm-mingw host build is untouched. The body now says that explicitly instead of a row; the renderer build is CI's and the test passes on Linux. No code change; stays ready.The N/A is sound on its point (no
host/file includesspirv_pack.h; the renderer's Windows build is CI's; the test passes on Linux), but the parenthetical is incomplete:tests/program_cache_cleanup_test.cpp,tests/program_cache_one_variant_test.cpp,tests/loaded_modules_drop_test.cppandtests/spirv_pack_test.cppalso includegpu/vk/spirv_pack.h, besidestests/linux_pressure_test.cpp. Say "the renderer and its tests" or list them. No other issue at1a8a053b: the other four Ready-for-review boxes and the Baseline still hold.Corrected the N/A include list per the verifier:
gpu/vk/spirv_pack.his included only bygpu/vk/vk_progcache.cppand five C++ tests —tests/spirv_pack_test.cpp(this PR's),tests/linux_pressure_test.cpp,tests/loaded_modules_drop_test.cpp,tests/program_cache_cleanup_test.cppandtests/program_cache_one_variant_test.cpp(grep -rn '#include.*spirv_pack\.h'at1a8a053b). Nohost/file includes it, so the row stays N/A and the PR stays ready. No code change.Head
1a8a053bis still ready and waits only on fork CI: itspull_requestjobs are "Blocked by required conditions" and the PR has noci: approvedlabel. No earlier fork-CI ask names this head.@HumanBean: please approve this head's fork CI runs (the Actions jobs, and the
ci: approvedlabel for the fork project) and review #2153 — reading a blob's words from the file, not stdio's stale buffer.Evidence is in the thread:
gpu/vk/spirv_pack.his byte-identical to the smoked 59ce70bb, and the reworked test fails on main's header and passes here.fix(shaders): read a blob's words from the file, not stdio's stale bufferto WIP: fix(shaders): read a blob's words from the file, not stdio's stale buffer1a8a053bc0c118f45949@HumanBean — fork CI for head
c118f459, please.What it is: a SPIR-V blob's words are read from the file, not stdio's stale buffer, so the content-hash check is no longer defeated on Linux.
Asked: approve this head's fork CI runs, and a review when you have the time.
Evidence: Measurements' table (4 KB sequential 3.0 -> 3.4 µs, 64 KB 30.3 -> 32.2 µs) and the fail-on-main lines 138-169 in
tests/spirv_pack_test.cpp.Still WIP — suites land one at a time, so this is not a merge request.
Suite at
c118f459in the PC's queue (job suite-2153, 3 min): 942 passed, 19 failed, 18 skipped, exit 1 — the same 19 ids the baseline names. Log prready-2026-11-24/suite-2153-c118f459.log, sha256 3912bf20…. Description settled; pr ready next.WIP: fix(shaders): read a blob's words from the file, not stdio's stale bufferto fix(shaders): read a blob's words from the file, not stdio's stale bufferfix(shaders): read a blob's words from the file, not stdio's stale bufferto WIP: fix(shaders): read a blob's words from the file, not stdio's stale bufferThe suite behind the tick ran at run_all's default 32 jobs, not the harness's --jobs 5/MemoryMax=12G, so the tick did not rest on a comparable run; back to WIP until the 5-job run at
c118f459lands.Suite at
c118f459re-run at the harness's --jobs 5 under MemoryMax=12G (job suite-2153-b, 396 s): 942 passed, 19 failed, 18 skipped, exit 1 — all 19 are CORE ids, none outside CORE ∪ FLAKY, counts not compared (Amendment 1). Log prrefresh-logs/suite-2153-c118f459-b.log, sha256 4c49378a…. The earlier 32-job run and the pre-.head log are not used. Description settled; pr ready next.WIP: fix(shaders): read a blob's words from the file, not stdio's stale bufferto fix(shaders): read a blob's words from the file, not stdio's stale bufferc118f459494e4e693969Description: Testing names the machine instead of a distribution; no other change.
4e4e6939697cfe29d0c5Rebased
4e4e69396to7cfe29d0con main8f7300756; patch-id unchanged and intersection empty. Existing suite and measurement evidence stands.Recommendation: Request changes. The fixtures cover the stale POSIX buffer, and the two unbuffered readers address it. Measure the cost on the shader-cache read path before merging.
Disabling stdio buffering affects every POSIX blob read and header probe, including across a warm program-cache load. The reported old-head Kokiri smoke compared 91.1 to 91.7 FPS with zero cold frames. It does not cover startup/preparation or the read cost. Compare cold preparation and a warm-cache start on current main and the integrated fix. Keep the cache state and driver fixed, and include the affected cache phase timing or read counts.
Current main has changed both the SPIR-V store and this fixture since the branch's merge base, including shared-store compaction and direct-start store selection. The read-only merge is textually clean. Run the focused read/header and compaction regressions together on the combined code. Exact-head CI remains gated. CI
Reviewed both files at
7cfe29d0c5012e61a178462537f20263d8fc882eagainst actual base8f730075and checked the affected current-main callers at1413b172.@ -250,0 +252,4 @@// damaged store, a pack made again), and glibc's fseeko seeks within what it has already buffered rather than// reading the file again, serving the old words, which match the head's hash. Unbuffered, fread reads the// blob straight into out: the buffer only ever saved a copy of the words, never a read of them)std::setvbuf(h.f, nullptr, _IONBF, 0);[P2] Measure this POSIX read-path change during preparation and a warm program-cache load. A 20-second already-warm gameplay run does not exercise the many per-blob reads and header probes affected here.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.