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