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

[ubsan] Support static linking with standalone runtime #80943

Conversation

MaskRay
Copy link
Member

@MaskRay MaskRay commented Feb 7, 2024

The standalone runtime (not
-fsanitize-minimal-runtime/-fsanitize-trap=undefined) installs some
signal handlers using real_sigaction. With static linking
(-static/-static-pie), the called REAL(sigaction) is null, leading to
an immediate segfault, which is confusing (#51538).

Fix #51538 by bailing out.

// REQUIRES: librt_has_multf3 from https://reviews.llvm.org/D109709
actually disabled the test because librt_has_* features are only for
compiler-rt/test/builtins. The test does not reproduce for me:
libclang_rt.builtins.a or libgcc. Revert the REQUIRES.

Created using spr 1.3.4
@MaskRay MaskRay requested review from kcc and PiJoules and removed request for kcc February 7, 2024 06:32
@llvmbot
Copy link
Collaborator

llvmbot commented Feb 7, 2024

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

Author: Fangrui Song (MaskRay)

Changes

The standalone runtime (not
-fsanitize-minimal-runtime/-fsanitize-trap=undefined) installs some
signal handlers using real_sigaction. With static linking
(-static/-static-pie), the called REAL(sigaction) is null, leading to
an immediate segfault, which is confusing (#51538).

Fix #51538 by bailing out.

// REQUIRES: librt_has_multf3 from https://reviews.llvm.org/D109709
actually disabled the test because librt_has_* features are only for
compiler-rt/test/builtins. The test does not reproduce for me:
libclang_rt.builtins.a or libgcc. Revert the REQUIRES.


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

2 Files Affected:

  • (modified) compiler-rt/lib/ubsan/ubsan_signals_standalone.cpp (+5)
  • (modified) compiler-rt/test/ubsan/TestCases/Misc/Linux/static-link.cpp (+1-1)
diff --git a/compiler-rt/lib/ubsan/ubsan_signals_standalone.cpp b/compiler-rt/lib/ubsan/ubsan_signals_standalone.cpp
index 354f847fab7138..68edd3a1b2062e 100644
--- a/compiler-rt/lib/ubsan/ubsan_signals_standalone.cpp
+++ b/compiler-rt/lib/ubsan/ubsan_signals_standalone.cpp
@@ -66,6 +66,11 @@ void InitializeDeadlySignals() {
     return;
   is_initialized = true;
   InitializeSignalInterceptors();
+#if SANITIZER_INTERCEPT_SIGNAL_AND_SIGACTION
+  // REAL(sigaction_symname) is nullptr in a static link. Bail out.
+  if (!REAL(sigaction_symname))
+    return;
+#endif
   InstallDeadlySignalHandlers(&UBsanOnDeadlySignal);
 }
 
diff --git a/compiler-rt/test/ubsan/TestCases/Misc/Linux/static-link.cpp b/compiler-rt/test/ubsan/TestCases/Misc/Linux/static-link.cpp
index cd185049567f79..f26b7b868cad62 100644
--- a/compiler-rt/test/ubsan/TestCases/Misc/Linux/static-link.cpp
+++ b/compiler-rt/test/ubsan/TestCases/Misc/Linux/static-link.cpp
@@ -1,7 +1,7 @@
 // REQUIRES: ubsan-standalone
 // REQUIRES: target={{x86_64.*}}
-// REQUIRES: librt_has_multf3
 // RUN: %clangxx -fsanitize=bool -static  %s -o %t && UBSAN_OPTIONS=handle_segv=0:handle_sigbus=0:handle_sigfpe=0 %run %t 2>&1 | FileCheck %s
+// RUN: %run %t 2>&1 | FileCheck %s
 #include <signal.h>
 #include <stdio.h>
 

@MaskRay MaskRay merged commit 2d5fb27 into main Feb 14, 2024
14 checks passed
@MaskRay MaskRay deleted the users/MaskRay/spr/ubsan-support-static-linking-with-standalone-runtime branch February 14, 2024 18:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using -fsanitize=undefined with -static segfault the binary
4 participants