Index: compiler-rt/test/asan/TestCases/Linux/odr-violation.cc =================================================================== --- compiler-rt/test/asan/TestCases/Linux/odr-violation.cc +++ compiler-rt/test/asan/TestCases/Linux/odr-violation.cc @@ -23,9 +23,16 @@ // RUN: %env_asan_opts=fast_unwind_on_malloc=0:detect_odr_violation=2:suppressions=%t.supp %run %t-ODR-EXE 2>&1 | FileCheck %s --check-prefix=DISABLED // RUN: rm -f %t.supp // -// Use private aliases for global variables: use indicator symbol to detect ODR violation. +// Use private aliases for global variables without indicator symbol. // RUN: %clangxx_asan -DBUILD_SO=1 -fPIC -shared -mllvm -asan-use-private-alias %s -o %t-ODR-SO.so -DSZ=100 // RUN: %clangxx_asan -mllvm -asan-use-private-alias %s %t-ODR-SO.so -Wl,-R. -o %t-ODR-EXE +// RUN: %env_asan_opts=fast_unwind_on_malloc=0 %run %t-ODR-EXE 2>&1 | FileCheck %s --check-prefix=DISABLED +// RUN: %env_asan_opts=fast_unwind_on_malloc=0:use_odr_indicator=false %run %t-ODR-EXE 2>&1 | FileCheck %s --check-prefix=DISABLED +// RUN: %env_asan_opts=fast_unwind_on_malloc=0:use_odr_indicator=true %run %t-ODR-EXE 2>&1 | FileCheck %s --check-prefix=DISABLED + +// Use private aliases for global variables: use indicator symbol to detect ODR violation. +// RUN: %clangxx_asan -DBUILD_SO=1 -fPIC -shared -mllvm -asan-use-private-alias -mllvm -asan-use-odr-indicator %s -o %t-ODR-SO.so -DSZ=100 +// RUN: %clangxx_asan -mllvm -asan-use-private-alias -mllvm -asan-use-odr-indicator %s %t-ODR-SO.so -Wl,-R. -o %t-ODR-EXE // RUN: %env_asan_opts=fast_unwind_on_malloc=0 %run %t-ODR-EXE 2>&1 | FileCheck %s --check-prefix=DISABLED // RUN: %env_asan_opts=fast_unwind_on_malloc=0:use_odr_indicator=false %run %t-ODR-EXE 2>&1 | FileCheck %s --check-prefix=DISABLED // RUN: %env_asan_opts=fast_unwind_on_malloc=0:use_odr_indicator=true not %run %t-ODR-EXE 2>&1 | FileCheck %s Index: llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp =================================================================== --- llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp +++ llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp @@ -344,10 +344,14 @@ cl::init(0)); static cl::opt - ClUsePrivateAliasForGlobals("asan-use-private-alias", - cl::desc("Use private aliases for global" - " variables"), - cl::Hidden, cl::init(false)); + ClUsePrivateAlias("asan-use-private-alias", + cl::desc("Use private aliases for global variables"), + cl::Hidden, cl::init(false)); + +static cl::opt + ClUseOdrIndicator("asan-use-odr-indicator", + cl::desc("Use odr indicators to improve ODR reporting"), + cl::Hidden, cl::init(false)); static cl::opt ClUseGlobalsGC("asan-globals-live-support", @@ -2173,12 +2177,14 @@ bool CanUsePrivateAliases = TargetTriple.isOSBinFormatELF() || TargetTriple.isOSBinFormatMachO() || TargetTriple.isOSBinFormatWasm(); - if (CanUsePrivateAliases && ClUsePrivateAliasForGlobals) { + if (CanUsePrivateAliases && ClUsePrivateAlias) { // Create local alias for NewGlobal to avoid crash on ODR between // instrumented and non-instrumented libraries. - auto *GA = + InstrumentedGlobal = GlobalAlias::create(GlobalValue::PrivateLinkage, "", NewGlobal); + } + if (ClUseOdrIndicator) { // With local aliases, we need to provide another externally visible // symbol __odr_asan_XXX to detect ODR violation. auto *ODRIndicatorSym = @@ -2192,7 +2198,6 @@ ODRIndicatorSym->setDLLStorageClass(NewGlobal->getDLLStorageClass()); ODRIndicatorSym->setAlignment(1); ODRIndicator = ODRIndicatorSym; - InstrumentedGlobal = GA; } Constant *Initializer = ConstantStruct::get( Index: llvm/test/Instrumentation/AddressSanitizer/local_alias.ll =================================================================== --- llvm/test/Instrumentation/AddressSanitizer/local_alias.ll +++ llvm/test/Instrumentation/AddressSanitizer/local_alias.ll @@ -1,4 +1,7 @@ -; RUN: opt < %s -asan -asan-module -asan-use-private-alias=1 -S | FileCheck %s +; RUN: opt < %s -asan -asan-module -S | FileCheck %s --check-prefixes=CHECK-NOALIAS,CHECK-NOINDICATOR +; RUN: opt < %s -asan -asan-module -asan-use-private-alias=1 -S | FileCheck %s --check-prefixes=CHECK-ALIAS,CHECK-NOINDICATOR +; RUN: opt < %s -asan -asan-module -asan-use-odr-indicator=1 -S | FileCheck %s --check-prefixes=CHECK-INDICATOR,CHECK-NOALIAS +; RUN: opt < %s -asan -asan-module -asan-use-private-alias=1 -asan-use-odr-indicator=1 -S | FileCheck %s --check-prefixes=CHECK-ALIAS,CHECK-INDICATOR target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu" @@ -6,8 +9,10 @@ @a = internal global [2 x i32] zeroinitializer, align 4 ; Check that we generate internal alias and odr indicator symbols for global to be protected. -; CHECK: @__odr_asan_gen_a = internal global i8 0, align 1 -; CHECK: @0 = private alias { [2 x i32], [56 x i8] }, { [2 x i32], [56 x i8] }* @a +; CHECK-NOINDICATOR-NOT: __odr_asan_gen_a +; CHECK-noALIAS: private alias +; CHECK-INDICATOR: @__odr_asan_gen_a = internal global i8 0, align 1 +; CHECK-ALIAS: @0 = private alias { [2 x i32], [56 x i8] }, { [2 x i32], [56 x i8] }* @a ; Function Attrs: nounwind sanitize_address uwtable define i32 @foo(i32 %M) #0 {