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* _Nullable 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* _Nullable 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* _Nullable realloc(void* _Nullable __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 void* _Nullable reallocarray(void* _Nullable __ptr, size_t __item_count, size_t __item_size) __BIONIC_ALLOC_SIZE(2, 3) __wur __INTRODUCED_IN(29); 74 75 /** 76 * [free(3)](http://man7.org/linux/man-pages/man3/free.3.html) deallocates 77 * memory on the heap. 78 */ 79 void free(void* _Nullable __ptr); 80 81 /** 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 */ 90 void* _Nullable memalign(size_t __alignment, size_t __byte_count) __mallocfunc __BIONIC_ALLOC_SIZE(2) __wur; 91 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. 95 * 96 * Available since API level 17. 97 */ 98 size_t malloc_usable_size(const void* _Nullable __ptr) __INTRODUCED_IN(17); 99 100 #define __MALLINFO_BODY \ 101 /** Total number of non-mmapped bytes currently allocated from OS. */ \ 102 size_t arena; \ 103 /** Number of free chunks. */ \ 104 size_t ordblks; \ 105 /** (Unused.) */ \ 106 size_t smblks; \ 107 /** (Unused.) */ \ 108 size_t hblks; \ 109 /** Total number of bytes in mmapped regions. */ \ 110 size_t hblkhd; \ 111 /** Maximum total allocated space; greater than total if trimming has occurred. */ \ 112 size_t usmblks; \ 113 /** (Unused.) */ \ 114 size_t fsmblks; \ 115 /** Total allocated space (normal or mmapped.) */ \ 116 size_t uordblks; \ 117 /** Total free space. */ \ 118 size_t fordblks; \ 119 /** Upper bound on number of bytes releasable by a trim operation. */ \ 120 size_t keepcost; 121 122 #ifndef STRUCT_MALLINFO_DECLARED 123 #define STRUCT_MALLINFO_DECLARED 1 124 struct mallinfo { __MALLINFO_BODY }; 125 #endif 126 127 /** 128 * [mallinfo(3)](http://man7.org/linux/man-pages/man3/mallinfo.3.html) returns 129 * information about the current state of the heap. Note that mallinfo() is 130 * inherently unreliable and consider using malloc_info() instead. 131 */ 132 struct mallinfo mallinfo(void); 133 134 /** 135 * On Android the struct mallinfo and struct mallinfo2 are the same. 136 */ 137 struct mallinfo2 { __MALLINFO_BODY }; 138 139 /** 140 * [mallinfo2(3)](http://man7.org/linux/man-pages/man3/mallinfo2.3.html) returns 141 * information about the current state of the heap. Note that mallinfo2() is 142 * inherently unreliable and consider using malloc_info() instead. 143 */ 144 struct mallinfo2 mallinfo2(void) __RENAME(mallinfo); 145 146 /** 147 * [malloc_info(3)](http://man7.org/linux/man-pages/man3/malloc_info.3.html) 148 * writes information about the current state of the heap to the given stream. 149 * 150 * The XML structure for malloc_info() is as follows: 151 * ``` 152 * <malloc version="jemalloc-1"> 153 * <heap nr="INT"> 154 * <allocated-large>INT</allocated-large> 155 * <allocated-huge>INT</allocated-huge> 156 * <allocated-bins>INT</allocated-bins> 157 * <bins-total>INT</bins-total> 158 * <bin nr="INT"> 159 * <allocated>INT</allocated> 160 * <nmalloc>INT</nmalloc> 161 * <ndalloc>INT</ndalloc> 162 * </bin> 163 * <!-- more bins --> 164 * </heap> 165 * <!-- more heaps --> 166 * </malloc> 167 * ``` 168 * 169 * Available since API level 23. 170 */ 171 int malloc_info(int __must_be_zero, FILE* _Nonnull __fp) __INTRODUCED_IN(23); 172 173 /** 174 * mallopt() option to set the decay time. Valid values are 0 and 1. 175 * 176 * Available since API level 27. 177 */ 178 #define M_DECAY_TIME (-100) 179 /** 180 * mallopt() option to immediately purge any memory not in use. This 181 * will release the memory back to the kernel. The value is ignored. 182 * 183 * Available since API level 28. 184 */ 185 #define M_PURGE (-101) 186 /** 187 * mallopt() option to immediately purge all possible memory back to 188 * the kernel. This call can take longer than a normal purge since it 189 * examines everything. In some cases, it can take more than twice the 190 * time of a M_PURGE call. The value is ignored. 191 * 192 * Available since API level 34. 193 */ 194 #define M_PURGE_ALL (-104) 195 196 /** 197 * mallopt() option to tune the allocator's choice of memory tags to 198 * make it more likely that a certain class of memory errors will be 199 * detected. This is only relevant if MTE is enabled in this process 200 * and ignored otherwise. The value argument should be one of the 201 * M_MEMTAG_TUNING_* flags. 202 * NOTE: This is only available in scudo. 203 * 204 * Available since API level 31. 205 */ 206 #define M_MEMTAG_TUNING (-102) 207 208 /** 209 * When passed as a value of M_MEMTAG_TUNING mallopt() call, enables 210 * deterministic detection of linear buffer overflow and underflow 211 * bugs by assigning distinct tag values to adjacent allocations. This 212 * mode has a slightly reduced chance to detect use-after-free bugs 213 * because only half of the possible tag values are available for each 214 * memory location. 215 * 216 * Please keep in mind that MTE can not detect overflow within the 217 * same tag granule (16-byte aligned chunk), and can miss small 218 * overflows even in this mode. Such overflow can not be the cause of 219 * a memory corruption, because the memory within one granule is never 220 * used for multiple allocations. 221 */ 222 #define M_MEMTAG_TUNING_BUFFER_OVERFLOW 0 223 224 /** 225 * When passed as a value of M_MEMTAG_TUNING mallopt() call, enables 226 * independently randomized tags for uniform ~93% probability of 227 * detecting both spatial (buffer overflow) and temporal (use after 228 * free) bugs. 229 */ 230 #define M_MEMTAG_TUNING_UAF 1 231 232 /** 233 * mallopt() option for per-thread memory initialization tuning. 234 * The value argument should be one of: 235 * 1: Disable automatic heap initialization and, where possible, memory tagging, 236 * on this thread. 237 * 0: Normal behavior. 238 * 239 * Available since API level 31. 240 */ 241 #define M_THREAD_DISABLE_MEM_INIT (-103) 242 /** 243 * mallopt() option to set the maximum number of items in the secondary 244 * cache of the scudo allocator. 245 * 246 * Available since API level 31. 247 */ 248 #define M_CACHE_COUNT_MAX (-200) 249 /** 250 * mallopt() option to set the maximum size in bytes of a cacheable item in 251 * the secondary cache of the scudo allocator. 252 * 253 * Available since API level 31. 254 */ 255 #define M_CACHE_SIZE_MAX (-201) 256 /** 257 * mallopt() option to increase the maximum number of shared thread-specific 258 * data structures that can be created. This number cannot be decreased, 259 * only increased and only applies to the scudo allocator. 260 * 261 * Available since API level 31. 262 */ 263 #define M_TSDS_COUNT_MAX (-202) 264 265 /** 266 * mallopt() option to decide whether heap memory is zero-initialized on 267 * allocation across the whole process. May be called at any time, including 268 * when multiple threads are running. An argument of zero indicates memory 269 * should not be zero-initialized, any other value indicates to initialize heap 270 * memory to zero. 271 * 272 * Note that this memory mitigation is only implemented in scudo and therefore 273 * this will have no effect when using another allocator (such as jemalloc on 274 * Android Go devices). 275 * 276 * Available since API level 31. 277 */ 278 #define M_BIONIC_ZERO_INIT (-203) 279 280 /** 281 * mallopt() option to change the heap tagging state. May be called at any 282 * time, including when multiple threads are running. 283 * The value must be one of the M_HEAP_TAGGING_LEVEL_ constants. 284 * NOTE: This is only available in scudo. 285 * 286 * Available since API level 31. 287 */ 288 #define M_BIONIC_SET_HEAP_TAGGING_LEVEL (-204) 289 290 /** 291 * Constants for use with the M_BIONIC_SET_HEAP_TAGGING_LEVEL mallopt() option. 292 */ 293 enum HeapTaggingLevel { 294 /** 295 * Disable heap tagging and memory tag checks (if supported). 296 * Heap tagging may not be re-enabled after being disabled. 297 */ 298 M_HEAP_TAGGING_LEVEL_NONE = 0, 299 #define M_HEAP_TAGGING_LEVEL_NONE M_HEAP_TAGGING_LEVEL_NONE 300 /** 301 * Address-only tagging. Heap pointers have a non-zero tag in the 302 * most significant ("top") byte which is checked in free(). Memory 303 * accesses ignore the tag using arm64's Top Byte Ignore (TBI) feature. 304 */ 305 M_HEAP_TAGGING_LEVEL_TBI = 1, 306 #define M_HEAP_TAGGING_LEVEL_TBI M_HEAP_TAGGING_LEVEL_TBI 307 /** 308 * Enable heap tagging and asynchronous memory tag checks (if supported). 309 * Disable stack trace collection. 310 */ 311 M_HEAP_TAGGING_LEVEL_ASYNC = 2, 312 #define M_HEAP_TAGGING_LEVEL_ASYNC M_HEAP_TAGGING_LEVEL_ASYNC 313 /** 314 * Enable heap tagging and synchronous memory tag checks (if supported). 315 * Enable stack trace collection. 316 */ 317 M_HEAP_TAGGING_LEVEL_SYNC = 3, 318 #define M_HEAP_TAGGING_LEVEL_SYNC M_HEAP_TAGGING_LEVEL_SYNC 319 }; 320 321 /** 322 * [mallopt(3)](http://man7.org/linux/man-pages/man3/mallopt.3.html) modifies 323 * heap behavior. Values of `__option` are the `M_` constants from this header. 324 * 325 * Returns 1 on success, 0 on error. 326 * 327 * Available since API level 26. 328 */ 329 int mallopt(int __option, int __value) __INTRODUCED_IN(26); 330 331 /** 332 * [__malloc_hook(3)](http://man7.org/linux/man-pages/man3/__malloc_hook.3.html) 333 * is called to implement malloc(). By default this points to the system's 334 * implementation. 335 * 336 * Available since API level 28. 337 * 338 * See also: [extra documentation](https://android.googlesource.com/platform/bionic/+/master/libc/malloc_hooks/README.md) 339 */ 340 extern void* _Nonnull (*volatile _Nonnull __malloc_hook)(size_t __byte_count, const void* _Nonnull __caller) __INTRODUCED_IN(28); 341 342 /** 343 * [__realloc_hook(3)](http://man7.org/linux/man-pages/man3/__realloc_hook.3.html) 344 * is called to implement realloc(). By default this points to the system's 345 * implementation. 346 * 347 * Available since API level 28. 348 * 349 * See also: [extra documentation](https://android.googlesource.com/platform/bionic/+/master/libc/malloc_hooks/README.md) 350 */ 351 extern void* _Nonnull (*volatile _Nonnull __realloc_hook)(void* _Nullable __ptr, size_t __byte_count, const void* _Nonnull __caller) __INTRODUCED_IN(28); 352 353 /** 354 * [__free_hook(3)](http://man7.org/linux/man-pages/man3/__free_hook.3.html) 355 * is called to implement free(). By default this points to the system's 356 * implementation. 357 * 358 * Available since API level 28. 359 * 360 * See also: [extra documentation](https://android.googlesource.com/platform/bionic/+/master/libc/malloc_hooks/README.md) 361 */ 362 extern void (*volatile _Nonnull __free_hook)(void* _Nullable __ptr, const void* _Nonnull __caller) __INTRODUCED_IN(28); 363 364 /** 365 * [__memalign_hook(3)](http://man7.org/linux/man-pages/man3/__memalign_hook.3.html) 366 * is called to implement memalign(). By default this points to the system's 367 * implementation. 368 * 369 * Available since API level 28. 370 * 371 * See also: [extra documentation](https://android.googlesource.com/platform/bionic/+/master/libc/malloc_hooks/README.md) 372 */ 373 extern void* _Nonnull (*volatile _Nonnull __memalign_hook)(size_t __alignment, size_t __byte_count, const void* _Nonnull __caller) __INTRODUCED_IN(28); 374 375 __END_DECLS 376