[compiler-rt] [sanitizer] Lift AsanDoesNotSupportStaticLinkage to sanitizer_common.h. NFC (PR #80948)
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Tue Feb 6 23:44:48 PST 2024
https://github.com/MaskRay created https://github.com/llvm/llvm-project/pull/80948
The `_DYNAMIC` reference from `AsanDoesNotSupportStaticLinkage` ensures
that `clang++ -fsanitize=address -static` gets a linker error. Move the
function to sanitizer_common.h to be used by more sanitizers on ELF
platforms.
Fuchsia opts out the check as its AsanDoesNotSupportStaticLinkage is a
no-op.
>From 10e9c5a30d7d1b03acd9927b4f9853437ab63a50 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Tue, 6 Feb 2024 23:44:40 -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/asan/asan_fuchsia.cpp | 2 --
compiler-rt/lib/asan/asan_internal.h | 1 -
compiler-rt/lib/asan/asan_linux.cpp | 7 -------
compiler-rt/lib/asan/asan_mac.cpp | 5 -----
compiler-rt/lib/asan/asan_rtl.cpp | 5 ++---
compiler-rt/lib/asan/asan_win.cpp | 2 --
compiler-rt/lib/memprof/memprof_internal.h | 1 -
compiler-rt/lib/memprof/memprof_linux.cpp | 7 -------
compiler-rt/lib/memprof/memprof_rtl.cpp | 2 +-
compiler-rt/lib/sanitizer_common/sanitizer_common.h | 11 +++++++++++
10 files changed, 14 insertions(+), 29 deletions(-)
diff --git a/compiler-rt/lib/asan/asan_fuchsia.cpp b/compiler-rt/lib/asan/asan_fuchsia.cpp
index 12625e9d75833..dbc4342e83388 100644
--- a/compiler-rt/lib/asan/asan_fuchsia.cpp
+++ b/compiler-rt/lib/asan/asan_fuchsia.cpp
@@ -57,8 +57,6 @@ void AsanCheckDynamicRTPrereqs() {}
void AsanCheckIncompatibleRT() {}
void InitializeAsanInterceptors() {}
-void *AsanDoesNotSupportStaticLinkage() { return nullptr; }
-
void InitializePlatformExceptionHandlers() {}
void AsanOnDeadlySignal(int signo, void *siginfo, void *context) {
UNIMPLEMENTED();
diff --git a/compiler-rt/lib/asan/asan_internal.h b/compiler-rt/lib/asan/asan_internal.h
index 2944ebe213b5d..06dfc4b177339 100644
--- a/compiler-rt/lib/asan/asan_internal.h
+++ b/compiler-rt/lib/asan/asan_internal.h
@@ -80,7 +80,6 @@ void ReplaceSystemMalloc();
// asan_linux.cpp / asan_mac.cpp / asan_win.cpp
uptr FindDynamicShadowStart();
-void *AsanDoesNotSupportStaticLinkage();
void AsanCheckDynamicRTPrereqs();
void AsanCheckIncompatibleRT();
diff --git a/compiler-rt/lib/asan/asan_linux.cpp b/compiler-rt/lib/asan/asan_linux.cpp
index 37d3bad1b1ec6..cdc1912a6d832 100644
--- a/compiler-rt/lib/asan/asan_linux.cpp
+++ b/compiler-rt/lib/asan/asan_linux.cpp
@@ -51,11 +51,9 @@ extern "C" void *_DYNAMIC;
# elif SANITIZER_NETBSD
# include <link_elf.h>
# include <ucontext.h>
-extern Elf_Dyn _DYNAMIC;
# else
# include <link.h>
# include <sys/ucontext.h>
-extern ElfW(Dyn) _DYNAMIC[];
# endif
typedef enum {
@@ -76,11 +74,6 @@ void InitializePlatformInterceptors() {}
void InitializePlatformExceptionHandlers() {}
bool IsSystemHeapAddress(uptr addr) { return false; }
-void *AsanDoesNotSupportStaticLinkage() {
- // This will fail to link with -static.
- return &_DYNAMIC;
-}
-
# if ASAN_PREMAP_SHADOW
uptr FindPremappedShadowStart(uptr shadow_size_bytes) {
uptr granularity = GetMmapGranularity();
diff --git a/compiler-rt/lib/asan/asan_mac.cpp b/compiler-rt/lib/asan/asan_mac.cpp
index 1b0e9b3fe0060..b250f796e165f 100644
--- a/compiler-rt/lib/asan/asan_mac.cpp
+++ b/compiler-rt/lib/asan/asan_mac.cpp
@@ -49,11 +49,6 @@ void InitializePlatformInterceptors() {}
void InitializePlatformExceptionHandlers() {}
bool IsSystemHeapAddress (uptr addr) { return false; }
-// No-op. Mac does not support static linkage anyway.
-void *AsanDoesNotSupportStaticLinkage() {
- return 0;
-}
-
uptr FindDynamicShadowStart() {
return MapDynamicShadow(MemToShadowSize(kHighMemEnd), ASAN_SHADOW_SCALE,
/*min_shadow_base_alignment*/ 0, kHighMemEnd);
diff --git a/compiler-rt/lib/asan/asan_rtl.cpp b/compiler-rt/lib/asan/asan_rtl.cpp
index a61deed7382b0..dbfad57955c75 100644
--- a/compiler-rt/lib/asan/asan_rtl.cpp
+++ b/compiler-rt/lib/asan/asan_rtl.cpp
@@ -410,6 +410,8 @@ static bool AsanInitInternal() {
return false;
}
+ // Make sure we are not statically linked.
+ DoesNotSupportStaticLinking();
AsanCheckIncompatibleRT();
AsanCheckDynamicRTPrereqs();
AvoidCVE_2016_2143();
@@ -421,9 +423,6 @@ static bool AsanInitInternal() {
InitializeHighMemEnd();
- // Make sure we are not statically linked.
- AsanDoesNotSupportStaticLinkage();
-
// Install tool-specific callbacks in sanitizer_common.
AddDieCallback(AsanDie);
SetCheckUnwindCallback(CheckUnwind);
diff --git a/compiler-rt/lib/asan/asan_win.cpp b/compiler-rt/lib/asan/asan_win.cpp
index 8507e675684ed..cda1f7a91e140 100644
--- a/compiler-rt/lib/asan/asan_win.cpp
+++ b/compiler-rt/lib/asan/asan_win.cpp
@@ -266,8 +266,6 @@ void PlatformTSDDtor(void *tsd) { AsanThread::TSDDtor(tsd); }
// }}}
// ---------------------- Various stuff ---------------- {{{
-void *AsanDoesNotSupportStaticLinkage() { return 0; }
-
uptr FindDynamicShadowStart() {
return MapDynamicShadow(MemToShadowSize(kHighMemEnd), ASAN_SHADOW_SCALE,
/*min_shadow_base_alignment*/ 0, kHighMemEnd);
diff --git a/compiler-rt/lib/memprof/memprof_internal.h b/compiler-rt/lib/memprof/memprof_internal.h
index 990e62ce1a55d..ad61d829d72ca 100644
--- a/compiler-rt/lib/memprof/memprof_internal.h
+++ b/compiler-rt/lib/memprof/memprof_internal.h
@@ -61,7 +61,6 @@ void ReplaceSystemMalloc();
// memprof_linux.cpp
uptr FindDynamicShadowStart();
-void *MemprofDoesNotSupportStaticLinkage();
// memprof_thread.cpp
MemprofThread *CreateMainThread();
diff --git a/compiler-rt/lib/memprof/memprof_linux.cpp b/compiler-rt/lib/memprof/memprof_linux.cpp
index fcb6f662a82e5..26a2b456b874e 100644
--- a/compiler-rt/lib/memprof/memprof_linux.cpp
+++ b/compiler-rt/lib/memprof/memprof_linux.cpp
@@ -38,8 +38,6 @@
#include <unistd.h>
#include <unwind.h>
-extern ElfW(Dyn) _DYNAMIC[];
-
typedef enum {
MEMPROF_RT_VERSION_UNDEFINED = 0,
MEMPROF_RT_VERSION_DYNAMIC,
@@ -57,11 +55,6 @@ namespace __memprof {
void InitializePlatformInterceptors() {}
void InitializePlatformExceptionHandlers() {}
-void *MemprofDoesNotSupportStaticLinkage() {
- // This will fail to link with -static.
- return &_DYNAMIC;
-}
-
uptr FindDynamicShadowStart() {
uptr shadow_size_bytes = MemToShadowSize(kHighMemEnd);
return MapDynamicShadow(shadow_size_bytes, SHADOW_SCALE,
diff --git a/compiler-rt/lib/memprof/memprof_rtl.cpp b/compiler-rt/lib/memprof/memprof_rtl.cpp
index 5e2e7bc2be3f7..83e377f8e4307 100644
--- a/compiler-rt/lib/memprof/memprof_rtl.cpp
+++ b/compiler-rt/lib/memprof/memprof_rtl.cpp
@@ -162,7 +162,7 @@ static void MemprofInitInternal() {
InitializeHighMemEnd();
// Make sure we are not statically linked.
- MemprofDoesNotSupportStaticLinkage();
+ DoesNotSupportStaticLinking();
// Install tool-specific callbacks in sanitizer_common.
AddDieCallback(MemprofDie);
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common.h b/compiler-rt/lib/sanitizer_common/sanitizer_common.h
index b99c0cffcbb11..999ddb2f82dbc 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common.h
@@ -290,6 +290,17 @@ bool SetEnv(const char *name, const char *value);
u32 GetUid();
void ReExec();
+
+#if defined(__ELF__) && !SANITIZER_FUCHSIA
+extern uptr kDynamic[] asm("_DYNAMIC");
+inline void DoesNotSupportStaticLinking() {
+ // This will fail to link with -static. However, -static-pie is not detected.
+ [[maybe_unused]] volatile auto x = &kDynamic;
+}
+#else
+inline void DoesNotSupportStaticLinking() {}
+#endif
+
void CheckASLR();
void CheckMPROTECT();
char **GetArgv();
More information about the llvm-commits
mailing list