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