1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2018 Eric van Gyzen 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <atf-c.h> 32 #include <errno.h> 33 #include <signal.h> 34 #include <stdbool.h> 35 #include <stdlib.h> 36 37 /* 38 * TODO add tests for: 39 * 40 * - all error cases 41 * - SS_DISABLE 42 * - no effect without SA_ONSTACK 43 */ 44 45 static sig_atomic_t disabled_correct = 1; 46 static sig_atomic_t level1_correct = -1; 47 static sig_atomic_t level2_correct = -1; 48 49 static void 50 sig_handler(int signo, siginfo_t *info __unused, void *ucp) 51 { 52 ucontext_t *uc = ucp; 53 54 // The alternate signal stack is enabled. 55 disabled_correct = disabled_correct && 56 (uc->uc_stack.ss_flags & SS_DISABLE) == 0; 57 if (signo == SIGUSR1) { 58 // The thread was NOT running on the alternate signal 59 // stack when this signal arrived. 60 level1_correct = (uc->uc_stack.ss_flags & SS_ONSTACK) == 0; 61 raise(SIGUSR2); 62 } else { 63 // The thread WAS running on the alternate signal 64 // stack when this signal arrived. 65 level2_correct = (uc->uc_stack.ss_flags & SS_ONSTACK) != 0; 66 } 67 } 68 69 ATF_TC(ss_onstack); 70 71 ATF_TC_HEAD(ss_onstack, tc) 72 { 73 74 atf_tc_set_md_var(tc, "descr", "Test reporting of SS_ONSTACK"); 75 } 76 77 ATF_TC_BODY(ss_onstack, tc) 78 { 79 stack_t ss = { 80 .ss_size = SIGSTKSZ, 81 }; 82 stack_t oss = { 83 .ss_size = 0, 84 }; 85 86 ss.ss_sp = malloc(ss.ss_size); 87 ATF_REQUIRE(ss.ss_sp != NULL); 88 ATF_REQUIRE(sigaltstack(&ss, &oss) == 0); 89 90 // There should be no signal stack currently configured. 91 ATF_CHECK(oss.ss_sp == NULL); 92 ATF_CHECK(oss.ss_size == 0); 93 ATF_CHECK((oss.ss_flags & SS_DISABLE) != 0); 94 ATF_CHECK((oss.ss_flags & SS_ONSTACK) == 0); 95 96 struct sigaction sa = { 97 .sa_sigaction = sig_handler, 98 .sa_flags = SA_ONSTACK | SA_SIGINFO, 99 }; 100 ATF_REQUIRE(sigemptyset(&sa.sa_mask) == 0); 101 ATF_REQUIRE(sigaction(SIGUSR1, &sa, NULL) == 0); 102 ATF_REQUIRE(sigaction(SIGUSR2, &sa, NULL) == 0); 103 ATF_REQUIRE(raise(SIGUSR1) == 0); 104 105 ATF_CHECK(level1_correct != -1); 106 ATF_CHECK(level1_correct == 1); 107 ATF_CHECK(level2_correct != -1); 108 ATF_CHECK(level2_correct == 1); 109 } 110 111 ATF_TP_ADD_TCS(tp) 112 { 113 114 ATF_TP_ADD_TC(tp, ss_onstack); 115 116 return (atf_no_error()); 117 } 118