1*c23551c9SAlan Maguire // SPDX-License-Identifier: GPL-2.0
2*c23551c9SAlan Maguire /* Copyright (c) 2021, Oracle and/or its affiliates. */
3*c23551c9SAlan Maguire 
4*c23551c9SAlan Maguire #include <test_progs.h>
5*c23551c9SAlan Maguire 
6*c23551c9SAlan Maguire /* Test that verifies exception handling is working. fork()
7*c23551c9SAlan Maguire  * triggers task_newtask tracepoint; that new task will have a
8*c23551c9SAlan Maguire  * NULL pointer task_works, and the associated task->task_works->func
9*c23551c9SAlan Maguire  * should not be NULL if task_works itself is non-NULL.
10*c23551c9SAlan Maguire  *
11*c23551c9SAlan Maguire  * So to verify exception handling we want to see a NULL task_works
12*c23551c9SAlan Maguire  * and task_works->func; if we see this we can conclude that the
13*c23551c9SAlan Maguire  * exception handler ran when we attempted to dereference task->task_works
14*c23551c9SAlan Maguire  * and zeroed the destination register.
15*c23551c9SAlan Maguire  */
16*c23551c9SAlan Maguire #include "exhandler_kern.skel.h"
17*c23551c9SAlan Maguire 
test_exhandler(void)18*c23551c9SAlan Maguire void test_exhandler(void)
19*c23551c9SAlan Maguire {
20*c23551c9SAlan Maguire 	int err = 0, duration = 0, status;
21*c23551c9SAlan Maguire 	struct exhandler_kern *skel;
22*c23551c9SAlan Maguire 	pid_t cpid;
23*c23551c9SAlan Maguire 
24*c23551c9SAlan Maguire 	skel = exhandler_kern__open_and_load();
25*c23551c9SAlan Maguire 	if (CHECK(!skel, "skel_load", "skeleton failed: %d\n", err))
26*c23551c9SAlan Maguire 		goto cleanup;
27*c23551c9SAlan Maguire 
28*c23551c9SAlan Maguire 	skel->bss->test_pid = getpid();
29*c23551c9SAlan Maguire 
30*c23551c9SAlan Maguire 	err = exhandler_kern__attach(skel);
31*c23551c9SAlan Maguire 	if (!ASSERT_OK(err, "attach"))
32*c23551c9SAlan Maguire 		goto cleanup;
33*c23551c9SAlan Maguire 	cpid = fork();
34*c23551c9SAlan Maguire 	if (!ASSERT_GT(cpid, -1, "fork failed"))
35*c23551c9SAlan Maguire 		goto cleanup;
36*c23551c9SAlan Maguire 	if (cpid == 0)
37*c23551c9SAlan Maguire 		_exit(0);
38*c23551c9SAlan Maguire 	waitpid(cpid, &status, 0);
39*c23551c9SAlan Maguire 
40*c23551c9SAlan Maguire 	ASSERT_NEQ(skel->bss->exception_triggered, 0, "verify exceptions occurred");
41*c23551c9SAlan Maguire cleanup:
42*c23551c9SAlan Maguire 	exhandler_kern__destroy(skel);
43*c23551c9SAlan Maguire }
44