t_ptrace_eventmask_wait.h revision 1.2 1 /* $NetBSD: t_ptrace_eventmask_wait.h,v 1.2 2025/05/02 02:24:32 riastradh 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 static void
30 eventmask_preserved(int event)
31 {
32 const int exitval = 5;
33 const int sigval = SIGSTOP;
34 pid_t child, wpid;
35 #if defined(TWAIT_HAVE_STATUS)
36 int status;
37 #endif
38 ptrace_event_t set_event, get_event;
39 const int len = sizeof(ptrace_event_t);
40
41 DPRINTF("Before forking process PID=%d\n", getpid());
42 SYSCALL_REQUIRE((child = fork()) != -1);
43 if (child == 0) {
44 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
45 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
46
47 DPRINTF("Before raising %s from child\n", strsignal(sigval));
48 FORKEE_ASSERT(raise(sigval) == 0);
49
50 DPRINTF("Before exiting of the child process\n");
51 _exit(exitval);
52 }
53 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
54
55 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
56 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
57
58 validate_status_stopped(status, sigval);
59
60 set_event.pe_set_event = event;
61 SYSCALL_REQUIRE(
62 ptrace(PT_SET_EVENT_MASK, child, &set_event, len) != -1);
63 SYSCALL_REQUIRE(
64 ptrace(PT_GET_EVENT_MASK, child, &get_event, len) != -1);
65 DPRINTF("set_event=%#x get_event=%#x\n", set_event.pe_set_event,
66 get_event.pe_set_event);
67 ATF_REQUIRE(memcmp(&set_event, &get_event, len) == 0);
68
69 DPRINTF("Before resuming the child process where it left off and "
70 "without signal to be sent\n");
71 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
72
73 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
74 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
75
76 validate_status_exited(status, exitval);
77
78 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
79 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
80 }
81
82 #define EVENTMASK_PRESERVED(test, event) \
83 ATF_TC(test); \
84 ATF_TC_HEAD(test, tc) \
85 { \
86 atf_tc_set_md_var(tc, "descr", \
87 "Verify that eventmask " #event " is preserved"); \
88 } \
89 \
90 ATF_TC_BODY(test, tc) \
91 { \
92 \
93 eventmask_preserved(event); \
94 }
95
96 EVENTMASK_PRESERVED(eventmask_preserved_empty, 0)
97 EVENTMASK_PRESERVED(eventmask_preserved_fork, PTRACE_FORK)
98 EVENTMASK_PRESERVED(eventmask_preserved_vfork, PTRACE_VFORK)
99 EVENTMASK_PRESERVED(eventmask_preserved_vfork_done, PTRACE_VFORK_DONE)
100 EVENTMASK_PRESERVED(eventmask_preserved_lwp_create, PTRACE_LWP_CREATE)
101 EVENTMASK_PRESERVED(eventmask_preserved_lwp_exit, PTRACE_LWP_EXIT)
102 EVENTMASK_PRESERVED(eventmask_preserved_posix_spawn, PTRACE_POSIX_SPAWN)
103
104 #define ATF_TP_ADD_TCS_PTRACE_WAIT_EVENTMASK() \
105 ATF_TP_ADD_TC(tp, eventmask_preserved_empty); \
106 ATF_TP_ADD_TC(tp, eventmask_preserved_fork); \
107 ATF_TP_ADD_TC(tp, eventmask_preserved_vfork); \
108 ATF_TP_ADD_TC(tp, eventmask_preserved_vfork_done); \
109 ATF_TP_ADD_TC(tp, eventmask_preserved_lwp_create); \
110 ATF_TP_ADD_TC(tp, eventmask_preserved_lwp_exit); \
111 ATF_TP_ADD_TC(tp, eventmask_preserved_posix_spawn);
112