blob: ed0fcf17e2740014b60cc740abceefa2d8df633a [file] [log] [blame]
Elliott Hughes04303f52014-09-18 16:11:59 -07001/*
2 * Copyright (C) 2014 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 <semaphore.h>
18
19#include <errno.h>
20#include <gtest/gtest.h>
21#include <limits.h>
22#include <pthread.h>
23#include <time.h>
24#include <unistd.h>
25
Elliott Hughese657eb42021-02-18 17:11:56 -080026#include "BionicDeathTest.h"
Elliott Hughes71ba5892018-02-07 12:44:45 -080027#include "SignalUtils.h"
Elliott Hughese657eb42021-02-18 17:11:56 -080028#include "private/bionic_constants.h"
29
30using semaphore_DeathTest = BionicDeathTest;
Elliott Hughes04303f52014-09-18 16:11:59 -070031
32TEST(semaphore, sem_init) {
33 sem_t s;
34
35 // Perfectly fine initial values.
36 ASSERT_EQ(0, sem_init(&s, 0, 0));
37 ASSERT_EQ(0, sem_init(&s, 0, 1));
38 ASSERT_EQ(0, sem_init(&s, 0, 123));
39
40 // Too small an initial value.
41 errno = 0;
42 ASSERT_EQ(-1, sem_init(&s, 0, -1));
43 ASSERT_EQ(EINVAL, errno);
44
45 ASSERT_EQ(SEM_VALUE_MAX, sysconf(_SC_SEM_VALUE_MAX));
46
47 // The largest initial value.
48 ASSERT_EQ(0, sem_init(&s, 0, SEM_VALUE_MAX));
49
50 // Too large an initial value.
51 errno = 0;
52 ASSERT_EQ(-1, sem_init(&s, 0, SEM_VALUE_MAX + 1));
53 ASSERT_EQ(EINVAL, errno);
54
55 ASSERT_EQ(0, sem_destroy(&s));
56}
57
58TEST(semaphore, sem_trywait) {
59 sem_t s;
60 ASSERT_EQ(0, sem_init(&s, 0, 3));
61 ASSERT_EQ(0, sem_trywait(&s));
62 ASSERT_EQ(0, sem_trywait(&s));
63 ASSERT_EQ(0, sem_trywait(&s));
64 errno = 0;
65 ASSERT_EQ(-1, sem_trywait(&s));
66 ASSERT_EQ(EAGAIN, errno);
67 ASSERT_EQ(0, sem_destroy(&s));
68}
69
70static void SemWaitThreadTestFn(sem_t& sem) {
71 ASSERT_EQ(0, sem_wait(&sem));
72}
73
74static void* SemWaitThreadFn(void* arg) {
75 SemWaitThreadTestFn(*reinterpret_cast<sem_t*>(arg));
76 return nullptr;
77}
78
79TEST(semaphore, sem_wait__sem_post) {
80 sem_t s;
81 ASSERT_EQ(0, sem_init(&s, 0, 0));
82
83 pthread_t t1, t2, t3;
Yi Kong32bc0fc2018-08-02 17:31:13 -070084 ASSERT_EQ(0, pthread_create(&t1, nullptr, SemWaitThreadFn, &s));
85 ASSERT_EQ(0, pthread_create(&t2, nullptr, SemWaitThreadFn, &s));
86 ASSERT_EQ(0, pthread_create(&t3, nullptr, SemWaitThreadFn, &s));
Elliott Hughes04303f52014-09-18 16:11:59 -070087
88 ASSERT_EQ(0, sem_post(&s));
89 ASSERT_EQ(0, sem_post(&s));
90 ASSERT_EQ(0, sem_post(&s));
91
92 void* result;
93 ASSERT_EQ(0, pthread_join(t1, &result));
94 ASSERT_EQ(0, pthread_join(t2, &result));
95 ASSERT_EQ(0, pthread_join(t3, &result));
96}
97
98static inline void timespec_add_ms(timespec& ts, size_t ms) {
99 ts.tv_sec += ms / 1000;
100 ts.tv_nsec += (ms % 1000) * 1000000;
101 if (ts.tv_nsec >= NS_PER_S) {
102 ts.tv_sec++;
103 ts.tv_nsec -= NS_PER_S;
104 }
105}
106
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800107static void sem_timedwait_helper(clockid_t clock,
108 int (*wait_function)(sem_t* __sem, const timespec* __ts)) {
Elliott Hughes04303f52014-09-18 16:11:59 -0700109 sem_t s;
110 ASSERT_EQ(0, sem_init(&s, 0, 0));
111
112 timespec ts;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800113 ASSERT_EQ(0, clock_gettime(clock, &ts));
Elliott Hughes04303f52014-09-18 16:11:59 -0700114 timespec_add_ms(ts, 100);
115
116 errno = 0;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800117 ASSERT_EQ(-1, wait_function(&s, &ts));
Elliott Hughes04303f52014-09-18 16:11:59 -0700118 ASSERT_EQ(ETIMEDOUT, errno);
119
120 // A negative timeout is an error.
121 errno = 0;
122 ts.tv_nsec = -1;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800123 ASSERT_EQ(-1, wait_function(&s, &ts));
Elliott Hughes04303f52014-09-18 16:11:59 -0700124 ASSERT_EQ(EINVAL, errno);
Yabin Cuic9a659c2015-11-05 15:36:08 -0800125 errno = 0;
126 ts.tv_nsec = NS_PER_S;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800127 ASSERT_EQ(-1, wait_function(&s, &ts));
Yabin Cuic9a659c2015-11-05 15:36:08 -0800128 ASSERT_EQ(EINVAL, errno);
129
130 errno = 0;
131 ts.tv_nsec = NS_PER_S - 1;
132 ts.tv_sec = -1;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800133 ASSERT_EQ(-1, wait_function(&s, &ts));
Yabin Cuic9a659c2015-11-05 15:36:08 -0800134 ASSERT_EQ(ETIMEDOUT, errno);
Elliott Hughes04303f52014-09-18 16:11:59 -0700135
136 ASSERT_EQ(0, sem_destroy(&s));
137}
138
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800139TEST(semaphore, sem_timedwait) {
140 sem_timedwait_helper(CLOCK_REALTIME, sem_timedwait);
141}
142
143TEST(semaphore, sem_timedwait_monotonic_np) {
144#if defined(__BIONIC__)
145 sem_timedwait_helper(CLOCK_MONOTONIC, sem_timedwait_monotonic_np);
146#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800147 GTEST_SKIP() << "sem_timedwait_monotonic_np is only supported on bionic";
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800148#endif // __BIONIC__
149}
150
Tom Cherry69010802019-05-07 20:33:05 -0700151TEST(semaphore, sem_clockwait) {
152#if defined(__BIONIC__)
153 sem_timedwait_helper(CLOCK_MONOTONIC, [](sem_t* __sem, const timespec* __ts) {
154 return sem_clockwait(__sem, CLOCK_MONOTONIC, __ts);
155 });
156 sem_timedwait_helper(CLOCK_REALTIME, [](sem_t* __sem, const timespec* __ts) {
157 return sem_clockwait(__sem, CLOCK_REALTIME, __ts);
158 });
159#else // __BIONIC__
160 GTEST_SKIP() << "sem_clockwait is only supported on bionic";
161#endif // __BIONIC__
162}
163
Elliott Hughese657eb42021-02-18 17:11:56 -0800164TEST_F(semaphore_DeathTest, sem_timedwait_null_timeout) {
Elliott Hughesdd586f22015-12-16 15:15:58 -0800165 sem_t s;
166 ASSERT_EQ(0, sem_init(&s, 0, 0));
167
168 ASSERT_EXIT(sem_timedwait(&s, nullptr), testing::KilledBySignal(SIGSEGV), "");
169}
170
Elliott Hughes04303f52014-09-18 16:11:59 -0700171TEST(semaphore, sem_getvalue) {
172 sem_t s;
173 ASSERT_EQ(0, sem_init(&s, 0, 0));
174
175 int i;
176 ASSERT_EQ(0, sem_getvalue(&s, &i));
177 ASSERT_EQ(0, i);
178
179 ASSERT_EQ(0, sem_post(&s));
180 ASSERT_EQ(0, sem_getvalue(&s, &i));
181 ASSERT_EQ(1, i);
182
183 ASSERT_EQ(0, sem_post(&s));
184 ASSERT_EQ(0, sem_getvalue(&s, &i));
185 ASSERT_EQ(2, i);
186
187 ASSERT_EQ(0, sem_wait(&s));
188 ASSERT_EQ(0, sem_getvalue(&s, &i));
189 ASSERT_EQ(1, i);
190}
Yabin Cuica482742016-01-25 17:38:44 -0800191
Elliott Hughesff1428a2018-11-12 16:01:37 -0800192extern "C" void android_set_application_target_sdk_version(int target);
Yabin Cuica482742016-01-25 17:38:44 -0800193
194static void sem_wait_test_signal_handler(int) {
195}
196
197static void* SemWaitEINTRThreadFn(void* arg) {
198 sem_t* sem = reinterpret_cast<sem_t*>(arg);
199 uintptr_t have_eintr = 0;
200 uintptr_t have_error = 0;
201 while (true) {
202 int result = sem_wait(sem);
203 if (result == 0) {
204 break;
205 }
206 if (result == -1) {
207 if (errno == EINTR) {
208 have_eintr = 1;
209 } else {
210 have_error = 1;
211 break;
212 }
213 }
214 }
215 return reinterpret_cast<void*>((have_eintr << 1) | have_error);
216}
217
218TEST(semaphore, sem_wait_no_EINTR_in_sdk_less_equal_than_23) {
219#if defined(__BIONIC__)
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800220 android_set_application_target_sdk_version(23);
Yabin Cuica482742016-01-25 17:38:44 -0800221 sem_t s;
222 ASSERT_EQ(0, sem_init(&s, 0, 0));
223 ScopedSignalHandler handler(SIGUSR1, sem_wait_test_signal_handler);
224 pthread_t thread;
225 ASSERT_EQ(0, pthread_create(&thread, nullptr, SemWaitEINTRThreadFn, &s));
226 // Give some time for the thread to run sem_wait.
227 usleep(500000);
228 ASSERT_EQ(0, pthread_kill(thread, SIGUSR1));
229 // Give some time for the thread to handle signal.
230 usleep(500000);
231 ASSERT_EQ(0, sem_post(&s));
232 void* result;
233 ASSERT_EQ(0, pthread_join(thread, &result));
234 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(result));
235#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800236 GTEST_SKIP() << "This test tests sem_wait's compatibility for old sdk versions";
Yabin Cuica482742016-01-25 17:38:44 -0800237#endif
238}
239
240TEST(semaphore, sem_wait_EINTR_in_sdk_greater_than_23) {
241#if defined(__BIONIC__)
242 android_set_application_target_sdk_version(24U);
243#endif
244 sem_t s;
245 ASSERT_EQ(0, sem_init(&s, 0, 0));
246 ScopedSignalHandler handler(SIGUSR1, sem_wait_test_signal_handler);
247 pthread_t thread;
248 ASSERT_EQ(0, pthread_create(&thread, nullptr, SemWaitEINTRThreadFn, &s));
249 // Give some time for the thread to run sem_wait.
250 usleep(500000);
251 ASSERT_EQ(0, pthread_kill(thread, SIGUSR1));
252 // Give some time for the thread to handle signal.
253 usleep(500000);
254 ASSERT_EQ(0, sem_post(&s));
255 void* result;
256 ASSERT_EQ(0, pthread_join(thread, &result));
257 ASSERT_EQ(2U, reinterpret_cast<uintptr_t>(result));
258}