d3d12_vulkan: copy_guarded and hash_guarded catch page faults under clang-cl (RT-CLANG-EHA) #1717

Closed
github-bot wants to merge 3 commits from gh-pr/1717/head into gh-pr/1717/base
Owner

@⁠ImLele69 on GitHub · 2026-11-25 09:29 UTC · original
ImLele69:rt-clang-eha → main · +43 −27 · 1 files · 3 commits · closed without merging

Claimed in the coordination issue as: RT-CLANG-EHA — #1608

What and why

copy_guarded and hash_guarded (shims/d3d12_vulkan_device.cpp) read the title's memory inside __try/__except, so that a page the title unmaps between the check and the read comes back as false instead of killing the process. That holds under MSVC. Under clang-cl with /EHsc it does not: clang lowers memcpy to an intrinsic it treats as unable to throw, and loads inlined into the __try block (the hash, the MOVNTDQA path) are not covered either, so the __except never runs.

Seen on a clang-cl/xwin build under Wine 11.21 (Fedora 44, RTX 2070 SUPER), twice during the story load: a slot-133 shadow refresh copied a page xmem had just unmapped and the process died. Startup trace exception record: read of 0x7fe6bac60000 in ucrtbase.dll+0x171a6 (memcpy), region MEM_RESERVE/MEM_PRIVATE; with a linker map, the stack is copy_guarded < ShadowPages::refresh < the device's submission callback < VulkanQueue::execute_now < CommandQueue::ExecuteCommandLists.

Change

The bodies become noinline functions (copy_unguarded, hash_unguarded) that the guards call through a volatile function pointer inside the __try. clang-cl then sees a call it cannot prove nothrow and covers it. No build flag changes.

