Skip to content

cmd/compile: &(*input) causes segfault #71056

Closed as not planned
Closed as not planned
@raphaelvigee

Description

@raphaelvigee

Go version

go version go1.23.4 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/me/Library/Caches/go-build'
GOENV='/Users/me/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/me/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/me/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.23.4/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.23.4/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.23.4'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/me/Library/Application Support/go/telemetry'
GCCGO='gccgo'
GOARM64='v8.0'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/Users/me/Documents/Code/project/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/p2/z4h94_g1745dxgtn7h72kjfr0000gn/T/go-build1265390623=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

package main

import (
	"slices"
	"sync"
)

type Child struct {
	Err error
	Something string
	Toy *Toy
	N int
}

type Toy struct {}

type Person struct {
	Children []Child
}

func clone(input *Person) *Person {
    // This variant causes the bug:
	//p := &(*input)
	//pp := p
    // This variant doesnt:
	p := *input
	pp := &p

	p.Children = slices.Clone(p.Children)

	for i, child := range p.Children {
		p.Children[i].Toy = &(*child.Toy)
	}

	return pp
}

func main() {
	p := &Person{Children: []Child{
		{N: 1, Toy: &Toy{}},
		{N: 2, Toy: &Toy{}},
		{N: 3, Toy: &Toy{}},
		{N: 4, Toy: &Toy{}},
		{N: 5, Toy: &Toy{}},
		{N: 6, Toy: &Toy{}},
	}}

	ch := make(chan struct{})

	var wg sync.WaitGroup
	for range 100000 {
		wg.Add(1)

		go func() {
			defer wg.Done()
			<-ch

			p := clone(p)

			p.Children = slices.DeleteFunc(p.Children, func(child Child) bool {
				return child.N %2 == 0
			})
		}()
	}

	close(ch)

	wg.Wait()
}

What did you see happen?

&(*input) caused segfault on p.Children[i].Toy = &(*child.Toy)

What did you expect to see?

*input and separate & doesnt

Metadata

Metadata

Assignees

No one assigned

    Labels

    compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions