Skip to content

cmd/compile: make+copy optimization evaluates source expression before make panics #82708

Open

Description

@Ch1n-ch1nless

I am looking at a behavior change introduced by CL 806600
(merged as commit 98ef86b, "cmd/compile/internal/walk: expand makeslicecopy optimization").

I am not sure whether this behavior is considered acceptable. This report
focuses on how to reproduce it with two toolchains built from source.

Toolchains

I built two toolchains from the same repository:

  • before: the parent of 98ef86b779, commit ab4ad11bad
  • after: commit 98ef86b779 itself
$ GOROOT=/path/to/go-before /path/to/go-before/bin/go version
go version go1.28-devel_ab4ad11bad Fri Aug 14 19:53:41 2026 -0700 linux/amd64

$ GOROOT=/path/to/go-after /path/to/go-after/bin/go version
go version go1.28-devel_98ef86b779 Fri Aug 14 20:02:36 2026 -0700 linux/amd64

The toolchains were built with ./make.bash:

$ git clone https://go.googlesource.com/go
$ cd go
$ git worktree add ../go-before 98ef86b779^
$ git worktree add ../go-after 98ef86b779

$ cd ../go-before/src
$ GOROOT_BOOTSTRAP=/path/to/bootstrap ./make.bash

$ cd ../../go-after/src
$ GOROOT_BOOTSTRAP=/path/to/bootstrap ./make.bash

/path/to/bootstrap is an existing Go installation. Both toolchains are run
with an explicit GOROOT pointing at the corresponding build.

Main reproduction

package main

import "fmt"

var Calls int
var Sink []int

func src() []int {
	Calls++
	return []int{1, 2, 3}
}

func main() {
	defer func() {
		if r := recover(); r != nil {
			fmt.Printf("recover, calls = %d\n", Calls)
		}
	}()

	n := -1
	m := make([]int, n)
	copy(m, src())
	Sink = m
}

Running the same file with the two toolchains:

$ GOROOT=/path/to/go-before /path/to/go-before/bin/go run main.go
recover, calls = 0

$ GOROOT=/path/to/go-after /path/to/go-after/bin/go run main.go
recover, calls = 1
Build Output
parent ab4ad11 recover, calls = 0
CL 806600, commit 98ef86b recover, calls = 1

The source file is identical in both runs. The only difference is the compiler
build.

Variant: src itself panics

The difference is not limited to the Calls counter. If src panics, the
panic value changes:

package main

import "fmt"

var Sink []int

func src() []int {
	var p *int
	return []int{*p}
}

func main() {
	defer func() {
		fmt.Printf("recover: %v\n", recover())
	}()

	n := -1
	m := make([]int, n)
	copy(m, src())
	Sink = m
}
$ GOROOT=/path/to/go-before /path/to/go-before/bin/go run main.go
recover: runtime error: makeslice: len out of range

$ GOROOT=/path/to/go-after /path/to/go-after/bin/go run main.go
recover: runtime error: invalid memory address or nil pointer dereference

Variant: the source is a slice expression

The same difference appears without a function call, using a source expression
that can panic:

package main

import "fmt"

var Sink []int

func main() {
	defer func() {
		fmt.Printf("recover: %v\n", recover())
	}()

	var s []int
	n := -1
	m := make([]int, n)
	copy(m, s[10:])
	Sink = m
}
$ GOROOT=/path/to/go-before /path/to/go-before/bin/go run main.go
recover: runtime error: makeslice: len out of range

$ GOROOT=/path/to/go-after /path/to/go-after/bin/go run main.go
recover: runtime error: slice bounds out of range [10:0]

Summary

Program Parent ab4ad11 CL 806600 98ef86b
src() increments Calls calls = 0 calls = 1
src() dereferences a nil pointer makeslice: len out of range invalid memory address or nil pointer dereference
copy(m, s[10:]) with a short s makeslice: len out of range slice bounds out of range [10:0]

Is this behavior expected? If not, what would be the preferred way to limit the
optimization? I can send a follow-up CL.

Activity

  1. randall77 commented on Oct 29, 2026

    @randall77
    Contributor

    I agree that this is a bug. In your main reproducer, src should never be called.
    Not sure what the right fix would be.

  2. added this to the Go1.28 milestone on Oct 29, 2026
  3. Ch1n-ch1nless commented on Oct 29, 2026

    @Ch1n-ch1nless
    ContributorAuthor

    I agree that this is a bug. In your main reproducer, src should never be called.
    Not sure what the right fix would be.

    I also don’t see a good solution to this problem. Perhaps, at this stage, we should revert this commit and add the tests from the issue to the compiler, so that in the future, as this optimization is further developed, we can immediately gauge how safe it is.

  4. cuonglm commented on Oct 29, 2026

    @cuonglm
    Member

    I agree that this is a bug. In your main reproducer, src should never be called.
    Not sure what the right fix would be.

    Maybe just disable the optimization if either source or destination have side effects?

  5. JunyangShao commented on Oct 29, 2026

    @JunyangShao
    Contributor

    @golang/compiler

  6. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Oct 29, 2026
  7. gopherbot commented on Oct 29, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/856401 mentions this issue: cmd/compile: don't apply make+copy optimization to side-effecting operands

  8. added
    FixPendingIssues that have a fix which has not yet been reviewed or submitted.
    on Nov 3, 2026
  9. thepudds commented on Nov 3, 2026

    @thepudds
    Member

    Was just chatting with @JunyangShao about this and wanted to record here that I thought @cuonglm's suggestion of disabling the optimization based on side effects seems like a very reasonable approach, especially to fix the bug (and later work could try to expand the optimization if worthwhile).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixPendingIssues that have a fix which has not yet been reviewed or submitted.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.release-blocker

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions