Index: lib/Transforms/Instrumentation/AddressSanitizer.cpp =================================================================== --- lib/Transforms/Instrumentation/AddressSanitizer.cpp +++ lib/Transforms/Instrumentation/AddressSanitizer.cpp @@ -2191,7 +2191,16 @@ return Version; } +static bool isAlreadyAsanInstrumented(const Module &M) { + // Looks for a function inserted by the second (module) pass of + // ASAN, to detect modules fully ASAN instrumented. + return M.getFunction(kAsanPoisonGlobalsName) != nullptr; +} + bool AddressSanitizerModule::runOnModule(Module &M) { + // Skip modules which already have ASan instrumentation. + if (isAlreadyAsanInstrumented(M)) + return false; C = &(M.getContext()); int LongSize = M.getDataLayout().getPointerSizeInBits(); IntptrTy = Type::getIntNTy(*C, LongSize); @@ -2314,6 +2323,10 @@ // virtual bool AddressSanitizer::doInitialization(Module &M) { + // Skip modules which already have ASan instrumentation. + if (isAlreadyAsanInstrumented(M)) + return false; + // Initialize the private fields. No one has accessed them before. GlobalsMD.init(M); @@ -2405,6 +2418,9 @@ } bool AddressSanitizer::runOnFunction(Function &F) { + // Skip modules which already have ASan instrumentation. + if (isAlreadyAsanInstrumented(*F.getParent())) + return false; if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage) return false; if (!ClDebugFunc.empty() && ClDebugFunc == F.getName()) return false; if (F.getName().startswith("__asan_")) return false; Index: test/Instrumentation/AddressSanitizer/skip-previously-instrumented.ll =================================================================== --- /dev/null +++ test/Instrumentation/AddressSanitizer/skip-previously-instrumented.ll @@ -0,0 +1,15 @@ +; Test to ensure previously-instrumented IR is handled gracefully +; (ASAN skipped and not aborted). +; RUN: opt < %s -asan -asan-module -S -o %t.ll +; RUN: opt < %t.ll -asan -asan-module -S -o %t2.ll +; RUN: diff %t.ll %t2.ll +source_filename = "hello.c" +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +@.str = private unnamed_addr constant [13 x i8] c"hello world\0A\00", align 1 + +!llvm.asan.globals = !{!0} + +!0 = !{[13 x i8]* @.str, !1, !"", i1 false, i1 false} +!1 = !{!"hello.c", i32 5, i32 12}