You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

leak_check.h 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2018 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // -----------------------------------------------------------------------------
  16. // File: leak_check.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // This file contains functions that affect leak checking behavior within
  20. // targets built with the LeakSanitizer (LSan), a memory leak detector that is
  21. // integrated within the AddressSanitizer (ASan) as an additional component, or
  22. // which can be used standalone. LSan and ASan are included (or can be provided)
  23. // as additional components for most compilers such as Clang, gcc and MSVC.
  24. // Note: this leak checking API is not yet supported in MSVC.
  25. // Leak checking is enabled by default in all ASan builds.
  26. //
  27. // https://clang.llvm.org/docs/LeakSanitizer.html
  28. // https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer
  29. //
  30. // GCC and Clang both automatically enable LeakSanitizer when AddressSanitizer
  31. // is enabled. To use the mode, simply pass `-fsanitize=address` to both the
  32. // compiler and linker. An example Bazel command could be
  33. //
  34. // $ bazel test --copt=-fsanitize=address --linkopt=-fsanitize=address ...
  35. //
  36. // GCC and Clang auto support a standalone LeakSanitizer mode (a mode which does
  37. // not also use AddressSanitizer). To use the mode, simply pass
  38. // `-fsanitize=leak` to both the compiler and linker. Since GCC does not
  39. // currently provide a way of detecting this mode at compile-time, GCC users
  40. // must also pass -DLEAK_SANIITIZER to the compiler. An example Bazel command
  41. // could be
  42. //
  43. // $ bazel test --copt=-DLEAK_SANITIZER --copt=-fsanitize=leak
  44. // --linkopt=-fsanitize=leak ...
  45. //
  46. // -----------------------------------------------------------------------------
  47. #ifndef ABSL_DEBUGGING_LEAK_CHECK_H_
  48. #define ABSL_DEBUGGING_LEAK_CHECK_H_
  49. #include <cstddef>
  50. #include "absl/base/config.h"
  51. namespace absl
  52. {
  53. ABSL_NAMESPACE_BEGIN
  54. // HaveLeakSanitizer()
  55. //
  56. // Returns true if a leak-checking sanitizer (either ASan or standalone LSan) is
  57. // currently built into this target.
  58. bool HaveLeakSanitizer();
  59. // LeakCheckerIsActive()
  60. //
  61. // Returns true if a leak-checking sanitizer (either ASan or standalone LSan) is
  62. // currently built into this target and is turned on.
  63. bool LeakCheckerIsActive();
  64. // DoIgnoreLeak()
  65. //
  66. // Implements `IgnoreLeak()` below. This function should usually
  67. // not be called directly; calling `IgnoreLeak()` is preferred.
  68. void DoIgnoreLeak(const void* ptr);
  69. // IgnoreLeak()
  70. //
  71. // Instruct the leak sanitizer to ignore leak warnings on the object referenced
  72. // by the passed pointer, as well as all heap objects transitively referenced
  73. // by it. The passed object pointer can point to either the beginning of the
  74. // object or anywhere within it.
  75. //
  76. // Example:
  77. //
  78. // static T* obj = IgnoreLeak(new T(...));
  79. //
  80. // If the passed `ptr` does not point to an actively allocated object at the
  81. // time `IgnoreLeak()` is called, the call is a no-op; if it is actively
  82. // allocated, leak sanitizer will assume this object is referenced even if
  83. // there is no actual reference in user memory.
  84. //
  85. template<typename T>
  86. T* IgnoreLeak(T* ptr)
  87. {
  88. DoIgnoreLeak(ptr);
  89. return ptr;
  90. }
  91. // FindAndReportLeaks()
  92. //
  93. // If any leaks are detected, prints a leak report and returns true. This
  94. // function may be called repeatedly, and does not affect end-of-process leak
  95. // checking.
  96. //
  97. // Example:
  98. // if (FindAndReportLeaks()) {
  99. // ... diagnostic already printed. Exit with failure code.
  100. // exit(1)
  101. // }
  102. bool FindAndReportLeaks();
  103. // LeakCheckDisabler
  104. //
  105. // This helper class indicates that any heap allocations done in the code block
  106. // covered by the scoped object, which should be allocated on the stack, will
  107. // not be reported as leaks. Leak check disabling will occur within the code
  108. // block and any nested function calls within the code block.
  109. //
  110. // Example:
  111. //
  112. // void Foo() {
  113. // LeakCheckDisabler disabler;
  114. // ... code that allocates objects whose leaks should be ignored ...
  115. // }
  116. //
  117. // REQUIRES: Destructor runs in same thread as constructor
  118. class LeakCheckDisabler
  119. {
  120. public:
  121. LeakCheckDisabler();
  122. LeakCheckDisabler(const LeakCheckDisabler&) = delete;
  123. LeakCheckDisabler& operator=(const LeakCheckDisabler&) = delete;
  124. ~LeakCheckDisabler();
  125. };
  126. // RegisterLivePointers()
  127. //
  128. // Registers `ptr[0,size-1]` as pointers to memory that is still actively being
  129. // referenced and for which leak checking should be ignored. This function is
  130. // useful if you store pointers in mapped memory, for memory ranges that we know
  131. // are correct but for which normal analysis would flag as leaked code.
  132. void RegisterLivePointers(const void* ptr, size_t size);
  133. // UnRegisterLivePointers()
  134. //
  135. // Deregisters the pointers previously marked as active in
  136. // `RegisterLivePointers()`, enabling leak checking of those pointers.
  137. void UnRegisterLivePointers(const void* ptr, size_t size);
  138. ABSL_NAMESPACE_END
  139. } // namespace absl
  140. #endif // ABSL_DEBUGGING_LEAK_CHECK_H_