From 0130c24fe5da57e74949466574c8ec4969e68121 Mon Sep 17 00:00:00 2001 From: Nikhil Joshi Date: Thu, 21 Jan 2021 19:01:54 +0530 Subject: [PATCH] Fix malloc-size calculation in test imagedim (#1100) * Fix enqueue_flags test to use correct barrier type. Currently, enqueue_flags test uses CLK_LOCAL_MEM_FENCE. Use CLK_GLOBAL_MEM_FENCE instead as all threads across work-groups need to wait here. * Add check for support for Read-Wrie images Read-Write images have required OpenCL 2.x. Read-Write image tests are already being skipped for 1.x devices. With OpenCL 3.0, read-write images being optional, the tests should be run or skipped depending on the implementation support. Add a check to decide if Read-Write images are supported or required to be supported depending on OpenCL version and decide if the tests should be run on skipped. Fixes issue #894 * Fix formatting in case of Read-Write image checks. Fix formatting in case of Read-write image checks. Also, combine two ifs into one in case of kerne_read_write tests * Fix some more formatting for RW-image checks Remove unnecessary spaces at various places. Also, fix lengthy lines. * Fix malloc-size calculation in test imagedim unsigned char size is silently assumed to be 1 in imagedim test of test_basic. Pass sizeof(type) in malloc size calculation. Also, change loop variable from signed to unsigned. Add checks for null pointer for malloced memory. * Use size_t instead of int for imagedim The size calculation for image with larger dimensions is overflowing with int values. Change image dim variables to use size_t to avoid overflow. While at it, fix formatting at various places. * Use new instead of malloc in test imagedim Use new and delete in place of malloc and free through test_basic imagedim to avoid NULL pointer checks. * Revert sizeof changes from size calculation As the types of width and height are now changed to size_t, sizeof is not required in size calculation. Revert the same. --- test_conformance/basic/test_imagedim.cpp | 158 ++++++++++++----------- 1 file changed, 80 insertions(+), 78 deletions(-) diff --git a/test_conformance/basic/test_imagedim.cpp b/test_conformance/basic/test_imagedim.cpp index 6064655f..008c88b6 100644 --- a/test_conformance/basic/test_imagedim.cpp +++ b/test_conformance/basic/test_imagedim.cpp @@ -38,24 +38,25 @@ static const char *image_dim_kernel_code = "}\n"; -static unsigned char * -generate_8888_image(int w, int h, MTdata d) +static unsigned char *generate_8888_image(size_t w, size_t h, MTdata d) { - unsigned char *ptr = (unsigned char*)malloc(w * h * 4); - int i; + unsigned char *ptr = new unsigned char[4 * w * h]; + size_t i; - for (i=0; i n) m >>= 1; - max_img_dim = (int)m; + max_img_dim = m; } if (max_img_width > max_img_dim) @@ -151,13 +152,14 @@ test_imagedim_pow2(cl_device_id device, cl_context context, cl_command_queue que d = init_genrand( gRandomSeed ); input_ptr = generate_8888_image(max_img_width, max_img_height, d); - output_ptr = (unsigned char*)malloc(sizeof(unsigned char) * 4 * max_img_width * max_img_height); + + output_ptr = new unsigned char[4 * max_img_width * max_img_height]; // test power of 2 width, height starting at 1 to 4K - for (i=1,i2=0; i<=max_img_height; i<<=1,i2++) + for (i = 1, i2 = 0; i <= max_img_height; i <<= 1, i2++) { img_height = (1 << i2); - for (j=1,j2=0; j<=max_img_width; j<<=1,j2++) + for (j = 1, j2 = 0; j <= max_img_width; j <<= 1, j2++) { img_width = (1 << j2); @@ -169,8 +171,8 @@ test_imagedim_pow2(cl_device_id device, cl_context context, cl_command_queue que if (!streams[0]) { log_error("create_image_2d failed. width = %d, height = %d\n", img_width, img_height); - free(input_ptr); - free(output_ptr); + delete[] input_ptr; + delete[] output_ptr; free_mtdata(d); return -1; } @@ -183,8 +185,8 @@ test_imagedim_pow2(cl_device_id device, cl_context context, cl_command_queue que { log_error("create_image_2d failed. width = %d, height = %d\n", img_width, img_height); clReleaseMemObject(streams[0]); - free(input_ptr); - free(output_ptr); + delete[] input_ptr; + delete[] output_ptr; free_mtdata(d); return -1; } @@ -197,8 +199,8 @@ test_imagedim_pow2(cl_device_id device, cl_context context, cl_command_queue que log_error("clWriteImage failed\n"); clReleaseMemObject(streams[0]); clReleaseMemObject(streams[1]); - free(input_ptr); - free(output_ptr); + delete[] input_ptr; + delete[] output_ptr; free_mtdata(d); return -1; } @@ -211,8 +213,8 @@ test_imagedim_pow2(cl_device_id device, cl_context context, cl_command_queue que log_error("clSetKernelArgs failed\n"); clReleaseMemObject(streams[0]); clReleaseMemObject(streams[1]); - free(input_ptr); - free(output_ptr); + delete[] input_ptr; + delete[] output_ptr; free_mtdata(d); return -1; } @@ -228,8 +230,8 @@ test_imagedim_pow2(cl_device_id device, cl_context context, cl_command_queue que img_width, img_height); clReleaseMemObject(streams[0]); clReleaseMemObject(streams[1]); - free(input_ptr); - free(output_ptr); + delete[] input_ptr; + delete[] output_ptr; free_mtdata(d); return -1; } @@ -241,8 +243,8 @@ test_imagedim_pow2(cl_device_id device, cl_context context, cl_command_queue que img_width, img_height); clReleaseMemObject(streams[0]); clReleaseMemObject(streams[1]); - free(input_ptr); - free(output_ptr); + delete[] input_ptr; + delete[] output_ptr; free_mtdata(d); return -1; } @@ -259,8 +261,8 @@ test_imagedim_pow2(cl_device_id device, cl_context context, cl_command_queue que } // cleanup - free(input_ptr); - free(output_ptr); + delete[] input_ptr; + delete[] output_ptr; free_mtdata(d); clReleaseSampler(sampler); clReleaseKernel(kernel); @@ -274,18 +276,18 @@ test_imagedim_pow2(cl_device_id device, cl_context context, cl_command_queue que int test_imagedim_non_pow2(cl_device_id device, cl_context context, cl_command_queue queue, int n_elems) { - cl_mem streams[2]; - cl_image_format img_format; - unsigned char *input_ptr, *output_ptr; - cl_program program; - cl_kernel kernel; - size_t threads[2], local_threads[2]; - cl_ulong max_mem_size; - int img_width, max_img_width; - int img_height, max_img_height; - int max_img_dim; - int i, j, i2, j2, err=0; - size_t max_image2d_width, max_image2d_height; + cl_mem streams[2]; + cl_image_format img_format; + unsigned char *input_ptr, *output_ptr; + cl_program program; + cl_kernel kernel; + size_t threads[2], local_threads[2]; + cl_ulong max_mem_size; + size_t img_width, max_img_width; + size_t img_height, max_img_height; + size_t max_img_dim; + int i, j, i2, j2, err = 0; + size_t max_image2d_width, max_image2d_height; int total_errors = 0; size_t max_local_workgroup_size[3]; MTdata d; @@ -365,10 +367,10 @@ test_imagedim_non_pow2(cl_device_id device, cl_context context, cl_command_queue d = init_genrand( gRandomSeed ); input_ptr = generate_8888_image(max_img_width, max_img_height, d); - output_ptr = (unsigned char*)malloc(sizeof(unsigned char) * 4 * max_img_width * max_img_height); + output_ptr = new unsigned char[4 * max_img_width * max_img_height]; int plus_minus; - for (plus_minus=0; plus_minus < 3; plus_minus++) + for (plus_minus = 0; plus_minus < 3; plus_minus++) { // test power of 2 width, height starting at 1 to 4K @@ -379,8 +381,8 @@ test_imagedim_non_pow2(cl_device_id device, cl_context context, cl_command_queue { img_width = (1 << j2); - int effective_img_height = img_height; - int effective_img_width = img_width; + size_t effective_img_height = img_height; + size_t effective_img_width = img_width; local_threads[0] = 1; local_threads[1] = 1; @@ -414,8 +416,8 @@ test_imagedim_non_pow2(cl_device_id device, cl_context context, cl_command_queue if (!streams[0]) { log_error("create_image_2d failed. width = %d, height = %d\n", effective_img_width, effective_img_height); - free(input_ptr); - free(output_ptr); + delete[] input_ptr; + delete[] output_ptr; free_mtdata(d); return -1; } @@ -428,8 +430,8 @@ test_imagedim_non_pow2(cl_device_id device, cl_context context, cl_command_queue { log_error("create_image_2d failed. width = %d, height = %d\n", effective_img_width, effective_img_height); clReleaseMemObject(streams[0]); - free(input_ptr); - free(output_ptr); + delete[] input_ptr; + delete[] output_ptr; free_mtdata(d); return -1; } @@ -442,8 +444,8 @@ test_imagedim_non_pow2(cl_device_id device, cl_context context, cl_command_queue log_error("clWriteImage failed\n"); clReleaseMemObject(streams[0]); clReleaseMemObject(streams[1]); - free(input_ptr); - free(output_ptr); + delete[] input_ptr; + delete[] output_ptr; free_mtdata(d); return -1; } @@ -456,8 +458,8 @@ test_imagedim_non_pow2(cl_device_id device, cl_context context, cl_command_queue log_error("clSetKernelArgs failed\n"); clReleaseMemObject(streams[0]); clReleaseMemObject(streams[1]); - free(input_ptr); - free(output_ptr); + delete[] input_ptr; + delete[] output_ptr; free_mtdata(d); return -1; } @@ -474,8 +476,8 @@ test_imagedim_non_pow2(cl_device_id device, cl_context context, cl_command_queue effective_img_width, effective_img_height, (int)local_threads[0], (int)local_threads[1]); clReleaseMemObject(streams[0]); clReleaseMemObject(streams[1]); - free(input_ptr); - free(output_ptr); + delete[] input_ptr; + delete[] output_ptr; free_mtdata(d); return -1; } @@ -487,8 +489,8 @@ test_imagedim_non_pow2(cl_device_id device, cl_context context, cl_command_queue effective_img_width, effective_img_height, (int)local_threads[0], (int)local_threads[1]); clReleaseMemObject(streams[0]); clReleaseMemObject(streams[1]); - free(input_ptr); - free(output_ptr); + delete[] input_ptr; + delete[] output_ptr; free_mtdata(d); return -1; } @@ -506,15 +508,15 @@ test_imagedim_non_pow2(cl_device_id device, cl_context context, cl_command_queue } - // cleanup - free(input_ptr); - free(output_ptr); - free_mtdata(d); - clReleaseSampler(sampler); - clReleaseKernel(kernel); - clReleaseProgram(program); + // cleanup + delete[] input_ptr; + delete[] output_ptr; + free_mtdata(d); + clReleaseSampler(sampler); + clReleaseKernel(kernel); + clReleaseProgram(program); - return total_errors; + return total_errors; }