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: go:linkname no longer works for type's method #63846

Closed
fumeboy opened this issue Oct 31, 2023 · 2 comments
Closed

cmd/compile: go:linkname no longer works for type's method #63846

fumeboy opened this issue Oct 31, 2023 · 2 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@fumeboy
Copy link

fumeboy commented Oct 31, 2023

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

1.20.1

Does this issue reproduce with the latest release?

yes

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

GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/bytedance/Library/Caches/go-build"
GOENV="/Users/bytedance/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/bytedance/go/pkg/mod"
GONOPROXY="*.byted.org,*.everphoto.cn,git.smartisan.com"
GONOSUMDB="*.byted.org,*.everphoto.cn,git.smartisan.com"
GOOS="darwin"
GOPATH="/Users/bytedance/go"
GOPRIVATE="*.byted.org,*.everphoto.cn,git.smartisan.com"
GOPROXY="https://go-mod-proxy.byted.org,https://goproxy.cn,https://proxy.golang.org,direct"
GOROOT="/Users/bytedance/x/go/sdk/go1.20.1"
GOSUMDB="sum.golang.google.cn"
GOTMPDIR=""
GOTOOLDIR="/Users/bytedance/x/go/sdk/go1.20.1/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.20.1"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/bytedance/x/go/sdk/go1.20.1/src/cmd/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/b1/75_q2h8x6_zcbd_746_lvht40000gn/T/go-build2405814180=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

i write this code to explain:

package a

import _ "unsafe"

type A string

//go:linkname A.Fmt fmt.Sprintf
func (A) Fmt(a ...any) string

func main() {
	println(A("%d").Fmt(1))
}

What did you expect to see?

at go1.19, i could run this program without compile error, the go:linkname works fine

What did you see instead?

./a.go:7:3: //go:linkname must refer to declared function or variable

and i print the context before the compile bad exit , i found the scope just contains funcdecl and typedecl, no type method decl

// cmd/compile/internal/noder/writer.go
switch obj := pw.curpkg.Scope().Lookup(l.local).(type) {
case *types2.Func, *types2.Var:
	if _, ok := pw.linknames[obj]; !ok {
		pw.linknames[obj] = l.remote
	} else {
		pw.errorf(l.pos, "duplicate //go:linkname for %s", l.local)
	}

default:

// print here
	for k,e := range pw.curpkg.Scope().Elems() {
		println(1, k,e.String()) // just funcdecl and typedecl, no type method decl
	}

	if types.AllowsGoVersion(1, 18) {
		pw.errorf(l.pos, "//go:linkname must refer to declared function or variable")
	}
}
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Oct 31, 2023
@mauri870 mauri870 added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 31, 2023
@mauri870
Copy link
Member

The go:linkname directive is meant to be used internally by the go runtime, and I believe it is not officially supported outside of this use case.

/cc @golang/compiler

@thanm
Copy link
Contributor

thanm commented Oct 31, 2023

Thanks for the report.

As @mauri870 remarks, this is not a supported use case for //go:linkname ; the linkname directive is intended for use in the runtime and in a few hand-picked cases in the standard library.

I should also add that //go:linkname is very definitely not covered by the Go 1 compatibility guarantee. We can (and do) make changes to the way //go:linkname works in new releases, and uses outside the Go runtime and standard library can easily break when moving from one release to the next.

Closing out this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants