Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const void*>(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<const void*>(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;
}

Expand Down Expand Up @@ -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<ID3D12Resource*>(mapped);
auto* const sentinel_resource = reinterpret_cast<ID3D12Resource*>(&list);
snapshot_chunks.push_back(UploadRingChunk{chunk_resource, mapped, sizeof(mapped)});
const unsigned char source = 42;
const auto source_address = reinterpret_cast<u64>(&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<u64>(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
Expand Down
8 changes: 8 additions & 0 deletions tests/cmake/startup_ring_copy.cmake
Original file line number Diff line number Diff line change
@@ -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()
22 changes: 22 additions & 0 deletions tests/offscreen_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned char*>(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();
Expand Down
Loading