xref: /freebsd/lib/libproc/tests/proc_test.c (revision 315ee00f)
1 /*-
2  * Copyright (c) 2014-2017 Mark Johnston <markj@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 
31 #include <libgen.h>
32 #include <stdio.h>
33 #include <stdint.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <atf-c.h>
37 #include <libelf.h>
38 #include <libproc.h>
39 
40 static const char *aout_object = "a.out";
41 static const char *ldelf_object = "ld-elf.so.1";
42 static const char *target_prog_file = "target_prog";
43 
44 /*
45  * Run the test program. If the sig parameter is set to true, the test program
46  * will deliver SIGUSR1 to itself during execution.
47  */
48 static struct proc_handle *
49 start_prog(const struct atf_tc *tc, bool sig)
50 {
51 	char *argv[3];
52 	struct proc_handle *phdl;
53 	int error;
54 
55 	asprintf(&argv[0], "%s/%s", atf_tc_get_config_var(tc, "srcdir"),
56 	    target_prog_file);
57 	ATF_REQUIRE(argv[0] != NULL);
58 
59 	if (sig) {
60 		argv[1] = strdup("-s");
61 		argv[2] = NULL;
62 	} else {
63 		argv[1] = NULL;
64 	}
65 
66 	error = proc_create(argv[0], argv, NULL, NULL, NULL, &phdl);
67 	ATF_REQUIRE_EQ_MSG(error, 0, "failed to run '%s'", target_prog_file);
68 	ATF_REQUIRE(phdl != NULL);
69 
70 	free(argv[0]);
71 	free(argv[1]);
72 
73 	return (phdl);
74 }
75 
76 static void
77 set_bkpt(struct proc_handle *phdl, uintptr_t addr, u_long *saved)
78 {
79 	int error;
80 
81 	error = proc_bkptset(phdl, addr, saved);
82 	ATF_REQUIRE_EQ_MSG(error, 0, "failed to set breakpoint at 0x%jx",
83 	    (uintmax_t)addr);
84 }
85 
86 static void
87 remove_bkpt(struct proc_handle *phdl, uintptr_t addr, u_long val)
88 {
89 	int error;
90 
91 	error = proc_bkptdel(phdl, addr, val);
92 	ATF_REQUIRE_EQ_MSG(error, 0,
93 	    "failed to delete breakpoint at 0x%jx", (uintmax_t)addr);
94 
95 	error = proc_regset(phdl, REG_PC, addr);
96 	ATF_REQUIRE_EQ_MSG(error, 0, "failed to reset program counter");
97 }
98 
99 /*
100  * Wait for the specified process to hit a breakpoint at the specified symbol.
101  */
102 static void
103 verify_bkpt(struct proc_handle *phdl, GElf_Sym *sym, const char *symname,
104     const char *mapname)
105 {
106 	char *name, *mapname_copy, *mapbname;
107 	GElf_Sym tsym;
108 	prmap_t *map;
109 	size_t namesz;
110 	u_long addr;
111 	int error, state;
112 
113 	state = proc_wstatus(phdl);
114 	ATF_REQUIRE_EQ_MSG(state, PS_STOP, "process has state %d", state);
115 
116 	/* Get the program counter and decrement it. */
117 	error = proc_regget(phdl, REG_PC, &addr);
118 	ATF_REQUIRE_EQ_MSG(error, 0, "failed to obtain PC for '%s'",
119 	    target_prog_file);
120 	proc_bkptregadj(&addr);
121 
122 	/*
123 	 * Make sure the PC matches the expected value obtained from the symbol
124 	 * definition we looked up earlier.
125 	 */
126 	ATF_CHECK_EQ_MSG(addr, sym->st_value,
127 	    "program counter 0x%lx doesn't match expected value 0x%jx",
128 	    addr, (uintmax_t)sym->st_value);
129 
130 	/*
131 	 * Ensure we can look up the r_debug_state symbol using its starting
132 	 * address and that the resulting symbol matches the one we found using
133 	 * a name lookup.
134 	 */
135 	namesz = strlen(symname) + 1;
136 	name = malloc(namesz);
137 	ATF_REQUIRE(name != NULL);
138 
139 	error = proc_addr2sym(phdl, addr, name, namesz, &tsym);
140 	ATF_REQUIRE_EQ_MSG(error, 0, "failed to look up symbol at 0x%lx", addr);
141 	ATF_REQUIRE_EQ(memcmp(sym, &tsym, sizeof(*sym)), 0);
142 	ATF_REQUIRE_EQ(strcmp(symname, name), 0);
143 	free(name);
144 
145 	map = proc_addr2map(phdl, addr);
146 	ATF_REQUIRE_MSG(map != NULL, "failed to look up map for address 0x%lx",
147 	    addr);
148 	mapname_copy = strdup(map->pr_mapname);
149 	mapbname = basename(mapname_copy);
150 	ATF_REQUIRE_EQ_MSG(strcmp(mapname, mapbname), 0,
151 	    "expected map name '%s' doesn't match '%s'", mapname, mapbname);
152 	free(mapname_copy);
153 }
154 
155 ATF_TC(map_alias_name2map);
156 ATF_TC_HEAD(map_alias_name2map, tc)
157 {
158 	atf_tc_set_md_var(tc, "descr",
159 	    "Callers are supposed to be able to use \"a.out\" as an alias for "
160 	    "the program executable. Make sure that proc_name2map() handles "
161 	    "this properly.");
162 }
163 ATF_TC_BODY(map_alias_name2map, tc)
164 {
165 	struct proc_handle *phdl;
166 	prmap_t *map1, *map2;
167 
168 	phdl = start_prog(tc, false);
169 
170 	/* Initialize the rtld_db handle. */
171 	(void)proc_rdagent(phdl);
172 
173 	/* Ensure that "target_prog" and "a.out" return the same map. */
174 	map1 = proc_name2map(phdl, target_prog_file);
175 	ATF_REQUIRE_MSG(map1 != NULL, "failed to look up map for '%s'",
176 	    target_prog_file);
177 	map2 = proc_name2map(phdl, aout_object);
178 	ATF_REQUIRE_MSG(map2 != NULL, "failed to look up map for '%s'",
179 	    aout_object);
180 	ATF_CHECK_EQ(strcmp(map1->pr_mapname, map2->pr_mapname), 0);
181 
182 	ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
183 
184 	proc_detach(phdl, 0);
185 }
186 
187 ATF_TC(map_prefix_name2map);
188 ATF_TC_HEAD(map_prefix_name2map, tc)
189 {
190 	atf_tc_set_md_var(tc, "descr",
191 	    "Verify that proc_name2map() returns prefix matches of the "
192 	    "basename of loaded objects if no full matches are found.");
193 }
194 ATF_TC_BODY(map_prefix_name2map, tc)
195 {
196 	struct proc_handle *phdl;
197 	prmap_t *map1, *map2;
198 
199 	phdl = start_prog(tc, false);
200 
201 	/* Initialize the rtld_db handle. */
202 	(void)proc_rdagent(phdl);
203 
204 	/* Make sure that "ld-elf" and "ld-elf.so" return the same map. */
205 	map1 = proc_name2map(phdl, "ld-elf");
206 	ATF_REQUIRE_MSG(map1 != NULL, "failed to look up map for 'ld-elf'");
207 	map2 = proc_name2map(phdl, "ld-elf.so");
208 	ATF_REQUIRE_MSG(map2 != NULL, "failed to look up map for 'ld-elf.so'");
209 	ATF_CHECK_EQ(strcmp(map1->pr_mapname, map2->pr_mapname), 0);
210 
211 	ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
212 
213 	proc_detach(phdl, 0);
214 }
215 
216 ATF_TC(map_alias_name2sym);
217 ATF_TC_HEAD(map_alias_name2sym, tc)
218 {
219 	atf_tc_set_md_var(tc, "descr",
220 	    "Callers are supposed to be able to use \"a.out\" as an alias for "
221 	    "the program executable. Make sure that proc_name2sym() handles "
222 	    "this properly.");
223 }
224 ATF_TC_BODY(map_alias_name2sym, tc)
225 {
226 	GElf_Sym sym1, sym2;
227 	prsyminfo_t si1, si2;
228 	struct proc_handle *phdl;
229 	int error;
230 
231 	phdl = start_prog(tc, false);
232 
233 	/* Initialize the rtld_db handle. */
234 	(void)proc_rdagent(phdl);
235 
236 	/*
237 	 * Make sure that "target_prog:main" and "a.out:main" return the same
238 	 * symbol.
239 	 */
240 	error = proc_name2sym(phdl, target_prog_file, "main", &sym1, &si1);
241 	ATF_REQUIRE_EQ_MSG(error, 0, "failed to look up 'main' via %s",
242 	    target_prog_file);
243 	error = proc_name2sym(phdl, aout_object, "main", &sym2, &si2);
244 	ATF_REQUIRE_EQ_MSG(error, 0, "failed to look up 'main' via %s",
245 	    aout_object);
246 
247 	ATF_CHECK_EQ(memcmp(&sym1, &sym2, sizeof(sym1)), 0);
248 	ATF_CHECK_EQ(si1.prs_id, si2.prs_id);
249 
250 	ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
251 
252 	proc_detach(phdl, 0);
253 }
254 
255 ATF_TC(symbol_lookup);
256 ATF_TC_HEAD(symbol_lookup, tc)
257 {
258 	atf_tc_set_md_var(tc, "descr",
259 	    "Look up a couple of well-known symbols in the test program, place "
260 	    "breakpoints on them, and verify that we hit the breakpoints. Also "
261 	    "make sure that we can use the breakpoint address to look up the "
262 	    "corresponding symbol.");
263 }
264 ATF_TC_BODY(symbol_lookup, tc)
265 {
266 	GElf_Sym main_sym, r_debug_state_sym;
267 	struct proc_handle *phdl;
268 	u_long saved;
269 	int error;
270 
271 	phdl = start_prog(tc, false);
272 
273 	error = proc_name2sym(phdl, target_prog_file, "main", &main_sym, NULL);
274 	ATF_REQUIRE_EQ_MSG(error, 0, "failed to look up 'main'");
275 
276 	error = proc_name2sym(phdl, ldelf_object, "r_debug_state",
277 	    &r_debug_state_sym, NULL);
278 	ATF_REQUIRE_EQ_MSG(error, 0, "failed to look up 'r_debug_state'");
279 
280 	set_bkpt(phdl, r_debug_state_sym.st_value, &saved);
281 	ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
282 	verify_bkpt(phdl, &r_debug_state_sym, "r_debug_state", ldelf_object);
283 	remove_bkpt(phdl, r_debug_state_sym.st_value, saved);
284 
285 	set_bkpt(phdl, main_sym.st_value, &saved);
286 	ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
287 	verify_bkpt(phdl, &main_sym, "main", target_prog_file);
288 	remove_bkpt(phdl, main_sym.st_value, saved);
289 
290 	ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
291 
292 	proc_detach(phdl, 0);
293 }
294 
295 ATF_TC(symbol_lookup_fail);
296 ATF_TC_HEAD(symbol_lookup_fail, tc)
297 {
298 	atf_tc_set_md_var(tc, "descr",
299 	    "Verify that proc_addr2sym() returns an error when given an offset "
300 	    "that it cannot resolve.");
301 }
302 ATF_TC_BODY(symbol_lookup_fail, tc)
303 {
304 	char symname[32];
305 	GElf_Sym sym;
306 	struct proc_handle *phdl;
307 	prmap_t *map;
308 	int error;
309 
310 	phdl = start_prog(tc, false);
311 
312 	/* Initialize the rtld_db handle. */
313 	(void)proc_rdagent(phdl);
314 
315 	map = proc_name2map(phdl, target_prog_file);
316 	ATF_REQUIRE_MSG(map != NULL, "failed to look up map for '%s'",
317 	    target_prog_file);
318 
319 	/*
320 	 * We shouldn't be able to find symbols at the beginning of a mapped
321 	 * file.
322 	 */
323 	error = proc_addr2sym(phdl, map->pr_vaddr, symname, sizeof(symname),
324 	    &sym);
325 	ATF_REQUIRE_MSG(error != 0, "unexpectedly found a symbol");
326 
327 	ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
328 
329 	proc_detach(phdl, 0);
330 }
331 
332 ATF_TC(signal_forward);
333 ATF_TC_HEAD(signal_forward, tc)
334 {
335 	atf_tc_set_md_var(tc, "descr",
336 	    "Run the test program in a mode which causes it to send a signal "
337 	    "to itself. Make sure that we intercept the signal and that "
338 	    "proc_continue() forwards it to the process.");
339 }
340 ATF_TC_BODY(signal_forward, tc)
341 {
342 	struct proc_handle *phdl;
343 	int state, status;
344 
345 	phdl = start_prog(tc, true);
346 	ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
347 
348 	/* The process should have been interrupted by a signal. */
349 	state = proc_wstatus(phdl);
350 	ATF_REQUIRE_EQ_MSG(state, PS_STOP, "process has unexpected state %d",
351 	    state);
352 
353 	/* Continue execution and allow the signal to be delivered. */
354 	ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
355 
356 	/*
357 	 * Make sure the process exited with status 0. If it didn't receive the
358 	 * SIGUSR1 that it sent to itself, it'll exit with a non-zero exit
359 	 * status, causing the test to fail.
360 	 */
361 	state = proc_wstatus(phdl);
362 	ATF_REQUIRE_EQ_MSG(state, PS_UNDEAD, "process has unexpected state %d",
363 	    state);
364 
365 	status = proc_getwstat(phdl);
366 	ATF_REQUIRE(status >= 0);
367 	ATF_REQUIRE(WIFEXITED(status));
368 	ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
369 
370 	proc_detach(phdl, 0);
371 }
372 
373 ATF_TC(symbol_sort_local);
374 ATF_TC_HEAD(symbol_sort_local, tc)
375 {
376 	atf_tc_set_md_var(tc, "descr",
377 	    "Ensure that proc_addr2sym() returns the non-local alias when "
378 	    "the address resolves to multiple symbols.");
379 }
380 ATF_TC_BODY(symbol_sort_local, tc)
381 {
382 	char symname[32];
383 	GElf_Sym bar_sym;
384 	struct proc_handle *phdl;
385 	int error;
386 
387 	phdl = start_prog(tc, true);
388 
389 	error = proc_name2sym(phdl, target_prog_file, "bar", &bar_sym, NULL);
390 	ATF_REQUIRE_MSG(error == 0, "failed to look up 'bar' in %s",
391 	    target_prog_file);
392 	ATF_REQUIRE(GELF_ST_BIND(bar_sym.st_info) == STB_LOCAL);
393 
394 	error = proc_addr2sym(phdl, bar_sym.st_value, symname, sizeof(symname),
395 	    &bar_sym);
396 	ATF_REQUIRE_MSG(error == 0, "failed to resolve 'bar' by addr");
397 
398 	ATF_REQUIRE_MSG(strcmp(symname, "baz") == 0,
399 	    "unexpected symbol name '%s'", symname);
400 	ATF_REQUIRE(GELF_ST_BIND(bar_sym.st_info) == STB_GLOBAL);
401 
402 	proc_detach(phdl, 0);
403 }
404 
405 ATF_TC(symbol_sort_prefix);
406 ATF_TC_HEAD(symbol_sort_prefix, tc)
407 {
408 	atf_tc_set_md_var(tc, "descr",
409 	    "Ensure that proc_addr2sym() returns the alias whose name is not "
410 	    "prefixed with '$' if one exists.");
411 }
412 ATF_TC_BODY(symbol_sort_prefix, tc)
413 {
414 	char symname[32];
415 	GElf_Sym qux_sym;
416 	struct proc_handle *phdl;
417 	int error;
418 
419 	phdl = start_prog(tc, true);
420 
421 	error = proc_name2sym(phdl, target_prog_file, "$qux", &qux_sym, NULL);
422 	ATF_REQUIRE_MSG(error == 0, "failed to look up '$qux' in %s",
423 	    target_prog_file);
424 
425 	error = proc_addr2sym(phdl, qux_sym.st_value, symname, sizeof(symname),
426 	    &qux_sym);
427 	ATF_REQUIRE_MSG(error == 0, "failed to resolve 'qux' by addr");
428 
429 	ATF_REQUIRE_MSG(strcmp(symname, "qux") == 0,
430 	    "unexpected symbol name '%s'", symname);
431 
432 	proc_detach(phdl, 0);
433 }
434 
435 ATF_TC(symbol_sort_underscore);
436 ATF_TC_HEAD(symbol_sort_underscore, tc)
437 {
438 	atf_tc_set_md_var(tc, "descr",
439 	    "Ensure that proc_addr2sym() returns the alias with fewest leading "
440 	    "underscores in the name when the address resolves to multiple "
441 	    "symbols.");
442 }
443 ATF_TC_BODY(symbol_sort_underscore, tc)
444 {
445 	char symname[32];
446 	GElf_Sym foo_sym;
447 	struct proc_handle *phdl;
448 	int error;
449 
450 	phdl = start_prog(tc, true);
451 
452 	error = proc_name2sym(phdl, target_prog_file, "foo", &foo_sym, NULL);
453 	ATF_REQUIRE_MSG(error == 0, "failed to look up 'foo' in %s",
454 	    target_prog_file);
455 
456 	error = proc_addr2sym(phdl, foo_sym.st_value, symname, sizeof(symname),
457 	    &foo_sym);
458 	ATF_REQUIRE_MSG(error == 0, "failed to resolve 'foo' by addr");
459 
460 	ATF_REQUIRE_MSG(strcmp(symname, "foo") == 0,
461 	    "unexpected symbol name '%s'", symname);
462 
463 	proc_detach(phdl, 0);
464 }
465 
466 ATF_TP_ADD_TCS(tp)
467 {
468 
469 	ATF_TP_ADD_TC(tp, map_alias_name2map);
470 	ATF_TP_ADD_TC(tp, map_prefix_name2map);
471 	ATF_TP_ADD_TC(tp, map_alias_name2sym);
472 	ATF_TP_ADD_TC(tp, symbol_lookup);
473 	ATF_TP_ADD_TC(tp, symbol_lookup_fail);
474 	ATF_TP_ADD_TC(tp, signal_forward);
475 	ATF_TP_ADD_TC(tp, symbol_sort_local);
476 	ATF_TP_ADD_TC(tp, symbol_sort_prefix);
477 	ATF_TP_ADD_TC(tp, symbol_sort_underscore);
478 
479 	return (atf_no_error());
480 }
481