Skip to content
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 · 9 comments
Closed

cmd/cgo: typedef pointer arguments regression #27340

ebfe opened this issue Aug 29, 2018 · 9 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. release-blocker
Milestone

Comments

@ebfe
Copy link
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
Copy link
Contributor Author

ebfe commented Aug 29, 2018

git bisect points to 94076fe

@ebfe
Copy link
Contributor Author

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 ianlancetaylor added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. release-blocker labels Aug 29, 2018
@ianlancetaylor ianlancetaylor added this to the Go1.11.1 milestone Aug 29, 2018
@ianlancetaylor
Copy link
Contributor

CC @randall77

Well this looks really painful.

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

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

@gopherbot
Copy link

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
Copy link
Contributor

@randall77 Any ideas on this one?

@ianlancetaylor
Copy link
Contributor

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
Copy link

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

@gopherbot
Copy link

Change https://golang.org/cl/154277 mentions this issue: [release-branch.go1.11] cmd/cgo: preserve type information across loadDWARF loop

gopherbot pushed a commit that referenced this issue Dec 14, 2018
…dDWARF loop

CL 122575 and its successors introduced a loop calling loadDWARF,
whereas before we only called it once. Pass a single typeConv to each
call, rather than creating a new one in loadDWARF itself. Change the
maps from dwarf.Type to use string keys rather than dwarf.Type keys,
since when the DWARF is reloaded the dwarf.Type pointers will be
different. These changes permit typeConv.Type to return a consistent
value for a given DWARF type, avoiding spurious type conversion errors
due to typedefs loaded after the first loop iteration.

Updates #27340
Fixes #27395

Change-Id: Ic33467bbfca4c54e95909621b35ba2a58216d96e
Reviewed-on: https://go-review.googlesource.com/c/152762
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
(cherry picked from commit 6d43587)
Reviewed-on: https://go-review.googlesource.com/c/154277
Run-TryBot: Filippo Valsorda <filippo@golang.org>
@golang golang locked and limited conversation to collaborators Dec 14, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. release-blocker
Projects
None yet
Development

No branches or pull requests

4 participants