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