From 68ee30fb4bcfaf59af2039375321745f2d36c70f Mon Sep 17 00:00:00 2001 From: John Kesapides <46718829+JohnKesapidesARM@users.noreply.github.com> Date: Tue, 9 Mar 2021 22:57:49 +0000 Subject: [PATCH] Fix possible size_t overflow in 32-bit builds. (#1131) * Fix possible size_t overflow in 32-bit builds. Use cl_ulong temporary values for row/slice_pitch. Signed-off-by: John Kesapides * Remove redundant casts Signed-off-by: John Kesapides --- .../kernel_image_methods/test_2D_array.cpp | 20 ++++++++++++------- .../images/kernel_image_methods/test_3D.cpp | 20 ++++++++++++------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/test_conformance/images/kernel_image_methods/test_2D_array.cpp b/test_conformance/images/kernel_image_methods/test_2D_array.cpp index 79248dd5..21a6b049 100644 --- a/test_conformance/images/kernel_image_methods/test_2D_array.cpp +++ b/test_conformance/images/kernel_image_methods/test_2D_array.cpp @@ -244,6 +244,9 @@ int test_get_image_info_2D_array(cl_device_id device, cl_context context, for( int i = 0; i < NUM_IMAGE_ITERATIONS; i++ ) { cl_ulong size; + cl_ulong slicePitch; + cl_ulong rowPitch; + // Loop until we get a size that a) will fit in the max alloc size and b) that an allocation of that // image, the result array, plus offset arrays, will fit in the global ram space do @@ -252,23 +255,26 @@ int test_get_image_info_2D_array(cl_device_id device, cl_context context, imageInfo.height = (size_t)random_log_in_range( 16, (int)maxHeight / 32, seed ); imageInfo.arraySize = (size_t)random_log_in_range( 16, (int)maxArraySize / 32, seed ); - imageInfo.rowPitch = imageInfo.width * pixelSize; - imageInfo.slicePitch = imageInfo.rowPitch * imageInfo.height; + rowPitch = imageInfo.width * pixelSize; + slicePitch = rowPitch * imageInfo.height; size_t extraWidth = (int)random_log_in_range( 0, 64, seed ); - imageInfo.rowPitch += extraWidth; + rowPitch += extraWidth; do { extraWidth++; - imageInfo.rowPitch += extraWidth; - } while ((imageInfo.rowPitch % pixelSize) != 0); + rowPitch += extraWidth; + } while ((rowPitch % pixelSize) != 0); size_t extraHeight = (int)random_log_in_range( 0, 8, seed ); - imageInfo.slicePitch = imageInfo.rowPitch * (imageInfo.height + extraHeight); + slicePitch = rowPitch * (imageInfo.height + extraHeight); - size = (cl_ulong)imageInfo.slicePitch * (cl_ulong)imageInfo.arraySize * 4 * 4; + size = slicePitch * imageInfo.arraySize * 4 * 4; } while( size > maxAllocSize || ( size * 3 ) > memSize ); + imageInfo.slicePitch = slicePitch; + imageInfo.rowPitch = rowPitch; + if( gDebugTrace ) log_info( " at size %d,%d,%d (pitch %d,%d) out of %d,%d,%d\n", (int)imageInfo.width, (int)imageInfo.height, (int)imageInfo.arraySize, (int)imageInfo.rowPitch, (int)imageInfo.slicePitch, (int)maxWidth, (int)maxHeight, (int)maxArraySize ); int ret = test_get_2Dimage_array_info_single( diff --git a/test_conformance/images/kernel_image_methods/test_3D.cpp b/test_conformance/images/kernel_image_methods/test_3D.cpp index 287005a5..aae433bd 100644 --- a/test_conformance/images/kernel_image_methods/test_3D.cpp +++ b/test_conformance/images/kernel_image_methods/test_3D.cpp @@ -105,6 +105,9 @@ int test_get_image_info_3D(cl_device_id device, cl_context context, for( int i = 0; i < NUM_IMAGE_ITERATIONS; i++ ) { cl_ulong size; + cl_ulong slicePitch; + cl_ulong rowPitch; + // Loop until we get a size that a) will fit in the max alloc size and b) that an allocation of that // image, the result array, plus offset arrays, will fit in the global ram space do @@ -113,23 +116,26 @@ int test_get_image_info_3D(cl_device_id device, cl_context context, imageInfo.height = (size_t)random_log_in_range( 16, (int)maxHeight / 32, seed ); imageInfo.depth = (size_t)random_log_in_range( 16, (int)maxDepth / 32, seed ); - imageInfo.rowPitch = imageInfo.width * pixelSize; - imageInfo.slicePitch = imageInfo.rowPitch * imageInfo.height; + rowPitch = imageInfo.width * pixelSize; + slicePitch = imageInfo.rowPitch * imageInfo.height; size_t extraWidth = (int)random_log_in_range( 0, 64, seed ); - imageInfo.rowPitch += extraWidth; + rowPitch += extraWidth; do { extraWidth++; - imageInfo.rowPitch += extraWidth; - } while ((imageInfo.rowPitch % pixelSize) != 0); + rowPitch += extraWidth; + } while ((rowPitch % pixelSize) != 0); size_t extraHeight = (int)random_log_in_range( 0, 8, seed ); - imageInfo.slicePitch = imageInfo.rowPitch * (imageInfo.height + extraHeight); + slicePitch = rowPitch * (imageInfo.height + extraHeight); - size = (cl_ulong)imageInfo.slicePitch * (cl_ulong)imageInfo.depth * 4 * 4; + size = slicePitch * imageInfo.depth * 4 * 4; } while( size > maxAllocSize || ( size * 3 ) > memSize ); + imageInfo.slicePitch = slicePitch; + imageInfo.rowPitch = rowPitch; + if( gDebugTrace ) log_info( " at size %d,%d,%d (pitch %d,%d) out of %d,%d,%d\n", (int)imageInfo.width, (int)imageInfo.height, (int)imageInfo.depth, (int)imageInfo.rowPitch, (int)imageInfo.slicePitch, (int)maxWidth, (int)maxHeight, (int)maxDepth ); int ret = test_get_image_info_single(context, queue, &imageInfo,