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