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