From: Kostya Serebryany Date: Wed, 24 Sep 2014 22:41:55 +0000 (+0000) Subject: [asan] don't instrument module CTORs that may be run before asan.module_ctor. This... X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=0e9d1148656105a17365494044a0a82d547ee76f;p=oota-llvm.git [asan] don't instrument module CTORs that may be run before asan.module_ctor. This fixes asan running together -coverage git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@218421 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/lib/Transforms/Instrumentation/AddressSanitizer.cpp index 83b64606985..03b80cc50d5 100644 --- a/lib/Transforms/Instrumentation/AddressSanitizer.cpp +++ b/lib/Transforms/Instrumentation/AddressSanitizer.cpp @@ -71,7 +71,7 @@ static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E; static const char *const kAsanModuleCtorName = "asan.module_ctor"; static const char *const kAsanModuleDtorName = "asan.module_dtor"; -static const int kAsanCtorAndDtorPriority = 1; +static const uint64_t kAsanCtorAndDtorPriority = 1; static const char *const kAsanReportErrorTemplate = "__asan_report_"; static const char *const kAsanReportLoadN = "__asan_report_load_n"; static const char *const kAsanReportStoreN = "__asan_report_store_n"; @@ -928,10 +928,12 @@ void AddressSanitizerModule::createInitializerPoisonCalls( ConstantStruct *CS = cast(OP); // Must have a function or null ptr. - // (CS->getOperand(0) is the init priority.) if (Function* F = dyn_cast(CS->getOperand(1))) { - if (F->getName() != kAsanModuleCtorName) - poisonOneInitializer(*F, ModuleName); + if (F->getName() == kAsanModuleCtorName) continue; + ConstantInt *Priority = dyn_cast(CS->getOperand(0)); + // Don't instrument CTORs that will run before asan.module_ctor. + if (Priority->getLimitedValue() <= kAsanCtorAndDtorPriority) continue; + poisonOneInitializer(*F, ModuleName); } } } diff --git a/test/Instrumentation/AddressSanitizer/instrument_initializer_metadata.ll b/test/Instrumentation/AddressSanitizer/instrument_initializer_metadata.ll index c119879351a..c2bb0aa845c 100644 --- a/test/Instrumentation/AddressSanitizer/instrument_initializer_metadata.ll +++ b/test/Instrumentation/AddressSanitizer/instrument_initializer_metadata.ll @@ -25,29 +25,39 @@ entry: ret void } -@llvm.global_ctors = appending global [1 x { i32, void ()* }] [{ i32, void ()* } { i32 65535, void ()* @_GLOBAL__I_a }] +@llvm.global_ctors = appending global [2 x { i32, void ()* }] [{ i32, void ()* } { i32 65535, void ()* @__late_ctor }, { i32, void ()* } { i32 0, void ()* @__early_ctor }] -define internal void @_GLOBAL__I_a() sanitize_address section ".text.startup" { +define internal void @__late_ctor() sanitize_address section ".text.startup" { entry: call void @__cxx_global_var_init() ret void } ; Clang indicated that @xxx was dynamically initailized. -; __asan_{before,after}_dynamic_init should be called from _GLOBAL__I_a +; __asan_{before,after}_dynamic_init should be called from __late_ctor -; CHECK: define internal void @_GLOBAL__I_a +; CHECK-LABEL: define internal void @__late_ctor ; CHECK-NOT: ret ; CHECK: call void @__asan_before_dynamic_init ; CHECK: call void @__cxx_global_var_init ; CHECK: call void @__asan_after_dynamic_init ; CHECK: ret +; CTOR with priority 0 should not be instrumented. +define internal void @__early_ctor() sanitize_address section ".text.startup" { +entry: + call void @__cxx_global_var_init() + ret void +} +; CHECK-LABEL: define internal void @__early_ctor +; CHECK-NOT: __asan +; CHECK: ret + ; Check that xxx is instrumented. define void @touch_xxx() sanitize_address { store i32 0, i32 *@xxx, align 4 ret void -; CHECK: define void @touch_xxx +; CHECK-LABEL: touch_xxx ; CHECK: call void @__asan_report_store4 ; CHECK: ret void }