Skip to content

Commit

Permalink
proc: bugfix: proc.Launch race (OS X)
Browse files Browse the repository at this point in the history
  • Loading branch information
aarzilli committed Nov 21, 2015
1 parent 5fd1bf4 commit 57f1ed3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
15 changes: 15 additions & 0 deletions proc/exec_darwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ fork_exec(char *argv0, char **argv, int size,
int fd[2];
if (pipe(fd) < 0) return -1;

// Create another pipe so that we know when we're about to exec. This ensures that control only returns
// back to Go-land when we call exec, effectively eliminating a race condition between launching the new
// process and trying to read its memory.
int wfd[2];
if (pipe(wfd) < 0) return -1;

kern_return_t kret;
pid_t pid = fork();
if (pid > 0) {
Expand All @@ -26,6 +32,11 @@ fork_exec(char *argv0, char **argv, int size,
char msg = 'c';
write(fd[1], &msg, 1);
close(fd[1]);

char w;
read(wfd[0], &w, 1);
close(wfd[0]);

return pid;
}

Expand Down Expand Up @@ -53,6 +64,10 @@ fork_exec(char *argv0, char **argv, int size,
pret = ptrace(PT_SIGEXC, 0, 0, 0);
if (pret != 0 && errno != 0) return -errno;

char msg = 'd';
write(wfd[1], &msg, 1);
close(wfd[1]);

// Create the child process.
execve(argv0, argv, environ);

Expand Down
4 changes: 4 additions & 0 deletions proc/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,10 @@ func (dbp *Process) getGoInformation() (ver GoVersion, isextld bool, err error)
err = fmt.Errorf("Could not determine version number: %v\n", err)
return
}
if vv.Unreadable != nil {
err = fmt.Errorf("Unreadable version number: %v\n", vv.Unreadable)
return
}

ver, ok := parseVersionString(constant.StringVal(vv.Value))
if !ok {
Expand Down
2 changes: 1 addition & 1 deletion proc/proc_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func Launch(cmd []string) (*Process, error) {
if err != nil {
return nil, err
}
err = dbp.Continue()
err = dbp.continueOnce()
return dbp, err
}

Expand Down

0 comments on commit 57f1ed3

Please sign in to comment.