Skip to content

Commit 0a0de0f

Browse files
rscgopherbot
authored andcommitted
runtime: revert use of __fork to work around Apple atfork bugs
An Apple engineer suggests that since __fork is not public API, it would be better to use a different fix. With the benefit of source code, they suggest using xpc_date_create_from_current instead of xpc_atfork_child. The latter sets some flags that disable certain functionality for the remainder of the process lifetime (expecting exec), while the former should do the necessary setup. Reverting the __fork fix in order to prepare a clean fix based on CL 451735 using xpc_date_create_from_current. This reverts commit c61d322. Change-Id: I2da293ff537237ffd2d40ad756d827c95c84635b Reviewed-on: https://go-review.googlesource.com/c/go/+/460475 Auto-Submit: Russ Cox <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Russ Cox <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent 82f09b7 commit 0a0de0f

File tree

7 files changed

+17
-81
lines changed

7 files changed

+17
-81
lines changed

src/syscall/exec_libc2.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
7878
// About to call fork.
7979
// No more allocation or calls of non-assembly functions.
8080
runtime_BeforeFork()
81-
r1, _, err1 = rawSyscall(forkTrampoline, 0, 0, 0)
81+
r1, _, err1 = rawSyscall(abi.FuncPCABI0(libc_fork_trampoline), 0, 0, 0)
8282
if err1 != 0 {
8383
runtime_AfterFork()
8484
return 0, err1
@@ -276,6 +276,6 @@ childerror:
276276
// send error code on pipe
277277
rawSyscall(abi.FuncPCABI0(libc_write_trampoline), uintptr(pipe), uintptr(unsafe.Pointer(&err1)), unsafe.Sizeof(err1))
278278
for {
279-
rawSyscall(exitTrampoline, 253, 0, 0)
279+
rawSyscall(abi.FuncPCABI0(libc_exit_trampoline), 253, 0, 0)
280280
}
281281
}

src/syscall/syscall_darwin.go

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,7 @@ func Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
2222
func RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
2323
func RawSyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
2424

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

5427
type SockaddrDatalink struct {
5528
Len uint8
@@ -237,12 +210,11 @@ func Kill(pid int, signum Signal) (err error) { return kill(pid, int(signum), 1)
237210
//sys writev(fd int, iovecs []Iovec) (cnt uintptr, err error)
238211
//sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
239212
//sys munmap(addr uintptr, length uintptr) (err error)
240-
//sysnb __fork() (pid int, err error)
213+
//sysnb fork() (pid int, err error)
241214
//sysnb ioctl(fd int, req int, arg int) (err error)
242215
//sysnb ioctlPtr(fd int, req uint, arg unsafe.Pointer) (err error) = SYS_ioctl
243216
//sysnb execve(path *byte, argv **byte, envp **byte) (err error)
244217
//sysnb exit(res int) (err error)
245-
//sysnb __exit(res int) (err error)
246218
//sys sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error)
247219
//sys fcntlPtr(fd int, cmd int, arg unsafe.Pointer) (val int, err error) = SYS_fcntl
248220
//sys unlinkat(fd int, path string, flags int) (err error)

src/syscall/syscall_openbsd_libc.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ import (
1010
"internal/abi"
1111
)
1212

13-
var (
14-
dupTrampoline = abi.FuncPCABI0(libc_dup3_trampoline)
15-
exitTrampoline = abi.FuncPCABI0(libc_exit_trampoline)
16-
forkTrampoline = abi.FuncPCABI0(libc_fork_trampoline)
17-
)
13+
var dupTrampoline = abi.FuncPCABI0(libc_dup3_trampoline)
1814

1915
func init() {
2016
execveOpenBSD = execve

src/syscall/zsyscall_darwin_amd64.go

Lines changed: 4 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/syscall/zsyscall_darwin_amd64.s

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,14 @@ TEXT ·libc_mmap_trampoline(SB),NOSPLIT,$0-0
221221
JMP libc_mmap(SB)
222222
TEXT ·libc_munmap_trampoline(SB),NOSPLIT,$0-0
223223
JMP libc_munmap(SB)
224-
TEXT ·libc___fork_trampoline(SB),NOSPLIT,$0-0
225-
JMP libc___fork(SB)
224+
TEXT ·libc_fork_trampoline(SB),NOSPLIT,$0-0
225+
JMP libc_fork(SB)
226226
TEXT ·libc_ioctl_trampoline(SB),NOSPLIT,$0-0
227227
JMP libc_ioctl(SB)
228228
TEXT ·libc_execve_trampoline(SB),NOSPLIT,$0-0
229229
JMP libc_execve(SB)
230230
TEXT ·libc_exit_trampoline(SB),NOSPLIT,$0-0
231231
JMP libc_exit(SB)
232-
TEXT ·libc___exit_trampoline(SB),NOSPLIT,$0-0
233-
JMP libc___exit(SB)
234232
TEXT ·libc_sysctl_trampoline(SB),NOSPLIT,$0-0
235233
JMP libc_sysctl(SB)
236234
TEXT ·libc_unlinkat_trampoline(SB),NOSPLIT,$0-0

src/syscall/zsyscall_darwin_arm64.go

Lines changed: 4 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/syscall/zsyscall_darwin_arm64.s

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,14 @@ TEXT ·libc_mmap_trampoline(SB),NOSPLIT,$0-0
221221
JMP libc_mmap(SB)
222222
TEXT ·libc_munmap_trampoline(SB),NOSPLIT,$0-0
223223
JMP libc_munmap(SB)
224-
TEXT ·libc___fork_trampoline(SB),NOSPLIT,$0-0
225-
JMP libc___fork(SB)
224+
TEXT ·libc_fork_trampoline(SB),NOSPLIT,$0-0
225+
JMP libc_fork(SB)
226226
TEXT ·libc_ioctl_trampoline(SB),NOSPLIT,$0-0
227227
JMP libc_ioctl(SB)
228228
TEXT ·libc_execve_trampoline(SB),NOSPLIT,$0-0
229229
JMP libc_execve(SB)
230230
TEXT ·libc_exit_trampoline(SB),NOSPLIT,$0-0
231231
JMP libc_exit(SB)
232-
TEXT ·libc___exit_trampoline(SB),NOSPLIT,$0-0
233-
JMP libc___exit(SB)
234232
TEXT ·libc_sysctl_trampoline(SB),NOSPLIT,$0-0
235233
JMP libc_sysctl(SB)
236234
TEXT ·libc_unlinkat_trampoline(SB),NOSPLIT,$0-0

0 commit comments

Comments
 (0)