From 291a354b2ffd67f47f011ddae91fa4ae81bd219f Mon Sep 17 00:00:00 2001 From: Gabriele Date: Wed, 25 Nov 2026 10:24:48 +0100 Subject: [PATCH 1/3] runtime: gta6rt builds with /EHa under clang-cl, so __try catches faults 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. --- runtime/CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 14cc5fbf2..7cd69f471 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -209,6 +209,11 @@ if(WIN32 AND LEONIDA_BUILD_WINDOWS_RUNTIME) add_dependencies(gta6rt gta6rt_tls_host bcpack_history_train) target_compile_definitions(gta6rt PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX UNICODE _UNICODE) target_compile_options(gta6rt PRIVATE /W4 /permissive- /utf-8) + # __try guards reads of the title's memory in several sources (copy_guarded and hash_guarded in the D3D12 layer, + # title_page_watch.h, the file system and the PE loader). clang-cl turns memcpy into an intrinsic it assumes cannot + # throw, so without asynchronous exceptions a fault inside one of those copies is not caught: the process dies on + # a page the title unmapped between the check and the read. MSVC always catches them. + target_compile_options(gta6rt PRIVATE $<$:/EHa>) # Static shim tables register themselves at startup; keep the linker from dropping their objects. # GTA6RT_HOST_BASE keeps the loader's own image off the default x64 EXE base (0x140000000), where a mapped EXE # without relocations wants to go. Windows moves the EXE with ASLR anyway; Wine loads it at its preferred base. From 44e7d30e032e58272995dd92a7ef06de9683958d Mon Sep 17 00:00:00 2001 From: Gabriele Date: Wed, 25 Nov 2026 10:43:46 +0100 Subject: [PATCH 2/3] runtime: gta6rt_d3d12_com builds with /EHa under clang-cl too 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. --- runtime/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 7cd69f471..47c1b892b 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -2184,6 +2184,8 @@ if(TARGET Vulkan::Headers) # /bigobj: a Debug build of d3d12_vulkan_device.cpp has more sections than a COFF object holds by default # (fatal error C1128), because without optimisation every inline function keeps its own COMDAT. target_compile_options(gta6rt_d3d12_com PRIVATE /W4 /permissive- /utf-8 /bigobj) + # copy_guarded and hash_guarded live here, not in gta6rt: the same /EHa as gta6rt's (see there). + target_compile_options(gta6rt_d3d12_com PRIVATE $<$:/EHa>) add_executable(d3d12_com_test test/d3d12_com_test.cpp) target_link_libraries(d3d12_com_test PRIVATE gta6rt_d3d12_com) From 36612c984f0a099030166a88f1e8c67021702ddf Mon Sep 17 00:00:00 2001 From: Gabriele Date: Wed, 25 Nov 2026 10:55:50 +0100 Subject: [PATCH 3/3] d3d12_vulkan: copy_guarded and hash_guarded call their body through a 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. --- runtime/CMakeLists.txt | 7 --- runtime/shims/d3d12_vulkan_device.cpp | 70 ++++++++++++++++----------- 2 files changed, 43 insertions(+), 34 deletions(-) diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 47c1b892b..14cc5fbf2 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -209,11 +209,6 @@ if(WIN32 AND LEONIDA_BUILD_WINDOWS_RUNTIME) add_dependencies(gta6rt gta6rt_tls_host bcpack_history_train) target_compile_definitions(gta6rt PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX UNICODE _UNICODE) target_compile_options(gta6rt PRIVATE /W4 /permissive- /utf-8) - # __try guards reads of the title's memory in several sources (copy_guarded and hash_guarded in the D3D12 layer, - # title_page_watch.h, the file system and the PE loader). clang-cl turns memcpy into an intrinsic it assumes cannot - # throw, so without asynchronous exceptions a fault inside one of those copies is not caught: the process dies on - # a page the title unmapped between the check and the read. MSVC always catches them. - target_compile_options(gta6rt PRIVATE $<$:/EHa>) # Static shim tables register themselves at startup; keep the linker from dropping their objects. # GTA6RT_HOST_BASE keeps the loader's own image off the default x64 EXE base (0x140000000), where a mapped EXE # without relocations wants to go. Windows moves the EXE with ASLR anyway; Wine loads it at its preferred base. @@ -2184,8 +2179,6 @@ if(TARGET Vulkan::Headers) # /bigobj: a Debug build of d3d12_vulkan_device.cpp has more sections than a COFF object holds by default # (fatal error C1128), because without optimisation every inline function keeps its own COMDAT. target_compile_options(gta6rt_d3d12_com PRIVATE /W4 /permissive- /utf-8 /bigobj) - # copy_guarded and hash_guarded live here, not in gta6rt: the same /EHa as gta6rt's (see there). - target_compile_options(gta6rt_d3d12_com PRIVATE $<$:/EHa>) add_executable(d3d12_com_test test/d3d12_com_test.cpp) target_link_libraries(d3d12_com_test PRIVATE gta6rt_d3d12_com) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 5dd6b42e4..92a5c105a 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -164,28 +164,40 @@ std::uint64_t vsharp_address(std::uint64_t va, std::uint64_t bytes, } // memcpy that a page fault ends instead of killing the process: the title may decommit a page between the -// VirtualQuery that saw it committed and the read. No C++ objects here: it is a __try frame. +// VirtualQuery that saw it committed and the read. // `streaming` reads with MOVNTDQA, for write-combined pages: ordinary loads from them are not cached, and memcpy // read 64 MiB of them in 253 ms on a Core i9-13900K (253 MiB/s), MOVNTDQA in 13 to 17 ms (4 to 5 GiB/s). Paid at every submission. #if defined(__clang__) && defined(_M_X64) __attribute__((target("sse4.1"))) #endif +__declspec(noinline) void copy_unguarded(std::uint8_t* to, const std::uint8_t* from, std::size_t bytes, bool streaming) { + if (streaming) { + const std::size_t head = std::min(bytes, (0 - reinterpret_cast(from)) & 63); + std::memcpy(to, from, head); + to += head, from += head, bytes -= head; + for (; bytes >= 64; to += 64, from += 64, bytes -= 64) { + const auto* in = reinterpret_cast(from); + const __m128i a = _mm_stream_load_si128(in), b = _mm_stream_load_si128(in + 1); + const __m128i c = _mm_stream_load_si128(in + 2), d = _mm_stream_load_si128(in + 3); + auto* out = reinterpret_cast<__m128i*>(to); + _mm_storeu_si128(out, a), _mm_storeu_si128(out + 1, b); + _mm_storeu_si128(out + 2, c), _mm_storeu_si128(out + 3, d); + } + } + std::memcpy(to, from, bytes); +} + +// The guarded reads call their body through a volatile pointer. Built with clang-cl and /EHsc, a __try covers a fault +// only inside a call the compiler cannot see through: it lowers memcpy to an intrinsic it treats as unable to throw, +// and loads inlined into the __try block are not covered either, so the __except never ran and a page the title had +// just unmapped killed the process. MSVC catches both ways; /EHa would too, but clang 20 then leaves std::variant's +// trivial destructor undefined at link time. +void (*volatile copy_unguarded_call)(std::uint8_t*, const std::uint8_t*, std::size_t, bool) = ©_unguarded; + +// No C++ objects here: it is a __try frame. bool copy_guarded(std::uint8_t* to, const std::uint8_t* from, std::size_t bytes, bool streaming) { __try { - if (streaming) { - const std::size_t head = std::min(bytes, (0 - reinterpret_cast(from)) & 63); - std::memcpy(to, from, head); - to += head, from += head, bytes -= head; - for (; bytes >= 64; to += 64, from += 64, bytes -= 64) { - const auto* in = reinterpret_cast(from); - const __m128i a = _mm_stream_load_si128(in), b = _mm_stream_load_si128(in + 1); - const __m128i c = _mm_stream_load_si128(in + 2), d = _mm_stream_load_si128(in + 3); - auto* out = reinterpret_cast<__m128i*>(to); - _mm_storeu_si128(out, a), _mm_storeu_si128(out + 1, b); - _mm_storeu_si128(out + 2, c), _mm_storeu_si128(out + 3, d); - } - } - std::memcpy(to, from, bytes); + copy_unguarded_call(to, from, bytes, streaming); return true; } __except (EXCEPTION_EXECUTE_HANDLER) { return false; @@ -207,20 +219,24 @@ inline std::uint64_t mix_words(std::uint64_t h, const std::uint8_t* at, std::siz } return h; } +__declspec(noinline) std::uint64_t hash_unguarded(const std::uint8_t* p, std::size_t bytes, bool every_byte) { + std::uint64_t h = 0x9e3779b97f4a7c15ull ^ bytes; + if (bytes <= (256u << 10) || every_byte) { + h = mix_words(h, p, bytes & ~std::size_t(7)); + for (std::size_t i = bytes & ~std::size_t(7); i < bytes; ++i) h = (h ^ p[i]) * 0x100000001b3ull; + } else { + for (std::size_t page = 0; page + 4096 <= bytes; page += 4096) { + h = mix_words(h, p + page, 64); + h = mix_words(h, p + page + 2048, 64); + } + h = mix_words(h, p + bytes - 64, 64); + } + return h; +} +std::uint64_t (*volatile hash_unguarded_call)(const std::uint8_t*, std::size_t, bool) = &hash_unguarded; // see copy_guarded bool hash_guarded(const std::uint8_t* p, std::size_t bytes, std::uint64_t& out, bool every_byte = false) { __try { - std::uint64_t h = 0x9e3779b97f4a7c15ull ^ bytes; - if (bytes <= (256u << 10) || every_byte) { - h = mix_words(h, p, bytes & ~std::size_t(7)); - for (std::size_t i = bytes & ~std::size_t(7); i < bytes; ++i) h = (h ^ p[i]) * 0x100000001b3ull; - } else { - for (std::size_t page = 0; page + 4096 <= bytes; page += 4096) { - h = mix_words(h, p + page, 64); - h = mix_words(h, p + page + 2048, 64); - } - h = mix_words(h, p + bytes - 64, 64); - } - out = h; + out = hash_unguarded_call(p, bytes, every_byte); return true; } __except (EXCEPTION_EXECUTE_HANDLER) { return false;