utests: fix compiler_fill_image_2d_array random bug

Submitted by Pan Xiuli on Oct. 29, 2015, 5:46 a.m.

Details

Message ID 1446097596-3034-1-git-send-email-xiuli.pan@intel.com
State New
Headers show
Series "utests: fix compiler_fill_image_2d_array random bug" ( rev: 1 ) in Beignet

Not browsing as part of any series.

Commit Message

Pan Xiuli Oct. 29, 2015, 5:46 a.m.
Use safer image write instead of map and memset. When
create image without data, we could not set pitch and
we don't know the pitch either. So use map and memset
the space is too dangerous if pitch is bigger than
w*sizeof(bpp), in this case the actually pitch is 512
but memset use pitch as 64*4=256. With only half space
set to 0, there will be undefined behavior when we want
to check the result for those space that we haven't set
to 0.

Signed-off-by: Pan Xiuli <xiuli.pan@intel.com>
---
 utests/compiler_fill_image_2d_array.cpp | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

Patch hide | download patch | download mbox

diff --git a/utests/compiler_fill_image_2d_array.cpp b/utests/compiler_fill_image_2d_array.cpp
index fc09362..ab7470e 100644
--- a/utests/compiler_fill_image_2d_array.cpp
+++ b/utests/compiler_fill_image_2d_array.cpp
@@ -11,6 +11,7 @@  static void compiler_fill_image_2d_array(void)
   size_t origin[3] = { };
   size_t region[3];
   uint32_t* dst;
+  uint32_t* src;
 
   memset(&desc, 0x0, sizeof(cl_image_desc));
   memset(&format, 0x0, sizeof(cl_image_format));
@@ -28,9 +29,16 @@  static void compiler_fill_image_2d_array(void)
 
   OCL_CREATE_IMAGE(buf[0], 0, &format, &desc, NULL);
 
-  OCL_MAP_BUFFER_GTT(0);
-  memset(buf_data[0], 0, sizeof(uint32_t) * w * h * array);
-  OCL_UNMAP_BUFFER_GTT(0);
+  region[0] = w;
+  region[1] = h;
+  region[2] = array;
+
+  // As we don't know the pitch right now, we cannot
+  // use map to setup the image. It is safer to use
+  // write image
+  src = (uint32_t*)malloc(sizeof(uint32_t) * w * h * array);
+  memset(src, 0, sizeof(uint32_t) * w * h * array);
+  OCL_WRITE_IMAGE(buf[0], origin, region, src);
 
   // Run the kernel
   OCL_SET_ARG(0, sizeof(cl_mem), &buf[0]);
@@ -43,9 +51,6 @@  static void compiler_fill_image_2d_array(void)
   OCL_NDRANGE(3);
 
   // Check result
-  region[0] = w;
-  region[1] = h;
-  region[2] = array;
   dst = (uint32_t*)malloc(w*h*array*sizeof(uint32_t));
   OCL_READ_IMAGE(buf[0], origin, region, dst);
 
@@ -79,6 +84,7 @@  static void compiler_fill_image_2d_array(void)
     }
   }
   free(dst);
+  free(src);
 }
 
 MAKE_UTEST_FROM_FUNCTION(compiler_fill_image_2d_array);

Comments

LGTM, thanks, pushed.

> -----Original Message-----

> From: Beignet [mailto:beignet-bounces@lists.freedesktop.org] On Behalf Of

> Pan Xiuli

> Sent: Thursday, October 29, 2015 13:47

> To: beignet@lists.freedesktop.org

> Cc: Pan, Xiuli

> Subject: [Beignet] [PATCH] utests: fix compiler_fill_image_2d_array random

> bug

> 

> Use safer image write instead of map and memset. When create image

> without data, we could not set pitch and we don't know the pitch either. So

> use map and memset the space is too dangerous if pitch is bigger than

> w*sizeof(bpp), in this case the actually pitch is 512 but memset use pitch as

> 64*4=256. With only half space set to 0, there will be undefined behavior

> when we want to check the result for those space that we haven't set to 0.

> 

> Signed-off-by: Pan Xiuli <xiuli.pan@intel.com>

> ---

>  utests/compiler_fill_image_2d_array.cpp | 18 ++++++++++++------

>  1 file changed, 12 insertions(+), 6 deletions(-)

> 

> diff --git a/utests/compiler_fill_image_2d_array.cpp

> b/utests/compiler_fill_image_2d_array.cpp

> index fc09362..ab7470e 100644

> --- a/utests/compiler_fill_image_2d_array.cpp

> +++ b/utests/compiler_fill_image_2d_array.cpp

> @@ -11,6 +11,7 @@ static void compiler_fill_image_2d_array(void)

>    size_t origin[3] = { };

>    size_t region[3];

>    uint32_t* dst;

> +  uint32_t* src;

> 

>    memset(&desc, 0x0, sizeof(cl_image_desc));

>    memset(&format, 0x0, sizeof(cl_image_format)); @@ -28,9 +29,16 @@

> static void compiler_fill_image_2d_array(void)

> 

>    OCL_CREATE_IMAGE(buf[0], 0, &format, &desc, NULL);

> 

> -  OCL_MAP_BUFFER_GTT(0);

> -  memset(buf_data[0], 0, sizeof(uint32_t) * w * h * array);

> -  OCL_UNMAP_BUFFER_GTT(0);

> +  region[0] = w;

> +  region[1] = h;

> +  region[2] = array;

> +

> +  // As we don't know the pitch right now, we cannot  // use map to

> + setup the image. It is safer to use  // write image  src =

> + (uint32_t*)malloc(sizeof(uint32_t) * w * h * array);  memset(src, 0,

> + sizeof(uint32_t) * w * h * array);  OCL_WRITE_IMAGE(buf[0], origin,

> + region, src);

> 

>    // Run the kernel

>    OCL_SET_ARG(0, sizeof(cl_mem), &buf[0]); @@ -43,9 +51,6 @@ static void

> compiler_fill_image_2d_array(void)

>    OCL_NDRANGE(3);

> 

>    // Check result

> -  region[0] = w;

> -  region[1] = h;

> -  region[2] = array;

>    dst = (uint32_t*)malloc(w*h*array*sizeof(uint32_t));

>    OCL_READ_IMAGE(buf[0], origin, region, dst);

> 

> @@ -79,6 +84,7 @@ static void compiler_fill_image_2d_array(void)

>      }

>    }

>    free(dst);

> +  free(src);

>  }

> 

>  MAKE_UTEST_FROM_FUNCTION(compiler_fill_image_2d_array);

> --

> 2.1.4

> 

> _______________________________________________

> Beignet mailing list

> Beignet@lists.freedesktop.org

> http://lists.freedesktop.org/mailman/listinfo/beignet