diff options
author | George Karpenkov <ekarpenkov@apple.com> | 2018-07-20 23:34:39 +0000 |
---|---|---|
committer | George Karpenkov <ekarpenkov@apple.com> | 2018-07-20 23:34:39 +0000 |
commit | c86f04a1f2a8f98425fdfc362cb5084f17d08881 (patch) | |
tree | cb557788b1e37ef8005b123807c17693296bc451 /lib/Driver/ToolChains/Darwin.cpp | |
parent | 67428b72eda39d9b712a128e5c6f3a6c9d6cc264 (diff) |
[Driver] Sanitizer support based on runtime library presence
The runtime libraries of sanitizers are built in compiler-rt, and Clang
can be built without compiler-rt, or compiler-rt can be configured to
only build certain sanitizers. The driver should provide reasonable
diagnostics and not a link-time error when a runtime library is missing.
This patch changes the driver for OS X to only support sanitizers of
which we can find the runtime libraries. The discussion for this patch
explains the rationale
Differential Revision: https://reviews.llvm.org/D15225
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@337635 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Driver/ToolChains/Darwin.cpp')
-rw-r--r-- | lib/Driver/ToolChains/Darwin.cpp | 73 |
1 files changed, 51 insertions, 22 deletions
diff --git a/lib/Driver/ToolChains/Darwin.cpp b/lib/Driver/ToolChains/Darwin.cpp index 9205dd52de..95ec8d64c2 100644 --- a/lib/Driver/ToolChains/Darwin.cpp +++ b/lib/Driver/ToolChains/Darwin.cpp @@ -916,13 +916,26 @@ unsigned DarwinClang::GetDefaultDwarfVersion() const { return 4; } +SmallString<128> MachO::runtimeLibDir(bool IsEmbedded) const { + SmallString<128> Dir(getDriver().ResourceDir); + llvm::sys::path::append( + Dir, "lib", IsEmbedded ? "macho_embedded" : "darwin"); + return Dir; +} + +std::string Darwin::getFileNameForSanitizerLib(StringRef SanitizerName, + bool Shared) const { + return (Twine("libclang_rt.") + SanitizerName + "_" + + getOSLibraryNameSuffix() + + (Shared ? "_dynamic.dylib" : ".a")).str(); + +} + void MachO::AddLinkRuntimeLib(const ArgList &Args, ArgStringList &CmdArgs, StringRef DarwinLibName, RuntimeLinkOptions Opts) const { - SmallString<128> Dir(getDriver().ResourceDir); - llvm::sys::path::append( - Dir, "lib", (Opts & RLO_IsEmbedded) ? "macho_embedded" : "darwin"); + SmallString<128> Dir = runtimeLibDir(Opts & RLO_IsEmbedded); SmallString<128> P(Dir); llvm::sys::path::append(P, DarwinLibName); @@ -1042,12 +1055,9 @@ void DarwinClang::AddLinkSanitizerLibArgs(const ArgList &Args, StringRef Sanitizer, bool Shared) const { auto RLO = RuntimeLinkOptions(RLO_AlwaysLink | (Shared ? RLO_AddRPath : 0U)); - AddLinkRuntimeLib(Args, CmdArgs, - (Twine("libclang_rt.") + Sanitizer + "_" + - getOSLibraryNameSuffix() + - (Shared ? "_dynamic.dylib" : ".a")) - .str(), - RLO); + std::string SanitizerRelFilename = + getFileNameForSanitizerLib(Sanitizer, Shared); + AddLinkRuntimeLib(Args, CmdArgs, SanitizerRelFilename, RLO); } ToolChain::RuntimeLibType DarwinClang::GetRuntimeLibType( @@ -2285,24 +2295,43 @@ void Darwin::CheckObjCARC() const { SanitizerMask Darwin::getSupportedSanitizers() const { const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64; SanitizerMask Res = ToolChain::getSupportedSanitizers(); - Res |= SanitizerKind::Address; - Res |= SanitizerKind::Leak; - Res |= SanitizerKind::Fuzzer; - Res |= SanitizerKind::FuzzerNoLink; + + { + using namespace SanitizerKind; + assert(!(Res & (Address | Leak | Fuzzer | FuzzerNoLink | Thread)) && + "Sanitizer is already registered as supported"); + } + + if (sanitizerRuntimeExists("asan")) + Res |= SanitizerKind::Address; + if (sanitizerRuntimeExists("lsan")) + Res |= SanitizerKind::Leak; + if (sanitizerRuntimeExists("fuzzer", /*Shared=*/false)) { + Res |= SanitizerKind::Fuzzer; + Res |= SanitizerKind::FuzzerNoLink; + } Res |= SanitizerKind::Function; - if (isTargetMacOS()) { - if (!isMacosxVersionLT(10, 9)) - Res |= SanitizerKind::Vptr; + if (isTargetMacOS() && !isMacosxVersionLT(10, 9)) + Res |= SanitizerKind::Vptr; + if (isTargetMacOS()) Res |= SanitizerKind::SafeStack; - if (IsX86_64) - Res |= SanitizerKind::Thread; - } else if (isTargetIOSSimulator() || isTargetTvOSSimulator()) { - if (IsX86_64) - Res |= SanitizerKind::Thread; - } + + if (sanitizerRuntimeExists("tsan") && IsX86_64 && + (isTargetMacOS() || isTargetIOSSimulator() || isTargetTvOSSimulator())) + Res |= SanitizerKind::Thread; + return Res; } void Darwin::printVerboseInfo(raw_ostream &OS) const { CudaInstallation.print(OS); } + +bool Darwin::sanitizerRuntimeExists(StringRef SanitizerName, + bool Shared) const { + std::string RelName = getFileNameForSanitizerLib(SanitizerName, Shared); + SmallString<128> Dir = runtimeLibDir(); + SmallString<128> AbsName(Dir); + llvm::sys::path::append(AbsName, RelName); + return getVFS().exists(AbsName); +} |