From cf8b9d8b3b831c65a99c3706fbeef370834b948e Mon Sep 17 00:00:00 2001 From: Leyukaka <45659323+Leyukaka@users.noreply.github.com> Date: Sun, 29 Nov 2026 16:59:53 +0100 Subject: [PATCH 1/3] startup: copy guest memory with guarded_memcpy in ring_copy --- CMakeLists.txt | 3 +++ src/startup_stubs/d3d12_xs_device.cpp | 38 ++++++++++++++++++++++++--- tests/offscreen_check.cpp | 13 +++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ba597fee39..6d1f410ba6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2182,10 +2182,13 @@ if(NOT OFFSCREEN_AGILITY_620_DIR) add_library(d3d12_xs_device_obj OBJECT src/startup_stubs/d3d12_xs_device.cpp) target_include_directories(d3d12_xs_device_obj PRIVATE src) target_compile_features(d3d12_xs_device_obj PRIVATE cxx_std_17) + target_compile_definitions(d3d12_xs_device_obj PRIVATE X2W_RING_COPY_TEST) target_sources(offscreen_check PRIVATE $) else() target_sources(offscreen_check PRIVATE src/startup_stubs/d3d12_xs_device.cpp) + target_compile_definitions(offscreen_check PRIVATE X2W_RING_COPY_TEST) endif() +add_test(NAME startup_ring_copy_fault COMMAND offscreen_check --ring-copy-fault) target_link_libraries(offscreen_check PRIVATE d3d12 dxgi dxguid d3dcompiler user32 gdi32 x2w_build_rev) target_include_directories(offscreen_check PRIVATE "${CMAKE_CURRENT_BINARY_DIR}") if(OFFSCREEN_AGILITY_620_DIR) diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 53921d6d51..6f64d90d4c 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -2884,9 +2884,18 @@ unsigned char* reserve_upload_ring(Wrapper* list, std::size_t bytes, u64* offset bool ring_copy(Wrapper* list, u64 title_address, std::size_t bytes, u64* offset, ID3D12Resource** resource = nullptr, bool snapshot = false) { - auto* dest = reserve_upload_ring(list, bytes, offset, resource, snapshot); - if (!dest) return false; - std::memcpy(dest, reinterpret_cast(title_address), bytes); + if (offset) *offset = 0; + if (resource) *resource = nullptr; + const UploadReservation reservation = reserve_upload_ring(list, bytes, snapshot); + if (!reservation.mapped) return false; + // Keep the bump allocation until normal chunk reuse. A fault must not publish partially copied bytes. + if (!x2w_local::guarded_memcpy(reservation.mapped, reinterpret_cast(title_address), bytes, false)) { + static LONG refused; + if (first_calls(refused, 8)) log("x2w-draw upload ring copy refused: title memory fault"); + return false; + } + if (offset) *offset = reservation.offset; + if (resource) *resource = reservation.resource; return true; } @@ -8136,6 +8145,29 @@ void note_texture_write(void* base, UINT bc7_width, UINT bc7_height) } } // namespace +#ifdef X2W_RING_COPY_TEST +bool x2w_test_ring_copy(const void* reserved_page) +{ + // This runs in a dedicated fixture process before any device or list has used the snapshot pool. + struct TestList { + WrapperOwn own{}; + Wrapper wrapper{}; + } list; + static_assert(offsetof(TestList, wrapper) == sizeof(WrapperOwn)); + unsigned char mapped[1536]{}; + snapshot_chunks.push_back(UploadRingChunk{nullptr, mapped, sizeof(mapped)}); + const unsigned char source = 42; + const auto source_address = reinterpret_cast(&source); + u64 offset = 0; + const bool first = ring_copy(&list.wrapper, source_address, sizeof(source), &offset, nullptr, true); + const bool refused = !ring_copy(&list.wrapper, reinterpret_cast(reserved_page), sizeof(source), + &offset, nullptr, true) && offset == 0; + const bool next = ring_copy(&list.wrapper, source_address, sizeof(source), &offset, nullptr, true); + snapshot_chunks.clear(); + return first && mapped[0] == source && refused && next && offset == 1024 && mapped[offset] == source; +} +#endif + void x2w_startup::attach_device(ID3D12Device* device, void** wrapped) { static const bool ready = (build_tables(), true); diff --git a/tests/offscreen_check.cpp b/tests/offscreen_check.cpp index 3d53da367c..e4be012db8 100644 --- a/tests/offscreen_check.cpp +++ b/tests/offscreen_check.cpp @@ -2424,8 +2424,21 @@ int run_self_test() return 0; } +bool x2w_test_ring_copy(const void* reserved_page); + int main(int argc, char** argv) { + if (argc == 2 && std::string_view(argv[1]) == "--ring-copy-fault") { + void* const page = VirtualAlloc(nullptr, 4096, MEM_RESERVE, PAGE_NOACCESS); + if (!page) { + std::fprintf(stderr, "ring-copy fixture could not reserve a page\n"); + return 1; + } + const bool passed = x2w_test_ring_copy(page); + VirtualFree(page, 0, MEM_RELEASE); + if (!passed) std::fprintf(stderr, "ring-copy fixture failed the refusal or adjacent copy checks\n"); + return passed ? 0 : 1; + } const CliResolution cli = resolve_cli(argc, argv); if (cli.action == CliAction::usage_error) return 2; if (cli.action == CliAction::self_test) return run_self_test(); From 7259bea9c06d8c2c0565b806d19742ecc927b994 Mon Sep 17 00:00:00 2001 From: Leyukaka <45659323+Leyukaka@users.noreply.github.com> Date: Sun, 29 Nov 2026 17:02:36 +0100 Subject: [PATCH 2/3] tests: register the ring_copy fixture in a test fragment --- CMakeLists.txt | 3 --- tests/cmake/startup_ring_copy.cmake | 8 ++++++++ 2 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 tests/cmake/startup_ring_copy.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d1f410ba6..ba597fee39 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2182,13 +2182,10 @@ if(NOT OFFSCREEN_AGILITY_620_DIR) add_library(d3d12_xs_device_obj OBJECT src/startup_stubs/d3d12_xs_device.cpp) target_include_directories(d3d12_xs_device_obj PRIVATE src) target_compile_features(d3d12_xs_device_obj PRIVATE cxx_std_17) - target_compile_definitions(d3d12_xs_device_obj PRIVATE X2W_RING_COPY_TEST) target_sources(offscreen_check PRIVATE $) else() target_sources(offscreen_check PRIVATE src/startup_stubs/d3d12_xs_device.cpp) - target_compile_definitions(offscreen_check PRIVATE X2W_RING_COPY_TEST) endif() -add_test(NAME startup_ring_copy_fault COMMAND offscreen_check --ring-copy-fault) target_link_libraries(offscreen_check PRIVATE d3d12 dxgi dxguid d3dcompiler user32 gdi32 x2w_build_rev) target_include_directories(offscreen_check PRIVATE "${CMAKE_CURRENT_BINARY_DIR}") if(OFFSCREEN_AGILITY_620_DIR) diff --git a/tests/cmake/startup_ring_copy.cmake b/tests/cmake/startup_ring_copy.cmake new file mode 100644 index 0000000000..854d9cedd8 --- /dev/null +++ b/tests/cmake/startup_ring_copy.cmake @@ -0,0 +1,8 @@ +if(TARGET offscreen_check) + if(TARGET d3d12_xs_device_obj) + target_compile_definitions(d3d12_xs_device_obj PRIVATE X2W_RING_COPY_TEST) + else() + target_compile_definitions(offscreen_check PRIVATE X2W_RING_COPY_TEST) + endif() + add_test(NAME startup_ring_copy_fault COMMAND offscreen_check --ring-copy-fault) +endif() From c8e24e1d1fd41b50a2bba6a84d6034891549169c Mon Sep 17 00:00:00 2001 From: Leyukaka <45659323+Leyukaka@users.noreply.github.com> Date: Sun, 29 Nov 2026 17:38:01 +0100 Subject: [PATCH 3/3] tests: check the ring_copy outputs and a fault after a readable prefix --- src/startup_stubs/d3d12_xs_device.cpp | 29 +++++++++++++++++++-------- tests/offscreen_check.cpp | 21 +++++++++++++------ 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 6f64d90d4c..e6124d3457 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -8146,7 +8146,7 @@ void note_texture_write(void* base, UINT bc7_width, UINT bc7_height) } // namespace #ifdef X2W_RING_COPY_TEST -bool x2w_test_ring_copy(const void* reserved_page) +bool x2w_test_ring_copy(const unsigned char* reserved_page, std::size_t readable_prefix_bytes) { // This runs in a dedicated fixture process before any device or list has used the snapshot pool. struct TestList { @@ -8154,17 +8154,30 @@ bool x2w_test_ring_copy(const void* reserved_page) Wrapper wrapper{}; } list; static_assert(offsetof(TestList, wrapper) == sizeof(WrapperOwn)); - unsigned char mapped[1536]{}; - snapshot_chunks.push_back(UploadRingChunk{nullptr, mapped, sizeof(mapped)}); + unsigned char mapped[2048]{}; + // The fixture only passes the resource pointer through the reservation. + auto* const chunk_resource = reinterpret_cast(mapped); + auto* const sentinel_resource = reinterpret_cast(&list); + snapshot_chunks.push_back(UploadRingChunk{chunk_resource, mapped, sizeof(mapped)}); const unsigned char source = 42; const auto source_address = reinterpret_cast(&source); u64 offset = 0; - const bool first = ring_copy(&list.wrapper, source_address, sizeof(source), &offset, nullptr, true); - const bool refused = !ring_copy(&list.wrapper, reinterpret_cast(reserved_page), sizeof(source), - &offset, nullptr, true) && offset == 0; - const bool next = ring_copy(&list.wrapper, source_address, sizeof(source), &offset, nullptr, true); + ID3D12Resource* resource = sentinel_resource; + const bool first = ring_copy(&list.wrapper, source_address, sizeof(source), &offset, &resource, true) + && offset == 0 && resource == chunk_resource; + const auto refuses_copy = [&](const unsigned char* start, std::size_t bytes) { + offset = UINT64_MAX; + resource = sentinel_resource; + return !ring_copy(&list.wrapper, reinterpret_cast(start), bytes, &offset, &resource, true) + && offset == 0 && resource == nullptr; + }; + const bool refused = refuses_copy(reserved_page, sizeof(source)); + const bool refused_prefix = refuses_copy(reserved_page - readable_prefix_bytes, readable_prefix_bytes + 1); + resource = sentinel_resource; + const bool next = ring_copy(&list.wrapper, source_address, sizeof(source), &offset, &resource, true) + && resource == chunk_resource; snapshot_chunks.clear(); - return first && mapped[0] == source && refused && next && offset == 1024 && mapped[offset] == source; + return first && mapped[0] == source && refused && refused_prefix && next && offset == 1536 && mapped[offset] == source; } #endif diff --git a/tests/offscreen_check.cpp b/tests/offscreen_check.cpp index e4be012db8..08e60477f5 100644 --- a/tests/offscreen_check.cpp +++ b/tests/offscreen_check.cpp @@ -2424,18 +2424,27 @@ int run_self_test() return 0; } -bool x2w_test_ring_copy(const void* reserved_page); +bool x2w_test_ring_copy(const unsigned char* reserved_page, std::size_t readable_prefix_bytes); int main(int argc, char** argv) { if (argc == 2 && std::string_view(argv[1]) == "--ring-copy-fault") { - void* const page = VirtualAlloc(nullptr, 4096, MEM_RESERVE, PAGE_NOACCESS); - if (!page) { - std::fprintf(stderr, "ring-copy fixture could not reserve a page\n"); + SYSTEM_INFO system_info{}; + GetSystemInfo(&system_info); + const std::size_t page_size = system_info.dwPageSize; + auto* const pages = static_cast(VirtualAlloc(nullptr, 2 * page_size, MEM_RESERVE, PAGE_NOACCESS)); + if (!pages) { + std::fprintf(stderr, "ring-copy fixture could not reserve two pages\n"); return 1; } - const bool passed = x2w_test_ring_copy(page); - VirtualFree(page, 0, MEM_RELEASE); + if (!VirtualAlloc(pages, page_size, MEM_COMMIT, PAGE_READWRITE)) { + std::fprintf(stderr, "ring-copy fixture could not commit the first page\n"); + VirtualFree(pages, 0, MEM_RELEASE); + return 1; + } + constexpr std::size_t readable_prefix_bytes = 16; + const bool passed = x2w_test_ring_copy(pages + page_size, readable_prefix_bytes); + VirtualFree(pages, 0, MEM_RELEASE); if (!passed) std::fprintf(stderr, "ring-copy fixture failed the refusal or adjacent copy checks\n"); return passed ? 0 : 1; }