1/*	$NetBSD: t_ptrace_misc_wait.h,v 1.1 2020/05/05 02:06:08 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
29static void
30user_va0_disable(int operation)
31{
32	pid_t child, wpid;
33#if defined(TWAIT_HAVE_STATUS)
34	int status;
35#endif
36	const int sigval = SIGSTOP;
37	int rv;
38
39	struct ptrace_siginfo info;
40
41	if (get_user_va0_disable() == 0)
42		atf_tc_skip("vm.user_va0_disable is set to 0");
43
44	memset(&info, 0, sizeof(info));
45
46	DPRINTF("Before forking process PID=%d\n", getpid());
47	SYSCALL_REQUIRE((child = fork()) != -1);
48	if (child == 0) {
49		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
50		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
51
52		DPRINTF("Before raising %s from child\n", strsignal(sigval));
53		FORKEE_ASSERT(raise(sigval) == 0);
54
55		/* NOTREACHED */
56		FORKEE_ASSERTX(0 && "This shall not be reached");
57		__unreachable();
58	}
59	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
60
61	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
62	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
63
64	validate_status_stopped(status, sigval);
65
66	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for "
67		"child\n");
68	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info,
69		sizeof(info)) != -1);
70
71	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
72	DPRINTF("Signal properties: si_signo=%#x si_code=%#x "
73		"si_errno=%#x\n",
74		info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
75		info.psi_siginfo.si_errno);
76
77	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
78	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
79
80	DPRINTF("Before resuming the child process in PC=0x0 "
81	    "and without signal to be sent\n");
82	errno = 0;
83	rv = ptrace(operation, child, (void *)0, 0);
84	ATF_REQUIRE_EQ(errno, EINVAL);
85	ATF_REQUIRE_EQ(rv, -1);
86
87	SYSCALL_REQUIRE(ptrace(PT_KILL, child, NULL, 0) != -1);
88
89	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
90	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
91	validate_status_signaled(status, SIGKILL, 0);
92
93	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
94	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
95}
96
97#define USER_VA0_DISABLE(test, operation)				\
98ATF_TC(test);								\
99ATF_TC_HEAD(test, tc)							\
100{									\
101	atf_tc_set_md_var(tc, "descr",					\
102	    "Verify behavior of " #operation " with PC set to 0x0");	\
103}									\
104									\
105ATF_TC_BODY(test, tc)							\
106{									\
107									\
108	user_va0_disable(operation);					\
109}
110
111USER_VA0_DISABLE(user_va0_disable_pt_continue, PT_CONTINUE)
112USER_VA0_DISABLE(user_va0_disable_pt_syscall, PT_SYSCALL)
113USER_VA0_DISABLE(user_va0_disable_pt_detach, PT_DETACH)
114
115#define ATF_TP_ADD_TCS_PTRACE_WAIT_MISC() \
116	ATF_TP_ADD_TC(tp, user_va0_disable_pt_continue); \
117	ATF_TP_ADD_TC(tp, user_va0_disable_pt_syscall); \
118	ATF_TP_ADD_TC(tp, user_va0_disable_pt_detach);
119