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>
40 * - no effect without SA_ONSTACK
43 static sig_atomic_t disabled_correct = 1;
44 static sig_atomic_t level1_correct = -1;
45 static sig_atomic_t level2_correct = -1;
48 sig_handler(int signo, siginfo_t *info __unused, void *ucp)
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;
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;
69 ATF_TC_HEAD(ss_onstack, tc)
72 atf_tc_set_md_var(tc, "descr", "Test reporting of SS_ONSTACK");
75 ATF_TC_BODY(ss_onstack, tc)
84 ss.ss_sp = malloc(ss.ss_size);
85 ATF_REQUIRE(ss.ss_sp != NULL);
86 ATF_REQUIRE(sigaltstack(&ss, &oss) == 0);
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);
94 struct sigaction sa = {
95 .sa_sigaction = sig_handler,
96 .sa_flags = SA_ONSTACK | SA_SIGINFO,
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);
103 ATF_CHECK(level1_correct != -1);
104 ATF_CHECK(level1_correct == 1);
105 ATF_CHECK(level2_correct != -1);
106 ATF_CHECK(level2_correct == 1);
112 ATF_TP_ADD_TC(tp, ss_onstack);
114 return (atf_no_error());