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: mishandles untyped constants in some cases #30527

Closed
remyoudompheng opened this issue Mar 1, 2019 · 6 comments

Comments

@remyoudompheng
Copy link
Contributor

commented Mar 1, 2019

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

$ go version
go version go1.12 linux/amd64

Does this issue reproduce with the latest release?

Yes, but it does not happen with go 1.11.5

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

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/remy/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/remy/travail/go:/storage/remy/gopath"
GOPROXY=""
GORACE=""
GOROOT="/usr/lib/go"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
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-build301410002=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Create a 2-file package which invokes a C function with untyped constants.

% cat a.go
package pkg

import "fmt"
import "math"

/*
#include 

typedef struct {
	uint64_t a;
	uint64_t b;
	void *next;
} T;

uint64_t mul(T *x, uint64_t mod, uint32_t unused) {
	return (x->a * x->b);
}
*/
import "C"

func F() {
	t := &C.T{a: 1, b: 2, next: nil}

	n := C.mul(t, math.MaxUint64, 1)
	fmt.Println(n)
}

func G() {
	t := &C.T{a: 1, b: 2, next: nil}

	n := C.mul(t, 1<<64-1, z)
	fmt.Println(n)
}

% cat b.go
package pkg

const (
	x = 1 << iota
	y
	z
)

What did you expect to see?

Successful compilation (as with go1.11.5)

What did you see instead?

./a.go:24:94: cannot use _cgo1 (type int) as type _Ctype_ulong in argument to _Cfunc_mul
./a.go:24:22: constant 18446744073709551615 overflows int
./a.go:31:70: cannot use _cgo2 (type int) as type _Ctype_uint in argument to _Cfunc_mul

The error disappears if MaxUint64 is defined in the same package, or if z is defined in the same file.

@remyoudompheng

This comment has been minimized.

Copy link
Contributor Author

commented Mar 1, 2019

This issue was discovered in remyoudompheng/go-liblzma@da7c45f

@ianlancetaylor ianlancetaylor added this to the Go1.13 milestone Mar 1, 2019
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Mar 1, 2019

This may be hard to fix.

@remyoudompheng

This comment has been minimized.

Copy link
Contributor Author

commented Mar 1, 2019

I wonder if it can be fixed by the following kind of patch. Let me see if it passes tests, I may submit a CL.

                if !p.needsPointerCheck(f, param.Go, args[i]) {
-                       fmt.Fprintf(&sb, "_cgo%d := %s; ", i, gofmtPos(arg, origArg.Pos()))
+                       fmt.Fprintf(&sb, "var _cgo%d %s = %s; ", i,
+                               gofmt(ptype), gofmtPos(arg, origArg.Pos()))
                        continue
                }
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Mar 1, 2019

Hmmm, good idea. Let me know. Thanks.

@gopherbot

This comment has been minimized.

Copy link

commented Mar 1, 2019

Change https://golang.org/cl/164897 mentions this issue: cmd/cgo: simplify and fix handling of untyped constants

@gopherbot gopherbot closed this in 711ea1e Mar 6, 2019
@gopherbot

This comment has been minimized.

Copy link

commented Mar 6, 2019

Change https://golang.org/cl/165748 mentions this issue: [release-branch.go1.12] cmd/cgo: simplify and fix handling of untyped constants

gopherbot pushed a commit that referenced this issue Mar 13, 2019
… constants

Instead of trying to guess type of constants in the AST,
which is hard, use the "var cgo%d Type = Constant"
so that typechecking is left to the Go compiler.

The previous code could still fail in some cases
for constants imported from other modules
or defined in other, non-cgo files.

Fixes #30527

Change-Id: I2120cd90e90a74b9d765eeec53f6a3d2cfc1b642
Reviewed-on: https://go-review.googlesource.com/c/go/+/164897
Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
(cherry picked from commit 711ea1e)
Reviewed-on: https://go-review.googlesource.com/c/go/+/165748
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.