From 25dc495568bddbbc3b71d0b1ddbadabcd24eabba Mon Sep 17 00:00:00 2001 From: Bryan Bernhart Date: Fri, 15 Apr 2022 11:49:33 -0700 Subject: [PATCH] Fix resource allocator fuzzer from incorrectly initializing. --- src/fuzzers/BUILD.gn | 5 ++ src/fuzzers/D3D12ResourceAllocatorFuzzer.cpp | 52 +++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/fuzzers/BUILD.gn b/src/fuzzers/BUILD.gn index cd0f2d167..0022910e1 100644 --- a/src/fuzzers/BUILD.gn +++ b/src/fuzzers/BUILD.gn @@ -28,6 +28,11 @@ if (is_win) { sources = [ "D3D12ResourceAllocatorFuzzer.cpp" ] deps = [ "${gpgmm_root_dir}:gpgmm" ] + + libs = [ + "d3d12.lib", + "dxgi.lib", + ] } } diff --git a/src/fuzzers/D3D12ResourceAllocatorFuzzer.cpp b/src/fuzzers/D3D12ResourceAllocatorFuzzer.cpp index 3d80a94c8..80104cf02 100644 --- a/src/fuzzers/D3D12ResourceAllocatorFuzzer.cpp +++ b/src/fuzzers/D3D12ResourceAllocatorFuzzer.cpp @@ -25,8 +25,56 @@ namespace { } // namespace extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv) { - gpgmm::d3d12::ALLOCATOR_DESC desc = {}; - gpgmm::d3d12::ResourceAllocator::CreateAllocator(desc, &gResourceAllocator); + gpgmm::d3d12::ALLOCATOR_DESC allocatorDesc = {}; + + // Populate the device + ComPtr d3dDevice; + if (FAILED(D3D12CreateDevice(nullptr, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&d3dDevice)))) { + return 0; + } + + allocatorDesc.Device = d3dDevice; + + // Populate the adapter + LUID adapterLUID = d3dDevice->GetAdapterLuid(); + ComPtr dxgiFactory; + if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&dxgiFactory)))) { + return 0; + } + + ComPtr dxgiFactory4; + if (FAILED(dxgiFactory.As(&dxgiFactory4))) { + return 0; + } + + ComPtr dxgiAdapter; + if (FAILED(dxgiFactory4->EnumAdapterByLuid(adapterLUID, IID_PPV_ARGS(&dxgiAdapter)))) { + return 0; + } + + allocatorDesc.Adapter = dxgiAdapter; + + // Populate the options. + D3D12_FEATURE_DATA_ARCHITECTURE arch = {}; + if (FAILED(d3dDevice->CheckFeatureSupport(D3D12_FEATURE_ARCHITECTURE, &arch, sizeof(arch)))) { + return 0; + } + + allocatorDesc.IsUMA = arch.UMA; + + D3D12_FEATURE_DATA_D3D12_OPTIONS options = {}; + if (FAILED(d3dDevice->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS, &options, + sizeof(options)))) { + return 0; + } + + allocatorDesc.ResourceHeapTier = options.ResourceHeapTier; + + if (FAILED( + gpgmm::d3d12::ResourceAllocator::CreateAllocator(allocatorDesc, &gResourceAllocator))) { + return 0; + } + return 0; }