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 #include <atf-c.h> 30 #include <errno.h> 31 #include <signal.h> 32 #include <stdbool.h> 33 #include <stdlib.h> 34 35 /* 36 * TODO add tests for: 37 * 38 * - all error cases 39 * - SS_DISABLE 40 * - no effect without SA_ONSTACK 41 */ 42 43 static sig_atomic_t disabled_correct = 1; 44 static sig_atomic_t level1_correct = -1; 45 static sig_atomic_t level2_correct = -1; 46 47 static void 48 sig_handler(int signo, siginfo_t *info __unused, void *ucp) 49 { 50 ucontext_t *uc = ucp; 51 52 // The alternate signal stack is enabled. 53 disabled_correct = disabled_correct && 54 (uc->uc_stack.ss_flags & SS_DISABLE) == 0; 55 if (signo == SIGUSR1) { 56 // The thread was NOT running on the alternate signal 57 // stack when this signal arrived. 58 level1_correct = (uc->uc_stack.ss_flags & SS_ONSTACK) == 0; 59 raise(SIGUSR2); 60 } else { 61 // The thread WAS running on the alternate signal 62 // stack when this signal arrived. 63 level2_correct = (uc->uc_stack.ss_flags & SS_ONSTACK) != 0; 64 } 65 } 66 67 ATF_TC(ss_onstack); 68 69 ATF_TC_HEAD(ss_onstack, tc) 70 { 71 72 atf_tc_set_md_var(tc, "descr", "Test reporting of SS_ONSTACK"); 73 } 74 75 ATF_TC_BODY(ss_onstack, tc) 76 { 77 stack_t ss = { 78 .ss_size = SIGSTKSZ, 79 }; 80 stack_t oss = { 81 .ss_size = 0, 82 }; 83 84 ss.ss_sp = malloc(ss.ss_size); 85 ATF_REQUIRE(ss.ss_sp != NULL); 86 ATF_REQUIRE(sigaltstack(&ss, &oss) == 0); 87 88 // There should be no signal stack currently configured. 89 ATF_CHECK(oss.ss_sp == NULL); 90 ATF_CHECK(oss.ss_size == 0); 91 ATF_CHECK((oss.ss_flags & SS_DISABLE) != 0); 92 ATF_CHECK((oss.ss_flags & SS_ONSTACK) == 0); 93 94 struct sigaction sa = { 95 .sa_sigaction = sig_handler, 96 .sa_flags = SA_ONSTACK | SA_SIGINFO, 97 }; 98 ATF_REQUIRE(sigemptyset(&sa.sa_mask) == 0); 99 ATF_REQUIRE(sigaction(SIGUSR1, &sa, NULL) == 0); 100 ATF_REQUIRE(sigaction(SIGUSR2, &sa, NULL) == 0); 101 ATF_REQUIRE(raise(SIGUSR1) == 0); 102 103 ATF_CHECK(level1_correct != -1); 104 ATF_CHECK(level1_correct == 1); 105 ATF_CHECK(level2_correct != -1); 106 ATF_CHECK(level2_correct == 1); 107 } 108 109 ATF_TP_ADD_TCS(tp) 110 { 111 112 ATF_TP_ADD_TC(tp, ss_onstack); 113 114 return (atf_no_error()); 115 } 116