blob: 5558134dfe6fc50873665fa93dd2318e4f468b3f [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 Hughes95646e62023-09-21 14:11:19 -070031#include "utils.h"
32
Elliott Hughes141b9172021-04-09 17:13:09 -070033using semaphore_DeathTest = SilentDeathTest;
Elliott Hughes04303f52014-09-18 16:11:59 -070034
35TEST(semaphore, sem_init) {
36 sem_t s;
37
38 // Perfectly fine initial values.
39 ASSERT_EQ(0, sem_init(&s, 0, 0));
40 ASSERT_EQ(0, sem_init(&s, 0, 1));
41 ASSERT_EQ(0, sem_init(&s, 0, 123));
42
43 // Too small an initial value.
44 errno = 0;
45 ASSERT_EQ(-1, sem_init(&s, 0, -1));
Elliott Hughes95646e62023-09-21 14:11:19 -070046 ASSERT_ERRNO(EINVAL);
Elliott Hughes04303f52014-09-18 16:11:59 -070047
48 ASSERT_EQ(SEM_VALUE_MAX, sysconf(_SC_SEM_VALUE_MAX));
49
50 // The largest initial value.
51 ASSERT_EQ(0, sem_init(&s, 0, SEM_VALUE_MAX));
52
53 // Too large an initial value.
54 errno = 0;
Yi Kong066b5d62023-12-08 19:51:45 +090055 ASSERT_EQ(-1, sem_init(&s, 0, static_cast<unsigned>(SEM_VALUE_MAX) + 1));
Elliott Hughes95646e62023-09-21 14:11:19 -070056 ASSERT_ERRNO(EINVAL);
Elliott Hughes04303f52014-09-18 16:11:59 -070057
58 ASSERT_EQ(0, sem_destroy(&s));
59}
60
61TEST(semaphore, sem_trywait) {
62 sem_t s;
63 ASSERT_EQ(0, sem_init(&s, 0, 3));
64 ASSERT_EQ(0, sem_trywait(&s));
65 ASSERT_EQ(0, sem_trywait(&s));
66 ASSERT_EQ(0, sem_trywait(&s));
67 errno = 0;
68 ASSERT_EQ(-1, sem_trywait(&s));
Elliott Hughes95646e62023-09-21 14:11:19 -070069 ASSERT_ERRNO(EAGAIN);
Elliott Hughes04303f52014-09-18 16:11:59 -070070 ASSERT_EQ(0, sem_destroy(&s));
71}
72
73static void SemWaitThreadTestFn(sem_t& sem) {
74 ASSERT_EQ(0, sem_wait(&sem));
75}
76
77static void* SemWaitThreadFn(void* arg) {
78 SemWaitThreadTestFn(*reinterpret_cast<sem_t*>(arg));
79 return nullptr;
80}
81
82TEST(semaphore, sem_wait__sem_post) {
83 sem_t s;
84 ASSERT_EQ(0, sem_init(&s, 0, 0));
85
86 pthread_t t1, t2, t3;
Yi Kong32bc0fc2018-08-02 17:31:13 -070087 ASSERT_EQ(0, pthread_create(&t1, nullptr, SemWaitThreadFn, &s));
88 ASSERT_EQ(0, pthread_create(&t2, nullptr, SemWaitThreadFn, &s));
89 ASSERT_EQ(0, pthread_create(&t3, nullptr, SemWaitThreadFn, &s));
Elliott Hughes04303f52014-09-18 16:11:59 -070090
91 ASSERT_EQ(0, sem_post(&s));
92 ASSERT_EQ(0, sem_post(&s));
93 ASSERT_EQ(0, sem_post(&s));
94
95 void* result;
96 ASSERT_EQ(0, pthread_join(t1, &result));
97 ASSERT_EQ(0, pthread_join(t2, &result));
98 ASSERT_EQ(0, pthread_join(t3, &result));
99}
100
101static inline void timespec_add_ms(timespec& ts, size_t ms) {
102 ts.tv_sec += ms / 1000;
103 ts.tv_nsec += (ms % 1000) * 1000000;
104 if (ts.tv_nsec >= NS_PER_S) {
105 ts.tv_sec++;
106 ts.tv_nsec -= NS_PER_S;
107 }
108}
109
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800110static void sem_timedwait_helper(clockid_t clock,
111 int (*wait_function)(sem_t* __sem, const timespec* __ts)) {
Elliott Hughes04303f52014-09-18 16:11:59 -0700112 sem_t s;
113 ASSERT_EQ(0, sem_init(&s, 0, 0));
114
115 timespec ts;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800116 ASSERT_EQ(0, clock_gettime(clock, &ts));
Elliott Hughes04303f52014-09-18 16:11:59 -0700117 timespec_add_ms(ts, 100);
118
119 errno = 0;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800120 ASSERT_EQ(-1, wait_function(&s, &ts));
Elliott Hughes95646e62023-09-21 14:11:19 -0700121 ASSERT_ERRNO(ETIMEDOUT);
Elliott Hughes04303f52014-09-18 16:11:59 -0700122
123 // A negative timeout is an error.
124 errno = 0;
125 ts.tv_nsec = -1;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800126 ASSERT_EQ(-1, wait_function(&s, &ts));
Elliott Hughes95646e62023-09-21 14:11:19 -0700127 ASSERT_ERRNO(EINVAL);
Yabin Cuic9a659c2015-11-05 15:36:08 -0800128 errno = 0;
129 ts.tv_nsec = NS_PER_S;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800130 ASSERT_EQ(-1, wait_function(&s, &ts));
Elliott Hughes95646e62023-09-21 14:11:19 -0700131 ASSERT_ERRNO(EINVAL);
Yabin Cuic9a659c2015-11-05 15:36:08 -0800132
133 errno = 0;
134 ts.tv_nsec = NS_PER_S - 1;
135 ts.tv_sec = -1;
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800136 ASSERT_EQ(-1, wait_function(&s, &ts));
Elliott Hughes95646e62023-09-21 14:11:19 -0700137 ASSERT_ERRNO(ETIMEDOUT);
Elliott Hughes04303f52014-09-18 16:11:59 -0700138
139 ASSERT_EQ(0, sem_destroy(&s));
140}
141
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800142TEST(semaphore, sem_timedwait) {
143 sem_timedwait_helper(CLOCK_REALTIME, sem_timedwait);
144}
145
146TEST(semaphore, sem_timedwait_monotonic_np) {
147#if defined(__BIONIC__)
148 sem_timedwait_helper(CLOCK_MONOTONIC, sem_timedwait_monotonic_np);
149#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800150 GTEST_SKIP() << "sem_timedwait_monotonic_np is only supported on bionic";
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800151#endif // __BIONIC__
152}
153
Tom Cherry69010802019-05-07 20:33:05 -0700154TEST(semaphore, sem_clockwait) {
155#if defined(__BIONIC__)
156 sem_timedwait_helper(CLOCK_MONOTONIC, [](sem_t* __sem, const timespec* __ts) {
157 return sem_clockwait(__sem, CLOCK_MONOTONIC, __ts);
158 });
159 sem_timedwait_helper(CLOCK_REALTIME, [](sem_t* __sem, const timespec* __ts) {
160 return sem_clockwait(__sem, CLOCK_REALTIME, __ts);
161 });
162#else // __BIONIC__
163 GTEST_SKIP() << "sem_clockwait is only supported on bionic";
164#endif // __BIONIC__
165}
166
Elliott Hughese657eb42021-02-18 17:11:56 -0800167TEST_F(semaphore_DeathTest, sem_timedwait_null_timeout) {
Elliott Hughesdd586f22015-12-16 15:15:58 -0800168 sem_t s;
169 ASSERT_EQ(0, sem_init(&s, 0, 0));
zijunzhaod3e06522023-03-29 18:20:51 +0000170#pragma clang diagnostic push
171#pragma clang diagnostic ignored "-Wnonnull"
Elliott Hughesdd586f22015-12-16 15:15:58 -0800172 ASSERT_EXIT(sem_timedwait(&s, nullptr), testing::KilledBySignal(SIGSEGV), "");
zijunzhaod3e06522023-03-29 18:20:51 +0000173#pragma clang diagnostic pop
Elliott Hughesdd586f22015-12-16 15:15:58 -0800174}
175
Elliott Hughes04303f52014-09-18 16:11:59 -0700176TEST(semaphore, sem_getvalue) {
177 sem_t s;
178 ASSERT_EQ(0, sem_init(&s, 0, 0));
179
180 int i;
181 ASSERT_EQ(0, sem_getvalue(&s, &i));
182 ASSERT_EQ(0, i);
183
184 ASSERT_EQ(0, sem_post(&s));
185 ASSERT_EQ(0, sem_getvalue(&s, &i));
186 ASSERT_EQ(1, i);
187
188 ASSERT_EQ(0, sem_post(&s));
189 ASSERT_EQ(0, sem_getvalue(&s, &i));
190 ASSERT_EQ(2, i);
191
192 ASSERT_EQ(0, sem_wait(&s));
193 ASSERT_EQ(0, sem_getvalue(&s, &i));
194 ASSERT_EQ(1, i);
195}
Yabin Cuica482742016-01-25 17:38:44 -0800196
Elliott Hughesff1428a2018-11-12 16:01:37 -0800197extern "C" void android_set_application_target_sdk_version(int target);
Yabin Cuica482742016-01-25 17:38:44 -0800198
199static void sem_wait_test_signal_handler(int) {
200}
201
202static void* SemWaitEINTRThreadFn(void* arg) {
203 sem_t* sem = reinterpret_cast<sem_t*>(arg);
204 uintptr_t have_eintr = 0;
205 uintptr_t have_error = 0;
206 while (true) {
207 int result = sem_wait(sem);
208 if (result == 0) {
209 break;
210 }
211 if (result == -1) {
212 if (errno == EINTR) {
213 have_eintr = 1;
214 } else {
215 have_error = 1;
216 break;
217 }
218 }
219 }
220 return reinterpret_cast<void*>((have_eintr << 1) | have_error);
221}
222
223TEST(semaphore, sem_wait_no_EINTR_in_sdk_less_equal_than_23) {
224#if defined(__BIONIC__)
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800225 android_set_application_target_sdk_version(23);
Yabin Cuica482742016-01-25 17:38:44 -0800226 sem_t s;
227 ASSERT_EQ(0, sem_init(&s, 0, 0));
228 ScopedSignalHandler handler(SIGUSR1, sem_wait_test_signal_handler);
229 pthread_t thread;
230 ASSERT_EQ(0, pthread_create(&thread, nullptr, SemWaitEINTRThreadFn, &s));
231 // Give some time for the thread to run sem_wait.
232 usleep(500000);
233 ASSERT_EQ(0, pthread_kill(thread, SIGUSR1));
234 // Give some time for the thread to handle signal.
235 usleep(500000);
236 ASSERT_EQ(0, sem_post(&s));
237 void* result;
238 ASSERT_EQ(0, pthread_join(thread, &result));
239 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(result));
240#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800241 GTEST_SKIP() << "This test tests sem_wait's compatibility for old sdk versions";
Yabin Cuica482742016-01-25 17:38:44 -0800242#endif
243}
244
245TEST(semaphore, sem_wait_EINTR_in_sdk_greater_than_23) {
246#if defined(__BIONIC__)
247 android_set_application_target_sdk_version(24U);
248#endif
249 sem_t s;
250 ASSERT_EQ(0, sem_init(&s, 0, 0));
251 ScopedSignalHandler handler(SIGUSR1, sem_wait_test_signal_handler);
252 pthread_t thread;
253 ASSERT_EQ(0, pthread_create(&thread, nullptr, SemWaitEINTRThreadFn, &s));
254 // Give some time for the thread to run sem_wait.
255 usleep(500000);
256 ASSERT_EQ(0, pthread_kill(thread, SIGUSR1));
257 // Give some time for the thread to handle signal.
258 usleep(500000);
259 ASSERT_EQ(0, sem_post(&s));
260 void* result;
261 ASSERT_EQ(0, pthread_join(thread, &result));
262 ASSERT_EQ(2U, reinterpret_cast<uintptr_t>(result));
263}