Skip to content

Commit acf6d3b

Browse files
authored
[UR] Fix two issues flagged by coverity (#17890)
* When creating a command buffer, we might encounter errors and leaked the queue. Now it is released if required. * When creating a device, we assert that getting the type was successful. This should never be false since none of the errors apply (it shouldn't allocate any memory either).
1 parent acf9907 commit acf6d3b

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

unified-runtime/source/adapters/opencl/command_buffer.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferCreateExp(
3838
const ur_exp_command_buffer_desc_t *pCommandBufferDesc,
3939
ur_exp_command_buffer_handle_t *phCommandBuffer) {
4040

41-
ur_queue_handle_t Queue = nullptr;
42-
ur_queue_properties_t QueueProperties = {UR_STRUCTURE_TYPE_QUEUE_PROPERTIES,
43-
nullptr, 0};
44-
const bool IsInOrder = pCommandBufferDesc->isInOrder;
45-
if (!IsInOrder) {
46-
QueueProperties.flags = UR_QUEUE_FLAG_OUT_OF_ORDER_EXEC_MODE_ENABLE;
47-
}
48-
UR_RETURN_ON_FAILURE(
49-
urQueueCreate(hContext, hDevice, &QueueProperties, &Queue));
50-
5141
cl_context CLContext = hContext->CLContext;
5242
cl_ext::clCreateCommandBufferKHR_fn clCreateCommandBufferKHR = nullptr;
5343
UR_RETURN_ON_FAILURE(
@@ -71,11 +61,24 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferCreateExp(
7161
CL_COMMAND_BUFFER_FLAGS_KHR,
7262
IsUpdatable ? CL_COMMAND_BUFFER_MUTABLE_KHR : 0u, 0};
7363

64+
ur_queue_handle_t Queue = nullptr;
65+
ur_queue_properties_t QueueProperties = {UR_STRUCTURE_TYPE_QUEUE_PROPERTIES,
66+
nullptr, 0};
67+
const bool IsInOrder = pCommandBufferDesc->isInOrder;
68+
if (!IsInOrder) {
69+
QueueProperties.flags = UR_QUEUE_FLAG_OUT_OF_ORDER_EXEC_MODE_ENABLE;
70+
}
71+
UR_RETURN_ON_FAILURE(
72+
urQueueCreate(hContext, hDevice, &QueueProperties, &Queue));
73+
7474
cl_int Res = CL_SUCCESS;
7575
const cl_command_queue CLQueue = Queue->CLQueue;
7676
auto CLCommandBuffer =
7777
clCreateCommandBufferKHR(1, &CLQueue, Properties, &Res);
78-
CL_RETURN_ON_FAILURE_AND_SET_NULL(Res, phCommandBuffer);
78+
if (Res != CL_SUCCESS) {
79+
urQueueRelease(Queue);
80+
CL_RETURN_ON_FAILURE_AND_SET_NULL(Res, phCommandBuffer);
81+
}
7982

8083
try {
8184
auto URCommandBuffer = std::make_unique<ur_exp_command_buffer_handle_t_>(

unified-runtime/source/adapters/opencl/device.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ struct ur_device_handle_t_ {
2828
if (Parent) {
2929
Type = Parent->Type;
3030
} else {
31-
clGetDeviceInfo(CLDevice, CL_DEVICE_TYPE, sizeof(cl_device_type), &Type,
32-
nullptr);
31+
[[maybe_unused]] auto Res = clGetDeviceInfo(
32+
CLDevice, CL_DEVICE_TYPE, sizeof(cl_device_type), &Type, nullptr);
33+
assert(Res == CL_SUCCESS);
3334
}
3435
}
3536

0 commit comments

Comments
 (0)