cgo behaviour change between go1.11.10 and go1.12.5

94 views
Skip to first unread message

Neil Schellenberger

unread,
Jun 11, 2019, 12:14:30 AM6/11/19
to golang-nuts
Hi Folks,

When upgrading our build container from 1.11.10 to 1.12.5, some of our tests failed in a surprising way.  I've boiled down the issue to this short example code fragment.

package main

// #include <string.h>
// struct S { unsigned char data[18]; };
import "C"
import "unsafe"

func main() {
var array [5]C.struct_S
for i := range array {
C.memset(unsafe.Pointer(&array[i].data[0]), 0xff, C.sizeof_struct_S)
}
for i := range array {
for j := range array[i].data {
if array[i].data[j] != C.uchar(0xff) {
panic("oops")
}
}
}
}

Under 1.11 this runs to completion; under 1.12 it panics.

Debugging and instrumenting shows that the problem is the first argument to memset is not what one would expect when run under 1.12.  (The address of a heap temporary copy rather than the original perhaps?)

Strangely, either parenthesising the argument expression to the address operand ("&(array[i].data[0])") or removing the final index expression ("&array[i].data") "fixes" the problem.

I've spelunked through the AST (identical under both); the liveness and escape analysis (different); the parse tree (different); and the go asm (different).  Frankly, my gc-fu is very weak and I don't really know what I'm looking for.

I assume that the root cause is that we were violating some sort of liveness or escape policy, but I can't figure out what it would be (or why minor syntactic changes "fix" things).  I'd be quite curious to know.

The workaround in the real code is simple, so this isn't a huge issue, just a a bit of a mystery.

Regards,
Neil

Ian Lance Taylor

unread,
Jun 11, 2019, 1:21:51 AM6/11/19
to Neil Schellenberger, golang-nuts
It's a bug. Would you mind opening an issue with this test case at
https://golang.org/issue? Thanks.

Ian

Neil Schellenberger

unread,
Jun 12, 2019, 3:14:41 PM6/12/19
to Ian Lance Taylor, golang-nuts
Sorry for the delay in responding.  Filed as https://github.com/golang/go/issues/32579.

Ian Lance Taylor

unread,
Jun 12, 2019, 8:05:45 PM6/12/19
to Neil Schellenberger, golang-nuts
On Wed, Jun 12, 2019 at 12:14 PM Neil Schellenberger
<neilschel...@gmail.com> wrote:
>
> Sorry for the delay in responding. Filed as https://github.com/golang/go/issues/32579.

Thanks.

Ian
Reply all
Reply to author
Forward
0 new messages