Skip to content

x/tools/go/ssa: no way to determine type of *deferStack #72914

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

Open
aykevl opened this issue Mar 18, 2025 · 1 comment
Open

x/tools/go/ssa: no way to determine type of *deferStack #72914

aykevl opened this issue Mar 18, 2025 · 1 comment
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@aykevl
Copy link

aykevl commented Mar 18, 2025

Go version

go version go1.24.0 linux/arm64

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=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/ayke/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/ayke/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3827790920=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='arm64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/home/ayke/src/tinygo/tinygo/go.mod'
GOMODCACHE='/home/ayke/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/ayke'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go1.24.0'
GOSUMDB='sum.golang.org'
GOTELEMETRY='on'
GOTELEMETRYDIR='/home/ayke/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go1.24.0/pkg/tool/linux_arm64'
GOVCS=''
GOVERSION='go1.24.0'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

I have the following code:

package main

func main() {
        for range func(func() bool) {} {
                defer func() {}()
        }
}

I ran it through ssadump:

$ ssadump -build GCFI code.go

What did you see happen?

See this short part of the output of the main function:

# Name: command-line-arguments.main
# Package: command-line-arguments
# Location: /home/ayke/src/tinygo/tinygo/tmp/issue4441.go:3:6
# Recover: 2
func main():
0:                                                                entry P:0 S:2
        t0 = ssa:deferstack()                                       *deferStack
        t1 = new *deferStack (defer$stack)                         **deferStack
        *t1 = t0
[...]

It has a new *deferStack (defer$stack) instruction. Unfortunately, it's hard to detect this specific type. For other types (in the go/types package) you can use type asserts and such to determine the type, but there is no way to do it for the deferStack.

(Also, typeutil.Map panics when it encounters a *deferStack type so typeutil.Map can't be used here, I'm not sure whether this is intended or not).

What did you expect to see?

I would like to see a way to detect this type, either by making the ssa.opaqueType public (to allow type asserts) or by making ssa.tDeferStack public (to compare against).

I guess I can use reflection to do the same, but that feels like a kludge and not how the API was meant to be used.

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Mar 18, 2025
@gopherbot gopherbot added this to the Unreleased milestone Mar 18, 2025
@adonovan
Copy link
Member

One option would be to change the type of the result of ssa:deferstack() to *struct{} and just assume that users aren't going to construct SSA graphs in which they pass the wrong kind of *struct{} values to defer.

@cherrymui cherrymui added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Analysis Issues related to static analysis (vet, x/tools/go/analysis) labels Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants