Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 1 | // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
| 2 | // Copyright (c) 2005, Google Inc. |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
| 8 | // |
| 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | // --- |
| 32 | // Author: Sanjay Ghemawat |
| 33 | // |
| 34 | // This has the implementation details of malloc_hook that are needed |
| 35 | // to use malloc-hook inside the tcmalloc system. It does not hold |
| 36 | // any of the client-facing calls that are used to add new hooks. |
| 37 | |
| 38 | #ifndef _MALLOC_HOOK_INL_H_ |
| 39 | #define _MALLOC_HOOK_INL_H_ |
| 40 | |
| 41 | #include <stddef.h> |
| 42 | #include <sys/types.h> |
| 43 | #include "base/atomicops.h" |
| 44 | #include "base/basictypes.h" |
| 45 | #include <gperftools/malloc_hook.h> |
| 46 | |
| 47 | namespace base { namespace internal { |
| 48 | |
| 49 | // Capacity of 8 means that HookList is 9 words. |
| 50 | static const int kHookListCapacity = 8; |
| 51 | // last entry is reserved for deprecated "singular" hooks. So we have |
| 52 | // 7 "normal" hooks per list |
| 53 | static const int kHookListMaxValues = 7; |
| 54 | static const int kHookListSingularIdx = 7; |
| 55 | |
| 56 | // HookList: a class that provides synchronized insertions and removals and |
| 57 | // lockless traversal. Most of the implementation is in malloc_hook.cc. |
| 58 | template <typename T> |
| 59 | struct PERFTOOLS_DLL_DECL HookList { |
| 60 | COMPILE_ASSERT(sizeof(T) <= sizeof(AtomicWord), T_should_fit_in_AtomicWord); |
| 61 | |
| 62 | // Adds value to the list. Note that duplicates are allowed. Thread-safe and |
| 63 | // blocking (acquires hooklist_spinlock). Returns true on success; false |
| 64 | // otherwise (failures include invalid value and no space left). |
| 65 | bool Add(T value); |
| 66 | |
| 67 | void FixupPrivEndLocked(); |
| 68 | |
| 69 | // Removes the first entry matching value from the list. Thread-safe and |
| 70 | // blocking (acquires hooklist_spinlock). Returns true on success; false |
| 71 | // otherwise (failures include invalid value and no value found). |
| 72 | bool Remove(T value); |
| 73 | |
| 74 | // Store up to n values of the list in output_array, and return the number of |
| 75 | // elements stored. Thread-safe and non-blocking. This is fast (one memory |
| 76 | // access) if the list is empty. |
| 77 | int Traverse(T* output_array, int n) const; |
| 78 | |
| 79 | // Fast inline implementation for fast path of Invoke*Hook. |
| 80 | bool empty() const { |
| 81 | return base::subtle::NoBarrier_Load(&priv_end) == 0; |
| 82 | } |
| 83 | |
| 84 | // Used purely to handle deprecated singular hooks |
| 85 | T GetSingular() const { |
| 86 | const AtomicWord *place = &priv_data[kHookListSingularIdx]; |
| 87 | return bit_cast<T>(base::subtle::NoBarrier_Load(place)); |
| 88 | } |
| 89 | |
| 90 | T ExchangeSingular(T new_val); |
| 91 | |
| 92 | // This internal data is not private so that the class is an aggregate and can |
| 93 | // be initialized by the linker. Don't access this directly. Use the |
| 94 | // INIT_HOOK_LIST macro in malloc_hook.cc. |
| 95 | |
| 96 | // One more than the index of the last valid element in priv_data. During |
| 97 | // 'Remove' this may be past the last valid element in priv_data, but |
| 98 | // subsequent values will be 0. |
| 99 | // |
| 100 | // Index kHookListCapacity-1 is reserved as 'deprecated' single hook pointer |
| 101 | AtomicWord priv_end; |
| 102 | AtomicWord priv_data[kHookListCapacity]; |
| 103 | }; |
| 104 | |
| 105 | ATTRIBUTE_VISIBILITY_HIDDEN extern HookList<MallocHook::NewHook> new_hooks_; |
| 106 | ATTRIBUTE_VISIBILITY_HIDDEN extern HookList<MallocHook::DeleteHook> delete_hooks_; |
| 107 | ATTRIBUTE_VISIBILITY_HIDDEN extern HookList<MallocHook::PreMmapHook> premmap_hooks_; |
| 108 | ATTRIBUTE_VISIBILITY_HIDDEN extern HookList<MallocHook::MmapHook> mmap_hooks_; |
| 109 | ATTRIBUTE_VISIBILITY_HIDDEN extern HookList<MallocHook::MmapReplacement> mmap_replacement_; |
| 110 | ATTRIBUTE_VISIBILITY_HIDDEN extern HookList<MallocHook::MunmapHook> munmap_hooks_; |
| 111 | ATTRIBUTE_VISIBILITY_HIDDEN extern HookList<MallocHook::MunmapReplacement> munmap_replacement_; |
| 112 | ATTRIBUTE_VISIBILITY_HIDDEN extern HookList<MallocHook::MremapHook> mremap_hooks_; |
| 113 | ATTRIBUTE_VISIBILITY_HIDDEN extern HookList<MallocHook::PreSbrkHook> presbrk_hooks_; |
| 114 | ATTRIBUTE_VISIBILITY_HIDDEN extern HookList<MallocHook::SbrkHook> sbrk_hooks_; |
| 115 | |
| 116 | } } // namespace base::internal |
| 117 | |
| 118 | // The following method is DEPRECATED |
| 119 | inline MallocHook::NewHook MallocHook::GetNewHook() { |
| 120 | return base::internal::new_hooks_.GetSingular(); |
| 121 | } |
| 122 | |
| 123 | inline void MallocHook::InvokeNewHook(const void* p, size_t s) { |
| 124 | if (!base::internal::new_hooks_.empty()) { |
| 125 | InvokeNewHookSlow(p, s); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // The following method is DEPRECATED |
| 130 | inline MallocHook::DeleteHook MallocHook::GetDeleteHook() { |
| 131 | return base::internal::delete_hooks_.GetSingular(); |
| 132 | } |
| 133 | |
| 134 | inline void MallocHook::InvokeDeleteHook(const void* p) { |
| 135 | if (!base::internal::delete_hooks_.empty()) { |
| 136 | InvokeDeleteHookSlow(p); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // The following method is DEPRECATED |
| 141 | inline MallocHook::PreMmapHook MallocHook::GetPreMmapHook() { |
| 142 | return base::internal::premmap_hooks_.GetSingular(); |
| 143 | } |
| 144 | |
| 145 | inline void MallocHook::InvokePreMmapHook(const void* start, |
| 146 | size_t size, |
| 147 | int protection, |
| 148 | int flags, |
| 149 | int fd, |
| 150 | off_t offset) { |
| 151 | if (!base::internal::premmap_hooks_.empty()) { |
| 152 | InvokePreMmapHookSlow(start, size, protection, flags, fd, offset); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // The following method is DEPRECATED |
| 157 | inline MallocHook::MmapHook MallocHook::GetMmapHook() { |
| 158 | return base::internal::mmap_hooks_.GetSingular(); |
| 159 | } |
| 160 | |
| 161 | inline void MallocHook::InvokeMmapHook(const void* result, |
| 162 | const void* start, |
| 163 | size_t size, |
| 164 | int protection, |
| 165 | int flags, |
| 166 | int fd, |
| 167 | off_t offset) { |
| 168 | if (!base::internal::mmap_hooks_.empty()) { |
| 169 | InvokeMmapHookSlow(result, start, size, protection, flags, fd, offset); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | inline bool MallocHook::InvokeMmapReplacement(const void* start, |
| 174 | size_t size, |
| 175 | int protection, |
| 176 | int flags, |
| 177 | int fd, |
| 178 | off_t offset, |
| 179 | void** result) { |
| 180 | if (!base::internal::mmap_replacement_.empty()) { |
| 181 | return InvokeMmapReplacementSlow(start, size, |
| 182 | protection, flags, |
| 183 | fd, offset, |
| 184 | result); |
| 185 | } |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | // The following method is DEPRECATED |
| 190 | inline MallocHook::MunmapHook MallocHook::GetMunmapHook() { |
| 191 | return base::internal::munmap_hooks_.GetSingular(); |
| 192 | } |
| 193 | |
| 194 | inline void MallocHook::InvokeMunmapHook(const void* p, size_t size) { |
| 195 | if (!base::internal::munmap_hooks_.empty()) { |
| 196 | InvokeMunmapHookSlow(p, size); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | inline bool MallocHook::InvokeMunmapReplacement( |
| 201 | const void* p, size_t size, int* result) { |
| 202 | if (!base::internal::mmap_replacement_.empty()) { |
| 203 | return InvokeMunmapReplacementSlow(p, size, result); |
| 204 | } |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | // The following method is DEPRECATED |
| 209 | inline MallocHook::MremapHook MallocHook::GetMremapHook() { |
| 210 | return base::internal::mremap_hooks_.GetSingular(); |
| 211 | } |
| 212 | |
| 213 | inline void MallocHook::InvokeMremapHook(const void* result, |
| 214 | const void* old_addr, |
| 215 | size_t old_size, |
| 216 | size_t new_size, |
| 217 | int flags, |
| 218 | const void* new_addr) { |
| 219 | if (!base::internal::mremap_hooks_.empty()) { |
| 220 | InvokeMremapHookSlow(result, old_addr, old_size, new_size, flags, new_addr); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // The following method is DEPRECATED |
| 225 | inline MallocHook::PreSbrkHook MallocHook::GetPreSbrkHook() { |
| 226 | return base::internal::presbrk_hooks_.GetSingular(); |
| 227 | } |
| 228 | |
| 229 | inline void MallocHook::InvokePreSbrkHook(ptrdiff_t increment) { |
| 230 | if (!base::internal::presbrk_hooks_.empty() && increment != 0) { |
| 231 | InvokePreSbrkHookSlow(increment); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | // The following method is DEPRECATED |
| 236 | inline MallocHook::SbrkHook MallocHook::GetSbrkHook() { |
| 237 | return base::internal::sbrk_hooks_.GetSingular(); |
| 238 | } |
| 239 | |
| 240 | inline void MallocHook::InvokeSbrkHook(const void* result, |
| 241 | ptrdiff_t increment) { |
| 242 | if (!base::internal::sbrk_hooks_.empty() && increment != 0) { |
| 243 | InvokeSbrkHookSlow(result, increment); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | #endif /* _MALLOC_HOOK_INL_H_ */ |