Skip to content
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

cmd/compile: unsafe.Sizeof with generic type parameter causes panic #47716

Closed
tdakkota opened this issue Aug 16, 2021 · 6 comments
Closed

cmd/compile: unsafe.Sizeof with generic type parameter causes panic #47716

tdakkota opened this issue Aug 16, 2021 · 6 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@tdakkota
Copy link

What version of Go are you using (go version)?

$ go version
go version devel go1.18-4d3cc84774 Thu Aug 12 20:22:27 2021 +0000 windows/amd64

Does this issue reproduce with the latest release?

No.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GO111MODULE=auto
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\tdakkota\AppData\Local\go-build
set GOENV=C:\Users\tdakkota\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\tdakkota\go\pkg\mod
set GOOS=windows
set GOPATH=C:\Users\tdakkota\go
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Users\tdakkota\GolandProjects\gh\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Users\tdakkota\GolandProjects\gh\go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=devel go1.18-4d3cc84774 Thu Aug 12 20:22:27 2021 +0000
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\tdakkota\AppData\Local\Temp\go-build471959828=/tmp/go-build -gno-record-gcc-switches

What did you do?

https://go2goplay.golang.org/p/f14OcREOTZ9

What did you expect to see?

Successful compilation or some compiler error.

What did you see instead?

panic: <nil> not an Int

goroutine 1 [running]:
go/constant.Int64Val({0x0, 0x0})
	/root/master/go/src/go/constant/value.go:504 +0x119
cmd/compile/internal/noder.(*irgen).validateBuiltin(0xc0000a6300, {0xc000024f96, 0x6}, 0xc0000897c0)
	/root/master/go/src/cmd/compile/internal/noder/validate.go:84 +0xd7
cmd/compile/internal/noder.(*irgen).validate(0xc0000a6300, {0xe49e80, 0xc0000897c0})
	/root/master/go/src/cmd/compile/internal/noder/validate.go:70 +0x157
cmd/compile/internal/noder.(*irgen).generate.func1({0xe49e80, 0xc0000897c0})
	/root/master/go/src/cmd/compile/internal/noder/irgen.go:259 +0x30
cmd/compile/internal/syntax.Crawl.func1({0xe49e80, 0xc0000897c0})
	/root/master/go/src/cmd/compile/internal/syntax/walk.go:41 +0x30
cmd/compile/internal/syntax.inspector.Visit(0xc00002ee80, {0xe49e80, 0xc0000897c0})
	/root/master/go/src/cmd/compile/internal/syntax/walk.go:24 +0x31
cmd/compile/internal/syntax.walker.node({{0xe471c0, 0xc00002ee80}}, {0xe49e80, 0xc0000897c0})
	/root/master/go/src/cmd/compile/internal/syntax/walk.go:76 +0x6b
cmd/compile/internal/syntax.walker.node({{0xe471c0, 0xc00002ee80}}, {0xe4a308, 0xc000353f00})
	/root/master/go/src/cmd/compile/internal/syntax/walk.go:267 +0x165c
cmd/compile/internal/syntax.walker.stmtList({{0xe471c0, 0xc00002ee80}}, {0xc000353f80, 0x2, 0xc000353e80})
	/root/master/go/src/cmd/compile/internal/syntax/walk.go:348 +0x91
cmd/compile/internal/syntax.walker.node({{0xe471c0, 0xc00002ee80}}, {0xe49e30, 0xc000089780})
	/root/master/go/src/cmd/compile/internal/syntax/walk.go:238 +0x13f2
cmd/compile/internal/syntax.walker.node({{0xe471c0, 0xc00002ee80}}, {0xe4a0b0, 0xc000065c80})
	/root/master/go/src/cmd/compile/internal/syntax/walk.go:125 +0x949
cmd/compile/internal/syntax.walker.declList({{0xe471c0, 0xc00002ee80}}, {0xc0000ba400, 0x6, 0xc0000976d8})
	/root/master/go/src/cmd/compile/internal/syntax/walk.go:336 +0x91
