Skip to content

Commit

Permalink
set up go/types.Config.Sizes according to GOARCH
Browse files Browse the repository at this point in the history
Otherwise we miscalculate int sizes, type sizes, alignments, and so on.
Caught by the GOARCH=386 go test on CI, since the os package imports
internal/syscall/unix, which uses arch-dependent padding.

The different padding between our incorrect use of go/types
and the correct typechecking done by the compiler caused different
obfuscation of fields, as the struct types stringified differently,
and they are used as a hash salt for field name obfuscation.
  • Loading branch information
mvdan committed Sep 4, 2024
1 parent 4fcce60 commit 48fac78
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
9 changes: 6 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1668,7 +1668,10 @@ func typecheck(pkgPath string, files []*ast.File, origImporter importerWithMap)
Instances: make(map[*ast.Ident]types.Instance),
}
// TODO(mvdan): we should probably set types.Config.GoVersion from go.mod
origTypesConfig := types.Config{Importer: origImporter}
origTypesConfig := types.Config{
Importer: origImporter,
Sizes: types.SizesFor("gc", sharedCache.GoEnv.GOARCH),
}
pkg, err := origTypesConfig.Check(pkgPath, fset, files, info)
if err != nil {
return nil, nil, fmt.Errorf("typecheck error: %v", err)
Expand Down Expand Up @@ -2367,8 +2370,8 @@ func flagSetValue(flags []string, name, value string) []string {

func fetchGoEnv() error {
out, err := exec.Command("go", "env", "-json",
// Keep in sync with sharedCache.GoEnv.
"GOOS", "GOMOD", "GOVERSION", "GOROOT",
// Keep in sync with [sharedCacheType.GoEnv].
"GOOS", "GOARCH", "GOMOD", "GOVERSION", "GOROOT",
).CombinedOutput()
if err != nil {
// TODO: cover this in the tests.
Expand Down
3 changes: 2 additions & 1 deletion shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ type sharedCacheType struct {
// Filled directly from "go env".
// Keep in sync with fetchGoEnv.
GoEnv struct {
GOOS string // i.e. the GOOS build target
GOOS string // the GOOS build target
GOARCH string // the GOARCH build target

GOMOD string
GOVERSION string
Expand Down
10 changes: 10 additions & 0 deletions testdata/script/crossbuild.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,13 @@ import "net/http"
func main() {
http.ListenAndServe("", nil)
}

-- 32bit.go --
//go:build arm

package main

// Will give "out of bounds" if we don't correctly set up types.Config.Sizes.
const is64bit = ^uint(0) >> 63 // 0 for 32-bit hosts, 1 for 64-bit ones.
var x [1]struct{}
var _ = x[is64bit]

0 comments on commit 48fac78

Please sign in to comment.