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