]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: disallow linkname referring to instantiations
authorCherry Mui <cherryyz@google.com>
Wed, 15 May 2024 02:34:58 +0000 (22:34 -0400)
committerCherry Mui <cherryyz@google.com>
Wed, 15 May 2024 19:27:25 +0000 (19:27 +0000)
Linknaming an instantiated generic symbol isn't particularly
useful: it doesn't guarantee the instantiation exists, and the
instantiated symbol name may be subject to change. Checked with a
large code corpus, currently there is no occurrance of linkname
to an instantiated generic symbol (or symbol with a bracket in its
name). This also suggests that it is not very useful. Linkname is
already an unsafe mechanism. We don't need to allow it to do more
unsafe things without justification.

Change-Id: Ifaa20c98166b28a9d7dc3290c013c2b5bb7682e7
Reviewed-on: https://go-review.googlesource.com/c/go/+/585458
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Than McIntosh <thanm@google.com>
src/cmd/compile/internal/noder/writer.go
test/linkname3.go

index a48e193c5fa69695bff5933e5729db60d96f0db6..13706f9dd25561f9dedbdc64064ddebfadb1a784 100644 (file)
@@ -12,6 +12,7 @@ import (
        "internal/buildcfg"
        "internal/pkgbits"
        "os"
+       "strings"
 
        "cmd/compile/internal/base"
        "cmd/compile/internal/ir"
@@ -2610,6 +2611,10 @@ func (pw *pkgWriter) collectDecls(noders []*noder) {
                                pw.errorf(l.pos, "//go:linkname only allowed in Go files that import \"unsafe\"")
                                continue
                        }
+                       if strings.Contains(l.remote, "[") && strings.Contains(l.remote, "]") {
+                               pw.errorf(l.pos, "//go:linkname reference of an instantiation is not allowed")
+                               continue
+                       }
 
                        switch obj := pw.curpkg.Scope().Lookup(l.local).(type) {
                        case *types2.Func, *types2.Var:
index df110cd064d151e4e7a2eee26fa842adda28436c..0d5df0b86e346762eb68fcfdcbc7442992d118ce 100644 (file)
@@ -13,13 +13,17 @@ type t int
 
 var x, y int
 
+func F[T any](T) {}
+
 //go:linkname x ok
 
 // ERROR "//go:linkname must refer to declared function or variable"
 // ERROR "//go:linkname must refer to declared function or variable"
 // ERROR "duplicate //go:linkname for x"
+// ERROR "//go:linkname reference of an instantiation is not allowed"
 
-//line linkname3.go:18
+//line linkname3.go:20
 //go:linkname nonexist nonexist
 //go:linkname t notvarfunc
 //go:linkname x duplicate
+//go:linkname i F[go.shape.int]