Skip to content

Memory: crash reports list XCL's own last writes to the registers' pages - #3629

Open
Royalflamejlh wants to merge 3 commits into
xcl-dev:mainfrom
Royalflamejlh:memory/write-log
Open

Royalflamejlh wants to merge 3 commits into
xcl-dev:mainfrom
Royalflamejlh:memory/write-log

Conversation

@Royalflamejlh

@Royalflamejlh Royalflamejlh commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

(Edited: head 105765416: an overlapped ReadFile is logged before its OVERLAPPED is used for another read, while it still holds its own result, or with the bytes asked for when the caller has reset it (second reviews). On main 38ad22d.)

Stacked on #3628 (xcl-run/crash-report-regions), itself on #3627; only the top commit belongs here.

A title can find its own heap overwritten (a pool block header holding float data) and fault long after the write. Nothing said whether XCL wrote there: page copies in kernelx, DirectStorage data, file reads into title buffers, texture downloads, sparse copies back and the writes made after the GPU all land in the title's memory without a trace. XCL now keeps its last 16,384 such writes in a ring without a lock, and an unhandled exception lists the ones that overlap each 64 KiB page of title memory a register points into, newest first, with their reason, age and thread. It is on without a switch.

What is in it

  • hle/write_log.rs (new): record(why, start, len), overlapping, writers_of; oldest overwritten first. A write takes a slot only from an older one and not while another is written there, so a slot's fields are always one write's. writers_of lists the newest 32 writes per range and counts the rest.
  • record calls where XCL writes title memory: kernelx.rs (page maps, moves, copies home, the zeroing decommit in a committed window, DirectStorage writes by page number), dstorage.rs and dstorage/parallel.rs, d3d12x/file_reads.rs (ReadFile into graphics memory once it finishes, with the bytes read; an overlapped read that failed or was given up is not recorded), title_textures.rs (texture downloads), sparse.rs (copy back, immediates), cpu_writes.rs (labels, fences, 64-bit values), frame.rs (the frame index).
  • eh.rs: the lines per register page in the crash report.

Measured under Wine: a record cost 70 ns on one thread, and 19-25 ns of wall time per record with 8 threads recording at once (2 M records, 3 runs); a query that matched all 16,384 kept writes 0.35-0.47 ms.

Tested

  • At 105765416: Under Wine on Mesa's CPU Vulkan driver, release, 1 run: file_reads write_log window_stats 11 passed, 0 failed; cargo test --lib -- window_stats file_reads write_log shader_mem console_heap eh:: at the top of the stack (D3D12.X: XCL_SHADER_MEM_STATS writes a placement table per allocation #3642, f1ae90ebb): 62 passed, 0 failed. a_reused_overlapped_logs_each_read_at_its_own_buffer failed without the fix.
  • RTX 5070 Ti 16 GB (driver 621.24), Ryzen 7 9800X3D, Windows 11 Pro 26300, debug, at the top of this stack (D3D12.X: XCL_SHADER_MEM_STATS writes a placement table per allocation #3642, head 5653fd69, with an earlier version of this PR's commit, before the second review's fixes): cargo test --workspace -- --test-threads=4, 2,931 passed, 0 failed, 51 ignored, 1 run; 28 SKIPPED lines (wave64 tests: no wave64 on this card; heap-window tests without XCL_HEAP_WINDOWS=1; no D3D12 debug layer). The stack's GPU tests (gpu_mem_stats_*, the writer and geometry-pass tests) ran and passed.
  • RTX 4060 Ti 16 GB, NVIDIA 615.71.09, AMD Ryzen 9 9950X3D, Linux, Wine 11.21 Staging, Rust 1.98.0, x86_64-pc-windows-gnu, release. Tests under Wine on Mesa's CPU Vulkan driver (no card).
  • cargo check -p xcl --release --target x86_64-pc-windows-gnu, with and without --tests: no errors, no new warnings.
  • cargo test -p xcl --release --target x86_64-pc-windows-gnu --lib -- --test-threads=8 kernelx dstorage cpu_writes hle::d3d12x::frame eh:: write_log window_stats file_reads: 151 passed, 0 failed, 1 ignored in 2 of 3 runs; in the third frame::tests::origin_events_issue_tokens_and_flips_have_statistics failed once and passed on its own.
  • The four write_log tests: overlap order, the ring keeping the last writes, a slot taken by a newer write or being written left alone, and a range listing its newest writes and counting the rest. file_reads::a_finished_read_is_a_known_write checks the bytes recorded for a plain and an overlapped read, and that a failed overlapped read records nothing.
  • 2 title runs on our development tree with an earlier version of this in (705 s and 1,501 s) had no unhandled exception, so the lines have not yet been seen at a real crash.

Regression run
RTX 2070 SUPER 8 GB (driver 621.01), Ryzen 7 5700X3D, Windows 11 Pro 26200, release builds by scripts/smoke.ps1, the same retail title for 60 s each.

main:

xcl-smoke v1
commit: 5eaadb54c14e0fc29597b2a1689d7dbd83491303
gpu: NVIDIA GeForce RTX 2070 SUPER
title: 5330474d
seconds: 60
exit: 0x0
frames shown: 1575 (PresentX 1575)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15670 MB

this PR's head:

xcl-smoke v1
commit: f95a41f5226a3432a7c926ebffcbb3ef60597f71
gpu: NVIDIA GeForce RTX 2070 SUPER
title: 5330474d
seconds: 60
exit: 0x0
frames shown: 1632 (PresentX 1632)
presents per 2 s (last 3): 60 61 60
device removals: 0
crashes: 0
peak commit: 15182 MB

Not tested

  • AMD, a real crash with the new lines, the record's cost on Windows.

AI use

  • Claude Code (Claude Opus 5.5): wrote the change, its tests and this description, measured the record's cost, and ported it to main.

@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory area:directstorage DirectStorage queues, requests, decompression area:docs-ci Documentation, scripts, workflows, repo governance area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH area:memory Virtual/physical memory, arenas, VRAM budget, UMA gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) labels Dec 2, 2026
@Foniass

