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

Closed
ImLele69 wants to merge 3 commits from upstream-pr/1717 into main
ImLele69 commented 2026-11-25 09:29:07 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by ImLele69 on 2026-11-25T09:29:07Z.


Claimed in the coordination issue as: RT-CLANG-EHA — https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7158385623

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).

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5488657856 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1717). Originally posted by [ImLele69](https://github.com/ImLele69) on 2026-11-25T09:29:07Z. --- Claimed in the coordination issue as: RT-CLANG-EHA — https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7158385623 ### 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 commented 2026-11-25 10:07:00 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by ImLele69 on 2026-11-25T10:07:00Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7159246868 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1717#issuecomment-7159246868). Originally posted by [ImLele69](https://github.com/ImLele69) on 2026-11-25T10:07:00Z. --- 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 commented 2026-11-25 12:01:27 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by ImLele69 on 2026-11-25T12:01:27Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7161816799 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1717#issuecomment-7161816799). Originally posted by [ImLele69](https://github.com/ImLele69) on 2026-11-25T12:01:27Z. --- 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

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!1717
No description provided.