New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cgo: typedef pointer arguments regression #27340

Closed
ebfe opened this Issue Aug 29, 2018 · 8 comments

Comments

Projects
None yet
4 participants
@ebfe
Contributor

ebfe commented Aug 29, 2018

There seems to be a regression regarding calling C functions with typedef pointer arguments in go 1.11/1.10.4

The following code

package main

// typedef struct {
// 	int a;
// } S, *PTRS;
//
// void cfuncptr(PTRS s) {}
import "C"

func main() {
	var s C.S
	C.cfuncptr(C.PTRS(&s))
}

previously generated a C compiler warning (#19832). With go 1.11 (and go 1.10.4) this results in a build failure

$ go version
go version go1.11 linux/amd64

$ go run c.go
# command-line-arguments
cgo-gcc-prolog: In function '_cgo_89f4f567c189_Cfunc_cfuncptr':
cgo-gcc-prolog:40:17: warning: passing argument 1 of 'cfuncptr' from incompatible pointer type [-Wincompatible-pointer-types]
./c.go:7:21: note: expected 'PTRS' {aka 'struct <anonymous> *'} but argument is of type 'struct <anonymous> *'
 // void cfuncptr(PTRS s) {}
                ~~~~~^
# command-line-arguments
./c.go:12:31: cannot use _Ctype_PTRS(&s) (type _Ctype_PTRS) as type *_Ctype_struct___1 in argument to _Cfunc_cfuncptr
``
@ebfe

This comment has been minimized.

Contributor

ebfe commented Aug 29, 2018

git bisect points to 94076fe

@ebfe

This comment has been minimized.

Contributor

ebfe commented Aug 29, 2018

Splitting up the typedef fixes the warning and the build failure, but this is
of course not possible/difficult to do when interfacing with other libraries.

package main

// typedef struct { int a; } S;
// typedef S *PTRS;
//
// void cfuncptr(PTRS s) {}
import "C"

func main() {
	var s C.S
	C.cfuncptr(C.PTRS(&s))
}
@ianlancetaylor

This comment has been minimized.

Contributor

ianlancetaylor commented Aug 29, 2018

CC @randall77

Well this looks really painful.

@FiloSottile FiloSottile modified the milestones: Go1.11.1, Go1.12 Aug 30, 2018

@FiloSottile

This comment has been minimized.

Member

FiloSottile commented Aug 30, 2018

@gopherbot please file this to be considered for backport to 1.11 and 1.10. This is a regression.

@gopherbot

This comment has been minimized.

gopherbot commented Aug 30, 2018

Backport issue(s) opened: #27395 (for 1.11), #27396 (for 1.10).

Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://golang.org/wiki/MinorReleases.

@ianlancetaylor

This comment has been minimized.

Contributor

ianlancetaylor commented Nov 28, 2018

@randall77 Any ideas on this one?

@ianlancetaylor

This comment has been minimized.

Contributor

ianlancetaylor commented Dec 5, 2018

The breaking change seems to have been https://golang.org/cl/122575 for #24161.

Before that change _cgo_gotypes.go has this:

type _Ctype_PTRS *_Ctype_struct___0

type _Ctype_S = _Ctype_struct___0

type _Ctype_struct___0 struct {
	a _Ctype_int
}

func _Cfunc_cfuncptr(p0 *_Ctype_struct___0) (r1 _Ctype_void) {

After that change the file has this:

type _Ctype_PTRS *_Ctype_struct___0

type _Ctype_S = _Ctype_struct___0

type _Ctype_struct___0 struct {
	a _Ctype_int
}

type _Ctype_struct___1 struct {
	a _Ctype_int
}

func _Cfunc_cfuncptr(p0 *_Ctype_struct___1) (r1 _Ctype_void) {

Either way the generated .cgo1.go file has this:

	var s _Ctype_struct___0
	(_Cfunc_cfuncptr)(_Ctype_PTRS(&s))

So the problem seems to be this introduced _Ctype_struct___1 type.

@ianlancetaylor ianlancetaylor self-assigned this Dec 5, 2018

@gopherbot

This comment has been minimized.

gopherbot commented Dec 5, 2018

Change https://golang.org/cl/152762 mentions this issue: cmd/cgo: preserve type information across loadDWARF loop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment