upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/device_proxy_test.cpp
diff options
context:
space:
mode:
authorJackson <37029272+NinJackson@users.noreply.github.com>2026-11-23 11:12:22 -0500
committerJackson <37029272+NinJackson@users.noreply.github.com>2026-11-23 11:12:22 -0500
commitaf1ad19d7542d177f8805d454bd7bf78ef82b520 (patch)
treeb2a9b6eb64b3ec48f7b73bc16dbc1d54aa3f919e /runtime/test/device_proxy_test.cpp
parent8575d36edc479218972497a7bded8651a42856c0 (diff)
parent355201c51e6aba95c582e2df344782b22e97bb31 (diff)
Merge branch 'pr-7021' into rt5-stop17-title-hook-leases
# Conflicts: # runtime/shims/d3d12_xs_device.cpp # runtime/shims/d3d12_xs_device.h # runtime/test/xs_proxy_mock_test.cpp
Diffstat (limited to 'runtime/test/device_proxy_test.cpp')
-rw-r--r--runtime/test/device_proxy_test.cpp65
1 files changed, 65 insertions, 0 deletions
diff --git a/runtime/test/device_proxy_test.cpp b/runtime/test/device_proxy_test.cpp
index 46a853e7c..36afc3c34 100644
--- a/runtime/test/device_proxy_test.cpp
+++ b/runtime/test/device_proxy_test.cpp
@@ -497,6 +497,71 @@ int main(int argc, char** argv) {
497 } 497 }
498 } 498 }
499 499
500 // Title-exported __XMemAllocationHooks__ backing for CreateDescriptorHeap (slot 14) + CPU handle translation
501 // against the real WARP D3D12 runtime.
502 {
503 struct HookState {
504 alignas(64) std::uint8_t backing[4096]{};
505 std::size_t alloc_size = 0;
506 std::uint64_t alloc_attr = 0;
507 void* freed_ptr = nullptr;
508 std::uint64_t freed_attr = 0;
509 int alloc_count = 0;
510 int free_count = 0;
511 };
512 static HookState hook_state;
513 hook_state = {};
514 struct HookCallbacks {
515 static void* __stdcall alloc(std::size_t size, std::uint64_t attr) {
516 ++hook_state.alloc_count;
517 hook_state.alloc_size = size;
518 hook_state.alloc_attr = attr;
519 if ((attr & 0x3FFFull) == 4 && size <= sizeof(hook_state.backing)) return hook_state.backing;
520 return nullptr;
521 }
522 static void __stdcall free(void* ptr, std::uint64_t attr) {
523 ++hook_state.free_count;
524 hook_state.freed_ptr = ptr;
525 hook_state.freed_attr = attr;
526 }
527 };
528 const rt::shims::XMemAllocationHooks title_hooks{&HookCallbacks::alloc, &HookCallbacks::free};
529 rt::shims::add_xmem_allocation_hooks(&title_hooks, &title_hooks);
530
531 D3D12_DESCRIPTOR_HEAP_DESC hooked_desc{};
532 hooked_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
533 hooked_desc.NumDescriptors = 4;
534 hooked_desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
535 const UINT inc = device->GetDescriptorHandleIncrementSize(hooked_desc.Type);
536 ComPtr<ID3D12DescriptorHeap> hooked_heap;
537 hr = device->CreateDescriptorHeap(&hooked_desc, IID_PPV_ARGS(hooked_heap.GetAddressOf()));
538 check(SUCCEEDED(hr) && hooked_heap != nullptr, "hooked slot 14 creates a descriptor heap");
539 check(hook_state.alloc_count == 1 && hook_state.alloc_size == hooked_desc.NumDescriptors * inc &&
540 hook_state.alloc_attr == rt::d3d12_xs::kXmemDescriptorHeapAttributes,
541 "slot 14 invokes __XMemAllocationHooks__ with heap byte size and D3D descriptor-heap attributes");
542 if (hooked_heap) {
543 void** hooked_vtbl = *reinterpret_cast<void***>(hooked_heap.Get());
544 const auto hooked_cpu = reinterpret_cast<GetCpuHandleFn>(hooked_vtbl[9])(hooked_heap.Get());
545 const auto hooked_gpu = reinterpret_cast<GetGpuHandleFn>(hooked_vtbl[10])(hooked_heap.Get());
546 const auto expected_base = reinterpret_cast<SIZE_T>(hook_state.backing);
547 check(hooked_cpu.ptr == expected_base,
548 "hooked heap slot 9 returns the title-supplied CPU descriptor base");
549 check(hooked_gpu.ptr != 0, "hooked heap slot 10 returns a valid GPU descriptor start");
550
551 // Create a CBV at slot 1 of the title-backed heap; WARP validates the translated real handle.
552 D3D12_CONSTANT_BUFFER_VIEW_DESC cbv{};
553 cbv.BufferLocation = 0;
554 cbv.SizeInBytes = 256;
555 device->CreateConstantBufferView(&cbv, D3D12_CPU_DESCRIPTOR_HANDLE{expected_base + inc});
556 check(true, "CreateConstantBufferView translates a title-backed CPU descriptor handle on WARP");
557 }
558 hooked_heap.Reset();
559 check(hook_state.free_count == 1 && hook_state.freed_ptr == hook_state.backing &&
560 hook_state.freed_attr == rt::d3d12_xs::kXmemDescriptorHeapAttributes,
561 "releasing the hooked heap frees its backing through __XMemAllocationHooks__");
562 rt::shims::remove_xmem_allocation_hooks(&title_hooks);
563 }
564
500 // Hold a reference to the PC device so the final check can prove the proxy's Release balances its own. 565 // Hold a reference to the PC device so the final check can prove the proxy's Release balances its own.
501 if (real) real->AddRef(); 566 if (real) real->AddRef();
502 // Drop every device child before the proxy so the PC device is not released first. 567 // Drop every device child before the proxy so the PC device is not released first.