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_common.h" 14 #include "sanitizer_common/sanitizer_libc.h" 15 #include "sanitizer_common/sanitizer_platform.h" 16 #include "gtest/gtest.h" 17 18 namespace __sanitizer { 19 20 static bool IsSorted(const uptr *array, uptr n) { 21 for (uptr i = 1; i < n; i++) { 22 if (array[i] < array[i - 1]) return false; 23 } 24 return true; 25 } 26 27 TEST(SanitizerCommon, SortTest) { 28 uptr array[100]; 29 uptr n = 100; 30 // Already sorted. 31 for (uptr i = 0; i < n; i++) { 32 array[i] = i; 33 } 34 SortArray(array, n); 35 EXPECT_TRUE(IsSorted(array, n)); 36 // Reverse order. 37 for (uptr i = 0; i < n; i++) { 38 array[i] = n - 1 - i; 39 } 40 SortArray(array, n); 41 EXPECT_TRUE(IsSorted(array, n)); 42 // Mixed order. 43 for (uptr i = 0; i < n; i++) { 44 array[i] = (i % 2 == 0) ? i : n - 1 - i; 45 } 46 SortArray(array, n); 47 EXPECT_TRUE(IsSorted(array, n)); 48 // All equal. 49 for (uptr i = 0; i < n; i++) { 50 array[i] = 42; 51 } 52 SortArray(array, n); 53 EXPECT_TRUE(IsSorted(array, n)); 54 // All but one sorted. 55 for (uptr i = 0; i < n - 1; i++) { 56 array[i] = i; 57 } 58 array[n - 1] = 42; 59 SortArray(array, n); 60 EXPECT_TRUE(IsSorted(array, n)); 61 // Minimal case - sort three elements. 62 array[0] = 1; 63 array[1] = 0; 64 SortArray(array, 2); 65 EXPECT_TRUE(IsSorted(array, 2)); 66 } 67 68 TEST(SanitizerCommon, MmapAlignedOrDie) { 69 uptr PageSize = GetPageSizeCached(); 70 for (uptr size = 1; size <= 32; size *= 2) { 71 for (uptr alignment = 1; alignment <= 32; alignment *= 2) { 72 for (int iter = 0; iter < 100; iter++) { 73 uptr res = (uptr)MmapAlignedOrDie( 74 size * PageSize, alignment * PageSize, "MmapAlignedOrDieTest"); 75 EXPECT_EQ(0U, res % (alignment * PageSize)); 76 internal_memset((void*)res, 1, size * PageSize); 77 UnmapOrDie((void*)res, size * PageSize); 78 } 79 } 80 } 81 } 82 83 #if SANITIZER_LINUX 84 TEST(SanitizerCommon, SanitizerSetThreadName) { 85 const char *names[] = { 86 "0123456789012", 87 "01234567890123", 88 "012345678901234", // Larger names will be truncated on linux. 89 }; 90 91 for (size_t i = 0; i < ARRAY_SIZE(names); i++) { 92 EXPECT_TRUE(SanitizerSetThreadName(names[i])); 93 char buff[100]; 94 EXPECT_TRUE(SanitizerGetThreadName(buff, sizeof(buff) - 1)); 95 EXPECT_EQ(0, internal_strcmp(buff, names[i])); 96 } 97 } 98 #endif 99 100 TEST(SanitizerCommon, InternalMmapVector) { 101 InternalMmapVector<uptr> vector(1); 102 for (uptr i = 0; i < 100; i++) { 103 EXPECT_EQ(i, vector.size()); 104 vector.push_back(i); 105 } 106 for (uptr i = 0; i < 100; i++) { 107 EXPECT_EQ(i, vector[i]); 108 } 109 for (int i = 99; i >= 0; i--) { 110 EXPECT_EQ((uptr)i, vector.back()); 111 vector.pop_back(); 112 EXPECT_EQ((uptr)i, vector.size()); 113 } 114 } 115 116 void TestThreadInfo(bool main) { 117 uptr stk_addr = 0; 118 uptr stk_size = 0; 119 uptr tls_addr = 0; 120 uptr tls_size = 0; 121 GetThreadStackAndTls(main, &stk_addr, &stk_size, &tls_addr, &tls_size); 122 123 int stack_var; 124 EXPECT_NE(stk_addr, (uptr)0); 125 EXPECT_NE(stk_size, (uptr)0); 126 EXPECT_GT((uptr)&stack_var, stk_addr); 127 EXPECT_LT((uptr)&stack_var, stk_addr + stk_size); 128 129 #if SANITIZER_LINUX && defined(__x86_64__) 130 static __thread int thread_var; 131 EXPECT_NE(tls_addr, (uptr)0); 132 EXPECT_NE(tls_size, (uptr)0); 133 EXPECT_GT((uptr)&thread_var, tls_addr); 134 EXPECT_LT((uptr)&thread_var, tls_addr + tls_size); 135 136 // Ensure that tls and stack do not intersect. 137 uptr tls_end = tls_addr + tls_size; 138 EXPECT_TRUE(tls_addr < stk_addr || tls_addr >= stk_addr + stk_size); 139 EXPECT_TRUE(tls_end < stk_addr || tls_end >= stk_addr + stk_size); 140 EXPECT_TRUE((tls_addr < stk_addr) == (tls_end < stk_addr)); 141 #endif 142 } 143 144 static void *WorkerThread(void *arg) { 145 TestThreadInfo(false); 146 return 0; 147 } 148 149 TEST(SanitizerCommon, ThreadStackTlsMain) { 150 InitTlsSize(); 151 TestThreadInfo(true); 152 } 153 154 TEST(SanitizerCommon, ThreadStackTlsWorker) { 155 InitTlsSize(); 156 pthread_t t; 157 pthread_create(&t, 0, WorkerThread, 0); 158 pthread_join(t, 0); 159 } 160 161 } // namespace __sanitizer 162