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