Skip to content

Commit c9762b8

Browse files
committed
syscall: move uses of Syscall to libSystem on darwin
Miscellaneous additional conversions from raw syscalls to using their libc equivalent. Update #17490 Change-Id: If9ab22cc1d676c1f20fb161ebf02b0c28f71585d Reviewed-on: https://go-review.googlesource.com/c/148257 Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 5d6e8f3 commit c9762b8

14 files changed

+378
-75
lines changed

src/syscall/bpf_bsd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build darwin dragonfly freebsd netbsd openbsd
5+
// +build dragonfly freebsd netbsd openbsd
66

77
// Berkeley packet filter for BSD variants
88

src/syscall/bpf_darwin.go

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Berkeley packet filter for Darwin
6+
7+
package syscall
8+
9+
import (
10+
"unsafe"
11+
)
12+
13+
// Deprecated: Use golang.org/x/net/bpf instead.
14+
func BpfStmt(code, k int) *BpfInsn {
15+
return &BpfInsn{Code: uint16(code), K: uint32(k)}
16+
}
17+
18+
// Deprecated: Use golang.org/x/net/bpf instead.
19+
func BpfJump(code, k, jt, jf int) *BpfInsn {
20+
return &BpfInsn{Code: uint16(code), Jt: uint8(jt), Jf: uint8(jf), K: uint32(k)}
21+
}
22+
23+
// Deprecated: Use golang.org/x/net/bpf instead.
24+
func BpfBuflen(fd int) (int, error) {
25+
var l int
26+
err := ioctlPtr(fd, BIOCGBLEN, unsafe.Pointer(&l))
27+
if err != nil {
28+
return 0, err
29+
}
30+
return l, nil
31+
}
32+
33+
// Deprecated: Use golang.org/x/net/bpf instead.
34+
func SetBpfBuflen(fd, l int) (int, error) {
35+
err := ioctlPtr(fd, BIOCSBLEN, unsafe.Pointer(&l))
36+
if err != nil {
37+
return 0, err
38+
}
39+
return l, nil
40+
}
41+
42+
// Deprecated: Use golang.org/x/net/bpf instead.
43+
func BpfDatalink(fd int) (int, error) {
44+
var t int
45+
err := ioctlPtr(fd, BIOCGDLT, unsafe.Pointer(&t))
46+
if err != nil {
47+
return 0, err
48+
}
49+
return t, nil
50+
}
51+
52+
// Deprecated: Use golang.org/x/net/bpf instead.
53+
func SetBpfDatalink(fd, t int) (int, error) {
54+
err := ioctlPtr(fd, BIOCSDLT, unsafe.Pointer(&t))
55+
if err != nil {
56+
return 0, err
57+
}
58+
return t, nil
59+
}
60+
61+
// Deprecated: Use golang.org/x/net/bpf instead.
62+
func SetBpfPromisc(fd, m int) error {
63+
err := ioctlPtr(fd, BIOCPROMISC, unsafe.Pointer(&m))
64+
if err != nil {
65+
return err
66+
}
67+
return nil
68+
}
69+
70+
// Deprecated: Use golang.org/x/net/bpf instead.
71+
func FlushBpf(fd int) error {
72+
err := ioctlPtr(fd, BIOCFLUSH, nil)
73+
if err != nil {
74+
return err
75+
}
76+
return nil
77+
}
78+
79+
type ivalue struct {
80+
name [IFNAMSIZ]byte
81+
value int16
82+
}
83+
84+
// Deprecated: Use golang.org/x/net/bpf instead.
85+
func BpfInterface(fd int, name string) (string, error) {
86+
var iv ivalue
87+
err := ioctlPtr(fd, BIOCGETIF, unsafe.Pointer(&iv))
88+
if err != nil {
89+
return "", err
90+
}
91+
return name, nil
92+
}
93+
94+
// Deprecated: Use golang.org/x/net/bpf instead.
95+
func SetBpfInterface(fd int, name string) error {
96+
var iv ivalue
97+
copy(iv.name[:], []byte(name))
98+
err := ioctlPtr(fd, BIOCSETIF, unsafe.Pointer(&iv))
99+
if err != nil {
100+
return err
101+
}
102+
return nil
103+
}
104+
105+
// Deprecated: Use golang.org/x/net/bpf instead.
106+
func BpfTimeout(fd int) (*Timeval, error) {
107+
var tv Timeval
108+
err := ioctlPtr(fd, BIOCGRTIMEOUT, unsafe.Pointer(&tv))
109+
if err != nil {
110+
return nil, err
111+
}
112+
return &tv, nil
113+
}
114+
115+
// Deprecated: Use golang.org/x/net/bpf instead.
116+
func SetBpfTimeout(fd int, tv *Timeval) error {
117+
err := ioctlPtr(fd, BIOCSRTIMEOUT, unsafe.Pointer(tv))
118+
if err != nil {
119+
return err
120+
}
121+
return nil
122+
}
123+
124+
// Deprecated: Use golang.org/x/net/bpf instead.
125+
func BpfStats(fd int) (*BpfStat, error) {
126+
var s BpfStat
127+
err := ioctlPtr(fd, BIOCGSTATS, unsafe.Pointer(&s))
128+
if err != nil {
129+
return nil, err
130+
}
131+
return &s, nil
132+
}
133+
134+
// Deprecated: Use golang.org/x/net/bpf instead.
135+
func SetBpfImmediate(fd, m int) error {
136+
err := ioctlPtr(fd, BIOCIMMEDIATE, unsafe.Pointer(&m))
137+
if err != nil {
138+
return err
139+
}
140+
return nil
141+
}
142+
143+
// Deprecated: Use golang.org/x/net/bpf instead.
144+
func SetBpf(fd int, i []BpfInsn) error {
145+
var p BpfProgram
146+
p.Len = uint32(len(i))
147+
p.Insns = (*BpfInsn)(unsafe.Pointer(&i[0]))
148+
err := ioctlPtr(fd, BIOCSETF, unsafe.Pointer(&p))
149+
if err != nil {
150+
return err
151+
}
152+
return nil
153+
}
154+
155+
// Deprecated: Use golang.org/x/net/bpf instead.
156+
func CheckBpfVersion(fd int) error {
157+
var v BpfVersion
158+
err := ioctlPtr(fd, BIOCVERSION, unsafe.Pointer(&v))
159+
if err != nil {
160+
return err
161+
}
162+
if v.Major != BPF_MAJOR_VERSION || v.Minor != BPF_MINOR_VERSION {
163+
return EINVAL
164+
}
165+
return nil
166+
}
167+
168+
// Deprecated: Use golang.org/x/net/bpf instead.
169+
func BpfHeadercmpl(fd int) (int, error) {
170+
var f int
171+
err := ioctlPtr(fd, BIOCGHDRCMPLT, unsafe.Pointer(&f))
172+
if err != nil {
173+
return 0, err
174+
}
175+
return f, nil
176+
}
177+
178+
// Deprecated: Use golang.org/x/net/bpf instead.
179+
func SetBpfHeadercmpl(fd, f int) error {
180+
err := ioctlPtr(fd, BIOCSHDRCMPLT, unsafe.Pointer(&f))
181+
if err != nil {
182+
return err
183+
}
184+
return nil
185+
}

