Skip to content

Commit

Permalink
[UR] Add validation check to urDeviceGet
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Vesely committed Sep 15, 2023
1 parent bcf2b2a commit 019551c
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions include/ur_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions scripts/core/device.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions source/loader/layers/validation/ur_valddi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions source/loader/ur_libapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions source/ur_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion test/conformance/device/urDeviceGet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
}

0 comments on commit 019551c

Please sign in to comment.