startup: opt-in guarded write of the view token in note_view #3097
No reviewers
Labels
No labels
accessibility
awaiting author
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
priority: P0
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/R0Y00--Xbox2Windows!3097
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3097"
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?
@lukaslw-lab on GitHub · 2026-11-27 15:03 UTC · original
lukaslw-lab:runtime/view-token-guarded-write→main· +376 −6 · 6 files · 8 commits · closed without mergingChange
Fixes #3096 (every view call asks
VirtualQueryand xmem's commit mutex before writing its token).X2W_D3D12_VIEW_TOKEN_GUARDED=1(opt-in):note_viewwrites the view token into the title's descriptor memory under an exception guard (write_view_token) instead of aftercommit_lazyand aVirtualQuery(x2w_local::writable). Unset, the check and the write are as onmain.writablereturning false did. A guard page has its guard bit set again, asprobe_readabledoes.title_backedis checked first). So this does not go against #2845, which asks to query first for reads of stale pointers, where faults are common.x2w_d3d12_view_token_writes(written, refused), and with the switch set a line before the render totals:x2w-render view tokens (X2W_D3D12_VIEW_TOKEN_GUARDED): written N, refused M.docs/startup-runtime.md;docs/experiments/startup-view-token-guarded.md.mainca659396): no new conflict withcpp-migrationor with any of the open PRs that touch these files.Verification
Windows 10 22H2, i7-4790K, RTX 3080 Ti (NVIDIA 32.0.16.1047) and WARP; dev and release; commit
cff24b08(mainca659396merged).startup_view_token_{unset,set}_{warp,hardware}: 4/4 per preset. Three descriptor heaps over title memory the fixture owns, protected after creation (writable, read-only, guard page). In both modes the writable heap holds both tokens, the read-only heap stays zero, and the guard page keeps its guard bit and stays zero. The export reports unset0, 0, set2, 2.the guarded path did not write 2 tokens and refuse 2; not setting the guard bit again failsthe guard page lost its guard bit.startup_logical_view_warpandstartup_root_heap_{warp,hardware}(no debug layer), the same in every run.Startup run of the main executable (#870), release on
main36d2cbeb, warm shader cache, scripted input, 250 s, the three merged switches in every run, two pairs (second in reverse order):buffer; device removaltokenper 1,000 translated drawsA confirmation run with the switch set logged
written 16447410, refused 0: the guard refused no write, so the tokens are the ones the default path writes. Thetokennull bindings scale with the draws recorded; runs without this change gave 23–34 per 1,000 translated draws on this machine. Not covered: other adapters, hosts with more cores, Wine.AI-assisted: the change, tests and this description were written with Claude; I ran the startup comparison.
🤖 Generated with Claude Code
@R0Y00 on GitHub · 2026-11-27 15:23 UTC · original · requested changes
The guarded path changes page protections and can write a partial token when a 32-byte descriptor spans a page boundary. The registered-range check only bounds the span, and
device_heapaccepts caller-supplied backing without an alignment check. A handle at the first descriptor of such a heap can therefore cross a page even when its offset inside the heap is aligned.I compiled the exact
write_view_tokenfromcff24b08in a source-built x64 probe with two committed pages, placing the destination 16 bytes before the boundary. With the right pagePAGE_READWRITE | PAGE_GUARD, the helper returned false but setPAGE_GUARDon both pages. With the right pagePAGE_READONLY, it returned false after changing the writable prefix. Both happened under/Odand/O2. The originalwritablerange check refused that span before writing. Please route page-crossing tokens through the existing full-span check and add a caller-backed boundary fixture that verifies the writable prefix and both page protections stay unchanged on refusal.@lukaslw-lab on GitHub · 2026-11-27 16:01 UTC · original
Thanks, confirmed. The fixture had only page-aligned heaps, so it never produced a token that crosses a page.
Fixed in
69befbc8: only a token that lies inside one 4 KiB page takes the guarded write (view_token_in_one_pagein
note_view). A token that crosses a page boundary keeps the full-spanwritable()check, with or without theswitch. Inside one page, a refusal comes on the first byte, so nothing is written and the guard re-arm touches only
that page.
startup_view_tokennow has two more caller-backed heaps whose base is 16 bytes before a page boundary. The rightpage is
PAGE_READONLYin one andPAGE_READWRITE | PAGE_GUARDin the other. A slot 18 SRV goes into descriptor 0 ofeach. In both modes the 16-byte writable prefix keeps its sentinel bytes, the left page stays
PAGE_READWRITE, theright page keeps its protection, and the right page stays zero.
^startup_view_token_set_warp+set_hardwarefail:a crossing heap's writable prefix changed(read-only right page); left page0x104=PAGE_READWRITE | PAGE_GUARD(guard right page);unsetpassesstartup_logical_view_warp,startup_root_heap_{warp,hardware}as before2, 2insetmode, so the crossing tokens did not go through the guard.token, because 4096 is a multiple of 32 and of 16.
Host: Windows 10 22H2 (19045), RTX 3080 Ti, NVIDIA 32.0.16.1047, and WARP.
@KelboLive on GitHub · 2026-11-27 18:54 UTC · original · approved these changes
@pigalot on GitHub · 2026-11-27 23:03 UTC · original · approved these changes
Independently reviewed
69befbc8integrated overmain4b0350ea(tree490df414). The merge is clean andgit diff --cached --check origin/mainis clean.R0Y00's page-crossing finding (changes requested on
cff24b08). Addressed.note_viewnow takes the guarded write only whenview_token_in_one_page(dest_title, token_bytes)holds. A crossing token keeps the full-spanwritable()check with or without the switch. Inside one page every byte shares one protection, so a refused write faults before any byte lands. The guard re-arm then touches only that page.startup_view_tokenadds the two caller-backed heaps whose base is 16 bytes before a page boundary, with the right pagePAGE_READONLYin one andPAGE_READWRITE | PAGE_GUARDin the other. The test checks the writable prefix, both protections and the right page's contents.What I traced. A guard page clears its guard bit on the first touch, so
info.Protectin the handler is the base protection, and| PAGE_GUARDrestores the original. xmem's vectored handler runs before the__exceptfilter, so a lazily committed page is committed on the first touch as the description says. Without xmem, a reserved page faults and is refused, ascommit_lazypluswritable()refuses it onmain.What I ran. I built
startup_view_token startup_descriptor_heap_type_check startup_descriptor_increment startup_logical_view_table_check startup_single_element_srv_check startup_view_alloc d3d12_xs_shim startup_stream_consumer offscreen_checkindevandrelease, then ran:I ran the second command once with
X2W_D3D12_VIEW_TOKEN_GUARDEDunset and once with it set to1.startup_view_token_{unset,set}_{warp,hardware}: 4/4 per preset, on WARP and on the RTX 5090. Unset reportswritten 0, refused 0and set reportswritten 2, refused 2. The PASS line includes "crossing tokens refused without a write or a protection change"./O2). 27 pass, andstartup_logical_view_warpreports a capability skip (77) in every arm, as in the description.main.Mutants in
src/startup_stubs/d3d12_capture.inl. Each was applied alone,d3d12_xs_shim startup_view_tokenrebuilt indev,startup_view_token_{set,unset}_warprun, and the source restored afterwards (git diff --quietclean).cff24b08condition)a crossing heap's writable prefix changedthe guard page lost its guard bitEXCEPTION_ACCESS_VIOLATIONdropped from the filterException: SegFaultthe writable heap's first descriptor holds no slot 18 tokenview_token_in_one_pagetestsdest + bytesinstead ofdest + bytes - 1Verdict: approve on
69befbc8. R0Y00's changes request oncff24b08still stands, so I am not merging.Environment: NVIDIA GeForce RTX 5090 driver 32.0.16.1088 and WARP 10.0.26100.9818, Windows 11 25H2 build 26200.9974, MSVC 19.52.37029; the debug layer is off (
X2W_D3D12_DEBUG=0in the fixture).CI:
Build and test on Windows,Assemble and translate gfx1013 fixturesandRun platform-independent Python checks on Linuxpass on69befbc8.This reply was AI-assisted.
@JohnConnorNPC on GitHub · 2026-11-28 07:58 UTC · original
Thanks for the update. I did a static review of head
69befbc8, and it looks ready from our side, pending R0Y00's re-review. R0Y00's changes-requested review (oncff24b08) still stands, so R0Y00 needs to re-review or dismiss it before this can merge. Both requested changes are on the head: crossing tokens take the full-span check again (if (view_token_guarded() && view_token_in_one_page(dest_title, token_bytes)), otherwisewritable()and then memcpy), and the caller-backed boundary fixture is in place.git merge-tree --write-tree origin/main <head>against mainc3204a9cis clean (tree5d2093f4). With the switch unset,note_viewkeeps main'stitle_backed→writable→ memcpy path.view_token_in_one_pageagainst a page-relative reference model (4 page bases including the top of the 64-bit space, sizes 0–64):cases=152064 mismatches=0.gh pr checks 3097shows Windows, gfx1013 and Linuxpass.ctest --preset prran100% tests passed out of 41, but theprname filter matches none of the 4startup_view_token_{unset,set}_{warp,hardware}tests, so CI has not run the new fixture yet.docs_links_check(Checked 149 Markdown files and 672 links.) andbidi_controls_checkboth pass. The docs/startup-runtime.md row links the new page.SHOULD
VirtualQuery(memory[guard])andall_zero(memory[guard], …). A re-arm overinfo.BaseAddress, info.RegionSizewould guard the whole merged 16-page read-write region and still pass every expect (static analysis, not run). An expect that the page after the guard page staysPAGE_READWRITEwould cover it.MEM_RESERVE | MEM_COMMITand loads no xmem.dll. Either add a reserved-page case under xmem, or say in the doc that only the startup run measured this.NIT
// Fixture probe (startup_view_alloc)comment andstruct ViewStoreProbe, so that comment now sits abovex2w_d3d12_view_token_writes.refused 0shows only that the guard refused nothing, not that "the tokens written match the default path".tokennulls per 1,000 draws were also higher with the switch (33.6 / 30.1 vs 25.7 / 23.5), which "scale with the draws recorded" does not explain.VirtualProtectre-arm, another thread's write to that page succeeds wherewritable()would refuse it. Withrefused 0this doesn't matter in practice, but "aswritablereturning false did" doesn't hold under concurrency.Not run: no build, Wine or GPU. This covers static review, the trial merge, the model and the pure-Python docs checks only.
This comment was AI-assisted.
@lukaslw-lab on GitHub · 2026-11-28 08:44 UTC · original
Thanks, @JohnConnorNPC. All five are in
88c171b1(with a merge ofmain1f07b2c1):PAGE_READWRITE. Your mutant (re-arm overinfo.BaseAddress,info.RegionSize) failsset_warpandset_hardwarewiththe page after the guard page changed its protection; added to the note's mutant list.startup_view_alloccomment, which sits onViewStoreProbeagain.refused 0shows no token was lost to a refusal, not that every token matches the default path. Thetokennulls are given as within the 23–34 spread of runs without the change, but not explained.write_view_token: between the guard fault and the re-arm, another thread's write goes through wherewritable()would refuse.^startup_view_token_4/4 in dev and release; view-token matrix 121/121 in each of release and dev, switch off and on (3 skips as before:startup_logical_view_warp,startup_root_heap_{warp,hardware}).prpreset's filter doesn't includestartup_view_token_*, so the fixture has only run locally (RTX 3080 Ti + WARP). Happy to add it to the preset if you want it there.AI-assisted (Claude); I ran the tests.
@pigalot on GitHub · 2026-11-28 10:55 UTC · original · approved these changes
Retested
88c171b1integrated overmainec71c290(tree21d59e8f). The merge is clean andgit diff --cached --check origin/mainis clean. This follows my approval on69befbc8, which had no blocking finding.What changed since
69befbc8. The author mergedmain1f07b2c1as39ead69c(no conflict resolution:git show --remerge-diff 39ead69cis empty), then pushed88c171b1. I readgit diff 39ead69c 88c171b1:tests/startup_view_token.cpp: one new expect, that the page after the guard page is stillPAGE_READWRITE.src/startup_stubs/d3d12_capture.inl: comment only (the concurrency caveat onwrite_view_token).src/startup_stubs/d3d12_xs_device.cpp: the export moved above thestartup_view_alloccomment; no code change.docs/experiments/startup-view-token-guarded.md: the lazy-commit, concurrency and null-binding wording.The runtime code is the same as at
69befbc8.note_viewandwrite_view_tokenare unchanged, and the re-arm is stillVirtualProtect(dest, bytes, info.Protect | PAGE_GUARD, &old), which covers one page because the token lies inside one page.R0Y00's page-crossing finding (changes requested on
cff24b08). Still addressed at this head: the crossing-token mutant below failsa crossing heap's writable prefix changed.JohnConnorNPC's points 1 to 5. Each one is in
88c171b1as the author describes. For point 1 I ran the mutant he named (re-arm overinfo.BaseAddress,info.RegionSize): it fails the new expect. With the test file from69befbc8, the same mutant passes, so the new expect is what catches it. Points 2, 4 and 5 are wording, and the new text matches what the code and the fixture do: the fixture commits its memory up front and loads no xmem.dll, so only the startup runs exercise lazy commit.What I ran. I built
startup_view_token d3d12_xs_shim startup_view_alloc startup_descriptor_heap_type_check startup_descriptor_increment startup_logical_view_table_check startup_single_element_srv_check startup_stream_consumer offscreen_checkindevandrelease, then ran:I ran the second command once with
X2W_D3D12_VIEW_TOKEN_GUARDEDunset and once with it set to1.startup_view_token_{unset,set}_{warp,hardware}: 4/4 in Debug and in Release/O2, on WARP and on the RTX 5090. Unset reportswritten 0, refused 0and set reportswritten 2, refused 2.startup_logical_view_warpreports a capability skip (77) in every arm.Mutants in
src/startup_stubs/d3d12_capture.inl. Each was applied alone,d3d12_xs_shim startup_view_tokenrebuilt indev,startup_view_token_{set,unset}_warprun, and the source restored afterwards (git diff --quietclean).Killed (10):
info.BaseAddress,info.RegionSize)the page after the guard page changed its protectionbytes + 0x1000)the page after the guard page changed its protectionthe guard page lost its guard bita crossing heap's writable prefix changedthe guarded path did not write 2 tokens and refuse 2the guarded path ran with the switch unset(unsetarm)the writable heap's first descriptor holds no slot 18 tokenSTATUS_GUARD_PAGE_VIOLATIONdropped from the filterset_warpfailsEXCEPTION_ACCESS_VIOLATIONdropped from the filterset_warpcrashesthe guarded path did not write 2 tokens and refuse 2Survives (1), not blocking:
EXCEPTION_IN_PAGE_ERRORdropped from the filter. The fixture has no page that raises an in-page error, and I don't see a cheap way to build one.Equivalent in outcome (1):
view_token_in_one_pagetestsdest + bytesinstead ofdest + bytes - 1. A token that ends exactly at a page end then takes the full-span check instead of the guarded write; the result is the same.Nit, not blocking. The header comment in
tests/startup_view_token.cppnow has one 196-column line (line 5); the rest of the comment wraps near 120.Verdict: approve on
88c171b1. R0Y00's changes request oncff24b08still stands on the PR, so I am not merging.Environment: NVIDIA GeForce RTX 5090 driver 32.0.16.1088 and WARP, Windows 11 25H2 build 26200.9974, MSVC 19.52.37029, Python 3.12.10.
X2W_TEST_DEBUG_LAYER=1was set for the hardware runs; the view-token fixture itself setsX2W_D3D12_DEBUG=0.CI:
Build and test on Windows,Assemble and translate gfx1013 fixturesandRun platform-independent Python checks on Linuxpass on88c171b1. As JohnConnorNPC noted, theprpreset's filter does not runstartup_view_token_*, so CI has not run the new fixture.This reply was AI-assisted.
@Leyukaka on GitHub · 2026-11-29 00:42 UTC · original
@R0Y00 your changes request here is on
cff24b08.69befbc8has both items: a page-crossing token keeps the full-spanwritable()check (view_token_in_one_pageinnote_view), andstartup_view_tokenhas two caller-backed heaps that start 16 bytes before a page boundary (read-only and guard right page).pigalot approved the current head
88c171b1. Could you re-review or dismiss it?@lukaslw-lab on GitHub · 2026-11-29 08:45 UTC · original
Merged
main(#3379) in6e8ee394. Both PRs changed the token write innote_view: the token is now built firstand, for a freeable view, replaced by its content token (#3379), then written by the guarded write or the
writable()fallback as before. Both PRs' helpers kept. The change againstmainis the reviewed one apart fromtokenno longer beingconst.Release and dev:
startup_view_token_*4/4; slot 17-22 view families 125/125 with the switch unset and set;startup_view_content*5/5 withX2W_D3D12_VIEW_TOKEN_GUARDEDunset and set.@JohnConnorNPC on GitHub · 2026-11-29 13:18 UTC · original
I re-checked head
6e8ee394, your merge ofmainwith #3379, against currentmain(c04e0dd6).git merge-tree --write-tree origin/main 6e8ee394exits 0. All five points from my last comment are in88c171b1, and thenote_viewconflict is resolved correctly.X2W_D3D12_VIEW_TOKEN_GUARDEDunset,note_viewbuilds the token and, for a freeable view, copiescontentover it. It returns whendest_title == dest_desktop || !title_backed(...), and otherwise writes onlyif (x2w_local::writable(...)). That ismain's condition, and nothing follows it innote_view. Building the token before the checks has no side effects.protection(memory[guard] + page) == PAGE_READWRITE, so the whole-region re-arm mutant is pinned. I didn't run it (no Windows here). Your note lists it as failing.refused 0claim are in. The export sits above thestartup_view_alloccomment again.6e8ee394: Windows, gfx1013 and Linux pass (100% tests passed out of 79).git diff --checkis clean, and the diff adds no binary files.No MUST from me.
SHOULD
prpreset.grep -c startup_view_tokenon the Windows job log is still0, so only your machine (RTX 3080 Ti + WARP) has runstartup_view_token_{unset,set}_warp. Adding the two_warptests and thestartup_view_tokentarget would let CI check the guarded path.Merge status: R0Y00's CHANGES_REQUESTED review on
cff24b08is still the review decision. Both of its points are in this head. GitHub blocks the merge until that review is re-done or dismissed.AI-assisted.
@lukaslw-lab on GitHub · 2026-12-01 01:07 UTC · original
Closing this for now.
mainfrom 28 Nov, the switch gave +0–3 fps in our startup A/B, within run-to-run noise. The larger cost in that profile wasnote_lockcontention, not the token write itself.mainis 537 commits past this branch's base.d3d12_xs_device.cppalone has +1,081/−142 lines since then, including the content tokens (#3379) that already changed this same token write. A rebase would need new review and new numbers. That review time is better spent on the P0 work.If a later profile shows
note_viewtoken writes as a cost, the branch stays on my fork (runtime/view-token-guarded-write, head6e8ee394) and can be reopened. Issue #3096 has the original reasoning.Thanks to everyone who reviewed it.
Pull request closed