diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 2a92ec33b0..5c7b458200 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; } @@ -8144,6 +8153,42 @@ void note_texture_write(void* base, UINT bc7_width, UINT bc7_height) } } // namespace +#ifdef X2W_RING_COPY_TEST +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 { + WrapperOwn own{}; + Wrapper wrapper{}; + } list; + static_assert(offsetof(TestList, wrapper) == sizeof(WrapperOwn)); + 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; + 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 && refused_prefix && next && offset == 1536 && mapped[offset] == source; +} +#endif + // Test seam (#3366): the fixture checks the presenter's refusal line, and a refusal needs no // device. queue_present only reaches present_to_window once a drawable plane has resolved, so the // refusal cannot be driven through the COM queue. Not exported; only tests/startup_present_log_check 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() diff --git a/tests/offscreen_check.cpp b/tests/offscreen_check.cpp index 3d53da367c..08e60477f5 100644 --- a/tests/offscreen_check.cpp +++ b/tests/offscreen_check.cpp @@ -2424,8 +2424,30 @@ int run_self_test() return 0; } +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") { + 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; + } + 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; + } 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();