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