1fbcd1dd1Schristos /*-
2fbcd1dd1Schristos * Copyright (c) 2014, 2015 Mark Johnston <markj@FreeBSD.org>
3fbcd1dd1Schristos * All rights reserved.
4fbcd1dd1Schristos *
5fbcd1dd1Schristos * Redistribution and use in source and binary forms, with or without
6fbcd1dd1Schristos * modification, are permitted provided that the following conditions
7fbcd1dd1Schristos * are met:
8fbcd1dd1Schristos * 1. Redistributions of source code must retain the above copyright
9fbcd1dd1Schristos * notice, this list of conditions and the following disclaimer.
10fbcd1dd1Schristos * 2. Redistributions in binary form must reproduce the above copyright
11fbcd1dd1Schristos * notice, this list of conditions and the following disclaimer in the
12fbcd1dd1Schristos * documentation and/or other materials provided with the distribution.
13fbcd1dd1Schristos *
14fbcd1dd1Schristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15fbcd1dd1Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16fbcd1dd1Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17fbcd1dd1Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18fbcd1dd1Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19fbcd1dd1Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20fbcd1dd1Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21fbcd1dd1Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22fbcd1dd1Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23fbcd1dd1Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24fbcd1dd1Schristos * SUCH DAMAGE.
25fbcd1dd1Schristos */
26fbcd1dd1Schristos
27fbcd1dd1Schristos #include <sys/cdefs.h>
2852ebe1c2Schristos #ifdef __FBSDID
2952ebe1c2Schristos __FBSDID("$FreeBSD: head/lib/libproc/tests/proc_test.c 286863 2015-08-17 23:19:36Z emaste $");
3052ebe1c2Schristos #endif
31*da66a31aSchristos __RCSID("$NetBSD: proc_test.c,v 1.6 2018/07/20 20:50:34 christos Exp $");
32fbcd1dd1Schristos
33fbcd1dd1Schristos #include <sys/types.h>
34fbcd1dd1Schristos #include <sys/wait.h>
35fbcd1dd1Schristos
36fbcd1dd1Schristos #include <libgen.h>
37fbcd1dd1Schristos #include <stdio.h>
38fbcd1dd1Schristos #include <stdint.h>
39fbcd1dd1Schristos #include <stdlib.h>
40fbcd1dd1Schristos #include <string.h>
41fbcd1dd1Schristos #include <atf-c.h>
42fbcd1dd1Schristos #include <libelf.h>
43fbcd1dd1Schristos #include <libproc.h>
44fbcd1dd1Schristos
45fbcd1dd1Schristos static const char *aout_object = "a.out";
4652ebe1c2Schristos #if defined(__NetBSD__)
4752ebe1c2Schristos static const char *r_debug_state = "_rtld_debug_state";
4852ebe1c2Schristos #elif defined(__FreeBSD__)
4952ebe1c2Schristos static const char *r_debug_state = "r_debug_state";
5052ebe1c2Schristos #endif
5152ebe1c2Schristos
5252ebe1c2Schristos #if defined(__NetBSD__)
5352ebe1c2Schristos static const char *ldelf_object = "ld.elf_so";
5452ebe1c2Schristos #elif defined(__FreeBSD__)
55fbcd1dd1Schristos static const char *ldelf_object = "ld-elf.so.1";
5652ebe1c2Schristos #endif
57fbcd1dd1Schristos static const char *target_prog_file = "target_prog";
58fbcd1dd1Schristos
5952ebe1c2Schristos #ifdef __NetBSD__
6052ebe1c2Schristos static char *
basename_r(const char * path,char * buf)6152ebe1c2Schristos basename_r(const char *path, char *buf)
6252ebe1c2Schristos {
6352ebe1c2Schristos // XX: We know this works
6452ebe1c2Schristos strlcpy(buf, strrchr(path, '/') + 1, PATH_MAX);
6552ebe1c2Schristos return buf;
6652ebe1c2Schristos }
6752ebe1c2Schristos #endif
6852ebe1c2Schristos
69fbcd1dd1Schristos /*
70fbcd1dd1Schristos * Run the test program. If the sig parameter is set to true, the test program
71fbcd1dd1Schristos * will deliver SIGUSR1 to itself during execution.
72fbcd1dd1Schristos */
73fbcd1dd1Schristos static struct proc_handle *
start_prog(const struct atf_tc * tc,bool sig)74fbcd1dd1Schristos start_prog(const struct atf_tc *tc, bool sig)
75fbcd1dd1Schristos {
76fbcd1dd1Schristos char *argv[3];
77fbcd1dd1Schristos struct proc_handle *phdl;
78fbcd1dd1Schristos int error;
79fbcd1dd1Schristos
80fbcd1dd1Schristos asprintf(&argv[0], "%s/%s", atf_tc_get_config_var(tc, "srcdir"),
81fbcd1dd1Schristos target_prog_file);
82fbcd1dd1Schristos ATF_REQUIRE(argv[0] != NULL);
83fbcd1dd1Schristos
84fbcd1dd1Schristos if (sig) {
85fbcd1dd1Schristos argv[1] = strdup("-s");
86fbcd1dd1Schristos argv[2] = NULL;
87fbcd1dd1Schristos } else {
88fbcd1dd1Schristos argv[1] = NULL;
89fbcd1dd1Schristos }
90fbcd1dd1Schristos
91fbcd1dd1Schristos error = proc_create(argv[0], argv, NULL, NULL, &phdl);
92fbcd1dd1Schristos ATF_REQUIRE_EQ_MSG(error, 0, "failed to run '%s'", target_prog_file);
93fbcd1dd1Schristos ATF_REQUIRE(phdl != NULL);
94fbcd1dd1Schristos
95fbcd1dd1Schristos free(argv[0]);
96fbcd1dd1Schristos free(argv[1]);
97fbcd1dd1Schristos
98fbcd1dd1Schristos return (phdl);
99fbcd1dd1Schristos }
100fbcd1dd1Schristos
101fbcd1dd1Schristos static void
set_bkpt(struct proc_handle * phdl,uintptr_t addr,proc_breakpoint_t * saved)1029a58287bSchristos set_bkpt(struct proc_handle *phdl, uintptr_t addr, proc_breakpoint_t *saved)
103fbcd1dd1Schristos {
104fbcd1dd1Schristos int error;
105fbcd1dd1Schristos
106fbcd1dd1Schristos error = proc_bkptset(phdl, addr, saved);
107fbcd1dd1Schristos ATF_REQUIRE_EQ_MSG(error, 0, "failed to set breakpoint at 0x%jx",
108fbcd1dd1Schristos (uintmax_t)addr);
109fbcd1dd1Schristos }
110fbcd1dd1Schristos
111fbcd1dd1Schristos static void
remove_bkpt(struct proc_handle * phdl,uintptr_t addr,proc_breakpoint_t * val)1129a58287bSchristos remove_bkpt(struct proc_handle *phdl, uintptr_t addr, proc_breakpoint_t *val)
113fbcd1dd1Schristos {
114fbcd1dd1Schristos int error;
115fbcd1dd1Schristos
116fbcd1dd1Schristos error = proc_bkptdel(phdl, addr, val);
117fbcd1dd1Schristos ATF_REQUIRE_EQ_MSG(error, 0,
118fbcd1dd1Schristos "failed to delete breakpoint at 0x%jx", (uintmax_t)addr);
119fbcd1dd1Schristos
120fbcd1dd1Schristos error = proc_regset(phdl, REG_PC, addr);
121fbcd1dd1Schristos ATF_REQUIRE_EQ_MSG(error, 0, "failed to reset program counter");
122fbcd1dd1Schristos }
123fbcd1dd1Schristos
124fbcd1dd1Schristos /*
125fbcd1dd1Schristos * Wait for the specified process to hit a breakpoint at the specified symbol.
126fbcd1dd1Schristos */
127fbcd1dd1Schristos static void
verify_bkpt(struct proc_handle * phdl,GElf_Sym * sym,const char * symname,const char * mapname)128fbcd1dd1Schristos verify_bkpt(struct proc_handle *phdl, GElf_Sym *sym, const char *symname,
129fbcd1dd1Schristos const char *mapname)
130fbcd1dd1Schristos {
131fbcd1dd1Schristos char mapbname[MAXPATHLEN], *name;
132fbcd1dd1Schristos GElf_Sym tsym;
133fbcd1dd1Schristos prmap_t *map;
134fbcd1dd1Schristos size_t namesz;
135fbcd1dd1Schristos u_long addr;
136fbcd1dd1Schristos int error, state;
137fbcd1dd1Schristos
138fbcd1dd1Schristos state = proc_wstatus(phdl);
139fbcd1dd1Schristos ATF_REQUIRE_EQ_MSG(state, PS_STOP, "process has state %d", state);
140fbcd1dd1Schristos
141fbcd1dd1Schristos /* Get the program counter and decrement it. */
142fbcd1dd1Schristos error = proc_regget(phdl, REG_PC, &addr);
143fbcd1dd1Schristos ATF_REQUIRE_EQ_MSG(error, 0, "failed to obtain PC for '%s'",
144fbcd1dd1Schristos target_prog_file);
145fbcd1dd1Schristos proc_bkptregadj(&addr);
146fbcd1dd1Schristos
147fbcd1dd1Schristos /*
148fbcd1dd1Schristos * Make sure the PC matches the expected value obtained from the symbol
149fbcd1dd1Schristos * definition we looked up earlier.
150fbcd1dd1Schristos */
151fbcd1dd1Schristos ATF_CHECK_EQ_MSG(addr, sym->st_value,
152fbcd1dd1Schristos "program counter 0x%lx doesn't match expected value 0x%jx",
153fbcd1dd1Schristos addr, (uintmax_t)sym->st_value);
154fbcd1dd1Schristos
155fbcd1dd1Schristos /*
156fbcd1dd1Schristos * Ensure we can look up the r_debug_state symbol using its starting
157fbcd1dd1Schristos * address and that the resulting symbol matches the one we found using
158fbcd1dd1Schristos * a name lookup.
159fbcd1dd1Schristos */
160fbcd1dd1Schristos namesz = strlen(symname) + 1;
161fbcd1dd1Schristos name = malloc(namesz);
162fbcd1dd1Schristos ATF_REQUIRE(name != NULL);
163fbcd1dd1Schristos
164fbcd1dd1Schristos error = proc_addr2sym(phdl, addr, name, namesz, &tsym);
165fbcd1dd1Schristos ATF_REQUIRE_EQ_MSG(error, 0, "failed to look up symbol at 0x%lx", addr);
166fbcd1dd1Schristos ATF_REQUIRE_EQ(memcmp(sym, &tsym, sizeof(*sym)), 0);
16752ebe1c2Schristos ATF_REQUIRE_EQ_MSG(strcmp(symname, name), 0,
16852ebe1c2Schristos "expected symbol name '%s' doesn't match '%s'", symname, name);
169fbcd1dd1Schristos free(name);
170fbcd1dd1Schristos
171fbcd1dd1Schristos map = proc_addr2map(phdl, addr);
172fbcd1dd1Schristos ATF_REQUIRE_MSG(map != NULL, "failed to look up map for address 0x%lx",
173fbcd1dd1Schristos addr);
174fbcd1dd1Schristos basename_r(map->pr_mapname, mapbname);
175fbcd1dd1Schristos ATF_REQUIRE_EQ_MSG(strcmp(mapname, mapbname), 0,
176fbcd1dd1Schristos "expected map name '%s' doesn't match '%s'", mapname, mapbname);
177fbcd1dd1Schristos }
178fbcd1dd1Schristos
179fbcd1dd1Schristos ATF_TC(map_alias_obj2map);
ATF_TC_HEAD(map_alias_obj2map,tc)180fbcd1dd1Schristos ATF_TC_HEAD(map_alias_obj2map, tc)
181fbcd1dd1Schristos {
182fbcd1dd1Schristos atf_tc_set_md_var(tc, "descr",
183fbcd1dd1Schristos "Callers are supposed to be able to use \"a.out\" as an alias for "
184fbcd1dd1Schristos "the program executable. Make sure that proc_obj2map() handles "
185fbcd1dd1Schristos "this properly.");
186fbcd1dd1Schristos }
ATF_TC_BODY(map_alias_obj2map,tc)187fbcd1dd1Schristos ATF_TC_BODY(map_alias_obj2map, tc)
188fbcd1dd1Schristos {
189fbcd1dd1Schristos struct proc_handle *phdl;
190fbcd1dd1Schristos prmap_t *map1, *map2;
191fbcd1dd1Schristos
192fbcd1dd1Schristos phdl = start_prog(tc, false);
193fbcd1dd1Schristos
194fbcd1dd1Schristos /* Initialize the rtld_db handle. */
195fbcd1dd1Schristos (void)proc_rdagent(phdl);
196fbcd1dd1Schristos
197fbcd1dd1Schristos /* Ensure that "target_prog" and "a.out" return the same map. */
198fbcd1dd1Schristos map1 = proc_obj2map(phdl, target_prog_file);
199fbcd1dd1Schristos ATF_REQUIRE_MSG(map1 != NULL, "failed to look up map for '%s'",
200fbcd1dd1Schristos target_prog_file);
201fbcd1dd1Schristos map2 = proc_obj2map(phdl, aout_object);
202fbcd1dd1Schristos ATF_REQUIRE_MSG(map2 != NULL, "failed to look up map for '%s'",
203fbcd1dd1Schristos aout_object);
204fbcd1dd1Schristos ATF_CHECK_EQ(strcmp(map1->pr_mapname, map2->pr_mapname), 0);
205fbcd1dd1Schristos
206800ae963Schristos ATF_CHECK_EQ_MSG(proc_detach(phdl, PRELEASE_HANG), 0, "failed to detach");
207fbcd1dd1Schristos
208fbcd1dd1Schristos proc_free(phdl);
209fbcd1dd1Schristos }
210fbcd1dd1Schristos
211fbcd1dd1Schristos ATF_TC(map_alias_name2map);
ATF_TC_HEAD(map_alias_name2map,tc)212fbcd1dd1Schristos ATF_TC_HEAD(map_alias_name2map, tc)
213fbcd1dd1Schristos {
214fbcd1dd1Schristos atf_tc_set_md_var(tc, "descr",
215fbcd1dd1Schristos "Callers are supposed to be able to use \"a.out\" as an alias for "
216fbcd1dd1Schristos "the program executable. Make sure that proc_name2map() handles "
217fbcd1dd1Schristos "this properly.");
218fbcd1dd1Schristos }
ATF_TC_BODY(map_alias_name2map,tc)219fbcd1dd1Schristos ATF_TC_BODY(map_alias_name2map, tc)
220fbcd1dd1Schristos {
221fbcd1dd1Schristos struct proc_handle *phdl;
222fbcd1dd1Schristos prmap_t *map1, *map2;
223fbcd1dd1Schristos
224fbcd1dd1Schristos phdl = start_prog(tc, false);
225fbcd1dd1Schristos
226fbcd1dd1Schristos /* Initialize the rtld_db handle. */
227fbcd1dd1Schristos (void)proc_rdagent(phdl);
228fbcd1dd1Schristos
229fbcd1dd1Schristos /* Ensure that "target_prog" and "a.out" return the same map. */
230fbcd1dd1Schristos map1 = proc_name2map(phdl, target_prog_file);
231fbcd1dd1Schristos ATF_REQUIRE_MSG(map1 != NULL, "failed to look up map for '%s'",
232fbcd1dd1Schristos target_prog_file);
233fbcd1dd1Schristos map2 = proc_name2map(phdl, aout_object);
234fbcd1dd1Schristos ATF_REQUIRE_MSG(map2 != NULL, "failed to look up map for '%s'",
235fbcd1dd1Schristos aout_object);
236fbcd1dd1Schristos ATF_CHECK_EQ(strcmp(map1->pr_mapname, map2->pr_mapname), 0);
237fbcd1dd1Schristos
238800ae963Schristos ATF_CHECK_EQ_MSG(proc_detach(phdl, PRELEASE_HANG), 0, "failed to detach");
239fbcd1dd1Schristos
240fbcd1dd1Schristos proc_free(phdl);
241fbcd1dd1Schristos }
242fbcd1dd1Schristos
243fbcd1dd1Schristos ATF_TC(map_alias_name2sym);
ATF_TC_HEAD(map_alias_name2sym,tc)244fbcd1dd1Schristos ATF_TC_HEAD(map_alias_name2sym, tc)
245fbcd1dd1Schristos {
246fbcd1dd1Schristos atf_tc_set_md_var(tc, "descr",
247fbcd1dd1Schristos "Callers are supposed to be able to use \"a.out\" as an alias for "
248fbcd1dd1Schristos "the program executable. Make sure that proc_name2sym() handles "
249fbcd1dd1Schristos "this properly.");
250fbcd1dd1Schristos }
ATF_TC_BODY(map_alias_name2sym,tc)251fbcd1dd1Schristos ATF_TC_BODY(map_alias_name2sym, tc)
252fbcd1dd1Schristos {
253fbcd1dd1Schristos GElf_Sym sym1, sym2;
254fbcd1dd1Schristos prsyminfo_t si1, si2;
255fbcd1dd1Schristos struct proc_handle *phdl;
256fbcd1dd1Schristos int error;
257fbcd1dd1Schristos
258fbcd1dd1Schristos phdl = start_prog(tc, false);
259fbcd1dd1Schristos
260fbcd1dd1Schristos /* Initialize the rtld_db handle. */
261fbcd1dd1Schristos (void)proc_rdagent(phdl);
262fbcd1dd1Schristos
263fbcd1dd1Schristos /*
264fbcd1dd1Schristos * Make sure that "target_prog:main" and "a.out:main" return the same
265fbcd1dd1Schristos * symbol.
266fbcd1dd1Schristos */
267fbcd1dd1Schristos error = proc_name2sym(phdl, target_prog_file, "main", &sym1, &si1);
268fbcd1dd1Schristos ATF_REQUIRE_EQ_MSG(error, 0, "failed to look up 'main' via %s",
269fbcd1dd1Schristos target_prog_file);
270fbcd1dd1Schristos error = proc_name2sym(phdl, aout_object, "main", &sym2, &si2);
271fbcd1dd1Schristos ATF_REQUIRE_EQ_MSG(error, 0, "failed to look up 'main' via %s",
272fbcd1dd1Schristos aout_object);
273fbcd1dd1Schristos
274fbcd1dd1Schristos ATF_CHECK_EQ(memcmp(&sym1, &sym2, sizeof(sym1)), 0);
275fbcd1dd1Schristos ATF_CHECK_EQ(si1.prs_id, si2.prs_id);
276fbcd1dd1Schristos
277800ae963Schristos ATF_CHECK_EQ_MSG(proc_detach(phdl, PRELEASE_HANG), 0, "failed to detach");
278fbcd1dd1Schristos
279fbcd1dd1Schristos proc_free(phdl);
280fbcd1dd1Schristos }
281fbcd1dd1Schristos
282fbcd1dd1Schristos ATF_TC(symbol_lookup);
ATF_TC_HEAD(symbol_lookup,tc)283fbcd1dd1Schristos ATF_TC_HEAD(symbol_lookup, tc)
284fbcd1dd1Schristos {
285fbcd1dd1Schristos atf_tc_set_md_var(tc, "descr",
286fbcd1dd1Schristos "Look up a couple of well-known symbols in the test program, place "
287fbcd1dd1Schristos "breakpoints on them, and verify that we hit the breakpoints. Also "
288fbcd1dd1Schristos "make sure that we can use the breakpoint address to look up the "
289fbcd1dd1Schristos "corresponding symbol.");
290fbcd1dd1Schristos }
ATF_TC_BODY(symbol_lookup,tc)291fbcd1dd1Schristos ATF_TC_BODY(symbol_lookup, tc)
292fbcd1dd1Schristos {
293fbcd1dd1Schristos GElf_Sym main_sym, r_debug_state_sym;
294fbcd1dd1Schristos struct proc_handle *phdl;
2959a58287bSchristos proc_breakpoint_t saved;
296fbcd1dd1Schristos int error;
297fbcd1dd1Schristos
298fbcd1dd1Schristos phdl = start_prog(tc, false);
299fbcd1dd1Schristos
300fbcd1dd1Schristos error = proc_name2sym(phdl, target_prog_file, "main", &main_sym, NULL);
301fbcd1dd1Schristos ATF_REQUIRE_EQ_MSG(error, 0, "failed to look up 'main'");
302fbcd1dd1Schristos
30352ebe1c2Schristos error = proc_name2sym(phdl, ldelf_object, r_debug_state,
304fbcd1dd1Schristos &r_debug_state_sym, NULL);
30552ebe1c2Schristos ATF_REQUIRE_EQ_MSG(error, 0, "failed to look up '%s'", r_debug_state);
306fbcd1dd1Schristos
307c4db4601Schristos set_bkpt(phdl, (uintptr_t)r_debug_state_sym.st_value, &saved);
308fbcd1dd1Schristos ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
30952ebe1c2Schristos verify_bkpt(phdl, &r_debug_state_sym, r_debug_state, ldelf_object);
3109a58287bSchristos remove_bkpt(phdl, (uintptr_t)r_debug_state_sym.st_value, &saved);
311fbcd1dd1Schristos
312c4db4601Schristos set_bkpt(phdl, (uintptr_t)main_sym.st_value, &saved);
313fbcd1dd1Schristos ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
314fbcd1dd1Schristos verify_bkpt(phdl, &main_sym, "main", target_prog_file);
3159a58287bSchristos remove_bkpt(phdl, (uintptr_t)main_sym.st_value, &saved);
316fbcd1dd1Schristos
317800ae963Schristos ATF_CHECK_EQ_MSG(proc_detach(phdl, PRELEASE_HANG), 0, "failed to detach");
318fbcd1dd1Schristos
319fbcd1dd1Schristos proc_free(phdl);
320fbcd1dd1Schristos }
321fbcd1dd1Schristos
322fbcd1dd1Schristos ATF_TC(symbol_lookup_fail);
ATF_TC_HEAD(symbol_lookup_fail,tc)323fbcd1dd1Schristos ATF_TC_HEAD(symbol_lookup_fail, tc)
324fbcd1dd1Schristos {
325fbcd1dd1Schristos atf_tc_set_md_var(tc, "descr",
326fbcd1dd1Schristos "Verify that proc_addr2sym() returns an error when given an offset "
327fbcd1dd1Schristos "that it cannot resolve.");
328fbcd1dd1Schristos }
ATF_TC_BODY(symbol_lookup_fail,tc)329fbcd1dd1Schristos ATF_TC_BODY(symbol_lookup_fail, tc)
330fbcd1dd1Schristos {
331fbcd1dd1Schristos char symname[32];
332fbcd1dd1Schristos GElf_Sym sym;
333fbcd1dd1Schristos struct proc_handle *phdl;
334fbcd1dd1Schristos prmap_t *map;
335fbcd1dd1Schristos int error;
336fbcd1dd1Schristos
337fbcd1dd1Schristos phdl = start_prog(tc, false);
338fbcd1dd1Schristos
339fbcd1dd1Schristos /* Initialize the rtld_db handle. */
340fbcd1dd1Schristos (void)proc_rdagent(phdl);
341fbcd1dd1Schristos
342fbcd1dd1Schristos map = proc_obj2map(phdl, target_prog_file);
343fbcd1dd1Schristos ATF_REQUIRE_MSG(map != NULL, "failed to look up map for '%s'",
344fbcd1dd1Schristos target_prog_file);
345fbcd1dd1Schristos
346fbcd1dd1Schristos /*
347fbcd1dd1Schristos * We shouldn't be able to find symbols at the beginning of a mapped
348fbcd1dd1Schristos * file.
349fbcd1dd1Schristos */
350fbcd1dd1Schristos error = proc_addr2sym(phdl, map->pr_vaddr, symname, sizeof(symname),
351fbcd1dd1Schristos &sym);
352fbcd1dd1Schristos ATF_REQUIRE_MSG(error != 0, "unexpectedly found a symbol");
353fbcd1dd1Schristos
354fbcd1dd1Schristos ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
355fbcd1dd1Schristos
356fbcd1dd1Schristos proc_free(phdl);
357fbcd1dd1Schristos }
358fbcd1dd1Schristos
359fbcd1dd1Schristos ATF_TC(signal_forward);
ATF_TC_HEAD(signal_forward,tc)360fbcd1dd1Schristos ATF_TC_HEAD(signal_forward, tc)
361fbcd1dd1Schristos {
362fbcd1dd1Schristos atf_tc_set_md_var(tc, "descr",
363fbcd1dd1Schristos "Run the test program in a mode which causes it to send a signal "
364fbcd1dd1Schristos "to itself. Make sure that we intercept the signal and that "
365fbcd1dd1Schristos "proc_continue() forwards it to the process.");
366fbcd1dd1Schristos }
ATF_TC_BODY(signal_forward,tc)367fbcd1dd1Schristos ATF_TC_BODY(signal_forward, tc)
368fbcd1dd1Schristos {
369fbcd1dd1Schristos struct proc_handle *phdl;
370fbcd1dd1Schristos int state, status;
371fbcd1dd1Schristos
372fbcd1dd1Schristos phdl = start_prog(tc, true);
373fbcd1dd1Schristos ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
374fbcd1dd1Schristos
375fbcd1dd1Schristos /* The process should have been interrupted by a signal. */
376fbcd1dd1Schristos state = proc_wstatus(phdl);
377fbcd1dd1Schristos ATF_REQUIRE_EQ_MSG(state, PS_STOP, "process has unexpected state %d",
378fbcd1dd1Schristos state);
379fbcd1dd1Schristos
380fbcd1dd1Schristos /* Continue execution and allow the signal to be delivered. */
381fbcd1dd1Schristos ATF_CHECK_EQ_MSG(proc_continue(phdl), 0, "failed to resume execution");
382fbcd1dd1Schristos
383fbcd1dd1Schristos /*
384fbcd1dd1Schristos * Make sure the process exited with status 0. If it didn't receive the
385fbcd1dd1Schristos * SIGUSR1 that it sent to itself, it'll exit with a non-zero exit
386fbcd1dd1Schristos * status, causing the test to fail.
387fbcd1dd1Schristos */
388fbcd1dd1Schristos state = proc_wstatus(phdl);
389fbcd1dd1Schristos ATF_REQUIRE_EQ_MSG(state, PS_UNDEAD, "process has unexpected state %d",
390fbcd1dd1Schristos state);
391fbcd1dd1Schristos
392fbcd1dd1Schristos status = proc_getwstat(phdl);
393fbcd1dd1Schristos ATF_REQUIRE(status >= 0);
394fbcd1dd1Schristos ATF_REQUIRE(WIFEXITED(status));
395fbcd1dd1Schristos ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
396fbcd1dd1Schristos
397fbcd1dd1Schristos proc_free(phdl);
398fbcd1dd1Schristos }
399fbcd1dd1Schristos
ATF_TP_ADD_TCS(tp)400fbcd1dd1Schristos ATF_TP_ADD_TCS(tp)
401fbcd1dd1Schristos {
402fbcd1dd1Schristos
403fbcd1dd1Schristos ATF_TP_ADD_TC(tp, map_alias_obj2map);
404fbcd1dd1Schristos ATF_TP_ADD_TC(tp, map_alias_name2map);
405fbcd1dd1Schristos ATF_TP_ADD_TC(tp, map_alias_name2sym);
406fbcd1dd1Schristos ATF_TP_ADD_TC(tp, symbol_lookup);
407fbcd1dd1Schristos ATF_TP_ADD_TC(tp, symbol_lookup_fail);
408fbcd1dd1Schristos ATF_TP_ADD_TC(tp, signal_forward);
409fbcd1dd1Schristos
410fbcd1dd1Schristos return (atf_no_error());
411fbcd1dd1Schristos }
412