Skip to content

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

Closed
ImLele69 wants to merge 3 commits into
ProjectLeonida:mainfrom
ImLele69:rt-clang-eha
Closed

ImLele69 wants to merge 3 commits into
ProjectLeonida:mainfrom
ImLele69:rt-clang-eha

Conversation

@ImLele69

@ImLele69 ImLele69 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

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

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

…lts inside memcpy

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.
… volatile pointer instead of relying on /EHa

/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.
@ImLele69 ImLele69 changed the title runtime: build gta6rt with /EHa under clang-cl so __try catches faults inside memcpy (RT-CLANG-EHA) d3d12_vulkan: copy_guarded and hash_guarded catch page faults under clang-cl (RT-CLANG-EHA) Nov 25, 2026
@ImLele69

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown
Contributor Author

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 ImLele69 closed this Nov 25, 2026
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.

1 participant