Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <gtest/gtest.h> |
| 18 | |
| 19 | #include <errno.h> |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 20 | #include <inttypes.h> |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 21 | #include <limits.h> |
Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 22 | #include <pthread.h> |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 23 | #include <signal.h> |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 24 | #include <sys/mman.h> |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 25 | #include <unistd.h> |
Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 26 | |
| 27 | TEST(pthread, pthread_key_create) { |
| 28 | pthread_key_t key; |
| 29 | ASSERT_EQ(0, pthread_key_create(&key, NULL)); |
| 30 | ASSERT_EQ(0, pthread_key_delete(key)); |
| 31 | // Can't delete a key that's already been deleted. |
| 32 | ASSERT_EQ(EINVAL, pthread_key_delete(key)); |
| 33 | } |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 34 | |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 35 | TEST(pthread, pthread_key_create_lots) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 36 | #if defined(__BIONIC__) // glibc uses keys internally that its sysconf value doesn't account for. |
Elliott Hughes | 1887621 | 2013-12-12 11:02:41 -0800 | [diff] [blame] | 37 | // POSIX says PTHREAD_KEYS_MAX should be at least 128. |
| 38 | ASSERT_GE(PTHREAD_KEYS_MAX, 128); |
Elliott Hughes | 718a5b5 | 2014-01-28 17:02:03 -0800 | [diff] [blame] | 39 | |
| 40 | int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX); |
| 41 | |
Elliott Hughes | 1887621 | 2013-12-12 11:02:41 -0800 | [diff] [blame] | 42 | // sysconf shouldn't return a smaller value. |
Elliott Hughes | 718a5b5 | 2014-01-28 17:02:03 -0800 | [diff] [blame] | 43 | ASSERT_GE(sysconf_max, PTHREAD_KEYS_MAX); |
Elliott Hughes | 1887621 | 2013-12-12 11:02:41 -0800 | [diff] [blame] | 44 | |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 45 | // We can allocate _SC_THREAD_KEYS_MAX keys. |
Elliott Hughes | 718a5b5 | 2014-01-28 17:02:03 -0800 | [diff] [blame] | 46 | sysconf_max -= 2; // (Except that gtest takes two for itself.) |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 47 | std::vector<pthread_key_t> keys; |
Elliott Hughes | 718a5b5 | 2014-01-28 17:02:03 -0800 | [diff] [blame] | 48 | for (int i = 0; i < sysconf_max; ++i) { |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 49 | pthread_key_t key; |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 50 | // If this fails, it's likely that GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT is wrong. |
Elliott Hughes | 718a5b5 | 2014-01-28 17:02:03 -0800 | [diff] [blame] | 51 | ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << sysconf_max; |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 52 | keys.push_back(key); |
| 53 | } |
| 54 | |
| 55 | // ...and that really is the maximum. |
| 56 | pthread_key_t key; |
| 57 | ASSERT_EQ(EAGAIN, pthread_key_create(&key, NULL)); |
| 58 | |
| 59 | // (Don't leak all those keys!) |
| 60 | for (size_t i = 0; i < keys.size(); ++i) { |
| 61 | ASSERT_EQ(0, pthread_key_delete(keys[i])); |
| 62 | } |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 63 | #else // __BIONIC__ |
| 64 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 65 | #endif // __BIONIC__ |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 68 | static void* IdFn(void* arg) { |
| 69 | return arg; |
| 70 | } |
| 71 | |
| 72 | static void* SleepFn(void* arg) { |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 73 | sleep(reinterpret_cast<uintptr_t>(arg)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 74 | return NULL; |
| 75 | } |
| 76 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 77 | static void* SpinFn(void* arg) { |
| 78 | volatile bool* b = reinterpret_cast<volatile bool*>(arg); |
| 79 | while (!*b) { |
| 80 | } |
| 81 | return NULL; |
| 82 | } |
| 83 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 84 | static void* JoinFn(void* arg) { |
| 85 | return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL)); |
| 86 | } |
| 87 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 88 | static void AssertDetached(pthread_t t, bool is_detached) { |
| 89 | pthread_attr_t attr; |
| 90 | ASSERT_EQ(0, pthread_getattr_np(t, &attr)); |
| 91 | int detach_state; |
| 92 | ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state)); |
| 93 | pthread_attr_destroy(&attr); |
| 94 | ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED)); |
| 95 | } |
| 96 | |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 97 | static void MakeDeadThread(pthread_t& t) { |
| 98 | ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL)); |
| 99 | void* result; |
| 100 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 101 | } |
| 102 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 103 | TEST(pthread, pthread_create) { |
| 104 | void* expected_result = reinterpret_cast<void*>(123); |
| 105 | // Can we create a thread? |
| 106 | pthread_t t; |
| 107 | ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result)); |
| 108 | // If we join, do we get the expected value back? |
| 109 | void* result; |
| 110 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 111 | ASSERT_EQ(expected_result, result); |
| 112 | } |
| 113 | |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 114 | TEST(pthread, pthread_create_EAGAIN) { |
| 115 | pthread_attr_t attributes; |
| 116 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 117 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1))); |
| 118 | |
| 119 | pthread_t t; |
| 120 | ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL)); |
| 121 | } |
| 122 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 123 | TEST(pthread, pthread_no_join_after_detach) { |
| 124 | pthread_t t1; |
| 125 | ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5))); |
| 126 | |
| 127 | // After a pthread_detach... |
| 128 | ASSERT_EQ(0, pthread_detach(t1)); |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 129 | AssertDetached(t1, true); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 130 | |
| 131 | // ...pthread_join should fail. |
| 132 | void* result; |
| 133 | ASSERT_EQ(EINVAL, pthread_join(t1, &result)); |
| 134 | } |
| 135 | |
| 136 | TEST(pthread, pthread_no_op_detach_after_join) { |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 137 | bool done = false; |
| 138 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 139 | pthread_t t1; |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 140 | ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 141 | |
| 142 | // If thread 2 is already waiting to join thread 1... |
| 143 | pthread_t t2; |
| 144 | ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1))); |
| 145 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 146 | sleep(1); // (Give t2 a chance to call pthread_join.) |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 147 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 148 | // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)... |
| 149 | ASSERT_EQ(0, pthread_detach(t1)); |
| 150 | AssertDetached(t1, false); |
| 151 | |
| 152 | done = true; |
| 153 | |
| 154 | // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes). |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 155 | void* join_result; |
| 156 | ASSERT_EQ(0, pthread_join(t2, &join_result)); |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 157 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 158 | } |
Elliott Hughes | 14f1959 | 2012-10-29 10:19:44 -0700 | [diff] [blame] | 159 | |
| 160 | TEST(pthread, pthread_join_self) { |
| 161 | void* result; |
| 162 | ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), &result)); |
| 163 | } |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 164 | |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 165 | struct TestBug37410 { |
| 166 | pthread_t main_thread; |
| 167 | pthread_mutex_t mutex; |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 168 | |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 169 | static void main() { |
| 170 | TestBug37410 data; |
| 171 | data.main_thread = pthread_self(); |
| 172 | ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL)); |
| 173 | ASSERT_EQ(0, pthread_mutex_lock(&data.mutex)); |
| 174 | |
| 175 | pthread_t t; |
| 176 | ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data))); |
| 177 | |
| 178 | // Wait for the thread to be running... |
| 179 | ASSERT_EQ(0, pthread_mutex_lock(&data.mutex)); |
| 180 | ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex)); |
| 181 | |
| 182 | // ...and exit. |
| 183 | pthread_exit(NULL); |
| 184 | } |
| 185 | |
| 186 | private: |
| 187 | static void* thread_fn(void* arg) { |
| 188 | TestBug37410* data = reinterpret_cast<TestBug37410*>(arg); |
| 189 | |
| 190 | // Let the main thread know we're running. |
| 191 | pthread_mutex_unlock(&data->mutex); |
| 192 | |
| 193 | // And wait for the main thread to exit. |
| 194 | pthread_join(data->main_thread, NULL); |
| 195 | |
| 196 | return NULL; |
| 197 | } |
| 198 | }; |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 199 | |
Elliott Hughes | 7fd803c | 2013-02-14 16:33:52 -0800 | [diff] [blame] | 200 | // Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to |
| 201 | // run this test (which exits normally) in its own process. |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 202 | TEST(pthread_DeathTest, pthread_bug_37410) { |
| 203 | // http://code.google.com/p/android/issues/detail?id=37410 |
| 204 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 205 | ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), ""); |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 206 | } |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 207 | |
| 208 | static void* SignalHandlerFn(void* arg) { |
| 209 | sigset_t wait_set; |
| 210 | sigfillset(&wait_set); |
| 211 | return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg))); |
| 212 | } |
| 213 | |
| 214 | TEST(pthread, pthread_sigmask) { |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 215 | // Check that SIGUSR1 isn't blocked. |
| 216 | sigset_t original_set; |
| 217 | sigemptyset(&original_set); |
| 218 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set)); |
| 219 | ASSERT_FALSE(sigismember(&original_set, SIGUSR1)); |
| 220 | |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 221 | // Block SIGUSR1. |
| 222 | sigset_t set; |
| 223 | sigemptyset(&set); |
| 224 | sigaddset(&set, SIGUSR1); |
| 225 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL)); |
| 226 | |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 227 | // Check that SIGUSR1 is blocked. |
| 228 | sigset_t final_set; |
| 229 | sigemptyset(&final_set); |
| 230 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set)); |
| 231 | ASSERT_TRUE(sigismember(&final_set, SIGUSR1)); |
| 232 | // ...and that sigprocmask agrees with pthread_sigmask. |
| 233 | sigemptyset(&final_set); |
| 234 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set)); |
| 235 | ASSERT_TRUE(sigismember(&final_set, SIGUSR1)); |
| 236 | |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 237 | // Spawn a thread that calls sigwait and tells us what it received. |
| 238 | pthread_t signal_thread; |
| 239 | int received_signal = -1; |
| 240 | ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal)); |
| 241 | |
| 242 | // Send that thread SIGUSR1. |
| 243 | pthread_kill(signal_thread, SIGUSR1); |
| 244 | |
| 245 | // See what it got. |
| 246 | void* join_result; |
| 247 | ASSERT_EQ(0, pthread_join(signal_thread, &join_result)); |
| 248 | ASSERT_EQ(SIGUSR1, received_signal); |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 249 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 250 | |
| 251 | // Restore the original signal mask. |
| 252 | ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL)); |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 253 | } |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 254 | |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 255 | #if defined(__BIONIC__) |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 256 | extern "C" pid_t __bionic_clone(int flags, void* child_stack, pid_t* parent_tid, void* tls, pid_t* child_tid, int (*fn)(void*), void* arg); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 257 | #endif // __BIONIC__ |
| 258 | |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 259 | TEST(pthread, __bionic_clone) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 260 | #if defined(__BIONIC__) |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 261 | // Check that our hand-written clone assembler sets errno correctly on failure. |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 262 | uintptr_t fake_child_stack[16]; |
| 263 | errno = 0; |
Chris Dearman | dd00364 | 2014-01-04 12:57:39 +0000 | [diff] [blame] | 264 | ASSERT_EQ(-1, __bionic_clone(CLONE_THREAD, &fake_child_stack[16], NULL, NULL, NULL, NULL, NULL)); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 265 | ASSERT_EQ(EINVAL, errno); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 266 | #else // __BIONIC__ |
| 267 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 268 | #endif // __BIONIC__ |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 269 | } |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 270 | |
| 271 | TEST(pthread, pthread_setname_np__too_long) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 272 | #if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise. |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 273 | ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux")); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 274 | #else // __BIONIC__ |
| 275 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 276 | #endif // __BIONIC__ |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | TEST(pthread, pthread_setname_np__self) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 280 | #if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise. |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 281 | ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1")); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 282 | #else // __BIONIC__ |
| 283 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 284 | #endif // __BIONIC__ |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | TEST(pthread, pthread_setname_np__other) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 288 | #if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise. |
Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 289 | // Emulator kernels don't currently support setting the name of other threads. |
| 290 | char* filename = NULL; |
| 291 | asprintf(&filename, "/proc/self/task/%d/comm", gettid()); |
| 292 | struct stat sb; |
| 293 | bool has_comm = (stat(filename, &sb) != -1); |
| 294 | free(filename); |
| 295 | |
| 296 | if (has_comm) { |
| 297 | pthread_t t1; |
| 298 | ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5))); |
| 299 | ASSERT_EQ(0, pthread_setname_np(t1, "short 2")); |
| 300 | } else { |
| 301 | fprintf(stderr, "skipping test: this kernel doesn't have /proc/self/task/tid/comm files!\n"); |
| 302 | } |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 303 | #else // __BIONIC__ |
| 304 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 305 | #endif // __BIONIC__ |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | TEST(pthread, pthread_setname_np__no_such_thread) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 309 | #if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise. |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 310 | pthread_t dead_thread; |
| 311 | MakeDeadThread(dead_thread); |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 312 | |
| 313 | // Call pthread_setname_np after thread has already exited. |
Elliott Hughes | a41ba2f | 2013-03-21 20:02:35 -0700 | [diff] [blame] | 314 | ASSERT_EQ(ESRCH, pthread_setname_np(dead_thread, "short 3")); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 315 | #else // __BIONIC__ |
| 316 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 317 | #endif // __BIONIC__ |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 318 | } |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 319 | |
| 320 | TEST(pthread, pthread_kill__0) { |
| 321 | // Signal 0 just tests that the thread exists, so it's safe to call on ourselves. |
| 322 | ASSERT_EQ(0, pthread_kill(pthread_self(), 0)); |
| 323 | } |
| 324 | |
| 325 | TEST(pthread, pthread_kill__invalid_signal) { |
| 326 | ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1)); |
| 327 | } |
| 328 | |
Elliott Hughes | fae89fc | 2013-02-21 11:22:23 -0800 | [diff] [blame] | 329 | static void pthread_kill__in_signal_handler_helper(int signal_number) { |
| 330 | static int count = 0; |
| 331 | ASSERT_EQ(SIGALRM, signal_number); |
| 332 | if (++count == 1) { |
| 333 | // Can we call pthread_kill from a signal handler? |
| 334 | ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM)); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | TEST(pthread, pthread_kill__in_signal_handler) { |
| 339 | struct sigaction action; |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 340 | struct sigaction original_action; |
Elliott Hughes | fae89fc | 2013-02-21 11:22:23 -0800 | [diff] [blame] | 341 | sigemptyset(&action.sa_mask); |
| 342 | action.sa_flags = 0; |
| 343 | action.sa_handler = pthread_kill__in_signal_handler_helper; |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 344 | ASSERT_EQ(0, sigaction(SIGALRM, &action, &original_action)); |
Elliott Hughes | fae89fc | 2013-02-21 11:22:23 -0800 | [diff] [blame] | 345 | ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM)); |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 346 | ASSERT_EQ(0, sigaction(SIGALRM, &original_action, NULL)); |
Elliott Hughes | fae89fc | 2013-02-21 11:22:23 -0800 | [diff] [blame] | 347 | } |
| 348 | |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 349 | TEST(pthread, pthread_detach__no_such_thread) { |
| 350 | pthread_t dead_thread; |
| 351 | MakeDeadThread(dead_thread); |
| 352 | |
| 353 | ASSERT_EQ(ESRCH, pthread_detach(dead_thread)); |
| 354 | } |
| 355 | |
Jeff Hao | 9b06cc3 | 2013-08-15 14:51:16 -0700 | [diff] [blame] | 356 | TEST(pthread, pthread_getcpuclockid__clock_gettime) { |
| 357 | pthread_t t; |
| 358 | ASSERT_EQ(0, pthread_create(&t, NULL, SleepFn, reinterpret_cast<void*>(5))); |
| 359 | |
| 360 | clockid_t c; |
| 361 | ASSERT_EQ(0, pthread_getcpuclockid(t, &c)); |
| 362 | timespec ts; |
| 363 | ASSERT_EQ(0, clock_gettime(c, &ts)); |
| 364 | } |
| 365 | |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 366 | TEST(pthread, pthread_getcpuclockid__no_such_thread) { |
| 367 | pthread_t dead_thread; |
| 368 | MakeDeadThread(dead_thread); |
| 369 | |
| 370 | clockid_t c; |
| 371 | ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c)); |
| 372 | } |
| 373 | |
| 374 | TEST(pthread, pthread_getschedparam__no_such_thread) { |
| 375 | pthread_t dead_thread; |
| 376 | MakeDeadThread(dead_thread); |
| 377 | |
| 378 | int policy; |
| 379 | sched_param param; |
| 380 | ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, ¶m)); |
| 381 | } |
| 382 | |
| 383 | TEST(pthread, pthread_setschedparam__no_such_thread) { |
| 384 | pthread_t dead_thread; |
| 385 | MakeDeadThread(dead_thread); |
| 386 | |
| 387 | int policy = 0; |
| 388 | sched_param param; |
| 389 | ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, ¶m)); |
| 390 | } |
| 391 | |
| 392 | TEST(pthread, pthread_join__no_such_thread) { |
| 393 | pthread_t dead_thread; |
| 394 | MakeDeadThread(dead_thread); |
| 395 | |
| 396 | void* result; |
| 397 | ASSERT_EQ(ESRCH, pthread_join(dead_thread, &result)); |
| 398 | } |
| 399 | |
| 400 | TEST(pthread, pthread_kill__no_such_thread) { |
| 401 | pthread_t dead_thread; |
| 402 | MakeDeadThread(dead_thread); |
| 403 | |
| 404 | ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0)); |
| 405 | } |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 406 | |
| 407 | TEST(pthread, pthread_join__multijoin) { |
| 408 | bool done = false; |
| 409 | |
| 410 | pthread_t t1; |
| 411 | ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done)); |
| 412 | |
| 413 | pthread_t t2; |
| 414 | ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1))); |
| 415 | |
| 416 | sleep(1); // (Give t2 a chance to call pthread_join.) |
| 417 | |
| 418 | // Multiple joins to the same thread should fail. |
| 419 | ASSERT_EQ(EINVAL, pthread_join(t1, NULL)); |
| 420 | |
| 421 | done = true; |
| 422 | |
| 423 | // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes). |
| 424 | void* join_result; |
| 425 | ASSERT_EQ(0, pthread_join(t2, &join_result)); |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 426 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 427 | } |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 428 | |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 429 | TEST(pthread, pthread_join__race) { |
| 430 | // http://b/11693195 --- pthread_join could return before the thread had actually exited. |
| 431 | // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread. |
| 432 | for (size_t i = 0; i < 1024; ++i) { |
| 433 | size_t stack_size = 64*1024; |
| 434 | void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0); |
| 435 | |
| 436 | pthread_attr_t a; |
| 437 | pthread_attr_init(&a); |
| 438 | pthread_attr_setstack(&a, stack, stack_size); |
| 439 | |
| 440 | pthread_t t; |
| 441 | ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL)); |
| 442 | ASSERT_EQ(0, pthread_join(t, NULL)); |
| 443 | ASSERT_EQ(0, munmap(stack, stack_size)); |
| 444 | } |
| 445 | } |
| 446 | |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 447 | static void* GetActualGuardSizeFn(void* arg) { |
| 448 | pthread_attr_t attributes; |
| 449 | pthread_getattr_np(pthread_self(), &attributes); |
| 450 | pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg)); |
| 451 | return NULL; |
| 452 | } |
| 453 | |
| 454 | static size_t GetActualGuardSize(const pthread_attr_t& attributes) { |
| 455 | size_t result; |
| 456 | pthread_t t; |
| 457 | pthread_create(&t, &attributes, GetActualGuardSizeFn, &result); |
| 458 | void* join_result; |
| 459 | pthread_join(t, &join_result); |
| 460 | return result; |
| 461 | } |
| 462 | |
| 463 | static void* GetActualStackSizeFn(void* arg) { |
| 464 | pthread_attr_t attributes; |
| 465 | pthread_getattr_np(pthread_self(), &attributes); |
| 466 | pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg)); |
| 467 | return NULL; |
| 468 | } |
| 469 | |
| 470 | static size_t GetActualStackSize(const pthread_attr_t& attributes) { |
| 471 | size_t result; |
| 472 | pthread_t t; |
| 473 | pthread_create(&t, &attributes, GetActualStackSizeFn, &result); |
| 474 | void* join_result; |
| 475 | pthread_join(t, &join_result); |
| 476 | return result; |
| 477 | } |
| 478 | |
| 479 | TEST(pthread, pthread_attr_setguardsize) { |
| 480 | pthread_attr_t attributes; |
| 481 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 482 | |
| 483 | // Get the default guard size. |
| 484 | size_t default_guard_size; |
| 485 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size)); |
| 486 | |
| 487 | // No such thing as too small: will be rounded up to one page by pthread_create. |
| 488 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128)); |
| 489 | size_t guard_size; |
| 490 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 491 | ASSERT_EQ(128U, guard_size); |
| 492 | ASSERT_EQ(4096U, GetActualGuardSize(attributes)); |
| 493 | |
| 494 | // Large enough and a multiple of the page size. |
| 495 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024)); |
| 496 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 497 | ASSERT_EQ(32*1024U, guard_size); |
| 498 | |
| 499 | // Large enough but not a multiple of the page size; will be rounded up by pthread_create. |
| 500 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1)); |
| 501 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 502 | ASSERT_EQ(32*1024U + 1, guard_size); |
| 503 | } |
| 504 | |
| 505 | TEST(pthread, pthread_attr_setstacksize) { |
| 506 | pthread_attr_t attributes; |
| 507 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 508 | |
| 509 | // Get the default stack size. |
| 510 | size_t default_stack_size; |
| 511 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size)); |
| 512 | |
| 513 | // Too small. |
| 514 | ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128)); |
| 515 | size_t stack_size; |
| 516 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 517 | ASSERT_EQ(default_stack_size, stack_size); |
| 518 | ASSERT_GE(GetActualStackSize(attributes), default_stack_size); |
| 519 | |
| 520 | // Large enough and a multiple of the page size. |
| 521 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024)); |
| 522 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 523 | ASSERT_EQ(32*1024U, stack_size); |
| 524 | ASSERT_EQ(GetActualStackSize(attributes), 32*1024U); |
| 525 | |
| 526 | // Large enough but not a multiple of the page size; will be rounded up by pthread_create. |
| 527 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1)); |
| 528 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 529 | ASSERT_EQ(32*1024U + 1, stack_size); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 530 | #if defined(__BIONIC__) |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 531 | // Bionic rounds up, which is what POSIX allows. |
| 532 | ASSERT_EQ(GetActualStackSize(attributes), (32 + 4)*1024U); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 533 | #else // __BIONIC__ |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 534 | // glibc rounds down, in violation of POSIX. They document this in their BUGS section. |
| 535 | ASSERT_EQ(GetActualStackSize(attributes), 32*1024U); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 536 | #endif // __BIONIC__ |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 537 | } |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 538 | |
| 539 | TEST(pthread, pthread_rwlock_smoke) { |
| 540 | pthread_rwlock_t l; |
| 541 | ASSERT_EQ(0, pthread_rwlock_init(&l, NULL)); |
| 542 | |
| 543 | ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); |
| 544 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 545 | |
| 546 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 547 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 548 | |
| 549 | ASSERT_EQ(0, pthread_rwlock_destroy(&l)); |
| 550 | } |
| 551 | |
| 552 | static int gOnceFnCallCount = 0; |
| 553 | static void OnceFn() { |
| 554 | ++gOnceFnCallCount; |
| 555 | } |
| 556 | |
| 557 | TEST(pthread, pthread_once_smoke) { |
| 558 | pthread_once_t once_control = PTHREAD_ONCE_INIT; |
| 559 | ASSERT_EQ(0, pthread_once(&once_control, OnceFn)); |
| 560 | ASSERT_EQ(0, pthread_once(&once_control, OnceFn)); |
| 561 | ASSERT_EQ(1, gOnceFnCallCount); |
| 562 | } |
| 563 | |
| 564 | static int gAtForkPrepareCalls = 0; |
| 565 | static void AtForkPrepare1() { gAtForkPrepareCalls = (gAtForkPrepareCalls << 4) | 1; } |
| 566 | static void AtForkPrepare2() { gAtForkPrepareCalls = (gAtForkPrepareCalls << 4) | 2; } |
| 567 | static int gAtForkParentCalls = 0; |
| 568 | static void AtForkParent1() { gAtForkParentCalls = (gAtForkParentCalls << 4) | 1; } |
| 569 | static void AtForkParent2() { gAtForkParentCalls = (gAtForkParentCalls << 4) | 2; } |
| 570 | static int gAtForkChildCalls = 0; |
| 571 | static void AtForkChild1() { gAtForkChildCalls = (gAtForkChildCalls << 4) | 1; } |
| 572 | static void AtForkChild2() { gAtForkChildCalls = (gAtForkChildCalls << 4) | 2; } |
| 573 | |
| 574 | TEST(pthread, pthread_atfork) { |
| 575 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1)); |
| 576 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2)); |
| 577 | |
| 578 | int pid = fork(); |
| 579 | ASSERT_NE(-1, pid) << strerror(errno); |
| 580 | |
| 581 | // Child and parent calls are made in the order they were registered. |
| 582 | if (pid == 0) { |
| 583 | ASSERT_EQ(0x12, gAtForkChildCalls); |
| 584 | _exit(0); |
| 585 | } |
| 586 | ASSERT_EQ(0x12, gAtForkParentCalls); |
| 587 | |
| 588 | // Prepare calls are made in the reverse order. |
| 589 | ASSERT_EQ(0x21, gAtForkPrepareCalls); |
| 590 | } |
| 591 | |
| 592 | TEST(pthread, pthread_attr_getscope) { |
| 593 | pthread_attr_t attr; |
| 594 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 595 | |
| 596 | int scope; |
| 597 | ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope)); |
| 598 | ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope); |
| 599 | } |