1 //===-- sanitizer_common_test.cc ------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
11 //
12 //===----------------------------------------------------------------------===//
13 #include "sanitizer_common/sanitizer_allocator_internal.h"
14 #include "sanitizer_common/sanitizer_common.h"
15 #include "sanitizer_common/sanitizer_flags.h"
16 #include "sanitizer_common/sanitizer_libc.h"
17 #include "sanitizer_common/sanitizer_platform.h"
18
19 #include "sanitizer_pthread_wrappers.h"
20
21 #include "gtest/gtest.h"
22
23 namespace __sanitizer {
24
IsSorted(const uptr * array,uptr n)25 static bool IsSorted(const uptr *array, uptr n) {
26 for (uptr i = 1; i < n; i++) {
27 if (array[i] < array[i - 1]) return false;
28 }
29 return true;
30 }
31
TEST(SanitizerCommon,SortTest)32 TEST(SanitizerCommon, SortTest) {
33 uptr array[100];
34 uptr n = 100;
35 // Already sorted.
36 for (uptr i = 0; i < n; i++) {
37 array[i] = i;
38 }
39 SortArray(array, n);
40 EXPECT_TRUE(IsSorted(array, n));
41 // Reverse order.
42 for (uptr i = 0; i < n; i++) {
43 array[i] = n - 1 - i;
44 }
45 SortArray(array, n);
46 EXPECT_TRUE(IsSorted(array, n));
47 // Mixed order.
48 for (uptr i = 0; i < n; i++) {
49 array[i] = (i % 2 == 0) ? i : n - 1 - i;
50 }
51 SortArray(array, n);
52 EXPECT_TRUE(IsSorted(array, n));
53 // All equal.
54 for (uptr i = 0; i < n; i++) {
55 array[i] = 42;
56 }
57 SortArray(array, n);
58 EXPECT_TRUE(IsSorted(array, n));
59 // All but one sorted.
60 for (uptr i = 0; i < n - 1; i++) {
61 array[i] = i;
62 }
63 array[n - 1] = 42;
64 SortArray(array, n);
65 EXPECT_TRUE(IsSorted(array, n));
66 // Minimal case - sort three elements.
67 array[0] = 1;
68 array[1] = 0;
69 SortArray(array, 2);
70 EXPECT_TRUE(IsSorted(array, 2));
71 }
72
TEST(SanitizerCommon,MmapAlignedOrDie)73 TEST(SanitizerCommon, MmapAlignedOrDie) {
74 uptr PageSize = GetPageSizeCached();
75 for (uptr size = 1; size <= 32; size *= 2) {
76 for (uptr alignment = 1; alignment <= 32; alignment *= 2) {
77 for (int iter = 0; iter < 100; iter++) {
78 uptr res = (uptr)MmapAlignedOrDie(
79 size * PageSize, alignment * PageSize, "MmapAlignedOrDieTest");
80 EXPECT_EQ(0U, res % (alignment * PageSize));
81 internal_memset((void*)res, 1, size * PageSize);
82 UnmapOrDie((void*)res, size * PageSize);
83 }
84 }
85 }
86 }
87
88 #if SANITIZER_LINUX
TEST(SanitizerCommon,SanitizerSetThreadName)89 TEST(SanitizerCommon, SanitizerSetThreadName) {
90 const char *names[] = {
91 "0123456789012",
92 "01234567890123",
93 "012345678901234", // Larger names will be truncated on linux.
94 };
95
96 for (size_t i = 0; i < ARRAY_SIZE(names); i++) {
97 EXPECT_TRUE(SanitizerSetThreadName(names[i]));
98 char buff[100];
99 EXPECT_TRUE(SanitizerGetThreadName(buff, sizeof(buff) - 1));
100 EXPECT_EQ(0, internal_strcmp(buff, names[i]));
101 }
102 }
103 #endif
104
TEST(SanitizerCommon,InternalMmapVector)105 TEST(SanitizerCommon, InternalMmapVector) {
106 InternalMmapVector<uptr> vector(1);
107 for (uptr i = 0; i < 100; i++) {
108 EXPECT_EQ(i, vector.size());
109 vector.push_back(i);
110 }
111 for (uptr i = 0; i < 100; i++) {
112 EXPECT_EQ(i, vector[i]);
113 }
114 for (int i = 99; i >= 0; i--) {
115 EXPECT_EQ((uptr)i, vector.back());
116 vector.pop_back();
117 EXPECT_EQ((uptr)i, vector.size());
118 }
119 InternalMmapVector<uptr> empty_vector(0);
120 CHECK_GT(empty_vector.capacity(), 0U);
121 CHECK_EQ(0U, empty_vector.size());
122 }
123
TestThreadInfo(bool main)124 void TestThreadInfo(bool main) {
125 uptr stk_addr = 0;
126 uptr stk_size = 0;
127 uptr tls_addr = 0;
128 uptr tls_size = 0;
129 GetThreadStackAndTls(main, &stk_addr, &stk_size, &tls_addr, &tls_size);
130
131 int stack_var;
132 EXPECT_NE(stk_addr, (uptr)0);
133 EXPECT_NE(stk_size, (uptr)0);
134 EXPECT_GT((uptr)&stack_var, stk_addr);
135 EXPECT_LT((uptr)&stack_var, stk_addr + stk_size);
136
137 #if SANITIZER_LINUX && defined(__x86_64__)
138 static __thread int thread_var;
139 EXPECT_NE(tls_addr, (uptr)0);
140 EXPECT_NE(tls_size, (uptr)0);
141 EXPECT_GT((uptr)&thread_var, tls_addr);
142 EXPECT_LT((uptr)&thread_var, tls_addr + tls_size);
143
144 // Ensure that tls and stack do not intersect.
145 uptr tls_end = tls_addr + tls_size;
146 EXPECT_TRUE(tls_addr < stk_addr || tls_addr >= stk_addr + stk_size);
147 EXPECT_TRUE(tls_end < stk_addr || tls_end >= stk_addr + stk_size);
148 EXPECT_TRUE((tls_addr < stk_addr) == (tls_end < stk_addr));
149 #endif
150 }
151
WorkerThread(void * arg)152 static void *WorkerThread(void *arg) {
153 TestThreadInfo(false);
154 return 0;
155 }
156
TEST(SanitizerCommon,ThreadStackTlsMain)157 TEST(SanitizerCommon, ThreadStackTlsMain) {
158 InitTlsSize();
159 TestThreadInfo(true);
160 }
161
TEST(SanitizerCommon,ThreadStackTlsWorker)162 TEST(SanitizerCommon, ThreadStackTlsWorker) {
163 InitTlsSize();
164 pthread_t t;
165 PTHREAD_CREATE(&t, 0, WorkerThread, 0);
166 PTHREAD_JOIN(t, 0);
167 }
168
UptrLess(uptr a,uptr b)169 bool UptrLess(uptr a, uptr b) {
170 return a < b;
171 }
172
TEST(SanitizerCommon,InternalBinarySearch)173 TEST(SanitizerCommon, InternalBinarySearch) {
174 static const uptr kSize = 5;
175 uptr arr[kSize];
176 for (uptr i = 0; i < kSize; i++) arr[i] = i * i;
177
178 for (uptr i = 0; i < kSize; i++)
179 ASSERT_EQ(InternalBinarySearch(arr, 0, kSize, i * i, UptrLess), i);
180
181 ASSERT_EQ(InternalBinarySearch(arr, 0, kSize, 7, UptrLess), kSize + 1);
182 }
183
184 #if SANITIZER_LINUX && !SANITIZER_ANDROID
TEST(SanitizerCommon,FindPathToBinary)185 TEST(SanitizerCommon, FindPathToBinary) {
186 char *true_path = FindPathToBinary("true");
187 EXPECT_NE((char*)0, internal_strstr(true_path, "/bin/true"));
188 InternalFree(true_path);
189 EXPECT_EQ(0, FindPathToBinary("unexisting_binary.ergjeorj"));
190 }
191 #endif
192
TEST(SanitizerCommon,StripPathPrefix)193 TEST(SanitizerCommon, StripPathPrefix) {
194 EXPECT_EQ(0, StripPathPrefix(0, "prefix"));
195 EXPECT_STREQ("foo", StripPathPrefix("foo", 0));
196 EXPECT_STREQ("dir/file.cc",
197 StripPathPrefix("/usr/lib/dir/file.cc", "/usr/lib/"));
198 EXPECT_STREQ("/file.cc", StripPathPrefix("/usr/myroot/file.cc", "/myroot"));
199 EXPECT_STREQ("file.h", StripPathPrefix("/usr/lib/./file.h", "/usr/lib/"));
200 }
201
TEST(SanitizerCommon,InternalScopedString)202 TEST(SanitizerCommon, InternalScopedString) {
203 InternalScopedString str(10);
204 EXPECT_EQ(0U, str.length());
205 EXPECT_STREQ("", str.data());
206
207 str.append("foo");
208 EXPECT_EQ(3U, str.length());
209 EXPECT_STREQ("foo", str.data());
210
211 int x = 1234;
212 str.append("%d", x);
213 EXPECT_EQ(7U, str.length());
214 EXPECT_STREQ("foo1234", str.data());
215
216 str.append("%d", x);
217 EXPECT_EQ(9U, str.length());
218 EXPECT_STREQ("foo123412", str.data());
219
220 str.clear();
221 EXPECT_EQ(0U, str.length());
222 EXPECT_STREQ("", str.data());
223
224 str.append("0123456789");
225 EXPECT_EQ(9U, str.length());
226 EXPECT_STREQ("012345678", str.data());
227 }
228
TEST(SanitizerCommon,PrintSourceLocation)229 TEST(SanitizerCommon, PrintSourceLocation) {
230 InternalScopedString str(128);
231 PrintSourceLocation(&str, "/dir/file.cc", 10, 5);
232 EXPECT_STREQ("/dir/file.cc:10:5", str.data());
233
234 str.clear();
235 PrintSourceLocation(&str, "/dir/file.cc", 11, 0);
236 EXPECT_STREQ("/dir/file.cc:11", str.data());
237
238 str.clear();
239 PrintSourceLocation(&str, "/dir/file.cc", 0, 0);
240 EXPECT_STREQ("/dir/file.cc", str.data());
241
242 // Check that we strip file prefix if necessary.
243 const char *old_strip_path_prefix = common_flags()->strip_path_prefix;
244 common_flags()->strip_path_prefix = "/dir/";
245 str.clear();
246 PrintSourceLocation(&str, "/dir/file.cc", 10, 5);
247 EXPECT_STREQ("file.cc:10:5", str.data());
248 common_flags()->strip_path_prefix = old_strip_path_prefix;
249 }
250
TEST(SanitizerCommon,PrintModuleAndOffset)251 TEST(SanitizerCommon, PrintModuleAndOffset) {
252 InternalScopedString str(128);
253 PrintModuleAndOffset(&str, "/dir/exe", 0x123);
254 EXPECT_STREQ("(/dir/exe+0x123)", str.data());
255
256 // Check that we strip file prefix if necessary.
257 const char *old_strip_path_prefix = common_flags()->strip_path_prefix;
258 common_flags()->strip_path_prefix = "/dir/";
259 str.clear();
260 PrintModuleAndOffset(&str, "/dir/exe", 0x123);
261 EXPECT_STREQ("(exe+0x123)", str.data());
262 common_flags()->strip_path_prefix = old_strip_path_prefix;
263 }
264
265 } // namespace __sanitizer
266