Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 1 | //===-- sanitizer_linux_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 | // Tests for sanitizer_linux.h |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Sergey Matveev | a615b26 | 2013-05-14 13:19:33 +0000 | [diff] [blame] | 14 | #include "sanitizer_common/sanitizer_platform.h" |
| 15 | #if SANITIZER_LINUX |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 16 | |
| 17 | #include "sanitizer_common/sanitizer_linux.h" |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 18 | |
| 19 | #include "sanitizer_common/sanitizer_common.h" |
Sergey Matveev | a615b26 | 2013-05-14 13:19:33 +0000 | [diff] [blame] | 20 | #include "gtest/gtest.h" |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 21 | |
| 22 | #include <pthread.h> |
| 23 | #include <sched.h> |
Alexey Samsonov | ff7c14f | 2013-04-23 12:49:12 +0000 | [diff] [blame] | 24 | #include <stdlib.h> |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 25 | |
Alexey Samsonov | 5c217b6 | 2013-02-28 11:33:51 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Alexander Potapenko | 32602c5 | 2013-03-14 12:06:54 +0000 | [diff] [blame] | 27 | #include <vector> |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 28 | |
| 29 | namespace __sanitizer { |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 30 | |
Alexander Potapenko | 32602c5 | 2013-03-14 12:06:54 +0000 | [diff] [blame] | 31 | struct TidReporterArgument { |
| 32 | TidReporterArgument() { |
| 33 | pthread_mutex_init(&terminate_thread_mutex, NULL); |
| 34 | pthread_mutex_init(&tid_reported_mutex, NULL); |
| 35 | pthread_cond_init(&terminate_thread_cond, NULL); |
| 36 | pthread_cond_init(&tid_reported_cond, NULL); |
| 37 | terminate_thread = false; |
| 38 | } |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 39 | |
Alexander Potapenko | 32602c5 | 2013-03-14 12:06:54 +0000 | [diff] [blame] | 40 | ~TidReporterArgument() { |
| 41 | pthread_mutex_destroy(&terminate_thread_mutex); |
| 42 | pthread_mutex_destroy(&tid_reported_mutex); |
| 43 | pthread_cond_destroy(&terminate_thread_cond); |
| 44 | pthread_cond_destroy(&tid_reported_cond); |
| 45 | } |
| 46 | |
| 47 | pid_t reported_tid; |
| 48 | // For signaling to spawned threads that they should terminate. |
| 49 | pthread_cond_t terminate_thread_cond; |
| 50 | pthread_mutex_t terminate_thread_mutex; |
| 51 | bool terminate_thread; |
| 52 | // For signaling to main thread that a child thread has reported its tid. |
| 53 | pthread_cond_t tid_reported_cond; |
| 54 | pthread_mutex_t tid_reported_mutex; |
Alexander Potapenko | 3d04854 | 2013-03-14 13:15:14 +0000 | [diff] [blame] | 55 | |
| 56 | private: |
| 57 | // Disallow evil constructors |
| 58 | TidReporterArgument(const TidReporterArgument &); |
| 59 | void operator=(const TidReporterArgument &); |
Alexander Potapenko | 32602c5 | 2013-03-14 12:06:54 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | class ThreadListerTest : public ::testing::Test { |
| 63 | protected: |
| 64 | virtual void SetUp() { |
| 65 | pthread_t pthread_id; |
| 66 | pid_t tid; |
| 67 | for (uptr i = 0; i < kThreadCount; i++) { |
| 68 | SpawnTidReporter(&pthread_id, &tid); |
| 69 | pthread_ids_.push_back(pthread_id); |
| 70 | tids_.push_back(tid); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | virtual void TearDown() { |
| 75 | pthread_mutex_lock(&thread_arg.terminate_thread_mutex); |
| 76 | thread_arg.terminate_thread = true; |
| 77 | pthread_cond_broadcast(&thread_arg.terminate_thread_cond); |
| 78 | pthread_mutex_unlock(&thread_arg.terminate_thread_mutex); |
Alexander Potapenko | 3d04854 | 2013-03-14 13:15:14 +0000 | [diff] [blame] | 79 | for (uptr i = 0; i < pthread_ids_.size(); i++) |
Alexander Potapenko | 32602c5 | 2013-03-14 12:06:54 +0000 | [diff] [blame] | 80 | pthread_join(pthread_ids_[i], NULL); |
| 81 | } |
| 82 | |
| 83 | void SpawnTidReporter(pthread_t *pthread_id, pid_t *tid); |
| 84 | |
| 85 | static const uptr kThreadCount = 20; |
| 86 | |
| 87 | std::vector<pthread_t> pthread_ids_; |
| 88 | std::vector<pid_t> tids_; |
| 89 | |
| 90 | TidReporterArgument thread_arg; |
| 91 | }; |
| 92 | |
Alexander Potapenko | 3d04854 | 2013-03-14 13:15:14 +0000 | [diff] [blame] | 93 | // Writes its TID once to reported_tid and waits until signaled to terminate. |
Alexander Potapenko | 32602c5 | 2013-03-14 12:06:54 +0000 | [diff] [blame] | 94 | void *TidReporterThread(void *argument) { |
| 95 | TidReporterArgument *arg = reinterpret_cast<TidReporterArgument *>(argument); |
| 96 | pthread_mutex_lock(&arg->tid_reported_mutex); |
| 97 | arg->reported_tid = GetTid(); |
| 98 | pthread_cond_broadcast(&arg->tid_reported_cond); |
| 99 | pthread_mutex_unlock(&arg->tid_reported_mutex); |
| 100 | |
| 101 | pthread_mutex_lock(&arg->terminate_thread_mutex); |
| 102 | while (!arg->terminate_thread) |
| 103 | pthread_cond_wait(&arg->terminate_thread_cond, |
| 104 | &arg->terminate_thread_mutex); |
| 105 | pthread_mutex_unlock(&arg->terminate_thread_mutex); |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 106 | return NULL; |
| 107 | } |
| 108 | |
Alexander Potapenko | 32602c5 | 2013-03-14 12:06:54 +0000 | [diff] [blame] | 109 | void ThreadListerTest::SpawnTidReporter(pthread_t *pthread_id, |
| 110 | pid_t *tid) { |
| 111 | pthread_mutex_lock(&thread_arg.tid_reported_mutex); |
| 112 | thread_arg.reported_tid = -1; |
| 113 | ASSERT_EQ(0, pthread_create(pthread_id, NULL, |
| 114 | TidReporterThread, |
| 115 | &thread_arg)); |
| 116 | while (thread_arg.reported_tid == -1) |
| 117 | pthread_cond_wait(&thread_arg.tid_reported_cond, |
| 118 | &thread_arg.tid_reported_mutex); |
| 119 | pthread_mutex_unlock(&thread_arg.tid_reported_mutex); |
| 120 | *tid = thread_arg.reported_tid; |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 121 | } |
Alexander Potapenko | 32602c5 | 2013-03-14 12:06:54 +0000 | [diff] [blame] | 122 | |
Alexey Samsonov | 827d4ef | 2013-03-14 15:15:35 +0000 | [diff] [blame] | 123 | static std::vector<pid_t> ReadTidsToVector(ThreadLister *thread_lister) { |
Alexander Potapenko | 32602c5 | 2013-03-14 12:06:54 +0000 | [diff] [blame] | 124 | std::vector<pid_t> listed_tids; |
| 125 | pid_t tid; |
Alexander Potapenko | 3d04854 | 2013-03-14 13:15:14 +0000 | [diff] [blame] | 126 | while ((tid = thread_lister->GetNextTID()) >= 0) |
Alexander Potapenko | 32602c5 | 2013-03-14 12:06:54 +0000 | [diff] [blame] | 127 | listed_tids.push_back(tid); |
Alexander Potapenko | 3d04854 | 2013-03-14 13:15:14 +0000 | [diff] [blame] | 128 | EXPECT_FALSE(thread_lister->error()); |
Alexander Potapenko | 32602c5 | 2013-03-14 12:06:54 +0000 | [diff] [blame] | 129 | return listed_tids; |
| 130 | } |
| 131 | |
| 132 | static bool Includes(std::vector<pid_t> first, std::vector<pid_t> second) { |
| 133 | std::sort(first.begin(), first.end()); |
| 134 | std::sort(second.begin(), second.end()); |
| 135 | return std::includes(first.begin(), first.end(), |
| 136 | second.begin(), second.end()); |
| 137 | } |
| 138 | |
| 139 | static bool HasElement(std::vector<pid_t> vector, pid_t element) { |
| 140 | return std::find(vector.begin(), vector.end(), element) != vector.end(); |
| 141 | } |
| 142 | |
| 143 | // ThreadLister's output should include the current thread's TID and the TID of |
| 144 | // every thread we spawned. |
| 145 | TEST_F(ThreadListerTest, ThreadListerSeesAllSpawnedThreads) { |
| 146 | pid_t self_tid = GetTid(); |
| 147 | ThreadLister thread_lister(getpid()); |
Alexander Potapenko | 3d04854 | 2013-03-14 13:15:14 +0000 | [diff] [blame] | 148 | std::vector<pid_t> listed_tids = ReadTidsToVector(&thread_lister); |
Alexander Potapenko | 32602c5 | 2013-03-14 12:06:54 +0000 | [diff] [blame] | 149 | ASSERT_TRUE(HasElement(listed_tids, self_tid)); |
| 150 | ASSERT_TRUE(Includes(listed_tids, tids_)); |
| 151 | } |
| 152 | |
| 153 | // Calling Reset() should not cause ThreadLister to forget any threads it's |
| 154 | // supposed to know about. |
| 155 | TEST_F(ThreadListerTest, ResetDoesNotForgetThreads) { |
| 156 | ThreadLister thread_lister(getpid()); |
| 157 | |
| 158 | // Run the loop body twice, because Reset() might behave differently if called |
| 159 | // on a freshly created object. |
| 160 | for (uptr i = 0; i < 2; i++) { |
| 161 | thread_lister.Reset(); |
Alexander Potapenko | 3d04854 | 2013-03-14 13:15:14 +0000 | [diff] [blame] | 162 | std::vector<pid_t> listed_tids = ReadTidsToVector(&thread_lister); |
Alexander Potapenko | 32602c5 | 2013-03-14 12:06:54 +0000 | [diff] [blame] | 163 | ASSERT_TRUE(Includes(listed_tids, tids_)); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // If new threads have spawned during ThreadLister object's lifetime, calling |
| 168 | // Reset() should cause ThreadLister to recognize their existence. |
| 169 | TEST_F(ThreadListerTest, ResetMakesNewThreadsKnown) { |
| 170 | ThreadLister thread_lister(getpid()); |
Alexander Potapenko | 3d04854 | 2013-03-14 13:15:14 +0000 | [diff] [blame] | 171 | std::vector<pid_t> threads_before_extra = ReadTidsToVector(&thread_lister); |
Alexander Potapenko | 32602c5 | 2013-03-14 12:06:54 +0000 | [diff] [blame] | 172 | |
| 173 | pthread_t extra_pthread_id; |
| 174 | pid_t extra_tid; |
| 175 | SpawnTidReporter(&extra_pthread_id, &extra_tid); |
| 176 | // Register the new thread so it gets terminated in TearDown(). |
| 177 | pthread_ids_.push_back(extra_pthread_id); |
| 178 | |
| 179 | // It would be very bizarre if the new TID had been listed before we even |
| 180 | // spawned that thread, but it would also cause a false success in this test, |
| 181 | // so better check for that. |
| 182 | ASSERT_FALSE(HasElement(threads_before_extra, extra_tid)); |
| 183 | |
| 184 | thread_lister.Reset(); |
| 185 | |
Alexander Potapenko | 3d04854 | 2013-03-14 13:15:14 +0000 | [diff] [blame] | 186 | std::vector<pid_t> threads_after_extra = ReadTidsToVector(&thread_lister); |
Alexander Potapenko | 32602c5 | 2013-03-14 12:06:54 +0000 | [diff] [blame] | 187 | ASSERT_TRUE(HasElement(threads_after_extra, extra_tid)); |
| 188 | } |
| 189 | |
Alexey Samsonov | ff7c14f | 2013-04-23 12:49:12 +0000 | [diff] [blame] | 190 | TEST(SanitizerCommon, SetEnvTest) { |
| 191 | const char kEnvName[] = "ENV_FOO"; |
| 192 | SetEnv(kEnvName, "value"); |
| 193 | EXPECT_STREQ("value", getenv(kEnvName)); |
| 194 | unsetenv(kEnvName); |
| 195 | EXPECT_EQ(0, getenv(kEnvName)); |
| 196 | } |
| 197 | |
Alexey Samsonov | 816bdb7 | 2013-05-30 09:16:04 +0000 | [diff] [blame] | 198 | #if defined(__x86_64__) || defined(__i386__) |
Sergey Matveev | 4c08644 | 2013-05-29 13:07:42 +0000 | [diff] [blame] | 199 | void *thread_self_offset_test_func(void *arg) { |
| 200 | bool result = |
| 201 | *(uptr *)((char *)ThreadSelf() + ThreadSelfOffset()) == ThreadSelf(); |
| 202 | return (void *)result; |
| 203 | } |
| 204 | |
| 205 | TEST(SanitizerLinux, ThreadSelfOffset) { |
| 206 | EXPECT_TRUE((bool)thread_self_offset_test_func(0)); |
| 207 | pthread_t tid; |
| 208 | void *result; |
| 209 | ASSERT_EQ(0, pthread_create(&tid, 0, thread_self_offset_test_func, 0)); |
| 210 | ASSERT_EQ(0, pthread_join(tid, &result)); |
| 211 | EXPECT_TRUE((bool)result); |
| 212 | } |
| 213 | |
| 214 | // libpthread puts the thread descriptor at the end of stack space. |
| 215 | void *thread_descriptor_size_test_func(void *arg) { |
| 216 | uptr descr_addr = ThreadSelf(); |
Sergey Matveev | 24323de | 2013-05-07 14:41:43 +0000 | [diff] [blame] | 217 | pthread_attr_t attr; |
| 218 | pthread_getattr_np(pthread_self(), &attr); |
| 219 | void *stackaddr; |
Sergey Matveev | 4c08644 | 2013-05-29 13:07:42 +0000 | [diff] [blame] | 220 | size_t stacksize; |
Sergey Matveev | 24323de | 2013-05-07 14:41:43 +0000 | [diff] [blame] | 221 | pthread_attr_getstack(&attr, &stackaddr, &stacksize); |
Sergey Matveev | 4c08644 | 2013-05-29 13:07:42 +0000 | [diff] [blame] | 222 | return (void *)((uptr)stackaddr + stacksize - descr_addr); |
Sergey Matveev | 24323de | 2013-05-07 14:41:43 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | TEST(SanitizerLinux, ThreadDescriptorSize) { |
| 226 | pthread_t tid; |
| 227 | void *result; |
Sergey Matveev | 4c08644 | 2013-05-29 13:07:42 +0000 | [diff] [blame] | 228 | ASSERT_EQ(0, pthread_create(&tid, 0, thread_descriptor_size_test_func, 0)); |
Sergey Matveev | 24323de | 2013-05-07 14:41:43 +0000 | [diff] [blame] | 229 | ASSERT_EQ(0, pthread_join(tid, &result)); |
| 230 | EXPECT_EQ((uptr)result, ThreadDescriptorSize()); |
| 231 | } |
| 232 | #endif |
| 233 | |
Sergey Matveev | 3de0086 | 2013-05-14 13:24:46 +0000 | [diff] [blame] | 234 | TEST(SanitizerCommon, LibraryNameIs) { |
| 235 | EXPECT_FALSE(LibraryNameIs("", "")); |
| 236 | |
| 237 | char full_name[256]; |
| 238 | const char *paths[] = { "", "/", "/path/to/" }; |
| 239 | const char *suffixes[] = { "", "-linux", ".1.2", "-linux.1.2" }; |
| 240 | const char *base_names[] = { "lib", "lib.0", "lib-i386" }; |
| 241 | const char *wrong_names[] = { "", "lib.9", "lib-x86_64" }; |
| 242 | for (uptr i = 0; i < ARRAY_SIZE(paths); i++) |
| 243 | for (uptr j = 0; j < ARRAY_SIZE(suffixes); j++) { |
| 244 | for (uptr k = 0; k < ARRAY_SIZE(base_names); k++) { |
| 245 | internal_snprintf(full_name, ARRAY_SIZE(full_name), "%s%s%s.so", |
| 246 | paths[i], base_names[k], suffixes[j]); |
| 247 | EXPECT_TRUE(LibraryNameIs(full_name, base_names[k])) |
| 248 | << "Full name " << full_name |
| 249 | << " doesn't match base name " << base_names[k]; |
| 250 | for (uptr m = 0; m < ARRAY_SIZE(wrong_names); m++) |
| 251 | EXPECT_FALSE(LibraryNameIs(full_name, wrong_names[m])) |
| 252 | << "Full name " << full_name |
| 253 | << " matches base name " << wrong_names[m]; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 258 | #if defined(__mips64) |
| 259 | // Effectively, this is a test for ThreadDescriptorSize() which is used to |
| 260 | // compute ThreadSelf(). |
| 261 | TEST(SanitizerLinux, ThreadSelfTest) { |
| 262 | ASSERT_EQ(pthread_self(), ThreadSelf()); |
| 263 | } |
| 264 | #endif |
| 265 | |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 266 | } // namespace __sanitizer |
| 267 | |
Kostya Serebryany | f931da8 | 2013-05-15 12:36:29 +0000 | [diff] [blame] | 268 | #endif // SANITIZER_LINUX |