Skip to content
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

[sanitizer] Reject unsupported -static at link time #83524

Conversation

MaskRay
Copy link
Member

@MaskRay MaskRay commented Mar 1, 2024

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.

Created using spr 1.3.4
@MaskRay MaskRay requested a review from vitalybuka March 1, 2024 05:13
@llvmbot llvmbot added compiler-rt compiler-rt:tsan Thread sanitizer compiler-rt:hwasan Hardware-assisted address sanitizer compiler-rt:msan Memory sanitizer compiler-rt:lsan Leak sanitizer compiler-rt:sanitizer labels Mar 1, 2024
@MaskRay MaskRay requested a review from thurstond March 1, 2024 05:13
@llvmbot
Copy link
Collaborator

llvmbot commented Mar 1, 2024

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Fangrui Song (MaskRay)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/83524.diff

5 Files Affected:

  • (modified) compiler-rt/lib/hwasan/hwasan_interceptors.cpp (+1)
  • (modified) compiler-rt/lib/lsan/lsan_interceptors.cpp (+1)
  • (modified) compiler-rt/lib/msan/msan_interceptors.cpp (+2)
  • (modified) compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp (+2)
  • (added) compiler-rt/test/sanitizer_common/TestCases/Linux/static-link.c (+2)
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

@@ -0,0 +1,2 @@
// UNSUPPORTED: hwasan, ubsan
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is special about these two sanitizers (why doesn't it also apply to lsan/msan/tsan)?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess hwasan, ubsan can be with or without interceptors

hwasan on non-android linux will be with interceptors

Copy link
Member Author

@MaskRay MaskRay Mar 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. The test is only for sanitizers that are guaranteed to use interceptors that are incompatible with -static links. I unsupport hwasan/ubsan because they may not use interceptors.

(hwasan has other incompatibility with static linking, but that's not as fundamental as interceptors).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks for explaining!

@@ -0,0 +1,2 @@
// UNSUPPORTED: hwasan, ubsan
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks for explaining!

@MaskRay MaskRay merged commit fe1d02b into main Mar 13, 2024
11 checks passed
@MaskRay MaskRay deleted the users/MaskRay/spr/sanitizer-reject-unsupported-static-at-link-time branch March 13, 2024 06:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler-rt:hwasan Hardware-assisted address sanitizer compiler-rt:lsan Leak sanitizer compiler-rt:msan Memory sanitizer compiler-rt:sanitizer compiler-rt:tsan Thread sanitizer compiler-rt
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants