-
Notifications
You must be signed in to change notification settings - Fork 586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix a deadlock in syscallbuf unmapping after vfork #3826
Merged
rocallahan
merged 2 commits into
rr-debugger:master
from
KJTsanaktsidis:ktsanaktsidis/fix_syscallbuf_unmap
Sep 17, 2024
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* -*- Mode: C; tab-width: 8; c-basic-offset: 2; indent-tabs-mode: nil; -*- */ | ||
|
||
#include "util.h" | ||
|
||
static int pipefds[2]; | ||
static const char *CHILD_PROCESS_NAME = "clone_syscallbuf_cleanup_child"; | ||
|
||
void assert_syscallbuf_count(int expected) { | ||
FILE *f = fopen("/proc/self/maps", "r"); | ||
test_assert(!!f); | ||
|
||
int actual = 0; | ||
for (;;) { | ||
size_t len = 0; | ||
char *line = NULL; | ||
int ret = getline(&line, &len, f); | ||
if (ret == -1) { | ||
break; | ||
} | ||
if (strstr(line, "rr-shared-syscallbuf")) { | ||
actual++; | ||
} | ||
free(line); | ||
} | ||
test_assert(expected == actual); | ||
} | ||
|
||
static int exec_proc(__attribute__((unused)) void* arg) { | ||
// Close the reading end of the pipe in this process | ||
close(pipefds[0]); | ||
|
||
// Wait for the parent to be blocked in a read(2) syscall waiting on the pipe | ||
char wchan_path[PATH_MAX]; | ||
snprintf(wchan_path, PATH_MAX, "/proc/%d/wchan", getppid()); | ||
char wchan[1024] = {0}; | ||
do { | ||
FILE *f = fopen(wchan_path, "r"); | ||
fread(wchan, 1, 1024, f); | ||
fclose(f); | ||
} while (strncmp(wchan, "pipe_read", 1024) != 0); | ||
|
||
sleep(1); | ||
|
||
assert_syscallbuf_count(2); | ||
// Now exec our child. The child will un-freeze the parent. | ||
char wpipe_name[PATH_MAX]; | ||
snprintf(wpipe_name, sizeof(wpipe_name), "/proc/self/fd/%d", pipefds[1]); | ||
execl("/proc/self/exe", CHILD_PROCESS_NAME, wpipe_name, NULL); | ||
|
||
test_assert("Not reached" && 0); | ||
return 0; | ||
} | ||
|
||
static void execd_child_proc(char *pipe_file) { | ||
// Unblock the parent by writing a . to the shared pipe | ||
pipefds[1] = open(pipe_file, O_WRONLY); | ||
test_assert(pipefds[1] != -1); | ||
|
||
char dummy[1] = { '.' }; | ||
int ret = write(pipefds[1], dummy, 1); | ||
test_assert(ret == 1); | ||
|
||
close(pipefds[1]); | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
// This is what get exec'd from exec_proc | ||
if (argc == 2 && strcmp(argv[0], CHILD_PROCESS_NAME) == 0) { | ||
execd_child_proc(argv[1]); | ||
return 0; | ||
} | ||
|
||
int ret; | ||
|
||
// Before forking, there should only be one syscallbuf | ||
assert_syscallbuf_count(1); | ||
|
||
ret = pipe2(pipefds, 0); | ||
test_assert(ret != -1); | ||
|
||
// Spawn a process which shares our address space, and will execve(2) | ||
const size_t stack_size = 1 << 20; | ||
void* exec_proc_stack = mmap(NULL, stack_size, PROT_READ | PROT_WRITE, | ||
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | ||
pid_t exec_proc_pid = clone(exec_proc, exec_proc_stack + stack_size, CLONE_VM | SIGCHLD, | ||
NULL, NULL, NULL, NULL); | ||
|
||
// This proces needs the read end of the pipe, close the write end | ||
close(pipefds[1]); | ||
|
||
// Block ourselves attempting to read from the pipe. | ||
char dummy[1] = { 0 }; | ||
ret = read(pipefds[0], dummy, 1); | ||
// When we are woken up, we should have read a byte | ||
test_assert(ret == 1); | ||
test_assert(dummy[0] == '.'); | ||
|
||
// This means the child exec'd so we should have cleaned up the syscallbuf | ||
assert_syscallbuf_count(1); | ||
|
||
// Reap the exec'd child | ||
test_assert(exec_proc_pid == waitpid(exec_proc_pid, NULL, 0)); | ||
atomic_puts("EXIT-SUCCESS"); | ||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source `dirname $0`/util.sh | ||
skip_if_no_syscall_buf | ||
record $TESTNAME | ||
replay |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* -*- Mode: C; tab-width: 8; c-basic-offset: 2; indent-tabs-mode: nil; -*- */ | ||
|
||
#include "util.h" | ||
|
||
static const size_t SHARED_MEMFD_SIZE = 4096; | ||
static int shared_memfd; | ||
static volatile char *shared_memfd_mapping; | ||
static const char *CHILD_PROCESS_NAME = "clone_syscallbuf_cleanup_child"; | ||
|
||
void assert_syscallbuf_count(int expected) { | ||
FILE *f = fopen("/proc/self/maps", "r"); | ||
test_assert(!!f); | ||
|
||
int actual = 0; | ||
for (;;) { | ||
size_t len = 0; | ||
char *line = NULL; | ||
int ret = getline(&line, &len, f); | ||
if (ret == -1) { | ||
break; | ||
} | ||
if (strstr(line, "rr-shared-syscallbuf")) { | ||
actual++; | ||
} | ||
free(line); | ||
} | ||
test_assert(expected == actual); | ||
} | ||
|
||
static int exec_proc(__attribute__((unused)) void* arg) { | ||
assert_syscallbuf_count(2); | ||
|
||
char memfd_name[PATH_MAX]; | ||
snprintf(memfd_name, sizeof(memfd_name), "/proc/self/fd/%d", shared_memfd); | ||
execl("/proc/self/exe", CHILD_PROCESS_NAME, memfd_name, NULL); | ||
|
||
test_assert("Not reached" && 0); | ||
return 0; | ||
} | ||
|
||
static void execd_child_proc(char *shared_memfd_mapping_path) { | ||
// Need to re-map the shared memory we're going to use to poke the parent process | ||
shared_memfd = open(shared_memfd_mapping_path, O_RDWR); | ||
test_assert(shared_memfd != -1); | ||
shared_memfd_mapping = mmap(NULL, SHARED_MEMFD_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, | ||
shared_memfd, 0); | ||
test_assert(shared_memfd_mapping != MAP_FAILED); | ||
|
||
// And do said poking | ||
shared_memfd_mapping[0] = 1; | ||
} | ||
|
||
|
||
int main(int argc, char **argv) { | ||
// This is what get exec'd from exec_proc | ||
if (argc == 2 && strcmp(argv[0], CHILD_PROCESS_NAME) == 0) { | ||
execd_child_proc(argv[1]); | ||
return 0; | ||
} | ||
|
||
int ret; | ||
|
||
// Before forking, there should only be one syscallbuf | ||
assert_syscallbuf_count(1); | ||
|
||
// mmap some memory that can be shared across execve(2) | ||
shared_memfd = memfd_create("shared_page", 0); | ||
test_assert(shared_memfd != -1); | ||
ret = ftruncate(shared_memfd, SHARED_MEMFD_SIZE); | ||
test_assert(ret != -1); | ||
shared_memfd_mapping = mmap(NULL, SHARED_MEMFD_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, | ||
shared_memfd, 0); | ||
test_assert(shared_memfd_mapping != MAP_FAILED); | ||
shared_memfd_mapping[0] = 0; | ||
|
||
// Spawn a process which shares our address space, and will execve(2) | ||
const size_t stack_size = 1 << 20; | ||
void* exec_proc_stack = mmap(NULL, stack_size, PROT_READ | PROT_WRITE, | ||
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | ||
test_assert(exec_proc_stack != MAP_FAILED); | ||
pid_t exec_proc_pid = clone(exec_proc, exec_proc_stack + stack_size, CLONE_VM | SIGCHLD, | ||
NULL, NULL, NULL, NULL); | ||
test_assert(exec_proc_pid != -1); | ||
|
||
// Now spin waiting for the exec'd process to set a bit in the shard mapping | ||
while (shared_memfd_mapping[0] != 1) { ; } | ||
|
||
// This means the child exec'd so we should have cleaned up the syscallbuf | ||
assert_syscallbuf_count(1); | ||
|
||
// Reap the exec'd child | ||
test_assert(exec_proc_pid == waitpid(exec_proc_pid, NULL, 0)); | ||
atomic_puts("EXIT-SUCCESS"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source `dirname $0`/util.sh | ||
skip_if_no_syscall_buf | ||
record $TESTNAME | ||
replay |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be a move, but it's fine.