-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[driver] Fix sanitizer libc++ runtime linking #120370
[driver] Fix sanitizer libc++ runtime linking #120370
Conversation
Created using spr 1.3.4
@llvm/pr-subscribers-compiler-rt-sanitizer @llvm/pr-subscribers-clang Author: Vitaly Buka (vitalybuka) Changes
Full diff: https://github.com/llvm/llvm-project/pull/120370.diff 3 Files Affected:
diff --git a/clang/lib/Driver/SanitizerArgs.cpp b/clang/lib/Driver/SanitizerArgs.cpp
index 81f94f23873661..0edfe641416129 100644
--- a/clang/lib/Driver/SanitizerArgs.cpp
+++ b/clang/lib/Driver/SanitizerArgs.cpp
@@ -1098,10 +1098,11 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
options::OPT_fno_sanitize_link_runtime, LinkRuntimes);
// Parse -link-cxx-sanitizer flag.
- LinkCXXRuntimes = Args.hasArg(options::OPT_fsanitize_link_cxx_runtime,
- options::OPT_fno_sanitize_link_cxx_runtime,
- LinkCXXRuntimes) ||
- D.CCCIsCXX();
+ LinkCXXRuntimes =
+ D.CCCIsCXX() && !Args.hasArg(clang::driver::options::OPT_nostdlibxx);
+ LinkCXXRuntimes =
+ Args.hasFlag(options::OPT_fsanitize_link_cxx_runtime,
+ options::OPT_fno_sanitize_link_cxx_runtime, LinkCXXRuntimes);
NeedsMemProfRt = Args.hasFlag(options::OPT_fmemory_profile,
options::OPT_fmemory_profile_EQ,
diff --git a/clang/test/Driver/sanitizer-ld.c b/clang/test/Driver/sanitizer-ld.c
index 60d60a6047b0f4..6ab1adf401ae0a 100644
--- a/clang/test/Driver/sanitizer-ld.c
+++ b/clang/test/Driver/sanitizer-ld.c
@@ -132,7 +132,14 @@
// RUN: -resource-dir=%S/Inputs/empty_resource_dir \
// RUN: --sysroot=%S/Inputs/basic_linux_tree \
// RUN: | FileCheck --check-prefix=CHECK-ASAN-LINUX-CXX %s
-//
+
+// RUN: %clangxx -### %s 2>&1 \
+// RUN: --target=i386-unknown-linux -fuse-ld=ld -stdlib=platform -fsanitize=address \
+// RUN: -resource-dir=%S/Inputs/empty_resource_dir \
+// RUN: --sysroot=%S/Inputs/basic_linux_tree \
+// RUN: -fsanitize-link-c++-runtime \
+// RUN: | FileCheck --check-prefix=CHECK-ASAN-LINUX-CXX %s
+
// CHECK-ASAN-LINUX-CXX: "{{(.*[^-.0-9A-Z_a-z])?}}ld{{(.exe)?}}"
// CHECK-ASAN-LINUX-CXX-NOT: "-lc"
// CHECK-ASAN-LINUX-CXX: "--whole-archive" "{{.*}}libclang_rt.asan.a" "--no-whole-archive"
@@ -145,6 +152,62 @@
// CHECK-ASAN-LINUX-CXX: "-ldl"
// CHECK-ASAN-LINUX-CXX: "-lresolv"
+// RUN: %clang -### %s 2>&1 \
+// RUN: --target=i386-unknown-linux -fuse-ld=ld -stdlib=platform -fsanitize=address \
+// RUN: -resource-dir=%S/Inputs/empty_resource_dir \
+// RUN: --sysroot=%S/Inputs/basic_linux_tree \
+// RUN: -fno-sanitize-link-c++-runtime \
+// RUN: | FileCheck --check-prefix=CHECK-ASAN-LINUX-CNOCXX %s
+
+// CHECK-ASAN-LINUX-CNOCXX: "{{(.*[^-.0-9A-Z_a-z])?}}ld{{(.exe)?}}"
+// CHECK-ASAN-LINUX-CNOCXX-NOT: "-lc"
+// CHECK-ASAN-LINUX-CNOCXX: "--whole-archive" "{{.*}}libclang_rt.asan.a" "--no-whole-archive"
+// CHECK-ASAN-LINUX-CNOCXX-NOT: libclang_rt.asan_cxx
+// CHECK-ASAN-LINUX-CNOCXX-NOT: "--dynamic-list"
+// CHECK-ASAN-LINUX-CNOCXX: "--export-dynamic"
+// CHECK-ASAN-LINUX-CNOCXX-NOT: stdc++
+// CHECK-ASAN-LINUX-CNOCXX: "-lpthread"
+// CHECK-ASAN-LINUX-CNOCXX: "-lrt"
+// CHECK-ASAN-LINUX-CNOCXX: "-ldl"
+// CHECK-ASAN-LINUX-CNOCXX: "-lresolv"
+
+// RUN: %clangxx -### %s 2>&1 \
+// RUN: --target=i386-unknown-linux -fuse-ld=ld -stdlib=platform -fsanitize=address \
+// RUN: -resource-dir=%S/Inputs/empty_resource_dir \
+// RUN: --sysroot=%S/Inputs/basic_linux_tree \
+// RUN: -fno-sanitize-link-c++-runtime \
+// RUN: | FileCheck --check-prefix=CHECK-ASAN-LINUX-NOCXX %s
+
+// CHECK-ASAN-LINUX-NOCXX: "{{(.*[^-.0-9A-Z_a-z])?}}ld{{(.exe)?}}"
+// CHECK-ASAN-LINUX-NOCXX-NOT: "-lc"
+// CHECK-ASAN-LINUX-NOCXX: "--whole-archive" "{{.*}}libclang_rt.asan.a" "--no-whole-archive"
+// CHECK-ASAN-LINUX-NOCXX-NOT: libclang_rt.asan_cxx
+// CHECK-ASAN-LINUX-NOCXX-NOT: "--dynamic-list"
+// CHECK-ASAN-LINUX-NOCXX: "--export-dynamic"
+// CHECK-ASAN-LINUX-NOCXX: stdc++
+// CHECK-ASAN-LINUX-NOCXX: "-lpthread"
+// CHECK-ASAN-LINUX-NOCXX: "-lrt"
+// CHECK-ASAN-LINUX-NOCXX: "-ldl"
+// CHECK-ASAN-LINUX-NOCXX: "-lresolv"
+
+// RUN: %clangxx -### %s 2>&1 \
+// RUN: --target=i386-unknown-linux -fuse-ld=ld -stdlib=platform -fsanitize=address \
+// RUN: -resource-dir=%S/Inputs/empty_resource_dir \
+// RUN: --sysroot=%S/Inputs/basic_linux_tree \
+// RUN: -nostdlib++ \
+// RUN: | FileCheck --check-prefix=CHECK-ASAN-LINUX-NOSTDCXX %s
+
+// CHECK-ASAN-LINUX-NOSTDCXX: "{{(.*[^-.0-9A-Z_a-z])?}}ld{{(.exe)?}}"
+// CHECK-ASAN-LINUX-NOSTDCXX-NOT: "-lc"
+// CHECK-ASAN-LINUX-NOSTDCXX: "--whole-archive" "{{.*}}libclang_rt.asan.a" "--no-whole-archive"
+// CHECK-ASAN-LINUX-NOSTDCXX-NOT: libclang_rt.asan_cxx
+// CHECK-ASAN-LINUX-NOSTDCXX-NOT: "--dynamic-list"
+// CHECK-ASAN-LINUX-NOSTDCXX: "--export-dynamic"
+// CHECK-ASAN-LINUX-NOSTDCXX: "-lpthread"
+// CHECK-ASAN-LINUX-NOSTDCXX: "-lrt"
+// CHECK-ASAN-LINUX-NOSTDCXX: "-ldl"
+// CHECK-ASAN-LINUX-NOSTDCXX: "-lresolv"
+
// RUN: %clang -### %s -o /dev/null -fsanitize=address \
// RUN: --target=i386-unknown-linux -fuse-ld=ld -stdlib=platform \
// RUN: -resource-dir=%S/Inputs/resource_dir \
diff --git a/compiler-rt/test/hwasan/TestCases/sizes.cpp b/compiler-rt/test/hwasan/TestCases/sizes.cpp
index ce2c4579d66f61..5bc281e588a99f 100644
--- a/compiler-rt/test/hwasan/TestCases/sizes.cpp
+++ b/compiler-rt/test/hwasan/TestCases/sizes.cpp
@@ -1,6 +1,6 @@
// This test requires operator new to be intercepted by the hwasan runtime,
// so we need to avoid linking against libc++.
-// RUN: %clangxx_hwasan %s -nostdlib++ -lstdc++ -o %t || %clangxx_hwasan %s -o %t
+// RUN: %clangxx_hwasan %s -nostdlib++ -lstdc++ -fsanitize-link-c++-runtime -o %t || %clangxx_hwasan %s -o %t
// RUN: %env_hwasan_opts=allocator_may_return_null=0 not %run %t malloc 2>&1 | FileCheck %s --check-prefix=CHECK-max
// RUN: %env_hwasan_opts=allocator_may_return_null=1 %run %t malloc 2>&1
// RUN: %env_hwasan_opts=allocator_may_return_null=0 not %run %t malloc max 2>&1 | FileCheck %s --check-prefix=CHECK-max
|
This reverts commit 9af5de3. Reason: buildbot breakage (https://lab.llvm.org/buildbot/#/builders/24/builds/3394/steps/10/logs/stdio) "Unexpectedly Passed Tests (1): llvm-libc++-shared.cfg.in :: libcxx/language.support/support.dynamic/libcpp_deallocate.sh.cpp"
This was reverted, because https://github.com/llvm/llvm-project/blob/main/libcxx/test/libcxx/language.support/support.dynamic/libcpp_deallocate.sh.cpp in libc++ started passing unexpectedly. I'm not entirely sure about why that is though - previously, it used to fail like this:
But after this change, it succeeds. If this is right and expected that it fixes the root cause, we could change the But as @vitalybuka mentioned in #108357 (comment), many also expect to be able to link with |
Linking this runtime requires C++ ABI, which breaks -nostdlib++ builds. However, UBSAN C++ runtime is only needed for CFI and VPTR checks. Unblocks #120370.
override defauld behavior implied from
CCCIsCXX