2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2018 Eric van Gyzen
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
42 * - no effect without SA_ONSTACK
45 static sig_atomic_t disabled_correct = 1;
46 static sig_atomic_t level1_correct = -1;
47 static sig_atomic_t level2_correct = -1;
50 sig_handler(int signo, siginfo_t *info __unused, void *ucp)
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;
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;
71 ATF_TC_HEAD(ss_onstack, tc)
74 atf_tc_set_md_var(tc, "descr", "Test reporting of SS_ONSTACK");
77 ATF_TC_BODY(ss_onstack, tc)
86 ss.ss_sp = malloc(ss.ss_size);
87 ATF_REQUIRE(ss.ss_sp != NULL);
88 ATF_REQUIRE(sigaltstack(&ss, &oss) == 0);
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);
96 struct sigaction sa = {
97 .sa_sigaction = sig_handler,
98 .sa_flags = SA_ONSTACK | SA_SIGINFO,
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);
105 ATF_CHECK(level1_correct != -1);
106 ATF_CHECK(level1_correct == 1);
107 ATF_CHECK(level2_correct != -1);
108 ATF_CHECK(level2_correct == 1);
114 ATF_TP_ADD_TC(tp, ss_onstack);
116 return (atf_no_error());