From: Russ Cox Date: Thu, 25 May 2023 16:47:06 +0000 (-0400) Subject: runtime: change heapObjectsCanMove to a func X-Git-Tag: go1.21rc1~226 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=789701e93a6f0063b6ef2c52c0557c672553d0e2;p=gostls13.git runtime: change heapObjectsCanMove to a func 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 TryBot-Result: Gopher Robot Run-TryBot: Russ Cox Reviewed-by: Austin Clements --- diff --git a/src/runtime/heap_test.go b/src/runtime/heap_test.go new file mode 100644 index 0000000000..4b73ab54fc --- /dev/null +++ b/src/runtime/heap_test.go @@ -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!") + } +} diff --git a/src/runtime/mgc.go b/src/runtime/mgc.go index d3658df489..de5ae0ae00 100644 --- a/src/runtime/mgc.go +++ b/src/runtime/mgc.go @@ -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 {