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: -godefs: incorrect output when using TAILQ_ENTRY #37479

Closed
yoursunny opened this issue Feb 26, 2020 · 5 comments
Closed

cmd/cgo: -godefs: incorrect output when using TAILQ_ENTRY #37479

yoursunny opened this issue Feb 26, 2020 · 5 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@yoursunny
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.14 linux/amd64

$ gcc --version
gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0

Does this issue reproduce with the latest release?

Yes, this issue appears since Go 1.14, but did not happen with Go 1.13.8.

What operating system and processor architecture are you using (go env)?

I have Ubuntu 18.04 on a server with Xeon Gold 6240 CPU.

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/user/.cache/go-build"
GOENV="/home/user/config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/user/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build148938248=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I am executing go tool cgo -godefs x.go command on the following input:

// +build ignore

package demo

/*
#include <stdbool.h>
#include <stdint.h>
#include <sys/queue.h>

typedef struct A A;

typedef TAILQ_ENTRY(A) N;

struct A
{
  N n;
};

typedef struct B
{
  A* a;
} B;
*/
import "C"

type N C.N

type A C.A

type B C.B

What did you expect to see?

I expect to see this output. This was the output from Go 1.13.8.

// Code generated by cmd/cgo -godefs; DO NOT EDIT.
// cgo -godefs x.go

package demo

type N struct {
	Next	*A
	Prev	**A
}

type A struct {
	N N
}

type B struct {
	A *A
}

What did you see instead?

I see this output instead. Notice that in type B, the variable A has type *_Ctype_struct_A, instead of the expected type *A.

// Code generated by cmd/cgo -godefs; DO NOT EDIT.
// cgo -godefs x.go

package demo

type N struct {
	Next	*A
	Prev	**A
}

type A struct {
	N N
}

type B struct {
	A *_Ctype_struct_A
}

Further information

It seems that this problem is correlated to TAILQ_ENTRY macro. If type A does not contain a variable of a type defined by this macro, the problem does not occur.

If I manually expand the macro into the snippet below, the problem persists.

typedef struct N {
  struct A* tqe_next;
  struct A** tqe_prev;
} N;
// Go 1.14 does not work with this version.

However, if I remove struct like the snippet below, the problem disappears.

typedef struct N {
  A* tqe_next;
  A** tqe_prev;
} N;
// Go 1.14 works with this version.
@ianlancetaylor ianlancetaylor changed the title cgo godefs: incorrect output when using TAILQ_ENTRY cmd/cgo: -godefs: incorrect output when using TAILQ_ENTRY Feb 26, 2020
@ianlancetaylor
Copy link
Contributor

Standalone test case:

package demo

/*
typedef struct A A;

typedef struct {
	struct A *next;
	struct A **prev;
} N;

struct A
{
  N n;
};

typedef struct B
{
  A* a;
} B;
*/
import "C"

type N C.N

type A C.A

type B C.B

@ianlancetaylor
Copy link
Contributor

This is caused by https://golang.org/cl/181857 for issue #31891.

CC @letientai299

@ianlancetaylor
Copy link
Contributor

The -godefs option is not actually supported for anything other than its documented purpose of generating files in the syscall package. I don't mind if somebody sends in a fix for this, but this is not a priority of the Go team. Sorry.

I'll add that I'm honestly not sure how -godefs is expected to work in the presence of a combination of typedefs and direct struct references.

@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 26, 2020
@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Feb 26, 2020
yoursunny added a commit to yoursunny/ndn-dpdk that referenced this issue Feb 26, 2020
tklauser added a commit to tklauser/go that referenced this issue Mar 29, 2020
CL 181857 broke the translation of certain C types when using cmd/cgo
-godefs.

Updates golang#31891
Fixes golang#37479
Fixes golang#37621

Change-Id: I301a749ec89585789cb0d213593bb8b7341beb88
@gopherbot
Copy link

Change https://golang.org/cl/226341 mentions this issue: cmd/cgo, misc/cgo: only cache anonymous struct typedefs with parent name

@gopherbot
Copy link

Change https://golang.org/cl/226497 mentions this issue: cmd/cgo, misc/cgo: only cache anonymous struct typedefs with parent name

@golang golang locked and limited conversation to collaborators Mar 30, 2021
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.
Projects
None yet
Development

No branches or pull requests

3 participants