From 7ea3e5dfc165f7fdbf7e98bda448cc3b7884530a Mon Sep 17 00:00:00 2001 From: Aaron Greig Date: Thu, 9 Nov 2023 14:05:36 +0000 Subject: [PATCH] [OpenCL] Address coverity issue with dodgy struct-to-array casts 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. --- source/adapters/opencl/enqueue.cpp | 52 +++++++++++++++++------------- source/adapters/opencl/program.cpp | 9 +++--- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/source/adapters/opencl/enqueue.cpp b/source/adapters/opencl/enqueue.cpp index 5f41878182..7239b23298 100644 --- a/source/adapters/opencl/enqueue.cpp +++ b/source/adapters/opencl/enqueue.cpp @@ -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(hQueue), - cl_adapter::cast(hBuffer), blockingRead, - cl_adapter::cast(&bufferOrigin), - cl_adapter::cast(&hostOrigin), - cl_adapter::cast(®ion), bufferRowPitch, - bufferSlicePitch, hostRowPitch, hostSlicePitch, pDst, numEventsInWaitList, + cl_adapter::cast(hBuffer), blockingRead, BufferOrigin, HostOrigin, + Region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, + pDst, numEventsInWaitList, cl_adapter::cast(phEventWaitList), cl_adapter::cast(phEvent))); @@ -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(hQueue), - cl_adapter::cast(hBuffer), blockingWrite, - cl_adapter::cast(&bufferOrigin), - cl_adapter::cast(&hostOrigin), - cl_adapter::cast(®ion), bufferRowPitch, - bufferSlicePitch, hostRowPitch, hostSlicePitch, pSrc, numEventsInWaitList, + cl_adapter::cast(hBuffer), blockingWrite, BufferOrigin, + HostOrigin, Region, bufferRowPitch, bufferSlicePitch, hostRowPitch, + hostSlicePitch, pSrc, numEventsInWaitList, cl_adapter::cast(phEventWaitList), cl_adapter::cast(phEvent))); @@ -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(hQueue), cl_adapter::cast(hBufferSrc), - cl_adapter::cast(hBufferDst), - cl_adapter::cast(&srcOrigin), - cl_adapter::cast(&dstOrigin), - cl_adapter::cast(®ion), srcRowPitch, srcSlicePitch, - dstRowPitch, dstSlicePitch, numEventsInWaitList, - cl_adapter::cast(phEventWaitList), + cl_adapter::cast(hBufferDst), SrcOrigin, DstOrigin, Region, + srcRowPitch, srcSlicePitch, dstRowPitch, dstSlicePitch, + numEventsInWaitList, cl_adapter::cast(phEventWaitList), cl_adapter::cast(phEvent))); return UR_RESULT_SUCCESS; @@ -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(hQueue), - cl_adapter::cast(hImage), blockingWrite, - cl_adapter::cast(&origin), - cl_adapter::cast(®ion), rowPitch, slicePitch, pSrc, - numEventsInWaitList, cl_adapter::cast(phEventWaitList), + cl_adapter::cast(hImage), blockingWrite, Origin, Region, rowPitch, + slicePitch, pSrc, numEventsInWaitList, + cl_adapter::cast(phEventWaitList), cl_adapter::cast(phEvent))); return UR_RESULT_SUCCESS; @@ -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(hQueue), cl_adapter::cast(hImageSrc), cl_adapter::cast(hImageDst), - cl_adapter::cast(&srcOrigin), - cl_adapter::cast(&dstOrigin), - cl_adapter::cast(®ion), numEventsInWaitList, + SrcOrigin, DstOrigin, Region, numEventsInWaitList, cl_adapter::cast(phEventWaitList), cl_adapter::cast(phEvent))); diff --git a/source/adapters/opencl/program.cpp b/source/adapters/opencl/program.cpp index 7f46b52cfa..c414085e4a 100644 --- a/source/adapters/opencl/program.cpp +++ b/source/adapters/opencl/program.cpp @@ -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(hDevice)}; + const size_t Lengths[1] = {size}; + cl_int BinaryStatus[1]; cl_int CLResult; *phProgram = cl_adapter::cast(clCreateProgramWithBinary( cl_adapter::cast(hContext), cl_adapter::cast(1u), - cl_adapter::cast(&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;