| Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
| Josh Gao | 61cf3f3 | 2016-03-08 15:27:15 -0800 | [diff] [blame] | 17 | #include <errno.h> |
| Elliott Hughes | afe58ad | 2014-09-04 13:54:42 -0700 | [diff] [blame] | 18 | #include <signal.h> |
| Tamas Zsoldos | 3bfa4bc | 2025-04-29 08:27:50 -0700 | [diff] [blame] | 19 | #include <sys/auxv.h> |
| Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 20 | #include <sys/cdefs.h> |
| Josh Gao | 61cf3f3 | 2016-03-08 15:27:15 -0800 | [diff] [blame] | 21 | #include <sys/syscall.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <unistd.h> |
| Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 24 | |
| Elliott Hughes | ca3f8e4 | 2019-10-28 15:59:38 -0700 | [diff] [blame] | 25 | #include <chrono> |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 26 | #include <thread> |
| 27 | |
| Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 28 | #include <android-base/macros.h> |
| Elliott Hughes | f2ee3db | 2025-05-06 14:59:21 -0700 | [diff] [blame] | 29 | #include <android-base/test_utils.h> |
| Evgeny Eltsin | 4d9264c | 2019-12-17 14:17:07 +0100 | [diff] [blame] | 30 | #include <android-base/threads.h> |
| 31 | |
| Elliott Hughes | afe58ad | 2014-09-04 13:54:42 -0700 | [diff] [blame] | 32 | #include <gtest/gtest.h> |
| Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 33 | |
| Elliott Hughes | 71ba589 | 2018-02-07 12:44:45 -0800 | [diff] [blame] | 34 | #include "SignalUtils.h" |
| Tamas Petz | 9903278 | 2025-05-14 14:03:43 +0200 | [diff] [blame] | 35 | #include "sme_utils.h" |
| Evgeny Eltsin | 4d9264c | 2019-12-17 14:17:07 +0100 | [diff] [blame] | 36 | #include "utils.h" |
| Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 37 | |
| Elliott Hughes | ca3f8e4 | 2019-10-28 15:59:38 -0700 | [diff] [blame] | 38 | using namespace std::chrono_literals; |
| 39 | |
| Colin Cross | 23b986c | 2022-10-19 15:03:59 -0700 | [diff] [blame] | 40 | #if defined(ANDROID_HOST_MUSL) |
| 41 | // Musl doesn't export __SIGRTMIN and __SIGRTMAX, #define |
| 42 | // them here. |
| 43 | #define __SIGRTMIN 32 |
| 44 | #define __SIGRTMAX 64 |
| 45 | #endif |
| 46 | |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 47 | static int SIGNAL_MIN() { |
| Elliott Hughes | 40d105c | 2013-10-16 12:53:58 -0700 | [diff] [blame] | 48 | return 1; // Signals start at 1 (SIGHUP), not 0. |
| 49 | } |
| 50 | |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 51 | template <typename SigSetT> |
| 52 | static int SIGNAL_MAX(SigSetT* set) { |
| 53 | return sizeof(*set) * 8; |
| Elliott Hughes | 40d105c | 2013-10-16 12:53:58 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 56 | template <typename SigSetT> |
| 57 | static void TestSigSet1(int (fn)(SigSetT*)) { |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 58 | // nullptr sigset_t*/sigset64_t*. |
| 59 | SigSetT* set_ptr = nullptr; |
| Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 60 | errno = 0; |
| 61 | ASSERT_EQ(-1, fn(set_ptr)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 62 | ASSERT_ERRNO(EINVAL); |
| Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 63 | |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 64 | // Non-nullptr. |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 65 | SigSetT set = {}; |
| Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 66 | errno = 0; |
| 67 | ASSERT_EQ(0, fn(&set)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 68 | ASSERT_ERRNO(0); |
| Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 71 | template <typename SigSetT> |
| 72 | static void TestSigSet2(int (fn)(SigSetT*, int)) { |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 73 | // nullptr sigset_t*/sigset64_t*. |
| 74 | SigSetT* set_ptr = nullptr; |
| Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 75 | errno = 0; |
| 76 | ASSERT_EQ(-1, fn(set_ptr, SIGSEGV)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 77 | ASSERT_ERRNO(EINVAL); |
| Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 78 | |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 79 | SigSetT set = {}; |
| Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 80 | |
| Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 81 | // Bad signal number: too small. |
| 82 | errno = 0; |
| 83 | ASSERT_EQ(-1, fn(&set, 0)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 84 | ASSERT_ERRNO(EINVAL); |
| Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 85 | |
| 86 | // Bad signal number: too high. |
| 87 | errno = 0; |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 88 | ASSERT_EQ(-1, fn(&set, SIGNAL_MAX(&set) + 1)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 89 | ASSERT_ERRNO(EINVAL); |
| Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 90 | |
| 91 | // Good signal numbers, low and high ends of range. |
| 92 | errno = 0; |
| Elliott Hughes | 40d105c | 2013-10-16 12:53:58 -0700 | [diff] [blame] | 93 | ASSERT_EQ(0, fn(&set, SIGNAL_MIN())); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 94 | ASSERT_ERRNO(0); |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 95 | ASSERT_EQ(0, fn(&set, SIGNAL_MAX(&set))); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 96 | ASSERT_ERRNO(0); |
| Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 99 | TEST(signal, sigaddset_invalid) { |
| 100 | TestSigSet2(sigaddset); |
| 101 | } |
| 102 | |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 103 | TEST(signal, sigaddset64_invalid) { |
| 104 | #if defined(__BIONIC__) |
| 105 | TestSigSet2(sigaddset64); |
| 106 | #endif |
| 107 | } |
| 108 | |
| Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 109 | TEST(signal, sigdelset_invalid) { |
| 110 | TestSigSet2(sigdelset); |
| 111 | } |
| 112 | |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 113 | TEST(signal, sigdelset64_invalid) { |
| 114 | #if defined(__BIONIC__) |
| 115 | TestSigSet2(sigdelset64); |
| 116 | #endif |
| 117 | } |
| 118 | |
| Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 119 | TEST(signal, sigemptyset_invalid) { |
| 120 | TestSigSet1(sigemptyset); |
| 121 | } |
| 122 | |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 123 | TEST(signal, sigemptyset64_invalid) { |
| 124 | #if defined(__BIONIC__) |
| 125 | TestSigSet1(sigemptyset64); |
| 126 | #endif |
| 127 | } |
| 128 | |
| Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 129 | TEST(signal, sigfillset_invalid) { |
| 130 | TestSigSet1(sigfillset); |
| 131 | } |
| Chris Dearman | d8a5a6f | 2012-12-07 18:41:10 -0800 | [diff] [blame] | 132 | |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 133 | TEST(signal, sigfillset64_invalid) { |
| 134 | #if defined(__BIONIC__) |
| 135 | TestSigSet1(sigfillset64); |
| 136 | #endif |
| 137 | } |
| 138 | |
| 139 | TEST(signal, sigismember_invalid) { |
| 140 | TestSigSet2(sigismember); |
| 141 | } |
| 142 | |
| 143 | TEST(signal, sigismember64_invalid) { |
| 144 | #if defined(__BIONIC__) |
| 145 | TestSigSet2(sigismember64); |
| 146 | #endif |
| 147 | } |
| 148 | |
| Chris Dearman | d8a5a6f | 2012-12-07 18:41:10 -0800 | [diff] [blame] | 149 | TEST(signal, raise_invalid) { |
| 150 | errno = 0; |
| 151 | ASSERT_EQ(-1, raise(-1)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 152 | ASSERT_ERRNO(EINVAL); |
| Chris Dearman | d8a5a6f | 2012-12-07 18:41:10 -0800 | [diff] [blame] | 153 | } |
| Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 154 | |
| Elliott Hughes | fae89fc | 2013-02-21 11:22:23 -0800 | [diff] [blame] | 155 | static void raise_in_signal_handler_helper(int signal_number) { |
| 156 | ASSERT_EQ(SIGALRM, signal_number); |
| 157 | static int count = 0; |
| 158 | if (++count == 1) { |
| 159 | raise(SIGALRM); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | TEST(signal, raise_in_signal_handler) { |
| 164 | ScopedSignalHandler ssh(SIGALRM, raise_in_signal_handler_helper); |
| 165 | raise(SIGALRM); |
| 166 | } |
| 167 | |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 168 | static int g_sigsuspend_signal_handler_call_count = 0; |
| 169 | |
| Elliott Hughes | 40d105c | 2013-10-16 12:53:58 -0700 | [diff] [blame] | 170 | TEST(signal, sigsuspend_sigpending) { |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 171 | SignalMaskRestorer smr; |
| 172 | |
| Elliott Hughes | 1f5af92 | 2013-10-15 18:01:56 -0700 | [diff] [blame] | 173 | // Block SIGALRM. |
| 174 | sigset_t just_SIGALRM; |
| 175 | sigemptyset(&just_SIGALRM); |
| 176 | sigaddset(&just_SIGALRM, SIGALRM); |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 177 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr)); |
| Elliott Hughes | 1f5af92 | 2013-10-15 18:01:56 -0700 | [diff] [blame] | 178 | |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 179 | ScopedSignalHandler ssh(SIGALRM, [](int) { ++g_sigsuspend_signal_handler_call_count; }); |
| Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 180 | |
| Elliott Hughes | 40d105c | 2013-10-16 12:53:58 -0700 | [diff] [blame] | 181 | // There should be no pending signals. |
| 182 | sigset_t pending; |
| 183 | sigemptyset(&pending); |
| 184 | ASSERT_EQ(0, sigpending(&pending)); |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 185 | for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) { |
| Elliott Hughes | 40d105c | 2013-10-16 12:53:58 -0700 | [diff] [blame] | 186 | EXPECT_FALSE(sigismember(&pending, i)) << i; |
| 187 | } |
| 188 | |
| Elliott Hughes | 1f5af92 | 2013-10-15 18:01:56 -0700 | [diff] [blame] | 189 | // Raise SIGALRM and check our signal handler wasn't called. |
| 190 | raise(SIGALRM); |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 191 | ASSERT_EQ(0, g_sigsuspend_signal_handler_call_count); |
| Elliott Hughes | 1f5af92 | 2013-10-15 18:01:56 -0700 | [diff] [blame] | 192 | |
| Elliott Hughes | 40d105c | 2013-10-16 12:53:58 -0700 | [diff] [blame] | 193 | // We should now have a pending SIGALRM but nothing else. |
| 194 | sigemptyset(&pending); |
| 195 | ASSERT_EQ(0, sigpending(&pending)); |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 196 | for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) { |
| Elliott Hughes | 40d105c | 2013-10-16 12:53:58 -0700 | [diff] [blame] | 197 | EXPECT_EQ((i == SIGALRM), sigismember(&pending, i)); |
| 198 | } |
| 199 | |
| Elliott Hughes | 1f5af92 | 2013-10-15 18:01:56 -0700 | [diff] [blame] | 200 | // Use sigsuspend to block everything except SIGALRM... |
| 201 | sigset_t not_SIGALRM; |
| 202 | sigfillset(¬_SIGALRM); |
| 203 | sigdelset(¬_SIGALRM, SIGALRM); |
| 204 | ASSERT_EQ(-1, sigsuspend(¬_SIGALRM)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 205 | ASSERT_ERRNO(EINTR); |
| Elliott Hughes | 1f5af92 | 2013-10-15 18:01:56 -0700 | [diff] [blame] | 206 | // ...and check that we now receive our pending SIGALRM. |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 207 | ASSERT_EQ(1, g_sigsuspend_signal_handler_call_count); |
| 208 | } |
| Elliott Hughes | 1f5af92 | 2013-10-15 18:01:56 -0700 | [diff] [blame] | 209 | |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 210 | static int g_sigsuspend64_signal_handler_call_count = 0; |
| 211 | |
| 212 | TEST(signal, sigsuspend64_sigpending64) { |
| 213 | SignalMaskRestorer smr; |
| 214 | |
| 215 | // Block SIGRTMIN. |
| 216 | sigset64_t just_SIGRTMIN; |
| 217 | sigemptyset64(&just_SIGRTMIN); |
| 218 | sigaddset64(&just_SIGRTMIN, SIGRTMIN); |
| 219 | ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr)); |
| 220 | |
| 221 | ScopedSignalHandler ssh(SIGRTMIN, [](int) { ++g_sigsuspend64_signal_handler_call_count; }); |
| 222 | |
| 223 | // There should be no pending signals. |
| 224 | sigset64_t pending; |
| 225 | sigemptyset64(&pending); |
| 226 | ASSERT_EQ(0, sigpending64(&pending)); |
| 227 | for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) { |
| 228 | EXPECT_FALSE(sigismember64(&pending, i)) << i; |
| 229 | } |
| 230 | |
| 231 | // Raise SIGRTMIN and check our signal handler wasn't called. |
| 232 | raise(SIGRTMIN); |
| 233 | ASSERT_EQ(0, g_sigsuspend64_signal_handler_call_count); |
| 234 | |
| 235 | // We should now have a pending SIGRTMIN but nothing else. |
| 236 | sigemptyset64(&pending); |
| 237 | ASSERT_EQ(0, sigpending64(&pending)); |
| 238 | for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) { |
| 239 | EXPECT_EQ((i == SIGRTMIN), sigismember64(&pending, i)); |
| 240 | } |
| 241 | |
| 242 | // Use sigsuspend64 to block everything except SIGRTMIN... |
| 243 | sigset64_t not_SIGRTMIN; |
| 244 | sigfillset64(¬_SIGRTMIN); |
| 245 | sigdelset64(¬_SIGRTMIN, SIGRTMIN); |
| 246 | ASSERT_EQ(-1, sigsuspend64(¬_SIGRTMIN)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 247 | ASSERT_ERRNO(EINTR); |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 248 | // ...and check that we now receive our pending SIGRTMIN. |
| 249 | ASSERT_EQ(1, g_sigsuspend64_signal_handler_call_count); |
| Elliott Hughes | 1f5af92 | 2013-10-15 18:01:56 -0700 | [diff] [blame] | 250 | } |
| Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 251 | |
| Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 252 | template <typename SigActionT, typename SigSetT> |
| 253 | static void TestSigAction(int (sigaction_fn)(int, const SigActionT*, SigActionT*), |
| 254 | int (sigaddset_fn)(SigSetT*, int), |
| 255 | int sig) { |
| Elliott Hughes | afe58ad | 2014-09-04 13:54:42 -0700 | [diff] [blame] | 256 | // Both bionic and glibc set SA_RESTORER when talking to the kernel on arm, |
| 257 | // arm64, x86, and x86-64. The version of glibc we're using also doesn't |
| Elliott Hughes | 6a65ccd | 2020-02-13 09:48:14 -0800 | [diff] [blame] | 258 | // define SA_RESTORER, but luckily it's the same value everywhere. |
| Elliott Hughes | aa13e83 | 2014-09-04 15:43:10 -0700 | [diff] [blame] | 259 | static const unsigned sa_restorer = 0x4000000; |
| Elliott Hughes | afe58ad | 2014-09-04 13:54:42 -0700 | [diff] [blame] | 260 | |
| Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 261 | // See what's currently set for this signal. |
| 262 | SigActionT original_sa = {}; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 263 | ASSERT_EQ(0, sigaction_fn(sig, nullptr, &original_sa)); |
| 264 | ASSERT_TRUE(original_sa.sa_handler == nullptr); |
| 265 | ASSERT_TRUE(original_sa.sa_sigaction == nullptr); |
| Elliott Hughes | aa13e83 | 2014-09-04 15:43:10 -0700 | [diff] [blame] | 266 | ASSERT_EQ(0U, original_sa.sa_flags & ~sa_restorer); |
| Goran Jakovljevic | 3796669 | 2018-02-12 09:03:10 +0100 | [diff] [blame] | 267 | #ifdef SA_RESTORER |
| Evgeny Eltsin | 11f6076 | 2018-02-05 13:33:35 +0100 | [diff] [blame] | 268 | ASSERT_EQ(bool(original_sa.sa_flags & sa_restorer), bool(original_sa.sa_restorer)); |
| Goran Jakovljevic | 3796669 | 2018-02-12 09:03:10 +0100 | [diff] [blame] | 269 | #endif |
| Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 270 | |
| 271 | // Set a traditional sa_handler signal handler. |
| Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 272 | auto no_op_signal_handler = [](int) {}; |
| 273 | SigActionT sa = {}; |
| 274 | sigaddset_fn(&sa.sa_mask, sig); |
| Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 275 | sa.sa_flags = SA_ONSTACK; |
| Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 276 | sa.sa_handler = no_op_signal_handler; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 277 | ASSERT_EQ(0, sigaction_fn(sig, &sa, nullptr)); |
| Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 278 | |
| 279 | // Check that we can read it back. |
| Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 280 | sa = {}; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 281 | ASSERT_EQ(0, sigaction_fn(sig, nullptr, &sa)); |
| Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 282 | ASSERT_TRUE(sa.sa_handler == no_op_signal_handler); |
| Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 283 | ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler); |
| Elliott Hughes | aa13e83 | 2014-09-04 15:43:10 -0700 | [diff] [blame] | 284 | ASSERT_EQ(static_cast<unsigned>(SA_ONSTACK), sa.sa_flags & ~sa_restorer); |
| Goran Jakovljevic | 3796669 | 2018-02-12 09:03:10 +0100 | [diff] [blame] | 285 | #ifdef SA_RESTORER |
| Evgeny Eltsin | 11f6076 | 2018-02-05 13:33:35 +0100 | [diff] [blame] | 286 | ASSERT_EQ(bool(sa.sa_flags & sa_restorer), bool(sa.sa_restorer)); |
| Goran Jakovljevic | 3796669 | 2018-02-12 09:03:10 +0100 | [diff] [blame] | 287 | #endif |
| Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 288 | |
| 289 | // Set a new-style sa_sigaction signal handler. |
| Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 290 | auto no_op_sigaction = [](int, siginfo_t*, void*) {}; |
| 291 | sa = {}; |
| 292 | sigaddset_fn(&sa.sa_mask, sig); |
| Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 293 | sa.sa_flags = SA_ONSTACK | SA_SIGINFO; |
| Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 294 | sa.sa_sigaction = no_op_sigaction; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 295 | ASSERT_EQ(0, sigaction_fn(sig, &sa, nullptr)); |
| Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 296 | |
| 297 | // Check that we can read it back. |
| Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 298 | sa = {}; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 299 | ASSERT_EQ(0, sigaction_fn(sig, nullptr, &sa)); |
| Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 300 | ASSERT_TRUE(sa.sa_sigaction == no_op_sigaction); |
| Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 301 | ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler); |
| Elliott Hughes | aa13e83 | 2014-09-04 15:43:10 -0700 | [diff] [blame] | 302 | ASSERT_EQ(static_cast<unsigned>(SA_ONSTACK | SA_SIGINFO), sa.sa_flags & ~sa_restorer); |
| Goran Jakovljevic | 3796669 | 2018-02-12 09:03:10 +0100 | [diff] [blame] | 303 | #ifdef SA_RESTORER |
| Evgeny Eltsin | 11f6076 | 2018-02-05 13:33:35 +0100 | [diff] [blame] | 304 | ASSERT_EQ(bool(sa.sa_flags & sa_restorer), bool(sa.sa_restorer)); |
| Goran Jakovljevic | 3796669 | 2018-02-12 09:03:10 +0100 | [diff] [blame] | 305 | #endif |
| Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 306 | |
| 307 | // Put everything back how it was. |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 308 | ASSERT_EQ(0, sigaction_fn(sig, &original_sa, nullptr)); |
| Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | TEST(signal, sigaction) { |
| 312 | TestSigAction(sigaction, sigaddset, SIGALRM); |
| 313 | } |
| 314 | |
| 315 | TEST(signal, sigaction64_SIGRTMIN) { |
| 316 | TestSigAction(sigaction64, sigaddset64, SIGRTMIN); |
| Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 317 | } |
| Elliott Hughes | aa0ebda | 2014-02-11 19:57:06 -0800 | [diff] [blame] | 318 | |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 319 | static void ClearSignalMask() { |
| 320 | uint64_t sigset = 0; |
| Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 321 | SignalSetAdd(&sigset, __SIGRTMIN); |
| 322 | if (syscall(__NR_rt_sigprocmask, SIG_SETMASK, &sigset, nullptr, sizeof(sigset)) != 0) { |
| 323 | abort(); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | static void FillSignalMask() { |
| 328 | uint64_t sigset = ~0ULL; |
| 329 | for (int signo = __SIGRTMIN + 1; signo < SIGRTMIN; ++signo) { |
| 330 | SignalSetDel(&sigset, signo); |
| 331 | } |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 332 | if (syscall(__NR_rt_sigprocmask, SIG_SETMASK, &sigset, nullptr, sizeof(sigset)) != 0) { |
| 333 | abort(); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | static uint64_t GetSignalMask() { |
| 338 | uint64_t sigset; |
| 339 | if (syscall(__NR_rt_sigprocmask, SIG_SETMASK, nullptr, &sigset, sizeof(sigset)) != 0) { |
| 340 | abort(); |
| 341 | } |
| 342 | return sigset; |
| 343 | } |
| 344 | |
| Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 345 | static void TestSignalMaskFiltered(uint64_t sigset) { |
| 346 | #if defined(__BIONIC__) |
| 347 | for (int signo = __SIGRTMIN; signo < SIGRTMIN; ++signo) { |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 348 | bool signal_blocked = sigset & (1ULL << (signo - 1)); |
| Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 349 | if (signo == __SIGRTMIN) { |
| 350 | // TIMER_SIGNAL must be blocked. |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 351 | EXPECT_EQ(true, signal_blocked) << "signal " << signo; |
| Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 352 | } else { |
| 353 | // The other reserved signals must not be blocked. |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 354 | EXPECT_EQ(false, signal_blocked) << "signal " << signo; |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 355 | } |
| 356 | } |
| Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 357 | #else |
| 358 | UNUSED(sigset); |
| 359 | #endif |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 362 | static void TestSignalMaskFunction(std::function<void()> fn) { |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 363 | ClearSignalMask(); |
| 364 | fn(); |
| Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 365 | TestSignalMaskFiltered(GetSignalMask()); |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | TEST(signal, sigaction_filter) { |
| 369 | ClearSignalMask(); |
| 370 | static uint64_t sigset; |
| 371 | struct sigaction sa = {}; |
| 372 | sa.sa_handler = [](int) { sigset = GetSignalMask(); }; |
| Josh Gao | ba40ff6 | 2019-01-22 22:53:49 -0800 | [diff] [blame] | 373 | sa.sa_flags = SA_ONSTACK | SA_NODEFER; |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 374 | sigfillset(&sa.sa_mask); |
| 375 | sigaction(SIGUSR1, &sa, nullptr); |
| 376 | raise(SIGUSR1); |
| Josh Gao | ba40ff6 | 2019-01-22 22:53:49 -0800 | [diff] [blame] | 377 | |
| 378 | // On LP32, struct sigaction::sa_mask is only 32-bits wide. |
| 379 | unsigned long expected_sigset = ~0UL; |
| 380 | |
| 381 | // SIGKILL and SIGSTOP are always blocked. |
| 382 | expected_sigset &= ~(1UL << (SIGKILL - 1)); |
| 383 | expected_sigset &= ~(1UL << (SIGSTOP - 1)); |
| 384 | |
| 385 | ASSERT_EQ(static_cast<uint64_t>(expected_sigset), sigset); |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | TEST(signal, sigaction64_filter) { |
| 389 | ClearSignalMask(); |
| 390 | static uint64_t sigset; |
| 391 | struct sigaction64 sa = {}; |
| 392 | sa.sa_handler = [](int) { sigset = GetSignalMask(); }; |
| Josh Gao | ba40ff6 | 2019-01-22 22:53:49 -0800 | [diff] [blame] | 393 | sa.sa_flags = SA_ONSTACK | SA_NODEFER; |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 394 | sigfillset64(&sa.sa_mask); |
| 395 | sigaction64(SIGUSR1, &sa, nullptr); |
| 396 | raise(SIGUSR1); |
| Josh Gao | ba40ff6 | 2019-01-22 22:53:49 -0800 | [diff] [blame] | 397 | |
| 398 | uint64_t expected_sigset = ~0ULL; |
| 399 | |
| 400 | // SIGKILL and SIGSTOP are always blocked. |
| 401 | expected_sigset &= ~(1ULL << (SIGKILL - 1)); |
| 402 | expected_sigset &= ~(1ULL << (SIGSTOP - 1)); |
| 403 | |
| 404 | ASSERT_EQ(expected_sigset, sigset); |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | TEST(signal, sigprocmask_setmask_filter) { |
| Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 408 | TestSignalMaskFunction([]() { |
| 409 | ClearSignalMask(); |
| 410 | sigset_t sigset_libc; |
| 411 | sigfillset(&sigset_libc); |
| 412 | ASSERT_EQ(0, sigprocmask(SIG_SETMASK, &sigset_libc, nullptr)); |
| 413 | }); |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | TEST(signal, sigprocmask64_setmask_filter) { |
| Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 417 | TestSignalMaskFunction([]() { |
| 418 | ClearSignalMask(); |
| 419 | sigset64_t sigset_libc; |
| 420 | sigfillset64(&sigset_libc); |
| 421 | ASSERT_EQ(0, sigprocmask64(SIG_SETMASK, &sigset_libc, nullptr)); |
| 422 | }); |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | TEST(signal, pthread_sigmask_setmask_filter) { |
| Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 426 | TestSignalMaskFunction([]() { |
| 427 | ClearSignalMask(); |
| 428 | sigset_t sigset_libc; |
| 429 | sigfillset(&sigset_libc); |
| 430 | ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &sigset_libc, nullptr)); |
| 431 | }); |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | TEST(signal, pthread_sigmask64_setmask_filter) { |
| Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 435 | TestSignalMaskFunction([]() { |
| 436 | ClearSignalMask(); |
| 437 | sigset64_t sigset_libc; |
| 438 | sigfillset64(&sigset_libc); |
| 439 | ASSERT_EQ(0, pthread_sigmask64(SIG_SETMASK, &sigset_libc, nullptr)); |
| 440 | }); |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | TEST(signal, sigprocmask_block_filter) { |
| Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 444 | TestSignalMaskFunction([]() { |
| 445 | ClearSignalMask(); |
| 446 | sigset_t sigset_libc; |
| 447 | sigfillset(&sigset_libc); |
| 448 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &sigset_libc, nullptr)); |
| 449 | }); |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | TEST(signal, sigprocmask64_block_filter) { |
| Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 453 | TestSignalMaskFunction([]() { |
| 454 | ClearSignalMask(); |
| 455 | sigset64_t sigset_libc; |
| 456 | sigfillset64(&sigset_libc); |
| 457 | ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &sigset_libc, nullptr)); |
| 458 | }); |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | TEST(signal, pthread_sigmask_block_filter) { |
| Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 462 | TestSignalMaskFunction([]() { |
| 463 | ClearSignalMask(); |
| 464 | sigset_t sigset_libc; |
| 465 | sigfillset(&sigset_libc); |
| 466 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &sigset_libc, nullptr)); |
| 467 | }); |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | TEST(signal, pthread_sigmask64_block_filter) { |
| Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 471 | TestSignalMaskFunction([]() { |
| 472 | ClearSignalMask(); |
| 473 | sigset64_t sigset_libc; |
| 474 | sigfillset64(&sigset_libc); |
| 475 | ASSERT_EQ(0, pthread_sigmask64(SIG_BLOCK, &sigset_libc, nullptr)); |
| 476 | }); |
| 477 | } |
| 478 | |
| 479 | TEST(signal, sigprocmask_unblock_filter) { |
| 480 | TestSignalMaskFunction([]() { |
| 481 | FillSignalMask(); |
| 482 | sigset_t sigset_libc; |
| 483 | sigfillset(&sigset_libc); |
| 484 | ASSERT_EQ(0, sigprocmask(SIG_UNBLOCK, &sigset_libc, nullptr)); |
| 485 | }); |
| 486 | } |
| 487 | |
| 488 | TEST(signal, sigprocmask64_unblock_filter) { |
| 489 | TestSignalMaskFunction([]() { |
| 490 | FillSignalMask(); |
| 491 | sigset64_t sigset_libc; |
| 492 | sigfillset64(&sigset_libc); |
| 493 | ASSERT_EQ(0, sigprocmask64(SIG_UNBLOCK, &sigset_libc, nullptr)); |
| 494 | }); |
| 495 | } |
| 496 | |
| 497 | TEST(signal, pthread_sigmask_unblock_filter) { |
| 498 | TestSignalMaskFunction([]() { |
| 499 | FillSignalMask(); |
| 500 | sigset_t sigset_libc; |
| 501 | sigfillset(&sigset_libc); |
| 502 | ASSERT_EQ(0, pthread_sigmask(SIG_UNBLOCK, &sigset_libc, nullptr)); |
| 503 | }); |
| 504 | } |
| 505 | |
| 506 | TEST(signal, pthread_sigmask64_unblock_filter) { |
| 507 | TestSignalMaskFunction([]() { |
| 508 | FillSignalMask(); |
| 509 | sigset64_t sigset_libc; |
| 510 | sigfillset64(&sigset_libc); |
| 511 | ASSERT_EQ(0, pthread_sigmask64(SIG_UNBLOCK, &sigset_libc, nullptr)); |
| 512 | }); |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | // glibc filters out signals via sigfillset, not the actual underlying functions. |
| 516 | TEST(signal, sigset_filter) { |
| 517 | #if defined(__BIONIC__) |
| Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 518 | TestSignalMaskFunction([]() { |
| 519 | for (int i = 1; i <= 64; ++i) { |
| 520 | sigset(i, SIG_HOLD); |
| 521 | } |
| 522 | }); |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 523 | #endif |
| 524 | } |
| 525 | |
| 526 | TEST(signal, sighold_filter) { |
| 527 | #if defined(__BIONIC__) |
| Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 528 | TestSignalMaskFunction([]() { |
| 529 | for (int i = 1; i <= 64; ++i) { |
| 530 | sighold(i); |
| 531 | } |
| 532 | }); |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 533 | #endif |
| 534 | } |
| 535 | |
| Elliott Hughes | 215baed | 2023-07-17 17:15:01 -0700 | [diff] [blame] | 536 | #if defined(__BIONIC__) && !defined(__riscv) |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 537 | // Not exposed via headers, but the symbols are available if you declare them yourself. |
| 538 | extern "C" int sigblock(int); |
| 539 | extern "C" int sigsetmask(int); |
| Elliott Hughes | 215baed | 2023-07-17 17:15:01 -0700 | [diff] [blame] | 540 | #define HAVE_SIGBLOCK_SIGSETMASK |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 541 | #endif |
| 542 | |
| 543 | TEST(signal, sigblock_filter) { |
| Elliott Hughes | 215baed | 2023-07-17 17:15:01 -0700 | [diff] [blame] | 544 | #if defined(HAVE_SIGBLOCK_SIGSETMASK) |
| Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 545 | TestSignalMaskFunction([]() { |
| 546 | sigblock(~0U); |
| 547 | }); |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 548 | #endif |
| 549 | } |
| 550 | |
| 551 | TEST(signal, sigsetmask_filter) { |
| Elliott Hughes | 215baed | 2023-07-17 17:15:01 -0700 | [diff] [blame] | 552 | #if defined(HAVE_SIGBLOCK_SIGSETMASK) |
| Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 553 | TestSignalMaskFunction([]() { |
| 554 | sigsetmask(~0U); |
| 555 | }); |
| Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 556 | #endif |
| 557 | } |
| 558 | |
| Elliott Hughes | aa0ebda | 2014-02-11 19:57:06 -0800 | [diff] [blame] | 559 | TEST(signal, sys_signame) { |
| Elliott Hughes | 671e236 | 2014-02-12 19:04:27 -0800 | [diff] [blame] | 560 | #if defined(__BIONIC__) |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 561 | ASSERT_TRUE(sys_signame[0] == nullptr); |
| Elliott Hughes | aa0ebda | 2014-02-11 19:57:06 -0800 | [diff] [blame] | 562 | ASSERT_STREQ("HUP", sys_signame[SIGHUP]); |
| 563 | #else |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 564 | GTEST_SKIP() << "glibc doesn't have sys_signame"; |
| Elliott Hughes | aa0ebda | 2014-02-11 19:57:06 -0800 | [diff] [blame] | 565 | #endif |
| 566 | } |
| 567 | |
| 568 | TEST(signal, sys_siglist) { |
| Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 569 | #if !defined(ANDROID_HOST_MUSL) |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 570 | ASSERT_TRUE(sys_siglist[0] == nullptr); |
| Elliott Hughes | aa0ebda | 2014-02-11 19:57:06 -0800 | [diff] [blame] | 571 | ASSERT_STREQ("Hangup", sys_siglist[SIGHUP]); |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 572 | #else |
| 573 | GTEST_SKIP() << "musl doesn't have sys_siglist"; |
| 574 | #endif |
| Elliott Hughes | aa0ebda | 2014-02-11 19:57:06 -0800 | [diff] [blame] | 575 | } |
| Elliott Hughes | 0990d4f | 2014-04-30 09:45:40 -0700 | [diff] [blame] | 576 | |
| 577 | TEST(signal, limits) { |
| Elliott Hughes | 6a65ccd | 2020-02-13 09:48:14 -0800 | [diff] [blame] | 578 | // These come from the kernel. |
| Elliott Hughes | 0990d4f | 2014-04-30 09:45:40 -0700 | [diff] [blame] | 579 | ASSERT_EQ(32, __SIGRTMIN); |
| Elliott Hughes | 6a65ccd | 2020-02-13 09:48:14 -0800 | [diff] [blame] | 580 | ASSERT_EQ(64, __SIGRTMAX); |
| Elliott Hughes | 0990d4f | 2014-04-30 09:45:40 -0700 | [diff] [blame] | 581 | |
| 582 | // We reserve a non-zero number at the bottom for ourselves. |
| 583 | ASSERT_GT(SIGRTMIN, __SIGRTMIN); |
| 584 | |
| Elliott Hughes | 0990d4f | 2014-04-30 09:45:40 -0700 | [diff] [blame] | 585 | // We don't currently reserve any at the top. |
| 586 | ASSERT_EQ(SIGRTMAX, __SIGRTMAX); |
| 587 | } |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 588 | |
| 589 | static int g_sigqueue_signal_handler_call_count = 0; |
| 590 | |
| 591 | static void SigqueueSignalHandler(int signum, siginfo_t* info, void*) { |
| 592 | ASSERT_EQ(SIGALRM, signum); |
| 593 | ASSERT_EQ(SIGALRM, info->si_signo); |
| 594 | ASSERT_EQ(SI_QUEUE, info->si_code); |
| 595 | ASSERT_EQ(1, info->si_value.sival_int); |
| 596 | ++g_sigqueue_signal_handler_call_count; |
| 597 | } |
| 598 | |
| 599 | TEST(signal, sigqueue) { |
| 600 | ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO); |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 601 | sigval sigval = {.sival_int = 1}; |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 602 | errno = 0; |
| 603 | ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 604 | ASSERT_ERRNO(0); |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 605 | ASSERT_EQ(1, g_sigqueue_signal_handler_call_count); |
| 606 | } |
| 607 | |
| Josh Gao | 726b63f | 2018-08-27 16:00:58 -0700 | [diff] [blame] | 608 | TEST(signal, pthread_sigqueue_self) { |
| Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 609 | #if !defined(ANDROID_HOST_MUSL) |
| Josh Gao | 726b63f | 2018-08-27 16:00:58 -0700 | [diff] [blame] | 610 | ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO); |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 611 | sigval sigval = {.sival_int = 1}; |
| Josh Gao | 726b63f | 2018-08-27 16:00:58 -0700 | [diff] [blame] | 612 | errno = 0; |
| 613 | ASSERT_EQ(0, pthread_sigqueue(pthread_self(), SIGALRM, sigval)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 614 | ASSERT_ERRNO(0); |
| Josh Gao | 726b63f | 2018-08-27 16:00:58 -0700 | [diff] [blame] | 615 | ASSERT_EQ(1, g_sigqueue_signal_handler_call_count); |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 616 | #else |
| 617 | GTEST_SKIP() << "musl doesn't have pthread_sigqueue"; |
| 618 | #endif |
| Josh Gao | 726b63f | 2018-08-27 16:00:58 -0700 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | TEST(signal, pthread_sigqueue_other) { |
| Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 622 | #if !defined(ANDROID_HOST_MUSL) |
| Josh Gao | 726b63f | 2018-08-27 16:00:58 -0700 | [diff] [blame] | 623 | ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO); |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 624 | sigval sigval = {.sival_int = 1}; |
| Josh Gao | 726b63f | 2018-08-27 16:00:58 -0700 | [diff] [blame] | 625 | |
| 626 | sigset_t mask; |
| 627 | sigfillset(&mask); |
| 628 | pthread_sigmask(SIG_SETMASK, &mask, nullptr); |
| 629 | pthread_t thread; |
| 630 | int rc = pthread_create(&thread, nullptr, |
| 631 | [](void*) -> void* { |
| 632 | sigset_t mask; |
| 633 | sigemptyset(&mask); |
| 634 | sigsuspend(&mask); |
| 635 | return nullptr; |
| 636 | }, |
| 637 | nullptr); |
| 638 | ASSERT_EQ(0, rc); |
| 639 | |
| 640 | errno = 0; |
| 641 | ASSERT_EQ(0, pthread_sigqueue(thread, SIGALRM, sigval)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 642 | ASSERT_ERRNO(0); |
| Josh Gao | 726b63f | 2018-08-27 16:00:58 -0700 | [diff] [blame] | 643 | pthread_join(thread, nullptr); |
| 644 | ASSERT_EQ(1, g_sigqueue_signal_handler_call_count); |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 645 | #else |
| 646 | GTEST_SKIP() << "musl doesn't have pthread_sigqueue"; |
| 647 | #endif |
| Josh Gao | 726b63f | 2018-08-27 16:00:58 -0700 | [diff] [blame] | 648 | } |
| 649 | |
| Elliott Hughes | 50fca4d | 2020-03-20 16:25:54 -0700 | [diff] [blame] | 650 | TEST(signal, sigwait_SIGALRM) { |
| 651 | SignalMaskRestorer smr; |
| 652 | |
| 653 | // Block SIGALRM. |
| 654 | sigset_t just_SIGALRM; |
| 655 | sigemptyset(&just_SIGALRM); |
| 656 | sigaddset(&just_SIGALRM, SIGALRM); |
| 657 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr)); |
| 658 | |
| 659 | // Raise SIGALRM. |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 660 | sigval sigval = {.sival_int = 1}; |
| Elliott Hughes | 50fca4d | 2020-03-20 16:25:54 -0700 | [diff] [blame] | 661 | ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval)); |
| 662 | |
| 663 | // Get pending SIGALRM. |
| 664 | int sig; |
| 665 | ASSERT_EQ(0, sigwait(&just_SIGALRM, &sig)); |
| 666 | ASSERT_EQ(SIGALRM, sig); |
| 667 | } |
| 668 | |
| 669 | TEST(signal, sigwait64_SIGRTMIN) { |
| 670 | SignalMaskRestorer smr; |
| 671 | |
| 672 | // Block SIGRTMIN. |
| 673 | sigset64_t just_SIGRTMIN; |
| 674 | sigemptyset64(&just_SIGRTMIN); |
| 675 | sigaddset64(&just_SIGRTMIN, SIGRTMIN); |
| 676 | ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr)); |
| 677 | |
| 678 | // Raise SIGRTMIN. |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 679 | sigval sigval = {.sival_int = 1}; |
| Elliott Hughes | 50fca4d | 2020-03-20 16:25:54 -0700 | [diff] [blame] | 680 | ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval)); |
| 681 | |
| 682 | // Get pending SIGRTMIN. |
| 683 | int sig; |
| 684 | ASSERT_EQ(0, sigwait64(&just_SIGRTMIN, &sig)); |
| 685 | ASSERT_EQ(SIGRTMIN, sig); |
| 686 | } |
| 687 | |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 688 | TEST(signal, sigwaitinfo) { |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 689 | SignalMaskRestorer smr; |
| 690 | |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 691 | // Block SIGALRM. |
| 692 | sigset_t just_SIGALRM; |
| 693 | sigemptyset(&just_SIGALRM); |
| 694 | sigaddset(&just_SIGALRM, SIGALRM); |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 695 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr)); |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 696 | |
| 697 | // Raise SIGALRM. |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 698 | sigval sigval = {.sival_int = 1}; |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 699 | ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval)); |
| 700 | |
| 701 | // Get pending SIGALRM. |
| 702 | siginfo_t info; |
| 703 | errno = 0; |
| 704 | ASSERT_EQ(SIGALRM, sigwaitinfo(&just_SIGALRM, &info)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 705 | ASSERT_ERRNO(0); |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 706 | ASSERT_EQ(SIGALRM, info.si_signo); |
| 707 | ASSERT_EQ(1, info.si_value.sival_int); |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 708 | } |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 709 | |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 710 | TEST(signal, sigwaitinfo64_SIGRTMIN) { |
| 711 | SignalMaskRestorer smr; |
| 712 | |
| 713 | // Block SIGRTMIN. |
| 714 | sigset64_t just_SIGRTMIN; |
| 715 | sigemptyset64(&just_SIGRTMIN); |
| 716 | sigaddset64(&just_SIGRTMIN, SIGRTMIN); |
| 717 | ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr)); |
| 718 | |
| 719 | // Raise SIGRTMIN. |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 720 | sigval sigval = {.sival_int = 1}; |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 721 | ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval)); |
| 722 | |
| 723 | // Get pending SIGRTMIN. |
| 724 | siginfo_t info; |
| 725 | errno = 0; |
| 726 | ASSERT_EQ(SIGRTMIN, sigwaitinfo64(&just_SIGRTMIN, &info)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 727 | ASSERT_ERRNO(0); |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 728 | ASSERT_EQ(SIGRTMIN, info.si_signo); |
| 729 | ASSERT_EQ(1, info.si_value.sival_int); |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | TEST(signal, sigtimedwait) { |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 733 | SignalMaskRestorer smr; |
| 734 | |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 735 | // Block SIGALRM. |
| 736 | sigset_t just_SIGALRM; |
| 737 | sigemptyset(&just_SIGALRM); |
| 738 | sigaddset(&just_SIGALRM, SIGALRM); |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 739 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr)); |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 740 | |
| 741 | // Raise SIGALRM. |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 742 | sigval sigval = { .sival_int = 1 }; |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 743 | ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval)); |
| 744 | |
| 745 | // Get pending SIGALRM. |
| 746 | siginfo_t info; |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 747 | timespec timeout = { .tv_sec = 2, .tv_nsec = 0 }; |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 748 | errno = 0; |
| 749 | ASSERT_EQ(SIGALRM, sigtimedwait(&just_SIGALRM, &info, &timeout)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 750 | ASSERT_ERRNO(0); |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 751 | } |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 752 | |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 753 | TEST(signal, sigtimedwait64_SIGRTMIN) { |
| 754 | SignalMaskRestorer smr; |
| 755 | |
| 756 | // Block SIGRTMIN. |
| 757 | sigset64_t just_SIGRTMIN; |
| 758 | sigemptyset64(&just_SIGRTMIN); |
| 759 | sigaddset64(&just_SIGRTMIN, SIGRTMIN); |
| 760 | ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr)); |
| 761 | |
| 762 | // Raise SIGALRM. |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 763 | sigval sigval = { .sival_int = 1 }; |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 764 | ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval)); |
| 765 | |
| 766 | // Get pending SIGALRM. |
| 767 | siginfo_t info; |
| 768 | timespec timeout = { .tv_sec = 2, .tv_nsec = 0 }; |
| 769 | errno = 0; |
| 770 | ASSERT_EQ(SIGRTMIN, sigtimedwait64(&just_SIGRTMIN, &info, &timeout)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 771 | ASSERT_ERRNO(0); |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 772 | } |
| 773 | |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 774 | TEST(signal, sigtimedwait_timeout) { |
| 775 | // Block SIGALRM. |
| 776 | sigset_t just_SIGALRM; |
| 777 | sigemptyset(&just_SIGALRM); |
| 778 | sigaddset(&just_SIGALRM, SIGALRM); |
| 779 | sigset_t original_set; |
| 780 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, &original_set)); |
| 781 | |
| 782 | // Wait timeout. |
| Elliott Hughes | ca3f8e4 | 2019-10-28 15:59:38 -0700 | [diff] [blame] | 783 | auto t0 = std::chrono::steady_clock::now(); |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 784 | siginfo_t info; |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 785 | timespec timeout = { .tv_sec = 0, .tv_nsec = 1000000 }; |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 786 | errno = 0; |
| 787 | ASSERT_EQ(-1, sigtimedwait(&just_SIGALRM, &info, &timeout)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 788 | ASSERT_ERRNO(EAGAIN); |
| Elliott Hughes | ca3f8e4 | 2019-10-28 15:59:38 -0700 | [diff] [blame] | 789 | auto t1 = std::chrono::steady_clock::now(); |
| 790 | ASSERT_GE(t1-t0, 1000000ns); |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 791 | |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 792 | ASSERT_EQ(0, sigprocmask(SIG_SETMASK, &original_set, nullptr)); |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 793 | } |
| Josh Gao | 61cf3f3 | 2016-03-08 15:27:15 -0800 | [diff] [blame] | 794 | |
| 795 | #if defined(__BIONIC__) |
| 796 | TEST(signal, rt_tgsigqueueinfo) { |
| 797 | // Test whether rt_tgsigqueueinfo allows sending arbitrary si_code values to self. |
| 798 | // If this fails, your kernel needs commit 66dd34a to be backported. |
| 799 | static constexpr char error_msg[] = |
| 800 | "\nPlease ensure that the following kernel patch has been applied:\n" |
| 801 | "* https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=66dd34ad31e5963d72a700ec3f2449291d322921\n"; |
| 802 | static siginfo received; |
| 803 | |
| Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 804 | struct sigaction handler = {}; |
| Josh Gao | 61cf3f3 | 2016-03-08 15:27:15 -0800 | [diff] [blame] | 805 | handler.sa_sigaction = [](int, siginfo_t* siginfo, void*) { received = *siginfo; }; |
| 806 | handler.sa_flags = SA_SIGINFO; |
| 807 | |
| 808 | ASSERT_EQ(0, sigaction(SIGUSR1, &handler, nullptr)); |
| 809 | |
| Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 810 | siginfo sent = {.si_code = SI_TKILL}; |
| Josh Gao | 61cf3f3 | 2016-03-08 15:27:15 -0800 | [diff] [blame] | 811 | ASSERT_EQ(0, syscall(SYS_rt_tgsigqueueinfo, getpid(), gettid(), SIGUSR1, &sent)) |
| 812 | << "rt_tgsigqueueinfo failed: " << strerror(errno) << error_msg; |
| 813 | ASSERT_EQ(sent.si_code, received.si_code) << "rt_tgsigqueueinfo modified si_code, expected " |
| 814 | << sent.si_code << ", received " << received.si_code |
| 815 | << error_msg; |
| 816 | |
| 817 | sent.si_code = SI_USER; |
| 818 | ASSERT_EQ(0, syscall(SYS_rt_tgsigqueueinfo, getpid(), gettid(), SIGUSR1, &sent)) |
| 819 | << "rt_tgsigqueueinfo failed: " << strerror(errno) << error_msg; |
| 820 | ASSERT_EQ(sent.si_code, received.si_code) << "rt_tgsigqueueinfo modified si_code, expected " |
| 821 | << sent.si_code << ", received " << received.si_code |
| 822 | << error_msg; |
| 823 | } |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 824 | #endif |
| Josh Gao | c244fcb | 2016-03-29 14:34:03 -0700 | [diff] [blame] | 825 | |
| Josh Gao | c244fcb | 2016-03-29 14:34:03 -0700 | [diff] [blame] | 826 | TEST(signal, sigset_size) { |
| Elliott Hughes | fc03503 | 2022-11-11 22:52:04 +0000 | [diff] [blame] | 827 | // The setjmp implementations assume that sigset_t can fit in a long. |
| 828 | // This is true because the 32-bit ABIs have broken rt signal support, |
| 829 | // but the 64-bit ABIs both have a SIGRTMAX defined as 64. |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 830 | #if defined(__BIONIC__) |
| Josh Gao | c244fcb | 2016-03-29 14:34:03 -0700 | [diff] [blame] | 831 | static_assert(sizeof(sigset_t) <= sizeof(long), "sigset_t doesn't fit in a long"); |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 832 | #endif |
| 833 | static_assert(sizeof(sigset64_t)*8 >= 64, "sigset64_t too small for real-time signals"); |
| Josh Gao | c244fcb | 2016-03-29 14:34:03 -0700 | [diff] [blame] | 834 | } |
| 835 | |
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 836 | TEST(signal, sigignore_EINVAL) { |
| 837 | errno = 0; |
| 838 | ASSERT_EQ(-1, sigignore(99999)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 839 | ASSERT_ERRNO(EINVAL); |
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | TEST(signal, sigignore) { |
| 843 | errno = 0; |
| 844 | EXPECT_EQ(-1, sigignore(SIGKILL)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 845 | EXPECT_ERRNO(EINVAL); |
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 846 | |
| 847 | errno = 0; |
| 848 | EXPECT_EQ(-1, sigignore(SIGSTOP)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 849 | EXPECT_ERRNO(EINVAL); |
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 850 | |
| 851 | ScopedSignalHandler sigalrm{SIGALRM}; |
| 852 | ASSERT_EQ(0, sigignore(SIGALRM)); |
| 853 | |
| 854 | struct sigaction sa; |
| 855 | ASSERT_EQ(0, sigaction(SIGALRM, nullptr, &sa)); |
| 856 | EXPECT_EQ(SIG_IGN, sa.sa_handler); |
| 857 | } |
| 858 | |
| 859 | TEST(signal, sighold_EINVAL) { |
| 860 | errno = 0; |
| 861 | ASSERT_EQ(-1, sighold(99999)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 862 | ASSERT_ERRNO(EINVAL); |
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | TEST(signal, sigpause_EINVAL) { |
| 866 | errno = 0; |
| 867 | ASSERT_EQ(-1, sigpause(99999)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 868 | ASSERT_ERRNO(EINVAL); |
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | TEST(signal, sigrelse_EINVAL) { |
| 872 | errno = 0; |
| 873 | ASSERT_EQ(-1, sigpause(99999)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 874 | ASSERT_ERRNO(EINVAL); |
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 875 | } |
| 876 | |
| Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 877 | static void TestSigholdSigpauseSigrelse(int sig) { |
| 878 | static int signal_handler_call_count = 0; |
| 879 | ScopedSignalHandler ssh{sig, [](int) { signal_handler_call_count++; }}; |
| 880 | SignalMaskRestorer mask_restorer; |
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 881 | sigset_t set; |
| 882 | |
| Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 883 | // sighold(SIGALRM/SIGRTMIN) should add SIGALRM/SIGRTMIN to the signal mask ... |
| 884 | ASSERT_EQ(0, sighold(sig)); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 885 | ASSERT_EQ(0, sigprocmask(SIG_SETMASK, nullptr, &set)); |
| Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 886 | EXPECT_TRUE(sigismember(&set, sig)); |
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 887 | |
| Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 888 | // ... preventing our SIGALRM/SIGRTMIN handler from running ... |
| 889 | raise(sig); |
| 890 | ASSERT_EQ(0, signal_handler_call_count); |
| 891 | // ... until sigpause(SIGALRM/SIGRTMIN) temporarily unblocks it. |
| 892 | ASSERT_EQ(-1, sigpause(sig)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 893 | ASSERT_ERRNO(EINTR); |
| Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 894 | ASSERT_EQ(1, signal_handler_call_count); |
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 895 | |
| Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 896 | if (sig >= SIGRTMIN && sizeof(void*) == 8) { |
| 897 | // But sigpause(SIGALRM/SIGRTMIN) shouldn't permanently unblock SIGALRM/SIGRTMIN. |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 898 | ASSERT_EQ(0, sigprocmask(SIG_SETMASK, nullptr, &set)); |
| Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 899 | EXPECT_TRUE(sigismember(&set, sig)); |
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 900 | |
| Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 901 | // Whereas sigrelse(SIGALRM/SIGRTMIN) should. |
| 902 | ASSERT_EQ(0, sigrelse(sig)); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 903 | ASSERT_EQ(0, sigprocmask(SIG_SETMASK, nullptr, &set)); |
| Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 904 | EXPECT_FALSE(sigismember(&set, sig)); |
| 905 | } else { |
| 906 | // sigismember won't work for SIGRTMIN on LP32. |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | TEST(signal, sighold_sigpause_sigrelse) { |
| 911 | TestSigholdSigpauseSigrelse(SIGALRM); |
| 912 | } |
| 913 | |
| 914 | TEST(signal, sighold_sigpause_sigrelse_RT) { |
| 915 | TestSigholdSigpauseSigrelse(SIGRTMIN); |
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | TEST(signal, sigset_EINVAL) { |
| 919 | errno = 0; |
| 920 | ASSERT_EQ(SIG_ERR, sigset(99999, SIG_DFL)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 921 | ASSERT_ERRNO(EINVAL); |
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 922 | } |
| 923 | |
| Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 924 | TEST(signal, sigset_RT) { |
| 925 | static int signal_handler_call_count = 0; |
| 926 | auto signal_handler = [](int) { signal_handler_call_count++; }; |
| 927 | ScopedSignalHandler ssh{SIGRTMIN, signal_handler}; |
| 928 | SignalMaskRestorer mask_restorer; |
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 929 | |
| Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 930 | ASSERT_EQ(signal_handler, sigset(SIGRTMIN, SIG_HOLD)); |
| 931 | #if defined(__LP64__) |
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 932 | sigset_t set; |
| Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 933 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set)); |
| 934 | ASSERT_TRUE(sigismember(&set, SIGRTMIN)); |
| 935 | #endif |
| 936 | |
| 937 | ASSERT_EQ(SIG_HOLD, sigset(SIGRTMIN, signal_handler)); |
| 938 | ASSERT_EQ(signal_handler, sigset(SIGRTMIN, signal_handler)); |
| 939 | ASSERT_EQ(0, signal_handler_call_count); |
| 940 | raise(SIGRTMIN); |
| 941 | ASSERT_EQ(1, signal_handler_call_count); |
| 942 | } |
| 943 | |
| 944 | TEST(signal, sigset) { |
| 945 | static int signal_handler_call_count = 0; |
| 946 | auto signal_handler = [](int) { signal_handler_call_count++; }; |
| 947 | ScopedSignalHandler ssh{SIGALRM, signal_handler}; |
| 948 | SignalMaskRestorer mask_restorer; |
| 949 | |
| 950 | ASSERT_EQ(0, signal_handler_call_count); |
| 951 | raise(SIGALRM); |
| 952 | ASSERT_EQ(1, signal_handler_call_count); |
| 953 | |
| 954 | // Block SIGALRM so the next sigset(SIGARLM) call will return SIG_HOLD. |
| 955 | sigset_t set; |
| 956 | sigemptyset(&set); |
| 957 | sigaddset(&set, SIGALRM); |
| 958 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &set, nullptr)); |
| 959 | |
| 960 | sigemptyset(&set); |
| 961 | ASSERT_EQ(SIG_HOLD, sigset(SIGALRM, signal_handler)); |
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 962 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set)); |
| 963 | EXPECT_FALSE(sigismember(&set, SIGALRM)); |
| 964 | |
| Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 965 | ASSERT_EQ(signal_handler, sigset(SIGALRM, SIG_IGN)); |
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 966 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set)); |
| 967 | EXPECT_FALSE(sigismember(&set, SIGALRM)); |
| 968 | |
| 969 | ASSERT_EQ(SIG_IGN, sigset(SIGALRM, SIG_DFL)); |
| 970 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set)); |
| 971 | EXPECT_FALSE(sigismember(&set, SIGALRM)); |
| 972 | |
| 973 | ASSERT_EQ(SIG_DFL, sigset(SIGALRM, SIG_HOLD)); |
| 974 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set)); |
| 975 | EXPECT_TRUE(sigismember(&set, SIGALRM)); |
| 976 | } |
| Elliott Hughes | 7532b32 | 2017-07-11 15:00:17 -0700 | [diff] [blame] | 977 | |
| 978 | TEST(signal, killpg_EINVAL) { |
| 979 | // POSIX leaves pgrp <= 1 undefined, but glibc fails with EINVAL for < 0 |
| 980 | // and passes 0 through to kill(2). |
| 981 | errno = 0; |
| 982 | ASSERT_EQ(-1, killpg(-1, SIGKILL)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 983 | ASSERT_ERRNO(EINVAL); |
| Elliott Hughes | 7532b32 | 2017-07-11 15:00:17 -0700 | [diff] [blame] | 984 | } |
| Elliott Hughes | 55c1e37 | 2024-08-08 19:37:14 +0000 | [diff] [blame] | 985 | |
| 986 | TEST(signal, sig2str) { |
| 987 | #if defined(__BIONIC__) |
| 988 | char str[SIG2STR_MAX]; |
| 989 | |
| 990 | // A regular signal. |
| 991 | ASSERT_EQ(0, sig2str(SIGHUP, str)); |
| 992 | ASSERT_STREQ("HUP", str); |
| 993 | |
| 994 | // A real-time signal. |
| 995 | ASSERT_EQ(0, sig2str(SIGRTMIN + 4, str)); |
| 996 | ASSERT_STREQ("RTMIN+4", str); |
| 997 | ASSERT_EQ(0, sig2str(SIGRTMAX - 4, str)); |
| 998 | ASSERT_STREQ("RTMAX-4", str); |
| 999 | // Special cases. |
| 1000 | ASSERT_EQ(0, sig2str(SIGRTMAX, str)); |
| 1001 | ASSERT_STREQ("RTMAX", str); |
| 1002 | ASSERT_EQ(0, sig2str(SIGRTMIN, str)); |
| 1003 | ASSERT_STREQ("RTMIN", str); |
| 1004 | // One of the signals the C library keeps to itself. |
| 1005 | ASSERT_EQ(-1, sig2str(32, str)); // __SIGRTMIN |
| 1006 | |
| 1007 | // Errors. |
| 1008 | ASSERT_EQ(-1, sig2str(-1, str)); // Too small. |
| 1009 | ASSERT_EQ(-1, sig2str(0, str)); // Still too small. |
| 1010 | ASSERT_EQ(-1, sig2str(1234, str)); // Too large. |
| 1011 | #else |
| 1012 | GTEST_SKIP() << "our old glibc doesn't have sig2str"; |
| 1013 | #endif |
| 1014 | } |
| 1015 | |
| 1016 | TEST(signal, str2sig) { |
| 1017 | #if defined(__BIONIC__) |
| 1018 | int sig; |
| 1019 | |
| 1020 | // A regular signal, by number. |
| 1021 | sig = -1; |
| 1022 | ASSERT_EQ(0, str2sig("9", &sig)); |
| 1023 | ASSERT_EQ(SIGKILL, sig); |
| 1024 | |
| 1025 | // A regular signal, by name. |
| 1026 | sig = -1; |
| 1027 | ASSERT_EQ(0, str2sig("HUP", &sig)); |
| 1028 | ASSERT_EQ(SIGHUP, sig); |
| 1029 | |
| 1030 | // A real-time signal, by number. |
| 1031 | sig = -1; |
| 1032 | ASSERT_EQ(0, str2sig("64", &sig)); |
| 1033 | ASSERT_EQ(SIGRTMAX, sig); |
| 1034 | |
| 1035 | // A real-time signal, by name and offset. |
| 1036 | sig = -1; |
| 1037 | ASSERT_EQ(0, str2sig("RTMAX-4", &sig)); |
| 1038 | ASSERT_EQ(SIGRTMAX - 4, sig); |
| 1039 | sig = -1; |
| 1040 | ASSERT_EQ(0, str2sig("RTMIN+4", &sig)); |
| 1041 | ASSERT_EQ(SIGRTMIN + 4, sig); |
| 1042 | // Unspecified by POSIX, but we try to be reasonable. |
| 1043 | sig = -1; |
| 1044 | ASSERT_EQ(0, str2sig("RTMAX-0", &sig)); |
| 1045 | ASSERT_EQ(SIGRTMAX, sig); |
| 1046 | sig = -1; |
| 1047 | ASSERT_EQ(0, str2sig("RTMIN+0", &sig)); |
| 1048 | ASSERT_EQ(SIGRTMIN, sig); |
| 1049 | // One of the signals the C library keeps to itself, numerically. |
| 1050 | ASSERT_EQ(-1, str2sig("32", &sig)); // __SIGRTMIN |
| 1051 | |
| 1052 | // Special cases. |
| 1053 | sig = -1; |
| 1054 | ASSERT_EQ(0, str2sig("RTMAX", &sig)); |
| 1055 | ASSERT_EQ(SIGRTMAX, sig); |
| 1056 | sig = -1; |
| 1057 | ASSERT_EQ(0, str2sig("RTMIN", &sig)); |
| 1058 | ASSERT_EQ(SIGRTMIN, sig); |
| 1059 | |
| 1060 | // Errors. |
| 1061 | ASSERT_EQ(-1, str2sig("SIGHUP", &sig)); // No "SIG" prefix allowed. |
| 1062 | ASSERT_EQ(-1, str2sig("-1", &sig)); // Too small. |
| 1063 | ASSERT_EQ(-1, str2sig("0", &sig)); // Still too small. |
| 1064 | ASSERT_EQ(-1, str2sig("1234", &sig)); // Too large. |
| 1065 | ASSERT_EQ(-1, str2sig("RTMAX-666", &sig)); // Offset too small. |
| 1066 | ASSERT_EQ(-1, str2sig("RTMIN+666", &sig)); // Offset too large. |
| 1067 | ASSERT_EQ(-1, str2sig("RTMAX-+1", &sig)); // Silly. |
| 1068 | ASSERT_EQ(-1, str2sig("RTMIN+-1", &sig)); // Silly. |
| 1069 | ASSERT_EQ(-1, str2sig("HUPs", &sig)); // Trailing junk. |
| 1070 | ASSERT_EQ(-1, str2sig("2b", &sig)); // Trailing junk. |
| 1071 | ASSERT_EQ(-1, str2sig("RTMIN+2b", &sig)); // Trailing junk. |
| 1072 | #else |
| 1073 | GTEST_SKIP() << "our old glibc doesn't have str2sig"; |
| 1074 | #endif |
| 1075 | } |
| Tamas Zsoldos | 3bfa4bc | 2025-04-29 08:27:50 -0700 | [diff] [blame] | 1076 | |
| 1077 | #if defined(__aarch64__) |
| Tamas Petz | 9903278 | 2025-05-14 14:03:43 +0200 | [diff] [blame] | 1078 | static void raises_sigusr1() { |
| Tamas Zsoldos | 3bfa4bc | 2025-04-29 08:27:50 -0700 | [diff] [blame] | 1079 | raise(SIGUSR1); |
| 1080 | } |
| 1081 | |
| 1082 | TEST(signal, sme_tpidr2_clear) { |
| 1083 | // When using SME, on entering a signal handler the kernel should clear TPIDR2_EL0, but this was |
| 1084 | // not always correctly done. This tests checks if the kernel correctly clears it or not. |
| Tamas Petz | 9903278 | 2025-05-14 14:03:43 +0200 | [diff] [blame] | 1085 | if (!sme_is_enabled()) { |
| Tamas Zsoldos | 3bfa4bc | 2025-04-29 08:27:50 -0700 | [diff] [blame] | 1086 | GTEST_SKIP() << "SME is not enabled on device."; |
| 1087 | } |
| 1088 | |
| 1089 | static uint64_t tpidr2 = 0; |
| 1090 | struct sigaction handler = {}; |
| 1091 | handler.sa_sigaction = [](int, siginfo_t*, void*) { |
| Tamas Petz | 9903278 | 2025-05-14 14:03:43 +0200 | [diff] [blame] | 1092 | tpidr2 = sme_tpidr2_el0(); |
| 1093 | sme_set_tpidr2_el0(0UL); |
| Tamas Zsoldos | 3bfa4bc | 2025-04-29 08:27:50 -0700 | [diff] [blame] | 1094 | }; |
| 1095 | handler.sa_flags = SA_SIGINFO; |
| 1096 | |
| 1097 | ASSERT_EQ(0, sigaction(SIGUSR1, &handler, nullptr)); |
| 1098 | |
| Tamas Petz | 9903278 | 2025-05-14 14:03:43 +0200 | [diff] [blame] | 1099 | sme_dormant_caller(&raises_sigusr1); |
| Tamas Zsoldos | 3bfa4bc | 2025-04-29 08:27:50 -0700 | [diff] [blame] | 1100 | |
| 1101 | ASSERT_EQ(0x0UL, tpidr2) |
| 1102 | << "Broken kernel! TPIDR2_EL0 was not null in the signal handler! " |
| 1103 | << "Please make sure the following patch has been applied to the kernel: " |
| 1104 | << "https://lore.kernel.org/linux-arm-kernel/[email protected]/"; |
| 1105 | } |
| 1106 | #endif |
| Elliott Hughes | f2ee3db | 2025-05-06 14:59:21 -0700 | [diff] [blame] | 1107 | |
| 1108 | TEST(signal, psignal) { |
| 1109 | CapturedStderr cap; |
| 1110 | psignal(SIGINT, "a b c"); |
| 1111 | ASSERT_EQ(cap.str(), "a b c: Interrupt\n"); |
| 1112 | } |
| 1113 | |
| 1114 | TEST(signal, psignal_null) { |
| 1115 | CapturedStderr cap; |
| 1116 | psignal(SIGINT, nullptr); |
| 1117 | ASSERT_EQ(cap.str(), "Interrupt\n"); |
| 1118 | } |
| 1119 | |
| 1120 | TEST(signal, psignal_empty) { |
| 1121 | CapturedStderr cap; |
| 1122 | psignal(SIGINT, ""); |
| 1123 | ASSERT_EQ(cap.str(), "Interrupt\n"); |
| 1124 | } |
| 1125 | |
| 1126 | TEST(signal, psiginfo) { |
| 1127 | CapturedStderr cap; |
| 1128 | siginfo_t si{.si_signo = SIGINT}; |
| 1129 | psiginfo(&si, "a b c"); |
| 1130 | ASSERT_EQ(cap.str(), "a b c: Interrupt\n"); |
| 1131 | } |
| 1132 | |
| 1133 | TEST(signal, psiginfo_null) { |
| 1134 | CapturedStderr cap; |
| 1135 | siginfo_t si{.si_signo = SIGINT}; |
| 1136 | psiginfo(&si, nullptr); |
| 1137 | ASSERT_EQ(cap.str(), "Interrupt\n"); |
| 1138 | } |
| 1139 | |
| 1140 | TEST(signal, psiginfo_empty) { |
| 1141 | CapturedStderr cap; |
| 1142 | siginfo_t si{.si_signo = SIGINT}; |
| 1143 | psiginfo(&si, ""); |
| 1144 | ASSERT_EQ(cap.str(), "Interrupt\n"); |
| 1145 | } |