Home | History | Annotate | Download | only in tests
      1 //===-- asan_test.cc ------------------------------------------------------===//
      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 "asan_test_utils.h"
     14 
     15 NOINLINE void *malloc_fff(size_t size) {
     16   void *res = malloc/**/(size); break_optimization(0); return res;}
     17 NOINLINE void *malloc_eee(size_t size) {
     18   void *res = malloc_fff(size); break_optimization(0); return res;}
     19 NOINLINE void *malloc_ddd(size_t size) {
     20   void *res = malloc_eee(size); break_optimization(0); return res;}
     21 NOINLINE void *malloc_ccc(size_t size) {
     22   void *res = malloc_ddd(size); break_optimization(0); return res;}
     23 NOINLINE void *malloc_bbb(size_t size) {
     24   void *res = malloc_ccc(size); break_optimization(0); return res;}
     25 NOINLINE void *malloc_aaa(size_t size) {
     26   void *res = malloc_bbb(size); break_optimization(0); return res;}
     27 
     28 #ifndef __APPLE__
     29 NOINLINE void *memalign_fff(size_t alignment, size_t size) {
     30   void *res = memalign/**/(alignment, size); break_optimization(0); return res;}
     31 NOINLINE void *memalign_eee(size_t alignment, size_t size) {
     32   void *res = memalign_fff(alignment, size); break_optimization(0); return res;}
     33 NOINLINE void *memalign_ddd(size_t alignment, size_t size) {
     34   void *res = memalign_eee(alignment, size); break_optimization(0); return res;}
     35 NOINLINE void *memalign_ccc(size_t alignment, size_t size) {
     36   void *res = memalign_ddd(alignment, size); break_optimization(0); return res;}
     37 NOINLINE void *memalign_bbb(size_t alignment, size_t size) {
     38   void *res = memalign_ccc(alignment, size); break_optimization(0); return res;}
     39 NOINLINE void *memalign_aaa(size_t alignment, size_t size) {
     40   void *res = memalign_bbb(alignment, size); break_optimization(0); return res;}
     41 #endif  // __APPLE__
     42 
     43 
     44 NOINLINE void free_ccc(void *p) { free(p); break_optimization(0);}
     45 NOINLINE void free_bbb(void *p) { free_ccc(p); break_optimization(0);}
     46 NOINLINE void free_aaa(void *p) { free_bbb(p); break_optimization(0);}
     47 
     48 
     49 template<typename T>
     50 NOINLINE void uaf_test(int size, int off) {
     51   char *p = (char *)malloc_aaa(size);
     52   free_aaa(p);
     53   for (int i = 1; i < 100; i++)
     54     free_aaa(malloc_aaa(i));
     55   fprintf(stderr, "writing %ld byte(s) at %p with offset %d\n",
     56           (long)sizeof(T), p, off);
     57   asan_write((T*)(p + off));
     58 }
     59 
     60 TEST(AddressSanitizer, HasFeatureAddressSanitizerTest) {
     61 #if defined(__has_feature) && __has_feature(address_sanitizer)
     62   bool asan = 1;
     63 #elif defined(__SANITIZE_ADDRESS__)
     64   bool asan = 1;
     65 #else
     66   bool asan = 0;
     67 #endif
     68   EXPECT_EQ(true, asan);
     69 }
     70 
     71 TEST(AddressSanitizer, SimpleDeathTest) {
     72   EXPECT_DEATH(exit(1), "");
     73 }
     74 
     75 TEST(AddressSanitizer, VariousMallocsTest) {
     76   int *a = (int*)malloc(100 * sizeof(int));
     77   a[50] = 0;
     78   free(a);
     79 
     80   int *r = (int*)malloc(10);
     81   r = (int*)realloc(r, 2000 * sizeof(int));
     82   r[1000] = 0;
     83   free(r);
     84 
     85   int *b = new int[100];
     86   b[50] = 0;
     87   delete [] b;
     88 
     89   int *c = new int;
     90   *c = 0;
     91   delete c;
     92 
     93 #if !defined(__APPLE__) && !defined(ANDROID) && !defined(__ANDROID__)
     94   int *pm;
     95   int pm_res = posix_memalign((void**)&pm, kPageSize, kPageSize);
     96   EXPECT_EQ(0, pm_res);
     97   free(pm);
     98 #endif
     99 
    100 #if !defined(__APPLE__)
    101   int *ma = (int*)memalign(kPageSize, kPageSize);
    102   EXPECT_EQ(0U, (uintptr_t)ma % kPageSize);
    103   ma[123] = 0;
    104   free(ma);
    105 #endif  // __APPLE__
    106 }
    107 
    108 TEST(AddressSanitizer, CallocTest) {
    109   int *a = (int*)calloc(100, sizeof(int));
    110   EXPECT_EQ(0, a[10]);
    111   free(a);
    112 }
    113 
    114 TEST(AddressSanitizer, VallocTest) {
    115   void *a = valloc(100);
    116   EXPECT_EQ(0U, (uintptr_t)a % kPageSize);
    117   free(a);
    118 }
    119 
    120 #ifndef __APPLE__
    121 TEST(AddressSanitizer, PvallocTest) {
    122   char *a = (char*)pvalloc(kPageSize + 100);
    123   EXPECT_EQ(0U, (uintptr_t)a % kPageSize);
    124   a[kPageSize + 101] = 1;  // we should not report an error here.
    125   free(a);
    126 
    127   a = (char*)pvalloc(0);  // pvalloc(0) should allocate at least one page.
    128   EXPECT_EQ(0U, (uintptr_t)a % kPageSize);
    129   a[101] = 1;  // we should not report an error here.
    130   free(a);
    131 }
    132 #endif  // __APPLE__
    133 
    134 void *TSDWorker(void *test_key) {
    135   if (test_key) {
    136     pthread_setspecific(*(pthread_key_t*)test_key, (void*)0xfeedface);
    137   }
    138   return NULL;
    139 }
    140 
    141 void TSDDestructor(void *tsd) {
    142   // Spawning a thread will check that the current thread id is not -1.
    143   pthread_t th;
    144   PTHREAD_CREATE(&th, NULL, TSDWorker, NULL);
    145   PTHREAD_JOIN(th, NULL);
    146 }
    147 
    148 // This tests triggers the thread-specific data destruction fiasco which occurs
    149 // if we don't manage the TSD destructors ourselves. We create a new pthread
    150 // key with a non-NULL destructor which is likely to be put after the destructor
    151 // of AsanThread in the list of destructors.
    152 // In this case the TSD for AsanThread will be destroyed before TSDDestructor
    153 // is called for the child thread, and a CHECK will fail when we call
    154 // pthread_create() to spawn the grandchild.
    155 TEST(AddressSanitizer, DISABLED_TSDTest) {
    156   pthread_t th;
    157   pthread_key_t test_key;
    158   pthread_key_create(&test_key, TSDDestructor);
    159   PTHREAD_CREATE(&th, NULL, TSDWorker, &test_key);
    160   PTHREAD_JOIN(th, NULL);
    161   pthread_key_delete(test_key);
    162 }
    163 
    164 TEST(AddressSanitizer, UAF_char) {
    165   const char *uaf_string = "AddressSanitizer:.*heap-use-after-free";
    166   EXPECT_DEATH(uaf_test<U1>(1, 0), uaf_string);
    167   EXPECT_DEATH(uaf_test<U1>(10, 0), uaf_string);
    168   EXPECT_DEATH(uaf_test<U1>(10, 10), uaf_string);
    169   EXPECT_DEATH(uaf_test<U1>(kLargeMalloc, 0), uaf_string);
    170   EXPECT_DEATH(uaf_test<U1>(kLargeMalloc, kLargeMalloc / 2), uaf_string);
    171 }
    172 
    173 TEST(AddressSanitizer, UAF_long_double) {
    174   if (sizeof(long double) == sizeof(double)) return;
    175   long double *p = Ident(new long double[10]);
    176   EXPECT_DEATH(Ident(p)[12] = 0, "WRITE of size 1[06]");
    177   EXPECT_DEATH(Ident(p)[0] = Ident(p)[12], "READ of size 1[06]");
    178   delete [] Ident(p);
    179 }
    180 
    181 struct Packed5 {
    182   int x;
    183   char c;
    184 } __attribute__((packed));
    185 
    186 
    187 TEST(AddressSanitizer, UAF_Packed5) {
    188   Packed5 *p = Ident(new Packed5[2]);
    189   EXPECT_DEATH(p[0] = p[3], "READ of size 5");
    190   EXPECT_DEATH(p[3] = p[0], "WRITE of size 5");
    191   delete [] Ident(p);
    192 }
    193 
    194 #if ASAN_HAS_BLACKLIST
    195 TEST(AddressSanitizer, IgnoreTest) {
    196   int *x = Ident(new int);
    197   delete Ident(x);
    198   *x = 0;
    199 }
    200 #endif  // ASAN_HAS_BLACKLIST
    201 
    202 struct StructWithBitField {
    203   int bf1:1;
    204   int bf2:1;
    205   int bf3:1;
    206   int bf4:29;
    207 };
    208 
    209 TEST(AddressSanitizer, BitFieldPositiveTest) {
    210   StructWithBitField *x = new StructWithBitField;
    211   delete Ident(x);
    212   EXPECT_DEATH(x->bf1 = 0, "use-after-free");
    213   EXPECT_DEATH(x->bf2 = 0, "use-after-free");
    214   EXPECT_DEATH(x->bf3 = 0, "use-after-free");
    215   EXPECT_DEATH(x->bf4 = 0, "use-after-free");
    216 }
    217 
    218 struct StructWithBitFields_8_24 {
    219   int a:8;
    220   int b:24;
    221 };
    222 
    223 TEST(AddressSanitizer, BitFieldNegativeTest) {
    224   StructWithBitFields_8_24 *x = Ident(new StructWithBitFields_8_24);
    225   x->a = 0;
    226   x->b = 0;
    227   delete Ident(x);
    228 }
    229 
    230 static size_t kOOMAllocationSize =
    231   SANITIZER_WORDSIZE == 64 ? (size_t)(1ULL << 48) : (0xf0000000);
    232 
    233 TEST(AddressSanitizer, OutOfMemoryTest) {
    234   EXPECT_EQ(0, realloc(0, kOOMAllocationSize));
    235   EXPECT_EQ(0, realloc(0, ~Ident(0)));
    236   EXPECT_EQ(0, malloc(kOOMAllocationSize));
    237   EXPECT_EQ(0, malloc(~Ident(0)));
    238   EXPECT_EQ(0, calloc(1, kOOMAllocationSize));
    239   EXPECT_EQ(0, calloc(1, ~Ident(0)));
    240 }
    241 
    242 TEST(AddressSanitizer, BadReallocTest) {
    243   int *a = (int*)Ident(malloc(100));
    244   a[0] = 42;
    245   EXPECT_EQ(0, realloc(a, kOOMAllocationSize));
    246   EXPECT_EQ(42, a[0]);
    247   free(a);
    248 }
    249 
    250 #if ASAN_NEEDS_SEGV
    251 namespace {
    252 
    253 const char kUnknownCrash[] = "AddressSanitizer: SEGV on unknown address";
    254 const char kOverriddenHandler[] = "ASan signal handler has been overridden\n";
    255 
    256 TEST(AddressSanitizer, WildAddressTest) {
    257   char *c = (char*)0x123;
    258   EXPECT_DEATH(*c = 0, kUnknownCrash);
    259 }
    260 
    261 void my_sigaction_sighandler(int, siginfo_t*, void*) {
    262   fprintf(stderr, kOverriddenHandler);
    263   exit(1);
    264 }
    265 
    266 void my_signal_sighandler(int signum) {
    267   fprintf(stderr, kOverriddenHandler);
    268   exit(1);
    269 }
    270 
    271 TEST(AddressSanitizer, SignalTest) {
    272   struct sigaction sigact;
    273   memset(&sigact, 0, sizeof(sigact));
    274   sigact.sa_sigaction = my_sigaction_sighandler;
    275   sigact.sa_flags = SA_SIGINFO;
    276   // ASan should silently ignore sigaction()...
    277   EXPECT_EQ(0, sigaction(SIGSEGV, &sigact, 0));
    278 #ifdef __APPLE__
    279   EXPECT_EQ(0, sigaction(SIGBUS, &sigact, 0));
    280 #endif
    281   char *c = (char*)0x123;
    282   EXPECT_DEATH(*c = 0, kUnknownCrash);
    283   // ... and signal().
    284   EXPECT_EQ(0, signal(SIGSEGV, my_signal_sighandler));
    285   EXPECT_DEATH(*c = 0, kUnknownCrash);
    286 }
    287 }  // namespace
    288 #endif
    289 
    290 static void TestLargeMalloc(size_t size) {
    291   char buff[1024];
    292   sprintf(buff, "is located 1 bytes to the left of %lu-byte", (long)size);
    293   EXPECT_DEATH(Ident((char*)malloc(size))[-1] = 0, buff);
    294 }
    295 
    296 TEST(AddressSanitizer, LargeMallocTest) {
    297   const int max_size = (SANITIZER_WORDSIZE == 32) ? 1 << 26 : 1 << 28;
    298   for (int i = 113; i < max_size; i = i * 2 + 13) {
    299     TestLargeMalloc(i);
    300   }
    301 }
    302 
    303 TEST(AddressSanitizer, HugeMallocTest) {
    304   if (SANITIZER_WORDSIZE != 64) return;
    305   size_t n_megs = 4100;
    306   TestLargeMalloc(n_megs << 20);
    307 }
    308 
    309 #ifndef __APPLE__
    310 void MemalignRun(size_t align, size_t size, int idx) {
    311   char *p = (char *)memalign(align, size);
    312   Ident(p)[idx] = 0;
    313   free(p);
    314 }
    315 
    316 TEST(AddressSanitizer, memalign) {
    317   for (int align = 16; align <= (1 << 23); align *= 2) {
    318     size_t size = align * 5;
    319     EXPECT_DEATH(MemalignRun(align, size, -1),
    320                  "is located 1 bytes to the left");
    321     EXPECT_DEATH(MemalignRun(align, size, size + 1),
    322                  "is located 1 bytes to the right");
    323   }
    324 }
    325 #endif
    326 
    327 void *ManyThreadsWorker(void *a) {
    328   for (int iter = 0; iter < 100; iter++) {
    329     for (size_t size = 100; size < 2000; size *= 2) {
    330       free(Ident(malloc(size)));
    331     }
    332   }
    333   return 0;
    334 }
    335 
    336 TEST(AddressSanitizer, ManyThreadsTest) {
    337   const size_t kNumThreads =
    338       (SANITIZER_WORDSIZE == 32 || ASAN_AVOID_EXPENSIVE_TESTS) ? 30 : 1000;
    339   pthread_t t[kNumThreads];
    340   for (size_t i = 0; i < kNumThreads; i++) {
    341     PTHREAD_CREATE(&t[i], 0, ManyThreadsWorker, (void*)i);
    342   }
    343   for (size_t i = 0; i < kNumThreads; i++) {
    344     PTHREAD_JOIN(t[i], 0);
    345   }
    346 }
    347 
    348 TEST(AddressSanitizer, ReallocTest) {
    349   const int kMinElem = 5;
    350   int *ptr = (int*)malloc(sizeof(int) * kMinElem);
    351   ptr[3] = 3;
    352   for (int i = 0; i < 10000; i++) {
    353     ptr = (int*)realloc(ptr,
    354         (my_rand() % 1000 + kMinElem) * sizeof(int));
    355     EXPECT_EQ(3, ptr[3]);
    356   }
    357   free(ptr);
    358   // Realloc pointer returned by malloc(0).
    359   int *ptr2 = Ident((int*)malloc(0));
    360   ptr2 = Ident((int*)realloc(ptr2, sizeof(*ptr2)));
    361   *ptr2 = 42;
    362   EXPECT_EQ(42, *ptr2);
    363   free(ptr2);
    364 }
    365 
    366 TEST(AddressSanitizer, ReallocFreedPointerTest) {
    367   void *ptr = Ident(malloc(42));
    368   ASSERT_TRUE(NULL != ptr);
    369   free(ptr);
    370   EXPECT_DEATH(ptr = realloc(ptr, 77), "attempting double-free");
    371 }
    372 
    373 TEST(AddressSanitizer, ReallocInvalidPointerTest) {
    374   void *ptr = Ident(malloc(42));
    375   EXPECT_DEATH(ptr = realloc((int*)ptr + 1, 77), "attempting free.*not malloc");
    376   free(ptr);
    377 }
    378 
    379 TEST(AddressSanitizer, ZeroSizeMallocTest) {
    380   // Test that malloc(0) and similar functions don't return NULL.
    381   void *ptr = Ident(malloc(0));
    382   EXPECT_TRUE(NULL != ptr);
    383   free(ptr);
    384 #if !defined(__APPLE__) && !defined(ANDROID) && !defined(__ANDROID__)
    385   int pm_res = posix_memalign(&ptr, 1<<20, 0);
    386   EXPECT_EQ(0, pm_res);
    387   EXPECT_TRUE(NULL != ptr);
    388   free(ptr);
    389 #endif
    390   int *int_ptr = new int[0];
    391   int *int_ptr2 = new int[0];
    392   EXPECT_TRUE(NULL != int_ptr);
    393   EXPECT_TRUE(NULL != int_ptr2);
    394   EXPECT_NE(int_ptr, int_ptr2);
    395   delete[] int_ptr;
    396   delete[] int_ptr2;
    397 }
    398 
    399 #ifndef __APPLE__
    400 static const char *kMallocUsableSizeErrorMsg =
    401   "AddressSanitizer: attempting to call malloc_usable_size()";
    402 
    403 TEST(AddressSanitizer, MallocUsableSizeTest) {
    404   const size_t kArraySize = 100;
    405   char *array = Ident((char*)malloc(kArraySize));
    406   int *int_ptr = Ident(new int);
    407   EXPECT_EQ(0U, malloc_usable_size(NULL));
    408   EXPECT_EQ(kArraySize, malloc_usable_size(array));
    409   EXPECT_EQ(sizeof(int), malloc_usable_size(int_ptr));
    410   EXPECT_DEATH(malloc_usable_size((void*)0x123), kMallocUsableSizeErrorMsg);
    411   EXPECT_DEATH(malloc_usable_size(array + kArraySize / 2),
    412                kMallocUsableSizeErrorMsg);
    413   free(array);
    414   EXPECT_DEATH(malloc_usable_size(array), kMallocUsableSizeErrorMsg);
    415   delete int_ptr;
    416 }
    417 #endif
    418 
    419 void WrongFree() {
    420   int *x = (int*)malloc(100 * sizeof(int));
    421   // Use the allocated memory, otherwise Clang will optimize it out.
    422   Ident(x);
    423   free(x + 1);
    424 }
    425 
    426 TEST(AddressSanitizer, WrongFreeTest) {
    427   EXPECT_DEATH(WrongFree(), ASAN_PCRE_DOTALL
    428                "ERROR: AddressSanitizer: attempting free.*not malloc"
    429                ".*is located 4 bytes inside of 400-byte region"
    430                ".*allocated by thread");
    431 }
    432 
    433 void DoubleFree() {
    434   int *x = (int*)malloc(100 * sizeof(int));
    435   fprintf(stderr, "DoubleFree: x=%p\n", x);
    436   free(x);
    437   free(x);
    438   fprintf(stderr, "should have failed in the second free(%p)\n", x);
    439   abort();
    440 }
    441 
    442 TEST(AddressSanitizer, DoubleFreeTest) {
    443   EXPECT_DEATH(DoubleFree(), ASAN_PCRE_DOTALL
    444                "ERROR: AddressSanitizer: attempting double-free"
    445                ".*is located 0 bytes inside of 400-byte region"
    446                ".*freed by thread T0 here"
    447                ".*previously allocated by thread T0 here");
    448 }
    449 
    450 template<int kSize>
    451 NOINLINE void SizedStackTest() {
    452   char a[kSize];
    453   char  *A = Ident((char*)&a);
    454   for (size_t i = 0; i < kSize; i++)
    455     A[i] = i;
    456   EXPECT_DEATH(A[-1] = 0, "");
    457   EXPECT_DEATH(A[-20] = 0, "");
    458   EXPECT_DEATH(A[-31] = 0, "");
    459   EXPECT_DEATH(A[kSize] = 0, "");
    460   EXPECT_DEATH(A[kSize + 1] = 0, "");
    461   EXPECT_DEATH(A[kSize + 10] = 0, "");
    462   EXPECT_DEATH(A[kSize + 31] = 0, "");
    463 }
    464 
    465 TEST(AddressSanitizer, SimpleStackTest) {
    466   SizedStackTest<1>();
    467   SizedStackTest<2>();
    468   SizedStackTest<3>();
    469   SizedStackTest<4>();
    470   SizedStackTest<5>();
    471   SizedStackTest<6>();
    472   SizedStackTest<7>();
    473   SizedStackTest<16>();
    474   SizedStackTest<25>();
    475   SizedStackTest<34>();
    476   SizedStackTest<43>();
    477   SizedStackTest<51>();
    478   SizedStackTest<62>();
    479   SizedStackTest<64>();
    480   SizedStackTest<128>();
    481 }
    482 
    483 TEST(AddressSanitizer, ManyStackObjectsTest) {
    484   char XXX[10];
    485   char YYY[20];
    486   char ZZZ[30];
    487   Ident(XXX);
    488   Ident(YYY);
    489   EXPECT_DEATH(Ident(ZZZ)[-1] = 0, ASAN_PCRE_DOTALL "XXX.*YYY.*ZZZ");
    490 }
    491 
    492 #if 0  // This test requires online symbolizer.
    493 // Moved to lit_tests/stack-oob-frames.cc.
    494 // Reenable here once we have online symbolizer by default.
    495 NOINLINE static void Frame0(int frame, char *a, char *b, char *c) {
    496   char d[4] = {0};
    497   char *D = Ident(d);
    498   switch (frame) {
    499     case 3: a[5]++; break;
    500     case 2: b[5]++; break;
    501     case 1: c[5]++; break;
    502     case 0: D[5]++; break;
    503   }
    504 }
    505 NOINLINE static void Frame1(int frame, char *a, char *b) {
    506   char c[4] = {0}; Frame0(frame, a, b, c);
    507   break_optimization(0);
    508 }
    509 NOINLINE static void Frame2(int frame, char *a) {
    510   char b[4] = {0}; Frame1(frame, a, b);
    511   break_optimization(0);
    512 }
    513 NOINLINE static void Frame3(int frame) {
    514   char a[4] = {0}; Frame2(frame, a);
    515   break_optimization(0);
    516 }
    517 
    518 TEST(AddressSanitizer, GuiltyStackFrame0Test) {
    519   EXPECT_DEATH(Frame3(0), "located .*in frame <.*Frame0");
    520 }
    521 TEST(AddressSanitizer, GuiltyStackFrame1Test) {
    522   EXPECT_DEATH(Frame3(1), "located .*in frame <.*Frame1");
    523 }
    524 TEST(AddressSanitizer, GuiltyStackFrame2Test) {
    525   EXPECT_DEATH(Frame3(2), "located .*in frame <.*Frame2");
    526 }
    527 TEST(AddressSanitizer, GuiltyStackFrame3Test) {
    528   EXPECT_DEATH(Frame3(3), "located .*in frame <.*Frame3");
    529 }
    530 #endif
    531 
    532 NOINLINE void LongJmpFunc1(jmp_buf buf) {
    533   // create three red zones for these two stack objects.
    534   int a;
    535   int b;
    536 
    537   int *A = Ident(&a);
    538   int *B = Ident(&b);
    539   *A = *B;
    540   longjmp(buf, 1);
    541 }
    542 
    543 NOINLINE void BuiltinLongJmpFunc1(jmp_buf buf) {
    544   // create three red zones for these two stack objects.
    545   int a;
    546   int b;
    547 
    548   int *A = Ident(&a);
    549   int *B = Ident(&b);
    550   *A = *B;
    551   __builtin_longjmp((void**)buf, 1);
    552 }
    553 
    554 NOINLINE void UnderscopeLongJmpFunc1(jmp_buf buf) {
    555   // create three red zones for these two stack objects.
    556   int a;
    557   int b;
    558 
    559   int *A = Ident(&a);
    560   int *B = Ident(&b);
    561   *A = *B;
    562   _longjmp(buf, 1);
    563 }
    564 
    565 NOINLINE void SigLongJmpFunc1(sigjmp_buf buf) {
    566   // create three red zones for these two stack objects.
    567   int a;
    568   int b;
    569 
    570   int *A = Ident(&a);
    571   int *B = Ident(&b);
    572   *A = *B;
    573   siglongjmp(buf, 1);
    574 }
    575 
    576 
    577 NOINLINE void TouchStackFunc() {
    578   int a[100];  // long array will intersect with redzones from LongJmpFunc1.
    579   int *A = Ident(a);
    580   for (int i = 0; i < 100; i++)
    581     A[i] = i*i;
    582 }
    583 
    584 // Test that we handle longjmp and do not report fals positives on stack.
    585 TEST(AddressSanitizer, LongJmpTest) {
    586   static jmp_buf buf;
    587   if (!setjmp(buf)) {
    588     LongJmpFunc1(buf);
    589   } else {
    590     TouchStackFunc();
    591   }
    592 }
    593 
    594 #if !defined(__ANDROID__) && \
    595     !defined(__powerpc64__) && !defined(__powerpc__)
    596 // Does not work on Power:
    597 // https://code.google.com/p/address-sanitizer/issues/detail?id=185
    598 TEST(AddressSanitizer, BuiltinLongJmpTest) {
    599   static jmp_buf buf;
    600   if (!__builtin_setjmp((void**)buf)) {
    601     BuiltinLongJmpFunc1(buf);
    602   } else {
    603     TouchStackFunc();
    604   }
    605 }
    606 #endif  // not defined(__ANDROID__)
    607 
    608 TEST(AddressSanitizer, UnderscopeLongJmpTest) {
    609   static jmp_buf buf;
    610   if (!_setjmp(buf)) {
    611     UnderscopeLongJmpFunc1(buf);
    612   } else {
    613     TouchStackFunc();
    614   }
    615 }
    616 
    617 TEST(AddressSanitizer, SigLongJmpTest) {
    618   static sigjmp_buf buf;
    619   if (!sigsetjmp(buf, 1)) {
    620     SigLongJmpFunc1(buf);
    621   } else {
    622     TouchStackFunc();
    623   }
    624 }
    625 
    626 #ifdef __EXCEPTIONS
    627 NOINLINE void ThrowFunc() {
    628   // create three red zones for these two stack objects.
    629   int a;
    630   int b;
    631 
    632   int *A = Ident(&a);
    633   int *B = Ident(&b);
    634   *A = *B;
    635   ASAN_THROW(1);
    636 }
    637 
    638 TEST(AddressSanitizer, CxxExceptionTest) {
    639   if (ASAN_UAR) return;
    640   // TODO(kcc): this test crashes on 32-bit for some reason...
    641   if (SANITIZER_WORDSIZE == 32) return;
    642   try {
    643     ThrowFunc();
    644   } catch(...) {}
    645   TouchStackFunc();
    646 }
    647 #endif
    648 
    649 void *ThreadStackReuseFunc1(void *unused) {
    650   // create three red zones for these two stack objects.
    651   int a;
    652   int b;
    653 
    654   int *A = Ident(&a);
    655   int *B = Ident(&b);
    656   *A = *B;
    657   pthread_exit(0);
    658   return 0;
    659 }
    660 
    661 void *ThreadStackReuseFunc2(void *unused) {
    662   TouchStackFunc();
    663   return 0;
    664 }
    665 
    666 TEST(AddressSanitizer, ThreadStackReuseTest) {
    667   pthread_t t;
    668   PTHREAD_CREATE(&t, 0, ThreadStackReuseFunc1, 0);
    669   PTHREAD_JOIN(t, 0);
    670   PTHREAD_CREATE(&t, 0, ThreadStackReuseFunc2, 0);
    671   PTHREAD_JOIN(t, 0);
    672 }
    673 
    674 #if defined(__i386__) || defined(__x86_64__)
    675 TEST(AddressSanitizer, Store128Test) {
    676   char *a = Ident((char*)malloc(Ident(12)));
    677   char *p = a;
    678   if (((uintptr_t)a % 16) != 0)
    679     p = a + 8;
    680   assert(((uintptr_t)p % 16) == 0);
    681   __m128i value_wide = _mm_set1_epi16(0x1234);
    682   EXPECT_DEATH(_mm_store_si128((__m128i*)p, value_wide),
    683                "AddressSanitizer: heap-buffer-overflow");
    684   EXPECT_DEATH(_mm_store_si128((__m128i*)p, value_wide),
    685                "WRITE of size 16");
    686   EXPECT_DEATH(_mm_store_si128((__m128i*)p, value_wide),
    687                "located 0 bytes to the right of 12-byte");
    688   free(a);
    689 }
    690 #endif
    691 
    692 string RightOOBErrorMessage(int oob_distance, bool is_write) {
    693   assert(oob_distance >= 0);
    694   char expected_str[100];
    695   sprintf(expected_str, ASAN_PCRE_DOTALL
    696           "buffer-overflow.*%s.*located %d bytes to the right",
    697           is_write ? "WRITE" : "READ", oob_distance);
    698   return string(expected_str);
    699 }
    700 
    701 string RightOOBWriteMessage(int oob_distance) {
    702   return RightOOBErrorMessage(oob_distance, /*is_write*/true);
    703 }
    704 
    705 string RightOOBReadMessage(int oob_distance) {
    706   return RightOOBErrorMessage(oob_distance, /*is_write*/false);
    707 }
    708 
    709 string LeftOOBErrorMessage(int oob_distance, bool is_write) {
    710   assert(oob_distance > 0);
    711   char expected_str[100];
    712   sprintf(expected_str, ASAN_PCRE_DOTALL "%s.*located %d bytes to the left",
    713           is_write ? "WRITE" : "READ", oob_distance);
    714   return string(expected_str);
    715 }
    716 
    717 string LeftOOBWriteMessage(int oob_distance) {
    718   return LeftOOBErrorMessage(oob_distance, /*is_write*/true);
    719 }
    720 
    721 string LeftOOBReadMessage(int oob_distance) {
    722   return LeftOOBErrorMessage(oob_distance, /*is_write*/false);
    723 }
    724 
    725 string LeftOOBAccessMessage(int oob_distance) {
    726   assert(oob_distance > 0);
    727   char expected_str[100];
    728   sprintf(expected_str, "located %d bytes to the left", oob_distance);
    729   return string(expected_str);
    730 }
    731 
    732 char* MallocAndMemsetString(size_t size, char ch) {
    733   char *s = Ident((char*)malloc(size));
    734   memset(s, ch, size);
    735   return s;
    736 }
    737 
    738 char* MallocAndMemsetString(size_t size) {
    739   return MallocAndMemsetString(size, 'z');
    740 }
    741 
    742 #if defined(__linux__) && !defined(ANDROID) && !defined(__ANDROID__)
    743 #define READ_TEST(READ_N_BYTES)                                          \
    744   char *x = new char[10];                                                \
    745   int fd = open("/proc/self/stat", O_RDONLY);                            \
    746   ASSERT_GT(fd, 0);                                                      \
    747   EXPECT_DEATH(READ_N_BYTES,                                             \
    748                ASAN_PCRE_DOTALL                                          \
    749                "AddressSanitizer: heap-buffer-overflow"                  \
    750                ".* is located 0 bytes to the right of 10-byte region");  \
    751   close(fd);                                                             \
    752   delete [] x;                                                           \
    753 
    754 TEST(AddressSanitizer, pread) {
    755   READ_TEST(pread(fd, x, 15, 0));
    756 }
    757 
    758 TEST(AddressSanitizer, pread64) {
    759   READ_TEST(pread64(fd, x, 15, 0));
    760 }
    761 
    762 TEST(AddressSanitizer, read) {
    763   READ_TEST(read(fd, x, 15));
    764 }
    765 #endif  // defined(__linux__) && !defined(ANDROID) && !defined(__ANDROID__)
    766 
    767 // This test case fails
    768 // Clang optimizes memcpy/memset calls which lead to unaligned access
    769 TEST(AddressSanitizer, DISABLED_MemIntrinsicUnalignedAccessTest) {
    770   int size = Ident(4096);
    771   char *s = Ident((char*)malloc(size));
    772   EXPECT_DEATH(memset(s + size - 1, 0, 2), RightOOBWriteMessage(0));
    773   free(s);
    774 }
    775 
    776 // TODO(samsonov): Add a test with malloc(0)
    777 // TODO(samsonov): Add tests for str* and mem* functions.
    778 
    779 NOINLINE static int LargeFunction(bool do_bad_access) {
    780   int *x = new int[100];
    781   x[0]++;
    782   x[1]++;
    783   x[2]++;
    784   x[3]++;
    785   x[4]++;
    786   x[5]++;
    787   x[6]++;
    788   x[7]++;
    789   x[8]++;
    790   x[9]++;
    791 
    792   x[do_bad_access ? 100 : 0]++; int res = __LINE__;
    793 
    794   x[10]++;
    795   x[11]++;
    796   x[12]++;
    797   x[13]++;
    798   x[14]++;
    799   x[15]++;
    800   x[16]++;
    801   x[17]++;
    802   x[18]++;
    803   x[19]++;
    804 
    805   delete x;
    806   return res;
    807 }
    808 
    809 // Test the we have correct debug info for the failing instruction.
    810 // This test requires the in-process symbolizer to be enabled by default.
    811 TEST(AddressSanitizer, DISABLED_LargeFunctionSymbolizeTest) {
    812   int failing_line = LargeFunction(false);
    813   char expected_warning[128];
    814   sprintf(expected_warning, "LargeFunction.*asan_test.*:%d", failing_line);
    815   EXPECT_DEATH(LargeFunction(true), expected_warning);
    816 }
    817 
    818 // Check that we unwind and symbolize correctly.
    819 TEST(AddressSanitizer, DISABLED_MallocFreeUnwindAndSymbolizeTest) {
    820   int *a = (int*)malloc_aaa(sizeof(int));
    821   *a = 1;
    822   free_aaa(a);
    823   EXPECT_DEATH(*a = 1, "free_ccc.*free_bbb.*free_aaa.*"
    824                "malloc_fff.*malloc_eee.*malloc_ddd");
    825 }
    826 
    827 static bool TryToSetThreadName(const char *name) {
    828 #if defined(__linux__) && defined(PR_SET_NAME)
    829   return 0 == prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0);
    830 #else
    831   return false;
    832 #endif
    833 }
    834 
    835 void *ThreadedTestAlloc(void *a) {
    836   EXPECT_EQ(true, TryToSetThreadName("AllocThr"));
    837   int **p = (int**)a;
    838   *p = new int;
    839   return 0;
    840 }
    841 
    842 void *ThreadedTestFree(void *a) {
    843   EXPECT_EQ(true, TryToSetThreadName("FreeThr"));
    844   int **p = (int**)a;
    845   delete *p;
    846   return 0;
    847 }
    848 
    849 void *ThreadedTestUse(void *a) {
    850   EXPECT_EQ(true, TryToSetThreadName("UseThr"));
    851   int **p = (int**)a;
    852   **p = 1;
    853   return 0;
    854 }
    855 
    856 void ThreadedTestSpawn() {
    857   pthread_t t;
    858   int *x;
    859   PTHREAD_CREATE(&t, 0, ThreadedTestAlloc, &x);
    860   PTHREAD_JOIN(t, 0);
    861   PTHREAD_CREATE(&t, 0, ThreadedTestFree, &x);
    862   PTHREAD_JOIN(t, 0);
    863   PTHREAD_CREATE(&t, 0, ThreadedTestUse, &x);
    864   PTHREAD_JOIN(t, 0);
    865 }
    866 
    867 TEST(AddressSanitizer, ThreadedTest) {
    868   EXPECT_DEATH(ThreadedTestSpawn(),
    869                ASAN_PCRE_DOTALL
    870                "Thread T.*created"
    871                ".*Thread T.*created"
    872                ".*Thread T.*created");
    873 }
    874 
    875 void *ThreadedTestFunc(void *unused) {
    876   // Check if prctl(PR_SET_NAME) is supported. Return if not.
    877   if (!TryToSetThreadName("TestFunc"))
    878     return 0;
    879   EXPECT_DEATH(ThreadedTestSpawn(),
    880                ASAN_PCRE_DOTALL
    881                "WRITE .*thread T. .UseThr."
    882                ".*freed by thread T. .FreeThr. here:"
    883                ".*previously allocated by thread T. .AllocThr. here:"
    884                ".*Thread T. .UseThr. created by T.*TestFunc"
    885                ".*Thread T. .FreeThr. created by T"
    886                ".*Thread T. .AllocThr. created by T"
    887                "");
    888   return 0;
    889 }
    890 
    891 TEST(AddressSanitizer, ThreadNamesTest) {
    892   // Run ThreadedTestFunc in a separate thread because it tries to set a
    893   // thread name and we don't want to change the main thread's name.
    894   pthread_t t;
    895   PTHREAD_CREATE(&t, 0, ThreadedTestFunc, 0);
    896   PTHREAD_JOIN(t, 0);
    897 }
    898 
    899 #if ASAN_NEEDS_SEGV
    900 TEST(AddressSanitizer, ShadowGapTest) {
    901 #if SANITIZER_WORDSIZE == 32
    902   char *addr = (char*)0x22000000;
    903 #else
    904 # if defined(__powerpc64__)
    905   char *addr = (char*)0x024000800000;
    906 # else
    907   char *addr = (char*)0x0000100000080000;
    908 # endif
    909 #endif
    910   EXPECT_DEATH(*addr = 1, "AddressSanitizer: SEGV on unknown");
    911 }
    912 #endif  // ASAN_NEEDS_SEGV
    913 
    914 extern "C" {
    915 NOINLINE static void UseThenFreeThenUse() {
    916   char *x = Ident((char*)malloc(8));
    917   *x = 1;
    918   free_aaa(x);
    919   *x = 2;
    920 }
    921 }
    922 
    923 TEST(AddressSanitizer, UseThenFreeThenUseTest) {
    924   EXPECT_DEATH(UseThenFreeThenUse(), "freed by thread");
    925 }
    926 
    927 TEST(AddressSanitizer, StrDupTest) {
    928   free(strdup(Ident("123")));
    929 }
    930 
    931 // Currently we create and poison redzone at right of global variables.
    932 static char static110[110];
    933 const char ConstGlob[7] = {1, 2, 3, 4, 5, 6, 7};
    934 static const char StaticConstGlob[3] = {9, 8, 7};
    935 
    936 TEST(AddressSanitizer, GlobalTest) {
    937   static char func_static15[15];
    938 
    939   static char fs1[10];
    940   static char fs2[10];
    941   static char fs3[10];
    942 
    943   glob5[Ident(0)] = 0;
    944   glob5[Ident(1)] = 0;
    945   glob5[Ident(2)] = 0;
    946   glob5[Ident(3)] = 0;
    947   glob5[Ident(4)] = 0;
    948 
    949   EXPECT_DEATH(glob5[Ident(5)] = 0,
    950                "0 bytes to the right of global variable.*glob5.* size 5");
    951   EXPECT_DEATH(glob5[Ident(5+6)] = 0,
    952                "6 bytes to the right of global variable.*glob5.* size 5");
    953   Ident(static110);  // avoid optimizations
    954   static110[Ident(0)] = 0;
    955   static110[Ident(109)] = 0;
    956   EXPECT_DEATH(static110[Ident(110)] = 0,
    957                "0 bytes to the right of global variable");
    958   EXPECT_DEATH(static110[Ident(110+7)] = 0,
    959                "7 bytes to the right of global variable");
    960 
    961   Ident(func_static15);  // avoid optimizations
    962   func_static15[Ident(0)] = 0;
    963   EXPECT_DEATH(func_static15[Ident(15)] = 0,
    964                "0 bytes to the right of global variable");
    965   EXPECT_DEATH(func_static15[Ident(15 + 9)] = 0,
    966                "9 bytes to the right of global variable");
    967 
    968   Ident(fs1);
    969   Ident(fs2);
    970   Ident(fs3);
    971 
    972   // We don't create left redzones, so this is not 100% guaranteed to fail.
    973   // But most likely will.
    974   EXPECT_DEATH(fs2[Ident(-1)] = 0, "is located.*of global variable");
    975 
    976   EXPECT_DEATH(Ident(Ident(ConstGlob)[8]),
    977                "is located 1 bytes to the right of .*ConstGlob");
    978   EXPECT_DEATH(Ident(Ident(StaticConstGlob)[5]),
    979                "is located 2 bytes to the right of .*StaticConstGlob");
    980 
    981   // call stuff from another file.
    982   GlobalsTest(0);
    983 }
    984 
    985 TEST(AddressSanitizer, GlobalStringConstTest) {
    986   static const char *zoo = "FOOBAR123";
    987   const char *p = Ident(zoo);
    988   EXPECT_DEATH(Ident(p[15]), "is ascii string 'FOOBAR123'");
    989 }
    990 
    991 TEST(AddressSanitizer, FileNameInGlobalReportTest) {
    992   static char zoo[10];
    993   const char *p = Ident(zoo);
    994   // The file name should be present in the report.
    995   EXPECT_DEATH(Ident(p[15]), "zoo.*asan_test.");
    996 }
    997 
    998 int *ReturnsPointerToALocalObject() {
    999   int a = 0;
   1000   return Ident(&a);
   1001 }
   1002 
   1003 #if ASAN_UAR == 1
   1004 TEST(AddressSanitizer, LocalReferenceReturnTest) {
   1005   int *(*f)() = Ident(ReturnsPointerToALocalObject);
   1006   int *p = f();
   1007   // Call 'f' a few more times, 'p' should still be poisoned.
   1008   for (int i = 0; i < 32; i++)
   1009     f();
   1010   EXPECT_DEATH(*p = 1, "AddressSanitizer: stack-use-after-return");
   1011   EXPECT_DEATH(*p = 1, "is located.*in frame .*ReturnsPointerToALocal");
   1012 }
   1013 #endif
   1014 
   1015 template <int kSize>
   1016 NOINLINE static void FuncWithStack() {
   1017   char x[kSize];
   1018   Ident(x)[0] = 0;
   1019   Ident(x)[kSize-1] = 0;
   1020 }
   1021 
   1022 static void LotsOfStackReuse() {
   1023   int LargeStack[10000];
   1024   Ident(LargeStack)[0] = 0;
   1025   for (int i = 0; i < 10000; i++) {
   1026     FuncWithStack<128 * 1>();
   1027     FuncWithStack<128 * 2>();
   1028     FuncWithStack<128 * 4>();
   1029     FuncWithStack<128 * 8>();
   1030     FuncWithStack<128 * 16>();
   1031     FuncWithStack<128 * 32>();
   1032     FuncWithStack<128 * 64>();
   1033     FuncWithStack<128 * 128>();
   1034     FuncWithStack<128 * 256>();
   1035     FuncWithStack<128 * 512>();
   1036     Ident(LargeStack)[0] = 0;
   1037   }
   1038 }
   1039 
   1040 TEST(AddressSanitizer, StressStackReuseTest) {
   1041   LotsOfStackReuse();
   1042 }
   1043 
   1044 TEST(AddressSanitizer, ThreadedStressStackReuseTest) {
   1045   const int kNumThreads = 20;
   1046   pthread_t t[kNumThreads];
   1047   for (int i = 0; i < kNumThreads; i++) {
   1048     PTHREAD_CREATE(&t[i], 0, (void* (*)(void *x))LotsOfStackReuse, 0);
   1049   }
   1050   for (int i = 0; i < kNumThreads; i++) {
   1051     PTHREAD_JOIN(t[i], 0);
   1052   }
   1053 }
   1054 
   1055 static void *PthreadExit(void *a) {
   1056   pthread_exit(0);
   1057   return 0;
   1058 }
   1059 
   1060 TEST(AddressSanitizer, PthreadExitTest) {
   1061   pthread_t t;
   1062   for (int i = 0; i < 1000; i++) {
   1063     PTHREAD_CREATE(&t, 0, PthreadExit, 0);
   1064     PTHREAD_JOIN(t, 0);
   1065   }
   1066 }
   1067 
   1068 #ifdef __EXCEPTIONS
   1069 NOINLINE static void StackReuseAndException() {
   1070   int large_stack[1000];
   1071   Ident(large_stack);
   1072   ASAN_THROW(1);
   1073 }
   1074 
   1075 // TODO(kcc): support exceptions with use-after-return.
   1076 TEST(AddressSanitizer, DISABLED_StressStackReuseAndExceptionsTest) {
   1077   for (int i = 0; i < 10000; i++) {
   1078     try {
   1079     StackReuseAndException();
   1080     } catch(...) {
   1081     }
   1082   }
   1083 }
   1084 #endif
   1085 
   1086 TEST(AddressSanitizer, MlockTest) {
   1087   EXPECT_EQ(0, mlockall(MCL_CURRENT));
   1088   EXPECT_EQ(0, mlock((void*)0x12345, 0x5678));
   1089   EXPECT_EQ(0, munlockall());
   1090   EXPECT_EQ(0, munlock((void*)0x987, 0x654));
   1091 }
   1092 
   1093 struct LargeStruct {
   1094   int foo[100];
   1095 };
   1096 
   1097 // Test for bug http://llvm.org/bugs/show_bug.cgi?id=11763.
   1098 // Struct copy should not cause asan warning even if lhs == rhs.
   1099 TEST(AddressSanitizer, LargeStructCopyTest) {
   1100   LargeStruct a;
   1101   *Ident(&a) = *Ident(&a);
   1102 }
   1103 
   1104 ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS
   1105 static void NoAddressSafety() {
   1106   char *foo = new char[10];
   1107   Ident(foo)[10] = 0;
   1108   delete [] foo;
   1109 }
   1110 
   1111 TEST(AddressSanitizer, AttributeNoAddressSafetyTest) {
   1112   Ident(NoAddressSafety)();
   1113 }
   1114 
   1115 // It doesn't work on Android, as calls to new/delete go through malloc/free.
   1116 // Neither it does on OS X, see
   1117 // https://code.google.com/p/address-sanitizer/issues/detail?id=131.
   1118 #if !defined(ANDROID) && !defined(__ANDROID__) && !defined(__APPLE__)
   1119 static string MismatchStr(const string &str) {
   1120   return string("AddressSanitizer: alloc-dealloc-mismatch \\(") + str;
   1121 }
   1122 
   1123 TEST(AddressSanitizer, AllocDeallocMismatch) {
   1124   EXPECT_DEATH(free(Ident(new int)),
   1125                MismatchStr("operator new vs free"));
   1126   EXPECT_DEATH(free(Ident(new int[2])),
   1127                MismatchStr("operator new \\[\\] vs free"));
   1128   EXPECT_DEATH(delete (Ident(new int[2])),
   1129                MismatchStr("operator new \\[\\] vs operator delete"));
   1130   EXPECT_DEATH(delete (Ident((int*)malloc(2 * sizeof(int)))),
   1131                MismatchStr("malloc vs operator delete"));
   1132   EXPECT_DEATH(delete [] (Ident(new int)),
   1133                MismatchStr("operator new vs operator delete \\[\\]"));
   1134   EXPECT_DEATH(delete [] (Ident((int*)malloc(2 * sizeof(int)))),
   1135                MismatchStr("malloc vs operator delete \\[\\]"));
   1136 }
   1137 #endif
   1138 
   1139 // ------------------ demo tests; run each one-by-one -------------
   1140 // e.g. --gtest_filter=*DemoOOBLeftHigh --gtest_also_run_disabled_tests
   1141 TEST(AddressSanitizer, DISABLED_DemoThreadedTest) {
   1142   ThreadedTestSpawn();
   1143 }
   1144 
   1145 void *SimpleBugOnSTack(void *x = 0) {
   1146   char a[20];
   1147   Ident(a)[20] = 0;
   1148   return 0;
   1149 }
   1150 
   1151 TEST(AddressSanitizer, DISABLED_DemoStackTest) {
   1152   SimpleBugOnSTack();
   1153 }
   1154 
   1155 TEST(AddressSanitizer, DISABLED_DemoThreadStackTest) {
   1156   pthread_t t;
   1157   PTHREAD_CREATE(&t, 0, SimpleBugOnSTack, 0);
   1158   PTHREAD_JOIN(t, 0);
   1159 }
   1160 
   1161 TEST(AddressSanitizer, DISABLED_DemoUAFLowIn) {
   1162   uaf_test<U1>(10, 0);
   1163 }
   1164 TEST(AddressSanitizer, DISABLED_DemoUAFLowLeft) {
   1165   uaf_test<U1>(10, -2);
   1166 }
   1167 TEST(AddressSanitizer, DISABLED_DemoUAFLowRight) {
   1168   uaf_test<U1>(10, 10);
   1169 }
   1170 
   1171 TEST(AddressSanitizer, DISABLED_DemoUAFHigh) {
   1172   uaf_test<U1>(kLargeMalloc, 0);
   1173 }
   1174 
   1175 TEST(AddressSanitizer, DISABLED_DemoOOM) {
   1176   size_t size = SANITIZER_WORDSIZE == 64 ? (size_t)(1ULL << 40) : (0xf0000000);
   1177   printf("%p\n", malloc(size));
   1178 }
   1179 
   1180 TEST(AddressSanitizer, DISABLED_DemoDoubleFreeTest) {
   1181   DoubleFree();
   1182 }
   1183 
   1184 TEST(AddressSanitizer, DISABLED_DemoNullDerefTest) {
   1185   int *a = 0;
   1186   Ident(a)[10] = 0;
   1187 }
   1188 
   1189 TEST(AddressSanitizer, DISABLED_DemoFunctionStaticTest) {
   1190   static char a[100];
   1191   static char b[100];
   1192   static char c[100];
   1193   Ident(a);
   1194   Ident(b);
   1195   Ident(c);
   1196   Ident(a)[5] = 0;
   1197   Ident(b)[105] = 0;
   1198   Ident(a)[5] = 0;
   1199 }
   1200 
   1201 TEST(AddressSanitizer, DISABLED_DemoTooMuchMemoryTest) {
   1202   const size_t kAllocSize = (1 << 28) - 1024;
   1203   size_t total_size = 0;
   1204   while (true) {
   1205     char *x = (char*)malloc(kAllocSize);
   1206     memset(x, 0, kAllocSize);
   1207     total_size += kAllocSize;
   1208     fprintf(stderr, "total: %ldM %p\n", (long)total_size >> 20, x);
   1209   }
   1210 }
   1211 
   1212 // http://code.google.com/p/address-sanitizer/issues/detail?id=66
   1213 TEST(AddressSanitizer, BufferOverflowAfterManyFrees) {
   1214   for (int i = 0; i < 1000000; i++) {
   1215     delete [] (Ident(new char [8644]));
   1216   }
   1217   char *x = new char[8192];
   1218   EXPECT_DEATH(x[Ident(8192)] = 0, "AddressSanitizer: heap-buffer-overflow");
   1219   delete [] Ident(x);
   1220 }
   1221 
   1222 
   1223 // Test that instrumentation of stack allocations takes into account
   1224 // AllocSize of a type, and not its StoreSize (16 vs 10 bytes for long double).
   1225 // See http://llvm.org/bugs/show_bug.cgi?id=12047 for more details.
   1226 TEST(AddressSanitizer, LongDoubleNegativeTest) {
   1227   long double a, b;
   1228   static long double c;
   1229   memcpy(Ident(&a), Ident(&b), sizeof(long double));
   1230   memcpy(Ident(&c), Ident(&b), sizeof(long double));
   1231 }
   1232 
   1233 TEST(AddressSanitizer, pthread_getschedparam) {
   1234   int policy;
   1235   struct sched_param param;
   1236   EXPECT_DEATH(
   1237       pthread_getschedparam(pthread_self(), &policy, Ident(&param) + 2),
   1238       "AddressSanitizer: stack-buffer-.*flow");
   1239   EXPECT_DEATH(
   1240       pthread_getschedparam(pthread_self(), Ident(&policy) - 1, &param),
   1241       "AddressSanitizer: stack-buffer-.*flow");
   1242   int res = pthread_getschedparam(pthread_self(), &policy, &param);
   1243   ASSERT_EQ(0, res);
   1244 }
   1245