Skip to content

Commit 9698372

Browse files
randall77dmitshur
authored andcommitted
[release-branch.go1.15] cmd/cgo: use go:notinheap for anonymous structs
They can't reasonably be allocated on the heap. Not a huge deal, but it has an interesting and useful side effect. After CL 249917, the compiler and runtime treat pointers to go:notinheap types as uintptrs instead of real pointers (no write barrier, not processed during stack scanning, ...). That feature is exactly what we want for cgo to fix #40954. All the cases we have of pointers declared in C, but which might actually be filled with non-pointer data, are of this form (JNI's jobject heirarch, Darwin's CFType heirarchy, ...). Fixes #40954 Change-Id: I44a3b9bc2513d4287107e39d0cbbd0efd46a3aae Reviewed-on: https://go-review.googlesource.com/c/go/+/250940 Run-TryBot: Emmanuel Odeke <[email protected]> TryBot-Result: Go Bot <[email protected]> Trust: Keith Randall <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/255321
1 parent 1420278 commit 9698372

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

src/cmd/cgo/gcc.go

+15
Original file line numberDiff line numberDiff line change
@@ -2434,6 +2434,18 @@ func (c *typeConv) loadType(dtype dwarf.Type, pos token.Pos, parent string) *Typ
24342434
tt := *t
24352435
tt.C = &TypeRepr{"%s %s", []interface{}{dt.Kind, tag}}
24362436
tt.Go = c.Ident("struct{}")
2437+
if dt.Kind == "struct" {
2438+
// We don't know what the representation of this struct is, so don't let
2439+
// anyone allocate one on the Go side. As a side effect of this annotation,
2440+
// pointers to this type will not be considered pointers in Go. They won't
2441+
// get writebarrier-ed or adjusted during a stack copy. This should handle
2442+
// all the cases badPointerTypedef used to handle, but hopefully will
2443+
// continue to work going forward without any more need for cgo changes.
2444+
tt.NotInHeap = true
2445+
// TODO: we should probably do the same for unions. Unions can't live
2446+
// on the Go heap, right? It currently doesn't work for unions because
2447+
// they are defined as a type alias for struct{}, not a defined type.
2448+
}
24372449
typedef[name.Name] = &tt
24382450
break
24392451
}
@@ -2504,6 +2516,7 @@ func (c *typeConv) loadType(dtype dwarf.Type, pos token.Pos, parent string) *Typ
25042516
}
25052517
t.Go = name
25062518
t.BadPointer = sub.BadPointer
2519+
t.NotInHeap = sub.NotInHeap
25072520
if unionWithPointer[sub.Go] {
25082521
unionWithPointer[t.Go] = true
25092522
}
@@ -2514,6 +2527,7 @@ func (c *typeConv) loadType(dtype dwarf.Type, pos token.Pos, parent string) *Typ
25142527
tt := *t
25152528
tt.Go = sub.Go
25162529
tt.BadPointer = sub.BadPointer
2530+
tt.NotInHeap = sub.NotInHeap
25172531
typedef[name.Name] = &tt
25182532
}
25192533

@@ -3022,6 +3036,7 @@ func (c *typeConv) anonymousStructTypedef(dt *dwarf.TypedefType) bool {
30223036
// non-pointers in this type.
30233037
// TODO: Currently our best solution is to find these manually and list them as
30243038
// they come up. A better solution is desired.
3039+
// Note: DEPRECATED. There is now a better solution. Search for NotInHeap in this file.
30253040
func (c *typeConv) badPointerTypedef(dt *dwarf.TypedefType) bool {
30263041
if c.badCFType(dt) {
30273042
return true

src/cmd/cgo/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ type Type struct {
151151
Go ast.Expr
152152
EnumValues map[string]int64
153153
Typedef string
154-
BadPointer bool
154+
BadPointer bool // this pointer type should be represented as a uintptr (deprecated)
155+
NotInHeap bool // this type should have a go:notinheap annotation
155156
}
156157

157158
// A FuncType collects information about a function type in both the C and Go worlds.

src/cmd/cgo/out.go

+3
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ func (p *Package) writeDefs() {
108108
sort.Strings(typedefNames)
109109
for _, name := range typedefNames {
110110
def := typedef[name]
111+
if def.NotInHeap {
112+
fmt.Fprintf(fgo2, "//go:notinheap\n")
113+
}
111114
fmt.Fprintf(fgo2, "type %s ", name)
112115
// We don't have source info for these types, so write them out without source info.
113116
// Otherwise types would look like:

test/fixedbugs/issue40954.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// run
2+
3+
// Copyright 2020 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package main
8+
9+
import (
10+
"unsafe"
11+
)
12+
13+
//go:notinheap
14+
type S struct{ x int }
15+
16+
func main() {
17+
var i int
18+
p := (*S)(unsafe.Pointer(uintptr(unsafe.Pointer(&i))))
19+
v := uintptr(unsafe.Pointer(p))
20+
// p is a pointer to a go:notinheap type. Like some C libraries,
21+
// we stored an integer in that pointer. That integer just happens
22+
// to be the address of i.
23+
// v is also the address of i.
24+
// p has a base type which is marked go:notinheap, so it
25+
// should not be adjusted when the stack is copied.
26+
recurse(100, p, v)
27+
}
28+
func recurse(n int, p *S, v uintptr) {
29+
if n > 0 {
30+
recurse(n-1, p, v)
31+
}
32+
if uintptr(unsafe.Pointer(p)) != v {
33+
panic("adjusted notinheap pointer")
34+
}
35+
}

0 commit comments

Comments
 (0)