Skip to content

Commit 7904946

Browse files
cmd/cgo: discard trailing zero-sized fields in a non-empty C struct
In order to fix issue #9401 the compiler was changed to add a padding byte to any non-empty Go struct that ends in a zero-sized field. That causes the Go version of such a C struct to have a different size than the C struct, which can considerable confusion. Change cgo so that it discards any such zero-sized fields, so that the Go and C structs are the same size. This is a change from previous releases, in that it used to be possible to refer to a zero-sized trailing field (by taking its address), and with this change it no longer is. That is unfortunate, but something has to change. It seems better to visibly break programs that do this rather than to silently break programs that rely on the struct sizes being the same. Update #9401. Fixes #11925. Change-Id: I3fba3f02f11265b3c41d68616f79dedb05b81225 Reviewed-on: https://go-review.googlesource.com/12864 Reviewed-by: Russ Cox <[email protected]>
1 parent c9d2c7f commit 7904946

File tree

4 files changed

+65
-6
lines changed

4 files changed

+65
-6
lines changed

misc/cgo/test/cgo_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,6 @@ func TestReturnAfterGrowFromGo(t *testing.T) { testReturnAfterGrowFromGo(t) }
6464
func Test9026(t *testing.T) { test9026(t) }
6565
func Test9557(t *testing.T) { test9557(t) }
6666
func Test10303(t *testing.T) { test10303(t, 10) }
67+
func Test11925(t *testing.T) { test11925(t) }
6768

6869
func BenchmarkCgoCall(b *testing.B) { benchCgoCall(b) }

misc/cgo/test/issue11925.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2015 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Issue 11925. Structs with zero-length trailing fields are now
6+
// padded by the Go compiler.
7+
8+
package cgotest
9+
10+
/*
11+
struct a11925 {
12+
int i;
13+
char a[0];
14+
char b[0];
15+
};
16+
17+
struct b11925 {
18+
int i;
19+
char a[0];
20+
char b[];
21+
};
22+
*/
23+
import "C"
24+
25+
import (
26+
"testing"
27+
"unsafe"
28+
)
29+
30+
func test11925(t *testing.T) {
31+
if C.sizeof_struct_a11925 != unsafe.Sizeof(C.struct_a11925{}) {
32+
t.Errorf("size of a changed: C %d, Go %d", C.sizeof_struct_a11925, unsafe.Sizeof(C.struct_a11925{}))
33+
}
34+
if C.sizeof_struct_b11925 != unsafe.Sizeof(C.struct_b11925{}) {
35+
t.Errorf("size of b changed: C %d, Go %d", C.sizeof_struct_b11925, unsafe.Sizeof(C.struct_b11925{}))
36+
}
37+
}

misc/cgo/test/issue8428.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ struct issue8428two {
2020
void *p;
2121
char b;
2222
char rest[0];
23+
char pad;
2324
};
2425
2526
struct issue8428three {
@@ -34,8 +35,10 @@ import "C"
3435
import "unsafe"
3536

3637
var _ = C.struct_issue8428one{
37-
b: C.char(0),
38-
rest: [0]C.char{},
38+
b: C.char(0),
39+
// The trailing rest field is not available in cgo.
40+
// See issue 11925.
41+
// rest: [0]C.char{},
3942
}
4043

4144
var _ = C.struct_issue8428two{

src/cmd/cgo/gcc.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,11 +1544,13 @@ func (c *typeConv) intExpr(n int64) ast.Expr {
15441544
}
15451545

15461546
// Add padding of given size to fld.
1547-
func (c *typeConv) pad(fld []*ast.Field, size int64) []*ast.Field {
1547+
func (c *typeConv) pad(fld []*ast.Field, sizes []int64, size int64) ([]*ast.Field, []int64) {
15481548
n := len(fld)
15491549
fld = fld[0 : n+1]
15501550
fld[n] = &ast.Field{Names: []*ast.Ident{c.Ident("_")}, Type: c.Opaque(size)}
1551-
return fld
1551+
sizes = sizes[0 : n+1]
1552+
sizes[n] = size
1553+
return fld, sizes
15521554
}
15531555

15541556
// Struct conversion: return Go and (gc) C syntax for type.
@@ -1559,6 +1561,7 @@ func (c *typeConv) Struct(dt *dwarf.StructType, pos token.Pos) (expr *ast.Struct
15591561
var buf bytes.Buffer
15601562
buf.WriteString("struct {")
15611563
fld := make([]*ast.Field, 0, 2*len(dt.Field)+1) // enough for padding around every field
1564+
sizes := make([]int64, 0, 2*len(dt.Field)+1)
15621565
off := int64(0)
15631566

15641567
// Rename struct fields that happen to be named Go keywords into
@@ -1594,7 +1597,7 @@ func (c *typeConv) Struct(dt *dwarf.StructType, pos token.Pos) (expr *ast.Struct
15941597
anon := 0
15951598
for _, f := range dt.Field {
15961599
if f.ByteOffset > off {
1597-
fld = c.pad(fld, f.ByteOffset-off)
1600+
fld, sizes = c.pad(fld, sizes, f.ByteOffset-off)
15981601
off = f.ByteOffset
15991602
}
16001603

@@ -1652,6 +1655,8 @@ func (c *typeConv) Struct(dt *dwarf.StructType, pos token.Pos) (expr *ast.Struct
16521655
ident[name] = name
16531656
}
16541657
fld[n] = &ast.Field{Names: []*ast.Ident{c.Ident(ident[name])}, Type: tgo}
1658+
sizes = sizes[0 : n+1]
1659+
sizes[n] = size
16551660
off += size
16561661
buf.WriteString(t.C.String())
16571662
buf.WriteString(" ")
@@ -1662,9 +1667,22 @@ func (c *typeConv) Struct(dt *dwarf.StructType, pos token.Pos) (expr *ast.Struct
16621667
}
16631668
}
16641669
if off < dt.ByteSize {
1665-
fld = c.pad(fld, dt.ByteSize-off)
1670+
fld, sizes = c.pad(fld, sizes, dt.ByteSize-off)
16661671
off = dt.ByteSize
16671672
}
1673+
1674+
// If the last field in a non-zero-sized struct is zero-sized
1675+
// the compiler is going to pad it by one (see issue 9401).
1676+
// We can't permit that, because then the size of the Go
1677+
// struct will not be the same as the size of the C struct.
1678+
// Our only option in such a case is to remove the field,
1679+
// which means that it can not be referenced from Go.
1680+
for off > 0 && sizes[len(sizes)-1] == 0 {
1681+
n := len(sizes)
1682+
fld = fld[0 : n-1]
1683+
sizes = sizes[0 : n-1]
1684+
}
1685+
16681686
if off != dt.ByteSize {
16691687
fatalf("%s: struct size calculation error off=%d bytesize=%d", lineno(pos), off, dt.ByteSize)
16701688
}

0 commit comments

Comments
 (0)