Skip to content

cmd/compile: memory corruption when setting outer values in functional iterators #70035

Closed

Description

@scr-oath

Go version

go version go1.23.2 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE='on'
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/scr/Library/Caches/go-build'
GOENV='/Users/scr/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/scr/.asdf/installs/golang/1.23.2/packages/pkg/mod'
GONOPROXY='go.ouryahoo.com'
GONOSUMDB='go.ouryahoo.com'
GOOS='darwin'
GOPATH='/Users/scr/.asdf/installs/golang/1.23.2/packages'
GOPRIVATE='go.ouryahoo.com'
GOPROXY='https://goproxy.ouryahoo.com:4443/goproxy,https://proxy.golang.org,direct'
GOROOT='/Users/scr/.asdf/installs/golang/1.23.2/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/scr/.asdf/installs/golang/1.23.2/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.23.2'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/scr/Library/Application Support/go/telemetry'
GCCGO='gccgo'
GOARM64='v8.0'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/scr/GolandProjects/scr/show-iteration-failure/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/wj/29xj08zd0vn3trjdyh197h_00000gq/T/go-build942010698=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

Wrote and ran this test

package bug

import (
	"slices"
	"testing"
)

func Bug() string {
	var ret string
	prefixes := []string{"prefix"}
	stringValues := []string{"foo", "bar", "baz"}
	for prefix := range slices.Values(prefixes) {
		var joined string
		for stringValue := range slices.Values(stringValues) {
			if joined == "" {
				joined = stringValue
			} else {
				joined = joined + "_" + stringValue
			}
		}
		ret = prefix + "+" + joined
	}
	return ret
}

func Test_Bug(t *testing.T) {
	got := Bug()
	want := "prefix+foo_bar_baz"
	if got != want {
		t.Errorf("Bug() = %v, want %v", got, want)
	}
}

What did you see happen?

image
=== RUN   Test_Bug
    bug_test.go:30: Bug() = prefix+foo_bar_IJ�, want prefix+foo_bar_baz
--- FAIL: Test_Bug (0.00s)

FAIL

What did you expect to see?

I expected not to see a result that contained uninitialized memory - it's different every time and seems to refer to bad bytes.

Activity

  1. changed the title [-]iter.Seq: setting outer values in functional iterators seem to have memory issues[/-] [+]cmd/compile: memory corruption when setting outer values in functional iterators[/+] on Oct 25, 2024
  2. prattmic commented on Oct 25, 2024

    @prattmic
    Member

    cc @golang/compiler

  3. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Oct 25, 2024
  4. dmitris commented on Oct 25, 2024

    @dmitris
    Contributor

    Playground version: https://go.dev/play/p/SrpUKjbzHq4
    If you uncomment any of the two fmt.Println(joined) lines, it works as expected (ex. https://go.dev/play/p/sCV-WRD9S5y), same if using Sprintf instead of combining strings with +.

  5. scr-oath commented on Oct 25, 2024

    @scr-oath
    Author

    Curiously enough, changing

    joined = joined + "_" + stringValue

    to

    joined += "_" + stringValue

    Seems to avoid this problem - maybe that's a clue?

  6. fengyoulin commented on Oct 25, 2024

    @fengyoulin
    Member

    It seems that currently nested rangefunc, function inline and escape analysis do not work well together.

    I can reproduce this problem on the master branch code using the following code:

    package main
    
    import (
    	"fmt"
    	"slices"
    )
    
    func Bug(s1, s2, s3 []string) string {
    	var c1 string
    	for v1 := range slices.Values(s1) {
    		var c2 string
    		for v2 := range slices.Values(s2) {
    			var c3 string
    			for v3 := range slices.Values(s3) {
    				c3 = c3 + v3
    			}
    			c2 = c2 + v2 + c3
    		}
    		c1 = c1 + v1 + c2
    	}
    	return c1
    }
    
    func main() {
    	got := Bug([]string{"1", "2", "3"}, []string{"a", "b", "c"}, []string{"A", "B", "C"})
    	want := "1aABCbABCcABC2aABCbABCcABC3aABCbABCcABC"
    	if got != want {
    		fmt.Println(fmt.Errorf("got %v, want %v", got, want))
    	}
    }
    

    But, you can avoid this problem, by -gcflags='-l'.
    Some calls to runtime.concatstring3 use on stack buffers, that should have been moved to the heap.

  7. dr2chase commented on Oct 25, 2024

    @dr2chase
    Contributor

    I suspect this involves outlives and containsClosure in escape/solve.go and I am looking there.

  8. cuonglm commented on Oct 25, 2024

    @cuonglm
    Member

    I suspect this involves outlives and containsClosure in escape/solve.go and I am looking there.

    This seems to be the nested rangefunc name generation. For example, the parent is Bug-range1, the child is Bug-range2, and with current rule, Bug-range1 does not contain Bug-range2, while it should.

  9. self-assigned this
    on Oct 25, 2024
  10. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    and removed
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Oct 25, 2024
  11. dr2chase commented on Oct 25, 2024

    @dr2chase
    Contributor

    I think I have a fix, sorry.

    I just make an explicit parent pointer, initialize it in deadlocals, use that.

  12. self-assigned this
    on Oct 25, 2024
  13. gopherbot commented on Oct 25, 2024

    @gopherbot
    Contributor

    Change https://go.dev/cl/622715 mentions this issue: cmd/compile: fix wrong escape analysis for nested rangefunc

  14. cuonglm commented on Oct 25, 2024

    @cuonglm
    Member

    I think I have a fix, sorry.

    I just make an explicit parent pointer, initialize it in deadlocals, use that.

    Ops, I have sent https://go-review.googlesource.com/c/go/+/622715.

    Feel free to abadon if you prefer your fix.

  15. gopherbot commented on Oct 25, 2024

    @gopherbot
    Contributor

    Change https://go.dev/cl/622656 mentions this issue: cmd/compile: use a non-fragile test for "does f contain closure c?"

  16. added a commit that references this issue on Oct 26, 2024
    889abb1
  17. cuonglm commented on Oct 26, 2024

    @cuonglm
    Member

    @dr2chase @randall77 Should we backport this?

  18. zigo101 commented on Nov 5, 2024

    @zigo101
    Contributor
  19. randall77 commented on Nov 5, 2024

    @randall77
    Contributor

    Probably.
    @gopherbot Please open a backport issue to 1.23.

    This causes a serious miscompilation in certain range-over-func situations.

  20. gopherbot commented on Nov 5, 2024

    @gopherbot
    Contributor

    Backport issue(s) opened: #70198 (for 1.23).

    Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://go.dev/wiki/MinorReleases.

  21. added this to the Go1.24 milestone on Nov 5, 2024
  22. added
    CriticalA critical problem that affects the availability or correctness of production systems built using Go
    on Nov 5, 2024
  23. gopherbot commented on Jan 3, 2025

    @gopherbot
    Contributor

    Change https://go.dev/cl/640036 mentions this issue: gopls: update toolchain to go1.23.4

  24. added a commit that references this issue on Jan 3, 2025
  25. locked and limited conversation to collaborators on Jan 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

CriticalA critical problem that affects the availability or correctness of production systems built using GoFrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.compiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions