[compiler-rt] c36fbe0 - Revert "[sanitizer] Don't run malloc hooks for stacktraces"

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 13 23:52:45 PDT 2022


Author: Vitaly Buka
Date: 2022-04-13T23:52:36-07:00
New Revision: c36fbe05a719cd5c88b5f655f3d49d6b4e32ab51

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

LOG: Revert "[sanitizer] Don't run malloc hooks for stacktraces"

Msan crashes on clang-s390x-linux bot

This reverts commit d3531fc7f0053a7fe68317521ee6491da6e36733.

Added: 
    

Modified: 
    compiler-rt/lib/msan/msan_allocator.cpp
    compiler-rt/lib/sanitizer_common/sanitizer_common.cpp
    compiler-rt/lib/sanitizer_common/sanitizer_common.h
    compiler-rt/lib/sanitizer_common/sanitizer_fuchsia.cpp
    compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
    compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
    compiler-rt/lib/sanitizer_common/sanitizer_symbolizer.h
    compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_markup.cpp
    compiler-rt/lib/sanitizer_common/sanitizer_unwind_linux_libcdep.cpp

Removed: 
    compiler-rt/test/sanitizer_common/TestCases/malloc_hook_skip.cpp


################################################################################
diff  --git a/compiler-rt/lib/msan/msan_allocator.cpp b/compiler-rt/lib/msan/msan_allocator.cpp
index 7ff1bc99a4486..0d5e85329850b 100644
--- a/compiler-rt/lib/msan/msan_allocator.cpp
+++ b/compiler-rt/lib/msan/msan_allocator.cpp
@@ -194,19 +194,16 @@ static void *MsanAllocate(StackTrace *stack, uptr size, uptr alignment,
       __msan_set_origin(allocated, size, o.raw_id());
     }
   }
