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: fails to generate certain types with Go 1.14 #37621

Closed
tklauser opened this issue Mar 3, 2020 · 11 comments
Closed

cmd/cgo: fails to generate certain types with Go 1.14 #37621

tklauser opened this issue Mar 3, 2020 · 11 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@tklauser
Copy link
Member

tklauser commented Mar 3, 2020

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

$ go version
go version go1.14 linux/amd64

Does this issue reproduce with the latest release?

Yes, it also reproduces with tip. However, it doesn't reproduce with go1.13.8

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/tklauser/.cache/go-build"
GOENV="/home/tklauser/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/tklauser/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="/home/tklauser/src/go/src/go.mod"
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-build562307077=/tmp/go-build -gno-record-gcc-switches"

What did you do?

$ cat repro.go
package unix

/*
#include <time.h>
#include <linux/errqueue.h>
*/
import "C"

type Timespec C.struct_timespec

type ScmTimestamping C.struct_scm_timestamping

$ go1.14 tool cgo -godefs repro.go
// Code generated by cmd/cgo -godefs; DO NOT EDIT.
// cgo -godefs cgo-fail-go1.14.go

package unix

type Timespec struct {
	Sec	int64
	Nsec	int64
}

type ScmTimestamping struct {
	Ts [3]_Ctype_struct_timespec
}

For some reason cgo is unable to resolve __Ctype_struct_timespec to Timespec in Go 1.14, while it does with Go 1.13 (see below). This looks like a regression to me.

Found while trying to update the code generation in golang.org/x/sys/unix to use Go 1.14.

What did you expect to see?

$ go1.13.8 tool cgo -godefs repro.go
// Code generated by cmd/cgo -godefs; DO NOT EDIT.
// cgo -godefs cgo-fail-go1.14.go

package unix

type Timespec struct {
	Sec	int64
	Nsec	int64
}

type ScmTimestamping struct {
	Ts [3]Timespec
}

What did you see instead?

See output of go1.14 tool cgo -godefs repro.go above.

@tklauser tklauser added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 3, 2020
@tklauser
Copy link
Member Author

tklauser commented Mar 3, 2020

/cc @ianlancetaylor

@tklauser
Copy link
Member Author

tklauser commented Mar 3, 2020

@gopherbot please consider this for backport to 1.14, it's a regression.

@gopherbot
Copy link
Contributor

Backport issue(s) opened: #37622 (for 1.14).

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

@tklauser
Copy link
Member Author

tklauser commented Mar 3, 2020

Found while trying to update the code generation in golang.org/x/sys/unix to use Go 1.14.

FWIW, this was the resulting diff when I tried to regenerate the files in x/sys/unix:

diff --git a/unix/linux/Dockerfile b/unix/linux/Dockerfile
index 0e4a930959aa..e93b354c56ab 100644
--- a/unix/linux/Dockerfile
+++ b/unix/linux/Dockerfile
@@ -18,9 +18,9 @@ RUN git clone --branch v5.5 --depth 1 https://kernel.googlesource.com/pub/scm/li
 RUN git clone --branch release/2.31/master --depth 1 git://sourceware.org/git/glibc.git
 
 # Get Go
-ENV GOLANG_VERSION 1.13.7
+ENV GOLANG_VERSION 1.14
 ENV GOLANG_DOWNLOAD_URL https://golang.org/dl/go$GOLANG_VERSION.linux-amd64.tar.gz
-ENV GOLANG_DOWNLOAD_SHA256 b3dd4bd781a0271b33168e627f7f43886b4c5d1c794a4015abf34e99c6526ca3
+ENV GOLANG_DOWNLOAD_SHA256 08df79b46b0adf498ea9f320a0f23d6ec59e9003660b4c9c1ce8e5e2c6f823ca
 
 RUN curl -fsSL "$GOLANG_DOWNLOAD_URL" -o golang.tar.gz \
     && echo "$GOLANG_DOWNLOAD_SHA256  golang.tar.gz" | sha256sum -c - \
diff --git a/unix/ztypes_linux.go b/unix/ztypes_linux.go
index 6c81e7515c87..ee0f94a1f393 100644
--- a/unix/ztypes_linux.go
+++ b/unix/ztypes_linux.go
@@ -1757,7 +1757,7 @@ const (
 )
 
 type ScmTimestamping struct {
-       Ts [3]Timespec
+       Ts [3]_Ctype_struct_timespec
 }
 
 const (
diff --git a/unix/ztypes_linux_s390x.go b/unix/ztypes_linux_s390x.go
index a111387b3ac5..44d756de41c7 100644
--- a/unix/ztypes_linux_s390x.go
+++ b/unix/ztypes_linux_s390x.go
@@ -165,12 +165,12 @@ const (
 )
 
 type PtraceRegs struct {
-       Psw                      PtracePsw
+       Psw                      _Ctype_struct__user_psw_struct
        Gprs                     [16]uint64
        Acrs                     [16]uint32
        Orig_gpr2                uint64
-       Fp_regs                  PtraceFpregs
-       Per_info                 PtracePer
+       Fp_regs                  _Ctype_struct__user_fpregs_struct
+       Per_info                 _Ctype_struct__user_per_struct
        Ieee_instruction_pointer uint64
 }

@ianlancetaylor
Copy link
Member

See also #37479. I guess we need to figure this out.

@bcmills
Copy link
Contributor

bcmills commented Mar 4, 2020

Possibly a side-effect of CL 181857?

@tklauser
Copy link
Member Author

tklauser commented Mar 5, 2020

Possibly a side-effect of CL 181857?

Yes, CL 181857 seems to have introduced this regression. If I revert the CL, the generated code is as expected.

@ianlancetaylor also traced down #37479 to the same CL.

@dmitshur dmitshur added this to the Go1.15 milestone Mar 5, 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
Contributor

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

@gopherbot
Copy link
Contributor

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

@gopherbot
Copy link
Contributor

Change https://golang.org/cl/226577 mentions this issue: unix: update Dockerfile to Linux 5.6, and Go 1.13.9

gopherbot pushed a commit to golang/sys that referenced this issue Mar 31, 2020
We cannot update to Go 1.14 yet, since cgo code generation for certain
types is broken due to golang/go#37621.

Change-Id: I7b60d23ff4e79a7bb7fa4f0e0625893e4eff81f1
Reviewed-on: https://go-review.googlesource.com/c/sys/+/226577
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matt Layher <mdlayher@gmail.com>
@gopherbot
Copy link
Contributor

Change https://golang.org/cl/227678 mentions this issue: unix: update Dockerfile to Go 1.14.2

gopherbot pushed a commit to golang/sys that referenced this issue Apr 9, 2020
Go 1.14.2 fixes golang/go#37621 which allows the Go types to be
generated properly again.

Change-Id: I556c7dd37713d68ef6aad79adc33b2d227e8bbd3
Reviewed-on: https://go-review.googlesource.com/c/sys/+/227678
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Matt Layher <mdlayher@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
@golang golang locked and limited conversation to collaborators Apr 8, 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

5 participants