src/syscall/exec_unix.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ func runtime_AfterExec()
248248

249249
// execveLibc is non-nil on OS using libc syscall, set to execve in exec_libc.go; this
250250
// avoids a build dependency for other platforms.
251-
var execveLibc func(path uintptr, argv uintptr, envp uintptr) (err Errno)
251+
var execveLibc func(path uintptr, argv uintptr, envp uintptr) Errno
252+
var execveDarwin func(path *byte, argv **byte, envp **byte) error
252253

253254
// Exec invokes the execve(2) system call.
254255
func Exec(argv0 string, argv []string, envv []string) (err error) {
@@ -266,13 +267,16 @@ func Exec(argv0 string, argv []string, envv []string) (err error) {
266267
}
267268
runtime_BeforeExec()
268269

269-
var err1 Errno
270+
var err1 error
270271
if runtime.GOOS == "solaris" || runtime.GOOS == "aix" {
271272
// RawSyscall should never be used on Solaris or AIX.
272273
err1 = execveLibc(
273274
uintptr(unsafe.Pointer(argv0p)),
274275
uintptr(unsafe.Pointer(&argvp[0])),
275276
uintptr(unsafe.Pointer(&envvp[0])))
277+
} else if runtime.GOOS == "darwin" {
278+
// Similarly on Darwin.
279+
err1 = execveDarwin(argv0p, &argvp[0], &envvp[0])
276280
} else {
277281
_, _, err1 = RawSyscall(SYS_EXECVE,
278282
uintptr(unsafe.Pointer(argv0p)),

src/syscall/flock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build linux darwin freebsd openbsd netbsd dragonfly
1+
// +build linux freebsd openbsd netbsd dragonfly
22

33
// Copyright 2014 The Go Authors. All rights reserved.
44
// Use of this source code is governed by a BSD-style

src/syscall/flock_darwin.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package syscall
6+
7+
import "unsafe"
8+
9+
// FcntlFlock performs a fcntl syscall for the F_GETLK, F_SETLK or F_SETLKW command.
10+
func FcntlFlock(fd uintptr, cmd int, lk *Flock_t) error {
11+
_, err := fcntlPtr(int(fd), cmd, unsafe.Pointer(lk))
12+
return err
13+
}

src/syscall/mkasm_darwin.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ func main() {
3333
}
3434
in := string(in1) + string(in2) + string(in3)
3535

36+
trampolines := map[string]bool{}
37+
3638
var out bytes.Buffer
3739

3840
fmt.Fprintf(&out, "// go run mkasm_darwin.go %s\n", strings.Join(os.Args[1:], " "))
@@ -43,8 +45,11 @@ func main() {
4345
continue
4446
}
4547
fn := line[5 : len(line)-13]
46-
fmt.Fprintf(&out, "TEXT ·%s_trampoline(SB),NOSPLIT,$0-0\n", fn)
47-
fmt.Fprintf(&out, "\tJMP\t%s(SB)\n", fn)
48+
if !trampolines[fn] {
49+
trampolines[fn] = true
50+
fmt.Fprintf(&out, "TEXT ·%s_trampoline(SB),NOSPLIT,$0-0\n", fn)
51+
fmt.Fprintf(&out, "\tJMP\t%s(SB)\n", fn)
52+
}
4853
}
4954
err = ioutil.WriteFile(fmt.Sprintf("zsyscall_darwin_%s.s", arch), out.Bytes(), 0644)
5055
if err != nil {

src/syscall/mksyscall.pl

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ ($)
9999
return ($1, $2);
100100
}
101101

102+
# set of trampolines we've already generated
103+
my %trampolines;
104+
102105
my $text = "";
103106
while(<>) {
104107
chomp;
@@ -338,14 +341,17 @@ ($)
338341
$text .= "\treturn\n";
339342
$text .= "}\n\n";
340343
if($darwin) {
341-
# The assembly trampoline that jumps to the libc routine.
342-
$text .= "func ${funcname}_trampoline()\n";
343-
# Map syscall.funcname to just plain funcname.
344-
# (The jump to this function is in the assembly trampoline, generated by mksyscallasm_darwin.go.)
345-
$text .= "//go:linkname $funcname $funcname\n";
346-
# Tell the linker that funcname can be found in libSystem using varname without the libc_ prefix.
347-
my $basename = substr $funcname, 5;
348-
$text .= "//go:cgo_import_dynamic $funcname $basename \"/usr/lib/libSystem.B.dylib\"\n";
344+
if (not exists $trampolines{$funcname}) {
345+
$trampolines{$funcname} = 1;
346+
# The assembly trampoline that jumps to the libc routine.
347+
$text .= "func ${funcname}_trampoline()\n";
348+
# Map syscall.funcname to just plain funcname.
349+
# (The jump to this function is in the assembly trampoline, generated by mksyscallasm_darwin.go.)
350+
$text .= "//go:linkname $funcname $funcname\n";
351+
# Tell the linker that funcname can be found in libSystem using varname without the libc_ prefix.
352+
my $basename = substr $funcname, 5;
353+
$text .= "//go:cgo_import_dynamic $funcname $basename \"/usr/lib/libSystem.B.dylib\"\n";
354+
}
349355
}
350356
}
351357

src/syscall/syscall_darwin.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,15 @@ func Kill(pid int, signum Signal) (err error) { return kill(pid, int(signum), 1)
339339
//sys munmap(addr uintptr, length uintptr) (err error)
340340
//sysnb fork() (pid int, err error)
341341
//sysnb ioctl(fd int, req int, arg int) (err error)
342-
//sysnb execve(path *byte, argv *byte, envp *byte) (err error)
342+
//sysnb ioctlPtr(fd int, req uint, arg unsafe.Pointer) (err error) = SYS_ioctl
343+
//sysnb execve(path *byte, argv **byte, envp **byte) (err error)
343344
//sysnb exit(res int) (err error)
344345
//sys sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error)
346+
//sys fcntlPtr(fd int, cmd int, arg unsafe.Pointer) (val int, err error) = SYS_fcntl
347+
348+
func init() {
349+
execveDarwin = execve
350+
}
345351

346352
func readlen(fd int, buf *byte, nbuf int) (n int, err error) {
347353
r0, _, e1 := syscall(funcPC(libc_read_trampoline), uintptr(fd), uintptr(unsafe.Pointer(buf)), uintptr(nbuf))

src/syscall/zsyscall_darwin_386.go

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

0 commit comments

Comments
 (0)