blob: 6a428fbbc2b9dd2ff3ef0defda95b76bbceefb58 [file] [log] [blame]
Alexey Samsonov7da85032012-09-28 12:24:23 +00001//===-- asan_noinst_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// This test file should be compiled w/o asan instrumentation.
13//===----------------------------------------------------------------------===//
Chandler Carruthd865fec2012-08-29 02:27:54 +000014
Kostya Serebryany1e172b42011-11-30 01:07:02 +000015#include "asan_allocator.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000016#include "asan_internal.h"
17#include "asan_mapping.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000018#include "asan_test_utils.h"
Stephen Hines6a211c52014-07-21 00:49:56 -070019#include <sanitizer/allocator_interface.h>
Kostya Serebryany1e172b42011-11-30 01:07:02 +000020
21#include <assert.h>
22#include <stdio.h>
23#include <stdlib.h>
Alexander Potapenko79d12e82012-07-23 08:22:27 +000024#include <string.h> // for memset()
Kostya Serebryany1e172b42011-11-30 01:07:02 +000025#include <algorithm>
Alexander Potapenko79d12e82012-07-23 08:22:27 +000026#include <vector>
Kostya Serebryany65199f12013-01-25 11:46:22 +000027#include <limits>
Kostya Serebryany1e172b42011-11-30 01:07:02 +000028
Stephen Hines2d1fdb22014-05-28 23:58:16 -070029// ATTENTION!
30// Please don't call intercepted functions (including malloc() and friends)
31// in this test. The static runtime library is linked explicitly (without
32// -fsanitize=address), thus the interceptors do not work correctly on OS X.
Alexey Samsonov05fa3802013-09-16 15:50:53 +000033
Alexey Samsonov05fa3802013-09-16 15:50:53 +000034// Make sure __asan_init is called before any test case is run.
35struct AsanInitCaller {
Stephen Hines86277eb2015-03-23 12:06:32 -070036 AsanInitCaller() {
37 __asan::DisableReexec();
38 __asan_init();
39 }
Alexey Samsonov05fa3802013-09-16 15:50:53 +000040};
41static AsanInitCaller asan_init_caller;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000042
43TEST(AddressSanitizer, InternalSimpleDeathTest) {
44 EXPECT_DEATH(exit(1), "");
45}
46
47static void MallocStress(size_t n) {
Evgeniy Stepanov48ddbef2013-01-14 15:12:26 +000048 u32 seed = my_rand();
Stephen Hines6d186232014-11-26 17:56:19 -080049 BufferedStackTrace stack1;
50 stack1.trace_buffer[0] = 0xa123;
51 stack1.trace_buffer[1] = 0xa456;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000052 stack1.size = 2;
53
Stephen Hines6d186232014-11-26 17:56:19 -080054 BufferedStackTrace stack2;
55 stack2.trace_buffer[0] = 0xb123;
56 stack2.trace_buffer[1] = 0xb456;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000057 stack2.size = 2;
58
Stephen Hines6d186232014-11-26 17:56:19 -080059 BufferedStackTrace stack3;
60 stack3.trace_buffer[0] = 0xc123;
61 stack3.trace_buffer[1] = 0xc456;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000062 stack3.size = 2;
63
64 std::vector<void *> vec;
65 for (size_t i = 0; i < n; i++) {
66 if ((i % 3) == 0) {
67 if (vec.empty()) continue;
Evgeniy Stepanov48ddbef2013-01-14 15:12:26 +000068 size_t idx = my_rand_r(&seed) % vec.size();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000069 void *ptr = vec[idx];
70 vec[idx] = vec.back();
71 vec.pop_back();
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000072 __asan::asan_free(ptr, &stack1, __asan::FROM_MALLOC);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000073 } else {
Evgeniy Stepanov48ddbef2013-01-14 15:12:26 +000074 size_t size = my_rand_r(&seed) % 1000 + 1;
75 switch ((my_rand_r(&seed) % 128)) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000076 case 0: size += 1024; break;
77 case 1: size += 2048; break;
78 case 2: size += 4096; break;
79 }
Evgeniy Stepanov48ddbef2013-01-14 15:12:26 +000080 size_t alignment = 1 << (my_rand_r(&seed) % 10 + 1);
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000081 char *ptr = (char*)__asan::asan_memalign(alignment, size,
82 &stack2, __asan::FROM_MALLOC);
Alexey Samsonov1b17f5b2013-11-13 14:46:58 +000083 EXPECT_EQ(size, __asan::asan_malloc_usable_size(ptr, 0, 0));
Kostya Serebryany1e172b42011-11-30 01:07:02 +000084 vec.push_back(ptr);
85 ptr[0] = 0;
86 ptr[size-1] = 0;
87 ptr[size/2] = 0;
88 }
89 }
90 for (size_t i = 0; i < vec.size(); i++)
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000091 __asan::asan_free(vec[i], &stack3, __asan::FROM_MALLOC);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000092}
93
94
95TEST(AddressSanitizer, NoInstMallocTest) {
Kostya Serebryanyd39a34e2013-03-14 13:16:09 +000096 MallocStress(ASAN_LOW_MEMORY ? 300000 : 1000000);
97}
98
99TEST(AddressSanitizer, ThreadedMallocStressTest) {
100 const int kNumThreads = 4;
101 const int kNumIterations = (ASAN_LOW_MEMORY) ? 10000 : 100000;
102 pthread_t t[kNumThreads];
103 for (int i = 0; i < kNumThreads; i++) {
104 PTHREAD_CREATE(&t[i], 0, (void* (*)(void *x))MallocStress,
105 (void*)kNumIterations);
106 }
107 for (int i = 0; i < kNumThreads; i++) {
108 PTHREAD_JOIN(t[i], 0);
109 }
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000110}
111
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000112static void PrintShadow(const char *tag, uptr ptr, size_t size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000113 fprintf(stderr, "%s shadow: %lx size % 3ld: ", tag, (long)ptr, (long)size);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000114 uptr prev_shadow = 0;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000115 for (sptr i = -32; i < (sptr)size + 32; i++) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000116 uptr shadow = __asan::MemToShadow(ptr + i);
Kostya Serebryanyee392552012-05-31 15:02:07 +0000117 if (i == 0 || i == (sptr)size)
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000118 fprintf(stderr, ".");
119 if (shadow != prev_shadow) {
120 prev_shadow = shadow;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000121 fprintf(stderr, "%02x", (int)*(u8*)shadow);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000122 }
123 }
124 fprintf(stderr, "\n");
125}
126
127TEST(AddressSanitizer, DISABLED_InternalPrintShadow) {
128 for (size_t size = 1; size <= 513; size++) {
129 char *ptr = new char[size];
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000130 PrintShadow("m", (uptr)ptr, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000131 delete [] ptr;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000132 PrintShadow("f", (uptr)ptr, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000133 }
134}
135
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000136TEST(AddressSanitizer, QuarantineTest) {
Stephen Hines6d186232014-11-26 17:56:19 -0800137 BufferedStackTrace stack;
138 stack.trace_buffer[0] = 0x890;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000139 stack.size = 1;
140
Kostya Serebryanyd39a34e2013-03-14 13:16:09 +0000141 const int size = 1024;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000142 void *p = __asan::asan_malloc(size, &stack);
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +0000143 __asan::asan_free(p, &stack, __asan::FROM_MALLOC);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000144 size_t i;
145 size_t max_i = 1 << 30;
146 for (i = 0; i < max_i; i++) {
147 void *p1 = __asan::asan_malloc(size, &stack);
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +0000148 __asan::asan_free(p1, &stack, __asan::FROM_MALLOC);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000149 if (p1 == p) break;
150 }
Kostya Serebryanyd39a34e2013-03-14 13:16:09 +0000151 EXPECT_GE(i, 10000U);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000152 EXPECT_LT(i, max_i);
153}
154
155void *ThreadedQuarantineTestWorker(void *unused) {
Alexey Samsonov1a7741b2012-07-24 08:26:19 +0000156 (void)unused;
Evgeniy Stepanov48ddbef2013-01-14 15:12:26 +0000157 u32 seed = my_rand();
Stephen Hines6d186232014-11-26 17:56:19 -0800158 BufferedStackTrace stack;
159 stack.trace_buffer[0] = 0x890;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000160 stack.size = 1;
161
162 for (size_t i = 0; i < 1000; i++) {
Evgeniy Stepanov48ddbef2013-01-14 15:12:26 +0000163 void *p = __asan::asan_malloc(1 + (my_rand_r(&seed) % 4000), &stack);
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +0000164 __asan::asan_free(p, &stack, __asan::FROM_MALLOC);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000165 }
166 return NULL;
167}
168
169// Check that the thread local allocators are flushed when threads are
170// destroyed.
171TEST(AddressSanitizer, ThreadedQuarantineTest) {
172 const int n_threads = 3000;
Stephen Hines6a211c52014-07-21 00:49:56 -0700173 size_t mmaped1 = __sanitizer_get_heap_size();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000174 for (int i = 0; i < n_threads; i++) {
175 pthread_t t;
Kostya Serebryany26976872012-12-03 09:43:56 +0000176 PTHREAD_CREATE(&t, NULL, ThreadedQuarantineTestWorker, 0);
177 PTHREAD_JOIN(t, 0);
Stephen Hines6a211c52014-07-21 00:49:56 -0700178 size_t mmaped2 = __sanitizer_get_heap_size();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000179 EXPECT_LT(mmaped2 - mmaped1, 320U * (1 << 20));
180 }
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000181}
182
183void *ThreadedOneSizeMallocStress(void *unused) {
Alexey Samsonov1a7741b2012-07-24 08:26:19 +0000184 (void)unused;
Stephen Hines6d186232014-11-26 17:56:19 -0800185 BufferedStackTrace stack;
186 stack.trace_buffer[0] = 0x890;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000187 stack.size = 1;
188 const size_t kNumMallocs = 1000;
189 for (int iter = 0; iter < 1000; iter++) {
190 void *p[kNumMallocs];
191 for (size_t i = 0; i < kNumMallocs; i++) {
192 p[i] = __asan::asan_malloc(32, &stack);
193 }
194 for (size_t i = 0; i < kNumMallocs; i++) {
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +0000195 __asan::asan_free(p[i], &stack, __asan::FROM_MALLOC);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000196 }
197 }
198 return NULL;
199}
200
201TEST(AddressSanitizer, ThreadedOneSizeMallocStressTest) {
202 const int kNumThreads = 4;
203 pthread_t t[kNumThreads];
204 for (int i = 0; i < kNumThreads; i++) {
Kostya Serebryany26976872012-12-03 09:43:56 +0000205 PTHREAD_CREATE(&t[i], 0, ThreadedOneSizeMallocStress, 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000206 }
207 for (int i = 0; i < kNumThreads; i++) {
Kostya Serebryany26976872012-12-03 09:43:56 +0000208 PTHREAD_JOIN(t[i], 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000209 }
210}
Kostya Serebryanybff53362012-06-25 14:23:07 +0000211
Alexey Samsonov05fa3802013-09-16 15:50:53 +0000212TEST(AddressSanitizer, ShadowRegionIsPoisonedTest) {
Kostya Serebryanye5ab9682013-01-23 13:27:43 +0000213 using __asan::kHighMemEnd;
Alexey Samsonov05fa3802013-09-16 15:50:53 +0000214 // Check that __asan_region_is_poisoned works for shadow regions.
215 uptr ptr = kLowShadowBeg + 200;
216 EXPECT_EQ(ptr, __asan_region_is_poisoned(ptr, 100));
217 ptr = kShadowGapBeg + 200;
218 EXPECT_EQ(ptr, __asan_region_is_poisoned(ptr, 100));
219 ptr = kHighShadowBeg + 200;
220 EXPECT_EQ(ptr, __asan_region_is_poisoned(ptr, 100));
Kostya Serebryanyb9e13192013-02-27 13:38:19 +0000221}
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700222
223// Test __asan_load1 & friends.
224TEST(AddressSanitizer, LoadStoreCallbacks) {
225 typedef void (*CB)(uptr p);
226 CB cb[2][5] = {
227 {
228 __asan_load1, __asan_load2, __asan_load4, __asan_load8, __asan_load16,
229 }, {
230 __asan_store1, __asan_store2, __asan_store4, __asan_store8,
231 __asan_store16,
232 }
233 };
234
235 uptr buggy_ptr;
236
237 __asan_test_only_reported_buggy_pointer = &buggy_ptr;
Stephen Hines6d186232014-11-26 17:56:19 -0800238 BufferedStackTrace stack;
239 stack.trace_buffer[0] = 0x890;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700240 stack.size = 1;
241
242 for (uptr len = 16; len <= 32; len++) {
243 char *ptr = (char*) __asan::asan_malloc(len, &stack);
244 uptr p = reinterpret_cast<uptr>(ptr);
245 for (uptr is_write = 0; is_write <= 1; is_write++) {
246 for (uptr size_log = 0; size_log <= 4; size_log++) {
247 uptr size = 1 << size_log;
248 CB call = cb[is_write][size_log];
249 // Iterate only size-aligned offsets.
250 for (uptr offset = 0; offset <= len; offset += size) {
251 buggy_ptr = 0;
252 call(p + offset);
253 if (offset + size <= len)
254 EXPECT_EQ(buggy_ptr, 0U);
255 else
256 EXPECT_EQ(buggy_ptr, p + offset);
257 }
258 }
259 }
260 __asan::asan_free(ptr, &stack, __asan::FROM_MALLOC);
261 }
262 __asan_test_only_reported_buggy_pointer = 0;
263}