asan_test.cc revision 609dc28bb32bdba7533c191bcb88f358d0f42187
1//===-- asan_test.cc ------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file is a part of AddressSanitizer, an address sanity checker. 11// 12//===----------------------------------------------------------------------===// 13#include <stdio.h> 14#include <signal.h> 15#include <stdlib.h> 16#include <string.h> 17#include <strings.h> 18#include <pthread.h> 19#include <stdint.h> 20#include <setjmp.h> 21#include <assert.h> 22 23#if defined(__i386__) or defined(__x86_64__) 24#include <emmintrin.h> 25#endif 26 27#include "asan_test_config.h" 28#include "asan_test_utils.h" 29 30#ifndef __APPLE__ 31#include <malloc.h> 32#endif // __APPLE__ 33 34#ifdef __APPLE__ 35static bool APPLE = true; 36#else 37static bool APPLE = false; 38#endif 39 40#if ASAN_HAS_EXCEPTIONS 41# define ASAN_THROW(x) throw (x) 42#else 43# define ASAN_THROW(x) 44#endif 45 46#include <sys/mman.h> 47 48typedef uint8_t U1; 49typedef uint16_t U2; 50typedef uint32_t U4; 51typedef uint64_t U8; 52 53static const char *progname; 54static const int kPageSize = 4096; 55 56// Simple stand-alone pseudorandom number generator. 57// Current algorithm is ANSI C linear congruential PRNG. 58static inline uint32_t my_rand(uint32_t* state) { 59 return (*state = *state * 1103515245 + 12345) >> 16; 60} 61 62static uint32_t global_seed = 0; 63 64const size_t kLargeMalloc = 1 << 24; 65 66template<class T> 67__attribute__((noinline)) 68void asan_write(T *a) { 69 *a = 0; 70} 71 72__attribute__((noinline)) 73void asan_write_sized_aligned(uint8_t *p, size_t size) { 74 EXPECT_EQ(0, ((uintptr_t)p % size)); 75 if (size == 1) asan_write((uint8_t*)p); 76 else if (size == 2) asan_write((uint16_t*)p); 77 else if (size == 4) asan_write((uint32_t*)p); 78 else if (size == 8) asan_write((uint64_t*)p); 79} 80 81__attribute__((noinline)) void *malloc_fff(size_t size) { 82 void *res = malloc/**/(size); break_optimization(0); return res;} 83__attribute__((noinline)) void *malloc_eee(size_t size) { 84 void *res = malloc_fff(size); break_optimization(0); return res;} 85__attribute__((noinline)) void *malloc_ddd(size_t size) { 86 void *res = malloc_eee(size); break_optimization(0); return res;} 87__attribute__((noinline)) void *malloc_ccc(size_t size) { 88 void *res = malloc_ddd(size); break_optimization(0); return res;} 89__attribute__((noinline)) void *malloc_bbb(size_t size) { 90 void *res = malloc_ccc(size); break_optimization(0); return res;} 91__attribute__((noinline)) void *malloc_aaa(size_t size) { 92 void *res = malloc_bbb(size); break_optimization(0); return res;} 93 94#ifndef __APPLE__ 95__attribute__((noinline)) void *memalign_fff(size_t alignment, size_t size) { 96 void *res = memalign/**/(alignment, size); break_optimization(0); return res;} 97__attribute__((noinline)) void *memalign_eee(size_t alignment, size_t size) { 98 void *res = memalign_fff(alignment, size); break_optimization(0); return res;} 99__attribute__((noinline)) void *memalign_ddd(size_t alignment, size_t size) { 100 void *res = memalign_eee(alignment, size); break_optimization(0); return res;} 101__attribute__((noinline)) void *memalign_ccc(size_t alignment, size_t size) { 102 void *res = memalign_ddd(alignment, size); break_optimization(0); return res;} 103__attribute__((noinline)) void *memalign_bbb(size_t alignment, size_t size) { 104 void *res = memalign_ccc(alignment, size); break_optimization(0); return res;} 105__attribute__((noinline)) void *memalign_aaa(size_t alignment, size_t size) { 106 void *res = memalign_bbb(alignment, size); break_optimization(0); return res;} 107#endif // __APPLE__ 108 109 110__attribute__((noinline)) 111 void free_ccc(void *p) { free(p); break_optimization(0);} 112__attribute__((noinline)) 113 void free_bbb(void *p) { free_ccc(p); break_optimization(0);} 114__attribute__((noinline)) 115 void free_aaa(void *p) { free_bbb(p); break_optimization(0);} 116 117template<class T> 118__attribute__((noinline)) 119void oob_test(int size, int off) { 120 char *p = (char*)malloc_aaa(size); 121 // fprintf(stderr, "writing %d byte(s) into [%p,%p) with offset %d\n", 122 // sizeof(T), p, p + size, off); 123 asan_write((T*)(p + off)); 124 free_aaa(p); 125} 126 127 128template<class T> 129__attribute__((noinline)) 130void uaf_test(int size, int off) { 131 char *p = (char *)malloc_aaa(size); 132 free_aaa(p); 133 for (int i = 1; i < 100; i++) 134 free_aaa(malloc_aaa(i)); 135 fprintf(stderr, "writing %ld byte(s) at %p with offset %d\n", 136 (long)sizeof(T), p, off); 137 asan_write((T*)(p + off)); 138} 139 140TEST(AddressSanitizer, HasFeatureAddressSanitizerTest) { 141#if defined(__has_feature) && __has_feature(address_sanitizer) 142 bool asan = 1; 143#else 144 bool asan = 0; 145#endif 146 EXPECT_EQ(true, asan); 147} 148 149TEST(AddressSanitizer, SimpleDeathTest) { 150 EXPECT_DEATH(exit(1), ""); 151} 152 153TEST(AddressSanitizer, VariousMallocsTest) { 154 // fprintf(stderr, "malloc:\n"); 155 int *a = (int*)malloc(100 * sizeof(int)); 156 a[50] = 0; 157 free(a); 158 159 // fprintf(stderr, "realloc:\n"); 160 int *r = (int*)malloc(10); 161 r = (int*)realloc(r, 2000 * sizeof(int)); 162 r[1000] = 0; 163 free(r); 164 165 // fprintf(stderr, "operator new []\n"); 166 int *b = new int[100]; 167 b[50] = 0; 168 delete [] b; 169 170 // fprintf(stderr, "operator new\n"); 171 int *c = new int; 172 *c = 0; 173 delete c; 174 175#ifndef __APPLE__ 176 // fprintf(stderr, "posix_memalign\n"); 177 int *pm; 178 int pm_res = posix_memalign((void**)&pm, kPageSize, kPageSize); 179 EXPECT_EQ(0, pm_res); 180 free(pm); 181 182 int *ma = (int*)memalign(kPageSize, kPageSize); 183 EXPECT_EQ(0, (uintptr_t)ma % kPageSize); 184 ma[123] = 0; 185 free(ma); 186#endif // __APPLE__ 187} 188 189TEST(AddressSanitizer, CallocTest) { 190 int *a = (int*)calloc(100, sizeof(int)); 191 EXPECT_EQ(0, a[10]); 192 free(a); 193} 194 195TEST(AddressSanitizer, VallocTest) { 196 void *a = valloc(100); 197 EXPECT_EQ(0, (uintptr_t)a % kPageSize); 198 free(a); 199} 200 201#ifndef __APPLE__ 202TEST(AddressSanitizer, PvallocTest) { 203 char *a = (char*)pvalloc(kPageSize + 100); 204 EXPECT_EQ(0, (uintptr_t)a % kPageSize); 205 a[kPageSize + 101] = 1; // we should not report an error here. 206 free(a); 207 208 a = (char*)pvalloc(0); // pvalloc(0) should allocate at least one page. 209 EXPECT_EQ(0, (uintptr_t)a % kPageSize); 210 a[101] = 1; // we should not report an error here. 211 free(a); 212} 213#endif // __APPLE__ 214 215void NoOpSignalHandler(int unused) { 216 fprintf(stderr, "NoOpSignalHandler (should not happen). Aborting\n"); 217 abort(); 218} 219 220void NoOpSigaction(int, siginfo_t *siginfo, void *context) { 221 fprintf(stderr, "NoOpSigaction (should not happen). Aborting\n"); 222 abort(); 223} 224 225TEST(AddressSanitizer, SignalTest) { 226 signal(SIGSEGV, NoOpSignalHandler); 227 signal(SIGILL, NoOpSignalHandler); 228 // If asan did not intercept sigaction NoOpSigaction will fire. 229 char *x = Ident((char*)malloc(5)); 230 EXPECT_DEATH(x[6]++, "is located 1 bytes to the right"); 231 free(Ident(x)); 232} 233 234TEST(AddressSanitizer, SigactionTest) { 235 { 236 struct sigaction sigact; 237 memset(&sigact, 0, sizeof(sigact)); 238 sigact.sa_sigaction = NoOpSigaction;; 239 sigact.sa_flags = SA_SIGINFO; 240 sigaction(SIGSEGV, &sigact, 0); 241 } 242 243 { 244 struct sigaction sigact; 245 memset(&sigact, 0, sizeof(sigact)); 246 sigact.sa_sigaction = NoOpSigaction;; 247 sigact.sa_flags = SA_SIGINFO; 248 sigaction(SIGILL, &sigact, 0); 249 } 250 251 // If asan did not intercept sigaction NoOpSigaction will fire. 252 char *x = Ident((char*)malloc(5)); 253 EXPECT_DEATH(x[6]++, "is located 1 bytes to the right"); 254 free(Ident(x)); 255} 256 257void *TSDWorker(void *test_key) { 258 if (test_key) { 259 pthread_setspecific(*(pthread_key_t*)test_key, (void*)0xfeedface); 260 } 261 return NULL; 262} 263 264void TSDDestructor(void *tsd) { 265 // Spawning a thread will check that the current thread id is not -1. 266 pthread_t th; 267 pthread_create(&th, NULL, TSDWorker, NULL); 268 pthread_join(th, NULL); 269} 270 271// This tests triggers the thread-specific data destruction fiasco which occurs 272// if we don't manage the TSD destructors ourselves. We create a new pthread 273// key with a non-NULL destructor which is likely to be put after the destructor 274// of AsanThread in the list of destructors. 275// In this case the TSD for AsanThread will be destroyed before TSDDestructor 276// is called for the child thread, and a CHECK will fail when we call 277// pthread_create() to spawn the grandchild. 278TEST(AddressSanitizer, DISABLED_TSDTest) { 279 pthread_t th; 280 pthread_key_t test_key; 281 pthread_key_create(&test_key, TSDDestructor); 282 pthread_create(&th, NULL, TSDWorker, &test_key); 283 pthread_join(th, NULL); 284 pthread_key_delete(test_key); 285} 286 287template<class T> 288void OOBTest() { 289 char expected_str[100]; 290 for (int size = sizeof(T); size < 20; size += 5) { 291 for (int i = -5; i < 0; i++) { 292 const char *str = 293 "is located.*%d byte.*to the left"; 294 sprintf(expected_str, str, abs(i)); 295 EXPECT_DEATH(oob_test<T>(size, i), expected_str); 296 } 297 298 for (int i = 0; i < size - sizeof(T) + 1; i++) 299 oob_test<T>(size, i); 300 301 for (int i = size - sizeof(T) + 1; i <= size + 3 * sizeof(T); i++) { 302 const char *str = 303 "is located.*%d byte.*to the right"; 304 int off = i >= size ? (i - size) : 0; 305 // we don't catch unaligned partially OOB accesses. 306 if (i % sizeof(T)) continue; 307 sprintf(expected_str, str, off); 308 EXPECT_DEATH(oob_test<T>(size, i), expected_str); 309 } 310 } 311 312 EXPECT_DEATH(oob_test<T>(kLargeMalloc, -1), 313 "is located.*1 byte.*to the left"); 314 EXPECT_DEATH(oob_test<T>(kLargeMalloc, kLargeMalloc), 315 "is located.*0 byte.*to the right"); 316} 317 318// TODO(glider): the following tests are EXTREMELY slow on Darwin: 319// AddressSanitizer.OOB_char (125503 ms) 320// AddressSanitizer.OOB_int (126890 ms) 321// AddressSanitizer.OOBRightTest (315605 ms) 322// AddressSanitizer.SimpleStackTest (366559 ms) 323 324TEST(AddressSanitizer, OOB_char) { 325 OOBTest<U1>(); 326} 327 328TEST(AddressSanitizer, OOB_int) { 329 OOBTest<U4>(); 330} 331 332TEST(AddressSanitizer, OOBRightTest) { 333 for (size_t access_size = 1; access_size <= 8; access_size *= 2) { 334 for (size_t alloc_size = 1; alloc_size <= 8; alloc_size++) { 335 for (size_t offset = 0; offset <= 8; offset += access_size) { 336 void *p = malloc(alloc_size); 337 // allocated: [p, p + alloc_size) 338 // accessed: [p + offset, p + offset + access_size) 339 uint8_t *addr = (uint8_t*)p + offset; 340 if (offset + access_size <= alloc_size) { 341 asan_write_sized_aligned(addr, access_size); 342 } else { 343 int outside_bytes = offset > alloc_size ? (offset - alloc_size) : 0; 344 const char *str = 345 "is located.%d *byte.*to the right"; 346 char expected_str[100]; 347 sprintf(expected_str, str, outside_bytes); 348 EXPECT_DEATH(asan_write_sized_aligned(addr, access_size), 349 expected_str); 350 } 351 free(p); 352 } 353 } 354 } 355} 356 357TEST(AddressSanitizer, UAF_char) { 358 const char *uaf_string = "AddressSanitizer.*heap-use-after-free"; 359 EXPECT_DEATH(uaf_test<U1>(1, 0), uaf_string); 360 EXPECT_DEATH(uaf_test<U1>(10, 0), uaf_string); 361 EXPECT_DEATH(uaf_test<U1>(10, 10), uaf_string); 362 EXPECT_DEATH(uaf_test<U1>(kLargeMalloc, 0), uaf_string); 363 EXPECT_DEATH(uaf_test<U1>(kLargeMalloc, kLargeMalloc / 2), uaf_string); 364} 365 366#if ASAN_HAS_BLACKLIST 367TEST(AddressSanitizer, IgnoreTest) { 368 int *x = Ident(new int); 369 delete Ident(x); 370 *x = 0; 371} 372#endif // ASAN_HAS_BLACKLIST 373 374struct StructWithBitField { 375 int bf1:1; 376 int bf2:1; 377 int bf3:1; 378 int bf4:29; 379}; 380 381TEST(AddressSanitizer, BitFieldPositiveTest) { 382 StructWithBitField *x = new StructWithBitField; 383 delete Ident(x); 384 EXPECT_DEATH(x->bf1 = 0, "use-after-free"); 385 EXPECT_DEATH(x->bf2 = 0, "use-after-free"); 386 EXPECT_DEATH(x->bf3 = 0, "use-after-free"); 387 EXPECT_DEATH(x->bf4 = 0, "use-after-free"); 388}; 389 390struct StructWithBitFields_8_24 { 391 int a:8; 392 int b:24; 393}; 394 395TEST(AddressSanitizer, BitFieldNegativeTest) { 396 StructWithBitFields_8_24 *x = Ident(new StructWithBitFields_8_24); 397 x->a = 0; 398 x->b = 0; 399 delete Ident(x); 400} 401 402TEST(AddressSanitizer, OutOfMemoryTest) { 403 size_t size = __WORDSIZE == 64 ? (size_t)(1ULL << 48) : (0xf0000000); 404 EXPECT_EQ(0, realloc(0, size)); 405 EXPECT_EQ(0, realloc(0, ~Ident(0))); 406 EXPECT_EQ(0, malloc(size)); 407 EXPECT_EQ(0, malloc(~Ident(0))); 408 EXPECT_EQ(0, calloc(1, size)); 409 EXPECT_EQ(0, calloc(1, ~Ident(0))); 410} 411 412#if ASAN_NEEDS_SEGV 413TEST(AddressSanitizer, WildAddressTest) { 414 char *c = (char*)0x123; 415 EXPECT_DEATH(*c = 0, "AddressSanitizer crashed on unknown address"); 416} 417#endif 418 419static void MallocStress(size_t n) { 420 uint32_t seed = my_rand(&global_seed); 421 for (size_t iter = 0; iter < 10; iter++) { 422 vector<void *> vec; 423 for (size_t i = 0; i < n; i++) { 424 if ((i % 3) == 0) { 425 if (vec.empty()) continue; 426 size_t idx = my_rand(&seed) % vec.size(); 427 void *ptr = vec[idx]; 428 vec[idx] = vec.back(); 429 vec.pop_back(); 430 free_aaa(ptr); 431 } else { 432 size_t size = my_rand(&seed) % 1000 + 1; 433#ifndef __APPLE__ 434 size_t alignment = 1 << (my_rand(&seed) % 7 + 3); 435 char *ptr = (char*)memalign_aaa(alignment, size); 436#else 437 char *ptr = (char*) malloc_aaa(size); 438#endif 439 vec.push_back(ptr); 440 ptr[0] = 0; 441 ptr[size-1] = 0; 442 ptr[size/2] = 0; 443 } 444 } 445 for (size_t i = 0; i < vec.size(); i++) 446 free_aaa(vec[i]); 447 } 448} 449 450TEST(AddressSanitizer, MallocStressTest) { 451 MallocStress(200000); 452} 453 454static void TestLargeMalloc(size_t size) { 455 char buff[1024]; 456 sprintf(buff, "is located 1 bytes to the left of %lu-byte", (long)size); 457 EXPECT_DEATH(Ident((char*)malloc(size))[-1] = 0, buff); 458} 459 460TEST(AddressSanitizer, LargeMallocTest) { 461 for (int i = 113; i < (1 << 28); i = i * 2 + 13) { 462 TestLargeMalloc(i); 463 } 464} 465 466TEST(AddressSanitizer, HugeMallocTest) { 467#ifdef __APPLE__ 468 // It was empirically found out that 1215 megabytes is the maximum amount of 469 // memory available to the process under AddressSanitizer on Darwin. 470 // (the libSystem malloc() allows allocating up to 2300 megabytes without 471 // ASan). 472 size_t n_megs = __WORDSIZE == 32 ? 1200 : 4100; 473#else 474 size_t n_megs = __WORDSIZE == 32 ? 2600 : 4100; 475#endif 476 TestLargeMalloc(n_megs << 20); 477} 478 479TEST(AddressSanitizer, ThreadedMallocStressTest) { 480 const int kNumThreads = 4; 481 pthread_t t[kNumThreads]; 482 for (int i = 0; i < kNumThreads; i++) { 483 pthread_create(&t[i], 0, (void* (*)(void *x))MallocStress, (void*)100000); 484 } 485 for (int i = 0; i < kNumThreads; i++) { 486 pthread_join(t[i], 0); 487 } 488} 489 490void *ManyThreadsWorker(void *a) { 491 for (int iter = 0; iter < 100; iter++) { 492 for (size_t size = 100; size < 2000; size *= 2) { 493 free(Ident(malloc(size))); 494 } 495 } 496 return 0; 497} 498 499TEST(AddressSanitizer, ManyThreadsTest) { 500 const size_t kNumThreads = __WORDSIZE == 32 ? 30 : 1000; 501 pthread_t t[kNumThreads]; 502 for (size_t i = 0; i < kNumThreads; i++) { 503 pthread_create(&t[i], 0, (void* (*)(void *x))ManyThreadsWorker, (void*)i); 504 } 505 for (size_t i = 0; i < kNumThreads; i++) { 506 pthread_join(t[i], 0); 507 } 508} 509 510TEST(AddressSanitizer, ReallocTest) { 511 const int kMinElem = 5; 512 int *ptr = (int*)malloc(sizeof(int) * kMinElem); 513 ptr[3] = 3; 514 for (int i = 0; i < 10000; i++) { 515 ptr = (int*)realloc(ptr, 516 (my_rand(&global_seed) % 1000 + kMinElem) * sizeof(int)); 517 EXPECT_EQ(3, ptr[3]); 518 } 519} 520 521void WrongFree() { 522 int *x = (int*)malloc(100 * sizeof(int)); 523 // Use the allocated memory, otherwise Clang will optimize it out. 524 Ident(x); 525 free(x + 1); 526} 527 528TEST(AddressSanitizer, WrongFreeTest) { 529 EXPECT_DEATH(WrongFree(), 530 "ERROR: AddressSanitizer attempting free.*not malloc"); 531} 532 533void DoubleFree() { 534 int *x = (int*)malloc(100 * sizeof(int)); 535 fprintf(stderr, "DoubleFree: x=%p\n", x); 536 free(x); 537 free(x); 538 fprintf(stderr, "should have failed in the second free(%p)\n", x); 539 abort(); 540} 541 542TEST(AddressSanitizer, DoubleFreeTest) { 543 EXPECT_DEATH(DoubleFree(), "ERROR: AddressSanitizer attempting double-free"); 544} 545 546template<int kSize> 547__attribute__((noinline)) 548void SizedStackTest() { 549 char a[kSize]; 550 char *A = Ident((char*)&a); 551 for (size_t i = 0; i < kSize; i++) 552 A[i] = i; 553 EXPECT_DEATH(A[-1] = 0, ""); 554 EXPECT_DEATH(A[-20] = 0, ""); 555 EXPECT_DEATH(A[-31] = 0, ""); 556 EXPECT_DEATH(A[kSize] = 0, ""); 557 EXPECT_DEATH(A[kSize + 1] = 0, ""); 558 EXPECT_DEATH(A[kSize + 10] = 0, ""); 559 EXPECT_DEATH(A[kSize + 31] = 0, ""); 560} 561 562TEST(AddressSanitizer, SimpleStackTest) { 563 SizedStackTest<1>(); 564 SizedStackTest<2>(); 565 SizedStackTest<3>(); 566 SizedStackTest<4>(); 567 SizedStackTest<5>(); 568 SizedStackTest<6>(); 569 SizedStackTest<7>(); 570 SizedStackTest<16>(); 571 SizedStackTest<25>(); 572 SizedStackTest<34>(); 573 SizedStackTest<43>(); 574 SizedStackTest<51>(); 575 SizedStackTest<62>(); 576 SizedStackTest<64>(); 577 SizedStackTest<128>(); 578} 579 580TEST(AddressSanitizer, ManyStackObjectsTest) { 581 char XXX[10]; 582 char YYY[20]; 583 char ZZZ[30]; 584 Ident(XXX); 585 Ident(YYY); 586 EXPECT_DEATH(Ident(ZZZ)[-1] = 0, ASAN_PCRE_DOTALL "XXX.*YYY.*ZZZ"); 587} 588 589__attribute__((noinline)) 590static void Frame0(int frame, char *a, char *b, char *c) { 591 char d[4] = {0}; 592 char *D = Ident(d); 593 switch (frame) { 594 case 3: a[5]++; break; 595 case 2: b[5]++; break; 596 case 1: c[5]++; break; 597 case 0: D[5]++; break; 598 } 599} 600__attribute__((noinline)) static void Frame1(int frame, char *a, char *b) { 601 char c[4] = {0}; Frame0(frame, a, b, c); 602 break_optimization(0); 603} 604__attribute__((noinline)) static void Frame2(int frame, char *a) { 605 char b[4] = {0}; Frame1(frame, a, b); 606 break_optimization(0); 607} 608__attribute__((noinline)) static void Frame3(int frame) { 609 char a[4] = {0}; Frame2(frame, a); 610 break_optimization(0); 611} 612 613TEST(AddressSanitizer, GuiltyStackFrame0Test) { 614 EXPECT_DEATH(Frame3(0), "located .*in frame <.*Frame0"); 615} 616TEST(AddressSanitizer, GuiltyStackFrame1Test) { 617 EXPECT_DEATH(Frame3(1), "located .*in frame <.*Frame1"); 618} 619TEST(AddressSanitizer, GuiltyStackFrame2Test) { 620 EXPECT_DEATH(Frame3(2), "located .*in frame <.*Frame2"); 621} 622TEST(AddressSanitizer, GuiltyStackFrame3Test) { 623 EXPECT_DEATH(Frame3(3), "located .*in frame <.*Frame3"); 624} 625 626__attribute__((noinline)) 627void LongJmpFunc1(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 longjmp(buf, 1); 636} 637 638__attribute__((noinline)) 639void UnderscopeLongJmpFunc1(jmp_buf buf) { 640 // create three red zones for these two stack objects. 641 int a; 642 int b; 643 644 int *A = Ident(&a); 645 int *B = Ident(&b); 646 *A = *B; 647 _longjmp(buf, 1); 648} 649 650__attribute__((noinline)) 651void SigLongJmpFunc1(sigjmp_buf buf) { 652 // create three red zones for these two stack objects. 653 int a; 654 int b; 655 656 int *A = Ident(&a); 657 int *B = Ident(&b); 658 *A = *B; 659 siglongjmp(buf, 1); 660} 661 662 663__attribute__((noinline)) 664void TouchStackFunc() { 665 int a[100]; // long array will intersect with redzones from LongJmpFunc1. 666 int *A = Ident(a); 667 for (int i = 0; i < 100; i++) 668 A[i] = i*i; 669} 670 671// Test that we handle longjmp and do not report fals positives on stack. 672TEST(AddressSanitizer, LongJmpTest) { 673 static jmp_buf buf; 674 if (!setjmp(buf)) { 675 LongJmpFunc1(buf); 676 } else { 677 TouchStackFunc(); 678 } 679} 680 681TEST(AddressSanitizer, UnderscopeLongJmpTest) { 682 static jmp_buf buf; 683 if (!_setjmp(buf)) { 684 UnderscopeLongJmpFunc1(buf); 685 } else { 686 TouchStackFunc(); 687 } 688} 689 690TEST(AddressSanitizer, SigLongJmpTest) { 691 static sigjmp_buf buf; 692 if (!sigsetjmp(buf, 1)) { 693 SigLongJmpFunc1(buf); 694 } else { 695 TouchStackFunc(); 696 } 697} 698 699#ifdef __EXCEPTIONS 700__attribute__((noinline)) 701void ThrowFunc() { 702 // create three red zones for these two stack objects. 703 int a; 704 int b; 705 706 int *A = Ident(&a); 707 int *B = Ident(&b); 708 *A = *B; 709 ASAN_THROW(1); 710} 711 712TEST(AddressSanitizer, CxxExceptionTest) { 713 if (ASAN_UAR) return; 714 // TODO(kcc): this test crashes on 32-bit for some reason... 715 if (__WORDSIZE == 32) return; 716 try { 717 ThrowFunc(); 718 } catch(...) {} 719 TouchStackFunc(); 720} 721#endif 722 723void *ThreadStackReuseFunc1(void *unused) { 724 // create three red zones for these two stack objects. 725 int a; 726 int b; 727 728 int *A = Ident(&a); 729 int *B = Ident(&b); 730 *A = *B; 731 pthread_exit(0); 732 return 0; 733} 734 735void *ThreadStackReuseFunc2(void *unused) { 736 TouchStackFunc(); 737 return 0; 738} 739 740TEST(AddressSanitizer, ThreadStackReuseTest) { 741 pthread_t t; 742 pthread_create(&t, 0, ThreadStackReuseFunc1, 0); 743 pthread_join(t, 0); 744 pthread_create(&t, 0, ThreadStackReuseFunc2, 0); 745 pthread_join(t, 0); 746} 747 748#if defined(__i386__) or defined(__x86_64__) 749TEST(AddressSanitizer, Store128Test) { 750 char *a = Ident((char*)malloc(Ident(12))); 751 char *p = a; 752 if (((uintptr_t)a % 16) != 0) 753 p = a + 8; 754 assert(((uintptr_t)p % 16) == 0); 755 __m128i value_wide = _mm_set1_epi16(0x1234); 756 EXPECT_DEATH(_mm_store_si128((__m128i*)p, value_wide), 757 "AddressSanitizer heap-buffer-overflow"); 758 EXPECT_DEATH(_mm_store_si128((__m128i*)p, value_wide), 759 "WRITE of size 16"); 760 EXPECT_DEATH(_mm_store_si128((__m128i*)p, value_wide), 761 "located 0 bytes to the right of 12-byte"); 762 free(a); 763} 764#endif 765 766static string RightOOBErrorMessage(int oob_distance) { 767 assert(oob_distance >= 0); 768 char expected_str[100]; 769 sprintf(expected_str, "located %d bytes to the right", oob_distance); 770 return string(expected_str); 771} 772 773static string LeftOOBErrorMessage(int oob_distance) { 774 assert(oob_distance > 0); 775 char expected_str[100]; 776 sprintf(expected_str, "located %d bytes to the left", oob_distance); 777 return string(expected_str); 778} 779 780template<class T> 781void MemSetOOBTestTemplate(size_t length) { 782 if (length == 0) return; 783 size_t size = Ident(sizeof(T) * length); 784 T *array = Ident((T*)malloc(size)); 785 int element = Ident(42); 786 int zero = Ident(0); 787 // memset interval inside array 788 memset(array, element, size); 789 memset(array, element, size - 1); 790 memset(array + length - 1, element, sizeof(T)); 791 memset(array, element, 1); 792 793 // memset 0 bytes 794 memset(array - 10, element, zero); 795 memset(array - 1, element, zero); 796 memset(array, element, zero); 797 memset(array + length, 0, zero); 798 memset(array + length + 1, 0, zero); 799 800 // try to memset bytes to the right of array 801 EXPECT_DEATH(memset(array, 0, size + 1), 802 RightOOBErrorMessage(0)); 803 EXPECT_DEATH(memset((char*)(array + length) - 1, element, 6), 804 RightOOBErrorMessage(4)); 805 EXPECT_DEATH(memset(array + 1, element, size + sizeof(T)), 806 RightOOBErrorMessage(2 * sizeof(T) - 1)); 807 // whole interval is to the right 808 EXPECT_DEATH(memset(array + length + 1, 0, 10), 809 RightOOBErrorMessage(sizeof(T))); 810 811 // try to memset bytes to the left of array 812 EXPECT_DEATH(memset((char*)array - 1, element, size), 813 LeftOOBErrorMessage(1)); 814 EXPECT_DEATH(memset((char*)array - 5, 0, 6), 815 LeftOOBErrorMessage(5)); 816 EXPECT_DEATH(memset(array - 5, element, size + 5 * sizeof(T)), 817 LeftOOBErrorMessage(5 * sizeof(T))); 818 // whole interval is to the left 819 EXPECT_DEATH(memset(array - 2, 0, sizeof(T)), 820 LeftOOBErrorMessage(2 * sizeof(T))); 821 822 // try to memset bytes both to the left & to the right 823 EXPECT_DEATH(memset((char*)array - 2, element, size + 4), 824 LeftOOBErrorMessage(2)); 825 826 free(array); 827} 828 829TEST(AddressSanitizer, MemSetOOBTest) { 830 MemSetOOBTestTemplate<char>(100); 831 MemSetOOBTestTemplate<int>(5); 832 MemSetOOBTestTemplate<double>(256); 833 // We can test arrays of structres/classes here, but what for? 834} 835 836// Same test for memcpy and memmove functions 837template <class T, class M> 838void MemTransferOOBTestTemplate(size_t length) { 839 if (length == 0) return; 840 size_t size = Ident(sizeof(T) * length); 841 T *src = Ident((T*)malloc(size)); 842 T *dest = Ident((T*)malloc(size)); 843 int zero = Ident(0); 844 845 // valid transfer of bytes between arrays 846 M::transfer(dest, src, size); 847 M::transfer(dest + 1, src, size - sizeof(T)); 848 M::transfer(dest, src + length - 1, sizeof(T)); 849 M::transfer(dest, src, 1); 850 851 // transfer zero bytes 852 M::transfer(dest - 1, src, 0); 853 M::transfer(dest + length, src, zero); 854 M::transfer(dest, src - 1, zero); 855 M::transfer(dest, src, zero); 856 857 // try to change mem to the right of dest 858 EXPECT_DEATH(M::transfer(dest + 1, src, size), 859 RightOOBErrorMessage(sizeof(T) - 1)); 860 EXPECT_DEATH(M::transfer((char*)(dest + length) - 1, src, 5), 861 RightOOBErrorMessage(3)); 862 863 // try to change mem to the left of dest 864 EXPECT_DEATH(M::transfer(dest - 2, src, size), 865 LeftOOBErrorMessage(2 * sizeof(T))); 866 EXPECT_DEATH(M::transfer((char*)dest - 3, src, 4), 867 LeftOOBErrorMessage(3)); 868 869 // try to access mem to the right of src 870 EXPECT_DEATH(M::transfer(dest, src + 2, size), 871 RightOOBErrorMessage(2 * sizeof(T) - 1)); 872 EXPECT_DEATH(M::transfer(dest, (char*)(src + length) - 3, 6), 873 RightOOBErrorMessage(2)); 874 875 // try to access mem to the left of src 876 EXPECT_DEATH(M::transfer(dest, src - 1, size), 877 LeftOOBErrorMessage(sizeof(T))); 878 EXPECT_DEATH(M::transfer(dest, (char*)src - 6, 7), 879 LeftOOBErrorMessage(6)); 880 881 // Generally we don't need to test cases where both accessing src and writing 882 // to dest address to poisoned memory. 883 884 T *big_src = Ident((T*)malloc(size * 2)); 885 T *big_dest = Ident((T*)malloc(size * 2)); 886 // try to change mem to both sides of dest 887 EXPECT_DEATH(M::transfer(dest - 1, big_src, size * 2), 888 LeftOOBErrorMessage(sizeof(T))); 889 // try to access mem to both sides of src 890 EXPECT_DEATH(M::transfer(big_dest, src - 2, size * 2), 891 LeftOOBErrorMessage(2 * sizeof(T))); 892 893 free(src); 894 free(dest); 895 free(big_src); 896 free(big_dest); 897} 898 899class MemCpyWrapper { 900 public: 901 static void* transfer(void *to, const void *from, size_t size) { 902 return memcpy(to, from, size); 903 } 904}; 905TEST(AddressSanitizer, MemCpyOOBTest) { 906 MemTransferOOBTestTemplate<char, MemCpyWrapper>(100); 907 MemTransferOOBTestTemplate<int, MemCpyWrapper>(1024); 908} 909 910class MemMoveWrapper { 911 public: 912 static void* transfer(void *to, const void *from, size_t size) { 913 return memmove(to, from, size); 914 } 915}; 916TEST(AddressSanitizer, MemMoveOOBTest) { 917 MemTransferOOBTestTemplate<char, MemMoveWrapper>(100); 918 MemTransferOOBTestTemplate<int, MemMoveWrapper>(1024); 919} 920 921// Tests for string functions 922 923// Used for string functions tests 924static char global_string[] = "global"; 925static size_t global_string_length = 6; 926 927// Input to a test is a zero-terminated string str with given length 928// Accesses to the bytes to the left and to the right of str 929// are presumed to produce OOB errors 930void StrLenOOBTestTemplate(char *str, size_t length, bool is_global) { 931 // Normal strlen calls 932 EXPECT_EQ(strlen(str), length); 933 if (length > 0) { 934 EXPECT_EQ(strlen(str + 1), length - 1); 935 EXPECT_EQ(strlen(str + length), 0); 936 } 937 // Arg of strlen is not malloced, OOB access 938 if (!is_global) { 939 // We don't insert RedZones to the left of global variables 940 EXPECT_DEATH(Ident(strlen(str - 1)), LeftOOBErrorMessage(1)); 941 EXPECT_DEATH(Ident(strlen(str - 5)), LeftOOBErrorMessage(5)); 942 } 943 EXPECT_DEATH(Ident(strlen(str + length + 1)), RightOOBErrorMessage(0)); 944 // Overwrite terminator 945 str[length] = 'a'; 946 // String is not zero-terminated, strlen will lead to OOB access 947 EXPECT_DEATH(Ident(strlen(str)), RightOOBErrorMessage(0)); 948 EXPECT_DEATH(Ident(strlen(str + length)), RightOOBErrorMessage(0)); 949 // Restore terminator 950 str[length] = 0; 951} 952TEST(AddressSanitizer, StrLenOOBTest) { 953 // Check heap-allocated string 954 size_t length = Ident(10); 955 char *heap_string = Ident((char*)malloc(length + 1)); 956 char stack_string[10 + 1]; 957 for (int i = 0; i < length; i++) { 958 heap_string[i] = 'a'; 959 stack_string[i] = 'b'; 960 } 961 heap_string[length] = 0; 962 stack_string[length] = 0; 963 StrLenOOBTestTemplate(heap_string, length, false); 964 // TODO(samsonov): Fix expected messages in StrLenOOBTestTemplate to 965 // make test for stack_string work. Or move it to output tests. 966 // StrLenOOBTestTemplate(stack_string, length, false); 967 StrLenOOBTestTemplate(global_string, global_string_length, true); 968 free(heap_string); 969} 970 971static inline char* MallocAndMemsetString(size_t size) { 972 char *s = Ident((char*)malloc(size)); 973 memset(s, 'z', size); 974 return s; 975} 976 977#ifndef __APPLE__ 978TEST(AddressSanitizer, StrNLenOOBTest) { 979 size_t size = Ident(123); 980 char *str = MallocAndMemsetString(size); 981 // Normal strnlen calls. 982 Ident(strnlen(str - 1, 0)); 983 Ident(strnlen(str, size)); 984 Ident(strnlen(str + size - 1, 1)); 985 str[size - 1] = '\0'; 986 Ident(strnlen(str, 2 * size)); 987 // Argument points to not allocated memory. 988 EXPECT_DEATH(Ident(strnlen(str - 1, 1)), LeftOOBErrorMessage(1)); 989 EXPECT_DEATH(Ident(strnlen(str + size, 1)), RightOOBErrorMessage(0)); 990 // Overwrite the terminating '\0' and hit unallocated memory. 991 str[size - 1] = 'z'; 992 EXPECT_DEATH(Ident(strnlen(str, size + 1)), RightOOBErrorMessage(0)); 993 free(str); 994} 995#endif 996 997TEST(AddressSanitizer, StrDupOOBTest) { 998 size_t size = Ident(42); 999 char *str = MallocAndMemsetString(size); 1000 char *new_str; 1001 // Normal strdup calls. 1002 str[size - 1] = '\0'; 1003 new_str = strdup(str); 1004 free(new_str); 1005 new_str = strdup(str + size - 1); 1006 free(new_str); 1007 // Argument points to not allocated memory. 1008 EXPECT_DEATH(Ident(strdup(str - 1)), LeftOOBErrorMessage(1)); 1009 EXPECT_DEATH(Ident(strdup(str + size)), RightOOBErrorMessage(0)); 1010 // Overwrite the terminating '\0' and hit unallocated memory. 1011 str[size - 1] = 'z'; 1012 EXPECT_DEATH(Ident(strdup(str)), RightOOBErrorMessage(0)); 1013 free(str); 1014} 1015 1016TEST(AddressSanitizer, StrCpyOOBTest) { 1017 size_t to_size = Ident(30); 1018 size_t from_size = Ident(6); // less than to_size 1019 char *to = Ident((char*)malloc(to_size)); 1020 char *from = Ident((char*)malloc(from_size)); 1021 // Normal strcpy calls. 1022 strcpy(from, "hello"); 1023 strcpy(to, from); 1024 strcpy(to + to_size - from_size, from); 1025 // Length of "from" is too small. 1026 EXPECT_DEATH(Ident(strcpy(from, "hello2")), RightOOBErrorMessage(0)); 1027 // "to" or "from" points to not allocated memory. 1028 EXPECT_DEATH(Ident(strcpy(to - 1, from)), LeftOOBErrorMessage(1)); 1029 EXPECT_DEATH(Ident(strcpy(to, from - 1)), LeftOOBErrorMessage(1)); 1030 EXPECT_DEATH(Ident(strcpy(to, from + from_size)), RightOOBErrorMessage(0)); 1031 EXPECT_DEATH(Ident(strcpy(to + to_size, from)), RightOOBErrorMessage(0)); 1032 // Overwrite the terminating '\0' character and hit unallocated memory. 1033 from[from_size - 1] = '!'; 1034 EXPECT_DEATH(Ident(strcpy(to, from)), RightOOBErrorMessage(0)); 1035 free(to); 1036 free(from); 1037} 1038 1039TEST(AddressSanitizer, StrNCpyOOBTest) { 1040 size_t to_size = Ident(20); 1041 size_t from_size = Ident(6); // less than to_size 1042 char *to = Ident((char*)malloc(to_size)); 1043 // From is a zero-terminated string "hello\0" of length 6 1044 char *from = Ident((char*)malloc(from_size)); 1045 strcpy(from, "hello"); 1046 // copy 0 bytes 1047 strncpy(to, from, 0); 1048 strncpy(to - 1, from - 1, 0); 1049 // normal strncpy calls 1050 strncpy(to, from, from_size); 1051 strncpy(to, from, to_size); 1052 strncpy(to, from + from_size - 1, to_size); 1053 strncpy(to + to_size - 1, from, 1); 1054 // One of {to, from} points to not allocated memory 1055 EXPECT_DEATH(Ident(strncpy(to, from - 1, from_size)), 1056 LeftOOBErrorMessage(1)); 1057 EXPECT_DEATH(Ident(strncpy(to - 1, from, from_size)), 1058 LeftOOBErrorMessage(1)); 1059 EXPECT_DEATH(Ident(strncpy(to, from + from_size, 1)), 1060 RightOOBErrorMessage(0)); 1061 EXPECT_DEATH(Ident(strncpy(to + to_size, from, 1)), 1062 RightOOBErrorMessage(0)); 1063 // Length of "to" is too small 1064 EXPECT_DEATH(Ident(strncpy(to + to_size - from_size + 1, from, from_size)), 1065 RightOOBErrorMessage(0)); 1066 EXPECT_DEATH(Ident(strncpy(to + 1, from, to_size)), 1067 RightOOBErrorMessage(0)); 1068 // Overwrite terminator in from 1069 from[from_size - 1] = '!'; 1070 // normal strncpy call 1071 strncpy(to, from, from_size); 1072 // Length of "from" is too small 1073 EXPECT_DEATH(Ident(strncpy(to, from, to_size)), 1074 RightOOBErrorMessage(0)); 1075 free(to); 1076 free(from); 1077} 1078 1079typedef char*(*PointerToStrChr)(const char*, int); 1080void RunStrChrTest(PointerToStrChr StrChr) { 1081 size_t size = Ident(100); 1082 char *str = MallocAndMemsetString(size); 1083 str[10] = 'q'; 1084 str[11] = '\0'; 1085 EXPECT_EQ(str, StrChr(str, 'z')); 1086 EXPECT_EQ(str + 10, StrChr(str, 'q')); 1087 EXPECT_EQ(NULL, StrChr(str, 'a')); 1088 // StrChr argument points to not allocated memory. 1089 EXPECT_DEATH(Ident(StrChr(str - 1, 'z')), LeftOOBErrorMessage(1)); 1090 EXPECT_DEATH(Ident(StrChr(str + size, 'z')), RightOOBErrorMessage(0)); 1091 // Overwrite the terminator and hit not allocated memory. 1092 str[11] = 'z'; 1093 EXPECT_DEATH(Ident(StrChr(str, 'a')), RightOOBErrorMessage(0)); 1094 free(str); 1095} 1096TEST(AddressSanitizer, StrChrAndIndexOOBTest) { 1097 RunStrChrTest(&strchr); 1098 RunStrChrTest(&index); 1099} 1100 1101TEST(AddressSanitizer, StrCmpAndFriendsLogicTest) { 1102 // strcmp 1103 EXPECT_EQ(0, strcmp("", "")); 1104 EXPECT_EQ(0, strcmp("abcd", "abcd")); 1105 EXPECT_GT(0, strcmp("ab", "ac")); 1106 EXPECT_GT(0, strcmp("abc", "abcd")); 1107 EXPECT_LT(0, strcmp("acc", "abc")); 1108 EXPECT_LT(0, strcmp("abcd", "abc")); 1109 1110 // strncmp 1111 EXPECT_EQ(0, strncmp("a", "b", 0)); 1112 EXPECT_EQ(0, strncmp("abcd", "abcd", 10)); 1113 EXPECT_EQ(0, strncmp("abcd", "abcef", 3)); 1114 EXPECT_GT(0, strncmp("abcde", "abcfa", 4)); 1115 EXPECT_GT(0, strncmp("a", "b", 5)); 1116 EXPECT_GT(0, strncmp("bc", "bcde", 4)); 1117 EXPECT_LT(0, strncmp("xyz", "xyy", 10)); 1118 EXPECT_LT(0, strncmp("baa", "aaa", 1)); 1119 EXPECT_LT(0, strncmp("zyx", "", 2)); 1120 1121 // strcasecmp 1122 EXPECT_EQ(0, strcasecmp("", "")); 1123 EXPECT_EQ(0, strcasecmp("zzz", "zzz")); 1124 EXPECT_EQ(0, strcasecmp("abCD", "ABcd")); 1125 EXPECT_GT(0, strcasecmp("aB", "Ac")); 1126 EXPECT_GT(0, strcasecmp("ABC", "ABCd")); 1127 EXPECT_LT(0, strcasecmp("acc", "abc")); 1128 EXPECT_LT(0, strcasecmp("ABCd", "abc")); 1129 1130 // strncasecmp 1131 EXPECT_EQ(0, strncasecmp("a", "b", 0)); 1132 EXPECT_EQ(0, strncasecmp("abCD", "ABcd", 10)); 1133 EXPECT_EQ(0, strncasecmp("abCd", "ABcef", 3)); 1134 EXPECT_GT(0, strncasecmp("abcde", "ABCfa", 4)); 1135 EXPECT_GT(0, strncasecmp("a", "B", 5)); 1136 EXPECT_GT(0, strncasecmp("bc", "BCde", 4)); 1137 EXPECT_LT(0, strncasecmp("xyz", "xyy", 10)); 1138 EXPECT_LT(0, strncasecmp("Baa", "aaa", 1)); 1139 EXPECT_LT(0, strncasecmp("zyx", "", 2)); 1140 1141 // memcmp 1142 EXPECT_EQ(0, memcmp("a", "b", 0)); 1143 EXPECT_EQ(0, memcmp("ab\0c", "ab\0c", 4)); 1144 EXPECT_GT(0, memcmp("\0ab", "\0ac", 3)); 1145 EXPECT_GT(0, memcmp("abb\0", "abba", 4)); 1146 EXPECT_LT(0, memcmp("ab\0cd", "ab\0c\0", 5)); 1147 EXPECT_LT(0, memcmp("zza", "zyx", 3)); 1148} 1149 1150typedef int(*PointerToStrCmp)(const char*, const char*); 1151void RunStrCmpTest(PointerToStrCmp StrCmp) { 1152 size_t size = Ident(100); 1153 char *s1 = MallocAndMemsetString(size); 1154 char *s2 = MallocAndMemsetString(size); 1155 s1[size - 1] = '\0'; 1156 s2[size - 1] = '\0'; 1157 // Normal StrCmp calls 1158 Ident(StrCmp(s1, s2)); 1159 Ident(StrCmp(s1, s2 + size - 1)); 1160 Ident(StrCmp(s1 + size - 1, s2 + size - 1)); 1161 s1[size - 1] = 'z'; 1162 s2[size - 1] = 'x'; 1163 Ident(StrCmp(s1, s2)); 1164 // One of arguments points to not allocated memory. 1165 EXPECT_DEATH(Ident(StrCmp)(s1 - 1, s2), LeftOOBErrorMessage(1)); 1166 EXPECT_DEATH(Ident(StrCmp)(s1, s2 - 1), LeftOOBErrorMessage(1)); 1167 EXPECT_DEATH(Ident(StrCmp)(s1 + size, s2), RightOOBErrorMessage(0)); 1168 EXPECT_DEATH(Ident(StrCmp)(s1, s2 + size), RightOOBErrorMessage(0)); 1169 // Hit unallocated memory and die. 1170 s2[size - 1] = 'z'; 1171 EXPECT_DEATH(Ident(StrCmp)(s1, s1), RightOOBErrorMessage(0)); 1172 EXPECT_DEATH(Ident(StrCmp)(s1 + size - 1, s2), RightOOBErrorMessage(0)); 1173 free(s1); 1174 free(s2); 1175} 1176 1177TEST(AddressSanitizer, StrCmpOOBTest) { 1178 RunStrCmpTest(&strcmp); 1179} 1180 1181TEST(AddressSanitizer, StrCaseCmpOOBTest) { 1182 RunStrCmpTest(&strcasecmp); 1183} 1184 1185typedef int(*PointerToStrNCmp)(const char*, const char*, size_t); 1186void RunStrNCmpTest(PointerToStrNCmp StrNCmp) { 1187 size_t size = Ident(100); 1188 char *s1 = MallocAndMemsetString(size); 1189 char *s2 = MallocAndMemsetString(size); 1190 s1[size - 1] = '\0'; 1191 s2[size - 1] = '\0'; 1192 // Normal StrNCmp calls 1193 Ident(StrNCmp(s1, s2, size + 2)); 1194 s1[size - 1] = 'z'; 1195 s2[size - 1] = 'x'; 1196 Ident(StrNCmp(s1 + size - 2, s2 + size - 2, size)); 1197 s2[size - 1] = 'z'; 1198 Ident(StrNCmp(s1 - 1, s2 - 1, 0)); 1199 Ident(StrNCmp(s1 + size - 1, s2 + size - 1, 1)); 1200 // One of arguments points to not allocated memory. 1201 EXPECT_DEATH(Ident(StrNCmp)(s1 - 1, s2, 1), LeftOOBErrorMessage(1)); 1202 EXPECT_DEATH(Ident(StrNCmp)(s1, s2 - 1, 1), LeftOOBErrorMessage(1)); 1203 EXPECT_DEATH(Ident(StrNCmp)(s1 + size, s2, 1), RightOOBErrorMessage(0)); 1204 EXPECT_DEATH(Ident(StrNCmp)(s1, s2 + size, 1), RightOOBErrorMessage(0)); 1205 // Hit unallocated memory and die. 1206 EXPECT_DEATH(Ident(StrNCmp)(s1 + 1, s2 + 1, size), RightOOBErrorMessage(0)); 1207 EXPECT_DEATH(Ident(StrNCmp)(s1 + size - 1, s2, 2), RightOOBErrorMessage(0)); 1208 free(s1); 1209 free(s2); 1210} 1211 1212TEST(AddressSanitizer, StrNCmpOOBTest) { 1213 RunStrNCmpTest(&strncmp); 1214} 1215 1216TEST(AddressSanitizer, StrNCaseCmpOOBTest) { 1217 RunStrNCmpTest(&strncasecmp); 1218} 1219 1220TEST(AddressSanitizer, MemCmpOOBTest) { 1221 size_t size = Ident(100); 1222 char *s1 = MallocAndMemsetString(size); 1223 char *s2 = MallocAndMemsetString(size); 1224 // Normal memcmp calls. 1225 Ident(memcmp(s1, s2, size)); 1226 Ident(memcmp(s1 + size - 1, s2 + size - 1, 1)); 1227 Ident(memcmp(s1 - 1, s2 - 1, 0)); 1228 // One of arguments points to not allocated memory. 1229 EXPECT_DEATH(Ident(memcmp)(s1 - 1, s2, 1), LeftOOBErrorMessage(1)); 1230 EXPECT_DEATH(Ident(memcmp)(s1, s2 - 1, 1), LeftOOBErrorMessage(1)); 1231 EXPECT_DEATH(Ident(memcmp)(s1 + size, s2, 1), RightOOBErrorMessage(0)); 1232 EXPECT_DEATH(Ident(memcmp)(s1, s2 + size, 1), RightOOBErrorMessage(0)); 1233 // Hit unallocated memory and die. 1234 EXPECT_DEATH(Ident(memcmp)(s1 + 1, s2 + 1, size), RightOOBErrorMessage(0)); 1235 EXPECT_DEATH(Ident(memcmp)(s1 + size - 1, s2, 2), RightOOBErrorMessage(0)); 1236 // Zero bytes are not terminators and don't prevent from OOB. 1237 s1[size - 1] = '\0'; 1238 s2[size - 1] = '\0'; 1239 EXPECT_DEATH(Ident(memcmp)(s1, s2, size + 1), RightOOBErrorMessage(0)); 1240 free(s1); 1241 free(s2); 1242} 1243 1244TEST(AddressSanitizer, StrCatOOBTest) { 1245 size_t to_size = Ident(100); 1246 char *to = MallocAndMemsetString(to_size); 1247 to[0] = '\0'; 1248 size_t from_size = Ident(20); 1249 char *from = MallocAndMemsetString(from_size); 1250 from[from_size - 1] = '\0'; 1251 // Normal strcat calls. 1252 strcat(to, from); 1253 strcat(to, from); 1254 strcat(to + from_size, from + from_size - 2); 1255 // Catenate empty string is not always an error. 1256 strcat(to - 1, from + from_size - 1); 1257 // One of arguments points to not allocated memory. 1258 EXPECT_DEATH(strcat(to - 1, from), LeftOOBErrorMessage(1)); 1259 EXPECT_DEATH(strcat(to, from - 1), LeftOOBErrorMessage(1)); 1260 EXPECT_DEATH(strcat(to + to_size, from), RightOOBErrorMessage(0)); 1261 EXPECT_DEATH(strcat(to, from + from_size), RightOOBErrorMessage(0)); 1262 1263 // "from" is not zero-terminated. 1264 from[from_size - 1] = 'z'; 1265 EXPECT_DEATH(strcat(to, from), RightOOBErrorMessage(0)); 1266 from[from_size - 1] = '\0'; 1267 // "to" is not zero-terminated. 1268 memset(to, 'z', to_size); 1269 EXPECT_DEATH(strcat(to, from), RightOOBErrorMessage(0)); 1270 // "to" is too short to fit "from". 1271 to[to_size - from_size + 1] = '\0'; 1272 EXPECT_DEATH(strcat(to, from), RightOOBErrorMessage(0)); 1273 // length of "to" is just enough. 1274 strcat(to, from + 1); 1275} 1276 1277static string OverlapErrorMessage(const string &func) { 1278 return func + "-param-overlap"; 1279} 1280 1281TEST(AddressSanitizer, StrArgsOverlapTest) { 1282 size_t size = Ident(100); 1283 char *str = Ident((char*)malloc(size)); 1284 1285 // Check "memcpy". Use Ident() to avoid inlining. 1286 memset(str, 'z', size); 1287 Ident(memcpy)(str + 1, str + 11, 10); 1288 Ident(memcpy)(str, str, 0); 1289 EXPECT_DEATH(Ident(memcpy)(str, str + 14, 15), OverlapErrorMessage("memcpy")); 1290 EXPECT_DEATH(Ident(memcpy)(str + 14, str, 15), OverlapErrorMessage("memcpy")); 1291 EXPECT_DEATH(Ident(memcpy)(str + 20, str + 20, 1), 1292 OverlapErrorMessage("memcpy")); 1293 1294 // Check "strcpy". 1295 memset(str, 'z', size); 1296 str[9] = '\0'; 1297 strcpy(str + 10, str); 1298 EXPECT_DEATH(strcpy(str + 9, str), OverlapErrorMessage("strcpy")); 1299 EXPECT_DEATH(strcpy(str, str + 4), OverlapErrorMessage("strcpy")); 1300 strcpy(str, str + 5); 1301 1302 // Check "strncpy". 1303 memset(str, 'z', size); 1304 strncpy(str, str + 10, 10); 1305 EXPECT_DEATH(strncpy(str, str + 9, 10), OverlapErrorMessage("strncpy")); 1306 EXPECT_DEATH(strncpy(str + 9, str, 10), OverlapErrorMessage("strncpy")); 1307 str[10] = '\0'; 1308 strncpy(str + 11, str, 20); 1309 EXPECT_DEATH(strncpy(str + 10, str, 20), OverlapErrorMessage("strncpy")); 1310 1311 // Check "strcat". 1312 memset(str, 'z', size); 1313 str[10] = '\0'; 1314 str[20] = '\0'; 1315 strcat(str, str + 10); 1316 strcat(str, str + 11); 1317 str[10] = '\0'; 1318 strcat(str + 11, str); 1319 EXPECT_DEATH(strcat(str, str + 9), OverlapErrorMessage("strcat")); 1320 EXPECT_DEATH(strcat(str + 9, str), OverlapErrorMessage("strcat")); 1321 EXPECT_DEATH(strcat(str + 10, str), OverlapErrorMessage("strcat")); 1322 1323 free(str); 1324} 1325 1326// At the moment we instrument memcpy/memove/memset calls at compile time so we 1327// can't handle OOB error if these functions are called by pointer, see disabled 1328// MemIntrinsicCallByPointerTest below 1329typedef void*(*PointerToMemTransfer)(void*, const void*, size_t); 1330typedef void*(*PointerToMemSet)(void*, int, size_t); 1331 1332void CallMemSetByPointer(PointerToMemSet MemSet) { 1333 size_t size = Ident(100); 1334 char *array = Ident((char*)malloc(size)); 1335 EXPECT_DEATH(MemSet(array, 0, 101), RightOOBErrorMessage(0)); 1336 free(array); 1337} 1338 1339void CallMemTransferByPointer(PointerToMemTransfer MemTransfer) { 1340 size_t size = Ident(100); 1341 char *src = Ident((char*)malloc(size)); 1342 char *dst = Ident((char*)malloc(size)); 1343 EXPECT_DEATH(MemTransfer(dst, src, 101), RightOOBErrorMessage(0)); 1344 free(src); 1345 free(dst); 1346} 1347 1348TEST(AddressSanitizer, DISABLED_MemIntrinsicCallByPointerTest) { 1349 CallMemSetByPointer(&memset); 1350 CallMemTransferByPointer(&memcpy); 1351 CallMemTransferByPointer(&memmove); 1352} 1353 1354// This test case fails 1355// Clang optimizes memcpy/memset calls which lead to unaligned access 1356TEST(AddressSanitizer, DISABLED_MemIntrinsicUnalignedAccessTest) { 1357 int size = Ident(4096); 1358 char *s = Ident((char*)malloc(size)); 1359 EXPECT_DEATH(memset(s + size - 1, 0, 2), RightOOBErrorMessage(0)); 1360 free(s); 1361} 1362 1363// TODO(samsonov): Add a test with malloc(0) 1364// TODO(samsonov): Add tests for str* and mem* functions. 1365 1366__attribute__((noinline)) 1367static int LargeFunction(bool do_bad_access) { 1368 int *x = new int[100]; 1369 x[0]++; 1370 x[1]++; 1371 x[2]++; 1372 x[3]++; 1373 x[4]++; 1374 x[5]++; 1375 x[6]++; 1376 x[7]++; 1377 x[8]++; 1378 x[9]++; 1379 1380 x[do_bad_access ? 100 : 0]++; int res = __LINE__; 1381 1382 x[10]++; 1383 x[11]++; 1384 x[12]++; 1385 x[13]++; 1386 x[14]++; 1387 x[15]++; 1388 x[16]++; 1389 x[17]++; 1390 x[18]++; 1391 x[19]++; 1392 1393 delete x; 1394 return res; 1395} 1396 1397// Test the we have correct debug info for the failing instruction. 1398// This test requires the in-process symbolizer to be enabled by default. 1399TEST(AddressSanitizer, DISABLED_LargeFunctionSymbolizeTest) { 1400 int failing_line = LargeFunction(false); 1401 char expected_warning[128]; 1402 sprintf(expected_warning, "LargeFunction.*asan_test.cc:%d", failing_line); 1403 EXPECT_DEATH(LargeFunction(true), expected_warning); 1404} 1405 1406// Check that we unwind and symbolize correctly. 1407TEST(AddressSanitizer, DISABLED_MallocFreeUnwindAndSymbolizeTest) { 1408 int *a = (int*)malloc_aaa(sizeof(int)); 1409 *a = 1; 1410 free_aaa(a); 1411 EXPECT_DEATH(*a = 1, "free_ccc.*free_bbb.*free_aaa.*" 1412 "malloc_fff.*malloc_eee.*malloc_ddd"); 1413} 1414 1415void *ThreadedTestAlloc(void *a) { 1416 int **p = (int**)a; 1417 *p = new int; 1418 return 0; 1419} 1420 1421void *ThreadedTestFree(void *a) { 1422 int **p = (int**)a; 1423 delete *p; 1424 return 0; 1425} 1426 1427void *ThreadedTestUse(void *a) { 1428 int **p = (int**)a; 1429 **p = 1; 1430 return 0; 1431} 1432 1433void ThreadedTestSpawn() { 1434 pthread_t t; 1435 int *x; 1436 pthread_create(&t, 0, ThreadedTestAlloc, &x); 1437 pthread_join(t, 0); 1438 pthread_create(&t, 0, ThreadedTestFree, &x); 1439 pthread_join(t, 0); 1440 pthread_create(&t, 0, ThreadedTestUse, &x); 1441 pthread_join(t, 0); 1442} 1443 1444TEST(AddressSanitizer, ThreadedTest) { 1445 EXPECT_DEATH(ThreadedTestSpawn(), 1446 ASAN_PCRE_DOTALL 1447 "Thread T.*created" 1448 ".*Thread T.*created" 1449 ".*Thread T.*created"); 1450} 1451 1452#if ASAN_NEEDS_SEGV 1453TEST(AddressSanitizer, ShadowGapTest) { 1454#if __WORDSIZE == 32 1455 char *addr = (char*)0x22000000; 1456#else 1457 char *addr = (char*)0x0000100000080000; 1458#endif 1459 EXPECT_DEATH(*addr = 1, "AddressSanitizer crashed on unknown"); 1460} 1461#endif // ASAN_NEEDS_SEGV 1462 1463extern "C" { 1464__attribute__((noinline)) 1465static void UseThenFreeThenUse() { 1466 char *x = Ident((char*)malloc(8)); 1467 *x = 1; 1468 free_aaa(x); 1469 *x = 2; 1470} 1471} 1472 1473TEST(AddressSanitizer, UseThenFreeThenUseTest) { 1474 EXPECT_DEATH(UseThenFreeThenUse(), "freed by thread"); 1475} 1476 1477TEST(AddressSanitizer, StrDupTest) { 1478 free(strdup(Ident("123"))); 1479} 1480 1481// Currently we create and poison redzone at right of global variables. 1482char glob5[5]; 1483static char static110[110]; 1484const char ConstGlob[7] = {1, 2, 3, 4, 5, 6, 7}; 1485static const char StaticConstGlob[3] = {9, 8, 7}; 1486extern int GlobalsTest(int x); 1487 1488TEST(AddressSanitizer, GlobalTest) { 1489 static char func_static15[15]; 1490 1491 static char fs1[10]; 1492 static char fs2[10]; 1493 static char fs3[10]; 1494 1495 glob5[Ident(0)] = 0; 1496 glob5[Ident(1)] = 0; 1497 glob5[Ident(2)] = 0; 1498 glob5[Ident(3)] = 0; 1499 glob5[Ident(4)] = 0; 1500 1501 EXPECT_DEATH(glob5[Ident(5)] = 0, 1502 "0 bytes to the right of global variable.*glob5.* size 5"); 1503 EXPECT_DEATH(glob5[Ident(5+6)] = 0, 1504 "6 bytes to the right of global variable.*glob5.* size 5"); 1505 Ident(static110); // avoid optimizations 1506 static110[Ident(0)] = 0; 1507 static110[Ident(109)] = 0; 1508 EXPECT_DEATH(static110[Ident(110)] = 0, 1509 "0 bytes to the right of global variable"); 1510 EXPECT_DEATH(static110[Ident(110+7)] = 0, 1511 "7 bytes to the right of global variable"); 1512 1513 Ident(func_static15); // avoid optimizations 1514 func_static15[Ident(0)] = 0; 1515 EXPECT_DEATH(func_static15[Ident(15)] = 0, 1516 "0 bytes to the right of global variable"); 1517 EXPECT_DEATH(func_static15[Ident(15 + 9)] = 0, 1518 "9 bytes to the right of global variable"); 1519 1520 Ident(fs1); 1521 Ident(fs2); 1522 Ident(fs3); 1523 1524 // We don't create left redzones, so this is not 100% guaranteed to fail. 1525 // But most likely will. 1526 EXPECT_DEATH(fs2[Ident(-1)] = 0, "is located.*of global variable"); 1527 1528 EXPECT_DEATH(Ident(Ident(ConstGlob)[8]), 1529 "is located 1 bytes to the right of .*ConstGlob"); 1530 EXPECT_DEATH(Ident(Ident(StaticConstGlob)[5]), 1531 "is located 2 bytes to the right of .*StaticConstGlob"); 1532 1533 // call stuff from another file. 1534 GlobalsTest(0); 1535} 1536 1537TEST(AddressSanitizer, GlobalStringConstTest) { 1538 static const char *zoo = "FOOBAR123"; 1539 const char *p = Ident(zoo); 1540 EXPECT_DEATH(Ident(p[15]), "is ascii string 'FOOBAR123'"); 1541} 1542 1543TEST(AddressSanitizer, FileNameInGlobalReportTest) { 1544 static char zoo[10]; 1545 const char *p = Ident(zoo); 1546 // The file name should be present in the report. 1547 EXPECT_DEATH(Ident(p[15]), "zoo.*asan_test.cc"); 1548} 1549 1550int *ReturnsPointerToALocalObject() { 1551 int a = 0; 1552 return Ident(&a); 1553} 1554 1555#if ASAN_UAR == 1 1556TEST(AddressSanitizer, LocalReferenceReturnTest) { 1557 int *(*f)() = Ident(ReturnsPointerToALocalObject); 1558 int *p = f(); 1559 // Call 'f' a few more times, 'p' should still be poisoned. 1560 for (int i = 0; i < 32; i++) 1561 f(); 1562 EXPECT_DEATH(*p = 1, "AddressSanitizer stack-use-after-return"); 1563 EXPECT_DEATH(*p = 1, "is located.*in frame .*ReturnsPointerToALocal"); 1564} 1565#endif 1566 1567template <int kSize> 1568__attribute__((noinline)) 1569static void FuncWithStack() { 1570 char x[kSize]; 1571 Ident(x)[0] = 0; 1572 Ident(x)[kSize-1] = 0; 1573} 1574 1575static void LotsOfStackReuse() { 1576 int LargeStack[10000]; 1577 Ident(LargeStack)[0] = 0; 1578 for (int i = 0; i < 10000; i++) { 1579 FuncWithStack<128 * 1>(); 1580 FuncWithStack<128 * 2>(); 1581 FuncWithStack<128 * 4>(); 1582 FuncWithStack<128 * 8>(); 1583 FuncWithStack<128 * 16>(); 1584 FuncWithStack<128 * 32>(); 1585 FuncWithStack<128 * 64>(); 1586 FuncWithStack<128 * 128>(); 1587 FuncWithStack<128 * 256>(); 1588 FuncWithStack<128 * 512>(); 1589 Ident(LargeStack)[0] = 0; 1590 } 1591} 1592 1593TEST(AddressSanitizer, StressStackReuseTest) { 1594 LotsOfStackReuse(); 1595} 1596 1597TEST(AddressSanitizer, ThreadedStressStackReuseTest) { 1598 const int kNumThreads = 20; 1599 pthread_t t[kNumThreads]; 1600 for (int i = 0; i < kNumThreads; i++) { 1601 pthread_create(&t[i], 0, (void* (*)(void *x))LotsOfStackReuse, 0); 1602 } 1603 for (int i = 0; i < kNumThreads; i++) { 1604 pthread_join(t[i], 0); 1605 } 1606} 1607 1608#ifdef __EXCEPTIONS 1609__attribute__((noinline)) 1610static void StackReuseAndException() { 1611 int large_stack[1000]; 1612 Ident(large_stack); 1613 ASAN_THROW(1); 1614} 1615 1616// TODO(kcc): support exceptions with use-after-return. 1617TEST(AddressSanitizer, DISABLED_StressStackReuseAndExceptionsTest) { 1618 for (int i = 0; i < 10000; i++) { 1619 try { 1620 StackReuseAndException(); 1621 } catch(...) { 1622 } 1623 } 1624} 1625#endif 1626 1627TEST(AddressSanitizer, MlockTest) { 1628 EXPECT_EQ(0, mlockall(MCL_CURRENT)); 1629 EXPECT_EQ(0, mlock((void*)0x12345, 0x5678)); 1630 EXPECT_EQ(0, munlockall()); 1631 EXPECT_EQ(0, munlock((void*)0x987, 0x654)); 1632} 1633 1634// ------------------ demo tests; run each one-by-one ------------- 1635// e.g. --gtest_filter=*DemoOOBLeftHigh --gtest_also_run_disabled_tests 1636TEST(AddressSanitizer, DISABLED_DemoThreadedTest) { 1637 ThreadedTestSpawn(); 1638} 1639 1640void *SimpleBugOnSTack(void *x = 0) { 1641 char a[20]; 1642 Ident(a)[20] = 0; 1643 return 0; 1644} 1645 1646TEST(AddressSanitizer, DISABLED_DemoStackTest) { 1647 SimpleBugOnSTack(); 1648} 1649 1650TEST(AddressSanitizer, DISABLED_DemoThreadStackTest) { 1651 pthread_t t; 1652 pthread_create(&t, 0, SimpleBugOnSTack, 0); 1653 pthread_join(t, 0); 1654} 1655 1656TEST(AddressSanitizer, DISABLED_DemoUAFLowIn) { 1657 uaf_test<U1>(10, 0); 1658} 1659TEST(AddressSanitizer, DISABLED_DemoUAFLowLeft) { 1660 uaf_test<U1>(10, -2); 1661} 1662TEST(AddressSanitizer, DISABLED_DemoUAFLowRight) { 1663 uaf_test<U1>(10, 10); 1664} 1665 1666TEST(AddressSanitizer, DISABLED_DemoUAFHigh) { 1667 uaf_test<U1>(kLargeMalloc, 0); 1668} 1669 1670TEST(AddressSanitizer, DISABLED_DemoOOBLeftLow) { 1671 oob_test<U1>(10, -1); 1672} 1673 1674TEST(AddressSanitizer, DISABLED_DemoOOBLeftHigh) { 1675 oob_test<U1>(kLargeMalloc, -1); 1676} 1677 1678TEST(AddressSanitizer, DISABLED_DemoOOBRightLow) { 1679 oob_test<U1>(10, 10); 1680} 1681 1682TEST(AddressSanitizer, DISABLED_DemoOOBRightHigh) { 1683 oob_test<U1>(kLargeMalloc, kLargeMalloc); 1684} 1685 1686TEST(AddressSanitizer, DISABLED_DemoOOM) { 1687 size_t size = __WORDSIZE == 64 ? (size_t)(1ULL << 40) : (0xf0000000); 1688 printf("%p\n", malloc(size)); 1689} 1690 1691TEST(AddressSanitizer, DISABLED_DemoDoubleFreeTest) { 1692 DoubleFree(); 1693} 1694 1695TEST(AddressSanitizer, DISABLED_DemoNullDerefTest) { 1696 int *a = 0; 1697 Ident(a)[10] = 0; 1698} 1699 1700TEST(AddressSanitizer, DISABLED_DemoFunctionStaticTest) { 1701 static char a[100]; 1702 static char b[100]; 1703 static char c[100]; 1704 Ident(a); 1705 Ident(b); 1706 Ident(c); 1707 Ident(a)[5] = 0; 1708 Ident(b)[105] = 0; 1709 Ident(a)[5] = 0; 1710} 1711 1712TEST(AddressSanitizer, DISABLED_DemoTooMuchMemoryTest) { 1713 const size_t kAllocSize = (1 << 28) - 1024; 1714 size_t total_size = 0; 1715 while (true) { 1716 char *x = (char*)malloc(kAllocSize); 1717 memset(x, 0, kAllocSize); 1718 total_size += kAllocSize; 1719 fprintf(stderr, "total: %ldM\n", (long)total_size >> 20); 1720 } 1721} 1722 1723#ifdef __APPLE__ 1724#include "asan_mac_test.h" 1725// TODO(glider): figure out whether we still need these tests. Is it correct 1726// to intercept CFAllocator? 1727TEST(AddressSanitizerMac, DISABLED_CFAllocatorDefaultDoubleFree) { 1728 EXPECT_DEATH( 1729 CFAllocatorDefaultDoubleFree(), 1730 "attempting double-free"); 1731} 1732 1733TEST(AddressSanitizerMac, DISABLED_CFAllocatorSystemDefaultDoubleFree) { 1734 EXPECT_DEATH( 1735 CFAllocatorSystemDefaultDoubleFree(), 1736 "attempting double-free"); 1737} 1738 1739TEST(AddressSanitizerMac, DISABLED_CFAllocatorMallocDoubleFree) { 1740 EXPECT_DEATH(CFAllocatorMallocDoubleFree(), "attempting double-free"); 1741} 1742 1743TEST(AddressSanitizerMac, DISABLED_CFAllocatorMallocZoneDoubleFree) { 1744 EXPECT_DEATH(CFAllocatorMallocZoneDoubleFree(), "attempting double-free"); 1745} 1746 1747TEST(AddressSanitizerMac, GCDDispatchAsync) { 1748 // Make sure the whole ASan report is printed, i.e. that we don't die 1749 // on a CHECK. 1750 EXPECT_DEATH(TestGCDDispatchAsync(), "Shadow byte and word"); 1751} 1752 1753TEST(AddressSanitizerMac, GCDDispatchSync) { 1754 // Make sure the whole ASan report is printed, i.e. that we don't die 1755 // on a CHECK. 1756 EXPECT_DEATH(TestGCDDispatchSync(), "Shadow byte and word"); 1757} 1758 1759 1760TEST(AddressSanitizerMac, GCDReuseWqthreadsAsync) { 1761 // Make sure the whole ASan report is printed, i.e. that we don't die 1762 // on a CHECK. 1763 EXPECT_DEATH(TestGCDReuseWqthreadsAsync(), "Shadow byte and word"); 1764} 1765 1766TEST(AddressSanitizerMac, GCDReuseWqthreadsSync) { 1767 // Make sure the whole ASan report is printed, i.e. that we don't die 1768 // on a CHECK. 1769 EXPECT_DEATH(TestGCDReuseWqthreadsSync(), "Shadow byte and word"); 1770} 1771 1772TEST(AddressSanitizerMac, GCDDispatchAfter) { 1773 // Make sure the whole ASan report is printed, i.e. that we don't die 1774 // on a CHECK. 1775 EXPECT_DEATH(TestGCDDispatchAfter(), "Shadow byte and word"); 1776} 1777 1778TEST(AddressSanitizerMac, GCDSourceEvent) { 1779 // Make sure the whole ASan report is printed, i.e. that we don't die 1780 // on a CHECK. 1781 EXPECT_DEATH(TestGCDSourceEvent(), "Shadow byte and word"); 1782} 1783 1784TEST(AddressSanitizerMac, GCDSourceCancel) { 1785 // Make sure the whole ASan report is printed, i.e. that we don't die 1786 // on a CHECK. 1787 EXPECT_DEATH(TestGCDSourceCancel(), "Shadow byte and word"); 1788} 1789 1790TEST(AddressSanitizerMac, GCDGroupAsync) { 1791 // Make sure the whole ASan report is printed, i.e. that we don't die 1792 // on a CHECK. 1793 EXPECT_DEATH(TestGCDGroupAsync(), "Shadow byte and word"); 1794} 1795 1796void *MallocIntrospectionLockWorker(void *_) { 1797 const int kNumPointers = 100; 1798 int i; 1799 void *pointers[kNumPointers]; 1800 for (i = 0; i < kNumPointers; i++) { 1801 pointers[i] = malloc(i + 1); 1802 } 1803 for (i = 0; i < kNumPointers; i++) { 1804 free(pointers[i]); 1805 } 1806 1807 return NULL; 1808} 1809 1810void *MallocIntrospectionLockForker(void *_) { 1811 pid_t result = fork(); 1812 if (result == -1) { 1813 perror("fork"); 1814 } 1815 assert(result != -1); 1816 if (result == 0) { 1817 // Call malloc in the child process to make sure we won't deadlock. 1818 void *ptr = malloc(42); 1819 free(ptr); 1820 exit(0); 1821 } else { 1822 // Return in the parent process. 1823 return NULL; 1824 } 1825} 1826 1827TEST(AddressSanitizerMac, MallocIntrospectionLock) { 1828 // Incorrect implementation of force_lock and force_unlock in our malloc zone 1829 // will cause forked processes to deadlock. 1830 // TODO(glider): need to detect that none of the child processes deadlocked. 1831 const int kNumWorkers = 5, kNumIterations = 100; 1832 int i, iter; 1833 for (iter = 0; iter < kNumIterations; iter++) { 1834 pthread_t workers[kNumWorkers], forker; 1835 for (i = 0; i < kNumWorkers; i++) { 1836 pthread_create(&workers[i], 0, MallocIntrospectionLockWorker, 0); 1837 } 1838 pthread_create(&forker, 0, MallocIntrospectionLockForker, 0); 1839 for (i = 0; i < kNumWorkers; i++) { 1840 pthread_join(workers[i], 0); 1841 } 1842 pthread_join(forker, 0); 1843 } 1844} 1845 1846void *TSDAllocWorker(void *test_key) { 1847 if (test_key) { 1848 void *mem = malloc(10); 1849 pthread_setspecific(*(pthread_key_t*)test_key, mem); 1850 } 1851 return NULL; 1852} 1853 1854TEST(AddressSanitizerMac, DISABLED_TSDWorkqueueTest) { 1855 pthread_t th; 1856 pthread_key_t test_key; 1857 pthread_key_create(&test_key, CallFreeOnWorkqueue); 1858 pthread_create(&th, NULL, TSDAllocWorker, &test_key); 1859 pthread_join(th, NULL); 1860 pthread_key_delete(test_key); 1861} 1862#endif // __APPLE__ 1863 1864int main(int argc, char **argv) { 1865 progname = argv[0]; 1866 testing::GTEST_FLAG(death_test_style) = "threadsafe"; 1867 testing::InitGoogleTest(&argc, argv); 1868 return RUN_ALL_TESTS(); 1869} 1870