summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/x86/sigtrap_loop.c
blob: 9d065479e89f94f745a7835381c44ab1968eed1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (C) 2025 Intel Corporation
 */
#define _GNU_SOURCE

#include <err.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ucontext.h>

#ifdef __x86_64__
# define REG_IP REG_RIP
#else
# define REG_IP REG_EIP
#endif

static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *), int flags)
{
	struct sigaction sa;

	memset(&sa, 0, sizeof(sa));
	sa.sa_sigaction = handler;
	sa.sa_flags = SA_SIGINFO | flags;
	sigemptyset(&sa.sa_mask);

	if (sigaction(sig, &sa, 0))
		err(1, "sigaction");

	return;
}

static void sigtrap(int sig, siginfo_t *info, void *ctx_void)
{
	ucontext_t *ctx = (ucontext_t *)ctx_void;
	static unsigned int loop_count_on_same_ip;
	static unsigned long last_trap_ip;

	if (last_trap_ip == ctx->uc_mcontext.gregs[REG_IP]) {
		printf("\tTrapped at %016lx\n", last_trap_ip);

		/*
		 * If the same IP is hit more than 10 times in a row, it is
		 * _considered_ an infinite loop.
		 */
		if (++loop_count_on_same_ip > 10) {
			printf("[FAIL]\tDetected SIGTRAP infinite loop\n");
			exit(1);
		}

		return;
	}

	loop_count_on_same_ip = 0;
	last_trap_ip = ctx->uc_mcontext.gregs[REG_IP];
	printf("\tTrapped at %016lx\n", last_trap_ip);
}

int main(int argc, char *argv[])
{
	sethandler(SIGTRAP, sigtrap, 0);

	/*
	 * Set the Trap Flag (TF) to single-step the test code, therefore to
	 * trigger a SIGTRAP signal after each instruction until the TF is
	 * cleared.
	 *
	 * Because the arithmetic flags are not significant here, the TF is
	 * set by pushing 0x302 onto the stack and then popping it into the
	 * flags register.
	 *
	 * Four instructions in the following asm code are executed with the
	 * TF set, thus the SIGTRAP handler is expected to run four times.
	 */
	printf("[RUN]\tSIGTRAP infinite loop detection\n");
	asm volatile(
#ifdef __x86_64__
		/*
		 * Avoid clobbering the redzone
		 *
		 * Equivalent to "sub $128, %rsp", however -128 can be encoded
		 * in a single byte immediate while 128 uses 4 bytes.
		 */
		"add $-128, %rsp\n\t"
#endif
		"push $0x302\n\t"
		"popf\n\t"
		"nop\n\t"
		"nop\n\t"
		"push $0x202\n\t"
		"popf\n\t"
#ifdef __x86_64__
		"sub $-128, %rsp\n\t"
#endif
	);

	printf("[OK]\tNo SIGTRAP infinite loop detected\n");
	return 0;
}