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