From 019551ca4062016c26c364b5aee11c73b617bc9f Mon Sep 17 00:00:00 2001 From: Petr Vesely Date: Fri, 15 Sep 2023 09:24:35 +0100 Subject: [PATCH] [UR] Add validation check to urDeviceGet --- include/ur_api.h | 2 ++ scripts/core/device.yml | 2 ++ source/loader/layers/validation/ur_valddi.cpp | 4 ++++ source/loader/ur_libapi.cpp | 2 ++ source/ur_api.cpp | 2 ++ test/conformance/device/urDeviceGet.cpp | 12 +++++++++++- 6 files changed, 23 insertions(+), 1 deletion(-) diff --git a/include/ur_api.h b/include/ur_api.h index 6c445fc408..298531957a 100644 --- a/include/ur_api.h +++ b/include/ur_api.h @@ -1304,6 +1304,8 @@ typedef enum ur_device_type_t { /// + `::UR_DEVICE_TYPE_VPU < DeviceType` /// - ::UR_RESULT_ERROR_INVALID_SIZE /// + `NumEntries == 0 && phDevices != NULL` +/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER +/// + `NumEntries > 0 && phDevices == NULL` /// - ::UR_RESULT_ERROR_INVALID_VALUE UR_APIEXPORT ur_result_t UR_APICALL urDeviceGet( diff --git a/scripts/core/device.yml b/scripts/core/device.yml index 4d304dc12f..3999fa70f2 100644 --- a/scripts/core/device.yml +++ b/scripts/core/device.yml @@ -146,6 +146,8 @@ params: returns: - $X_RESULT_ERROR_INVALID_SIZE: - "`NumEntries == 0 && phDevices != NULL`" + - $X_RESULT_ERROR_INVALID_NULL_POINTER: + - "`NumEntries > 0 && phDevices == NULL`" - $X_RESULT_ERROR_INVALID_VALUE --- #-------------------------------------------------------------------------- type: enum diff --git a/source/loader/layers/validation/ur_valddi.cpp b/source/loader/layers/validation/ur_valddi.cpp index fe689c8537..4698c0cf1b 100644 --- a/source/loader/layers/validation/ur_valddi.cpp +++ b/source/loader/layers/validation/ur_valddi.cpp @@ -419,6 +419,10 @@ __urdlllocal ur_result_t UR_APICALL urDeviceGet( return UR_RESULT_ERROR_INVALID_NULL_HANDLE; } + if (NumEntries > 0 && phDevices == NULL) { + return UR_RESULT_ERROR_INVALID_NULL_POINTER; + } + if (UR_DEVICE_TYPE_VPU < DeviceType) { return UR_RESULT_ERROR_INVALID_ENUMERATION; } diff --git a/source/loader/ur_libapi.cpp b/source/loader/ur_libapi.cpp index dced46e279..357c0c3c15 100644 --- a/source/loader/ur_libapi.cpp +++ b/source/loader/ur_libapi.cpp @@ -714,6 +714,8 @@ ur_result_t UR_APICALL urPlatformGetBackendOption( /// + `::UR_DEVICE_TYPE_VPU < DeviceType` /// - ::UR_RESULT_ERROR_INVALID_SIZE /// + `NumEntries == 0 && phDevices != NULL` +/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER +/// + `NumEntries > 0 && phDevices == NULL` /// - ::UR_RESULT_ERROR_INVALID_VALUE ur_result_t UR_APICALL urDeviceGet( ur_platform_handle_t hPlatform, ///< [in] handle of the platform instance diff --git a/source/ur_api.cpp b/source/ur_api.cpp index 4bc1cc2889..93c06a40ab 100644 --- a/source/ur_api.cpp +++ b/source/ur_api.cpp @@ -618,6 +618,8 @@ ur_result_t UR_APICALL urPlatformGetBackendOption( /// + `::UR_DEVICE_TYPE_VPU < DeviceType` /// - ::UR_RESULT_ERROR_INVALID_SIZE /// + `NumEntries == 0 && phDevices != NULL` +/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER +/// + `NumEntries > 0 && phDevices == NULL` /// - ::UR_RESULT_ERROR_INVALID_VALUE ur_result_t UR_APICALL urDeviceGet( ur_platform_handle_t hPlatform, ///< [in] handle of the platform instance diff --git a/test/conformance/device/urDeviceGet.cpp b/test/conformance/device/urDeviceGet.cpp index 85a4818d09..e8aa356a58 100644 --- a/test/conformance/device/urDeviceGet.cpp +++ b/test/conformance/device/urDeviceGet.cpp @@ -49,7 +49,7 @@ TEST_F(urDeviceGetTest, InvalidEnumerationDevicesType) { urDeviceGet(platform, UR_DEVICE_TYPE_FORCE_UINT32, 0, nullptr, &count)); } -TEST_F(urDeviceGetTest, InvalidValueNumEntries) { +TEST_F(urDeviceGetTest, InvalidSizeNumEntries) { uint32_t count = 0; ASSERT_SUCCESS( urDeviceGet(platform, UR_DEVICE_TYPE_ALL, 0, nullptr, &count)); @@ -59,3 +59,13 @@ TEST_F(urDeviceGetTest, InvalidValueNumEntries) { UR_RESULT_ERROR_INVALID_SIZE, urDeviceGet(platform, UR_DEVICE_TYPE_ALL, 0, devices.data(), nullptr)); } + +TEST_F(urDeviceGetTest, InvalidNullPointerDevices) { + uint32_t count = 0; + ASSERT_SUCCESS( + urDeviceGet(platform, UR_DEVICE_TYPE_ALL, 0, nullptr, &count)); + ASSERT_NE(count, 0); + ASSERT_EQ_RESULT( + UR_RESULT_ERROR_INVALID_NULL_POINTER, + urDeviceGet(platform, UR_DEVICE_TYPE_ALL, count, nullptr, nullptr)); +}