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
Closed

cmd/cgo: mishandles untyped constants in some cases #30527

remyoudompheng opened this issue Mar 1, 2019 · 6 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@remyoudompheng
Copy link
Contributor

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

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

@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 1, 2019
@ianlancetaylor ianlancetaylor added this to the Go1.13 milestone Mar 1, 2019
@ianlancetaylor
Copy link
Contributor

This may be hard to fix.

@remyoudompheng
Copy link
Contributor Author

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

Hmmm, good idea. Let me know. Thanks.

@gopherbot
Copy link

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

@gopherbot
Copy link

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>
@golang golang locked and limited conversation to collaborators Mar 5, 2020
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