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