From d841fb46944fa9a865aab919d67cff0d90c4d896 Mon Sep 17 00:00:00 2001 From: Alexey Samsonov Date: Wed, 1 Apr 2015 22:42:25 +0000 Subject: [PATCH] [UBSan] Embed UBSan into ASan runtime (Clang part). Summary: Change the way we use ASan and UBSan together. Instead of keeping two separate runtimes (libclang_rt.asan and libclang_rt.ubsan), embed UBSan into ASan and get rid of libclang_rt.ubsan. If UBSan is not supported on a platform, all UBSan sources are just compiled into dummy empty object files. UBSan initialization code (e.g. flag parsing) is directly called from ASan initialization, so we are able to enforce correct initialization order. This mirrors the approach we already use for ASan+LSan. This change doesn't modify the way we use standalone UBSan. Test Plan: regression test suite Reviewers: kubabrecka, zaks.anna, kcc, rsmith Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D8645 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@233860 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Driver/SanitizerArgs.cpp | 3 ++- lib/Driver/ToolChains.cpp | 6 +++--- lib/Driver/Tools.cpp | 14 +++----------- test/Driver/sanitizer-ld.c | 7 +++---- 4 files changed, 11 insertions(+), 19 deletions(-) diff --git a/lib/Driver/SanitizerArgs.cpp b/lib/Driver/SanitizerArgs.cpp index fb907a7eea..88725cdedd 100644 --- a/lib/Driver/SanitizerArgs.cpp +++ b/lib/Driver/SanitizerArgs.cpp @@ -159,7 +159,8 @@ static bool getDefaultBlacklist(const Driver &D, uint64_t Kinds, } bool SanitizerArgs::needsUbsanRt() const { - return !UbsanTrapOnError && hasOneOf(Sanitizers, NeedsUbsanRt); + return !UbsanTrapOnError && hasOneOf(Sanitizers, NeedsUbsanRt) && + !Sanitizers.has(SanitizerKind::Address); } bool SanitizerArgs::requiresPIE() const { diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp index 61a2ac6934..658ffb6d32 100644 --- a/lib/Driver/ToolChains.cpp +++ b/lib/Driver/ToolChains.cpp @@ -338,6 +338,9 @@ void DarwinClang::AddLinkSanitizerLibArgs(const ArgList &Args, OS + "_dynamic.dylib").str(), /*AlwaysLink*/ true, /*IsEmbedded*/ false, /*AddRPath*/ true); + // Add explicit dependcy on -lc++abi, as -lc++ doesn't re-export + // all RTTI-related symbols that UBSan uses. + CmdArgs.push_back("-lc++abi"); } void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args, @@ -401,9 +404,6 @@ void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args, << "-fsanitize=undefined"; } else { AddLinkSanitizerLibArgs(Args, CmdArgs, "ubsan"); - // Add explicit dependcy on -lc++abi, as -lc++ doesn't re-export - // all RTTI-related symbols that UBSan uses. - CmdArgs.push_back("-lc++abi"); } } diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp index 3a57129d63..d96c294581 100644 --- a/lib/Driver/Tools.cpp +++ b/lib/Driver/Tools.cpp @@ -2307,18 +2307,10 @@ collectSanitizerRuntimes(const ToolChain &TC, const ArgList &Args, StaticRuntimes.push_back("msan"); if (SanArgs.needsTsanRt()) StaticRuntimes.push_back("tsan"); - // WARNING: UBSan should always go last. if (SanArgs.needsUbsanRt()) { - // Check if UBSan is combined with another sanitizers. - if (StaticRuntimes.empty()) { - StaticRuntimes.push_back("ubsan_standalone"); - if (SanArgs.linkCXXRuntimes()) - StaticRuntimes.push_back("ubsan_standalone_cxx"); - } else { - StaticRuntimes.push_back("ubsan"); - if (SanArgs.linkCXXRuntimes()) - StaticRuntimes.push_back("ubsan_cxx"); - } + StaticRuntimes.push_back("ubsan_standalone"); + if (SanArgs.linkCXXRuntimes()) + StaticRuntimes.push_back("ubsan_standalone_cxx"); } } diff --git a/test/Driver/sanitizer-ld.c b/test/Driver/sanitizer-ld.c index 97205f4ba9..586d0c729c 100644 --- a/test/Driver/sanitizer-ld.c +++ b/test/Driver/sanitizer-ld.c @@ -227,8 +227,7 @@ // RUN: | FileCheck --check-prefix=CHECK-ASAN-UBSAN-LINUX %s // CHECK-ASAN-UBSAN-LINUX: "{{.*}}ld{{(.exe)?}}" // CHECK-ASAN-UBSAN-LINUX: "-whole-archive" "{{.*}}libclang_rt.asan-i386.a" "-no-whole-archive" -// CHECK-ASAN-UBSAN-LINUX: "-whole-archive" "{{.*}}libclang_rt.ubsan-i386.a" "-no-whole-archive" -// CHECK-ASAN-UBSAN-LINUX-NOT: libclang_rt.ubsan_cxx +// CHECK-ASAN-UBSAN-LINUX-NOT: libclang_rt.ubsan // CHECK-ASAN-UBSAN-LINUX-NOT: "-lstdc++" // CHECK-ASAN-UBSAN-LINUX: "-lpthread" @@ -238,8 +237,8 @@ // RUN: | FileCheck --check-prefix=CHECK-ASAN-UBSAN-LINUX-CXX %s // CHECK-ASAN-UBSAN-LINUX-CXX: "{{.*}}ld{{(.exe)?}}" // CHECK-ASAN-UBSAN-LINUX-CXX: "-whole-archive" "{{.*}}libclang_rt.asan-i386.a" "-no-whole-archive" -// CHECK-ASAN-UBSAN-LINUX-CXX: "-whole-archive" "{{.*}}libclang_rt.ubsan-i386.a" "-no-whole-archive" -// CHECK-ASAN-UBSAN-LINUX-CXX: "-whole-archive" "{{.*}}libclang_rt.ubsan_cxx-i386.a" "-no-whole-archive" +// CHECK-ASAN-UBSAN-LINUX-CXX: "-whole-archive" "{{.*}}libclang_rt.asan_cxx-i386.a" "-no-whole-archive" +// CHECK-ASAN-UBSAN-LINUX-CXX-NOT: libclang_rt.ubsan // CHECK-ASAN-UBSAN-LINUX-CXX: "-lstdc++" // CHECK-ASAN-UBSAN-LINUX-CXX: "-lpthread" -- 2.50.1