leak_check.cc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2017 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. // Wrappers around lsan_interface functions.
  16. //
  17. // These are always-available run-time functions manipulating the LeakSanitizer,
  18. // even when the lsan_interface (and LeakSanitizer) is not available. When
  19. // LeakSanitizer is not linked in, these functions become no-op stubs.
  20. #include "absl/debugging/leak_check.h"
  21. #include "absl/base/attributes.h"
  22. #include "absl/base/config.h"
  23. #if defined(ABSL_HAVE_LEAK_SANITIZER)
  24. #include <sanitizer/lsan_interface.h>
  25. #if ABSL_HAVE_ATTRIBUTE_WEAK
  26. extern "C" ABSL_ATTRIBUTE_WEAK int __lsan_is_turned_off();
  27. #endif
  28. namespace absl {
  29. ABSL_NAMESPACE_BEGIN
  30. bool HaveLeakSanitizer() { return true; }
  31. #if ABSL_HAVE_ATTRIBUTE_WEAK
  32. bool LeakCheckerIsActive() {
  33. return !(&__lsan_is_turned_off && __lsan_is_turned_off());
  34. }
  35. #else
  36. bool LeakCheckerIsActive() { return true; }
  37. #endif
  38. bool FindAndReportLeaks() { return __lsan_do_recoverable_leak_check(); }
  39. void DoIgnoreLeak(const void* ptr) { __lsan_ignore_object(ptr); }
  40. void RegisterLivePointers(const void* ptr, size_t size) {
  41. __lsan_register_root_region(ptr, size);
  42. }
  43. void UnRegisterLivePointers(const void* ptr, size_t size) {
  44. __lsan_unregister_root_region(ptr, size);
  45. }
  46. LeakCheckDisabler::LeakCheckDisabler() { __lsan_disable(); }
  47. LeakCheckDisabler::~LeakCheckDisabler() { __lsan_enable(); }
  48. ABSL_NAMESPACE_END
  49. } // namespace absl
  50. #else // defined(ABSL_HAVE_LEAK_SANITIZER)
  51. namespace absl {
  52. ABSL_NAMESPACE_BEGIN
  53. bool HaveLeakSanitizer() { return false; }
  54. bool LeakCheckerIsActive() { return false; }
  55. void DoIgnoreLeak(const void*) { }
  56. void RegisterLivePointers(const void*, size_t) { }
  57. void UnRegisterLivePointers(const void*, size_t) { }
  58. LeakCheckDisabler::LeakCheckDisabler() = default;
  59. LeakCheckDisabler::~LeakCheckDisabler() = default;
  60. ABSL_NAMESPACE_END
  61. } // namespace absl
  62. #endif // defined(ABSL_HAVE_LEAK_SANITIZER)