Skip to content

Commit

Permalink
cmd/link/internal/ld, syscall: use libc based msync on darwin for Go …
Browse files Browse the repository at this point in the history
…≥ 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>
  • Loading branch information
tklauser authored and gopherbot committed Sep 13, 2022
1 parent 8df21a7 commit d36466f
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/cmd/link/internal/ld/msync_darwin_libc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build darwin && go1.20

package ld

import _ "unsafe" // for go:linkname

//go:linkname msync syscall.msync
func msync(b []byte, flags int) (err error)
24 changes: 24 additions & 0 deletions src/cmd/link/internal/ld/msync_darwin_syscall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build darwin && !go1.20

package ld

import (
"syscall"
"unsafe"
)

func msync(b []byte, flags int) (err error) {
var p unsafe.Pointer
if len(b) > 0 {
p = unsafe.Pointer(&b[0])
}
_, _, errno := syscall.Syscall(syscall.SYS_MSYNC, uintptr(p), uintptr(len(b)), uintptr(flags))
if errno != 0 {
return errno
}
return nil
}
2 changes: 1 addition & 1 deletion src/cmd/link/internal/ld/outbuf_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ func (out *OutBuf) purgeSignatureCache() {
// When we mmap the output buffer, it doesn't have a code signature
// (as we haven't generated one). Invalidate the kernel cache now that
// we have generated the signature. See issue #42684.
syscall.Syscall(syscall.SYS_MSYNC, uintptr(unsafe.Pointer(&out.buf[0])), uintptr(len(out.buf)), syscall.MS_INVALIDATE)
msync(out.buf, syscall.MS_INVALIDATE)
// Best effort. Ignore error.
}
1 change: 1 addition & 0 deletions src/syscall/syscall_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func Kill(pid int, signum Signal) (err error) { return kill(pid, int(signum), 1)
//sys Mlock(b []byte) (err error)
//sys Mlockall(flags int) (err error)
//sys Mprotect(b []byte, prot int) (err error)
//sys msync(b []byte, flags int) (err error)
//sys Munlock(b []byte) (err error)
//sys Munlockall() (err error)
//sys Open(path string, mode int, perm uint32) (fd int, err error)
Expand Down
20 changes: 20 additions & 0 deletions src/syscall/zsyscall_darwin_amd64.go

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

2 changes: 2 additions & 0 deletions src/syscall/zsyscall_darwin_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ TEXT ·libc_mlockall_trampoline(SB),NOSPLIT,$0-0
JMP libc_mlockall(SB)
TEXT ·libc_mprotect_trampoline(SB),NOSPLIT,$0-0
JMP libc_mprotect(SB)
TEXT ·libc_msync_trampoline(SB),NOSPLIT,$0-0
JMP libc_msync(SB)
TEXT ·libc_munlock_trampoline(SB),NOSPLIT,$0-0
JMP libc_munlock(SB)
TEXT ·libc_munlockall_trampoline(SB),NOSPLIT,$0-0
Expand Down
20 changes: 20 additions & 0 deletions src/syscall/zsyscall_darwin_arm64.go

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

2 changes: 2 additions & 0 deletions src/syscall/zsyscall_darwin_arm64.s
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ TEXT ·libc_mlockall_trampoline(SB),NOSPLIT,$0-0
JMP libc_mlockall(SB)
TEXT ·libc_mprotect_trampoline(SB),NOSPLIT,$0-0
JMP libc_mprotect(SB)
TEXT ·libc_msync_trampoline(SB),NOSPLIT,$0-0
JMP libc_msync(SB)
TEXT ·libc_munlock_trampoline(SB),NOSPLIT,$0-0
JMP libc_munlock(SB)
TEXT ·libc_munlockall_trampoline(SB),NOSPLIT,$0-0
Expand Down

0 comments on commit d36466f

Please sign in to comment.