Skip to content

Commit

Permalink
[OpenCL] Address coverity issue with dodgy struct-to-array casts
Browse files Browse the repository at this point in the history
Also fix a similar issue coverity had with CreateProgramWithBinary which
was passing array pointers straight through to the cl entry point by
putting these params in local const arrays.
  • Loading branch information
aarongreig committed Nov 9, 2023
1 parent 0e24ab8 commit 7ea3e5d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
52 changes: 29 additions & 23 deletions source/adapters/opencl/enqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,16 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferReadRect(
size_t hostRowPitch, size_t hostSlicePitch, void *pDst,
uint32_t numEventsInWaitList, const ur_event_handle_t *phEventWaitList,
ur_event_handle_t *phEvent) {
const size_t BufferOrigin[3] = {bufferOrigin.x, bufferOrigin.y,
bufferOrigin.z};
const size_t HostOrigin[3] = {hostOrigin.x, hostOrigin.y, hostOrigin.z};
const size_t Region[3] = {region.width, region.height, region.depth};

CL_RETURN_ON_FAILURE(clEnqueueReadBufferRect(
cl_adapter::cast<cl_command_queue>(hQueue),
cl_adapter::cast<cl_mem>(hBuffer), blockingRead,
cl_adapter::cast<const size_t *>(&bufferOrigin),
cl_adapter::cast<const size_t *>(&hostOrigin),
cl_adapter::cast<const size_t *>(&region), bufferRowPitch,
bufferSlicePitch, hostRowPitch, hostSlicePitch, pDst, numEventsInWaitList,
cl_adapter::cast<cl_mem>(hBuffer), blockingRead, BufferOrigin, HostOrigin,
Region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch,
pDst, numEventsInWaitList,
cl_adapter::cast<const cl_event *>(phEventWaitList),
cl_adapter::cast<cl_event *>(phEvent)));

Expand All @@ -121,14 +123,16 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferWriteRect(
size_t hostRowPitch, size_t hostSlicePitch, void *pSrc,
uint32_t numEventsInWaitList, const ur_event_handle_t *phEventWaitList,
ur_event_handle_t *phEvent) {
const size_t BufferOrigin[3] = {bufferOrigin.x, bufferOrigin.y,
bufferOrigin.z};
const size_t HostOrigin[3] = {hostOrigin.x, hostOrigin.y, hostOrigin.z};
const size_t Region[3] = {region.width, region.height, region.depth};

CL_RETURN_ON_FAILURE(clEnqueueWriteBufferRect(
cl_adapter::cast<cl_command_queue>(hQueue),
cl_adapter::cast<cl_mem>(hBuffer), blockingWrite,
cl_adapter::cast<const size_t *>(&bufferOrigin),
cl_adapter::cast<const size_t *>(&hostOrigin),
cl_adapter::cast<const size_t *>(&region), bufferRowPitch,
bufferSlicePitch, hostRowPitch, hostSlicePitch, pSrc, numEventsInWaitList,
cl_adapter::cast<cl_mem>(hBuffer), blockingWrite, BufferOrigin,
HostOrigin, Region, bufferRowPitch, bufferSlicePitch, hostRowPitch,
hostSlicePitch, pSrc, numEventsInWaitList,
cl_adapter::cast<const cl_event *>(phEventWaitList),
cl_adapter::cast<cl_event *>(phEvent)));

Expand Down Expand Up @@ -158,16 +162,16 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferCopyRect(
size_t srcSlicePitch, size_t dstRowPitch, size_t dstSlicePitch,
uint32_t numEventsInWaitList, const ur_event_handle_t *phEventWaitList,
ur_event_handle_t *phEvent) {
const size_t SrcOrigin[3] = {srcOrigin.x, srcOrigin.y, srcOrigin.z};
const size_t DstOrigin[3] = {dstOrigin.x, dstOrigin.y, dstOrigin.z};
const size_t Region[3] = {region.width, region.height, region.depth};

CL_RETURN_ON_FAILURE(clEnqueueCopyBufferRect(
cl_adapter::cast<cl_command_queue>(hQueue),
cl_adapter::cast<cl_mem>(hBufferSrc),
cl_adapter::cast<cl_mem>(hBufferDst),
cl_adapter::cast<const size_t *>(&srcOrigin),
cl_adapter::cast<const size_t *>(&dstOrigin),
cl_adapter::cast<const size_t *>(&region), srcRowPitch, srcSlicePitch,
dstRowPitch, dstSlicePitch, numEventsInWaitList,
cl_adapter::cast<const cl_event *>(phEventWaitList),
cl_adapter::cast<cl_mem>(hBufferDst), SrcOrigin, DstOrigin, Region,
srcRowPitch, srcSlicePitch, dstRowPitch, dstSlicePitch,
numEventsInWaitList, cl_adapter::cast<const cl_event *>(phEventWaitList),
cl_adapter::cast<cl_event *>(phEvent)));

return UR_RESULT_SUCCESS;
Expand Down Expand Up @@ -210,13 +214,14 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemImageWrite(
ur_rect_offset_t origin, ur_rect_region_t region, size_t rowPitch,
size_t slicePitch, void *pSrc, uint32_t numEventsInWaitList,
const ur_event_handle_t *phEventWaitList, ur_event_handle_t *phEvent) {
const size_t Origin[3] = {origin.x, origin.y, origin.z};
const size_t Region[3] = {region.width, region.height, region.depth};

CL_RETURN_ON_FAILURE(clEnqueueWriteImage(
cl_adapter::cast<cl_command_queue>(hQueue),
cl_adapter::cast<cl_mem>(hImage), blockingWrite,
cl_adapter::cast<const size_t *>(&origin),
cl_adapter::cast<const size_t *>(&region), rowPitch, slicePitch, pSrc,
numEventsInWaitList, cl_adapter::cast<const cl_event *>(phEventWaitList),
cl_adapter::cast<cl_mem>(hImage), blockingWrite, Origin, Region, rowPitch,
slicePitch, pSrc, numEventsInWaitList,
cl_adapter::cast<const cl_event *>(phEventWaitList),
cl_adapter::cast<cl_event *>(phEvent)));

return UR_RESULT_SUCCESS;
Expand All @@ -228,13 +233,14 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemImageCopy(
ur_rect_offset_t dstOrigin, ur_rect_region_t region,
uint32_t numEventsInWaitList, const ur_event_handle_t *phEventWaitList,
ur_event_handle_t *phEvent) {
const size_t SrcOrigin[3] = {srcOrigin.x, srcOrigin.y, srcOrigin.z};
const size_t DstOrigin[3] = {dstOrigin.x, dstOrigin.y, dstOrigin.z};
const size_t Region[3] = {region.width, region.height, region.depth};

CL_RETURN_ON_FAILURE(clEnqueueCopyImage(
cl_adapter::cast<cl_command_queue>(hQueue),
cl_adapter::cast<cl_mem>(hImageSrc), cl_adapter::cast<cl_mem>(hImageDst),
cl_adapter::cast<const size_t *>(&srcOrigin),
cl_adapter::cast<const size_t *>(&dstOrigin),
cl_adapter::cast<const size_t *>(&region), numEventsInWaitList,
SrcOrigin, DstOrigin, Region, numEventsInWaitList,
cl_adapter::cast<const cl_event *>(phEventWaitList),
cl_adapter::cast<cl_event *>(phEvent)));

Expand Down
9 changes: 5 additions & 4 deletions source/adapters/opencl/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,14 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithBinary(
const uint8_t *pBinary, const ur_program_properties_t *,
ur_program_handle_t *phProgram) {

cl_int BinaryStatus;
const cl_device_id Devices[1] = {cl_adapter::cast<cl_device_id>(hDevice)};
const size_t Lengths[1] = {size};
cl_int BinaryStatus[1];
cl_int CLResult;
*phProgram = cl_adapter::cast<ur_program_handle_t>(clCreateProgramWithBinary(
cl_adapter::cast<cl_context>(hContext), cl_adapter::cast<cl_uint>(1u),
cl_adapter::cast<const cl_device_id *>(&hDevice), &size, &pBinary,
&BinaryStatus, &CLResult));
CL_RETURN_ON_FAILURE(BinaryStatus);
Devices, Lengths, &pBinary, BinaryStatus, &CLResult));
CL_RETURN_ON_FAILURE(BinaryStatus[0]);
CL_RETURN_ON_FAILURE(CLResult);

return UR_RESULT_SUCCESS;
Expand Down

0 comments on commit 7ea3e5d

Please sign in to comment.