1 //===-- asan_test.cpp -----------------------------------------------------===// 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 //===----------------------------------------------------------------------===// 12 #include "asan_test_utils.h" 13 14 #include <errno.h> 15 #include <stdarg.h> 16 17 #ifdef _LIBCPP_GET_C_LOCALE 18 #define SANITIZER_GET_C_LOCALE _LIBCPP_GET_C_LOCALE 19 #else 20 #if defined(__FreeBSD__) 21 #define SANITIZER_GET_C_LOCALE 0 22 #elif defined(__NetBSD__) 23 #define SANITIZER_GET_C_LOCALE LC_C_LOCALE 24 #endif 25 #endif 26 27 #if defined(__sun__) && defined(__svr4__) 28 using std::_setjmp; 29 using std::_longjmp; 30 #endif 31 32 NOINLINE void *malloc_fff(size_t size) { 33 void *res = malloc/**/(size); break_optimization(0); return res;} 34 NOINLINE void *malloc_eee(size_t size) { 35 void *res = malloc_fff(size); break_optimization(0); return res;} 36 NOINLINE void *malloc_ddd(size_t size) { 37 void *res = malloc_eee(size); break_optimization(0); return res;} 38 NOINLINE void *malloc_ccc(size_t size) { 39 void *res = malloc_ddd(size); break_optimization(0); return res;} 40 NOINLINE void *malloc_bbb(size_t size) { 41 void *res = malloc_ccc(size); break_optimization(0); return res;} 42 NOINLINE void *malloc_aaa(size_t size) { 43 void *res = malloc_bbb(size); break_optimization(0); return res;} 44 45 NOINLINE void free_ccc(void *p) { free(p); break_optimization(0);} 46 NOINLINE void free_bbb(void *p) { free_ccc(p); break_optimization(0);} 47 NOINLINE void free_aaa(void *p) { free_bbb(p); break_optimization(0);} 48 49 template<typename T> 50 NOINLINE void uaf_test(int size, int off) { 51 void *p = malloc_aaa(size); 52 free_aaa(p); 53 for (int i = 1; i < 100; i++) 54 free_aaa(malloc_aaa(i)); 55 fprintf(stderr, "writing %ld byte(s) at %p with offset %d\n", 56 (long)sizeof(T), p, off); 57 asan_write((T *)((char *)p + off)); 58 } 59 60 TEST(AddressSanitizer, HasFeatureAddressSanitizerTest) { 61 #if defined(__has_feature) && __has_feature(address_sanitizer) 62 bool asan = 1; 63 #elif defined(__SANITIZE_ADDRESS__) 64 bool asan = 1; 65 #else 66 bool asan = 0; 67 #endif 68 EXPECT_EQ(true, asan); 69 } 70 71 TEST(AddressSanitizer, SimpleDeathTest) { 72 EXPECT_DEATH(exit(1), ""); 73 } 74 75 TEST(AddressSanitizer, VariousMallocsTest) { 76 int *a = (int*)malloc(100 * sizeof(int)); 77 a[50] = 0; 78 free(a); 79 80 int *r = (int*)malloc(10); 81 r = (int*)realloc(r, 2000 * sizeof(int)); 82 r[1000] = 0; 83 free(r); 84 85 int *b = new int[100]; 86 b[50] = 0; 87 delete [] b; 88 89 int *c = new int; 90 *c = 0; 91 delete c; 92 93 #if SANITIZER_TEST_HAS_POSIX_MEMALIGN 94 void *pm = 0; 95 // Valid allocation. 96 int pm_res = posix_memalign(&pm, kPageSize, kPageSize); 97 EXPECT_EQ(0, pm_res); 98 EXPECT_NE(nullptr, pm); 99 free(pm); 100 #endif // SANITIZER_TEST_HAS_POSIX_MEMALIGN 101 102 #if SANITIZER_TEST_HAS_MEMALIGN 103 int *ma = (int*)memalign(kPageSize, kPageSize); 104 EXPECT_EQ(0U, (uintptr_t)ma % kPageSize); 105 ma[123] = 0; 106 free(ma); 107 #endif // SANITIZER_TEST_HAS_MEMALIGN 108 } 109 110 TEST(AddressSanitizer, CallocTest) { 111 int *a = (int*)calloc(100, sizeof(int)); 112 EXPECT_EQ(0, a[10]); 113 free(a); 114 } 115 116 TEST(AddressSanitizer, CallocReturnsZeroMem) { 117 size_t sizes[] = {16, 1000, 10000, 100000, 2100000}; 118 for (size_t s = 0; s < sizeof(sizes)/sizeof(sizes[0]); s++) { 119 size_t size = sizes[s]; 120 for (size_t iter = 0; iter < 5; iter++) { 121 char *x = Ident((char*)calloc(1, size)); 122 EXPECT_EQ(x[0], 0); 123 EXPECT_EQ(x[size - 1], 0); 124 EXPECT_EQ(x[size / 2], 0); 125 EXPECT_EQ(x[size / 3], 0); 126 EXPECT_EQ(x[size / 4], 0); 127 memset(x, 0x42, size); 128 free(Ident(x)); 129 #if !defined(_WIN32) 130 // FIXME: OOM on Windows. We should just make this a lit test 131 // with quarantine size set to 1. 132 free(Ident(malloc(Ident(1 << 27)))); // Try to drain the quarantine. 133 #endif 134 } 135 } 136 } 137 138 // No valloc on Windows or Android. 139 #if !defined(_WIN32) && !defined(__ANDROID__) 140 TEST(AddressSanitizer, VallocTest) { 141 void *a = valloc(100); 142 EXPECT_EQ(0U, (uintptr_t)a % kPageSize); 143 free(a); 144 } 145 #endif 146 147 #if SANITIZER_TEST_HAS_PVALLOC 148 TEST(AddressSanitizer, PvallocTest) { 149 char *a = (char*)pvalloc(kPageSize + 100); 150 EXPECT_EQ(0U, (uintptr_t)a % kPageSize); 151 a[kPageSize + 101] = 1; // we should not report an error here. 152 free(a); 153 154 a = (char*)pvalloc(0); // pvalloc(0) should allocate at least one page. 155 EXPECT_EQ(0U, (uintptr_t)a % kPageSize); 156 a[101] = 1; // we should not report an error here. 157 free(a); 158 } 159 #endif // SANITIZER_TEST_HAS_PVALLOC 160 161 #if !defined(_WIN32) 162 // FIXME: Use an equivalent of pthread_setspecific on Windows. 163 void *TSDWorker(void *test_key) { 164 if (test_key) { 165 pthread_setspecific(*(pthread_key_t*)test_key, (void*)0xfeedface); 166 } 167 return NULL; 168 } 169 170 void TSDDestructor(void *tsd) { 171 // Spawning a thread will check that the current thread id is not -1. 172 pthread_t th; 173 PTHREAD_CREATE(&th, NULL, TSDWorker, NULL); 174 PTHREAD_JOIN(th, NULL); 175 } 176 177 // This tests triggers the thread-specific data destruction fiasco which occurs 178 // if we don't manage the TSD destructors ourselves. We create a new pthread 179 // key with a non-NULL destructor which is likely to be put after the destructor 180 // of AsanThread in the list of destructors. 181 // In this case the TSD for AsanThread will be destroyed before TSDDestructor 182 // is called for the child thread, and a CHECK will fail when we call 183 // pthread_create() to spawn the grandchild. 184 TEST(AddressSanitizer, DISABLED_TSDTest) { 185 pthread_t th; 186 pthread_key_t test_key; 187 pthread_key_create(&test_key, TSDDestructor); 188 PTHREAD_CREATE(&th, NULL, TSDWorker, &test_key); 189 PTHREAD_JOIN(th, NULL); 190 pthread_key_delete(test_key); 191 } 192 #endif 193 194 TEST(AddressSanitizer, UAF_char) { 195 const char *uaf_string = "AddressSanitizer:.*heap-use-after-free"; 196 EXPECT_DEATH(uaf_test<U1>(1, 0), uaf_string); 197 EXPECT_DEATH(uaf_test<U1>(10, 0), uaf_string); 198 EXPECT_DEATH(uaf_test<U1>(10, 10), uaf_string); 199 EXPECT_DEATH(uaf_test<U1>(kLargeMalloc, 0), uaf_string); 200 EXPECT_DEATH(uaf_test<U1>(kLargeMalloc, kLargeMalloc / 2), uaf_string); 201 } 202 203 TEST(AddressSanitizer, UAF_long_double) { 204 if (sizeof(long double) == sizeof(double)) return; 205 long double *p = Ident(new long double[10]); 206 EXPECT_DEATH(Ident(p)[12] = 0, "WRITE of size 1[026]"); 207 EXPECT_DEATH(Ident(p)[0] = Ident(p)[12], "READ of size 1[026]"); 208 delete [] Ident(p); 209 } 210 211 #if !defined(_WIN32) 212 struct Packed5 { 213 int x; 214 char c; 215 } __attribute__((packed)); 216 #else 217 # pragma pack(push, 1) 218 struct Packed5 { 219 int x; 220 char c; 221 }; 222 # pragma pack(pop) 223 #endif 224 225 TEST(AddressSanitizer, UAF_Packed5) { 226 static_assert(sizeof(Packed5) == 5, "Please check the keywords used"); 227 Packed5 *p = Ident(new Packed5[2]); 228 EXPECT_DEATH(p[0] = p[3], "READ of size 5"); 229 EXPECT_DEATH(p[3] = p[0], "WRITE of size 5"); 230 delete [] Ident(p); 231 } 232 233 #if ASAN_HAS_BLACKLIST 234 TEST(AddressSanitizer, IgnoreTest) { 235 int *x = Ident(new int); 236 delete Ident(x); 237 *x = 0; 238 } 239 #endif // ASAN_HAS_BLACKLIST 240 241 struct StructWithBitField { 242 int bf1:1; 243 int bf2:1; 244 int bf3:1; 245 int bf4:29; 246 }; 247 248 TEST(AddressSanitizer, BitFieldPositiveTest) { 249 StructWithBitField *x = new StructWithBitField; 250 delete Ident(x); 251 EXPECT_DEATH(x->bf1 = 0, "use-after-free"); 252 EXPECT_DEATH(x->bf2 = 0, "use-after-free"); 253 EXPECT_DEATH(x->bf3 = 0, "use-after-free"); 254 EXPECT_DEATH(x->bf4 = 0, "use-after-free"); 255 } 256 257 struct StructWithBitFields_8_24 { 258 int a:8; 259 int b:24; 260 }; 261 262 TEST(AddressSanitizer, BitFieldNegativeTest) { 263 StructWithBitFields_8_24 *x = Ident(new StructWithBitFields_8_24); 264 x->a = 0; 265 x->b = 0; 266 delete Ident(x); 267 } 268 269 #if ASAN_NEEDS_SEGV 270 namespace { 271 272 const char kSEGVCrash[] = "AddressSanitizer: SEGV on unknown address"; 273 const char kOverriddenSigactionHandler[] = "Test sigaction handler\n"; 274 const char kOverriddenSignalHandler[] = "Test signal handler\n"; 275 276 TEST(AddressSanitizer, WildAddressTest) { 277 char *c = (char*)0x123; 278 EXPECT_DEATH(*c = 0, kSEGVCrash); 279 } 280 281 void my_sigaction_sighandler(int, siginfo_t*, void*) { 282 fprintf(stderr, kOverriddenSigactionHandler); 283 exit(1); 284 } 285 286 void my_signal_sighandler(int signum) { 287 fprintf(stderr, kOverriddenSignalHandler); 288 exit(1); 289 } 290 291 TEST(AddressSanitizer, SignalTest) { 292 struct sigaction sigact; 293 memset(&sigact, 0, sizeof(sigact)); 294 sigact.sa_sigaction = my_sigaction_sighandler; 295 sigact.sa_flags = SA_SIGINFO; 296 char *c = (char *)0x123; 297 298 EXPECT_DEATH(*c = 0, kSEGVCrash); 299 300 // ASan should allow to set sigaction()... 301 EXPECT_EQ(0, sigaction(SIGSEGV, &sigact, 0)); 302 #ifdef __APPLE__ 303 EXPECT_EQ(0, sigaction(SIGBUS, &sigact, 0)); 304 #endif 305 EXPECT_DEATH(*c = 0, kOverriddenSigactionHandler); 306 307 // ... and signal(). 308 EXPECT_NE(SIG_ERR, signal(SIGSEGV, my_signal_sighandler)); 309 EXPECT_DEATH(*c = 0, kOverriddenSignalHandler); 310 } 311 } // namespace 312 #endif 313 314 static void TestLargeMalloc(size_t size) { 315 char buff[1024]; 316 sprintf(buff, "is located 1 bytes to the left of %lu-byte", (long)size); 317 EXPECT_DEATH(Ident((char*)malloc(size))[-1] = 0, buff); 318 } 319 320 TEST(AddressSanitizer, LargeMallocTest) { 321 const int max_size = (SANITIZER_WORDSIZE == 32) ? 1 << 26 : 1 << 28; 322 for (int i = 113; i < max_size; i = i * 2 + 13) { 323 TestLargeMalloc(i); 324 } 325 } 326 327 #if !GTEST_USES_SIMPLE_RE 328 TEST(AddressSanitizer, HugeMallocTest) { 329 if (SANITIZER_WORDSIZE != 64 || ASAN_AVOID_EXPENSIVE_TESTS) return; 330 size_t n_megs = 4100; 331 EXPECT_DEATH(Ident((char*)malloc(n_megs << 20))[-1] = 0, 332 "is located 1 bytes to the left|" 333 "AddressSanitizer failed to allocate"); 334 } 335 #endif 336 337 #if SANITIZER_TEST_HAS_MEMALIGN 338 void MemalignRun(size_t align, size_t size, int idx) { 339 char *p = (char *)memalign(align, size); 340 Ident(p)[idx] = 0; 341 free(p); 342 } 343 344 TEST(AddressSanitizer, memalign) { 345 for (int align = 16; align <= (1 << 23); align *= 2) { 346 size_t size = align * 5; 347 EXPECT_DEATH(MemalignRun(align, size, -1), 348 "is located 1 bytes to the left"); 349 EXPECT_DEATH(MemalignRun(align, size, size + 1), 350 "is located 1 bytes to the right"); 351 } 352 } 353 #endif // SANITIZER_TEST_HAS_MEMALIGN 354 355 void *ManyThreadsWorker(void *a) { 356 for (int iter = 0; iter < 100; iter++) { 357 for (size_t size = 100; size < 2000; size *= 2) { 358 free(Ident(malloc(size))); 359 } 360 } 361 return 0; 362 } 363 364 #if !defined(__aarch64__) && !defined(__powerpc64__) 365 // FIXME: Infinite loop in AArch64 (PR24389). 366 // FIXME: Also occasional hang on powerpc. Maybe same problem as on AArch64? 367 TEST(AddressSanitizer, ManyThreadsTest) { 368 const size_t kNumThreads = 369 (SANITIZER_WORDSIZE == 32 || ASAN_AVOID_EXPENSIVE_TESTS) ? 30 : 1000; 370 pthread_t t[kNumThreads]; 371 for (size_t i = 0; i < kNumThreads; i++) { 372 PTHREAD_CREATE(&t[i], 0, ManyThreadsWorker, (void*)i); 373 } 374 for (size_t i = 0; i < kNumThreads; i++) { 375 PTHREAD_JOIN(t[i], 0); 376 } 377 } 378 #endif 379 380 TEST(AddressSanitizer, ReallocTest) { 381 const int kMinElem = 5; 382 int *ptr = (int*)malloc(sizeof(int) * kMinElem); 383 ptr[3] = 3; 384 for (int i = 0; i < 10000; i++) { 385 ptr = (int*)realloc(ptr, 386 (my_rand() % 1000 + kMinElem) * sizeof(int)); 387 EXPECT_EQ(3, ptr[3]); 388 } 389 free(ptr); 390 // Realloc pointer returned by malloc(0). 391 int *ptr2 = Ident((int*)malloc(0)); 392 ptr2 = Ident((int*)realloc(ptr2, sizeof(*ptr2))); 393 *ptr2 = 42; 394 EXPECT_EQ(42, *ptr2); 395 free(ptr2); 396 } 397 398 TEST(AddressSanitizer, ReallocFreedPointerTest) { 399 void *ptr = Ident(malloc(42)); 400 ASSERT_TRUE(NULL != ptr); 401 free(ptr); 402 EXPECT_DEATH(ptr = realloc(ptr, 77), "attempting double-free"); 403 } 404 405 TEST(AddressSanitizer, ReallocInvalidPointerTest) { 406 void *ptr = Ident(malloc(42)); 407 EXPECT_DEATH(ptr = realloc((int*)ptr + 1, 77), "attempting free.*not malloc"); 408 free(ptr); 409 } 410 411 TEST(AddressSanitizer, ZeroSizeMallocTest) { 412 // Test that malloc(0) and similar functions don't return NULL. 413 void *ptr = Ident(malloc(0)); 414 EXPECT_TRUE(NULL != ptr); 415 free(ptr); 416 #if SANITIZER_TEST_HAS_POSIX_MEMALIGN 417 int pm_res = posix_memalign(&ptr, 1<<20, 0); 418 EXPECT_EQ(0, pm_res); 419 EXPECT_TRUE(NULL != ptr); 420 free(ptr); 421 #endif // SANITIZER_TEST_HAS_POSIX_MEMALIGN 422 int *int_ptr = new int[0]; 423 int *int_ptr2 = new int[0]; 424 EXPECT_TRUE(NULL != int_ptr); 425 EXPECT_TRUE(NULL != int_ptr2); 426 EXPECT_NE(int_ptr, int_ptr2); 427 delete[] int_ptr; 428 delete[] int_ptr2; 429 } 430 431 #if SANITIZER_TEST_HAS_MALLOC_USABLE_SIZE 432 static const char *kMallocUsableSizeErrorMsg = 433 "AddressSanitizer: attempting to call malloc_usable_size()"; 434 435 TEST(AddressSanitizer, MallocUsableSizeTest) { 436 const size_t kArraySize = 100; 437 char *array = Ident((char*)malloc(kArraySize)); 438 int *int_ptr = Ident(new int); 439 EXPECT_EQ(0U, malloc_usable_size(NULL)); 440 EXPECT_EQ(kArraySize, malloc_usable_size(array)); 441 EXPECT_EQ(sizeof(int), malloc_usable_size(int_ptr)); 442 EXPECT_DEATH(malloc_usable_size((void*)0x123), kMallocUsableSizeErrorMsg); 443 EXPECT_DEATH(malloc_usable_size(array + kArraySize / 2), 444 kMallocUsableSizeErrorMsg); 445 free(array); 446 EXPECT_DEATH(malloc_usable_size(array), kMallocUsableSizeErrorMsg); 447 delete int_ptr; 448 } 449 #endif // SANITIZER_TEST_HAS_MALLOC_USABLE_SIZE 450 451 void WrongFree() { 452 int *x = (int*)malloc(100 * sizeof(int)); 453 // Use the allocated memory, otherwise Clang will optimize it out. 454 Ident(x); 455 free(x + 1); 456 } 457 458 #if !defined(_WIN32) // FIXME: This should be a lit test. 459 TEST(AddressSanitizer, WrongFreeTest) { 460 EXPECT_DEATH(WrongFree(), ASAN_PCRE_DOTALL 461 "ERROR: AddressSanitizer: attempting free.*not malloc" 462 ".*is located 4 bytes inside of 400-byte region" 463 ".*allocated by thread"); 464 } 465 #endif 466 467 void DoubleFree() { 468 int *x = (int*)malloc(100 * sizeof(int)); 469 fprintf(stderr, "DoubleFree: x=%p\n", (void *)x); 470 free(x); 471 free(x); 472 fprintf(stderr, "should have failed in the second free(%p)\n", (void *)x); 473 abort(); 474 } 475 476 #if !defined(_WIN32) // FIXME: This should be a lit test. 477 TEST(AddressSanitizer, DoubleFreeTest) { 478 EXPECT_DEATH(DoubleFree(), ASAN_PCRE_DOTALL 479 "ERROR: AddressSanitizer: attempting double-free" 480 ".*is located 0 bytes inside of 400-byte region" 481 ".*freed by thread T0 here" 482 ".*previously allocated by thread T0 here"); 483 } 484 #endif 485 486 template<int kSize> 487 NOINLINE void SizedStackTest() { 488 char a[kSize]; 489 char *A = Ident((char*)&a); 490 const char *expected_death = "AddressSanitizer: stack-buffer-"; 491 for (size_t i = 0; i < kSize; i++) 492 A[i] = i; 493 EXPECT_DEATH(A[-1] = 0, expected_death); 494 EXPECT_DEATH(A[-5] = 0, expected_death); 495 EXPECT_DEATH(A[kSize] = 0, expected_death); 496 EXPECT_DEATH(A[kSize + 1] = 0, expected_death); 497 EXPECT_DEATH(A[kSize + 5] = 0, expected_death); 498 if (kSize > 16) 499 EXPECT_DEATH(A[kSize + 31] = 0, expected_death); 500 } 501 502 TEST(AddressSanitizer, SimpleStackTest) { 503 SizedStackTest<1>(); 504 SizedStackTest<2>(); 505 SizedStackTest<3>(); 506 SizedStackTest<4>(); 507 SizedStackTest<5>(); 508 SizedStackTest<6>(); 509 SizedStackTest<7>(); 510 SizedStackTest<16>(); 511 SizedStackTest<25>(); 512 SizedStackTest<34>(); 513 SizedStackTest<43>(); 514 SizedStackTest<51>(); 515 SizedStackTest<62>(); 516 SizedStackTest<64>(); 517 SizedStackTest<128>(); 518 } 519 520 #if !defined(_WIN32) 521 // FIXME: It's a bit hard to write multi-line death test expectations 522 // in a portable way. Anyways, this should just be turned into a lit test. 523 TEST(AddressSanitizer, ManyStackObjectsTest) { 524 char XXX[10]; 525 char YYY[20]; 526 char ZZZ[30]; 527 Ident(XXX); 528 Ident(YYY); 529 EXPECT_DEATH(Ident(ZZZ)[-1] = 0, ASAN_PCRE_DOTALL "XXX.*YYY.*ZZZ"); 530 } 531 #endif 532 533 #if 0 // This test requires online symbolizer. 534 // Moved to lit_tests/stack-oob-frames.cpp. 535 // Reenable here once we have online symbolizer by default. 536 NOINLINE static void Frame0(int frame, char *a, char *b, char *c) { 537 char d[4] = {0}; 538 char *D = Ident(d); 539 switch (frame) { 540 case 3: a[5]++; break; 541 case 2: b[5]++; break; 542 case 1: c[5]++; break; 543 case 0: D[5]++; break; 544 } 545 } 546 NOINLINE static void Frame1(int frame, char *a, char *b) { 547 char c[4] = {0}; Frame0(frame, a, b, c); 548 break_optimization(0); 549 } 550 NOINLINE static void Frame2(int frame, char *a) { 551 char b[4] = {0}; Frame1(frame, a, b); 552 break_optimization(0); 553 } 554 NOINLINE static void Frame3(int frame) { 555 char a[4] = {0}; Frame2(frame, a); 556 break_optimization(0); 557 } 558 559 TEST(AddressSanitizer, GuiltyStackFrame0Test) { 560 EXPECT_DEATH(Frame3(0), "located .*in frame <.*Frame0"); 561 } 562 TEST(AddressSanitizer, GuiltyStackFrame1Test) { 563 EXPECT_DEATH(Frame3(1), "located .*in frame <.*Frame1"); 564 } 565 TEST(AddressSanitizer, GuiltyStackFrame2Test) { 566 EXPECT_DEATH(Frame3(2), "located .*in frame <.*Frame2"); 567 } 568 TEST(AddressSanitizer, GuiltyStackFrame3Test) { 569 EXPECT_DEATH(Frame3(3), "located .*in frame <.*Frame3"); 570 } 571 #endif 572 573 NOINLINE void LongJmpFunc1(jmp_buf buf) { 574 // create three red zones for these two stack objects. 575 int a; 576 int b; 577 578 int *A = Ident(&a); 579 int *B = Ident(&b); 580 *A = *B; 581 longjmp(buf, 1); 582 } 583 584 NOINLINE void TouchStackFunc() { 585 int a[100]; // long array will intersect with redzones from LongJmpFunc1. 586 int *A = Ident(a); 587 for (int i = 0; i < 100; i++) 588 A[i] = i*i; 589 } 590 591 // Test that we handle longjmp and do not report false positives on stack. 592 TEST(AddressSanitizer, LongJmpTest) { 593 static jmp_buf buf; 594 if (!setjmp(buf)) { 595 LongJmpFunc1(buf); 596 } else { 597 TouchStackFunc(); 598 } 599 } 600 601 #if !defined(_WIN32) // Only basic longjmp is available on Windows. 602 NOINLINE void UnderscopeLongJmpFunc1(jmp_buf buf) { 603 // create three red zones for these two stack objects. 604 int a; 605 int b; 606 607 int *A = Ident(&a); 608 int *B = Ident(&b); 609 *A = *B; 610 _longjmp(buf, 1); 611 } 612 613 NOINLINE void SigLongJmpFunc1(sigjmp_buf buf) { 614 // create three red zones for these two stack objects. 615 int a; 616 int b; 617 618 int *A = Ident(&a); 619 int *B = Ident(&b); 620 *A = *B; 621 siglongjmp(buf, 1); 622 } 623 624 #if !defined(__ANDROID__) && !defined(__arm__) && \ 625 !defined(__aarch64__) && !defined(__mips__) && \ 626 !defined(__mips64) && !defined(__s390__) 627 NOINLINE void BuiltinLongJmpFunc1(jmp_buf buf) { 628 // create three red zones for these two stack objects. 629 int a; 630 int b; 631 632 int *A = Ident(&a); 633 int *B = Ident(&b); 634 *A = *B; 635 __builtin_longjmp((void**)buf, 1); 636 } 637 638 // Does not work on ARM: 639 // https://github.com/google/sanitizers/issues/185 640 TEST(AddressSanitizer, BuiltinLongJmpTest) { 641 static jmp_buf buf; 642 if (!__builtin_setjmp((void**)buf)) { 643 BuiltinLongJmpFunc1(buf); 644 } else { 645 TouchStackFunc(); 646 } 647 } 648 #endif // !defined(__ANDROID__) && !defined(__arm__) && 649 // !defined(__aarch64__) && !defined(__mips__) 650 // !defined(__mips64) && !defined(__s390__) 651 652 TEST(AddressSanitizer, UnderscopeLongJmpTest) { 653 static jmp_buf buf; 654 if (!_setjmp(buf)) { 655 UnderscopeLongJmpFunc1(buf); 656 } else { 657 TouchStackFunc(); 658 } 659 } 660 661 TEST(AddressSanitizer, SigLongJmpTest) { 662 static sigjmp_buf buf; 663 if (!sigsetjmp(buf, 1)) { 664 SigLongJmpFunc1(buf); 665 } else { 666 TouchStackFunc(); 667 } 668 } 669 #endif 670 671 // FIXME: Why does clang-cl define __EXCEPTIONS? 672 #if defined(__EXCEPTIONS) && !defined(_WIN32) 673 NOINLINE void ThrowFunc() { 674 // create three red zones for these two stack objects. 675 int a; 676 int b; 677 678 int *A = Ident(&a); 679 int *B = Ident(&b); 680 *A = *B; 681 ASAN_THROW(1); 682 } 683 684 TEST(AddressSanitizer, CxxExceptionTest) { 685 if (ASAN_UAR) return; 686 // TODO(kcc): this test crashes on 32-bit for some reason... 687 if (SANITIZER_WORDSIZE == 32) return; 688 try { 689 ThrowFunc(); 690 } catch(...) {} 691 TouchStackFunc(); 692 } 693 #endif 694 695 void *ThreadStackReuseFunc1(void *unused) { 696 // create three red zones for these two stack objects. 697 int a; 698 int b; 699 700 int *A = Ident(&a); 701 int *B = Ident(&b); 702 *A = *B; 703 pthread_exit(0); 704 return 0; 705 } 706 707 void *ThreadStackReuseFunc2(void *unused) { 708 TouchStackFunc(); 709 return 0; 710 } 711 712 #if !defined(__thumb__) 713 TEST(AddressSanitizer, ThreadStackReuseTest) { 714 pthread_t t; 715 PTHREAD_CREATE(&t, 0, ThreadStackReuseFunc1, 0); 716 PTHREAD_JOIN(t, 0); 717 PTHREAD_CREATE(&t, 0, ThreadStackReuseFunc2, 0); 718 PTHREAD_JOIN(t, 0); 719 } 720 #endif 721 722 #if defined(__SSE2__) 723 #include <emmintrin.h> 724 TEST(AddressSanitizer, Store128Test) { 725 char *a = Ident((char*)malloc(Ident(12))); 726 char *p = a; 727 if (((uintptr_t)a % 16) != 0) 728 p = a + 8; 729 assert(((uintptr_t)p % 16) == 0); 730 __m128i value_wide = _mm_set1_epi16(0x1234); 731 EXPECT_DEATH(_mm_store_si128((__m128i*)p, value_wide), 732 "AddressSanitizer: heap-buffer-overflow"); 733 EXPECT_DEATH(_mm_store_si128((__m128i*)p, value_wide), 734 "WRITE of size 16"); 735 EXPECT_DEATH(_mm_store_si128((__m128i*)p, value_wide), 736 "located 0 bytes to the right of 12-byte"); 737 free(a); 738 } 739 #endif 740 741 // FIXME: All tests that use this function should be turned into lit tests. 742 string RightOOBErrorMessage(int oob_distance, bool is_write) { 743 assert(oob_distance >= 0); 744 char expected_str[100]; 745 sprintf(expected_str, ASAN_PCRE_DOTALL 746 #if !GTEST_USES_SIMPLE_RE 747 "buffer-overflow.*%s.*" 748 #endif 749 "located %d bytes to the right", 750 #if !GTEST_USES_SIMPLE_RE 751 is_write ? "WRITE" : "READ", 752 #endif 753 oob_distance); 754 return string(expected_str); 755 } 756 757 string RightOOBWriteMessage(int oob_distance) { 758 return RightOOBErrorMessage(oob_distance, /*is_write*/true); 759 } 760 761 string RightOOBReadMessage(int oob_distance) { 762 return RightOOBErrorMessage(oob_distance, /*is_write*/false); 763 } 764 765 // FIXME: All tests that use this function should be turned into lit tests. 766 string LeftOOBErrorMessage(int oob_distance, bool is_write) { 767 assert(oob_distance > 0); 768 char expected_str[100]; 769 sprintf(expected_str, 770 #if !GTEST_USES_SIMPLE_RE 771 ASAN_PCRE_DOTALL "%s.*" 772 #endif 773 "located %d bytes to the left", 774 #if !GTEST_USES_SIMPLE_RE 775 is_write ? "WRITE" : "READ", 776 #endif 777 oob_distance); 778 return string(expected_str); 779 } 780 781 string LeftOOBWriteMessage(int oob_distance) { 782 return LeftOOBErrorMessage(oob_distance, /*is_write*/true); 783 } 784 785 string LeftOOBReadMessage(int oob_distance) { 786 return LeftOOBErrorMessage(oob_distance, /*is_write*/false); 787 } 788 789 string LeftOOBAccessMessage(int oob_distance) { 790 assert(oob_distance > 0); 791 char expected_str[100]; 792 sprintf(expected_str, "located %d bytes to the left", oob_distance); 793 return string(expected_str); 794 } 795 796 char* MallocAndMemsetString(size_t size, char ch) { 797 char *s = Ident((char*)malloc(size)); 798 memset(s, ch, size); 799 return s; 800 } 801 802 char* MallocAndMemsetString(size_t size) { 803 return MallocAndMemsetString(size, 'z'); 804 } 805 806 #if defined(__linux__) && !defined(__ANDROID__) 807 #define READ_TEST(READ_N_BYTES) \ 808 char *x = new char[10]; \ 809 int fd = open("/proc/self/stat", O_RDONLY); \ 810 ASSERT_GT(fd, 0); \ 811 EXPECT_DEATH(READ_N_BYTES, \ 812 ASAN_PCRE_DOTALL \ 813 "AddressSanitizer: heap-buffer-overflow" \ 814 ".* is located 0 bytes to the right of 10-byte region"); \ 815 close(fd); \ 816 delete [] x; \ 817 818 TEST(AddressSanitizer, pread) { 819 READ_TEST(pread(fd, x, 15, 0)); 820 } 821 822 TEST(AddressSanitizer, pread64) { 823 READ_TEST(pread64(fd, x, 15, 0)); 824 } 825 826 TEST(AddressSanitizer, read) { 827 READ_TEST(read(fd, x, 15)); 828 } 829 #endif // defined(__linux__) && !defined(__ANDROID__) 830 831 // This test case fails 832 // Clang optimizes memcpy/memset calls which lead to unaligned access 833 TEST(AddressSanitizer, DISABLED_MemIntrinsicUnalignedAccessTest) { 834 int size = Ident(4096); 835 char *s = Ident((char*)malloc(size)); 836 EXPECT_DEATH(memset(s + size - 1, 0, 2), RightOOBWriteMessage(0)); 837 free(s); 838 } 839 840 NOINLINE static int LargeFunction(bool do_bad_access) { 841 int *x = new int[100]; 842 x[0]++; 843 x[1]++; 844 x[2]++; 845 x[3]++; 846 x[4]++; 847 x[5]++; 848 x[6]++; 849 x[7]++; 850 x[8]++; 851 x[9]++; 852 853 x[do_bad_access ? 100 : 0]++; int res = __LINE__; 854 855 x[10]++; 856 x[11]++; 857 x[12]++; 858 x[13]++; 859 x[14]++; 860 x[15]++; 861 x[16]++; 862 x[17]++; 863 x[18]++; 864 x[19]++; 865 866 delete[] x; 867 return res; 868 } 869 870 // Test the we have correct debug info for the failing instruction. 871 // This test requires the in-process symbolizer to be enabled by default. 872 TEST(AddressSanitizer, DISABLED_LargeFunctionSymbolizeTest) { 873 int failing_line = LargeFunction(false); 874 char expected_warning[128]; 875 sprintf(expected_warning, "LargeFunction.*asan_test.*:%d", failing_line); 876 EXPECT_DEATH(LargeFunction(true), expected_warning); 877 } 878 879 // Check that we unwind and symbolize correctly. 880 TEST(AddressSanitizer, DISABLED_MallocFreeUnwindAndSymbolizeTest) { 881 int *a = (int*)malloc_aaa(sizeof(int)); 882 *a = 1; 883 free_aaa(a); 884 EXPECT_DEATH(*a = 1, "free_ccc.*free_bbb.*free_aaa.*" 885 "malloc_fff.*malloc_eee.*malloc_ddd"); 886 } 887 888 static bool TryToSetThreadName(const char *name) { 889 #if defined(__linux__) && defined(PR_SET_NAME) 890 return 0 == prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0); 891 #else 892 return false; 893 #endif 894 } 895 896 void *ThreadedTestAlloc(void *a) { 897 EXPECT_EQ(true, TryToSetThreadName("AllocThr")); 898 int **p = (int**)a; 899 *p = new int; 900 return 0; 901 } 902 903 void *ThreadedTestFree(void *a) { 904 EXPECT_EQ(true, TryToSetThreadName("FreeThr")); 905 int **p = (int**)a; 906 delete *p; 907 return 0; 908 } 909 910 void *ThreadedTestUse(void *a) { 911 EXPECT_EQ(true, TryToSetThreadName("UseThr")); 912 int **p = (int**)a; 913 **p = 1; 914 return 0; 915 } 916 917 void ThreadedTestSpawn() { 918 pthread_t t; 919 int *x; 920 PTHREAD_CREATE(&t, 0, ThreadedTestAlloc, &x); 921 PTHREAD_JOIN(t, 0); 922 PTHREAD_CREATE(&t, 0, ThreadedTestFree, &x); 923 PTHREAD_JOIN(t, 0); 924 PTHREAD_CREATE(&t, 0, ThreadedTestUse, &x); 925 PTHREAD_JOIN(t, 0); 926 } 927 928 #if !defined(_WIN32) // FIXME: This should be a lit test. 929 TEST(AddressSanitizer, ThreadedTest) { 930 EXPECT_DEATH(ThreadedTestSpawn(), 931 ASAN_PCRE_DOTALL 932 "Thread T.*created" 933 ".*Thread T.*created" 934 ".*Thread T.*created"); 935 } 936 #endif 937 938 void *ThreadedTestFunc(void *unused) { 939 // Check if prctl(PR_SET_NAME) is supported. Return if not. 940 if (!TryToSetThreadName("TestFunc")) 941 return 0; 942 EXPECT_DEATH(ThreadedTestSpawn(), 943 ASAN_PCRE_DOTALL 944 "WRITE .*thread T. .UseThr." 945 ".*freed by thread T. .FreeThr. here:" 946 ".*previously allocated by thread T. .AllocThr. here:" 947 ".*Thread T. .UseThr. created by T.*TestFunc" 948 ".*Thread T. .FreeThr. created by T" 949 ".*Thread T. .AllocThr. created by T" 950 ""); 951 return 0; 952 } 953 954 TEST(AddressSanitizer, ThreadNamesTest) { 955 // Run ThreadedTestFunc in a separate thread because it tries to set a 956 // thread name and we don't want to change the main thread's name. 957 pthread_t t; 958 PTHREAD_CREATE(&t, 0, ThreadedTestFunc, 0); 959 PTHREAD_JOIN(t, 0); 960 } 961 962 #if ASAN_NEEDS_SEGV 963 TEST(AddressSanitizer, ShadowGapTest) { 964 #if SANITIZER_WORDSIZE == 32 965 char *addr = (char*)0x23000000; 966 #else 967 # if defined(__powerpc64__) 968 char *addr = (char*)0x024000800000; 969 # elif defined(__s390x__) 970 char *addr = (char*)0x11000000000000; 971 # else 972 char *addr = (char*)0x0000100000080000; 973 # endif 974 #endif 975 EXPECT_DEATH(*addr = 1, "AddressSanitizer: (SEGV|BUS) on unknown"); 976 } 977 #endif // ASAN_NEEDS_SEGV 978 979 extern "C" { 980 NOINLINE static void UseThenFreeThenUse() { 981 char *x = Ident((char*)malloc(8)); 982 *x = 1; 983 free_aaa(x); 984 *x = 2; 985 } 986 } 987 988 TEST(AddressSanitizer, UseThenFreeThenUseTest) { 989 EXPECT_DEATH(UseThenFreeThenUse(), "freed by thread"); 990 } 991 992 TEST(AddressSanitizer, StrDupTest) { 993 free(strdup(Ident("123"))); 994 } 995 996 // Currently we create and poison redzone at right of global variables. 997 static char static110[110]; 998 const char ConstGlob[7] = {1, 2, 3, 4, 5, 6, 7}; 999 static const char StaticConstGlob[3] = {9, 8, 7}; 1000 1001 TEST(AddressSanitizer, GlobalTest) { 1002 static char func_static15[15]; 1003 1004 static char fs1[10]; 1005 static char fs2[10]; 1006 static char fs3[10]; 1007 1008 glob5[Ident(0)] = 0; 1009 glob5[Ident(1)] = 0; 1010 glob5[Ident(2)] = 0; 1011 glob5[Ident(3)] = 0; 1012 glob5[Ident(4)] = 0; 1013 1014 EXPECT_DEATH(glob5[Ident(5)] = 0, 1015 "0 bytes to the right of global variable.*glob5.* size 5"); 1016 EXPECT_DEATH(glob5[Ident(5+6)] = 0, 1017 "6 bytes to the right of global variable.*glob5.* size 5"); 1018 Ident(static110); // avoid optimizations 1019 static110[Ident(0)] = 0; 1020 static110[Ident(109)] = 0; 1021 EXPECT_DEATH(static110[Ident(110)] = 0, 1022 "0 bytes to the right of global variable"); 1023 EXPECT_DEATH(static110[Ident(110+7)] = 0, 1024 "7 bytes to the right of global variable"); 1025 1026 Ident(func_static15); // avoid optimizations 1027 func_static15[Ident(0)] = 0; 1028 EXPECT_DEATH(func_static15[Ident(15)] = 0, 1029 "0 bytes to the right of global variable"); 1030 EXPECT_DEATH(func_static15[Ident(15 + 9)] = 0, 1031 "9 bytes to the right of global variable"); 1032 1033 Ident(fs1); 1034 Ident(fs2); 1035 Ident(fs3); 1036 1037 // We don't create left redzones, so this is not 100% guaranteed to fail. 1038 // But most likely will. 1039 EXPECT_DEATH(fs2[Ident(-1)] = 0, "is located.*of global variable"); 1040 1041 EXPECT_DEATH(Ident(Ident(ConstGlob)[8]), 1042 "is located 1 bytes to the right of .*ConstGlob"); 1043 EXPECT_DEATH(Ident(Ident(StaticConstGlob)[5]), 1044 "is located 2 bytes to the right of .*StaticConstGlob"); 1045 1046 // call stuff from another file. 1047 GlobalsTest(0); 1048 } 1049 1050 TEST(AddressSanitizer, GlobalStringConstTest) { 1051 static const char *zoo = "FOOBAR123"; 1052 const char *p = Ident(zoo); 1053 EXPECT_DEATH(Ident(p[15]), "is ascii string 'FOOBAR123'"); 1054 } 1055 1056 TEST(AddressSanitizer, FileNameInGlobalReportTest) { 1057 static char zoo[10]; 1058 const char *p = Ident(zoo); 1059 // The file name should be present in the report. 1060 EXPECT_DEATH(Ident(p[15]), "zoo.*asan_test."); 1061 } 1062 1063 int *ReturnsPointerToALocalObject() { 1064 int a = 0; 1065 return Ident(&a); 1066 } 1067 1068 #if ASAN_UAR == 1 1069 TEST(AddressSanitizer, LocalReferenceReturnTest) { 1070 int *(*f)() = Ident(ReturnsPointerToALocalObject); 1071 int *p = f(); 1072 // Call 'f' a few more times, 'p' should still be poisoned. 1073 for (int i = 0; i < 32; i++) 1074 f(); 1075 EXPECT_DEATH(*p = 1, "AddressSanitizer: stack-use-after-return"); 1076 EXPECT_DEATH(*p = 1, "is located.*in frame .*ReturnsPointerToALocal"); 1077 } 1078 #endif 1079 1080 template <int kSize> 1081 NOINLINE static void FuncWithStack() { 1082 char x[kSize]; 1083 Ident(x)[0] = 0; 1084 Ident(x)[kSize-1] = 0; 1085 } 1086 1087 static void LotsOfStackReuse() { 1088 int LargeStack[10000]; 1089 Ident(LargeStack)[0] = 0; 1090 for (int i = 0; i < 10000; i++) { 1091 FuncWithStack<128 * 1>(); 1092 FuncWithStack<128 * 2>(); 1093 FuncWithStack<128 * 4>(); 1094 FuncWithStack<128 * 8>(); 1095 FuncWithStack<128 * 16>(); 1096 FuncWithStack<128 * 32>(); 1097 FuncWithStack<128 * 64>(); 1098 FuncWithStack<128 * 128>(); 1099 FuncWithStack<128 * 256>(); 1100 FuncWithStack<128 * 512>(); 1101 Ident(LargeStack)[0] = 0; 1102 } 1103 } 1104 1105 TEST(AddressSanitizer, StressStackReuseTest) { 1106 LotsOfStackReuse(); 1107 } 1108 1109 TEST(AddressSanitizer, ThreadedStressStackReuseTest) { 1110 const int kNumThreads = 20; 1111 pthread_t t[kNumThreads]; 1112 for (int i = 0; i < kNumThreads; i++) { 1113 PTHREAD_CREATE(&t[i], 0, (void* (*)(void *x))LotsOfStackReuse, 0); 1114 } 1115 for (int i = 0; i < kNumThreads; i++) { 1116 PTHREAD_JOIN(t[i], 0); 1117 } 1118 } 1119 1120 // pthread_exit tries to perform unwinding stuff that leads to dlopen'ing 1121 // libgcc_s.so. dlopen in its turn calls malloc to store "libgcc_s.so" string 1122 // that confuses LSan on Thumb because it fails to understand that this 1123 // allocation happens in dynamic linker and should be ignored. 1124 #if !defined(__thumb__) 1125 static void *PthreadExit(void *a) { 1126 pthread_exit(0); 1127 return 0; 1128 } 1129 1130 TEST(AddressSanitizer, PthreadExitTest) { 1131 pthread_t t; 1132 for (int i = 0; i < 1000; i++) { 1133 PTHREAD_CREATE(&t, 0, PthreadExit, 0); 1134 PTHREAD_JOIN(t, 0); 1135 } 1136 } 1137 #endif 1138 1139 // FIXME: Why does clang-cl define __EXCEPTIONS? 1140 #if defined(__EXCEPTIONS) && !defined(_WIN32) 1141 NOINLINE static void StackReuseAndException() { 1142 int large_stack[1000]; 1143 Ident(large_stack); 1144 ASAN_THROW(1); 1145 } 1146 1147 // TODO(kcc): support exceptions with use-after-return. 1148 TEST(AddressSanitizer, DISABLED_StressStackReuseAndExceptionsTest) { 1149 for (int i = 0; i < 10000; i++) { 1150 try { 1151 StackReuseAndException(); 1152 } catch(...) { 1153 } 1154 } 1155 } 1156 #endif 1157 1158 #if !defined(_WIN32) 1159 TEST(AddressSanitizer, MlockTest) { 1160 EXPECT_EQ(0, mlockall(MCL_CURRENT)); 1161 EXPECT_EQ(0, mlock((void*)0x12345, 0x5678)); 1162 EXPECT_EQ(0, munlockall()); 1163 EXPECT_EQ(0, munlock((void*)0x987, 0x654)); 1164 } 1165 #endif 1166 1167 struct LargeStruct { 1168 int foo[100]; 1169 }; 1170 1171 // Test for bug http://llvm.org/bugs/show_bug.cgi?id=11763. 1172 // Struct copy should not cause asan warning even if lhs == rhs. 1173 TEST(AddressSanitizer, LargeStructCopyTest) { 1174 LargeStruct a; 1175 *Ident(&a) = *Ident(&a); 1176 } 1177 1178 ATTRIBUTE_NO_SANITIZE_ADDRESS 1179 static void NoSanitizeAddress() { 1180 char *foo = new char[10]; 1181 Ident(foo)[10] = 0; 1182 delete [] foo; 1183 } 1184 1185 TEST(AddressSanitizer, AttributeNoSanitizeAddressTest) { 1186 Ident(NoSanitizeAddress)(); 1187 } 1188 1189 // The new/delete/etc mismatch checks don't work on Android, 1190 // as calls to new/delete go through malloc/free. 1191 // OS X support is tracked here: 1192 // https://github.com/google/sanitizers/issues/131 1193 // Windows support is tracked here: 1194 // https://github.com/google/sanitizers/issues/309 1195 #if !defined(__ANDROID__) && \ 1196 !defined(__APPLE__) && \ 1197 !defined(_WIN32) 1198 static string MismatchStr(const string &str) { 1199 return string("AddressSanitizer: alloc-dealloc-mismatch \\(") + str; 1200 } 1201 1202 static string MismatchOrNewDeleteTypeStr(const string &mismatch_str) { 1203 return "(" + MismatchStr(mismatch_str) + 1204 ")|(AddressSanitizer: new-delete-type-mismatch)"; 1205 } 1206 1207 TEST(AddressSanitizer, AllocDeallocMismatch) { 1208 EXPECT_DEATH(free(Ident(new int)), 1209 MismatchStr("operator new vs free")); 1210 EXPECT_DEATH(free(Ident(new int[2])), 1211 MismatchStr("operator new \\[\\] vs free")); 1212 EXPECT_DEATH( 1213 delete (Ident(new int[2])), 1214 MismatchOrNewDeleteTypeStr("operator new \\[\\] vs operator delete")); 1215 EXPECT_DEATH(delete (Ident((int *)malloc(2 * sizeof(int)))), 1216 MismatchOrNewDeleteTypeStr("malloc vs operator delete")); 1217 EXPECT_DEATH(delete [] (Ident(new int)), 1218 MismatchStr("operator new vs operator delete \\[\\]")); 1219 EXPECT_DEATH(delete [] (Ident((int*)malloc(2 * sizeof(int)))), 1220 MismatchStr("malloc vs operator delete \\[\\]")); 1221 } 1222 #endif 1223 1224 // ------------------ demo tests; run each one-by-one ------------- 1225 // e.g. --gtest_filter=*DemoOOBLeftHigh --gtest_also_run_disabled_tests 1226 TEST(AddressSanitizer, DISABLED_DemoThreadedTest) { 1227 ThreadedTestSpawn(); 1228 } 1229 1230 void *SimpleBugOnSTack(void *x = 0) { 1231 char a[20]; 1232 Ident(a)[20] = 0; 1233 return 0; 1234 } 1235 1236 TEST(AddressSanitizer, DISABLED_DemoStackTest) { 1237 SimpleBugOnSTack(); 1238 } 1239 1240 TEST(AddressSanitizer, DISABLED_DemoThreadStackTest) { 1241 pthread_t t; 1242 PTHREAD_CREATE(&t, 0, SimpleBugOnSTack, 0); 1243 PTHREAD_JOIN(t, 0); 1244 } 1245 1246 TEST(AddressSanitizer, DISABLED_DemoUAFLowIn) { 1247 uaf_test<U1>(10, 0); 1248 } 1249 TEST(AddressSanitizer, DISABLED_DemoUAFLowLeft) { 1250 uaf_test<U1>(10, -2); 1251 } 1252 TEST(AddressSanitizer, DISABLED_DemoUAFLowRight) { 1253 uaf_test<U1>(10, 10); 1254 } 1255 1256 TEST(AddressSanitizer, DISABLED_DemoUAFHigh) { 1257 uaf_test<U1>(kLargeMalloc, 0); 1258 } 1259 1260 TEST(AddressSanitizer, DISABLED_DemoOOM) { 1261 size_t size = SANITIZER_WORDSIZE == 64 ? (size_t)(1ULL << 40) : (0xf0000000); 1262 printf("%p\n", malloc(size)); 1263 } 1264 1265 TEST(AddressSanitizer, DISABLED_DemoDoubleFreeTest) { 1266 DoubleFree(); 1267 } 1268 1269 TEST(AddressSanitizer, DISABLED_DemoNullDerefTest) { 1270 int *a = 0; 1271 Ident(a)[10] = 0; 1272 } 1273 1274 TEST(AddressSanitizer, DISABLED_DemoFunctionStaticTest) { 1275 static char a[100]; 1276 static char b[100]; 1277 static char c[100]; 1278 Ident(a); 1279 Ident(b); 1280 Ident(c); 1281 Ident(a)[5] = 0; 1282 Ident(b)[105] = 0; 1283 Ident(a)[5] = 0; 1284 } 1285 1286 TEST(AddressSanitizer, DISABLED_DemoTooMuchMemoryTest) { 1287 const size_t kAllocSize = (1 << 28) - 1024; 1288 size_t total_size = 0; 1289 while (true) { 1290 void *x = malloc(kAllocSize); 1291 memset(x, 0, kAllocSize); 1292 total_size += kAllocSize; 1293 fprintf(stderr, "total: %ldM %p\n", (long)total_size >> 20, x); 1294 } 1295 } 1296 1297 #if !defined(__NetBSD__) && !defined(__i386__) 1298 // https://github.com/google/sanitizers/issues/66 1299 TEST(AddressSanitizer, BufferOverflowAfterManyFrees) { 1300 for (int i = 0; i < 1000000; i++) { 1301 delete [] (Ident(new char [8644])); 1302 } 1303 char *x = new char[8192]; 1304 EXPECT_DEATH(x[Ident(8192)] = 0, "AddressSanitizer: heap-buffer-overflow"); 1305 delete [] Ident(x); 1306 } 1307 #endif 1308 1309 1310 // Test that instrumentation of stack allocations takes into account 1311 // AllocSize of a type, and not its StoreSize (16 vs 10 bytes for long double). 1312 // See http://llvm.org/bugs/show_bug.cgi?id=12047 for more details. 1313 TEST(AddressSanitizer, LongDoubleNegativeTest) { 1314 long double a, b; 1315 static long double c; 1316 memcpy(Ident(&a), Ident(&b), sizeof(long double)); 1317 memcpy(Ident(&c), Ident(&b), sizeof(long double)); 1318 } 1319 1320 #if !defined(_WIN32) 1321 TEST(AddressSanitizer, pthread_getschedparam) { 1322 int policy; 1323 struct sched_param param; 1324 EXPECT_DEATH( 1325 pthread_getschedparam(pthread_self(), &policy, Ident(¶m) + 2), 1326 "AddressSanitizer: stack-buffer-.*flow"); 1327 EXPECT_DEATH( 1328 pthread_getschedparam(pthread_self(), Ident(&policy) - 1, ¶m), 1329 "AddressSanitizer: stack-buffer-.*flow"); 1330 int res = pthread_getschedparam(pthread_self(), &policy, ¶m); 1331 ASSERT_EQ(0, res); 1332 } 1333 #endif 1334 1335 #if SANITIZER_TEST_HAS_PRINTF_L 1336 static int vsnprintf_l_wrapper(char *s, size_t n, 1337 locale_t l, const char *format, ...) { 1338 va_list va; 1339 va_start(va, format); 1340 int res = vsnprintf_l(s, n , l, format, va); 1341 va_end(va); 1342 return res; 1343 } 1344 1345 TEST(AddressSanitizer, snprintf_l) { 1346 char buff[5]; 1347 // Check that snprintf_l() works fine with Asan. 1348 int res = snprintf_l(buff, 5, SANITIZER_GET_C_LOCALE, "%s", "snprintf_l()"); 1349 EXPECT_EQ(12, res); 1350 // Check that vsnprintf_l() works fine with Asan. 1351 res = vsnprintf_l_wrapper(buff, 5, SANITIZER_GET_C_LOCALE, "%s", 1352 "vsnprintf_l()"); 1353 EXPECT_EQ(13, res); 1354 1355 EXPECT_DEATH( 1356 snprintf_l(buff, 10, SANITIZER_GET_C_LOCALE, "%s", "snprintf_l()"), 1357 "AddressSanitizer: stack-buffer-overflow"); 1358 EXPECT_DEATH(vsnprintf_l_wrapper(buff, 10, SANITIZER_GET_C_LOCALE, "%s", 1359 "vsnprintf_l()"), 1360 "AddressSanitizer: stack-buffer-overflow"); 1361 } 1362 #endif 1363