1 /*
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 <limits.h>
21 #include <pthread.h>
22 #include <time.h>
23 #include <unistd.h>
24 
25 #include <android-base/silent_death_test.h>
26 #include <gtest/gtest.h>
27 
28 #include "SignalUtils.h"
29 #include "private/bionic_constants.h"
30 
31 #include "utils.h"
32 
33 using semaphore_DeathTest = SilentDeathTest;
34 
TEST(semaphore,sem_init)35 TEST(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));
46   ASSERT_ERRNO(EINVAL);
47 
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;
55   ASSERT_EQ(-1, sem_init(&s, 0, static_cast<unsigned>(SEM_VALUE_MAX) + 1));
56   ASSERT_ERRNO(EINVAL);
57 
58   ASSERT_EQ(0, sem_destroy(&s));
59 }
60 
TEST(semaphore,sem_trywait)61 TEST(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));
69   ASSERT_ERRNO(EAGAIN);
70   ASSERT_EQ(0, sem_destroy(&s));
71 }
72 
SemWaitThreadTestFn(sem_t & sem)73 static void SemWaitThreadTestFn(sem_t& sem) {
74   ASSERT_EQ(0, sem_wait(&sem));
75 }
76 
SemWaitThreadFn(void * arg)77 static void* SemWaitThreadFn(void* arg) {
78   SemWaitThreadTestFn(*reinterpret_cast<sem_t*>(arg));
79   return nullptr;
80 }
81 
TEST(semaphore,sem_wait__sem_post)82 TEST(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;
87   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));
90 
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 
timespec_add_ms(timespec & ts,size_t ms)101 static 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 
sem_timedwait_helper(clockid_t clock,int (* wait_function)(sem_t * __sem,const timespec * __ts))110 static void sem_timedwait_helper(clockid_t clock,
111                                  int (*wait_function)(sem_t* __sem, const timespec* __ts)) {
112   sem_t s;
113   ASSERT_EQ(0, sem_init(&s, 0, 0));
114 
115   timespec ts;
116   ASSERT_EQ(0, clock_gettime(clock, &ts));
117   timespec_add_ms(ts, 100);
118 
119   errno = 0;
120   ASSERT_EQ(-1, wait_function(&s, &ts));
121   ASSERT_ERRNO(ETIMEDOUT);
122 
123   // A negative timeout is an error.
124   errno = 0;
125   ts.tv_nsec = -1;
126   ASSERT_EQ(-1, wait_function(&s, &ts));
127   ASSERT_ERRNO(EINVAL);
128   errno = 0;
129   ts.tv_nsec = NS_PER_S;
130   ASSERT_EQ(-1, wait_function(&s, &ts));
131   ASSERT_ERRNO(EINVAL);
132 
133   errno = 0;
134   ts.tv_nsec = NS_PER_S - 1;
135   ts.tv_sec = -1;
136   ASSERT_EQ(-1, wait_function(&s, &ts));
137   ASSERT_ERRNO(ETIMEDOUT);
138 
139   ASSERT_EQ(0, sem_destroy(&s));
140 }
141 
TEST(semaphore,sem_timedwait)142 TEST(semaphore, sem_timedwait) {
143   sem_timedwait_helper(CLOCK_REALTIME, sem_timedwait);
144 }
145 
TEST(semaphore,sem_timedwait_monotonic_np)146 TEST(semaphore, sem_timedwait_monotonic_np) {
147 #if defined(__BIONIC__)
148   sem_timedwait_helper(CLOCK_MONOTONIC, sem_timedwait_monotonic_np);
149 #else   // __BIONIC__
150   GTEST_SKIP() << "sem_timedwait_monotonic_np is only supported on bionic";
151 #endif  // __BIONIC__
152 }
153 
TEST(semaphore,sem_clockwait)154 TEST(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 
TEST_F(semaphore_DeathTest,sem_timedwait_null_timeout)167 TEST_F(semaphore_DeathTest, sem_timedwait_null_timeout) {
168   sem_t s;
169   ASSERT_EQ(0, sem_init(&s, 0, 0));
170 #pragma clang diagnostic push
171 #pragma clang diagnostic ignored "-Wnonnull"
172   ASSERT_EXIT(sem_timedwait(&s, nullptr), testing::KilledBySignal(SIGSEGV), "");
173 #pragma clang diagnostic pop
174 }
175 
TEST(semaphore,sem_getvalue)176 TEST(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 }
196 
197 extern "C" void android_set_application_target_sdk_version(int target);
198 
sem_wait_test_signal_handler(int)199 static void sem_wait_test_signal_handler(int) {
200 }
201 
SemWaitEINTRThreadFn(void * arg)202 static 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 
TEST(semaphore,sem_wait_no_EINTR_in_sdk_less_equal_than_23)223 TEST(semaphore, sem_wait_no_EINTR_in_sdk_less_equal_than_23) {
224 #if defined(__BIONIC__)
225   android_set_application_target_sdk_version(23);
226   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
241   GTEST_SKIP() << "This test tests sem_wait's compatibility for old sdk versions";
242 #endif
243 }
244 
TEST(semaphore,sem_wait_EINTR_in_sdk_greater_than_23)245 TEST(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 }
264