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