xref: /freebsd/contrib/llvm-project/compiler-rt/lib/asan/asan_interface_internal.h (revision d9a42747950146bf03cda7f6e25d219253f8a57a)
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_f1(uptr addr, uptr size);
94   SANITIZER_INTERFACE_ATTRIBUTE
95   void __asan_set_shadow_f2(uptr addr, uptr size);
96   SANITIZER_INTERFACE_ATTRIBUTE
97   void __asan_set_shadow_f3(uptr addr, uptr size);
98   SANITIZER_INTERFACE_ATTRIBUTE
99   void __asan_set_shadow_f5(uptr addr, uptr size);
100   SANITIZER_INTERFACE_ATTRIBUTE
101   void __asan_set_shadow_f8(uptr addr, uptr size);
102 
103   // These two functions are used by instrumented code in the
104   // use-after-scope mode. They mark memory for local variables as
105   // unaddressable when they leave scope and addressable before the
106   // function exits.
107   SANITIZER_INTERFACE_ATTRIBUTE
108   void __asan_poison_stack_memory(uptr addr, uptr size);
109   SANITIZER_INTERFACE_ATTRIBUTE
110   void __asan_unpoison_stack_memory(uptr addr, uptr size);
111 
112   // Performs cleanup before a NoReturn function. Must be called before things
113   // like _exit and execl to avoid false positives on stack.
114   SANITIZER_INTERFACE_ATTRIBUTE void __asan_handle_no_return();
115 
116   SANITIZER_INTERFACE_ATTRIBUTE
117   void __asan_poison_memory_region(void const volatile *addr, uptr size);
118   SANITIZER_INTERFACE_ATTRIBUTE
119   void __asan_unpoison_memory_region(void const volatile *addr, uptr size);
120 
121   SANITIZER_INTERFACE_ATTRIBUTE
122   int __asan_address_is_poisoned(void const volatile *addr);
123 
124   SANITIZER_INTERFACE_ATTRIBUTE
125   uptr __asan_region_is_poisoned(uptr beg, uptr size);
126 
127   SANITIZER_INTERFACE_ATTRIBUTE
128   void __asan_describe_address(uptr addr);
129 
130   SANITIZER_INTERFACE_ATTRIBUTE
131   int __asan_report_present();
132 
133   SANITIZER_INTERFACE_ATTRIBUTE
134   uptr __asan_get_report_pc();
135   SANITIZER_INTERFACE_ATTRIBUTE
136   uptr __asan_get_report_bp();
137   SANITIZER_INTERFACE_ATTRIBUTE
138   uptr __asan_get_report_sp();
139   SANITIZER_INTERFACE_ATTRIBUTE
140   uptr __asan_get_report_address();
141   SANITIZER_INTERFACE_ATTRIBUTE
142   int __asan_get_report_access_type();
143   SANITIZER_INTERFACE_ATTRIBUTE
144   uptr __asan_get_report_access_size();
145   SANITIZER_INTERFACE_ATTRIBUTE
146   const char * __asan_get_report_description();
147 
148   SANITIZER_INTERFACE_ATTRIBUTE
149   const char * __asan_locate_address(uptr addr, char *name, uptr name_size,
150                                      uptr *region_address, uptr *region_size);
151 
152   SANITIZER_INTERFACE_ATTRIBUTE
153   uptr __asan_get_alloc_stack(uptr addr, uptr *trace, uptr size,
154                               u32 *thread_id);
155 
156   SANITIZER_INTERFACE_ATTRIBUTE
157   uptr __asan_get_free_stack(uptr addr, uptr *trace, uptr size,
158                              u32 *thread_id);
159 
160   SANITIZER_INTERFACE_ATTRIBUTE
161   void __asan_get_shadow_mapping(uptr *shadow_scale, uptr *shadow_offset);
162 
163   SANITIZER_INTERFACE_ATTRIBUTE
164   void __asan_report_error(uptr pc, uptr bp, uptr sp,
165                            uptr addr, int is_write, uptr access_size, u32 exp);
166 
167   SANITIZER_INTERFACE_ATTRIBUTE
168   void __asan_set_death_callback(void (*callback)(void));
169   SANITIZER_INTERFACE_ATTRIBUTE
170   void __asan_set_error_report_callback(void (*callback)(const char*));
171 
172   SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
173   void __asan_on_error();
174 
175   SANITIZER_INTERFACE_ATTRIBUTE void __asan_print_accumulated_stats();
176 
177   SANITIZER_INTERFACE_ATTRIBUTE
178   const char *__asan_default_options();
179 
180   SANITIZER_INTERFACE_ATTRIBUTE
181   extern uptr __asan_shadow_memory_dynamic_address;
182 
183   // Global flag, copy of ASAN_OPTIONS=detect_stack_use_after_return
184   SANITIZER_INTERFACE_ATTRIBUTE
185   extern int __asan_option_detect_stack_use_after_return;
186 
187   SANITIZER_INTERFACE_ATTRIBUTE
188   extern uptr *__asan_test_only_reported_buggy_pointer;
189 
190   SANITIZER_INTERFACE_ATTRIBUTE void __asan_load1(uptr p);
191   SANITIZER_INTERFACE_ATTRIBUTE void __asan_load2(uptr p);
192   SANITIZER_INTERFACE_ATTRIBUTE void __asan_load4(uptr p);
193   SANITIZER_INTERFACE_ATTRIBUTE void __asan_load8(uptr p);
194   SANITIZER_INTERFACE_ATTRIBUTE void __asan_load16(uptr p);
195   SANITIZER_INTERFACE_ATTRIBUTE void __asan_store1(uptr p);
196   SANITIZER_INTERFACE_ATTRIBUTE void __asan_store2(uptr p);
197   SANITIZER_INTERFACE_ATTRIBUTE void __asan_store4(uptr p);
198   SANITIZER_INTERFACE_ATTRIBUTE void __asan_store8(uptr p);
199   SANITIZER_INTERFACE_ATTRIBUTE void __asan_store16(uptr p);
200   SANITIZER_INTERFACE_ATTRIBUTE void __asan_loadN(uptr p, uptr size);
201   SANITIZER_INTERFACE_ATTRIBUTE void __asan_storeN(uptr p, uptr size);
202 
203   SANITIZER_INTERFACE_ATTRIBUTE void __asan_load1_noabort(uptr p);
204   SANITIZER_INTERFACE_ATTRIBUTE void __asan_load2_noabort(uptr p);
205   SANITIZER_INTERFACE_ATTRIBUTE void __asan_load4_noabort(uptr p);
206   SANITIZER_INTERFACE_ATTRIBUTE void __asan_load8_noabort(uptr p);
207   SANITIZER_INTERFACE_ATTRIBUTE void __asan_load16_noabort(uptr p);
208   SANITIZER_INTERFACE_ATTRIBUTE void __asan_store1_noabort(uptr p);
209   SANITIZER_INTERFACE_ATTRIBUTE void __asan_store2_noabort(uptr p);
210   SANITIZER_INTERFACE_ATTRIBUTE void __asan_store4_noabort(uptr p);
211   SANITIZER_INTERFACE_ATTRIBUTE void __asan_store8_noabort(uptr p);
212   SANITIZER_INTERFACE_ATTRIBUTE void __asan_store16_noabort(uptr p);
213   SANITIZER_INTERFACE_ATTRIBUTE void __asan_loadN_noabort(uptr p, uptr size);
214   SANITIZER_INTERFACE_ATTRIBUTE void __asan_storeN_noabort(uptr p, uptr size);
215 
216   SANITIZER_INTERFACE_ATTRIBUTE void __asan_exp_load1(uptr p, u32 exp);
217   SANITIZER_INTERFACE_ATTRIBUTE void __asan_exp_load2(uptr p, u32 exp);
218   SANITIZER_INTERFACE_ATTRIBUTE void __asan_exp_load4(uptr p, u32 exp);
219   SANITIZER_INTERFACE_ATTRIBUTE void __asan_exp_load8(uptr p, u32 exp);
220   SANITIZER_INTERFACE_ATTRIBUTE void __asan_exp_load16(uptr p, u32 exp);
221   SANITIZER_INTERFACE_ATTRIBUTE void __asan_exp_store1(uptr p, u32 exp);
222   SANITIZER_INTERFACE_ATTRIBUTE void __asan_exp_store2(uptr p, u32 exp);
223   SANITIZER_INTERFACE_ATTRIBUTE void __asan_exp_store4(uptr p, u32 exp);
224   SANITIZER_INTERFACE_ATTRIBUTE void __asan_exp_store8(uptr p, u32 exp);
225   SANITIZER_INTERFACE_ATTRIBUTE void __asan_exp_store16(uptr p, u32 exp);
226   SANITIZER_INTERFACE_ATTRIBUTE void __asan_exp_loadN(uptr p, uptr size,
227                                                       u32 exp);
228   SANITIZER_INTERFACE_ATTRIBUTE void __asan_exp_storeN(uptr p, uptr size,
229                                                        u32 exp);
230 
231   SANITIZER_INTERFACE_ATTRIBUTE
232       void* __asan_memcpy(void *dst, const void *src, uptr size);
233   SANITIZER_INTERFACE_ATTRIBUTE
234       void* __asan_memset(void *s, int c, uptr n);
235   SANITIZER_INTERFACE_ATTRIBUTE
236       void* __asan_memmove(void* dest, const void* src, uptr n);
237 
238   SANITIZER_INTERFACE_ATTRIBUTE
239   void __asan_poison_cxx_array_cookie(uptr p);
240   SANITIZER_INTERFACE_ATTRIBUTE
241   uptr __asan_load_cxx_array_cookie(uptr *p);
242   SANITIZER_INTERFACE_ATTRIBUTE
243   void __asan_poison_intra_object_redzone(uptr p, uptr size);
244   SANITIZER_INTERFACE_ATTRIBUTE
245   void __asan_unpoison_intra_object_redzone(uptr p, uptr size);
246   SANITIZER_INTERFACE_ATTRIBUTE
247   void __asan_alloca_poison(uptr addr, uptr size);
248   SANITIZER_INTERFACE_ATTRIBUTE
249   void __asan_allocas_unpoison(uptr top, uptr bottom);
250 
251   SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
252   const char* __asan_default_suppressions();
253 
254   SANITIZER_INTERFACE_ATTRIBUTE void __asan_handle_vfork(void *sp);
255 
256   SANITIZER_INTERFACE_ATTRIBUTE int __asan_update_allocation_context(
257       void *addr);
258 }  // extern "C"
259 
260 #endif  // ASAN_INTERFACE_INTERNAL_H
261