From: Petr Hosek Date: Mon, 29 Oct 2018 20:37:52 +0000 (+0000) Subject: [Driver] Support sanitized libraries on Fuchsia X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f5be668dff6191878d3fc104549c8b92916fea72;p=clang [Driver] Support sanitized libraries on Fuchsia When using sanitizers, add //lib/ to the list of library paths to support using sanitized version of runtime libraries if available. Differential Revision: https://reviews.llvm.org/D53487 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@345537 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Driver/ToolChains/CommonArgs.cpp b/lib/Driver/ToolChains/CommonArgs.cpp index 171d1aebdb..a2db515310 100644 --- a/lib/Driver/ToolChains/CommonArgs.cpp +++ b/lib/Driver/ToolChains/CommonArgs.cpp @@ -560,6 +560,40 @@ static bool addSanitizerDynamicList(const ToolChain &TC, const ArgList &Args, return false; } +static void addSanitizerLibPath(const ToolChain &TC, const ArgList &Args, + ArgStringList &CmdArgs, StringRef Name) { + for (const auto &LibPath : TC.getLibraryPaths()) { + if (!LibPath.empty()) { + SmallString<128> P(LibPath); + llvm::sys::path::append(P, Name); + if (TC.getVFS().exists(P)) + CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + P)); + } + } +} + +void tools::addSanitizerPathLibArgs(const ToolChain &TC, const ArgList &Args, + ArgStringList &CmdArgs) { + const SanitizerArgs &SanArgs = TC.getSanitizerArgs(); + if (SanArgs.needsAsanRt()) { + addSanitizerLibPath(TC, Args, CmdArgs, "asan"); + } + if (SanArgs.needsHwasanRt()) { + addSanitizerLibPath(TC, Args, CmdArgs, "hwasan"); + } + if (SanArgs.needsLsanRt()) { + addSanitizerLibPath(TC, Args, CmdArgs, "lsan"); + } + if (SanArgs.needsMsanRt()) { + addSanitizerLibPath(TC, Args, CmdArgs, "msan"); + } + if (SanArgs.needsTsanRt()) { + addSanitizerLibPath(TC, Args, CmdArgs, "tsan"); + } +} + + + void tools::linkSanitizerRuntimeDeps(const ToolChain &TC, ArgStringList &CmdArgs) { // Force linking against the system libraries sanitizers depends on diff --git a/lib/Driver/ToolChains/CommonArgs.h b/lib/Driver/ToolChains/CommonArgs.h index e8ebe2225e..291f80401e 100644 --- a/lib/Driver/ToolChains/CommonArgs.h +++ b/lib/Driver/ToolChains/CommonArgs.h @@ -32,6 +32,10 @@ void claimNoWarnArgs(const llvm::opt::ArgList &Args); bool addSanitizerRuntimes(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs); +void addSanitizerPathLibArgs(const ToolChain &TC, + const llvm::opt::ArgList &Args, + llvm::opt::ArgStringList &CmdArgs); + void linkSanitizerRuntimeDeps(const ToolChain &TC, llvm::opt::ArgStringList &CmdArgs); diff --git a/lib/Driver/ToolChains/Fuchsia.cpp b/lib/Driver/ToolChains/Fuchsia.cpp index 54c34ff159..94441fce7c 100644 --- a/lib/Driver/ToolChains/Fuchsia.cpp +++ b/lib/Driver/ToolChains/Fuchsia.cpp @@ -76,10 +76,11 @@ void fuchsia::Linker::ConstructJob(Compilation &C, const JobAction &JA, else if (Args.hasArg(options::OPT_shared)) CmdArgs.push_back("-shared"); + const SanitizerArgs &SanArgs = ToolChain.getSanitizerArgs(); + if (!Args.hasArg(options::OPT_shared)) { std::string Dyld = D.DyldPrefix; - if (ToolChain.getSanitizerArgs().needsAsanRt() && - ToolChain.getSanitizerArgs().needsSharedRt()) + if (SanArgs.needsAsanRt() && SanArgs.needsSharedRt()) Dyld += "asan/"; Dyld += "ld.so.1"; CmdArgs.push_back("-dynamic-linker"); @@ -98,6 +99,8 @@ void fuchsia::Linker::ConstructJob(Compilation &C, const JobAction &JA, Args.AddAllArgs(CmdArgs, options::OPT_L); Args.AddAllArgs(CmdArgs, options::OPT_u); + addSanitizerPathLibArgs(ToolChain, Args, CmdArgs); + ToolChain.AddFilePathLibArgs(Args, CmdArgs); if (D.isUsingLTO()) { diff --git a/test/Driver/fuchsia.c b/test/Driver/fuchsia.c index b26ab0417e..6975c86e76 100644 --- a/test/Driver/fuchsia.c +++ b/test/Driver/fuchsia.c @@ -66,22 +66,28 @@ // RUN: -resource-dir=%S/Inputs/resource_dir_with_per_target_subdir \ // RUN: -fuse-ld=lld \ // RUN: | FileCheck %s -check-prefix=CHECK-ASAN-X86 +// CHECK-ASAN-X86: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]" // CHECK-ASAN-X86: "-fsanitize=address" // CHECK-ASAN-X86: "-fsanitize-address-globals-dead-stripping" // CHECK-ASAN-X86: "-dynamic-linker" "asan/ld.so.1" -// CHECK-ASAN-X86: "{{.*[/\\]}}libclang_rt.asan.so" -// CHECK-ASAN-X86: "{{.*[/\\]}}libclang_rt.asan-preinit.a" +// CHECK-ASAN-X86: "-L[[RESOURCE_DIR]]{{/|\\\\}}x86_64-fuchsia{{/|\\\\}}lib{{/|\\\\}}asan" +// CHECK-ASAN-X86: "-L[[RESOURCE_DIR]]{{/|\\\\}}x86_64-fuchsia{{/|\\\\}}lib" +// CHECK-ASAN-X86: "[[RESOURCE_DIR]]{{/|\\\\}}x86_64-fuchsia{{/|\\\\}}lib{{/|\\\\}}libclang_rt.asan.so" +// CHECK-ASAN-X86: "[[RESOURCE_DIR]]{{/|\\\\}}x86_64-fuchsia{{/|\\\\}}lib{{/|\\\\}}libclang_rt.asan-preinit.a" // RUN: %clang %s -### --target=aarch64-fuchsia \ // RUN: -fsanitize=address 2>&1 \ // RUN: -resource-dir=%S/Inputs/resource_dir_with_per_target_subdir \ // RUN: -fuse-ld=lld \ // RUN: | FileCheck %s -check-prefix=CHECK-ASAN-AARCH64 +// CHECK-ASAN-AARCH64: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]" // CHECK-ASAN-AARCH64: "-fsanitize=address" // CHECK-ASAN-AARCH64: "-fsanitize-address-globals-dead-stripping" // CHECK-ASAN-AARCH64: "-dynamic-linker" "asan/ld.so.1" -// CHECK-ASAN-AARCH64: "{{.*[/\\]}}libclang_rt.asan.so" -// CHECK-ASAN-AARCH64: "{{.*[/\\]}}libclang_rt.asan-preinit.a" +// CHECK-ASAN-AARCH64: "-L[[RESOURCE_DIR]]{{/|\\\\}}aarch64-fuchsia{{/|\\\\}}lib{{/|\\\\}}asan" +// CHECK-ASAN-AARCH64: "-L[[RESOURCE_DIR]]{{/|\\\\}}aarch64-fuchsia{{/|\\\\}}lib" +// CHECK-ASAN-AARCH64: "[[RESOURCE_DIR]]{{/|\\\\}}aarch64-fuchsia{{/|\\\\}}lib{{/|\\\\}}libclang_rt.asan.so" +// CHECK-ASAN-AARCH64: "[[RESOURCE_DIR]]{{/|\\\\}}aarch64-fuchsia{{/|\\\\}}lib{{/|\\\\}}libclang_rt.asan-preinit.a" // RUN: %clang %s -### --target=x86_64-fuchsia \ // RUN: -fsanitize=address -fPIC -shared 2>&1 \