xref: /freebsd/contrib/llvm-project/compiler-rt/include/sanitizer/allocator_interface.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 //===-- allocator_interface.h ---------------------------------------------===//
2 //
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
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 <sanitizer/common_interface_defs.h>
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_CDECL __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_CDECL __sanitizer_get_ownership(const volatile void *p);
29 
30 /* If a pointer lies within an allocation, it will return the start address
31    of the allocation. Otherwise, it returns nullptr. */
32 const void *SANITIZER_CDECL __sanitizer_get_allocated_begin(const void *p);
33 
34 /* Returns the number of bytes reserved for the pointer p.
35    Requires (get_ownership(p) == true) or (p == 0). */
36 size_t SANITIZER_CDECL __sanitizer_get_allocated_size(const volatile void *p);
37 
38 /* Returns the number of bytes reserved for the pointer p.
39    Requires __sanitizer_get_allocated_begin(p) == p. */
40 size_t SANITIZER_CDECL
41 __sanitizer_get_allocated_size_fast(const volatile void *p);
42 
43 /* Number of bytes, allocated and not yet freed by the application. */
44 size_t SANITIZER_CDECL __sanitizer_get_current_allocated_bytes(void);
45 
46 /* Number of bytes, mmaped by the allocator to fulfill allocation requests.
47    Generally, for request of X bytes, allocator can reserve and add to free
48    lists a large number of chunks of size X to use them for future requests.
49    All these chunks count toward the heap size. Currently, allocator never
50    releases memory to OS (instead, it just puts freed chunks to free
51    lists). */
52 size_t SANITIZER_CDECL __sanitizer_get_heap_size(void);
53 
54 /* Number of bytes, mmaped by the allocator, which can be used to fulfill
55    allocation requests. When a user program frees memory chunk, it can first
56    fall into quarantine and will count toward __sanitizer_get_free_bytes()
57    later. */
58 size_t SANITIZER_CDECL __sanitizer_get_free_bytes(void);
59 
60 /* Number of bytes in unmapped pages, that are released to OS. Currently,
61    always returns 0. */
62 size_t SANITIZER_CDECL __sanitizer_get_unmapped_bytes(void);
63 
64 /* Malloc hooks that may be optionally provided by user.
65    - __sanitizer_malloc_hook(ptr, size) is called immediately after allocation
66      of "size" bytes, which returned "ptr".
67    - __sanitizer_free_hook(ptr) is called immediately before deallocation of
68      "ptr".
69    - __sanitizer_ignore_free_hook(ptr) is called immediately before deallocation
70      of "ptr", and if it returns a non-zero value, the deallocation of "ptr"
71      will not take place. This allows software to make free a no-op until it
72      calls free() again in the same pointer at a later time. Hint: read this as
73      "ignore the free" rather than "ignore the hook".
74 */
75 void SANITIZER_CDECL __sanitizer_malloc_hook(const volatile void *ptr,
76                                              size_t size);
77 void SANITIZER_CDECL __sanitizer_free_hook(const volatile void *ptr);
78 int SANITIZER_CDECL __sanitizer_ignore_free_hook(const volatile void *ptr);
79 
80 /* Installs a pair of hooks for malloc/free.
81    Several (currently, 5) hook pairs may be installed, they are executed
82    in the order they were installed and after calling
83    __sanitizer_malloc_hook/__sanitizer_free_hook.
84    Unlike __sanitizer_malloc_hook/__sanitizer_free_hook these hooks can be
85    chained and do not rely on weak symbols working on the platform, but
86    require __sanitizer_install_malloc_and_free_hooks to be called at startup
87    and thus will not be called on malloc/free very early in the process.
88    Returns the number of hooks currently installed or 0 on failure.
89    Not thread-safe, should be called in the main thread before starting
90    other threads.
91 */
92 int SANITIZER_CDECL __sanitizer_install_malloc_and_free_hooks(
93     void(SANITIZER_CDECL *malloc_hook)(const volatile void *, size_t),
94     void(SANITIZER_CDECL *free_hook)(const volatile void *));
95 
96 /* Drains allocator quarantines (calling thread's and global ones), returns
97    freed memory back to OS and releases other non-essential internal allocator
98    resources in attempt to reduce process RSS.
99    Currently available with ASan only.
100 */
101 void SANITIZER_CDECL __sanitizer_purge_allocator(void);
102 #ifdef __cplusplus
103 } // extern "C"
104 #endif
105 
106 #endif
107