[PATCH] D32895: [ASAN] Insert call to __asan_init and load of dynamic shadow address in correct order

Eugene Leviant via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri May 5 03:08:44 PDT 2017


evgeny777 created this revision.
evgeny777 added a project: Sanitizers.

Good time of the day!

I've ported ASAN to proprietary system, where I (besides everything else) need to instrument application startup code. To accomplish this task I've added check for appropriate function name and triple in maybeInsertAsanInitAtFunctionEntry(). However I got following IR after that:

  define void @_startup() #0 {
  entry:
    %0 = load i64, i64* @__asan_shadow_memory_dynamic_address
    call void @__asan_init()
    .....

Needless to say that it doesn't work as expected, because __asan_shadow_memory_dynamic_address is initialized in __asan_init().
This patch fixes the problem for me, by enforcing correct order of operations.
I don't know how to write unit test for it, so any suggestions/comments are appreciated.

Thanks.


Repository:
  rL LLVM

https://reviews.llvm.org/D32895

Files:
  lib/Transforms/Instrumentation/AddressSanitizer.cpp


Index: lib/Transforms/Instrumentation/AddressSanitizer.cpp
===================================================================
--- lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -2242,16 +2242,11 @@
   if (!ClDebugFunc.empty() && ClDebugFunc == F.getName()) return false;
   if (F.getName().startswith("__asan_")) return false;
 
-  bool FunctionModified = false;
-
-  // If needed, insert __asan_init before checking for SanitizeAddress attr.
-  // This function needs to be called even if the function body is not
-  // instrumented.  
-  if (maybeInsertAsanInitAtFunctionEntry(F))
-    FunctionModified = true;
-  
   // Leave if the function doesn't need instrumentation.
-  if (!F.hasFnAttribute(Attribute::SanitizeAddress)) return FunctionModified;
+  // The __asan_init function needs to be called even if the function body is
+  // not instrumented.
+  if (!F.hasFnAttribute(Attribute::SanitizeAddress))
+    return maybeInsertAsanInitAtFunctionEntry(F);
 
   DEBUG(dbgs() << "ASAN instrumenting:\n" << F << "\n");
 
@@ -2261,6 +2256,7 @@
   FunctionStateRAII CleanupObj(this);
 
   maybeInsertDynamicShadowAtFunctionEntry(F);
+  bool FunctionModified = maybeInsertAsanInitAtFunctionEntry(F);
 
   // We can't instrument allocas used with llvm.localescape. Only static allocas
   // can be passed to that intrinsic.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32895.97920.patch
Type: text/x-patch
Size: 1388 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170505/16547bb8/attachment.bin>


More information about the llvm-commits mailing list