xref: /freebsd/contrib/llvm-project/compiler-rt/lib/asan/asan_interface_internal.h (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
1 //===-- asan_interface_internal.h -------------------------------*- C++ -*-===//
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 // This file is a part of AddressSanitizer, an address sanity checker.
10 //
11 // This header declares the AddressSanitizer runtime interface functions.
12 // The runtime library has to define these functions so the instrumented program
13 // could call them.
14 //
15 // See also include/sanitizer/asan_interface.h
16 //===----------------------------------------------------------------------===//
17 #ifndef ASAN_INTERFACE_INTERNAL_H
18 #define ASAN_INTERFACE_INTERNAL_H
19 
20 #include "sanitizer_common/sanitizer_internal_defs.h"
21 
22 #include "asan_init_version.h"
23 
24 using __sanitizer::uptr;
25 using __sanitizer::u64;
26 using __sanitizer::u32;
27 
28 extern "C" {
29   // This function should be called at the very beginning of the process,
30   // before any instrumented code is executed and before any call to malloc.
31   SANITIZER_INTERFACE_ATTRIBUTE void __asan_init();
32 
33   // This function exists purely to get a linker/loader error when using
34   // incompatible versions of instrumentation and runtime library. Please note
35   // that __asan_version_mismatch_check is a macro that is replaced with
36   // __asan_version_mismatch_check_vXXX at compile-time.
37   SANITIZER_INTERFACE_ATTRIBUTE void __asan_version_mismatch_check();
38 
39   // This structure is used to describe the source location of a place where
40   // global was defined.
41   struct __asan_global_source_location {
42     const char *filename;
43     int line_no;
44     int column_no;
45   };
46 
47   // This structure describes an instrumented global variable.
48   struct __asan_global {
49     uptr beg;                // The address of the global.
50     uptr size;               // The original size of the global.
51     uptr size_with_redzone;  // The size with the redzone.
52     const char *name;        // Name as a C string.
53     const char *module_name; // Module name as a C string. This pointer is a
54                              // unique identifier of a module.
55     uptr has_dynamic_init;   // Non-zero if the global has dynamic initializer.
56     uptr windows_padding;    // TODO: Figure out how to remove this padding
57                              // that's simply here to make the MSVC incremental
58                              // linker happy...
59     uptr odr_indicator;      // The address of the ODR indicator symbol.
60   };
61 
62   // These functions can be called on some platforms to find globals in the same
63   // loaded image as `flag' and apply __asan_(un)register_globals to them,
64   // filtering out redundant calls.
65   SANITIZER_INTERFACE_ATTRIBUTE
66   void __asan_register_image_globals(uptr *flag);
67   SANITIZER_INTERFACE_ATTRIBUTE
68   void __asan_unregister_image_globals(uptr *flag);
69 
70   SANITIZER_INTERFACE_ATTRIBUTE
71   void __asan_register_elf_globals(uptr *flag, void *start, void *stop);
72   SANITIZER_INTERFACE_ATTRIBUTE
73   void __asan_unregister_elf_globals(uptr *flag, void *start, void *stop);
74 
75   // These two functions should be called by the instrumented code.
76   // 'globals' is an array of structures describing 'n' globals.
77   SANITIZER_INTERFACE_ATTRIBUTE
78   void __asan_register_globals(__asan_global *globals, uptr n);
79   SANITIZER_INTERFACE_ATTRIBUTE
80   void __asan_unregister_globals(__asan_global *globals, uptr n);
81 
82   // These two functions should be called before and after dynamic initializers
83   // of a single module run, respectively.
84   SANITIZER_INTERFACE_ATTRIBUTE
85   void __asan_before_dynamic_init(const char *module_name);
86   SANITIZER_INTERFACE_ATTRIBUTE
87   void __asan_after_dynamic_init();
88 
89   // Sets bytes of the given range of the shadow memory into specific value.
90   SANITIZER_INTERFACE_ATTRIBUTE
91   void __asan_set_shadow_00(uptr addr, uptr size);
92   SANITIZER_INTERFACE_ATTRIBUTE
93   void __asan_set_shadow_01(uptr addr, uptr size);
94   SANITIZER_INTERFACE_ATTRIBUTE
95   void __asan_set_shadow_02(uptr addr, uptr size);
96   SANITIZER_INTERFACE_ATTRIBUTE
97   void __asan_set_shadow_03(uptr addr, uptr size);
98   SANITIZER_INTERFACE_ATTRIBUTE
99   void __asan_set_shadow_04(uptr addr, uptr size);
100   SANITIZER_INTERFACE_ATTRIBUTE
101   void __asan_set_shadow_05(uptr addr, uptr size);
102   SANITIZER_INTERFACE_ATTRIBUTE
103   void __asan_set_shadow_06(uptr addr, uptr size);
104   SANITIZER_INTERFACE_ATTRIBUTE
105   void __asan_set_shadow_07(uptr addr, uptr size);
106   SANITIZER_INTERFACE_ATTRIBUTE
107   void __asan_set_shadow_f1(uptr addr, uptr size);
108   SANITIZER_INTERFACE_ATTRIBUTE
109   void __asan_set_shadow_f2(uptr addr, uptr size);
110   SANITIZER_INTERFACE_ATTRIBUTE
111   void __asan_set_shadow_f3(uptr addr, uptr size);
112   SANITIZER_INTERFACE_ATTRIBUTE
113   void __asan_set_shadow_f5(uptr addr, uptr size);
114   SANITIZER_INTERFACE_ATTRIBUTE
115   void __asan_set_shadow_f8(uptr addr, uptr size);
116 
117   // These two functions are used by instrumented code in the
118   // use-after-scope mode. They mark memory for local variables as
119   // unaddressable when they leave scope and addressable before the
120   // function exits.
121   SANITIZER_INTERFACE_ATTRIBUTE
122   void __asan_poison_stack_memory(uptr addr, uptr size);
123   SANITIZER_INTERFACE_ATTRIBUTE
124   void __asan_unpoison_stack_memory(uptr addr, uptr size);
125 
126   // Performs cleanup before a NoReturn function. Must be called before things
127   // like _exit and execl to avoid false positives on stack.
128   SANITIZER_INTERFACE_ATTRIBUTE void __asan_handle_no_return();
129 
130   SANITIZER_INTERFACE_ATTRIBUTE
131   void __asan_poison_memory_region(void const volatile *addr, uptr size);
132   SANITIZER_INTERFACE_ATTRIBUTE
133   void __asan_unpoison_memory_region(void const volatile *addr, uptr size);
134 
135   SANITIZER_INTERFACE_ATTRIBUTE
136   int __asan_address_is_poisoned(void const volatile *addr);
137 
138   SANITIZER_INTERFACE_ATTRIBUTE
139   uptr __asan_region_is_poisoned(uptr beg, uptr size);
140 
141   SANITIZER_INTERFACE_ATTRIBUTE
142   void __asan_describe_address(uptr addr);
143 
144   SANITIZER_INTERFACE_ATTRIBUTE
145   int __asan_report_present();
146 
147   SANITIZER_INTERFACE_ATTRIBUTE
148   uptr __asan_get_report_pc();
149   SANITIZER_INTERFACE_ATTRIBUTE
150   uptr __asan_get_report_bp();
151   SANITIZER_INTERFACE_ATTRIBUTE
152   uptr __asan_get_report_sp();
153   SANITIZER_INTERFACE_ATTRIBUTE
154   uptr __asan_get_report_address();
155   SANITIZER_INTERFACE_ATTRIBUTE
156   int __asan_get_report_access_type();
157   SANITIZER_INTERFACE_ATTRIBUTE
158   uptr __asan_get_report_access_size();
159   SANITIZER_INTERFACE_ATTRIBUTE
160   const char * __asan_get_report_description();
161 
162   SANITIZER_INTERFACE_ATTRIBUTE
163   const char * __asan_locate_address(uptr addr, char *name, uptr name_size,
164                                      uptr *region_address, uptr *region_size);
165 
166   SANITIZER_INTERFACE_ATTRIBUTE
167   uptr __asan_get_alloc_stack(uptr addr, uptr *trace, uptr size,
168                               u32 *thread_id);
169 
170   SANITIZER_INTERFACE_ATTRIBUTE
171   uptr __asan_get_free_stack(uptr addr, uptr *trace, uptr size,
172                              u32 *thread_id);
173 
174   SANITIZER_INTERFACE_ATTRIBUTE
175   void __asan_get_shadow_mapping(uptr *shadow_scale, uptr *shadow_offset);
176 
177   SANITIZER_INTERFACE_ATTRIBUTE
178   void __asan_report_error(uptr pc, uptr bp, uptr sp,
179                            uptr addr, int is_write, uptr access_size, u32 exp);
180 
181   SANITIZER_INTERFACE_ATTRIBUTE
182   void __asan_set_death_callback(void (*callback)(void));
183   SANITIZER_INTERFACE_ATTRIBUTE
184   void __asan_set_error_report_callback(void (*callback)(const char*));
185 
186   SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
187   void __asan_on_error();
188 
189   SANITIZER_INTERFACE_ATTRIBUTE void __asan_print_accumulated_stats();
190 
191   SANITIZER_INTERFACE_ATTRIBUTE
192   const char *__asan_default_options();
193 
194   SANITIZER_INTERFACE_ATTRIBUTE
195   extern uptr __asan_shadow_memory_dynamic_address;
196 
197   // Global flag, copy of ASAN_OPTIONS=detect_stack_use_after_return
198   SANITIZER_INTERFACE_ATTRIBUTE
199   extern int __asan_option_detect_stack_use_after_return;
200 
201   SANITIZER_INTERFACE_ATTRIBUTE
202   extern uptr *__asan_test_only_reported_buggy_pointer;
203 
204   SANITIZER_INTERFACE_ATTRIBUTE void __asan_load1(uptr p);
205   SANITIZER_INTERFACE_ATTRIBUTE void __asan_load2(uptr p);
206   SANITIZER_INTERFACE_ATTRIBUTE void __asan_load4(uptr p);
207   SANITIZER_INTERFACE_ATTRIBUTE void __asan_load8(uptr p);
208   SANITIZER_INTERFACE_ATTRIBUTE void __asan_load16(uptr p);
209   SANITIZER_INTERFACE_ATTRIBUTE void __asan_store1(uptr p);
210   SANITIZER_INTERFACE_ATTRIBUTE void __asan_store2(uptr p);
211   SANITIZER_INTERFACE_ATTRIBUTE void __asan_store4(uptr p);
212   SANITIZER_INTERFACE_ATTRIBUTE void __asan_store8(uptr p);
213   SANITIZER_INTERFACE_ATTRIBUTE void __asan_store16(uptr p);
214   SANITIZER_INTERFACE_ATTRIBUTE void __asan_loadN(uptr p, uptr size);
215   SANITIZER_INTERFACE_ATTRIBUTE void __asan_storeN(uptr p, uptr size);
216 
217   SANITIZER_INTERFACE_ATTRIBUTE void __asan_load1_noabort(uptr p);
218   SANITIZER_INTERFACE_ATTRIBUTE void __asan_load2_noabort(uptr p);
219   SANITIZER_INTERFACE_ATTRIBUTE void __asan_load4_noabort(uptr p);
220   SANITIZER_INTERFACE_ATTRIBUTE void __asan_load8_noabort(uptr p);
221   SANITIZER_INTERFACE_ATTRIBUTE void __asan_load16_noabort(uptr p);
222   SANITIZER_INTERFACE_ATTRIBUTE void __asan_store1_noabort(uptr p);
223   SANITIZER_INTERFACE_ATTRIBUTE void __asan_store2_noabort(uptr p);
224   SANITIZER_INTERFACE_ATTRIBUTE void __asan_store4_noabort(uptr p);
225   SANITIZER_INTERFACE_ATTRIBUTE void __asan_store8_noabort(uptr p);
226   SANITIZER_INTERFACE_ATTRIBUTE void __asan_store16_noabort(uptr p);
227   SANITIZER_INTERFACE_ATTRIBUTE void __asan_loadN_noabort(uptr p, uptr size);
228   SANITIZER_INTERFACE_ATTRIBUTE void __asan_storeN_noabort(uptr p, uptr size);
229 
230   SANITIZER_INTERFACE_ATTRIBUTE void __asan_exp_load1(uptr p, u32 exp);
231   SANITIZER_INTERFACE_ATTRIBUTE void __asan_exp_load2(uptr p, u32 exp);
232   SANITIZER_INTERFACE_ATTRIBUTE void __asan_exp_load4(uptr p, u32 exp);
233   SANITIZER_INTERFACE_ATTRIBUTE void __asan_exp_load8(uptr p, u32 exp);
234   SANITIZER_INTERFACE_ATTRIBUTE void __asan_exp_load16(uptr p, u32 exp);
235   SANITIZER_INTERFACE_ATTRIBUTE void __asan_exp_store1(uptr p, u32 exp);
236   SANITIZER_INTERFACE_ATTRIBUTE void __asan_exp_store2(uptr p, u32 exp);
237   SANITIZER_INTERFACE_ATTRIBUTE void __asan_exp_store4(uptr p, u32 exp);
238   SANITIZER_INTERFACE_ATTRIBUTE void __asan_exp_store8(uptr p, u32 exp);
239   SANITIZER_INTERFACE_ATTRIBUTE void __asan_exp_store16(uptr p, u32 exp);
240   SANITIZER_INTERFACE_ATTRIBUTE void __asan_exp_loadN(uptr p, uptr size,
241                                                       u32 exp);
242   SANITIZER_INTERFACE_ATTRIBUTE void __asan_exp_storeN(uptr p, uptr size,
243                                                        u32 exp);
244 
245   SANITIZER_INTERFACE_ATTRIBUTE
246       void* __asan_memcpy(void *dst, const void *src, uptr size);
247   SANITIZER_INTERFACE_ATTRIBUTE
248       void* __asan_memset(void *s, int c, uptr n);
249   SANITIZER_INTERFACE_ATTRIBUTE
250       void* __asan_memmove(void* dest, const void* src, uptr n);
251 
252   SANITIZER_INTERFACE_ATTRIBUTE
253   void __asan_poison_cxx_array_cookie(uptr p);
254   SANITIZER_INTERFACE_ATTRIBUTE
255   uptr __asan_load_cxx_array_cookie(uptr *p);
256   SANITIZER_INTERFACE_ATTRIBUTE
257   void __asan_poison_intra_object_redzone(uptr p, uptr size);
258   SANITIZER_INTERFACE_ATTRIBUTE
259   void __asan_unpoison_intra_object_redzone(uptr p, uptr size);
260   SANITIZER_INTERFACE_ATTRIBUTE
261   void __asan_alloca_poison(uptr addr, uptr size);
262   SANITIZER_INTERFACE_ATTRIBUTE
263   void __asan_allocas_unpoison(uptr top, uptr bottom);
264 
265   SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
266   const char* __asan_default_suppressions();
267 
268   SANITIZER_INTERFACE_ATTRIBUTE void __asan_handle_vfork(void *sp);
269 
270   SANITIZER_INTERFACE_ATTRIBUTE int __asan_update_allocation_context(
271       void *addr);
272 }  // extern "C"
273 
274 #endif  // ASAN_INTERFACE_INTERNAL_H
275