]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: generate args_stackmap for ABI0 assembly func regardless of linkname
authorCherry Mui <cherryyz@google.com>
Fri, 24 May 2024 19:00:56 +0000 (15:00 -0400)
committerCherry Mui <cherryyz@google.com>
Fri, 7 Jun 2024 15:22:22 +0000 (15:22 +0000)
Currently, the compiler generates the argument stack map based on
the function signature for bodyless function declarations, if it
is not linknamed. The assumption is that linknamed function is
provided by (Go code in) another package, so its args stack map
will be generated when compiling that package.

Now we have linknames added to declarations of assembly functions,
to signal that this function is accessed externally. Examples
include runtime.morestack_noctxt, math/big.addVV. In the current
implementation the compiler does not generate its args stack map.
That causes the assembly function's args stack map missing.
Instead, change it to generate the stack map if it is a
declaration of an ABI0 function, which can only be defined in
assembly and passed to the compiler through the -symabis flag. The
stack map generation currently only works with ABI0 layout anyway,
so we don't need to handle ABIInternal assembly functions.

Change-Id: Ic9da3b4854c604e64ed01584da3865994f5b95b8
Reviewed-on: https://go-review.googlesource.com/c/go/+/587928
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Than McIntosh <thanm@google.com>
src/cmd/compile/internal/gc/compile.go
src/cmd/compile/internal/liveness/plive.go
test/linknameasm.dir/a_amd64.s [new file with mode: 0644]
test/linknameasm.dir/x.go [new file with mode: 0644]
test/linknameasm.go [new file with mode: 0644]

index 159fd29c48feb986252eb74cc4f101f09129ff5e..496daacb42d6da306eb5a5640aad3ab3f4b45055 100644 (file)
@@ -58,8 +58,13 @@ func enqueueFunc(fn *ir.Func) {
                types.CalcSize(fn.Type())
                a := ssagen.AbiForBodylessFuncStackMap(fn)
                abiInfo := a.ABIAnalyzeFuncType(fn.Type()) // abiInfo has spill/home locations for wrapper
-               liveness.WriteFuncMap(fn, abiInfo)
                if fn.ABI == obj.ABI0 {
+                       // The current args_stackmap generation assumes the function
+                       // is ABI0, and only ABI0 assembly function can have a FUNCDATA
+                       // reference to args_stackmap (see cmd/internal/obj/plist.go:Flushplist).
+                       // So avoid introducing an args_stackmap if the func is not ABI0.
+                       liveness.WriteFuncMap(fn, abiInfo)
+
                        x := ssagen.EmitArgInfo(fn, abiInfo)
                        objw.Global(x, int32(len(x.P)), obj.RODATA|obj.LOCAL)
                }
index 1a36035f46d68850490efdfb13b60b3e1e1d14a1..708f0f20235daae61d567e36e861bd8e88f3d0a1 100644 (file)
@@ -1536,7 +1536,7 @@ func isfat(t *types.Type) bool {
 // inputs and outputs as the value of symbol <fn>.args_stackmap.
 // If fn has outputs, two bitmaps are written, otherwise just one.
 func WriteFuncMap(fn *ir.Func, abiInfo *abi.ABIParamResultInfo) {
-       if ir.FuncName(fn) == "_" || fn.Sym().Linkname != "" {
+       if ir.FuncName(fn) == "_" {
                return
        }
        nptr := int(abiInfo.ArgWidth() / int64(types.PtrSize))
diff --git a/test/linknameasm.dir/a_amd64.s b/test/linknameasm.dir/a_amd64.s
new file mode 100644 (file)
index 0000000..2799609
--- /dev/null
@@ -0,0 +1,7 @@
+// Copyright 2024 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+TEXT   ·asm(SB),0,$0-8
+       CALL    ·callback(SB)
+       RET
diff --git a/test/linknameasm.dir/x.go b/test/linknameasm.dir/x.go
new file mode 100644 (file)
index 0000000..38bca6f
--- /dev/null
@@ -0,0 +1,26 @@
+// Copyright 2024 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Test that a linkname applied on an assembly declaration
+// does not affect stack map generation.
+
+package main
+
+import (
+       "runtime"
+       _ "unsafe"
+)
+
+//go:linkname asm
+func asm(*int)
+
+func main() {
+       x := new(int)
+       asm(x)
+}
+
+// called from asm
+func callback() {
+       runtime.GC() // scan stack
+}
diff --git a/test/linknameasm.go b/test/linknameasm.go
new file mode 100644 (file)
index 0000000..119f4bd
--- /dev/null
@@ -0,0 +1,9 @@
+// buildrundir
+
+// Copyright 2024 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build amd64
+
+package ignored