[compiler-rt] [hwasan] Move __hwasan_thread_enter/__hwasan_thread_exit out of namespace (PR #72123)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 13 06:54:05 PST 2023


https://github.com/nikic created https://github.com/llvm/llvm-project/pull/72123

Due to a GCC bug (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25940), GCC doesn't consider extern "C" functions with the same name but different namespace to be the same. As such, the default visibility attribute (on a declaration outside the namespace) doesn't get applied to the definition in the namespace and the symbol is not exported.

This came up as an ABI diff when switching between gcc and clang for compiling compiler-rt.

>From 8fe37cfbe7f9889c269cf7f0015cfb7bd2cc30c5 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 13 Nov 2023 15:50:21 +0100
Subject: [PATCH] [hwasan] Move __hwasan_thread_enter/__hwasan_thread_exit out
 of namespace

Due to a GCC bug (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25940),
GCC doesn't consider extern "C" functions with the same name but
different namespace to be the same. As such, the default visibility
attribute (on a declaration outside the namespace) doesn't get
applied to the definition in the namespace and the symbol is not
exported.

This came up as an ABI diff when switching between gcc and clang
for compiling compiler-rt.
---
 compiler-rt/lib/hwasan/hwasan_linux.cpp | 40 +++++++++++++------------
 1 file changed, 21 insertions(+), 19 deletions(-)

diff --git a/compiler-rt/lib/hwasan/hwasan_linux.cpp b/compiler-rt/lib/hwasan/hwasan_linux.cpp
index 6f5e9432974efdb..81226da976d1161 100644
--- a/compiler-rt/lib/hwasan/hwasan_linux.cpp
+++ b/compiler-rt/lib/hwasan/hwasan_linux.cpp
@@ -294,25 +294,6 @@ void InstallAtExitHandler() { atexit(HwasanAtExit); }
 
 // ---------------------- TSD ---------------- {{{1
 
-extern "C" void __hwasan_thread_enter() {
-  hwasanThreadList().CreateCurrentThread()->EnsureRandomStateInited();
-}
-
-extern "C" void __hwasan_thread_exit() {
-  Thread *t = GetCurrentThread();
-  // Make sure that signal handler can not see a stale current thread pointer.
-  atomic_signal_fence(memory_order_seq_cst);
-  if (t) {
-    // Block async signals on the thread as the handler can be instrumented.
-    // After this point instrumented code can't access essential data from TLS
-    // and will crash.
-    // Bionic already calls __hwasan_thread_exit with blocked signals.
-    if (SANITIZER_GLIBC)
-      BlockSignals();
-    hwasanThreadList().ReleaseThread(t);
-  }
-}
-
 #  if HWASAN_WITH_INTERCEPTORS
 static pthread_key_t tsd_key;
 static bool tsd_key_inited = false;
@@ -561,4 +542,25 @@ void InstallAtExitCheckLeaks() {
 
 }  // namespace __hwasan
 
+using namespace __hwasan;
+
+extern "C" void __hwasan_thread_enter() {
+  hwasanThreadList().CreateCurrentThread()->EnsureRandomStateInited();
+}
+
+extern "C" void __hwasan_thread_exit() {
+  Thread *t = GetCurrentThread();
+  // Make sure that signal handler can not see a stale current thread pointer.
+  atomic_signal_fence(memory_order_seq_cst);
+  if (t) {
+    // Block async signals on the thread as the handler can be instrumented.
+    // After this point instrumented code can't access essential data from TLS
+    // and will crash.
+    // Bionic already calls __hwasan_thread_exit with blocked signals.
+    if (SANITIZER_GLIBC)
+      BlockSignals();
+    hwasanThreadList().ReleaseThread(t);
+  }
+}
+
 #endif  // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD



More information about the llvm-commits mailing list