Threads that fault on the same CLWB site together lost the race: a #UD handled
after another thread rewrote the site decoded CLFLUSHOPT (or the NOP), did not
know it and returned EXCEPTION_CONTINUE_SEARCH, which ends the title.
- The handler continues at a site that already holds the form written on this
host (or the NOP's halfway state): the CPU runs it.
- One lock around unprotect, re-check, write and protect again, so two threads
never interleave on a page; a site found rewritten under it is not written.
- The NOP is written opcode first (66 0F 1F /6 in between is a NOP too).
- The sites counter counts each site once, by the thread that wrote it.
cpu_emulation_test calls the handler with a made-up #UD, so it runs on hosts
with CLWB: first #UD, a second one on the rewritten site, and 16 threads on
2048 fresh execute-read sites. Before the fix 27958 of 32768 concurrent #UDs
were passed on, 4810 rewrites were counted for 2048 sites and pages were left
writable; with it all checks pass.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review follow-up for #1564:
- Registration and the cleanup of exit, quick_exit and _cexit hold one recursive lock of the run's own, from before the thread-local callback through the table's drain (and _cexit's stream flush), as the UCRT holds its exit lock: two threads never clean up at once, and a registration from another thread waits for the cleanup. The exiting thread may still register from a handler, _cexit still returns, and the lock is released before the run ends (end_run), also when a handler ends the run from inside a drain.
- Each guest cleanup callback (atexit, at_quick_exit, the thread-local destructor callback) runs inside a C++ exception boundary: one that throws ends the run through game_abort, as the UCRT's terminate() would (exit code 3), instead of leaving the process with 0xE06D7363.
tiny_atexit gains four modes, held to the native sequence: concurrent_cexit (a second thread's _cexit, with a thread_local of its own, waits for the cleanup), quick_exit_register (an at_quick_exit from a second thread during a quick_exit drain waits, so its handler never runs), quick_exit_throw and exit_throw (exit code 3). In the concurrent modes the slow handler waits until the second thread is about to call, so the sequence is fixed under any scheduling with the lock. All four fail on c5b51e9 through --run.
The game's at_quick_exit() (_crt_at_quick_exit) and _register_thread_local_exe_atexit_callback still
reached the host CRT, which the game shares with gta6rt. Under `gta6rt --run`:
- quick_exit() ran none of the game's at_quick_exit handlers;
- exit() and _cexit() never ran the exiting thread's thread_local destructors, and a second
registration in the host's one-slot callback would terminate gta6rt.
Now _crt_at_quick_exit goes to a second table of the run's own, which quick_exit() runs newest first
under the exit lock; _exit() and _Exit() run none (they no longer share game_quick_exit). The thread-local
callback is the run's own: exit() and _cexit() call it with DLL_PROCESS_DETACH before the atexit handlers,
as the UCRT does, and a second registration aborts, as the UCRT's terminate() does. Both reset per run.
tiny_atexit gains five modes (at_quick_exit, at_quick_exit_exit, at_quick_exit__exit, thread_local_exit,
thread_local_cexit). Their expected sequences in check_atexit.cmake were measured by running the fixture
natively on Windows 11; before this change --run differed on at_quick_exit, thread_local_exit and
thread_local_cexit. run_tiny_atexit now holds 13 paths to the Windows sequence.
retire_orphan_windows frees the retired windows whose submissions have
completed after this submission's refresh_shadows, and freeing writes
the window's copy back to the title's pages once more
(free_retired_shadow). The refresh had already copied those pages to
the live copies, so the slot-133 buffer that replaced the window read
the older bytes until the next submission.
Moving the free ahead of the refresh, as suggested, is not enough on
its own: the write-back opens the pages it writes, and the refresh's own
copy back of the live copies' batches (write_back_titles) then takes
those pages for ones the title wrote and drops the buffer's GPU results
there (measured with this test: with the free moved first, the words
written through the buffer did not all reach the pages). So the free stays after the refresh, and a
retired copy's bytes that go back are written into every other copy over
them that still held the title's older bytes, as the next refresh would;
a copy that changed a byte keeps its own. Under lock_, in place, no
allocation. The epoch/ticket gating (src/command_list_epochs.h,
stamp_retired_windows) is untouched.
d3d12_title_va_test --window-orphans: the submission that frees the
window reads the held words through the buffer placed over the ring.
With LEONIDA_TITLE_WRITEBACK=0 (d3d12_title_window_orphans_free) it read
0 of 64 before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gt9hwNPurxyJ9LAfUbNiYr
ShadowPages::write_back compared the copies in 256-byte pieces and kept
one flag per piece: once a live copy had changed any byte of it, a
retired window's copy was left out of the whole piece. A retired window
and the slot-133 buffer that replaced it can have written disjoint
bytes of one piece (the window [384, 512), the buffer [256, 384)), so
the window's bytes were dropped. The batch path (`only` null) had the
same flag (`live = live || pass == 0`).
The piece now keeps a fixed 256-entry mask on the stack (no heap under
lock_) of the bytes a live copy changed, set in the first pass; a
retired copy goes back on every byte it changed that no live copy did,
in runs. Copies are still compared against the title's bytes as they
were before any copy went back, and a live copy now writes only the
bytes it changed, so one live copy no longer writes the title's older
bytes over another's result in the same piece.
d3d12_title_va_test --window-orphans: the held submission writes through
the window from ring + 384, a later one through the buffer placed over
the ring from ring + 128, and all the held words must reach the title's
pages (32 of 64 did before). The ring is write-combined so the buffer is
a copy on every driver. New ctest entry d3d12_title_window_orphans_free
runs it with LEONIDA_TITLE_WRITEBACK=0, where the window's free is the
only way back.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gt9hwNPurxyJ9LAfUbNiYr
cover_pool_request planned its cover (title_pool_cover) under short
shared locks of the alias table, then made the mapping and entered it
with replace_twin, which erased every range in the span unconditionally.
A slot-133 buffer is entered (Resource::alias_title_va) without
title_window_mutex, so one placed in either view of the span in between
was erased with the windows. And replace_twin's result was ignored: the
cover was recorded in pool_spans and returned as made either way.
replace_twin now takes the caller's predicate and, under the same
unique lock as the replacement, refuses with nothing changed when a
range it would erase is not one the caller planned to replace. The
cover passes the windows its plan found. On refusal the window it made
is dropped at once (drop_title_window: nothing answered from it, so no
submission names it) and the request falls back to the zero-size view
as before. The same drop now follows add_twin's refusal in
resolve_pool_memory, which left its new window charged to the budget
and refreshed with nothing answering from it.
title_window_plan_test: a non-window range entered in the cover's span
(CPU view, then twin view) between title_pool_cover and replace_twin is
refused and left intact.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gt9hwNPurxyJ9LAfUbNiYr
title_pool_cover translated a mapping found in the pool's other view back
with `x - (twin - va)` modulo 2^64. A window that starts below the twin
view (m.title_va < twin - va) wrapped to near UINT64_MAX, passed the
pool bounds check, and replace_twin then erased it whole, leaving the
part of it outside the cover unanswered. A twin view that does not fit
in 64 bits (twin < va - pool_lo, or pool_hi - va > UINT64_MAX - twin)
made the twin-view query wrap into an empty range, so a range in the way
was not seen at all.
The twin view's bounds [twin_lo, twin_hi) are now checked up front,
queried in non-wrapping coordinates, and a hit outside the view it was
found in refuses the cover like one outside the pool.
title_window_plan_test: check_pool_cover_bounds with the review's
example (pool [0x100000, 0x500000), va 0x120000, twin 0x420000, window
[0x200000, 0x440000)) and the two overflow cases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gt9hwNPurxyJ9LAfUbNiYr
With LEONIDA_ASYNC_PIPELINES=1, graphics and mesh pipelines (all their stages) now take
LEONIDA_ASYNC_MIN_BYTES_GRAPHICS when it is set, and LEONIDA_ASYNC_MIN_BYTES otherwise, so
nothing changes while it is unset. Compute keeps LEONIDA_ASYNC_MIN_BYTES. 0 makes every graphics
pipeline asynchronous. One startup line states both thresholds; text that is not a number is
ignored with a log line.
Thread dumps from a retail run showed the draw thread building lazy graphics variants under the
1 MiB threshold in place, 2.5-11 s each, back to back, with no present for about 100 s. A skipped
draw costs one draw. The image needs the small lighting and post compute programs, and those
are still built in place.
Tests: async_pipeline_size_test covers the per-kind decision and the unset fallback;
d3d12_pipeline_workers_test --async-graphics-zero / --async-graphics-unset (new ctests) hold
the compile worker with compute over its threshold.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ShadowPages::write_back decided page by page, inside its loop, whether a page's GPU result goes back. A copy
that faults on another guard (a geometry watch cell) makes the handler reopen the whole 64 KiB tile on the
write-back's own thread (reopen_range, under the lock #2418 lets it reuse), which clears kClean and kProtected
there. The runs after it in the tile then read as written by the title: their GPU results were dropped from
a batch already taken off the queue, and the next forward copy put the title's older bytes over them.
The decision is now taken for every page of the range before the first copy, into a host-heap vector the
tracker keeps. A page the title wrote before the write-back (not guarded and not clean after the asking)
is still held, so the title's words win as before.
Test: d3d12_title_window_write_back_fault_tile (d3d12_title_va_test --window-writeback-fault-tile), the
#2418 fault case with a second dispatch on the third page of the tile and a title write on the second.
Found by DadaCap's review of #2418.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Resolve runtime/README.md conflict with #2220 (LEONIDA_PIPELINE_ORDER_LOG)
and note in docs/d3d12_vulkan.md that LEONIDA_COMPILE_GATE=adaptive and 1
gate graphics creates as well (#2412).
Review of #2462:
- An unclear calibration (a slow submission the create does not explain) is no
longer written to compile_gate_calibration.txt, and an unclear line an
earlier build wrote is not read as a cached verdict: the next launch measures
again instead of staying classic until the driver changes
(compile_gate_policy::cache_verdict, tested in compile_gate_policy_test).
- After a fence wait that fails, the fence and command pool are destroyed only
once the pending submission's fence signals (up to 60 s); on a lost device or
if it still has not, they are left alive. The verdict stays classic.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With LEONIDA_COMPILE_GATE unset or `auto`, the device measures the driver once
before the VulkanContext exists: a worker creates a synthetic compute pipeline
(SPIR-V written in code, new specialization seed each run, no pipeline cache)
while the queue gets empty submissions waited on a fence. Quick submissions with
the create still in the driver give the adaptive gate; a held one, a create too
fast to tell, an unexplained slow submission or any failure give classic. The
verdict is cached per vendor/device/driver/pipelineCacheUUID in
compile_gate_calibration.txt beside the pipeline cache;
LEONIDA_COMPILE_GATE_RECALIBRATE=1 measures again. Only the title process
calibrates; build hosts and tests keep classic. `classic`, `adaptive`, `1`, `0`
still force a mode.
Tests: compile_gate_policy_test (no GPU: settings, NVIDIA version decode,
verdicts, cache file, SPIR-V shape) and vk_compile_gate_calibration_test (GPU:
a verdict within 20 s, adaptive on NVIDIA 621.40).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
qhukz13 claimed and fixed the same race for main in #2361
(RT-COMPILE-OWNER-RACE). The single-worker branch of next_locked now uses
their code line for line, so whichever of the two lands second merges
trivially. The pool branch is as before 9b8760d32. No behaviour change;
d3d12_compile_pool_test passes 5 of 5 repeats.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Second review on #2412:
- enter_submit no longer switches to the held limit for good once it sees a create of a kind that holds
submissions: each pass checks held_blocking(). With one in the driver the held limit applies on its own (also when
it is under the plain limit) and the budget is off; without, the plain limit and budget apply, so a submission
goes ahead at the plain limit once the holding create left while a create of a kind with no verdict stays. The
"reached the limit, but ..." notice comes once per wait, and the wait wakes when a holding create leaves.
- SubmitTurn passes `probe` into enter_submit: a present or wait never goes ahead at the probe budget, only at the
limit.
- VulkanPresenter::submit's turn does not probe (it covers the startup frame's fence wait and the present); the
nested queue_submit turn still probes the vkQueueSubmit alone.
- submit_mutex_test: gate_second_review covers the four cases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- CreateKind, CreateInfo and GateAhead move out of SubmitMutex to namespace scope (CompileGateKind,
CompileGateCreate, CompileGateAhead), with using aliases so SubmitMutex::CreateInfo and the rest still work:
clang and GCC reject a `= {}` default argument of a nested class with default member initializers inside the
enclosing class (review on #2412).
- LEONIDA_COMPILE_GATE unset is the classic gate again, as on main: compute creates only, no limit, no probes;
graphics creates do not pass it. The adaptive gate is LEONIDA_COMPILE_GATE=adaptive: on a driver that holds
submissions (630.20) its first probe of a kind enters the driver after the 2.5 s probe budget and loses the
device if that create stays 10 s more, which the classic gate waits out. `1` and `0` are unchanged.
- submit_mutex_test: a classic-gate case (a graphics create holds nothing back, a 3 s compute create is waited out
with no verdict), the adaptive cases opt into graphics gating; passes unset, with `adaptive` and with `1`.
- README rows and docs/d3d12_vulkan.md say so; the "held verdict 2 s into the probe" wording is corrected (it comes
when the probe returns, and the probe holds the submit lock until then).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The title writes the same words into thousands of slots of its console
descriptor heap, many of them data that reads as a V# into title memory
with no copy, and each fix_vsharp of those costs VirtualQuery calls: a
third of the submitting thread's time after the story load under Wine.
refresh_console_heap now keeps fix_vsharp's answers for the pass over the
copied slots and, separately, for the pass over the slots the changes
since the last submission touch, and drops them after each.
Retail boot, Proton, RTX 3090, 900 s: heap refresh after the story load
107.8 -> 18.8 ms a submission (max 5,332 -> 1,109), submissions after it
1,977 -> 2,527.
AI-assisted; tested under WineHQ 11.22 and GE-Proton11-14 on an RTX 3090
(NVIDIA 590.48.01).
The demanded job leaves the running list only after its function returns,
so has_compile_waiter() could still see it right after the log said it had
finished (1 failure in 10 runs). Found while merging this into #2275.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>