forked from aosp-mirror/platform_bionic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpthread_test.cpp
More file actions
2691 lines (2251 loc) · 89 KB
/
Copy pathpthread_test.cpp
File metadata and controls
2691 lines (2251 loc) · 89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gtest/gtest.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <malloc.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <time.h>
#include <unistd.h>
#include <unwind.h>
#include <atomic>
#include <vector>
#include <android-base/parseint.h>
#include <android-base/scopeguard.h>
#include <android-base/strings.h>
#include "private/bionic_constants.h"
#include "private/bionic_macros.h"
#include "BionicDeathTest.h"
#include "ScopedSignalHandler.h"
#include "utils.h"
TEST(pthread, pthread_key_create) {
pthread_key_t key;
ASSERT_EQ(0, pthread_key_create(&key, NULL));
ASSERT_EQ(0, pthread_key_delete(key));
// Can't delete a key that's already been deleted.
ASSERT_EQ(EINVAL, pthread_key_delete(key));
}
TEST(pthread, pthread_keys_max) {
// POSIX says PTHREAD_KEYS_MAX should be at least _POSIX_THREAD_KEYS_MAX.
ASSERT_GE(PTHREAD_KEYS_MAX, _POSIX_THREAD_KEYS_MAX);
}
TEST(pthread, sysconf_SC_THREAD_KEYS_MAX_eq_PTHREAD_KEYS_MAX) {
int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX);
ASSERT_EQ(sysconf_max, PTHREAD_KEYS_MAX);
}
TEST(pthread, pthread_key_many_distinct) {
// As gtest uses pthread keys, we can't allocate exactly PTHREAD_KEYS_MAX
// pthread keys, but We should be able to allocate at least this many keys.
int nkeys = PTHREAD_KEYS_MAX / 2;
std::vector<pthread_key_t> keys;
auto scope_guard = android::base::make_scope_guard([&keys] {
for (const auto& key : keys) {
EXPECT_EQ(0, pthread_key_delete(key));
}
});
for (int i = 0; i < nkeys; ++i) {
pthread_key_t key;
// If this fails, it's likely that LIBC_PTHREAD_KEY_RESERVED_COUNT is wrong.
ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << nkeys;
keys.push_back(key);
ASSERT_EQ(0, pthread_setspecific(key, reinterpret_cast<void*>(i)));
}
for (int i = keys.size() - 1; i >= 0; --i) {
ASSERT_EQ(reinterpret_cast<void*>(i), pthread_getspecific(keys.back()));
pthread_key_t key = keys.back();
keys.pop_back();
ASSERT_EQ(0, pthread_key_delete(key));
}
}
TEST(pthread, pthread_key_not_exceed_PTHREAD_KEYS_MAX) {
std::vector<pthread_key_t> keys;
int rv = 0;
// Pthread keys are used by gtest, so PTHREAD_KEYS_MAX should
// be more than we are allowed to allocate now.
for (int i = 0; i < PTHREAD_KEYS_MAX; i++) {
pthread_key_t key;
rv = pthread_key_create(&key, NULL);
if (rv == EAGAIN) {
break;
}
EXPECT_EQ(0, rv);
keys.push_back(key);
}
// Don't leak keys.
for (const auto& key : keys) {
EXPECT_EQ(0, pthread_key_delete(key));
}
keys.clear();
// We should have eventually reached the maximum number of keys and received
// EAGAIN.
ASSERT_EQ(EAGAIN, rv);
}
TEST(pthread, pthread_key_delete) {
void* expected = reinterpret_cast<void*>(1234);
pthread_key_t key;
ASSERT_EQ(0, pthread_key_create(&key, NULL));
ASSERT_EQ(0, pthread_setspecific(key, expected));
ASSERT_EQ(expected, pthread_getspecific(key));
ASSERT_EQ(0, pthread_key_delete(key));
// After deletion, pthread_getspecific returns NULL.
ASSERT_EQ(NULL, pthread_getspecific(key));
// And you can't use pthread_setspecific with the deleted key.
ASSERT_EQ(EINVAL, pthread_setspecific(key, expected));
}
TEST(pthread, pthread_key_fork) {
void* expected = reinterpret_cast<void*>(1234);
pthread_key_t key;
ASSERT_EQ(0, pthread_key_create(&key, NULL));
ASSERT_EQ(0, pthread_setspecific(key, expected));
ASSERT_EQ(expected, pthread_getspecific(key));
pid_t pid = fork();
ASSERT_NE(-1, pid) << strerror(errno);
if (pid == 0) {
// The surviving thread inherits all the forking thread's TLS values...
ASSERT_EQ(expected, pthread_getspecific(key));
_exit(99);
}
AssertChildExited(pid, 99);
ASSERT_EQ(expected, pthread_getspecific(key));
ASSERT_EQ(0, pthread_key_delete(key));
}
static void* DirtyKeyFn(void* key) {
return pthread_getspecific(*reinterpret_cast<pthread_key_t*>(key));
}
TEST(pthread, pthread_key_dirty) {
pthread_key_t key;
ASSERT_EQ(0, pthread_key_create(&key, NULL));
size_t stack_size = 640 * 1024;
void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
ASSERT_NE(MAP_FAILED, stack);
memset(stack, 0xff, stack_size);
pthread_attr_t attr;
ASSERT_EQ(0, pthread_attr_init(&attr));
ASSERT_EQ(0, pthread_attr_setstack(&attr, stack, stack_size));
pthread_t t;
ASSERT_EQ(0, pthread_create(&t, &attr, DirtyKeyFn, &key));
void* result;
ASSERT_EQ(0, pthread_join(t, &result));
ASSERT_EQ(nullptr, result); // Not ~0!
ASSERT_EQ(0, munmap(stack, stack_size));
ASSERT_EQ(0, pthread_key_delete(key));
}
TEST(pthread, static_pthread_key_used_before_creation) {
#if defined(__BIONIC__)
// See http://b/19625804. The bug is about a static/global pthread key being used before creation.
// So here tests if the static/global default value 0 can be detected as invalid key.
static pthread_key_t key;
ASSERT_EQ(nullptr, pthread_getspecific(key));
ASSERT_EQ(EINVAL, pthread_setspecific(key, nullptr));
ASSERT_EQ(EINVAL, pthread_key_delete(key));
#else
GTEST_LOG_(INFO) << "This test tests bionic pthread key implementation detail.\n";
#endif
}
static void* IdFn(void* arg) {
return arg;
}
class SpinFunctionHelper {
public:
SpinFunctionHelper() {
SpinFunctionHelper::spin_flag_ = true;
}
~SpinFunctionHelper() {
UnSpin();
}
auto GetFunction() -> void* (*)(void*) {
return SpinFunctionHelper::SpinFn;
}
void UnSpin() {
SpinFunctionHelper::spin_flag_ = false;
}
private:
static void* SpinFn(void*) {
while (spin_flag_) {}
return NULL;
}
static std::atomic<bool> spin_flag_;
};
// It doesn't matter if spin_flag_ is used in several tests,
// because it is always set to false after each test. Each thread
// loops on spin_flag_ can find it becomes false at some time.
std::atomic<bool> SpinFunctionHelper::spin_flag_;
static void* JoinFn(void* arg) {
return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
}
static void AssertDetached(pthread_t t, bool is_detached) {
pthread_attr_t attr;
ASSERT_EQ(0, pthread_getattr_np(t, &attr));
int detach_state;
ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
pthread_attr_destroy(&attr);
ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
}
static void MakeDeadThread(pthread_t& t) {
ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
ASSERT_EQ(0, pthread_join(t, NULL));
}
TEST(pthread, pthread_create) {
void* expected_result = reinterpret_cast<void*>(123);
// Can we create a thread?
pthread_t t;
ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
// If we join, do we get the expected value back?
void* result;
ASSERT_EQ(0, pthread_join(t, &result));
ASSERT_EQ(expected_result, result);
}
TEST(pthread, pthread_create_EAGAIN) {
pthread_attr_t attributes;
ASSERT_EQ(0, pthread_attr_init(&attributes));
ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
pthread_t t;
ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL));
}
TEST(pthread, pthread_no_join_after_detach) {
SpinFunctionHelper spin_helper;
pthread_t t1;
ASSERT_EQ(0, pthread_create(&t1, NULL, spin_helper.GetFunction(), NULL));
// After a pthread_detach...
ASSERT_EQ(0, pthread_detach(t1));
AssertDetached(t1, true);
// ...pthread_join should fail.
ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
}
TEST(pthread, pthread_no_op_detach_after_join) {
SpinFunctionHelper spin_helper;
pthread_t t1;
ASSERT_EQ(0, pthread_create(&t1, NULL, spin_helper.GetFunction(), NULL));
// If thread 2 is already waiting to join thread 1...
pthread_t t2;
ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
sleep(1); // (Give t2 a chance to call pthread_join.)
#if defined(__BIONIC__)
ASSERT_EQ(EINVAL, pthread_detach(t1));
#else
ASSERT_EQ(0, pthread_detach(t1));
#endif
AssertDetached(t1, false);
spin_helper.UnSpin();
// ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
void* join_result;
ASSERT_EQ(0, pthread_join(t2, &join_result));
ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
}
TEST(pthread, pthread_join_self) {
ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), NULL));
}
struct TestBug37410 {
pthread_t main_thread;
pthread_mutex_t mutex;
static void main() {
TestBug37410 data;
data.main_thread = pthread_self();
ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL));
ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
pthread_t t;
ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data)));
// Wait for the thread to be running...
ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex));
// ...and exit.
pthread_exit(NULL);
}
private:
static void* thread_fn(void* arg) {
TestBug37410* data = reinterpret_cast<TestBug37410*>(arg);
// Let the main thread know we're running.
pthread_mutex_unlock(&data->mutex);
// And wait for the main thread to exit.
pthread_join(data->main_thread, NULL);
return NULL;
}
};
// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
// run this test (which exits normally) in its own process.
class pthread_DeathTest : public BionicDeathTest {};
TEST_F(pthread_DeathTest, pthread_bug_37410) {
// http://code.google.com/p/android/issues/detail?id=37410
ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), "");
}
static void* SignalHandlerFn(void* arg) {
sigset64_t wait_set;
sigfillset64(&wait_set);
return reinterpret_cast<void*>(sigwait64(&wait_set, reinterpret_cast<int*>(arg)));
}
TEST(pthread, pthread_sigmask) {
// Check that SIGUSR1 isn't blocked.
sigset_t original_set;
sigemptyset(&original_set);
ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set));
ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
// Block SIGUSR1.
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
// Check that SIGUSR1 is blocked.
sigset_t final_set;
sigemptyset(&final_set);
ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set));
ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
// ...and that sigprocmask agrees with pthread_sigmask.
sigemptyset(&final_set);
ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set));
ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
// Spawn a thread that calls sigwait and tells us what it received.
pthread_t signal_thread;
int received_signal = -1;
ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
// Send that thread SIGUSR1.
pthread_kill(signal_thread, SIGUSR1);
// See what it got.
void* join_result;
ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
ASSERT_EQ(SIGUSR1, received_signal);
ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
// Restore the original signal mask.
ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL));
}
TEST(pthread, pthread_sigmask64_SIGTRMIN) {
// Check that SIGRTMIN isn't blocked.
sigset64_t original_set;
sigemptyset64(&original_set);
ASSERT_EQ(0, pthread_sigmask64(SIG_BLOCK, NULL, &original_set));
ASSERT_FALSE(sigismember64(&original_set, SIGRTMIN));
// Block SIGRTMIN.
sigset64_t set;
sigemptyset64(&set);
sigaddset64(&set, SIGRTMIN);
ASSERT_EQ(0, pthread_sigmask64(SIG_BLOCK, &set, NULL));
// Check that SIGRTMIN is blocked.
sigset64_t final_set;
sigemptyset64(&final_set);
ASSERT_EQ(0, pthread_sigmask64(SIG_BLOCK, NULL, &final_set));
ASSERT_TRUE(sigismember64(&final_set, SIGRTMIN));
// ...and that sigprocmask64 agrees with pthread_sigmask64.
sigemptyset64(&final_set);
ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, NULL, &final_set));
ASSERT_TRUE(sigismember64(&final_set, SIGRTMIN));
// Spawn a thread that calls sigwait64 and tells us what it received.
pthread_t signal_thread;
int received_signal = -1;
ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
// Send that thread SIGRTMIN.
pthread_kill(signal_thread, SIGRTMIN);
// See what it got.
void* join_result;
ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
ASSERT_EQ(SIGRTMIN, received_signal);
ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
// Restore the original signal mask.
ASSERT_EQ(0, pthread_sigmask64(SIG_SETMASK, &original_set, NULL));
}
static void test_pthread_setname_np__pthread_getname_np(pthread_t t) {
ASSERT_EQ(0, pthread_setname_np(t, "short"));
char name[32];
ASSERT_EQ(0, pthread_getname_np(t, name, sizeof(name)));
ASSERT_STREQ("short", name);
// The limit is 15 characters --- the kernel's buffer is 16, but includes a NUL.
ASSERT_EQ(0, pthread_setname_np(t, "123456789012345"));
ASSERT_EQ(0, pthread_getname_np(t, name, sizeof(name)));
ASSERT_STREQ("123456789012345", name);
ASSERT_EQ(ERANGE, pthread_setname_np(t, "1234567890123456"));
// The passed-in buffer should be at least 16 bytes.
ASSERT_EQ(0, pthread_getname_np(t, name, 16));
ASSERT_EQ(ERANGE, pthread_getname_np(t, name, 15));
}
TEST(pthread, pthread_setname_np__pthread_getname_np__self) {
test_pthread_setname_np__pthread_getname_np(pthread_self());
}
TEST(pthread, pthread_setname_np__pthread_getname_np__other) {
SpinFunctionHelper spin_helper;
pthread_t t;
ASSERT_EQ(0, pthread_create(&t, nullptr, spin_helper.GetFunction(), nullptr));
test_pthread_setname_np__pthread_getname_np(t);
spin_helper.UnSpin();
ASSERT_EQ(0, pthread_join(t, nullptr));
}
// http://b/28051133: a kernel misfeature means that you can't change the
// name of another thread if you've set PR_SET_DUMPABLE to 0.
TEST(pthread, pthread_setname_np__pthread_getname_np__other_PR_SET_DUMPABLE) {
ASSERT_EQ(0, prctl(PR_SET_DUMPABLE, 0)) << strerror(errno);
SpinFunctionHelper spin_helper;
pthread_t t;
ASSERT_EQ(0, pthread_create(&t, nullptr, spin_helper.GetFunction(), nullptr));
test_pthread_setname_np__pthread_getname_np(t);
spin_helper.UnSpin();
ASSERT_EQ(0, pthread_join(t, nullptr));
}
TEST_F(pthread_DeathTest, pthread_setname_np__no_such_thread) {
pthread_t dead_thread;
MakeDeadThread(dead_thread);
EXPECT_DEATH(pthread_setname_np(dead_thread, "short 3"), "invalid pthread_t");
}
TEST_F(pthread_DeathTest, pthread_setname_np__null_thread) {
pthread_t null_thread = 0;
EXPECT_EQ(ENOENT, pthread_setname_np(null_thread, "short 3"));
}
TEST_F(pthread_DeathTest, pthread_getname_np__no_such_thread) {
pthread_t dead_thread;
MakeDeadThread(dead_thread);
char name[64];
EXPECT_DEATH(pthread_getname_np(dead_thread, name, sizeof(name)), "invalid pthread_t");
}
TEST_F(pthread_DeathTest, pthread_getname_np__null_thread) {
pthread_t null_thread = 0;
char name[64];
EXPECT_EQ(ENOENT, pthread_getname_np(null_thread, name, sizeof(name)));
}
TEST(pthread, pthread_kill__0) {
// Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
}
TEST(pthread, pthread_kill__invalid_signal) {
ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
}
static void pthread_kill__in_signal_handler_helper(int signal_number) {
static int count = 0;
ASSERT_EQ(SIGALRM, signal_number);
if (++count == 1) {
// Can we call pthread_kill from a signal handler?
ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
}
}
TEST(pthread, pthread_kill__in_signal_handler) {
ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper);
ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
}
TEST_F(pthread_DeathTest, pthread_detach__no_such_thread) {
pthread_t dead_thread;
MakeDeadThread(dead_thread);
EXPECT_DEATH(pthread_detach(dead_thread), "invalid pthread_t");
}
TEST_F(pthread_DeathTest, pthread_detach__null_thread) {
pthread_t null_thread = 0;
EXPECT_EQ(ESRCH, pthread_detach(null_thread));
}
TEST(pthread, pthread_getcpuclockid__clock_gettime) {
SpinFunctionHelper spin_helper;
pthread_t t;
ASSERT_EQ(0, pthread_create(&t, NULL, spin_helper.GetFunction(), NULL));
clockid_t c;
ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
timespec ts;
ASSERT_EQ(0, clock_gettime(c, &ts));
spin_helper.UnSpin();
ASSERT_EQ(0, pthread_join(t, nullptr));
}
TEST_F(pthread_DeathTest, pthread_getcpuclockid__no_such_thread) {
pthread_t dead_thread;
MakeDeadThread(dead_thread);
clockid_t c;
EXPECT_DEATH(pthread_getcpuclockid(dead_thread, &c), "invalid pthread_t");
}
TEST_F(pthread_DeathTest, pthread_getcpuclockid__null_thread) {
pthread_t null_thread = 0;
clockid_t c;
EXPECT_EQ(ESRCH, pthread_getcpuclockid(null_thread, &c));
}
TEST_F(pthread_DeathTest, pthread_getschedparam__no_such_thread) {
pthread_t dead_thread;
MakeDeadThread(dead_thread);
int policy;
sched_param param;
EXPECT_DEATH(pthread_getschedparam(dead_thread, &policy, ¶m), "invalid pthread_t");
}
TEST_F(pthread_DeathTest, pthread_getschedparam__null_thread) {
pthread_t null_thread = 0;
int policy;
sched_param param;
EXPECT_EQ(ESRCH, pthread_getschedparam(null_thread, &policy, ¶m));
}
TEST_F(pthread_DeathTest, pthread_setschedparam__no_such_thread) {
pthread_t dead_thread;
MakeDeadThread(dead_thread);
int policy = 0;
sched_param param;
EXPECT_DEATH(pthread_setschedparam(dead_thread, policy, ¶m), "invalid pthread_t");
}
TEST_F(pthread_DeathTest, pthread_setschedparam__null_thread) {
pthread_t null_thread = 0;
int policy = 0;
sched_param param;
EXPECT_EQ(ESRCH, pthread_setschedparam(null_thread, policy, ¶m));
}
TEST_F(pthread_DeathTest, pthread_setschedprio__no_such_thread) {
pthread_t dead_thread;
MakeDeadThread(dead_thread);
EXPECT_DEATH(pthread_setschedprio(dead_thread, 123), "invalid pthread_t");
}
TEST_F(pthread_DeathTest, pthread_setschedprio__null_thread) {
pthread_t null_thread = 0;
EXPECT_EQ(ESRCH, pthread_setschedprio(null_thread, 123));
}
TEST_F(pthread_DeathTest, pthread_join__no_such_thread) {
pthread_t dead_thread;
MakeDeadThread(dead_thread);
EXPECT_DEATH(pthread_join(dead_thread, NULL), "invalid pthread_t");
}
TEST_F(pthread_DeathTest, pthread_join__null_thread) {
pthread_t null_thread = 0;
EXPECT_EQ(ESRCH, pthread_join(null_thread, NULL));
}
TEST_F(pthread_DeathTest, pthread_kill__no_such_thread) {
pthread_t dead_thread;
MakeDeadThread(dead_thread);
EXPECT_DEATH(pthread_kill(dead_thread, 0), "invalid pthread_t");
}
TEST_F(pthread_DeathTest, pthread_kill__null_thread) {
pthread_t null_thread = 0;
EXPECT_EQ(ESRCH, pthread_kill(null_thread, 0));
}
TEST(pthread, pthread_join__multijoin) {
SpinFunctionHelper spin_helper;
pthread_t t1;
ASSERT_EQ(0, pthread_create(&t1, NULL, spin_helper.GetFunction(), NULL));
pthread_t t2;
ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
sleep(1); // (Give t2 a chance to call pthread_join.)
// Multiple joins to the same thread should fail.
ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
spin_helper.UnSpin();
// ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
void* join_result;
ASSERT_EQ(0, pthread_join(t2, &join_result));
ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
}
TEST(pthread, pthread_join__race) {
// http://b/11693195 --- pthread_join could return before the thread had actually exited.
// If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread.
for (size_t i = 0; i < 1024; ++i) {
size_t stack_size = 640*1024;
void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
pthread_attr_t a;
pthread_attr_init(&a);
pthread_attr_setstack(&a, stack, stack_size);
pthread_t t;
ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL));
ASSERT_EQ(0, pthread_join(t, NULL));
ASSERT_EQ(0, munmap(stack, stack_size));
}
}
static void* GetActualGuardSizeFn(void* arg) {
pthread_attr_t attributes;
pthread_getattr_np(pthread_self(), &attributes);
pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
return NULL;
}
static size_t GetActualGuardSize(const pthread_attr_t& attributes) {
size_t result;
pthread_t t;
pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
pthread_join(t, NULL);
return result;
}
static void* GetActualStackSizeFn(void* arg) {
pthread_attr_t attributes;
pthread_getattr_np(pthread_self(), &attributes);
pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
return NULL;
}
static size_t GetActualStackSize(const pthread_attr_t& attributes) {
size_t result;
pthread_t t;
pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
pthread_join(t, NULL);
return result;
}
TEST(pthread, pthread_attr_setguardsize_tiny) {
pthread_attr_t attributes;
ASSERT_EQ(0, pthread_attr_init(&attributes));
// No such thing as too small: will be rounded up to one page by pthread_create.
ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
size_t guard_size;
ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
ASSERT_EQ(128U, guard_size);
ASSERT_EQ(4096U, GetActualGuardSize(attributes));
}
TEST(pthread, pthread_attr_setguardsize_reasonable) {
pthread_attr_t attributes;
ASSERT_EQ(0, pthread_attr_init(&attributes));
// Large enough and a multiple of the page size.
ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
size_t guard_size;
ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
ASSERT_EQ(32*1024U, guard_size);
ASSERT_EQ(32*1024U, GetActualGuardSize(attributes));
}
TEST(pthread, pthread_attr_setguardsize_needs_rounding) {
pthread_attr_t attributes;
ASSERT_EQ(0, pthread_attr_init(&attributes));
// Large enough but not a multiple of the page size.
ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
size_t guard_size;
ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
ASSERT_EQ(32*1024U + 1, guard_size);
ASSERT_EQ(36*1024U, GetActualGuardSize(attributes));
}
TEST(pthread, pthread_attr_setguardsize_enormous) {
pthread_attr_t attributes;
ASSERT_EQ(0, pthread_attr_init(&attributes));
// Larger than the stack itself. (Historically we mistakenly carved
// the guard out of the stack itself, rather than adding it after the
// end.)
ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024*1024));
size_t guard_size;
ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
ASSERT_EQ(32*1024*1024U, guard_size);
ASSERT_EQ(32*1024*1024U, GetActualGuardSize(attributes));
}
TEST(pthread, pthread_attr_setstacksize) {
pthread_attr_t attributes;
ASSERT_EQ(0, pthread_attr_init(&attributes));
// Get the default stack size.
size_t default_stack_size;
ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
// Too small.
ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
size_t stack_size;
ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
ASSERT_EQ(default_stack_size, stack_size);
ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
// Large enough and a multiple of the page size; may be rounded up by pthread_create.
ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
ASSERT_EQ(32*1024U, stack_size);
ASSERT_GE(GetActualStackSize(attributes), 32*1024U);
// Large enough but not aligned; will be rounded up by pthread_create.
ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
ASSERT_EQ(32*1024U + 1, stack_size);
#if defined(__BIONIC__)
ASSERT_GT(GetActualStackSize(attributes), 32*1024U + 1);
#else // __BIONIC__
// glibc rounds down, in violation of POSIX. They document this in their BUGS section.
ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
#endif // __BIONIC__
}
TEST(pthread, pthread_rwlockattr_smoke) {
pthread_rwlockattr_t attr;
ASSERT_EQ(0, pthread_rwlockattr_init(&attr));
int pshared_value_array[] = {PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_SHARED};
for (size_t i = 0; i < sizeof(pshared_value_array) / sizeof(pshared_value_array[0]); ++i) {
ASSERT_EQ(0, pthread_rwlockattr_setpshared(&attr, pshared_value_array[i]));
int pshared;
ASSERT_EQ(0, pthread_rwlockattr_getpshared(&attr, &pshared));
ASSERT_EQ(pshared_value_array[i], pshared);
}
int kind_array[] = {PTHREAD_RWLOCK_PREFER_READER_NP,
PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP};
for (size_t i = 0; i < sizeof(kind_array) / sizeof(kind_array[0]); ++i) {
ASSERT_EQ(0, pthread_rwlockattr_setkind_np(&attr, kind_array[i]));
int kind;
ASSERT_EQ(0, pthread_rwlockattr_getkind_np(&attr, &kind));
ASSERT_EQ(kind_array[i], kind);
}
ASSERT_EQ(0, pthread_rwlockattr_destroy(&attr));
}
TEST(pthread, pthread_rwlock_init_same_as_PTHREAD_RWLOCK_INITIALIZER) {
pthread_rwlock_t lock1 = PTHREAD_RWLOCK_INITIALIZER;
pthread_rwlock_t lock2;
ASSERT_EQ(0, pthread_rwlock_init(&lock2, NULL));
ASSERT_EQ(0, memcmp(&lock1, &lock2, sizeof(lock1)));
}
TEST(pthread, pthread_rwlock_smoke) {
pthread_rwlock_t l;
ASSERT_EQ(0, pthread_rwlock_init(&l, NULL));
// Single read lock
ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
// Multiple read lock
ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
// Write lock
ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
// Try writer lock
ASSERT_EQ(0, pthread_rwlock_trywrlock(&l));
ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l));
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
// Try reader lock
ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
// Try writer lock after unlock
ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
// EDEADLK in "read after write"
ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l));
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
// EDEADLK in "write after write"
ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l));
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
ASSERT_EQ(0, pthread_rwlock_destroy(&l));
}
struct RwlockWakeupHelperArg {
pthread_rwlock_t lock;
enum Progress {
LOCK_INITIALIZED,
LOCK_WAITING,
LOCK_RELEASED,
LOCK_ACCESSED,
LOCK_TIMEDOUT,
};
std::atomic<Progress> progress;
std::atomic<pid_t> tid;
std::function<int (pthread_rwlock_t*)> trylock_function;
std::function<int (pthread_rwlock_t*)> lock_function;
std::function<int (pthread_rwlock_t*, const timespec*)> timed_lock_function;
clockid_t clock;
};
static void pthread_rwlock_wakeup_helper(RwlockWakeupHelperArg* arg) {
arg->tid = gettid();
ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
ASSERT_EQ(EBUSY, arg->trylock_function(&arg->lock));
ASSERT_EQ(0, arg->lock_function(&arg->lock));
ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress);
ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock));
arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED;
}
static void test_pthread_rwlock_reader_wakeup_writer(std::function<int (pthread_rwlock_t*)> lock_function) {
RwlockWakeupHelperArg wakeup_arg;
ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL));
ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock));
wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
wakeup_arg.tid = 0;
wakeup_arg.trylock_function = &pthread_rwlock_trywrlock;
wakeup_arg.lock_function = lock_function;
pthread_t thread;
ASSERT_EQ(0, pthread_create(&thread, NULL,
reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_helper), &wakeup_arg));
WaitUntilThreadSleep(wakeup_arg.tid);
ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
ASSERT_EQ(0, pthread_join(thread, NULL));
ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress);
ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
}
TEST(pthread, pthread_rwlock_reader_wakeup_writer) {
test_pthread_rwlock_reader_wakeup_writer(pthread_rwlock_wrlock);
}
TEST(pthread, pthread_rwlock_reader_wakeup_writer_timedwait) {
timespec ts;
ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
ts.tv_sec += 1;
test_pthread_rwlock_reader_wakeup_writer([&](pthread_rwlock_t* lock) {
return pthread_rwlock_timedwrlock(lock, &ts);
});
}
TEST(pthread, pthread_rwlock_reader_wakeup_writer_timedwait_monotonic_np) {
#if defined(__BIONIC__)
timespec ts;
ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
ts.tv_sec += 1;
test_pthread_rwlock_reader_wakeup_writer(
[&](pthread_rwlock_t* lock) { return pthread_rwlock_timedwrlock_monotonic_np(lock, &ts); });
#else // __BIONIC__
GTEST_LOG_(INFO) << "This test does nothing since pthread_rwlock_timedwrlock_monotonic_np is "
"only supported on bionic";
#endif // __BIONIC__
}
static void test_pthread_rwlock_writer_wakeup_reader(std::function<int (pthread_rwlock_t*)> lock_function) {
RwlockWakeupHelperArg wakeup_arg;
ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL));
ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock));
wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
wakeup_arg.tid = 0;
wakeup_arg.trylock_function = &pthread_rwlock_tryrdlock;
wakeup_arg.lock_function = lock_function;
pthread_t thread;
ASSERT_EQ(0, pthread_create(&thread, NULL,
reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_helper), &wakeup_arg));
WaitUntilThreadSleep(wakeup_arg.tid);
ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
ASSERT_EQ(0, pthread_join(thread, NULL));
ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress);
ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
}
TEST(pthread, pthread_rwlock_writer_wakeup_reader) {
test_pthread_rwlock_writer_wakeup_reader(pthread_rwlock_rdlock);
}
TEST(pthread, pthread_rwlock_writer_wakeup_reader_timedwait) {
timespec ts;
ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
ts.tv_sec += 1;
test_pthread_rwlock_writer_wakeup_reader([&](pthread_rwlock_t* lock) {
return pthread_rwlock_timedrdlock(lock, &ts);
});
}
TEST(pthread, pthread_rwlock_writer_wakeup_reader_timedwait_monotonic_np) {
#if defined(__BIONIC__)
timespec ts;
ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));