blob: c5d297b819e1b797cbc0cbef1e92a3628f81d758 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
Ian Rogers2c344d32012-08-28 15:53:10 -07002 * Copyright (C) 2012 The Android Open Source Project
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003 *
Ian Rogers2c344d32012-08-28 15:53:10 -07004 * 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
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08007 *
Ian Rogers2c344d32012-08-28 15:53:10 -07008 * 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.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080015 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080016
Elliott Hughes462e90c2018-08-21 16:10:48 -070017#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 */
Ian Rogers2c344d32012-08-28 15:53:10 -070027
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080028#include <sys/cdefs.h>
29#include <stddef.h>
Dan Albert4caa1f02014-08-20 09:16:57 -070030#include <stdio.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080031
32__BEGIN_DECLS
33
Elliott Hughes36a88e82016-07-30 09:58:15 -070034#define __BIONIC_ALLOC_SIZE(...) __attribute__((__alloc_size__(__VA_ARGS__)))
Elliott Hughes36a88e82016-07-30 09:58:15 -070035
Elliott Hughes462e90c2018-08-21 16:10:48 -070036/**
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 */
zijunzhao1a490582022-12-14 02:15:40 +000043void* _Nullable malloc(size_t __byte_count) __mallocfunc __BIONIC_ALLOC_SIZE(1) __wur;
Elliott Hughes462e90c2018-08-21 16:10:48 -070044
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 */
zijunzhao1a490582022-12-14 02:15:40 +000052void* _Nullable calloc(size_t __item_count, size_t __item_size) __mallocfunc __BIONIC_ALLOC_SIZE(1,2) __wur;
Elliott Hughes462e90c2018-08-21 16:10:48 -070053
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 */
zijunzhao1a490582022-12-14 02:15:40 +000061void* _Nullable realloc(void* _Nullable __ptr, size_t __byte_count) __BIONIC_ALLOC_SIZE(2) __wur;
Elliott Hughes462e90c2018-08-21 16:10:48 -070062
63/**
Elliott Hughesb1770852018-09-18 12:52:42 -070064 * [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 */
zijunzhao1a490582022-12-14 02:15:40 +000073void* _Nullable reallocarray(void* _Nullable __ptr, size_t __item_count, size_t __item_size) __BIONIC_ALLOC_SIZE(2, 3) __wur __INTRODUCED_IN(29);
Elliott Hughesb1770852018-09-18 12:52:42 -070074
75/**
Elliott Hughes462e90c2018-08-21 16:10:48 -070076 * [free(3)](http://man7.org/linux/man-pages/man3/free.3.html) deallocates
77 * memory on the heap.
78 */
zijunzhao1a490582022-12-14 02:15:40 +000079void free(void* _Nullable __ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080080
Elliott Hughes462e90c2018-08-21 16:10:48 -070081/**
82 * [memalign(3)](http://man7.org/linux/man-pages/man3/memalign.3.html) allocates
83 * memory on the heap with the required alignment.
84 *
85 * Returns a pointer to the allocated memory on success and returns a null
86 * pointer and sets `errno` on failure.
87 *
88 * See also posix_memalign().
89 */
zijunzhao1a490582022-12-14 02:15:40 +000090void* _Nullable memalign(size_t __alignment, size_t __byte_count) __mallocfunc __BIONIC_ALLOC_SIZE(2) __wur;
Elliott Hughes462e90c2018-08-21 16:10:48 -070091
92/**
93 * [malloc_usable_size(3)](http://man7.org/linux/man-pages/man3/malloc_usable_size.3.html)
94 * returns the actual size of the given heap block.
Elliott Hughes462e90c2018-08-21 16:10:48 -070095 */
Elliott Hughes655e4302023-06-16 12:39:33 -070096size_t malloc_usable_size(const void* _Nullable __ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080097
Christopher Ferris8248e622021-12-03 13:55:57 -080098#define __MALLINFO_BODY \
99 /** Total number of non-mmapped bytes currently allocated from OS. */ \
100 size_t arena; \
101 /** Number of free chunks. */ \
102 size_t ordblks; \
103 /** (Unused.) */ \
104 size_t smblks; \
105 /** (Unused.) */ \
106 size_t hblks; \
107 /** Total number of bytes in mmapped regions. */ \
108 size_t hblkhd; \
109 /** Maximum total allocated space; greater than total if trimming has occurred. */ \
110 size_t usmblks; \
111 /** (Unused.) */ \
112 size_t fsmblks; \
113 /** Total allocated space (normal or mmapped.) */ \
114 size_t uordblks; \
115 /** Total free space. */ \
116 size_t fordblks; \
117 /** Upper bound on number of bytes releasable by a trim operation. */ \
118 size_t keepcost;
119
Ian Rogers2c344d32012-08-28 15:53:10 -0700120#ifndef STRUCT_MALLINFO_DECLARED
121#define STRUCT_MALLINFO_DECLARED 1
Christopher Ferris8248e622021-12-03 13:55:57 -0800122struct mallinfo { __MALLINFO_BODY };
Elliott Hughes462e90c2018-08-21 16:10:48 -0700123#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800124
Elliott Hughes462e90c2018-08-21 16:10:48 -0700125/**
126 * [mallinfo(3)](http://man7.org/linux/man-pages/man3/mallinfo.3.html) returns
Elliott Hughes4fa9b8c2019-04-30 12:39:46 -0700127 * information about the current state of the heap. Note that mallinfo() is
128 * inherently unreliable and consider using malloc_info() instead.
Elliott Hughes462e90c2018-08-21 16:10:48 -0700129 */
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700130struct mallinfo mallinfo(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800131
Elliott Hughes462e90c2018-08-21 16:10:48 -0700132/**
Christopher Ferris8248e622021-12-03 13:55:57 -0800133 * On Android the struct mallinfo and struct mallinfo2 are the same.
134 */
135struct mallinfo2 { __MALLINFO_BODY };
136
137/**
138 * [mallinfo2(3)](http://man7.org/linux/man-pages/man3/mallinfo2.3.html) returns
139 * information about the current state of the heap. Note that mallinfo2() is
140 * inherently unreliable and consider using malloc_info() instead.
141 */
142struct mallinfo2 mallinfo2(void) __RENAME(mallinfo);
143
144/**
Elliott Hughes462e90c2018-08-21 16:10:48 -0700145 * [malloc_info(3)](http://man7.org/linux/man-pages/man3/malloc_info.3.html)
146 * writes information about the current state of the heap to the given stream.
Dan Albert4caa1f02014-08-20 09:16:57 -0700147 *
Elliott Hughes462e90c2018-08-21 16:10:48 -0700148 * The XML structure for malloc_info() is as follows:
149 * ```
Dan Albert4caa1f02014-08-20 09:16:57 -0700150 * <malloc version="jemalloc-1">
151 * <heap nr="INT">
152 * <allocated-large>INT</allocated-large>
153 * <allocated-huge>INT</allocated-huge>
154 * <allocated-bins>INT</allocated-bins>
155 * <bins-total>INT</bins-total>
156 * <bin nr="INT">
157 * <allocated>INT</allocated>
158 * <nmalloc>INT</nmalloc>
159 * <ndalloc>INT</ndalloc>
160 * </bin>
161 * <!-- more bins -->
162 * </heap>
163 * <!-- more heaps -->
164 * </malloc>
Elliott Hughes462e90c2018-08-21 16:10:48 -0700165 * ```
166 *
167 * Available since API level 23.
Dan Albert4caa1f02014-08-20 09:16:57 -0700168 */
zijunzhao1a490582022-12-14 02:15:40 +0000169int malloc_info(int __must_be_zero, FILE* _Nonnull __fp) __INTRODUCED_IN(23);
Dan Albert4caa1f02014-08-20 09:16:57 -0700170
Christopher Ferris8daea552018-10-23 11:17:24 -0700171/**
172 * mallopt() option to set the decay time. Valid values are 0 and 1.
173 *
174 * Available since API level 27.
175 */
Chih-Hung Hsiehfa658eb2020-03-04 11:14:42 -0800176#define M_DECAY_TIME (-100)
Christopher Ferris8daea552018-10-23 11:17:24 -0700177/**
178 * mallopt() option to immediately purge any memory not in use. This
179 * will release the memory back to the kernel. The value is ignored.
180 *
181 * Available since API level 28.
182 */
Chih-Hung Hsiehfa658eb2020-03-04 11:14:42 -0800183#define M_PURGE (-101)
Christopher Ferrisd86eb862023-02-28 12:45:54 -0800184/**
185 * mallopt() option to immediately purge all possible memory back to
186 * the kernel. This call can take longer than a normal purge since it
187 * examines everything. In some cases, it can take more than twice the
188 * time of a M_PURGE call. The value is ignored.
189 *
190 * Available since API level 34.
191 */
192#define M_PURGE_ALL (-104)
Evgenii Stepanovd8d561c2021-06-22 10:18:12 -0700193
194/**
195 * mallopt() option to tune the allocator's choice of memory tags to
196 * make it more likely that a certain class of memory errors will be
197 * detected. This is only relevant if MTE is enabled in this process
198 * and ignored otherwise. The value argument should be one of the
199 * M_MEMTAG_TUNING_* flags.
200 * NOTE: This is only available in scudo.
201 *
202 * Available since API level 31.
203 */
204#define M_MEMTAG_TUNING (-102)
205
206/**
207 * When passed as a value of M_MEMTAG_TUNING mallopt() call, enables
208 * deterministic detection of linear buffer overflow and underflow
209 * bugs by assigning distinct tag values to adjacent allocations. This
210 * mode has a slightly reduced chance to detect use-after-free bugs
211 * because only half of the possible tag values are available for each
212 * memory location.
213 *
214 * Please keep in mind that MTE can not detect overflow within the
215 * same tag granule (16-byte aligned chunk), and can miss small
216 * overflows even in this mode. Such overflow can not be the cause of
217 * a memory corruption, because the memory within one granule is never
218 * used for multiple allocations.
219 */
220#define M_MEMTAG_TUNING_BUFFER_OVERFLOW 0
221
222/**
223 * When passed as a value of M_MEMTAG_TUNING mallopt() call, enables
224 * independently randomized tags for uniform ~93% probability of
225 * detecting both spatial (buffer overflow) and temporal (use after
226 * free) bugs.
227 */
228#define M_MEMTAG_TUNING_UAF 1
229
230/**
Peter Collingbourne978eb162020-09-21 15:26:02 -0700231 * mallopt() option for per-thread memory initialization tuning.
232 * The value argument should be one of:
Christopher Ferrise28867c2023-04-27 17:03:59 -0700233 * 1: Disable automatic heap initialization on this thread only.
234 * If memory tagging is enabled, disable as much as possible of the
235 * memory tagging initialization for this thread.
Peter Collingbourne978eb162020-09-21 15:26:02 -0700236 * 0: Normal behavior.
237 *
238 * Available since API level 31.
239 */
240#define M_THREAD_DISABLE_MEM_INIT (-103)
Christopher Ferris88448792020-07-28 14:15:31 -0700241/**
242 * mallopt() option to set the maximum number of items in the secondary
243 * cache of the scudo allocator.
244 *
245 * Available since API level 31.
246 */
247#define M_CACHE_COUNT_MAX (-200)
248/**
249 * mallopt() option to set the maximum size in bytes of a cacheable item in
250 * the secondary cache of the scudo allocator.
251 *
252 * Available since API level 31.
253 */
254#define M_CACHE_SIZE_MAX (-201)
255/**
256 * mallopt() option to increase the maximum number of shared thread-specific
257 * data structures that can be created. This number cannot be decreased,
258 * only increased and only applies to the scudo allocator.
259 *
260 * Available since API level 31.
261 */
262#define M_TSDS_COUNT_MAX (-202)
Elliott Hughes462e90c2018-08-21 16:10:48 -0700263
264/**
Mitch Phillips9cad8422021-01-20 16:03:27 -0800265 * mallopt() option to decide whether heap memory is zero-initialized on
266 * allocation across the whole process. May be called at any time, including
267 * when multiple threads are running. An argument of zero indicates memory
268 * should not be zero-initialized, any other value indicates to initialize heap
269 * memory to zero.
Elliott Hughes446b4dd2021-01-14 13:34:20 -0800270 *
Evgenii Stepanovd8d561c2021-06-22 10:18:12 -0700271 * Note that this memory mitigation is only implemented in scudo and therefore
Mitch Phillips9cad8422021-01-20 16:03:27 -0800272 * this will have no effect when using another allocator (such as jemalloc on
273 * Android Go devices).
Elliott Hughes446b4dd2021-01-14 13:34:20 -0800274 *
275 * Available since API level 31.
276 */
Mitch Phillips9cad8422021-01-20 16:03:27 -0800277#define M_BIONIC_ZERO_INIT (-203)
Elliott Hughes446b4dd2021-01-14 13:34:20 -0800278
279/**
280 * mallopt() option to change the heap tagging state. May be called at any
281 * time, including when multiple threads are running.
282 * The value must be one of the M_HEAP_TAGGING_LEVEL_ constants.
Evgenii Stepanovd8d561c2021-06-22 10:18:12 -0700283 * NOTE: This is only available in scudo.
Elliott Hughes446b4dd2021-01-14 13:34:20 -0800284 *
285 * Available since API level 31.
286 */
287#define M_BIONIC_SET_HEAP_TAGGING_LEVEL (-204)
288
289/**
290 * Constants for use with the M_BIONIC_SET_HEAP_TAGGING_LEVEL mallopt() option.
291 */
292enum HeapTaggingLevel {
293 /**
294 * Disable heap tagging and memory tag checks (if supported).
295 * Heap tagging may not be re-enabled after being disabled.
296 */
297 M_HEAP_TAGGING_LEVEL_NONE = 0,
298#define M_HEAP_TAGGING_LEVEL_NONE M_HEAP_TAGGING_LEVEL_NONE
299 /**
300 * Address-only tagging. Heap pointers have a non-zero tag in the
301 * most significant ("top") byte which is checked in free(). Memory
302 * accesses ignore the tag using arm64's Top Byte Ignore (TBI) feature.
303 */
304 M_HEAP_TAGGING_LEVEL_TBI = 1,
305#define M_HEAP_TAGGING_LEVEL_TBI M_HEAP_TAGGING_LEVEL_TBI
306 /**
307 * Enable heap tagging and asynchronous memory tag checks (if supported).
308 * Disable stack trace collection.
309 */
310 M_HEAP_TAGGING_LEVEL_ASYNC = 2,
311#define M_HEAP_TAGGING_LEVEL_ASYNC M_HEAP_TAGGING_LEVEL_ASYNC
312 /**
313 * Enable heap tagging and synchronous memory tag checks (if supported).
314 * Enable stack trace collection.
315 */
316 M_HEAP_TAGGING_LEVEL_SYNC = 3,
317#define M_HEAP_TAGGING_LEVEL_SYNC M_HEAP_TAGGING_LEVEL_SYNC
318};
319
320/**
Elliott Hughes462e90c2018-08-21 16:10:48 -0700321 * [mallopt(3)](http://man7.org/linux/man-pages/man3/mallopt.3.html) modifies
322 * heap behavior. Values of `__option` are the `M_` constants from this header.
323 *
324 * Returns 1 on success, 0 on error.
325 *
326 * Available since API level 26.
327 */
Elliott Hughesfaa74342017-08-11 17:34:44 -0700328int mallopt(int __option, int __value) __INTRODUCED_IN(26);
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700329
Elliott Hughes462e90c2018-08-21 16:10:48 -0700330/**
331 * [__malloc_hook(3)](http://man7.org/linux/man-pages/man3/__malloc_hook.3.html)
332 * is called to implement malloc(). By default this points to the system's
333 * implementation.
334 *
335 * Available since API level 28.
336 *
337 * See also: [extra documentation](https://android.googlesource.com/platform/bionic/+/master/libc/malloc_hooks/README.md)
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800338 */
zijunzhao1a490582022-12-14 02:15:40 +0000339extern void* _Nonnull (*volatile _Nonnull __malloc_hook)(size_t __byte_count, const void* _Nonnull __caller) __INTRODUCED_IN(28);
Elliott Hughes462e90c2018-08-21 16:10:48 -0700340
341/**
342 * [__realloc_hook(3)](http://man7.org/linux/man-pages/man3/__realloc_hook.3.html)
343 * is called to implement realloc(). By default this points to the system's
344 * implementation.
345 *
346 * Available since API level 28.
347 *
348 * See also: [extra documentation](https://android.googlesource.com/platform/bionic/+/master/libc/malloc_hooks/README.md)
349 */
zijunzhao1a490582022-12-14 02:15:40 +0000350extern void* _Nonnull (*volatile _Nonnull __realloc_hook)(void* _Nullable __ptr, size_t __byte_count, const void* _Nonnull __caller) __INTRODUCED_IN(28);
Elliott Hughes462e90c2018-08-21 16:10:48 -0700351
352/**
353 * [__free_hook(3)](http://man7.org/linux/man-pages/man3/__free_hook.3.html)
354 * is called to implement free(). 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 */
zijunzhao1a490582022-12-14 02:15:40 +0000361extern void (*volatile _Nonnull __free_hook)(void* _Nullable __ptr, const void* _Nonnull __caller) __INTRODUCED_IN(28);
Elliott Hughes462e90c2018-08-21 16:10:48 -0700362
363/**
364 * [__memalign_hook(3)](http://man7.org/linux/man-pages/man3/__memalign_hook.3.html)
365 * is called to implement memalign(). 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 */
zijunzhao1a490582022-12-14 02:15:40 +0000372extern void* _Nonnull (*volatile _Nonnull __memalign_hook)(size_t __alignment, size_t __byte_count, const void* _Nonnull __caller) __INTRODUCED_IN(28);
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800373
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800374__END_DECLS