]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: invalidate kernel cache on darwin
authorCherry Zhang <cherryyz@google.com>
Sun, 22 Nov 2020 16:43:29 +0000 (11:43 -0500)
committerCherry Zhang <cherryyz@google.com>
Tue, 1 Dec 2020 23:39:15 +0000 (23:39 +0000)
Apparently, the darwin kernel may cache the code signature at
mmap. When we mmap the output buffer, it doesn't have a code
signature (as we haven't generated one). Invalidate the kernel
cache after writing the file.

See https://github.com/golang/go/issues/42684#issuecomment-731704900
for more information.

Updates #38485.
Fixes #42684.

Change-Id: Iac2ef756ca1454c856944423e5040b8e17a6b420
Reviewed-on: https://go-review.googlesource.com/c/go/+/272258
Trust: Cherry Zhang <cherryyz@google.com>
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
src/cmd/link/internal/ld/outbuf.go
src/cmd/link/internal/ld/outbuf_darwin.go
src/cmd/link/internal/ld/outbuf_notdarwin.go [new file with mode: 0644]

index 6cae064679da2ba17c74f0e5023ca43233a2c456..530836ef7cf292fb7849089be5d501cfb574d3e6 100644 (file)
@@ -113,6 +113,7 @@ func (out *OutBuf) Close() error {
        }
        if out.isMmapped() {
                out.copyHeap()
+               out.purgeSignatureCache()
                out.munmap()
        }
        if out.f == nil {
index d7e3372230b0dc682e760894c06c65a0cd662c70..9444b6567e8b723b03560154e1271f65ae3c4907 100644 (file)
@@ -36,3 +36,12 @@ func (out *OutBuf) fallocate(size uint64) error {
 
        return nil
 }
+
+func (out *OutBuf) purgeSignatureCache() {
+       // Apparently, the Darwin kernel may cache the code signature at mmap.
+       // 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)
+       // Best effort. Ignore error.
+}
diff --git a/src/cmd/link/internal/ld/outbuf_notdarwin.go b/src/cmd/link/internal/ld/outbuf_notdarwin.go
new file mode 100644 (file)
index 0000000..8c5666f
--- /dev/null
@@ -0,0 +1,9 @@
+// Copyright 2020 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.
+
+// +build !darwin
+
+package ld
+
+func (out *OutBuf) purgeSignatureCache() {}