[compiler-rt] 0aefc94 - [asan][ubsan][test] Add weak attributes for dyld Mach-O weak-def-coalescing

Roy Sundahl via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 13 11:52:44 PDT 2022


Author: Roy Sundahl
Date: 2022-07-13T11:46:56-07:00
New Revision: 0aefc94651b0a0ae46741f622c7da276b3cfa698

URL: https://github.com/llvm/llvm-project/commit/0aefc94651b0a0ae46741f622c7da276b3cfa698
DIFF: https://github.com/llvm/llvm-project/commit/0aefc94651b0a0ae46741f622c7da276b3cfa698.diff

LOG: [asan][ubsan][test] Add weak attributes for dyld Mach-O weak-def-coalescing

Apple's dynamic linker won't weak-def_coalesce from a file unless there is
at least one weak symbol in the compilation unit so  local __ubsan_on_report
never has the chance to override the weak one even though the dynamic linker
may see it first. This works around the issue by adding an unused weak symbol.
(Amended: Remove excessive clang-format artifacts)

rdar://95244261

Differential Revision: https://reviews.llvm.org/D127929

Added: 
    

Modified: 
    compiler-rt/test/asan/TestCases/debug_double_free.cpp
    compiler-rt/test/asan/TestCases/debug_report.cpp
    compiler-rt/test/asan/TestCases/default_options.cpp
    compiler-rt/test/asan/TestCases/on_error_callback.cpp
    compiler-rt/test/ubsan/TestCases/Misc/monitor.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/test/asan/TestCases/debug_double_free.cpp b/compiler-rt/test/asan/TestCases/debug_double_free.cpp
index 44c820b638d61..de5ac7b0c8d5c 100644
--- a/compiler-rt/test/asan/TestCases/debug_double_free.cpp
+++ b/compiler-rt/test/asan/TestCases/debug_double_free.cpp
@@ -37,7 +37,12 @@ int main() {
   return 0;
 }
 
-void __asan_on_error() {
+// Required for dyld macOS 12.0+
+#if (__APPLE__)
+__attribute__((weak))
+#endif
+extern "C" void
+__asan_on_error() {
   int present = __asan_report_present();
   void *addr = __asan_get_report_address();
   const char *description = __asan_get_report_description();

diff  --git a/compiler-rt/test/asan/TestCases/debug_report.cpp b/compiler-rt/test/asan/TestCases/debug_report.cpp
index 14bb70e9f5937..617b7ee91e18d 100644
--- a/compiler-rt/test/asan/TestCases/debug_report.cpp
+++ b/compiler-rt/test/asan/TestCases/debug_report.cpp
@@ -37,7 +37,12 @@ int main() {
 # define PTR_FMT "%p"
 #endif
 
-void __asan_on_error() {
+// Required for dyld macOS 12.0+
+#if (__APPLE__)
+__attribute__((weak))
+#endif
+extern "C" void
+__asan_on_error() {
   int present = __asan_report_present();
   void *pc = __asan_get_report_pc();
   void *bp = __asan_get_report_bp();

diff  --git a/compiler-rt/test/asan/TestCases/default_options.cpp b/compiler-rt/test/asan/TestCases/default_options.cpp
index 83afba0c3cfc0..526dab6450e9b 100644
--- a/compiler-rt/test/asan/TestCases/default_options.cpp
+++ b/compiler-rt/test/asan/TestCases/default_options.cpp
@@ -6,9 +6,13 @@
 
 const char *kAsanDefaultOptions = "verbosity=1 help=1";
 
-extern "C"
+// Required for dyld macOS 12.0+
+#if (__APPLE__)
+__attribute__((weak))
+#endif
 __attribute__((no_sanitize_address))
-const char *__asan_default_options() {
+extern "C" const char *
+__asan_default_options() {
   // CHECK: Available flags for AddressSanitizer:
   return kAsanDefaultOptions;
 }

diff  --git a/compiler-rt/test/asan/TestCases/on_error_callback.cpp b/compiler-rt/test/asan/TestCases/on_error_callback.cpp
index f37d1eb7f7f83..f65a8f1abe831 100644
--- a/compiler-rt/test/asan/TestCases/on_error_callback.cpp
+++ b/compiler-rt/test/asan/TestCases/on_error_callback.cpp
@@ -6,8 +6,12 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-extern "C"
-void __asan_on_error() {
+// Required for dyld macOS 12.0+
+#if (__APPLE__)
+__attribute__((weak))
+#endif
+extern "C" void
+__asan_on_error() {
   fprintf(stderr, "__asan_on_error called\n");
   fflush(stderr);
 }

diff  --git a/compiler-rt/test/ubsan/TestCases/Misc/monitor.cpp b/compiler-rt/test/ubsan/TestCases/Misc/monitor.cpp
index f3b13e33f1237..239ed0d07bcc9 100644
--- a/compiler-rt/test/ubsan/TestCases/Misc/monitor.cpp
+++ b/compiler-rt/test/ubsan/TestCases/Misc/monitor.cpp
@@ -10,19 +10,21 @@
 
 #include <cstdio>
 
-extern "C" {
-void __ubsan_get_current_report_data(const char **OutIssueKind,
-                                     const char **OutMessage,
-                                     const char **OutFilename,
-                                     unsigned *OutLine, unsigned *OutCol,
-                                     char **OutMemoryAddr);
-
-// Override the definition of __ubsan_on_report from the runtime, just for
-// testing purposes.
-void __ubsan_on_report(void) {
+// Override __ubsan_on_report() from the runtime, just for testing purposes.
+// Required for dyld macOS 12.0+
+#if (__APPLE__)
+__attribute__((weak))
+#endif
+extern "C" void
+__ubsan_on_report(void) {
+  void __ubsan_get_current_report_data(
+      const char **OutIssueKind, const char **OutMessage,
+      const char **OutFilename, unsigned *OutLine, unsigned *OutCol,
+      char **OutMemoryAddr);
   const char *IssueKind, *Message, *Filename;
   unsigned Line, Col;
   char *Addr;
+
   __ubsan_get_current_report_data(&IssueKind, &Message, &Filename, &Line, &Col,
                                   &Addr);
 
@@ -33,7 +35,6 @@ void __ubsan_on_report(void) {
 
   (void)Addr;
 }
-}
 
 int main() {
   char C = 3;


        


More information about the llvm-commits mailing list