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

x/tools/go/loader: fails to correctly determine integer type size based on GOARCH #26257

Closed
kortschak opened this issue Jul 7, 2018 · 1 comment

Comments

@kortschak
Copy link
Contributor

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

go version go1.10.3 linux/arm

Does this issue reproduce with the latest release?

This is not feasible to test.

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

GOARCH="arm"
GOOS="linux"

What did you do?

Run go/loader to load a package on an arm device.

package main

import (
	"go/token"
	"log"

	"golang.org/x/tools/go/loader"
)

func main() {
	var conf loader.Config
	conf.Fset = token.NewFileSet()
	conf.Import("foo")
	_, err := conf.Load()
	if err != nil {
		log.Fatal(err)
	}
}

The package in foo makes reference to the size of int.

package main

const (
	MaxInt  = 1<<(IntBits-1) - 1
	IntBits = 1 << (^uint(0)>>32&1 + ^uint(0)>>16&1 + ^uint(0)>>8&1 + 3)
)

func main() {
	var u32 uint32
	u32 = MaxInt
	println(u32)
}

This is a valid Go program;

$ go run foo.go 
2147483647

but running the loader program above results in

$ go run loader.go 
/home/pi/src/foo/foo.go:10:8: MaxInt (untyped int constant 9223372036854775807) overflows uint32
2018/07/07 12:09:36 couldn't load packages due to errors: foo
exit status 1

What did you expect to see?

No output.

What did you see instead?

The error above and a non-zero exit status.

@gopherbot gopherbot added this to the Unreleased milestone Jul 7, 2018
@jimmyfrasche
Copy link
Member

This is correct.

go/loader uses go/types. go/types is sufficient to reproduce: https://play.golang.org/p/Vy-hXe7A7Q6

There is no error if you instruct it to use sizes for arm explicitly, however: https://play.golang.org/p/tYNZ4VEkCOw (or any other platform with a 4-byte word size).

If you don't set an explicit Size in the typechecker config, go/types uses SizesFor("gc", "amd64") regardless of the platform it's running on, hence the overflow.

@golang golang locked and limited conversation to collaborators Jul 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants