From 4728301c343b451681fc6b907a45ce2d28cffcf1 Mon Sep 17 00:00:00 2001 From: Bright Chen Date: Tue, 31 Dec 2024 21:38:37 +0800 Subject: [PATCH 1/2] Instruct LeakSanitizer to ignore designated memory leaks of Server and Singleton --- src/brpc/server.cpp | 12 +++++++++--- 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, 33 insertions(+), 6 deletions(-) diff --git a/src/brpc/server.cpp b/src/brpc/server.cpp index 9ab79fc3ea..e98e8f3492 100644 --- a/src/brpc/server.cpp +++ b/src/brpc/server.cpp @@ -24,12 +24,14 @@ #include // 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 +887,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 9eb4283e4d..924fe4395d 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 aec55fe005..231e9982b6 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 . extern "C" { diff --git a/src/butil/lazy_instance.h b/src/butil/lazy_instance.h index 38b48a87e0..e1daeb5894 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::New(instance); } diff --git a/src/butil/memory/singleton.h b/src/butil/memory/singleton.h index 51ce52544c..f76a8317b5 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(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; } From f3c1b55ed7107079cc469c90187c78996c706fab Mon Sep 17 00:00:00 2001 From: Bright Chen Date: Thu, 2 Jan 2025 21:01:24 +0800 Subject: [PATCH 2/2] Remove the redundant line --- src/brpc/server.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/brpc/server.cpp b/src/brpc/server.cpp index e98e8f3492..ade8e274d5 100644 --- a/src/brpc/server.cpp +++ b/src/brpc/server.cpp @@ -26,8 +26,7 @@ #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/logging.h" // CHECK #include "butil/time.h" #include "butil/class_name.h" #include "butil/string_printf.h"