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> |
| 20 | #include <gtest/gtest.h> |
| 21 | #include <limits.h> |
| 22 | #include <pthread.h> |
| 23 | #include <time.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include "private/bionic_constants.h" |
Elliott Hughes | 71ba589 | 2018-02-07 12:44:45 -0800 | [diff] [blame] | 27 | #include "SignalUtils.h" |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 28 | |
| 29 | TEST(semaphore, sem_init) { |
| 30 | sem_t s; |
| 31 | |
| 32 | // Perfectly fine initial values. |
| 33 | ASSERT_EQ(0, sem_init(&s, 0, 0)); |
| 34 | ASSERT_EQ(0, sem_init(&s, 0, 1)); |
| 35 | ASSERT_EQ(0, sem_init(&s, 0, 123)); |
| 36 | |
| 37 | // Too small an initial value. |
| 38 | errno = 0; |
| 39 | ASSERT_EQ(-1, sem_init(&s, 0, -1)); |
| 40 | ASSERT_EQ(EINVAL, errno); |
| 41 | |
| 42 | ASSERT_EQ(SEM_VALUE_MAX, sysconf(_SC_SEM_VALUE_MAX)); |
| 43 | |
| 44 | // The largest initial value. |
| 45 | ASSERT_EQ(0, sem_init(&s, 0, SEM_VALUE_MAX)); |
| 46 | |
| 47 | // Too large an initial value. |
| 48 | errno = 0; |
| 49 | ASSERT_EQ(-1, sem_init(&s, 0, SEM_VALUE_MAX + 1)); |
| 50 | ASSERT_EQ(EINVAL, errno); |
| 51 | |
| 52 | ASSERT_EQ(0, sem_destroy(&s)); |
| 53 | } |
| 54 | |
| 55 | TEST(semaphore, sem_trywait) { |
| 56 | sem_t s; |
| 57 | ASSERT_EQ(0, sem_init(&s, 0, 3)); |
| 58 | ASSERT_EQ(0, sem_trywait(&s)); |
| 59 | ASSERT_EQ(0, sem_trywait(&s)); |
| 60 | ASSERT_EQ(0, sem_trywait(&s)); |
| 61 | errno = 0; |
| 62 | ASSERT_EQ(-1, sem_trywait(&s)); |
| 63 | ASSERT_EQ(EAGAIN, errno); |
| 64 | ASSERT_EQ(0, sem_destroy(&s)); |
| 65 | } |
| 66 | |
| 67 | static void SemWaitThreadTestFn(sem_t& sem) { |
| 68 | ASSERT_EQ(0, sem_wait(&sem)); |
| 69 | } |
| 70 | |
| 71 | static void* SemWaitThreadFn(void* arg) { |
| 72 | SemWaitThreadTestFn(*reinterpret_cast<sem_t*>(arg)); |
| 73 | return nullptr; |
| 74 | } |
| 75 | |
| 76 | TEST(semaphore, sem_wait__sem_post) { |
| 77 | sem_t s; |
| 78 | ASSERT_EQ(0, sem_init(&s, 0, 0)); |
| 79 | |
| 80 | pthread_t t1, t2, t3; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 81 | ASSERT_EQ(0, pthread_create(&t1, nullptr, SemWaitThreadFn, &s)); |
| 82 | ASSERT_EQ(0, pthread_create(&t2, nullptr, SemWaitThreadFn, &s)); |
| 83 | ASSERT_EQ(0, pthread_create(&t3, nullptr, SemWaitThreadFn, &s)); |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 84 | |
| 85 | ASSERT_EQ(0, sem_post(&s)); |
| 86 | ASSERT_EQ(0, sem_post(&s)); |
| 87 | ASSERT_EQ(0, sem_post(&s)); |
| 88 | |
| 89 | void* result; |
| 90 | ASSERT_EQ(0, pthread_join(t1, &result)); |
| 91 | ASSERT_EQ(0, pthread_join(t2, &result)); |
| 92 | ASSERT_EQ(0, pthread_join(t3, &result)); |
| 93 | } |
| 94 | |
| 95 | static inline void timespec_add_ms(timespec& ts, size_t ms) { |
| 96 | ts.tv_sec += ms / 1000; |
| 97 | ts.tv_nsec += (ms % 1000) * 1000000; |
| 98 | if (ts.tv_nsec >= NS_PER_S) { |
| 99 | ts.tv_sec++; |
| 100 | ts.tv_nsec -= NS_PER_S; |
| 101 | } |
| 102 | } |
| 103 | |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 104 | static void sem_timedwait_helper(clockid_t clock, |
| 105 | int (*wait_function)(sem_t* __sem, const timespec* __ts)) { |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 106 | sem_t s; |
| 107 | ASSERT_EQ(0, sem_init(&s, 0, 0)); |
| 108 | |
| 109 | timespec ts; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 110 | ASSERT_EQ(0, clock_gettime(clock, &ts)); |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 111 | timespec_add_ms(ts, 100); |
| 112 | |
| 113 | errno = 0; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 114 | ASSERT_EQ(-1, wait_function(&s, &ts)); |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 115 | ASSERT_EQ(ETIMEDOUT, errno); |
| 116 | |
| 117 | // A negative timeout is an error. |
| 118 | errno = 0; |
| 119 | ts.tv_nsec = -1; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 120 | ASSERT_EQ(-1, wait_function(&s, &ts)); |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 121 | ASSERT_EQ(EINVAL, errno); |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 122 | errno = 0; |
| 123 | ts.tv_nsec = NS_PER_S; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 124 | ASSERT_EQ(-1, wait_function(&s, &ts)); |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 125 | ASSERT_EQ(EINVAL, errno); |
| 126 | |
| 127 | errno = 0; |
| 128 | ts.tv_nsec = NS_PER_S - 1; |
| 129 | ts.tv_sec = -1; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 130 | ASSERT_EQ(-1, wait_function(&s, &ts)); |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 131 | ASSERT_EQ(ETIMEDOUT, errno); |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 132 | |
| 133 | ASSERT_EQ(0, sem_destroy(&s)); |
| 134 | } |
| 135 | |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 136 | TEST(semaphore, sem_timedwait) { |
| 137 | sem_timedwait_helper(CLOCK_REALTIME, sem_timedwait); |
| 138 | } |
| 139 | |
| 140 | TEST(semaphore, sem_timedwait_monotonic_np) { |
| 141 | #if defined(__BIONIC__) |
| 142 | sem_timedwait_helper(CLOCK_MONOTONIC, sem_timedwait_monotonic_np); |
| 143 | #else // __BIONIC__ |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 144 | GTEST_SKIP() << "sem_timedwait_monotonic_np is only supported on bionic"; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 145 | #endif // __BIONIC__ |
| 146 | } |
| 147 | |
Elliott Hughes | dd586f2 | 2015-12-16 15:15:58 -0800 | [diff] [blame] | 148 | TEST(semaphore_DeathTest, sem_timedwait_null_timeout) { |
| 149 | sem_t s; |
| 150 | ASSERT_EQ(0, sem_init(&s, 0, 0)); |
| 151 | |
| 152 | ASSERT_EXIT(sem_timedwait(&s, nullptr), testing::KilledBySignal(SIGSEGV), ""); |
| 153 | } |
| 154 | |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 155 | TEST(semaphore, sem_getvalue) { |
| 156 | sem_t s; |
| 157 | ASSERT_EQ(0, sem_init(&s, 0, 0)); |
| 158 | |
| 159 | int i; |
| 160 | ASSERT_EQ(0, sem_getvalue(&s, &i)); |
| 161 | ASSERT_EQ(0, i); |
| 162 | |
| 163 | ASSERT_EQ(0, sem_post(&s)); |
| 164 | ASSERT_EQ(0, sem_getvalue(&s, &i)); |
| 165 | ASSERT_EQ(1, i); |
| 166 | |
| 167 | ASSERT_EQ(0, sem_post(&s)); |
| 168 | ASSERT_EQ(0, sem_getvalue(&s, &i)); |
| 169 | ASSERT_EQ(2, i); |
| 170 | |
| 171 | ASSERT_EQ(0, sem_wait(&s)); |
| 172 | ASSERT_EQ(0, sem_getvalue(&s, &i)); |
| 173 | ASSERT_EQ(1, i); |
| 174 | } |
Yabin Cui | ca48274 | 2016-01-25 17:38:44 -0800 | [diff] [blame] | 175 | |
Elliott Hughes | ff1428a | 2018-11-12 16:01:37 -0800 | [diff] [blame] | 176 | extern "C" void android_set_application_target_sdk_version(int target); |
Yabin Cui | ca48274 | 2016-01-25 17:38:44 -0800 | [diff] [blame] | 177 | |
| 178 | static void sem_wait_test_signal_handler(int) { |
| 179 | } |
| 180 | |
| 181 | static void* SemWaitEINTRThreadFn(void* arg) { |
| 182 | sem_t* sem = reinterpret_cast<sem_t*>(arg); |
| 183 | uintptr_t have_eintr = 0; |
| 184 | uintptr_t have_error = 0; |
| 185 | while (true) { |
| 186 | int result = sem_wait(sem); |
| 187 | if (result == 0) { |
| 188 | break; |
| 189 | } |
| 190 | if (result == -1) { |
| 191 | if (errno == EINTR) { |
| 192 | have_eintr = 1; |
| 193 | } else { |
| 194 | have_error = 1; |
| 195 | break; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | return reinterpret_cast<void*>((have_eintr << 1) | have_error); |
| 200 | } |
| 201 | |
| 202 | TEST(semaphore, sem_wait_no_EINTR_in_sdk_less_equal_than_23) { |
| 203 | #if defined(__BIONIC__) |
Elliott Hughes | 5bc78c8 | 2016-11-16 11:35:43 -0800 | [diff] [blame] | 204 | android_set_application_target_sdk_version(__ANDROID_API_M__); |
Yabin Cui | ca48274 | 2016-01-25 17:38:44 -0800 | [diff] [blame] | 205 | sem_t s; |
| 206 | ASSERT_EQ(0, sem_init(&s, 0, 0)); |
| 207 | ScopedSignalHandler handler(SIGUSR1, sem_wait_test_signal_handler); |
| 208 | pthread_t thread; |
| 209 | ASSERT_EQ(0, pthread_create(&thread, nullptr, SemWaitEINTRThreadFn, &s)); |
| 210 | // Give some time for the thread to run sem_wait. |
| 211 | usleep(500000); |
| 212 | ASSERT_EQ(0, pthread_kill(thread, SIGUSR1)); |
| 213 | // Give some time for the thread to handle signal. |
| 214 | usleep(500000); |
| 215 | ASSERT_EQ(0, sem_post(&s)); |
| 216 | void* result; |
| 217 | ASSERT_EQ(0, pthread_join(thread, &result)); |
| 218 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(result)); |
| 219 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 220 | 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] | 221 | #endif |
| 222 | } |
| 223 | |
| 224 | TEST(semaphore, sem_wait_EINTR_in_sdk_greater_than_23) { |
| 225 | #if defined(__BIONIC__) |
| 226 | android_set_application_target_sdk_version(24U); |
| 227 | #endif |
| 228 | sem_t s; |
| 229 | ASSERT_EQ(0, sem_init(&s, 0, 0)); |
| 230 | ScopedSignalHandler handler(SIGUSR1, sem_wait_test_signal_handler); |
| 231 | pthread_t thread; |
| 232 | ASSERT_EQ(0, pthread_create(&thread, nullptr, SemWaitEINTRThreadFn, &s)); |
| 233 | // Give some time for the thread to run sem_wait. |
| 234 | usleep(500000); |
| 235 | ASSERT_EQ(0, pthread_kill(thread, SIGUSR1)); |
| 236 | // Give some time for the thread to handle signal. |
| 237 | usleep(500000); |
| 238 | ASSERT_EQ(0, sem_post(&s)); |
| 239 | void* result; |
| 240 | ASSERT_EQ(0, pthread_join(thread, &result)); |
| 241 | ASSERT_EQ(2U, reinterpret_cast<uintptr_t>(result)); |
| 242 | } |