Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 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> |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 20 | #include <limits.h> |
| 21 | #include <pthread.h> |
| 22 | #include <time.h> |
| 23 | #include <unistd.h> |
| 24 | |
Elliott Hughes | 141b917 | 2021-04-09 17:13:09 -0700 | [diff] [blame] | 25 | #include <android-base/silent_death_test.h> |
| 26 | #include <gtest/gtest.h> |
| 27 | |
Elliott Hughes | 71ba589 | 2018-02-07 12:44:45 -0800 | [diff] [blame] | 28 | #include "SignalUtils.h" |
Elliott Hughes | e657eb4 | 2021-02-18 17:11:56 -0800 | [diff] [blame] | 29 | #include "private/bionic_constants.h" |
| 30 | |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 31 | #include "utils.h" |
| 32 | |
Elliott Hughes | 141b917 | 2021-04-09 17:13:09 -0700 | [diff] [blame] | 33 | using semaphore_DeathTest = SilentDeathTest; |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 34 | |
| 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)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 46 | ASSERT_ERRNO(EINVAL); |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 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; |
Yi Kong | 066b5d6 | 2023-12-08 19:51:45 +0900 | [diff] [blame^] | 55 | ASSERT_EQ(-1, sem_init(&s, 0, static_cast<unsigned>(SEM_VALUE_MAX) + 1)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 56 | ASSERT_ERRNO(EINVAL); |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 57 | |
| 58 | ASSERT_EQ(0, sem_destroy(&s)); |
| 59 | } |
| 60 | |
| 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)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 69 | ASSERT_ERRNO(EAGAIN); |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 70 | ASSERT_EQ(0, sem_destroy(&s)); |
| 71 | } |
| 72 | |
| 73 | static void SemWaitThreadTestFn(sem_t& sem) { |
| 74 | ASSERT_EQ(0, sem_wait(&sem)); |
| 75 | } |
| 76 | |
| 77 | static void* SemWaitThreadFn(void* arg) { |
| 78 | SemWaitThreadTestFn(*reinterpret_cast<sem_t*>(arg)); |
| 79 | return nullptr; |
| 80 | } |
| 81 | |
| 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; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 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)); |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 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 | |
| 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 | |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 110 | static void sem_timedwait_helper(clockid_t clock, |
| 111 | int (*wait_function)(sem_t* __sem, const timespec* __ts)) { |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 112 | sem_t s; |
| 113 | ASSERT_EQ(0, sem_init(&s, 0, 0)); |
| 114 | |
| 115 | timespec ts; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 116 | ASSERT_EQ(0, clock_gettime(clock, &ts)); |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 117 | timespec_add_ms(ts, 100); |
| 118 | |
| 119 | errno = 0; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 120 | ASSERT_EQ(-1, wait_function(&s, &ts)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 121 | ASSERT_ERRNO(ETIMEDOUT); |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 122 | |
| 123 | // A negative timeout is an error. |
| 124 | errno = 0; |
| 125 | ts.tv_nsec = -1; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 126 | ASSERT_EQ(-1, wait_function(&s, &ts)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 127 | ASSERT_ERRNO(EINVAL); |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 128 | errno = 0; |
| 129 | ts.tv_nsec = NS_PER_S; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 130 | ASSERT_EQ(-1, wait_function(&s, &ts)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 131 | ASSERT_ERRNO(EINVAL); |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 132 | |
| 133 | errno = 0; |
| 134 | ts.tv_nsec = NS_PER_S - 1; |
| 135 | ts.tv_sec = -1; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 136 | ASSERT_EQ(-1, wait_function(&s, &ts)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 137 | ASSERT_ERRNO(ETIMEDOUT); |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 138 | |
| 139 | ASSERT_EQ(0, sem_destroy(&s)); |
| 140 | } |
| 141 | |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 142 | TEST(semaphore, sem_timedwait) { |
| 143 | sem_timedwait_helper(CLOCK_REALTIME, sem_timedwait); |
| 144 | } |
| 145 | |
| 146 | TEST(semaphore, sem_timedwait_monotonic_np) { |
| 147 | #if defined(__BIONIC__) |
| 148 | sem_timedwait_helper(CLOCK_MONOTONIC, sem_timedwait_monotonic_np); |
| 149 | #else // __BIONIC__ |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 150 | GTEST_SKIP() << "sem_timedwait_monotonic_np is only supported on bionic"; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 151 | #endif // __BIONIC__ |
| 152 | } |
| 153 | |
Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | e657eb4 | 2021-02-18 17:11:56 -0800 | [diff] [blame] | 167 | TEST_F(semaphore_DeathTest, sem_timedwait_null_timeout) { |
Elliott Hughes | dd586f2 | 2015-12-16 15:15:58 -0800 | [diff] [blame] | 168 | sem_t s; |
| 169 | ASSERT_EQ(0, sem_init(&s, 0, 0)); |
zijunzhao | d3e0652 | 2023-03-29 18:20:51 +0000 | [diff] [blame] | 170 | #pragma clang diagnostic push |
| 171 | #pragma clang diagnostic ignored "-Wnonnull" |
Elliott Hughes | dd586f2 | 2015-12-16 15:15:58 -0800 | [diff] [blame] | 172 | ASSERT_EXIT(sem_timedwait(&s, nullptr), testing::KilledBySignal(SIGSEGV), ""); |
zijunzhao | d3e0652 | 2023-03-29 18:20:51 +0000 | [diff] [blame] | 173 | #pragma clang diagnostic pop |
Elliott Hughes | dd586f2 | 2015-12-16 15:15:58 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 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 | } |
Yabin Cui | ca48274 | 2016-01-25 17:38:44 -0800 | [diff] [blame] | 196 | |
Elliott Hughes | ff1428a | 2018-11-12 16:01:37 -0800 | [diff] [blame] | 197 | extern "C" void android_set_application_target_sdk_version(int target); |
Yabin Cui | ca48274 | 2016-01-25 17:38:44 -0800 | [diff] [blame] | 198 | |
| 199 | static void sem_wait_test_signal_handler(int) { |
| 200 | } |
| 201 | |
| 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 | |
| 223 | TEST(semaphore, sem_wait_no_EINTR_in_sdk_less_equal_than_23) { |
| 224 | #if defined(__BIONIC__) |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 225 | android_set_application_target_sdk_version(23); |
Yabin Cui | ca48274 | 2016-01-25 17:38:44 -0800 | [diff] [blame] | 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 |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 241 | GTEST_SKIP() << "This test tests sem_wait's compatibility for old sdk versions"; |
Yabin Cui | ca48274 | 2016-01-25 17:38:44 -0800 | [diff] [blame] | 242 | #endif |
| 243 | } |
| 244 | |
| 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 | } |