h_segv.c revision 1.12
11.12Schristos/*	$NetBSD: h_segv.c,v 1.12 2019/01/27 16:29:56 christos Exp $	*/
21.1Schristos
31.1Schristos/*-
41.1Schristos * Copyright (c) 2017 The NetBSD Foundation, Inc.
51.1Schristos * All rights reserved.
61.1Schristos *
71.1Schristos * This code is derived from software contributed to The NetBSD Foundation
81.1Schristos * by Christos Zoulas.
91.1Schristos *
101.1Schristos * Redistribution and use in source and binary forms, with or without
111.1Schristos * modification, are permitted provided that the following conditions
121.1Schristos * are met:
131.1Schristos * 1. Redistributions of source code must retain the above copyright
141.1Schristos *    notice, this list of conditions and the following disclaimer.
151.1Schristos * 2. Redistributions in binary form must reproduce the above copyright
161.1Schristos *    notice, this list of conditions and the following disclaimer in the
171.1Schristos *    documentation and/or other materials provided with the distribution.
181.1Schristos *
191.1Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Schristos * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Schristos * POSSIBILITY OF SUCH DAMAGE.
301.1Schristos */
311.1Schristos#include <sys/cdefs.h>
321.12Schristos__RCSID("$NetBSD: h_segv.c,v 1.12 2019/01/27 16:29:56 christos Exp $");
331.10Sriastrad
341.10Sriastrad#define	__TEST_FENV
351.1Schristos
361.4Skamil#include <sys/types.h>
371.5Skamil#include <sys/mman.h>
381.4Skamil#include <sys/ptrace.h>
391.8Sriastrad
401.8Sriastrad#include <err.h>
411.9Sriastrad#include <fenv.h>
421.12Schristos#if (__arm__ && !__SOFTFP__) || __aarch64__
431.11Smartin#include <ieeefp.h> /* only need for ARM Cortex/Neon hack */
441.12Schristos#endif
451.8Sriastrad#include <signal.h>
461.1Schristos#include <stdio.h>
471.8Sriastrad#include <stdlib.h>
481.1Schristos#include <string.h>
491.1Schristos#include <unistd.h>
501.1Schristos
511.1Schristosstatic int flags;
521.1Schristos#define F_RECURSE 	1
531.1Schristos#define F_HANDLE	2
541.1Schristos#define F_MASK		4
551.3Skamil#define F_IGNORE	8
561.11Smartin#define	F_CHECK		16
571.1Schristos
581.1Schristosstatic struct {
591.1Schristos	const char *n;
601.1Schristos	int v;
611.1Schristos} nv[] = {
621.1Schristos	{ "recurse",	F_RECURSE },
631.1Schristos	{ "handle",	F_HANDLE },
641.1Schristos	{ "mask",	F_MASK },
651.11Smartin	{ "ignore",	F_IGNORE },
661.11Smartin	{ "check",	F_CHECK }
671.1Schristos};
681.1Schristos
691.4Skamilstatic int sig;
701.4Skamilstatic struct {
711.4Skamil	const char *n;
721.4Skamil	int v;
731.4Skamil} sn[] = {
741.4Skamil	{ "segv",	SIGSEGV },
751.5Skamil	{ "trap",	SIGTRAP },
761.5Skamil	{ "ill",	SIGILL },
771.5Skamil	{ "fpe",	SIGFPE },
781.5Skamil	{ "bus",	SIGBUS }
791.4Skamil};
801.4Skamil
811.1Schristosstatic void
821.5Skamiltrigger_segv(void)
831.5Skamil{
841.5Skamil	volatile int *p = (int *)(intptr_t)atoi("0");
851.5Skamil
861.5Skamil	*p = 1;
871.5Skamil}
881.5Skamil
891.5Skamilstatic void
901.5Skamiltrigger_trap(void)
911.1Schristos{
921.5Skamil
931.4Skamil#ifdef PTRACE_BREAKPOINT_ASM
941.5Skamil	PTRACE_BREAKPOINT_ASM;
951.5Skamil#else
961.5Skamil	/* port me */
971.5Skamil#endif
981.5Skamil}
991.5Skamil
1001.5Skamilstatic void
1011.5Skamiltrigger_ill(void)
1021.5Skamil{
1031.5Skamil
1041.5Skamil#ifdef PTRACE_ILLEGAL_ASM
1051.5Skamil	PTRACE_ILLEGAL_ASM;
1061.4Skamil#else
1071.5Skamil	/* port me */
1081.5Skamil#endif
1091.5Skamil}
1101.5Skamil
1111.5Skamilstatic void
1121.11Smartincheck_fpe(void)
1131.11Smartin{
1141.11Smartin#if (__arm__ && !__SOFTFP__) || __aarch64__
1151.11Smartin	/*
1161.11Smartin	 * Some NEON fpus do not implement IEEE exception handling,
1171.11Smartin	 * skip these tests if running on them and compiled for
1181.11Smartin	 * hard float.
1191.11Smartin	 */
1201.11Smartin	if (0 == fpsetmask(fpsetmask(FP_X_INV))) {
1211.11Smartin		printf("FPU does not implement exception handling\n");
1221.11Smartin		exit(EXIT_FAILURE);
1231.11Smartin	}
1241.11Smartin#endif
1251.11Smartin	exit(EXIT_SUCCESS);
1261.11Smartin}
1271.11Smartin
1281.11Smartinstatic void
1291.5Skamiltrigger_fpe(void)
1301.5Skamil{
1311.9Sriastrad	volatile double a = getpid();
1321.9Sriastrad	volatile double b = strtol("0", NULL, 0);
1331.5Skamil
1341.10Sriastrad#ifdef __HAVE_FENV
1351.9Sriastrad	feenableexcept(FE_ALL_EXCEPT);
1361.10Sriastrad#endif
1371.9Sriastrad
1381.9Sriastrad	usleep((int)(a/b));
1391.5Skamil}
1401.5Skamil
1411.5Skamilstatic void
1421.5Skamiltrigger_bus(void)
1431.5Skamil{
1441.5Skamil	FILE *fp;
1451.5Skamil	char *p;
1461.5Skamil
1471.5Skamil	/* Open an empty file for writing. */
1481.5Skamil	fp = tmpfile();
1491.5Skamil	if (fp == NULL)
1501.5Skamil		err(EXIT_FAILURE, "tmpfile");
1511.5Skamil
1521.7Skamil	/*
1531.7Skamil	 * Map an empty file with mmap(2) to a pointer.
1541.7Skamil	 *
1551.7Skamil	 * PROT_READ handles read-modify-write sequences emitted for
1561.7Skamil	 * certain combinations of CPUs and compilers (e.g. Alpha AXP).
1571.7Skamil	 */
1581.6Skamil	p = mmap(0, 1, PROT_READ|PROT_WRITE, MAP_PRIVATE, fileno(fp), 0);
1591.5Skamil	if (p == MAP_FAILED)
1601.5Skamil		err(EXIT_FAILURE, "mmap");
1611.5Skamil
1621.5Skamil	/* Invalid memory access causes CPU trap, translated to SIGBUS */
1631.5Skamil	*p = 'a';
1641.5Skamil}
1651.5Skamil
1661.5Skamilstatic void
1671.5Skamiltrigger(void)
1681.5Skamil{
1691.5Skamil
1701.5Skamil	switch (sig) {
1711.5Skamil	case SIGSEGV:
1721.5Skamil		trigger_segv();
1731.5Skamil		break;
1741.5Skamil	case SIGTRAP:
1751.5Skamil		trigger_trap();
1761.5Skamil		break;
1771.5Skamil	case SIGILL:
1781.5Skamil		trigger_ill();
1791.5Skamil		break;
1801.5Skamil	case SIGFPE:
1811.5Skamil		trigger_fpe();
1821.5Skamil		break;
1831.5Skamil	case SIGBUS:
1841.5Skamil		trigger_bus();
1851.5Skamil		break;
1861.5Skamil	default:
1871.5Skamil		break;
1881.4Skamil	}
1891.5Skamil}
1901.5Skamil
1911.5Skamilstatic void
1921.5Skamilfoo(int s)
1931.5Skamil{
1941.5Skamil	char buf[64];
1951.5Skamil	int i = snprintf(buf, sizeof(buf), "got %d\n", s);
1961.5Skamil	write(2, buf, i);
1971.5Skamil
1981.5Skamil	if (flags & F_RECURSE)
1991.5Skamil		trigger();
2001.5Skamil
2011.5Skamil	exit(EXIT_SUCCESS);
2021.1Schristos}
2031.1Schristos
2041.1Schristosstatic __dead void
2051.1Schristosusage(void)
2061.1Schristos{
2071.4Skamil	const char *pname = getprogname();
2081.4Skamil
2091.5Skamil	fprintf(stderr, "Usage: %s segv|trap|ill|fpe|bus "
2101.11Smartin	                "[recurse|mask|handle|ignore|check] ...\n", pname);
2111.5Skamil
2121.1Schristos	exit(EXIT_FAILURE);
2131.1Schristos}
2141.1Schristos
2151.1Schristosint
2161.1Schristosmain(int argc, char *argv[])
2171.1Schristos{
2181.5Skamil
2191.1Schristos	if (argc == 1)
2201.1Schristos	    usage();
2211.1Schristos
2221.1Schristos	for (int i = 1; i < argc; i++) {
2231.1Schristos		size_t j;
2241.4Skamil		for (j = 0; j < __arraycount(nv); j++) {
2251.1Schristos			if (strcmp(nv[j].n, argv[i]) == 0) {
2261.1Schristos				flags |= nv[j].v;
2271.5Skamil				goto consumed;
2281.1Schristos			}
2291.5Skamil		}
2301.5Skamil		for (j = 0; j < __arraycount(sn); j++) {
2311.4Skamil			if (strcmp(sn[j].n, argv[i]) == 0) {
2321.4Skamil				sig = sn[j].v;
2331.5Skamil				goto consumed;
2341.4Skamil			}
2351.4Skamil		}
2361.5Skamil
2371.5Skamil		usage();
2381.5Skamil
2391.5Skamil	consumed:
2401.5Skamil		continue;
2411.1Schristos	}
2421.1Schristos
2431.4Skamil	if (flags == 0 || sig == 0)
2441.1Schristos		usage();
2451.1Schristos
2461.11Smartin	if (flags & F_CHECK && sig != SIGFPE) {
2471.11Smartin		fprintf(stderr, "can only check for fpe support\n");
2481.11Smartin		return 1;
2491.11Smartin	}
2501.11Smartin	if (flags & F_CHECK)
2511.11Smartin		check_fpe();
2521.11Smartin
2531.1Schristos	if (flags & F_HANDLE) {
2541.1Schristos		struct sigaction sa;
2551.1Schristos
2561.1Schristos		sa.sa_flags = SA_RESTART;
2571.1Schristos		sa.sa_handler = foo;
2581.1Schristos		sigemptyset(&sa.sa_mask);
2591.4Skamil		if (sigaction(sig, &sa, NULL) == -1)
2601.1Schristos			err(EXIT_FAILURE, "sigaction");
2611.1Schristos	}
2621.1Schristos
2631.1Schristos	if (flags & F_MASK) {
2641.1Schristos		sigset_t set;
2651.1Schristos
2661.1Schristos		sigemptyset(&set);
2671.4Skamil		sigaddset(&set, sig);
2681.1Schristos		if (sigprocmask(SIG_BLOCK, &set, NULL) == -1)
2691.1Schristos			err(EXIT_FAILURE, "sigprocmask");
2701.1Schristos	}
2711.1Schristos
2721.3Skamil	if (flags & F_IGNORE) {
2731.3Skamil		struct sigaction sa;
2741.3Skamil
2751.3Skamil		memset(&sa, 0, sizeof(sa));
2761.3Skamil		sa.sa_handler = SIG_IGN;
2771.3Skamil		sigemptyset(&sa.sa_mask);
2781.4Skamil		if (sigaction(sig, &sa, NULL) == -1)
2791.3Skamil			err(EXIT_FAILURE, "sigaction");
2801.3Skamil	}
2811.3Skamil
2821.5Skamil	trigger();
2831.5Skamil
2841.1Schristos	return EXIT_SUCCESS;
2851.1Schristos}
286