[compiler-rt] [sanitizer] Reject unsupported -static at link time (PR #83524)
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 29 21:13:14 PST 2024
https://github.com/MaskRay created https://github.com/llvm/llvm-project/pull/83524
Most sanitizers don't support static linking. One primary reason is the
incompatibility with interceptors. `GetTlsSize` is another reason.
asan/memprof use `__interception::DoesNotSupportStaticLinking`
(`_DYNAMIC` reference) to reject -static at link time. Port this
detector to other sanitizers. dfsan actually supports -static for
certain cases. Don't touch dfsan.
>From d15d96864ecd4d32ec41e29753b0e89867a55e84 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Thu, 29 Feb 2024 21:13:05 -0800
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
=?UTF-8?q?l=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.4
---
compiler-rt/lib/hwasan/hwasan_interceptors.cpp | 1 +
compiler-rt/lib/lsan/lsan_interceptors.cpp | 1 +
compiler-rt/lib/msan/msan_interceptors.cpp | 2 ++
compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp | 2 ++
compiler-rt/test/sanitizer_common/TestCases/Linux/static-link.c | 2 ++
5 files changed, 8 insertions(+)
create mode 100644 compiler-rt/test/sanitizer_common/TestCases/Linux/static-link.c
diff --git a/compiler-rt/lib/hwasan/hwasan_interceptors.cpp b/compiler-rt/lib/hwasan/hwasan_interceptors.cpp
index 96df4dd0c24d7d..d519ac3a459b67 100644
--- a/compiler-rt/lib/hwasan/hwasan_interceptors.cpp
+++ b/compiler-rt/lib/hwasan/hwasan_interceptors.cpp
@@ -520,6 +520,7 @@ void InitializeInterceptors() {
CHECK_EQ(inited, 0);
# if HWASAN_WITH_INTERCEPTORS
+ __interception::DoesNotSupportStaticLinking();
InitializeCommonInterceptors();
(void)(read_iovec);
diff --git a/compiler-rt/lib/lsan/lsan_interceptors.cpp b/compiler-rt/lib/lsan/lsan_interceptors.cpp
index 885f7ad5ddba96..1fd0010f9ea936 100644
--- a/compiler-rt/lib/lsan/lsan_interceptors.cpp
+++ b/compiler-rt/lib/lsan/lsan_interceptors.cpp
@@ -543,6 +543,7 @@ namespace __lsan {
void InitializeInterceptors() {
// Fuchsia doesn't use interceptors that require any setup.
#if !SANITIZER_FUCHSIA
+ __interception::DoesNotSupportStaticLinking();
InitializeSignalInterceptors();
INTERCEPT_FUNCTION(malloc);
diff --git a/compiler-rt/lib/msan/msan_interceptors.cpp b/compiler-rt/lib/msan/msan_interceptors.cpp
index 2c9f2c01e14b06..6e0b2bf2ef5b6f 100644
--- a/compiler-rt/lib/msan/msan_interceptors.cpp
+++ b/compiler-rt/lib/msan/msan_interceptors.cpp
@@ -1762,6 +1762,8 @@ void InitializeInterceptors() {
static int inited = 0;
CHECK_EQ(inited, 0);
+ __interception::DoesNotSupportStaticLinking();
+
new(interceptor_ctx()) InterceptorContext();
InitializeCommonInterceptors();
diff --git a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
index a9f6673ac44e90..dd8550d642cce8 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
@@ -2861,6 +2861,8 @@ void InitializeInterceptors() {
REAL(memcpy) = internal_memcpy;
#endif
+ __interception::DoesNotSupportStaticLinking();
+
new(interceptor_ctx()) InterceptorContext();
InitializeCommonInterceptors();
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/static-link.c b/compiler-rt/test/sanitizer_common/TestCases/Linux/static-link.c
new file mode 100644
index 00000000000000..f45f718cb08d34
--- /dev/null
+++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/static-link.c
@@ -0,0 +1,2 @@
+// UNSUPPORTED: hwasan, ubsan
+// RUN: not %clangxx -static %s -o /dev/null
More information about the llvm-commits
mailing list