Foniass commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Review (head b8e0e3e, diff read, not a merge-check; draft, checks red):

  • Two writers on one slot can tear why. record (hle/write_log.rs ~49-57) writes why and why_len as two relaxed stores guarded only by seq. If a writer is preempted after its fetch_add while 16,384 more records arrive, the writer that wraps onto the same slot publishes its seq, and the stalled writer's field stores land after it. A reader in overlapping (~86) can then see a matching seq with why from one write and why_len from the other and pass them to from_raw_parts + from_utf8_unchecked: UB inside the crash handler. Storing the reason as one word (an index into a reason table, or a &'static str pointer) removes the tear, and the writer should give up the slot if seq changed under it.
  • ReadFile is logged for every caller and before the read. The kernelbase ReadFile / ReadFileEx hooks (vfs_hooks.rs ~558, ~574) record every read in the process with the requested size before the call, so failed or short reads are logged as full writes, and reads into memory the report never queries (title_region covers only windows, pair views and graphics reservations) push out the title-memory writes the report is for. Recording after the read, with the bytes read and only for a title-region destination, avoids both.
  • No per-page limit in the crash report. eh.rs ~123 logs every kept write overlapping each register's 64 KiB page; labels, fences and the frame index are rewritten to the same few addresses, so one register can print up to 16,384 lines. A cap of a few dozen newest lines per page plus a count of the rest keeps it readable.
  • The two tests share the global ring. the_ring_keeps_the_last_writes (~124) records KEEP + 5 entries into the same static RING that writes_are_found_by_overlap_newest_first uses; run in parallel, it can push out first / second / beside before the other test asserts. Other tests that reach map_pages, write_physical_pages and the dstorage paths now record into it too.

Fine: the record points sit right before the copies in kernelx, dstorage, sparse, cpu_writes, frame and title_textures with the right addresses and lengths; the reader's seq / fence pattern is right with one writer per slot; the stack is declared. The xcl-smoke blocks are still owed (the reason PR description is red).

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@Royalflamejlh

Copy link
Copy Markdown
Contributor Author

Thanks. Fixed at 666ed73: a write takes its slot only from an older one and not while another is being written there; ReadFile is recorded after the read, with the bytes read, for graphics memory only (ReadFileEx is no longer recorded, since its bytes are known only at completion); a page lists its newest 32 writes and counts the rest; each test has its own ring. The record re-timed at 70 ns on one thread, 19–25 ns with 8 (in the commit message).

AI use: Claude Code (Claude Opus 5.5) made the fixes and wrote this reply.

@Foniass

