leak_check.cc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. // Wrappers around lsan_interface functions.
  15. // When lsan is not linked in, these functions are not available,
  16. // therefore Abseil code which depends on these functions is conditioned on the
  17. // definition of LEAK_SANITIZER.
  18. #include "absl/base/attributes.h"
  19. #include "absl/debugging/leak_check.h"
  20. #ifndef LEAK_SANITIZER
  21. namespace absl {
  22. ABSL_NAMESPACE_BEGIN
  23. bool HaveLeakSanitizer() { return false; }
  24. bool LeakCheckerIsActive() { return false; }
  25. void DoIgnoreLeak(const void*) { }
  26. void RegisterLivePointers(const void*, size_t) { }
  27. void UnRegisterLivePointers(const void*, size_t) { }
  28. LeakCheckDisabler::LeakCheckDisabler() { }
  29. LeakCheckDisabler::~LeakCheckDisabler() { }
  30. ABSL_NAMESPACE_END
  31. } // namespace absl
  32. #else
  33. #include <sanitizer/lsan_interface.h>
  34. #if ABSL_HAVE_ATTRIBUTE_WEAK
  35. extern "C" ABSL_ATTRIBUTE_WEAK int __lsan_is_turned_off();
  36. #endif
  37. namespace absl {
  38. ABSL_NAMESPACE_BEGIN
  39. bool HaveLeakSanitizer() { return true; }
  40. #if ABSL_HAVE_ATTRIBUTE_WEAK
  41. bool LeakCheckerIsActive() {
  42. return !(&__lsan_is_turned_off && __lsan_is_turned_off());
  43. }
  44. #else
  45. bool LeakCheckerIsActive() { return true; }
  46. #endif
  47. bool FindAndReportLeaks() { return __lsan_do_recoverable_leak_check(); }
  48. void DoIgnoreLeak(const void* ptr) { __lsan_ignore_object(ptr); }
  49. void RegisterLivePointers(const void* ptr, size_t size) {
  50. __lsan_register_root_region(ptr, size);
  51. }
  52. void UnRegisterLivePointers(const void* ptr, size_t size) {
  53. __lsan_unregister_root_region(ptr, size);
  54. }
  55. LeakCheckDisabler::LeakCheckDisabler() { __lsan_disable(); }
  56. LeakCheckDisabler::~LeakCheckDisabler() { __lsan_enable(); }
  57. ABSL_NAMESPACE_END
  58. } // namespace absl
  59. #endif // LEAK_SANITIZER