Skip to content

Commit e216ee7

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
syscall: clean up variable declarations in forkAndExecInChild
The various forkAndExecInChild implementations have comments explaining that they pre-declare variables to force allocations to occur before forking, but then later use ":=" declarations for additional variables. To make it clearer that those ":=" declarations do not allocate, we move their declarations up to the predeclared blocks. For #57208. Change-Id: Ie8cb577fa7180b51b64d6dc398169053fdf8ea97 Reviewed-on: https://go-review.googlesource.com/c/go/+/456516 Auto-Submit: Bryan Mills <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 01636cf commit e216ee7

File tree

6 files changed

+98
-71
lines changed

6 files changed

+98
-71
lines changed

src/syscall/exec_bsd.go

+11-8
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,13 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
5555
// Declare all variables at top in case any
5656
// declarations require heap allocation (e.g., err1).
5757
var (
58-
r1 uintptr
59-
err1 Errno
60-
nextfd int
61-
i int
58+
r1 uintptr
59+
err1 Errno
60+
nextfd int
61+
i int
62+
pgrp _C_int
63+
cred *Credential
64+
ngroups, groups uintptr
6265
)
6366

6467
// guard against side effects of shuffling fds below.
@@ -119,7 +122,7 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
119122
if sys.Foreground {
120123
// This should really be pid_t, however _C_int (aka int32) is
121124
// generally equivalent.
122-
pgrp := _C_int(sys.Pgid)
125+
pgrp = _C_int(sys.Pgid)
123126
if pgrp == 0 {
124127
r1, _, err1 = RawSyscall(SYS_GETPID, 0, 0, 0)
125128
if err1 != 0 {
@@ -149,9 +152,9 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
149152
}
150153

151154
// User and groups
152-
if cred := sys.Credential; cred != nil {
153-
ngroups := uintptr(len(cred.Groups))
154-
groups := uintptr(0)
155+
if cred = sys.Credential; cred != nil {
156+
ngroups = uintptr(len(cred.Groups))
157+
groups = uintptr(0)
155158
if ngroups > 0 {
156159
groups = uintptr(unsafe.Pointer(&cred.Groups[0]))
157160
}

src/syscall/exec_freebsd.go

+14-10
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,14 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
6060
// Declare all variables at top in case any
6161
// declarations require heap allocation (e.g., err1).
6262
var (
63-
r1 uintptr
64-
err1 Errno
65-
nextfd int
66-
i int
63+
r1 uintptr
64+
err1 Errno
65+
nextfd int
66+
i int
67+
pgrp _C_int
68+
cred *Credential
69+
ngroups, groups uintptr
70+
upid uintptr
6771
)
6872

6973
// Record parent PID so child can test if it has died.
@@ -127,7 +131,7 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
127131
if sys.Foreground {
128132
// This should really be pid_t, however _C_int (aka int32) is
129133
// generally equivalent.
130-
pgrp := _C_int(sys.Pgid)
134+
pgrp = _C_int(sys.Pgid)
131135
if pgrp == 0 {
132136
r1, _, err1 = RawSyscall(SYS_GETPID, 0, 0, 0)
133137
if err1 != 0 {
@@ -157,9 +161,9 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
157161
}
158162

159163
// User and groups
160-
if cred := sys.Credential; cred != nil {
161-
ngroups := uintptr(len(cred.Groups))
162-
groups := uintptr(0)
164+
if cred = sys.Credential; cred != nil {
165+
ngroups = uintptr(len(cred.Groups))
166+
groups = uintptr(0)
163167
if ngroups > 0 {
164168
groups = uintptr(unsafe.Pointer(&cred.Groups[0]))
165169
}
@@ -204,8 +208,8 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
204208
// using SIGKILL.
205209
r1, _, _ = RawSyscall(SYS_GETPPID, 0, 0, 0)
206210
if r1 != ppid {
207-
pid, _, _ := RawSyscall(SYS_GETPID, 0, 0, 0)
208-
_, _, err1 = RawSyscall(SYS_KILL, pid, uintptr(sys.Pdeathsig), 0)
211+
upid, _, _ = RawSyscall(SYS_GETPID, 0, 0, 0)
212+
_, _, err1 = RawSyscall(SYS_KILL, upid, uintptr(sys.Pdeathsig), 0)
209213
if err1 != 0 {
210214
goto childerror
211215
}

src/syscall/exec_libc.go

+11-8
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,13 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
8181
// Declare all variables at top in case any
8282
// declarations require heap allocation (e.g., err1).
8383
var (
84-
r1 uintptr
85-
err1 Errno
86-
nextfd int
87-
i int
84+
r1 uintptr
85+
err1 Errno
86+
nextfd int
87+
i int
88+
pgrp _Pid_t
89+
cred *Credential
90+
ngroups, groups uintptr
8891
)
8992

9093
// guard against side effects of shuffling fds below.
@@ -135,7 +138,7 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
135138
}
136139

137140
if sys.Foreground {
138-
pgrp := _Pid_t(sys.Pgid)
141+
pgrp = _Pid_t(sys.Pgid)
139142
if pgrp == 0 {
140143
r1, err1 = getpid()
141144
if err1 != 0 {
@@ -165,9 +168,9 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
165168
}
166169

167170
// User and groups
168-
if cred := sys.Credential; cred != nil {
169-
ngroups := uintptr(len(cred.Groups))
170-
groups := uintptr(0)
171+
if cred = sys.Credential; cred != nil {
172+
ngroups = uintptr(len(cred.Groups))
173+
groups = uintptr(0)
171174
if ngroups > 0 {
172175
groups = uintptr(unsafe.Pointer(&cred.Groups[0]))
173176
}

src/syscall/exec_libc2.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,17 @@ func runtime_AfterForkInChild()
5252
// functions that do not grow the stack.
5353
//
5454
//go:norace
55-
func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr *ProcAttr, sys *SysProcAttr, pipe int) (pid int, err Errno) {
55+
func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr *ProcAttr, sys *SysProcAttr, pipe int) (pid int, err1 Errno) {
5656
// Declare all variables at top in case any
5757
// declarations require heap allocation (e.g., err1).
5858
var (
59-
r1 uintptr
60-
err1 Errno
61-
nextfd int
62-
i int
59+
r1 uintptr
60+
nextfd int
61+
i int
62+
err error
63+
pgrp _C_int
64+
cred *Credential
65+
ngroups, groups uintptr
6366
)
6467

6568
// guard against side effects of shuffling fds below.
@@ -94,7 +97,7 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
9497

9598
// Enable tracing if requested.
9699
if sys.Ptrace {
97-
if err := ptrace(PTRACE_TRACEME, 0, 0, 0); err != nil {
100+
if err = ptrace(PTRACE_TRACEME, 0, 0, 0); err != nil {
98101
err1 = err.(Errno)
99102
goto childerror
100103
}
@@ -120,7 +123,7 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
120123
if sys.Foreground {
121124
// This should really be pid_t, however _C_int (aka int32) is
122125
// generally equivalent.
123-
pgrp := _C_int(sys.Pgid)
126+
pgrp = _C_int(sys.Pgid)
124127
if pgrp == 0 {
125128
r1, _, err1 = rawSyscall(abi.FuncPCABI0(libc_getpid_trampoline), 0, 0, 0)
126129
if err1 != 0 {
@@ -149,9 +152,9 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
149152
}
150153

151154
// User and groups
152-
if cred := sys.Credential; cred != nil {
153-
ngroups := uintptr(len(cred.Groups))
154-
groups := uintptr(0)
155+
if cred = sys.Credential; cred != nil {
156+
ngroups = uintptr(len(cred.Groups))
157+
groups = uintptr(0)
155158
if ngroups > 0 {
156159
groups = uintptr(unsafe.Pointer(&cred.Groups[0]))
157160
}

0 commit comments

Comments
 (0)