[04/12] utest: error handling to avoid null pointer dereference.

Submitted by Luo, Xionghu on May 5, 2016, 4:11 p.m.

Details

Message ID 1462464714-15444-4-git-send-email-xionghu.luo@intel.com
State New
Headers show
Series "Series without cover letter" ( rev: 1 ) in Beignet

Not browsing as part of any series.

Commit Message

Luo, Xionghu May 5, 2016, 4:11 p.m.
From: Luo Xionghu <xionghu.luo@intel.com>

Signed-off-by: Luo Xionghu <xionghu.luo@intel.com>
---
 benchmark/benchmark_copy_buf.cpp      |  2 ++
 utests/compiler_box_blur_float.cpp    |  3 +++
 utests/get_cl_info.cpp                |  6 ++++++
 utests/image_from_buffer.cpp          |  5 +++++
 utests/load_program_from_bin_file.cpp |  4 ++++
 utests/load_program_from_gen_bin.cpp  |  4 ++++
 utests/load_program_from_spir.cpp     |  4 ++++
 utests/runtime_compile_link.cpp       | 17 ++++++++++++-----
 utests/runtime_flat_address_space.cpp |  2 +-
 utests/sub_buffer.cpp                 |  2 +-
 utests/utest_helper.cpp               | 12 ++++++++++++
 11 files changed, 54 insertions(+), 7 deletions(-)

Patch hide | download patch | download mbox

diff --git a/benchmark/benchmark_copy_buf.cpp b/benchmark/benchmark_copy_buf.cpp
index 92abf54..a85af8c 100644
--- a/benchmark/benchmark_copy_buf.cpp
+++ b/benchmark/benchmark_copy_buf.cpp
@@ -16,6 +16,8 @@  double benchmark_copy_buf(void)
 
   buf0 = (cl_char *)clEnqueueMapBuffer(queue, buf[0], CL_TRUE, CL_MAP_WRITE, 0, sizeof(char), 0, NULL, NULL, NULL);
 
+  OCL_ASSERT(buf0 != NULL);
+
   for (i=0; i < sz; i++) {
     buf0[i]=(rand() & 0xFF);
   }
diff --git a/utests/compiler_box_blur_float.cpp b/utests/compiler_box_blur_float.cpp
index 8a75a25..8d500fc 100644
--- a/utests/compiler_box_blur_float.cpp
+++ b/utests/compiler_box_blur_float.cpp
@@ -14,6 +14,9 @@  static void compiler_box_blur_float()
 
   /* Load the picture */
   tmp = cl_read_bmp("sample.bmp", &w, &h);
+  if(tmp == NULL)
+    return;
+
   sz = w * h * sizeof(float[4]);
   src = (float4*)malloc(sz);
 
diff --git a/utests/get_cl_info.cpp b/utests/get_cl_info.cpp
index 2536690..bdd7e0c 100644
--- a/utests/get_cl_info.cpp
+++ b/utests/get_cl_info.cpp
@@ -181,9 +181,15 @@  void get_program_info(void)
     int sz;
     char *ker_path = (char *)malloc(4096 * sizeof(char));
     const char *kiss_path = getenv("OCL_KERNEL_PATH");
+    if(!kiss_path)
+      return;
+
     string line;
     string source_code;
 
+    if(strlen(kiss_path) > 4000)
+      return;
+
     sprintf(ker_path, "%s/%s", kiss_path, "compiler_if_else.cl");
 
     ifstream in(ker_path);
diff --git a/utests/image_from_buffer.cpp b/utests/image_from_buffer.cpp
index b1171d1..0084f50 100644
--- a/utests/image_from_buffer.cpp
+++ b/utests/image_from_buffer.cpp
@@ -34,6 +34,11 @@  static void image_from_buffer(void)
   size_t buffer_sz = sizeof(uint32_t) * w * h;
   uint32_t* src_data;
   src_data = (uint32_t*)memalign(base_address_alignment, buffer_sz);
+  if(!src_data) {
+    fprintf(stderr, "run out of memory\n");
+    return;
+  }
+
   for (uint32_t j = 0; j < h; ++j)
     for (uint32_t i = 0; i < w; i++)
       src_data[j * w + i] = j * w + i;
