diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp index 2a07c90819809da69c7f646c2311160eb0baf5ed..e0e167a9a82580adfca34a48c0ecef3336734ff9 100644 --- a/lib/Driver/Tools.cpp +++ b/lib/Driver/Tools.cpp @@ -1582,21 +1582,21 @@ static void addSanitizerRTLinkFlagsLinux( llvm::sys::path::append( LibSanitizer, "lib", "linux", (Twine("libclang_rt.") + Sanitizer + "-" + TC.getArchName() + ".a")); + // Sanitizer runtime may need to come before -lstdc++ (or -lc++, libstdc++.a, // etc.) so that the linker picks custom versions of the global 'operator // new' and 'operator delete' symbols. We take the extreme (but simple) // strategy of inserting it at the front of the link command. It also // needs to be forced to end up in the executable, so wrap it in // whole-archive. - if (BeforeLibStdCXX) { - SmallVector<const char *, 3> PrefixArgs; - PrefixArgs.push_back("-whole-archive"); - PrefixArgs.push_back(Args.MakeArgString(LibSanitizer)); - PrefixArgs.push_back("-no-whole-archive"); - CmdArgs.insert(CmdArgs.begin(), PrefixArgs.begin(), PrefixArgs.end()); - } else { - CmdArgs.push_back(Args.MakeArgString(LibSanitizer)); - } + SmallVector<const char *, 3> LibSanitizerArgs; + LibSanitizerArgs.push_back("-whole-archive"); + LibSanitizerArgs.push_back(Args.MakeArgString(LibSanitizer)); + LibSanitizerArgs.push_back("-no-whole-archive"); + + CmdArgs.insert(BeforeLibStdCXX ? CmdArgs.begin() : CmdArgs.end(), + LibSanitizerArgs.begin(), LibSanitizerArgs.end()); + CmdArgs.push_back("-lpthread"); CmdArgs.push_back("-ldl"); CmdArgs.push_back("-export-dynamic"); @@ -1658,8 +1658,22 @@ static void addMsanRTLinux(const ToolChain &TC, const ArgList &Args, /// If UndefinedBehaviorSanitizer is enabled, add appropriate linker flags /// (Linux). static void addUbsanRTLinux(const ToolChain &TC, const ArgList &Args, - ArgStringList &CmdArgs) { + ArgStringList &CmdArgs, bool IsCXX, + bool HasOtherSanitizerRt) { + if (Args.hasArg(options::OPT_shared)) + return; + + // Need a copy of sanitizer_common. This could come from another sanitizer + // runtime; if we're not including one, include our own copy. + if (!HasOtherSanitizerRt) + addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "san", true); + addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "ubsan", false); + + // Only include the bits of the runtime which need a C++ ABI library if + // we're linking in C++ mode. + if (IsCXX) + addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "ubsan_cxx", false); } static bool shouldUseFramePointer(const ArgList &Args, @@ -5876,7 +5890,9 @@ void linuxtools::Link::ConstructJob(Compilation &C, const JobAction &JA, // Call these before we add the C++ ABI library. if (Sanitize.needsUbsanRt()) - addUbsanRTLinux(getToolChain(), Args, CmdArgs); + addUbsanRTLinux(getToolChain(), Args, CmdArgs, D.CCCIsCXX, + Sanitize.needsAsanRt() || Sanitize.needsTsanRt() || + Sanitize.needsMsanRt()); if (Sanitize.needsAsanRt()) addAsanRTLinux(getToolChain(), Args, CmdArgs); if (Sanitize.needsTsanRt()) diff --git a/runtime/compiler-rt/Makefile b/runtime/compiler-rt/Makefile index 2a2cd7b905cbb0a18b9d4e9e2d0f6287a33a7f7a..e946de21a1dcf549ce661881a9b2c2d1e6d803be 100644 --- a/runtime/compiler-rt/Makefile +++ b/runtime/compiler-rt/Makefile @@ -99,13 +99,14 @@ TryCompile = \ # We currently only try to generate runtime libraries on x86. ifeq ($(ARCH),x86) RuntimeLibrary.linux.Configs += \ - full-i386.a profile-i386.a asan-i386.a ubsan-i386.a + full-i386.a profile-i386.a san-i386.a asan-i386.a ubsan-i386.a \ + ubsan_cxx-i386.a endif ifeq ($(ARCH),x86_64) RuntimeLibrary.linux.Configs += \ - full-x86_64.a profile-x86_64.a asan-x86_64.a tsan-x86_64.a msan-x86_64.a \ - ubsan-x86_64.a + full-x86_64.a profile-x86_64.a san-x86_64.a asan-x86_64.a \ + tsan-x86_64.a msan-x86_64.a ubsan-x86_64.a ubsan_cxx-x86_64.a # We need to build 32-bit ASan/UBsan libraries on 64-bit platform, and add them # to the list of runtime libraries to make # "clang -fsanitize=(address|undefined) -m32" work. @@ -113,7 +114,8 @@ RuntimeLibrary.linux.Configs += \ # executable. test_source = $(LLVM_SRC_ROOT)/tools/clang/runtime/compiler-rt/clang_linux_test_input.c ifeq ($(call TryCompile,$(ToolDir)/clang,$(test_source),-m32),0) -RuntimeLibrary.linux.Configs += asan-i386.a ubsan-i386.a +RuntimeLibrary.linux.Configs += san-i386.a asan-i386.a ubsan-i386.a \ + ubsan_cxx-i386.a endif ifneq ($(LLVM_ANDROID_TOOLCHAIN_DIR),) RuntimeLibrary.linux.Configs += asan-arm-android.so diff --git a/test/Driver/sanitizer-ld.c b/test/Driver/sanitizer-ld.c index 9bb2eab29aabb90c55cc0b04406402199c933ce7..89cbb6b284fd3094e23e4fdd6f52bc8a991fb993 100644 --- a/test/Driver/sanitizer-ld.c +++ b/test/Driver/sanitizer-ld.c @@ -87,9 +87,52 @@ // RUN: --sysroot=%S/Inputs/basic_linux_tree \ // RUN: | FileCheck --check-prefix=CHECK-UBSAN-LINUX %s // CHECK-UBSAN-LINUX: "{{.*}}ld{{(.exe)?}}" -// CHECK-UBSAN-LINUX-NOT: "-lc" -// CHECK-UBSAN-LINUX: libclang_rt.ubsan-i386.a" +// CHECK-UBSAN-LINUX-NOT: libclang_rt.asan +// CHECK-UBSAN-LINUX: "-whole-archive" "{{.*}}libclang_rt.san-i386.a" "-no-whole-archive" +// CHECK-UBSAN-LINUX-NOT: libclang_rt.asan +// CHECK-UBSAN-LINUX: "-whole-archive" "{{.*}}libclang_rt.ubsan-i386.a" "-no-whole-archive" +// CHECK-UBSAN-LINUX-NOT: libclang_rt.ubsan_cxx // CHECK-UBSAN-LINUX: "-lpthread" +// CHECK-UBSAN-LINUX-NOT: "-lstdc++" + +// RUN: %clangxx -fsanitize=undefined %s -### -o %t.o 2>&1 \ +// RUN: -target i386-unknown-linux \ +// RUN: --sysroot=%S/Inputs/basic_linux_tree \ +// RUN: | FileCheck --check-prefix=CHECK-UBSAN-LINUX-CXX %s +// CHECK-UBSAN-LINUX-CXX: "{{.*}}ld{{(.exe)?}}" +// CHECK-UBSAN-LINUX-CXX-NOT: libclang_rt.asan +// CHECK-UBSAN-LINUX-CXX: "-whole-archive" "{{.*}}libclang_rt.san-i386.a" "-no-whole-archive" +// CHECK-UBSAN-LINUX-CXX-NOT: libclang_rt.asan +// CHECK-UBSAN-LINUX-CXX: "-whole-archive" "{{.*}}libclang_rt.ubsan-i386.a" "-no-whole-archive" +// CHECK-UBSAN-LINUX-CXX: "-whole-archive" "{{.*}}libclang_rt.ubsan_cxx-i386.a" "-no-whole-archive" +// CHECK-UBSAN-LINUX-CXX: "-lpthread" +// CHECK-UBSAN-LINUX-CXX: "-lstdc++" + +// RUN: %clang -fsanitize=address,undefined %s -### -o %t.o 2>&1 \ +// RUN: -target i386-unknown-linux \ +// RUN: --sysroot=%S/Inputs/basic_linux_tree \ +// RUN: | FileCheck --check-prefix=CHECK-ASAN-UBSAN-LINUX %s +// CHECK-ASAN-UBSAN-LINUX: "{{.*}}ld{{(.exe)?}}" +// CHECK-ASAN-UBSAN-LINUX-NOT: libclang_rt.san +// CHECK-ASAN-UBSAN-LINUX: "-whole-archive" "{{.*}}libclang_rt.asan-i386.a" "-no-whole-archive" +// CHECK-ASAN-UBSAN-LINUX-NOT: libclang_rt.san +// 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: "-lpthread" +// CHECK-ASAN-UBSAN-LINUX-NOT: "-lstdc++" + +// RUN: %clangxx -fsanitize=address,undefined %s -### -o %t.o 2>&1 \ +// RUN: -target i386-unknown-linux \ +// RUN: --sysroot=%S/Inputs/basic_linux_tree \ +// RUN: | FileCheck --check-prefix=CHECK-ASAN-UBSAN-LINUX-CXX %s +// CHECK-ASAN-UBSAN-LINUX-CXX: "{{.*}}ld{{(.exe)?}}" +// CHECK-ASAN-UBSAN-LINUX-CXX-NOT: libclang_rt.san +// CHECK-ASAN-UBSAN-LINUX-CXX: "-whole-archive" "{{.*}}libclang_rt.asan-i386.a" "-no-whole-archive" +// CHECK-ASAN-UBSAN-LINUX-CXX-NOT: libclang_rt.san +// 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: "-lpthread" +// CHECK-ASAN-UBSAN-LINUX-CXX: "-lstdc++" // RUN: %clang -fsanitize=undefined %s -### -o %t.o 2>&1 \ // RUN: -target i386-unknown-linux \ @@ -97,6 +140,4 @@ // RUN: -shared \ // RUN: | FileCheck --check-prefix=CHECK-UBSAN-LINUX-SHARED %s // CHECK-UBSAN-LINUX-SHARED: "{{.*}}ld{{(.exe)?}}" -// CHECK-UBSAN-LINUX-SHARED-NOT: "-lc" -// CHECK-UBSAN-LINUX-SHARED: libclang_rt.ubsan-i386.a" -// CHECK-UBSAN-LINUX-SHARED: "-lpthread" +// CHECK-UBSAN-LINUX-SHARED-NOT: libclang_rt.ubsan-i386.a"