Skip to content

Commit

Permalink
nvapi: Return appropriate error codes from Reflex entry points when p…
Browse files Browse the repository at this point in the history
…assed null pointers
  • Loading branch information
Saancreed authored and jp7677 committed Mar 22, 2024
1 parent 2c52a61 commit efcc764
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
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);
}
}
}
}

0 comments on commit efcc764

Please sign in to comment.