diff --git a/utests/load_program_from_bin_file.cpp b/utests/load_program_from_bin_file.cpp
index feefacc..117e15a 100644
--- a/utests/load_program_from_bin_file.cpp
+++ b/utests/load_program_from_bin_file.cpp
@@ -18,6 +18,10 @@  static void test_load_program_from_bin_file(void)
     char *ker_path = NULL;
 
     cl_file_map_t *fm = cl_file_map_new();
+    if(!fm) {
+      fprintf(stderr, "run out of memory\n");
+      return;
+    }
     ker_path = cl_do_kiss_path("compiler_ceil.bin", device);
     OCL_ASSERT (cl_file_map_open(fm, ker_path) == CL_FILE_MAP_SUCCESS);
 
diff --git a/utests/load_program_from_gen_bin.cpp b/utests/load_program_from_gen_bin.cpp
index 3db13b2..26c9485 100644
--- a/utests/load_program_from_gen_bin.cpp
+++ b/utests/load_program_from_gen_bin.cpp
@@ -18,6 +18,10 @@  static void test_load_program_from_gen_bin(void)
     char *ker_path = NULL;
 
     cl_file_map_t *fm = cl_file_map_new();
+    if(!fm) {
+      fprintf(stderr, "run out of memory\n");
+      return;
+    }
     ker_path = cl_do_kiss_path("compiler_ceil.cl", device);
     OCL_ASSERT (cl_file_map_open(fm, ker_path) == CL_FILE_MAP_SUCCESS);
 
diff --git a/utests/load_program_from_spir.cpp b/utests/load_program_from_spir.cpp
index 8ea1cd4..9fc28e2 100644
--- a/utests/load_program_from_spir.cpp
+++ b/utests/load_program_from_spir.cpp
@@ -31,6 +31,10 @@  static void test_load_program_from_spir(void)
     char *ker_path = NULL;
 
     cl_file_map_t *fm = cl_file_map_new();
+    if(!fm) {
+      fprintf(stderr, "run out of memory\n");
+      return;
+    }
     ker_path = cl_do_kiss_path("compiler_ceil32.spir", device);
     OCL_ASSERT (cl_file_map_open(fm, ker_path) == CL_FILE_MAP_SUCCESS);
 
diff --git a/utests/runtime_compile_link.cpp b/utests/runtime_compile_link.cpp
index 4a39b6a..48439d2 100644
--- a/utests/runtime_compile_link.cpp
+++ b/utests/runtime_compile_link.cpp
@@ -12,6 +12,9 @@  int init_program(const char* name, cl_context ctx, cl_program *pg )
   char* ker_path = cl_do_kiss_path(name, device);
 
   cl_file_map_t *fm = cl_file_map_new();
+  if(!fm)
+    return CL_FALSE;
+
   err = cl_file_map_open(fm, ker_path);
   if(err != CL_FILE_MAP_SUCCESS)
     OCL_ASSERT(0);
@@ -20,7 +23,7 @@  int init_program(const char* name, cl_context ctx, cl_program *pg )
   *pg = clCreateProgramWithSource(ctx, 1, &src, NULL, &err);
   free(ker_path);
   cl_file_map_delete(fm);
-  return 0;
+  return CL_SUCCESS;
 
 }
 
@@ -31,15 +34,18 @@  void runtime_compile_link(void)
 
   const char* header_file_name="runtime_compile_link.h";
   cl_program foo_pg;
-  init_program(header_file_name, ctx, &foo_pg);
+  err = init_program(header_file_name, ctx, &foo_pg);
+  OCL_ASSERT(err==CL_SUCCESS);
 
   const char* myinc_file_name="include/runtime_compile_link_inc.h";
   cl_program myinc_pg;
-  init_program(myinc_file_name, ctx, &myinc_pg);
+  err = init_program(myinc_file_name, ctx, &myinc_pg);
+  OCL_ASSERT(err==CL_SUCCESS);
 
   const char* file_name_A="runtime_compile_link_a.cl";
   cl_program program_A;
-  init_program(file_name_A, ctx, &program_A);
+  err = init_program(file_name_A, ctx, &program_A);
+  OCL_ASSERT(err==CL_SUCCESS);
 
   cl_program input_headers[2] = { foo_pg, myinc_pg};
   const char * input_header_names[2] = {header_file_name, myinc_file_name}; 
