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