blob: 6f8797f08e60d0a2ec4d62a526f9c9251719b3f0 [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>
Elliott Hughes04303f52014-09-18 16:11:59 -070020#include <limits.h>
21#include <pthread.h>
22#include <time.h>
23#include <unistd.h>
24
Elliott Hughes141b9172021-04-09 17:13:09 -070025#include <android-base/silent_death_test.h>
26#include <gtest/gtest.h>
27
Elliott Hughes71ba5892018-02-07 12:44:45 -080028#include "SignalUtils.h"
Elliott Hughese657eb42021-02-18 17:11:56 -080029#include "private/bionic_constants.h"
30
Elliott Hughes141b9172021-04-09 17:13:09 -070031using semaphore_DeathTest = SilentDeathTest;
Elliott Hughes04303f52014-09-18 16:11:59 -070032
33TEST(semaphore, sem_init) {
34 sem_t s;
35
36 // Perfectly fine initial values.
37 ASSERT_EQ(0, sem_init(&s, 0, 0));
38 ASSERT_EQ(0, sem_init(&s, 0, 1));
39 ASSERT_EQ(0, sem_init(&s, 0, 123));
40
41 // Too small an initial value.
42 errno = 0;
43 ASSERT_EQ(-1, sem_init(&s, 0, -1));
44 ASSERT_EQ(EINVAL, errno);
45
46 ASSERT_EQ(SEM_VALUE_MAX, sysconf(_SC_SEM_VALUE_MAX));
47
48 // The largest initial value.
49 ASSERT_EQ(0, sem_init(&s, 0, SEM_VALUE_MAX));
50
51 // Too large an initial value.
52 errno = 0;
53 ASSERT_EQ(-1, sem_init(&s, 0, SEM_VALUE_MAX + 1));
54 ASSERT_EQ(EINVAL, errno);
55
56 ASSERT_EQ(0, sem_destroy(&s));
57}
58
59TEST(semaphore, sem_trywait) {
60 sem_t s;
61 ASSERT_EQ(0, sem_init(&s, 0, 3));
62 ASSERT_EQ(0, sem_trywait(&s));
63 ASSERT_EQ(0, sem_trywait(&s));
64 ASSERT_EQ(0, sem_trywait(&s));
65 errno = 0;
66 ASSERT_EQ(-1, sem_trywait(&s));
67 ASSERT_EQ(EAGAIN, errno);
68 ASSERT_EQ(0, sem_destroy(&s));
69}
70
71static void SemWaitThreadTestFn(sem_t& sem) {
72 ASSERT_EQ(0, sem_wait(&sem));
73}
74
75static void* SemWaitThreadFn(void* arg) {
76 SemWaitThreadTestFn(*reinterpret_cast<sem_t*>(arg));
77 return nullptr;
78}
79
80TEST(semaphore, sem_wait__sem_post) {
81 sem_t s;
82 ASSERT_EQ(0, sem_init(&s, 0, 0));
83
84 pthread_t t1, t2, t3;
Yi Kong32bc0fc2018-08-02 17:31:13 -070085 ASSERT_EQ(0, pthread_create(&t1, nullptr, SemWaitThreadFn, &s));
86 ASSERT_EQ(0, pthread_create(&t2, nullptr, SemWaitThreadFn, &s));
87 ASSERT_EQ(0, pthread_create(&t3, nullptr, SemWaitThreadFn, &s));
Elliott Hughes04303f52014-09-18 16:11:59 -070088
89 ASSERT_EQ(0, sem_post(&s));
90 ASSERT_EQ(0, sem_post(&s));
91 ASSERT_EQ(0, sem_post(&s));
92
93 void* result;
94 ASSERT_EQ(0, pthread_join(t1, &result));
95 ASSERT_EQ(0, pthread_join(t2, &result));
96 ASSERT_EQ(0, pthread_join(t3, &result));
97}
98
99static inline void timespec_add_ms(timespec& ts, size_t ms) {
100 ts.tv_sec += ms / 1000;
101 ts.tv_nsec += (ms % 1000) * 1000000;
102 if (ts.tv_nsec >= NS_PER_S) {
103 ts.tv_sec++;
104 ts.tv_nsec -= NS_PER_S;
105 }
106}
107
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800108static void sem_timedwait_helper(clockid_t clock,
109 int (*wait_function)(sem_t* __sem, const timespec* __ts)) {
Elliott Hughes04303f52014-09-18 16:11:59 -0700110 sem_t s;
111 ASSERT_EQ(0, sem_init(&s, 0, 0));
112
113 timespec ts;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800114 ASSERT_EQ(0, clock_gettime(clock, &ts));
Elliott Hughes04303f52014-09-18 16:11:59 -0700115 timespec_add_ms(ts, 100);
116
117 errno = 0;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800118 ASSERT_EQ(-1, wait_function(&s, &ts));
Elliott Hughes04303f52014-09-18 16:11:59 -0700119 ASSERT_EQ(ETIMEDOUT, errno);
120
121 // A negative timeout is an error.
122 errno = 0;
123 ts.tv_nsec = -1;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800124 ASSERT_EQ(-1, wait_function(&s, &ts));
Elliott Hughes04303f52014-09-18 16:11:59 -0700125 ASSERT_EQ(EINVAL, errno);
Yabin Cuic9a659c2015-11-05 15:36:08 -0800126 errno = 0;
127 ts.tv_nsec = NS_PER_S;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800128 ASSERT_EQ(-1, wait_function(&s, &ts));
Yabin Cuic9a659c2015-11-05 15:36:08 -0800129 ASSERT_EQ(EINVAL, errno);
130
131 errno = 0;
132 ts.tv_nsec = NS_PER_S - 1;
133 ts.tv_sec = -1;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800134 ASSERT_EQ(-1, wait_function(&s, &ts));
Yabin Cuic9a659c2015-11-05 15:36:08 -0800135 ASSERT_EQ(ETIMEDOUT, errno);
Elliott Hughes04303f52014-09-18 16:11:59 -0700136
137 ASSERT_EQ(0, sem_destroy(&s));
138}
139
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800140TEST(semaphore, sem_timedwait) {
141 sem_timedwait_helper(CLOCK_REALTIME, sem_timedwait);
142}
143
144TEST(semaphore, sem_timedwait_monotonic_np) {
145#if defined(__BIONIC__)
146 sem_timedwait_helper(CLOCK_MONOTONIC, sem_timedwait_monotonic_np);
147#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800148 GTEST_SKIP() << "sem_timedwait_monotonic_np is only supported on bionic";
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800149#endif // __BIONIC__
150}
151
Tom Cherry69010802019-05-07 20:33:05 -0700152TEST(semaphore, sem_clockwait) {
153#if defined(__BIONIC__)
154 sem_timedwait_helper(CLOCK_MONOTONIC, [](sem_t* __sem, const timespec* __ts) {
155 return sem_clockwait(__sem, CLOCK_MONOTONIC, __ts);
156 });
157 sem_timedwait_helper(CLOCK_REALTIME, [](sem_t* __sem, const timespec* __ts) {
158 return sem_clockwait(__sem, CLOCK_REALTIME, __ts);
159 });
160#else // __BIONIC__
161 GTEST_SKIP() << "sem_clockwait is only supported on bionic";
162#endif // __BIONIC__
163}
164
Elliott Hughese657eb42021-02-18 17:11:56 -0800165TEST_F(semaphore_DeathTest, sem_timedwait_null_timeout) {
Elliott Hughesdd586f22015-12-16 15:15:58 -0800166 sem_t s;
167 ASSERT_EQ(0, sem_init(&s, 0, 0));
zijunzhaod3e06522023-03-29 18:20:51 +0000168#pragma clang diagnostic push
169#pragma clang diagnostic ignored "-Wnonnull"
Elliott Hughesdd586f22015-12-16 15:15:58 -0800170 ASSERT_EXIT(sem_timedwait(&s, nullptr), testing::KilledBySignal(SIGSEGV), "");
zijunzhaod3e06522023-03-29 18:20:51 +0000171#pragma clang diagnostic pop
Elliott Hughesdd586f22015-12-16 15:15:58 -0800172}
173
Elliott Hughes04303f52014-09-18 16:11:59 -0700174TEST(semaphore, sem_getvalue) {
175 sem_t s;
176 ASSERT_EQ(0, sem_init(&s, 0, 0));
177
178 int i;
179 ASSERT_EQ(0, sem_getvalue(&s, &i));
180 ASSERT_EQ(0, i);
181
182 ASSERT_EQ(0, sem_post(&s));
183 ASSERT_EQ(0, sem_getvalue(&s, &i));
184 ASSERT_EQ(1, i);
185
186 ASSERT_EQ(0, sem_post(&s));
187 ASSERT_EQ(0, sem_getvalue(&s, &i));
188 ASSERT_EQ(2, i);
189
190 ASSERT_EQ(0, sem_wait(&s));
191 ASSERT_EQ(0, sem_getvalue(&s, &i));
192 ASSERT_EQ(1, i);
193}
Yabin Cuica482742016-01-25 17:38:44 -0800194
Elliott Hughesff1428a2018-11-12 16:01:37 -0800195extern "C" void android_set_application_target_sdk_version(int target);
Yabin Cuica482742016-01-25 17:38:44 -0800196
197static void sem_wait_test_signal_handler(int) {
198}
199
200static void* SemWaitEINTRThreadFn(void* arg) {
201 sem_t* sem = reinterpret_cast<sem_t*>(arg);
202 uintptr_t have_eintr = 0;
203 uintptr_t have_error = 0;
204 while (true) {
205 int result = sem_wait(sem);
206 if (result == 0) {
207 break;
208 }
209 if (result == -1) {
210 if (errno == EINTR) {
211 have_eintr = 1;
212 } else {
213 have_error = 1;
214 break;
215 }
216 }
217 }
218 return reinterpret_cast<void*>((have_eintr << 1) | have_error);
219}
220
221TEST(semaphore, sem_wait_no_EINTR_in_sdk_less_equal_than_23) {
222#if defined(__BIONIC__)
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800223 android_set_application_target_sdk_version(23);
Yabin Cuica482742016-01-25 17:38:44 -0800224 sem_t s;
225 ASSERT_EQ(0, sem_init(&s, 0, 0));
226 ScopedSignalHandler handler(SIGUSR1, sem_wait_test_signal_handler);
227 pthread_t thread;
228 ASSERT_EQ(0, pthread_create(&thread, nullptr, SemWaitEINTRThreadFn, &s));
229 // Give some time for the thread to run sem_wait.
230 usleep(500000);
231 ASSERT_EQ(0, pthread_kill(thread, SIGUSR1));
232 // Give some time for the thread to handle signal.
233 usleep(500000);
234 ASSERT_EQ(0, sem_post(&s));
235 void* result;
236 ASSERT_EQ(0, pthread_join(thread, &result));
237 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(result));
238#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800239 GTEST_SKIP() << "This test tests sem_wait's compatibility for old sdk versions";
Yabin Cuica482742016-01-25 17:38:44 -0800240#endif
241}
242
243TEST(semaphore, sem_wait_EINTR_in_sdk_greater_than_23) {
244#if defined(__BIONIC__)
245 android_set_application_target_sdk_version(24U);
246#endif
247 sem_t s;
248 ASSERT_EQ(0, sem_init(&s, 0, 0));
249 ScopedSignalHandler handler(SIGUSR1, sem_wait_test_signal_handler);
250 pthread_t thread;
251 ASSERT_EQ(0, pthread_create(&thread, nullptr, SemWaitEINTRThreadFn, &s));
252 // Give some time for the thread to run sem_wait.
253 usleep(500000);
254 ASSERT_EQ(0, pthread_kill(thread, SIGUSR1));
255 // Give some time for the thread to handle signal.
256 usleep(500000);
257 ASSERT_EQ(0, sem_post(&s));
258 void* result;
259 ASSERT_EQ(0, pthread_join(thread, &result));
260 ASSERT_EQ(2U, reinterpret_cast<uintptr_t>(result));
261}