]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: change heapObjectsCanMove to a func
authorRuss Cox <rsc@golang.org>
Thu, 25 May 2023 16:47:06 +0000 (12:47 -0400)
committerRuss Cox <rsc@golang.org>
Thu, 25 May 2023 18:35:49 +0000 (18:35 +0000)
A var is problematic because the zero value is already false,
so if it goes away, it will appear to be false.
I'm also not sure about go:linkname on vars,
so switch to func for both reasons.

Also add a test.

Change-Id: I2318a5390d98577aec025152e65543491489defb
Reviewed-on: https://go-review.googlesource.com/c/go/+/498261
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
src/runtime/heap_test.go [new file with mode: 0644]
src/runtime/mgc.go

diff --git a/src/runtime/heap_test.go b/src/runtime/heap_test.go
new file mode 100644 (file)
index 0000000..4b73ab5
--- /dev/null
@@ -0,0 +1,21 @@
+// Copyright 2023 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.
+
+package runtime_test
+
+import (
+       "testing"
+       _ "unsafe"
+)
+
+//go:linkname heapObjectsCanMove runtime.heapObjectsCanMove
+func heapObjectsCanMove() bool
+
+func TestHeapObjectsCanMove(t *testing.T) {
+       if heapObjectsCanMove() {
+               // If this happens (or this test stops building),
+               // it will break go4.org/unsafe/assume-no-moving-gc.
+               t.Fatalf("heap objects can move!")
+       }
+}
index d3658df489bc156dec5b636ece5fb98d6b68a766..de5ae0ae00c4f1e6b12b104e98cb43704caa0555 100644 (file)
@@ -149,7 +149,7 @@ const (
        sweepMinHeapDistance = 1024 * 1024
 )
 
-// heapObjectsCanMove is always false in the current garbage collector.
+// heapObjectsCanMove always returns false in the current garbage collector.
 // It exists for go4.org/unsafe/assume-no-moving-gc, which is an
 // unfortunate idea that had an even more unfortunate implementation.
 // Every time a new Go release happened, the package stopped building,
@@ -165,7 +165,11 @@ const (
 //
 // If the Go garbage collector ever does move heap objects, we can set
 // this to true to break all the programs using assume-no-moving-gc.
-var heapObjectsCanMove = false
+//
+//go:linkname heapObjectsCanMove
+func heapObjectsCanMove() bool {
+       return false
+}
 
 func gcinit() {
        if unsafe.Sizeof(workbuf{}) != _WorkbufSize {