leak_check.cc (2418B)
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 21 #include "absl/debugging/leak_check.h" 22 23 #include "absl/base/attributes.h" 24 #include "absl/base/config.h" 25 26 #if defined(ABSL_HAVE_LEAK_SANITIZER) 27 28 #include <sanitizer/lsan_interface.h> 29 30 #if ABSL_HAVE_ATTRIBUTE_WEAK 31 extern "C" ABSL_ATTRIBUTE_WEAK int __lsan_is_turned_off() { return 0; } 32 #endif 33 34 namespace absl { 35 ABSL_NAMESPACE_BEGIN 36 bool HaveLeakSanitizer() { return true; } 37 38 #if ABSL_HAVE_ATTRIBUTE_WEAK 39 bool LeakCheckerIsActive() { 40 return __lsan_is_turned_off() == 0; 41 } 42 #else 43 bool LeakCheckerIsActive() { return true; } 44 #endif 45 46 bool FindAndReportLeaks() { return __lsan_do_recoverable_leak_check() != 0; } 47 void DoIgnoreLeak(const void* ptr) { __lsan_ignore_object(ptr); } 48 void RegisterLivePointers(const void* ptr, size_t size) { 49 __lsan_register_root_region(ptr, size); 50 } 51 void UnRegisterLivePointers(const void* ptr, size_t size) { 52 __lsan_unregister_root_region(ptr, size); 53 } 54 LeakCheckDisabler::LeakCheckDisabler() { __lsan_disable(); } 55 LeakCheckDisabler::~LeakCheckDisabler() { __lsan_enable(); } 56 ABSL_NAMESPACE_END 57 } // namespace absl 58 59 #else // defined(ABSL_HAVE_LEAK_SANITIZER) 60 61 namespace absl { 62 ABSL_NAMESPACE_BEGIN 63 bool HaveLeakSanitizer() { return false; } 64 bool LeakCheckerIsActive() { return false; } 65 void DoIgnoreLeak(const void*) { } 66 void RegisterLivePointers(const void*, size_t) { } 67 void UnRegisterLivePointers(const void*, size_t) { } 68 LeakCheckDisabler::LeakCheckDisabler() = default; 69 LeakCheckDisabler::~LeakCheckDisabler() = default; 70 ABSL_NAMESPACE_END 71 } // namespace absl 72 73 #endif // defined(ABSL_HAVE_LEAK_SANITIZER)