From 76a2c87a2c4e78f40f0f70bda3da93c773630179 Mon Sep 17 00:00:00 2001 From: Keith Randall Date: Fri, 2 Oct 2020 16:04:12 -0700 Subject: [release-branch.go1.15] cmd/compile: export notinheap annotation to object file In the rare case when a cgo type makes it into an object file, we need the go:notinheap annotation to go with it. Fixes #41432. Change-Id: Ie2ef241ee49661792e0d8c8c46c51b2fe5c6fa7c Reviewed-on: https://go-review.googlesource.com/c/go/+/259300 Trust: Keith Randall Reviewed-by: Ian Lance Taylor Reviewed-by: Cherry Zhang --- misc/cgo/test/testdata/issue41761.go | 20 ++++++++++++++++++++ misc/cgo/test/testdata/issue41761a/a.go | 14 ++++++++++++++ src/cmd/compile/internal/gc/iexport.go | 6 ++++++ src/cmd/compile/internal/gc/iimport.go | 5 +++++ 4 files changed, 45 insertions(+) create mode 100644 misc/cgo/test/testdata/issue41761.go create mode 100644 misc/cgo/test/testdata/issue41761a/a.go diff --git a/misc/cgo/test/testdata/issue41761.go b/misc/cgo/test/testdata/issue41761.go new file mode 100644 index 0000000000..919c749251 --- /dev/null +++ b/misc/cgo/test/testdata/issue41761.go @@ -0,0 +1,20 @@ +// Copyright 2020 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. + +package cgotest + +/* + typedef struct S S; +*/ +import "C" + +import ( + "cgotest/issue41761a" + "testing" +) + +func test41761(t *testing.T) { + var x issue41761a.T + _ = (*C.struct_S)(x.X) +} diff --git a/misc/cgo/test/testdata/issue41761a/a.go b/misc/cgo/test/testdata/issue41761a/a.go new file mode 100644 index 0000000000..ca5c18191e --- /dev/null +++ b/misc/cgo/test/testdata/issue41761a/a.go @@ -0,0 +1,14 @@ +// Copyright 2020 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. + +package issue41761a + +/* + typedef struct S S; +*/ +import "C" + +type T struct { + X *C.S +} diff --git a/src/cmd/compile/internal/gc/iexport.go b/src/cmd/compile/internal/gc/iexport.go index 35b8d985cb..81eb975b08 100644 --- a/src/cmd/compile/internal/gc/iexport.go +++ b/src/cmd/compile/internal/gc/iexport.go @@ -492,6 +492,7 @@ func (p *iexporter) doDecl(n *Node) { w.signature(m.Type) } + w.typeExt(t) for _, m := range ms.Slice() { w.methExt(m) } @@ -1012,6 +1013,11 @@ func (w *exportWriter) symIdx(s *types.Sym) { } } +func (w *exportWriter) typeExt(t *types.Type) { + // Export whether this type is marked notinheap. + w.bool(t.NotInHeap()) +} + // Inline bodies. func (w *exportWriter) stmtList(list Nodes) { diff --git a/src/cmd/compile/internal/gc/iimport.go b/src/cmd/compile/internal/gc/iimport.go index 104b5fb79a..d9148eae22 100644 --- a/src/cmd/compile/internal/gc/iimport.go +++ b/src/cmd/compile/internal/gc/iimport.go @@ -346,6 +346,7 @@ func (r *importReader) doDecl(n *Node) { } t.Methods().Set(ms) + r.typeExt(t) for _, m := range ms { r.methExt(m) } @@ -710,6 +711,10 @@ func (r *importReader) symIdx(s *types.Sym) { } } +func (r *importReader) typeExt(t *types.Type) { + t.SetNotInHeap(r.bool()) +} + func (r *importReader) doInline(n *Node) { if len(n.Func.Inl.Body) != 0 { Fatalf("%v already has inline body", n) -- cgit v1.2.3-54-g00ecf