1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker * Copyright (C) 2012 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker *
4*8d67ca89SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*8d67ca89SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*8d67ca89SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*8d67ca89SAndroid Build Coastguard Worker *
8*8d67ca89SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*8d67ca89SAndroid Build Coastguard Worker *
10*8d67ca89SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*8d67ca89SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*8d67ca89SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8d67ca89SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*8d67ca89SAndroid Build Coastguard Worker * limitations under the License.
15*8d67ca89SAndroid Build Coastguard Worker */
16*8d67ca89SAndroid Build Coastguard Worker
17*8d67ca89SAndroid Build Coastguard Worker #pragma once
18*8d67ca89SAndroid Build Coastguard Worker
19*8d67ca89SAndroid Build Coastguard Worker /**
20*8d67ca89SAndroid Build Coastguard Worker * @file malloc.h
21*8d67ca89SAndroid Build Coastguard Worker * @brief Heap memory allocation.
22*8d67ca89SAndroid Build Coastguard Worker *
23*8d67ca89SAndroid Build Coastguard Worker * [Debugging Native Memory Use](https://source.android.com/devices/tech/debug/native-memory)
24*8d67ca89SAndroid Build Coastguard Worker * is the canonical source for documentation on Android's heap debugging
25*8d67ca89SAndroid Build Coastguard Worker * features.
26*8d67ca89SAndroid Build Coastguard Worker */
27*8d67ca89SAndroid Build Coastguard Worker
28*8d67ca89SAndroid Build Coastguard Worker #include <sys/cdefs.h>
29*8d67ca89SAndroid Build Coastguard Worker #include <stddef.h>
30*8d67ca89SAndroid Build Coastguard Worker #include <stdio.h>
31*8d67ca89SAndroid Build Coastguard Worker
32*8d67ca89SAndroid Build Coastguard Worker __BEGIN_DECLS
33*8d67ca89SAndroid Build Coastguard Worker
34*8d67ca89SAndroid Build Coastguard Worker #define __BIONIC_ALLOC_SIZE(...) __attribute__((__alloc_size__(__VA_ARGS__)))
35*8d67ca89SAndroid Build Coastguard Worker
36*8d67ca89SAndroid Build Coastguard Worker /**
37*8d67ca89SAndroid Build Coastguard Worker * [malloc(3)](https://man7.org/linux/man-pages/man3/malloc.3.html) allocates
38*8d67ca89SAndroid Build Coastguard Worker * memory on the heap.
39*8d67ca89SAndroid Build Coastguard Worker *
40*8d67ca89SAndroid Build Coastguard Worker * Returns a pointer to the allocated memory on success and returns a null
41*8d67ca89SAndroid Build Coastguard Worker * pointer and sets `errno` on failure.
42*8d67ca89SAndroid Build Coastguard Worker *
43*8d67ca89SAndroid Build Coastguard Worker * Note that Android (like most Unix systems) allows "overcommit". This
44*8d67ca89SAndroid Build Coastguard Worker * allows processes to allocate more memory than the system has, provided
45*8d67ca89SAndroid Build Coastguard Worker * they don't use it all. This works because only "dirty" pages that have
46*8d67ca89SAndroid Build Coastguard Worker * been written to actually require physical memory. In practice, this
47*8d67ca89SAndroid Build Coastguard Worker * means that it's rare to see memory allocation functions return a null
48*8d67ca89SAndroid Build Coastguard Worker * pointer, and that a non-null pointer does not mean that you actually
49*8d67ca89SAndroid Build Coastguard Worker * have all of the memory you asked for.
50*8d67ca89SAndroid Build Coastguard Worker *
51*8d67ca89SAndroid Build Coastguard Worker * Note also that the Linux Out Of Memory (OOM) killer behaves differently
52*8d67ca89SAndroid Build Coastguard Worker * for code run via `adb shell`. The assumption is that if you ran
53*8d67ca89SAndroid Build Coastguard Worker * something via `adb shell` you're a developer who actually wants the
54*8d67ca89SAndroid Build Coastguard Worker * device to do what you're asking it to do _even if_ that means killing
55*8d67ca89SAndroid Build Coastguard Worker * other processes. Obviously this is not the case for apps, which will
56*8d67ca89SAndroid Build Coastguard Worker * be killed in preference to killing other processes.
57*8d67ca89SAndroid Build Coastguard Worker */
58*8d67ca89SAndroid Build Coastguard Worker __nodiscard void* _Nullable malloc(size_t __byte_count) __mallocfunc __BIONIC_ALLOC_SIZE(1);
59*8d67ca89SAndroid Build Coastguard Worker
60*8d67ca89SAndroid Build Coastguard Worker /**
61*8d67ca89SAndroid Build Coastguard Worker * [calloc(3)](https://man7.org/linux/man-pages/man3/calloc.3.html) allocates
62*8d67ca89SAndroid Build Coastguard Worker * and clears memory on the heap.
63*8d67ca89SAndroid Build Coastguard Worker *
64*8d67ca89SAndroid Build Coastguard Worker * Returns a pointer to the allocated memory on success and returns a null
65*8d67ca89SAndroid Build Coastguard Worker * pointer and sets `errno` on failure (but see the notes for malloc()).
66*8d67ca89SAndroid Build Coastguard Worker */
67*8d67ca89SAndroid Build Coastguard Worker __nodiscard void* _Nullable calloc(size_t __item_count, size_t __item_size) __mallocfunc __BIONIC_ALLOC_SIZE(1,2);
68*8d67ca89SAndroid Build Coastguard Worker
69*8d67ca89SAndroid Build Coastguard Worker /**
70*8d67ca89SAndroid Build Coastguard Worker * [realloc(3)](https://man7.org/linux/man-pages/man3/realloc.3.html) resizes
71*8d67ca89SAndroid Build Coastguard Worker * allocated memory on the heap.
72*8d67ca89SAndroid Build Coastguard Worker *
73*8d67ca89SAndroid Build Coastguard Worker * Returns a pointer (which may be different from `__ptr`) to the resized
74*8d67ca89SAndroid Build Coastguard Worker * memory on success and returns a null pointer and sets `errno` on failure
75*8d67ca89SAndroid Build Coastguard Worker * (but see the notes for malloc()).
76*8d67ca89SAndroid Build Coastguard Worker */
77*8d67ca89SAndroid Build Coastguard Worker __nodiscard void* _Nullable realloc(void* _Nullable __ptr, size_t __byte_count) __BIONIC_ALLOC_SIZE(2);
78*8d67ca89SAndroid Build Coastguard Worker
79*8d67ca89SAndroid Build Coastguard Worker /**
80*8d67ca89SAndroid Build Coastguard Worker * [reallocarray(3)](https://man7.org/linux/man-pages/man3/realloc.3.html) resizes
81*8d67ca89SAndroid Build Coastguard Worker * allocated memory on the heap.
82*8d67ca89SAndroid Build Coastguard Worker *
83*8d67ca89SAndroid Build Coastguard Worker * Equivalent to `realloc(__ptr, __item_count * __item_size)` but fails if the
84*8d67ca89SAndroid Build Coastguard Worker * multiplication overflows.
85*8d67ca89SAndroid Build Coastguard Worker *
86*8d67ca89SAndroid Build Coastguard Worker * Returns a pointer (which may be different from `__ptr`) to the resized
87*8d67ca89SAndroid Build Coastguard Worker * memory on success and returns a null pointer and sets `errno` on failure
88*8d67ca89SAndroid Build Coastguard Worker * (but see the notes for malloc()).
89*8d67ca89SAndroid Build Coastguard Worker */
90*8d67ca89SAndroid Build Coastguard Worker #if __ANDROID_API__ >= 29
91*8d67ca89SAndroid Build Coastguard Worker __nodiscard void* _Nullable reallocarray(void* _Nullable __ptr, size_t __item_count, size_t __item_size) __BIONIC_ALLOC_SIZE(2, 3) __INTRODUCED_IN(29);
92*8d67ca89SAndroid Build Coastguard Worker #elif defined(__ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__)
93*8d67ca89SAndroid Build Coastguard Worker #include <errno.h>
reallocarray(void * _Nullable __ptr,size_t __item_count,size_t __item_size)94*8d67ca89SAndroid Build Coastguard Worker static __inline __nodiscard void* _Nullable reallocarray(void* _Nullable __ptr, size_t __item_count, size_t __item_size) __BIONIC_ALLOC_SIZE(2, 3) {
95*8d67ca89SAndroid Build Coastguard Worker size_t __new_size;
96*8d67ca89SAndroid Build Coastguard Worker if (__builtin_mul_overflow(__item_count, __item_size, &__new_size)) {
97*8d67ca89SAndroid Build Coastguard Worker errno = ENOMEM;
98*8d67ca89SAndroid Build Coastguard Worker return NULL;
99*8d67ca89SAndroid Build Coastguard Worker }
100*8d67ca89SAndroid Build Coastguard Worker return realloc(__ptr, __new_size);
101*8d67ca89SAndroid Build Coastguard Worker }
102*8d67ca89SAndroid Build Coastguard Worker #endif
103*8d67ca89SAndroid Build Coastguard Worker
104*8d67ca89SAndroid Build Coastguard Worker /**
105*8d67ca89SAndroid Build Coastguard Worker * [free(3)](https://man7.org/linux/man-pages/man3/free.3.html) deallocates
106*8d67ca89SAndroid Build Coastguard Worker * memory on the heap.
107*8d67ca89SAndroid Build Coastguard Worker */
108*8d67ca89SAndroid Build Coastguard Worker void free(void* _Nullable __ptr);
109*8d67ca89SAndroid Build Coastguard Worker
110*8d67ca89SAndroid Build Coastguard Worker /**
111*8d67ca89SAndroid Build Coastguard Worker * [memalign(3)](https://man7.org/linux/man-pages/man3/memalign.3.html) allocates
112*8d67ca89SAndroid Build Coastguard Worker * memory on the heap with the required alignment.
113*8d67ca89SAndroid Build Coastguard Worker *
114*8d67ca89SAndroid Build Coastguard Worker * Returns a pointer to the allocated memory on success and returns a null
115*8d67ca89SAndroid Build Coastguard Worker * pointer and sets `errno` on failure (but see the notes for malloc()).
116*8d67ca89SAndroid Build Coastguard Worker *
117*8d67ca89SAndroid Build Coastguard Worker * See also posix_memalign().
118*8d67ca89SAndroid Build Coastguard Worker */
119*8d67ca89SAndroid Build Coastguard Worker __nodiscard void* _Nullable memalign(size_t __alignment, size_t __byte_count) __mallocfunc __BIONIC_ALLOC_SIZE(2);
120*8d67ca89SAndroid Build Coastguard Worker
121*8d67ca89SAndroid Build Coastguard Worker /**
122*8d67ca89SAndroid Build Coastguard Worker * [malloc_usable_size(3)](https://man7.org/linux/man-pages/man3/malloc_usable_size.3.html)
123*8d67ca89SAndroid Build Coastguard Worker * returns the actual size of the given heap block.
124*8d67ca89SAndroid Build Coastguard Worker */
125*8d67ca89SAndroid Build Coastguard Worker __nodiscard size_t malloc_usable_size(const void* _Nullable __ptr);
126*8d67ca89SAndroid Build Coastguard Worker
127*8d67ca89SAndroid Build Coastguard Worker #define __MALLINFO_BODY \
128*8d67ca89SAndroid Build Coastguard Worker /** Total number of non-mmapped bytes currently allocated from OS. */ \
129*8d67ca89SAndroid Build Coastguard Worker size_t arena; \
130*8d67ca89SAndroid Build Coastguard Worker /** Number of free chunks. */ \
131*8d67ca89SAndroid Build Coastguard Worker size_t ordblks; \
132*8d67ca89SAndroid Build Coastguard Worker /** (Unused.) */ \
133*8d67ca89SAndroid Build Coastguard Worker size_t smblks; \
134*8d67ca89SAndroid Build Coastguard Worker /** (Unused.) */ \
135*8d67ca89SAndroid Build Coastguard Worker size_t hblks; \
136*8d67ca89SAndroid Build Coastguard Worker /** Total number of bytes in mmapped regions. */ \
137*8d67ca89SAndroid Build Coastguard Worker size_t hblkhd; \
138*8d67ca89SAndroid Build Coastguard Worker /** Maximum total allocated space; greater than total if trimming has occurred. */ \
139*8d67ca89SAndroid Build Coastguard Worker size_t usmblks; \
140*8d67ca89SAndroid Build Coastguard Worker /** (Unused.) */ \
141*8d67ca89SAndroid Build Coastguard Worker size_t fsmblks; \
142*8d67ca89SAndroid Build Coastguard Worker /** Total allocated space (normal or mmapped.) */ \
143*8d67ca89SAndroid Build Coastguard Worker size_t uordblks; \
144*8d67ca89SAndroid Build Coastguard Worker /** Total free space. */ \
145*8d67ca89SAndroid Build Coastguard Worker size_t fordblks; \
146*8d67ca89SAndroid Build Coastguard Worker /** Upper bound on number of bytes releasable by a trim operation. */ \
147*8d67ca89SAndroid Build Coastguard Worker size_t keepcost;
148*8d67ca89SAndroid Build Coastguard Worker
149*8d67ca89SAndroid Build Coastguard Worker #ifndef STRUCT_MALLINFO_DECLARED
150*8d67ca89SAndroid Build Coastguard Worker #define STRUCT_MALLINFO_DECLARED 1
151*8d67ca89SAndroid Build Coastguard Worker struct mallinfo { __MALLINFO_BODY };
152*8d67ca89SAndroid Build Coastguard Worker #endif
153*8d67ca89SAndroid Build Coastguard Worker
154*8d67ca89SAndroid Build Coastguard Worker /**
155*8d67ca89SAndroid Build Coastguard Worker * [mallinfo(3)](https://man7.org/linux/man-pages/man3/mallinfo.3.html) returns
156*8d67ca89SAndroid Build Coastguard Worker * information about the current state of the heap. Note that mallinfo() is
157*8d67ca89SAndroid Build Coastguard Worker * inherently unreliable and consider using malloc_info() instead.
158*8d67ca89SAndroid Build Coastguard Worker */
159*8d67ca89SAndroid Build Coastguard Worker struct mallinfo mallinfo(void);
160*8d67ca89SAndroid Build Coastguard Worker
161*8d67ca89SAndroid Build Coastguard Worker /**
162*8d67ca89SAndroid Build Coastguard Worker * On Android the struct mallinfo and struct mallinfo2 are the same.
163*8d67ca89SAndroid Build Coastguard Worker */
164*8d67ca89SAndroid Build Coastguard Worker struct mallinfo2 { __MALLINFO_BODY };
165*8d67ca89SAndroid Build Coastguard Worker
166*8d67ca89SAndroid Build Coastguard Worker /**
167*8d67ca89SAndroid Build Coastguard Worker * [mallinfo2(3)](https://man7.org/linux/man-pages/man3/mallinfo2.3.html) returns
168*8d67ca89SAndroid Build Coastguard Worker * information about the current state of the heap. Note that mallinfo2() is
169*8d67ca89SAndroid Build Coastguard Worker * inherently unreliable and consider using malloc_info() instead.
170*8d67ca89SAndroid Build Coastguard Worker */
171*8d67ca89SAndroid Build Coastguard Worker struct mallinfo2 mallinfo2(void) __RENAME(mallinfo);
172*8d67ca89SAndroid Build Coastguard Worker
173*8d67ca89SAndroid Build Coastguard Worker /**
174*8d67ca89SAndroid Build Coastguard Worker * [malloc_info(3)](https://man7.org/linux/man-pages/man3/malloc_info.3.html)
175*8d67ca89SAndroid Build Coastguard Worker * writes information about the current state of the heap to the given stream.
176*8d67ca89SAndroid Build Coastguard Worker *
177*8d67ca89SAndroid Build Coastguard Worker * The XML structure for malloc_info() is as follows:
178*8d67ca89SAndroid Build Coastguard Worker * ```
179*8d67ca89SAndroid Build Coastguard Worker * <malloc version="jemalloc-1">
180*8d67ca89SAndroid Build Coastguard Worker * <heap nr="INT">
181*8d67ca89SAndroid Build Coastguard Worker * <allocated-large>INT</allocated-large>
182*8d67ca89SAndroid Build Coastguard Worker * <allocated-huge>INT</allocated-huge>
183*8d67ca89SAndroid Build Coastguard Worker * <allocated-bins>INT</allocated-bins>
184*8d67ca89SAndroid Build Coastguard Worker * <bins-total>INT</bins-total>
185*8d67ca89SAndroid Build Coastguard Worker * <bin nr="INT">
186*8d67ca89SAndroid Build Coastguard Worker * <allocated>INT</allocated>
187*8d67ca89SAndroid Build Coastguard Worker * <nmalloc>INT</nmalloc>
188*8d67ca89SAndroid Build Coastguard Worker * <ndalloc>INT</ndalloc>
189*8d67ca89SAndroid Build Coastguard Worker * </bin>
190*8d67ca89SAndroid Build Coastguard Worker * <!-- more bins -->
191*8d67ca89SAndroid Build Coastguard Worker * </heap>
192*8d67ca89SAndroid Build Coastguard Worker * <!-- more heaps -->
193*8d67ca89SAndroid Build Coastguard Worker * </malloc>
194*8d67ca89SAndroid Build Coastguard Worker * ```
195*8d67ca89SAndroid Build Coastguard Worker *
196*8d67ca89SAndroid Build Coastguard Worker * Available since API level 23.
197*8d67ca89SAndroid Build Coastguard Worker */
198*8d67ca89SAndroid Build Coastguard Worker
199*8d67ca89SAndroid Build Coastguard Worker #if __BIONIC_AVAILABILITY_GUARD(23)
200*8d67ca89SAndroid Build Coastguard Worker int malloc_info(int __must_be_zero, FILE* _Nonnull __fp) __INTRODUCED_IN(23);
201*8d67ca89SAndroid Build Coastguard Worker #endif /* __BIONIC_AVAILABILITY_GUARD(23) */
202*8d67ca89SAndroid Build Coastguard Worker
203*8d67ca89SAndroid Build Coastguard Worker
204*8d67ca89SAndroid Build Coastguard Worker /**
205*8d67ca89SAndroid Build Coastguard Worker * mallopt() option to set the decay time. Valid values are -1, 0 and 1.
206*8d67ca89SAndroid Build Coastguard Worker * -1 : Disable the releasing of unused pages. This value is available since
207*8d67ca89SAndroid Build Coastguard Worker * API level 35.
208*8d67ca89SAndroid Build Coastguard Worker * 0 : Release the unused pages immediately.
209*8d67ca89SAndroid Build Coastguard Worker * 1 : Release the unused pages at a device-specific interval.
210*8d67ca89SAndroid Build Coastguard Worker *
211*8d67ca89SAndroid Build Coastguard Worker * Available since API level 27.
212*8d67ca89SAndroid Build Coastguard Worker */
213*8d67ca89SAndroid Build Coastguard Worker #define M_DECAY_TIME (-100)
214*8d67ca89SAndroid Build Coastguard Worker /**
215*8d67ca89SAndroid Build Coastguard Worker * mallopt() option to immediately purge any memory not in use. This
216*8d67ca89SAndroid Build Coastguard Worker * will release the memory back to the kernel. The value is ignored.
217*8d67ca89SAndroid Build Coastguard Worker *
218*8d67ca89SAndroid Build Coastguard Worker * Available since API level 28.
219*8d67ca89SAndroid Build Coastguard Worker */
220*8d67ca89SAndroid Build Coastguard Worker #define M_PURGE (-101)
221*8d67ca89SAndroid Build Coastguard Worker /**
222*8d67ca89SAndroid Build Coastguard Worker * mallopt() option to immediately purge all possible memory back to
223*8d67ca89SAndroid Build Coastguard Worker * the kernel. This call can take longer than a normal purge since it
224*8d67ca89SAndroid Build Coastguard Worker * examines everything. In some cases, it can take more than twice the
225*8d67ca89SAndroid Build Coastguard Worker * time of a M_PURGE call. The value is ignored.
226*8d67ca89SAndroid Build Coastguard Worker *
227*8d67ca89SAndroid Build Coastguard Worker * Available since API level 34.
228*8d67ca89SAndroid Build Coastguard Worker */
229*8d67ca89SAndroid Build Coastguard Worker #define M_PURGE_ALL (-104)
230*8d67ca89SAndroid Build Coastguard Worker
231*8d67ca89SAndroid Build Coastguard Worker /**
232*8d67ca89SAndroid Build Coastguard Worker * mallopt() option to tune the allocator's choice of memory tags to
233*8d67ca89SAndroid Build Coastguard Worker * make it more likely that a certain class of memory errors will be
234*8d67ca89SAndroid Build Coastguard Worker * detected. This is only relevant if MTE is enabled in this process
235*8d67ca89SAndroid Build Coastguard Worker * and ignored otherwise. The value argument should be one of the
236*8d67ca89SAndroid Build Coastguard Worker * M_MEMTAG_TUNING_* flags.
237*8d67ca89SAndroid Build Coastguard Worker * NOTE: This is only available in scudo.
238*8d67ca89SAndroid Build Coastguard Worker *
239*8d67ca89SAndroid Build Coastguard Worker * Available since API level 31.
240*8d67ca89SAndroid Build Coastguard Worker */
241*8d67ca89SAndroid Build Coastguard Worker #define M_MEMTAG_TUNING (-102)
242*8d67ca89SAndroid Build Coastguard Worker
243*8d67ca89SAndroid Build Coastguard Worker /**
244*8d67ca89SAndroid Build Coastguard Worker * When passed as a value of M_MEMTAG_TUNING mallopt() call, enables
245*8d67ca89SAndroid Build Coastguard Worker * deterministic detection of linear buffer overflow and underflow
246*8d67ca89SAndroid Build Coastguard Worker * bugs by assigning distinct tag values to adjacent allocations. This
247*8d67ca89SAndroid Build Coastguard Worker * mode has a slightly reduced chance to detect use-after-free bugs
248*8d67ca89SAndroid Build Coastguard Worker * because only half of the possible tag values are available for each
249*8d67ca89SAndroid Build Coastguard Worker * memory location.
250*8d67ca89SAndroid Build Coastguard Worker *
251*8d67ca89SAndroid Build Coastguard Worker * Please keep in mind that MTE can not detect overflow within the
252*8d67ca89SAndroid Build Coastguard Worker * same tag granule (16-byte aligned chunk), and can miss small
253*8d67ca89SAndroid Build Coastguard Worker * overflows even in this mode. Such overflow can not be the cause of
254*8d67ca89SAndroid Build Coastguard Worker * a memory corruption, because the memory within one granule is never
255*8d67ca89SAndroid Build Coastguard Worker * used for multiple allocations.
256*8d67ca89SAndroid Build Coastguard Worker */
257*8d67ca89SAndroid Build Coastguard Worker #define M_MEMTAG_TUNING_BUFFER_OVERFLOW 0
258*8d67ca89SAndroid Build Coastguard Worker
259*8d67ca89SAndroid Build Coastguard Worker /**
260*8d67ca89SAndroid Build Coastguard Worker * When passed as a value of M_MEMTAG_TUNING mallopt() call, enables
261*8d67ca89SAndroid Build Coastguard Worker * independently randomized tags for uniform ~93% probability of
262*8d67ca89SAndroid Build Coastguard Worker * detecting both spatial (buffer overflow) and temporal (use after
263*8d67ca89SAndroid Build Coastguard Worker * free) bugs.
264*8d67ca89SAndroid Build Coastguard Worker */
265*8d67ca89SAndroid Build Coastguard Worker #define M_MEMTAG_TUNING_UAF 1
266*8d67ca89SAndroid Build Coastguard Worker
267*8d67ca89SAndroid Build Coastguard Worker /**
268*8d67ca89SAndroid Build Coastguard Worker * mallopt() option for per-thread memory initialization tuning.
269*8d67ca89SAndroid Build Coastguard Worker * The value argument should be one of:
270*8d67ca89SAndroid Build Coastguard Worker * 1: Disable automatic heap initialization on this thread only.
271*8d67ca89SAndroid Build Coastguard Worker * If memory tagging is enabled, disable as much as possible of the
272*8d67ca89SAndroid Build Coastguard Worker * memory tagging initialization for this thread.
273*8d67ca89SAndroid Build Coastguard Worker * 0: Normal behavior.
274*8d67ca89SAndroid Build Coastguard Worker *
275*8d67ca89SAndroid Build Coastguard Worker * Available since API level 31.
276*8d67ca89SAndroid Build Coastguard Worker */
277*8d67ca89SAndroid Build Coastguard Worker #define M_THREAD_DISABLE_MEM_INIT (-103)
278*8d67ca89SAndroid Build Coastguard Worker /**
279*8d67ca89SAndroid Build Coastguard Worker * mallopt() option to set the maximum number of items in the secondary
280*8d67ca89SAndroid Build Coastguard Worker * cache of the scudo allocator.
281*8d67ca89SAndroid Build Coastguard Worker *
282*8d67ca89SAndroid Build Coastguard Worker * Available since API level 31.
283*8d67ca89SAndroid Build Coastguard Worker */
284*8d67ca89SAndroid Build Coastguard Worker #define M_CACHE_COUNT_MAX (-200)
285*8d67ca89SAndroid Build Coastguard Worker /**
286*8d67ca89SAndroid Build Coastguard Worker * mallopt() option to set the maximum size in bytes of a cacheable item in
287*8d67ca89SAndroid Build Coastguard Worker * the secondary cache of the scudo allocator.
288*8d67ca89SAndroid Build Coastguard Worker *
289*8d67ca89SAndroid Build Coastguard Worker * Available since API level 31.
290*8d67ca89SAndroid Build Coastguard Worker */
291*8d67ca89SAndroid Build Coastguard Worker #define M_CACHE_SIZE_MAX (-201)
292*8d67ca89SAndroid Build Coastguard Worker /**
293*8d67ca89SAndroid Build Coastguard Worker * mallopt() option to increase the maximum number of shared thread-specific
294*8d67ca89SAndroid Build Coastguard Worker * data structures that can be created. This number cannot be decreased,
295*8d67ca89SAndroid Build Coastguard Worker * only increased and only applies to the scudo allocator.
296*8d67ca89SAndroid Build Coastguard Worker *
297*8d67ca89SAndroid Build Coastguard Worker * Available since API level 31.
298*8d67ca89SAndroid Build Coastguard Worker */
299*8d67ca89SAndroid Build Coastguard Worker #define M_TSDS_COUNT_MAX (-202)
300*8d67ca89SAndroid Build Coastguard Worker
301*8d67ca89SAndroid Build Coastguard Worker /**
302*8d67ca89SAndroid Build Coastguard Worker * mallopt() option to decide whether heap memory is zero-initialized on
303*8d67ca89SAndroid Build Coastguard Worker * allocation across the whole process. May be called at any time, including
304*8d67ca89SAndroid Build Coastguard Worker * when multiple threads are running. An argument of zero indicates memory
305*8d67ca89SAndroid Build Coastguard Worker * should not be zero-initialized, any other value indicates to initialize heap
306*8d67ca89SAndroid Build Coastguard Worker * memory to zero.
307*8d67ca89SAndroid Build Coastguard Worker *
308*8d67ca89SAndroid Build Coastguard Worker * Note that this memory mitigation is only implemented in scudo and therefore
309*8d67ca89SAndroid Build Coastguard Worker * this will have no effect when using another allocator (such as jemalloc on
310*8d67ca89SAndroid Build Coastguard Worker * Android Go devices).
311*8d67ca89SAndroid Build Coastguard Worker *
312*8d67ca89SAndroid Build Coastguard Worker * Available since API level 31.
313*8d67ca89SAndroid Build Coastguard Worker */
314*8d67ca89SAndroid Build Coastguard Worker #define M_BIONIC_ZERO_INIT (-203)
315*8d67ca89SAndroid Build Coastguard Worker
316*8d67ca89SAndroid Build Coastguard Worker /**
317*8d67ca89SAndroid Build Coastguard Worker * mallopt() option to change the heap tagging state. May be called at any
318*8d67ca89SAndroid Build Coastguard Worker * time, including when multiple threads are running.
319*8d67ca89SAndroid Build Coastguard Worker * The value must be one of the M_HEAP_TAGGING_LEVEL_ constants.
320*8d67ca89SAndroid Build Coastguard Worker * NOTE: This is only available in scudo.
321*8d67ca89SAndroid Build Coastguard Worker *
322*8d67ca89SAndroid Build Coastguard Worker * Available since API level 31.
323*8d67ca89SAndroid Build Coastguard Worker */
324*8d67ca89SAndroid Build Coastguard Worker #define M_BIONIC_SET_HEAP_TAGGING_LEVEL (-204)
325*8d67ca89SAndroid Build Coastguard Worker
326*8d67ca89SAndroid Build Coastguard Worker /**
327*8d67ca89SAndroid Build Coastguard Worker * Constants for use with the M_BIONIC_SET_HEAP_TAGGING_LEVEL mallopt() option.
328*8d67ca89SAndroid Build Coastguard Worker */
329*8d67ca89SAndroid Build Coastguard Worker enum HeapTaggingLevel {
330*8d67ca89SAndroid Build Coastguard Worker /**
331*8d67ca89SAndroid Build Coastguard Worker * Disable heap tagging and memory tag checks (if supported).
332*8d67ca89SAndroid Build Coastguard Worker * Heap tagging may not be re-enabled after being disabled.
333*8d67ca89SAndroid Build Coastguard Worker */
334*8d67ca89SAndroid Build Coastguard Worker M_HEAP_TAGGING_LEVEL_NONE = 0,
335*8d67ca89SAndroid Build Coastguard Worker #define M_HEAP_TAGGING_LEVEL_NONE M_HEAP_TAGGING_LEVEL_NONE
336*8d67ca89SAndroid Build Coastguard Worker /**
337*8d67ca89SAndroid Build Coastguard Worker * Address-only tagging. Heap pointers have a non-zero tag in the
338*8d67ca89SAndroid Build Coastguard Worker * most significant ("top") byte which is checked in free(). Memory
339*8d67ca89SAndroid Build Coastguard Worker * accesses ignore the tag using arm64's Top Byte Ignore (TBI) feature.
340*8d67ca89SAndroid Build Coastguard Worker */
341*8d67ca89SAndroid Build Coastguard Worker M_HEAP_TAGGING_LEVEL_TBI = 1,
342*8d67ca89SAndroid Build Coastguard Worker #define M_HEAP_TAGGING_LEVEL_TBI M_HEAP_TAGGING_LEVEL_TBI
343*8d67ca89SAndroid Build Coastguard Worker /**
344*8d67ca89SAndroid Build Coastguard Worker * Enable heap tagging and asynchronous memory tag checks (if supported).
345*8d67ca89SAndroid Build Coastguard Worker * Disable stack trace collection.
346*8d67ca89SAndroid Build Coastguard Worker */
347*8d67ca89SAndroid Build Coastguard Worker M_HEAP_TAGGING_LEVEL_ASYNC = 2,
348*8d67ca89SAndroid Build Coastguard Worker #define M_HEAP_TAGGING_LEVEL_ASYNC M_HEAP_TAGGING_LEVEL_ASYNC
349*8d67ca89SAndroid Build Coastguard Worker /**
350*8d67ca89SAndroid Build Coastguard Worker * Enable heap tagging and synchronous memory tag checks (if supported).
351*8d67ca89SAndroid Build Coastguard Worker * Enable stack trace collection.
352*8d67ca89SAndroid Build Coastguard Worker */
353*8d67ca89SAndroid Build Coastguard Worker M_HEAP_TAGGING_LEVEL_SYNC = 3,
354*8d67ca89SAndroid Build Coastguard Worker #define M_HEAP_TAGGING_LEVEL_SYNC M_HEAP_TAGGING_LEVEL_SYNC
355*8d67ca89SAndroid Build Coastguard Worker };
356*8d67ca89SAndroid Build Coastguard Worker
357*8d67ca89SAndroid Build Coastguard Worker /**
358*8d67ca89SAndroid Build Coastguard Worker * mallopt() option to print human readable statistics about the memory
359*8d67ca89SAndroid Build Coastguard Worker * allocator to the log. There is no format for this data, each allocator
360*8d67ca89SAndroid Build Coastguard Worker * can use a different format, and the data that is printed can
361*8d67ca89SAndroid Build Coastguard Worker * change at any time. This is expected to be used as a debugging aid.
362*8d67ca89SAndroid Build Coastguard Worker *
363*8d67ca89SAndroid Build Coastguard Worker * Available since API level 35.
364*8d67ca89SAndroid Build Coastguard Worker */
365*8d67ca89SAndroid Build Coastguard Worker #define M_LOG_STATS (-205)
366*8d67ca89SAndroid Build Coastguard Worker
367*8d67ca89SAndroid Build Coastguard Worker /**
368*8d67ca89SAndroid Build Coastguard Worker * [mallopt(3)](https://man7.org/linux/man-pages/man3/mallopt.3.html) modifies
369*8d67ca89SAndroid Build Coastguard Worker * heap behavior. Values of `__option` are the `M_` constants from this header.
370*8d67ca89SAndroid Build Coastguard Worker *
371*8d67ca89SAndroid Build Coastguard Worker * Returns 1 on success, 0 on error.
372*8d67ca89SAndroid Build Coastguard Worker *
373*8d67ca89SAndroid Build Coastguard Worker * Available since API level 26.
374*8d67ca89SAndroid Build Coastguard Worker */
375*8d67ca89SAndroid Build Coastguard Worker
376*8d67ca89SAndroid Build Coastguard Worker #if __BIONIC_AVAILABILITY_GUARD(26)
377*8d67ca89SAndroid Build Coastguard Worker int mallopt(int __option, int __value) __INTRODUCED_IN(26);
378*8d67ca89SAndroid Build Coastguard Worker #endif /* __BIONIC_AVAILABILITY_GUARD(26) */
379*8d67ca89SAndroid Build Coastguard Worker
380*8d67ca89SAndroid Build Coastguard Worker
381*8d67ca89SAndroid Build Coastguard Worker /**
382*8d67ca89SAndroid Build Coastguard Worker * [__malloc_hook(3)](https://man7.org/linux/man-pages/man3/__malloc_hook.3.html)
383*8d67ca89SAndroid Build Coastguard Worker * is called to implement malloc(). By default this points to the system's
384*8d67ca89SAndroid Build Coastguard Worker * implementation.
385*8d67ca89SAndroid Build Coastguard Worker *
386*8d67ca89SAndroid Build Coastguard Worker * Available since API level 28.
387*8d67ca89SAndroid Build Coastguard Worker *
388*8d67ca89SAndroid Build Coastguard Worker * See also: [extra documentation](https://android.googlesource.com/platform/bionic/+/main/libc/malloc_hooks/README.md)
389*8d67ca89SAndroid Build Coastguard Worker */
390*8d67ca89SAndroid Build Coastguard Worker
391*8d67ca89SAndroid Build Coastguard Worker #if __BIONIC_AVAILABILITY_GUARD(28)
392*8d67ca89SAndroid Build Coastguard Worker extern void* _Nonnull (*volatile _Nonnull __malloc_hook)(size_t __byte_count, const void* _Nonnull __caller) __INTRODUCED_IN(28);
393*8d67ca89SAndroid Build Coastguard Worker
394*8d67ca89SAndroid Build Coastguard Worker /**
395*8d67ca89SAndroid Build Coastguard Worker * [__realloc_hook(3)](https://man7.org/linux/man-pages/man3/__realloc_hook.3.html)
396*8d67ca89SAndroid Build Coastguard Worker * is called to implement realloc(). By default this points to the system's
397*8d67ca89SAndroid Build Coastguard Worker * implementation.
398*8d67ca89SAndroid Build Coastguard Worker *
399*8d67ca89SAndroid Build Coastguard Worker * Available since API level 28.
400*8d67ca89SAndroid Build Coastguard Worker *
401*8d67ca89SAndroid Build Coastguard Worker * See also: [extra documentation](https://android.googlesource.com/platform/bionic/+/main/libc/malloc_hooks/README.md)
402*8d67ca89SAndroid Build Coastguard Worker */
403*8d67ca89SAndroid Build Coastguard Worker extern void* _Nonnull (*volatile _Nonnull __realloc_hook)(void* _Nullable __ptr, size_t __byte_count, const void* _Nonnull __caller) __INTRODUCED_IN(28);
404*8d67ca89SAndroid Build Coastguard Worker
405*8d67ca89SAndroid Build Coastguard Worker /**
406*8d67ca89SAndroid Build Coastguard Worker * [__free_hook(3)](https://man7.org/linux/man-pages/man3/__free_hook.3.html)
407*8d67ca89SAndroid Build Coastguard Worker * is called to implement free(). By default this points to the system's
408*8d67ca89SAndroid Build Coastguard Worker * implementation.
409*8d67ca89SAndroid Build Coastguard Worker *
410*8d67ca89SAndroid Build Coastguard Worker * Available since API level 28.
411*8d67ca89SAndroid Build Coastguard Worker *
412*8d67ca89SAndroid Build Coastguard Worker * See also: [extra documentation](https://android.googlesource.com/platform/bionic/+/main/libc/malloc_hooks/README.md)
413*8d67ca89SAndroid Build Coastguard Worker */
414*8d67ca89SAndroid Build Coastguard Worker extern void (*volatile _Nonnull __free_hook)(void* _Nullable __ptr, const void* _Nonnull __caller) __INTRODUCED_IN(28);
415*8d67ca89SAndroid Build Coastguard Worker
416*8d67ca89SAndroid Build Coastguard Worker /**
417*8d67ca89SAndroid Build Coastguard Worker * [__memalign_hook(3)](https://man7.org/linux/man-pages/man3/__memalign_hook.3.html)
418*8d67ca89SAndroid Build Coastguard Worker * is called to implement memalign(). By default this points to the system's
419*8d67ca89SAndroid Build Coastguard Worker * implementation.
420*8d67ca89SAndroid Build Coastguard Worker *
421*8d67ca89SAndroid Build Coastguard Worker * Available since API level 28.
422*8d67ca89SAndroid Build Coastguard Worker *
423*8d67ca89SAndroid Build Coastguard Worker * See also: [extra documentation](https://android.googlesource.com/platform/bionic/+/main/libc/malloc_hooks/README.md)
424*8d67ca89SAndroid Build Coastguard Worker */
425*8d67ca89SAndroid Build Coastguard Worker extern void* _Nonnull (*volatile _Nonnull __memalign_hook)(size_t __alignment, size_t __byte_count, const void* _Nonnull __caller) __INTRODUCED_IN(28);
426*8d67ca89SAndroid Build Coastguard Worker #endif /* __BIONIC_AVAILABILITY_GUARD(28) */
427*8d67ca89SAndroid Build Coastguard Worker
428*8d67ca89SAndroid Build Coastguard Worker
429*8d67ca89SAndroid Build Coastguard Worker __END_DECLS
430