Module Name: src Committed By: christos Date: Thu Dec 7 19:46:40 UTC 2017
Modified Files: src/tests/kernel: Makefile Added Files: src/tests/kernel: h_segv.c t_trapsignal.sh Log Message: Add trapsignal tests that make sure that traps don't end up spinning indefinitely, discussed in tech-kern. To generate a diff of this commit: cvs rdiff -u -r1.47 -r1.48 src/tests/kernel/Makefile cvs rdiff -u -r0 -r1.1 src/tests/kernel/h_segv.c \ src/tests/kernel/t_trapsignal.sh Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/tests/kernel/Makefile diff -u src/tests/kernel/Makefile:1.47 src/tests/kernel/Makefile:1.48 --- src/tests/kernel/Makefile:1.47 Wed Dec 6 08:54:26 2017 +++ src/tests/kernel/Makefile Thu Dec 7 14:46:40 2017 @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.47 2017/12/06 13:54:26 christos Exp $ +# $NetBSD: Makefile,v 1.48 2017/12/07 19:46:40 christos Exp $ NOMAN= # defined @@ -19,11 +19,13 @@ TESTS_C+= t_sysctl TESTS_SH= t_umount TESTS_SH+= t_umountstress TESTS_SH+= t_ps_strings +TESTS_SH+= t_trapsignal TESTS_SH+= t_interp BINDIR= ${TESTSDIR} PROGS= h_ps_strings1 PROGS+= h_ps_strings2 +PROGS+= h_segv LDADD.t_mqueue+= -lrt Added files: Index: src/tests/kernel/h_segv.c diff -u /dev/null src/tests/kernel/h_segv.c:1.1 --- /dev/null Thu Dec 7 14:46:40 2017 +++ src/tests/kernel/h_segv.c Thu Dec 7 14:46:40 2017 @@ -0,0 +1,117 @@ +/* $NetBSD: h_segv.c,v 1.1 2017/12/07 19:46:40 christos Exp $ */ + +/*- + * Copyright (c) 2017 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include <sys/cdefs.h> +__RCSID("$NetBSD: h_segv.c,v 1.1 2017/12/07 19:46:40 christos Exp $"); + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <unistd.h> +#include <signal.h> +#include <err.h> + +// A faulting address +static int *p = (int *) 0xfefefef0; + +static int flags; +#define F_RECURSE 1 +#define F_HANDLE 2 +#define F_MASK 4 + +static struct { + const char *n; + int v; +} nv[] = { + { "recurse", F_RECURSE }, + { "handle", F_HANDLE }, + { "mask", F_MASK }, +}; + +static void +foo(int s) +{ + char buf[64]; + int i = snprintf(buf, sizeof(buf), "got %d\n", s); + write(2, buf, i); + if (flags & F_RECURSE) + *p = 0; + exit(EXIT_SUCCESS); +} + +static __dead void +usage(void) +{ + fprintf(stderr, "Usage: %s recurse|mask|unhandle ...\n", getprogname()); + exit(EXIT_FAILURE); +} + +int +main(int argc, char *argv[]) +{ + if (argc == 1) + usage(); + + for (int i = 1; i < argc; i++) { + size_t j; + for (j = 0; j < __arraycount(nv); j++) + if (strcmp(nv[j].n, argv[i]) == 0) { + flags |= nv[j].v; + break; + } + if (j == __arraycount(nv)) + usage(); + } + + if (flags == 0) + usage(); + + if (flags & F_HANDLE) { + struct sigaction sa; + + sa.sa_flags = SA_RESTART; + sa.sa_handler = foo; + sigemptyset(&sa.sa_mask); + if (sigaction(SIGSEGV, &sa, NULL) == -1) + err(EXIT_FAILURE, "sigaction"); + } + + if (flags & F_MASK) { + sigset_t set; + + sigemptyset(&set); + sigaddset(&set, SIGSEGV); + if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) + err(EXIT_FAILURE, "sigprocmask"); + } + + *p = 1; + return EXIT_SUCCESS; +} Index: src/tests/kernel/t_trapsignal.sh diff -u /dev/null src/tests/kernel/t_trapsignal.sh:1.1 --- /dev/null Thu Dec 7 14:46:40 2017 +++ src/tests/kernel/t_trapsignal.sh Thu Dec 7 14:46:40 2017 @@ -0,0 +1,94 @@ +# $NetBSD: t_trapsignal.sh,v 1.1 2017/12/07 19:46:40 christos Exp $ +# +# Copyright (c) 2017 The NetBSD Foundation, Inc. +# All rights reserved. +# +# This code is derived from software contributed to The NetBSD Foundation +# by Christos Zoulas. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS +# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS +# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# + +HELPER=$(atf_get_srcdir)/h_segv +atf_test_case trap_simple +trap_simple() +{ + atf_set "descr" "Test unhandled traps exit with the right exit code" +} +trap_simple_body() +{ + atf_check -s signal:11 -o "inline:" -e "inline:" \ + ${HELPER} recurse +} + +atf_test_case trap_handle +trap_handle() +{ + atf_set "descr" "Test handled traps call the signal handler" +} +trap_handle_body() +{ + atf_check -s exit:0 -o "inline:" -e "inline:got 11\n" \ + ${HELPER} handle +} + +atf_test_case trap_mask +trap_mask() +{ + atf_set "descr" "Test that masking the trapped signal get reset" +} +trap_mask_body() +{ + atf_check -s signal:11 -o "inline:" -e "inline:" \ + ${HELPER} mask +} + +atf_test_case trap_handle_mask +trap_handle_mask() +{ + atf_set "descr" "Test handled and masked traps get reset" +} +trap_handle_mask_body() +{ + atf_check -s signal:11 -o "inline:" -e "inline:" \ + ${HELPER} mask handle +} + +atf_test_case trap_handle_recurse +trap_handle_recurse() +{ + atf_set "descr" "Test that receiving the trap in the handler resets" +} + +trap_handle_recurse_body() +{ + atf_check -s signal:11 -o "inline:" -e "inline:got 11\n" \ + ${HELPER} handle recurse +} + +atf_init_test_cases() +{ + atf_add_test_case trap_simple + atf_add_test_case trap_handle + atf_add_test_case trap_mask + atf_add_test_case trap_handle_recurse +}