blob: e68f1ff03aceb91a1a0817b7bfaff4b34286909e [file] [log] [blame]
Elliott Hughesbfeab1b2012-09-05 17:47:37 -07001/*
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 Hughes5b9310e2013-10-02 16:59:05 -070020#include <inttypes.h>
Elliott Hughesb95cf0d2013-07-15 14:51:07 -070021#include <limits.h>
Elliott Hughes04620a32014-03-07 17:59:05 -080022#include <malloc.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070023#include <pthread.h>
Christopher Ferrisf04935c2013-12-20 18:43:21 -080024#include <signal.h>
Yabin Cui140f3672015-02-03 10:32:00 -080025#include <stdio.h>
Elliott Hughes70b24b12013-11-15 11:51:07 -080026#include <sys/mman.h>
Elliott Hughes4d098ca2016-04-11 12:43:05 -070027#include <sys/prctl.h>
Elliott Hughes57b7a612014-08-25 17:26:50 -070028#include <sys/syscall.h>
Narayan Kamath51e6cb32014-03-03 15:38:51 +000029#include <time.h>
Elliott Hughes4d014e12012-09-07 16:47:54 -070030#include <unistd.h>
Yabin Cui33ac04a2015-09-22 11:16:15 -070031#include <unwind.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070032
Yabin Cui08ee8d22015-02-11 17:04:36 -080033#include <atomic>
Josh Gaoddf757e2018-10-17 15:23:03 -070034#include <future>
Yabin Cuib5845722015-03-16 22:46:42 -070035#include <vector>
Yabin Cui08ee8d22015-02-11 17:04:36 -080036
Yabin Cui6b9c85b2018-01-23 12:56:18 -080037#include <android-base/parseint.h>
Tom Cherryb8ab6182017-04-05 16:20:29 -070038#include <android-base/scopeguard.h>
Yabin Cui6b9c85b2018-01-23 12:56:18 -080039#include <android-base/strings.h>
Tom Cherryb8ab6182017-04-05 16:20:29 -070040
Yabin Cuic9a659c2015-11-05 15:36:08 -080041#include "private/bionic_constants.h"
Yabin Cui17393b02015-03-21 15:08:25 -070042#include "private/bionic_macros.h"
Yabin Cui17393b02015-03-21 15:08:25 -070043#include "BionicDeathTest.h"
Elliott Hughes71ba5892018-02-07 12:44:45 -080044#include "SignalUtils.h"
Elliott Hughes15dfd632015-09-22 16:40:14 -070045#include "utils.h"
46
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070047TEST(pthread, pthread_key_create) {
48 pthread_key_t key;
Yi Kong32bc0fc2018-08-02 17:31:13 -070049 ASSERT_EQ(0, pthread_key_create(&key, nullptr));
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070050 ASSERT_EQ(0, pthread_key_delete(key));
51 // Can't delete a key that's already been deleted.
52 ASSERT_EQ(EINVAL, pthread_key_delete(key));
53}
Elliott Hughes4d014e12012-09-07 16:47:54 -070054
Dan Albertc4bcc752014-09-30 11:48:24 -070055TEST(pthread, pthread_keys_max) {
Yabin Cui6c238f22014-12-11 20:50:41 -080056 // POSIX says PTHREAD_KEYS_MAX should be at least _POSIX_THREAD_KEYS_MAX.
57 ASSERT_GE(PTHREAD_KEYS_MAX, _POSIX_THREAD_KEYS_MAX);
Dan Albertc4bcc752014-09-30 11:48:24 -070058}
Elliott Hughes718a5b52014-01-28 17:02:03 -080059
Yabin Cui6c238f22014-12-11 20:50:41 -080060TEST(pthread, sysconf_SC_THREAD_KEYS_MAX_eq_PTHREAD_KEYS_MAX) {
Dan Albertc4bcc752014-09-30 11:48:24 -070061 int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX);
Yabin Cui6c238f22014-12-11 20:50:41 -080062 ASSERT_EQ(sysconf_max, PTHREAD_KEYS_MAX);
Dan Albertc4bcc752014-09-30 11:48:24 -070063}
64
65TEST(pthread, pthread_key_many_distinct) {
Yabin Cui6c238f22014-12-11 20:50:41 -080066 // As gtest uses pthread keys, we can't allocate exactly PTHREAD_KEYS_MAX
67 // pthread keys, but We should be able to allocate at least this many keys.
68 int nkeys = PTHREAD_KEYS_MAX / 2;
Dan Albertc4bcc752014-09-30 11:48:24 -070069 std::vector<pthread_key_t> keys;
70
Tom Cherryb8ab6182017-04-05 16:20:29 -070071 auto scope_guard = android::base::make_scope_guard([&keys] {
Elliott Hughes0b2acdf2015-10-02 18:25:19 -070072 for (const auto& key : keys) {
Dan Albertc4bcc752014-09-30 11:48:24 -070073 EXPECT_EQ(0, pthread_key_delete(key));
74 }
75 });
76
77 for (int i = 0; i < nkeys; ++i) {
78 pthread_key_t key;
Elliott Hughes61706932015-03-31 10:56:58 -070079 // If this fails, it's likely that LIBC_PTHREAD_KEY_RESERVED_COUNT is wrong.
Yi Kong32bc0fc2018-08-02 17:31:13 -070080 ASSERT_EQ(0, pthread_key_create(&key, nullptr)) << i << " of " << nkeys;
Dan Albertc4bcc752014-09-30 11:48:24 -070081 keys.push_back(key);
82 ASSERT_EQ(0, pthread_setspecific(key, reinterpret_cast<void*>(i)));
83 }
84
85 for (int i = keys.size() - 1; i >= 0; --i) {
86 ASSERT_EQ(reinterpret_cast<void*>(i), pthread_getspecific(keys.back()));
87 pthread_key_t key = keys.back();
88 keys.pop_back();
89 ASSERT_EQ(0, pthread_key_delete(key));
90 }
91}
92
Yabin Cui6c238f22014-12-11 20:50:41 -080093TEST(pthread, pthread_key_not_exceed_PTHREAD_KEYS_MAX) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +000094 std::vector<pthread_key_t> keys;
Dan Albertc4bcc752014-09-30 11:48:24 -070095 int rv = 0;
Yabin Cui6c238f22014-12-11 20:50:41 -080096
97 // Pthread keys are used by gtest, so PTHREAD_KEYS_MAX should
98 // be more than we are allowed to allocate now.
99 for (int i = 0; i < PTHREAD_KEYS_MAX; i++) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000100 pthread_key_t key;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700101 rv = pthread_key_create(&key, nullptr);
Dan Albertc4bcc752014-09-30 11:48:24 -0700102 if (rv == EAGAIN) {
103 break;
104 }
105 EXPECT_EQ(0, rv);
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000106 keys.push_back(key);
107 }
108
Dan Albertc4bcc752014-09-30 11:48:24 -0700109 // Don't leak keys.
Elliott Hughes0b2acdf2015-10-02 18:25:19 -0700110 for (const auto& key : keys) {
Dan Albertc4bcc752014-09-30 11:48:24 -0700111 EXPECT_EQ(0, pthread_key_delete(key));
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000112 }
Dan Albertc4bcc752014-09-30 11:48:24 -0700113 keys.clear();
114
115 // We should have eventually reached the maximum number of keys and received
116 // EAGAIN.
117 ASSERT_EQ(EAGAIN, rv);
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000118}
119
Elliott Hughesebb770f2014-06-25 13:46:46 -0700120TEST(pthread, pthread_key_delete) {
121 void* expected = reinterpret_cast<void*>(1234);
122 pthread_key_t key;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700123 ASSERT_EQ(0, pthread_key_create(&key, nullptr));
Elliott Hughesebb770f2014-06-25 13:46:46 -0700124 ASSERT_EQ(0, pthread_setspecific(key, expected));
125 ASSERT_EQ(expected, pthread_getspecific(key));
126 ASSERT_EQ(0, pthread_key_delete(key));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700127 // After deletion, pthread_getspecific returns nullptr.
128 ASSERT_EQ(nullptr, pthread_getspecific(key));
Elliott Hughesebb770f2014-06-25 13:46:46 -0700129 // And you can't use pthread_setspecific with the deleted key.
130 ASSERT_EQ(EINVAL, pthread_setspecific(key, expected));
131}
132
Elliott Hughes40a52172014-07-30 14:48:10 -0700133TEST(pthread, pthread_key_fork) {
134 void* expected = reinterpret_cast<void*>(1234);
135 pthread_key_t key;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700136 ASSERT_EQ(0, pthread_key_create(&key, nullptr));
Elliott Hughes40a52172014-07-30 14:48:10 -0700137 ASSERT_EQ(0, pthread_setspecific(key, expected));
138 ASSERT_EQ(expected, pthread_getspecific(key));
139
140 pid_t pid = fork();
141 ASSERT_NE(-1, pid) << strerror(errno);
142
143 if (pid == 0) {
144 // The surviving thread inherits all the forking thread's TLS values...
145 ASSERT_EQ(expected, pthread_getspecific(key));
146 _exit(99);
147 }
148
Elliott Hughes33697a02016-01-26 13:04:57 -0800149 AssertChildExited(pid, 99);
Elliott Hughes40a52172014-07-30 14:48:10 -0700150
151 ASSERT_EQ(expected, pthread_getspecific(key));
Dan Albert1d53ae22014-09-02 15:24:26 -0700152 ASSERT_EQ(0, pthread_key_delete(key));
Elliott Hughes40a52172014-07-30 14:48:10 -0700153}
154
155static void* DirtyKeyFn(void* key) {
156 return pthread_getspecific(*reinterpret_cast<pthread_key_t*>(key));
157}
158
159TEST(pthread, pthread_key_dirty) {
160 pthread_key_t key;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700161 ASSERT_EQ(0, pthread_key_create(&key, nullptr));
Elliott Hughes40a52172014-07-30 14:48:10 -0700162
Yabin Cuia36158a2015-11-16 21:06:16 -0800163 size_t stack_size = 640 * 1024;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700164 void* stack = mmap(nullptr, stack_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
Elliott Hughes40a52172014-07-30 14:48:10 -0700165 ASSERT_NE(MAP_FAILED, stack);
166 memset(stack, 0xff, stack_size);
167
168 pthread_attr_t attr;
169 ASSERT_EQ(0, pthread_attr_init(&attr));
170 ASSERT_EQ(0, pthread_attr_setstack(&attr, stack, stack_size));
171
172 pthread_t t;
173 ASSERT_EQ(0, pthread_create(&t, &attr, DirtyKeyFn, &key));
174
175 void* result;
176 ASSERT_EQ(0, pthread_join(t, &result));
177 ASSERT_EQ(nullptr, result); // Not ~0!
178
179 ASSERT_EQ(0, munmap(stack, stack_size));
Dan Albert1d53ae22014-09-02 15:24:26 -0700180 ASSERT_EQ(0, pthread_key_delete(key));
Elliott Hughes40a52172014-07-30 14:48:10 -0700181}
182
Yabin Cui5ddbb3f2015-03-05 20:35:32 -0800183TEST(pthread, static_pthread_key_used_before_creation) {
184#if defined(__BIONIC__)
185 // See http://b/19625804. The bug is about a static/global pthread key being used before creation.
186 // So here tests if the static/global default value 0 can be detected as invalid key.
187 static pthread_key_t key;
188 ASSERT_EQ(nullptr, pthread_getspecific(key));
189 ASSERT_EQ(EINVAL, pthread_setspecific(key, nullptr));
190 ASSERT_EQ(EINVAL, pthread_key_delete(key));
191#else
192 GTEST_LOG_(INFO) << "This test tests bionic pthread key implementation detail.\n";
193#endif
194}
195
Elliott Hughes4d014e12012-09-07 16:47:54 -0700196static void* IdFn(void* arg) {
197 return arg;
198}
199
Yabin Cui63481602014-12-01 17:41:04 -0800200class SpinFunctionHelper {
201 public:
202 SpinFunctionHelper() {
203 SpinFunctionHelper::spin_flag_ = true;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400204 }
Elliott Hughes0bd9d132017-11-02 13:11:13 -0700205
Yabin Cui63481602014-12-01 17:41:04 -0800206 ~SpinFunctionHelper() {
207 UnSpin();
208 }
Elliott Hughes0bd9d132017-11-02 13:11:13 -0700209
Yabin Cui63481602014-12-01 17:41:04 -0800210 auto GetFunction() -> void* (*)(void*) {
211 return SpinFunctionHelper::SpinFn;
212 }
213
214 void UnSpin() {
215 SpinFunctionHelper::spin_flag_ = false;
216 }
217
218 private:
219 static void* SpinFn(void*) {
220 while (spin_flag_) {}
Yi Kong32bc0fc2018-08-02 17:31:13 -0700221 return nullptr;
Yabin Cui63481602014-12-01 17:41:04 -0800222 }
Yabin Cuia36158a2015-11-16 21:06:16 -0800223 static std::atomic<bool> spin_flag_;
Yabin Cui63481602014-12-01 17:41:04 -0800224};
225
226// It doesn't matter if spin_flag_ is used in several tests,
227// because it is always set to false after each test. Each thread
228// loops on spin_flag_ can find it becomes false at some time.
Yabin Cuia36158a2015-11-16 21:06:16 -0800229std::atomic<bool> SpinFunctionHelper::spin_flag_;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400230
Elliott Hughes4d014e12012-09-07 16:47:54 -0700231static void* JoinFn(void* arg) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700232 return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), nullptr));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700233}
234
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400235static void AssertDetached(pthread_t t, bool is_detached) {
236 pthread_attr_t attr;
237 ASSERT_EQ(0, pthread_getattr_np(t, &attr));
238 int detach_state;
239 ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
240 pthread_attr_destroy(&attr);
241 ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
242}
243
Elliott Hughes7484c212017-02-02 02:41:38 +0000244static void MakeDeadThread(pthread_t& t) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700245 ASSERT_EQ(0, pthread_create(&t, nullptr, IdFn, nullptr));
246 ASSERT_EQ(0, pthread_join(t, nullptr));
Elliott Hughes7484c212017-02-02 02:41:38 +0000247}
248
Elliott Hughes4d014e12012-09-07 16:47:54 -0700249TEST(pthread, pthread_create) {
250 void* expected_result = reinterpret_cast<void*>(123);
251 // Can we create a thread?
252 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700253 ASSERT_EQ(0, pthread_create(&t, nullptr, IdFn, expected_result));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700254 // If we join, do we get the expected value back?
255 void* result;
256 ASSERT_EQ(0, pthread_join(t, &result));
257 ASSERT_EQ(expected_result, result);
258}
259
Elliott Hughes3e898472013-02-12 16:40:24 +0000260TEST(pthread, pthread_create_EAGAIN) {
261 pthread_attr_t attributes;
262 ASSERT_EQ(0, pthread_attr_init(&attributes));
263 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
264
265 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700266 ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, nullptr));
Elliott Hughes3e898472013-02-12 16:40:24 +0000267}
268
Elliott Hughes4d014e12012-09-07 16:47:54 -0700269TEST(pthread, pthread_no_join_after_detach) {
Elliott Hughes725b2a92016-03-23 11:20:47 -0700270 SpinFunctionHelper spin_helper;
Yabin Cui63481602014-12-01 17:41:04 -0800271
Elliott Hughes4d014e12012-09-07 16:47:54 -0700272 pthread_t t1;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700273 ASSERT_EQ(0, pthread_create(&t1, nullptr, spin_helper.GetFunction(), nullptr));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700274
275 // After a pthread_detach...
276 ASSERT_EQ(0, pthread_detach(t1));
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400277 AssertDetached(t1, true);
Elliott Hughes4d014e12012-09-07 16:47:54 -0700278
279 // ...pthread_join should fail.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700280 ASSERT_EQ(EINVAL, pthread_join(t1, nullptr));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700281}
282
283TEST(pthread, pthread_no_op_detach_after_join) {
Elliott Hughes725b2a92016-03-23 11:20:47 -0700284 SpinFunctionHelper spin_helper;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400285
Elliott Hughes4d014e12012-09-07 16:47:54 -0700286 pthread_t t1;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700287 ASSERT_EQ(0, pthread_create(&t1, nullptr, spin_helper.GetFunction(), nullptr));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700288
289 // If thread 2 is already waiting to join thread 1...
290 pthread_t t2;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700291 ASSERT_EQ(0, pthread_create(&t2, nullptr, JoinFn, reinterpret_cast<void*>(t1)));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700292
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400293 sleep(1); // (Give t2 a chance to call pthread_join.)
Elliott Hughes4d014e12012-09-07 16:47:54 -0700294
Yabin Cuibbb04322015-03-19 15:19:25 -0700295#if defined(__BIONIC__)
296 ASSERT_EQ(EINVAL, pthread_detach(t1));
297#else
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400298 ASSERT_EQ(0, pthread_detach(t1));
Yabin Cuibbb04322015-03-19 15:19:25 -0700299#endif
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400300 AssertDetached(t1, false);
301
Elliott Hughes725b2a92016-03-23 11:20:47 -0700302 spin_helper.UnSpin();
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400303
304 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
Elliott Hughes4d014e12012-09-07 16:47:54 -0700305 void* join_result;
306 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700307 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700308}
Elliott Hughes14f19592012-10-29 10:19:44 -0700309
310TEST(pthread, pthread_join_self) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700311 ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), nullptr));
Elliott Hughes14f19592012-10-29 10:19:44 -0700312}
Elliott Hughes4f251be2012-11-01 16:33:29 -0700313
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800314struct TestBug37410 {
315 pthread_t main_thread;
316 pthread_mutex_t mutex;
Elliott Hughes4f251be2012-11-01 16:33:29 -0700317
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800318 static void main() {
319 TestBug37410 data;
320 data.main_thread = pthread_self();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700321 ASSERT_EQ(0, pthread_mutex_init(&data.mutex, nullptr));
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800322 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
323
324 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700325 ASSERT_EQ(0, pthread_create(&t, nullptr, TestBug37410::thread_fn, reinterpret_cast<void*>(&data)));
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800326
327 // Wait for the thread to be running...
328 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
329 ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex));
330
331 // ...and exit.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700332 pthread_exit(nullptr);
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800333 }
334
335 private:
336 static void* thread_fn(void* arg) {
337 TestBug37410* data = reinterpret_cast<TestBug37410*>(arg);
338
339 // Let the main thread know we're running.
340 pthread_mutex_unlock(&data->mutex);
341
342 // And wait for the main thread to exit.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700343 pthread_join(data->main_thread, nullptr);
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800344
Yi Kong32bc0fc2018-08-02 17:31:13 -0700345 return nullptr;
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800346 }
347};
Elliott Hughes4f251be2012-11-01 16:33:29 -0700348
Elliott Hughes7fd803c2013-02-14 16:33:52 -0800349// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
350// run this test (which exits normally) in its own process.
Yabin Cui9df70402014-11-05 18:01:01 -0800351
352class pthread_DeathTest : public BionicDeathTest {};
353
354TEST_F(pthread_DeathTest, pthread_bug_37410) {
Elliott Hughes4f251be2012-11-01 16:33:29 -0700355 // http://code.google.com/p/android/issues/detail?id=37410
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800356 ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), "");
Elliott Hughes4f251be2012-11-01 16:33:29 -0700357}
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800358
359static void* SignalHandlerFn(void* arg) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800360 sigset64_t wait_set;
361 sigfillset64(&wait_set);
362 return reinterpret_cast<void*>(sigwait64(&wait_set, reinterpret_cast<int*>(arg)));
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800363}
364
365TEST(pthread, pthread_sigmask) {
Elliott Hughes19e62322013-10-15 11:23:57 -0700366 // Check that SIGUSR1 isn't blocked.
367 sigset_t original_set;
368 sigemptyset(&original_set);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700369 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, nullptr, &original_set));
Elliott Hughes19e62322013-10-15 11:23:57 -0700370 ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
371
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800372 // Block SIGUSR1.
373 sigset_t set;
374 sigemptyset(&set);
375 sigaddset(&set, SIGUSR1);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700376 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, nullptr));
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800377
Elliott Hughes19e62322013-10-15 11:23:57 -0700378 // Check that SIGUSR1 is blocked.
379 sigset_t final_set;
380 sigemptyset(&final_set);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700381 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, nullptr, &final_set));
Elliott Hughes19e62322013-10-15 11:23:57 -0700382 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
383 // ...and that sigprocmask agrees with pthread_sigmask.
384 sigemptyset(&final_set);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700385 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &final_set));
Elliott Hughes19e62322013-10-15 11:23:57 -0700386 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
387
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800388 // Spawn a thread that calls sigwait and tells us what it received.
389 pthread_t signal_thread;
390 int received_signal = -1;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700391 ASSERT_EQ(0, pthread_create(&signal_thread, nullptr, SignalHandlerFn, &received_signal));
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800392
393 // Send that thread SIGUSR1.
394 pthread_kill(signal_thread, SIGUSR1);
395
396 // See what it got.
397 void* join_result;
398 ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
399 ASSERT_EQ(SIGUSR1, received_signal);
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700400 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes19e62322013-10-15 11:23:57 -0700401
402 // Restore the original signal mask.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700403 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, nullptr));
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800404}
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800405
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800406TEST(pthread, pthread_sigmask64_SIGTRMIN) {
407 // Check that SIGRTMIN isn't blocked.
408 sigset64_t original_set;
409 sigemptyset64(&original_set);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700410 ASSERT_EQ(0, pthread_sigmask64(SIG_BLOCK, nullptr, &original_set));
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800411 ASSERT_FALSE(sigismember64(&original_set, SIGRTMIN));
412
413 // Block SIGRTMIN.
414 sigset64_t set;
415 sigemptyset64(&set);
416 sigaddset64(&set, SIGRTMIN);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700417 ASSERT_EQ(0, pthread_sigmask64(SIG_BLOCK, &set, nullptr));
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800418
419 // Check that SIGRTMIN is blocked.
420 sigset64_t final_set;
421 sigemptyset64(&final_set);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700422 ASSERT_EQ(0, pthread_sigmask64(SIG_BLOCK, nullptr, &final_set));
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800423 ASSERT_TRUE(sigismember64(&final_set, SIGRTMIN));
424 // ...and that sigprocmask64 agrees with pthread_sigmask64.
425 sigemptyset64(&final_set);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700426 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, nullptr, &final_set));
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800427 ASSERT_TRUE(sigismember64(&final_set, SIGRTMIN));
428
429 // Spawn a thread that calls sigwait64 and tells us what it received.
430 pthread_t signal_thread;
431 int received_signal = -1;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700432 ASSERT_EQ(0, pthread_create(&signal_thread, nullptr, SignalHandlerFn, &received_signal));
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800433
434 // Send that thread SIGRTMIN.
435 pthread_kill(signal_thread, SIGRTMIN);
436
437 // See what it got.
438 void* join_result;
439 ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
440 ASSERT_EQ(SIGRTMIN, received_signal);
441 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
442
443 // Restore the original signal mask.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700444 ASSERT_EQ(0, pthread_sigmask64(SIG_SETMASK, &original_set, nullptr));
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800445}
446
Elliott Hughes725b2a92016-03-23 11:20:47 -0700447static void test_pthread_setname_np__pthread_getname_np(pthread_t t) {
448 ASSERT_EQ(0, pthread_setname_np(t, "short"));
449 char name[32];
450 ASSERT_EQ(0, pthread_getname_np(t, name, sizeof(name)));
451 ASSERT_STREQ("short", name);
452
Elliott Hughesd1aea302015-04-25 10:05:24 -0700453 // The limit is 15 characters --- the kernel's buffer is 16, but includes a NUL.
Elliott Hughes725b2a92016-03-23 11:20:47 -0700454 ASSERT_EQ(0, pthread_setname_np(t, "123456789012345"));
455 ASSERT_EQ(0, pthread_getname_np(t, name, sizeof(name)));
456 ASSERT_STREQ("123456789012345", name);
457
458 ASSERT_EQ(ERANGE, pthread_setname_np(t, "1234567890123456"));
459
460 // The passed-in buffer should be at least 16 bytes.
461 ASSERT_EQ(0, pthread_getname_np(t, name, 16));
462 ASSERT_EQ(ERANGE, pthread_getname_np(t, name, 15));
Elliott Hughes3e898472013-02-12 16:40:24 +0000463}
464
Elliott Hughes725b2a92016-03-23 11:20:47 -0700465TEST(pthread, pthread_setname_np__pthread_getname_np__self) {
466 test_pthread_setname_np__pthread_getname_np(pthread_self());
Elliott Hughes3e898472013-02-12 16:40:24 +0000467}
468
Elliott Hughes725b2a92016-03-23 11:20:47 -0700469TEST(pthread, pthread_setname_np__pthread_getname_np__other) {
470 SpinFunctionHelper spin_helper;
Yabin Cui63481602014-12-01 17:41:04 -0800471
Elliott Hughes725b2a92016-03-23 11:20:47 -0700472 pthread_t t;
Elliott Hughes4d098ca2016-04-11 12:43:05 -0700473 ASSERT_EQ(0, pthread_create(&t, nullptr, spin_helper.GetFunction(), nullptr));
474 test_pthread_setname_np__pthread_getname_np(t);
475 spin_helper.UnSpin();
476 ASSERT_EQ(0, pthread_join(t, nullptr));
477}
478
479// http://b/28051133: a kernel misfeature means that you can't change the
480// name of another thread if you've set PR_SET_DUMPABLE to 0.
481TEST(pthread, pthread_setname_np__pthread_getname_np__other_PR_SET_DUMPABLE) {
482 ASSERT_EQ(0, prctl(PR_SET_DUMPABLE, 0)) << strerror(errno);
483
484 SpinFunctionHelper spin_helper;
485
486 pthread_t t;
487 ASSERT_EQ(0, pthread_create(&t, nullptr, spin_helper.GetFunction(), nullptr));
Elliott Hughes725b2a92016-03-23 11:20:47 -0700488 test_pthread_setname_np__pthread_getname_np(t);
489 spin_helper.UnSpin();
490 ASSERT_EQ(0, pthread_join(t, nullptr));
Elliott Hughes3e898472013-02-12 16:40:24 +0000491}
492
Elliott Hughes11859d42017-02-13 17:59:29 -0800493TEST_F(pthread_DeathTest, pthread_setname_np__no_such_thread) {
Elliott Hughesbcb15292017-02-07 21:05:30 +0000494 pthread_t dead_thread;
495 MakeDeadThread(dead_thread);
496
Elliott Hughes6ce686c2017-02-21 13:15:20 -0800497 EXPECT_DEATH(pthread_setname_np(dead_thread, "short 3"), "invalid pthread_t");
498}
499
500TEST_F(pthread_DeathTest, pthread_setname_np__null_thread) {
501 pthread_t null_thread = 0;
502 EXPECT_EQ(ENOENT, pthread_setname_np(null_thread, "short 3"));
Elliott Hughes11859d42017-02-13 17:59:29 -0800503}
504
505TEST_F(pthread_DeathTest, pthread_getname_np__no_such_thread) {
506 pthread_t dead_thread;
507 MakeDeadThread(dead_thread);
508
Elliott Hughesbcb15292017-02-07 21:05:30 +0000509 char name[64];
Elliott Hughes6ce686c2017-02-21 13:15:20 -0800510 EXPECT_DEATH(pthread_getname_np(dead_thread, name, sizeof(name)), "invalid pthread_t");
511}
512
513TEST_F(pthread_DeathTest, pthread_getname_np__null_thread) {
514 pthread_t null_thread = 0;
515
516 char name[64];
517 EXPECT_EQ(ENOENT, pthread_getname_np(null_thread, name, sizeof(name)));
Elliott Hughesbcb15292017-02-07 21:05:30 +0000518}
519
Elliott Hughes9d23e042013-02-15 19:21:51 -0800520TEST(pthread, pthread_kill__0) {
521 // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
522 ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
523}
524
525TEST(pthread, pthread_kill__invalid_signal) {
526 ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
527}
528
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800529static void pthread_kill__in_signal_handler_helper(int signal_number) {
530 static int count = 0;
531 ASSERT_EQ(SIGALRM, signal_number);
532 if (++count == 1) {
533 // Can we call pthread_kill from a signal handler?
534 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
535 }
536}
537
538TEST(pthread, pthread_kill__in_signal_handler) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800539 ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper);
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800540 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
541}
542
Josh Gaoddf757e2018-10-17 15:23:03 -0700543TEST(pthread, pthread_kill__exited_thread) {
544 static std::promise<pid_t> tid_promise;
545 pthread_t thread;
546 ASSERT_EQ(0, pthread_create(&thread, nullptr,
547 [](void*) -> void* {
548 tid_promise.set_value(gettid());
549 return nullptr;
550 },
551 nullptr));
552
553 pid_t tid = tid_promise.get_future().get();
554 while (TEMP_FAILURE_RETRY(syscall(__NR_tgkill, getpid(), tid, 0)) != -1) {
555 continue;
556 }
557 ASSERT_EQ(ESRCH, errno);
558
559 ASSERT_EQ(ESRCH, pthread_kill(thread, 0));
560}
561
Elliott Hughes11859d42017-02-13 17:59:29 -0800562TEST_F(pthread_DeathTest, pthread_detach__no_such_thread) {
Elliott Hughes7484c212017-02-02 02:41:38 +0000563 pthread_t dead_thread;
564 MakeDeadThread(dead_thread);
565
Elliott Hughes6ce686c2017-02-21 13:15:20 -0800566 EXPECT_DEATH(pthread_detach(dead_thread), "invalid pthread_t");
567}
568
569TEST_F(pthread_DeathTest, pthread_detach__null_thread) {
570 pthread_t null_thread = 0;
571 EXPECT_EQ(ESRCH, pthread_detach(null_thread));
Elliott Hughes7484c212017-02-02 02:41:38 +0000572}
573
Jeff Hao9b06cc32013-08-15 14:51:16 -0700574TEST(pthread, pthread_getcpuclockid__clock_gettime) {
Elliott Hughes725b2a92016-03-23 11:20:47 -0700575 SpinFunctionHelper spin_helper;
Yabin Cui63481602014-12-01 17:41:04 -0800576
Jeff Hao9b06cc32013-08-15 14:51:16 -0700577 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700578 ASSERT_EQ(0, pthread_create(&t, nullptr, spin_helper.GetFunction(), nullptr));
Jeff Hao9b06cc32013-08-15 14:51:16 -0700579
580 clockid_t c;
581 ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
582 timespec ts;
583 ASSERT_EQ(0, clock_gettime(c, &ts));
Elliott Hughes725b2a92016-03-23 11:20:47 -0700584 spin_helper.UnSpin();
Yabin Cuia36158a2015-11-16 21:06:16 -0800585 ASSERT_EQ(0, pthread_join(t, nullptr));
Jeff Hao9b06cc32013-08-15 14:51:16 -0700586}
587
Elliott Hughes11859d42017-02-13 17:59:29 -0800588TEST_F(pthread_DeathTest, pthread_getcpuclockid__no_such_thread) {
Elliott Hughesbcb15292017-02-07 21:05:30 +0000589 pthread_t dead_thread;
590 MakeDeadThread(dead_thread);
591
592 clockid_t c;
Elliott Hughes6ce686c2017-02-21 13:15:20 -0800593 EXPECT_DEATH(pthread_getcpuclockid(dead_thread, &c), "invalid pthread_t");
594}
595
596TEST_F(pthread_DeathTest, pthread_getcpuclockid__null_thread) {
597 pthread_t null_thread = 0;
598 clockid_t c;
599 EXPECT_EQ(ESRCH, pthread_getcpuclockid(null_thread, &c));
Elliott Hughesbcb15292017-02-07 21:05:30 +0000600}
601
Elliott Hughes11859d42017-02-13 17:59:29 -0800602TEST_F(pthread_DeathTest, pthread_getschedparam__no_such_thread) {
Elliott Hughesbcb15292017-02-07 21:05:30 +0000603 pthread_t dead_thread;
604 MakeDeadThread(dead_thread);
605
606 int policy;
607 sched_param param;
Elliott Hughes6ce686c2017-02-21 13:15:20 -0800608 EXPECT_DEATH(pthread_getschedparam(dead_thread, &policy, &param), "invalid pthread_t");
609}
610
611TEST_F(pthread_DeathTest, pthread_getschedparam__null_thread) {
612 pthread_t null_thread = 0;
613 int policy;
614 sched_param param;
615 EXPECT_EQ(ESRCH, pthread_getschedparam(null_thread, &policy, &param));
Elliott Hughesbcb15292017-02-07 21:05:30 +0000616}
617
Elliott Hughes11859d42017-02-13 17:59:29 -0800618TEST_F(pthread_DeathTest, pthread_setschedparam__no_such_thread) {
Elliott Hughesbcb15292017-02-07 21:05:30 +0000619 pthread_t dead_thread;
620 MakeDeadThread(dead_thread);
621
622 int policy = 0;
623 sched_param param;
Elliott Hughes6ce686c2017-02-21 13:15:20 -0800624 EXPECT_DEATH(pthread_setschedparam(dead_thread, policy, &param), "invalid pthread_t");
625}
626
627TEST_F(pthread_DeathTest, pthread_setschedparam__null_thread) {
628 pthread_t null_thread = 0;
629 int policy = 0;
630 sched_param param;
631 EXPECT_EQ(ESRCH, pthread_setschedparam(null_thread, policy, &param));
Elliott Hughesbcb15292017-02-07 21:05:30 +0000632}
633
Elliott Hughesdff08ce2017-10-16 09:58:45 -0700634TEST_F(pthread_DeathTest, pthread_setschedprio__no_such_thread) {
635 pthread_t dead_thread;
636 MakeDeadThread(dead_thread);
637
638 EXPECT_DEATH(pthread_setschedprio(dead_thread, 123), "invalid pthread_t");
639}
640
641TEST_F(pthread_DeathTest, pthread_setschedprio__null_thread) {
642 pthread_t null_thread = 0;
643 EXPECT_EQ(ESRCH, pthread_setschedprio(null_thread, 123));
644}
645
Elliott Hughes11859d42017-02-13 17:59:29 -0800646TEST_F(pthread_DeathTest, pthread_join__no_such_thread) {
Elliott Hughes7484c212017-02-02 02:41:38 +0000647 pthread_t dead_thread;
648 MakeDeadThread(dead_thread);
649
Yi Kong32bc0fc2018-08-02 17:31:13 -0700650 EXPECT_DEATH(pthread_join(dead_thread, nullptr), "invalid pthread_t");
Elliott Hughes6ce686c2017-02-21 13:15:20 -0800651}
652
653TEST_F(pthread_DeathTest, pthread_join__null_thread) {
654 pthread_t null_thread = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700655 EXPECT_EQ(ESRCH, pthread_join(null_thread, nullptr));
Elliott Hughes7484c212017-02-02 02:41:38 +0000656}
657
Elliott Hughes11859d42017-02-13 17:59:29 -0800658TEST_F(pthread_DeathTest, pthread_kill__no_such_thread) {
Elliott Hughes7484c212017-02-02 02:41:38 +0000659 pthread_t dead_thread;
660 MakeDeadThread(dead_thread);
661
Elliott Hughes6ce686c2017-02-21 13:15:20 -0800662 EXPECT_DEATH(pthread_kill(dead_thread, 0), "invalid pthread_t");
663}
664
665TEST_F(pthread_DeathTest, pthread_kill__null_thread) {
666 pthread_t null_thread = 0;
667 EXPECT_EQ(ESRCH, pthread_kill(null_thread, 0));
Elliott Hughes7484c212017-02-02 02:41:38 +0000668}
669
msg5550f020d12013-06-06 14:59:28 -0400670TEST(pthread, pthread_join__multijoin) {
Elliott Hughes725b2a92016-03-23 11:20:47 -0700671 SpinFunctionHelper spin_helper;
msg5550f020d12013-06-06 14:59:28 -0400672
673 pthread_t t1;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700674 ASSERT_EQ(0, pthread_create(&t1, nullptr, spin_helper.GetFunction(), nullptr));
msg5550f020d12013-06-06 14:59:28 -0400675
676 pthread_t t2;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700677 ASSERT_EQ(0, pthread_create(&t2, nullptr, JoinFn, reinterpret_cast<void*>(t1)));
msg5550f020d12013-06-06 14:59:28 -0400678
679 sleep(1); // (Give t2 a chance to call pthread_join.)
680
681 // Multiple joins to the same thread should fail.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700682 ASSERT_EQ(EINVAL, pthread_join(t1, nullptr));
msg5550f020d12013-06-06 14:59:28 -0400683
Elliott Hughes725b2a92016-03-23 11:20:47 -0700684 spin_helper.UnSpin();
msg5550f020d12013-06-06 14:59:28 -0400685
686 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
687 void* join_result;
688 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700689 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
msg5550f020d12013-06-06 14:59:28 -0400690}
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700691
Elliott Hughes70b24b12013-11-15 11:51:07 -0800692TEST(pthread, pthread_join__race) {
693 // http://b/11693195 --- pthread_join could return before the thread had actually exited.
694 // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread.
695 for (size_t i = 0; i < 1024; ++i) {
Yabin Cuia36158a2015-11-16 21:06:16 -0800696 size_t stack_size = 640*1024;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700697 void* stack = mmap(nullptr, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
Elliott Hughes70b24b12013-11-15 11:51:07 -0800698
699 pthread_attr_t a;
700 pthread_attr_init(&a);
701 pthread_attr_setstack(&a, stack, stack_size);
702
703 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700704 ASSERT_EQ(0, pthread_create(&t, &a, IdFn, nullptr));
705 ASSERT_EQ(0, pthread_join(t, nullptr));
Elliott Hughes70b24b12013-11-15 11:51:07 -0800706 ASSERT_EQ(0, munmap(stack, stack_size));
707 }
708}
709
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700710static void* GetActualGuardSizeFn(void* arg) {
711 pthread_attr_t attributes;
712 pthread_getattr_np(pthread_self(), &attributes);
713 pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700714 return nullptr;
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700715}
716
717static size_t GetActualGuardSize(const pthread_attr_t& attributes) {
718 size_t result;
719 pthread_t t;
720 pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700721 pthread_join(t, nullptr);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700722 return result;
723}
724
725static void* GetActualStackSizeFn(void* arg) {
726 pthread_attr_t attributes;
727 pthread_getattr_np(pthread_self(), &attributes);
728 pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700729 return nullptr;
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700730}
731
732static size_t GetActualStackSize(const pthread_attr_t& attributes) {
733 size_t result;
734 pthread_t t;
735 pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700736 pthread_join(t, nullptr);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700737 return result;
738}
739
Elliott Hughesd6c678c2017-06-27 17:01:57 -0700740TEST(pthread, pthread_attr_setguardsize_tiny) {
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700741 pthread_attr_t attributes;
742 ASSERT_EQ(0, pthread_attr_init(&attributes));
743
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700744 // No such thing as too small: will be rounded up to one page by pthread_create.
745 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
746 size_t guard_size;
747 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
748 ASSERT_EQ(128U, guard_size);
749 ASSERT_EQ(4096U, GetActualGuardSize(attributes));
Elliott Hughesd6c678c2017-06-27 17:01:57 -0700750}
751
752TEST(pthread, pthread_attr_setguardsize_reasonable) {
753 pthread_attr_t attributes;
754 ASSERT_EQ(0, pthread_attr_init(&attributes));
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700755
756 // Large enough and a multiple of the page size.
757 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
Elliott Hughesd6c678c2017-06-27 17:01:57 -0700758 size_t guard_size;
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700759 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
760 ASSERT_EQ(32*1024U, guard_size);
Elliott Hughesd6c678c2017-06-27 17:01:57 -0700761 ASSERT_EQ(32*1024U, GetActualGuardSize(attributes));
762}
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700763
Elliott Hughesd6c678c2017-06-27 17:01:57 -0700764TEST(pthread, pthread_attr_setguardsize_needs_rounding) {
765 pthread_attr_t attributes;
766 ASSERT_EQ(0, pthread_attr_init(&attributes));
767
768 // Large enough but not a multiple of the page size.
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700769 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
Elliott Hughesd6c678c2017-06-27 17:01:57 -0700770 size_t guard_size;
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700771 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
772 ASSERT_EQ(32*1024U + 1, guard_size);
Elliott Hughesd6c678c2017-06-27 17:01:57 -0700773 ASSERT_EQ(36*1024U, GetActualGuardSize(attributes));
774}
775
776TEST(pthread, pthread_attr_setguardsize_enormous) {
777 pthread_attr_t attributes;
778 ASSERT_EQ(0, pthread_attr_init(&attributes));
779
780 // Larger than the stack itself. (Historically we mistakenly carved
781 // the guard out of the stack itself, rather than adding it after the
782 // end.)
783 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024*1024));
784 size_t guard_size;
785 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
786 ASSERT_EQ(32*1024*1024U, guard_size);
787 ASSERT_EQ(32*1024*1024U, GetActualGuardSize(attributes));
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700788}
789
790TEST(pthread, pthread_attr_setstacksize) {
791 pthread_attr_t attributes;
792 ASSERT_EQ(0, pthread_attr_init(&attributes));
793
794 // Get the default stack size.
795 size_t default_stack_size;
796 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
797
798 // Too small.
799 ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
800 size_t stack_size;
801 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
802 ASSERT_EQ(default_stack_size, stack_size);
803 ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
804
Yabin Cui917d3902015-01-08 12:32:42 -0800805 // Large enough and a multiple of the page size; may be rounded up by pthread_create.
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700806 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
807 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
808 ASSERT_EQ(32*1024U, stack_size);
Yabin Cui917d3902015-01-08 12:32:42 -0800809 ASSERT_GE(GetActualStackSize(attributes), 32*1024U);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700810
Yabin Cui917d3902015-01-08 12:32:42 -0800811 // Large enough but not aligned; will be rounded up by pthread_create.
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700812 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
813 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
814 ASSERT_EQ(32*1024U + 1, stack_size);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800815#if defined(__BIONIC__)
Yabin Cui917d3902015-01-08 12:32:42 -0800816 ASSERT_GT(GetActualStackSize(attributes), 32*1024U + 1);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800817#else // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700818 // glibc rounds down, in violation of POSIX. They document this in their BUGS section.
819 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800820#endif // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700821}
Elliott Hughesc3f11402013-10-30 14:40:09 -0700822
Yabin Cui76615da2015-03-17 14:22:09 -0700823TEST(pthread, pthread_rwlockattr_smoke) {
824 pthread_rwlockattr_t attr;
825 ASSERT_EQ(0, pthread_rwlockattr_init(&attr));
826
827 int pshared_value_array[] = {PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_SHARED};
828 for (size_t i = 0; i < sizeof(pshared_value_array) / sizeof(pshared_value_array[0]); ++i) {
829 ASSERT_EQ(0, pthread_rwlockattr_setpshared(&attr, pshared_value_array[i]));
830 int pshared;
831 ASSERT_EQ(0, pthread_rwlockattr_getpshared(&attr, &pshared));
832 ASSERT_EQ(pshared_value_array[i], pshared);
833 }
834
835 int kind_array[] = {PTHREAD_RWLOCK_PREFER_READER_NP,
836 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP};
837 for (size_t i = 0; i < sizeof(kind_array) / sizeof(kind_array[0]); ++i) {
838 ASSERT_EQ(0, pthread_rwlockattr_setkind_np(&attr, kind_array[i]));
839 int kind;
840 ASSERT_EQ(0, pthread_rwlockattr_getkind_np(&attr, &kind));
841 ASSERT_EQ(kind_array[i], kind);
842 }
843
844 ASSERT_EQ(0, pthread_rwlockattr_destroy(&attr));
845}
846
847TEST(pthread, pthread_rwlock_init_same_as_PTHREAD_RWLOCK_INITIALIZER) {
848 pthread_rwlock_t lock1 = PTHREAD_RWLOCK_INITIALIZER;
849 pthread_rwlock_t lock2;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700850 ASSERT_EQ(0, pthread_rwlock_init(&lock2, nullptr));
Yabin Cui76615da2015-03-17 14:22:09 -0700851 ASSERT_EQ(0, memcmp(&lock1, &lock2, sizeof(lock1)));
852}
853
Elliott Hughesc3f11402013-10-30 14:40:09 -0700854TEST(pthread, pthread_rwlock_smoke) {
855 pthread_rwlock_t l;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700856 ASSERT_EQ(0, pthread_rwlock_init(&l, nullptr));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700857
Calin Juravle76f352e2014-05-19 13:41:10 +0100858 // Single read lock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700859 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
860 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
861
Calin Juravle76f352e2014-05-19 13:41:10 +0100862 // Multiple read lock
863 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
864 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
865 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
866 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
867
868 // Write lock
Calin Juravle92687e42014-05-22 19:21:22 +0100869 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
870 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
Calin Juravle76f352e2014-05-19 13:41:10 +0100871
872 // Try writer lock
873 ASSERT_EQ(0, pthread_rwlock_trywrlock(&l));
874 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
875 ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l));
876 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
877
878 // Try reader lock
879 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
880 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
881 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
882 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
883 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
884
885 // Try writer lock after unlock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700886 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
887 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
888
Calin Juravle76f352e2014-05-19 13:41:10 +0100889 // EDEADLK in "read after write"
890 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
891 ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l));
892 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
893
894 // EDEADLK in "write after write"
895 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
896 ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l));
897 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
Calin Juravle76f352e2014-05-19 13:41:10 +0100898
Elliott Hughesc3f11402013-10-30 14:40:09 -0700899 ASSERT_EQ(0, pthread_rwlock_destroy(&l));
900}
901
Yabin Cui08ee8d22015-02-11 17:04:36 -0800902struct RwlockWakeupHelperArg {
903 pthread_rwlock_t lock;
904 enum Progress {
905 LOCK_INITIALIZED,
906 LOCK_WAITING,
907 LOCK_RELEASED,
Yabin Cuic9a659c2015-11-05 15:36:08 -0800908 LOCK_ACCESSED,
909 LOCK_TIMEDOUT,
Yabin Cui08ee8d22015-02-11 17:04:36 -0800910 };
911 std::atomic<Progress> progress;
Yabin Cuif7969852015-04-02 17:47:48 -0700912 std::atomic<pid_t> tid;
Yabin Cuic9a659c2015-11-05 15:36:08 -0800913 std::function<int (pthread_rwlock_t*)> trylock_function;
914 std::function<int (pthread_rwlock_t*)> lock_function;
915 std::function<int (pthread_rwlock_t*, const timespec*)> timed_lock_function;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800916 clockid_t clock;
Yabin Cui08ee8d22015-02-11 17:04:36 -0800917};
918
Yabin Cuic9a659c2015-11-05 15:36:08 -0800919static void pthread_rwlock_wakeup_helper(RwlockWakeupHelperArg* arg) {
Yabin Cuif7969852015-04-02 17:47:48 -0700920 arg->tid = gettid();
Yabin Cui08ee8d22015-02-11 17:04:36 -0800921 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
922 arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
923
Yabin Cuic9a659c2015-11-05 15:36:08 -0800924 ASSERT_EQ(EBUSY, arg->trylock_function(&arg->lock));
925 ASSERT_EQ(0, arg->lock_function(&arg->lock));
Yabin Cui08ee8d22015-02-11 17:04:36 -0800926 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress);
927 ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock));
928
929 arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED;
930}
931
Yabin Cuic9a659c2015-11-05 15:36:08 -0800932static void test_pthread_rwlock_reader_wakeup_writer(std::function<int (pthread_rwlock_t*)> lock_function) {
Yabin Cui08ee8d22015-02-11 17:04:36 -0800933 RwlockWakeupHelperArg wakeup_arg;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700934 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, nullptr));
Yabin Cui08ee8d22015-02-11 17:04:36 -0800935 ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock));
936 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
Yabin Cuif7969852015-04-02 17:47:48 -0700937 wakeup_arg.tid = 0;
Tom Cherry60ddedf2018-02-20 15:40:02 -0800938 wakeup_arg.trylock_function = &pthread_rwlock_trywrlock;
Yabin Cuic9a659c2015-11-05 15:36:08 -0800939 wakeup_arg.lock_function = lock_function;
Yabin Cui08ee8d22015-02-11 17:04:36 -0800940
941 pthread_t thread;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700942 ASSERT_EQ(0, pthread_create(&thread, nullptr,
Yabin Cuic9a659c2015-11-05 15:36:08 -0800943 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_helper), &wakeup_arg));
Yabin Cuif7969852015-04-02 17:47:48 -0700944 WaitUntilThreadSleep(wakeup_arg.tid);
945 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
946
Yabin Cui08ee8d22015-02-11 17:04:36 -0800947 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
948 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
949
Yi Kong32bc0fc2018-08-02 17:31:13 -0700950 ASSERT_EQ(0, pthread_join(thread, nullptr));
Yabin Cui08ee8d22015-02-11 17:04:36 -0800951 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress);
952 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
953}
954
Yabin Cuic9a659c2015-11-05 15:36:08 -0800955TEST(pthread, pthread_rwlock_reader_wakeup_writer) {
956 test_pthread_rwlock_reader_wakeup_writer(pthread_rwlock_wrlock);
Yabin Cui08ee8d22015-02-11 17:04:36 -0800957}
958
Yabin Cuic9a659c2015-11-05 15:36:08 -0800959TEST(pthread, pthread_rwlock_reader_wakeup_writer_timedwait) {
960 timespec ts;
961 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
962 ts.tv_sec += 1;
963 test_pthread_rwlock_reader_wakeup_writer([&](pthread_rwlock_t* lock) {
964 return pthread_rwlock_timedwrlock(lock, &ts);
965 });
966}
967
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800968TEST(pthread, pthread_rwlock_reader_wakeup_writer_timedwait_monotonic_np) {
969#if defined(__BIONIC__)
970 timespec ts;
971 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
972 ts.tv_sec += 1;
973 test_pthread_rwlock_reader_wakeup_writer(
974 [&](pthread_rwlock_t* lock) { return pthread_rwlock_timedwrlock_monotonic_np(lock, &ts); });
975#else // __BIONIC__
976 GTEST_LOG_(INFO) << "This test does nothing since pthread_rwlock_timedwrlock_monotonic_np is "
977 "only supported on bionic";
978#endif // __BIONIC__
979}
980
Yabin Cuic9a659c2015-11-05 15:36:08 -0800981static void test_pthread_rwlock_writer_wakeup_reader(std::function<int (pthread_rwlock_t*)> lock_function) {
Yabin Cui08ee8d22015-02-11 17:04:36 -0800982 RwlockWakeupHelperArg wakeup_arg;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700983 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, nullptr));
Yabin Cui08ee8d22015-02-11 17:04:36 -0800984 ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock));
985 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
Yabin Cuif7969852015-04-02 17:47:48 -0700986 wakeup_arg.tid = 0;
Tom Cherry60ddedf2018-02-20 15:40:02 -0800987 wakeup_arg.trylock_function = &pthread_rwlock_tryrdlock;
Yabin Cuic9a659c2015-11-05 15:36:08 -0800988 wakeup_arg.lock_function = lock_function;
Yabin Cui08ee8d22015-02-11 17:04:36 -0800989
990 pthread_t thread;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700991 ASSERT_EQ(0, pthread_create(&thread, nullptr,
Yabin Cuic9a659c2015-11-05 15:36:08 -0800992 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_helper), &wakeup_arg));
Yabin Cuif7969852015-04-02 17:47:48 -0700993 WaitUntilThreadSleep(wakeup_arg.tid);
994 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
995
Yabin Cui08ee8d22015-02-11 17:04:36 -0800996 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
997 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
998
Yi Kong32bc0fc2018-08-02 17:31:13 -0700999 ASSERT_EQ(0, pthread_join(thread, nullptr));
Yabin Cui08ee8d22015-02-11 17:04:36 -08001000 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress);
1001 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
1002}
1003
Yabin Cuic9a659c2015-11-05 15:36:08 -08001004TEST(pthread, pthread_rwlock_writer_wakeup_reader) {
1005 test_pthread_rwlock_writer_wakeup_reader(pthread_rwlock_rdlock);
1006}
1007
1008TEST(pthread, pthread_rwlock_writer_wakeup_reader_timedwait) {
1009 timespec ts;
1010 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
1011 ts.tv_sec += 1;
1012 test_pthread_rwlock_writer_wakeup_reader([&](pthread_rwlock_t* lock) {
1013 return pthread_rwlock_timedrdlock(lock, &ts);
1014 });
1015}
1016
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08001017TEST(pthread, pthread_rwlock_writer_wakeup_reader_timedwait_monotonic_np) {
1018#if defined(__BIONIC__)
1019 timespec ts;
1020 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
1021 ts.tv_sec += 1;
1022 test_pthread_rwlock_writer_wakeup_reader(
1023 [&](pthread_rwlock_t* lock) { return pthread_rwlock_timedrdlock_monotonic_np(lock, &ts); });
1024#else // __BIONIC__
1025 GTEST_LOG_(INFO) << "This test does nothing since pthread_rwlock_timedrdlock_monotonic_np is "
1026 "only supported on bionic";
1027#endif // __BIONIC__
1028}
1029
Yabin Cuic9a659c2015-11-05 15:36:08 -08001030static void pthread_rwlock_wakeup_timeout_helper(RwlockWakeupHelperArg* arg) {
1031 arg->tid = gettid();
1032 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
1033 arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
1034
1035 ASSERT_EQ(EBUSY, arg->trylock_function(&arg->lock));
1036
1037 timespec ts;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08001038 ASSERT_EQ(0, clock_gettime(arg->clock, &ts));
Yabin Cuic9a659c2015-11-05 15:36:08 -08001039 ASSERT_EQ(ETIMEDOUT, arg->timed_lock_function(&arg->lock, &ts));
1040 ts.tv_nsec = -1;
1041 ASSERT_EQ(EINVAL, arg->timed_lock_function(&arg->lock, &ts));
1042 ts.tv_nsec = NS_PER_S;
1043 ASSERT_EQ(EINVAL, arg->timed_lock_function(&arg->lock, &ts));
1044 ts.tv_nsec = NS_PER_S - 1;
1045 ts.tv_sec = -1;
1046 ASSERT_EQ(ETIMEDOUT, arg->timed_lock_function(&arg->lock, &ts));
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08001047 ASSERT_EQ(0, clock_gettime(arg->clock, &ts));
Yabin Cuic9a659c2015-11-05 15:36:08 -08001048 ts.tv_sec += 1;
1049 ASSERT_EQ(ETIMEDOUT, arg->timed_lock_function(&arg->lock, &ts));
1050 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, arg->progress);
1051 arg->progress = RwlockWakeupHelperArg::LOCK_TIMEDOUT;
1052}
1053
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08001054static void pthread_rwlock_timedrdlock_timeout_helper(
1055 clockid_t clock, int (*lock_function)(pthread_rwlock_t* __rwlock, const timespec* __timeout)) {
Yabin Cuic9a659c2015-11-05 15:36:08 -08001056 RwlockWakeupHelperArg wakeup_arg;
1057 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, nullptr));
1058 ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock));
1059 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
1060 wakeup_arg.tid = 0;
Tom Cherry60ddedf2018-02-20 15:40:02 -08001061 wakeup_arg.trylock_function = &pthread_rwlock_tryrdlock;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08001062 wakeup_arg.timed_lock_function = lock_function;
1063 wakeup_arg.clock = clock;
1064
1065 pthread_t thread;
1066 ASSERT_EQ(0, pthread_create(&thread, nullptr,
1067 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_timeout_helper), &wakeup_arg));
1068 WaitUntilThreadSleep(wakeup_arg.tid);
1069 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
1070
1071 ASSERT_EQ(0, pthread_join(thread, nullptr));
1072 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_TIMEDOUT, wakeup_arg.progress);
1073 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
1074 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
1075}
1076
1077TEST(pthread, pthread_rwlock_timedrdlock_timeout) {
1078 pthread_rwlock_timedrdlock_timeout_helper(CLOCK_REALTIME, pthread_rwlock_timedrdlock);
1079}
1080
1081TEST(pthread, pthread_rwlock_timedrdlock_monotonic_np_timeout) {
1082#if defined(__BIONIC__)
1083 pthread_rwlock_timedrdlock_timeout_helper(CLOCK_MONOTONIC,
1084 pthread_rwlock_timedrdlock_monotonic_np);
1085#else // __BIONIC__
1086 GTEST_LOG_(INFO) << "This test does nothing since pthread_rwlock_timedrdlock_monotonic_np is "
1087 "only supported on bionic";
1088#endif // __BIONIC__
1089}
1090
1091static void pthread_rwlock_timedwrlock_timeout_helper(
1092 clockid_t clock, int (*lock_function)(pthread_rwlock_t* __rwlock, const timespec* __timeout)) {
1093 RwlockWakeupHelperArg wakeup_arg;
1094 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, nullptr));
1095 ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock));
1096 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
1097 wakeup_arg.tid = 0;
1098 wakeup_arg.trylock_function = &pthread_rwlock_trywrlock;
1099 wakeup_arg.timed_lock_function = lock_function;
1100 wakeup_arg.clock = clock;
Yabin Cuic9a659c2015-11-05 15:36:08 -08001101
1102 pthread_t thread;
1103 ASSERT_EQ(0, pthread_create(&thread, nullptr,
1104 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_timeout_helper), &wakeup_arg));
1105 WaitUntilThreadSleep(wakeup_arg.tid);
1106 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
1107
1108 ASSERT_EQ(0, pthread_join(thread, nullptr));
1109 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_TIMEDOUT, wakeup_arg.progress);
1110 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
1111 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
1112}
1113
1114TEST(pthread, pthread_rwlock_timedwrlock_timeout) {
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08001115 pthread_rwlock_timedwrlock_timeout_helper(CLOCK_REALTIME, pthread_rwlock_timedwrlock);
1116}
Yabin Cuic9a659c2015-11-05 15:36:08 -08001117
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08001118TEST(pthread, pthread_rwlock_timedwrlock_monotonic_np_timeout) {
1119#if defined(__BIONIC__)
1120 pthread_rwlock_timedwrlock_timeout_helper(CLOCK_MONOTONIC,
1121 pthread_rwlock_timedwrlock_monotonic_np);
1122#else // __BIONIC__
1123 GTEST_LOG_(INFO) << "This test does nothing since pthread_rwlock_timedwrlock_monotonic_np is "
1124 "only supported on bionic";
1125#endif // __BIONIC__
Yabin Cuic9a659c2015-11-05 15:36:08 -08001126}
1127
Yabin Cui76615da2015-03-17 14:22:09 -07001128class RwlockKindTestHelper {
1129 private:
1130 struct ThreadArg {
1131 RwlockKindTestHelper* helper;
1132 std::atomic<pid_t>& tid;
1133
1134 ThreadArg(RwlockKindTestHelper* helper, std::atomic<pid_t>& tid)
1135 : helper(helper), tid(tid) { }
1136 };
1137
1138 public:
1139 pthread_rwlock_t lock;
1140
1141 public:
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -07001142 explicit RwlockKindTestHelper(int kind_type) {
Yabin Cui76615da2015-03-17 14:22:09 -07001143 InitRwlock(kind_type);
1144 }
1145
1146 ~RwlockKindTestHelper() {
1147 DestroyRwlock();
1148 }
1149
1150 void CreateWriterThread(pthread_t& thread, std::atomic<pid_t>& tid) {
1151 tid = 0;
1152 ThreadArg* arg = new ThreadArg(this, tid);
Yi Kong32bc0fc2018-08-02 17:31:13 -07001153 ASSERT_EQ(0, pthread_create(&thread, nullptr,
Yabin Cui76615da2015-03-17 14:22:09 -07001154 reinterpret_cast<void* (*)(void*)>(WriterThreadFn), arg));
1155 }
1156
1157 void CreateReaderThread(pthread_t& thread, std::atomic<pid_t>& tid) {
1158 tid = 0;
1159 ThreadArg* arg = new ThreadArg(this, tid);
Yi Kong32bc0fc2018-08-02 17:31:13 -07001160 ASSERT_EQ(0, pthread_create(&thread, nullptr,
Yabin Cui76615da2015-03-17 14:22:09 -07001161 reinterpret_cast<void* (*)(void*)>(ReaderThreadFn), arg));
1162 }
1163
1164 private:
1165 void InitRwlock(int kind_type) {
1166 pthread_rwlockattr_t attr;
1167 ASSERT_EQ(0, pthread_rwlockattr_init(&attr));
1168 ASSERT_EQ(0, pthread_rwlockattr_setkind_np(&attr, kind_type));
1169 ASSERT_EQ(0, pthread_rwlock_init(&lock, &attr));
1170 ASSERT_EQ(0, pthread_rwlockattr_destroy(&attr));
1171 }
1172
1173 void DestroyRwlock() {
1174 ASSERT_EQ(0, pthread_rwlock_destroy(&lock));
1175 }
1176
1177 static void WriterThreadFn(ThreadArg* arg) {
1178 arg->tid = gettid();
1179
1180 RwlockKindTestHelper* helper = arg->helper;
1181 ASSERT_EQ(0, pthread_rwlock_wrlock(&helper->lock));
1182 ASSERT_EQ(0, pthread_rwlock_unlock(&helper->lock));
1183 delete arg;
1184 }
1185
1186 static void ReaderThreadFn(ThreadArg* arg) {
1187 arg->tid = gettid();
1188
1189 RwlockKindTestHelper* helper = arg->helper;
1190 ASSERT_EQ(0, pthread_rwlock_rdlock(&helper->lock));
1191 ASSERT_EQ(0, pthread_rwlock_unlock(&helper->lock));
1192 delete arg;
1193 }
1194};
1195
1196TEST(pthread, pthread_rwlock_kind_PTHREAD_RWLOCK_PREFER_READER_NP) {
1197 RwlockKindTestHelper helper(PTHREAD_RWLOCK_PREFER_READER_NP);
1198 ASSERT_EQ(0, pthread_rwlock_rdlock(&helper.lock));
1199
1200 pthread_t writer_thread;
1201 std::atomic<pid_t> writer_tid;
1202 helper.CreateWriterThread(writer_thread, writer_tid);
1203 WaitUntilThreadSleep(writer_tid);
1204
1205 pthread_t reader_thread;
1206 std::atomic<pid_t> reader_tid;
1207 helper.CreateReaderThread(reader_thread, reader_tid);
Yi Kong32bc0fc2018-08-02 17:31:13 -07001208 ASSERT_EQ(0, pthread_join(reader_thread, nullptr));
Yabin Cui76615da2015-03-17 14:22:09 -07001209
1210 ASSERT_EQ(0, pthread_rwlock_unlock(&helper.lock));
Yi Kong32bc0fc2018-08-02 17:31:13 -07001211 ASSERT_EQ(0, pthread_join(writer_thread, nullptr));
Yabin Cui76615da2015-03-17 14:22:09 -07001212}
1213
1214TEST(pthread, pthread_rwlock_kind_PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP) {
1215 RwlockKindTestHelper helper(PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
1216 ASSERT_EQ(0, pthread_rwlock_rdlock(&helper.lock));
1217
1218 pthread_t writer_thread;
1219 std::atomic<pid_t> writer_tid;
1220 helper.CreateWriterThread(writer_thread, writer_tid);
1221 WaitUntilThreadSleep(writer_tid);
1222
1223 pthread_t reader_thread;
1224 std::atomic<pid_t> reader_tid;
1225 helper.CreateReaderThread(reader_thread, reader_tid);
1226 WaitUntilThreadSleep(reader_tid);
1227
1228 ASSERT_EQ(0, pthread_rwlock_unlock(&helper.lock));
Yi Kong32bc0fc2018-08-02 17:31:13 -07001229 ASSERT_EQ(0, pthread_join(writer_thread, nullptr));
1230 ASSERT_EQ(0, pthread_join(reader_thread, nullptr));
Yabin Cui76615da2015-03-17 14:22:09 -07001231}
1232
Elliott Hughes1728b232014-05-14 10:02:03 -07001233static int g_once_fn_call_count = 0;
Elliott Hughesc3f11402013-10-30 14:40:09 -07001234static void OnceFn() {
Elliott Hughes1728b232014-05-14 10:02:03 -07001235 ++g_once_fn_call_count;
Elliott Hughesc3f11402013-10-30 14:40:09 -07001236}
1237
1238TEST(pthread, pthread_once_smoke) {
1239 pthread_once_t once_control = PTHREAD_ONCE_INIT;
1240 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
1241 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
Elliott Hughes1728b232014-05-14 10:02:03 -07001242 ASSERT_EQ(1, g_once_fn_call_count);
Elliott Hughesc3f11402013-10-30 14:40:09 -07001243}
1244
Elliott Hughes3694ec62014-05-14 11:46:08 -07001245static std::string pthread_once_1934122_result = "";
1246
1247static void Routine2() {
1248 pthread_once_1934122_result += "2";
1249}
1250
1251static void Routine1() {
1252 pthread_once_t once_control_2 = PTHREAD_ONCE_INIT;
1253 pthread_once_1934122_result += "1";
1254 pthread_once(&once_control_2, &Routine2);
1255}
1256
1257TEST(pthread, pthread_once_1934122) {
1258 // Very old versions of Android couldn't call pthread_once from a
1259 // pthread_once init routine. http://b/1934122.
1260 pthread_once_t once_control_1 = PTHREAD_ONCE_INIT;
1261 ASSERT_EQ(0, pthread_once(&once_control_1, &Routine1));
1262 ASSERT_EQ("12", pthread_once_1934122_result);
1263}
1264
Elliott Hughes1728b232014-05-14 10:02:03 -07001265static int g_atfork_prepare_calls = 0;
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001266static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 1; }
1267static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 2; }
Elliott Hughes1728b232014-05-14 10:02:03 -07001268static int g_atfork_parent_calls = 0;
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001269static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 1; }
1270static void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 2; }
Elliott Hughes1728b232014-05-14 10:02:03 -07001271static int g_atfork_child_calls = 0;
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001272static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 1; }
1273static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 2; }
Elliott Hughesc3f11402013-10-30 14:40:09 -07001274
Dmitriy Ivanov00e37812014-11-20 16:53:47 -08001275TEST(pthread, pthread_atfork_smoke) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001276 ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
1277 ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
Elliott Hughesc3f11402013-10-30 14:40:09 -07001278
Elliott Hughes33697a02016-01-26 13:04:57 -08001279 pid_t pid = fork();
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001280 ASSERT_NE(-1, pid) << strerror(errno);
Elliott Hughesc3f11402013-10-30 14:40:09 -07001281
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001282 // Child and parent calls are made in the order they were registered.
1283 if (pid == 0) {
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001284 ASSERT_EQ(12, g_atfork_child_calls);
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001285 _exit(0);
1286 }
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001287 ASSERT_EQ(12, g_atfork_parent_calls);
Elliott Hughesc3f11402013-10-30 14:40:09 -07001288
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001289 // Prepare calls are made in the reverse order.
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001290 ASSERT_EQ(21, g_atfork_prepare_calls);
Elliott Hughes33697a02016-01-26 13:04:57 -08001291 AssertChildExited(pid, 0);
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001292}
1293
Elliott Hughesc3f11402013-10-30 14:40:09 -07001294TEST(pthread, pthread_attr_getscope) {
1295 pthread_attr_t attr;
1296 ASSERT_EQ(0, pthread_attr_init(&attr));
1297
1298 int scope;
1299 ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope));
1300 ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope);
1301}
Narayan Kamath51e6cb32014-03-03 15:38:51 +00001302
1303TEST(pthread, pthread_condattr_init) {
1304 pthread_condattr_t attr;
1305 pthread_condattr_init(&attr);
1306
1307 clockid_t clock;
1308 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
1309 ASSERT_EQ(CLOCK_REALTIME, clock);
1310
1311 int pshared;
1312 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
1313 ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared);
1314}
1315
1316TEST(pthread, pthread_condattr_setclock) {
1317 pthread_condattr_t attr;
1318 pthread_condattr_init(&attr);
1319
1320 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME));
1321 clockid_t clock;
1322 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
1323 ASSERT_EQ(CLOCK_REALTIME, clock);
1324
1325 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
1326 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
1327 ASSERT_EQ(CLOCK_MONOTONIC, clock);
1328
1329 ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID));
1330}
1331
1332TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) {
Yabin Cui32651b82015-03-13 20:30:00 -07001333#if defined(__BIONIC__)
Narayan Kamath51e6cb32014-03-03 15:38:51 +00001334 pthread_condattr_t attr;
1335 pthread_condattr_init(&attr);
1336
1337 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
1338 ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
1339
1340 pthread_cond_t cond_var;
1341 ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr));
1342
1343 ASSERT_EQ(0, pthread_cond_signal(&cond_var));
1344 ASSERT_EQ(0, pthread_cond_broadcast(&cond_var));
1345
Yabin Cui32651b82015-03-13 20:30:00 -07001346 attr = static_cast<pthread_condattr_t>(*reinterpret_cast<uint32_t*>(cond_var.__private));
Narayan Kamath51e6cb32014-03-03 15:38:51 +00001347 clockid_t clock;
1348 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
1349 ASSERT_EQ(CLOCK_MONOTONIC, clock);
1350 int pshared;
1351 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
1352 ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
Yabin Cui32651b82015-03-13 20:30:00 -07001353#else // !defined(__BIONIC__)
1354 GTEST_LOG_(INFO) << "This tests a bionic implementation detail.\n";
1355#endif // !defined(__BIONIC__)
1356}
1357
1358class pthread_CondWakeupTest : public ::testing::Test {
1359 protected:
1360 pthread_mutex_t mutex;
1361 pthread_cond_t cond;
1362
1363 enum Progress {
1364 INITIALIZED,
1365 WAITING,
1366 SIGNALED,
1367 FINISHED,
1368 };
1369 std::atomic<Progress> progress;
1370 pthread_t thread;
Yabin Cuic9a659c2015-11-05 15:36:08 -08001371 std::function<int (pthread_cond_t* cond, pthread_mutex_t* mutex)> wait_function;
Yabin Cui32651b82015-03-13 20:30:00 -07001372
1373 protected:
Yabin Cuic9a659c2015-11-05 15:36:08 -08001374 void SetUp() override {
1375 ASSERT_EQ(0, pthread_mutex_init(&mutex, nullptr));
1376 }
1377
1378 void InitCond(clockid_t clock=CLOCK_REALTIME) {
1379 pthread_condattr_t attr;
1380 ASSERT_EQ(0, pthread_condattr_init(&attr));
1381 ASSERT_EQ(0, pthread_condattr_setclock(&attr, clock));
1382 ASSERT_EQ(0, pthread_cond_init(&cond, &attr));
1383 ASSERT_EQ(0, pthread_condattr_destroy(&attr));
1384 }
1385
1386 void StartWaitingThread(std::function<int (pthread_cond_t* cond, pthread_mutex_t* mutex)> wait_function) {
Yabin Cui32651b82015-03-13 20:30:00 -07001387 progress = INITIALIZED;
Yabin Cuic9a659c2015-11-05 15:36:08 -08001388 this->wait_function = wait_function;
Yi Kong32bc0fc2018-08-02 17:31:13 -07001389 ASSERT_EQ(0, pthread_create(&thread, nullptr, reinterpret_cast<void* (*)(void*)>(WaitThreadFn), this));
Yabin Cuic9a659c2015-11-05 15:36:08 -08001390 while (progress != WAITING) {
Yabin Cui32651b82015-03-13 20:30:00 -07001391 usleep(5000);
1392 }
1393 usleep(5000);
1394 }
1395
Yabin Cuic9a659c2015-11-05 15:36:08 -08001396 void TearDown() override {
1397 ASSERT_EQ(0, pthread_join(thread, nullptr));
1398 ASSERT_EQ(FINISHED, progress);
1399 ASSERT_EQ(0, pthread_cond_destroy(&cond));
1400 ASSERT_EQ(0, pthread_mutex_destroy(&mutex));
1401 }
1402
Yabin Cui32651b82015-03-13 20:30:00 -07001403 private:
1404 static void WaitThreadFn(pthread_CondWakeupTest* test) {
1405 ASSERT_EQ(0, pthread_mutex_lock(&test->mutex));
1406 test->progress = WAITING;
1407 while (test->progress == WAITING) {
Yabin Cuic9a659c2015-11-05 15:36:08 -08001408 ASSERT_EQ(0, test->wait_function(&test->cond, &test->mutex));
Yabin Cui32651b82015-03-13 20:30:00 -07001409 }
1410 ASSERT_EQ(SIGNALED, test->progress);
1411 test->progress = FINISHED;
1412 ASSERT_EQ(0, pthread_mutex_unlock(&test->mutex));
1413 }
1414};
1415
Yabin Cuic9a659c2015-11-05 15:36:08 -08001416TEST_F(pthread_CondWakeupTest, signal_wait) {
1417 InitCond();
1418 StartWaitingThread([](pthread_cond_t* cond, pthread_mutex_t* mutex) {
1419 return pthread_cond_wait(cond, mutex);
1420 });
Yabin Cui32651b82015-03-13 20:30:00 -07001421 progress = SIGNALED;
Yabin Cuic9a659c2015-11-05 15:36:08 -08001422 ASSERT_EQ(0, pthread_cond_signal(&cond));
Yabin Cui32651b82015-03-13 20:30:00 -07001423}
1424
Yabin Cuic9a659c2015-11-05 15:36:08 -08001425TEST_F(pthread_CondWakeupTest, broadcast_wait) {
1426 InitCond();
1427 StartWaitingThread([](pthread_cond_t* cond, pthread_mutex_t* mutex) {
1428 return pthread_cond_wait(cond, mutex);
1429 });
Yabin Cui32651b82015-03-13 20:30:00 -07001430 progress = SIGNALED;
Yabin Cuic9a659c2015-11-05 15:36:08 -08001431 ASSERT_EQ(0, pthread_cond_broadcast(&cond));
Narayan Kamath51e6cb32014-03-03 15:38:51 +00001432}
Elliott Hughes0e714a52014-03-03 16:42:47 -08001433
Yabin Cuic9a659c2015-11-05 15:36:08 -08001434TEST_F(pthread_CondWakeupTest, signal_timedwait_CLOCK_REALTIME) {
1435 InitCond(CLOCK_REALTIME);
Elliott Hughes0e714a52014-03-03 16:42:47 -08001436 timespec ts;
1437 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
Yabin Cuic9a659c2015-11-05 15:36:08 -08001438 ts.tv_sec += 1;
1439 StartWaitingThread([&](pthread_cond_t* cond, pthread_mutex_t* mutex) {
1440 return pthread_cond_timedwait(cond, mutex, &ts);
1441 });
1442 progress = SIGNALED;
1443 ASSERT_EQ(0, pthread_cond_signal(&cond));
1444}
Elliott Hughes0e714a52014-03-03 16:42:47 -08001445
Yabin Cuic9a659c2015-11-05 15:36:08 -08001446TEST_F(pthread_CondWakeupTest, signal_timedwait_CLOCK_MONOTONIC) {
1447 InitCond(CLOCK_MONOTONIC);
1448 timespec ts;
1449 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
1450 ts.tv_sec += 1;
1451 StartWaitingThread([&](pthread_cond_t* cond, pthread_mutex_t* mutex) {
1452 return pthread_cond_timedwait(cond, mutex, &ts);
1453 });
1454 progress = SIGNALED;
1455 ASSERT_EQ(0, pthread_cond_signal(&cond));
1456}
Elliott Hughes0e714a52014-03-03 16:42:47 -08001457
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08001458TEST_F(pthread_CondWakeupTest, signal_timedwait_CLOCK_MONOTONIC_np) {
1459#if defined(__BIONIC__)
1460 InitCond(CLOCK_REALTIME);
1461 timespec ts;
1462 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
1463 ts.tv_sec += 1;
1464 StartWaitingThread([&](pthread_cond_t* cond, pthread_mutex_t* mutex) {
1465 return pthread_cond_timedwait_monotonic_np(cond, mutex, &ts);
1466 });
1467 progress = SIGNALED;
1468 ASSERT_EQ(0, pthread_cond_signal(&cond));
1469#else // __BIONIC__
1470 GTEST_LOG_(INFO) << "This test does nothing since pthread_cond_timedwait_monotonic_np is only "
1471 "supported on bionic";
1472#endif // __BIONIC__
1473}
1474
1475static void pthread_cond_timedwait_timeout_helper(clockid_t clock,
1476 int (*wait_function)(pthread_cond_t* __cond,
1477 pthread_mutex_t* __mutex,
1478 const timespec* __timeout)) {
Yabin Cuic9a659c2015-11-05 15:36:08 -08001479 pthread_mutex_t mutex;
1480 ASSERT_EQ(0, pthread_mutex_init(&mutex, nullptr));
1481 pthread_cond_t cond;
1482 ASSERT_EQ(0, pthread_cond_init(&cond, nullptr));
1483 ASSERT_EQ(0, pthread_mutex_lock(&mutex));
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08001484
Yabin Cuic9a659c2015-11-05 15:36:08 -08001485 timespec ts;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08001486 ASSERT_EQ(0, clock_gettime(clock, &ts));
1487 ASSERT_EQ(ETIMEDOUT, wait_function(&cond, &mutex, &ts));
Yabin Cuic9a659c2015-11-05 15:36:08 -08001488 ts.tv_nsec = -1;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08001489 ASSERT_EQ(EINVAL, wait_function(&cond, &mutex, &ts));
Yabin Cuic9a659c2015-11-05 15:36:08 -08001490 ts.tv_nsec = NS_PER_S;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08001491 ASSERT_EQ(EINVAL, wait_function(&cond, &mutex, &ts));
Yabin Cuic9a659c2015-11-05 15:36:08 -08001492 ts.tv_nsec = NS_PER_S - 1;
1493 ts.tv_sec = -1;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08001494 ASSERT_EQ(ETIMEDOUT, wait_function(&cond, &mutex, &ts));
Yabin Cuic9a659c2015-11-05 15:36:08 -08001495 ASSERT_EQ(0, pthread_mutex_unlock(&mutex));
Elliott Hughes0e714a52014-03-03 16:42:47 -08001496}
Elliott Hughes57b7a612014-08-25 17:26:50 -07001497
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08001498TEST(pthread, pthread_cond_timedwait_timeout) {
1499 pthread_cond_timedwait_timeout_helper(CLOCK_REALTIME, pthread_cond_timedwait);
1500}
1501
1502TEST(pthread, pthread_cond_timedwait_monotonic_np_timeout) {
1503#if defined(__BIONIC__)
1504 pthread_cond_timedwait_timeout_helper(CLOCK_MONOTONIC, pthread_cond_timedwait_monotonic_np);
1505#else // __BIONIC__
1506 GTEST_LOG_(INFO) << "This test does nothing since pthread_cond_timedwait_monotonic_np is only "
1507 "supported on bionic";
1508#endif // __BIONIC__
1509}
1510
Elliott Hughes57b7a612014-08-25 17:26:50 -07001511TEST(pthread, pthread_attr_getstack__main_thread) {
1512 // This test is only meaningful for the main thread, so make sure we're running on it!
1513 ASSERT_EQ(getpid(), syscall(__NR_gettid));
1514
1515 // Get the main thread's attributes.
1516 pthread_attr_t attributes;
1517 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1518
1519 // Check that we correctly report that the main thread has no guard page.
1520 size_t guard_size;
1521 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
1522 ASSERT_EQ(0U, guard_size); // The main thread has no guard page.
1523
1524 // Get the stack base and the stack size (both ways).
1525 void* stack_base;
1526 size_t stack_size;
1527 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1528 size_t stack_size2;
1529 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1530
1531 // The two methods of asking for the stack size should agree.
1532 EXPECT_EQ(stack_size, stack_size2);
1533
Yabin Cuib0c6f2db2015-05-19 15:09:23 -07001534#if defined(__BIONIC__)
dimitry6dfa5b52018-01-30 13:24:28 +01001535 // Find stack in /proc/self/maps using a pointer to the stack.
1536 //
1537 // We do not use "[stack]" label because in native-bridge environment it is not
1538 // guaranteed to point to the right stack. A native bridge implementation may
1539 // keep separate stack for the guest code.
Yi Kong32bc0fc2018-08-02 17:31:13 -07001540 void* maps_stack_hi = nullptr;
Elliott Hughes15dfd632015-09-22 16:40:14 -07001541 std::vector<map_record> maps;
1542 ASSERT_TRUE(Maps::parse_maps(&maps));
dimitry6dfa5b52018-01-30 13:24:28 +01001543 uintptr_t stack_address = reinterpret_cast<uintptr_t>(&maps_stack_hi);
Elliott Hughes0b2acdf2015-10-02 18:25:19 -07001544 for (const auto& map : maps) {
dimitry6dfa5b52018-01-30 13:24:28 +01001545 if (map.addr_start <= stack_address && map.addr_end > stack_address){
Elliott Hughes15dfd632015-09-22 16:40:14 -07001546 maps_stack_hi = reinterpret_cast<void*>(map.addr_end);
Elliott Hughes57b7a612014-08-25 17:26:50 -07001547 break;
1548 }
1549 }
Elliott Hughes57b7a612014-08-25 17:26:50 -07001550
dimitry6dfa5b52018-01-30 13:24:28 +01001551 // The high address of the /proc/self/maps stack region should equal stack_base + stack_size.
Yabin Cuib0c6f2db2015-05-19 15:09:23 -07001552 // Remember that the stack grows down (and is mapped in on demand), so the low address of the
1553 // region isn't very interesting.
1554 EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size);
1555
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001556 // The stack size should correspond to RLIMIT_STACK.
Elliott Hughes57b7a612014-08-25 17:26:50 -07001557 rlimit rl;
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001558 ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl));
Elliott Hughes27a9aed2014-09-04 16:09:25 -07001559 uint64_t original_rlim_cur = rl.rlim_cur;
Elliott Hughes27a9aed2014-09-04 16:09:25 -07001560 if (rl.rlim_cur == RLIM_INFINITY) {
1561 rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB.
1562 }
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001563 EXPECT_EQ(rl.rlim_cur, stack_size);
1564
Tom Cherryb8ab6182017-04-05 16:20:29 -07001565 auto guard = android::base::make_scope_guard([&rl, original_rlim_cur]() {
Elliott Hughes27a9aed2014-09-04 16:09:25 -07001566 rl.rlim_cur = original_rlim_cur;
1567 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1568 });
1569
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001570 //
1571 // What if RLIMIT_STACK is smaller than the stack's current extent?
1572 //
Elliott Hughes57b7a612014-08-25 17:26:50 -07001573 rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already.
1574 rl.rlim_max = RLIM_INFINITY;
1575 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1576
1577 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1578 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1579 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1580
1581 EXPECT_EQ(stack_size, stack_size2);
1582 ASSERT_EQ(1024U, stack_size);
1583
1584 //
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001585 // What if RLIMIT_STACK isn't a whole number of pages?
Elliott Hughes57b7a612014-08-25 17:26:50 -07001586 //
1587 rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages.
1588 rl.rlim_max = RLIM_INFINITY;
1589 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1590
1591 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1592 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1593 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1594
1595 EXPECT_EQ(stack_size, stack_size2);
1596 ASSERT_EQ(6666U, stack_size);
Yabin Cuib0c6f2db2015-05-19 15:09:23 -07001597#endif
Elliott Hughes57b7a612014-08-25 17:26:50 -07001598}
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001599
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001600struct GetStackSignalHandlerArg {
1601 volatile bool done;
Chih-Hung Hsieh9af13d22016-06-02 14:40:09 -07001602 void* signal_stack_base;
1603 size_t signal_stack_size;
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001604 void* main_stack_base;
1605 size_t main_stack_size;
1606};
1607
1608static GetStackSignalHandlerArg getstack_signal_handler_arg;
1609
1610static void getstack_signal_handler(int sig) {
1611 ASSERT_EQ(SIGUSR1, sig);
1612 // Use sleep() to make current thread be switched out by the kernel to provoke the error.
1613 sleep(1);
1614 pthread_attr_t attr;
1615 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attr));
1616 void* stack_base;
1617 size_t stack_size;
1618 ASSERT_EQ(0, pthread_attr_getstack(&attr, &stack_base, &stack_size));
Chih-Hung Hsieh9af13d22016-06-02 14:40:09 -07001619
1620 // Verify if the stack used by the signal handler is the alternate stack just registered.
1621 ASSERT_LE(getstack_signal_handler_arg.signal_stack_base, &attr);
1622 ASSERT_LT(static_cast<void*>(&attr),
1623 static_cast<char*>(getstack_signal_handler_arg.signal_stack_base) +
1624 getstack_signal_handler_arg.signal_stack_size);
1625
1626 // Verify if the main thread's stack got in the signal handler is correct.
1627 ASSERT_EQ(getstack_signal_handler_arg.main_stack_base, stack_base);
1628 ASSERT_LE(getstack_signal_handler_arg.main_stack_size, stack_size);
1629
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001630 getstack_signal_handler_arg.done = true;
1631}
1632
1633// The previous code obtained the main thread's stack by reading the entry in
1634// /proc/self/task/<pid>/maps that was labeled [stack]. Unfortunately, on x86/x86_64, the kernel
1635// relies on sp0 in task state segment(tss) to label the stack map with [stack]. If the kernel
1636// switches a process while the main thread is in an alternate stack, then the kernel will label
1637// the wrong map with [stack]. This test verifies that when the above situation happens, the main
1638// thread's stack is found correctly.
1639TEST(pthread, pthread_attr_getstack_in_signal_handler) {
Yabin Cui61e4d462016-03-07 17:44:58 -08001640 // This test is only meaningful for the main thread, so make sure we're running on it!
1641 ASSERT_EQ(getpid(), syscall(__NR_gettid));
1642
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001643 const size_t sig_stack_size = 16 * 1024;
Yi Kong32bc0fc2018-08-02 17:31:13 -07001644 void* sig_stack = mmap(nullptr, sig_stack_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001645 -1, 0);
1646 ASSERT_NE(MAP_FAILED, sig_stack);
1647 stack_t ss;
1648 ss.ss_sp = sig_stack;
1649 ss.ss_size = sig_stack_size;
1650 ss.ss_flags = 0;
1651 stack_t oss;
1652 ASSERT_EQ(0, sigaltstack(&ss, &oss));
1653
Yabin Cui61e4d462016-03-07 17:44:58 -08001654 pthread_attr_t attr;
1655 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attr));
1656 void* main_stack_base;
1657 size_t main_stack_size;
1658 ASSERT_EQ(0, pthread_attr_getstack(&attr, &main_stack_base, &main_stack_size));
1659
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001660 ScopedSignalHandler handler(SIGUSR1, getstack_signal_handler, SA_ONSTACK);
1661 getstack_signal_handler_arg.done = false;
Chih-Hung Hsieh9af13d22016-06-02 14:40:09 -07001662 getstack_signal_handler_arg.signal_stack_base = sig_stack;
1663 getstack_signal_handler_arg.signal_stack_size = sig_stack_size;
1664 getstack_signal_handler_arg.main_stack_base = main_stack_base;
1665 getstack_signal_handler_arg.main_stack_size = main_stack_size;
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001666 kill(getpid(), SIGUSR1);
1667 ASSERT_EQ(true, getstack_signal_handler_arg.done);
1668
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001669 ASSERT_EQ(0, sigaltstack(&oss, nullptr));
1670 ASSERT_EQ(0, munmap(sig_stack, sig_stack_size));
1671}
1672
Yabin Cui917d3902015-01-08 12:32:42 -08001673static void pthread_attr_getstack_18908062_helper(void*) {
1674 char local_variable;
1675 pthread_attr_t attributes;
1676 pthread_getattr_np(pthread_self(), &attributes);
1677 void* stack_base;
1678 size_t stack_size;
1679 pthread_attr_getstack(&attributes, &stack_base, &stack_size);
1680
1681 // Test whether &local_variable is in [stack_base, stack_base + stack_size).
1682 ASSERT_LE(reinterpret_cast<char*>(stack_base), &local_variable);
1683 ASSERT_LT(&local_variable, reinterpret_cast<char*>(stack_base) + stack_size);
1684}
1685
1686// Check whether something on stack is in the range of
1687// [stack_base, stack_base + stack_size). see b/18908062.
1688TEST(pthread, pthread_attr_getstack_18908062) {
1689 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -07001690 ASSERT_EQ(0, pthread_create(&t, nullptr,
Yabin Cui917d3902015-01-08 12:32:42 -08001691 reinterpret_cast<void* (*)(void*)>(pthread_attr_getstack_18908062_helper),
Yi Kong32bc0fc2018-08-02 17:31:13 -07001692 nullptr));
1693 ASSERT_EQ(0, pthread_join(t, nullptr));
Yabin Cui917d3902015-01-08 12:32:42 -08001694}
1695
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001696#if defined(__BIONIC__)
Elliott Hughesf2083612015-11-11 13:32:28 -08001697static pthread_mutex_t pthread_gettid_np_mutex = PTHREAD_MUTEX_INITIALIZER;
1698
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001699static void* pthread_gettid_np_helper(void* arg) {
1700 *reinterpret_cast<pid_t*>(arg) = gettid();
Elliott Hughesf2083612015-11-11 13:32:28 -08001701
1702 // Wait for our parent to call pthread_gettid_np on us before exiting.
1703 pthread_mutex_lock(&pthread_gettid_np_mutex);
1704 pthread_mutex_unlock(&pthread_gettid_np_mutex);
Yi Kong32bc0fc2018-08-02 17:31:13 -07001705 return nullptr;
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001706}
1707#endif
1708
1709TEST(pthread, pthread_gettid_np) {
1710#if defined(__BIONIC__)
1711 ASSERT_EQ(gettid(), pthread_gettid_np(pthread_self()));
1712
Elliott Hughesf2083612015-11-11 13:32:28 -08001713 // Ensure the other thread doesn't exit until after we've called
1714 // pthread_gettid_np on it.
1715 pthread_mutex_lock(&pthread_gettid_np_mutex);
1716
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001717 pid_t t_gettid_result;
1718 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -07001719 pthread_create(&t, nullptr, pthread_gettid_np_helper, &t_gettid_result);
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001720
1721 pid_t t_pthread_gettid_np_result = pthread_gettid_np(t);
1722
Elliott Hughesf2083612015-11-11 13:32:28 -08001723 // Release the other thread and wait for it to exit.
1724 pthread_mutex_unlock(&pthread_gettid_np_mutex);
Yi Kong32bc0fc2018-08-02 17:31:13 -07001725 ASSERT_EQ(0, pthread_join(t, nullptr));
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001726
1727 ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result);
1728#else
1729 GTEST_LOG_(INFO) << "This test does nothing.\n";
1730#endif
1731}
Elliott Hughes34c987a2014-09-22 16:01:26 -07001732
1733static size_t cleanup_counter = 0;
1734
Derek Xue41996952014-09-25 11:05:32 +01001735static void AbortCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001736 abort();
1737}
1738
Derek Xue41996952014-09-25 11:05:32 +01001739static void CountCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001740 ++cleanup_counter;
1741}
1742
Derek Xue41996952014-09-25 11:05:32 +01001743static void PthreadCleanupTester() {
Yi Kong32bc0fc2018-08-02 17:31:13 -07001744 pthread_cleanup_push(CountCleanupRoutine, nullptr);
1745 pthread_cleanup_push(CountCleanupRoutine, nullptr);
1746 pthread_cleanup_push(AbortCleanupRoutine, nullptr);
Elliott Hughes34c987a2014-09-22 16:01:26 -07001747
1748 pthread_cleanup_pop(0); // Pop the abort without executing it.
1749 pthread_cleanup_pop(1); // Pop one count while executing it.
1750 ASSERT_EQ(1U, cleanup_counter);
1751 // Exit while the other count is still on the cleanup stack.
Yi Kong32bc0fc2018-08-02 17:31:13 -07001752 pthread_exit(nullptr);
Elliott Hughes34c987a2014-09-22 16:01:26 -07001753
1754 // Calls to pthread_cleanup_pop/pthread_cleanup_push must always be balanced.
1755 pthread_cleanup_pop(0);
1756}
1757
Derek Xue41996952014-09-25 11:05:32 +01001758static void* PthreadCleanupStartRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001759 PthreadCleanupTester();
Yi Kong32bc0fc2018-08-02 17:31:13 -07001760 return nullptr;
Elliott Hughes34c987a2014-09-22 16:01:26 -07001761}
1762
1763TEST(pthread, pthread_cleanup_push__pthread_cleanup_pop) {
1764 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -07001765 ASSERT_EQ(0, pthread_create(&t, nullptr, PthreadCleanupStartRoutine, nullptr));
1766 ASSERT_EQ(0, pthread_join(t, nullptr));
Elliott Hughes34c987a2014-09-22 16:01:26 -07001767 ASSERT_EQ(2U, cleanup_counter);
1768}
Derek Xue41996952014-09-25 11:05:32 +01001769
1770TEST(pthread, PTHREAD_MUTEX_DEFAULT_is_PTHREAD_MUTEX_NORMAL) {
1771 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_DEFAULT);
1772}
1773
1774TEST(pthread, pthread_mutexattr_gettype) {
1775 pthread_mutexattr_t attr;
1776 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1777
1778 int attr_type;
1779
1780 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL));
1781 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1782 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, attr_type);
1783
1784 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
1785 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1786 ASSERT_EQ(PTHREAD_MUTEX_ERRORCHECK, attr_type);
1787
1788 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE));
1789 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1790 ASSERT_EQ(PTHREAD_MUTEX_RECURSIVE, attr_type);
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001791
1792 ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
1793}
1794
Yabin Cui6b9c85b2018-01-23 12:56:18 -08001795TEST(pthread, pthread_mutexattr_protocol) {
1796 pthread_mutexattr_t attr;
1797 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1798
1799 int protocol;
1800 ASSERT_EQ(0, pthread_mutexattr_getprotocol(&attr, &protocol));
1801 ASSERT_EQ(PTHREAD_PRIO_NONE, protocol);
1802 for (size_t repeat = 0; repeat < 2; ++repeat) {
1803 for (int set_protocol : {PTHREAD_PRIO_NONE, PTHREAD_PRIO_INHERIT}) {
1804 ASSERT_EQ(0, pthread_mutexattr_setprotocol(&attr, set_protocol));
1805 ASSERT_EQ(0, pthread_mutexattr_getprotocol(&attr, &protocol));
1806 ASSERT_EQ(protocol, set_protocol);
1807 }
1808 }
1809}
1810
Yabin Cui17393b02015-03-21 15:08:25 -07001811struct PthreadMutex {
1812 pthread_mutex_t lock;
1813
Yabin Cui6b9c85b2018-01-23 12:56:18 -08001814 explicit PthreadMutex(int mutex_type, int protocol = PTHREAD_PRIO_NONE) {
1815 init(mutex_type, protocol);
Yabin Cui17393b02015-03-21 15:08:25 -07001816 }
1817
1818 ~PthreadMutex() {
1819 destroy();
1820 }
1821
1822 private:
Yabin Cui6b9c85b2018-01-23 12:56:18 -08001823 void init(int mutex_type, int protocol) {
Yabin Cui17393b02015-03-21 15:08:25 -07001824 pthread_mutexattr_t attr;
1825 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1826 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, mutex_type));
Yabin Cui6b9c85b2018-01-23 12:56:18 -08001827 ASSERT_EQ(0, pthread_mutexattr_setprotocol(&attr, protocol));
Yabin Cui17393b02015-03-21 15:08:25 -07001828 ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
1829 ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
1830 }
1831
1832 void destroy() {
1833 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1834 }
1835
1836 DISALLOW_COPY_AND_ASSIGN(PthreadMutex);
1837};
Derek Xue41996952014-09-25 11:05:32 +01001838
Yabin Cui6b9c85b2018-01-23 12:56:18 -08001839static void TestPthreadMutexLockNormal(int protocol) {
1840 PthreadMutex m(PTHREAD_MUTEX_NORMAL, protocol);
Derek Xue41996952014-09-25 11:05:32 +01001841
Yabin Cui17393b02015-03-21 15:08:25 -07001842 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
1843 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
Elliott Hughesd31d4c12015-12-14 17:35:10 -08001844 ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
1845 ASSERT_EQ(EBUSY, pthread_mutex_trylock(&m.lock));
1846 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
Derek Xue41996952014-09-25 11:05:32 +01001847}
1848
Yabin Cui6b9c85b2018-01-23 12:56:18 -08001849static void TestPthreadMutexLockErrorCheck(int protocol) {
1850 PthreadMutex m(PTHREAD_MUTEX_ERRORCHECK, protocol);
Derek Xue41996952014-09-25 11:05:32 +01001851
Yabin Cui17393b02015-03-21 15:08:25 -07001852 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
1853 ASSERT_EQ(EDEADLK, pthread_mutex_lock(&m.lock));
1854 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1855 ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
Yabin Cui6b9c85b2018-01-23 12:56:18 -08001856 if (protocol == PTHREAD_PRIO_NONE) {
1857 ASSERT_EQ(EBUSY, pthread_mutex_trylock(&m.lock));
1858 } else {
1859 ASSERT_EQ(EDEADLK, pthread_mutex_trylock(&m.lock));
1860 }
Yabin Cui17393b02015-03-21 15:08:25 -07001861 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1862 ASSERT_EQ(EPERM, pthread_mutex_unlock(&m.lock));
Derek Xue41996952014-09-25 11:05:32 +01001863}
1864
Yabin Cui6b9c85b2018-01-23 12:56:18 -08001865static void TestPthreadMutexLockRecursive(int protocol) {
1866 PthreadMutex m(PTHREAD_MUTEX_RECURSIVE, protocol);
Derek Xue41996952014-09-25 11:05:32 +01001867
Yabin Cui17393b02015-03-21 15:08:25 -07001868 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
1869 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
1870 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1871 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1872 ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
Elliott Hughesd31d4c12015-12-14 17:35:10 -08001873 ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
1874 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
Yabin Cui17393b02015-03-21 15:08:25 -07001875 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1876 ASSERT_EQ(EPERM, pthread_mutex_unlock(&m.lock));
1877}
1878
Yabin Cui6b9c85b2018-01-23 12:56:18 -08001879TEST(pthread, pthread_mutex_lock_NORMAL) {
1880 TestPthreadMutexLockNormal(PTHREAD_PRIO_NONE);
1881}
1882
1883TEST(pthread, pthread_mutex_lock_ERRORCHECK) {
1884 TestPthreadMutexLockErrorCheck(PTHREAD_PRIO_NONE);
1885}
1886
1887TEST(pthread, pthread_mutex_lock_RECURSIVE) {
1888 TestPthreadMutexLockRecursive(PTHREAD_PRIO_NONE);
1889}
1890
1891TEST(pthread, pthread_mutex_lock_pi) {
Yabin Cui6b9c85b2018-01-23 12:56:18 -08001892 TestPthreadMutexLockNormal(PTHREAD_PRIO_INHERIT);
1893 TestPthreadMutexLockErrorCheck(PTHREAD_PRIO_INHERIT);
1894 TestPthreadMutexLockRecursive(PTHREAD_PRIO_INHERIT);
1895}
1896
Yabin Cui5a00ba72018-01-26 17:32:31 -08001897TEST(pthread, pthread_mutex_pi_count_limit) {
1898#if defined(__BIONIC__) && !defined(__LP64__)
1899 // Bionic only supports 65536 pi mutexes in 32-bit programs.
1900 pthread_mutexattr_t attr;
1901 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1902 ASSERT_EQ(0, pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT));
1903 std::vector<pthread_mutex_t> mutexes(65536);
1904 // Test if we can use 65536 pi mutexes at the same time.
1905 // Run 2 times to check if freed pi mutexes can be recycled.
1906 for (int repeat = 0; repeat < 2; ++repeat) {
1907 for (auto& m : mutexes) {
1908 ASSERT_EQ(0, pthread_mutex_init(&m, &attr));
1909 }
1910 pthread_mutex_t m;
1911 ASSERT_EQ(ENOMEM, pthread_mutex_init(&m, &attr));
1912 for (auto& m : mutexes) {
1913 ASSERT_EQ(0, pthread_mutex_lock(&m));
1914 }
1915 for (auto& m : mutexes) {
1916 ASSERT_EQ(0, pthread_mutex_unlock(&m));
1917 }
1918 for (auto& m : mutexes) {
1919 ASSERT_EQ(0, pthread_mutex_destroy(&m));
1920 }
1921 }
1922 ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
1923#else
1924 GTEST_LOG_(INFO) << "This test does nothing as pi mutex count isn't limited.\n";
1925#endif
1926}
1927
Yabin Cui17393b02015-03-21 15:08:25 -07001928TEST(pthread, pthread_mutex_init_same_as_static_initializers) {
1929 pthread_mutex_t lock_normal = PTHREAD_MUTEX_INITIALIZER;
1930 PthreadMutex m1(PTHREAD_MUTEX_NORMAL);
1931 ASSERT_EQ(0, memcmp(&lock_normal, &m1.lock, sizeof(pthread_mutex_t)));
1932 pthread_mutex_destroy(&lock_normal);
1933
1934 pthread_mutex_t lock_errorcheck = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
1935 PthreadMutex m2(PTHREAD_MUTEX_ERRORCHECK);
1936 ASSERT_EQ(0, memcmp(&lock_errorcheck, &m2.lock, sizeof(pthread_mutex_t)));
1937 pthread_mutex_destroy(&lock_errorcheck);
1938
1939 pthread_mutex_t lock_recursive = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
1940 PthreadMutex m3(PTHREAD_MUTEX_RECURSIVE);
1941 ASSERT_EQ(0, memcmp(&lock_recursive, &m3.lock, sizeof(pthread_mutex_t)));
1942 ASSERT_EQ(0, pthread_mutex_destroy(&lock_recursive));
Derek Xue41996952014-09-25 11:05:32 +01001943}
Yabin Cui5a00ba72018-01-26 17:32:31 -08001944
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001945class MutexWakeupHelper {
1946 private:
Yabin Cui17393b02015-03-21 15:08:25 -07001947 PthreadMutex m;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001948 enum Progress {
1949 LOCK_INITIALIZED,
1950 LOCK_WAITING,
1951 LOCK_RELEASED,
1952 LOCK_ACCESSED
1953 };
1954 std::atomic<Progress> progress;
Yabin Cuif7969852015-04-02 17:47:48 -07001955 std::atomic<pid_t> tid;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001956
1957 static void thread_fn(MutexWakeupHelper* helper) {
Yabin Cuif7969852015-04-02 17:47:48 -07001958 helper->tid = gettid();
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001959 ASSERT_EQ(LOCK_INITIALIZED, helper->progress);
1960 helper->progress = LOCK_WAITING;
1961
Yabin Cui17393b02015-03-21 15:08:25 -07001962 ASSERT_EQ(0, pthread_mutex_lock(&helper->m.lock));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001963 ASSERT_EQ(LOCK_RELEASED, helper->progress);
Yabin Cui17393b02015-03-21 15:08:25 -07001964 ASSERT_EQ(0, pthread_mutex_unlock(&helper->m.lock));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001965
1966 helper->progress = LOCK_ACCESSED;
1967 }
1968
1969 public:
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -07001970 explicit MutexWakeupHelper(int mutex_type) : m(mutex_type) {
Yabin Cui17393b02015-03-21 15:08:25 -07001971 }
1972
1973 void test() {
1974 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001975 progress = LOCK_INITIALIZED;
Yabin Cuif7969852015-04-02 17:47:48 -07001976 tid = 0;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001977
1978 pthread_t thread;
Yi Kong32bc0fc2018-08-02 17:31:13 -07001979 ASSERT_EQ(0, pthread_create(&thread, nullptr,
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001980 reinterpret_cast<void* (*)(void*)>(MutexWakeupHelper::thread_fn), this));
1981
Yabin Cuif7969852015-04-02 17:47:48 -07001982 WaitUntilThreadSleep(tid);
1983 ASSERT_EQ(LOCK_WAITING, progress);
1984
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001985 progress = LOCK_RELEASED;
Yabin Cui17393b02015-03-21 15:08:25 -07001986 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001987
Yi Kong32bc0fc2018-08-02 17:31:13 -07001988 ASSERT_EQ(0, pthread_join(thread, nullptr));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001989 ASSERT_EQ(LOCK_ACCESSED, progress);
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001990 }
1991};
1992
1993TEST(pthread, pthread_mutex_NORMAL_wakeup) {
Yabin Cui17393b02015-03-21 15:08:25 -07001994 MutexWakeupHelper helper(PTHREAD_MUTEX_NORMAL);
1995 helper.test();
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001996}
1997
1998TEST(pthread, pthread_mutex_ERRORCHECK_wakeup) {
Yabin Cui17393b02015-03-21 15:08:25 -07001999 MutexWakeupHelper helper(PTHREAD_MUTEX_ERRORCHECK);
2000 helper.test();
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08002001}
2002
2003TEST(pthread, pthread_mutex_RECURSIVE_wakeup) {
Yabin Cui17393b02015-03-21 15:08:25 -07002004 MutexWakeupHelper helper(PTHREAD_MUTEX_RECURSIVE);
2005 helper.test();
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08002006}
2007
Yabin Cui6b9c85b2018-01-23 12:56:18 -08002008static int GetThreadPriority(pid_t tid) {
2009 // sched_getparam() returns the static priority of a thread, which can't reflect a thread's
2010 // priority after priority inheritance. So read /proc/<pid>/stat to get the dynamic priority.
2011 std::string filename = android::base::StringPrintf("/proc/%d/stat", tid);
2012 std::string content;
2013 int result = INT_MAX;
2014 if (!android::base::ReadFileToString(filename, &content)) {
2015 return result;
2016 }
2017 std::vector<std::string> strs = android::base::Split(content, " ");
2018 if (strs.size() < 18) {
2019 return result;
2020 }
2021 if (!android::base::ParseInt(strs[17], &result)) {
2022 return INT_MAX;
2023 }
2024 return result;
2025}
2026
2027class PIMutexWakeupHelper {
2028private:
2029 PthreadMutex m;
2030 int protocol;
2031 enum Progress {
2032 LOCK_INITIALIZED,
2033 LOCK_CHILD_READY,
2034 LOCK_WAITING,
2035 LOCK_RELEASED,
2036 };
2037 std::atomic<Progress> progress;
2038 std::atomic<pid_t> main_tid;
2039 std::atomic<pid_t> child_tid;
2040 PthreadMutex start_thread_m;
2041
2042 static void thread_fn(PIMutexWakeupHelper* helper) {
2043 helper->child_tid = gettid();
2044 ASSERT_EQ(LOCK_INITIALIZED, helper->progress);
2045 ASSERT_EQ(0, setpriority(PRIO_PROCESS, gettid(), 1));
2046 ASSERT_EQ(21, GetThreadPriority(gettid()));
2047 ASSERT_EQ(0, pthread_mutex_lock(&helper->m.lock));
2048 helper->progress = LOCK_CHILD_READY;
2049 ASSERT_EQ(0, pthread_mutex_lock(&helper->start_thread_m.lock));
2050
2051 ASSERT_EQ(0, pthread_mutex_unlock(&helper->start_thread_m.lock));
2052 WaitUntilThreadSleep(helper->main_tid);
2053 ASSERT_EQ(LOCK_WAITING, helper->progress);
2054
2055 if (helper->protocol == PTHREAD_PRIO_INHERIT) {
2056 ASSERT_EQ(20, GetThreadPriority(gettid()));
2057 } else {
2058 ASSERT_EQ(21, GetThreadPriority(gettid()));
2059 }
2060 helper->progress = LOCK_RELEASED;
2061 ASSERT_EQ(0, pthread_mutex_unlock(&helper->m.lock));
2062 }
2063
2064public:
2065 explicit PIMutexWakeupHelper(int mutex_type, int protocol)
2066 : m(mutex_type, protocol), protocol(protocol), start_thread_m(PTHREAD_MUTEX_NORMAL) {
2067 }
2068
2069 void test() {
2070 ASSERT_EQ(0, pthread_mutex_lock(&start_thread_m.lock));
2071 main_tid = gettid();
2072 ASSERT_EQ(20, GetThreadPriority(main_tid));
2073 progress = LOCK_INITIALIZED;
2074 child_tid = 0;
2075
2076 pthread_t thread;
Yi Kong32bc0fc2018-08-02 17:31:13 -07002077 ASSERT_EQ(0, pthread_create(&thread, nullptr,
Yabin Cui6b9c85b2018-01-23 12:56:18 -08002078 reinterpret_cast<void* (*)(void*)>(PIMutexWakeupHelper::thread_fn), this));
2079
2080 WaitUntilThreadSleep(child_tid);
2081 ASSERT_EQ(LOCK_CHILD_READY, progress);
2082 ASSERT_EQ(0, pthread_mutex_unlock(&start_thread_m.lock));
2083 progress = LOCK_WAITING;
2084 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
2085
2086 ASSERT_EQ(LOCK_RELEASED, progress);
2087 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
2088 ASSERT_EQ(0, pthread_join(thread, nullptr));
2089 }
2090};
2091
2092TEST(pthread, pthread_mutex_pi_wakeup) {
Yabin Cui6b9c85b2018-01-23 12:56:18 -08002093 for (int type : {PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_RECURSIVE, PTHREAD_MUTEX_ERRORCHECK}) {
2094 for (int protocol : {PTHREAD_PRIO_INHERIT}) {
2095 PIMutexWakeupHelper helper(type, protocol);
2096 helper.test();
2097 }
2098 }
2099}
2100
Yabin Cui140f3672015-02-03 10:32:00 -08002101TEST(pthread, pthread_mutex_owner_tid_limit) {
Yabin Cuie69c2452015-02-13 16:21:25 -08002102#if defined(__BIONIC__) && !defined(__LP64__)
Yabin Cui140f3672015-02-03 10:32:00 -08002103 FILE* fp = fopen("/proc/sys/kernel/pid_max", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07002104 ASSERT_TRUE(fp != nullptr);
Yabin Cui140f3672015-02-03 10:32:00 -08002105 long pid_max;
2106 ASSERT_EQ(1, fscanf(fp, "%ld", &pid_max));
2107 fclose(fp);
Yabin Cuie69c2452015-02-13 16:21:25 -08002108 // Bionic's pthread_mutex implementation on 32-bit devices uses 16 bits to represent owner tid.
Yabin Cui140f3672015-02-03 10:32:00 -08002109 ASSERT_LE(pid_max, 65536);
Yabin Cuie69c2452015-02-13 16:21:25 -08002110#else
2111 GTEST_LOG_(INFO) << "This test does nothing as 32-bit tid is supported by pthread_mutex.\n";
2112#endif
Yabin Cui140f3672015-02-03 10:32:00 -08002113}
Yabin Cuib5845722015-03-16 22:46:42 -07002114
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08002115static void pthread_mutex_timedlock_helper(clockid_t clock,
2116 int (*lock_function)(pthread_mutex_t* __mutex,
2117 const timespec* __timeout)) {
Yabin Cuic9a659c2015-11-05 15:36:08 -08002118 pthread_mutex_t m;
2119 ASSERT_EQ(0, pthread_mutex_init(&m, nullptr));
2120
2121 // If the mutex is already locked, pthread_mutex_timedlock should time out.
2122 ASSERT_EQ(0, pthread_mutex_lock(&m));
2123
2124 timespec ts;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08002125 ASSERT_EQ(0, clock_gettime(clock, &ts));
2126 ASSERT_EQ(ETIMEDOUT, lock_function(&m, &ts));
Yabin Cuic9a659c2015-11-05 15:36:08 -08002127 ts.tv_nsec = -1;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08002128 ASSERT_EQ(EINVAL, lock_function(&m, &ts));
Yabin Cuic9a659c2015-11-05 15:36:08 -08002129 ts.tv_nsec = NS_PER_S;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08002130 ASSERT_EQ(EINVAL, lock_function(&m, &ts));
Yabin Cuic9a659c2015-11-05 15:36:08 -08002131 ts.tv_nsec = NS_PER_S - 1;
2132 ts.tv_sec = -1;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08002133 ASSERT_EQ(ETIMEDOUT, lock_function(&m, &ts));
Yabin Cuic9a659c2015-11-05 15:36:08 -08002134
2135 // If the mutex is unlocked, pthread_mutex_timedlock should succeed.
2136 ASSERT_EQ(0, pthread_mutex_unlock(&m));
2137
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08002138 ASSERT_EQ(0, clock_gettime(clock, &ts));
Yabin Cuic9a659c2015-11-05 15:36:08 -08002139 ts.tv_sec += 1;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08002140 ASSERT_EQ(0, lock_function(&m, &ts));
Yabin Cuic9a659c2015-11-05 15:36:08 -08002141
2142 ASSERT_EQ(0, pthread_mutex_unlock(&m));
2143 ASSERT_EQ(0, pthread_mutex_destroy(&m));
2144}
2145
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08002146TEST(pthread, pthread_mutex_timedlock) {
2147 pthread_mutex_timedlock_helper(CLOCK_REALTIME, pthread_mutex_timedlock);
2148}
2149
2150TEST(pthread, pthread_mutex_timedlock_monotonic_np) {
2151#if defined(__BIONIC__)
2152 pthread_mutex_timedlock_helper(CLOCK_MONOTONIC, pthread_mutex_timedlock_monotonic_np);
2153#else // __BIONIC__
2154 GTEST_LOG_(INFO) << "This test does nothing since pthread_mutex_timedlock_monotonic_np is only "
2155 "supported on bionic";
2156#endif // __BIONIC__
2157}
2158
2159static void pthread_mutex_timedlock_pi_helper(clockid_t clock,
2160 int (*lock_function)(pthread_mutex_t* __mutex,
2161 const timespec* __timeout)) {
Yabin Cui6b9c85b2018-01-23 12:56:18 -08002162 PthreadMutex m(PTHREAD_MUTEX_NORMAL, PTHREAD_PRIO_INHERIT);
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08002163
Yabin Cui6b9c85b2018-01-23 12:56:18 -08002164 timespec ts;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08002165 clock_gettime(clock, &ts);
Yabin Cui6b9c85b2018-01-23 12:56:18 -08002166 ts.tv_sec += 1;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08002167 ASSERT_EQ(0, lock_function(&m.lock, &ts));
2168
2169 struct ThreadArgs {
2170 clockid_t clock;
2171 int (*lock_function)(pthread_mutex_t* __mutex, const timespec* __timeout);
2172 PthreadMutex& m;
2173 };
2174
2175 ThreadArgs thread_args = {
2176 .clock = clock,
2177 .lock_function = lock_function,
2178 .m = m,
2179 };
Yabin Cui6b9c85b2018-01-23 12:56:18 -08002180
2181 auto ThreadFn = [](void* arg) -> void* {
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08002182 auto args = static_cast<ThreadArgs*>(arg);
Yabin Cui6b9c85b2018-01-23 12:56:18 -08002183 timespec ts;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08002184 clock_gettime(args->clock, &ts);
Yabin Cui6b9c85b2018-01-23 12:56:18 -08002185 ts.tv_sec += 1;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08002186 intptr_t result = args->lock_function(&args->m.lock, &ts);
Yabin Cui6b9c85b2018-01-23 12:56:18 -08002187 return reinterpret_cast<void*>(result);
2188 };
2189
2190 pthread_t thread;
Yi Kong32bc0fc2018-08-02 17:31:13 -07002191 ASSERT_EQ(0, pthread_create(&thread, nullptr, ThreadFn, &thread_args));
Yabin Cui6b9c85b2018-01-23 12:56:18 -08002192 void* result;
2193 ASSERT_EQ(0, pthread_join(thread, &result));
2194 ASSERT_EQ(ETIMEDOUT, reinterpret_cast<intptr_t>(result));
2195 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
2196}
2197
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08002198TEST(pthread, pthread_mutex_timedlock_pi) {
2199 pthread_mutex_timedlock_pi_helper(CLOCK_REALTIME, pthread_mutex_timedlock);
2200}
2201
2202TEST(pthread, pthread_mutex_timedlock_monotonic_np_pi) {
2203#if defined(__BIONIC__)
2204 pthread_mutex_timedlock_pi_helper(CLOCK_MONOTONIC, pthread_mutex_timedlock_monotonic_np);
2205#else // __BIONIC__
2206 GTEST_LOG_(INFO) << "This test does nothing since pthread_mutex_timedlock_monotonic_np is only "
2207 "supported on bionic";
2208#endif // __BIONIC__
2209}
2210
Yabin Cui9651fdf2018-03-14 12:02:21 -07002211TEST(pthread, pthread_mutex_using_destroyed_mutex) {
2212#if defined(__BIONIC__)
2213 pthread_mutex_t m;
2214 ASSERT_EQ(0, pthread_mutex_init(&m, nullptr));
2215 ASSERT_EQ(0, pthread_mutex_destroy(&m));
2216 ASSERT_EXIT(pthread_mutex_lock(&m), ::testing::KilledBySignal(SIGABRT),
2217 "pthread_mutex_lock called on a destroyed mutex");
2218 ASSERT_EXIT(pthread_mutex_unlock(&m), ::testing::KilledBySignal(SIGABRT),
2219 "pthread_mutex_unlock called on a destroyed mutex");
2220 ASSERT_EXIT(pthread_mutex_trylock(&m), ::testing::KilledBySignal(SIGABRT),
2221 "pthread_mutex_trylock called on a destroyed mutex");
2222 timespec ts;
2223 ASSERT_EXIT(pthread_mutex_timedlock(&m, &ts), ::testing::KilledBySignal(SIGABRT),
2224 "pthread_mutex_timedlock called on a destroyed mutex");
Tom Cherryc6b5bcd2018-03-05 14:14:44 -08002225 ASSERT_EXIT(pthread_mutex_timedlock_monotonic_np(&m, &ts), ::testing::KilledBySignal(SIGABRT),
2226 "pthread_mutex_timedlock_monotonic_np called on a destroyed mutex");
Yabin Cui9651fdf2018-03-14 12:02:21 -07002227 ASSERT_EXIT(pthread_mutex_destroy(&m), ::testing::KilledBySignal(SIGABRT),
2228 "pthread_mutex_destroy called on a destroyed mutex");
2229#else
2230 GTEST_LOG_(INFO) << "This test tests bionic pthread mutex implementation details.";
2231#endif
2232}
2233
Yabin Cuib5845722015-03-16 22:46:42 -07002234class StrictAlignmentAllocator {
2235 public:
2236 void* allocate(size_t size, size_t alignment) {
2237 char* p = new char[size + alignment * 2];
2238 allocated_array.push_back(p);
2239 while (!is_strict_aligned(p, alignment)) {
2240 ++p;
2241 }
2242 return p;
2243 }
2244
2245 ~StrictAlignmentAllocator() {
Elliott Hughes0b2acdf2015-10-02 18:25:19 -07002246 for (const auto& p : allocated_array) {
2247 delete[] p;
Yabin Cuib5845722015-03-16 22:46:42 -07002248 }
2249 }
2250
2251 private:
2252 bool is_strict_aligned(char* p, size_t alignment) {
2253 return (reinterpret_cast<uintptr_t>(p) % (alignment * 2)) == alignment;
2254 }
2255
2256 std::vector<char*> allocated_array;
2257};
2258
2259TEST(pthread, pthread_types_allow_four_bytes_alignment) {
2260#if defined(__BIONIC__)
2261 // For binary compatibility with old version, we need to allow 4-byte aligned data for pthread types.
2262 StrictAlignmentAllocator allocator;
2263 pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(
2264 allocator.allocate(sizeof(pthread_mutex_t), 4));
Yi Kong32bc0fc2018-08-02 17:31:13 -07002265 ASSERT_EQ(0, pthread_mutex_init(mutex, nullptr));
Yabin Cuib5845722015-03-16 22:46:42 -07002266 ASSERT_EQ(0, pthread_mutex_lock(mutex));
2267 ASSERT_EQ(0, pthread_mutex_unlock(mutex));
2268 ASSERT_EQ(0, pthread_mutex_destroy(mutex));
2269
2270 pthread_cond_t* cond = reinterpret_cast<pthread_cond_t*>(
2271 allocator.allocate(sizeof(pthread_cond_t), 4));
Yi Kong32bc0fc2018-08-02 17:31:13 -07002272 ASSERT_EQ(0, pthread_cond_init(cond, nullptr));
Yabin Cuib5845722015-03-16 22:46:42 -07002273 ASSERT_EQ(0, pthread_cond_signal(cond));
2274 ASSERT_EQ(0, pthread_cond_broadcast(cond));
2275 ASSERT_EQ(0, pthread_cond_destroy(cond));
2276
2277 pthread_rwlock_t* rwlock = reinterpret_cast<pthread_rwlock_t*>(
2278 allocator.allocate(sizeof(pthread_rwlock_t), 4));
Yi Kong32bc0fc2018-08-02 17:31:13 -07002279 ASSERT_EQ(0, pthread_rwlock_init(rwlock, nullptr));
Yabin Cuib5845722015-03-16 22:46:42 -07002280 ASSERT_EQ(0, pthread_rwlock_rdlock(rwlock));
2281 ASSERT_EQ(0, pthread_rwlock_unlock(rwlock));
2282 ASSERT_EQ(0, pthread_rwlock_wrlock(rwlock));
2283 ASSERT_EQ(0, pthread_rwlock_unlock(rwlock));
2284 ASSERT_EQ(0, pthread_rwlock_destroy(rwlock));
2285
2286#else
2287 GTEST_LOG_(INFO) << "This test tests bionic implementation details.";
2288#endif
2289}
Christopher Ferris60907c72015-06-09 18:46:15 -07002290
2291TEST(pthread, pthread_mutex_lock_null_32) {
2292#if defined(__BIONIC__) && !defined(__LP64__)
Dan Albertbaa2a972015-08-13 16:58:50 -07002293 // For LP32, the pthread lock/unlock functions allow a NULL mutex and return
2294 // EINVAL in that case: http://b/19995172.
2295 //
2296 // We decorate the public defintion with _Nonnull so that people recompiling
2297 // their code with get a warning and might fix their bug, but need to pass
2298 // NULL here to test that we remain compatible.
2299 pthread_mutex_t* null_value = nullptr;
2300 ASSERT_EQ(EINVAL, pthread_mutex_lock(null_value));
Christopher Ferris60907c72015-06-09 18:46:15 -07002301#else
2302 GTEST_LOG_(INFO) << "This test tests bionic implementation details on 32 bit devices.";
2303#endif
2304}
2305
2306TEST(pthread, pthread_mutex_unlock_null_32) {
2307#if defined(__BIONIC__) && !defined(__LP64__)
Dan Albertbaa2a972015-08-13 16:58:50 -07002308 // For LP32, the pthread lock/unlock functions allow a NULL mutex and return
2309 // EINVAL in that case: http://b/19995172.
2310 //
2311 // We decorate the public defintion with _Nonnull so that people recompiling
2312 // their code with get a warning and might fix their bug, but need to pass
2313 // NULL here to test that we remain compatible.
2314 pthread_mutex_t* null_value = nullptr;
2315 ASSERT_EQ(EINVAL, pthread_mutex_unlock(null_value));
Christopher Ferris60907c72015-06-09 18:46:15 -07002316#else
2317 GTEST_LOG_(INFO) << "This test tests bionic implementation details on 32 bit devices.";
2318#endif
2319}
2320
2321TEST_F(pthread_DeathTest, pthread_mutex_lock_null_64) {
2322#if defined(__BIONIC__) && defined(__LP64__)
2323 pthread_mutex_t* null_value = nullptr;
2324 ASSERT_EXIT(pthread_mutex_lock(null_value), testing::KilledBySignal(SIGSEGV), "");
2325#else
2326 GTEST_LOG_(INFO) << "This test tests bionic implementation details on 64 bit devices.";
2327#endif
2328}
2329
2330TEST_F(pthread_DeathTest, pthread_mutex_unlock_null_64) {
2331#if defined(__BIONIC__) && defined(__LP64__)
2332 pthread_mutex_t* null_value = nullptr;
2333 ASSERT_EXIT(pthread_mutex_unlock(null_value), testing::KilledBySignal(SIGSEGV), "");
2334#else
2335 GTEST_LOG_(INFO) << "This test tests bionic implementation details on 64 bit devices.";
2336#endif
2337}
Yabin Cui33ac04a2015-09-22 11:16:15 -07002338
2339extern _Unwind_Reason_Code FrameCounter(_Unwind_Context* ctx, void* arg);
2340
2341static volatile bool signal_handler_on_altstack_done;
2342
Josh Gao61db9ac2017-03-15 19:42:05 -07002343__attribute__((__noinline__))
2344static void signal_handler_backtrace() {
2345 // Check if we have enough stack space for unwinding.
2346 int count = 0;
2347 _Unwind_Backtrace(FrameCounter, &count);
2348 ASSERT_GT(count, 0);
2349}
2350
2351__attribute__((__noinline__))
2352static void signal_handler_logging() {
2353 // Check if we have enough stack space for logging.
2354 std::string s(2048, '*');
2355 GTEST_LOG_(INFO) << s;
2356 signal_handler_on_altstack_done = true;
2357}
2358
2359__attribute__((__noinline__))
2360static void signal_handler_snprintf() {
2361 // Check if we have enough stack space for snprintf to a PATH_MAX buffer, plus some extra.
2362 char buf[PATH_MAX + 2048];
2363 ASSERT_GT(snprintf(buf, sizeof(buf), "/proc/%d/status", getpid()), 0);
2364}
2365
Yabin Cui33ac04a2015-09-22 11:16:15 -07002366static void SignalHandlerOnAltStack(int signo, siginfo_t*, void*) {
2367 ASSERT_EQ(SIGUSR1, signo);
Josh Gao61db9ac2017-03-15 19:42:05 -07002368 signal_handler_backtrace();
2369 signal_handler_logging();
2370 signal_handler_snprintf();
Yabin Cui33ac04a2015-09-22 11:16:15 -07002371}
2372
Josh Gao415daa82017-03-06 17:45:33 -08002373TEST(pthread, big_enough_signal_stack) {
Yabin Cui33ac04a2015-09-22 11:16:15 -07002374 signal_handler_on_altstack_done = false;
2375 ScopedSignalHandler handler(SIGUSR1, SignalHandlerOnAltStack, SA_SIGINFO | SA_ONSTACK);
2376 kill(getpid(), SIGUSR1);
2377 ASSERT_TRUE(signal_handler_on_altstack_done);
2378}
Yabin Cuie7c2fff2015-11-05 22:06:09 -08002379
2380TEST(pthread, pthread_barrierattr_smoke) {
2381 pthread_barrierattr_t attr;
2382 ASSERT_EQ(0, pthread_barrierattr_init(&attr));
2383 int pshared;
2384 ASSERT_EQ(0, pthread_barrierattr_getpshared(&attr, &pshared));
2385 ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared);
2386 ASSERT_EQ(0, pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
2387 ASSERT_EQ(0, pthread_barrierattr_getpshared(&attr, &pshared));
2388 ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
2389 ASSERT_EQ(0, pthread_barrierattr_destroy(&attr));
2390}
2391
Yabin Cui81d27972016-03-22 13:45:55 -07002392struct BarrierTestHelperData {
2393 size_t thread_count;
2394 pthread_barrier_t barrier;
2395 std::atomic<int> finished_mask;
2396 std::atomic<int> serial_thread_count;
Yabin Cuie7c2fff2015-11-05 22:06:09 -08002397 size_t iteration_count;
Yabin Cui81d27972016-03-22 13:45:55 -07002398 std::atomic<size_t> finished_iteration_count;
2399
2400 BarrierTestHelperData(size_t thread_count, size_t iteration_count)
2401 : thread_count(thread_count), finished_mask(0), serial_thread_count(0),
2402 iteration_count(iteration_count), finished_iteration_count(0) {
2403 }
2404};
2405
2406struct BarrierTestHelperArg {
2407 int id;
2408 BarrierTestHelperData* data;
Yabin Cuie7c2fff2015-11-05 22:06:09 -08002409};
2410
2411static void BarrierTestHelper(BarrierTestHelperArg* arg) {
Yabin Cui81d27972016-03-22 13:45:55 -07002412 for (size_t i = 0; i < arg->data->iteration_count; ++i) {
2413 int result = pthread_barrier_wait(&arg->data->barrier);
2414 if (result == PTHREAD_BARRIER_SERIAL_THREAD) {
2415 arg->data->serial_thread_count++;
2416 } else {
2417 ASSERT_EQ(0, result);
2418 }
Yabin Cuid5c04c52017-05-02 12:57:39 -07002419 int mask = arg->data->finished_mask.fetch_or(1 << arg->id);
Yabin Cuiab4cddc2017-05-02 16:18:13 -07002420 mask |= 1 << arg->id;
Yabin Cuid5c04c52017-05-02 12:57:39 -07002421 if (mask == ((1 << arg->data->thread_count) - 1)) {
Yabin Cui81d27972016-03-22 13:45:55 -07002422 ASSERT_EQ(1, arg->data->serial_thread_count);
2423 arg->data->finished_iteration_count++;
2424 arg->data->finished_mask = 0;
2425 arg->data->serial_thread_count = 0;
2426 }
Yabin Cuie7c2fff2015-11-05 22:06:09 -08002427 }
2428}
2429
2430TEST(pthread, pthread_barrier_smoke) {
2431 const size_t BARRIER_ITERATION_COUNT = 10;
2432 const size_t BARRIER_THREAD_COUNT = 10;
Yabin Cui81d27972016-03-22 13:45:55 -07002433 BarrierTestHelperData data(BARRIER_THREAD_COUNT, BARRIER_ITERATION_COUNT);
2434 ASSERT_EQ(0, pthread_barrier_init(&data.barrier, nullptr, data.thread_count));
2435 std::vector<pthread_t> threads(data.thread_count);
Yabin Cuie7c2fff2015-11-05 22:06:09 -08002436 std::vector<BarrierTestHelperArg> args(threads.size());
2437 for (size_t i = 0; i < threads.size(); ++i) {
Yabin Cui81d27972016-03-22 13:45:55 -07002438 args[i].id = i;
2439 args[i].data = &data;
Yabin Cuie7c2fff2015-11-05 22:06:09 -08002440 ASSERT_EQ(0, pthread_create(&threads[i], nullptr,
2441 reinterpret_cast<void* (*)(void*)>(BarrierTestHelper), &args[i]));
2442 }
Yabin Cuie7c2fff2015-11-05 22:06:09 -08002443 for (size_t i = 0; i < threads.size(); ++i) {
2444 ASSERT_EQ(0, pthread_join(threads[i], nullptr));
2445 }
Yabin Cui81d27972016-03-22 13:45:55 -07002446 ASSERT_EQ(data.iteration_count, data.finished_iteration_count);
2447 ASSERT_EQ(0, pthread_barrier_destroy(&data.barrier));
2448}
2449
2450struct BarrierDestroyTestArg {
2451 std::atomic<int> tid;
2452 pthread_barrier_t* barrier;
2453};
2454
2455static void BarrierDestroyTestHelper(BarrierDestroyTestArg* arg) {
2456 arg->tid = gettid();
2457 ASSERT_EQ(0, pthread_barrier_wait(arg->barrier));
Yabin Cuie7c2fff2015-11-05 22:06:09 -08002458}
2459
2460TEST(pthread, pthread_barrier_destroy) {
2461 pthread_barrier_t barrier;
2462 ASSERT_EQ(0, pthread_barrier_init(&barrier, nullptr, 2));
2463 pthread_t thread;
Yabin Cui81d27972016-03-22 13:45:55 -07002464 BarrierDestroyTestArg arg;
Yabin Cuie7c2fff2015-11-05 22:06:09 -08002465 arg.tid = 0;
2466 arg.barrier = &barrier;
Yabin Cuie7c2fff2015-11-05 22:06:09 -08002467 ASSERT_EQ(0, pthread_create(&thread, nullptr,
Yabin Cui81d27972016-03-22 13:45:55 -07002468 reinterpret_cast<void* (*)(void*)>(BarrierDestroyTestHelper), &arg));
Yabin Cuie7c2fff2015-11-05 22:06:09 -08002469 WaitUntilThreadSleep(arg.tid);
2470 ASSERT_EQ(EBUSY, pthread_barrier_destroy(&barrier));
2471 ASSERT_EQ(PTHREAD_BARRIER_SERIAL_THREAD, pthread_barrier_wait(&barrier));
2472 // Verify if the barrier can be destroyed directly after pthread_barrier_wait().
2473 ASSERT_EQ(0, pthread_barrier_destroy(&barrier));
2474 ASSERT_EQ(0, pthread_join(thread, nullptr));
2475#if defined(__BIONIC__)
2476 ASSERT_EQ(EINVAL, pthread_barrier_destroy(&barrier));
2477#endif
2478}
2479
2480struct BarrierOrderingTestHelperArg {
2481 pthread_barrier_t* barrier;
2482 size_t* array;
2483 size_t array_length;
2484 size_t id;
2485};
2486
2487void BarrierOrderingTestHelper(BarrierOrderingTestHelperArg* arg) {
2488 const size_t ITERATION_COUNT = 10000;
2489 for (size_t i = 1; i <= ITERATION_COUNT; ++i) {
2490 arg->array[arg->id] = i;
Yabin Cuic9a659c2015-11-05 15:36:08 -08002491 int result = pthread_barrier_wait(arg->barrier);
2492 ASSERT_TRUE(result == 0 || result == PTHREAD_BARRIER_SERIAL_THREAD);
Yabin Cuie7c2fff2015-11-05 22:06:09 -08002493 for (size_t j = 0; j < arg->array_length; ++j) {
2494 ASSERT_EQ(i, arg->array[j]);
2495 }
Yabin Cuic9a659c2015-11-05 15:36:08 -08002496 result = pthread_barrier_wait(arg->barrier);
2497 ASSERT_TRUE(result == 0 || result == PTHREAD_BARRIER_SERIAL_THREAD);
Yabin Cuie7c2fff2015-11-05 22:06:09 -08002498 }
2499}
2500
2501TEST(pthread, pthread_barrier_check_ordering) {
2502 const size_t THREAD_COUNT = 4;
2503 pthread_barrier_t barrier;
2504 ASSERT_EQ(0, pthread_barrier_init(&barrier, nullptr, THREAD_COUNT));
2505 size_t array[THREAD_COUNT];
2506 std::vector<pthread_t> threads(THREAD_COUNT);
2507 std::vector<BarrierOrderingTestHelperArg> args(THREAD_COUNT);
2508 for (size_t i = 0; i < THREAD_COUNT; ++i) {
2509 args[i].barrier = &barrier;
2510 args[i].array = array;
2511 args[i].array_length = THREAD_COUNT;
2512 args[i].id = i;
2513 ASSERT_EQ(0, pthread_create(&threads[i], nullptr,
2514 reinterpret_cast<void* (*)(void*)>(BarrierOrderingTestHelper),
2515 &args[i]));
2516 }
2517 for (size_t i = 0; i < THREAD_COUNT; ++i) {
2518 ASSERT_EQ(0, pthread_join(threads[i], nullptr));
2519 }
2520}
Yabin Cuife3a83a2015-11-17 16:03:18 -08002521
Elliott Hughes463faad2018-07-06 14:34:49 -07002522TEST(pthread, pthread_barrier_init_zero_count) {
2523 pthread_barrier_t barrier;
2524 ASSERT_EQ(EINVAL, pthread_barrier_init(&barrier, nullptr, 0));
2525}
2526
Yabin Cuife3a83a2015-11-17 16:03:18 -08002527TEST(pthread, pthread_spinlock_smoke) {
2528 pthread_spinlock_t lock;
2529 ASSERT_EQ(0, pthread_spin_init(&lock, 0));
2530 ASSERT_EQ(0, pthread_spin_trylock(&lock));
2531 ASSERT_EQ(0, pthread_spin_unlock(&lock));
2532 ASSERT_EQ(0, pthread_spin_lock(&lock));
2533 ASSERT_EQ(EBUSY, pthread_spin_trylock(&lock));
2534 ASSERT_EQ(0, pthread_spin_unlock(&lock));
2535 ASSERT_EQ(0, pthread_spin_destroy(&lock));
2536}
Elliott Hughes53dc9dd2017-09-19 14:02:50 -07002537
Elliott Hughes8aecba72017-10-17 15:34:41 -07002538TEST(pthread, pthread_attr_getdetachstate__pthread_attr_setdetachstate) {
Elliott Hughes53dc9dd2017-09-19 14:02:50 -07002539 pthread_attr_t attr;
2540 ASSERT_EQ(0, pthread_attr_init(&attr));
2541
Elliott Hughes8aecba72017-10-17 15:34:41 -07002542 int state;
Elliott Hughes53dc9dd2017-09-19 14:02:50 -07002543 ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED));
Elliott Hughes8aecba72017-10-17 15:34:41 -07002544 ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &state));
2545 ASSERT_EQ(PTHREAD_CREATE_DETACHED, state);
2546
Elliott Hughes53dc9dd2017-09-19 14:02:50 -07002547 ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
Elliott Hughes8aecba72017-10-17 15:34:41 -07002548 ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &state));
2549 ASSERT_EQ(PTHREAD_CREATE_JOINABLE, state);
2550
Elliott Hughes53dc9dd2017-09-19 14:02:50 -07002551 ASSERT_EQ(EINVAL, pthread_attr_setdetachstate(&attr, 123));
Elliott Hughes8aecba72017-10-17 15:34:41 -07002552 ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &state));
2553 ASSERT_EQ(PTHREAD_CREATE_JOINABLE, state);
Elliott Hughes53dc9dd2017-09-19 14:02:50 -07002554}
2555
2556TEST(pthread, pthread_create__mmap_failures) {
2557 pthread_attr_t attr;
2558 ASSERT_EQ(0, pthread_attr_init(&attr));
2559 ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED));
2560
2561 const auto kPageSize = sysconf(_SC_PAGE_SIZE);
2562
Elliott Hughes57512982017-10-02 22:49:18 -07002563 // Use up all the VMAs. By default this is 64Ki (though some will already be in use).
Elliott Hughes53dc9dd2017-09-19 14:02:50 -07002564 std::vector<void*> pages;
Elliott Hughes57512982017-10-02 22:49:18 -07002565 pages.reserve(64 * 1024);
Elliott Hughes53dc9dd2017-09-19 14:02:50 -07002566 int prot = PROT_NONE;
2567 while (true) {
2568 void* page = mmap(nullptr, kPageSize, prot, MAP_ANON|MAP_PRIVATE, -1, 0);
2569 if (page == MAP_FAILED) break;
2570 pages.push_back(page);
2571 prot = (prot == PROT_NONE) ? PROT_READ : PROT_NONE;
2572 }
2573
2574 // Try creating threads, freeing up a page each time we fail.
2575 size_t EAGAIN_count = 0;
2576 size_t i = 0;
2577 for (; i < pages.size(); ++i) {
2578 pthread_t t;
2579 int status = pthread_create(&t, &attr, IdFn, nullptr);
2580 if (status != EAGAIN) break;
2581 ++EAGAIN_count;
2582 ASSERT_EQ(0, munmap(pages[i], kPageSize));
2583 }
2584
2585 // Creating a thread uses at least six VMAs: the stack, the TLS, and a guard each side of both.
2586 // So we should have seen at least six failures.
2587 ASSERT_GE(EAGAIN_count, 6U);
2588
2589 for (; i < pages.size(); ++i) {
2590 ASSERT_EQ(0, munmap(pages[i], kPageSize));
2591 }
2592}
Elliott Hughesdff08ce2017-10-16 09:58:45 -07002593
2594TEST(pthread, pthread_setschedparam) {
2595 sched_param p = { .sched_priority = INT_MIN };
2596 ASSERT_EQ(EINVAL, pthread_setschedparam(pthread_self(), INT_MIN, &p));
2597}
2598
2599TEST(pthread, pthread_setschedprio) {
2600 ASSERT_EQ(EINVAL, pthread_setschedprio(pthread_self(), INT_MIN));
2601}
Elliott Hughes8aecba72017-10-17 15:34:41 -07002602
2603TEST(pthread, pthread_attr_getinheritsched__pthread_attr_setinheritsched) {
2604 pthread_attr_t attr;
2605 ASSERT_EQ(0, pthread_attr_init(&attr));
2606
2607 int state;
2608 ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED));
2609 ASSERT_EQ(0, pthread_attr_getinheritsched(&attr, &state));
2610 ASSERT_EQ(PTHREAD_INHERIT_SCHED, state);
2611
2612 ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED));
2613 ASSERT_EQ(0, pthread_attr_getinheritsched(&attr, &state));
2614 ASSERT_EQ(PTHREAD_EXPLICIT_SCHED, state);
2615
2616 ASSERT_EQ(EINVAL, pthread_attr_setinheritsched(&attr, 123));
2617 ASSERT_EQ(0, pthread_attr_getinheritsched(&attr, &state));
2618 ASSERT_EQ(PTHREAD_EXPLICIT_SCHED, state);
2619}
2620
2621TEST(pthread, pthread_attr_setinheritsched__PTHREAD_INHERIT_SCHED__PTHREAD_EXPLICIT_SCHED) {
2622 pthread_attr_t attr;
2623 ASSERT_EQ(0, pthread_attr_init(&attr));
2624
2625 // If we set invalid scheduling attributes but choose to inherit, everything's fine...
2626 sched_param param = { .sched_priority = sched_get_priority_max(SCHED_FIFO) + 1 };
2627 ASSERT_EQ(0, pthread_attr_setschedparam(&attr, &param));
2628 ASSERT_EQ(0, pthread_attr_setschedpolicy(&attr, SCHED_FIFO));
2629 ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED));
2630
2631 pthread_t t;
2632 ASSERT_EQ(0, pthread_create(&t, &attr, IdFn, nullptr));
2633 ASSERT_EQ(0, pthread_join(t, nullptr));
2634
Elliott Hughes7a660662017-10-30 09:26:06 -07002635#if defined(__LP64__)
2636 // If we ask to use them, though, we'll see a failure...
Elliott Hughes8aecba72017-10-17 15:34:41 -07002637 ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED));
2638 ASSERT_EQ(EINVAL, pthread_create(&t, &attr, IdFn, nullptr));
Elliott Hughes7a660662017-10-30 09:26:06 -07002639#else
2640 // For backwards compatibility with broken apps, we just ignore failures
2641 // to set scheduler attributes on LP32.
2642#endif
Elliott Hughes8aecba72017-10-17 15:34:41 -07002643}
2644
2645TEST(pthread, pthread_attr_setinheritsched_PTHREAD_INHERIT_SCHED_takes_effect) {
2646 sched_param param = { .sched_priority = sched_get_priority_min(SCHED_FIFO) };
2647 int rc = pthread_setschedparam(pthread_self(), SCHED_FIFO, &param);
2648 if (rc == EPERM) {
2649 GTEST_LOG_(INFO) << "pthread_setschedparam failed with EPERM, skipping test\n";
2650 return;
2651 }
2652 ASSERT_EQ(0, rc);
2653
2654 pthread_attr_t attr;
2655 ASSERT_EQ(0, pthread_attr_init(&attr));
2656 ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED));
2657
Elliott Hughes0bd9d132017-11-02 13:11:13 -07002658 SpinFunctionHelper spin_helper;
Elliott Hughes8aecba72017-10-17 15:34:41 -07002659 pthread_t t;
Elliott Hughes0bd9d132017-11-02 13:11:13 -07002660 ASSERT_EQ(0, pthread_create(&t, &attr, spin_helper.GetFunction(), nullptr));
Elliott Hughes8aecba72017-10-17 15:34:41 -07002661 int actual_policy;
2662 sched_param actual_param;
2663 ASSERT_EQ(0, pthread_getschedparam(t, &actual_policy, &actual_param));
2664 ASSERT_EQ(SCHED_FIFO, actual_policy);
Elliott Hughes0bd9d132017-11-02 13:11:13 -07002665 spin_helper.UnSpin();
Elliott Hughes8aecba72017-10-17 15:34:41 -07002666 ASSERT_EQ(0, pthread_join(t, nullptr));
2667}
2668
2669TEST(pthread, pthread_attr_setinheritsched_PTHREAD_EXPLICIT_SCHED_takes_effect) {
2670 sched_param param = { .sched_priority = sched_get_priority_min(SCHED_FIFO) };
2671 int rc = pthread_setschedparam(pthread_self(), SCHED_FIFO, &param);
2672 if (rc == EPERM) {
2673 GTEST_LOG_(INFO) << "pthread_setschedparam failed with EPERM, skipping test\n";
2674 return;
2675 }
2676 ASSERT_EQ(0, rc);
2677
2678 pthread_attr_t attr;
2679 ASSERT_EQ(0, pthread_attr_init(&attr));
2680 ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED));
2681 ASSERT_EQ(0, pthread_attr_setschedpolicy(&attr, SCHED_OTHER));
2682
Elliott Hughes0bd9d132017-11-02 13:11:13 -07002683 SpinFunctionHelper spin_helper;
Elliott Hughes8aecba72017-10-17 15:34:41 -07002684 pthread_t t;
Elliott Hughes0bd9d132017-11-02 13:11:13 -07002685 ASSERT_EQ(0, pthread_create(&t, &attr, spin_helper.GetFunction(), nullptr));
Elliott Hughes8aecba72017-10-17 15:34:41 -07002686 int actual_policy;
2687 sched_param actual_param;
2688 ASSERT_EQ(0, pthread_getschedparam(t, &actual_policy, &actual_param));
2689 ASSERT_EQ(SCHED_OTHER, actual_policy);
Elliott Hughes0bd9d132017-11-02 13:11:13 -07002690 spin_helper.UnSpin();
Elliott Hughes8aecba72017-10-17 15:34:41 -07002691 ASSERT_EQ(0, pthread_join(t, nullptr));
2692}
2693
2694TEST(pthread, pthread_attr_setinheritsched__takes_effect_despite_SCHED_RESET_ON_FORK) {
2695 sched_param param = { .sched_priority = sched_get_priority_min(SCHED_FIFO) };
2696 int rc = pthread_setschedparam(pthread_self(), SCHED_FIFO | SCHED_RESET_ON_FORK, &param);
2697 if (rc == EPERM) {
2698 GTEST_LOG_(INFO) << "pthread_setschedparam failed with EPERM, skipping test\n";
2699 return;
2700 }
2701 ASSERT_EQ(0, rc);
2702
2703 pthread_attr_t attr;
2704 ASSERT_EQ(0, pthread_attr_init(&attr));
2705 ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED));
2706
Elliott Hughes0bd9d132017-11-02 13:11:13 -07002707 SpinFunctionHelper spin_helper;
Elliott Hughes8aecba72017-10-17 15:34:41 -07002708 pthread_t t;
Elliott Hughes0bd9d132017-11-02 13:11:13 -07002709 ASSERT_EQ(0, pthread_create(&t, &attr, spin_helper.GetFunction(), nullptr));
Elliott Hughes8aecba72017-10-17 15:34:41 -07002710 int actual_policy;
2711 sched_param actual_param;
2712 ASSERT_EQ(0, pthread_getschedparam(t, &actual_policy, &actual_param));
2713 ASSERT_EQ(SCHED_FIFO | SCHED_RESET_ON_FORK, actual_policy);
Elliott Hughes0bd9d132017-11-02 13:11:13 -07002714 spin_helper.UnSpin();
Elliott Hughes8aecba72017-10-17 15:34:41 -07002715 ASSERT_EQ(0, pthread_join(t, nullptr));
2716}