Skip to content

startup: opt-in guarded write of the view token in note_view - #3097

Open
lukaslw-lab wants to merge 7 commits into
R0Y00:mainfrom
lukaslw-lab:runtime/view-token-guarded-write
Open

lukaslw-lab wants to merge 7 commits into
R0Y00:mainfrom
lukaslw-lab:runtime/view-token-guarded-write

Conversation

@lukaslw-lab

Copy link
Copy Markdown
Contributor

Change

Fixes #3096 (every view call asks VirtualQuery and xmem's commit mutex before writing its token).

X2W_D3D12_VIEW_TOKEN_GUARDED=1 (opt-in): note_view writes the view token into the title's descriptor memory under an exception guard (write_view_token) instead of after commit_lazy and a VirtualQuery (x2w_local::writable). Unset, the check and the write are as on main.

Verification

Windows 10 22H2, i7-4790K, RTX 3080 Ti (NVIDIA 32.0.16.1047) and WARP; dev and release; commit cff24b08 (main ca659396 merged).

ctest --preset <dev|release> --no-tests=error -j 1 -R '^startup_view_token_' -V
ctest --preset <dev|release> --no-tests=error -j 1 -R '^(startup_view_alloc_.*|startup_resource_retire_.*|startup_descriptor_.*|startup_address_.*|startup_stream_draw.*|startup_table_alias_.*|startup_sampler_heap.*|startup_copied_vertex_srv_.*|startup_root_.*|startup_memory_srv.*|startup_formatted_view.*|startup_logical_view.*|startup_1d_srv.*|startup_single_element.*|startup_slot133_lazy.*)$'
  • New 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 unset 0, 0, set 2, 2.
    • Negative controls (local mutations): ignoring the switch fails the guarded path did not write 2 tokens and refuse 2; not setting the guard bit again fails the guard page lost its guard bit.
  • Affected families, switch unset and set: 115/115 with the switch unset and set per preset; capability skips startup_logical_view_warp and startup_root_heap_{warp,hardware} (no debug layer), the same in every run.

Startup run of the main executable (#870), release on main 36d2cbeb, warm shader cache, scripted input, 250 s, the three merged switches in every run, two pairs (second in reverse order):

150–240 s switch unset switch set
mean fps, pair 1 / pair 2 15.5 / 15.1 16.6 / 15.6
time in view calls (threads); µs per call 1.19 / 1.20; 10.4 / 11.2 0.88 / 0.92; 7.0 / 6.7
view calls per second 114k / 107k 126k / 136k
null bindings buffer; device removal 0; none 0; none
null bindings token per 1,000 translated draws 25.7 / 23.5 33.6 / 30.1

A 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. The token null 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

lukaslw-lab and others added 4 commits November 27, 2026 14:57
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 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@lukaslw-lab

Copy link
Copy Markdown
Contributor Author

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_page
in note_view). A token that crosses a page boundary keeps the full-span writable() check, with or without the
switch. 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_token now has two more caller-backed heaps whose base is 16 bytes before a page boundary. The right
page is PAGE_READONLY in one and PAGE_READWRITE | PAGE_GUARD in the other. A slot 18 SRV goes into descriptor 0 of
each. In both modes the 16-byte writable prefix keeps its sentinel bytes, the left page stays PAGE_READWRITE, the
right page keeps its protection, and the right page stays zero.

check (release + dev, WARP + hardware) result
^startup_view_token_ 4/4 per preset
mutant: guard crossing tokens too (old condition) set_warp + set_hardware fail: a crossing heap's writable prefix changed (read-only right page); left page 0x104 = PAGE_READWRITE | PAGE_GUARD (guard right page); unset passes
view-token matrix (same list as before) 115/115 per arm (switch off / on × release / dev); skips startup_logical_view_warp, startup_root_heap_{warp,hardware} as before
  • The guarded counters stay at 2, 2 in set mode, so the crossing tokens did not go through the guard.
  • The startup numbers in the PR were not re-run. A heap whose base is aligned to its stride never gives a crossing
    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.

@pigalot pigalot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 reports written 0, refused 0 and set reports written 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, and startup_logical_view_warp reports 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.

@JohnConnorNPC

Copy link
Copy Markdown
Collaborator

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 (if (view_token_guarded() && view_token_in_one_page(dest_title, token_bytes)), otherwise writable() and then memcpy), and the caller-backed boundary fixture is in place.

  • Merge: git merge-tree --write-tree origin/main <head> against main c3204a9 is clean (tree 5d2093f4). With the switch unset, note_view keeps main's title_backed → writable → memcpy path.
  • view_token_in_one_page against a page-relative reference model (4 page bases including the top of the 64-bit space, sizes 0–64): cases=152064 mismatches=0.
  • CI: gh pr checks 3097 shows Windows, gfx1013 and Linux pass. ctest --preset pr ran 100% tests passed out of 41, but the pr name filter matches none of the 4 startup_view_token_{unset,set}_{warp,hardware} tests, so CI has not run the new fixture yet.
  • On the merged tree, docs_links_check (Checked 149 Markdown files and 672 links.) and bidi_controls_check both pass. The docs/startup-runtime.md row links the new page.

SHOULD

  1. Nothing tests the "guard bit set again, on that page only" part. The fixture reads only VirtualQuery(memory[guard]) and all_zero(memory[guard], …). A re-arm over info.BaseAddress, info.RegionSize would 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 stays PAGE_READWRITE would cover it.
  2. No fixture covers the lazy-commit sentence ("committed by xmem's vectored handler on the first touch"): the test allocates with MEM_RESERVE | MEM_COMMIT and 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

  1. The new export sits between the // Fixture probe (startup_view_alloc) comment and struct ViewStoreProbe, so that comment now sits above x2w_d3d12_view_token_writes.
  2. refused 0 shows only that the guard refused nothing, not that "the tokens written match the default path". token nulls 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.
  3. Between the kernel clearing the guard bit and the VirtualProtect re-arm, another thread's write to that page succeeds where writable() would refuse it. With refused 0 this doesn't matter in practice, but "as writable returning 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 and others added 2 commits November 28, 2026 09:12
- 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>
@lukaslw-lab

Copy link
Copy Markdown
Contributor Author

Thanks, @JohnConnorNPC. All five are in 88c171b1 (with a merge of main 1f07b2c1):

# Change
1 The fixture now requires the page after the guard page to stay PAGE_READWRITE. Your mutant (re-arm over info.BaseAddress, info.RegionSize) fails set_warp and set_hardware with the page after the guard page changed its protection; added to the note's mutant list.
2 The note says only the startup runs exercise lazy commit; the fixture commits up front and loads no xmem.dll.
3 The export moved above the startup_view_alloc comment, which sits on ViewStoreProbe again.
4 Reworded: refused 0 shows no token was lost to a refusal, not that every token matches the default path. The token nulls are given as within the 23–34 spread of runs without the change, but not explained.
5 Caveat added to the note and to the comment on write_view_token: between the guard fault and the re-arm, another thread's write goes through where writable() 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}).
  • On CI: right, the pr preset's filter doesn't include startup_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 pigalot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 still PAGE_READWRITE.
  • src/startup_stubs/d3d12_capture.inl: comment only (the concurrency caveat on write_view_token).
  • src/startup_stubs/d3d12_xs_device.cpp: the export moved above the startup_view_alloc comment; 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 reports written 0, refused 0 and set reports written 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_warp reports 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_ERROR dropped 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_page tests dest + bytes instead of dest + 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

startup: every view call asks VirtualQuery and xmem's commit mutex before writing its 32-byte token

5 participants