[compiler-rt] [asan][windows] use __builtin_function_address to avoid problematic codegen in weak function registration (PR #108327)

Charlie Barto via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 11 21:20:08 PDT 2024


https://github.com/barcharcraz updated https://github.com/llvm/llvm-project/pull/108327

>From 161445bc645111c669bb6ebf49ccc0ea5cf9ab0c Mon Sep 17 00:00:00 2001
From: Charlie Barto <chbarto at microsoft.com>
Date: Wed, 11 Sep 2024 21:09:28 -0700
Subject: [PATCH] use __builtin_function_address to avoid problematic UB
 induced codegen in asan weak function registration

---
 .../sanitizer_common/sanitizer_win_thunk_interception.h    | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_win_thunk_interception.h b/compiler-rt/lib/sanitizer_common/sanitizer_win_thunk_interception.h
index fa7b18fdc18f6d..278450d68ac513 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_win_thunk_interception.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_win_thunk_interception.h
@@ -62,8 +62,10 @@ void initialize_thunks(const sanitizer_thunk *begin,
 // startup that will replace sanitizer_export with local_function
 #ifdef __clang__
 #  define REGISTER_WEAK_OPTNONE __attribute__((optnone))
+#  define REGISTER_WEAK_FUNCTION_ADDRESS(fn) __builtin_function_start(fn)
 #else
 #  define REGISTER_WEAK_OPTNONE
+#  define REGISTER_WEAK_FUNCTION_ADDRESS(fn) &fn
 #endif
 
 #define REGISTER_WEAK_FUNCTION(local_function)                          \
@@ -71,8 +73,9 @@ void initialize_thunks(const sanitizer_thunk *begin,
   extern "C" void WEAK_EXPORT_NAME(local_function)();                   \
   WIN_WEAK_IMPORT_DEF(local_function)                                   \
   REGISTER_WEAK_OPTNONE static int register_weak_##local_function() {   \
-    if ((uintptr_t) & local_function != (uintptr_t) &                   \
-        WEAK_EXPORT_NAME(local_function)) {                             \
+    if ((uintptr_t)REGISTER_WEAK_FUNCTION_ADDRESS(local_function) !=    \
+        (uintptr_t)REGISTER_WEAK_FUNCTION_ADDRESS(                      \
+            WEAK_EXPORT_NAME(local_function))) {                        \
       return __sanitizer::register_weak(                                \
           SANITIZER_STRINGIFY(WEAK_EXPORT_NAME(local_function)),        \
           reinterpret_cast<__sanitizer::uptr>(local_function));         \



More information about the llvm-commits mailing list