[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:13:10 PDT 2024


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

Previously we were relying on optnone for this, but that didn't seem to be sufficient.

>From 0df07f38de2cc97940cc99a20b40ff7581a36b81 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

---
 .../lib/sanitizer_common/sanitizer_win_thunk_interception.h  | 5 +++--
 1 file changed, 3 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..80c56f8f3c9d0f 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,7 @@ 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