diff options
author | Keith Randall <khr@golang.org> | 2017-08-11 09:48:46 -0700 |
---|---|---|
committer | Keith Randall <khr@golang.org> | 2017-08-14 20:11:21 +0000 |
commit | 2c990f4596b8211ec24b095d80e66ec82a5a5021 (patch) | |
tree | 22372766cf1910ce050886152abd7cba68a36a82 /src/runtime/mfinal.go | |
parent | 1f631a2f9a20d8dc57fb877fb95f807c895d1c40 (diff) | |
download | go-2c990f4596b8211ec24b095d80e66ec82a5a5021.tar.gz go-2c990f4596b8211ec24b095d80e66ec82a5a5021.zip |
runtime: add a use of runtime.KeepAlive's argument
This makes sure that its argument is marked live on entry.
We need its arg to be live so defers of KeepAlive get
scanned correctly by the GC.
Fixes #21402
Change-Id: I906813e433d0e9726ca46483723303338da5b4d7
Reviewed-on: https://go-review.googlesource.com/55150
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/runtime/mfinal.go')
-rw-r--r-- | src/runtime/mfinal.go | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/src/runtime/mfinal.go b/src/runtime/mfinal.go index a8729b1aa4..c11a6f15a4 100644 --- a/src/runtime/mfinal.go +++ b/src/runtime/mfinal.go @@ -461,11 +461,7 @@ func findObject(v unsafe.Pointer) (s *mspan, x unsafe.Pointer, n uintptr) { return } -// Mark KeepAlive as noinline so that the current compiler will ensure -// that the argument is alive at the point of the function call. -// If it were inlined, it would disappear, and there would be nothing -// keeping the argument alive. Perhaps a future compiler will recognize -// runtime.KeepAlive specially and do something more efficient. +// Mark KeepAlive as noinline so that it is easily detectable as an intrinsic. //go:noinline // KeepAlive marks its argument as currently reachable. @@ -487,4 +483,11 @@ func findObject(v unsafe.Pointer) (s *mspan, x unsafe.Pointer, n uintptr) { // Without the KeepAlive call, the finalizer could run at the start of // syscall.Read, closing the file descriptor before syscall.Read makes // the actual system call. -func KeepAlive(interface{}) {} +func KeepAlive(x interface{}) { + // Introduce a use of x that the compiler can't eliminate. + // This makes sure x is alive on entry. We need x to be alive + // on entry for "defer runtime.KeepAlive(x)"; see issue 21402. + if cgoAlwaysFalse { + println(x) + } +} |