summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLu Guanqun <guanqun.lu@intel.com>2013-01-10 09:19:09 +0800
committerZhigang Gong <zhigang.gong@linux.intel.com>2013-04-10 14:51:31 +0800
commitbf493ee1b0c9466624368d07760f5194e6f50ab6 (patch)
tree3b160b953ee837d831676961f219f99a967e3557
parentf43a42815477ce158073de1f8459ecc199239f17 (diff)
add check for memory allocation size
When it exceeds the max allocation size, it should fail and return CL_INVALID_BUFFER_SIZE. Signed-off-by: Lu Guanqun <guanqun.lu@intel.com> Reviewed-by: Zhigang Gong <zhigang.gong@linux.intel.com>
-rw-r--r--src/cl_mem.c11
-rw-r--r--utests/CMakeLists.txt1
-rw-r--r--utests/cl_create_kernel.cpp16
3 files changed, 27 insertions, 1 deletions
diff --git a/src/cl_mem.c b/src/cl_mem.c
index 1beb0c3a..c67d6ce8 100644
--- a/src/cl_mem.c
+++ b/src/cl_mem.c
@@ -41,13 +41,22 @@ cl_mem_allocate(cl_context ctx,
cl_mem mem = NULL;
cl_int err = CL_SUCCESS;
size_t alignment = 64;
+ cl_ulong max_mem_size;
assert(ctx);
FATAL_IF (flags & CL_MEM_ALLOC_HOST_PTR,
"CL_MEM_ALLOC_HOST_PTR unsupported"); /* XXX */
FATAL_IF (flags & CL_MEM_USE_HOST_PTR,
"CL_MEM_USE_HOST_PTR unsupported"); /* XXX */
- if (UNLIKELY(sz == 0)) {
+
+ if ((err = cl_get_device_info(ctx->device,
+ CL_DEVICE_MAX_MEM_ALLOC_SIZE,
+ sizeof(max_mem_size),
+ &max_mem_size,
+ NULL)) != CL_SUCCESS) {
+ goto error;
+ }
+ if (UNLIKELY(sz == 0 || sz > max_mem_size)) {
err = CL_INVALID_BUFFER_SIZE;
goto error;
}
diff --git a/utests/CMakeLists.txt b/utests/CMakeLists.txt
index 0c136ee8..8bdd72ea 100644
--- a/utests/CMakeLists.txt
+++ b/utests/CMakeLists.txt
@@ -3,6 +3,7 @@ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}
link_directories (${LLVM_LIBRARY_DIR})
ADD_LIBRARY(utests SHARED
+ cl_create_kernel.cpp
utest_error.c
compiler_shader_toy.cpp
compiler_mandelbrot.cpp
diff --git a/utests/cl_create_kernel.cpp b/utests/cl_create_kernel.cpp
new file mode 100644
index 00000000..36a7c382
--- /dev/null
+++ b/utests/cl_create_kernel.cpp
@@ -0,0 +1,16 @@
+#include "utest_helper.hpp"
+
+static void test_create_kernel(void)
+{
+ cl_ulong max_mem_size;
+ cl_int status;
+
+ OCL_CALL(clGetDeviceInfo, device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof(max_mem_size), &max_mem_size, NULL);
+ OCL_ASSERT(max_mem_size < (cl_ulong)-1);
+ // increment the size so that following clCreateBuffer() would fail.
+ ++max_mem_size;
+ buf[0] = clCreateBuffer(ctx, 0, max_mem_size, NULL, &status);
+ OCL_ASSERT(status == CL_INVALID_BUFFER_SIZE);
+}
+
+MAKE_UTEST_FROM_FUNCTION(test_create_kernel);