xref: /netbsd-src/tests/lib/libc/sys/t_ptrace_core_wait.h (revision 33c9cc11cd6d0d67eab745b83fc9f58da6fa42af)
1 /*	$NetBSD: t_ptrace_core_wait.h,v 1.9 2024/06/29 07:10:27 rin 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 /*
31  * Parse the core file and find the requested note.  If the reading or parsing
32  * fails, the test is failed.  If the note is found, it is read onto buf, up to
33  * buf_len.  The actual length of the note is returned (which can be greater
34  * than buf_len, indicating that it has been truncated).  If the note is not
35  * found, -1 is returned.
36  *
37  * If the note_name ends in '*', then we find the first note that matches
38  * the note_name prefix up to the '*' character, e.g.:
39  *
40  *	NetBSD-CORE@*
41  *
42  * finds the first note whose name prefix matches "NetBSD-CORE@".
43  */
core_find_note(const char * core_path,const char * note_name,uint64_t note_type,void * buf,size_t buf_len)44 static ssize_t core_find_note(const char *core_path,
45     const char *note_name, uint64_t note_type, void *buf, size_t buf_len)
46 {
47 	int core_fd;
48 	Elf *core_elf;
49 	size_t core_numhdr, i;
50 	ssize_t ret = -1;
51 	size_t name_len = strlen(note_name);
52 	bool prefix_match = false;
53 
54 	if (note_name[name_len - 1] == '*') {
55 		prefix_match = true;
56 		name_len--;
57 	} else {
58 		/* note: we assume note name will be null-terminated */
59 		name_len++;
60 	}
61 
62 	SYSCALL_REQUIRE((core_fd = open(core_path, O_RDONLY)) != -1);
63 	SYSCALL_REQUIRE(elf_version(EV_CURRENT) != EV_NONE);
64 	SYSCALL_REQUIRE((core_elf = elf_begin(core_fd, ELF_C_READ, NULL)));
65 
66 	SYSCALL_REQUIRE(elf_getphnum(core_elf, &core_numhdr) != 0);
67 	for (i = 0; i < core_numhdr && ret == -1; i++) {
68 		GElf_Phdr core_hdr;
69 		size_t offset;
70 		SYSCALL_REQUIRE(gelf_getphdr(core_elf, i, &core_hdr));
71 		if (core_hdr.p_type != PT_NOTE)
72 		    continue;
73 
74 		for (offset = core_hdr.p_offset;
75 		    offset < core_hdr.p_offset + core_hdr.p_filesz;) {
76 			Elf64_Nhdr note_hdr;
77 			char name_buf[64];
78 
79 			switch (gelf_getclass(core_elf)) {
80 			case ELFCLASS64:
81 				SYSCALL_REQUIRE(pread(core_fd, &note_hdr,
82 				    sizeof(note_hdr), offset)
83 				    == sizeof(note_hdr));
84 				offset += sizeof(note_hdr);
85 				break;
86 			case ELFCLASS32:
87 				{
88 				Elf32_Nhdr tmp_hdr;
89 				SYSCALL_REQUIRE(pread(core_fd, &tmp_hdr,
90 				    sizeof(tmp_hdr), offset)
91 				    == sizeof(tmp_hdr));
92 				offset += sizeof(tmp_hdr);
93 				note_hdr.n_namesz = tmp_hdr.n_namesz;
94 				note_hdr.n_descsz = tmp_hdr.n_descsz;
95 				note_hdr.n_type = tmp_hdr.n_type;
96 				}
97 				break;
98 			}
99 
100 			/* indicates end of notes */
101 			if (note_hdr.n_namesz == 0 || note_hdr.n_descsz == 0)
102 				break;
103 			if (((prefix_match &&
104 			      note_hdr.n_namesz > name_len) ||
105 			     (!prefix_match &&
106 			      note_hdr.n_namesz == name_len)) &&
107 			    note_hdr.n_namesz <= sizeof(name_buf)) {
108 				SYSCALL_REQUIRE(pread(core_fd, name_buf,
109 				    note_hdr.n_namesz, offset)
110 				    == (ssize_t)(size_t)note_hdr.n_namesz);
111 
112 				if (!strncmp(note_name, name_buf, name_len) &&
113 				    note_hdr.n_type == note_type)
114 					ret = note_hdr.n_descsz;
115 			}
116 
117 			offset += note_hdr.n_namesz;
118 			/* fix to alignment */
119 			offset = roundup(offset, core_hdr.p_align);
120 
121 			/* if name & type matched above */
122 			if (ret != -1) {
123 				ssize_t read_len = MIN(buf_len,
124 				    note_hdr.n_descsz);
125 				SYSCALL_REQUIRE(pread(core_fd, buf,
126 				    read_len, offset) == read_len);
127 				break;
128 			}
129 
130 			offset += note_hdr.n_descsz;
131 			/* fix to alignment */
132 			offset = roundup(offset, core_hdr.p_align);
133 		}
134 	}
135 
136 	elf_end(core_elf);
137 	close(core_fd);
138 
139 	return ret;
140 }
141 
142 ATF_TC(core_dump_procinfo);
ATF_TC_HEAD(core_dump_procinfo,tc)143 ATF_TC_HEAD(core_dump_procinfo, tc)
144 {
145 	atf_tc_set_md_var(tc, "descr",
146 		"Trigger a core dump and verify its contents.");
147 }
148 
ATF_TC_BODY(core_dump_procinfo,tc)149 ATF_TC_BODY(core_dump_procinfo, tc)
150 {
151 	const int exitval = 5;
152 	pid_t child, wpid;
153 #if defined(TWAIT_HAVE_STATUS)
154 	const int sigval = SIGTRAP;
155 	int status;
156 #endif
157 	char core_path[] = "/tmp/core.XXXXXX";
158 	int core_fd;
159 	struct netbsd_elfcore_procinfo procinfo;
160 
161 	DPRINTF("Before forking process PID=%d\n", getpid());
162 	SYSCALL_REQUIRE((child = fork()) != -1);
163 	if (child == 0) {
164 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
165 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
166 
167 		DPRINTF("Before triggering SIGTRAP\n");
168 		trigger_trap();
169 
170 		DPRINTF("Before exiting of the child process\n");
171 		_exit(exitval);
172 	}
173 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
174 
175 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
176 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
177 
178 	validate_status_stopped(status, sigval);
179 
180 	SYSCALL_REQUIRE((core_fd = mkstemp(core_path)) != -1);
181 	close(core_fd);
182 
183 	DPRINTF("Call DUMPCORE for the child process\n");
184 	SYSCALL_REQUIRE(ptrace(PT_DUMPCORE, child, core_path, strlen(core_path))
185 	    != -1);
186 
187 	DPRINTF("Read core file\n");
188 	ATF_REQUIRE_EQ(core_find_note(core_path, "NetBSD-CORE",
189 	    ELF_NOTE_NETBSD_CORE_PROCINFO, &procinfo, sizeof(procinfo)),
190 	    sizeof(procinfo));
191 
192 	ATF_CHECK_EQ(procinfo.cpi_version, 1);
193 	ATF_CHECK_EQ(procinfo.cpi_cpisize, sizeof(procinfo));
194 	ATF_CHECK_EQ(procinfo.cpi_signo, SIGTRAP);
195 	ATF_CHECK_EQ(procinfo.cpi_pid, child);
196 	ATF_CHECK_EQ(procinfo.cpi_ppid, getpid());
197 	ATF_CHECK_EQ(procinfo.cpi_pgrp, getpgid(child));
198 	ATF_CHECK_EQ(procinfo.cpi_sid, getsid(child));
199 	ATF_CHECK_EQ(procinfo.cpi_ruid, getuid());
200 	ATF_CHECK_EQ(procinfo.cpi_euid, geteuid());
201 	ATF_CHECK_EQ(procinfo.cpi_rgid, getgid());
202 	ATF_CHECK_EQ(procinfo.cpi_egid, getegid());
203 	ATF_CHECK_EQ(procinfo.cpi_nlwps, 1);
204 	ATF_CHECK(procinfo.cpi_siglwp > 0);
205 
206 	unlink(core_path);
207 
208 	DPRINTF("Before resuming the child process where it left off and "
209 	    "without signal to be sent\n");
210 
211 #if defined(__aarch64__) || defined(__arm__) || defined(__hppa__) || \
212     defined(__powerpc__) || defined(__riscv__) || defined(__sh3__) || \
213     defined(sparc) || defined(__vax__)
214 	/*
215 	 * For these archs, program counter is not automatically incremented
216 	 * by a trap instruction. We cannot increment PC in the trap handler,
217 	 * which breaks applications depending on this behavior, e.g., GDB.
218 	 * Therefore, we need to pass PC++ instead of (void *)1 (== PC) to
219 	 * PT_CONTINUE here.
220 	 */
221 	struct reg r;
222 
223 	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
224 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child,
225 	    (void *)(PTRACE_REG_PC(&r) + PTRACE_BREAKPOINT_SIZE), 0) != -1);
226 #else
227 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
228 #endif
229 
230 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
231 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
232 
233 	validate_status_exited(status, exitval);
234 
235 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
236 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
237 }
238 
239 #define ATF_TP_ADD_TCS_PTRACE_WAIT_CORE() \
240 	ATF_TP_ADD_TC(tp, core_dump_procinfo);
241