Skip to content

Commit

Permalink
[release-branch.go1.19] runtime: revert "call __fork instead of fork …
Browse files Browse the repository at this point in the history
…on darwin"

A recent comment on #57263 reports an unexplained crash in a cgo program
that is fixed by reverting the __fork fix. We don't have any viable fix for the
os/exec bug at this point, so give up on a fix for the January point releases.

This reverts CL 459178 (commit 91bc4cd).

Fixes #57690.

Change-Id: Ieb38d9bc7f967e9a726429eab2ea515d5ca0847f
Reviewed-on: https://go-review.googlesource.com/c/go/+/461115
Run-TryBot: Russ Cox <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Cherry Mui <[email protected]>
  • Loading branch information
rsc committed Jan 9, 2023
1 parent c8104a1 commit 7d06dd6
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 81 deletions.
4 changes: 2 additions & 2 deletions src/syscall/exec_libc2.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
// About to call fork.
// No more allocation or calls of non-assembly functions.
runtime_BeforeFork()
r1, _, err1 = rawSyscall(forkTrampoline, 0, 0, 0)
r1, _, err1 = rawSyscall(abi.FuncPCABI0(libc_fork_trampoline), 0, 0, 0)
if err1 != 0 {
runtime_AfterFork()
return 0, err1
Expand Down Expand Up @@ -276,6 +276,6 @@ childerror:
// send error code on pipe
rawSyscall(abi.FuncPCABI0(libc_write_trampoline), uintptr(pipe), uintptr(unsafe.Pointer(&err1)), unsafe.Sizeof(err1))
for {
rawSyscall(exitTrampoline, 253, 0, 0)
rawSyscall(abi.FuncPCABI0(libc_exit_trampoline), 253, 0, 0)
}
}
32 changes: 2 additions & 30 deletions src/syscall/syscall_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,7 @@ func Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
func RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
func RawSyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)

// These are called from exec_libc2.go in the child of fork.
// The names differ between macOS and OpenBSD, so we need
// to declare the specific ones used here to keep the exec_libc2.go
// code portable.
//
// We use __fork and __exit, not fork and exit, to avoid the libc atfork
// and atexit handlers. The atfork handlers have caused fork child
// hangs in the past (see #33565, #56784). The atexit handlers have
// not, but the non-libc ports all invoke the system call, so doing
// the same here makes sense. In general we wouldn't expect
// atexit handlers to work terribly well in a fork child anyway.
// (Also, perhaps the atfork handlers clear the atexit handlers,
// in which case we definitely need to avoid calling the libc exit
// if we bypass the libc fork.)
//
// Other calls that are made in the child after the fork are
// ptrace, setsid, setpgid, getpid, ioctl, chroot, setgroups,
// setgid, setuid, chdir, dup2, fcntl, close, execve, and write.
// Those are all simple kernel wrappers that should be safe
// to be called directly. The fcntl and ioctl functions do run
// some code around the kernel call, but they don't call any
// other functions, so for now we keep using them instead of
// calling the lower-level __fcntl and __ioctl functions.
var (
dupTrampoline = abi.FuncPCABI0(libc_dup2_trampoline)
exitTrampoline = abi.FuncPCABI0(libc___exit_trampoline)
forkTrampoline = abi.FuncPCABI0(libc___fork_trampoline)
)
var dupTrampoline = abi.FuncPCABI0(libc_dup2_trampoline)

type SockaddrDatalink struct {
Len uint8
Expand Down Expand Up @@ -236,12 +209,11 @@ func Kill(pid int, signum Signal) (err error) { return kill(pid, int(signum), 1)
//sys writev(fd int, iovecs []Iovec) (cnt uintptr, err error)
//sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
//sys munmap(addr uintptr, length uintptr) (err error)
//sysnb __fork() (pid int, err error)
//sysnb fork() (pid int, err error)
//sysnb ioctl(fd int, req int, arg int) (err error)
//sysnb ioctlPtr(fd int, req uint, arg unsafe.Pointer) (err error) = SYS_ioctl
//sysnb execve(path *byte, argv **byte, envp **byte) (err error)
//sysnb exit(res int) (err error)
//sysnb __exit(res int) (err error)
//sys sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error)
//sys fcntlPtr(fd int, cmd int, arg unsafe.Pointer) (val int, err error) = SYS_fcntl
//sys unlinkat(fd int, path string, flags int) (err error)
Expand Down
6 changes: 1 addition & 5 deletions src/syscall/syscall_openbsd_libc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ import (
"internal/abi"
)

var (
dupTrampoline = abi.FuncPCABI0(libc_dup3_trampoline)
exitTrampoline = abi.FuncPCABI0(libc_exit_trampoline)
forkTrampoline = abi.FuncPCABI0(libc_fork_trampoline)
)
var dupTrampoline = abi.FuncPCABI0(libc_dup3_trampoline)

func init() {
execveOpenBSD = execve
Expand Down
22 changes: 4 additions & 18 deletions src/syscall/zsyscall_darwin_amd64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions src/syscall/zsyscall_darwin_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,14 @@ TEXT ·libc_mmap_trampoline(SB),NOSPLIT,$0-0
JMP libc_mmap(SB)
TEXT ·libc_munmap_trampoline(SB),NOSPLIT,$0-0
JMP libc_munmap(SB)
TEXT ·libc___fork_trampoline(SB),NOSPLIT,$0-0
JMP libc___fork(SB)
TEXT ·libc_fork_trampoline(SB),NOSPLIT,$0-0
JMP libc_fork(SB)
TEXT ·libc_ioctl_trampoline(SB),NOSPLIT,$0-0
JMP libc_ioctl(SB)
TEXT ·libc_execve_trampoline(SB),NOSPLIT,$0-0
JMP libc_execve(SB)
TEXT ·libc_exit_trampoline(SB),NOSPLIT,$0-0
JMP libc_exit(SB)
TEXT ·libc___exit_trampoline(SB),NOSPLIT,$0-0
JMP libc___exit(SB)
TEXT ·libc_sysctl_trampoline(SB),NOSPLIT,$0-0
JMP libc_sysctl(SB)
TEXT ·libc_unlinkat_trampoline(SB),NOSPLIT,$0-0
Expand Down
22 changes: 4 additions & 18 deletions src/syscall/zsyscall_darwin_arm64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions src/syscall/zsyscall_darwin_arm64.s
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,14 @@ TEXT ·libc_mmap_trampoline(SB),NOSPLIT,$0-0
JMP libc_mmap(SB)
TEXT ·libc_munmap_trampoline(SB),NOSPLIT,$0-0
JMP libc_munmap(SB)
TEXT ·libc___fork_trampoline(SB),NOSPLIT,$0-0
JMP libc___fork(SB)
TEXT ·libc_fork_trampoline(SB),NOSPLIT,$0-0
JMP libc_fork(SB)
TEXT ·libc_ioctl_trampoline(SB),NOSPLIT,$0-0
JMP libc_ioctl(SB)
TEXT ·libc_execve_trampoline(SB),NOSPLIT,$0-0
JMP libc_execve(SB)
TEXT ·libc_exit_trampoline(SB),NOSPLIT,$0-0
JMP libc_exit(SB)
TEXT ·libc___exit_trampoline(SB),NOSPLIT,$0-0
JMP libc___exit(SB)
TEXT ·libc_sysctl_trampoline(SB),NOSPLIT,$0-0
JMP libc_sysctl(SB)
TEXT ·libc_unlinkat_trampoline(SB),NOSPLIT,$0-0
Expand Down

0 comments on commit 7d06dd6

Please sign in to comment.