The first version of this PR added /EHa instead. That did not work: /EHa on gta6rt never reached these functions, which gta6rt_d3d12_com compiles, and /EHa on that library or on every source does not link with clang 20 (std::variant's trivial destructor is referenced but never emitted: 155 targets failed). Those commits are undone in this branch; the diff against main is only d3d12_vulkan_device.cpp.

Tested

  • A small program with the same toolchain (clang-cl 20, lld-link 20, xwin), /EHsc, under Wine 11.21: a fault from a MEM_RESERVE page is caught through the pointer call, both via memcpy and via a MOVNTDQA loop, and a copy from committed memory still succeeds. The same function with memcpy directly in the __try dies at the same ucrtbase.dll address as the game run.
  • Every target under runtime/ cross-builds (ninja -k 0, 0 failed) on main 3de22a190.
  • A boot of the retail 1.0.2.1 with this change (on top of other local changes, counts only) went past the point where the two earlier runs died in this copy; it then stopped on an unrelated abort, which I'm handling separately.

Other __try users (file_system.cpp, pe_loader.cpp, title_page_watch.h, startup_trace.h) may have the same limit under clang-cl; I have not changed or tested them here.

AI-assisted: yes (Claude Code).

**@⁠ImLele69** on GitHub · 2026-11-25 09:29 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1717) `ImLele69:rt-clang-eha` → `main` · +43 −27 · 1 files · 3 commits · closed without merging Claimed in the coordination issue as: RT-CLANG-EHA — https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608 ### What and why `copy_guarded` and `hash_guarded` (`shims/d3d12_vulkan_device.cpp`) read the title's memory inside `__try`/`__except`, so that a page the title unmaps between the check and the read comes back as `false` instead of killing the process. That holds under MSVC. Under clang-cl with `/EHsc` it does not: clang lowers `memcpy` to an intrinsic it treats as unable to throw, and loads inlined into the `__try` block (the hash, the `MOVNTDQA` path) are not covered either, so the `__except` never runs. Seen on a clang-cl/xwin build under Wine 11.21 (Fedora 44, RTX 2070 SUPER), twice during the story load: a slot-133 shadow refresh copied a page xmem had just unmapped and the process died. Startup trace exception record: read of `0x7fe6bac60000` in `ucrtbase.dll+0x171a6` (memcpy), region `MEM_RESERVE`/`MEM_PRIVATE`; with a linker map, the stack is `copy_guarded` < `ShadowPages::refresh` < the device's submission callback < `VulkanQueue::execute_now` < `CommandQueue::ExecuteCommandLists`. ### Change The bodies become `noinline` functions (`copy_unguarded`, `hash_unguarded`) that the guards call through a `volatile` function pointer inside the `__try`. clang-cl then sees a call it cannot prove `nothrow` and covers it. No build flag changes. The first version of this PR added `/EHa` instead. That did not work: `/EHa` on `gta6rt` never reached these functions, which `gta6rt_d3d12_com` compiles, and `/EHa` on that library or on every source does not link with clang 20 (`std::variant`'s trivial destructor is referenced but never emitted: 155 targets failed). Those commits are undone in this branch; the diff against `main` is only `d3d12_vulkan_device.cpp`. ### Tested - A small program with the same toolchain (clang-cl 20, lld-link 20, xwin), `/EHsc`, under Wine 11.21: a fault from a `MEM_RESERVE` page is caught through the pointer call, both via `memcpy` and via a `MOVNTDQA` loop, and a copy from committed memory still succeeds. The same function with `memcpy` directly in the `__try` dies at the same `ucrtbase.dll` address as the game run. - Every target under `runtime/` cross-builds (`ninja -k 0`, 0 failed) on `main` `3de22a190`. - A boot of the retail 1.0.2.1 with this change (on top of other local changes, counts only) went past the point where the two earlier runs died in this copy; it then stopped on an unrelated abort, which I'm handling separately. Other `__try` users (`file_system.cpp`, `pe_loader.cpp`, `title_page_watch.h`, `startup_trace.h`) may have the same limit under clang-cl; I have not changed or tested them here. AI-assisted: yes (Claude Code).
copy_guarded, hash_guarded and the other __try readers of the title's memory only caught a
page fault under MSVC. clang-cl lowers memcpy to an intrinsic it treats as unable to throw,
so without asynchronous exceptions the __except never runs: a shadow refresh that read a page
xmem had just unmapped killed the process (ucrtbase memcpy, region MEM_RESERVE). Reproduced
with a 20-line program under Wine 11.21: /EHsc dies, /EHa returns false from the guard. Only
dstorage.cpp and thread_stacks.cpp had /EHa; the whole target needs it.
copy_guarded and hash_guarded are in d3d12_vulkan_device.cpp, which the static library
gta6rt_d3d12_com compiles, not gta6rt: the first commit's /EHa never reached them (its build
line still had /EHsc only), and a story-load boot died in the same copy.
/EHa for gta6rt never reached these: they are compiled in gta6rt_d3d12_com. And /EHa on that
library, or on every source, does not link with clang 20: it leaves std::variant's trivial
destructor undefined (155 targets failed). So the /EHa lines go, and the fix moves to the two
functions: their bodies are noinline functions called through a volatile pointer inside the
__try. clang-cl then sees a call it cannot prove nothrow, and covers it; a fault in the inlined
memcpy or loads of the old bodies was not. Checked with a small program under Wine 11.21 built
with /EHsc: a fault from a MEM_RESERVE page is caught through both the memcpy and the MOVNTDQA
path. Every target of runtime/ builds.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:02:16 +00:00
github-bot closed this pull request 2026-11-26 10:02:16 +00:00
Author
Owner

@⁠ImLele69 on GitHub · 2026-11-25 10:07 UTC · original

Updated: the /EHa approach in the first push did not reach copy_guarded (it is compiled in gta6rt_d3d12_com, not gta6rt), and /EHa on that library does not link with clang 20. The branch now leaves the build flags alone and calls the guarded bodies through a volatile function pointer instead; details and tests in the description.

**@⁠ImLele69** on GitHub · 2026-11-25 10:07 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1717#issuecomment-7159246868) Updated: the /EHa approach in the first push did not reach `copy_guarded` (it is compiled in `gta6rt_d3d12_com`, not `gta6rt`), and /EHa on that library does not link with clang 20. The branch now leaves the build flags alone and calls the guarded bodies through a volatile function pointer instead; details and tests in the description.
Author
Owner

@⁠ImLele69 on GitHub · 2026-11-25 12:01 UTC · original

Closing: #1723 (merged as ba626820e) fixes the same thing the same way. copy_guarded now copies through src/guarded_copy.h and hash_guarded reads in a noinline frame of its own, both noted as needed for clang-cl without /EHa. Nothing left here that main doesn't have.

**@⁠ImLele69** on GitHub · 2026-11-25 12:01 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1717#issuecomment-7161816799) Closing: #1723 (merged as `ba626820e`) fixes the same thing the same way. `copy_guarded` now copies through `src/guarded_copy.h` and `hash_guarded` reads in a `noinline` frame of its own, both noted as needed for clang-cl without /EHa. Nothing left here that `main` doesn't have.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.