Skip to content

Commit b7fae46

Browse files
committed
Revert "Revert "[asan] Default to -fsanitize-address-use-odr-indicator for non-Windows""
This reverts commit 3ec0b90.
1 parent 78fdcbb commit b7fae46

File tree

14 files changed

+49
-44
lines changed

14 files changed

+49
-44
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2309,7 +2309,7 @@ defm sanitize_address_globals_dead_stripping : BoolOption<"f", "sanitize-address
23092309
NegFlag<SetFalse, [], [ClangOption], "Disable linker dead stripping of globals in AddressSanitizer">>,
23102310
Group<f_clang_Group>;
23112311
defm sanitize_address_use_odr_indicator : BoolOption<"f", "sanitize-address-use-odr-indicator",
2312-
CodeGenOpts<"SanitizeAddressUseOdrIndicator">, DefaultFalse,
2312+
CodeGenOpts<"SanitizeAddressUseOdrIndicator">, DefaultTrue,
23132313
PosFlag<SetTrue, [], [ClangOption], "Enable ODR indicator globals to avoid false ODR violation"
23142314
" reports in partially sanitized programs at the cost of an increase in binary size">,
23152315
NegFlag<SetFalse, [], [ClangOption], "Disable ODR indicator globals">>,

clang/lib/Driver/SanitizerArgs.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -967,10 +967,14 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
967967
options::OPT_fsanitize_address_globals_dead_stripping,
968968
options::OPT_fno_sanitize_address_globals_dead_stripping, true);
969969

970+
// Enable ODR indicators which allow better handling of mixed instrumented
971+
// and uninstrumented globals. Disable them for Windows where weak odr
972+
// indicators (.weak.__odr_asan_gen*) may cause multiple definition linker
973+
// errors in the absence of -lldmingw.
970974
AsanUseOdrIndicator =
971975
Args.hasFlag(options::OPT_fsanitize_address_use_odr_indicator,
972976
options::OPT_fno_sanitize_address_use_odr_indicator,
973-
AsanUseOdrIndicator);
977+
!TC.getTriple().isOSWindows());
974978

