[compiler-rt] [NFC][sanitizer] Simplify splitting TLS and stack (PR #108672)
Vitaly Buka via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 17 13:18:27 PDT 2024
https://github.com/vitalybuka updated https://github.com/llvm/llvm-project/pull/108672
>From 97b6e37d4ada7a0f02988ae992606d3d42ebae96 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Fri, 13 Sep 2024 20:06:26 -0700
Subject: [PATCH 1/8] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20ch?=
=?UTF-8?q?anges=20to=20main=20this=20commit=20is=20based=20on?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.4
[skip ci]
---
.../test/sanitizer_common/TestCases/Linux/tls_get_addr.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/tls_get_addr.c b/compiler-rt/test/sanitizer_common/TestCases/Linux/tls_get_addr.c
index 2f88c22d19dbf9..0aff6039ac4e8a 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/Linux/tls_get_addr.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/tls_get_addr.c
@@ -10,6 +10,9 @@
// These don't intercept __tls_get_addr.
// XFAIL: lsan,hwasan,ubsan
+// FIXME: Fails for unknown reasons.
+// UNSUPPORTED: powerpc64le-target-arch
+
#ifndef BUILD_SO
# include <assert.h>
# include <dlfcn.h>
>From 05d12ada822a0e19b83f6b831a4815ef55856e7b Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Mon, 16 Sep 2024 10:18:22 -0700
Subject: [PATCH 2/8] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20ch?=
=?UTF-8?q?anges=20introduced=20through=20rebase?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.4
[skip ci]
---
.../sanitizer_linux_libcdep.cpp | 18 ++++++++----------
1 file changed, 8 insertions(+), 10 deletions(-)
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
index 55889ae1222ad8..61bbd3ae3b7c3c 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
@@ -40,6 +40,10 @@
# include <sys/resource.h>
# include <syslog.h>
+# ifdef SANITIZER_GLIBC
+# include <gnu/libc-version.h>
+# endif
+
# if !defined(ElfW)
# define ElfW(type) Elf_##type
# endif
@@ -198,17 +202,11 @@ bool SetEnv(const char *name, const char *value) {
__attribute__((unused)) static bool GetLibcVersion(int *major, int *minor,
int *patch) {
-# ifdef _CS_GNU_LIBC_VERSION
- char buf[64];
- uptr len = confstr(_CS_GNU_LIBC_VERSION, buf, sizeof(buf));
- if (len >= sizeof(buf))
- return false;
- buf[len] = 0;
- static const char kGLibC[] = "glibc ";
- if (internal_strncmp(buf, kGLibC, sizeof(kGLibC) - 1) != 0)
- return false;
- const char *p = buf + sizeof(kGLibC) - 1;
+# ifdef SANITIZER_GLIBC
+ const char *p = gnu_get_libc_version();
*major = internal_simple_strtoll(p, &p, 10);
+ // Caller does not expect anything else.
+ CHECK_EQ(*major, 2);
*minor = (*p == '.') ? internal_simple_strtoll(p + 1, &p, 10) : 0;
*patch = (*p == '.') ? internal_simple_strtoll(p + 1, &p, 10) : 0;
return true;
>From e6793e526699f4aebd6f222c8d0167d9c11a802f Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Mon, 16 Sep 2024 10:18:48 -0700
Subject: [PATCH 3/8] CHECK
Created using spr 1.3.4
---
.../sanitizer_linux_libcdep.cpp | 18 ++++++++++--------
1 file changed, 10 insertions(+), 8 deletions(-)
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
index d40fc54732f48c..20d112c3155d08 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
@@ -40,10 +40,6 @@
# include <sys/resource.h>
# include <syslog.h>
-# ifdef SANITIZER_GLIBC
-# include <gnu/libc-version.h>
-# endif
-
# if !defined(ElfW)
# define ElfW(type) Elf_##type
# endif
@@ -202,11 +198,17 @@ bool SetEnv(const char *name, const char *value) {
__attribute__((unused)) static bool GetLibcVersion(int *major, int *minor,
int *patch) {
-# ifdef SANITIZER_GLIBC
- const char *p = gnu_get_libc_version();
+# ifdef _CS_GNU_LIBC_VERSION
+ char buf[64];
+ uptr len = confstr(_CS_GNU_LIBC_VERSION, buf, sizeof(buf));
+ if (len >= sizeof(buf))
+ return false;
+ buf[len] = 0;
+ static const char kGLibC[] = "glibc ";
+ if (internal_strncmp(buf, kGLibC, sizeof(kGLibC) - 1) != 0)
+ return false;
+ const char *p = buf + sizeof(kGLibC) - 1;
*major = internal_simple_strtoll(p, &p, 10);
- // Caller does not expect anything else.
- CHECK_EQ(*major, 2);
*minor = (*p == '.') ? internal_simple_strtoll(p + 1, &p, 10) : 0;
*patch = (*p == '.') ? internal_simple_strtoll(p + 1, &p, 10) : 0;
return true;
>From 89b97e97e2e6b57ff2f7ff9c226b3baa17bd3984 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Mon, 16 Sep 2024 10:37:23 -0700
Subject: [PATCH 4/8] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20ch?=
=?UTF-8?q?anges=20introduced=20through=20rebase?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.4
[skip ci]
---
.../sanitizer_linux_libcdep.cpp | 18 ++++++++----------
1 file changed, 8 insertions(+), 10 deletions(-)
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
index 55889ae1222ad8..61bbd3ae3b7c3c 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
@@ -40,6 +40,10 @@
# include <sys/resource.h>
# include <syslog.h>
+# ifdef SANITIZER_GLIBC
+# include <gnu/libc-version.h>
+# endif
+
# if !defined(ElfW)
# define ElfW(type) Elf_##type
# endif
@@ -198,17 +202,11 @@ bool SetEnv(const char *name, const char *value) {
__attribute__((unused)) static bool GetLibcVersion(int *major, int *minor,
int *patch) {
-# ifdef _CS_GNU_LIBC_VERSION
- char buf[64];
- uptr len = confstr(_CS_GNU_LIBC_VERSION, buf, sizeof(buf));
- if (len >= sizeof(buf))
- return false;
- buf[len] = 0;
- static const char kGLibC[] = "glibc ";
- if (internal_strncmp(buf, kGLibC, sizeof(kGLibC) - 1) != 0)
- return false;
- const char *p = buf + sizeof(kGLibC) - 1;
+# ifdef SANITIZER_GLIBC
+ const char *p = gnu_get_libc_version();
*major = internal_simple_strtoll(p, &p, 10);
+ // Caller does not expect anything else.
+ CHECK_EQ(*major, 2);
*minor = (*p == '.') ? internal_simple_strtoll(p + 1, &p, 10) : 0;
*patch = (*p == '.') ? internal_simple_strtoll(p + 1, &p, 10) : 0;
return true;
>From f7677660ae1997ac8271d23be2c933d5a30870d0 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Mon, 16 Sep 2024 10:50:06 -0700
Subject: [PATCH 5/8] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20ch?=
=?UTF-8?q?anges=20introduced=20through=20rebase?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.4
[skip ci]
---
.../sanitizer_linux_libcdep.cpp | 18 ++++++++----------
1 file changed, 8 insertions(+), 10 deletions(-)
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
index 55889ae1222ad8..61bbd3ae3b7c3c 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
@@ -40,6 +40,10 @@
# include <sys/resource.h>
# include <syslog.h>
+# ifdef SANITIZER_GLIBC
+# include <gnu/libc-version.h>
+# endif
+
# if !defined(ElfW)
# define ElfW(type) Elf_##type
# endif
@@ -198,17 +202,11 @@ bool SetEnv(const char *name, const char *value) {
__attribute__((unused)) static bool GetLibcVersion(int *major, int *minor,
int *patch) {
-# ifdef _CS_GNU_LIBC_VERSION
- char buf[64];
- uptr len = confstr(_CS_GNU_LIBC_VERSION, buf, sizeof(buf));
- if (len >= sizeof(buf))
- return false;
- buf[len] = 0;
- static const char kGLibC[] = "glibc ";
- if (internal_strncmp(buf, kGLibC, sizeof(kGLibC) - 1) != 0)
- return false;
- const char *p = buf + sizeof(kGLibC) - 1;
+# ifdef SANITIZER_GLIBC
+ const char *p = gnu_get_libc_version();
*major = internal_simple_strtoll(p, &p, 10);
+ // Caller does not expect anything else.
+ CHECK_EQ(*major, 2);
*minor = (*p == '.') ? internal_simple_strtoll(p + 1, &p, 10) : 0;
*patch = (*p == '.') ? internal_simple_strtoll(p + 1, &p, 10) : 0;
return true;
>From 044c911aa7eae9a778f36b7b316a69d8ebc03edd Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Mon, 16 Sep 2024 10:50:40 -0700
Subject: [PATCH 6/8] drop if
Created using spr 1.3.4
---
.../sanitizer_linux_libcdep.cpp | 18 ++++++++++--------
1 file changed, 10 insertions(+), 8 deletions(-)
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
index 863ae951d34a6c..0239d485dd5c83 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
@@ -40,10 +40,6 @@
# include <sys/resource.h>
# include <syslog.h>
-# ifdef SANITIZER_GLIBC
-# include <gnu/libc-version.h>
-# endif
-
# if !defined(ElfW)
# define ElfW(type) Elf_##type
# endif
@@ -202,11 +198,17 @@ bool SetEnv(const char *name, const char *value) {
__attribute__((unused)) static bool GetLibcVersion(int *major, int *minor,
int *patch) {
-# ifdef SANITIZER_GLIBC
- const char *p = gnu_get_libc_version();
+# ifdef _CS_GNU_LIBC_VERSION
+ char buf[64];
+ uptr len = confstr(_CS_GNU_LIBC_VERSION, buf, sizeof(buf));
+ if (len >= sizeof(buf))
+ return false;
+ buf[len] = 0;
+ static const char kGLibC[] = "glibc ";
+ if (internal_strncmp(buf, kGLibC, sizeof(kGLibC) - 1) != 0)
+ return false;
+ const char *p = buf + sizeof(kGLibC) - 1;
*major = internal_simple_strtoll(p, &p, 10);
- // Caller does not expect anything else.
- CHECK_EQ(*major, 2);
*minor = (*p == '.') ? internal_simple_strtoll(p + 1, &p, 10) : 0;
*patch = (*p == '.') ? internal_simple_strtoll(p + 1, &p, 10) : 0;
return true;
>From c696b88073f581cb7a7f1b63acebadc82708c48f Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Mon, 16 Sep 2024 20:52:25 -0700
Subject: [PATCH 7/8] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20ch?=
=?UTF-8?q?anges=20introduced=20through=20rebase?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.4
[skip ci]
---
compiler-rt/lib/asan/asan_rtl.cpp | 3 -
compiler-rt/lib/dfsan/dfsan.cpp | 2 +
compiler-rt/lib/hwasan/hwasan.cpp | 4 +-
compiler-rt/lib/lsan/lsan.cpp | 2 +-
compiler-rt/lib/memprof/memprof_rtl.cpp | 3 -
compiler-rt/lib/msan/msan.cpp | 3 +-
.../sanitizer_common_nolibc.cpp | 1 +
.../sanitizer_common/sanitizer_fuchsia.cpp | 1 -
.../lib/sanitizer_common/sanitizer_linux.cpp | 4 +-
.../sanitizer_linux_libcdep.cpp | 199 ++++++++++--------
.../lib/sanitizer_common/sanitizer_mac.cpp | 3 -
.../lib/sanitizer_common/sanitizer_win.cpp | 3 -
.../tests/sanitizer_linux_test.cpp | 2 +
.../lib/tsan/rtl/tsan_interceptors_posix.cpp | 56 ++---
.../lib/tsan/rtl/tsan_platform_linux.cpp | 1 -
compiler-rt/lib/ubsan/ubsan_init.cpp | 2 +-
.../sanitizer_common/TestCases/dlsym_alloc.c | 2 -
17 files changed, 152 insertions(+), 139 deletions(-)
diff --git a/compiler-rt/lib/asan/asan_rtl.cpp b/compiler-rt/lib/asan/asan_rtl.cpp
index a390802af28d09..19c6c210b564c5 100644
--- a/compiler-rt/lib/asan/asan_rtl.cpp
+++ b/compiler-rt/lib/asan/asan_rtl.cpp
@@ -478,9 +478,6 @@ static bool AsanInitInternal() {
if (flags()->start_deactivated)
AsanDeactivate();
- // interceptors
- InitTlsSize();
-
// Create main thread.
AsanThread *main_thread = CreateMainThread();
CHECK_EQ(0, main_thread->tid());
diff --git a/compiler-rt/lib/dfsan/dfsan.cpp b/compiler-rt/lib/dfsan/dfsan.cpp
index 1972a07d15ac51..886e93e5fa8139 100644
--- a/compiler-rt/lib/dfsan/dfsan.cpp
+++ b/compiler-rt/lib/dfsan/dfsan.cpp
@@ -1262,6 +1262,8 @@ static void DFsanInit(int argc, char **argv, char **envp) {
CheckASLR();
+ InitializePlatformEarly();
+
if (!InitShadowWithReExec(dfsan_get_track_origins())) {
Printf("FATAL: DataflowSanitizer can not mmap the shadow memory.\n");
DumpProcessMap();
diff --git a/compiler-rt/lib/hwasan/hwasan.cpp b/compiler-rt/lib/hwasan/hwasan.cpp
index ccdc0b4bc21bd3..24384d8b4d2cf1 100644
--- a/compiler-rt/lib/hwasan/hwasan.cpp
+++ b/compiler-rt/lib/hwasan/hwasan.cpp
@@ -357,8 +357,6 @@ __attribute__((constructor(0))) void __hwasan_init() {
hwasan_init_is_running = 1;
SanitizerToolName = "HWAddressSanitizer";
- InitTlsSize();
-
CacheBinaryName();
InitializeFlags();
@@ -367,6 +365,8 @@ __attribute__((constructor(0))) void __hwasan_init() {
__sanitizer_set_report_path(common_flags()->log_path);
+ InitializePlatformEarly();
+
AndroidTestTlsSlot();
DisableCoreDumperIfNecessary();
diff --git a/compiler-rt/lib/lsan/lsan.cpp b/compiler-rt/lib/lsan/lsan.cpp
index 7a27b600f203f7..798294b499e2f0 100644
--- a/compiler-rt/lib/lsan/lsan.cpp
+++ b/compiler-rt/lib/lsan/lsan.cpp
@@ -92,10 +92,10 @@ extern "C" void __lsan_init() {
CacheBinaryName();
AvoidCVE_2016_2143();
InitializeFlags();
+ InitializePlatformEarly();
InitCommonLsan();
InitializeAllocator();
ReplaceSystemMalloc();
- InitTlsSize();
InitializeInterceptors();
InitializeThreads();
InstallDeadlySignalHandlers(LsanOnDeadlySignal);
diff --git a/compiler-rt/lib/memprof/memprof_rtl.cpp b/compiler-rt/lib/memprof/memprof_rtl.cpp
index cf4bde808bfad6..2cc6c2df5a6fe4 100644
--- a/compiler-rt/lib/memprof/memprof_rtl.cpp
+++ b/compiler-rt/lib/memprof/memprof_rtl.cpp
@@ -213,9 +213,6 @@ static void MemprofInitInternal() {
InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
- // interceptors
- InitTlsSize();
-
// Create main thread.
MemprofThread *main_thread = CreateMainThread();
CHECK_EQ(0, main_thread->tid());
diff --git a/compiler-rt/lib/msan/msan.cpp b/compiler-rt/lib/msan/msan.cpp
index 2ee05f43ec5e56..6c27ab21eeebfd 100644
--- a/compiler-rt/lib/msan/msan.cpp
+++ b/compiler-rt/lib/msan/msan.cpp
@@ -457,10 +457,11 @@ void __msan_init() {
__sanitizer_set_report_path(common_flags()->log_path);
+ InitializePlatformEarly();
+
InitializeInterceptors();
InstallAtForkHandler();
CheckASLR();
- InitTlsSize();
InstallDeadlySignalHandlers(MsanOnDeadlySignal);
InstallAtExitHandler(); // Needs __cxa_atexit interceptor.
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_nolibc.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_common_nolibc.cpp
index 7d88575160c6c6..e49285f22dff99 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common_nolibc.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_nolibc.cpp
@@ -22,6 +22,7 @@ namespace __sanitizer {
#if !SANITIZER_WINDOWS
# if SANITIZER_LINUX
void LogMessageOnPrintf(const char *str) {}
+void InitTlsSize() {}
# endif
void WriteToSyslog(const char *buffer) {}
void Abort() { internal__exit(1); }
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_fuchsia.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_fuchsia.cpp
index a67b2a8725eca8..75dcf546729f6e 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_fuchsia.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_fuchsia.cpp
@@ -94,7 +94,6 @@ void DisableCoreDumperIfNecessary() {}
void InstallDeadlySignalHandlers(SignalHandlerType handler) {}
void SetAlternateSignalStack() {}
void UnsetAlternateSignalStack() {}
-void InitTlsSize() {}
bool SignalContext::IsStackOverflow() const { return false; }
void SignalContext::DumpAllRegisters(void *context) { UNIMPLEMENTED(); }
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
index 6359f4348e3c48..1c637d109649b6 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
@@ -2672,9 +2672,7 @@ static void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
void SignalContext::InitPcSpBp() { GetPcSpBp(context, &pc, &sp, &bp); }
-void InitializePlatformEarly() {
- // Do nothing.
-}
+void InitializePlatformEarly() { InitTlsSize(); }
void CheckASLR() {
# if SANITIZER_NETBSD
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
index a5101291904430..aa156acd7b657a 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
@@ -200,21 +200,6 @@ bool SetEnv(const char *name, const char *value) {
}
# endif
-__attribute__((unused)) static bool GetLibcVersion(int *major, int *minor,
- int *patch) {
-# if SANITIZER_GLIBC
- const char *p = gnu_get_libc_version();
- *major = internal_simple_strtoll(p, &p, 10);
- // Caller does not expect anything else.
- CHECK_EQ(*major, 2);
- *minor = (*p == '.') ? internal_simple_strtoll(p + 1, &p, 10) : 0;
- *patch = (*p == '.') ? internal_simple_strtoll(p + 1, &p, 10) : 0;
- return true;
-# else
- return false;
-# endif
-}
-
// True if we can use dlpi_tls_data. glibc before 2.25 may leave NULL (BZ
// #19826) so dlpi_tls_data cannot be used.
//
@@ -224,110 +209,137 @@ __attribute__((unused)) static bool GetLibcVersion(int *major, int *minor,
__attribute__((unused)) static int g_use_dlpi_tls_data;
# if SANITIZER_GLIBC && !SANITIZER_GO
-__attribute__((unused)) static size_t g_tls_size;
-void InitTlsSize() {
- int major, minor, patch;
- g_use_dlpi_tls_data =
- GetLibcVersion(&major, &minor, &patch) && major == 2 && minor >= 25;
-
-# if defined(__aarch64__) || defined(__x86_64__) || \
- defined(__powerpc64__) || defined(__loongarch__)
- void *get_tls_static_info = dlsym(RTLD_DEFAULT, "_dl_get_tls_static_info");
- size_t tls_align;
- ((void (*)(size_t *, size_t *))get_tls_static_info)(&g_tls_size, &tls_align);
-# endif
+static void GetGLibcVersion(int *major, int *minor, int *patch) {
+ const char *p = gnu_get_libc_version();
+ *major = internal_simple_strtoll(p, &p, 10);
+ // Caller does not expect anything else.
+ CHECK_EQ(*major, 2);
+ *minor = (*p == '.') ? internal_simple_strtoll(p + 1, &p, 10) : 0;
+ *patch = (*p == '.') ? internal_simple_strtoll(p + 1, &p, 10) : 0;
}
-# else
-void InitTlsSize() {}
# endif // SANITIZER_GLIBC && !SANITIZER_GO
// On glibc x86_64, ThreadDescriptorSize() needs to be precise due to the usage
// of g_tls_size. On other targets, ThreadDescriptorSize() is only used by lsan
// to get the pointer to thread-specific data keys in the thread control block.
-# if (SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_SOLARIS) && \
- !SANITIZER_ANDROID && !SANITIZER_GO
+# if (SANITIZER_FREEBSD || SANITIZER_GLIBC) && !SANITIZER_GO
// sizeof(struct pthread) from glibc.
-static atomic_uintptr_t thread_descriptor_size;
+static uptr thread_descriptor_size;
+// FIXME: Implementation is very GLIBC specific, but it's used by FREEBSD.
static uptr ThreadDescriptorSizeFallback() {
-# if defined(__x86_64__) || defined(__i386__) || defined(__arm__)
+# if defined(__x86_64__) || defined(__i386__) || defined(__arm__) || \
+ SANITIZER_RISCV64
+# if SANITIZER_GLIBC
int major;
int minor;
int patch;
- if (GetLibcVersion(&major, &minor, &patch) && major == 2) {
- /* sizeof(struct pthread) values from various glibc versions. */
- if (SANITIZER_X32)
- return 1728; // Assume only one particular version for x32.
- // For ARM sizeof(struct pthread) changed in Glibc 2.23.
- if (SANITIZER_ARM)
- return minor <= 22 ? 1120 : 1216;
- if (minor <= 3)
- return FIRST_32_SECOND_64(1104, 1696);
- if (minor == 4)
- return FIRST_32_SECOND_64(1120, 1728);
- if (minor == 5)
- return FIRST_32_SECOND_64(1136, 1728);
- if (minor <= 9)
- return FIRST_32_SECOND_64(1136, 1712);
- if (minor == 10)
- return FIRST_32_SECOND_64(1168, 1776);
- if (minor == 11 || (minor == 12 && patch == 1))
- return FIRST_32_SECOND_64(1168, 2288);
- if (minor <= 14)
- return FIRST_32_SECOND_64(1168, 2304);
- if (minor < 32) // Unknown version
- return FIRST_32_SECOND_64(1216, 2304);
- // minor == 32
- return FIRST_32_SECOND_64(1344, 2496);
- }
+ GetGLibcVersion(&major, &minor, &patch);
+# else // SANITIZER_GLIBC
return 0;
-# elif defined(__s390__) || defined(__sparc__)
+# endif // SANITIZER_GLIBC
+# endif
+
+# if defined(__x86_64__) || defined(__i386__) || defined(__arm__)
+ /* sizeof(struct pthread) values from various glibc versions. */
+ if (SANITIZER_X32)
+ return 1728; // Assume only one particular version for x32.
+ // For ARM sizeof(struct pthread) changed in Glibc 2.23.
+ if (SANITIZER_ARM)
+ return minor <= 22 ? 1120 : 1216;
+ if (minor <= 3)
+ return FIRST_32_SECOND_64(1104, 1696);
+ if (minor == 4)
+ return FIRST_32_SECOND_64(1120, 1728);
+ if (minor == 5)
+ return FIRST_32_SECOND_64(1136, 1728);
+ if (minor <= 9)
+ return FIRST_32_SECOND_64(1136, 1712);
+ if (minor == 10)
+ return FIRST_32_SECOND_64(1168, 1776);
+ if (minor == 11 || (minor == 12 && patch == 1))
+ return FIRST_32_SECOND_64(1168, 2288);
+ if (minor <= 14)
+ return FIRST_32_SECOND_64(1168, 2304);
+ if (minor < 32) // Unknown version
+ return FIRST_32_SECOND_64(1216, 2304);
+ // minor == 32
+ return FIRST_32_SECOND_64(1344, 2496);
+# endif
+
+# if SANITIZER_RISCV64
+ // TODO: consider adding an optional runtime check for an unknown (untested)
+ // glibc version
+ if (minor <= 28) // WARNING: the highest tested version is 2.29
+ return 1772; // no guarantees for this one
+ if (minor <= 31)
+ return 1772; // tested against glibc 2.29, 2.31
+ return 1936; // tested against glibc 2.32
+# endif
+
+# if defined(__s390__) || defined(__sparc__)
// The size of a prefix of TCB including pthread::{specific_1stblock,specific}
// suffices. Just return offsetof(struct pthread, specific_used), which hasn't
// changed since 2007-05. Technically this applies to i386/x86_64 as well but
// we call _dl_get_tls_static_info and need the precise size of struct
// pthread.
return FIRST_32_SECOND_64(524, 1552);
-# elif defined(__mips__)
+# endif
+
+# if defined(__mips__)
// TODO(sagarthakur): add more values as per different glibc versions.
return FIRST_32_SECOND_64(1152, 1776);
-# elif SANITIZER_LOONGARCH64
+# endif
+
+# if SANITIZER_LOONGARCH64
return 1856; // from glibc 2.36
-# elif SANITIZER_RISCV64
- int major;
- int minor;
- int patch;
- if (GetLibcVersion(&major, &minor, &patch) && major == 2) {
- // TODO: consider adding an optional runtime check for an unknown (untested)
- // glibc version
- if (minor <= 28) // WARNING: the highest tested version is 2.29
- return 1772; // no guarantees for this one
- if (minor <= 31)
- return 1772; // tested against glibc 2.29, 2.31
- return 1936; // tested against glibc 2.32
- }
- return 0;
-# elif defined(__aarch64__)
+# endif
+
+# if defined(__aarch64__)
// The sizeof (struct pthread) is the same from GLIBC 2.17 to 2.22.
return 1776;
-# elif defined(__powerpc64__)
+# endif
+
+# if defined(__powerpc64__)
return 1776; // from glibc.ppc64le 2.20-8.fc21
# endif
}
-uptr ThreadDescriptorSize() {
- uptr val = atomic_load_relaxed(&thread_descriptor_size);
- if (val)
- return val;
- // _thread_db_sizeof_pthread is a GLIBC_PRIVATE symbol that is exported in
- // glibc 2.34 and later.
- if (unsigned *psizeof = static_cast<unsigned *>(
- dlsym(RTLD_DEFAULT, "_thread_db_sizeof_pthread")))
- val = *psizeof;
- if (!val)
- val = ThreadDescriptorSizeFallback();
- atomic_store_relaxed(&thread_descriptor_size, val);
- return val;
+uptr ThreadDescriptorSize() { return thread_descriptor_size; }
+
+# if SANITIZER_GLIBC
+__attribute__((unused)) static size_t g_tls_size;
+# endif
+
+void InitTlsSize() {
+# if SANITIZER_GLIBC
+ int major, minor, patch;
+ GetGLibcVersion(&major, &minor, &patch);
+ g_use_dlpi_tls_data = major == 2 && minor >= 25;
+
+ if (major == 2 && minor >= 34) {
+ // _thread_db_sizeof_pthread is a GLIBC_PRIVATE symbol that is exported in
+ // glibc 2.34 and later.
+ if (unsigned *psizeof = static_cast<unsigned *>(
+ dlsym(RTLD_DEFAULT, "_thread_db_sizeof_pthread"))) {
+ thread_descriptor_size = *psizeof;
+ }
+ }
+
+# if defined(__aarch64__) || defined(__x86_64__) || \
+ defined(__powerpc64__) || defined(__loongarch__)
+ auto *get_tls_static_info = (void (*)(size_t *, size_t *))dlsym(
+ RTLD_DEFAULT, "_dl_get_tls_static_info");
+ size_t tls_align;
+ // Can be null if static link.
+ if (get_tls_static_info)
+ get_tls_static_info(&g_tls_size, &tls_align);
+# endif
+
+# endif // SANITIZER_GLIBC
+
+ if (!thread_descriptor_size)
+ thread_descriptor_size = ThreadDescriptorSizeFallback();
}
# if defined(__mips__) || defined(__powerpc64__) || SANITIZER_RISCV64 || \
@@ -350,7 +362,12 @@ static uptr TlsPreTcbSize() {
return kTlsPreTcbSize;
}
# endif
+# else // (SANITIZER_FREEBSD || SANITIZER_GLIBC) && !SANITIZER_GO
+void InitTlsSize() {}
+# endif // (SANITIZER_FREEBSD || SANITIZER_GLIBC) && !SANITIZER_GO
+# if (SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_SOLARIS) && \
+ !SANITIZER_ANDROID && !SANITIZER_GO
namespace {
struct TlsBlock {
uptr begin, end, align;
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
index 2a36104e6f9f29..26d2e8d4ed7680 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
@@ -545,9 +545,6 @@ uptr GetTlsSize() {
return 0;
}
-void InitTlsSize() {
-}
-
uptr TlsBaseAddr() {
uptr segbase = 0;
#if defined(__x86_64__)
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_win.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_win.cpp
index 2c8f8343519ed8..7cee571314868e 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_win.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_win.cpp
@@ -873,9 +873,6 @@ uptr GetTlsSize() {
return 0;
}
-void InitTlsSize() {
-}
-
void GetThreadStackAndTls(bool main, uptr *stk_begin, uptr *stk_end,
uptr *tls_begin, uptr *tls_end) {
# if SANITIZER_GO
diff --git a/compiler-rt/lib/sanitizer_common/tests/sanitizer_linux_test.cpp b/compiler-rt/lib/sanitizer_common/tests/sanitizer_linux_test.cpp
index 338c4d3bab2b04..b286ab72a5c795 100644
--- a/compiler-rt/lib/sanitizer_common/tests/sanitizer_linux_test.cpp
+++ b/compiler-rt/lib/sanitizer_common/tests/sanitizer_linux_test.cpp
@@ -202,6 +202,8 @@ TEST(SanitizerLinux, ThreadDescriptorSize) {
void *result;
ASSERT_EQ(0, pthread_create(&tid, 0, thread_descriptor_size_test_func, 0));
ASSERT_EQ(0, pthread_join(tid, &result));
+ EXPECT_EQ(0u, ThreadDescriptorSize());
+ InitTlsSize();
EXPECT_EQ((uptr)result, ThreadDescriptorSize());
}
# endif
diff --git a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
index 460cbacf3408cf..924339191df133 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
@@ -12,9 +12,11 @@
// sanitizer_common/sanitizer_common_interceptors.inc
//===----------------------------------------------------------------------===//
+#include "sanitizer_common/sanitizer_allocator_dlsym.h"
#include "sanitizer_common/sanitizer_atomic.h"
#include "sanitizer_common/sanitizer_errno.h"
#include "sanitizer_common/sanitizer_glibc_version.h"
+#include "sanitizer_common/sanitizer_internal_defs.h"
#include "sanitizer_common/sanitizer_libc.h"
#include "sanitizer_common/sanitizer_linux.h"
#include "sanitizer_common/sanitizer_platform_limits_netbsd.h"
@@ -252,6 +254,12 @@ SANITIZER_WEAK_CXX_DEFAULT_IMPL void OnPotentiallyBlockingRegionBegin() {}
SANITIZER_WEAK_CXX_DEFAULT_IMPL void OnPotentiallyBlockingRegionEnd() {}
#endif
+struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {
+ static bool UseImpl() {
+ return in_symbolizer() || (ctx && !ctx->initialized);
+ }
+};
+
} // namespace __tsan
static ThreadSignalContext *SigCtx(ThreadState *thr) {
@@ -659,8 +667,8 @@ TSAN_INTERCEPTOR(void, _longjmp, uptr *env, int val) {
#if !SANITIZER_APPLE
TSAN_INTERCEPTOR(void*, malloc, uptr size) {
- if (in_symbolizer())
- return InternalAlloc(size);
+ if (DlsymAlloc::Use())
+ return DlsymAlloc::Allocate(size);
void *p = 0;
{
SCOPED_INTERCEPTOR_RAW(malloc, size);
@@ -678,9 +686,9 @@ TSAN_INTERCEPTOR(void*, __libc_memalign, uptr align, uptr sz) {
return user_memalign(thr, pc, align, sz);
}
-TSAN_INTERCEPTOR(void *, calloc, uptr n, uptr size) {
- if (in_symbolizer())
- return InternalCalloc(n, size);
+TSAN_INTERCEPTOR(void*, calloc, uptr n, uptr size) {
+ if (DlsymAlloc::Use())
+ return DlsymAlloc::Callocate(n, size);
void *p = 0;
{
SCOPED_INTERCEPTOR_RAW(calloc, n, size);
@@ -691,8 +699,8 @@ TSAN_INTERCEPTOR(void *, calloc, uptr n, uptr size) {
}
TSAN_INTERCEPTOR(void*, realloc, void *p, uptr size) {
- if (in_symbolizer())
- return InternalRealloc(p, size);
+ if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(p))
+ return DlsymAlloc::Realloc(p, size);
if (p)
invoke_free_hook(p);
{
@@ -703,9 +711,9 @@ TSAN_INTERCEPTOR(void*, realloc, void *p, uptr size) {
return p;
}
-TSAN_INTERCEPTOR(void *, reallocarray, void *p, uptr n, uptr size) {
- if (in_symbolizer())
- return InternalReallocArray(p, n, size);
+TSAN_INTERCEPTOR(void*, reallocarray, void *p, uptr n, uptr size) {
+ if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(p))
+ return DlsymAlloc::ReallocArray(p, n, size);
if (p)
invoke_free_hook(p);
{
@@ -717,20 +725,20 @@ TSAN_INTERCEPTOR(void *, reallocarray, void *p, uptr n, uptr size) {
}
TSAN_INTERCEPTOR(void, free, void *p) {
- if (p == 0)
+ if (UNLIKELY(!p))
return;
- if (in_symbolizer())
- return InternalFree(p);
+ if (DlsymAlloc::PointerIsMine(p))
+ return DlsymAlloc::Free(p);
invoke_free_hook(p);
SCOPED_INTERCEPTOR_RAW(free, p);
user_free(thr, pc, p);
}
TSAN_INTERCEPTOR(void, cfree, void *p) {
- if (p == 0)
+ if (UNLIKELY(!p))
return;
- if (in_symbolizer())
- return InternalFree(p);
+ if (DlsymAlloc::PointerIsMine(p))
+ return DlsymAlloc::Free(p);
invoke_free_hook(p);
SCOPED_INTERCEPTOR_RAW(cfree, p);
user_free(thr, pc, p);
@@ -818,15 +826,15 @@ TSAN_INTERCEPTOR(void*, memalign, uptr align, uptr sz) {
#if !SANITIZER_APPLE
TSAN_INTERCEPTOR(void*, aligned_alloc, uptr align, uptr sz) {
- if (in_symbolizer())
- return InternalAlloc(sz, nullptr, align);
+ if (DlsymAlloc::Use())
+ return DlsymAlloc::Allocate(sz, align);
SCOPED_INTERCEPTOR_RAW(aligned_alloc, align, sz);
return user_aligned_alloc(thr, pc, align, sz);
}
TSAN_INTERCEPTOR(void*, valloc, uptr sz) {
- if (in_symbolizer())
- return InternalAlloc(sz, nullptr, GetPageSizeCached());
+ if (DlsymAlloc::Use())
+ return DlsymAlloc::Allocate(sz, GetPageSizeCached());
SCOPED_INTERCEPTOR_RAW(valloc, sz);
return user_valloc(thr, pc, sz);
}
@@ -834,10 +842,10 @@ TSAN_INTERCEPTOR(void*, valloc, uptr sz) {
#if SANITIZER_LINUX
TSAN_INTERCEPTOR(void*, pvalloc, uptr sz) {
- if (in_symbolizer()) {
+ if (DlsymAlloc::Use()) {
uptr PageSize = GetPageSizeCached();
sz = sz ? RoundUpTo(sz, PageSize) : PageSize;
- return InternalAlloc(sz, nullptr, PageSize);
+ return DlsymAlloc::Allocate(sz, PageSize);
}
SCOPED_INTERCEPTOR_RAW(pvalloc, sz);
return user_pvalloc(thr, pc, sz);
@@ -849,8 +857,8 @@ TSAN_INTERCEPTOR(void*, pvalloc, uptr sz) {
#if !SANITIZER_APPLE
TSAN_INTERCEPTOR(int, posix_memalign, void **memptr, uptr align, uptr sz) {
- if (in_symbolizer()) {
- void *p = InternalAlloc(sz, nullptr, align);
+ if (DlsymAlloc::Use()) {
+ void *p = DlsymAlloc::Allocate(sz, align);
if (!p)
return errno_ENOMEM;
*memptr = p;
diff --git a/compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp b/compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp
index 621c679a05db45..3e08a1bece98f0 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp
@@ -418,7 +418,6 @@ void InitializePlatform() {
Die();
}
- InitTlsSize();
#endif // !SANITIZER_GO
}
diff --git a/compiler-rt/lib/ubsan/ubsan_init.cpp b/compiler-rt/lib/ubsan/ubsan_init.cpp
index 5802d58896f0fe..aea7ca00e3cb3f 100644
--- a/compiler-rt/lib/ubsan/ubsan_init.cpp
+++ b/compiler-rt/lib/ubsan/ubsan_init.cpp
@@ -43,8 +43,8 @@ static void CommonStandaloneInit() {
SanitizerToolName = GetSanititizerToolName();
CacheBinaryName();
InitializeFlags();
- __sanitizer::InitializePlatformEarly();
__sanitizer_set_report_path(common_flags()->log_path);
+ __sanitizer::InitializePlatformEarly();
AndroidLogInit();
InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
CommonInit();
diff --git a/compiler-rt/test/sanitizer_common/TestCases/dlsym_alloc.c b/compiler-rt/test/sanitizer_common/TestCases/dlsym_alloc.c
index 7b5b9cf34a90f9..4aa87afe47f4ea 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/dlsym_alloc.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/dlsym_alloc.c
@@ -1,7 +1,5 @@
// RUN: %clang -O0 %s -o %t && %run %t
-// FIXME: TSAN does not use DlsymAlloc.
-// UNSUPPORTED: tsan
// FIXME: investigate why this fails on macos
// UNSUPPORTED: darwin
>From 619b4cdb826b4082bca9ceaf49a4914438ab1e33 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Mon, 16 Sep 2024 21:37:27 -0700
Subject: [PATCH 8/8] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20ch?=
=?UTF-8?q?anges=20introduced=20through=20rebase?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.4
[skip ci]
---
compiler-rt/lib/asan/asan_rtl.cpp | 3 -
compiler-rt/lib/dfsan/dfsan.cpp | 2 +
compiler-rt/lib/hwasan/hwasan.cpp | 4 +-
compiler-rt/lib/lsan/lsan.cpp | 2 +-
compiler-rt/lib/memprof/memprof_rtl.cpp | 3 -
compiler-rt/lib/msan/msan.cpp | 3 +-
.../sanitizer_common_nolibc.cpp | 1 +
.../sanitizer_common/sanitizer_fuchsia.cpp | 1 -
.../lib/sanitizer_common/sanitizer_linux.cpp | 4 +-
.../sanitizer_linux_libcdep.cpp | 199 ++++++++++--------
.../lib/sanitizer_common/sanitizer_mac.cpp | 3 -
.../lib/sanitizer_common/sanitizer_win.cpp | 3 -
.../tests/sanitizer_linux_test.cpp | 2 +
.../lib/tsan/rtl/tsan_interceptors_posix.cpp | 25 ++-
.../lib/tsan/rtl/tsan_platform_linux.cpp | 1 -
compiler-rt/lib/ubsan/ubsan_init.cpp | 2 +-
.../sanitizer_common/TestCases/dlsym_alloc.c | 2 -
17 files changed, 143 insertions(+), 117 deletions(-)
diff --git a/compiler-rt/lib/asan/asan_rtl.cpp b/compiler-rt/lib/asan/asan_rtl.cpp
index a390802af28d09..19c6c210b564c5 100644
--- a/compiler-rt/lib/asan/asan_rtl.cpp
+++ b/compiler-rt/lib/asan/asan_rtl.cpp
@@ -478,9 +478,6 @@ static bool AsanInitInternal() {
if (flags()->start_deactivated)
AsanDeactivate();
- // interceptors
- InitTlsSize();
-
// Create main thread.
AsanThread *main_thread = CreateMainThread();
CHECK_EQ(0, main_thread->tid());
diff --git a/compiler-rt/lib/dfsan/dfsan.cpp b/compiler-rt/lib/dfsan/dfsan.cpp
index 1972a07d15ac51..886e93e5fa8139 100644
--- a/compiler-rt/lib/dfsan/dfsan.cpp
+++ b/compiler-rt/lib/dfsan/dfsan.cpp
@@ -1262,6 +1262,8 @@ static void DFsanInit(int argc, char **argv, char **envp) {
CheckASLR();
+ InitializePlatformEarly();
+
if (!InitShadowWithReExec(dfsan_get_track_origins())) {
Printf("FATAL: DataflowSanitizer can not mmap the shadow memory.\n");
DumpProcessMap();
diff --git a/compiler-rt/lib/hwasan/hwasan.cpp b/compiler-rt/lib/hwasan/hwasan.cpp
index ccdc0b4bc21bd3..24384d8b4d2cf1 100644
--- a/compiler-rt/lib/hwasan/hwasan.cpp
+++ b/compiler-rt/lib/hwasan/hwasan.cpp
@@ -357,8 +357,6 @@ __attribute__((constructor(0))) void __hwasan_init() {
hwasan_init_is_running = 1;
SanitizerToolName = "HWAddressSanitizer";
- InitTlsSize();
-
CacheBinaryName();
InitializeFlags();
@@ -367,6 +365,8 @@ __attribute__((constructor(0))) void __hwasan_init() {
__sanitizer_set_report_path(common_flags()->log_path);
+ InitializePlatformEarly();
+
AndroidTestTlsSlot();
DisableCoreDumperIfNecessary();
diff --git a/compiler-rt/lib/lsan/lsan.cpp b/compiler-rt/lib/lsan/lsan.cpp
index 7a27b600f203f7..798294b499e2f0 100644
--- a/compiler-rt/lib/lsan/lsan.cpp
+++ b/compiler-rt/lib/lsan/lsan.cpp
@@ -92,10 +92,10 @@ extern "C" void __lsan_init() {
CacheBinaryName();
AvoidCVE_2016_2143();
InitializeFlags();
+ InitializePlatformEarly();
InitCommonLsan();
InitializeAllocator();
ReplaceSystemMalloc();
- InitTlsSize();
InitializeInterceptors();
InitializeThreads();
InstallDeadlySignalHandlers(LsanOnDeadlySignal);
diff --git a/compiler-rt/lib/memprof/memprof_rtl.cpp b/compiler-rt/lib/memprof/memprof_rtl.cpp
index cf4bde808bfad6..2cc6c2df5a6fe4 100644
--- a/compiler-rt/lib/memprof/memprof_rtl.cpp
+++ b/compiler-rt/lib/memprof/memprof_rtl.cpp
@@ -213,9 +213,6 @@ static void MemprofInitInternal() {
InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
- // interceptors
- InitTlsSize();
-
// Create main thread.
MemprofThread *main_thread = CreateMainThread();
CHECK_EQ(0, main_thread->tid());
diff --git a/compiler-rt/lib/msan/msan.cpp b/compiler-rt/lib/msan/msan.cpp
index 2ee05f43ec5e56..6c27ab21eeebfd 100644
--- a/compiler-rt/lib/msan/msan.cpp
+++ b/compiler-rt/lib/msan/msan.cpp
@@ -457,10 +457,11 @@ void __msan_init() {
__sanitizer_set_report_path(common_flags()->log_path);
+ InitializePlatformEarly();
+
InitializeInterceptors();
InstallAtForkHandler();
CheckASLR();
- InitTlsSize();
InstallDeadlySignalHandlers(MsanOnDeadlySignal);
InstallAtExitHandler(); // Needs __cxa_atexit interceptor.
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_nolibc.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_common_nolibc.cpp
index 7d88575160c6c6..e49285f22dff99 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common_nolibc.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_nolibc.cpp
@@ -22,6 +22,7 @@ namespace __sanitizer {
#if !SANITIZER_WINDOWS
# if SANITIZER_LINUX
void LogMessageOnPrintf(const char *str) {}
+void InitTlsSize() {}
# endif
void WriteToSyslog(const char *buffer) {}
void Abort() { internal__exit(1); }
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_fuchsia.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_fuchsia.cpp
index a67b2a8725eca8..75dcf546729f6e 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_fuchsia.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_fuchsia.cpp
@@ -94,7 +94,6 @@ void DisableCoreDumperIfNecessary() {}
void InstallDeadlySignalHandlers(SignalHandlerType handler) {}
void SetAlternateSignalStack() {}
void UnsetAlternateSignalStack() {}
-void InitTlsSize() {}
bool SignalContext::IsStackOverflow() const { return false; }
void SignalContext::DumpAllRegisters(void *context) { UNIMPLEMENTED(); }
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
index 6359f4348e3c48..1c637d109649b6 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
@@ -2672,9 +2672,7 @@ static void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
void SignalContext::InitPcSpBp() { GetPcSpBp(context, &pc, &sp, &bp); }
-void InitializePlatformEarly() {
- // Do nothing.
-}
+void InitializePlatformEarly() { InitTlsSize(); }
void CheckASLR() {
# if SANITIZER_NETBSD
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
index a5101291904430..aa156acd7b657a 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
@@ -200,21 +200,6 @@ bool SetEnv(const char *name, const char *value) {
}
# endif
-__attribute__((unused)) static bool GetLibcVersion(int *major, int *minor,
- int *patch) {
-# if SANITIZER_GLIBC
- const char *p = gnu_get_libc_version();
- *major = internal_simple_strtoll(p, &p, 10);
- // Caller does not expect anything else.
- CHECK_EQ(*major, 2);
- *minor = (*p == '.') ? internal_simple_strtoll(p + 1, &p, 10) : 0;
- *patch = (*p == '.') ? internal_simple_strtoll(p + 1, &p, 10) : 0;
- return true;
-# else
- return false;
-# endif
-}
-
// True if we can use dlpi_tls_data. glibc before 2.25 may leave NULL (BZ
// #19826) so dlpi_tls_data cannot be used.
//
@@ -224,110 +209,137 @@ __attribute__((unused)) static bool GetLibcVersion(int *major, int *minor,
__attribute__((unused)) static int g_use_dlpi_tls_data;
# if SANITIZER_GLIBC && !SANITIZER_GO
-__attribute__((unused)) static size_t g_tls_size;
-void InitTlsSize() {
- int major, minor, patch;
- g_use_dlpi_tls_data =
- GetLibcVersion(&major, &minor, &patch) && major == 2 && minor >= 25;
-
-# if defined(__aarch64__) || defined(__x86_64__) || \
- defined(__powerpc64__) || defined(__loongarch__)
- void *get_tls_static_info = dlsym(RTLD_DEFAULT, "_dl_get_tls_static_info");
- size_t tls_align;
- ((void (*)(size_t *, size_t *))get_tls_static_info)(&g_tls_size, &tls_align);
-# endif
+static void GetGLibcVersion(int *major, int *minor, int *patch) {
+ const char *p = gnu_get_libc_version();
+ *major = internal_simple_strtoll(p, &p, 10);
+ // Caller does not expect anything else.
+ CHECK_EQ(*major, 2);
+ *minor = (*p == '.') ? internal_simple_strtoll(p + 1, &p, 10) : 0;
+ *patch = (*p == '.') ? internal_simple_strtoll(p + 1, &p, 10) : 0;
}
-# else
-void InitTlsSize() {}
# endif // SANITIZER_GLIBC && !SANITIZER_GO
// On glibc x86_64, ThreadDescriptorSize() needs to be precise due to the usage
// of g_tls_size. On other targets, ThreadDescriptorSize() is only used by lsan
// to get the pointer to thread-specific data keys in the thread control block.
-# if (SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_SOLARIS) && \
- !SANITIZER_ANDROID && !SANITIZER_GO
+# if (SANITIZER_FREEBSD || SANITIZER_GLIBC) && !SANITIZER_GO
// sizeof(struct pthread) from glibc.
-static atomic_uintptr_t thread_descriptor_size;
+static uptr thread_descriptor_size;
+// FIXME: Implementation is very GLIBC specific, but it's used by FREEBSD.
static uptr ThreadDescriptorSizeFallback() {
-# if defined(__x86_64__) || defined(__i386__) || defined(__arm__)
+# if defined(__x86_64__) || defined(__i386__) || defined(__arm__) || \
+ SANITIZER_RISCV64
+# if SANITIZER_GLIBC
int major;
int minor;
int patch;
- if (GetLibcVersion(&major, &minor, &patch) && major == 2) {
- /* sizeof(struct pthread) values from various glibc versions. */
- if (SANITIZER_X32)
- return 1728; // Assume only one particular version for x32.
- // For ARM sizeof(struct pthread) changed in Glibc 2.23.
- if (SANITIZER_ARM)
- return minor <= 22 ? 1120 : 1216;
- if (minor <= 3)
- return FIRST_32_SECOND_64(1104, 1696);
- if (minor == 4)
- return FIRST_32_SECOND_64(1120, 1728);
- if (minor == 5)
- return FIRST_32_SECOND_64(1136, 1728);
- if (minor <= 9)
- return FIRST_32_SECOND_64(1136, 1712);
- if (minor == 10)
- return FIRST_32_SECOND_64(1168, 1776);
- if (minor == 11 || (minor == 12 && patch == 1))
- return FIRST_32_SECOND_64(1168, 2288);
- if (minor <= 14)
- return FIRST_32_SECOND_64(1168, 2304);
- if (minor < 32) // Unknown version
- return FIRST_32_SECOND_64(1216, 2304);
- // minor == 32
- return FIRST_32_SECOND_64(1344, 2496);
- }
+ GetGLibcVersion(&major, &minor, &patch);
+# else // SANITIZER_GLIBC
return 0;
-# elif defined(__s390__) || defined(__sparc__)
+# endif // SANITIZER_GLIBC
+# endif
+
+# if defined(__x86_64__) || defined(__i386__) || defined(__arm__)
+ /* sizeof(struct pthread) values from various glibc versions. */
+ if (SANITIZER_X32)
+ return 1728; // Assume only one particular version for x32.
+ // For ARM sizeof(struct pthread) changed in Glibc 2.23.
+ if (SANITIZER_ARM)
+ return minor <= 22 ? 1120 : 1216;
+ if (minor <= 3)
+ return FIRST_32_SECOND_64(1104, 1696);
+ if (minor == 4)
+ return FIRST_32_SECOND_64(1120, 1728);
+ if (minor == 5)
+ return FIRST_32_SECOND_64(1136, 1728);
+ if (minor <= 9)
+ return FIRST_32_SECOND_64(1136, 1712);
+ if (minor == 10)
+ return FIRST_32_SECOND_64(1168, 1776);
+ if (minor == 11 || (minor == 12 && patch == 1))
+ return FIRST_32_SECOND_64(1168, 2288);
+ if (minor <= 14)
+ return FIRST_32_SECOND_64(1168, 2304);
+ if (minor < 32) // Unknown version
+ return FIRST_32_SECOND_64(1216, 2304);
+ // minor == 32
+ return FIRST_32_SECOND_64(1344, 2496);
+# endif
+
+# if SANITIZER_RISCV64
+ // TODO: consider adding an optional runtime check for an unknown (untested)
+ // glibc version
+ if (minor <= 28) // WARNING: the highest tested version is 2.29
+ return 1772; // no guarantees for this one
+ if (minor <= 31)
+ return 1772; // tested against glibc 2.29, 2.31
+ return 1936; // tested against glibc 2.32
+# endif
+
+# if defined(__s390__) || defined(__sparc__)
// The size of a prefix of TCB including pthread::{specific_1stblock,specific}
// suffices. Just return offsetof(struct pthread, specific_used), which hasn't
// changed since 2007-05. Technically this applies to i386/x86_64 as well but
// we call _dl_get_tls_static_info and need the precise size of struct
// pthread.
return FIRST_32_SECOND_64(524, 1552);
-# elif defined(__mips__)
+# endif
+
+# if defined(__mips__)
// TODO(sagarthakur): add more values as per different glibc versions.
return FIRST_32_SECOND_64(1152, 1776);
-# elif SANITIZER_LOONGARCH64
+# endif
+
+# if SANITIZER_LOONGARCH64
return 1856; // from glibc 2.36
-# elif SANITIZER_RISCV64
- int major;
- int minor;
- int patch;
- if (GetLibcVersion(&major, &minor, &patch) && major == 2) {
- // TODO: consider adding an optional runtime check for an unknown (untested)
- // glibc version
- if (minor <= 28) // WARNING: the highest tested version is 2.29
- return 1772; // no guarantees for this one
- if (minor <= 31)
- return 1772; // tested against glibc 2.29, 2.31
- return 1936; // tested against glibc 2.32
- }
- return 0;
-# elif defined(__aarch64__)
+# endif
+
+# if defined(__aarch64__)
// The sizeof (struct pthread) is the same from GLIBC 2.17 to 2.22.
return 1776;
-# elif defined(__powerpc64__)
+# endif
+
+# if defined(__powerpc64__)
return 1776; // from glibc.ppc64le 2.20-8.fc21
# endif
}
-uptr ThreadDescriptorSize() {
- uptr val = atomic_load_relaxed(&thread_descriptor_size);
- if (val)
- return val;
- // _thread_db_sizeof_pthread is a GLIBC_PRIVATE symbol that is exported in
- // glibc 2.34 and later.
- if (unsigned *psizeof = static_cast<unsigned *>(
- dlsym(RTLD_DEFAULT, "_thread_db_sizeof_pthread")))
- val = *psizeof;
- if (!val)
- val = ThreadDescriptorSizeFallback();
- atomic_store_relaxed(&thread_descriptor_size, val);
- return val;
+uptr ThreadDescriptorSize() { return thread_descriptor_size; }
+
+# if SANITIZER_GLIBC
+__attribute__((unused)) static size_t g_tls_size;
+# endif
+
+void InitTlsSize() {
+# if SANITIZER_GLIBC
+ int major, minor, patch;
+ GetGLibcVersion(&major, &minor, &patch);
+ g_use_dlpi_tls_data = major == 2 && minor >= 25;
+
+ if (major == 2 && minor >= 34) {
+ // _thread_db_sizeof_pthread is a GLIBC_PRIVATE symbol that is exported in
+ // glibc 2.34 and later.
+ if (unsigned *psizeof = static_cast<unsigned *>(
+ dlsym(RTLD_DEFAULT, "_thread_db_sizeof_pthread"))) {
+ thread_descriptor_size = *psizeof;
+ }
+ }
+
+# if defined(__aarch64__) || defined(__x86_64__) || \
+ defined(__powerpc64__) || defined(__loongarch__)
+ auto *get_tls_static_info = (void (*)(size_t *, size_t *))dlsym(
+ RTLD_DEFAULT, "_dl_get_tls_static_info");
+ size_t tls_align;
+ // Can be null if static link.
+ if (get_tls_static_info)
+ get_tls_static_info(&g_tls_size, &tls_align);
+# endif
+
+# endif // SANITIZER_GLIBC
+
+ if (!thread_descriptor_size)
+ thread_descriptor_size = ThreadDescriptorSizeFallback();
}
# if defined(__mips__) || defined(__powerpc64__) || SANITIZER_RISCV64 || \
@@ -350,7 +362,12 @@ static uptr TlsPreTcbSize() {
return kTlsPreTcbSize;
}
# endif
+# else // (SANITIZER_FREEBSD || SANITIZER_GLIBC) && !SANITIZER_GO
+void InitTlsSize() {}
+# endif // (SANITIZER_FREEBSD || SANITIZER_GLIBC) && !SANITIZER_GO
+# if (SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_SOLARIS) && \
+ !SANITIZER_ANDROID && !SANITIZER_GO
namespace {
struct TlsBlock {
uptr begin, end, align;
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
index 2a36104e6f9f29..26d2e8d4ed7680 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
@@ -545,9 +545,6 @@ uptr GetTlsSize() {
return 0;
}
-void InitTlsSize() {
-}
-
uptr TlsBaseAddr() {
uptr segbase = 0;
#if defined(__x86_64__)
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_win.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_win.cpp
index 2c8f8343519ed8..7cee571314868e 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_win.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_win.cpp
@@ -873,9 +873,6 @@ uptr GetTlsSize() {
return 0;
}
-void InitTlsSize() {
-}
-
void GetThreadStackAndTls(bool main, uptr *stk_begin, uptr *stk_end,
uptr *tls_begin, uptr *tls_end) {
# if SANITIZER_GO
diff --git a/compiler-rt/lib/sanitizer_common/tests/sanitizer_linux_test.cpp b/compiler-rt/lib/sanitizer_common/tests/sanitizer_linux_test.cpp
index 338c4d3bab2b04..b286ab72a5c795 100644
--- a/compiler-rt/lib/sanitizer_common/tests/sanitizer_linux_test.cpp
+++ b/compiler-rt/lib/sanitizer_common/tests/sanitizer_linux_test.cpp
@@ -202,6 +202,8 @@ TEST(SanitizerLinux, ThreadDescriptorSize) {
void *result;
ASSERT_EQ(0, pthread_create(&tid, 0, thread_descriptor_size_test_func, 0));
ASSERT_EQ(0, pthread_join(tid, &result));
+ EXPECT_EQ(0u, ThreadDescriptorSize());
+ InitTlsSize();
EXPECT_EQ((uptr)result, ThreadDescriptorSize());
}
# endif
diff --git a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
index 460cbacf3408cf..eaa0f6d0de60c2 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
@@ -12,9 +12,11 @@
// sanitizer_common/sanitizer_common_interceptors.inc
//===----------------------------------------------------------------------===//
+#include "sanitizer_common/sanitizer_allocator_dlsym.h"
#include "sanitizer_common/sanitizer_atomic.h"
#include "sanitizer_common/sanitizer_errno.h"
#include "sanitizer_common/sanitizer_glibc_version.h"
+#include "sanitizer_common/sanitizer_internal_defs.h"
#include "sanitizer_common/sanitizer_libc.h"
#include "sanitizer_common/sanitizer_linux.h"
#include "sanitizer_common/sanitizer_platform_limits_netbsd.h"
@@ -252,6 +254,15 @@ SANITIZER_WEAK_CXX_DEFAULT_IMPL void OnPotentiallyBlockingRegionBegin() {}
SANITIZER_WEAK_CXX_DEFAULT_IMPL void OnPotentiallyBlockingRegionEnd() {}
#endif
+// FIXME: Use for `in_symbolizer()` as well. As-is we can't use
+// `DlSymAllocator`, because it uses the primary allocator only. Symbolizer
+// requires support of the secondary allocator for larger blocks.
+struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {
+ static bool UseImpl() {
+ return (ctx && !ctx->initialized);
+ }
+};
+
} // namespace __tsan
static ThreadSignalContext *SigCtx(ThreadState *thr) {
@@ -661,6 +672,8 @@ TSAN_INTERCEPTOR(void, _longjmp, uptr *env, int val) {
TSAN_INTERCEPTOR(void*, malloc, uptr size) {
if (in_symbolizer())
return InternalAlloc(size);
+ if (DlsymAlloc::Use())
+ return DlsymAlloc::Allocate(size);
void *p = 0;
{
SCOPED_INTERCEPTOR_RAW(malloc, size);
@@ -681,6 +694,8 @@ TSAN_INTERCEPTOR(void*, __libc_memalign, uptr align, uptr sz) {
TSAN_INTERCEPTOR(void *, calloc, uptr n, uptr size) {
if (in_symbolizer())
return InternalCalloc(n, size);
+ if (DlsymAlloc::Use())
+ return DlsymAlloc::Callocate(n, size);
void *p = 0;
{
SCOPED_INTERCEPTOR_RAW(calloc, n, size);
@@ -693,6 +708,8 @@ TSAN_INTERCEPTOR(void *, calloc, uptr n, uptr size) {
TSAN_INTERCEPTOR(void*, realloc, void *p, uptr size) {
if (in_symbolizer())
return InternalRealloc(p, size);
+ if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(p))
+ return DlsymAlloc::Realloc(p, size);
if (p)
invoke_free_hook(p);
{
@@ -717,20 +734,24 @@ TSAN_INTERCEPTOR(void *, reallocarray, void *p, uptr n, uptr size) {
}
TSAN_INTERCEPTOR(void, free, void *p) {
- if (p == 0)
+ if (UNLIKELY(!p))
return;
if (in_symbolizer())
return InternalFree(p);
+ if (DlsymAlloc::PointerIsMine(p))
+ return DlsymAlloc::Free(p);
invoke_free_hook(p);
SCOPED_INTERCEPTOR_RAW(free, p);
user_free(thr, pc, p);
}
TSAN_INTERCEPTOR(void, cfree, void *p) {
- if (p == 0)
+ if (UNLIKELY(!p))
return;
if (in_symbolizer())
return InternalFree(p);
+ if (DlsymAlloc::PointerIsMine(p))
+ return DlsymAlloc::Free(p);
invoke_free_hook(p);
SCOPED_INTERCEPTOR_RAW(cfree, p);
user_free(thr, pc, p);
diff --git a/compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp b/compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp
index 621c679a05db45..3e08a1bece98f0 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp
@@ -418,7 +418,6 @@ void InitializePlatform() {
Die();
}
- InitTlsSize();
#endif // !SANITIZER_GO
}
diff --git a/compiler-rt/lib/ubsan/ubsan_init.cpp b/compiler-rt/lib/ubsan/ubsan_init.cpp
index 5802d58896f0fe..aea7ca00e3cb3f 100644
--- a/compiler-rt/lib/ubsan/ubsan_init.cpp
+++ b/compiler-rt/lib/ubsan/ubsan_init.cpp
@@ -43,8 +43,8 @@ static void CommonStandaloneInit() {
SanitizerToolName = GetSanititizerToolName();
CacheBinaryName();
InitializeFlags();
- __sanitizer::InitializePlatformEarly();
__sanitizer_set_report_path(common_flags()->log_path);
+ __sanitizer::InitializePlatformEarly();
AndroidLogInit();
InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
CommonInit();
diff --git a/compiler-rt/test/sanitizer_common/TestCases/dlsym_alloc.c b/compiler-rt/test/sanitizer_common/TestCases/dlsym_alloc.c
index 7b5b9cf34a90f9..4aa87afe47f4ea 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/dlsym_alloc.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/dlsym_alloc.c
@@ -1,7 +1,5 @@
// RUN: %clang -O0 %s -o %t && %run %t
-// FIXME: TSAN does not use DlsymAlloc.
-// UNSUPPORTED: tsan
// FIXME: investigate why this fails on macos
// UNSUPPORTED: darwin
More information about the llvm-commits
mailing list