summaryrefslogtreecommitdiff
path: root/backend
diff options
context:
space:
mode:
authorJunyan He <junyan.he@linux.intel.com>2015-01-12 13:55:22 +0800
committerZhigang Gong <zhigang.gong@intel.com>2015-01-13 14:33:08 +0800
commit62ad21c61dd0440d017f3f2e54c8ed79df6c667c (patch)
treee5295285ff156273827f366b418e7f5230d7cc4d /backend
parent8702b08890e1ad3bb5d9b93430684ec4025a7c06 (diff)
Fix the printf buffer size bug.
We can not know the accurate size of the printf buffer size before run the kernel. Sometimes, especially when the global work items size is huge, the output buffer is not enough and the print message logic will cause the segment fault. We increase the printf buffer to 16M at most and add out of range check to avoid crash. Signed-off-by: Junyan He <junyan.he@linux.intel.com> Signed-off-by: Zhigang Gong <zhigang.gong@intel.com> Reviewed-by: Zhigang Gong <zhigang.gong@linux.intel.com>
Diffstat (limited to 'backend')
-rw-r--r--backend/src/backend/program.cpp5
-rw-r--r--backend/src/backend/program.h2
-rw-r--r--backend/src/backend/program.hpp4
-rw-r--r--backend/src/ir/printf.cpp16
-rw-r--r--backend/src/ir/printf.hpp2
5 files changed, 17 insertions, 12 deletions
diff --git a/backend/src/backend/program.cpp b/backend/src/backend/program.cpp
index b22e4a6a..38ce9c83 100644
--- a/backend/src/backend/program.cpp
+++ b/backend/src/backend/program.cpp
@@ -1068,12 +1068,13 @@ namespace gbe {
static void kernelOutputPrintf(void * printf_info, void* index_addr,
void* buf_addr, size_t global_wk_sz0,
- size_t global_wk_sz1, size_t global_wk_sz2)
+ size_t global_wk_sz1, size_t global_wk_sz2,
+ size_t output_sz)
{
if (printf_info == NULL) return;
ir::PrintfSet *ps = (ir::PrintfSet *)printf_info;
ps->outputPrintf(index_addr, buf_addr, global_wk_sz0,
- global_wk_sz1, global_wk_sz2);
+ global_wk_sz1, global_wk_sz2, output_sz);
}
static void kernelGetCompileWorkGroupSize(gbe_kernel gbeKernel, size_t wg_size[3]) {
diff --git a/backend/src/backend/program.h b/backend/src/backend/program.h
index 6acc0102..dc5662fc 100644
--- a/backend/src/backend/program.h
+++ b/backend/src/backend/program.h
@@ -157,7 +157,7 @@ typedef uint32_t (gbe_get_printf_sizeof_size_cb)(void* printf_info);
extern gbe_get_printf_sizeof_size_cb *gbe_get_printf_sizeof_size;
typedef void (gbe_output_printf_cb) (void* printf_info, void* index_addr, void* buf_addr,
- size_t global_wk_sz0, size_t global_wk_sz1, size_t global_wk_sz2);
+ size_t global_wk_sz0, size_t global_wk_sz1, size_t global_wk_sz2, size_t outbuf_sz);
extern gbe_output_printf_cb* gbe_output_printf;
/*! Create a new program from the given source code (zero terminated string) */
diff --git a/backend/src/backend/program.hpp b/backend/src/backend/program.hpp
index 66f90aa8..cff2463c 100644
--- a/backend/src/backend/program.hpp
+++ b/backend/src/backend/program.hpp
@@ -155,10 +155,10 @@ namespace gbe {
}
void outputPrintf(void* index_addr, void* buf_addr, size_t global_wk_sz0,
- size_t global_wk_sz1, size_t global_wk_sz2) {
+ size_t global_wk_sz1, size_t global_wk_sz2, size_t output_sz) {
if(printfSet)
printfSet->outputPrintf(index_addr, buf_addr, global_wk_sz0,
- global_wk_sz1, global_wk_sz2);
+ global_wk_sz1, global_wk_sz2, output_sz);
}
ir::FunctionArgument::InfoFromLLVM* getArgInfo(uint32_t id) const { return &args[id].info; }
diff --git a/backend/src/ir/printf.cpp b/backend/src/ir/printf.cpp
index 0e9f81c0..fa108dc1 100644
--- a/backend/src/ir/printf.cpp
+++ b/backend/src/ir/printf.cpp
@@ -105,16 +105,20 @@ namespace gbe
#define PRINT_SOMETHING(target_ty, conv) do { \
if (!vec_i) \
pf_str = pf_str + std::string(#conv); \
- printf(pf_str.c_str(), \
- ((target_ty *)((char *)buf_addr + sizeOfSize * global_wk_sz0 * global_wk_sz1 * global_wk_sz2 * n \
- + slot.state->out_buf_sizeof_offset * \
- global_wk_sz0 * global_wk_sz1 * global_wk_sz2)) \
- [(k*global_wk_sz0*global_wk_sz1 + j*global_wk_sz0 + i) * vec_num + vec_i]);\
+ char *ptr = ((char *)buf_addr + sizeOfSize * global_wk_sz0 * global_wk_sz1 * global_wk_sz2 * n \
+ + slot.state->out_buf_sizeof_offset * \
+ global_wk_sz0 * global_wk_sz1 * global_wk_sz2); \
+ target_ty* obj_ptr = ((target_ty *)ptr) + (k*global_wk_sz0*global_wk_sz1 + j*global_wk_sz0 + i) * vec_num + vec_i; \
+ if ((char *)obj_ptr + sizeof(target_ty) > (char *)buf_addr + output_sz) { \
+ printf("\n\n!!!The printf message is out of range because of the limited buffer, ignore.\n"); \
+ return; \
+ } \
+ printf(pf_str.c_str(), *obj_ptr); \
} while (0)
void PrintfSet::outputPrintf(void* index_addr, void* buf_addr, size_t global_wk_sz0,
- size_t global_wk_sz1, size_t global_wk_sz2)
+ size_t global_wk_sz1, size_t global_wk_sz2, size_t output_sz)
{
LockOutput lock;
size_t i, j, k;
diff --git a/backend/src/ir/printf.hpp b/backend/src/ir/printf.hpp
index cc1f8dc0..f6c6bcfa 100644
--- a/backend/src/ir/printf.hpp
+++ b/backend/src/ir/printf.hpp
@@ -253,7 +253,7 @@ namespace gbe
}
void outputPrintf(void* index_addr, void* buf_addr, size_t global_wk_sz0,
- size_t global_wk_sz1, size_t global_wk_sz2);
+ size_t global_wk_sz1, size_t global_wk_sz2, size_t output_sz);
private:
vector<PrintfFmt> fmts;