[PATCH] D43669: [cfi] Lazy CFI initialization

Vitaly Buka via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 22 19:29:06 PST 2018


vitalybuka created this revision.
vitalybuka added reviewers: pcc, eugenis.

If SANITIZER_CAN_USE_PREINIT_ARRAY=0 interceptors or cfi callbacks can be called
before constructor.


https://reviews.llvm.org/D43669

Files:
  compiler-rt/lib/cfi/cfi.cc


Index: compiler-rt/lib/cfi/cfi.cc
===================================================================
--- compiler-rt/lib/cfi/cfi.cc
+++ compiler-rt/lib/cfi/cfi.cc
@@ -367,14 +367,26 @@
 
 using namespace __cfi;
 
+static bool cfi_inited;
+extern "C" void __cfi_init();
+
+#define ENSURE_CFI_INITED()                                                    \
+  do {                                                                         \
+    if (UNLIKELY(!cfi_inited)) {                                               \
+      __cfi_init();                                                            \
+    }                                                                          \
+  } while (0)
+
 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void
 __cfi_slowpath(u64 CallSiteTypeId, void *Ptr) {
+  ENSURE_CFI_INITED();
   CfiSlowPathCommon(CallSiteTypeId, Ptr, nullptr);
 }
 
 #ifdef CFI_ENABLE_DIAG
 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void
 __cfi_slowpath_diag(u64 CallSiteTypeId, void *Ptr, void *DiagData) {
+  ENSURE_CFI_INITED();
   CfiSlowPathCommon(CallSiteTypeId, Ptr, DiagData);
 }
 #endif
@@ -388,25 +400,33 @@
 // We could insert a high-priority constructor into the library, but that would
 // not help with the uninstrumented libraries.
 INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
+  ENSURE_CFI_INITED();
   EnterLoader();
   void *handle = REAL(dlopen)(filename, flag);
   ExitLoader();
   return handle;
 }
 
 INTERCEPTOR(int, dlclose, void *handle) {
+  ENSURE_CFI_INITED();
   EnterLoader();
   int res = REAL(dlclose)(handle);
   ExitLoader();
   return res;
 }
 
+BlockingMutex cfi_init_lock(LINKER_INITIALIZED);
+
 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
 #if !SANITIZER_CAN_USE_PREINIT_ARRAY
 // On ELF platforms, the constructor is invoked using .preinit_array (see below)
 __attribute__((constructor(0)))
 #endif
 void __cfi_init() {
+  BlockingMutexLock lock(&cfi_init_lock);
+  if (cfi_inited)
+    return;
+  cfi_inited = true;
   SanitizerToolName = "CFI";
   InitializeFlags();
   InitShadow();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43669.135585.patch
Type: text/x-patch
Size: 2048 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180223/3770a6cc/attachment.bin>


More information about the llvm-commits mailing list