@@ -55,7 +61,8 @@  void runtime_compile_link(void)
   OCL_ASSERT(err==CL_SUCCESS);
   const char* file_name_B="runtime_compile_link_b.cl";
   cl_program program_B;
-  init_program(file_name_B, ctx, &program_B);
+  err = init_program(file_name_B, ctx, &program_B);
+  OCL_ASSERT(err==CL_SUCCESS);
 
   err = clCompileProgram(program_B,
                                 0, NULL, // num_devices & device_list
diff --git a/utests/runtime_flat_address_space.cpp b/utests/runtime_flat_address_space.cpp
index 6430edb..cf94cf5 100644
--- a/utests/runtime_flat_address_space.cpp
+++ b/utests/runtime_flat_address_space.cpp
@@ -56,7 +56,7 @@  main(int argc, char *argv[])
     dst_buffer = (int *)clEnqueueMapBuffer(queue, dst[j], CL_TRUE, CL_MAP_READ, 0, sizeof(int)*n, 0, NULL, NULL, &status);
     if (status != CL_SUCCESS)
       goto error;
-    for (uint32_t i = 0; i < n; ++i)
+    for (uint32_t i = 0; dst_buffer && i < n; ++i)
       if (dst_buffer[i] != int(i)) {
         fprintf(stderr, "run-time flat address space failed\n");
         exit(-1);
diff --git a/utests/sub_buffer.cpp b/utests/sub_buffer.cpp
index 6228034..04cfee7 100644
--- a/utests/sub_buffer.cpp
+++ b/utests/sub_buffer.cpp
@@ -116,7 +116,7 @@  void sub_buffer_check(void)
 #endif
             for (int i = 0; i < 32; i++) {
 
-                if (((char *)mapped_ptr)[i] != sub_buf_content[i]) {
+                if (mapped_ptr && ((char *)mapped_ptr)[i] != sub_buf_content[i]) {
                     printf ("different index is %d\n", i);
                     OCL_ASSERT(0);
                 }
diff --git a/utests/utest_helper.cpp b/utests/utest_helper.cpp
index 9696dac..86d604c 100644
--- a/utests/utest_helper.cpp
+++ b/utests/utest_helper.cpp
@@ -241,6 +241,10 @@  cl_kernel_init(const char *file_name, const char *kernel_name, int format, const
       assert(0);
     } else if (format == SOURCE) {
       cl_file_map_t *fm = cl_file_map_new();
+      if(!fm) {
+        fprintf(stderr, "run out of memory\n");
+        goto error;
+      }
       FATAL_IF (cl_file_map_open(fm, ker_path) != CL_FILE_MAP_SUCCESS,
                 "Failed to open file \"%s\" with kernel \"%s\". Did you properly set OCL_KERNEL_PATH variable?",
                 file_name, kernel_name);
@@ -292,6 +296,10 @@  cl_kernel_compile(const char *file_name, const char *kernel_name, const char * c
     if (program) clReleaseProgram(program);
     ker_path = cl_do_kiss_path(file_name, device);
     cl_file_map_t *fm = cl_file_map_new();
+    if(!fm) {
+      fprintf(stderr, "run out of memory\n");
+      goto error;
+    }
     FATAL_IF (cl_file_map_open(fm, ker_path) != CL_FILE_MAP_SUCCESS,
                 "Failed to open file \"%s\" with kernel \"%s\". Did you properly set OCL_KERNEL_PATH variable?",
                 file_name, kernel_name);
@@ -339,6 +347,10 @@  cl_kernel_link(const char *file_name, const char *kernel_name, const char * link
     if (program) clReleaseProgram(program);
     ker_path = cl_do_kiss_path(file_name, device);
     cl_file_map_t *fm = cl_file_map_new();
+    if(!fm) {
+      fprintf(stderr, "run out of memory\n");
+      goto error;
+    }
     FATAL_IF (cl_file_map_open(fm, ker_path) != CL_FILE_MAP_SUCCESS,
                 "Failed to open file \"%s\" with kernel \"%s\". Did you properly set OCL_KERNEL_PATH variable?",
                 file_name, kernel_name);