xref: /netbsd-src/tests/lib/libc/sys/t_ptrace_eventmask_wait.h (revision bb8e4b817e29e67e2bd91ed027aa6d99e69257bf)
1 /*	$NetBSD: t_ptrace_eventmask_wait.h,v 1.1 2020/05/05 00:01:14 kamil Exp $	*/
2 
3 /*-
4  * Copyright (c) 2016, 2017, 2018, 2019, 2020 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 
30 static void
eventmask_preserved(int event)31 eventmask_preserved(int event)
32 {
33 	const int exitval = 5;
34 	const int sigval = SIGSTOP;
35 	pid_t child, wpid;
36 #if defined(TWAIT_HAVE_STATUS)
37 	int status;
38 #endif
39 	ptrace_event_t set_event, get_event;
40 	const int len = sizeof(ptrace_event_t);
41 
42 	DPRINTF("Before forking process PID=%d\n", getpid());
43 	SYSCALL_REQUIRE((child = fork()) != -1);
44 	if (child == 0) {
45 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
46 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
47 
48 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
49 		FORKEE_ASSERT(raise(sigval) == 0);
50 
51 		DPRINTF("Before exiting of the child process\n");
52 		_exit(exitval);
53 	}
54 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
55 
56 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
57 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
58 
59 	validate_status_stopped(status, sigval);
60 
61 	set_event.pe_set_event = event;
62 	SYSCALL_REQUIRE(
63 	    ptrace(PT_SET_EVENT_MASK, child, &set_event, len) != -1);
64 	SYSCALL_REQUIRE(
65 	    ptrace(PT_GET_EVENT_MASK, child, &get_event, len) != -1);
66 	DPRINTF("set_event=%#x get_event=%#x\n", set_event.pe_set_event,
67 	    get_event.pe_set_event);
68 	ATF_REQUIRE(memcmp(&set_event, &get_event, len) == 0);
69 
70 	DPRINTF("Before resuming the child process where it left off and "
71 	    "without signal to be sent\n");
72 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
73 
74 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
75 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
76 
77 	validate_status_exited(status, exitval);
78 
79 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
80 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
81 }
82 
83 #define EVENTMASK_PRESERVED(test, event)				\
84 ATF_TC(test);								\
85 ATF_TC_HEAD(test, tc)							\
86 {									\
87 	atf_tc_set_md_var(tc, "descr",					\
88 	    "Verify that eventmask " #event " is preserved");		\
89 }									\
90 									\
91 ATF_TC_BODY(test, tc)							\
92 {									\
93 									\
94 	eventmask_preserved(event);					\
95 }
96 
97 EVENTMASK_PRESERVED(eventmask_preserved_empty, 0)
98 EVENTMASK_PRESERVED(eventmask_preserved_fork, PTRACE_FORK)
99 EVENTMASK_PRESERVED(eventmask_preserved_vfork, PTRACE_VFORK)
100 EVENTMASK_PRESERVED(eventmask_preserved_vfork_done, PTRACE_VFORK_DONE)
101 EVENTMASK_PRESERVED(eventmask_preserved_lwp_create, PTRACE_LWP_CREATE)
102 EVENTMASK_PRESERVED(eventmask_preserved_lwp_exit, PTRACE_LWP_EXIT)
103 EVENTMASK_PRESERVED(eventmask_preserved_posix_spawn, PTRACE_POSIX_SPAWN)
104 
105 #define ATF_TP_ADD_TCS_PTRACE_WAIT_EVENTMASK() \
106 	ATF_TP_ADD_TC(tp, eventmask_preserved_empty); \
107 	ATF_TP_ADD_TC(tp, eventmask_preserved_fork); \
108 	ATF_TP_ADD_TC(tp, eventmask_preserved_vfork); \
109 	ATF_TP_ADD_TC(tp, eventmask_preserved_vfork_done); \
110 	ATF_TP_ADD_TC(tp, eventmask_preserved_lwp_create); \
111 	ATF_TP_ADD_TC(tp, eventmask_preserved_lwp_exit); \
112 	ATF_TP_ADD_TC(tp, eventmask_preserved_posix_spawn);
113