[compiler-rt] dd66aaf - [sanitizer] Allow to override GetDTLSRange (#108348)

via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 12 13:38:57 PDT 2024


Author: Vitaly Buka
Date: 2024-09-12T13:38:53-07:00
New Revision: dd66aaf85a2258324851ac19eb08094928ac1eac

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

LOG: [sanitizer] Allow to override GetDTLSRange (#108348)

And rename it into __sanitizer_get_dtls_size.

The test will be in a separate patch, as I
expected reverts of the test.

Added: 
    

Modified: 
    compiler-rt/lib/sanitizer_common/sanitizer_common_interface.inc
    compiler-rt/lib/sanitizer_common/sanitizer_interface_internal.h
    compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp
    compiler-rt/lib/sanitizer_common/weak_symbols.txt

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interface.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interface.inc
index 91be9e9dcc4551..66744aa021e62b 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interface.inc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interface.inc
@@ -22,6 +22,7 @@ INTERFACE_FUNCTION(__sanitizer_verify_double_ended_contiguous_container)
 INTERFACE_WEAK_FUNCTION(__sanitizer_on_print)
 INTERFACE_WEAK_FUNCTION(__sanitizer_report_error_summary)
 INTERFACE_WEAK_FUNCTION(__sanitizer_sandbox_on_notify)
+INTERFACE_WEAK_FUNCTION(__sanitizer_get_dtls_size)
 // Sanitizer weak hooks
 INTERFACE_WEAK_FUNCTION(__sanitizer_weak_hook_memcmp)
 INTERFACE_WEAK_FUNCTION(__sanitizer_weak_hook_strcmp)

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_interface_internal.h b/compiler-rt/lib/sanitizer_common/sanitizer_interface_internal.h
index cd0d45e2f3fab1..c424ab1cecf94f 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_interface_internal.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_interface_internal.h
@@ -49,6 +49,11 @@ __sanitizer_sandbox_on_notify(__sanitizer_sandbox_arguments *args);
 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void
 __sanitizer_report_error_summary(const char *error_summary);
 
+// Returns size of dynamically allocated block. This function can be overridden
+// by the client.
+SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE __sanitizer::uptr
+__sanitizer_get_dtls_size(const void *tls_begin);
+
 SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_dump();
 SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_dump_coverage(
     const __sanitizer::uptr *pcs, const __sanitizer::uptr len);

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp
index 17b9ff4aa6c40b..5e9a78706bf24d 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp
@@ -110,14 +110,15 @@ SANITIZER_WEAK_ATTRIBUTE
 const void *__sanitizer_get_allocated_begin(const void *p);
 }
 
-static uptr GetDTLSRange(uptr tls_beg) {
-  const void *start = __sanitizer_get_allocated_begin((void *)tls_beg);
+SANITIZER_INTERFACE_WEAK_DEF(uptr, __sanitizer_get_dtls_size,
+                             const void *tls_begin) {
+  const void *start = __sanitizer_get_allocated_begin(tls_begin);
   if (!start)
     return 0;
-  CHECK_EQ(start, (void *)tls_beg);
+  CHECK_EQ(start, tls_begin);
   uptr tls_size = __sanitizer_get_allocated_size(start);
   VReport(2, "__tls_get_addr: glibc DTLS suspected; tls={%p,0x%zx}\n",
-          (void *)tls_beg, tls_size);
+          tls_begin, tls_size);
   return tls_size;
 }
 
@@ -143,8 +144,8 @@ DTLS::DTV *DTLS_on_tls_get_addr(void *arg_void, void *res,
     VReport(2, "__tls_get_addr: static tls: %p\n", (void *)tls_beg);
     tls_size = 0;
   } else {
-    tls_size = GetDTLSRange(tls_beg);
-    if (tls_size) {
+    tls_size = __sanitizer_get_dtls_size(reinterpret_cast<void *>(tls_beg));
+    if (!tls_size) {
       VReport(2, "__tls_get_addr: Can't guess glibc version\n");
       // This may happen inside the DTOR of main thread, so just ignore it.
     }
@@ -162,6 +163,9 @@ bool DTLSInDestruction(DTLS *dtls) {
 }
 
 #else
+SANITIZER_INTERFACE_WEAK_DEF(uptr, __sanitizer_get_dtls_size, const void *) {
+  return 0;
+}
 DTLS::DTV *DTLS_on_tls_get_addr(void *arg, void *res,
   unsigned long, unsigned long) { return 0; }
 DTLS *DTLS_Get() { return 0; }

diff  --git a/compiler-rt/lib/sanitizer_common/weak_symbols.txt b/compiler-rt/lib/sanitizer_common/weak_symbols.txt
index 1eb1ce8d6b9c55..77e7b5d9f702e2 100644
--- a/compiler-rt/lib/sanitizer_common/weak_symbols.txt
+++ b/compiler-rt/lib/sanitizer_common/weak_symbols.txt
@@ -1,4 +1,5 @@
 ___sanitizer_free_hook
+___sanitizer_get_dtls_size
 ___sanitizer_malloc_hook
 ___sanitizer_report_error_summary
 ___sanitizer_sandbox_on_notify


        


More information about the llvm-commits mailing list