[llvm] r242510 - [asan] Fix invalid debug info for promotable allocas

Kuba Brecka kuba.brecka at gmail.com
Thu Jul 16 23:29:58 PDT 2015


Author: kuba.brecka
Date: Fri Jul 17 01:29:57 2015
New Revision: 242510

URL: http://llvm.org/viewvc/llvm-project?rev=242510&view=rev
Log:
[asan] Fix invalid debug info for promotable allocas

Since r230724 ("Skip promotable allocas to improve performance at -O0"), there is a regression in the generated debug info for those non-instrumented variables. When inspecting such a variable's value in LLDB, you often get garbage instead of the actual value. ASan instrumentation is inserted before the creation of the non-instrumented alloca. The only allocas that are considered standard stack variables are the ones declared in the first basic-block, but the initial instrumentation setup in the function breaks that invariant.

This patch makes sure uninstrumented allocas stay in the first BB.

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


Added:
    llvm/trunk/test/Instrumentation/AddressSanitizer/debug_info_noninstrumented_alloca.ll
Modified:
    llvm/trunk/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Modified: llvm/trunk/lib/Transforms/Instrumentation/AddressSanitizer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/AddressSanitizer.cpp?rev=242510&r1=242509&r2=242510&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Instrumentation/AddressSanitizer.cpp (original)
+++ llvm/trunk/lib/Transforms/Instrumentation/AddressSanitizer.cpp Fri Jul 17 01:29:57 2015
@@ -525,6 +525,7 @@ struct FunctionStackPoisoner : public In
   ShadowMapping Mapping;
 
   SmallVector<AllocaInst *, 16> AllocaVec;
+  SmallVector<AllocaInst *, 16> NonInstrumentedStaticAllocaVec;
   SmallVector<Instruction *, 8> RetVec;
   unsigned StackAlignment;
 
@@ -625,7 +626,10 @@ struct FunctionStackPoisoner : public In
 
   /// \brief Collect Alloca instructions we want (and can) handle.
   void visitAllocaInst(AllocaInst &AI) {
-    if (!ASan.isInterestingAlloca(AI)) return;
+    if (!ASan.isInterestingAlloca(AI)) {
+      if (AI.isStaticAlloca()) NonInstrumentedStaticAllocaVec.push_back(&AI);
+      return;
+    }
 
     StackAlignment = std::max(StackAlignment, AI.getAlignment());
     if (ASan.isDynamicAlloca(AI))
@@ -1734,6 +1738,8 @@ void FunctionStackPoisoner::poisonStack(
   IRBuilder<> IRB(InsBefore);
   IRB.SetCurrentDebugLocation(EntryDebugLocation);
 
+  for (auto *AI : NonInstrumentedStaticAllocaVec) AI->moveBefore(InsBefore);
+
   SmallVector<ASanStackVariableDescription, 16> SVD;
   SVD.reserve(AllocaVec.size());
   for (AllocaInst *AI : AllocaVec) {

Added: llvm/trunk/test/Instrumentation/AddressSanitizer/debug_info_noninstrumented_alloca.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Instrumentation/AddressSanitizer/debug_info_noninstrumented_alloca.ll?rev=242510&view=auto
==============================================================================
--- llvm/trunk/test/Instrumentation/AddressSanitizer/debug_info_noninstrumented_alloca.ll (added)
+++ llvm/trunk/test/Instrumentation/AddressSanitizer/debug_info_noninstrumented_alloca.ll Fri Jul 17 01:29:57 2015
@@ -0,0 +1,26 @@
+; This test checks that non-instrumented allocas stay in the first basic block.
+; Only first-basic-block allocas are considered stack slots, and moving them
+; breaks debug info.
+
+; RUN: opt < %s -asan -asan-module -S | FileCheck %s
+
+target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-apple-macosx10.10.0"
+
+define i32 @foo() sanitize_address {
+entry:
+  ; Regular alloca, will get instrumented (forced by the ptrtoint below).
+  %instrumented = alloca i32, align 4
+
+  ; Won't be instrumented because of asan-skip-promotable-allocas.
+  %non_instrumented = alloca i32, align 4
+  store i32 0, i32* %non_instrumented, align 4
+  %value = load i32, i32* %non_instrumented, align 4
+
+  %ptr = ptrtoint i32* %instrumented to i64
+  ret i32 %value
+}
+
+; CHECK: entry:
+; CHECK: %non_instrumented = alloca i32, align 4
+; CHECK: load i32, i32* @__asan_option_detect_stack_use_after_return





More information about the llvm-commits mailing list