00001 // Copyright 2018 The Abseil Authors. 00002 // 00003 // Licensed under the Apache License, Version 2.0 (the "License"); 00004 // you may not use this file except in compliance with the License. 00005 // You may obtain a copy of the License at 00006 // 00007 // https://www.apache.org/licenses/LICENSE-2.0 00008 // 00009 // Unless required by applicable law or agreed to in writing, software 00010 // distributed under the License is distributed on an "AS IS" BASIS, 00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00012 // See the License for the specific language governing permissions and 00013 // limitations under the License. 00014 // 00015 // ----------------------------------------------------------------------------- 00016 // File: leak_check.h 00017 // ----------------------------------------------------------------------------- 00018 // 00019 // This file contains functions that affect leak checking behavior within 00020 // targets built with the LeakSanitizer (LSan), a memory leak detector that is 00021 // integrated within the AddressSanitizer (ASan) as an additional component, or 00022 // which can be used standalone. LSan and ASan are included (or can be provided) 00023 // as additional components for most compilers such as Clang, gcc and MSVC. 00024 // Note: this leak checking API is not yet supported in MSVC. 00025 // Leak checking is enabled by default in all ASan builds. 00026 // 00027 // See https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer 00028 // 00029 // ----------------------------------------------------------------------------- 00030 #ifndef ABSL_DEBUGGING_LEAK_CHECK_H_ 00031 #define ABSL_DEBUGGING_LEAK_CHECK_H_ 00032 00033 #include <cstddef> 00034 00035 namespace absl { 00036 00037 // HaveLeakSanitizer() 00038 // 00039 // Returns true if a leak-checking sanitizer (either ASan or standalone LSan) is 00040 // currently built into this target. 00041 bool HaveLeakSanitizer(); 00042 00043 // DoIgnoreLeak() 00044 // 00045 // Implements `IgnoreLeak()` below. This function should usually 00046 // not be called directly; calling `IgnoreLeak()` is preferred. 00047 void DoIgnoreLeak(const void* ptr); 00048 00049 // IgnoreLeak() 00050 // 00051 // Instruct the leak sanitizer to ignore leak warnings on the object referenced 00052 // by the passed pointer, as well as all heap objects transitively referenced 00053 // by it. The passed object pointer can point to either the beginning of the 00054 // object or anywhere within it. 00055 // 00056 // Example: 00057 // 00058 // static T* obj = IgnoreLeak(new T(...)); 00059 // 00060 // If the passed `ptr` does not point to an actively allocated object at the 00061 // time `IgnoreLeak()` is called, the call is a no-op; if it is actively 00062 // allocated, the object must not get deallocated later. 00063 // 00064 template <typename T> 00065 T* IgnoreLeak(T* ptr) { 00066 DoIgnoreLeak(ptr); 00067 return ptr; 00068 } 00069 00070 // LeakCheckDisabler 00071 // 00072 // This helper class indicates that any heap allocations done in the code block 00073 // covered by the scoped object, which should be allocated on the stack, will 00074 // not be reported as leaks. Leak check disabling will occur within the code 00075 // block and any nested function calls within the code block. 00076 // 00077 // Example: 00078 // 00079 // void Foo() { 00080 // LeakCheckDisabler disabler; 00081 // ... code that allocates objects whose leaks should be ignored ... 00082 // } 00083 // 00084 // REQUIRES: Destructor runs in same thread as constructor 00085 class LeakCheckDisabler { 00086 public: 00087 LeakCheckDisabler(); 00088 LeakCheckDisabler(const LeakCheckDisabler&) = delete; 00089 LeakCheckDisabler& operator=(const LeakCheckDisabler&) = delete; 00090 ~LeakCheckDisabler(); 00091 }; 00092 00093 // RegisterLivePointers() 00094 // 00095 // Registers `ptr[0,size-1]` as pointers to memory that is still actively being 00096 // referenced and for which leak checking should be ignored. This function is 00097 // useful if you store pointers in mapped memory, for memory ranges that we know 00098 // are correct but for which normal analysis would flag as leaked code. 00099 void RegisterLivePointers(const void* ptr, size_t size); 00100 00101 // UnRegisterLivePointers() 00102 // 00103 // Deregisters the pointers previously marked as active in 00104 // `RegisterLivePointers()`, enabling leak checking of those pointers. 00105 void UnRegisterLivePointers(const void* ptr, size_t size); 00106 00107 } // namespace absl 00108 00109 #endif // ABSL_DEBUGGING_LEAK_CHECK_H_