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 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 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 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 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 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 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 162 TEST(SanitizerCommon, ThreadStackTlsWorker) { 163 InitTlsSize(); 164 pthread_t t; 165 PTHREAD_CREATE(&t, 0, WorkerThread, 0); 166 PTHREAD_JOIN(t, 0); 167 } 168 169 bool UptrLess(uptr a, uptr b) { 170 return a < b; 171 } 172 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 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 #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 } 200 #endif 201 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 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 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 262 } // namespace __sanitizer 263