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