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