Skip to content

Commit 8443ce5

Browse files
authored
[sanitizer] Lift AsanDoesNotSupportStaticLinkage to sanitizer_common.h. NFC (llvm#80948)
The `_DYNAMIC` reference from `AsanDoesNotSupportStaticLinkage` ensures that `clang++ -fsanitize=address -static` gets a linker error. `MemprofDoesNotSupportStaticLinkage` is similar for `-fmemory-profile`. Move the functions to sanitizer_common.h to be used by more sanitizers on ELF platforms. Fuchsia does not use interposition and opts out the check (its `AsanDoesNotSupportStaticLinkage` is a no-op).
1 parent db836f6 commit 8443ce5

10 files changed

+15
-30
lines changed

compiler-rt/lib/asan/asan_fuchsia.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ void AsanCheckDynamicRTPrereqs() {}
5757
void AsanCheckIncompatibleRT() {}
5858
void InitializeAsanInterceptors() {}
5959

60-
void *AsanDoesNotSupportStaticLinkage() { return nullptr; }
61-
6260
void InitializePlatformExceptionHandlers() {}
6361
void AsanOnDeadlySignal(int signo, void *siginfo, void *context) {
6462
UNIMPLEMENTED();

compiler-rt/lib/asan/asan_internal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ void ReplaceSystemMalloc();
8080

8181
// asan_linux.cpp / asan_mac.cpp / asan_win.cpp
8282
uptr FindDynamicShadowStart();
83-
void *AsanDoesNotSupportStaticLinkage();
8483
void AsanCheckDynamicRTPrereqs();
8584
void AsanCheckIncompatibleRT();
8685

compiler-rt/lib/asan/asan_linux.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,12 @@
4747

4848
# if SANITIZER_ANDROID || SANITIZER_FREEBSD || SANITIZER_SOLARIS
4949
# include <ucontext.h>
50-
extern "C" void *_DYNAMIC;
5150
# elif SANITIZER_NETBSD
5251
# include <link_elf.h>
5352
# include <ucontext.h>
54-
extern Elf_Dyn _DYNAMIC;
5553
# else
5654
# include <link.h>
5755
# include <sys/ucontext.h>
58-
extern ElfW(Dyn) _DYNAMIC[];
5956
# endif
6057

6158
typedef enum {
@@ -76,11 +73,6 @@ void InitializePlatformInterceptors() {}
7673
void InitializePlatformExceptionHandlers() {}
7774
bool IsSystemHeapAddress(uptr addr) { return false; }
7875

79-
void *AsanDoesNotSupportStaticLinkage() {
80-
// This will fail to link with -static.
81-
return &_DYNAMIC;
82-
}
83-
8476
# if ASAN_PREMAP_SHADOW
8577
uptr FindPremappedShadowStart(uptr shadow_size_bytes) {
8678
uptr granularity = GetMmapGranularity();

compiler-rt/lib/asan/asan_mac.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ void InitializePlatformInterceptors() {}
4949
void InitializePlatformExceptionHandlers() {}
5050
bool IsSystemHeapAddress (uptr addr) { return false; }
5151

52-
// No-op. Mac does not support static linkage anyway.
53-
void *AsanDoesNotSupportStaticLinkage() {
54-
return 0;
55-
}
56-
5752
uptr FindDynamicShadowStart() {
5853
return MapDynamicShadow(MemToShadowSize(kHighMemEnd), ASAN_SHADOW_SCALE,
5954
/*min_shadow_base_alignment*/ 0, kHighMemEnd);

compiler-rt/lib/asan/asan_rtl.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,8 @@ static bool AsanInitInternal() {
410410
return false;
411411
}
412412

413+
// Make sure we are not statically linked.
414+
__interception::DoesNotSupportStaticLinking();
413415
AsanCheckIncompatibleRT();
414416
AsanCheckDynamicRTPrereqs();
415417
AvoidCVE_2016_2143();
@@ -421,9 +423,6 @@ static bool AsanInitInternal() {
421423

422424
InitializeHighMemEnd();
423425

424-
// Make sure we are not statically linked.
425-
AsanDoesNotSupportStaticLinkage();
426-
427426
// Install tool-specific callbacks in sanitizer_common.
428427
AddDieCallback(AsanDie);
429428
SetCheckUnwindCallback(CheckUnwind);

compiler-rt/lib/asan/asan_win.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,6 @@ void PlatformTSDDtor(void *tsd) { AsanThread::TSDDtor(tsd); }
266266
// }}}
267267

268268
// ---------------------- Various stuff ---------------- {{{
269-
void *AsanDoesNotSupportStaticLinkage() { return 0; }
270-
271269
uptr FindDynamicShadowStart() {
272270
return MapDynamicShadow(MemToShadowSize(kHighMemEnd), ASAN_SHADOW_SCALE,
273271
/*min_shadow_base_alignment*/ 0, kHighMemEnd);

compiler-rt/lib/interception/interception.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,18 @@ typedef unsigned long long uptr;
348348
#else
349349
typedef unsigned long uptr;
350350
#endif // _WIN64
351+
352+
#if defined(__ELF__) && !SANITIZER_FUCHSIA
353+
// The use of interceptors makes many sanitizers unusable for static linking.
354+
// Define a function, if called, will cause a linker error (undefined _DYNAMIC).
355+
// However, -static-pie (which is not common) cannot be detected at link time.
356+
extern uptr kDynamic[] asm("_DYNAMIC");
357+
inline void DoesNotSupportStaticLinking() {
358+
[[maybe_unused]] volatile auto x = &kDynamic;
359+
}
360+
#else
361+
inline void DoesNotSupportStaticLinking() {}
362+
#endif
351363
} // namespace __interception
352364

353365
#define INCLUDED_FROM_INTERCEPTION_LIB

compiler-rt/lib/memprof/memprof_internal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ void ReplaceSystemMalloc();
5757

5858
// memprof_linux.cpp
5959
uptr FindDynamicShadowStart();
60-
void *MemprofDoesNotSupportStaticLinkage();
6160

6261
// memprof_thread.cpp
6362
MemprofThread *CreateMainThread();

compiler-rt/lib/memprof/memprof_linux.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
#include <unistd.h>
3939
#include <unwind.h>
4040

41-
extern ElfW(Dyn) _DYNAMIC[];
42-
4341
typedef enum {
4442
MEMPROF_RT_VERSION_UNDEFINED = 0,
4543
MEMPROF_RT_VERSION_DYNAMIC,
@@ -57,11 +55,6 @@ namespace __memprof {
5755
void InitializePlatformInterceptors() {}
5856
void InitializePlatformExceptionHandlers() {}
5957

60-
void *MemprofDoesNotSupportStaticLinkage() {
61-
// This will fail to link with -static.
62-
return &_DYNAMIC;
63-
}
64-
6558
uptr FindDynamicShadowStart() {
6659
uptr shadow_size_bytes = MemToShadowSize(kHighMemEnd);
6760
return MapDynamicShadow(shadow_size_bytes, SHADOW_SCALE,

compiler-rt/lib/memprof/memprof_rtl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ static void MemprofInitInternal() {
162162
InitializeHighMemEnd();
163163

164164
// Make sure we are not statically linked.
165-
MemprofDoesNotSupportStaticLinkage();
165+
__interception::DoesNotSupportStaticLinking();
166166

167167
// Install tool-specific callbacks in sanitizer_common.
168168
AddDieCallback(MemprofDie);

0 commit comments

Comments
 (0)