Repository navigation
startup: opt-in guarded write of the view token in note_view - #3097
lukaslw-lab wants to merge 7 commits into
Conversation
X2W_D3D12_VIEW_TOKEN_GUARDED=1: note_view writes the view token into title descriptor memory under an exception guard instead of commit_lazy (xmem's commit mutex) plus a VirtualQuery per view. A refused write (no access, read-only, guard page with its bit set again, in-page error) leaves the descriptor without a token, as before. Fixture startup_view_token_* runs both modes over a writable, a read-only and a guard-page heap; the export x2w_d3d12_view_token_writes tells the paths apart. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
R0Y00
left a comment
There was a problem hiding this comment.
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_heap accepts 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_token from cff24b08 in a source-built x64 probe with two committed pages, placing the destination 16 bytes before the boundary. With the right page PAGE_READWRITE | PAGE_GUARD, the helper returned false but set PAGE_GUARD on both pages. With the right page PAGE_READONLY, it returned false after changing the writable prefix. Both happened under /Od and /O2. The original writable range 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.
A descriptor heap's title backing has no alignment check, so a 32-byte view token can cross a page boundary. The guarded write then changed the writable prefix before faulting on a read-only page, and re-armed PAGE_GUARD on both pages. Only a token inside one 4 KiB page now takes the guarded write; a crossing token keeps x2w_local::writable, with or without the switch. startup_view_token adds two caller-backed heaps that start 16 bytes before a page boundary (read-only and guard right page) and checks the prefix, both page protections and the right page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks, confirmed. The fixture had only page-aligned heaps, so it never produced a token that crosses a page. Fixed in
Host: Windows 10 22H2 (19045), RTX 3080 Ti, NVIDIA 32.0.16.1047, and WARP. |
pigalot
left a comment
There was a problem hiding this comment.
Independently reviewed 69befbc8 integrated over main 4b0350ea (tree 490df414). The merge is clean and git diff --cached --check origin/main is clean.
R0Y00's page-crossing finding (changes requested on cff24b08). Addressed. note_view now takes the guarded write only when view_token_in_one_page(dest_title, token_bytes) holds. A crossing token keeps the full-span writable() 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_token adds the two caller-backed heaps whose base is 16 bytes before a page boundary, with the right page PAGE_READONLY in one and PAGE_READWRITE | PAGE_GUARD in 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.Protect in the handler is the base protection, and | PAGE_GUARD restores the original. xmem's vectored handler runs before the __except filter, so a lazily committed page is committed on the first touch as the description says. Without xmem, a reserved page faults and is refused, as commit_lazy plus writable() refuses it on main.
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_check in dev and release, then ran:
ctest --test-dir out/build/<preset> --no-tests=error -j 1 -V -R '^startup_view_token_'
ctest --test-dir out/build/<preset> --no-tests=error -j 1 -R '^(startup_view_alloc_.*|startup_descriptor_.*|startup_table_alias_.*|startup_copied_vertex_srv_.*|startup_formatted_view.*|startup_logical_view.*|startup_single_element.*)$'I ran the second command once with X2W_D3D12_VIEW_TOKEN_GUARDED unset and once with it set to 1.
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".- Neighbours: 28 tests in each of the four arms (switch unset and set, Debug and Release
/O2). 27 pass, andstartup_logical_view_warpreports a capability skip (77) in every arm, as in the description. - The test is new, so it is not registered on
main.
Mutants in src/startup_stubs/d3d12_capture.inl. Each was applied alone, d3d12_xs_shim startup_view_token rebuilt in dev, startup_view_token_{set,unset}_warp run, and the source restored afterwards (git diff --quiet clean).
| Mutant | Result |
|---|---|
crossing tokens take the guarded write too (the cff24b08 condition) |
killed: a crossing heap's writable prefix changed |
| guard bit not set again | killed: the guard page lost its guard bit |
EXCEPTION_ACCESS_VIOLATION dropped from the filter |
killed: Exception: SegFault |
| token not copied | killed: the writable heap's first descriptor holds no slot 18 token |
view_token_in_one_page tests dest + bytes instead of dest + bytes - 1 |
survives, equivalent in outcome: a token that ends exactly at a page end takes the full-span check instead of the guarded write |
Verdict: approve on 69befbc8. R0Y00's changes request on cff24b08 still 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=0 in the fixture).
CI: Build and test on Windows, Assemble and translate gfx1013 fixtures and Run platform-independent Python checks on Linux pass on 69befbc8.
This reply was AI-assisted.
|
Thanks for the update. I did a static review of head 69befbc, and it looks ready from our side, pending R0Y00's re-review. R0Y00's changes-requested review (on cff24b0) 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 (
SHOULD
NIT
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. |
- startup_view_token requires the page after the guard page to stay PAGE_READWRITE, so a re-arm over the whole region fails (mutant run on WARP and hardware). - Move x2w_d3d12_view_token_writes above the startup_view_alloc comment, which belongs to ViewStoreProbe. - Note: lazy commit is exercised only by the startup runs; refused 0 shows no token was lost to a refusal, not that every token matches; the token null counts are within the spread of runs without the change but not explained; a write by another thread between the guard fault and the re-arm goes through (also in the write_view_token comment). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks, @JohnConnorNPC. All five are in
AI-assisted (Claude); I ran the tests. |
pigalot
left a comment
There was a problem hiding this comment.
Retested 88c171b1 integrated over main ec71c290 (tree 21d59e8f). The merge is clean and git diff --cached --check origin/main is clean. This follows my approval on 69befbc8, which had no blocking finding.
What changed since 69befbc8. The author merged main 1f07b2c1 as 39ead69c (no conflict resolution: git show --remerge-diff 39ead69c is empty), then pushed 88c171b1. I read git 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_view and write_view_token are unchanged, and the re-arm is still VirtualProtect(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 fails a crossing heap's writable prefix changed.
JohnConnorNPC's points 1 to 5. Each one is in 88c171b1 as the author describes. For point 1 I ran the mutant he named (re-arm over info.BaseAddress, info.RegionSize): it fails the new expect. With the test file from 69befbc8, 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_check in dev and release, then ran:
ctest --test-dir out/build/<preset> --no-tests=error -j 1 -V -R '^startup_view_token_'
ctest --test-dir out/build/<preset> --no-tests=error -j 1 -R '^(startup_view_alloc_.*|startup_descriptor_.*|startup_table_alias_.*|startup_copied_vertex_srv_.*|startup_formatted_view.*|startup_logical_view.*|startup_single_element.*)$'I ran the second command once with X2W_D3D12_VIEW_TOKEN_GUARDED unset and once with it set to 1.
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.- Neighbours: 26 tests in each of the four arms (switch unset and set, Debug and Release). 25 pass, and
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_token rebuilt in dev, startup_view_token_{set,unset}_warp run, and the source restored afterwards (git diff --quiet clean).
Killed (10):
| Mutant | First failure |
|---|---|
re-arm over the whole region (info.BaseAddress, info.RegionSize) |
the page after the guard page changed its protection |
re-arm over two pages (bytes + 0x1000) |
the page after the guard page changed its protection |
| guard bit not set again | the guard page lost its guard bit |
| crossing tokens take the guarded write too | a crossing heap's writable prefix changed |
| switch ignored | the guarded path did not write 2 tokens and refuse 2 |
| switch always on | the guarded path ran with the switch unset (unset arm) |
| token not copied | the writable heap's first descriptor holds no slot 18 token |
STATUS_GUARD_PAGE_VIOLATION dropped from the filter |
set_warp fails |
EXCEPTION_ACCESS_VIOLATION dropped from the filter |
set_warp crashes |
| a refusal counted as written | the guarded path did not write 2 tokens and refuse 2 |
Survives (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.cpp now has one 196-column line (line 5); the rest of the comment wraps near 120.
Verdict: approve on 88c171b1. R0Y00's changes request on cff24b08 still 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=1 was set for the hardware runs; the view-token fixture itself sets X2W_D3D12_DEBUG=0.
CI: Build and test on Windows, Assemble and translate gfx1013 fixtures and Run platform-independent Python checks on Linux pass on 88c171b1. As JohnConnorNPC noted, the pr preset's filter does not run startup_view_token_*, so CI has not run the new fixture.
This reply was AI-assisted.
Change
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 startup: skip unmapped ranges in guarded title-memory reads (VirtualQuery before __try) #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