Alexey Samsonov | 91bb8e0 | 2014-07-07 17:39:31 +0000 | [diff] [blame] | 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. */ |
Petr Hosek | 016d18c | 2017-12-21 20:51:16 +0000 | [diff] [blame] | 35 | size_t __sanitizer_get_current_allocated_bytes(void); |
Alexey Samsonov | 91bb8e0 | 2014-07-07 17:39:31 +0000 | [diff] [blame] | 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). */ |
Petr Hosek | 016d18c | 2017-12-21 20:51:16 +0000 | [diff] [blame] | 43 | size_t __sanitizer_get_heap_size(void); |
Alexey Samsonov | 91bb8e0 | 2014-07-07 17:39:31 +0000 | [diff] [blame] | 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. */ |
Petr Hosek | 016d18c | 2017-12-21 20:51:16 +0000 | [diff] [blame] | 49 | size_t __sanitizer_get_free_bytes(void); |
Alexey Samsonov | 91bb8e0 | 2014-07-07 17:39:31 +0000 | [diff] [blame] | 50 | |
| 51 | /* Number of bytes in unmapped pages, that are released to OS. Currently, |
| 52 | always returns 0. */ |
Petr Hosek | 016d18c | 2017-12-21 20:51:16 +0000 | [diff] [blame] | 53 | size_t __sanitizer_get_unmapped_bytes(void); |
Alexey Samsonov | 91bb8e0 | 2014-07-07 17:39:31 +0000 | [diff] [blame] | 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); |
Kostya Serebryany | bf6a04f | 2016-06-16 20:06:06 +0000 | [diff] [blame] | 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 | |
Alex Shlyapnikov | 028c4cd | 2017-10-23 17:12:07 +0000 | [diff] [blame] | 79 | /* Drains allocator quarantines (calling thread's and global ones), returns |
| 80 | freed memory back to OS and releases other non-essential internal allocator |
| 81 | resources in attempt to reduce process RSS. |
| 82 | Currently available with ASan only. |
| 83 | */ |
Petr Hosek | 016d18c | 2017-12-21 20:51:16 +0000 | [diff] [blame] | 84 | void __sanitizer_purge_allocator(void); |
Alexey Samsonov | 91bb8e0 | 2014-07-07 17:39:31 +0000 | [diff] [blame] | 85 | #ifdef __cplusplus |
| 86 | } // extern "C" |
| 87 | #endif |
| 88 | |
| 89 | #endif |