Changeset View
Changeset View
Standalone View
Standalone View
lib/libc/tests/gen/posix_spawn_test.c
| Show All 24 Lines | |||||
| */ | */ | ||||
| /* | /* | ||||
| * Test program for posix_spawn() and posix_spawnp() as specified by | * Test program for posix_spawn() and posix_spawnp() as specified by | ||||
| * IEEE Std. 1003.1-2008. | * IEEE Std. 1003.1-2008. | ||||
| */ | */ | ||||
| #include <sys/param.h> | #include <sys/param.h> | ||||
| #include <sys/mman.h> | |||||
| #include <sys/stat.h> | #include <sys/stat.h> | ||||
| #include <sys/wait.h> | #include <sys/wait.h> | ||||
| #include <dlfcn.h> | |||||
| #include <errno.h> | #include <errno.h> | ||||
| #include <fcntl.h> | #include <fcntl.h> | ||||
| #include <stdio.h> | #include <stdio.h> | ||||
| #include <stdlib.h> | #include <stdlib.h> | ||||
| #include <string.h> | #include <string.h> | ||||
| #include <spawn.h> | #include <spawn.h> | ||||
| #include <atf-c.h> | #include <atf-c.h> | ||||
| ▲ Show 20 Lines • Show All 125 Lines • ▼ Show 20 Lines | for (size_t i = 0; i < nitems(spawnp_eacces_tests); i++) { | ||||
| error = posix_spawnp(&pid, myargs[0], NULL, NULL, myargs, | error = posix_spawnp(&pid, myargs[0], NULL, NULL, myargs, | ||||
| myenv); | myenv); | ||||
| ATF_CHECK_INTEQ_MSG(tc->error_expected, error, | ATF_CHECK_INTEQ_MSG(tc->error_expected, error, | ||||
| "path '%s'", tc->pathvar); | "path '%s'", tc->pathvar); | ||||
| } | } | ||||
| } | } | ||||
| #define NUM_DSO 256 | |||||
| ATF_TC_WITHOUT_HEAD(posix_spawnp_stackunderflow); | |||||
| ATF_TC_BODY(posix_spawnp_stackunderflow, tc) | |||||
| { | |||||
| struct stat sb; | |||||
| char dsopath[MAXPATHLEN]; | |||||
| char *myargs[] = { "true", NULL }; | |||||
| void **handles; | |||||
| char *dsomap; | |||||
| size_t dsosz; | |||||
| int error, fd, nfd, status; | |||||
| pid_t pid, waitres; | |||||
| /* Make sure we have no child processes. */ | |||||
| while (waitpid(-1, NULL, 0) != -1) | |||||
| ; | |||||
| ATF_REQUIRE_MSG(errno == ECHILD, "errno was not ECHILD: %d", errno); | |||||
| (void)snprintf(dsopath, sizeof(dsopath), "%s/libdummy.so", | |||||
| atf_tc_get_config_var(tc, "srcdir")); | |||||
| fd = open(dsopath, O_RDONLY); | |||||
kib: why RDWR? | |||||
| ATF_REQUIRE(fd >= 0); | |||||
| /* | |||||
| * We'll open our original shlib and fdlopen() it repeatedly until we | |||||
| * have a lot of DSOs open, then we'll trigger a posix_spawnp. This | |||||
| * previously unearthed suboptimal stack usage in rtld that caused | |||||
| * posix_spawnp()'s effectively-vforked environment to underflow its | |||||
| * stack. | |||||
| * | |||||
| * This test requires ASAN to be useful, because emulating the original | |||||
| * problem faithfully is difficult to do in isolation: you have to be | |||||
| * able to land the stack allocation in posix_spawnp() in just the right | |||||
| * place to detect corruption of adjacent memory. | |||||
| */ | |||||
| ATF_REQUIRE(fstat(fd, &sb) == 0); | |||||
| dsosz = sb.st_size; | |||||
| dsomap = mmap(NULL, dsosz, PROT_READ, MAP_SHARED, fd, 0); | |||||
Done Inline Actionswhy PROT_WRITE? kib: why PROT_WRITE? | |||||
| ATF_REQUIRE(dsomap != MAP_FAILED); | |||||
| handles = calloc(sizeof(*handles), NUM_DSO); | |||||
| ATF_REQUIRE(handles != NULL); | |||||
| for (int i = 0; i < NUM_DSO; i++) { | |||||
| nfd = memfd_create("dsobase", MFD_CLOEXEC); | |||||
| ATF_REQUIRE(nfd >= 0); | |||||
| ATF_REQUIRE(ftruncate(nfd, dsosz) == 0); | |||||
| ATF_REQUIRE(write(nfd, dsomap, dsosz) == dsosz); | |||||
| handles[i] = fdlopen(nfd, RTLD_LAZY); | |||||
| ATF_REQUIRE(handles[i] != NULL); | |||||
| if (i > 0) | |||||
| ATF_REQUIRE(handles[i] != handles[i - 1]); | |||||
Done Inline ActionsAn earlier version of this change also tried to create unique SOVERSIONs for every loaded object, but it seems to be sufficient to just have a different st_dev/st_ino for each one. I retained this assertion just in case something goes wrong, though. kevans: An earlier version of this change also tried to create unique SOVERSIONs for every loaded… | |||||
| close(nfd); | |||||
| } | |||||
| /* Simple test. */ | |||||
| error = posix_spawnp(&pid, myargs[0], NULL, NULL, myargs, myenv); | |||||
| ATF_REQUIRE(error == 0); | |||||
| waitres = waitpid(pid, &status, 0); | |||||
| ATF_REQUIRE(waitres == pid); | |||||
| ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 0); | |||||
| } | |||||
| ATF_TP_ADD_TCS(tp) | ATF_TP_ADD_TCS(tp) | ||||
| { | { | ||||
| ATF_TP_ADD_TC(tp, posix_spawn_simple_test); | ATF_TP_ADD_TC(tp, posix_spawn_simple_test); | ||||
| ATF_TP_ADD_TC(tp, posix_spawn_no_such_command_negative_test); | ATF_TP_ADD_TC(tp, posix_spawn_no_such_command_negative_test); | ||||
| ATF_TP_ADD_TC(tp, posix_spawnp_enoexec_fallback); | ATF_TP_ADD_TC(tp, posix_spawnp_enoexec_fallback); | ||||
| ATF_TP_ADD_TC(tp, posix_spawnp_enoexec_fallback_null_argv0); | ATF_TP_ADD_TC(tp, posix_spawnp_enoexec_fallback_null_argv0); | ||||
| ATF_TP_ADD_TC(tp, posix_spawnp_eacces); | ATF_TP_ADD_TC(tp, posix_spawnp_eacces); | ||||
| ATF_TP_ADD_TC(tp, posix_spawnp_stackunderflow); | |||||
| return (atf_no_error()); | return (atf_no_error()); | ||||
| } | } | ||||
why RDWR?