Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 1 | // Copyright 2018 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 | // ----------------------------------------------------------------------------- |
| 16 | // File: leak_check.h |
| 17 | // ----------------------------------------------------------------------------- |
| 18 | // |
| 19 | // This file contains functions that affect leak checking behavior within |
| 20 | // targets built with the LeakSanitizer (LSan), a memory leak detector that is |
| 21 | // integrated within the AddressSanitizer (ASan) as an additional component, or |
| 22 | // which can be used standalone. LSan and ASan are included (or can be provided) |
| 23 | // as additional components for most compilers such as Clang, gcc and MSVC. |
| 24 | // Note: this leak checking API is not yet supported in MSVC. |
| 25 | // Leak checking is enabled by default in all ASan builds. |
| 26 | // |
| 27 | // See https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer |
| 28 | // |
| 29 | // ----------------------------------------------------------------------------- |
| 30 | #ifndef ABSL_DEBUGGING_LEAK_CHECK_H_ |
| 31 | #define ABSL_DEBUGGING_LEAK_CHECK_H_ |
| 32 | |
| 33 | #include <cstddef> |
| 34 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 35 | #include "absl/base/config.h" |
| 36 | |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 37 | namespace absl { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 38 | ABSL_NAMESPACE_BEGIN |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 39 | |
| 40 | // HaveLeakSanitizer() |
| 41 | // |
| 42 | // Returns true if a leak-checking sanitizer (either ASan or standalone LSan) is |
| 43 | // currently built into this target. |
| 44 | bool HaveLeakSanitizer(); |
| 45 | |
| 46 | // DoIgnoreLeak() |
| 47 | // |
| 48 | // Implements `IgnoreLeak()` below. This function should usually |
| 49 | // not be called directly; calling `IgnoreLeak()` is preferred. |
| 50 | void DoIgnoreLeak(const void* ptr); |
| 51 | |
| 52 | // IgnoreLeak() |
| 53 | // |
| 54 | // Instruct the leak sanitizer to ignore leak warnings on the object referenced |
| 55 | // by the passed pointer, as well as all heap objects transitively referenced |
| 56 | // by it. The passed object pointer can point to either the beginning of the |
| 57 | // object or anywhere within it. |
| 58 | // |
| 59 | // Example: |
| 60 | // |
| 61 | // static T* obj = IgnoreLeak(new T(...)); |
| 62 | // |
| 63 | // If the passed `ptr` does not point to an actively allocated object at the |
| 64 | // time `IgnoreLeak()` is called, the call is a no-op; if it is actively |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 65 | // allocated, leak sanitizer will assume this object is referenced even if |
| 66 | // there is no actual reference in user memory. |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 67 | // |
| 68 | template <typename T> |
| 69 | T* IgnoreLeak(T* ptr) { |
| 70 | DoIgnoreLeak(ptr); |
| 71 | return ptr; |
| 72 | } |
| 73 | |
| 74 | // LeakCheckDisabler |
| 75 | // |
| 76 | // This helper class indicates that any heap allocations done in the code block |
| 77 | // covered by the scoped object, which should be allocated on the stack, will |
| 78 | // not be reported as leaks. Leak check disabling will occur within the code |
| 79 | // block and any nested function calls within the code block. |
| 80 | // |
| 81 | // Example: |
| 82 | // |
| 83 | // void Foo() { |
| 84 | // LeakCheckDisabler disabler; |
| 85 | // ... code that allocates objects whose leaks should be ignored ... |
| 86 | // } |
| 87 | // |
| 88 | // REQUIRES: Destructor runs in same thread as constructor |
| 89 | class LeakCheckDisabler { |
| 90 | public: |
| 91 | LeakCheckDisabler(); |
| 92 | LeakCheckDisabler(const LeakCheckDisabler&) = delete; |
| 93 | LeakCheckDisabler& operator=(const LeakCheckDisabler&) = delete; |
| 94 | ~LeakCheckDisabler(); |
| 95 | }; |
| 96 | |
| 97 | // RegisterLivePointers() |
| 98 | // |
| 99 | // Registers `ptr[0,size-1]` as pointers to memory that is still actively being |
| 100 | // referenced and for which leak checking should be ignored. This function is |
| 101 | // useful if you store pointers in mapped memory, for memory ranges that we know |
| 102 | // are correct but for which normal analysis would flag as leaked code. |
| 103 | void RegisterLivePointers(const void* ptr, size_t size); |
| 104 | |
| 105 | // UnRegisterLivePointers() |
| 106 | // |
| 107 | // Deregisters the pointers previously marked as active in |
| 108 | // `RegisterLivePointers()`, enabling leak checking of those pointers. |
| 109 | void UnRegisterLivePointers(const void* ptr, size_t size); |
| 110 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 111 | ABSL_NAMESPACE_END |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 112 | } // namespace absl |
| 113 | |
| 114 | #endif // ABSL_DEBUGGING_LEAK_CHECK_H_ |