Foniass commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Follow-up review (head 666ed73, since b8e0e3e, diff read, not a merge-check; draft, checks red):

  • Torn why / why_len in the crash handler: fixed. Ring::write (hle/write_log.rs ~75-87) claims a slot with a compare_exchange to BUSY only if its seq is not newer than this write, so a stalled writer fails the exchange; the reader skips BUSY slots and rechecks seq after the acquire fence (~93-103). a_slot_taken_by_a_newer_write_or_being_written_is_left_alone covers both cases.
  • ReadFile logged for every caller and before the read: mostly fixed. The record moved to file_reads::returned (hle/d3d12x/file_reads.rs ~41): after the call, only for a destination in graphics memory, with the bytes read. The overlapped path (~60) still records the requested p.len, including a read that completed with an error or was dropped after GIVE_UP, and only at the next look that calls take_finished, so a crash before that look misses it. ReadFileEx is no longer recorded.
  • No per-page limit in the crash report: fixed. writers_of (~115) lists the newest 32 writes per page and then and N older writes, with a test.
  • Tests sharing the global ring: fixed; the write_log tests build their own Ring.

New:

  • The description no longer matches the commit. It still lists vfs_hooks.rs (ReadFile, ReadFileEx) as record points, gives the old costs (66 ns, 18 ns, 11 µs) where the commit message now says 70 ns, 19-25 ns and 0.35-0.47 ms, and says "the two new tests" (now four plus the file_reads assertion). The xcl-smoke blocks are still owed (the reason PR description is red).

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@Royalflamejlh

Copy link
Copy Markdown
Contributor Author

Fixed at 53f2c1f: the overlapped path records the bytes the read returned, and nothing for a failed or given-up read; the test covers both. The description is rewritten to match the commit (record points, costs, tests). The smoke blocks are for the previous head and run again for this one.

AI use: Claude Code (Claude Opus 5.5) made the fixes and wrote this reply.

@Foniass

Foniass commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Follow-up review (head 53f2c1f, since 666ed73, diff read, not a merge-check; draft, checks red):

  • ReadFile (overlapped) recorded with the requested length, even on failure: fixed. take_finished (file_reads.rs ~56-62) now records only a read whose status is 0, with the bytes from InternalHigh capped at the request, and records nothing for a failed read or one dropped after GIVE_UP; the test covers a failed read and a short one (0x1800 of 0x2000).
  • Description out of date: unchanged; it still lists vfs_hooks.rs (ReadFile, ReadFileEx) and the old costs.

New: none.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@Royalflamejlh
Royalflamejlh marked this pull request as ready for review December 2, 2026 04:10
@Foniass

Foniass commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Follow-up review (head f95a41f, since 53f2c1f, diff read, not a merge-check; checks red):

  • Earlier points: unchanged. git range-diff shows the own commit identical to the previous one; this push only rebases the stack onto the current main.

New: none.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@gut0to gut0to left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

AI-assisted review (model: GPT-6). I traced the pending overlapped-read record through completion polling.

