xref: /netbsd-src/tests/kernel/t_trapsignal.sh (revision 9e739a844928855416a1b8aa3655d1f5c7257fc5)
1# $NetBSD: t_trapsignal.sh,v 1.6 2023/05/05 01:27:18 gutteridge Exp $
2#
3# Copyright (c) 2017 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# This code is derived from software contributed to The NetBSD Foundation
7# by Christos Zoulas.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29#
30
31HELPER=$(atf_get_srcdir)/h_segv
32
33# SIGSEGV
34
35atf_test_case segv_simple
36segv_simple_head()
37{
38	atf_set "descr" "Test unhandled SIGSEGV with the right exit code"
39}
40segv_simple_body()
41{
42	atf_check -s signal:11 -o "inline:" -e "inline:" \
43		${HELPER} segv recurse
44}
45
46atf_test_case segv_handle
47segv_handle_head()
48{
49	atf_set "descr" "Test handled SIGSEGV traps call the signal handler"
50}
51segv_handle_body()
52{
53	atf_check -s exit:0 -o "inline:" -e "inline:got 11\n" \
54		${HELPER} segv handle
55}
56
57atf_test_case segv_mask
58segv_mask_head()
59{
60	atf_set "descr" "Test that masking SIGSEGV get reset"
61}
62segv_mask_body()
63{
64	atf_check -s signal:11 -o "inline:" -e "inline:" \
65		${HELPER} segv mask
66}
67
68atf_test_case segv_handle_mask
69segv_handle_mask_head()
70{
71	atf_set "descr" "Test handled and masked SIGSEGV traps get reset"
72}
73segv_handle_mask_body()
74{
75	atf_check -s signal:11 -o "inline:" -e "inline:" \
76		${HELPER} segv mask handle
77}
78
79atf_test_case segv_handle_recurse
80segv_handle_recurse_head()
81{
82	atf_set "descr" "Test that receiving SIGSEGV in the handler resets"
83}
84
85segv_handle_recurse_body()
86{
87	atf_check -s signal:11 -o "inline:" -e "inline:got 11\n" \
88		${HELPER} segv handle recurse
89}
90
91atf_test_case segv_ignore
92segv_ignore_head()
93{
94	atf_set "descr" "Test ignored SIGSEGV trap with right exit code"
95}
96
97segv_ignore_body()
98{
99	atf_check -s signal:11 -o "inline:" -e "inline:" \
100		${HELPER} segv ignore
101}
102
103# SIGTRAP
104
105atf_test_case trap_simple
106trap_simple_head()
107{
108	atf_set "descr" "Test unhandled SIGTRAP with the right exit code"
109}
110trap_simple_body()
111{
112	atf_check -s signal:5 -o "inline:" -e "inline:" \
113		${HELPER} trap recurse
114}
115
116atf_test_case trap_handle
117trap_handle_head()
118{
119	atf_set "descr" "Test handled SIGTRAP traps call the signal handler"
120}
121trap_handle_body()
122{
123	atf_check -s exit:0 -o "inline:" -e "inline:got 5\n" \
124		${HELPER} trap handle
125}
126
127atf_test_case trap_mask
128trap_mask_head()
129{
130	atf_set "descr" "Test that masking the trapped SIGTRAP signal get reset"
131}
132trap_mask_body()
133{
134	atf_check -s signal:5 -o "inline:" -e "inline:" \
135		${HELPER} trap mask
136}
137
138atf_test_case trap_handle_mask
139trap_handle_mask_head()
140{
141	atf_set "descr" "Test handled and masked SIGTRAP traps get reset"
142}
143trap_handle_mask_body()
144{
145	atf_check -s signal:5 -o "inline:" -e "inline:" \
146		${HELPER} trap mask handle
147}
148
149atf_test_case trap_handle_recurse
150trap_handle_recurse_head()
151{
152	atf_set "descr" "Test that receiving SIGTRAP in the handler resets"
153}
154
155trap_handle_recurse_body()
156{
157	atf_check -s signal:5 -o "inline:" -e "inline:got 5\n" \
158		${HELPER} trap handle recurse
159}
160
161atf_test_case trap_ignore
162trap_ignore_head()
163{
164	atf_set "descr" "Test ignored trap with right exit code"
165}
166
167trap_ignore_body()
168{
169	atf_check -s signal:5 -o "inline:" -e "inline:" \
170		${HELPER} trap ignore
171}
172
173# SIGFPE
174
175fpe_available()
176{
177	if ${HELPER} fpe check > msg.$$
178	then
179		rm -f msg.$$
180	else
181		msg=$( cat msg.$$ )
182		rm -f msg.$$
183		atf_skip "$msg"
184	fi
185}
186
187atf_test_case fpe_simple
188fpe_simple_head()
189{
190	atf_set "descr" "Test unhandled SIGFPE with the right exit code"
191}
192fpe_simple_body()
193{
194	fpe_available
195	atf_check -s signal:8 -o "inline:" -e "inline:" \
196		${HELPER} fpe recurse
197}
198
199atf_test_case fpe_handle
200fpe_handle_head()
201{
202	atf_set "descr" "Test handled SIGFPE traps call the signal handler"
203}
204fpe_handle_body()
205{
206	fpe_available
207	atf_check -s exit:0 -o "inline:" -e "inline:got 8\n" \
208		${HELPER} fpe handle
209}
210
211atf_test_case fpe_mask
212fpe_mask_head()
213{
214	atf_set "descr" "Test that masking the trapped SIGFPE signal get reset"
215}
216fpe_mask_body()
217{
218	fpe_available
219	atf_check -s signal:8 -o "inline:" -e "inline:" \
220		${HELPER} fpe mask
221}
222
223atf_test_case fpe_handle_mask
224fpe_handle_mask_head()
225{
226	atf_set "descr" "Test handled and masked SIGFPE traps get reset"
227}
228fpe_handle_mask_body()
229{
230	fpe_available
231	atf_check -s signal:8 -o "inline:" -e "inline:" \
232		${HELPER} fpe mask handle
233}
234
235atf_test_case fpe_handle_recurse
236fpe_handle_recurse_head()
237{
238	atf_set "descr" "Test that receiving SIGFPE in the handler resets"
239}
240
241fpe_handle_recurse_body()
242{
243	fpe_available
244	atf_check -s signal:8 -o "inline:" -e "inline:got 8\n" \
245		${HELPER} fpe handle recurse
246}
247
248atf_test_case fpe_ignore
249fpe_ignore_head()
250{
251	atf_set "descr" "Test ignored trap with right exit code"
252}
253
254fpe_ignore_body()
255{
256	fpe_available
257	atf_check -s signal:8 -o "inline:" -e "inline:" \
258		${HELPER} fpe ignore
259}
260
261# SIGBUS
262
263atf_test_case bus_simple
264bus_simple_head()
265{
266	atf_set "descr" "Test unhandled SIGBUS with the right exit code"
267}
268bus_simple_body()
269{
270	atf_check -s signal:10 -o "inline:" -e "inline:" \
271		${HELPER} bus recurse
272}
273
274atf_test_case bus_handle
275bus_handle_head()
276{
277	atf_set "descr" "Test handled SIGBUS traps call the signal handler"
278}
279bus_handle_body()
280{
281	atf_check -s exit:0 -o "inline:" -e "inline:got 10\n" \
282		${HELPER} bus handle
283}
284
285atf_test_case bus_mask
286bus_mask_head()
287{
288	atf_set "descr" "Test that masking the trapped SIGBUS signal get reset"
289}
290bus_mask_body()
291{
292	atf_check -s signal:10 -o "inline:" -e "inline:" \
293		${HELPER} bus mask
294}
295
296atf_test_case bus_handle_mask
297bus_handle_mask_head()
298{
299	atf_set "descr" "Test handled and masked SIGBUS traps get reset"
300}
301bus_handle_mask_body()
302{
303	atf_check -s signal:10 -o "inline:" -e "inline:" \
304		${HELPER} bus mask handle
305}
306
307atf_test_case bus_handle_recurse
308bus_handle_recurse_head()
309{
310	atf_set "descr" "Test that receiving SIGBUS in the handler resets"
311}
312
313bus_handle_recurse_body()
314{
315	atf_check -s signal:10 -o "inline:" -e "inline:got 10\n" \
316		${HELPER} bus handle recurse
317}
318
319atf_test_case bus_ignore
320bus_ignore_head()
321{
322	atf_set "descr" "Test ignored trap with right exit code"
323}
324
325bus_ignore_body()
326{
327	atf_check -s signal:10 -o "inline:" -e "inline:" \
328		${HELPER} bus ignore
329}
330
331atf_init_test_cases()
332{
333	atf_add_test_case segv_simple
334	atf_add_test_case segv_handle
335	atf_add_test_case segv_mask
336	atf_add_test_case segv_handle_recurse
337	atf_add_test_case segv_ignore
338
339	atf_add_test_case trap_simple
340	atf_add_test_case trap_handle
341	atf_add_test_case trap_mask
342	atf_add_test_case trap_handle_recurse
343	atf_add_test_case trap_ignore
344
345#	atf_add_test_case ill_simple
346#	atf_add_test_case ill_handle
347#	atf_add_test_case ill_mask
348#	atf_add_test_case ill_handle_recurse
349#	atf_add_test_case ill_ignore
350
351	atf_add_test_case fpe_simple
352	atf_add_test_case fpe_handle
353	atf_add_test_case fpe_mask
354	atf_add_test_case fpe_handle_recurse
355	atf_add_test_case fpe_ignore
356
357	atf_add_test_case bus_simple
358	atf_add_test_case bus_handle
359	atf_add_test_case bus_mask
360	atf_add_test_case bus_handle_recurse
361	atf_add_test_case bus_ignore
362}
363