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