Comment thread crates/xcl/src/hle/d3d12x/file_reads.rs Outdated
return true;
}
if status == Some(0) {
crate::hle::write_log::record("ReadFile (overlapped)", p.at, (ov.add(1).read_volatile() as usize).min(p.len));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could this pending request be correlated with an operation generation instead of only the OVERLAPPED* address? The entry remains in PENDING until this poll. If the caller reuses a completed OVERLAPPED for another read first, this stale entry sees the new status and InternalHigh, then records the new byte count at the previous p.at, creating a false crash-report attribution. Could the old entry be removed or replaced when the pointer is reused, with a test that reuses one OVERLAPPED before take_finished? AI-assisted review (model: GPT-6); I traced the request identity from enqueue through completion.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right: a caller that reused a finished OVERLAPPED before the next look made the old entry read the new read's status and byte count. The ReadFile hook now calls file_reads::reusing(ov) before the call (vfs_hooks.rs). That takes any entry on that OVERLAPPED while it still holds the earlier read's result, so each read is logged at its own buffer with its own bytes. a_reused_overlapped_logs_each_read_at_its_own_buffer reuses one OVERLAPPED before take_finished. Without the fix the first buffer got the second read's 0x400 bytes; with it, 1 run each under Wine, it passed. Head 69ccace1.

AI use: Claude Code (Claude Opus 5.5) made the fix and wrote this reply.

@bmjask

bmjask commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

@Prime1997 could you take a look at this one for review and merge? My automated review notes are above, if any.

@Foniass

Foniass commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Follow-up review of 69ccace1, since f95a41f5 (code reading only, not a merge-check; PR description red) · 1 medium, 1 nit

In short: The PR keeps XCL's last 16,384 writes into title memory in a lock-free ring and lists the ones on each register's 64 KiB page in the crash report. Since f95a41f5 the only change to the PR's own commit is the overlapped ReadFile path: hk_read_file now calls file_reads::reusing before each ReadFile, so a pending read on the same OVERLAPPED is logged before the next one overwrites its result. Nothing blocks on correctness, but the new path drops the record when the caller has reset the OVERLAPPED. The xcl-smoke block still names f95a41f5, which is why PR description is red.

Medium

  1. A reset OVERLAPPED drops the earlier read from the log.

    • reusing (file_reads.rs ~49-58, called from vfs_hooks.rs ~559 before orig) reads Internal / InternalHigh after the caller has prepared the struct for the next read.
    • Many callers zero the whole OVERLAPPED before each request. finish (~77-78) then sees status 0 with InternalHigh 0 and records length 0, and Ring::record returns early. A read that landed in graphics memory is missing from the crash report.
    • The new test only covers a caller that leaves those fields alone.

    Fix: on the reuse path, record the requested p.len when the struct no longer holds a result, with a reason saying the length is not known, and add that case to a_reused_overlapped_logs_each_read_at_its_own_buffer. ReadFileEx and WriteFile on the same OVERLAPPED still log the other call's byte count through take_finished; at least say so in the module doc.

Nit

  1. Tested names an older commit. Tested says the full-suite run at D3D12.X: XCL_SHADER_MEM_STATS writes a placement table per allocation #3642's 5653fd69 "carries this PR's commit unchanged". That head has no file_reads::reusing, so it carries the previous version.
Checked and fine
  • Earlier finding fixed: torn why / why_len in the crash handler. A slot is claimed with compare_exchange to BUSY, and the reader skips BUSY and rechecks seq (write_log.rs ~74-103).
  • Earlier finding fixed: ReadFile was logged for every caller and before the read. It is now recorded after the call, only for graphics memory, with the bytes read.
  • Earlier finding fixed: no per-page limit. writers_of lists the newest 32 and counts the rest, with a test.
  • Earlier finding fixed: tests shared the global ring. They now build their own Ring.
  • Earlier finding fixed: the description was out of date.
  • reusing takes only PENDING, in the same lock order as take_finished, and costs one atomic load when nothing is pending.
  • a_reused_overlapped_logs_each_read_at_its_own_buffer fails against the previous take_finished.
  • The PR's own commit is +285/-11, under the target; the rest of the 769 lines belongs to the stacked xcl run: the crash report places every register in the title's memory #3628 / D3D12.X: XCL_WINDOW_STATS logs each memory window's traffic #3627.

Written by Claude Code (Claude Opus 5.5) and posted by Foniass without a human check of each point.

Royalflamejlh and others added 3 commits December 2, 2026 14:26
An address the title gives the GPU outside every memory window is no GPU
memory, and nothing said so: with 4 windows the title's GPU views lay
outside them and the world's mesh programs read empty meshlet lists
(xcl-dev#3565). With XCL_WINDOW_STATS=<seconds>, every that many seconds:

- a line for each window: kind, range, bytes with memory on the GPU
  (tiles of a sparse or view window, the committed bytes of a coherent
  one), and since the line before the bytes uploaded into it, the pages
  the write watch found written, the bytes read back and the GPU-view
  tiles filled;
- a line with the addresses given outside every window, by
  view_want::Path, and the pages written outside them.

The first 8 addresses of each path outside every window are logged with
their size and what lies there (a GPU view of the title's memory, a
graphics reservation, plain memory, nothing committed). The counters are
relaxed atomics where the uploads, read-backs, write-watch looks, mirror
fills and view_want asks already happen; off, each is one load. Nothing
is remapped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An unhandled exception logged the page events at the pages the registers
point into, and only where there were some. A crash where a register held
an address in a memory window with no page events, the case of memory the
title corrupted in a coherent window, said nothing about where that
address lay.

Each register that points into a memory window (sparse, coherent, view), a
view of a pair or a graphics reservation now gets one line: the region, the
64 KiB page, its page events, and what XCL_WINDOW_STATS counted for its
window when it is on. Other registers keep the line they had. Locks are
only tried on this path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A title can find its own heap overwritten (a pool block header holding
float data) and fault long after the write. Nothing said whether XCL
wrote there: page copies in kernelx, DirectStorage data, file reads into
title buffers, texture downloads, sparse copies back and the writes made
after the GPU all land in title memory without a trace.

hle::write_log keeps the last 16 384 of them (start, length, reason,
time, thread) in a ring without a lock, oldest overwritten first, and
writers_of(start, len) gives the ones that overlap a range as lines,
newest first, the newest 32 of them a line each and the rest counted. A
write takes its slot only from an older one and not while another is
written there, so a slot's fields are always one write's. Recorded:
kernelx page maps, moves, copies home and the zeroing decommit in a
committed window; DirectStorage writes (virtual, by page number, BCPack
fills, the parallel path); ReadFile reads into graphics memory once they
finish, with the bytes read (a failed one not at all; an overlapped one
at the first look that finds it done, or when its OVERLAPPED is used
for another read, while it still holds this one's result, or with the
bytes asked for when the caller has reset it); title_textures' WriteProcessMemory runs;
sparse copy_back pages and immediates; cpu_writes' labels, fences and
64-bit values; the frame index write.

An unhandled exception now logs, for each 64 KiB page of title memory a
register points into, the writes kept there.

A record cost 70 ns on one thread under Wine, and 19-25 ns of wall time
per record with 8 threads recording at once (2 M records, 3 runs); a
query that matched all 16 384 kept writes 0.35-0.47 ms. It is on without
a switch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added needs-pr-check Failing the PR description check (CONTRIBUTING.md) and removed needs-pr-check Failing the PR description check (CONTRIBUTING.md) labels Dec 2, 2026
@Royalflamejlh

Copy link
Copy Markdown
Contributor Author
  1. Right: a caller that zeroes the OVERLAPPED before reusing it left status 0 and 0 bytes, so the read was dropped. Fixed at 10576541: on the reuse path, 0 bytes logs the length asked for, as ReadFile (overlapped; its OVERLAPPED was reset: the bytes asked for). a_reused_overlapped_logs_each_read_at_its_own_buffer now covers a reset OVERLAPPED too, and failed without the fix. The module doc says only ReadFile is followed, so a ReadFileEx or WriteFile on the same OVERLAPPED before the look gives the read its result.
  2. Tested now says that the run at 5653fd69 had an earlier version of this commit. A whole-suite run at the stack's new top is queued on Windows, and its counts go in when it ends.

AI use: Claude Code (Claude Opus 5.5) made the changes and wrote this reply.

@github-actions github-actions Bot added the needs-pr-check Failing the PR description check (CONTRIBUTING.md) label Dec 2, 2026
@Foniass

Foniass commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Follow-up review of 10576541, since 69ccace1 (code reading only, not a merge-check; PR description red) · no new findings

In short: This push fixes the reset-OVERLAPPED point from my last review. When the earlier read's struct was reset before reuse (status 0, no byte count), the read is now logged with the bytes it asked for, under a reason that says so. The module doc now says that only ReadFile is followed. Tested now says which earlier version the GPU run was made on. The smoke block names f95a41f5 (PR description is red).

Checked and fine
  • Earlier finding fixed: a reset OVERLAPPED dropped the read from the log. finish now records p.len as "ReadFile (overlapped; its OVERLAPPED was reset: the bytes asked for)" when the read is done but the struct holds 0 bytes. a_reused_overlapped_logs_each_read_at_its_own_buffer covers it with a struct zeroed before reuse.
  • Earlier finding fixed: ReadFileEx / WriteFile on the same OVERLAPPED are now named in the module doc.
  • Earlier finding fixed: Tested said the GPU run carried this commit unchanged. It now says the run was on an earlier version.
  • The rest of the push is D3D12.X: XCL_WINDOW_STATS logs each memory window's traffic #3627 / xcl run: the crash report places every register in the title's memory #3628 underneath.

Written by Claude Code (Claude Opus 5.5) and posted by Foniass without a human check of each point.

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

Labels

area:d3d12 D3D12.X runtime, command lists, resources, sparse memory area:directstorage DirectStorage queues, requests, decompression area:docs-ci Documentation, scripts, workflows, repo governance area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH area:memory Virtual/physical memory, arenas, VRAM budget, UMA gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) needs-pr-check Failing the PR description check (CONTRIBUTING.md)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants