From f317b6e2b1865092d808718af7c1c8671624d7dd Mon Sep 17 00:00:00 2001 From: Vedant Kumar Date: Wed, 22 Jun 2016 17:30:58 +0000 Subject: [PATCH] [asan] Do not instrument accesses to profiling globals It's only useful to asan-itize profiling globals while debugging llvm's profiling instrumentation passes. Enabling asan along with instrprof or gcov instrumentation shouldn't incur extra overhead. This patch is in the same spirit as r264805 and r273202, which disabled tsan instrumentation of instrprof/gcov globals. Differential Revision: http://reviews.llvm.org/D21541 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@273444 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Instrumentation/AddressSanitizer.cpp | 19 ++++++++++++++----- .../do-not-instrument-profiling-globals.ll | 9 +++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 test/Instrumentation/AddressSanitizer/do-not-instrument-profiling-globals.ll diff --git a/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/lib/Transforms/Instrumentation/AddressSanitizer.cpp index 396e23b88ce..33693eb5417 100644 --- a/lib/Transforms/Instrumentation/AddressSanitizer.cpp +++ b/lib/Transforms/Instrumentation/AddressSanitizer.cpp @@ -857,10 +857,19 @@ static GlobalVariable *createPrivateGlobalForSourceLoc(Module &M, return GV; } -static bool GlobalWasGeneratedByAsan(GlobalVariable *G) { - return G->getName().startswith(kAsanGenPrefix) || - G->getName().startswith(kSanCovGenPrefix) || - G->getName().startswith(kODRGenPrefix); +/// \brief Check if \p G has been created by a trusted compiler pass. +static bool GlobalWasGeneratedByCompiler(GlobalVariable *G) { + // Do not instrument asan globals. + if (G->getName().startswith(kAsanGenPrefix) || + G->getName().startswith(kSanCovGenPrefix) || + G->getName().startswith(kODRGenPrefix)) + return true; + + // Do not instrument gcov counter arrays. + if (G->getName() == "__llvm_gcov_ctr") + return true; + + return false; } Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) { @@ -1243,7 +1252,7 @@ bool AddressSanitizerModule::ShouldInstrumentGlobal(GlobalVariable *G) { if (GlobalsMD.get(G).IsBlacklisted) return false; if (!Ty->isSized()) return false; if (!G->hasInitializer()) return false; - if (GlobalWasGeneratedByAsan(G)) return false; // Our own global. + if (GlobalWasGeneratedByCompiler(G)) return false; // Our own globals. // Touch only those globals that will not be defined in other modules. // Don't handle ODR linkage types and COMDATs since other modules may be built // without ASan. diff --git a/test/Instrumentation/AddressSanitizer/do-not-instrument-profiling-globals.ll b/test/Instrumentation/AddressSanitizer/do-not-instrument-profiling-globals.ll new file mode 100644 index 00000000000..82fb2f0d2f7 --- /dev/null +++ b/test/Instrumentation/AddressSanitizer/do-not-instrument-profiling-globals.ll @@ -0,0 +1,9 @@ +; This test checks that we don't instrument globals created by profiling passes. +; RUN: opt < %s -asan -asan-module -S | FileCheck %s + +@__profc_test = private global [1 x i64] zeroinitializer, section "__DATA,__llvm_prf_cnts", align 8 +@__llvm_gcov_ctr = internal global [1 x i64] zeroinitializer + +; CHECK-DAG: @asan.module_ctor +; CHECK-NOT: @__asan_gen{{.*}}__llvm_gcov_ctr +; CHECK-NOT: @__asan_gen{{.*}}__profc_test -- 2.50.1