[Beignet] [PATCH 2/2] Runtime: fix caffe segmentation fault when exit.

Yang Rong rong.r.yang at intel.com
Mon Jun 20 16:15:29 UTC 2016


For static object release, such as context, program, may called after
delete static variable gbeLoader, so when in gbeLoader's destructor
function, set the release relative functions to NULL, and when call
these functions, add a check.

This patch could fix caffe's segmentation fault when exit

Signed-off-by: Yang Rong <rong.r.yang at intel.com>
---
 src/cl_gbe_loader.cpp | 5 +++++
 src/cl_program.c      | 8 ++++++--
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/cl_gbe_loader.cpp b/src/cl_gbe_loader.cpp
index 2c7ef1e..aa13a3d 100644
--- a/src/cl_gbe_loader.cpp
+++ b/src/cl_gbe_loader.cpp
@@ -320,6 +320,11 @@ struct GbeLoaderInitializer
 
     if (dlhInterp != NULL)
       dlclose(dlhInterp);
+
+    //When destroy, set the release relative functions
+    //to NULL to avoid dangling pointer visit.
+    compiler_program_clean_llvm_resource = NULL;
+    interp_program_delete = NULL;
   }
 
   bool compilerLoaded;
diff --git a/src/cl_program.c b/src/cl_program.c
index 93eba03..75262db 100644
--- a/src/cl_program.c
+++ b/src/cl_program.c
@@ -112,8 +112,12 @@ cl_program_delete(cl_program p)
   /* Free the program as allocated by the compiler */
   if (p->opaque) {
     if (CompilerSupported())
-      compiler_program_clean_llvm_resource(p->opaque);
-    interp_program_delete(p->opaque);
+      //For static variables release, gbeLoader may have been released, so
+      //compiler_program_clean_llvm_resource and interp_program_delete may be NULL.
+      if(compiler_program_clean_llvm_resource)
+        compiler_program_clean_llvm_resource(p->opaque);
+    if(interp_program_delete)
+      interp_program_delete(p->opaque);
   }
 
   p->magic = CL_MAGIC_DEAD_HEADER; /* For safety */
-- 
2.1.4



More information about the Beignet mailing list