cmd/compile/internal/syntax.walker.node({{0xe471c0, 0xc00002ee80}}, {0xe4a060, 0xc000377a90})
	/root/master/go/src/cmd/compile/internal/syntax/walk.go:85 +0xfee
cmd/compile/internal/syntax.Walk(...)
	/root/master/go/src/cmd/compile/internal/syntax/walk.go:57
cmd/compile/internal/syntax.Inspect(...)
	/root/master/go/src/cmd/compile/internal/syntax/walk.go:18
cmd/compile/internal/syntax.Crawl({0xe4a060, 0xc000377a90}, 0xc00002ee70)
	/root/master/go/src/cmd/compile/internal/syntax/walk.go:40 +0x75
cmd/compile/internal/noder.(*irgen).generate(0xc0000a6300, {0xc00000e448, 0x1, 0x203000})
	/root/master/go/src/cmd/compile/internal/noder/irgen.go:258 +0x1dd
cmd/compile/internal/noder.check2({0xc00000e448, 0x1, 0x1})
	/root/master/go/src/cmd/compile/internal/noder/irgen.go:92 +0x16d
cmd/compile/internal/noder.LoadPackage({0xc00001e260, 0x1, 0x0})
	/root/master/go/src/cmd/compile/internal/noder/noder.go:90 +0x365
cmd/compile/internal/gc.Main(0xd19b48)
	/root/master/go/src/cmd/compile/internal/gc/main.go:190 +0xaf3
main.main()
	/root/master/go/src/cmd/compile/main.go:55 +0xdd

It seems g.info.Types[call].Value is nil here, but g.info.Types[call] is not zero value.

got, ok := constant.Int64Val(g.info.Types[call].Value)

@findleyr
Copy link
Contributor

Thanks for the report, and the investigation.

I'm guessing this is a consequence of https://golang.org/cl/335413; Sizeof is no longer necessarily constant.

CC @griesemer @mdempsky

@mknyszek mknyszek added this to the Go1.18 milestone Aug 16, 2021
@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 16, 2021
@griesemer
Copy link
Contributor

Simpler reproducer:

package p

import "unsafe"

func _[T any](x T) {
	_ = unsafe.Sizeof(x)
}

@griesemer griesemer added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Aug 16, 2021
@griesemer
Copy link
Contributor

Here's a partial fix that avoids the crash. But we should probably check that the compiler's size computation agrees:

diff --git a/src/cmd/compile/internal/noder/validate.go b/src/cmd/compile/internal/noder/validate.go
index 68a059b96f..bc9092b8f3 100644
--- a/src/cmd/compile/internal/noder/validate.go
+++ b/src/cmd/compile/internal/noder/validate.go
@@ -81,7 +81,16 @@ func (g *irgen) validateBuiltin(name string, call *syntax.CallExpr) {
                // Check that types2+gcSizes calculates sizes the same
                // as cmd/compile does.
 
-               got, ok := constant.Int64Val(g.info.Types[call].Value)
+               tv := g.info.Types[call]
+               if !tv.IsValue() {
+                       base.FatalfAt(g.pos(call), "expected a value")
+               }
+
+               if tv.Value == nil {
+                       break // unsafe op is not a constant
+               }
+
+               got, ok := constant.Int64Val(tv.Value)
                if !ok {
                        base.FatalfAt(g.pos(call), "expected int64 constant value")
                }

cc: @danscales

@mdempsky
Copy link
Member

To check, the current status is that unsafe.{Align,Size,Offset}of will now have type uintptr but not necessarily be constant, if the argument is a type parameter or derived from one? E.g., unsafe.Sizeof([]T(nil)) or unsafe.Offsetof(struct{a T; b byte}{}.b)?

@griesemer
Copy link
Contributor

griesemer commented Aug 16, 2021

Per the current (in-progress) spec:

A (variable of) type T has variable size if T
is a type parameter, or if it is an array or struct type containing elements
or fields of variable size. Otherwise the size is constant.

@gopherbot
Copy link

Change https://golang.org/cl/344250 mentions this issue: cmd/compile: fixes for non-constant Sizeof/Alignof

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

7 participants