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