1*1f20091dSchristos /* $NetBSD: t_sigaltstack.c,v 1.2 2020/05/01 21:35:30 christos Exp $ */
21da5a7c8Sryo
31da5a7c8Sryo /*-
41da5a7c8Sryo * Copyright (c) 2020 The NetBSD Foundation, Inc.
51da5a7c8Sryo * All rights reserved.
61da5a7c8Sryo *
71da5a7c8Sryo * Redistribution and use in source and binary forms, with or without
81da5a7c8Sryo * modification, are permitted provided that the following conditions
91da5a7c8Sryo * are met:
101da5a7c8Sryo * 1. Redistributions of source code must retain the above copyright
111da5a7c8Sryo * notice, this list of conditions and the following disclaimer.
121da5a7c8Sryo * 2. Redistributions in binary form must reproduce the above copyright
131da5a7c8Sryo * notice, this list of conditions and the following disclaimer in the
141da5a7c8Sryo * documentation and/or other materials provided with the distribution.
151da5a7c8Sryo *
161da5a7c8Sryo * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
171da5a7c8Sryo * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
181da5a7c8Sryo * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
191da5a7c8Sryo * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
201da5a7c8Sryo * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
211da5a7c8Sryo * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
221da5a7c8Sryo * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
231da5a7c8Sryo * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
241da5a7c8Sryo * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
251da5a7c8Sryo * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
261da5a7c8Sryo * POSSIBILITY OF SUCH DAMAGE.
271da5a7c8Sryo */
281da5a7c8Sryo
291da5a7c8Sryo #include <sys/cdefs.h>
30*1f20091dSchristos __RCSID("$NetBSD: t_sigaltstack.c,v 1.2 2020/05/01 21:35:30 christos Exp $");
311da5a7c8Sryo
321da5a7c8Sryo #include <signal.h>
331da5a7c8Sryo #include <stdbool.h>
341da5a7c8Sryo
351da5a7c8Sryo #include <atf-c.h>
361da5a7c8Sryo
371da5a7c8Sryo #include "h_macros.h"
381da5a7c8Sryo
391da5a7c8Sryo static stack_t sigstk;
401da5a7c8Sryo static bool handler_called;
411da5a7c8Sryo static bool handler_use_altstack;
421da5a7c8Sryo
431da5a7c8Sryo static void
handler(int signo __unused)441da5a7c8Sryo handler(int signo __unused)
451da5a7c8Sryo {
46*1f20091dSchristos char sp[128];
471da5a7c8Sryo
481da5a7c8Sryo handler_called = true;
491da5a7c8Sryo
501da5a7c8Sryo /* checking if the stack pointer is within the range of altstack */
511da5a7c8Sryo if ((char *)sigstk.ss_sp <= sp &&
521da5a7c8Sryo ((char *)sigstk.ss_sp + sigstk.ss_size) > sp)
531da5a7c8Sryo handler_use_altstack = true;
541da5a7c8Sryo else
551da5a7c8Sryo handler_use_altstack = false;
561da5a7c8Sryo }
571da5a7c8Sryo
581da5a7c8Sryo ATF_TC(sigaltstack_onstack);
ATF_TC_HEAD(sigaltstack_onstack,tc)591da5a7c8Sryo ATF_TC_HEAD(sigaltstack_onstack, tc)
601da5a7c8Sryo {
611da5a7c8Sryo atf_tc_set_md_var(tc, "descr",
621da5a7c8Sryo "Checks for using signal stack with SA_ONSTACK");
631da5a7c8Sryo }
641da5a7c8Sryo
ATF_TC_BODY(sigaltstack_onstack,tc)651da5a7c8Sryo ATF_TC_BODY(sigaltstack_onstack, tc)
661da5a7c8Sryo {
671da5a7c8Sryo struct sigaction sa;
681da5a7c8Sryo int i;
691da5a7c8Sryo
701da5a7c8Sryo /* set a signal handler use alternative stack */
711da5a7c8Sryo memset(&sigstk, 0, sizeof(sigstk));
721da5a7c8Sryo sigstk.ss_sp = malloc(SIGSTKSZ);
731da5a7c8Sryo ATF_REQUIRE(sigstk.ss_sp != NULL);
741da5a7c8Sryo sigstk.ss_size = SIGSTKSZ;
751da5a7c8Sryo sigstk.ss_flags = 0;
761da5a7c8Sryo ATF_REQUIRE(sigaltstack(&sigstk, 0) == 0);
771da5a7c8Sryo
781da5a7c8Sryo sigemptyset(&sa.sa_mask);
791da5a7c8Sryo sa.sa_handler = handler;
801da5a7c8Sryo sa.sa_flags = SA_ONSTACK;
811da5a7c8Sryo sigaction(SIGUSR1, &sa, NULL);
821da5a7c8Sryo
831da5a7c8Sryo /* test several times */
841da5a7c8Sryo for (i = 1; i <= 5; i++) {
851da5a7c8Sryo handler_called = false;
861da5a7c8Sryo kill(getpid(), SIGUSR1);
871da5a7c8Sryo
881da5a7c8Sryo if (!handler_called)
891da5a7c8Sryo atf_tc_fail("signal handler wasn't called (count=%d)", i);
901da5a7c8Sryo if (!handler_use_altstack)
911da5a7c8Sryo atf_tc_fail("alternative stack wasn't used (count=%d)", i);
921da5a7c8Sryo }
931da5a7c8Sryo }
941da5a7c8Sryo
ATF_TP_ADD_TCS(tp)951da5a7c8Sryo ATF_TP_ADD_TCS(tp)
961da5a7c8Sryo {
971da5a7c8Sryo ATF_TP_ADD_TC(tp, sigaltstack_onstack);
981da5a7c8Sryo
991da5a7c8Sryo return atf_no_error();
1001da5a7c8Sryo }
101