/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2018 Eric van Gyzen4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <sys/cdefs.h>28#include <atf-c.h>29#include <errno.h>30#include <signal.h>31#include <stdbool.h>32#include <stdlib.h>3334/*35* TODO add tests for:36*37* - all error cases38* - SS_DISABLE39* - no effect without SA_ONSTACK40*/4142static sig_atomic_t disabled_correct = 1;43static sig_atomic_t level1_correct = -1;44static sig_atomic_t level2_correct = -1;4546static void47sig_handler(int signo, siginfo_t *info __unused, void *ucp)48{49ucontext_t *uc = ucp;5051// The alternate signal stack is enabled.52disabled_correct = disabled_correct &&53(uc->uc_stack.ss_flags & SS_DISABLE) == 0;54if (signo == SIGUSR1) {55// The thread was NOT running on the alternate signal56// stack when this signal arrived.57level1_correct = (uc->uc_stack.ss_flags & SS_ONSTACK) == 0;58raise(SIGUSR2);59} else {60// The thread WAS running on the alternate signal61// stack when this signal arrived.62level2_correct = (uc->uc_stack.ss_flags & SS_ONSTACK) != 0;63}64}6566ATF_TC(ss_onstack);6768ATF_TC_HEAD(ss_onstack, tc)69{7071atf_tc_set_md_var(tc, "descr", "Test reporting of SS_ONSTACK");72}7374ATF_TC_BODY(ss_onstack, tc)75{76stack_t ss = {77.ss_size = SIGSTKSZ,78};79stack_t oss = {80.ss_size = 0,81};8283ss.ss_sp = malloc(ss.ss_size);84ATF_REQUIRE(ss.ss_sp != NULL);85ATF_REQUIRE(sigaltstack(&ss, &oss) == 0);8687// There should be no signal stack currently configured.88ATF_CHECK(oss.ss_sp == NULL);89ATF_CHECK(oss.ss_size == 0);90ATF_CHECK((oss.ss_flags & SS_DISABLE) != 0);91ATF_CHECK((oss.ss_flags & SS_ONSTACK) == 0);9293struct sigaction sa = {94.sa_sigaction = sig_handler,95.sa_flags = SA_ONSTACK | SA_SIGINFO,96};97ATF_REQUIRE(sigemptyset(&sa.sa_mask) == 0);98ATF_REQUIRE(sigaction(SIGUSR1, &sa, NULL) == 0);99ATF_REQUIRE(sigaction(SIGUSR2, &sa, NULL) == 0);100ATF_REQUIRE(raise(SIGUSR1) == 0);101102ATF_CHECK(level1_correct != -1);103ATF_CHECK(level1_correct == 1);104ATF_CHECK(level2_correct != -1);105ATF_CHECK(level2_correct == 1);106}107108ATF_TP_ADD_TCS(tp)109{110111ATF_TP_ADD_TC(tp, ss_onstack);112113return (atf_no_error());114}115116117