xref: /aosp_15_r20/external/compiler-rt/include/sanitizer/asan_interface.h (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot //===-- sanitizer/asan_interface.h ------------------------------*- C++ -*-===//
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 // This file is a part of AddressSanitizer.
11*7c3d14c8STreehugger Robot //
12*7c3d14c8STreehugger Robot // Public interface header.
13*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
14*7c3d14c8STreehugger Robot #ifndef SANITIZER_ASAN_INTERFACE_H
15*7c3d14c8STreehugger Robot #define SANITIZER_ASAN_INTERFACE_H
16*7c3d14c8STreehugger Robot 
17*7c3d14c8STreehugger Robot #include <sanitizer/common_interface_defs.h>
18*7c3d14c8STreehugger Robot 
19*7c3d14c8STreehugger Robot #ifdef __cplusplus
20*7c3d14c8STreehugger Robot extern "C" {
21*7c3d14c8STreehugger Robot #endif
22*7c3d14c8STreehugger Robot   // Marks memory region [addr, addr+size) as unaddressable.
23*7c3d14c8STreehugger Robot   // This memory must be previously allocated by the user program. Accessing
24*7c3d14c8STreehugger Robot   // addresses in this region from instrumented code is forbidden until
25*7c3d14c8STreehugger Robot   // this region is unpoisoned. This function is not guaranteed to poison
26*7c3d14c8STreehugger Robot   // the whole region - it may poison only subregion of [addr, addr+size) due
27*7c3d14c8STreehugger Robot   // to ASan alignment restrictions.
28*7c3d14c8STreehugger Robot   // Method is NOT thread-safe in the sense that no two threads can
29*7c3d14c8STreehugger Robot   // (un)poison memory in the same memory region simultaneously.
30*7c3d14c8STreehugger Robot   void __asan_poison_memory_region(void const volatile *addr, size_t size);
31*7c3d14c8STreehugger Robot   // Marks memory region [addr, addr+size) as addressable.
32*7c3d14c8STreehugger Robot   // This memory must be previously allocated by the user program. Accessing
33*7c3d14c8STreehugger Robot   // addresses in this region is allowed until this region is poisoned again.
34*7c3d14c8STreehugger Robot   // This function may unpoison a superregion of [addr, addr+size) due to
35*7c3d14c8STreehugger Robot   // ASan alignment restrictions.
36*7c3d14c8STreehugger Robot   // Method is NOT thread-safe in the sense that no two threads can
37*7c3d14c8STreehugger Robot   // (un)poison memory in the same memory region simultaneously.
38*7c3d14c8STreehugger Robot   void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
39*7c3d14c8STreehugger Robot 
40*7c3d14c8STreehugger Robot // User code should use macros instead of functions.
41*7c3d14c8STreehugger Robot #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
42*7c3d14c8STreehugger Robot #define ASAN_POISON_MEMORY_REGION(addr, size) \
43*7c3d14c8STreehugger Robot   __asan_poison_memory_region((addr), (size))
44*7c3d14c8STreehugger Robot #define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
45*7c3d14c8STreehugger Robot   __asan_unpoison_memory_region((addr), (size))
46*7c3d14c8STreehugger Robot #else
47*7c3d14c8STreehugger Robot #define ASAN_POISON_MEMORY_REGION(addr, size) \
48*7c3d14c8STreehugger Robot   ((void)(addr), (void)(size))
49*7c3d14c8STreehugger Robot #define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
50*7c3d14c8STreehugger Robot   ((void)(addr), (void)(size))
51*7c3d14c8STreehugger Robot #endif
52*7c3d14c8STreehugger Robot 
53*7c3d14c8STreehugger Robot   // Returns 1 if addr is poisoned (i.e. 1-byte read/write access to this
54*7c3d14c8STreehugger Robot   // address will result in error report from AddressSanitizer).
55*7c3d14c8STreehugger Robot   // Otherwise returns 0.
56*7c3d14c8STreehugger Robot   int __asan_address_is_poisoned(void const volatile *addr);
57*7c3d14c8STreehugger Robot 
58*7c3d14c8STreehugger Robot   // If at least one byte in [beg, beg+size) is poisoned, return the address
59*7c3d14c8STreehugger Robot   // of the first such byte. Otherwise return 0.
60*7c3d14c8STreehugger Robot   void *__asan_region_is_poisoned(void *beg, size_t size);
61*7c3d14c8STreehugger Robot 
62*7c3d14c8STreehugger Robot   // Print the description of addr (useful when debugging in gdb).
63*7c3d14c8STreehugger Robot   void __asan_describe_address(void *addr);
64*7c3d14c8STreehugger Robot 
65*7c3d14c8STreehugger Robot   // Useful for calling from a debugger to get information about an ASan error.
66*7c3d14c8STreehugger Robot   // Returns 1 if an error has been (or is being) reported, otherwise returns 0.
67*7c3d14c8STreehugger Robot   int __asan_report_present();
68*7c3d14c8STreehugger Robot 
69*7c3d14c8STreehugger Robot   // Useful for calling from a debugger to get information about an ASan error.
70*7c3d14c8STreehugger Robot   // If an error has been (or is being) reported, the following functions return
71*7c3d14c8STreehugger Robot   // the pc, bp, sp, address, access type (0 = read, 1 = write), access size and
72*7c3d14c8STreehugger Robot   // bug description (e.g. "heap-use-after-free"). Otherwise they return 0.
73*7c3d14c8STreehugger Robot   void *__asan_get_report_pc();
74*7c3d14c8STreehugger Robot   void *__asan_get_report_bp();
75*7c3d14c8STreehugger Robot   void *__asan_get_report_sp();
76*7c3d14c8STreehugger Robot   void *__asan_get_report_address();
77*7c3d14c8STreehugger Robot   int __asan_get_report_access_type();
78*7c3d14c8STreehugger Robot   size_t __asan_get_report_access_size();
79*7c3d14c8STreehugger Robot   const char *__asan_get_report_description();
80*7c3d14c8STreehugger Robot 
81*7c3d14c8STreehugger Robot   // Useful for calling from the debugger to get information about a pointer.
82*7c3d14c8STreehugger Robot   // Returns the category of the given pointer as a constant string.
83*7c3d14c8STreehugger Robot   // Possible return values are "global", "stack", "stack-fake", "heap",
84*7c3d14c8STreehugger Robot   // "heap-invalid", "shadow-low", "shadow-gap", "shadow-high", "unknown".
85*7c3d14c8STreehugger Robot   // If global or stack, tries to also return the variable name, address and
86*7c3d14c8STreehugger Robot   // size. If heap, tries to return the chunk address and size. 'name' should
87*7c3d14c8STreehugger Robot   // point to an allocated buffer of size 'name_size'.
88*7c3d14c8STreehugger Robot   const char *__asan_locate_address(void *addr, char *name, size_t name_size,
89*7c3d14c8STreehugger Robot                                     void **region_address, size_t *region_size);
90*7c3d14c8STreehugger Robot 
91*7c3d14c8STreehugger Robot   // Useful for calling from the debugger to get the allocation stack trace
92*7c3d14c8STreehugger Robot   // and thread ID for a heap address. Stores up to 'size' frames into 'trace',
93*7c3d14c8STreehugger Robot   // returns the number of stored frames or 0 on error.
94*7c3d14c8STreehugger Robot   size_t __asan_get_alloc_stack(void *addr, void **trace, size_t size,
95*7c3d14c8STreehugger Robot                                 int *thread_id);
96*7c3d14c8STreehugger Robot 
97*7c3d14c8STreehugger Robot   // Useful for calling from the debugger to get the free stack trace
98*7c3d14c8STreehugger Robot   // and thread ID for a heap address. Stores up to 'size' frames into 'trace',
99*7c3d14c8STreehugger Robot   // returns the number of stored frames or 0 on error.
100*7c3d14c8STreehugger Robot   size_t __asan_get_free_stack(void *addr, void **trace, size_t size,
101*7c3d14c8STreehugger Robot                                int *thread_id);
102*7c3d14c8STreehugger Robot 
103*7c3d14c8STreehugger Robot   // Useful for calling from the debugger to get the current shadow memory
104*7c3d14c8STreehugger Robot   // mapping.
105*7c3d14c8STreehugger Robot   void __asan_get_shadow_mapping(size_t *shadow_scale, size_t *shadow_offset);
106*7c3d14c8STreehugger Robot 
107*7c3d14c8STreehugger Robot   // This is an internal function that is called to report an error.
108*7c3d14c8STreehugger Robot   // However it is still a part of the interface because users may want to
109*7c3d14c8STreehugger Robot   // set a breakpoint on this function in a debugger.
110*7c3d14c8STreehugger Robot   void __asan_report_error(void *pc, void *bp, void *sp,
111*7c3d14c8STreehugger Robot                            void *addr, int is_write, size_t access_size);
112*7c3d14c8STreehugger Robot 
113*7c3d14c8STreehugger Robot   // Deprecated. Call __sanitizer_set_death_callback instead.
114*7c3d14c8STreehugger Robot   void __asan_set_death_callback(void (*callback)(void));
115*7c3d14c8STreehugger Robot 
116*7c3d14c8STreehugger Robot   void __asan_set_error_report_callback(void (*callback)(const char*));
117*7c3d14c8STreehugger Robot 
118*7c3d14c8STreehugger Robot   // User may provide function that would be called right when ASan detects
119*7c3d14c8STreehugger Robot   // an error. This can be used to notice cases when ASan detects an error, but
120*7c3d14c8STreehugger Robot   // the program crashes before ASan report is printed.
121*7c3d14c8STreehugger Robot   void __asan_on_error();
122*7c3d14c8STreehugger Robot 
123*7c3d14c8STreehugger Robot   // Prints accumulated stats to stderr. Used for debugging.
124*7c3d14c8STreehugger Robot   void __asan_print_accumulated_stats();
125*7c3d14c8STreehugger Robot 
126*7c3d14c8STreehugger Robot   // This function may be optionally provided by user and should return
127*7c3d14c8STreehugger Robot   // a string containing ASan runtime options. See asan_flags.h for details.
128*7c3d14c8STreehugger Robot   const char* __asan_default_options();
129*7c3d14c8STreehugger Robot 
130*7c3d14c8STreehugger Robot   // The following 2 functions facilitate garbage collection in presence of
131*7c3d14c8STreehugger Robot   // asan's fake stack.
132*7c3d14c8STreehugger Robot 
133*7c3d14c8STreehugger Robot   // Returns an opaque handler to be used later in __asan_addr_is_in_fake_stack.
134*7c3d14c8STreehugger Robot   // Returns NULL if the current thread does not have a fake stack.
135*7c3d14c8STreehugger Robot   void *__asan_get_current_fake_stack();
136*7c3d14c8STreehugger Robot 
137*7c3d14c8STreehugger Robot   // If fake_stack is non-NULL and addr belongs to a fake frame in
138*7c3d14c8STreehugger Robot   // fake_stack, returns the address on real stack that corresponds to
139*7c3d14c8STreehugger Robot   // the fake frame and sets beg/end to the boundaries of this fake frame.
140*7c3d14c8STreehugger Robot   // Otherwise returns NULL and does not touch beg/end.
141*7c3d14c8STreehugger Robot   // If beg/end are NULL, they are not touched.
142*7c3d14c8STreehugger Robot   // This function may be called from a thread other than the owner of
143*7c3d14c8STreehugger Robot   // fake_stack, but the owner thread need to be alive.
144*7c3d14c8STreehugger Robot   void *__asan_addr_is_in_fake_stack(void *fake_stack, void *addr, void **beg,
145*7c3d14c8STreehugger Robot                                      void **end);
146*7c3d14c8STreehugger Robot 
147*7c3d14c8STreehugger Robot #ifdef __cplusplus
148*7c3d14c8STreehugger Robot }  // extern "C"
149*7c3d14c8STreehugger Robot #endif
150*7c3d14c8STreehugger Robot 
151*7c3d14c8STreehugger Robot #endif  // SANITIZER_ASAN_INTERFACE_H
152