1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
#include "test/jemalloc_test.h"
#ifndef _WIN32
#include <sys/wait.h>
#endif
#ifndef _WIN32
static void
wait_for_child_exit(int pid) {
int status;
while (true) {
if (waitpid(pid, &status, 0) == -1) {
test_fail("Unexpected waitpid() failure.");
}
if (WIFSIGNALED(status)) {
test_fail("Unexpected child termination due to "
"signal %d", WTERMSIG(status));
break;
}
if (WIFEXITED(status)) {
if (WEXITSTATUS(status) != 0) {
test_fail("Unexpected child exit value %d",
WEXITSTATUS(status));
}
break;
}
}
}
#endif
TEST_BEGIN(test_fork) {
#ifndef _WIN32
void *p;
pid_t pid;
/* Set up a manually managed arena for test. */
unsigned arena_ind;
size_t sz = sizeof(unsigned);
expect_d_eq(mallctl("arenas.create", (void *)&arena_ind, &sz, NULL, 0),
0, "Unexpected mallctl() failure");
/* Migrate to the new arena. */
unsigned old_arena_ind;
sz = sizeof(old_arena_ind);
expect_d_eq(mallctl("thread.arena", (void *)&old_arena_ind, &sz,
(void *)&arena_ind, sizeof(arena_ind)), 0,
"Unexpected mallctl() failure");
p = malloc(1);
expect_ptr_not_null(p, "Unexpected malloc() failure");
pid = fork();
free(p);
p = malloc(64);
expect_ptr_not_null(p, "Unexpected malloc() failure");
free(p);
if (pid == -1) {
/* Error. */
test_fail("Unexpected fork() failure");
} else if (pid == 0) {
/* Child. */
_exit(0);
} else {
wait_for_child_exit(pid);
}
#else
test_skip("fork(2) is irrelevant to Windows");
#endif
}
TEST_END
#ifndef _WIN32
static void *
do_fork_thd(void *arg) {
malloc(1);
int pid = fork();
if (pid == -1) {
/* Error. */
test_fail("Unexpected fork() failure");
} else if (pid == 0) {
/* Child. */
char *args[] = {"true", NULL};
execvp(args[0], args);
test_fail("Exec failed");
} else {
/* Parent */
wait_for_child_exit(pid);
}
return NULL;
}
#endif
#ifndef _WIN32
static void
do_test_fork_multithreaded() {
thd_t child;
thd_create(&child, do_fork_thd, NULL);
do_fork_thd(NULL);
thd_join(child, NULL);
}
#endif
TEST_BEGIN(test_fork_multithreaded) {
#ifndef _WIN32
/*
* We've seen bugs involving hanging on arenas_lock (though the same
* class of bugs can happen on any mutex). The bugs are intermittent
* though, so we want to run the test multiple times. Since we hold the
* arenas lock only early in the process lifetime, we can't just run
* this test in a loop (since, after all the arenas are initialized, we
* won't acquire arenas_lock any further). We therefore repeat the test
* with multiple processes.
*/
for (int i = 0; i < 100; i++) {
int pid = fork();
if (pid == -1) {
/* Error. */
test_fail("Unexpected fork() failure,");
} else if (pid == 0) {
/* Child. */
do_test_fork_multithreaded();
_exit(0);
} else {
wait_for_child_exit(pid);
}
}
#else
test_skip("fork(2) is irrelevant to Windows");
#endif
}
TEST_END
int
main(void) {
return test_no_reentrancy(
test_fork,
test_fork_multithreaded);
}
|