Module Name:    src
Committed By:   riastradh
Date:           Mon Jun 17 23:35:52 UTC 2024

Modified Files:
        src/external/gpl3/gcc/dist/libsanitizer/tsan:
            tsan_interceptors_posix.cpp tsan_rtl.cpp tsan_rtl.h

Log Message:
libtsan: Work around large TLS alignment issue.

PR lib/58349: tsan expects cacheline-aligned thread-local variables
but ld.elf_so only supports pointer-aligned


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 \
    src/external/gpl3/gcc/dist/libsanitizer/tsan/tsan_interceptors_posix.cpp \
    src/external/gpl3/gcc/dist/libsanitizer/tsan/tsan_rtl.cpp
cvs rdiff -u -r1.4 -r1.5 \
    src/external/gpl3/gcc/dist/libsanitizer/tsan/tsan_rtl.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/external/gpl3/gcc/dist/libsanitizer/tsan/tsan_interceptors_posix.cpp
diff -u src/external/gpl3/gcc/dist/libsanitizer/tsan/tsan_interceptors_posix.cpp:1.3 src/external/gpl3/gcc/dist/libsanitizer/tsan/tsan_interceptors_posix.cpp:1.4
--- src/external/gpl3/gcc/dist/libsanitizer/tsan/tsan_interceptors_posix.cpp:1.3	Mon Jul 31 01:44:57 2023
+++ src/external/gpl3/gcc/dist/libsanitizer/tsan/tsan_interceptors_posix.cpp	Mon Jun 17 23:35:52 2024
@@ -1082,7 +1082,7 @@ TSAN_INTERCEPTOR(void, pthread_exit, voi
   {
     SCOPED_INTERCEPTOR_RAW(pthread_exit, retval);
 #if !SANITIZER_MAC && !SANITIZER_ANDROID
-    CHECK_EQ(thr, &cur_thread_placeholder);
+    CHECK_EQ(thr, reinterpret_cast<char *>((reinterpret_cast<uptr>(cur_thread_placeholder) + SANITIZER_CACHE_LINE_SIZE - 1) & ~static_cast<uptr>(SANITIZER_CACHE_LINE_SIZE - 1)));
 #endif
   }
   REAL(pthread_exit)(retval);
Index: src/external/gpl3/gcc/dist/libsanitizer/tsan/tsan_rtl.cpp
diff -u src/external/gpl3/gcc/dist/libsanitizer/tsan/tsan_rtl.cpp:1.3 src/external/gpl3/gcc/dist/libsanitizer/tsan/tsan_rtl.cpp:1.4
--- src/external/gpl3/gcc/dist/libsanitizer/tsan/tsan_rtl.cpp:1.3	Mon Jul 31 01:44:58 2023
+++ src/external/gpl3/gcc/dist/libsanitizer/tsan/tsan_rtl.cpp	Mon Jun 17 23:35:52 2024
@@ -41,12 +41,16 @@ void (*on_initialize)(void);
 int (*on_finalize)(int);
 #endif
 
+// XXX PR lib/58349 (https://gnats.NetBSD.org/58349): NetBSD ld.elf_so
+// doesn't support TLS alignment beyond void *, so we have to buffer
+// some extra space and do the alignment ourselves at all the reference
+// sites.
 #if !SANITIZER_GO && !SANITIZER_MAC
 __attribute__((tls_model("initial-exec")))
-THREADLOCAL char cur_thread_placeholder[sizeof(ThreadState)] ALIGNED(
+THREADLOCAL char cur_thread_placeholder[sizeof(ThreadState) + SANITIZER_CACHE_LINE_SIZE - 1] ALIGNED(
     SANITIZER_CACHE_LINE_SIZE);
 #endif
-static char ctx_placeholder[sizeof(Context)] ALIGNED(SANITIZER_CACHE_LINE_SIZE);
+static char ctx_placeholder[sizeof(Context) + SANITIZER_CACHE_LINE_SIZE - 1] ALIGNED(SANITIZER_CACHE_LINE_SIZE);
 Context *ctx;
 
 // Can be overriden by a front-end.
@@ -366,7 +370,7 @@ void Initialize(ThreadState *thr) {
   // Install tool-specific callbacks in sanitizer_common.
   SetCheckUnwindCallback(CheckUnwind);
 
-  ctx = new(ctx_placeholder) Context;
+  ctx = new(reinterpret_cast<char *>((reinterpret_cast<uptr>(ctx_placeholder) + SANITIZER_CACHE_LINE_SIZE - 1) & ~static_cast<uptr>(SANITIZER_CACHE_LINE_SIZE - 1))) Context;
   const char *env_name = SANITIZER_GO ? "GORACE" : "TSAN_OPTIONS";
   const char *options = GetEnv(env_name);
   CacheBinaryName();

Index: src/external/gpl3/gcc/dist/libsanitizer/tsan/tsan_rtl.h
diff -u src/external/gpl3/gcc/dist/libsanitizer/tsan/tsan_rtl.h:1.4 src/external/gpl3/gcc/dist/libsanitizer/tsan/tsan_rtl.h:1.5
--- src/external/gpl3/gcc/dist/libsanitizer/tsan/tsan_rtl.h:1.4	Sun Feb 25 01:12:16 2024
+++ src/external/gpl3/gcc/dist/libsanitizer/tsan/tsan_rtl.h	Mon Jun 17 23:35:52 2024
@@ -235,16 +235,16 @@ inline ThreadState *cur_thread_init() { 
 __attribute__((tls_model("initial-exec")))
 extern THREADLOCAL char cur_thread_placeholder[];
 inline ThreadState *cur_thread() {
-  return reinterpret_cast<ThreadState *>(cur_thread_placeholder)->current;
+  return reinterpret_cast<ThreadState *>((reinterpret_cast<uptr>(cur_thread_placeholder) + SANITIZER_CACHE_LINE_SIZE - 1) & ~static_cast<uptr>(SANITIZER_CACHE_LINE_SIZE - 1))->current;
 }
 inline ThreadState *cur_thread_init() {
-  ThreadState *thr = reinterpret_cast<ThreadState *>(cur_thread_placeholder);
+  ThreadState *thr = reinterpret_cast<ThreadState *>((reinterpret_cast<uptr>(cur_thread_placeholder) + SANITIZER_CACHE_LINE_SIZE - 1) & ~static_cast<uptr>(SANITIZER_CACHE_LINE_SIZE - 1));
   if (UNLIKELY(!thr->current))
     thr->current = thr;
   return thr->current;
 }
 inline void set_cur_thread(ThreadState *thr) {
-  reinterpret_cast<ThreadState *>(cur_thread_placeholder)->current = thr;
+  reinterpret_cast<ThreadState *>((reinterpret_cast<uptr>(cur_thread_placeholder) + SANITIZER_CACHE_LINE_SIZE - 1) & ~static_cast<uptr>(SANITIZER_CACHE_LINE_SIZE - 1))->current = thr;
 }
 inline void cur_thread_finalize() { }
 #  endif  // SANITIZER_MAC || SANITIZER_ANDROID

Reply via email to