blob: b07704ea62ea217576ac426ee50cef058a04d2aa [file] [log] [blame]
Austin Schuh745610d2015-09-06 18:19:50 -07001// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2// Copyright (c) 2005, Google Inc.
3// All rights reserved.
Brian Silverman20350ac2021-11-17 18:19:55 -08004//
Austin Schuh745610d2015-09-06 18:19:50 -07005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
Brian Silverman20350ac2021-11-17 18:19:55 -08008//
Austin Schuh745610d2015-09-06 18:19:50 -07009// * 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.
Brian Silverman20350ac2021-11-17 18:19:55 -080018//
Austin Schuh745610d2015-09-06 18:19:50 -070019// 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
Brian Silverman20350ac2021-11-17 18:19:55 -080047#include "common.h" // for UNLIKELY
48
Austin Schuh745610d2015-09-06 18:19:50 -070049namespace base { namespace internal {
50
51// Capacity of 8 means that HookList is 9 words.
52static const int kHookListCapacity = 8;
53// last entry is reserved for deprecated "singular" hooks. So we have
54// 7 "normal" hooks per list
55static const int kHookListMaxValues = 7;
56static const int kHookListSingularIdx = 7;
57
58// HookList: a class that provides synchronized insertions and removals and
59// lockless traversal. Most of the implementation is in malloc_hook.cc.
60template <typename T>
61struct PERFTOOLS_DLL_DECL HookList {
62 COMPILE_ASSERT(sizeof(T) <= sizeof(AtomicWord), T_should_fit_in_AtomicWord);
63
64 // Adds value to the list. Note that duplicates are allowed. Thread-safe and
65 // blocking (acquires hooklist_spinlock). Returns true on success; false
66 // otherwise (failures include invalid value and no space left).
67 bool Add(T value);
68
69 void FixupPrivEndLocked();
70
71 // Removes the first entry matching value from the list. Thread-safe and
72 // blocking (acquires hooklist_spinlock). Returns true on success; false
73 // otherwise (failures include invalid value and no value found).
74 bool Remove(T value);
75
76 // Store up to n values of the list in output_array, and return the number of
77 // elements stored. Thread-safe and non-blocking. This is fast (one memory
78 // access) if the list is empty.
79 int Traverse(T* output_array, int n) const;
80
81 // Fast inline implementation for fast path of Invoke*Hook.
82 bool empty() const {
83 return base::subtle::NoBarrier_Load(&priv_end) == 0;
84 }
85
86 // Used purely to handle deprecated singular hooks
87 T GetSingular() const {
88 const AtomicWord *place = &priv_data[kHookListSingularIdx];
89 return bit_cast<T>(base::subtle::NoBarrier_Load(place));
90 }
91
92 T ExchangeSingular(T new_val);
93
94 // This internal data is not private so that the class is an aggregate and can
95 // be initialized by the linker. Don't access this directly. Use the
96 // INIT_HOOK_LIST macro in malloc_hook.cc.
97
98 // One more than the index of the last valid element in priv_data. During
99 // 'Remove' this may be past the last valid element in priv_data, but
100 // subsequent values will be 0.
101 //
102 // Index kHookListCapacity-1 is reserved as 'deprecated' single hook pointer
103 AtomicWord priv_end;
104 AtomicWord priv_data[kHookListCapacity];
105};
106
107ATTRIBUTE_VISIBILITY_HIDDEN extern HookList<MallocHook::NewHook> new_hooks_;
108ATTRIBUTE_VISIBILITY_HIDDEN extern HookList<MallocHook::DeleteHook> delete_hooks_;
109ATTRIBUTE_VISIBILITY_HIDDEN extern HookList<MallocHook::PreMmapHook> premmap_hooks_;
110ATTRIBUTE_VISIBILITY_HIDDEN extern HookList<MallocHook::MmapHook> mmap_hooks_;
111ATTRIBUTE_VISIBILITY_HIDDEN extern HookList<MallocHook::MmapReplacement> mmap_replacement_;
112ATTRIBUTE_VISIBILITY_HIDDEN extern HookList<MallocHook::MunmapHook> munmap_hooks_;
113ATTRIBUTE_VISIBILITY_HIDDEN extern HookList<MallocHook::MunmapReplacement> munmap_replacement_;
114ATTRIBUTE_VISIBILITY_HIDDEN extern HookList<MallocHook::MremapHook> mremap_hooks_;
115ATTRIBUTE_VISIBILITY_HIDDEN extern HookList<MallocHook::PreSbrkHook> presbrk_hooks_;
116ATTRIBUTE_VISIBILITY_HIDDEN extern HookList<MallocHook::SbrkHook> sbrk_hooks_;
117
118} } // namespace base::internal
119
120// The following method is DEPRECATED
121inline MallocHook::NewHook MallocHook::GetNewHook() {
122 return base::internal::new_hooks_.GetSingular();
123}
124
125inline void MallocHook::InvokeNewHook(const void* p, size_t s) {
Brian Silverman20350ac2021-11-17 18:19:55 -0800126 if (PREDICT_FALSE(!base::internal::new_hooks_.empty())) {
Austin Schuh745610d2015-09-06 18:19:50 -0700127 InvokeNewHookSlow(p, s);
128 }
129}
130
131// The following method is DEPRECATED
132inline MallocHook::DeleteHook MallocHook::GetDeleteHook() {
133 return base::internal::delete_hooks_.GetSingular();
134}
135
136inline void MallocHook::InvokeDeleteHook(const void* p) {
Brian Silverman20350ac2021-11-17 18:19:55 -0800137 if (PREDICT_FALSE(!base::internal::delete_hooks_.empty())) {
Austin Schuh745610d2015-09-06 18:19:50 -0700138 InvokeDeleteHookSlow(p);
139 }
140}
141
142// The following method is DEPRECATED
143inline MallocHook::PreMmapHook MallocHook::GetPreMmapHook() {
144 return base::internal::premmap_hooks_.GetSingular();
145}
146
147inline void MallocHook::InvokePreMmapHook(const void* start,
148 size_t size,
149 int protection,
150 int flags,
151 int fd,
152 off_t offset) {
153 if (!base::internal::premmap_hooks_.empty()) {
154 InvokePreMmapHookSlow(start, size, protection, flags, fd, offset);
155 }
156}
157
158// The following method is DEPRECATED
159inline MallocHook::MmapHook MallocHook::GetMmapHook() {
160 return base::internal::mmap_hooks_.GetSingular();
161}
162
163inline void MallocHook::InvokeMmapHook(const void* result,
164 const void* start,
165 size_t size,
166 int protection,
167 int flags,
168 int fd,
169 off_t offset) {
170 if (!base::internal::mmap_hooks_.empty()) {
171 InvokeMmapHookSlow(result, start, size, protection, flags, fd, offset);
172 }
173}
174
175inline bool MallocHook::InvokeMmapReplacement(const void* start,
176 size_t size,
177 int protection,
178 int flags,
179 int fd,
180 off_t offset,
181 void** result) {
182 if (!base::internal::mmap_replacement_.empty()) {
183 return InvokeMmapReplacementSlow(start, size,
184 protection, flags,
185 fd, offset,
186 result);
187 }
188 return false;
189}
190
191// The following method is DEPRECATED
192inline MallocHook::MunmapHook MallocHook::GetMunmapHook() {
193 return base::internal::munmap_hooks_.GetSingular();
194}
195
196inline void MallocHook::InvokeMunmapHook(const void* p, size_t size) {
197 if (!base::internal::munmap_hooks_.empty()) {
198 InvokeMunmapHookSlow(p, size);
199 }
200}
201
202inline bool MallocHook::InvokeMunmapReplacement(
203 const void* p, size_t size, int* result) {
204 if (!base::internal::mmap_replacement_.empty()) {
205 return InvokeMunmapReplacementSlow(p, size, result);
206 }
207 return false;
208}
209
210// The following method is DEPRECATED
211inline MallocHook::MremapHook MallocHook::GetMremapHook() {
212 return base::internal::mremap_hooks_.GetSingular();
213}
214
215inline void MallocHook::InvokeMremapHook(const void* result,
216 const void* old_addr,
217 size_t old_size,
218 size_t new_size,
219 int flags,
220 const void* new_addr) {
221 if (!base::internal::mremap_hooks_.empty()) {
222 InvokeMremapHookSlow(result, old_addr, old_size, new_size, flags, new_addr);
223 }
224}
225
226// The following method is DEPRECATED
227inline MallocHook::PreSbrkHook MallocHook::GetPreSbrkHook() {
228 return base::internal::presbrk_hooks_.GetSingular();
229}
230
231inline void MallocHook::InvokePreSbrkHook(ptrdiff_t increment) {
232 if (!base::internal::presbrk_hooks_.empty() && increment != 0) {
233 InvokePreSbrkHookSlow(increment);
234 }
235}
236
237// The following method is DEPRECATED
238inline MallocHook::SbrkHook MallocHook::GetSbrkHook() {
239 return base::internal::sbrk_hooks_.GetSingular();
240}
241
242inline void MallocHook::InvokeSbrkHook(const void* result,
243 ptrdiff_t increment) {
244 if (!base::internal::sbrk_hooks_.empty() && increment != 0) {
245 InvokeSbrkHookSlow(result, increment);
246 }
247}
248
249#endif /* _MALLOC_HOOK_INL_H_ */