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