Skip to content

cmd/compile: riscv64 miscompiles struct copy, corrupting a []byte slice field #80127

Description

@jasonmontleon

Go version

go version go1.26.4-X:nodwarf5 linux/amd64 (golang-1.26.4-2.fc44.x86_64)

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/jason/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/jason/.config/go/env'
GOEXE=''
GOEXPERIMENT='nodwarf5'
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build2783132525=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/dev/null'
GOMODCACHE='/home/jason/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/jason/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/golang'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/jason/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/usr/lib/golang/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.26.4-X:nodwarf5'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

I was looking into a couple kubernetes test failures on riscv64.
https://riscv-kojipkgs.fedoraproject.org/koji/buildinfo?buildID=92143

In particular

--- FAIL: TestValidateControllerRevisionUpdate (0.01s)
    --- FAIL: TestValidateControllerRevisionUpdate/changed_revision (0.00s)
        validation_test.go:1495: changed revision: unexpected error: [data: Invalid value: null: field is immutable]
FAIL

The test passes on other architectures and on riscv64 with -N and/or -l: -gcflags=all='-N -l'

$ CGO_ENABLED=0 GOOS=linux GOARCH=riscv64 GOFLAGS=-mod=vendor go test -gcflags=all='-N' -run 'TestValidateControllerRevisionUpdate$' ./pkg/apis/apps/validation
ok  	k8s.io/kubernetes/pkg/apis/apps/validation	(cached)
$ CGO_ENABLED=0 GOOS=linux GOARCH=riscv64 GOFLAGS=-mod=vendor go test -gcflags=all='-l' -run 'TestValidateControllerRevisionUpdate$' ./pkg/apis/apps/validation
ok  	k8s.io/kubernetes/pkg/apis/apps/validation	(cached)
$ CGO_ENABLED=0 GOOS=linux GOARCH=riscv64 GOFLAGS=-mod=vendor go test -run 'TestValidateControllerRevisionUpdate$' ./pkg/apis/apps/validation
--- FAIL: TestValidateControllerRevisionUpdate (0.05s)
    --- FAIL: TestValidateControllerRevisionUpdate/changed_revision (0.00s)
        validation_test.go:1500: changed revision: unexpected error: [data: Invalid value: null: field is immutable]
FAIL
FAIL	k8s.io/kubernetes/pkg/apis/apps/validation	0.478s
FAIL

claude cooked up the the following as a reproducer, although in this case only -N seems to cause it to work.

package main

import "fmt"

// ---- plain-Go analogs of the Kubernetes types (no external deps) ----

type myTime struct { // mimics time.Time inside metav1.Time
	wall uint64
	ext  int64
	loc  *int
}

// ObjectMeta mimics k8s.io/apimachinery metav1.ObjectMeta field-for-field
type ObjectMeta struct {
	Name, GenerateName, Namespace, SelfLink, UID, ResourceVersion string
	Generation                 int64
	CreationTimestamp          myTime
	DeletionTimestamp          *myTime
	DeletionGracePeriodSeconds *int64
	Labels                     map[string]string
	Annotations                map[string]string
	OwnerReferences            []int
	Finalizers                 []string
	ManagedFields              []int
}

// RawExtension mimics runtime.RawExtension; first field is a []byte slice
type RawExtension struct {
	Raw    []byte
	Object interface{}
}

// CR mimics apps.ControllerRevision: big struct, then RawExtension, then int64
type CR struct {
	ObjectMeta ObjectMeta
	Data       RawExtension
	Revision   int64
}

func main() {
	raw := RawExtension{Raw: []byte("0123456789")} // length 10

	// Two back-to-back composite literals, BOTH initialized from the same `raw`.
	a := CR{ObjectMeta: ObjectMeta{Name: "n", Namespace: "ns", ResourceVersion: "1"}, Data: raw, Revision: 0}
	b := CR{ObjectMeta: ObjectMeta{Name: "n", Namespace: "ns", ResourceVersion: "1"}, Data: raw, Revision: 1}

	fmt.Printf("a.Data.Raw = %q  (len %d)\n", a.Data.Raw, len(a.Data.Raw))
	fmt.Printf("b.Data.Raw = %q  (len %d)\n", b.Data.Raw, len(b.Data.Raw))

	if len(a.Data.Raw) == 10 && len(b.Data.Raw) == 10 {
		fmt.Println("RESULT: OK  - both slices intact (correct codegen)")
	} else {
		fmt.Println("RESULT: BUG - a slice was corrupted by the compiler (miscompile)")
	}
}
$ GOARCH=amd64 go run repro.go
a.Data.Raw = "0123456789"  (len 10)
b.Data.Raw = "0123456789"  (len 10)
RESULT: OK  - both slices intact (correct codegen)
$ CGO_ENABLED=0 GOOS=linux GOARCH=riscv64 go run -gcflags=all='-N' repro.go
a.Data.Raw = "0123456789"  (len 10)
b.Data.Raw = "0123456789"  (len 10)
RESULT: OK  - both slices intact (correct codegen)
$ CGO_ENABLED=0 GOOS=linux GOARCH=riscv64 go run repro.go
a.Data.Raw = "0123456789"  (len 10)
b.Data.Raw = "n"  (len 1)
RESULT: BUG - a slice was corrupted by the compiler (miscompile)
$ CGO_ENABLED=0 GOOS=linux GOARCH=riscv64 go run -gcflags=all='-l' repro.go
a.Data.Raw = "0123456789"  (len 10)
b.Data.Raw = "n"  (len 1)
RESULT: BUG - a slice was corrupted by the compiler (miscompile)

What did you see happen?

A kubernetes test fails with inlining and optimization enabled.

What did you expect to see?

The tests succeeding without disabling optimization/inlining.

Metadata

Metadata

Assignees

Labels

NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.compiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Fields

No fields configured for issues without a type.

Projects

Status
In Progress

Relationships

None yet

Development

No branches or pull requests

Issue actions