Skip to content

Commit

Permalink
For Go 1.21+, ask the Go runtime about its GC.
Browse files Browse the repository at this point in the history
  • Loading branch information
bradfitz committed May 25, 2023
1 parent 7e06285 commit e7c30c7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 37 deletions.
20 changes: 9 additions & 11 deletions assume-no-moving-gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,23 @@
// analysis keeps on the stack. Ensuring things aren't stack-allocated
// is the caller's responsibility.
//
// This package is then updated for new Go versions when that
// is still the case and explodes at runtime with a failure
// otherwise, unless an environment variable overrides it.
// This package is then updated as needed for new Go versions when
// that is still the case and explodes at runtime with a failure
// otherwise, with the idea that it's better to not start at all than
// to silently corrupt your data at runtime.
//
// To use:
//
// import _ "go4.org/unsafe/assume-no-moving-gc"
//
// There is no API.
//
// It is intentional that this package will break code that's not updated
// regularly to double check its assumptions about the world and new Go
// versions. If you play stupid games with unsafe pointers, the stupid prize
// is this maintenance cost. (The alternative would be memory corruption if
// some unmaintained, unsafe library were built with a future version of Go
// that worked very differently than when the unsafe library was built.)
// Ideally you shouldn't write unsafe code, though.
// As of Go 1.21, this package asks the Go runtime whether it can move
// heap objects around. If you get an error on versions prior to that,
// go get go4.org/unsafe/assume-no-moving-gc@latest and things will
// work.
//
// The GitHub repo is at https://github.com/go4org/unsafe-assume-no-moving-gc
package assume_no_moving_gc

const env = "ASSUME_NO_MOVING_GC_UNSAFE_RISK_IT_WITH"
const env = "ASSUME_NO_MOVING_GC_UNSAFE"
36 changes: 36 additions & 0 deletions check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2020 Brad Fitzpatrick. 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 go1.21
// +build go1.21

package assume_no_moving_gc

import (
"os"
_ "unsafe"
)

//go:linkname heapObjectsCanMove runtime.heapObjectsCanMove
func heapObjectsCanMove() bool

func init() {
if !heapObjectsCanMove() {
// The unsafe assumptions made by the package
// importing this package still hold. All's good. (at
// least unless they made other assumption this
// package doesn't concern itself with)
return
}
if os.Getenv(env) == "play-with-fire" {
return
}
panic(`
Something in this program imports go4.org/unsafe/assume-no-moving-gc to
declare that it assumes a non-moving garbage collector, but the version
of Go you're using declares that its heap objects can now move around.
This program is no longer safe. You should update your packages which import
go4.org/unsafe/assume-no-moving-gc. To risk it and bypass this check, set
ASSUME_NO_MOVING_GC_UNSAFE=play-with-fire and cross your fingers.`)
}
26 changes: 0 additions & 26 deletions untested.go

This file was deleted.

0 comments on commit e7c30c7

Please sign in to comment.