1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2019 Kyle Evans <kevans@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <errno.h> 30 #include <signal.h> 31 #include <stdbool.h> 32 33 #include <atf-c.h> 34 35 /* Return the status of the specified sig's bit. */ 36 static bool 37 sigbitstatus(const sigset_t *set, int sig) 38 { 39 40 return (set->__bits[_SIG_WORD(sig)] & _SIG_BIT(sig)) != 0; 41 } 42 43 /* Verify that sig is the lone bit set in the sigset. */ 44 static void 45 siglonebit(const sigset_t *set, int sig) 46 { 47 int i; 48 49 for (i = 0; i < _SIG_WORDS; ++i) { 50 if (i != _SIG_WORD(sig)) 51 ATF_REQUIRE_MSG(set->__bits[i] == 0, 52 "word %d altered to %x", i, set->__bits[i]); 53 else 54 ATF_REQUIRE_MSG((set->__bits[i] & ~_SIG_BIT(sig)) == 0, 55 "word %d has other bits set: %x", i, 56 set->__bits[i] & ~_SIG_BIT(sig)); 57 } 58 } 59 60 static void 61 sigcompare(const sigset_t *left, const sigset_t *right) 62 { 63 int i; 64 65 for (i = 0; i < _SIG_WORDS; ++i) { 66 ATF_REQUIRE_MSG(left->__bits[i] == right->__bits[i], 67 "sig comparison failed at %d; left=%x, right=%x", 68 i, left->__bits[i], right->__bits[i]); 69 } 70 } 71 72 /* 73 * Test implementation details of our sigsetops... make sure the correct bits 74 * are getting set, for the most part, and that duplicate operations don't 75 * error out. 76 */ 77 ATF_TC_WITHOUT_HEAD(posix_sigsetop_test); 78 ATF_TC_BODY(posix_sigsetop_test, tc) 79 { 80 sigset_t set; 81 int i; 82 83 ATF_REQUIRE(sigfillset(&set) == 0); 84 for (i = 0; i < _SIG_WORDS; ++i) { 85 ATF_REQUIRE_MSG(set.__bits[i] == ~0U, "sigfillset failed @ %d", 86 i); 87 } 88 ATF_REQUIRE(sigemptyset(&set) == 0); 89 for (i = 0; i < _SIG_WORDS; ++i) { 90 ATF_REQUIRE_MSG(set.__bits[i] == 0, "sigemptyset failed @ %d", 91 i); 92 } 93 /* Ensure that sigismember reflects the empty set status. */ 94 for (i = 1; i < NSIG; ++i) { 95 ATF_REQUIRE(sigismember(&set, i) == 0); 96 } 97 98 ATF_REQUIRE(sigaddset(&set, -1) == -1 && errno == EINVAL); 99 ATF_REQUIRE(sigaddset(&set, _SIG_MAXSIG + 1) == -1 && errno == EINVAL); 100 ATF_REQUIRE(sigdelset(&set, -1) == -1 && errno == EINVAL); 101 ATF_REQUIRE(sigdelset(&set, _SIG_MAXSIG + 1) == -1 && errno == EINVAL); 102 103 ATF_REQUIRE(sigaddset(&set, SIGSEGV) == 0); 104 ATF_REQUIRE(sigismember(&set, SIGSEGV) != 0); 105 ATF_REQUIRE_MSG(sigbitstatus(&set, SIGSEGV), "sigaddset failure"); 106 siglonebit(&set, SIGSEGV); 107 108 /* 109 * A second addition should succeed without altering the state. This 110 * should be trivially true. 111 */ 112 ATF_REQUIRE(sigaddset(&set, SIGSEGV) == 0); 113 ATF_REQUIRE_MSG(sigbitstatus(&set, SIGSEGV), 114 "sigaddset twice changed bit"); 115 116 ATF_REQUIRE(sigdelset(&set, SIGSEGV) == 0); 117 ATF_REQUIRE_MSG(!sigbitstatus(&set, SIGSEGV), "sigdelset failure"); 118 ATF_REQUIRE(sigismember(&set, SIGSEGV) == 0); 119 ATF_REQUIRE(sigdelset(&set, SIGSEGV) == 0); 120 ATF_REQUIRE_MSG(!sigbitstatus(&set, SIGSEGV), 121 "sigdelset twice changed bit"); 122 for (i = 0; i < _SIG_WORDS; ++i) { 123 ATF_REQUIRE_MSG(set.__bits[i] == 0, "set not empty @ %d", 124 i); 125 } 126 for (i = 1; i < NSIG; ++i) { 127 ATF_REQUIRE(sigismember(&set, i) == 0); 128 } 129 } 130 131 /* 132 * Test extended sigset ops for union/intersection and testing of empty set. 133 */ 134 ATF_TC_WITHOUT_HEAD(extended_sigsetop_test); 135 ATF_TC_BODY(extended_sigsetop_test, tc) 136 { 137 sigset_t chkset, set1, set2, set3; 138 139 sigemptyset(&chkset); 140 sigemptyset(&set1); 141 sigemptyset(&set2); 142 ATF_REQUIRE(sigisemptyset(&chkset) != 0); 143 sigaddset(&set1, SIGSEGV); 144 sigaddset(&set2, SIGKILL); 145 sigaddset(&chkset, SIGSEGV); 146 ATF_REQUIRE(sigisemptyset(&chkset) == 0); 147 sigaddset(&chkset, SIGKILL); 148 ATF_REQUIRE(sigorset(&set3, &set1, &set2) == 0); 149 ATF_REQUIRE(sigbitstatus(&set3, SIGSEGV)); 150 ATF_REQUIRE(sigbitstatus(&set3, SIGKILL)); 151 152 /* 153 * chkset was built with our POSIX-specified set operations that we've 154 * already tested, so it's a good comparison. 155 */ 156 sigcompare(&chkset, &set3); 157 /* 158 * Clear chkset; make sure sigisemptyset() still looks ok. sigaddset 159 * and sigdelset have already been tested to make sure that they're not 160 * touching other bits. 161 */ 162 sigdelset(&chkset, SIGSEGV); 163 sigdelset(&chkset, SIGKILL); 164 ATF_REQUIRE(sigisemptyset(&chkset) != 0); 165 ATF_REQUIRE(sigandset(&set3, &set1, &set2) == 0); 166 /* Make sure we clobbered these. */ 167 ATF_REQUIRE(!sigbitstatus(&set3, SIGSEGV)); 168 ATF_REQUIRE(!sigbitstatus(&set3, SIGKILL)); 169 ATF_REQUIRE(sigisemptyset(&set3) != 0); 170 /* Rebuild for sigandset test */ 171 sigemptyset(&set1); 172 sigemptyset(&set2); 173 sigaddset(&set1, SIGSEGV); 174 sigaddset(&set1, SIGKILL); 175 sigaddset(&set2, SIGSEGV); 176 ATF_REQUIRE(sigandset(&set3, &set1, &set2) == 0); 177 ATF_REQUIRE(sigbitstatus(&set3, SIGSEGV)); 178 ATF_REQUIRE(!sigbitstatus(&set3, SIGKILL)); 179 } 180 181 ATF_TP_ADD_TCS(tp) 182 { 183 184 ATF_TP_ADD_TC(tp, posix_sigsetop_test); 185 ATF_TP_ADD_TC(tp, extended_sigsetop_test); 186 187 return (atf_no_error()); 188 } 189