[compiler-rt] r271714 - [esan] Specify which tool via a global variable

Derek Bruening via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 3 12:40:09 PDT 2016


Author: bruening
Date: Fri Jun  3 14:40:08 2016
New Revision: 271714

URL: http://llvm.org/viewvc/llvm-project?rev=271714&view=rev
Log:
[esan] Specify which tool via a global variable

Summary:
Adds a global variable to specify the tool, to support handling early
interceptors that invoke instrumented code, thus requiring shadow memory to
be initialized prior to __esan_init() being invoked.

Reviewers: aizatsky

Subscribers: vitalybuka, zhaoqin, kcc, eugenis, llvm-commits, kubabrecka

Differential Revision: http://reviews.llvm.org/D20974

Modified:
    compiler-rt/trunk/lib/esan/esan.cpp
    compiler-rt/trunk/lib/esan/esan.h
    compiler-rt/trunk/lib/esan/esan_interface.cpp
    compiler-rt/trunk/lib/esan/esan_interface_internal.h

Modified: compiler-rt/trunk/lib/esan/esan.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/esan/esan.cpp?rev=271714&r1=271713&r2=271714&view=diff
==============================================================================
--- compiler-rt/trunk/lib/esan/esan.cpp (original)
+++ compiler-rt/trunk/lib/esan/esan.cpp Fri Jun  3 14:40:08 2016
@@ -30,7 +30,6 @@ extern void __cxa_atexit(void (*function
 namespace __esan {
 
 bool EsanIsInitialized;
-ToolType WhichTool;
 ShadowMapping Mapping;
 
 // Different tools use different scales within the same shadow mapping scheme.
@@ -65,22 +64,22 @@ static const uptr ShadowScale[] = {
 void processRangeAccess(uptr PC, uptr Addr, int Size, bool IsWrite) {
   VPrintf(3, "in esan::%s %p: %c %p %d\n", __FUNCTION__, PC,
           IsWrite ? 'w' : 'r', Addr, Size);
-  if (WhichTool == ESAN_CacheFrag) {
+  if (__esan_which_tool == ESAN_CacheFrag) {
     // TODO(bruening): add shadow mapping and update shadow bits here.
     // We'll move this to cache_frag.cpp once we have something.
-  } else if (WhichTool == ESAN_WorkingSet) {
+  } else if (__esan_which_tool == ESAN_WorkingSet) {
     processRangeAccessWorkingSet(PC, Addr, Size, IsWrite);
   }
 }
 
 bool processSignal(int SigNum, void (*Handler)(int), void (**Result)(int)) {
-  if (WhichTool == ESAN_WorkingSet)
+  if (__esan_which_tool == ESAN_WorkingSet)
     return processWorkingSetSignal(SigNum, Handler, Result);
   return true;
 }
 
 bool processSigaction(int SigNum, const void *Act, void *OldAct) {
-  if (WhichTool == ESAN_WorkingSet)
+  if (__esan_which_tool == ESAN_WorkingSet)
     return processWorkingSetSigaction(SigNum, Act, OldAct);
   return true;
 }
@@ -140,7 +139,7 @@ static void initializeShadow() {
 
   DCHECK(verifyShadowScheme());
 
-  Mapping.initialize(ShadowScale[WhichTool]);
+  Mapping.initialize(ShadowScale[__esan_which_tool]);
 
   VPrintf(1, "Shadow scale=%d offset=%p\n", Mapping.Scale, Mapping.Offset);
 
@@ -150,7 +149,7 @@ static void initializeShadow() {
             (ShadowEnd - ShadowStart) >> 30);
 
     uptr Map;
-    if (WhichTool == ESAN_WorkingSet) {
+    if (__esan_which_tool == ESAN_WorkingSet) {
       // We want to identify all shadow pages that are touched so we start
       // out inaccessible.
       Map = (uptr)MmapFixedNoAccess(ShadowStart, ShadowEnd- ShadowStart,
@@ -176,10 +175,9 @@ static void initializeShadow() {
 void initializeLibrary(ToolType Tool) {
   // We assume there is only one thread during init.
   if (EsanIsInitialized) {
-    CHECK(Tool == WhichTool);
+    CHECK(Tool == __esan_which_tool);
     return;
   }
-  WhichTool = Tool;
   SanitizerToolName = "EfficiencySanitizer";
   CacheBinaryName();
   initializeFlags();
@@ -190,17 +188,17 @@ void initializeLibrary(ToolType Tool) {
   ::__cxa_atexit((void (*)())finalizeLibrary);
 
   VPrintf(1, "in esan::%s\n", __FUNCTION__);
-  if (WhichTool <= ESAN_None || WhichTool >= ESAN_Max) {
-    Printf("ERROR: unknown tool %d requested\n", WhichTool);
+  if (__esan_which_tool <= ESAN_None || __esan_which_tool >= ESAN_Max) {
+    Printf("ERROR: unknown tool %d requested\n", __esan_which_tool);
     Die();
   }
 
   initializeShadow();
   initializeInterceptors();
 
-  if (WhichTool == ESAN_CacheFrag) {
+  if (__esan_which_tool == ESAN_CacheFrag) {
     initializeCacheFrag();
-  } else if (WhichTool == ESAN_WorkingSet) {
+  } else if (__esan_which_tool == ESAN_WorkingSet) {
     initializeWorkingSet();
   }
 
@@ -209,9 +207,9 @@ void initializeLibrary(ToolType Tool) {
 
 int finalizeLibrary() {
   VPrintf(1, "in esan::%s\n", __FUNCTION__);
-  if (WhichTool == ESAN_CacheFrag) {
+  if (__esan_which_tool == ESAN_CacheFrag) {
     return finalizeCacheFrag();
-  } else if (WhichTool == ESAN_WorkingSet) {
+  } else if (__esan_which_tool == ESAN_WorkingSet) {
     return finalizeWorkingSet();
   }
   return 0;
@@ -219,7 +217,7 @@ int finalizeLibrary() {
 
 void processCompilationUnitInit(void *Ptr) {
   VPrintf(2, "in esan::%s\n", __FUNCTION__);
-  if (WhichTool == ESAN_CacheFrag) {
+  if (__esan_which_tool == ESAN_CacheFrag) {
     DCHECK(Ptr != nullptr);
     processCacheFragCompilationUnitInit(Ptr);
   } else {
@@ -231,7 +229,7 @@ void processCompilationUnitInit(void *Pt
 // For the main executable module, this is called after finalizeLibrary.
 void processCompilationUnitExit(void *Ptr) {
   VPrintf(2, "in esan::%s\n", __FUNCTION__);
-  if (WhichTool == ESAN_CacheFrag) {
+  if (__esan_which_tool == ESAN_CacheFrag) {
     DCHECK(Ptr != nullptr);
     processCacheFragCompilationUnitExit(Ptr);
   } else {

Modified: compiler-rt/trunk/lib/esan/esan.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/esan/esan.h?rev=271714&r1=271713&r2=271714&view=diff
==============================================================================
--- compiler-rt/trunk/lib/esan/esan.h (original)
+++ compiler-rt/trunk/lib/esan/esan.h Fri Jun  3 14:40:08 2016
@@ -34,8 +34,6 @@ namespace __esan {
 
 extern bool EsanIsInitialized;
 
-extern ToolType WhichTool;
-
 void initializeLibrary(ToolType Tool);
 int finalizeLibrary();
 // Esan creates the variable per tool per compilation unit at compile time

Modified: compiler-rt/trunk/lib/esan/esan_interface.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/esan/esan_interface.cpp?rev=271714&r1=271713&r2=271714&view=diff
==============================================================================
--- compiler-rt/trunk/lib/esan/esan_interface.cpp (original)
+++ compiler-rt/trunk/lib/esan/esan_interface.cpp Fri Jun  3 14:40:08 2016
@@ -18,7 +18,10 @@
 using namespace __esan; // NOLINT
 
 void __esan_init(ToolType Tool, void *Ptr) {
-  WhichTool = Tool;
+  if (Tool != __esan_which_tool) {
+    Printf("ERROR: tool mismatch: %d vs %d\n", Tool, __esan_which_tool);
+    Die();
+  }
   initializeLibrary(Tool);
   processCompilationUnitInit(Ptr);
 }

Modified: compiler-rt/trunk/lib/esan/esan_interface_internal.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/esan/esan_interface_internal.h?rev=271714&r1=271713&r2=271714&view=diff
==============================================================================
--- compiler-rt/trunk/lib/esan/esan_interface_internal.h (original)
+++ compiler-rt/trunk/lib/esan/esan_interface_internal.h Fri Jun  3 14:40:08 2016
@@ -32,6 +32,11 @@ typedef enum Type : u32 {
   ESAN_Max,
 } ToolType;
 
+// To handle interceptors that invoke instrumented code prior to
+// __esan_init() being called, the instrumentation module creates this
+// global variable specifying the tool.
+extern ToolType __esan_which_tool;
+
 // This function should be called at the very beginning of the process,
 // before any instrumented code is executed and before any call to malloc.
 SANITIZER_INTERFACE_ATTRIBUTE void __esan_init(ToolType Tool, void *Ptr);




More information about the llvm-commits mailing list