Alexey Samsonov | 0dc3177 | 2012-07-16 11:27:17 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Alexey Samsonov | 1dcd1d9 | 2013-09-03 13:20:48 +0000 | [diff] [blame] | 13 | #include "sanitizer_common/sanitizer_allocator_internal.h" |
Alexey Samsonov | 0dc3177 | 2012-07-16 11:27:17 +0000 | [diff] [blame] | 14 | #include "sanitizer_common/sanitizer_common.h" |
Alexey Samsonov | bb4697f | 2013-11-14 09:41:24 +0000 | [diff] [blame] | 15 | #include "sanitizer_common/sanitizer_flags.h" |
Kostya Serebryany | dddb18b | 2012-12-07 11:27:24 +0000 | [diff] [blame] | 16 | #include "sanitizer_common/sanitizer_libc.h" |
Sergey Matveev | a615b26 | 2013-05-14 13:19:33 +0000 | [diff] [blame] | 17 | #include "sanitizer_common/sanitizer_platform.h" |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 18 | |
| 19 | #include "sanitizer_pthread_wrappers.h" |
| 20 | |
Alexey Samsonov | 0dc3177 | 2012-07-16 11:27:17 +0000 | [diff] [blame] | 21 | #include "gtest/gtest.h" |
| 22 | |
| 23 | namespace __sanitizer { |
| 24 | |
| 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 | |
| 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 | |
Kostya Serebryany | cc75259 | 2012-12-06 06:10:31 +0000 | [diff] [blame] | 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)); |
Dmitry Vyukov | 81088eb | 2012-12-25 09:20:58 +0000 | [diff] [blame] | 81 | internal_memset((void*)res, 1, size * PageSize); |
Kostya Serebryany | cc75259 | 2012-12-06 06:10:31 +0000 | [diff] [blame] | 82 | UnmapOrDie((void*)res, size * PageSize); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
Sergey Matveev | a615b26 | 2013-05-14 13:19:33 +0000 | [diff] [blame] | 88 | #if SANITIZER_LINUX |
Kostya Serebryany | dddb18b | 2012-12-07 11:27:24 +0000 | [diff] [blame] | 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)); |
Kostya Serebryany | dddb18b | 2012-12-07 11:27:24 +0000 | [diff] [blame] | 100 | EXPECT_EQ(0, internal_strcmp(buff, names[i])); |
| 101 | } |
| 102 | } |
| 103 | #endif |
| 104 | |
Alexey Samsonov | a64d435 | 2013-06-14 09:59:40 +0000 | [diff] [blame] | 105 | TEST(SanitizerCommon, InternalMmapVector) { |
| 106 | InternalMmapVector<uptr> vector(1); |
Kostya Serebryany | b5f9521 | 2013-02-26 13:30:27 +0000 | [diff] [blame] | 107 | for (uptr i = 0; i < 100; i++) { |
Alexey Samsonov | bcf393e | 2013-03-05 12:23:07 +0000 | [diff] [blame] | 108 | EXPECT_EQ(i, vector.size()); |
Kostya Serebryany | b5f9521 | 2013-02-26 13:30:27 +0000 | [diff] [blame] | 109 | vector.push_back(i); |
| 110 | } |
| 111 | for (uptr i = 0; i < 100; i++) { |
Alexey Samsonov | bcf393e | 2013-03-05 12:23:07 +0000 | [diff] [blame] | 112 | EXPECT_EQ(i, vector[i]); |
Kostya Serebryany | b5f9521 | 2013-02-26 13:30:27 +0000 | [diff] [blame] | 113 | } |
Alexey Samsonov | 352b245 | 2013-03-05 11:58:25 +0000 | [diff] [blame] | 114 | for (int i = 99; i >= 0; i--) { |
Alexey Samsonov | bcf393e | 2013-03-05 12:23:07 +0000 | [diff] [blame] | 115 | EXPECT_EQ((uptr)i, vector.back()); |
Alexey Samsonov | 352b245 | 2013-03-05 11:58:25 +0000 | [diff] [blame] | 116 | vector.pop_back(); |
Alexey Samsonov | bcf393e | 2013-03-05 12:23:07 +0000 | [diff] [blame] | 117 | EXPECT_EQ((uptr)i, vector.size()); |
Alexey Samsonov | 352b245 | 2013-03-05 11:58:25 +0000 | [diff] [blame] | 118 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 119 | InternalMmapVector<uptr> empty_vector(0); |
| 120 | CHECK_GT(empty_vector.capacity(), 0U); |
| 121 | CHECK_EQ(0U, empty_vector.size()); |
Kostya Serebryany | b5f9521 | 2013-02-26 13:30:27 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Sergey Matveev | 24323de | 2013-05-07 14:41:43 +0000 | [diff] [blame] | 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 | |
Sergey Matveev | a615b26 | 2013-05-14 13:19:33 +0000 | [diff] [blame] | 137 | #if SANITIZER_LINUX && defined(__x86_64__) |
Sergey Matveev | 24323de | 2013-05-07 14:41:43 +0000 | [diff] [blame] | 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 | |
| 152 | static void *WorkerThread(void *arg) { |
| 153 | TestThreadInfo(false); |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | TEST(SanitizerCommon, ThreadStackTlsMain) { |
| 158 | InitTlsSize(); |
| 159 | TestThreadInfo(true); |
| 160 | } |
| 161 | |
Sergey Matveev | a615b26 | 2013-05-14 13:19:33 +0000 | [diff] [blame] | 162 | TEST(SanitizerCommon, ThreadStackTlsWorker) { |
Sergey Matveev | 24323de | 2013-05-07 14:41:43 +0000 | [diff] [blame] | 163 | InitTlsSize(); |
| 164 | pthread_t t; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 165 | PTHREAD_CREATE(&t, 0, WorkerThread, 0); |
| 166 | PTHREAD_JOIN(t, 0); |
Sergey Matveev | 24323de | 2013-05-07 14:41:43 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Sergey Matveev | 384a448 | 2013-08-26 13:24:43 +0000 | [diff] [blame] | 169 | bool UptrLess(uptr a, uptr b) { |
| 170 | return a < b; |
| 171 | } |
| 172 | |
| 173 | TEST(SanitizerCommon, InternalBinarySearch) { |
Alexey Samsonov | c154820 | 2013-08-28 11:26:09 +0000 | [diff] [blame] | 174 | static const uptr kSize = 5; |
| 175 | uptr arr[kSize]; |
| 176 | for (uptr i = 0; i < kSize; i++) arr[i] = i * i; |
Sergey Matveev | 384a448 | 2013-08-26 13:24:43 +0000 | [diff] [blame] | 177 | |
Alexey Samsonov | c154820 | 2013-08-28 11:26:09 +0000 | [diff] [blame] | 178 | for (uptr i = 0; i < kSize; i++) |
| 179 | ASSERT_EQ(InternalBinarySearch(arr, 0, kSize, i * i, UptrLess), i); |
Sergey Matveev | 384a448 | 2013-08-26 13:24:43 +0000 | [diff] [blame] | 180 | |
Alexey Samsonov | c154820 | 2013-08-28 11:26:09 +0000 | [diff] [blame] | 181 | ASSERT_EQ(InternalBinarySearch(arr, 0, kSize, 7, UptrLess), kSize + 1); |
Sergey Matveev | 384a448 | 2013-08-26 13:24:43 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Alexey Samsonov | db9d684 | 2013-09-04 06:40:48 +0000 | [diff] [blame] | 184 | #if SANITIZER_LINUX && !SANITIZER_ANDROID |
Alexey Samsonov | 1dcd1d9 | 2013-09-03 13:20:48 +0000 | [diff] [blame] | 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 | } |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 191 | #elif SANITIZER_WINDOWS |
| 192 | TEST(SanitizerCommon, FindPathToBinary) { |
| 193 | // ntdll.dll should be on PATH in all supported test environments on all |
| 194 | // supported Windows versions. |
| 195 | char *ntdll_path = FindPathToBinary("ntdll.dll"); |
| 196 | EXPECT_NE((char*)0, internal_strstr(ntdll_path, "ntdll.dll")); |
| 197 | InternalFree(ntdll_path); |
| 198 | EXPECT_EQ(0, FindPathToBinary("unexisting_binary.ergjeorj")); |
| 199 | } |
Alexey Samsonov | 1dcd1d9 | 2013-09-03 13:20:48 +0000 | [diff] [blame] | 200 | #endif |
| 201 | |
Alexey Samsonov | 90b0f1e | 2013-10-04 08:55:03 +0000 | [diff] [blame] | 202 | TEST(SanitizerCommon, StripPathPrefix) { |
| 203 | EXPECT_EQ(0, StripPathPrefix(0, "prefix")); |
| 204 | EXPECT_STREQ("foo", StripPathPrefix("foo", 0)); |
| 205 | EXPECT_STREQ("dir/file.cc", |
| 206 | StripPathPrefix("/usr/lib/dir/file.cc", "/usr/lib/")); |
| 207 | EXPECT_STREQ("/file.cc", StripPathPrefix("/usr/myroot/file.cc", "/myroot")); |
| 208 | EXPECT_STREQ("file.h", StripPathPrefix("/usr/lib/./file.h", "/usr/lib/")); |
| 209 | } |
| 210 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 211 | TEST(SanitizerCommon, RemoveANSIEscapeSequencesFromString) { |
| 212 | RemoveANSIEscapeSequencesFromString(nullptr); |
| 213 | const char *buffs[22] = { |
| 214 | "Default", "Default", |
| 215 | "\033[95mLight magenta", "Light magenta", |
| 216 | "\033[30mBlack\033[32mGreen\033[90mGray", "BlackGreenGray", |
| 217 | "\033[106mLight cyan \033[107mWhite ", "Light cyan White ", |
| 218 | "\033[31mHello\033[0m World", "Hello World", |
| 219 | "\033[38;5;82mHello \033[38;5;198mWorld", "Hello World", |
| 220 | "123[653456789012", "123[653456789012", |
| 221 | "Normal \033[5mBlink \033[25mNormal", "Normal Blink Normal", |
| 222 | "\033[106m\033[107m", "", |
| 223 | "", "", |
| 224 | " ", " ", |
| 225 | }; |
| 226 | |
| 227 | for (size_t i = 0; i < ARRAY_SIZE(buffs); i+=2) { |
| 228 | char *buffer_copy = internal_strdup(buffs[i]); |
| 229 | RemoveANSIEscapeSequencesFromString(buffer_copy); |
| 230 | EXPECT_STREQ(buffer_copy, buffs[i+1]); |
| 231 | InternalFree(buffer_copy); |
| 232 | } |
| 233 | } |
| 234 | |
Alexey Samsonov | bb4697f | 2013-11-14 09:41:24 +0000 | [diff] [blame] | 235 | TEST(SanitizerCommon, InternalScopedString) { |
| 236 | InternalScopedString str(10); |
| 237 | EXPECT_EQ(0U, str.length()); |
| 238 | EXPECT_STREQ("", str.data()); |
| 239 | |
| 240 | str.append("foo"); |
| 241 | EXPECT_EQ(3U, str.length()); |
| 242 | EXPECT_STREQ("foo", str.data()); |
| 243 | |
| 244 | int x = 1234; |
| 245 | str.append("%d", x); |
| 246 | EXPECT_EQ(7U, str.length()); |
| 247 | EXPECT_STREQ("foo1234", str.data()); |
| 248 | |
| 249 | str.append("%d", x); |
| 250 | EXPECT_EQ(9U, str.length()); |
| 251 | EXPECT_STREQ("foo123412", str.data()); |
| 252 | |
| 253 | str.clear(); |
| 254 | EXPECT_EQ(0U, str.length()); |
| 255 | EXPECT_STREQ("", str.data()); |
| 256 | |
| 257 | str.append("0123456789"); |
| 258 | EXPECT_EQ(9U, str.length()); |
| 259 | EXPECT_STREQ("012345678", str.data()); |
| 260 | } |
| 261 | |
Alexey Samsonov | ba5e996 | 2013-01-30 07:45:58 +0000 | [diff] [blame] | 262 | } // namespace __sanitizer |