975979
if (AllAddedKinds & SanitizerKind::PointerCompare & ~AllRemove) {
976980
AsanInvalidPointerCmp = true;
@@ -1292,8 +1296,8 @@ void SanitizerArgs::addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
12921296
if (AsanGlobalsDeadStripping)
12931297
CmdArgs.push_back("-fsanitize-address-globals-dead-stripping");
12941298

1295-
if (AsanUseOdrIndicator)
1296-
CmdArgs.push_back("-fsanitize-address-use-odr-indicator");
1299+
if (!AsanUseOdrIndicator)
1300+
CmdArgs.push_back("-fno-sanitize-address-use-odr-indicator");
12971301

12981302
if (AsanInvalidPointerCmp) {
12991303
CmdArgs.push_back("-mllvm");

clang/test/CodeGen/asan-globals-odr.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
// RUN: %clang_cc1 -fsanitize=address -emit-llvm -o - -triple x86_64-linux %s | FileCheck %s --check-prefixes=CHECK,INDICATOR0,GLOB_VAR,ALIAS0
2-
// RUN: %clang_cc1 -fsanitize=address -fsanitize-address-use-odr-indicator -emit-llvm -o - -triple x86_64-linux %s | FileCheck %s --check-prefixes=CHECK,INDICATOR1,GLOB_ALIAS_INDICATOR,ALIAS1
1+
// RUN: %clang_cc1 -fsanitize=address -emit-llvm -o - -triple x86_64-linux %s | FileCheck %s --check-prefixes=CHECK,INDICATOR1,GLOB_ALIAS_INDICATOR,ALIAS1
32
// RUN: %clang_cc1 -fsanitize=address -fno-sanitize-address-use-odr-indicator -emit-llvm -o - -triple x86_64-linux %s | FileCheck %s --check-prefixes=CHECK,INDICATOR0,GLOB_VAR,ALIAS0
43
// RUN: %clang_cc1 -fsanitize=address -fno-sanitize-address-use-odr-indicator -fsanitize-address-use-odr-indicator -emit-llvm -o - -triple x86_64-linux %s | FileCheck %s --check-prefixes=CHECK,INDICATOR1,GLOB_ALIAS_INDICATOR,ALIAS1
54
// RUN: %clang_cc1 -fsanitize=address -fsanitize-address-use-odr-indicator -fno-sanitize-address-use-odr-indicator -emit-llvm -o - -triple x86_64-linux %s | FileCheck %s --check-prefixes=CHECK,INDICATOR0,GLOB_VAR,ALIAS0
65

76
// No alias on Windows but indicators should work.
8-
// RUN: %clang_cc1 -fsanitize=address -emit-llvm -o - -triple x86_64-windows-msvc %s | FileCheck %s --check-prefixes=CHECK,GLOB_VAR,ALIAS0
9-
// RUN: %clang_cc1 -fsanitize=address -fsanitize-address-use-odr-indicator -emit-llvm -o - -triple x86_64-windows-msvc %s | FileCheck %s --check-prefixes=CHECK,INDICATOR1,GLOB_VAR_INDICATOR,ALIAS0
7+
// RUN: %clang_cc1 -fsanitize=address -fno-sanitize-address-use-odr-indicator -emit-llvm -o - -triple x86_64-windows-msvc %s | FileCheck %s --check-prefixes=CHECK,GLOB_VAR,ALIAS0
8+
// RUN: %clang_cc1 -fsanitize=address -emit-llvm -o - -triple x86_64-windows-msvc %s | FileCheck %s --check-prefixes=CHECK,INDICATOR1,GLOB_VAR_INDICATOR,ALIAS0
109

1110
int global;
1211

clang/test/CodeGen/asan-static-odr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// RUN: %clang_cc1 -fsanitize=address -emit-llvm -o - -triple x86_64-linux %s | FileCheck %s
1+
// RUN: %clang_cc1 -fsanitize=address -fno-sanitize-address-use-odr-indicator -emit-llvm -o - -triple x86_64-linux %s | FileCheck %s
22

33
// No alias on Windows but indicators should work.
4-
// RUN: %clang_cc1 -fsanitize=address -emit-llvm -o - -triple x86_64-windows-msvc %s | FileCheck %s
4+
// RUN: %clang_cc1 -fsanitize=address -fno-sanitize-address-use-odr-indicator -emit-llvm -o - -triple x86_64-windows-msvc %s | FileCheck %s
55

66
static int global;
77

clang/test/Driver/fsanitize.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -263,23 +263,18 @@
263263
// RUN: FileCheck %s --check-prefix=CHECK-NO-CHECK-ASAN-CALLBACK
264264
// CHECK-NO-CHECK-ASAN-CALLBACK-NOT: "-mllvm" "-asan-instrumentation-with-call-threshold=0"
265265

266-
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=address -fsanitize-address-use-odr-indicator %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ASAN-ODR-INDICATOR
267-
// RUN: %clang_cl --target=x86_64-windows -fsanitize=address -fsanitize-address-use-odr-indicator -### -- %s 2>&1 | FileCheck %s --check-prefix=CHECK-ASAN-ODR-INDICATOR
268-
// CHECK-ASAN-ODR-INDICATOR: -cc1{{.*}}-fsanitize-address-use-odr-indicator
266+
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=address %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ASAN-ODR-INDICATOR
267+
// RUN: %clang_cl --target=x86_64-windows -fsanitize=address -### -- %s 2>&1 | FileCheck %s --check-prefix=CHECK-ASAN-ODR-INDICATOR-OFF
268+
// CHECK-ASAN-ODR-INDICATOR-NOT: "-fsanitize-address-use-odr-indicator"
269269

270270
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=address -fno-sanitize-address-use-odr-indicator %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ASAN-ODR-INDICATOR-OFF
271271
// RUN: %clang_cl --target=x86_64-windows -fsanitize=address -fno-sanitize-address-use-odr-indicator -### -- %s 2>&1 | FileCheck %s --check-prefix=CHECK-ASAN-ODR-INDICATOR-OFF
272-
// CHECK-ASAN-ODR-INDICATOR-OFF-NOT: -cc1{{.*}}address-generate-odr-globals
272+
// CHECK-ASAN-ODR-INDICATOR-OFF: "-cc1" {{.*}} "-fno-sanitize-address-use-odr-indicator"
273273

274-
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=address -fno-sanitize-address-use-odr-indicator -fsanitize-address-use-odr-indicator %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ASAN-ODR-INDICATOR-BOTH
275-
// RUN: %clang_cl --target=x86_64-windows -fsanitize=address -fno-sanitize-address-use-odr-indicator -fsanitize-address-use-odr-indicator -### -- %s 2>&1 | FileCheck %s --check-prefix=CHECK-ASAN-ODR-INDICATOR-BOTH
276-
// CHECK-ASAN-ODR-INDICATOR-BOTH: -cc1{{.*}}-fsanitize-address-use-odr-indicator
274+
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=address -fno-sanitize-address-use-odr-indicator -fsanitize-address-use-odr-indicator %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ASAN-ODR-INDICATOR
275+
// RUN: %clang_cl --target=x86_64-windows -fsanitize=address -fno-sanitize-address-use-odr-indicator -fsanitize-address-use-odr-indicator -### -- %s 2>&1 | FileCheck %s --check-prefix=CHECK-ASAN-ODR-INDICATOR
277276

278-
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=address -fsanitize-address-use-odr-indicator -fno-sanitize-address-use-odr-indicator %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ASAN-ODR-INDICATOR-BOTH-OFF
279-
// CHECK-ASAN-ODR-INDICATOR-BOTH-OFF-NOT: -cc1{{.*}}address-generate-odr-globals
280-
281-
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=address %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ASAN-WITHOUT-ODR-INDICATOR
282-
// CHECK-ASAN-WITHOUT-ODR-INDICATOR-NOT: -cc1{{.*}}address-generate-odr-globals
277+
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=address -fsanitize-address-use-odr-indicator -fno-sanitize-address-use-odr-indicator %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ASAN-ODR-INDICATOR-OFF
283278

284279
// RUN: %clang --target=x86_64-linux-gnu -fsanitize-memory-track-origins -pie %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ONLY-TRACK-ORIGINS
285280
// CHECK-ONLY-TRACK-ORIGINS: warning: argument unused during compilation: '-fsanitize-memory-track-origins'

compiler-rt/test/asan/TestCases/Linux/odr-violation.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@
66
// We use fast_unwind_on_malloc=0 to have full unwinding even w/o frame
77
// pointers. This setting is not on by default because it's too expensive.
88
//
9+
// Note, -asan-use-private-alias=1 -asan-use-odr-indicator=1 is the default.
10+
// -fno-sanitize-address-use-odr-indicator turns off both.
11+
//
912
// Different size: detect a bug if detect_odr_violation>=1
10-
// RUN: %clangxx_asan -g -DBUILD_SO=1 -fPIC -shared %s -o %dynamiclib
11-
// RUN: %clangxx_asan -g %s %ld_flags_rpath_exe -o %t-ODR-EXE
13+
// RUN: %clangxx_asan -g -DBUILD_SO=1 -fPIC -shared -fno-sanitize-address-use-odr-indicator %s -o %dynamiclib
14+
// RUN: %clangxx_asan -g -fno-sanitize-address-use-odr-indicator %s %ld_flags_rpath_exe -o %t-ODR-EXE
1215
// RUN: %env_asan_opts=fast_unwind_on_malloc=0:detect_odr_violation=1 not %run %t-ODR-EXE 2>&1 | FileCheck %s
1316
// RUN: %env_asan_opts=fast_unwind_on_malloc=0:detect_odr_violation=2 not %run %t-ODR-EXE 2>&1 | FileCheck %s
1417
// RUN: %env_asan_opts=fast_unwind_on_malloc=0:detect_odr_violation=0 %run %t-ODR-EXE 2>&1 | FileCheck %s --check-prefix=DISABLED
1518
// RUN: %env_asan_opts=fast_unwind_on_malloc=0 not %run %t-ODR-EXE 2>&1 | FileCheck %s
1619
//
1720
// Same size: report a bug only if detect_odr_violation>=2.
18-
// RUN: %clangxx_asan -g -DBUILD_SO=1 -fPIC -shared %s -o %dynamiclib -DSZ=100
21+
// RUN: %clangxx_asan -g -DBUILD_SO=1 -fPIC -shared -fno-sanitize-address-use-odr-indicator %s -o %dynamiclib -DSZ=100
1922
// RUN: %env_asan_opts=fast_unwind_on_malloc=0:detect_odr_violation=1 %run %t-ODR-EXE 2>&1 | FileCheck %s --check-prefix=DISABLED
2023
// RUN: %env_asan_opts=fast_unwind_on_malloc=0:detect_odr_violation=2 not %run %t-ODR-EXE 2>&1 | FileCheck %s
2124
// RUN: %env_asan_opts=fast_unwind_on_malloc=0 not %run %t-ODR-EXE 2>&1 | FileCheck %s
@@ -26,13 +29,13 @@
2629
// RUN: rm -f %t.supp
2730
//
2831
// Use private aliases for global variables without indicator symbol.
29-
// RUN: %clangxx_asan -g -DBUILD_SO=1 -fPIC -shared -mllvm -asan-use-private-alias %s -o %dynamiclib -DSZ=100
30-
// RUN: %clangxx_asan -g -mllvm -asan-use-private-alias %s %ld_flags_rpath_exe -o %t-ODR-EXE
32+
// RUN: %clangxx_asan -g -DBUILD_SO=1 -fPIC -shared -mllvm -asan-use-odr-indicator=0 %s -o %dynamiclib -DSZ=100
33+
// RUN: %clangxx_asan -g -mllvm -asan-use-odr-indicator=0 %s %ld_flags_rpath_exe -o %t-ODR-EXE
3134
// RUN: %env_asan_opts=fast_unwind_on_malloc=0 %run %t-ODR-EXE 2>&1 | FileCheck %s --check-prefix=DISABLED
3235

3336
// Use private aliases for global variables: use indicator symbol to detect ODR violation.
34-
// RUN: %clangxx_asan -g -DBUILD_SO=1 -fPIC -shared -mllvm -asan-use-private-alias -mllvm -asan-use-odr-indicator %s -o %dynamiclib -DSZ=100
35-
// RUN: %clangxx_asan -g -mllvm -asan-use-private-alias -mllvm -asan-use-odr-indicator %s %ld_flags_rpath_exe -o %t-ODR-EXE
37+
// RUN: %clangxx_asan -g -DBUILD_SO=1 -fPIC -shared %s -o %dynamiclib -DSZ=100
38+
// RUN: %clangxx_asan -g %s %ld_flags_rpath_exe -o %t-ODR-EXE
3639
// RUN: %env_asan_opts=fast_unwind_on_malloc=0 not %run %t-ODR-EXE 2>&1 | FileCheck %s
3740

3841
// Same as above but with clang switches.

compiler-rt/test/asan/TestCases/Linux/odr_indicators.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clangxx_asan -fPIC %s -o %t
1+
// RUN: %clangxx_asan -fno-sanitize-address-use-odr-indicator -fPIC %s -o %t
22
// RUN: %env_asan_opts=report_globals=2 %run %t 2>&1 | FileCheck %s --check-prefixes=CHECK,INDICATOR0
33

44
// RUN: %clangxx_asan -fsanitize-address-use-odr-indicator -fPIC %s -o %t

llvm/include/llvm/Transforms/Instrumentation/AddressSanitizer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct AddressSanitizerOptions {
3636
class AddressSanitizerPass : public PassInfoMixin<AddressSanitizerPass> {
3737
public:
3838
AddressSanitizerPass(const AddressSanitizerOptions &Options,
39-
bool UseGlobalGC = true, bool UseOdrIndicator = false,
39+
bool UseGlobalGC = true, bool UseOdrIndicator = true,
4040
AsanDtorKind DestructorKind = AsanDtorKind::Global,
4141
AsanCtorKind ConstructorKind = AsanCtorKind::Global);
4242
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,12 +404,12 @@ static cl::opt<uint32_t> ClForceExperiment(
404404
static cl::opt<bool>
405405
ClUsePrivateAlias("asan-use-private-alias",
406406
cl::desc("Use private aliases for global variables"),
407-
cl::Hidden, cl::init(false));
407+
cl::Hidden, cl::init(true));
408408

409409
static cl::opt<bool>
410410
ClUseOdrIndicator("asan-use-odr-indicator",
411411
cl::desc("Use odr indicators to improve ODR reporting"),
412-
cl::Hidden, cl::init(false));
412+
cl::Hidden, cl::init(true));
413413

414414
static cl::opt<bool>
415415
ClUseGlobalsGC("asan-globals-live-support",
@@ -780,16 +780,20 @@ class ModuleAddressSanitizer {
780780
public:
781781
ModuleAddressSanitizer(Module &M, bool CompileKernel = false,
782782
bool Recover = false, bool UseGlobalsGC = true,
783-
bool UseOdrIndicator = false,
783+
bool UseOdrIndicator = true,
784784
AsanDtorKind DestructorKind = AsanDtorKind::Global,
785785
AsanCtorKind ConstructorKind = AsanCtorKind::Global)
786786
: CompileKernel(ClEnableKasan.getNumOccurrences() > 0 ? ClEnableKasan
787787
: CompileKernel),
788788
Recover(ClRecover.getNumOccurrences() > 0 ? ClRecover : Recover),
789789
UseGlobalsGC(UseGlobalsGC && ClUseGlobalsGC && !this->CompileKernel),
790790
// Enable aliases as they should have no downside with ODR indicators.
791-
UsePrivateAlias(UseOdrIndicator || ClUsePrivateAlias),
792-
UseOdrIndicator(UseOdrIndicator || ClUseOdrIndicator),
791+
UsePrivateAlias(ClUsePrivateAlias.getNumOccurrences() > 0
792+
? ClUsePrivateAlias
793+
: UseOdrIndicator),
794+
UseOdrIndicator(ClUseOdrIndicator.getNumOccurrences() > 0
795+
? ClUseOdrIndicator
796+
: UseOdrIndicator),
793797
// Not a typo: ClWithComdat is almost completely pointless without
794798
// ClUseGlobalsGC (because then it only works on modules without
795799
// globals, which are rare); it is a prerequisite for ClUseGlobalsGC;

llvm/test/Instrumentation/AddressSanitizer/global_metadata.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ target triple = "x86_64-unknown-linux-gnu"
1616

1717
; Check that globals were instrumented:
1818

19-
; CHECK: @global = global { i32, [28 x i8] } zeroinitializer, align 32
20-
; CHECK: @.str = internal constant { [14 x i8], [18 x i8] } { [14 x i8] c"Hello, world!\00", [18 x i8] zeroinitializer }, align 32
19+
; CHECK: @global = global { i32, [28 x i8] } zeroinitializer, comdat, align 32
20+
; CHECK: @.str = internal constant { [14 x i8], [18 x i8] } { [14 x i8] c"Hello, world!\00", [18 x i8] zeroinitializer }, comdat({{.*}}), align 32
2121

2222
; Check emitted location descriptions:
2323
; CHECK: [[VARNAME:@___asan_gen_.[0-9]+]] = private unnamed_addr constant [7 x i8] c"global\00", align 1

0 commit comments

Comments
 (0)