xref: /freebsd/contrib/llvm-project/compiler-rt/lib/asan_abi/asan_abi_shim.cpp (revision b64c5a0ace59af62eff52bfe110a521dc73c937b)
1 //===-asan_abi_shim.cpp - ASan Stable ABI Shim-----------------------------===//
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 #include "../asan/asan_interface_internal.h"
10 #include "asan_abi.h"
11 #include <assert.h>
12 
13 extern "C" {
14 // Functions concerning instrumented global variables
15 void __asan_register_image_globals(uptr *flag) {
16   __asan_abi_register_image_globals();
17 }
18 void __asan_unregister_image_globals(uptr *flag) {
19   __asan_abi_unregister_image_globals();
20 }
21 void __asan_register_elf_globals(uptr *flag, void *start, void *stop) {
22   bool bFlag = *flag;
23   __asan_abi_register_elf_globals(&bFlag, start, stop);
24   *flag = bFlag;
25 }
26 void __asan_unregister_elf_globals(uptr *flag, void *start, void *stop) {
27   bool bFlag = *flag;
28   __asan_abi_unregister_elf_globals(&bFlag, start, stop);
29   *flag = bFlag;
30 }
31 void __asan_register_globals(__asan_global *globals, uptr n) {
32   __asan_abi_register_globals(globals, n);
33 }
34 void __asan_unregister_globals(__asan_global *globals, uptr n) {
35   __asan_abi_unregister_globals(globals, n);
36 }
37 
38 // Functions concerning dynamic library initialization
39 void __asan_before_dynamic_init(const char *module_name) {
40   __asan_abi_before_dynamic_init(module_name);
41 }
42 void __asan_after_dynamic_init(void) { __asan_abi_after_dynamic_init(); }
43 
44 // Functions concerning block memory destinations
45 void *__asan_memcpy(void *dst, const void *src, uptr size) {
46   return __asan_abi_memcpy(dst, src, size);
47 }
48 void *__asan_memset(void *s, int c, uptr n) {
49   return __asan_abi_memset(s, c, n);
50 }
51 void *__asan_memmove(void *dest, const void *src, uptr n) {
52   return __asan_abi_memmove(dest, src, n);
53 }
54 
55 // Functions concerning RTL startup and initialization
56 void __asan_init(void) {
57   static_assert(sizeof(uptr) == 8 || sizeof(uptr) == 4);
58   static_assert(sizeof(u64) == 8);
59   static_assert(sizeof(u32) == 4);
60 
61   __asan_abi_init();
62 }
63 
64 void __asan_handle_no_return(void) { __asan_abi_handle_no_return(); }
65 
66 // Variables concerning RTL state. These provisionally exist for completeness
67 // but will likely move into the Stable ABI implementation and not in the shim.
68 uptr __asan_shadow_memory_dynamic_address = (uptr)0L;
69 int __asan_option_detect_stack_use_after_return = 1;
70 
71 // Functions concerning memory load and store reporting
72 void __asan_report_load1(uptr addr) {
73   __asan_abi_report_load_n((void *)addr, 1, true);
74 }
75 void __asan_report_load2(uptr addr) {
76   __asan_abi_report_load_n((void *)addr, 2, true);
77 }
78 void __asan_report_load4(uptr addr) {
79   __asan_abi_report_load_n((void *)addr, 4, true);
80 }
81 void __asan_report_load8(uptr addr) {
82   __asan_abi_report_load_n((void *)addr, 8, true);
83 }
84 void __asan_report_load16(uptr addr) {
85   __asan_abi_report_load_n((void *)addr, 16, true);
86 }
87 void __asan_report_load_n(uptr addr, uptr size) {
88   __asan_abi_report_load_n((void *)addr, size, true);
89 }
90 void __asan_report_store1(uptr addr) {
91   __asan_abi_report_store_n((void *)addr, 1, true);
92 }
93 void __asan_report_store2(uptr addr) {
94   __asan_abi_report_store_n((void *)addr, 2, true);
95 }
96 void __asan_report_store4(uptr addr) {
97   __asan_abi_report_store_n((void *)addr, 4, true);
98 }
99 void __asan_report_store8(uptr addr) {
100   __asan_abi_report_store_n((void *)addr, 8, true);
101 }
102 void __asan_report_store16(uptr addr) {
103   __asan_abi_report_store_n((void *)addr, 16, true);
104 }
105 void __asan_report_store_n(uptr addr, uptr size) {
106   __asan_abi_report_store_n((void *)addr, size, true);
107 }
108 
109 // Functions concerning memory load and store reporting (experimental variants)
110 void __asan_report_exp_load1(uptr addr, u32 exp) {
111   __asan_abi_report_exp_load_n((void *)addr, exp, 1, true);
112 }
113 void __asan_report_exp_load2(uptr addr, u32 exp) {
114   __asan_abi_report_exp_load_n((void *)addr, exp, 2, true);
115 }
116 void __asan_report_exp_load4(uptr addr, u32 exp) {
117   __asan_abi_report_exp_load_n((void *)addr, exp, 4, true);
118 }
119 void __asan_report_exp_load8(uptr addr, u32 exp) {
120   __asan_abi_report_exp_load_n((void *)addr, exp, 8, true);
121 }
122 void __asan_report_exp_load16(uptr addr, u32 exp) {
123   __asan_abi_report_exp_load_n((void *)addr, exp, 16, true);
124 }
125 void __asan_report_exp_load_n(uptr addr, uptr size, u32 exp) {
126   __asan_abi_report_exp_load_n((void *)addr, size, exp, true);
127 }
128 void __asan_report_exp_store1(uptr addr, u32 exp) {
129   __asan_abi_report_exp_store_n((void *)addr, exp, 1, true);
130 }
131 void __asan_report_exp_store2(uptr addr, u32 exp) {
132   __asan_abi_report_exp_store_n((void *)addr, exp, 2, true);
133 }
134 void __asan_report_exp_store4(uptr addr, u32 exp) {
135   __asan_abi_report_exp_store_n((void *)addr, exp, 4, true);
136 }
137 void __asan_report_exp_store8(uptr addr, u32 exp) {
138   __asan_abi_report_exp_store_n((void *)addr, exp, 8, true);
139 }
140 void __asan_report_exp_store16(uptr addr, u32 exp) {
141   __asan_abi_report_exp_store_n((void *)addr, exp, 16, true);
142 }
143 void __asan_report_exp_store_n(uptr addr, uptr size, u32 exp) {
144   __asan_abi_report_exp_store_n((void *)addr, size, exp, true);
145 }
146 
147 // Functions concerning memory load and store reporting (noabort variants)
148 void __asan_report_load1_noabort(uptr addr) {
149   __asan_abi_report_load_n((void *)addr, 1, false);
150 }
151 void __asan_report_load2_noabort(uptr addr) {
152   __asan_abi_report_load_n((void *)addr, 2, false);
153 }
154 void __asan_report_load4_noabort(uptr addr) {
155   __asan_abi_report_load_n((void *)addr, 4, false);
156 }
157 void __asan_report_load8_noabort(uptr addr) {
158   __asan_abi_report_load_n((void *)addr, 8, false);
159 }
160 void __asan_report_load16_noabort(uptr addr) {
161   __asan_abi_report_load_n((void *)addr, 16, false);
162 }
163 void __asan_report_load_n_noabort(uptr addr, uptr size) {
164   __asan_abi_report_load_n((void *)addr, size, false);
165 }
166 void __asan_report_store1_noabort(uptr addr) {
167   __asan_abi_report_store_n((void *)addr, 1, false);
168 }
169 void __asan_report_store2_noabort(uptr addr) {
170   __asan_abi_report_store_n((void *)addr, 2, false);
171 }
172 void __asan_report_store4_noabort(uptr addr) {
173   __asan_abi_report_store_n((void *)addr, 4, false);
174 }
175 void __asan_report_store8_noabort(uptr addr) {
176   __asan_abi_report_store_n((void *)addr, 8, false);
177 }
178 void __asan_report_store16_noabort(uptr addr) {
179   __asan_abi_report_store_n((void *)addr, 16, false);
180 }
181 void __asan_report_store_n_noabort(uptr addr, uptr size) {
182   __asan_abi_report_store_n((void *)addr, size, false);
183 }
184 
185 // Functions concerning memory load and store
186 void __asan_load1(uptr addr) { __asan_abi_load_n((void *)addr, 1, true); }
187 void __asan_load2(uptr addr) { __asan_abi_load_n((void *)addr, 2, true); }
188 void __asan_load4(uptr addr) { __asan_abi_load_n((void *)addr, 4, true); }
189 void __asan_load8(uptr addr) { __asan_abi_load_n((void *)addr, 8, true); }
190 void __asan_load16(uptr addr) { __asan_abi_load_n((void *)addr, 16, true); }
191 void __asan_loadN(uptr addr, uptr size) {
192   __asan_abi_load_n((void *)addr, size, true);
193 }
194 void __asan_store1(uptr addr) { __asan_abi_store_n((void *)addr, 1, true); }
195 void __asan_store2(uptr addr) { __asan_abi_store_n((void *)addr, 2, true); }
196 void __asan_store4(uptr addr) { __asan_abi_store_n((void *)addr, 4, true); }
197 void __asan_store8(uptr addr) { __asan_abi_store_n((void *)addr, 8, true); }
198 void __asan_store16(uptr addr) { __asan_abi_store_n((void *)addr, 16, true); }
199 void __asan_storeN(uptr addr, uptr size) {
200   __asan_abi_store_n((void *)addr, size, true);
201 }
202 
203 // Functions concerning memory load and store (experimental variants)
204 void __asan_exp_load1(uptr addr, u32 exp) {
205   __asan_abi_exp_load_n((void *)addr, 1, exp, true);
206 }
207 void __asan_exp_load2(uptr addr, u32 exp) {
208   __asan_abi_exp_load_n((void *)addr, 2, exp, true);
209 }
210 void __asan_exp_load4(uptr addr, u32 exp) {
211   __asan_abi_exp_load_n((void *)addr, 4, exp, true);
212 }
213 void __asan_exp_load8(uptr addr, u32 exp) {
214   __asan_abi_exp_load_n((void *)addr, 8, exp, true);
215 }
216 void __asan_exp_load16(uptr addr, u32 exp) {
217   __asan_abi_exp_load_n((void *)addr, 16, exp, true);
218 }
219 void __asan_exp_loadN(uptr addr, uptr size, u32 exp) {
220   __asan_abi_exp_load_n((void *)addr, size, exp, true);
221 }
222 void __asan_exp_store1(uptr addr, u32 exp) {
223   __asan_abi_exp_store_n((void *)addr, 1, exp, true);
224 }
225 void __asan_exp_store2(uptr addr, u32 exp) {
226   __asan_abi_exp_store_n((void *)addr, 2, exp, true);
227 }
228 void __asan_exp_store4(uptr addr, u32 exp) {
229   __asan_abi_exp_store_n((void *)addr, 4, exp, true);
230 }
231 void __asan_exp_store8(uptr addr, u32 exp) {
232   __asan_abi_exp_store_n((void *)addr, 8, exp, true);
233 }
234 void __asan_exp_store16(uptr addr, u32 exp) {
235   __asan_abi_exp_store_n((void *)addr, 16, exp, true);
236 }
237 void __asan_exp_storeN(uptr addr, uptr size, u32 exp) {
238   __asan_abi_exp_store_n((void *)addr, size, exp, true);
239 }
240 
241 // Functions concerning memory load and store (noabort variants)
242 void __asan_load1_noabort(uptr addr) {
243   __asan_abi_load_n((void *)addr, 1, false);
244 }
245 void __asan_load2_noabort(uptr addr) {
246   __asan_abi_load_n((void *)addr, 2, false);
247 }
248 void __asan_load4_noabort(uptr addr) {
249   __asan_abi_load_n((void *)addr, 4, false);
250 }
251 void __asan_load8_noabort(uptr addr) {
252   __asan_abi_load_n((void *)addr, 8, false);
253 }
254 void __asan_load16_noabort(uptr addr) {
255   __asan_abi_load_n((void *)addr, 16, false);
256 }
257 void __asan_loadN_noabort(uptr addr, uptr size) {
258   __asan_abi_load_n((void *)addr, size, false);
259 }
260 void __asan_store1_noabort(uptr addr) {
261   __asan_abi_store_n((void *)addr, 1, false);
262 }
263 void __asan_store2_noabort(uptr addr) {
264   __asan_abi_store_n((void *)addr, 2, false);
265 }
266 void __asan_store4_noabort(uptr addr) {
267   __asan_abi_store_n((void *)addr, 4, false);
268 }
269 void __asan_store8_noabort(uptr addr) {
270   __asan_abi_store_n((void *)addr, 8, false);
271 }
272 void __asan_store16_noabort(uptr addr) {
273   __asan_abi_store_n((void *)addr, 16, false);
274 }
275 void __asan_storeN_noabort(uptr addr, uptr size) {
276   __asan_abi_store_n((void *)addr, size, false);
277 }
278 
279 // Functions concerning query about whether memory is poisoned
280 int __asan_address_is_poisoned(void const volatile *addr) {
281   return __asan_abi_address_is_poisoned(addr);
282 }
283 uptr __asan_region_is_poisoned(uptr beg, uptr size) {
284   return (uptr)__asan_abi_region_is_poisoned((void *)beg, size);
285 }
286 
287 // Functions concerning the poisoning of memory
288 void __asan_poison_memory_region(void const volatile *addr, uptr size) {
289   __asan_abi_poison_memory_region(addr, size);
290 }
291 void __asan_unpoison_memory_region(void const volatile *addr, uptr size) {
292   __asan_abi_unpoison_memory_region(addr, size);
293 }
294 
295 // Functions concerning the partial poisoning of memory
296 void __asan_set_shadow_00(uptr addr, uptr size) {
297   __asan_abi_set_shadow_xx_n((void *)addr, 0x00, size);
298 }
299 void __asan_set_shadow_01(uptr addr, uptr size) {
300   __asan_abi_set_shadow_xx_n((void *)addr, 0x01, size);
301 }
302 void __asan_set_shadow_02(uptr addr, uptr size) {
303   __asan_abi_set_shadow_xx_n((void *)addr, 0x02, size);
304 }
305 void __asan_set_shadow_03(uptr addr, uptr size) {
306   __asan_abi_set_shadow_xx_n((void *)addr, 0x03, size);
307 }
308 void __asan_set_shadow_04(uptr addr, uptr size) {
309   __asan_abi_set_shadow_xx_n((void *)addr, 0x04, size);
310 }
311 void __asan_set_shadow_05(uptr addr, uptr size) {
312   __asan_abi_set_shadow_xx_n((void *)addr, 0x05, size);
313 }
314 void __asan_set_shadow_06(uptr addr, uptr size) {
315   __asan_abi_set_shadow_xx_n((void *)addr, 0x06, size);
316 }
317 void __asan_set_shadow_07(uptr addr, uptr size) {
318   __asan_abi_set_shadow_xx_n((void *)addr, 0x07, size);
319 }
320 void __asan_set_shadow_f1(uptr addr, uptr size) {
321   __asan_abi_set_shadow_xx_n((void *)addr, 0xf1, size);
322 }
323 void __asan_set_shadow_f2(uptr addr, uptr size) {
324   __asan_abi_set_shadow_xx_n((void *)addr, 0xf2, size);
325 }
326 void __asan_set_shadow_f3(uptr addr, uptr size) {
327   __asan_abi_set_shadow_xx_n((void *)addr, 0xf3, size);
328 }
329 void __asan_set_shadow_f5(uptr addr, uptr size) {
330   __asan_abi_set_shadow_xx_n((void *)addr, 0xf5, size);
331 }
332 void __asan_set_shadow_f8(uptr addr, uptr size) {
333   __asan_abi_set_shadow_xx_n((void *)addr, 0xf8, size);
334 }
335 
336 // Functions concerning stack poisoning
337 void __asan_poison_stack_memory(uptr addr, uptr size) {
338   __asan_abi_poison_stack_memory((void *)addr, size);
339 }
340 void __asan_unpoison_stack_memory(uptr addr, uptr size) {
341   __asan_abi_unpoison_stack_memory((void *)addr, size);
342 }
343 
344 // Functions concerning redzone poisoning
345 void __asan_poison_intra_object_redzone(uptr p, uptr size) {
346   __asan_abi_poison_intra_object_redzone((void *)p, size);
347 }
348 void __asan_unpoison_intra_object_redzone(uptr p, uptr size) {
349   __asan_abi_unpoison_intra_object_redzone((void *)p, size);
350 }
351 
352 // Functions concerning array cookie poisoning
353 void __asan_poison_cxx_array_cookie(uptr p) {
354   __asan_abi_poison_cxx_array_cookie((void *)p);
355 }
356 uptr __asan_load_cxx_array_cookie(uptr *p) {
357   return (uptr)__asan_abi_load_cxx_array_cookie((void **)p);
358 }
359 
360 // Functions concerning fake stacks
361 void *__asan_get_current_fake_stack(void) {
362   return __asan_abi_get_current_fake_stack();
363 }
364 void *__asan_addr_is_in_fake_stack(void *fake_stack, void *addr, void **beg,
365                                    void **end) {
366   return __asan_abi_addr_is_in_fake_stack(fake_stack, addr, beg, end);
367 }
368 
369 // Functions concerning poisoning and unpoisoning fake stack alloca
370 void __asan_alloca_poison(uptr addr, uptr size) {
371   __asan_abi_alloca_poison((void *)addr, size);
372 }
373 void __asan_allocas_unpoison(uptr top, uptr bottom) {
374   __asan_abi_allocas_unpoison((void *)top, (void *)bottom);
375 }
376 
377 // Functions concerning fake stack malloc
378 uptr __asan_stack_malloc_0(uptr size) {
379   return (uptr)__asan_abi_stack_malloc_n(0, size);
380 }
381 uptr __asan_stack_malloc_1(uptr size) {
382   return (uptr)__asan_abi_stack_malloc_n(1, size);
383 }
384 uptr __asan_stack_malloc_2(uptr size) {
385   return (uptr)__asan_abi_stack_malloc_n(2, size);
386 }
387 uptr __asan_stack_malloc_3(uptr size) {
388   return (uptr)__asan_abi_stack_malloc_n(3, size);
389 }
390 uptr __asan_stack_malloc_4(uptr size) {
391   return (uptr)__asan_abi_stack_malloc_n(4, size);
392 }
393 uptr __asan_stack_malloc_5(uptr size) {
394   return (uptr)__asan_abi_stack_malloc_n(5, size);
395 }
396 uptr __asan_stack_malloc_6(uptr size) {
397   return (uptr)__asan_abi_stack_malloc_n(6, size);
398 }
399 uptr __asan_stack_malloc_7(uptr size) {
400   return (uptr)__asan_abi_stack_malloc_n(7, size);
401 }
402 uptr __asan_stack_malloc_8(uptr size) {
403   return (uptr)__asan_abi_stack_malloc_n(8, size);
404 }
405 uptr __asan_stack_malloc_9(uptr size) {
406   return (uptr)__asan_abi_stack_malloc_n(9, size);
407 }
408 uptr __asan_stack_malloc_10(uptr size) {
409   return (uptr)__asan_abi_stack_malloc_n(10, size);
410 }
411 
412 // Functions concerning fake stack malloc (always variants)
413 uptr __asan_stack_malloc_always_0(uptr size) {
414   return (uptr)__asan_abi_stack_malloc_always_n(0, size);
415 }
416 uptr __asan_stack_malloc_always_1(uptr size) {
417   return (uptr)__asan_abi_stack_malloc_always_n(1, size);
418 }
419 uptr __asan_stack_malloc_always_2(uptr size) {
420   return (uptr)__asan_abi_stack_malloc_always_n(2, size);
421 }
422 uptr __asan_stack_malloc_always_3(uptr size) {
423   return (uptr)__asan_abi_stack_malloc_always_n(3, size);
424 }
425 uptr __asan_stack_malloc_always_4(uptr size) {
426   return (uptr)__asan_abi_stack_malloc_always_n(4, size);
427 }
428 uptr __asan_stack_malloc_always_5(uptr size) {
429   return (uptr)__asan_abi_stack_malloc_always_n(5, size);
430 }
431 uptr __asan_stack_malloc_always_6(uptr size) {
432   return (uptr)__asan_abi_stack_malloc_always_n(6, size);
433 }
434 uptr __asan_stack_malloc_always_7(uptr size) {
435   return (uptr)__asan_abi_stack_malloc_always_n(7, size);
436 }
437 uptr __asan_stack_malloc_always_8(uptr size) {
438   return (uptr)__asan_abi_stack_malloc_always_n(8, size);
439 }
440 uptr __asan_stack_malloc_always_9(uptr size) {
441   return (uptr)__asan_abi_stack_malloc_always_n(9, size);
442 }
443 uptr __asan_stack_malloc_always_10(uptr size) {
444   return (uptr)__asan_abi_stack_malloc_always_n(10, size);
445 }
446 
447 // Functions concerning fake stack free
448 void __asan_stack_free_0(uptr ptr, uptr size) {
449   __asan_abi_stack_free_n(0, (void *)ptr, size);
450 }
451 void __asan_stack_free_1(uptr ptr, uptr size) {
452   __asan_abi_stack_free_n(1, (void *)ptr, size);
453 }
454 void __asan_stack_free_2(uptr ptr, uptr size) {
455   __asan_abi_stack_free_n(2, (void *)ptr, size);
456 }
457 void __asan_stack_free_3(uptr ptr, uptr size) {
458   __asan_abi_stack_free_n(3, (void *)ptr, size);
459 }
460 void __asan_stack_free_4(uptr ptr, uptr size) {
461   __asan_abi_stack_free_n(4, (void *)ptr, size);
462 }
463 void __asan_stack_free_5(uptr ptr, uptr size) {
464   __asan_abi_stack_free_n(5, (void *)ptr, size);
465 }
466 void __asan_stack_free_6(uptr ptr, uptr size) {
467   __asan_abi_stack_free_n(6, (void *)ptr, size);
468 }
469 void __asan_stack_free_7(uptr ptr, uptr size) {
470   __asan_abi_stack_free_n(7, (void *)ptr, size);
471 }
472 void __asan_stack_free_8(uptr ptr, uptr size) {
473   __asan_abi_stack_free_n(8, (void *)ptr, size);
474 }
475 void __asan_stack_free_9(uptr ptr, uptr size) {
476   __asan_abi_stack_free_n(9, (void *)ptr, size);
477 }
478 void __asan_stack_free_10(uptr ptr, uptr size) {
479   __asan_abi_stack_free_n(10, (void *)ptr, size);
480 }
481 }
482