Skip to content

Commit 6b29a36

Browse files
authored
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
1 parent d02c7fd commit 6b29a36

5 files changed

Lines changed: 32 additions & 6 deletions

File tree

src/brpc/server.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
#include <google/protobuf/descriptor.h> // ServiceDescriptor
2525
#include "idl_options.pb.h" // option(idl_support)
2626
#include "bthread/unstable.h" // bthread_keytable_pool_init
27-
#include "butil/macros.h" // ARRAY_SIZE
28-
#include "butil/fd_guard.h" // fd_guard
29-
#include "butil/logging.h" // CHECK
27+
#include "butil/macros.h" // ARRAY_SIZE
28+
#include "butil/fd_guard.h" // fd_guard
29+
#include "butil/logging.h" // CHECK
3030
#include "butil/time.h"
3131
#include "butil/class_name.h"
3232
#include "butil/string_printf.h"
33+
#include "butil/debug/leak_annotations.h"
3334
#include "brpc/log.h"
3435
#include "brpc/compress.h"
3536
#include "brpc/policy/nova_pbrpc_protocol.h"
@@ -885,6 +886,10 @@ int Server::StartInternal(const butil::EndPoint& endpoint,
885886
_session_local_data_pool->Reserve(_options.reserved_session_local_data);
886887
}
887888

889+
// Leak of `_keytable_pool' and others is by design.
890+
// See comments in Server::Join() for details.
891+
// Instruct LeakSanitizer to ignore the designated memory leak.
892+
ANNOTATE_SCOPED_MEMORY_LEAK;
888893
// Init _keytable_pool always. If the server was stopped before, the pool
889894
// should be destroyed in Join().
890895
_keytable_pool = new bthread_keytable_pool_t;

src/butil/compiler_specific.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,19 @@
199199
#define WARN_UNUSED_RESULT
200200
#endif
201201

202+
// Compiler feature-detection.
203+
// clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension
204+
#if defined(__has_feature)
205+
#define BUTIL_HAS_FEATURE(FEATURE) __has_feature(FEATURE)
206+
#else
207+
#define BUTIL_HAS_FEATURE(FEATURE) 0
208+
#endif
209+
210+
// Instruct ASan is enabled.
211+
#if BUTIL_HAS_FEATURE(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
212+
#define BUTIL_USE_ASAN 1
213+
#endif
214+
202215
// Tell the compiler a function is using a printf-style format string.
203216
// |format_param| is the one-based index of the format string parameter;
204217
// |dots_param| is the one-based index of the "..." parameter.

src/butil/debug/leak_annotations.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// ANNOTATE_LEAKING_OBJECT_PTR(X): the heap object referenced by pointer X will
2020
// be annotated as a leak.
2121

22-
#if defined(LEAK_SANITIZER) && !defined(OS_NACL)
22+
#if (defined(LEAK_SANITIZER) && !defined(OS_NACL)) || defined(BUTIL_USE_ASAN)
2323

2424
// Public LSan API from <sanitizer/lsan_interface.h>.
2525
extern "C" {

src/butil/lazy_instance.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ struct LeakyLazyInstanceTraits {
105105
#endif
106106

107107
static Type* New(void* instance) {
108+
// Instruct LeakSanitizer to ignore the designated memory leak.
108109
ANNOTATE_SCOPED_MEMORY_LEAK;
109110
return DefaultLazyInstanceTraits<Type>::New(instance);
110111
}

src/butil/memory/singleton.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "butil/memory/aligned_memory.h"
2626
#include "butil/third_party/dynamic_annotations/dynamic_annotations.h"
2727
#include "butil/threading/thread_restrictions.h"
28+
#include "butil/debug/leak_annotations.h"
2829

2930
namespace butil {
3031
namespace internal {
@@ -266,8 +267,14 @@ class Singleton {
266267
butil::subtle::Release_Store(
267268
&instance_, reinterpret_cast<butil::subtle::AtomicWord>(newval));
268269

269-
if (newval != NULL && Traits::kRegisterAtExit)
270-
butil::AtExitManager::RegisterCallback(OnExit, NULL);
270+
if (newval != NULL) {
271+
if (Traits::kRegisterAtExit) {
272+
butil::AtExitManager::RegisterCallback(OnExit, NULL);
273+
} else {
274+
// Instruct LeakSanitizer to ignore the designated memory leak.
275+
ANNOTATE_LEAKING_OBJECT_PTR(newval);
276+
}
277+
}
271278

272279
return newval;
273280
}

0 commit comments

Comments
 (0)