1*76248f07Spho /* $NetBSD: t_signal_and_sp.c,v 1.1 2024/04/22 07:24:22 pho Exp $ */
2*76248f07Spho
3*76248f07Spho /*
4*76248f07Spho * Copyright (c) 2024 The NetBSD Foundation, Inc.
5*76248f07Spho * All rights reserved.
6*76248f07Spho *
7*76248f07Spho * Redistribution and use in source and binary forms, with or without
8*76248f07Spho * modification, are permitted provided that the following conditions
9*76248f07Spho * are met:
10*76248f07Spho * 1. Redistributions of source code must retain the above copyright
11*76248f07Spho * notice, this list of conditions and the following disclaimer.
12*76248f07Spho * 2. Redistributions in binary form must reproduce the above copyright
13*76248f07Spho * notice, this list of conditions and the following disclaimer in the
14*76248f07Spho * documentation and/or other materials provided with the distribution.
15*76248f07Spho *
16*76248f07Spho * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*76248f07Spho * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*76248f07Spho * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*76248f07Spho * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*76248f07Spho * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*76248f07Spho * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*76248f07Spho * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*76248f07Spho * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*76248f07Spho * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*76248f07Spho * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*76248f07Spho * POSSIBILITY OF SUCH DAMAGE.
27*76248f07Spho */
28*76248f07Spho
29*76248f07Spho #include <atf-c.h>
30*76248f07Spho
31*76248f07Spho #if defined(HAVE_STACK_POINTER_H)
32*76248f07Spho # include <signal.h>
33*76248f07Spho # include <string.h>
34*76248f07Spho # include <sys/stdint.h>
35*76248f07Spho # include <sys/time.h>
36*76248f07Spho # include "stack_pointer.h"
37*76248f07Spho
38*76248f07Spho static volatile void* stack_pointer = NULL;
on_alarm(int sig)39*76248f07Spho static void on_alarm(int sig __attribute__((__unused__)))
40*76248f07Spho {
41*76248f07Spho /*
42*76248f07Spho * Store the stack pointer into a variable so that we can test if
43*76248f07Spho * it's aligned.
44*76248f07Spho */
45*76248f07Spho LOAD_SP(stack_pointer);
46*76248f07Spho
47*76248f07Spho /*
48*76248f07Spho * Now we are going to return from a signal
49*76248f07Spho * handler. __sigtramp_siginfo_2 will call setcontext(2) with a
50*76248f07Spho * ucontext provided by the kernel. When that fails it will call
51*76248f07Spho * _Exit(2) with the errno, and the test will fail.
52*76248f07Spho */
53*76248f07Spho }
54*76248f07Spho #endif
55*76248f07Spho
56*76248f07Spho ATF_TC(misaligned_sp_and_signal);
ATF_TC_HEAD(misaligned_sp_and_signal,tc)57*76248f07Spho ATF_TC_HEAD(misaligned_sp_and_signal, tc)
58*76248f07Spho {
59*76248f07Spho atf_tc_set_md_var(tc, "descr", "process can return from a signal"
60*76248f07Spho " handler even if the stack pointer is misaligned when a signal"
61*76248f07Spho " arrives");
62*76248f07Spho }
ATF_TC_BODY(misaligned_sp_and_signal,tc)63*76248f07Spho ATF_TC_BODY(misaligned_sp_and_signal, tc)
64*76248f07Spho {
65*76248f07Spho #if defined(HAVE_STACK_POINTER_H)
66*76248f07Spho /*
67*76248f07Spho * Set up a handler for SIGALRM.
68*76248f07Spho */
69*76248f07Spho struct sigaction sa;
70*76248f07Spho memset(&sa, 0, sizeof(sa));
71*76248f07Spho sa.sa_handler = &on_alarm;
72*76248f07Spho ATF_REQUIRE(sigaction(SIGALRM, &sa, NULL) == 0);
73*76248f07Spho
74*76248f07Spho /*
75*76248f07Spho * Set up an interval timer so that we receive SIGALRM after 50 ms.
76*76248f07Spho */
77*76248f07Spho struct itimerval itv;
78*76248f07Spho memset(&itv, 0, sizeof(itv));
79*76248f07Spho itv.it_value.tv_usec = 1000 * 50;
80*76248f07Spho ATF_REQUIRE(setitimer(ITIMER_MONOTONIC, &itv, NULL) == 0);
81*76248f07Spho
82*76248f07Spho /*
83*76248f07Spho * Now misalign the SP. Wait for the signal to arrive and see what
84*76248f07Spho * happens. This should be fine as long as we don't use it to
85*76248f07Spho * access memory.
86*76248f07Spho */
87*76248f07Spho MISALIGN_SP;
88*76248f07Spho while (stack_pointer == NULL) {
89*76248f07Spho /*
90*76248f07Spho * Make sure the compiler does not optimize this busy loop
91*76248f07Spho * away.
92*76248f07Spho */
93*76248f07Spho __asm__("" : : : "memory");
94*76248f07Spho }
95*76248f07Spho /*
96*76248f07Spho * We could successfully return from a signal handler. Now we
97*76248f07Spho * should fix the SP before calling any functions.
98*76248f07Spho */
99*76248f07Spho FIX_SP;
100*76248f07Spho
101*76248f07Spho /*
102*76248f07Spho * But was the stack pointer aligned when we were on the signal
103*76248f07Spho * handler?
104*76248f07Spho */
105*76248f07Spho ATF_CHECK_MSG(is_sp_aligned((uintptr_t)stack_pointer),
106*76248f07Spho "signal handler was called with a misaligned sp: %p",
107*76248f07Spho stack_pointer);
108*76248f07Spho #else
109*76248f07Spho atf_tc_skip("Not implemented for this platform");
110*76248f07Spho #endif
111*76248f07Spho }
112*76248f07Spho
ATF_TP_ADD_TCS(tp)113*76248f07Spho ATF_TP_ADD_TCS(tp)
114*76248f07Spho {
115*76248f07Spho ATF_TP_ADD_TC(tp, misaligned_sp_and_signal);
116*76248f07Spho return atf_no_error();
117*76248f07Spho }
118