1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
base / allocator / partition_allocator / src / partition_alloc / shim / shim_alloc_functions.h [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef PARTITION_ALLOC_SHIM_SHIM_ALLOC_FUNCTIONS_H_
#error This header is meant to be included only once by allocator_shim*.cc
#endif
#ifndef PARTITION_ALLOC_SHIM_SHIM_ALLOC_FUNCTIONS_H_
#define PARTITION_ALLOC_SHIM_SHIM_ALLOC_FUNCTIONS_H_
#include <cerrno>
#include "partition_alloc/build_config.h"
#include "partition_alloc/buildflags.h"
#include "partition_alloc/partition_alloc_base/bits.h"
#include "partition_alloc/partition_alloc_base/compiler_specific.h"
#include "partition_alloc/partition_alloc_base/memory/page_size.h"
#include "partition_alloc/partition_alloc_check.h"
// This ".h" file is not a header, but a source file meant to be included only
// once, exclusively from one of the allocator_shim*.cc files. See the top-level
// check.
//
// A possible alternative: rename this file to .inc, at the expense of losing
// syntax highlighting in text editors.
//
// NOLINTNEXTLINE(google-build-namespaces)
namespace {
PA_ALWAYS_INLINE size_t GetCachedPageSize() {
static size_t pagesize = 0;
if (pagesize == 0) [[unlikely]] {
pagesize = partition_alloc::internal::base::GetPageSize();
}
return pagesize;
}
} // namespace
// The Shim* functions below are the entry-points into the shim-layer and
// are supposed to be invoked by the allocator_shim_override_*
// headers to route the malloc / new symbols through the shim layer.
// They are defined as ALWAYS_INLINE in order to remove a level of indirection
// between the system-defined entry points and the shim implementations.
extern "C" {
// The general pattern for allocations is:
// - Try to allocate, if succeeded return the pointer.
// - If the allocation failed:
// - Call the std::new_handler if it was a C++ allocation.
// - Call the std::new_handler if it was a malloc() (or calloc() or similar)
// AND Setallocator_shim::internal::CallNewHandlerOnMallocFailure(true).
// - If the std::new_handler is NOT set just return nullptr.
// - If the std::new_handler is set:
// - Assume it will abort() if it fails (very likely the new_handler will
// just suicide printing a message).
// - Assume it did succeed if it returns, in which case reattempt the alloc.
PA_ALWAYS_INLINE void* ShimCppNew(size_t size) {
const allocator_shim::AllocatorDispatch* const chain_head =
allocator_shim::internal::GetChainHead();
void* context = nullptr;
#if PA_BUILDFLAG(IS_APPLE) && !PA_BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
context = malloc_default_zone();
#endif
// `[[unlikely]]` is _not_ effective when used in the form of
// `do [[unlikely]] { ... } while (expr);`, so we use the following form
// instead.
void* ptr = chain_head->alloc_function(size, context);
while (!ptr && allocator_shim::internal::CallNewHandler(size)) [[unlikely]] {
ptr = chain_head->alloc_function(size, context);
}
return ptr;
}
PA_ALWAYS_INLINE void* ShimCppNewNoThrow(size_t size) {
const allocator_shim::AllocatorDispatch* const chain_head =
allocator_shim::internal::GetChainHead();
void* context = nullptr;
#if PA_BUILDFLAG(IS_APPLE) && !PA_BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
context = malloc_default_zone();
#endif
return chain_head->alloc_unchecked_function(size, context);
}
PA_ALWAYS_INLINE void* ShimCppAlignedNew(size_t size, size_t alignment) {
const allocator_shim::AllocatorDispatch* const chain_head =
allocator_shim::internal::GetChainHead();
void* context = nullptr;
#if PA_BUILDFLAG(IS_APPLE) && !PA_BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
context = malloc_default_zone();
#endif
void* ptr = chain_head->alloc_aligned_function(alignment, size, context);
while (!ptr && allocator_shim::internal::CallNewHandler(size)) [[unlikely]] {
ptr = chain_head->alloc_aligned_function(alignment, size, context);
}
return ptr;
}
PA_ALWAYS_INLINE void ShimCppDelete(void* address) {
const allocator_shim::AllocatorDispatch* const chain_head =
allocator_shim::internal::GetChainHead();
void* context = nullptr;
#if PA_BUILDFLAG(IS_APPLE) && !PA_BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
context = malloc_default_zone();
#endif
return chain_head->free_function(address, context);
}
PA_ALWAYS_INLINE void* ShimMalloc(size_t size, void* context) {
const allocator_shim::AllocatorDispatch* const chain_head =
allocator_shim::internal::GetChainHead();
void* ptr = chain_head->alloc_function(size, context);
while (!ptr &&
allocator_shim::internal::g_call_new_handler_on_malloc_failure &&
allocator_shim::internal::CallNewHandler(size)) [[unlikely]] {
ptr = chain_head->alloc_function(size, context);
}
return ptr;
}
PA_ALWAYS_INLINE void* ShimCalloc(size_t n, size_t size, void* context) {
const allocator_shim::AllocatorDispatch* const chain_head =
allocator_shim::internal::GetChainHead();
void* ptr = chain_head->alloc_zero_initialized_function(n, size, context);
while (!ptr &&
allocator_shim::internal::g_call_new_handler_on_malloc_failure &&
allocator_shim::internal::CallNewHandler(size)) [[unlikely]] {
ptr = chain_head->alloc_zero_initialized_function(n, size, context);
}
return ptr;
}
PA_ALWAYS_INLINE void* ShimRealloc(void* address, size_t size, void* context) {
const allocator_shim::AllocatorDispatch* const chain_head =
allocator_shim::internal::GetChainHead();
void* ptr = chain_head->realloc_function(address, size, context);
// realloc(size == 0) means free() and might return a nullptr. We should
// not call the std::new_handler in that case, though.
while (!ptr && size != 0 &&
allocator_shim::internal::g_call_new_handler_on_malloc_failure &&
allocator_shim::internal::CallNewHandler(size)) [[unlikely]] {
ptr = chain_head->realloc_function(address, size, context);
}
return ptr;
}
PA_ALWAYS_INLINE void* ShimMemalign(size_t alignment,
size_t size,
void* context) {
const allocator_shim::AllocatorDispatch* const chain_head =
allocator_shim::internal::GetChainHead();
void* ptr = chain_head->alloc_aligned_function(alignment, size, context);
while (!ptr &&
allocator_shim::internal::g_call_new_handler_on_malloc_failure &&
allocator_shim::internal::CallNewHandler(size)) [[unlikely]] {
ptr = chain_head->alloc_aligned_function(alignment, size, context);
}
return ptr;
}
PA_ALWAYS_INLINE int ShimPosixMemalign(void** res,
size_t alignment,
size_t size) {
// posix_memalign is supposed to check the arguments. See tc_posix_memalign()
// in tc_malloc.cc.
if (((alignment % sizeof(void*)) != 0) ||
!partition_alloc::internal::base::bits::HasSingleBit(alignment))
[[unlikely]] {
return EINVAL;
}
void* ptr = ShimMemalign(alignment, size, nullptr);
*res = ptr;
if (ptr) [[likely]] {
return 0;
}
return ENOMEM;
}
PA_ALWAYS_INLINE void* ShimValloc(size_t size, void* context) {
return ShimMemalign(GetCachedPageSize(), size, context);
}
PA_ALWAYS_INLINE void* ShimPvalloc(size_t size) {
// pvalloc(0) should allocate one page, according to its man page.
size_t page_size = GetCachedPageSize();
if (size == 0) [[unlikely]] {
size = page_size;
} else {
size = partition_alloc::internal::base::bits::AlignUp(size, page_size);
}
// The third argument is nullptr because pvalloc is glibc only and does not
// exist on OSX/BSD systems.
return ShimMemalign(page_size, size, nullptr);
}
PA_ALWAYS_INLINE void ShimFree(void* address, void* context) {
const allocator_shim::AllocatorDispatch* const chain_head =
allocator_shim::internal::GetChainHead();
return chain_head->free_function(address, context);
}
PA_ALWAYS_INLINE size_t ShimGetSizeEstimate(const void* address,
void* context) {
const allocator_shim::AllocatorDispatch* const chain_head =
allocator_shim::internal::GetChainHead();
return chain_head->get_size_estimate_function(const_cast<void*>(address),
context);
}
PA_ALWAYS_INLINE size_t ShimGoodSize(size_t size, void* context) {
const allocator_shim::AllocatorDispatch* const chain_head =
allocator_shim::internal::GetChainHead();
return chain_head->good_size_function(size, context);
}
PA_ALWAYS_INLINE bool ShimClaimedAddress(void* address, void* context) {
const allocator_shim::AllocatorDispatch* const chain_head =
allocator_shim::internal::GetChainHead();
return chain_head->claimed_address_function(address, context);
}
PA_ALWAYS_INLINE unsigned ShimBatchMalloc(size_t size,
void** results,
unsigned num_requested,
void* context) {
const allocator_shim::AllocatorDispatch* const chain_head =
allocator_shim::internal::GetChainHead();
return chain_head->batch_malloc_function(size, results, num_requested,
context);
}
PA_ALWAYS_INLINE void ShimBatchFree(void** to_be_freed,
unsigned num_to_be_freed,
void* context) {
const allocator_shim::AllocatorDispatch* const chain_head =
allocator_shim::internal::GetChainHead();
return chain_head->batch_free_function(to_be_freed, num_to_be_freed, context);
}
PA_ALWAYS_INLINE void ShimFreeDefiniteSize(void* ptr,
size_t size,
void* context) {
const allocator_shim::AllocatorDispatch* const chain_head =
allocator_shim::internal::GetChainHead();
return chain_head->free_definite_size_function(ptr, size, context);
}
PA_ALWAYS_INLINE void ShimTryFreeDefault(void* ptr, void* context) {
const allocator_shim::AllocatorDispatch* const chain_head =
allocator_shim::internal::GetChainHead();
return chain_head->try_free_default_function(ptr, context);
}
PA_ALWAYS_INLINE void* ShimAlignedMalloc(size_t size,
size_t alignment,
void* context) {
const allocator_shim::AllocatorDispatch* const chain_head =
allocator_shim::internal::GetChainHead();
void* ptr = chain_head->aligned_malloc_function(size, alignment, context);
while (!ptr &&
allocator_shim::internal::g_call_new_handler_on_malloc_failure &&
allocator_shim::internal::CallNewHandler(size)) [[unlikely]] {
ptr = chain_head->aligned_malloc_function(size, alignment, context);
}
return ptr;
}
PA_ALWAYS_INLINE void* ShimAlignedRealloc(void* address,
size_t size,
size_t alignment,
void* context) {
const allocator_shim::AllocatorDispatch* const chain_head =
allocator_shim::internal::GetChainHead();
void* ptr =
chain_head->aligned_realloc_function(address, size, alignment, context);
// _aligned_realloc(size == 0) means _aligned_free() and might return a
// nullptr. We should not call the std::new_handler in that case, though.
while (!ptr && size != 0 &&
allocator_shim::internal::g_call_new_handler_on_malloc_failure &&
allocator_shim::internal::CallNewHandler(size)) [[unlikely]] {
ptr =
chain_head->aligned_realloc_function(address, size, alignment, context);
}
return ptr;
}
PA_ALWAYS_INLINE void ShimAlignedFree(void* address, void* context) {
const allocator_shim::AllocatorDispatch* const chain_head =
allocator_shim::internal::GetChainHead();
return chain_head->aligned_free_function(address, context);
}
#undef PA_ALWAYS_INLINE
} // extern "C"
#endif // PARTITION_ALLOC_SHIM_SHIM_ALLOC_FUNCTIONS_H_