blob: 07d59e09a72f2dbb02b53ab9549a9b4ffce1e68d [file] [log] [blame]
Alexey Samsonovb0ddf222012-09-19 07:07:46 +00001//===-- asan_test.cc ------------------------------------------------------===//
Kostya Serebryany1e172b42011-11-30 01:07:02 +00002//
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//===----------------------------------------------------------------------===//
Kostya Serebryany1e172b42011-11-30 01:07:02 +000013#include "asan_test_utils.h"
14
Timur Iskhodzhanov93810672012-03-23 13:10:59 +000015NOINLINE void *malloc_fff(size_t size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000016 void *res = malloc/**/(size); break_optimization(0); return res;}
Timur Iskhodzhanov93810672012-03-23 13:10:59 +000017NOINLINE void *malloc_eee(size_t size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000018 void *res = malloc_fff(size); break_optimization(0); return res;}
Timur Iskhodzhanov93810672012-03-23 13:10:59 +000019NOINLINE void *malloc_ddd(size_t size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000020 void *res = malloc_eee(size); break_optimization(0); return res;}
Timur Iskhodzhanov93810672012-03-23 13:10:59 +000021NOINLINE void *malloc_ccc(size_t size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000022 void *res = malloc_ddd(size); break_optimization(0); return res;}
Timur Iskhodzhanov93810672012-03-23 13:10:59 +000023NOINLINE void *malloc_bbb(size_t size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000024 void *res = malloc_ccc(size); break_optimization(0); return res;}
Timur Iskhodzhanov93810672012-03-23 13:10:59 +000025NOINLINE void *malloc_aaa(size_t size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000026 void *res = malloc_bbb(size); break_optimization(0); return res;}
27
Timur Iskhodzhanov93810672012-03-23 13:10:59 +000028NOINLINE void free_ccc(void *p) { free(p); break_optimization(0);}
29NOINLINE void free_bbb(void *p) { free_ccc(p); break_optimization(0);}
30NOINLINE void free_aaa(void *p) { free_bbb(p); break_optimization(0);}
Kostya Serebryany1e172b42011-11-30 01:07:02 +000031
Timur Iskhodzhanov2dcf4492012-04-09 11:50:27 +000032template<typename T>
Timur Iskhodzhanov93810672012-03-23 13:10:59 +000033NOINLINE void uaf_test(int size, int off) {
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -070034 void *p = malloc_aaa(size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000035 free_aaa(p);
36 for (int i = 1; i < 100; i++)
37 free_aaa(malloc_aaa(i));
38 fprintf(stderr, "writing %ld byte(s) at %p with offset %d\n",
39 (long)sizeof(T), p, off);
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -070040 asan_write((T *)((char *)p + off));
Kostya Serebryany1e172b42011-11-30 01:07:02 +000041}
42
Kostya Serebryany13ebae62011-12-27 21:57:12 +000043TEST(AddressSanitizer, HasFeatureAddressSanitizerTest) {
44#if defined(__has_feature) && __has_feature(address_sanitizer)
45 bool asan = 1;
Kostya Serebryanybadab162012-11-30 10:41:42 +000046#elif defined(__SANITIZE_ADDRESS__)
47 bool asan = 1;
Kostya Serebryany13ebae62011-12-27 21:57:12 +000048#else
49 bool asan = 0;
50#endif
51 EXPECT_EQ(true, asan);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000052}
53
54TEST(AddressSanitizer, SimpleDeathTest) {
55 EXPECT_DEATH(exit(1), "");
56}
57
58TEST(AddressSanitizer, VariousMallocsTest) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000059 int *a = (int*)malloc(100 * sizeof(int));
60 a[50] = 0;
61 free(a);
62
Kostya Serebryany1e172b42011-11-30 01:07:02 +000063 int *r = (int*)malloc(10);
64 r = (int*)realloc(r, 2000 * sizeof(int));
65 r[1000] = 0;
66 free(r);
67
Kostya Serebryany1e172b42011-11-30 01:07:02 +000068 int *b = new int[100];
69 b[50] = 0;
70 delete [] b;
71
Kostya Serebryany1e172b42011-11-30 01:07:02 +000072 int *c = new int;
73 *c = 0;
74 delete c;
75
Stephen Hines2d1fdb22014-05-28 23:58:16 -070076#if SANITIZER_TEST_HAS_POSIX_MEMALIGN
Kostya Serebryany1e172b42011-11-30 01:07:02 +000077 int *pm;
78 int pm_res = posix_memalign((void**)&pm, kPageSize, kPageSize);
79 EXPECT_EQ(0, pm_res);
80 free(pm);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070081#endif // SANITIZER_TEST_HAS_POSIX_MEMALIGN
Kostya Serebryany1e172b42011-11-30 01:07:02 +000082
Stephen Hines2d1fdb22014-05-28 23:58:16 -070083#if SANITIZER_TEST_HAS_MEMALIGN
Kostya Serebryany1e172b42011-11-30 01:07:02 +000084 int *ma = (int*)memalign(kPageSize, kPageSize);
Alexey Samsonov76e84282012-09-12 12:07:36 +000085 EXPECT_EQ(0U, (uintptr_t)ma % kPageSize);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000086 ma[123] = 0;
87 free(ma);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070088#endif // SANITIZER_TEST_HAS_MEMALIGN
Kostya Serebryany1e172b42011-11-30 01:07:02 +000089}
90
91TEST(AddressSanitizer, CallocTest) {
92 int *a = (int*)calloc(100, sizeof(int));
93 EXPECT_EQ(0, a[10]);
94 free(a);
95}
96
Alexey Samsonov05fa3802013-09-16 15:50:53 +000097TEST(AddressSanitizer, CallocReturnsZeroMem) {
98 size_t sizes[] = {16, 1000, 10000, 100000, 2100000};
99 for (size_t s = 0; s < sizeof(sizes)/sizeof(sizes[0]); s++) {
100 size_t size = sizes[s];
101 for (size_t iter = 0; iter < 5; iter++) {
102 char *x = Ident((char*)calloc(1, size));
103 EXPECT_EQ(x[0], 0);
104 EXPECT_EQ(x[size - 1], 0);
105 EXPECT_EQ(x[size / 2], 0);
106 EXPECT_EQ(x[size / 3], 0);
107 EXPECT_EQ(x[size / 4], 0);
108 memset(x, 0x42, size);
109 free(Ident(x));
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700110#if !defined(_WIN32)
111 // FIXME: OOM on Windows. We should just make this a lit test
112 // with quarantine size set to 1.
Alexey Samsonov05fa3802013-09-16 15:50:53 +0000113 free(Ident(malloc(Ident(1 << 27)))); // Try to drain the quarantine.
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700114#endif
Alexey Samsonov05fa3802013-09-16 15:50:53 +0000115 }
116 }
117}
118
Dan Albert31a5cd82014-06-14 00:50:03 +0000119// No valloc on Windows or Android.
Dan Albertb625a872014-10-20 15:35:01 +0000120#if !defined(_WIN32) && !defined(__ANDROID__)
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000121TEST(AddressSanitizer, VallocTest) {
122 void *a = valloc(100);
Alexey Samsonov76e84282012-09-12 12:07:36 +0000123 EXPECT_EQ(0U, (uintptr_t)a % kPageSize);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000124 free(a);
125}
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700126#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000127
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700128#if SANITIZER_TEST_HAS_PVALLOC
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000129TEST(AddressSanitizer, PvallocTest) {
130 char *a = (char*)pvalloc(kPageSize + 100);
Alexey Samsonov76e84282012-09-12 12:07:36 +0000131 EXPECT_EQ(0U, (uintptr_t)a % kPageSize);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000132 a[kPageSize + 101] = 1; // we should not report an error here.
133 free(a);
134
135 a = (char*)pvalloc(0); // pvalloc(0) should allocate at least one page.
Alexey Samsonov76e84282012-09-12 12:07:36 +0000136 EXPECT_EQ(0U, (uintptr_t)a % kPageSize);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000137 a[101] = 1; // we should not report an error here.
138 free(a);
139}
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700140#endif // SANITIZER_TEST_HAS_PVALLOC
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000141
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700142#if !defined(_WIN32)
143// FIXME: Use an equivalent of pthread_setspecific on Windows.
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000144void *TSDWorker(void *test_key) {
145 if (test_key) {
146 pthread_setspecific(*(pthread_key_t*)test_key, (void*)0xfeedface);
147 }
148 return NULL;
149}
150
151void TSDDestructor(void *tsd) {
152 // Spawning a thread will check that the current thread id is not -1.
153 pthread_t th;
Kostya Serebryany26976872012-12-03 09:43:56 +0000154 PTHREAD_CREATE(&th, NULL, TSDWorker, NULL);
155 PTHREAD_JOIN(th, NULL);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000156}
157
158// This tests triggers the thread-specific data destruction fiasco which occurs
159// if we don't manage the TSD destructors ourselves. We create a new pthread
160// key with a non-NULL destructor which is likely to be put after the destructor
161// of AsanThread in the list of destructors.
162// In this case the TSD for AsanThread will be destroyed before TSDDestructor
163// is called for the child thread, and a CHECK will fail when we call
164// pthread_create() to spawn the grandchild.
165TEST(AddressSanitizer, DISABLED_TSDTest) {
166 pthread_t th;
167 pthread_key_t test_key;
168 pthread_key_create(&test_key, TSDDestructor);
Kostya Serebryany26976872012-12-03 09:43:56 +0000169 PTHREAD_CREATE(&th, NULL, TSDWorker, &test_key);
170 PTHREAD_JOIN(th, NULL);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000171 pthread_key_delete(test_key);
172}
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700173#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000174
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000175TEST(AddressSanitizer, UAF_char) {
Kostya Serebryanyca9b5dd2012-10-15 13:30:38 +0000176 const char *uaf_string = "AddressSanitizer:.*heap-use-after-free";
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000177 EXPECT_DEATH(uaf_test<U1>(1, 0), uaf_string);
178 EXPECT_DEATH(uaf_test<U1>(10, 0), uaf_string);
179 EXPECT_DEATH(uaf_test<U1>(10, 10), uaf_string);
180 EXPECT_DEATH(uaf_test<U1>(kLargeMalloc, 0), uaf_string);
181 EXPECT_DEATH(uaf_test<U1>(kLargeMalloc, kLargeMalloc / 2), uaf_string);
182}
183
Kostya Serebryany366984e2013-02-19 11:30:25 +0000184TEST(AddressSanitizer, UAF_long_double) {
Kostya Serebryany7f4df1a2013-02-19 13:43:44 +0000185 if (sizeof(long double) == sizeof(double)) return;
Kostya Serebryany366984e2013-02-19 11:30:25 +0000186 long double *p = Ident(new long double[10]);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700187 EXPECT_DEATH(Ident(p)[12] = 0, "WRITE of size 1[026]");
188 EXPECT_DEATH(Ident(p)[0] = Ident(p)[12], "READ of size 1[026]");
Kostya Serebryany366984e2013-02-19 11:30:25 +0000189 delete [] Ident(p);
190}
191
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700192#if !defined(_WIN32)
Kostya Serebryany366984e2013-02-19 11:30:25 +0000193struct Packed5 {
194 int x;
195 char c;
196} __attribute__((packed));
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700197#else
198# pragma pack(push, 1)
199struct Packed5 {
200 int x;
201 char c;
202};
203# pragma pack(pop)
204#endif
Kostya Serebryany366984e2013-02-19 11:30:25 +0000205
206TEST(AddressSanitizer, UAF_Packed5) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700207 static_assert(sizeof(Packed5) == 5, "Please check the keywords used");
Kostya Serebryany366984e2013-02-19 11:30:25 +0000208 Packed5 *p = Ident(new Packed5[2]);
209 EXPECT_DEATH(p[0] = p[3], "READ of size 5");
210 EXPECT_DEATH(p[3] = p[0], "WRITE of size 5");
211 delete [] Ident(p);
212}
213
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000214#if ASAN_HAS_BLACKLIST
215TEST(AddressSanitizer, IgnoreTest) {
216 int *x = Ident(new int);
217 delete Ident(x);
218 *x = 0;
219}
220#endif // ASAN_HAS_BLACKLIST
221
222struct StructWithBitField {
223 int bf1:1;
224 int bf2:1;
225 int bf3:1;
226 int bf4:29;
227};
228
229TEST(AddressSanitizer, BitFieldPositiveTest) {
230 StructWithBitField *x = new StructWithBitField;
231 delete Ident(x);
232 EXPECT_DEATH(x->bf1 = 0, "use-after-free");
233 EXPECT_DEATH(x->bf2 = 0, "use-after-free");
234 EXPECT_DEATH(x->bf3 = 0, "use-after-free");
235 EXPECT_DEATH(x->bf4 = 0, "use-after-free");
Alexey Samsonov76e84282012-09-12 12:07:36 +0000236}
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000237
238struct StructWithBitFields_8_24 {
239 int a:8;
240 int b:24;
241};
242
243TEST(AddressSanitizer, BitFieldNegativeTest) {
244 StructWithBitFields_8_24 *x = Ident(new StructWithBitFields_8_24);
245 x->a = 0;
246 x->b = 0;
247 delete Ident(x);
248}
249
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000250#if ASAN_NEEDS_SEGV
Alexander Potapenko58b017b2012-09-18 12:49:51 +0000251namespace {
252
Kostya Serebryanyca9b5dd2012-10-15 13:30:38 +0000253const char kUnknownCrash[] = "AddressSanitizer: SEGV on unknown address";
Alexander Potapenko58b017b2012-09-18 12:49:51 +0000254const char kOverriddenHandler[] = "ASan signal handler has been overridden\n";
255
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000256TEST(AddressSanitizer, WildAddressTest) {
257 char *c = (char*)0x123;
Alexander Potapenko58b017b2012-09-18 12:49:51 +0000258 EXPECT_DEATH(*c = 0, kUnknownCrash);
259}
260
261void my_sigaction_sighandler(int, siginfo_t*, void*) {
262 fprintf(stderr, kOverriddenHandler);
263 exit(1);
264}
265
Alexey Samsonovb0ddf222012-09-19 07:07:46 +0000266void my_signal_sighandler(int signum) {
Alexander Potapenko58b017b2012-09-18 12:49:51 +0000267 fprintf(stderr, kOverriddenHandler);
268 exit(1);
269}
270
271TEST(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}
Alexey Samsonovb0ddf222012-09-19 07:07:46 +0000287} // namespace
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000288#endif
289
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000290static 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
296TEST(AddressSanitizer, LargeMallocTest) {
Evgeniy Stepanov00545a32013-03-18 09:22:58 +0000297 const int max_size = (SANITIZER_WORDSIZE == 32) ? 1 << 26 : 1 << 28;
298 for (int i = 113; i < max_size; i = i * 2 + 13) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000299 TestLargeMalloc(i);
300 }
301}
302
303TEST(AddressSanitizer, HugeMallocTest) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700304 if (SANITIZER_WORDSIZE != 64 || ASAN_AVOID_EXPENSIVE_TESTS) return;
Kostya Serebryanyd39a34e2013-03-14 13:16:09 +0000305 size_t n_megs = 4100;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700306 EXPECT_DEATH(Ident((char*)malloc(n_megs << 20))[-1] = 0,
307 "is located 1 bytes to the left|"
308 "AddressSanitizer failed to allocate");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000309}
310
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700311#if SANITIZER_TEST_HAS_MEMALIGN
Kostya Serebryanyc3111052012-12-20 12:11:52 +0000312void MemalignRun(size_t align, size_t size, int idx) {
Kostya Serebryanyc3111052012-12-20 12:11:52 +0000313 char *p = (char *)memalign(align, size);
314 Ident(p)[idx] = 0;
315 free(p);
316}
317
318TEST(AddressSanitizer, memalign) {
319 for (int align = 16; align <= (1 << 23); align *= 2) {
320 size_t size = align * 5;
321 EXPECT_DEATH(MemalignRun(align, size, -1),
322 "is located 1 bytes to the left");
323 EXPECT_DEATH(MemalignRun(align, size, size + 1),
324 "is located 1 bytes to the right");
325 }
326}
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700327#endif // SANITIZER_TEST_HAS_MEMALIGN
Kostya Serebryanyc3111052012-12-20 12:11:52 +0000328
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000329void *ManyThreadsWorker(void *a) {
330 for (int iter = 0; iter < 100; iter++) {
331 for (size_t size = 100; size < 2000; size *= 2) {
332 free(Ident(malloc(size)));
333 }
334 }
335 return 0;
336}
337
338TEST(AddressSanitizer, ManyThreadsTest) {
Kostya Serebryany26976872012-12-03 09:43:56 +0000339 const size_t kNumThreads =
340 (SANITIZER_WORDSIZE == 32 || ASAN_AVOID_EXPENSIVE_TESTS) ? 30 : 1000;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000341 pthread_t t[kNumThreads];
342 for (size_t i = 0; i < kNumThreads; i++) {
Kostya Serebryany26976872012-12-03 09:43:56 +0000343 PTHREAD_CREATE(&t[i], 0, ManyThreadsWorker, (void*)i);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000344 }
345 for (size_t i = 0; i < kNumThreads; i++) {
Kostya Serebryany26976872012-12-03 09:43:56 +0000346 PTHREAD_JOIN(t[i], 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000347 }
348}
349
350TEST(AddressSanitizer, ReallocTest) {
351 const int kMinElem = 5;
352 int *ptr = (int*)malloc(sizeof(int) * kMinElem);
353 ptr[3] = 3;
354 for (int i = 0; i < 10000; i++) {
355 ptr = (int*)realloc(ptr,
Evgeniy Stepanov48ddbef2013-01-14 15:12:26 +0000356 (my_rand() % 1000 + kMinElem) * sizeof(int));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000357 EXPECT_EQ(3, ptr[3]);
358 }
Alexey Samsonova3ab1a72013-01-28 11:24:13 +0000359 free(ptr);
360 // Realloc pointer returned by malloc(0).
361 int *ptr2 = Ident((int*)malloc(0));
Alexey Samsonova3ab1a72013-01-28 11:24:13 +0000362 ptr2 = Ident((int*)realloc(ptr2, sizeof(*ptr2)));
Alexey Samsonova3ab1a72013-01-28 11:24:13 +0000363 *ptr2 = 42;
364 EXPECT_EQ(42, *ptr2);
365 free(ptr2);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000366}
367
Timur Iskhodzhanov41d69f42013-05-20 13:05:58 +0000368TEST(AddressSanitizer, ReallocFreedPointerTest) {
369 void *ptr = Ident(malloc(42));
370 ASSERT_TRUE(NULL != ptr);
371 free(ptr);
372 EXPECT_DEATH(ptr = realloc(ptr, 77), "attempting double-free");
373}
374
375TEST(AddressSanitizer, ReallocInvalidPointerTest) {
376 void *ptr = Ident(malloc(42));
377 EXPECT_DEATH(ptr = realloc((int*)ptr + 1, 77), "attempting free.*not malloc");
Alexey Samsonov2ec879e2013-07-18 12:59:52 +0000378 free(ptr);
Timur Iskhodzhanov41d69f42013-05-20 13:05:58 +0000379}
380
Alexey Samsonovd9169932013-01-29 07:51:34 +0000381TEST(AddressSanitizer, ZeroSizeMallocTest) {
382 // Test that malloc(0) and similar functions don't return NULL.
383 void *ptr = Ident(malloc(0));
Alexey Samsonova1800782013-01-29 12:08:12 +0000384 EXPECT_TRUE(NULL != ptr);
Alexey Samsonovd9169932013-01-29 07:51:34 +0000385 free(ptr);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700386#if SANITIZER_TEST_HAS_POSIX_MEMALIGN
Alexey Samsonovd9169932013-01-29 07:51:34 +0000387 int pm_res = posix_memalign(&ptr, 1<<20, 0);
388 EXPECT_EQ(0, pm_res);
Alexey Samsonova1800782013-01-29 12:08:12 +0000389 EXPECT_TRUE(NULL != ptr);
Alexey Samsonovd9169932013-01-29 07:51:34 +0000390 free(ptr);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700391#endif // SANITIZER_TEST_HAS_POSIX_MEMALIGN
Alexey Samsonova1800782013-01-29 12:08:12 +0000392 int *int_ptr = new int[0];
Alexey Samsonovd9169932013-01-29 07:51:34 +0000393 int *int_ptr2 = new int[0];
Alexey Samsonova1800782013-01-29 12:08:12 +0000394 EXPECT_TRUE(NULL != int_ptr);
395 EXPECT_TRUE(NULL != int_ptr2);
396 EXPECT_NE(int_ptr, int_ptr2);
Alexey Samsonovd9169932013-01-29 07:51:34 +0000397 delete[] int_ptr;
398 delete[] int_ptr2;
399}
400
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700401#if SANITIZER_TEST_HAS_MALLOC_USABLE_SIZE
Alexey Samsonov4fd95f12012-01-17 06:39:10 +0000402static const char *kMallocUsableSizeErrorMsg =
Kostya Serebryanyca9b5dd2012-10-15 13:30:38 +0000403 "AddressSanitizer: attempting to call malloc_usable_size()";
Alexey Samsonov4fd95f12012-01-17 06:39:10 +0000404
405TEST(AddressSanitizer, MallocUsableSizeTest) {
406 const size_t kArraySize = 100;
407 char *array = Ident((char*)malloc(kArraySize));
408 int *int_ptr = Ident(new int);
Alexey Samsonov76e84282012-09-12 12:07:36 +0000409 EXPECT_EQ(0U, malloc_usable_size(NULL));
Alexey Samsonov4fd95f12012-01-17 06:39:10 +0000410 EXPECT_EQ(kArraySize, malloc_usable_size(array));
411 EXPECT_EQ(sizeof(int), malloc_usable_size(int_ptr));
412 EXPECT_DEATH(malloc_usable_size((void*)0x123), kMallocUsableSizeErrorMsg);
413 EXPECT_DEATH(malloc_usable_size(array + kArraySize / 2),
414 kMallocUsableSizeErrorMsg);
415 free(array);
416 EXPECT_DEATH(malloc_usable_size(array), kMallocUsableSizeErrorMsg);
Alexey Samsonov2ec879e2013-07-18 12:59:52 +0000417 delete int_ptr;
Alexey Samsonov4fd95f12012-01-17 06:39:10 +0000418}
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700419#endif // SANITIZER_TEST_HAS_MALLOC_USABLE_SIZE
Alexey Samsonov4fd95f12012-01-17 06:39:10 +0000420
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000421void WrongFree() {
422 int *x = (int*)malloc(100 * sizeof(int));
423 // Use the allocated memory, otherwise Clang will optimize it out.
424 Ident(x);
425 free(x + 1);
426}
427
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700428#if !defined(_WIN32) // FIXME: This should be a lit test.
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000429TEST(AddressSanitizer, WrongFreeTest) {
Alexey Samsonov7dd282c2013-03-22 07:40:34 +0000430 EXPECT_DEATH(WrongFree(), ASAN_PCRE_DOTALL
431 "ERROR: AddressSanitizer: attempting free.*not malloc"
432 ".*is located 4 bytes inside of 400-byte region"
433 ".*allocated by thread");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000434}
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700435#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000436
437void DoubleFree() {
438 int *x = (int*)malloc(100 * sizeof(int));
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700439 fprintf(stderr, "DoubleFree: x=%p\n", (void *)x);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000440 free(x);
441 free(x);
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700442 fprintf(stderr, "should have failed in the second free(%p)\n", (void *)x);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000443 abort();
444}
445
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700446#if !defined(_WIN32) // FIXME: This should be a lit test.
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000447TEST(AddressSanitizer, DoubleFreeTest) {
Kostya Serebryany27f49322012-02-08 00:42:29 +0000448 EXPECT_DEATH(DoubleFree(), ASAN_PCRE_DOTALL
Kostya Serebryanyca9b5dd2012-10-15 13:30:38 +0000449 "ERROR: AddressSanitizer: attempting double-free"
Kostya Serebryany27f49322012-02-08 00:42:29 +0000450 ".*is located 0 bytes inside of 400-byte region"
451 ".*freed by thread T0 here"
452 ".*previously allocated by thread T0 here");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000453}
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700454#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000455
456template<int kSize>
Timur Iskhodzhanov93810672012-03-23 13:10:59 +0000457NOINLINE void SizedStackTest() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000458 char a[kSize];
459 char *A = Ident((char*)&a);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700460 const char *expected_death = "AddressSanitizer: stack-buffer-";
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000461 for (size_t i = 0; i < kSize; i++)
462 A[i] = i;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700463 EXPECT_DEATH(A[-1] = 0, expected_death);
464 EXPECT_DEATH(A[-5] = 0, expected_death);
465 EXPECT_DEATH(A[kSize] = 0, expected_death);
466 EXPECT_DEATH(A[kSize + 1] = 0, expected_death);
467 EXPECT_DEATH(A[kSize + 5] = 0, expected_death);
468 if (kSize > 16)
469 EXPECT_DEATH(A[kSize + 31] = 0, expected_death);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000470}
471
472TEST(AddressSanitizer, SimpleStackTest) {
473 SizedStackTest<1>();
474 SizedStackTest<2>();
475 SizedStackTest<3>();
476 SizedStackTest<4>();
477 SizedStackTest<5>();
478 SizedStackTest<6>();
479 SizedStackTest<7>();
480 SizedStackTest<16>();
481 SizedStackTest<25>();
482 SizedStackTest<34>();
483 SizedStackTest<43>();
484 SizedStackTest<51>();
485 SizedStackTest<62>();
486 SizedStackTest<64>();
487 SizedStackTest<128>();
488}
489
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700490#if !defined(_WIN32)
491// FIXME: It's a bit hard to write multi-line death test expectations
492// in a portable way. Anyways, this should just be turned into a lit test.
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000493TEST(AddressSanitizer, ManyStackObjectsTest) {
494 char XXX[10];
495 char YYY[20];
496 char ZZZ[30];
497 Ident(XXX);
498 Ident(YYY);
499 EXPECT_DEATH(Ident(ZZZ)[-1] = 0, ASAN_PCRE_DOTALL "XXX.*YYY.*ZZZ");
500}
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700501#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000502
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000503#if 0 // This test requires online symbolizer.
504// Moved to lit_tests/stack-oob-frames.cc.
505// Reenable here once we have online symbolizer by default.
Timur Iskhodzhanov93810672012-03-23 13:10:59 +0000506NOINLINE static void Frame0(int frame, char *a, char *b, char *c) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000507 char d[4] = {0};
508 char *D = Ident(d);
509 switch (frame) {
510 case 3: a[5]++; break;
511 case 2: b[5]++; break;
512 case 1: c[5]++; break;
513 case 0: D[5]++; break;
514 }
515}
Timur Iskhodzhanov93810672012-03-23 13:10:59 +0000516NOINLINE static void Frame1(int frame, char *a, char *b) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000517 char c[4] = {0}; Frame0(frame, a, b, c);
518 break_optimization(0);
519}
Timur Iskhodzhanov93810672012-03-23 13:10:59 +0000520NOINLINE static void Frame2(int frame, char *a) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000521 char b[4] = {0}; Frame1(frame, a, b);
522 break_optimization(0);
523}
Timur Iskhodzhanov93810672012-03-23 13:10:59 +0000524NOINLINE static void Frame3(int frame) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000525 char a[4] = {0}; Frame2(frame, a);
526 break_optimization(0);
527}
528
529TEST(AddressSanitizer, GuiltyStackFrame0Test) {
530 EXPECT_DEATH(Frame3(0), "located .*in frame <.*Frame0");
531}
532TEST(AddressSanitizer, GuiltyStackFrame1Test) {
533 EXPECT_DEATH(Frame3(1), "located .*in frame <.*Frame1");
534}
535TEST(AddressSanitizer, GuiltyStackFrame2Test) {
536 EXPECT_DEATH(Frame3(2), "located .*in frame <.*Frame2");
537}
538TEST(AddressSanitizer, GuiltyStackFrame3Test) {
539 EXPECT_DEATH(Frame3(3), "located .*in frame <.*Frame3");
540}
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000541#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000542
Timur Iskhodzhanov93810672012-03-23 13:10:59 +0000543NOINLINE void LongJmpFunc1(jmp_buf buf) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000544 // 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 longjmp(buf, 1);
552}
553
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700554NOINLINE void TouchStackFunc() {
555 int a[100]; // long array will intersect with redzones from LongJmpFunc1.
556 int *A = Ident(a);
557 for (int i = 0; i < 100; i++)
558 A[i] = i*i;
559}
560
561// Test that we handle longjmp and do not report false positives on stack.
562TEST(AddressSanitizer, LongJmpTest) {
563 static jmp_buf buf;
564 if (!setjmp(buf)) {
565 LongJmpFunc1(buf);
566 } else {
567 TouchStackFunc();
568 }
569}
570
571#if !defined(_WIN32) // Only basic longjmp is available on Windows.
Timur Iskhodzhanov93810672012-03-23 13:10:59 +0000572NOINLINE void UnderscopeLongJmpFunc1(jmp_buf buf) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000573 // create three red zones for these two stack objects.
574 int a;
575 int b;
576
577 int *A = Ident(&a);
578 int *B = Ident(&b);
579 *A = *B;
580 _longjmp(buf, 1);
581}
582
Timur Iskhodzhanov93810672012-03-23 13:10:59 +0000583NOINLINE void SigLongJmpFunc1(sigjmp_buf buf) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000584 // create three red zones for these two stack objects.
585 int a;
586 int b;
587
588 int *A = Ident(&a);
589 int *B = Ident(&b);
590 *A = *B;
591 siglongjmp(buf, 1);
592}
593
Stephen Hines6d186232014-11-26 17:56:19 -0800594#if !defined(__ANDROID__) && !defined(__arm__) && \
Stephen Hines86277eb2015-03-23 12:06:32 -0700595 !defined(__powerpc64__) && !defined(__powerpc__) && \
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700596 !defined(__aarch64__) && !defined(__mips__) && \
597 !defined(__mips64)
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700598NOINLINE void BuiltinLongJmpFunc1(jmp_buf buf) {
599 // create three red zones for these two stack objects.
600 int a;
601 int b;
602
603 int *A = Ident(&a);
604 int *B = Ident(&b);
605 *A = *B;
606 __builtin_longjmp((void**)buf, 1);
607}
608
Stephen Hines6d186232014-11-26 17:56:19 -0800609// Does not work on Power and ARM:
Kostya Serebryany0bdc46c2013-05-15 15:01:14 +0000610// https://code.google.com/p/address-sanitizer/issues/detail?id=185
Kostya Serebryanyc3a5c172012-11-29 09:02:14 +0000611TEST(AddressSanitizer, BuiltinLongJmpTest) {
Kostya Serebryanyd3ca78f2012-11-28 15:01:23 +0000612 static jmp_buf buf;
Kostya Serebryanyc3a5c172012-11-29 09:02:14 +0000613 if (!__builtin_setjmp((void**)buf)) {
Kostya Serebryanyd3ca78f2012-11-28 15:01:23 +0000614 BuiltinLongJmpFunc1(buf);
615 } else {
616 TouchStackFunc();
617 }
618}
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700619#endif // !defined(__ANDROID__) && !defined(__powerpc64__) &&
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700620 // !defined(__powerpc__) && !defined(__arm__) &&
621 // !defined(__mips__) && !defined(__mips64)
Kostya Serebryanyd3ca78f2012-11-28 15:01:23 +0000622
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000623TEST(AddressSanitizer, UnderscopeLongJmpTest) {
624 static jmp_buf buf;
625 if (!_setjmp(buf)) {
626 UnderscopeLongJmpFunc1(buf);
627 } else {
628 TouchStackFunc();
629 }
630}
631
632TEST(AddressSanitizer, SigLongJmpTest) {
633 static sigjmp_buf buf;
634 if (!sigsetjmp(buf, 1)) {
635 SigLongJmpFunc1(buf);
636 } else {
637 TouchStackFunc();
638 }
639}
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700640#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000641
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700642// FIXME: Why does clang-cl define __EXCEPTIONS?
643#if defined(__EXCEPTIONS) && !defined(_WIN32)
Timur Iskhodzhanov93810672012-03-23 13:10:59 +0000644NOINLINE void ThrowFunc() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000645 // create three red zones for these two stack objects.
646 int a;
647 int b;
648
649 int *A = Ident(&a);
650 int *B = Ident(&b);
651 *A = *B;
652 ASAN_THROW(1);
653}
654
655TEST(AddressSanitizer, CxxExceptionTest) {
656 if (ASAN_UAR) return;
657 // TODO(kcc): this test crashes on 32-bit for some reason...
Kostya Serebryany5af39e52012-11-21 12:38:58 +0000658 if (SANITIZER_WORDSIZE == 32) return;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000659 try {
660 ThrowFunc();
661 } catch(...) {}
662 TouchStackFunc();
663}
664#endif
665
666void *ThreadStackReuseFunc1(void *unused) {
667 // create three red zones for these two stack objects.
668 int a;
669 int b;
670
671 int *A = Ident(&a);
672 int *B = Ident(&b);
673 *A = *B;
674 pthread_exit(0);
675 return 0;
676}
677
678void *ThreadStackReuseFunc2(void *unused) {
679 TouchStackFunc();
680 return 0;
681}
682
683TEST(AddressSanitizer, ThreadStackReuseTest) {
684 pthread_t t;
Kostya Serebryany26976872012-12-03 09:43:56 +0000685 PTHREAD_CREATE(&t, 0, ThreadStackReuseFunc1, 0);
686 PTHREAD_JOIN(t, 0);
687 PTHREAD_CREATE(&t, 0, ThreadStackReuseFunc2, 0);
688 PTHREAD_JOIN(t, 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000689}
690
Kostya Serebryanyb3712702013-12-05 07:36:09 +0000691#if defined(__i686__) || defined(__x86_64__)
692#include <emmintrin.h>
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000693TEST(AddressSanitizer, Store128Test) {
694 char *a = Ident((char*)malloc(Ident(12)));
695 char *p = a;
696 if (((uintptr_t)a % 16) != 0)
697 p = a + 8;
698 assert(((uintptr_t)p % 16) == 0);
699 __m128i value_wide = _mm_set1_epi16(0x1234);
700 EXPECT_DEATH(_mm_store_si128((__m128i*)p, value_wide),
Kostya Serebryanyca9b5dd2012-10-15 13:30:38 +0000701 "AddressSanitizer: heap-buffer-overflow");
Kostya Serebryanyacd5c612011-12-07 21:30:20 +0000702 EXPECT_DEATH(_mm_store_si128((__m128i*)p, value_wide),
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000703 "WRITE of size 16");
704 EXPECT_DEATH(_mm_store_si128((__m128i*)p, value_wide),
705 "located 0 bytes to the right of 12-byte");
706 free(a);
707}
708#endif
709
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700710// FIXME: All tests that use this function should be turned into lit tests.
Kostya Serebryany6cbfae42013-01-22 06:50:42 +0000711string RightOOBErrorMessage(int oob_distance, bool is_write) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000712 assert(oob_distance >= 0);
713 char expected_str[100];
Kostya Serebryany1b057b22013-02-26 07:25:18 +0000714 sprintf(expected_str, ASAN_PCRE_DOTALL
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700715#if !GTEST_USES_SIMPLE_RE
716 "buffer-overflow.*%s.*"
717#endif
718 "located %d bytes to the right",
719#if !GTEST_USES_SIMPLE_RE
720 is_write ? "WRITE" : "READ",
721#endif
722 oob_distance);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000723 return string(expected_str);
724}
725
Kostya Serebryany6cbfae42013-01-22 06:50:42 +0000726string RightOOBWriteMessage(int oob_distance) {
Alexander Potapenkoef8dfd82012-12-12 12:59:47 +0000727 return RightOOBErrorMessage(oob_distance, /*is_write*/true);
728}
729
Kostya Serebryany6cbfae42013-01-22 06:50:42 +0000730string RightOOBReadMessage(int oob_distance) {
Alexander Potapenkoef8dfd82012-12-12 12:59:47 +0000731 return RightOOBErrorMessage(oob_distance, /*is_write*/false);
732}
733
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700734// FIXME: All tests that use this function should be turned into lit tests.
Kostya Serebryany6cbfae42013-01-22 06:50:42 +0000735string LeftOOBErrorMessage(int oob_distance, bool is_write) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000736 assert(oob_distance > 0);
737 char expected_str[100];
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700738 sprintf(expected_str,
739#if !GTEST_USES_SIMPLE_RE
740 ASAN_PCRE_DOTALL "%s.*"
741#endif
742 "located %d bytes to the left",
743#if !GTEST_USES_SIMPLE_RE
744 is_write ? "WRITE" : "READ",
745#endif
746 oob_distance);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000747 return string(expected_str);
748}
749
Kostya Serebryany6cbfae42013-01-22 06:50:42 +0000750string LeftOOBWriteMessage(int oob_distance) {
Alexander Potapenkoef8dfd82012-12-12 12:59:47 +0000751 return LeftOOBErrorMessage(oob_distance, /*is_write*/true);
752}
753
Kostya Serebryany6cbfae42013-01-22 06:50:42 +0000754string LeftOOBReadMessage(int oob_distance) {
Alexander Potapenkoef8dfd82012-12-12 12:59:47 +0000755 return LeftOOBErrorMessage(oob_distance, /*is_write*/false);
756}
757
Kostya Serebryany6cbfae42013-01-22 06:50:42 +0000758string LeftOOBAccessMessage(int oob_distance) {
Alexander Potapenkoada9ba12012-12-12 16:10:46 +0000759 assert(oob_distance > 0);
760 char expected_str[100];
761 sprintf(expected_str, "located %d bytes to the left", oob_distance);
762 return string(expected_str);
763}
764
Kostya Serebryany6cbfae42013-01-22 06:50:42 +0000765char* MallocAndMemsetString(size_t size, char ch) {
Kostya Serebryany44997c32013-01-21 15:04:36 +0000766 char *s = Ident((char*)malloc(size));
767 memset(s, ch, size);
768 return s;
769}
Kostya Serebryany6cbfae42013-01-22 06:50:42 +0000770
771char* MallocAndMemsetString(size_t size) {
Kostya Serebryany44997c32013-01-21 15:04:36 +0000772 return MallocAndMemsetString(size, 'z');
773}
774
Dan Albertb625a872014-10-20 15:35:01 +0000775#if defined(__linux__) && !defined(__ANDROID__)
Kostya Serebryanyc20b3212013-01-18 06:43:13 +0000776#define READ_TEST(READ_N_BYTES) \
777 char *x = new char[10]; \
778 int fd = open("/proc/self/stat", O_RDONLY); \
779 ASSERT_GT(fd, 0); \
780 EXPECT_DEATH(READ_N_BYTES, \
781 ASAN_PCRE_DOTALL \
782 "AddressSanitizer: heap-buffer-overflow" \
783 ".* is located 0 bytes to the right of 10-byte region"); \
784 close(fd); \
785 delete [] x; \
786
Kostya Serebryany8530e2b2012-12-12 09:54:35 +0000787TEST(AddressSanitizer, pread) {
Kostya Serebryanyc20b3212013-01-18 06:43:13 +0000788 READ_TEST(pread(fd, x, 15, 0));
Kostya Serebryany8530e2b2012-12-12 09:54:35 +0000789}
790
791TEST(AddressSanitizer, pread64) {
Kostya Serebryanyc20b3212013-01-18 06:43:13 +0000792 READ_TEST(pread64(fd, x, 15, 0));
Kostya Serebryany8530e2b2012-12-12 09:54:35 +0000793}
794
795TEST(AddressSanitizer, read) {
Kostya Serebryanyc20b3212013-01-18 06:43:13 +0000796 READ_TEST(read(fd, x, 15));
Kostya Serebryany8530e2b2012-12-12 09:54:35 +0000797}
Dan Albertb625a872014-10-20 15:35:01 +0000798#endif // defined(__linux__) && !defined(__ANDROID__)
Kostya Serebryany8530e2b2012-12-12 09:54:35 +0000799
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000800// This test case fails
801// Clang optimizes memcpy/memset calls which lead to unaligned access
802TEST(AddressSanitizer, DISABLED_MemIntrinsicUnalignedAccessTest) {
803 int size = Ident(4096);
804 char *s = Ident((char*)malloc(size));
Alexander Potapenkoef8dfd82012-12-12 12:59:47 +0000805 EXPECT_DEATH(memset(s + size - 1, 0, 2), RightOOBWriteMessage(0));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000806 free(s);
807}
808
809// TODO(samsonov): Add a test with malloc(0)
810// TODO(samsonov): Add tests for str* and mem* functions.
811
Timur Iskhodzhanov93810672012-03-23 13:10:59 +0000812NOINLINE static int LargeFunction(bool do_bad_access) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000813 int *x = new int[100];
814 x[0]++;
815 x[1]++;
816 x[2]++;
817 x[3]++;
818 x[4]++;
819 x[5]++;
820 x[6]++;
821 x[7]++;
822 x[8]++;
823 x[9]++;
824
825 x[do_bad_access ? 100 : 0]++; int res = __LINE__;
826
827 x[10]++;
828 x[11]++;
829 x[12]++;
830 x[13]++;
831 x[14]++;
832 x[15]++;
833 x[16]++;
834 x[17]++;
835 x[18]++;
836 x[19]++;
837
Stephen Hines6d186232014-11-26 17:56:19 -0800838 delete[] x;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000839 return res;
840}
841
842// Test the we have correct debug info for the failing instruction.
843// This test requires the in-process symbolizer to be enabled by default.
844TEST(AddressSanitizer, DISABLED_LargeFunctionSymbolizeTest) {
845 int failing_line = LargeFunction(false);
846 char expected_warning[128];
Kostya Serebryany5a155412012-12-05 17:56:54 +0000847 sprintf(expected_warning, "LargeFunction.*asan_test.*:%d", failing_line);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000848 EXPECT_DEATH(LargeFunction(true), expected_warning);
849}
850
851// Check that we unwind and symbolize correctly.
852TEST(AddressSanitizer, DISABLED_MallocFreeUnwindAndSymbolizeTest) {
853 int *a = (int*)malloc_aaa(sizeof(int));
854 *a = 1;
855 free_aaa(a);
856 EXPECT_DEATH(*a = 1, "free_ccc.*free_bbb.*free_aaa.*"
857 "malloc_fff.*malloc_eee.*malloc_ddd");
858}
859
Kostya Serebryanyb8a59a02013-01-10 11:55:43 +0000860static bool TryToSetThreadName(const char *name) {
861#if defined(__linux__) && defined(PR_SET_NAME)
862 return 0 == prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0);
863#else
864 return false;
Kostya Serebryany716e2f22012-12-07 15:15:01 +0000865#endif
866}
867
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000868void *ThreadedTestAlloc(void *a) {
Kostya Serebryanyb8a59a02013-01-10 11:55:43 +0000869 EXPECT_EQ(true, TryToSetThreadName("AllocThr"));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000870 int **p = (int**)a;
871 *p = new int;
872 return 0;
873}
874
875void *ThreadedTestFree(void *a) {
Kostya Serebryanyb8a59a02013-01-10 11:55:43 +0000876 EXPECT_EQ(true, TryToSetThreadName("FreeThr"));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000877 int **p = (int**)a;
878 delete *p;
879 return 0;
880}
881
882void *ThreadedTestUse(void *a) {
Kostya Serebryanyb8a59a02013-01-10 11:55:43 +0000883 EXPECT_EQ(true, TryToSetThreadName("UseThr"));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000884 int **p = (int**)a;
885 **p = 1;
886 return 0;
887}
888
889void ThreadedTestSpawn() {
890 pthread_t t;
891 int *x;
Kostya Serebryany26976872012-12-03 09:43:56 +0000892 PTHREAD_CREATE(&t, 0, ThreadedTestAlloc, &x);
893 PTHREAD_JOIN(t, 0);
894 PTHREAD_CREATE(&t, 0, ThreadedTestFree, &x);
895 PTHREAD_JOIN(t, 0);
896 PTHREAD_CREATE(&t, 0, ThreadedTestUse, &x);
897 PTHREAD_JOIN(t, 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000898}
899
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700900#if !defined(_WIN32) // FIXME: This should be a lit test.
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000901TEST(AddressSanitizer, ThreadedTest) {
902 EXPECT_DEATH(ThreadedTestSpawn(),
903 ASAN_PCRE_DOTALL
904 "Thread T.*created"
905 ".*Thread T.*created"
906 ".*Thread T.*created");
907}
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700908#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000909
Kostya Serebryanyb8a59a02013-01-10 11:55:43 +0000910void *ThreadedTestFunc(void *unused) {
911 // Check if prctl(PR_SET_NAME) is supported. Return if not.
912 if (!TryToSetThreadName("TestFunc"))
913 return 0;
Kostya Serebryany716e2f22012-12-07 15:15:01 +0000914 EXPECT_DEATH(ThreadedTestSpawn(),
915 ASAN_PCRE_DOTALL
Kostya Serebryanya390ece2012-12-11 06:23:10 +0000916 "WRITE .*thread T. .UseThr."
917 ".*freed by thread T. .FreeThr. here:"
918 ".*previously allocated by thread T. .AllocThr. here:"
Kostya Serebryanyb8a59a02013-01-10 11:55:43 +0000919 ".*Thread T. .UseThr. created by T.*TestFunc"
920 ".*Thread T. .FreeThr. created by T"
921 ".*Thread T. .AllocThr. created by T"
Kostya Serebryany716e2f22012-12-07 15:15:01 +0000922 "");
Kostya Serebryanyb8a59a02013-01-10 11:55:43 +0000923 return 0;
Kostya Serebryany716e2f22012-12-07 15:15:01 +0000924}
Kostya Serebryanyb8a59a02013-01-10 11:55:43 +0000925
926TEST(AddressSanitizer, ThreadNamesTest) {
927 // Run ThreadedTestFunc in a separate thread because it tries to set a
928 // thread name and we don't want to change the main thread's name.
929 pthread_t t;
930 PTHREAD_CREATE(&t, 0, ThreadedTestFunc, 0);
931 PTHREAD_JOIN(t, 0);
932}
Kostya Serebryany716e2f22012-12-07 15:15:01 +0000933
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000934#if ASAN_NEEDS_SEGV
935TEST(AddressSanitizer, ShadowGapTest) {
Kostya Serebryany5af39e52012-11-21 12:38:58 +0000936#if SANITIZER_WORDSIZE == 32
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000937 char *addr = (char*)0x22000000;
938#else
Kostya Serebryany9277b1f2013-05-16 07:54:28 +0000939# if defined(__powerpc64__)
940 char *addr = (char*)0x024000800000;
941# else
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000942 char *addr = (char*)0x0000100000080000;
Kostya Serebryany9277b1f2013-05-16 07:54:28 +0000943# endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000944#endif
Kostya Serebryanyca9b5dd2012-10-15 13:30:38 +0000945 EXPECT_DEATH(*addr = 1, "AddressSanitizer: SEGV on unknown");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000946}
947#endif // ASAN_NEEDS_SEGV
948
949extern "C" {
Timur Iskhodzhanov93810672012-03-23 13:10:59 +0000950NOINLINE static void UseThenFreeThenUse() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000951 char *x = Ident((char*)malloc(8));
952 *x = 1;
953 free_aaa(x);
954 *x = 2;
955}
956}
957
958TEST(AddressSanitizer, UseThenFreeThenUseTest) {
959 EXPECT_DEATH(UseThenFreeThenUse(), "freed by thread");
960}
961
962TEST(AddressSanitizer, StrDupTest) {
963 free(strdup(Ident("123")));
964}
965
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000966// Currently we create and poison redzone at right of global variables.
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000967static char static110[110];
968const char ConstGlob[7] = {1, 2, 3, 4, 5, 6, 7};
969static const char StaticConstGlob[3] = {9, 8, 7};
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000970
971TEST(AddressSanitizer, GlobalTest) {
972 static char func_static15[15];
973
974 static char fs1[10];
975 static char fs2[10];
976 static char fs3[10];
977
978 glob5[Ident(0)] = 0;
979 glob5[Ident(1)] = 0;
980 glob5[Ident(2)] = 0;
981 glob5[Ident(3)] = 0;
982 glob5[Ident(4)] = 0;
983
984 EXPECT_DEATH(glob5[Ident(5)] = 0,
985 "0 bytes to the right of global variable.*glob5.* size 5");
986 EXPECT_DEATH(glob5[Ident(5+6)] = 0,
987 "6 bytes to the right of global variable.*glob5.* size 5");
988 Ident(static110); // avoid optimizations
989 static110[Ident(0)] = 0;
990 static110[Ident(109)] = 0;
991 EXPECT_DEATH(static110[Ident(110)] = 0,
992 "0 bytes to the right of global variable");
993 EXPECT_DEATH(static110[Ident(110+7)] = 0,
994 "7 bytes to the right of global variable");
995
996 Ident(func_static15); // avoid optimizations
997 func_static15[Ident(0)] = 0;
998 EXPECT_DEATH(func_static15[Ident(15)] = 0,
999 "0 bytes to the right of global variable");
1000 EXPECT_DEATH(func_static15[Ident(15 + 9)] = 0,
1001 "9 bytes to the right of global variable");
1002
1003 Ident(fs1);
1004 Ident(fs2);
1005 Ident(fs3);
1006
1007 // We don't create left redzones, so this is not 100% guaranteed to fail.
1008 // But most likely will.
1009 EXPECT_DEATH(fs2[Ident(-1)] = 0, "is located.*of global variable");
1010
1011 EXPECT_DEATH(Ident(Ident(ConstGlob)[8]),
1012 "is located 1 bytes to the right of .*ConstGlob");
1013 EXPECT_DEATH(Ident(Ident(StaticConstGlob)[5]),
1014 "is located 2 bytes to the right of .*StaticConstGlob");
1015
1016 // call stuff from another file.
1017 GlobalsTest(0);
1018}
1019
1020TEST(AddressSanitizer, GlobalStringConstTest) {
1021 static const char *zoo = "FOOBAR123";
1022 const char *p = Ident(zoo);
1023 EXPECT_DEATH(Ident(p[15]), "is ascii string 'FOOBAR123'");
1024}
1025
Kostya Serebryanyc37ca572011-12-15 22:57:32 +00001026TEST(AddressSanitizer, FileNameInGlobalReportTest) {
1027 static char zoo[10];
1028 const char *p = Ident(zoo);
1029 // The file name should be present in the report.
Kostya Serebryany5a155412012-12-05 17:56:54 +00001030 EXPECT_DEATH(Ident(p[15]), "zoo.*asan_test.");
Kostya Serebryanyc37ca572011-12-15 22:57:32 +00001031}
1032
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001033int *ReturnsPointerToALocalObject() {
1034 int a = 0;
1035 return Ident(&a);
1036}
1037
Kostya Serebryany918b18a2011-12-08 23:30:48 +00001038#if ASAN_UAR == 1
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001039TEST(AddressSanitizer, LocalReferenceReturnTest) {
1040 int *(*f)() = Ident(ReturnsPointerToALocalObject);
Kostya Serebryany918b18a2011-12-08 23:30:48 +00001041 int *p = f();
1042 // Call 'f' a few more times, 'p' should still be poisoned.
1043 for (int i = 0; i < 32; i++)
1044 f();
Kostya Serebryanyca9b5dd2012-10-15 13:30:38 +00001045 EXPECT_DEATH(*p = 1, "AddressSanitizer: stack-use-after-return");
Kostya Serebryany918b18a2011-12-08 23:30:48 +00001046 EXPECT_DEATH(*p = 1, "is located.*in frame .*ReturnsPointerToALocal");
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001047}
Kostya Serebryany918b18a2011-12-08 23:30:48 +00001048#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001049
1050template <int kSize>
Timur Iskhodzhanov93810672012-03-23 13:10:59 +00001051NOINLINE static void FuncWithStack() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001052 char x[kSize];
1053 Ident(x)[0] = 0;
1054 Ident(x)[kSize-1] = 0;
1055}
1056
1057static void LotsOfStackReuse() {
1058 int LargeStack[10000];
1059 Ident(LargeStack)[0] = 0;
1060 for (int i = 0; i < 10000; i++) {
1061 FuncWithStack<128 * 1>();
1062 FuncWithStack<128 * 2>();
1063 FuncWithStack<128 * 4>();
1064 FuncWithStack<128 * 8>();
1065 FuncWithStack<128 * 16>();
1066 FuncWithStack<128 * 32>();
1067 FuncWithStack<128 * 64>();
1068 FuncWithStack<128 * 128>();
1069 FuncWithStack<128 * 256>();
1070 FuncWithStack<128 * 512>();
1071 Ident(LargeStack)[0] = 0;
1072 }
1073}
1074
1075TEST(AddressSanitizer, StressStackReuseTest) {
1076 LotsOfStackReuse();
1077}
1078
1079TEST(AddressSanitizer, ThreadedStressStackReuseTest) {
1080 const int kNumThreads = 20;
1081 pthread_t t[kNumThreads];
1082 for (int i = 0; i < kNumThreads; i++) {
Kostya Serebryany26976872012-12-03 09:43:56 +00001083 PTHREAD_CREATE(&t[i], 0, (void* (*)(void *x))LotsOfStackReuse, 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001084 }
1085 for (int i = 0; i < kNumThreads; i++) {
Kostya Serebryany26976872012-12-03 09:43:56 +00001086 PTHREAD_JOIN(t[i], 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001087 }
1088}
1089
Kostya Serebryanyf58f9982012-02-07 00:27:15 +00001090static void *PthreadExit(void *a) {
1091 pthread_exit(0);
Evgeniy Stepanov7b7b55e2012-02-13 12:36:44 +00001092 return 0;
Kostya Serebryanyf58f9982012-02-07 00:27:15 +00001093}
1094
1095TEST(AddressSanitizer, PthreadExitTest) {
1096 pthread_t t;
1097 for (int i = 0; i < 1000; i++) {
Kostya Serebryany26976872012-12-03 09:43:56 +00001098 PTHREAD_CREATE(&t, 0, PthreadExit, 0);
1099 PTHREAD_JOIN(t, 0);
Kostya Serebryanyf58f9982012-02-07 00:27:15 +00001100 }
1101}
1102
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001103// FIXME: Why does clang-cl define __EXCEPTIONS?
1104#if defined(__EXCEPTIONS) && !defined(_WIN32)
Timur Iskhodzhanov93810672012-03-23 13:10:59 +00001105NOINLINE static void StackReuseAndException() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001106 int large_stack[1000];
1107 Ident(large_stack);
1108 ASAN_THROW(1);
1109}
1110
1111// TODO(kcc): support exceptions with use-after-return.
1112TEST(AddressSanitizer, DISABLED_StressStackReuseAndExceptionsTest) {
1113 for (int i = 0; i < 10000; i++) {
1114 try {
1115 StackReuseAndException();
1116 } catch(...) {
1117 }
1118 }
1119}
1120#endif
1121
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001122#if !defined(_WIN32)
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001123TEST(AddressSanitizer, MlockTest) {
1124 EXPECT_EQ(0, mlockall(MCL_CURRENT));
1125 EXPECT_EQ(0, mlock((void*)0x12345, 0x5678));
1126 EXPECT_EQ(0, munlockall());
1127 EXPECT_EQ(0, munlock((void*)0x987, 0x654));
1128}
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001129#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001130
Kostya Serebryanyc655cfa2012-01-17 18:43:52 +00001131struct LargeStruct {
1132 int foo[100];
1133};
1134
1135// Test for bug http://llvm.org/bugs/show_bug.cgi?id=11763.
1136// Struct copy should not cause asan warning even if lhs == rhs.
1137TEST(AddressSanitizer, LargeStructCopyTest) {
1138 LargeStruct a;
1139 *Ident(&a) = *Ident(&a);
1140}
1141
Alexey Samsonovf7f2e432013-09-06 11:07:33 +00001142ATTRIBUTE_NO_SANITIZE_ADDRESS
1143static void NoSanitizeAddress() {
Kostya Serebryany3be19f42012-01-30 21:34:59 +00001144 char *foo = new char[10];
1145 Ident(foo)[10] = 0;
1146 delete [] foo;
1147}
1148
Alexey Samsonovf7f2e432013-09-06 11:07:33 +00001149TEST(AddressSanitizer, AttributeNoSanitizeAddressTest) {
1150 Ident(NoSanitizeAddress)();
Kostya Serebryany3be19f42012-01-30 21:34:59 +00001151}
1152
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001153// The new/delete/etc mismatch checks don't work on Android,
1154// as calls to new/delete go through malloc/free.
1155// OS X support is tracked here:
1156// https://code.google.com/p/address-sanitizer/issues/detail?id=131
1157// Windows support is tracked here:
1158// https://code.google.com/p/address-sanitizer/issues/detail?id=309
Dan Albertb625a872014-10-20 15:35:01 +00001159#if !defined(__ANDROID__) && \
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001160 !defined(__APPLE__) && \
1161 !defined(_WIN32)
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +00001162static string MismatchStr(const string &str) {
1163 return string("AddressSanitizer: alloc-dealloc-mismatch \\(") + str;
1164}
1165
Alexey Samsonov8f0e3112013-01-14 10:18:38 +00001166TEST(AddressSanitizer, AllocDeallocMismatch) {
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +00001167 EXPECT_DEATH(free(Ident(new int)),
1168 MismatchStr("operator new vs free"));
1169 EXPECT_DEATH(free(Ident(new int[2])),
1170 MismatchStr("operator new \\[\\] vs free"));
1171 EXPECT_DEATH(delete (Ident(new int[2])),
1172 MismatchStr("operator new \\[\\] vs operator delete"));
1173 EXPECT_DEATH(delete (Ident((int*)malloc(2 * sizeof(int)))),
1174 MismatchStr("malloc vs operator delete"));
1175 EXPECT_DEATH(delete [] (Ident(new int)),
1176 MismatchStr("operator new vs operator delete \\[\\]"));
1177 EXPECT_DEATH(delete [] (Ident((int*)malloc(2 * sizeof(int)))),
1178 MismatchStr("malloc vs operator delete \\[\\]"));
1179}
Alexey Samsonov29e09222013-01-14 11:07:59 +00001180#endif
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +00001181
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001182// ------------------ demo tests; run each one-by-one -------------
1183// e.g. --gtest_filter=*DemoOOBLeftHigh --gtest_also_run_disabled_tests
1184TEST(AddressSanitizer, DISABLED_DemoThreadedTest) {
1185 ThreadedTestSpawn();
1186}
1187
1188void *SimpleBugOnSTack(void *x = 0) {
1189 char a[20];
1190 Ident(a)[20] = 0;
1191 return 0;
1192}
1193
1194TEST(AddressSanitizer, DISABLED_DemoStackTest) {
1195 SimpleBugOnSTack();
1196}
1197
1198TEST(AddressSanitizer, DISABLED_DemoThreadStackTest) {
1199 pthread_t t;
Kostya Serebryany26976872012-12-03 09:43:56 +00001200 PTHREAD_CREATE(&t, 0, SimpleBugOnSTack, 0);
1201 PTHREAD_JOIN(t, 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001202}
1203
1204TEST(AddressSanitizer, DISABLED_DemoUAFLowIn) {
1205 uaf_test<U1>(10, 0);
1206}
1207TEST(AddressSanitizer, DISABLED_DemoUAFLowLeft) {
1208 uaf_test<U1>(10, -2);
1209}
1210TEST(AddressSanitizer, DISABLED_DemoUAFLowRight) {
1211 uaf_test<U1>(10, 10);
1212}
1213
1214TEST(AddressSanitizer, DISABLED_DemoUAFHigh) {
1215 uaf_test<U1>(kLargeMalloc, 0);
1216}
1217
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001218TEST(AddressSanitizer, DISABLED_DemoOOM) {
Kostya Serebryany5af39e52012-11-21 12:38:58 +00001219 size_t size = SANITIZER_WORDSIZE == 64 ? (size_t)(1ULL << 40) : (0xf0000000);
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001220 printf("%p\n", malloc(size));
1221}
1222
1223TEST(AddressSanitizer, DISABLED_DemoDoubleFreeTest) {
1224 DoubleFree();
1225}
1226
1227TEST(AddressSanitizer, DISABLED_DemoNullDerefTest) {
1228 int *a = 0;
1229 Ident(a)[10] = 0;
1230}
1231
1232TEST(AddressSanitizer, DISABLED_DemoFunctionStaticTest) {
1233 static char a[100];
1234 static char b[100];
1235 static char c[100];
1236 Ident(a);
1237 Ident(b);
1238 Ident(c);
1239 Ident(a)[5] = 0;
1240 Ident(b)[105] = 0;
1241 Ident(a)[5] = 0;
1242}
1243
1244TEST(AddressSanitizer, DISABLED_DemoTooMuchMemoryTest) {
1245 const size_t kAllocSize = (1 << 28) - 1024;
1246 size_t total_size = 0;
1247 while (true) {
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -07001248 void *x = malloc(kAllocSize);
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001249 memset(x, 0, kAllocSize);
1250 total_size += kAllocSize;
Kostya Serebryany0aa04b32012-08-14 15:18:40 +00001251 fprintf(stderr, "total: %ldM %p\n", (long)total_size >> 20, x);
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001252 }
1253}
1254
Kostya Serebryany4eaa1782012-04-19 14:53:51 +00001255// http://code.google.com/p/address-sanitizer/issues/detail?id=66
Kostya Serebryany07963932012-04-23 10:08:16 +00001256TEST(AddressSanitizer, BufferOverflowAfterManyFrees) {
Kostya Serebryany4eaa1782012-04-19 14:53:51 +00001257 for (int i = 0; i < 1000000; i++) {
1258 delete [] (Ident(new char [8644]));
1259 }
1260 char *x = new char[8192];
Kostya Serebryanyca9b5dd2012-10-15 13:30:38 +00001261 EXPECT_DEATH(x[Ident(8192)] = 0, "AddressSanitizer: heap-buffer-overflow");
Kostya Serebryany4eaa1782012-04-19 14:53:51 +00001262 delete [] Ident(x);
1263}
1264
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001265
Evgeniy Stepanov5b6eab92012-03-02 10:42:10 +00001266// Test that instrumentation of stack allocations takes into account
1267// AllocSize of a type, and not its StoreSize (16 vs 10 bytes for long double).
1268// See http://llvm.org/bugs/show_bug.cgi?id=12047 for more details.
1269TEST(AddressSanitizer, LongDoubleNegativeTest) {
1270 long double a, b;
Kostya Serebryany9b90e952012-03-21 15:29:28 +00001271 static long double c;
Evgeniy Stepanov5b6eab92012-03-02 10:42:10 +00001272 memcpy(Ident(&a), Ident(&b), sizeof(long double));
Kostya Serebryany9b90e952012-03-21 15:29:28 +00001273 memcpy(Ident(&c), Ident(&b), sizeof(long double));
Alexey Samsonov76e84282012-09-12 12:07:36 +00001274}
Evgeniy Stepanov56d34722013-05-21 08:12:08 +00001275
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001276#if !defined(_WIN32)
Evgeniy Stepanov56d34722013-05-21 08:12:08 +00001277TEST(AddressSanitizer, pthread_getschedparam) {
1278 int policy;
1279 struct sched_param param;
Kostya Serebryany30e970f2013-05-22 08:54:30 +00001280 EXPECT_DEATH(
1281 pthread_getschedparam(pthread_self(), &policy, Ident(&param) + 2),
Alexey Samsonov722f2e62013-06-06 14:08:40 +00001282 "AddressSanitizer: stack-buffer-.*flow");
Kostya Serebryany30e970f2013-05-22 08:54:30 +00001283 EXPECT_DEATH(
1284 pthread_getschedparam(pthread_self(), Ident(&policy) - 1, &param),
Alexey Samsonov722f2e62013-06-06 14:08:40 +00001285 "AddressSanitizer: stack-buffer-.*flow");
Evgeniy Stepanov56d34722013-05-21 08:12:08 +00001286 int res = pthread_getschedparam(pthread_self(), &policy, &param);
1287 ASSERT_EQ(0, res);
1288}
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001289#endif
Stephen Hines86277eb2015-03-23 12:06:32 -07001290
1291#if SANITIZER_TEST_HAS_PRINTF_L
1292static int vsnprintf_l_wrapper(char *s, size_t n,
1293 locale_t l, const char *format, ...) {
1294 va_list va;
1295 va_start(va, format);
1296 int res = vsnprintf_l(s, n , l, format, va);
1297 va_end(va);
1298 return res;
1299}
1300
1301TEST(AddressSanitizer, snprintf_l) {
1302 char buff[5];
1303 // Check that snprintf_l() works fine with Asan.
1304 int res = snprintf_l(buff, 5,
1305 _LIBCPP_GET_C_LOCALE, "%s", "snprintf_l()");
1306 EXPECT_EQ(12, res);
1307 // Check that vsnprintf_l() works fine with Asan.
1308 res = vsnprintf_l_wrapper(buff, 5,
1309 _LIBCPP_GET_C_LOCALE, "%s", "vsnprintf_l()");
1310 EXPECT_EQ(13, res);
1311
1312 EXPECT_DEATH(snprintf_l(buff, 10,
1313 _LIBCPP_GET_C_LOCALE, "%s", "snprintf_l()"),
1314 "AddressSanitizer: stack-buffer-overflow");
1315 EXPECT_DEATH(vsnprintf_l_wrapper(buff, 10,
1316 _LIBCPP_GET_C_LOCALE, "%s", "vsnprintf_l()"),
1317 "AddressSanitizer: stack-buffer-overflow");
1318}
1319#endif