|
|
|||
File indexing completed on 2025-12-16 09:40:50
0001 // Copyright 2018 The Abseil Authors. 0002 // 0003 // Licensed under the Apache License, Version 2.0 (the "License"); 0004 // you may not use this file except in compliance with the License. 0005 // You may obtain a copy of the License at 0006 // 0007 // https://www.apache.org/licenses/LICENSE-2.0 0008 // 0009 // Unless required by applicable law or agreed to in writing, software 0010 // distributed under the License is distributed on an "AS IS" BASIS, 0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0012 // See the License for the specific language governing permissions and 0013 // limitations under the License. 0014 // 0015 // ----------------------------------------------------------------------------- 0016 // File: leak_check.h 0017 // ----------------------------------------------------------------------------- 0018 // 0019 // This file contains functions that affect leak checking behavior within 0020 // targets built with the LeakSanitizer (LSan), a memory leak detector that is 0021 // integrated within the AddressSanitizer (ASan) as an additional component, or 0022 // which can be used standalone. LSan and ASan are included (or can be provided) 0023 // as additional components for most compilers such as Clang, gcc and MSVC. 0024 // Note: this leak checking API is not yet supported in MSVC. 0025 // Leak checking is enabled by default in all ASan builds. 0026 // 0027 // https://clang.llvm.org/docs/LeakSanitizer.html 0028 // https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer 0029 // 0030 // GCC and Clang both automatically enable LeakSanitizer when AddressSanitizer 0031 // is enabled. To use the mode, simply pass `-fsanitize=address` to both the 0032 // compiler and linker. An example Bazel command could be 0033 // 0034 // $ bazel test --copt=-fsanitize=address --linkopt=-fsanitize=address ... 0035 // 0036 // GCC and Clang auto support a standalone LeakSanitizer mode (a mode which does 0037 // not also use AddressSanitizer). To use the mode, simply pass 0038 // `-fsanitize=leak` to both the compiler and linker. Since GCC does not 0039 // currently provide a way of detecting this mode at compile-time, GCC users 0040 // must also pass -DLEAK_SANITIZER to the compiler. An example Bazel command 0041 // could be 0042 // 0043 // $ bazel test --copt=-DLEAK_SANITIZER --copt=-fsanitize=leak 0044 // --linkopt=-fsanitize=leak ... 0045 // 0046 // ----------------------------------------------------------------------------- 0047 #ifndef ABSL_DEBUGGING_LEAK_CHECK_H_ 0048 #define ABSL_DEBUGGING_LEAK_CHECK_H_ 0049 0050 #include <cstddef> 0051 0052 #include "absl/base/config.h" 0053 0054 namespace absl { 0055 ABSL_NAMESPACE_BEGIN 0056 0057 // HaveLeakSanitizer() 0058 // 0059 // Returns true if a leak-checking sanitizer (either ASan or standalone LSan) is 0060 // currently built into this target. 0061 bool HaveLeakSanitizer(); 0062 0063 // LeakCheckerIsActive() 0064 // 0065 // Returns true if a leak-checking sanitizer (either ASan or standalone LSan) is 0066 // currently built into this target and is turned on. 0067 bool LeakCheckerIsActive(); 0068 0069 // DoIgnoreLeak() 0070 // 0071 // Implements `IgnoreLeak()` below. This function should usually 0072 // not be called directly; calling `IgnoreLeak()` is preferred. 0073 void DoIgnoreLeak(const void* ptr); 0074 0075 // IgnoreLeak() 0076 // 0077 // Instruct the leak sanitizer to ignore leak warnings on the object referenced 0078 // by the passed pointer, as well as all heap objects transitively referenced 0079 // by it. The passed object pointer can point to either the beginning of the 0080 // object or anywhere within it. 0081 // 0082 // Example: 0083 // 0084 // static T* obj = IgnoreLeak(new T(...)); 0085 // 0086 // If the passed `ptr` does not point to an actively allocated object at the 0087 // time `IgnoreLeak()` is called, the call is a no-op; if it is actively 0088 // allocated, leak sanitizer will assume this object is referenced even if 0089 // there is no actual reference in user memory. 0090 // 0091 template <typename T> 0092 T* IgnoreLeak(T* ptr) { 0093 DoIgnoreLeak(ptr); 0094 return ptr; 0095 } 0096 0097 // FindAndReportLeaks() 0098 // 0099 // If any leaks are detected, prints a leak report and returns true. This 0100 // function may be called repeatedly, and does not affect end-of-process leak 0101 // checking. 0102 // 0103 // Example: 0104 // if (FindAndReportLeaks()) { 0105 // ... diagnostic already printed. Exit with failure code. 0106 // exit(1) 0107 // } 0108 bool FindAndReportLeaks(); 0109 0110 // LeakCheckDisabler 0111 // 0112 // This helper class indicates that any heap allocations done in the code block 0113 // covered by the scoped object, which should be allocated on the stack, will 0114 // not be reported as leaks. Leak check disabling will occur within the code 0115 // block and any nested function calls within the code block. 0116 // 0117 // Example: 0118 // 0119 // void Foo() { 0120 // LeakCheckDisabler disabler; 0121 // ... code that allocates objects whose leaks should be ignored ... 0122 // } 0123 // 0124 // REQUIRES: Destructor runs in same thread as constructor 0125 class LeakCheckDisabler { 0126 public: 0127 LeakCheckDisabler(); 0128 LeakCheckDisabler(const LeakCheckDisabler&) = delete; 0129 LeakCheckDisabler& operator=(const LeakCheckDisabler&) = delete; 0130 ~LeakCheckDisabler(); 0131 }; 0132 0133 // RegisterLivePointers() 0134 // 0135 // Registers `ptr[0,size-1]` as pointers to memory that is still actively being 0136 // referenced and for which leak checking should be ignored. This function is 0137 // useful if you store pointers in mapped memory, for memory ranges that we know 0138 // are correct but for which normal analysis would flag as leaked code. 0139 void RegisterLivePointers(const void* ptr, size_t size); 0140 0141 // UnRegisterLivePointers() 0142 // 0143 // Deregisters the pointers previously marked as active in 0144 // `RegisterLivePointers()`, enabling leak checking of those pointers. 0145 void UnRegisterLivePointers(const void* ptr, size_t size); 0146 0147 ABSL_NAMESPACE_END 0148 } // namespace absl 0149 0150 #endif // ABSL_DEBUGGING_LEAK_CHECK_H_
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|