Index: lib/Driver/SanitizerArgs.cpp =================================================================== --- lib/Driver/SanitizerArgs.cpp +++ lib/Driver/SanitizerArgs.cpp @@ -618,6 +618,29 @@ CmdArgs.push_back(Args.MakeArgString(LinkerOptionFlag)); } +// Check if ASan should use GC-friendly instrumentation for globals. +// First of all, there is no point if -fdata-sections is off (expect for MachO, +// where this is not a factor). Also, on ELF this feature requires an assembler +// extension that only works with -integrated-as at the moment. +static bool useAsanGlobalsGC(const ToolChain &TC, + const llvm::opt::ArgList &Args) { + bool IntegratedAs = TC.useIntegratedAs(); + bool DataSections = + Args.hasFlag(options::OPT_fdata_sections, options::OPT_fno_data_sections, + tools::isUseSeparateSections(TC.getTriple())); + + switch (TC.getTriple().getObjectFormat()) { + case llvm::Triple::MachO: + return true; + case llvm::Triple::COFF: + return DataSections; + case llvm::Triple::ELF: + return DataSections && IntegratedAs; + default: + return false; + } +} + void SanitizerArgs::addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, types::ID InputType) const { @@ -745,6 +768,11 @@ Sanitizers.Mask & CFIClasses) << "-fvisibility="; } + + if (Sanitizers.has(Address) && !useAsanGlobalsGC(TC, Args)) { + CmdArgs.push_back("-mllvm"); + CmdArgs.push_back("-asan-globals-live-support=0"); + } } SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A, Index: test/Driver/asan-gc.c =================================================================== --- /dev/null +++ test/Driver/asan-gc.c @@ -0,0 +1,14 @@ +// RUN: %clang -target x86_64-linux-gnu -fsanitize=address %s -### 2>&1 | FileCheck %s --check-prefix=WITHOUT-GC +// RUN: %clang -target x86_64-linux-gnu -fsanitize=address -fdata-sections %s -### 2>&1 | FileCheck %s --check-prefix=WITH-GC +// RUN: %clang -target x86_64-linux-gnu -fsanitize=address -fno-data-sections -fdata-sections %s -### 2>&1 | FileCheck %s --check-prefix=WITH-GC +// RUN: %clang -target x86_64-linux-gnu -fsanitize=address -fno-integrated-as -fdata-sections %s -### 2>&1 | FileCheck %s --check-prefix=WITHOUT-GC + +// RUN: %clang -target x86_64-apple-macosx10.11.0 -fsanitize=address %s -### 2>&1 | FileCheck %s --check-prefix=WITH-GC +// RUN: %clang -target x86_64-apple-macosx10.11.0 -fsanitize=address -fno-integrated-as %s -### 2>&1 | FileCheck %s --check-prefix=WITH-GC + +// RUN: %clang -target x86_64-pc-windows-msvc19.0.24215 -fsanitize=address %s -### 2>&1 | FileCheck %s --check-prefix=WITHOUT-GC +// RUN: %clang -target x86_64-pc-windows-msvc19.0.24215 -fsanitize=address -fdata-sections %s -### 2>&1 | FileCheck %s --check-prefix=WITH-GC + + +// WITHOUT-GC: "-mllvm" "-asan-globals-live-support=0" +// WITH-GC-NOT: -asan-globals-live-support=0