summaryrefslogtreecommitdiff
path: root/utests/compiler_mixed_pointer.cpp
diff options
context:
space:
mode:
authorRuiling Song <ruiling.song@intel.com>2014-07-30 13:59:29 +0800
committerZhigang Gong <zhigang.gong@intel.com>2014-07-30 15:23:43 +0800
commite16f34c027b3e7a19355c91d728632d82995b2b0 (patch)
tree188e10aea38d79b2c8fee9e2c91b569bcf383880 /utests/compiler_mixed_pointer.cpp
parent778f56447172c737eceb8aaccca3d2801f242152 (diff)
downloadbeignet-e16f34c027b3e7a19355c91d728632d82995b2b0.tar.gz
GBE: Refine bti usage in backend & runtime.
Previously, we simply map 2G surface for memory access, which has obvious security issue, user can easily read/write graphics memory that does not belong to him. To prevent such kind of behaviour, We bind each surface to a dedicated bti. HW provides automatic bounds check. For out-of-bound write, it will be ignored. And for read out-of-bound, hardware will simply return zero value. The idea behind the patch is for a load/store instruction, it will search through the LLVM use-def chain until finding out where the address comes from. Then the bti is saved in ir::Instruction and used for the later code generation. And for mixed pointer case, a load/store will access more than one bti. To simplify some code, '0' is reserved for constant address space, '1' is reserved for private address space. Other btis are assigned automatically by backend. Signed-off-by: Ruiling Song <ruiling.song@intel.com> Reviewed-by: Zhigang Gong <zhigang.gong@linux.intel.com>
Diffstat (limited to 'utests/compiler_mixed_pointer.cpp')
-rw-r--r--utests/compiler_mixed_pointer.cpp119
1 files changed, 119 insertions, 0 deletions
diff --git a/utests/compiler_mixed_pointer.cpp b/utests/compiler_mixed_pointer.cpp
new file mode 100644
index 00000000..9531fb29
--- /dev/null
+++ b/utests/compiler_mixed_pointer.cpp
@@ -0,0 +1,119 @@
+#include "utest_helper.hpp"
+
+static void cpu(int global_id, int *src1, int *src2, int *dst) {
+ int * tmp = NULL;
+
+ switch(global_id) {
+ case 0:
+ case 1:
+ case 4:
+ tmp = src1;
+ break;
+ default:
+ tmp = src2;
+ break;
+ }
+ dst[global_id] = tmp[global_id];
+
+}
+static void cpu1(int global_id, int *src, int *dst1, int *dst2) {
+ int * tmp = global_id < 5 ? dst1 : dst2;
+ tmp[global_id] = src[global_id];
+}
+
+void compiler_mixed_pointer(void)
+{
+ const size_t n = 16;
+ int cpu_dst[16], cpu_src[16], cpu_src1[16];
+
+ // Setup kernel and buffers
+ OCL_CREATE_KERNEL("compiler_mixed_pointer");
+ OCL_CREATE_BUFFER(buf[0], 0, n * sizeof(uint32_t), NULL);
+ OCL_CREATE_BUFFER(buf[1], 0, n * sizeof(uint32_t), NULL);
+ OCL_CREATE_BUFFER(buf[2], 0, n * sizeof(uint32_t), NULL);
+ OCL_SET_ARG(0, sizeof(cl_mem), &buf[0]);
+ OCL_SET_ARG(1, sizeof(cl_mem), &buf[1]);
+ OCL_SET_ARG(2, sizeof(cl_mem), &buf[2]);
+ globals[0] = 16;
+ locals[0] = 16;
+
+ // Run random tests
+ for (uint32_t pass = 0; pass < 1; ++pass) {
+ OCL_MAP_BUFFER(0);
+ OCL_MAP_BUFFER(1);
+ for (int32_t i = 0; i < (int32_t) n; ++i) {
+ cpu_src[i] = ((int32_t*)buf_data[0])[i] = i;
+ cpu_src1[i] = ((int32_t*)buf_data[1])[i] = 65536-i;
+ }
+ OCL_UNMAP_BUFFER(0);
+ OCL_UNMAP_BUFFER(1);
+
+ // Run the kernel on GPU
+ OCL_NDRANGE(1);
+
+ // Run on CPU
+ for (int32_t i = 0; i <(int32_t) n; ++i) cpu(i, cpu_src, cpu_src1, cpu_dst);
+
+ // Compare
+ OCL_MAP_BUFFER(2);
+ for (size_t i = 0; i < n; ++i) {
+// printf(" %d %d\n", cpu_dst[i], ((int32_t*)buf_data[2])[i]);
+ OCL_ASSERT(((int32_t*)buf_data[2])[i] == cpu_dst[i]);
+ }
+ OCL_UNMAP_BUFFER(2);
+ }
+}
+
+MAKE_UTEST_FROM_FUNCTION(compiler_mixed_pointer);
+
+void compiler_mixed_pointer1(void)
+{
+ const size_t n = 16;
+ int cpu_dst1[16], cpu_dst2[16], cpu_src[16];
+
+ // Setup kernel and buffers
+ OCL_CREATE_KERNEL_FROM_FILE("compiler_mixed_pointer", "compiler_mixed_pointer1");
+ OCL_CREATE_BUFFER(buf[0], 0, n * sizeof(uint32_t), NULL);
+ OCL_CREATE_BUFFER(buf[1], 0, n * sizeof(uint32_t), NULL);
+ OCL_CREATE_BUFFER(buf[2], 0, n * sizeof(uint32_t), NULL);
+ OCL_SET_ARG(0, sizeof(cl_mem), &buf[0]);
+ OCL_SET_ARG(1, sizeof(cl_mem), &buf[1]);
+ OCL_SET_ARG(2, sizeof(cl_mem), &buf[2]);
+ globals[0] = 16;
+ locals[0] = 16;
+
+ // Run random tests
+ for (uint32_t pass = 0; pass < 1; ++pass) {
+ OCL_MAP_BUFFER(0);
+ OCL_MAP_BUFFER(1);
+ OCL_MAP_BUFFER(2);
+ for (int32_t i = 0; i < (int32_t) n; ++i) {
+ cpu_src[i] = ((int32_t*)buf_data[0])[i] = i;
+ cpu_dst1[i] = ((int32_t*)buf_data[1])[i] = 0xff;
+ cpu_dst2[i] = ((int32_t*)buf_data[2])[i] = 0xff;
+ }
+ OCL_UNMAP_BUFFER(0);
+ OCL_UNMAP_BUFFER(1);
+ OCL_UNMAP_BUFFER(2);
+
+ // Run the kernel on GPU
+ OCL_NDRANGE(1);
+
+ // Run on CPU
+ for (int32_t i = 0; i <(int32_t) n; ++i) cpu1(i, cpu_src, cpu_dst1, cpu_dst2);
+
+ // Compare
+ OCL_MAP_BUFFER(1);
+ OCL_MAP_BUFFER(2);
+ for (size_t i = 0; i < n; ++i) {
+// printf(" %d %d\n", cpu_dst1[i], ((int32_t*)buf_data[1])[i]);
+// printf(" %d %d\n", ((int32_t*)buf_data[2])[i], cpu_dst2[i]);
+ OCL_ASSERT(((int32_t*)buf_data[1])[i] == cpu_dst1[i]);
+ OCL_ASSERT(((int32_t*)buf_data[2])[i] == cpu_dst2[i]);
+ }
+ OCL_UNMAP_BUFFER(1);
+ OCL_UNMAP_BUFFER(2);
+ }
+}
+
+MAKE_UTEST_FROM_FUNCTION(compiler_mixed_pointer1);