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