]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libproc/tests/proc_test.c
Update llvm, clang, lld and lldb to release_39 branch r288513.
[FreeBSD/FreeBSD.git] / lib / libproc / tests / proc_test.c
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 __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, &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 mapbname[MAXPATHLEN], *name;
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         basename_r(map->pr_mapname, mapbname);
151         ATF_REQUIRE_EQ_MSG(strcmp(mapname, mapbname), 0,
152             "expected map name '%s' doesn't match '%s'", mapname, mapbname);
153 }
154
155 ATF_TC(map_alias_obj2map);
156 ATF_TC_HEAD(map_alias_obj2map, 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_obj2map() handles "
161             "this properly.");
162 }
163 ATF_TC_BODY(map_alias_obj2map, 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_obj2map(phdl, target_prog_file);
175         ATF_REQUIRE_MSG(map1 != NULL, "failed to look up map for '%s'",
176             target_prog_file);
177         map2 = proc_obj2map(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_free(phdl);
185 }
186
187 ATF_TC(map_alias_name2map);
188 ATF_TC_HEAD(map_alias_name2map, tc)
189 {
190         atf_tc_set_md_var(tc, "descr",
191             "Callers are supposed to be able to use \"a.out\" as an alias for "
192             "the program executable. Make sure that proc_name2map() handles "
193             "this properly.");
194 }
195 ATF_TC_BODY(map_alias_name2map, tc)
196 {
197         struct proc_handle *phdl;
198         prmap_t *map1, *map2;
199
200         phdl = start_prog(tc, false);
201
202         /* Initialize the rtld_db handle. */
203         (void)proc_rdagent(phdl);
204
205         /* Ensure that "target_prog" and "a.out" return the same map. */
206         map1 = proc_name2map(phdl, target_prog_file);
207         ATF_REQUIRE_MSG(map1 != NULL, "failed to look up map for '%s'",
208             target_prog_file);
209         map2 = proc_name2map(phdl, aout_object);
210         ATF_REQUIRE_MSG(map2 != NULL, "failed to look up map for '%s'",
211             aout_object);
212         ATF_CHECK_EQ(strcmp(map1->pr_mapname, map2->pr_mapname), 0);
213
214         ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
215
216         proc_free(phdl);
217 }
218
219 ATF_TC(map_alias_name2sym);
220 ATF_TC_HEAD(map_alias_name2sym, tc)
221 {
222         atf_tc_set_md_var(tc, "descr",
223             "Callers are supposed to be able to use \"a.out\" as an alias for "
224             "the program executable. Make sure that proc_name2sym() handles "
225             "this properly.");
226 }
227 ATF_TC_BODY(map_alias_name2sym, tc)
228 {
229         GElf_Sym sym1, sym2;
230         prsyminfo_t si1, si2;
231         struct proc_handle *phdl;
232         int error;
233
234         phdl = start_prog(tc, false);
235
236         /* Initialize the rtld_db handle. */
237         (void)proc_rdagent(phdl);
238
239         /*
240          * Make sure that "target_prog:main" and "a.out:main" return the same
241          * symbol.
242          */
243         error = proc_name2sym(phdl, target_prog_file, "main", &sym1, &si1);
244         ATF_REQUIRE_EQ_MSG(error, 0, "failed to look up 'main' via %s",
245             target_prog_file);
246         error = proc_name2sym(phdl, aout_object, "main", &sym2, &si2);
247         ATF_REQUIRE_EQ_MSG(error, 0, "failed to look up 'main' via %s",
248             aout_object);
249
250         ATF_CHECK_EQ(memcmp(&sym1, &sym2, sizeof(sym1)), 0);
251         ATF_CHECK_EQ(si1.prs_id, si2.prs_id);
252
253         ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
254
255         proc_free(phdl);
256 }
257
258 ATF_TC(symbol_lookup);
259 ATF_TC_HEAD(symbol_lookup, tc)
260 {
261         atf_tc_set_md_var(tc, "descr",
262             "Look up a couple of well-known symbols in the test program, place "
263             "breakpoints on them, and verify that we hit the breakpoints. Also "
264             "make sure that we can use the breakpoint address to look up the "
265             "corresponding symbol.");
266 }
267 ATF_TC_BODY(symbol_lookup, tc)
268 {
269         GElf_Sym main_sym, r_debug_state_sym;
270         struct proc_handle *phdl;
271         u_long saved;
272         int error;
273
274         phdl = start_prog(tc, false);
275
276         error = proc_name2sym(phdl, target_prog_file, "main", &main_sym, NULL);
277         ATF_REQUIRE_EQ_MSG(error, 0, "failed to look up 'main'");
278
279         error = proc_name2sym(phdl, ldelf_object, "r_debug_state",
280             &r_debug_state_sym, NULL);
281         ATF_REQUIRE_EQ_MSG(error, 0, "failed to look up 'r_debug_state'");
282
283         set_bkpt(phdl, r_debug_state_sym.st_value, &saved);
284         ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
285         verify_bkpt(phdl, &r_debug_state_sym, "r_debug_state", ldelf_object);
286         remove_bkpt(phdl, r_debug_state_sym.st_value, saved);
287
288         set_bkpt(phdl, main_sym.st_value, &saved);
289         ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
290         verify_bkpt(phdl, &main_sym, "main", target_prog_file);
291         remove_bkpt(phdl, main_sym.st_value, saved);
292
293         ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
294
295         proc_free(phdl);
296 }
297
298 ATF_TC(symbol_lookup_fail);
299 ATF_TC_HEAD(symbol_lookup_fail, tc)
300 {
301         atf_tc_set_md_var(tc, "descr",
302             "Verify that proc_addr2sym() returns an error when given an offset "
303             "that it cannot resolve.");
304 }
305 ATF_TC_BODY(symbol_lookup_fail, tc)
306 {
307         char symname[32];
308         GElf_Sym sym;
309         struct proc_handle *phdl;
310         prmap_t *map;
311         int error;
312
313         phdl = start_prog(tc, false);
314
315         /* Initialize the rtld_db handle. */
316         (void)proc_rdagent(phdl);
317
318         map = proc_obj2map(phdl, target_prog_file);
319         ATF_REQUIRE_MSG(map != NULL, "failed to look up map for '%s'",
320             target_prog_file);
321
322         /*
323          * We shouldn't be able to find symbols at the beginning of a mapped
324          * file.
325          */
326         error = proc_addr2sym(phdl, map->pr_vaddr, symname, sizeof(symname),
327             &sym);
328         ATF_REQUIRE_MSG(error != 0, "unexpectedly found a symbol");
329
330         ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
331
332         proc_free(phdl);
333 }
334
335 ATF_TC(signal_forward);
336 ATF_TC_HEAD(signal_forward, tc)
337 {
338         atf_tc_set_md_var(tc, "descr",
339             "Run the test program in a mode which causes it to send a signal "
340             "to itself. Make sure that we intercept the signal and that "
341             "proc_continue() forwards it to the process.");
342 }
343 ATF_TC_BODY(signal_forward, tc)
344 {
345         struct proc_handle *phdl;
346         int state, status;
347
348         phdl = start_prog(tc, true);
349         ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
350
351         /* The process should have been interrupted by a signal. */
352         state = proc_wstatus(phdl);
353         ATF_REQUIRE_EQ_MSG(state, PS_STOP, "process has unexpected state %d",
354             state);
355
356         /* Continue execution and allow the signal to be delivered. */
357         ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
358
359         /*
360          * Make sure the process exited with status 0. If it didn't receive the
361          * SIGUSR1 that it sent to itself, it'll exit with a non-zero exit
362          * status, causing the test to fail.
363          */
364         state = proc_wstatus(phdl);
365         ATF_REQUIRE_EQ_MSG(state, PS_UNDEAD, "process has unexpected state %d",
366             state);
367
368         status = proc_getwstat(phdl);
369         ATF_REQUIRE(status >= 0);
370         ATF_REQUIRE(WIFEXITED(status));
371         ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
372
373         proc_free(phdl);
374 }
375
376 ATF_TP_ADD_TCS(tp)
377 {
378
379         ATF_TP_ADD_TC(tp, map_alias_obj2map);
380         ATF_TP_ADD_TC(tp, map_alias_name2map);
381         ATF_TP_ADD_TC(tp, map_alias_name2sym);
382         ATF_TP_ADD_TC(tp, symbol_lookup);
383         ATF_TP_ADD_TC(tp, symbol_lookup_fail);
384         ATF_TP_ADD_TC(tp, signal_forward);
385
386         return (atf_no_error());
387 }