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