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