Skip to content

weak: Make panics on pointers to objects not allocated in heap #71726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
grudzinski opened this issue Feb 13, 2025 · 3 comments
Closed

weak: Make panics on pointers to objects not allocated in heap #71726

grudzinski opened this issue Feb 13, 2025 · 3 comments
Assignees
Labels
BugReport Issues describing a possible bug in the Go implementation. compiler/runtime Issues related to the Go compiler and/or runtime. NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@grudzinski
Copy link

Go version

1.24

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE='on'
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/roman/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/roman/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3567574948=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/home/roman/go/src/github.com/grudzinski/tests/go.mod'
GOMODCACHE='/home/roman/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/roman/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/roman/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.24.0'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

package main

import (
	"weak"
)

func main() {
	type nothing [0]int // or struct{}
	p := new(nothing) // special case, points to an addr stub allocated in BSS
	weak.Make[nothing](p)
}

Another case

package main

import (
	"weak"
)

var n int // allocated in BSS
var np = &n 

func main() {
	weak.Make[int](np)
}

What did you see happen?

fatal error: getWeakHandle on invalid pointer

goroutine 1 gp=0xc000002380 m=0 mp=0x4f7d60 [running]:
runtime.throw({0x47fea8?, 0xc000034008?})
	/usr/local/go/src/runtime/panic.go:1096 +0x48 fp=0xc000062668 sp=0xc000062638 pc=0x460e08
runtime.getWeakHandle(0x516970)
	/usr/local/go/src/runtime/mheap.go:2269 +0x113 fp=0xc0000626b0 sp=0xc000062668 pc=0x425e73
runtime.getOrAddWeakHandle(0x516970)
	/usr/local/go/src/runtime/mheap.go:2201 +0x1c fp=0xc000062700 sp=0xc0000626b0 pc=0x425bbc
weak.runtime_registerWeakPointer(0x0?)
	/usr/local/go/src/runtime/mheap.go:2102 +0x13 fp=0xc000062718 sp=0xc000062700 pc=0x460833
weak.Make[...](0x516970?)
	/usr/local/go/src/weak/pointer.go:72 +0x5d fp=0xc000062730 sp=0xc000062718 pc=0x4692fd
main.main()
	/home/roman/go/src/github.com/grudzinski/tests/main.go:11 +0x25 fp=0xc000062750 sp=0xc000062730 pc=0x469285
runtime.main()
	/usr/local/go/src/runtime/proc.go:283 +0x28b fp=0xc0000627e0 sp=0xc000062750 pc=0x4344eb
runtime.goexit({})
	/usr/local/go/src/runtime/asm_amd64.s:1700 +0x1 fp=0xc0000627e8 sp=0xc0000627e0 pc=0x465f41

goroutine 2 gp=0xc0000028c0 m=nil [force gc (idle)]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
	/usr/local/go/src/runtime/proc.go:435 +0xce fp=0xc000062fa8 sp=0xc000062f88 pc=0x460eee
runtime.goparkunlock(...)
	/usr/local/go/src/runtime/proc.go:441
runtime.forcegchelper()
	/usr/local/go/src/runtime/proc.go:348 +0xb3 fp=0xc000062fe0 sp=0xc000062fa8 pc=0x434833
runtime.goexit({})
	/usr/local/go/src/runtime/asm_amd64.s:1700 +0x1 fp=0xc000062fe8 sp=0xc000062fe0 pc=0x465f41
created by runtime.init.7 in goroutine 1
	/usr/local/go/src/runtime/proc.go:336 +0x1a

goroutine 3 gp=0xc000002e00 m=nil [GC sweep wait]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
	/usr/local/go/src/runtime/proc.go:435 +0xce fp=0xc000063780 sp=0xc000063760 pc=0x460eee
runtime.goparkunlock(...)
	/usr/local/go/src/runtime/proc.go:441
runtime.bgsweep(0xc00007a000)
	/usr/local/go/src/runtime/mgcsweep.go:276 +0x94 fp=0xc0000637c8 sp=0xc000063780 pc=0x420734
runtime.gcenable.gowrap1()
	/usr/local/go/src/runtime/mgc.go:204 +0x25 fp=0xc0000637e0 sp=0xc0000637c8 pc=0x414e65
runtime.goexit({})
	/usr/local/go/src/runtime/asm_amd64.s:1700 +0x1 fp=0xc0000637e8 sp=0xc0000637e0 pc=0x465f41
created by runtime.gcenable in goroutine 1
	/usr/local/go/src/runtime/mgc.go:204 +0x66

goroutine 4 gp=0xc000002fc0 m=nil [GC scavenge wait]:
runtime.gopark(0xc00007a000?, 0x499910?, 0x1?, 0x0?, 0xc000002fc0?)
	/usr/local/go/src/runtime/proc.go:435 +0xce fp=0xc000063f78 sp=0xc000063f58 pc=0x460eee
runtime.goparkunlock(...)
	/usr/local/go/src/runtime/proc.go:441
runtime.(*scavengerState).park(0x4f6f80)
	/usr/local/go/src/runtime/mgcscavenge.go:425 +0x49 fp=0xc000063fa8 sp=0xc000063f78 pc=0x41e1e9
runtime.bgscavenge(0xc00007a000)
	/usr/local/go/src/runtime/mgcscavenge.go:653 +0x3c fp=0xc000063fc8 sp=0xc000063fa8 pc=0x41e75c
runtime.gcenable.gowrap2()
	/usr/local/go/src/runtime/mgc.go:205 +0x25 fp=0xc000063fe0 sp=0xc000063fc8 pc=0x414e05
runtime.goexit({})
	/usr/local/go/src/runtime/asm_amd64.s:1700 +0x1 fp=0xc000063fe8 sp=0xc000063fe0 pc=0x465f41
created by runtime.gcenable in goroutine 1
	/usr/local/go/src/runtime/mgc.go:205 +0xa5

What did you expect to see?

I expect no panics for this cases.

@mknyszek
Copy link
Contributor

Indeed, I think we missed that we have to apply an exception for linker-allocated objects. SetFinalizer and AddCleanup do, so weak.Make should as well, but it's going to be a bit more complex for weak.Make since we still have to produce a weak.Pointer that compares equal.

I'll take a look and hopefully send a fix soon.

@mknyszek mknyszek self-assigned this Feb 13, 2025
@mknyszek mknyszek added NeedsFix The path to resolution is known, but the work has not been done. compiler/runtime Issues related to the Go compiler and/or runtime. labels Feb 13, 2025
@mknyszek mknyszek added this to the Backlog milestone Feb 13, 2025
@gabyhelp gabyhelp added the BugReport Issues describing a possible bug in the Go implementation. label Feb 13, 2025
@mknyszek mknyszek changed the title weak: weak.Make panics on pointers to objects not allocated in heap weak: Make panics on pointers to objects not allocated in heap Feb 14, 2025
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/649460 mentions this issue: weak: accept linker-allocated objects to Make

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BugReport Issues describing a possible bug in the Go implementation. compiler/runtime Issues related to the Go compiler and/or runtime. NeedsFix The path to resolution is known, but the work has not been done.
Projects
Development

No branches or pull requests

4 participants