Skip to content

Commit

Permalink
[sanitizer] Lift AsanDoesNotSupportStaticLinkage to sanitizer_common.…
Browse files Browse the repository at this point in the history
…h. NFC (#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).
  • Loading branch information
MaskRay committed Feb 17, 2024
1 parent db836f6 commit 8443ce5
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 30 deletions.
2 changes: 0 additions & 2 deletions compiler-rt/lib/asan/asan_fuchsia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 0 additions & 1 deletion compiler-rt/lib/asan/asan_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ void ReplaceSystemMalloc();

// asan_linux.cpp / asan_mac.cpp / asan_win.cpp
uptr FindDynamicShadowStart();
void *AsanDoesNotSupportStaticLinkage();
void AsanCheckDynamicRTPrereqs();
void AsanCheckIncompatibleRT();

Expand Down
8 changes: 0 additions & 8 deletions compiler-rt/lib/asan/asan_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,12 @@

# if SANITIZER_ANDROID || SANITIZER_FREEBSD || SANITIZER_SOLARIS
# include <ucontext.h>
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 {
Expand All @@ -76,11 +73,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();
Expand Down
5 changes: 0 additions & 5 deletions compiler-rt/lib/asan/asan_mac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions compiler-rt/lib/asan/asan_rtl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ static bool AsanInitInternal() {
return false;
}

// Make sure we are not statically linked.
__interception::DoesNotSupportStaticLinking();
AsanCheckIncompatibleRT();
AsanCheckDynamicRTPrereqs();
AvoidCVE_2016_2143();
Expand All @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions compiler-rt/lib/asan/asan_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions compiler-rt/lib/interception/interception.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,18 @@ typedef unsigned long long uptr;
#else
typedef unsigned long uptr;
#endif // _WIN64

#if defined(__ELF__) && !SANITIZER_FUCHSIA
// The use of interceptors makes many sanitizers unusable for static linking.
// Define a function, if called, will cause a linker error (undefined _DYNAMIC).
// However, -static-pie (which is not common) cannot be detected at link time.
extern uptr kDynamic[] asm("_DYNAMIC");
inline void DoesNotSupportStaticLinking() {
[[maybe_unused]] volatile auto x = &kDynamic;
}
#else
inline void DoesNotSupportStaticLinking() {}
#endif
} // namespace __interception

#define INCLUDED_FROM_INTERCEPTION_LIB
Expand Down
1 change: 0 additions & 1 deletion compiler-rt/lib/memprof/memprof_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ void ReplaceSystemMalloc();

// memprof_linux.cpp
uptr FindDynamicShadowStart();
void *MemprofDoesNotSupportStaticLinkage();

// memprof_thread.cpp
MemprofThread *CreateMainThread();
Expand Down
7 changes: 0 additions & 7 deletions compiler-rt/lib/memprof/memprof_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/memprof/memprof_rtl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ static void MemprofInitInternal() {
InitializeHighMemEnd();

// Make sure we are not statically linked.
MemprofDoesNotSupportStaticLinkage();
__interception::DoesNotSupportStaticLinking();

// Install tool-specific callbacks in sanitizer_common.
AddDieCallback(MemprofDie);
Expand Down

0 comments on commit 8443ce5

Please sign in to comment.