Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nvapi: Guard against null device in Reflex calls #170

Merged
merged 2 commits into from
Mar 22, 2024
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
3 changes: 3 additions & 0 deletions src/d3d/nvapi_d3d_low_latency_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ namespace dxvk {
}

Com<ID3DLowLatencyDevice> NvapiD3dLowLatencyDevice::GetLowLatencyDevice(IUnknown* device) {
if (device == nullptr)
return nullptr;

std::scoped_lock lock(m_LowLatencyDeviceMutex);
auto it = m_lowLatencyDeviceMap.find(device);
if (it != m_lowLatencyDeviceMap.end())
Expand Down
15 changes: 15 additions & 0 deletions src/nvapi_d3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ extern "C" {
if (nvapiAdapterRegistry == nullptr)
return ApiNotInitialized(n);

if (pDevice == nullptr)
return InvalidArgument(n);

if (!nvapiD3dInstance->IsReflexAvailable(pDevice))
return NoImplementation(n, alreadyLoggedNoReflex);

Expand All @@ -135,6 +138,9 @@ extern "C" {
if (pSetSleepModeParams->version != NV_SET_SLEEP_MODE_PARAMS_VER1)
return IncompatibleStructVersion(n);

if (pDevice == nullptr)
return InvalidArgument(n);

if (!nvapiD3dInstance->IsReflexAvailable(pDevice))
return NoImplementation(n, alreadyLoggedNoReflex);

Expand All @@ -155,6 +161,9 @@ extern "C" {
if (pGetSleepStatusParams->version != NV_GET_SLEEP_STATUS_PARAMS_VER1)
return IncompatibleStructVersion(n);

if (pDevice == nullptr)
return InvalidArgument(n);

if (!nvapiD3dInstance->IsReflexAvailable(pDevice))
return NoImplementation(n, alreadyLoggedNoReflex);

Expand All @@ -175,6 +184,9 @@ extern "C" {
if (pGetLatencyParams->version != NV_LATENCY_RESULT_PARAMS_VER1)
return IncompatibleStructVersion(n);

if (pDev == nullptr)
return InvalidArgument(n);

if (nvapiD3dInstance->IsUsingLfx() || !NvapiD3dLowLatencyDevice::SupportsLowLatency(pDev))
return NoImplementation(n, alreadyLoggedNoImpl);

Expand All @@ -196,6 +208,9 @@ extern "C" {
if (pSetLatencyMarkerParams->version != NV_LATENCY_MARKER_PARAMS_VER1)
return IncompatibleStructVersion(n);

if (pDev == nullptr)
return InvalidArgument(n);

if (nvapiD3dInstance->IsUsingLfx() || !NvapiD3dLowLatencyDevice::SupportsLowLatency(pDev))
return NoImplementation(n, alreadyLoggedNoImpl);

Expand Down
4 changes: 2 additions & 2 deletions src/nvapi_d3d12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ extern "C" {
return ApiNotInitialized(n);

if (pCommandQueue == nullptr)
return InvalidArgument(n);
return InvalidPointer(n);

ID3D12Device* pDevice;
if (FAILED(pCommandQueue->GetDevice(IID_PPV_ARGS(&pDevice))))
Expand All @@ -435,7 +435,7 @@ extern "C" {
return IncompatibleStructVersion(n);

if (pCommandQueue == nullptr)
return InvalidArgument(n);
return InvalidPointer(n);

ID3D12Device* pDevice;
if (FAILED(pCommandQueue->GetDevice(IID_PPV_ARGS(&pDevice))))
Expand Down
33 changes: 33 additions & 0 deletions tests/nvapi_d3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,39 @@ TEST_CASE("D3D Reflex/LatencyFleX depending methods succeed", "[.d3d]") {
ALLOW_CALL(*lfx, IsAvailable())
.RETURN(false);

SECTION("Reflex methods fail when given null device") {
SetupResourceFactory(std::move(dxgiFactory), std::move(vulkan), std::move(nvml), std::move(lfx));
REQUIRE(NvAPI_Initialize() == NVAPI_OK);

SECTION("GetSleepStatus returns invalid-argument") {
NV_GET_SLEEP_STATUS_PARAMS_V1 params{};
params.version = NV_GET_SLEEP_STATUS_PARAMS_VER1;
REQUIRE(NvAPI_D3D_GetSleepStatus(nullptr, &params) == NVAPI_INVALID_ARGUMENT);
}

SECTION("SetSleepMode returns invalid-argument") {
NV_SET_SLEEP_MODE_PARAMS params{};
params.version = NV_SET_SLEEP_MODE_PARAMS_VER;
REQUIRE(NvAPI_D3D_SetSleepMode(nullptr, &params) == NVAPI_INVALID_ARGUMENT);
}

SECTION("Sleep returns invalid-argument") {
REQUIRE(NvAPI_D3D_Sleep(nullptr) == NVAPI_INVALID_ARGUMENT);
}

SECTION("GetLatency returns invalid-argument") {
NV_LATENCY_RESULT_PARAMS params;
params.version = NV_LATENCY_RESULT_PARAMS_VER;
REQUIRE(NvAPI_D3D_GetLatency(nullptr, &params) == NVAPI_INVALID_ARGUMENT);
}

SECTION("SetLatencyMarker returns invalid-argument") {
NV_LATENCY_MARKER_PARAMS params;
params.version = NV_LATENCY_MARKER_PARAMS_VER;
REQUIRE(NvAPI_D3D_SetLatencyMarker(nullptr, &params) == NVAPI_INVALID_ARGUMENT);
}
}

SECTION("LatencyFleX depending methods succeed when LFX is available") {
ALLOW_CALL(*lfx, IsAvailable())
.RETURN(true);
Expand Down
17 changes: 17 additions & 0 deletions tests/nvapi_d3d12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,13 @@ TEST_CASE("D3D12 methods succeed", "[.d3d12]") {
REQUIRE(NvAPI_Initialize() == NVAPI_OK);
REQUIRE(NvAPI_D3D12_NotifyOutOfBandCommandQueue(&commandQueue, OUT_OF_BAND_RENDER) == NVAPI_NO_IMPLEMENTATION);
}

SECTION("NotifyOutOfBandCommandQueue with null command queue returns invalid-pointer") {
SetupResourceFactory(std::move(dxgiFactory), std::move(vulkan), std::move(nvml), std::move(lfx));

REQUIRE(NvAPI_Initialize() == NVAPI_OK);
REQUIRE(NvAPI_D3D12_NotifyOutOfBandCommandQueue(nullptr, OUT_OF_BAND_RENDER) == NVAPI_INVALID_POINTER);
}
}

SECTION("SetAsyncFrameMarker succeeds") {
Expand Down Expand Up @@ -874,6 +881,16 @@ TEST_CASE("D3D12 methods succeed", "[.d3d12]") {
params.version = NV_LATENCY_MARKER_PARAMS_VER;
REQUIRE(NvAPI_D3D12_SetAsyncFrameMarker(&commandQueue, &params) != NVAPI_INCOMPATIBLE_STRUCT_VERSION);
}

SECTION("SetAsyncFrameMarker with null command queue returns invalid-pointer") {
SetupResourceFactory(std::move(dxgiFactory), std::move(vulkan), std::move(nvml), std::move(lfx));

REQUIRE(NvAPI_Initialize() == NVAPI_OK);

NV_LATENCY_MARKER_PARAMS params{};
params.version = NV_LATENCY_MARKER_PARAMS_VER;
REQUIRE(NvAPI_D3D12_SetAsyncFrameMarker(nullptr, &params) == NVAPI_INVALID_POINTER);
}
}
}
}