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