-  if (!IsInSymbolizerOrUnwider()) {
-    UnpoisonParam(2);
-    RunMallocHooks(allocated, size);
-  }
+  UnpoisonParam(2);
+  RunMallocHooks(allocated, size);
   return allocated;
 }
 
 void MsanDeallocate(StackTrace *stack, void *p) {
   CHECK(p);
-  if (!IsInSymbolizerOrUnwider()) {
-    UnpoisonParam(1);
-    RunFreeHooks(p);
-  }
+  UnpoisonParam(1);
+  RunFreeHooks(p);
+
   Metadata *meta = reinterpret_cast<Metadata *>(allocator.GetMetaData(p));
   uptr size = meta->requested_size;
   meta->requested_size = 0;

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_common.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_common.cpp
index 674d19d2a3916..e30a93da5b598 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common.cpp
@@ -310,23 +310,7 @@ struct MallocFreeHook {
 
 static MallocFreeHook MFHooks[kMaxMallocFreeHooks];
 
-#if !SANITIZER_SUPPORTS_THREADLOCAL || SANITIZER_GO || SANITIZER_MAC || \
-    SANITIZER_ANDROID
-// FIXME: Prevent hooks on other platforms.
-static constexpr int disable_malloc_hooks = 0;
-ScopedDisableMallocHooks::ScopedDisableMallocHooks() {}
-ScopedDisableMallocHooks::~ScopedDisableMallocHooks() {}
-#else
-static THREADLOCAL int disable_malloc_hooks = 0;
-ScopedDisableMallocHooks::ScopedDisableMallocHooks() { ++disable_malloc_hooks; }
-ScopedDisableMallocHooks::~ScopedDisableMallocHooks() {
-  --disable_malloc_hooks;
-}
-#endif
-
 void RunMallocHooks(void *ptr, uptr size) {
-  if (disable_malloc_hooks)
-    return;
   __sanitizer_malloc_hook(ptr, size);
   for (int i = 0; i < kMaxMallocFreeHooks; i++) {
     auto hook = MFHooks[i].malloc_hook;
@@ -337,8 +321,6 @@ void RunMallocHooks(void *ptr, uptr size) {
 }
 
 void RunFreeHooks(void *ptr) {
-  if (disable_malloc_hooks)
-    return;
   __sanitizer_free_hook(ptr);
   for (int i = 0; i < kMaxMallocFreeHooks; i++) {
     auto hook = MFHooks[i].free_hook;

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_common.h b/compiler-rt/lib/sanitizer_common/sanitizer_common.h
index 0245687dd31e2..17570d6068856 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common.h
@@ -170,18 +170,9 @@ void SetShadowRegionHugePageMode(uptr addr, uptr length);
 bool DontDumpShadowMemory(uptr addr, uptr length);
 // Check if the built VMA size matches the runtime one.
 void CheckVMASize();
-
 void RunMallocHooks(void *ptr, uptr size);
 void RunFreeHooks(void *ptr);
 
-// Prevents RunMallocHooks and RunFreeHooks. Can be used in places where hooks
-// are undesirable, like in symbolizer or unwinder.
-class ScopedDisableMallocHooks {
- public:
-  ScopedDisableMallocHooks();
-  ~ScopedDisableMallocHooks();
-};
-
 class ReservedAddressRange {
  public:
   uptr Init(uptr size, const char *name = nullptr, uptr fixed_addr = 0);

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_fuchsia.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_fuchsia.cpp
index 97bef87a1165b..848953a6ab007 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_fuchsia.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_fuchsia.cpp
@@ -75,7 +75,6 @@ void Abort() { abort(); }
 int Atexit(void (*function)(void)) { return atexit(function); }
 
 void GetThreadStackTopAndBottom(bool, uptr *stack_top, uptr *stack_bottom) {
-  ScopedDisableMallocHooks disable_hooks;  // pthread can malloc.
   pthread_attr_t attr;
   CHECK_EQ(pthread_getattr_np(pthread_self(), &attr), 0);
   void *base;

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
index b7577314b8bde..25ad825f568bd 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
@@ -103,7 +103,6 @@ void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
                                 uptr *stack_bottom) {
   CHECK(stack_top);
   CHECK(stack_bottom);
-  ScopedDisableMallocHooks disable_hooks;  // pthread can malloc.
   if (at_initialization) {
     // This is the main thread. Libpthread may not be initialized yet.
     struct rlimit rl;

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
index e8f7af484fb84..41b90b9f35802 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
@@ -410,7 +410,6 @@ void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
                                 uptr *stack_bottom) {
   CHECK(stack_top);
   CHECK(stack_bottom);
-  ScopedDisableMallocHooks disable_hooks;  // pthread can malloc.
   uptr stacksize = pthread_get_stacksize_np(pthread_self());
   // pthread_get_stacksize_np() returns an incorrect stack size for the main
   // thread on Mavericks. See

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer.h b/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer.h
index 96b27c047dc1f..bad4761e345fe 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer.h
@@ -212,8 +212,6 @@ class Symbolizer final {
     ~SymbolizerScope();
    private:
     const Symbolizer *sym_;
-
-    ScopedDisableMallocHooks disable_hooks_;  // Symbolizer can malloc.
   };
 };
 

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_markup.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_markup.cpp
index 9670dcf829e2e..1ec0c5cad7a20 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_markup.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_markup.cpp
@@ -126,7 +126,6 @@ _Unwind_Reason_Code Unwind_Trace(struct _Unwind_Context *ctx, void *param) {
 void BufferedStackTrace::UnwindSlow(uptr pc, u32 max_depth) {
   CHECK_GE(max_depth, 2);
   size = 0;
-  ScopedDisableMallocHooks disable_hooks;  // libunwind can malloc.
   UnwindTraceArg arg = {this, Min(max_depth + 1, kStackTraceMax)};
   _Unwind_Backtrace(Unwind_Trace, &arg);
   CHECK_GT(size, 0);

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_unwind_linux_libcdep.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_unwind_linux_libcdep.cpp
index 89db2a3aa14f3..b2628dcc4dc1f 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_unwind_linux_libcdep.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_unwind_linux_libcdep.cpp
@@ -126,7 +126,6 @@ void SanitizerInitializeUnwinder() {
 void BufferedStackTrace::UnwindSlow(uptr pc, u32 max_depth) {
   CHECK_GE(max_depth, 2);
   size = 0;
-  ScopedDisableMallocHooks disable_hooks;  // libunwind can malloc.
   UnwindTraceArg arg = {this, Min(max_depth + 1, kStackTraceMax)};
   _Unwind_Backtrace(Unwind_Trace, &arg);
   // We need to pop a few frames so that pc is on top.
@@ -157,7 +156,6 @@ void BufferedStackTrace::UnwindSlow(uptr pc, void *context, u32 max_depth) {
     return;
   }
 
-  ScopedDisableMallocHooks disable_hooks;  // Maybe unneeded, but won't hurt.
   void *map = acquire_my_map_info_list();
   CHECK(map);
   InternalMmapVector<backtrace_frame_t> frames(kStackTraceMax);

diff  --git a/compiler-rt/test/sanitizer_common/TestCases/malloc_hook_skip.cpp b/compiler-rt/test/sanitizer_common/TestCases/malloc_hook_skip.cpp
deleted file mode 100644
index a276acd39bb0c..0000000000000
--- a/compiler-rt/test/sanitizer_common/TestCases/malloc_hook_skip.cpp
+++ /dev/null
@@ -1,38 +0,0 @@
-// RUN: %clangxx -O0 -g %s -o %t && %run %t
-
-// Test requires platform with thread local support with no dependency on malloc.
-// UNSUPPORTED: android
-// UNSUPPORTED: ios
-// UNSUPPORTED: darwin
-
-#include <assert.h>
-#include <sanitizer/allocator_interface.h>
-#include <sanitizer/coverage_interface.h>
-#include <stdlib.h>
-
-static int hooks;
-extern "C" {
-
-void __sanitizer_malloc_hook(const volatile void *ptr, size_t sz) { ++hooks; }
-
-void __sanitizer_free_hook(const volatile void *ptr) { ++hooks; }
-
-} // extern "C"
-
-void MallocHook(const volatile void *ptr, size_t sz) { ++hooks; }
-void FreeHook(const volatile void *ptr) { ++hooks; }
-
-int main() {
-  int before;
-
-  before = hooks;
-  __sanitizer_print_stack_trace();
-  assert(before == hooks);
-
-  __sanitizer_install_malloc_and_free_hooks(MallocHook, FreeHook);
-  before = hooks;
-  __sanitizer_print_stack_trace();
-  assert(before == hooks);
-
-  return 0;
-}


        


More information about the llvm-commits mailing list