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