1*3cab2bb3Spatrick //===-- allocator_interface.h ---------------------------------------------===// 2*3cab2bb3Spatrick // 3*3cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*3cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information. 5*3cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*3cab2bb3Spatrick // 7*3cab2bb3Spatrick //===----------------------------------------------------------------------===// 8*3cab2bb3Spatrick // 9*3cab2bb3Spatrick // Public interface header for allocator used in sanitizers (ASan/TSan/MSan). 10*3cab2bb3Spatrick //===----------------------------------------------------------------------===// 11*3cab2bb3Spatrick #ifndef SANITIZER_ALLOCATOR_INTERFACE_H 12*3cab2bb3Spatrick #define SANITIZER_ALLOCATOR_INTERFACE_H 13*3cab2bb3Spatrick 14*3cab2bb3Spatrick #include <stddef.h> 15*3cab2bb3Spatrick 16*3cab2bb3Spatrick #ifdef __cplusplus 17*3cab2bb3Spatrick extern "C" { 18*3cab2bb3Spatrick #endif 19*3cab2bb3Spatrick /* Returns the estimated number of bytes that will be reserved by allocator 20*3cab2bb3Spatrick for request of "size" bytes. If allocator can't allocate that much 21*3cab2bb3Spatrick memory, returns the maximal possible allocation size, otherwise returns 22*3cab2bb3Spatrick "size". */ 23*3cab2bb3Spatrick size_t __sanitizer_get_estimated_allocated_size(size_t size); 24*3cab2bb3Spatrick 25*3cab2bb3Spatrick /* Returns true if p was returned by the allocator and 26*3cab2bb3Spatrick is not yet freed. */ 27*3cab2bb3Spatrick int __sanitizer_get_ownership(const volatile void *p); 28*3cab2bb3Spatrick 29*3cab2bb3Spatrick /* Returns the number of bytes reserved for the pointer p. 30*3cab2bb3Spatrick Requires (get_ownership(p) == true) or (p == 0). */ 31*3cab2bb3Spatrick size_t __sanitizer_get_allocated_size(const volatile void *p); 32*3cab2bb3Spatrick 33*3cab2bb3Spatrick /* Number of bytes, allocated and not yet freed by the application. */ 34*3cab2bb3Spatrick size_t __sanitizer_get_current_allocated_bytes(void); 35*3cab2bb3Spatrick 36*3cab2bb3Spatrick /* Number of bytes, mmaped by the allocator to fulfill allocation requests. 37*3cab2bb3Spatrick Generally, for request of X bytes, allocator can reserve and add to free 38*3cab2bb3Spatrick lists a large number of chunks of size X to use them for future requests. 39*3cab2bb3Spatrick All these chunks count toward the heap size. Currently, allocator never 40*3cab2bb3Spatrick releases memory to OS (instead, it just puts freed chunks to free 41*3cab2bb3Spatrick lists). */ 42*3cab2bb3Spatrick size_t __sanitizer_get_heap_size(void); 43*3cab2bb3Spatrick 44*3cab2bb3Spatrick /* Number of bytes, mmaped by the allocator, which can be used to fulfill 45*3cab2bb3Spatrick allocation requests. When a user program frees memory chunk, it can first 46*3cab2bb3Spatrick fall into quarantine and will count toward __sanitizer_get_free_bytes() 47*3cab2bb3Spatrick later. */ 48*3cab2bb3Spatrick size_t __sanitizer_get_free_bytes(void); 49*3cab2bb3Spatrick 50*3cab2bb3Spatrick /* Number of bytes in unmapped pages, that are released to OS. Currently, 51*3cab2bb3Spatrick always returns 0. */ 52*3cab2bb3Spatrick size_t __sanitizer_get_unmapped_bytes(void); 53*3cab2bb3Spatrick 54*3cab2bb3Spatrick /* Malloc hooks that may be optionally provided by user. 55*3cab2bb3Spatrick __sanitizer_malloc_hook(ptr, size) is called immediately after 56*3cab2bb3Spatrick allocation of "size" bytes, which returned "ptr". 57*3cab2bb3Spatrick __sanitizer_free_hook(ptr) is called immediately before 58*3cab2bb3Spatrick deallocation of "ptr". */ 59*3cab2bb3Spatrick void __sanitizer_malloc_hook(const volatile void *ptr, size_t size); 60*3cab2bb3Spatrick void __sanitizer_free_hook(const volatile void *ptr); 61*3cab2bb3Spatrick 62*3cab2bb3Spatrick /* Installs a pair of hooks for malloc/free. 63*3cab2bb3Spatrick Several (currently, 5) hook pairs may be installed, they are executed 64*3cab2bb3Spatrick in the order they were installed and after calling 65*3cab2bb3Spatrick __sanitizer_malloc_hook/__sanitizer_free_hook. 66*3cab2bb3Spatrick Unlike __sanitizer_malloc_hook/__sanitizer_free_hook these hooks can be 67*3cab2bb3Spatrick chained and do not rely on weak symbols working on the platform, but 68*3cab2bb3Spatrick require __sanitizer_install_malloc_and_free_hooks to be called at startup 69*3cab2bb3Spatrick and thus will not be called on malloc/free very early in the process. 70*3cab2bb3Spatrick Returns the number of hooks currently installed or 0 on failure. 71*3cab2bb3Spatrick Not thread-safe, should be called in the main thread before starting 72*3cab2bb3Spatrick other threads. 73*3cab2bb3Spatrick */ 74*3cab2bb3Spatrick int __sanitizer_install_malloc_and_free_hooks( 75*3cab2bb3Spatrick void (*malloc_hook)(const volatile void *, size_t), 76*3cab2bb3Spatrick void (*free_hook)(const volatile void *)); 77*3cab2bb3Spatrick 78*3cab2bb3Spatrick /* Drains allocator quarantines (calling thread's and global ones), returns 79*3cab2bb3Spatrick freed memory back to OS and releases other non-essential internal allocator 80*3cab2bb3Spatrick resources in attempt to reduce process RSS. 81*3cab2bb3Spatrick Currently available with ASan only. 82*3cab2bb3Spatrick */ 83*3cab2bb3Spatrick void __sanitizer_purge_allocator(void); 84*3cab2bb3Spatrick #ifdef __cplusplus 85*3cab2bb3Spatrick } // extern "C" 86*3cab2bb3Spatrick #endif 87*3cab2bb3Spatrick 88*3cab2bb3Spatrick #endif 89