This is an automated email from the ASF dual-hosted git repository.

wwbmmm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new 6b29a365 Instruct LeakSanitizer to ignore designated memory leaks of 
Server and Singleton (#2858)
6b29a365 is described below

commit 6b29a365a82cd66083cd6ef3b91abf79974e0ca8
Author: Bright Chen <chenguangmin...@foxmail.com>
AuthorDate: Mon Jan 6 14:41:59 2025 +0800

    Instruct LeakSanitizer to ignore designated memory leaks of Server and 
Singleton (#2858)
    
    * Instruct LeakSanitizer to ignore designated memory leaks of Server and 
Singleton
    
    * Remove the redundant line
---
 src/brpc/server.cpp                | 11 ++++++++---
 src/butil/compiler_specific.h      | 13 +++++++++++++
 src/butil/debug/leak_annotations.h |  2 +-
 src/butil/lazy_instance.h          |  1 +
 src/butil/memory/singleton.h       | 11 +++++++++--
 5 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/src/brpc/server.cpp b/src/brpc/server.cpp
index 9ab79fc3..ade8e274 100644
--- a/src/brpc/server.cpp
+++ b/src/brpc/server.cpp
@@ -24,12 +24,13 @@
 #include <google/protobuf/descriptor.h>             // ServiceDescriptor
 #include "idl_options.pb.h"                         // option(idl_support)
 #include "bthread/unstable.h"                       // 
bthread_keytable_pool_init
-#include "butil/macros.h"                            // ARRAY_SIZE
-#include "butil/fd_guard.h"                          // fd_guard
-#include "butil/logging.h"                           // CHECK
+#include "butil/macros.h"                           // ARRAY_SIZE
+#include "butil/fd_guard.h"                         // fd_guard
+#include "butil/logging.h"                          // CHECK
 #include "butil/time.h"
 #include "butil/class_name.h"
 #include "butil/string_printf.h"
+#include "butil/debug/leak_annotations.h"
 #include "brpc/log.h"
 #include "brpc/compress.h"
 #include "brpc/policy/nova_pbrpc_protocol.h"
@@ -885,6 +886,10 @@ int Server::StartInternal(const butil::EndPoint& endpoint,
         
_session_local_data_pool->Reserve(_options.reserved_session_local_data);
     }
 
+    // Leak of `_keytable_pool' and others is by design.
+    // See comments in Server::Join() for details.
+    // Instruct LeakSanitizer to ignore the designated memory leak.
+    ANNOTATE_SCOPED_MEMORY_LEAK;
     // Init _keytable_pool always. If the server was stopped before, the pool
     // should be destroyed in Join().
     _keytable_pool = new bthread_keytable_pool_t;
diff --git a/src/butil/compiler_specific.h b/src/butil/compiler_specific.h
index 9eb4283e..924fe439 100644
--- a/src/butil/compiler_specific.h
+++ b/src/butil/compiler_specific.h
@@ -199,6 +199,19 @@
 #define WARN_UNUSED_RESULT
 #endif
 
+// Compiler feature-detection.
+// clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension
+#if defined(__has_feature)
+#define BUTIL_HAS_FEATURE(FEATURE) __has_feature(FEATURE)
+#else
+#define BUTIL_HAS_FEATURE(FEATURE) 0
+#endif
+
+// Instruct ASan is enabled.
+#if BUTIL_HAS_FEATURE(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
+#define BUTIL_USE_ASAN 1
+#endif
+
 // Tell the compiler a function is using a printf-style format string.
 // |format_param| is the one-based index of the format string parameter;
 // |dots_param| is the one-based index of the "..." parameter.
diff --git a/src/butil/debug/leak_annotations.h 
b/src/butil/debug/leak_annotations.h
index aec55fe0..231e9982 100644
--- a/src/butil/debug/leak_annotations.h
+++ b/src/butil/debug/leak_annotations.h
@@ -19,7 +19,7 @@
 // ANNOTATE_LEAKING_OBJECT_PTR(X): the heap object referenced by pointer X will
 // be annotated as a leak.
 
-#if defined(LEAK_SANITIZER) && !defined(OS_NACL)
+#if (defined(LEAK_SANITIZER) && !defined(OS_NACL)) || defined(BUTIL_USE_ASAN)
 
 // Public LSan API from <sanitizer/lsan_interface.h>.
 extern "C" {
diff --git a/src/butil/lazy_instance.h b/src/butil/lazy_instance.h
index 38b48a87..e1daeb58 100644
--- a/src/butil/lazy_instance.h
+++ b/src/butil/lazy_instance.h
@@ -105,6 +105,7 @@ struct LeakyLazyInstanceTraits {
 #endif
 
   static Type* New(void* instance) {
+    // Instruct LeakSanitizer to ignore the designated memory leak.
     ANNOTATE_SCOPED_MEMORY_LEAK;
     return DefaultLazyInstanceTraits<Type>::New(instance);
   }
diff --git a/src/butil/memory/singleton.h b/src/butil/memory/singleton.h
index 51ce5254..f76a8317 100644
--- a/src/butil/memory/singleton.h
+++ b/src/butil/memory/singleton.h
@@ -25,6 +25,7 @@
 #include "butil/memory/aligned_memory.h"
 #include "butil/third_party/dynamic_annotations/dynamic_annotations.h"
 #include "butil/threading/thread_restrictions.h"
+#include "butil/debug/leak_annotations.h"
 
 namespace butil {
 namespace internal {
@@ -266,8 +267,14 @@ class Singleton {
       butil::subtle::Release_Store(
           &instance_, reinterpret_cast<butil::subtle::AtomicWord>(newval));
 
-      if (newval != NULL && Traits::kRegisterAtExit)
-        butil::AtExitManager::RegisterCallback(OnExit, NULL);
+      if (newval != NULL) {
+        if (Traits::kRegisterAtExit) {
+          butil::AtExitManager::RegisterCallback(OnExit, NULL);
+        } else {
+          // Instruct LeakSanitizer to ignore the designated memory leak.
+          ANNOTATE_LEAKING_OBJECT_PTR(newval);
+        }
+      }
 
       return newval;
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org
For additional commands, e-mail: dev-h...@brpc.apache.org

Reply via email to