Skip to content

Commit d36466f

Browse files
tklausergopherbot
authored andcommitted
cmd/link/internal/ld, syscall: use libc based msync on darwin for Go ≥ 1.20
Direct syscalls should no longer be used on darwin. Instead, directly call libc's msync when using Go ≥ 1.20 for bootstrap. For #54265 Change-Id: Ie3f1e6ccd1a06e7f0ddd88cdef5067393a69e8db Reviewed-on: https://go-review.googlesource.com/c/go/+/430336 Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
1 parent 8df21a7 commit d36466f

8 files changed

+82
-1
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2022 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+
//go:build darwin && go1.20
6+
7+
package ld
8+
9+
import _ "unsafe" // for go:linkname
10+
11+
//go:linkname msync syscall.msync
12+
func msync(b []byte, flags int) (err error)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2022 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+
//go:build darwin && !go1.20
6+
7+
package ld
8+
9+
import (
10+
"syscall"
11+
"unsafe"
12+
)
13+
14+
func msync(b []byte, flags int) (err error) {
15+
var p unsafe.Pointer
16+
if len(b) > 0 {
17+
p = unsafe.Pointer(&b[0])
18+
}
19+
_, _, errno := syscall.Syscall(syscall.SYS_MSYNC, uintptr(p), uintptr(len(b)), uintptr(flags))
20+
if errno != 0 {
21+
return errno
22+
}
23+
return nil
24+
}

src/cmd/link/internal/ld/outbuf_darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ func (out *OutBuf) purgeSignatureCache() {
4343
// When we mmap the output buffer, it doesn't have a code signature
4444
// (as we haven't generated one). Invalidate the kernel cache now that
4545
// we have generated the signature. See issue #42684.
46-
syscall.Syscall(syscall.SYS_MSYNC, uintptr(unsafe.Pointer(&out.buf[0])), uintptr(len(out.buf)), syscall.MS_INVALIDATE)
46+
msync(out.buf, syscall.MS_INVALIDATE)
4747
// Best effort. Ignore error.
4848
}

src/syscall/syscall_darwin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ func Kill(pid int, signum Signal) (err error) { return kill(pid, int(signum), 1)
171171
//sys Mlock(b []byte) (err error)
172172
//sys Mlockall(flags int) (err error)
173173
//sys Mprotect(b []byte, prot int) (err error)
174+
//sys msync(b []byte, flags int) (err error)
174175
//sys Munlock(b []byte) (err error)
175176
//sys Munlockall() (err error)
176177
//sys Open(path string, mode int, perm uint32) (fd int, err error)

src/syscall/zsyscall_darwin_amd64.go

Lines changed: 20 additions & 0 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ TEXT ·libc_mlockall_trampoline(SB),NOSPLIT,$0-0
143143
JMP libc_mlockall(SB)
144144
TEXT ·libc_mprotect_trampoline(SB),NOSPLIT,$0-0
145145
JMP libc_mprotect(SB)
146+
TEXT ·libc_msync_trampoline(SB),NOSPLIT,$0-0
147+
JMP libc_msync(SB)
146148
TEXT ·libc_munlock_trampoline(SB),NOSPLIT,$0-0
147149
JMP libc_munlock(SB)
148150
TEXT ·libc_munlockall_trampoline(SB),NOSPLIT,$0-0

src/syscall/zsyscall_darwin_arm64.go

Lines changed: 20 additions & 0 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ TEXT ·libc_mlockall_trampoline(SB),NOSPLIT,$0-0
143143
JMP libc_mlockall(SB)
144144
TEXT ·libc_mprotect_trampoline(SB),NOSPLIT,$0-0
145145
JMP libc_mprotect(SB)
146+
TEXT ·libc_msync_trampoline(SB),NOSPLIT,$0-0
147+
JMP libc_msync(SB)
146148
TEXT ·libc_munlock_trampoline(SB),NOSPLIT,$0-0
147149
JMP libc_munlock(SB)
148150
TEXT ·libc_munlockall_trampoline(SB),NOSPLIT,$0-0

0 commit comments

Comments
 (0)