Skip to content

maps: maps.Clone reference semantics when cloning a map with large value types #64474

Closed

Description

@santegoeds

Go version

go version go1.21.4 linux/amd64

What operating system and processor architecture are you using (go env)?

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/tdog/.cache/go-build'
GOENV='/home/tdog/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/tdog/.asdf/installs/golang/1.21.4/packages/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/tdog/.asdf/installs/golang/1.21.4/packages'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/tdog/.asdf/installs/golang/1.21.4/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/tdog/.asdf/installs/golang/1.21.4/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.21.4'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/tdog/dev/maps-clone/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 -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1194712858=/tmp/go-build -gno-record-gcc-switches'

What did you do?

https://go.dev/play/p/vkz2JOzn6zV

package main

import (
	"fmt"
	"maps"
)

type Value struct {
	Val     int
	padding [100]int
}

func main() {
	const key = "key"

	m := make(map[string]Value)

	m[key] = Value{Val: 1}

	c := maps.Clone(m)

	v := m[key]
	v.Val = 2
	m[key] = v

	fmt.Println(m[key].Val, c[key].Val)
}

Possibly related to https://go-review.googlesource.com/c/go/+/471400

What did you expect to see?

2 1

What did you see instead?

2 2

Activity

  1. added
    compiler/runtimeIssues related to the Go compiler and/or runtime.
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Nov 30, 2023
  2. bcmills commented on Nov 30, 2023

    @bcmills
    Contributor

    @gopherbot, please backport to Go 1.21. This appears to cause subtle aliasing bugs in cloned maps.

  3. gopherbot commented on Nov 30, 2023

    @gopherbot
    Contributor

    Backport issue(s) opened: #64475 (for 1.21).

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

  4. findleyr commented on Nov 30, 2023

    @findleyr
    Contributor

    (I made a minor correction to the original report, changing the expectation to 2 1 (from 1 2)).

  5. mauri870 commented on Nov 30, 2023

    @mauri870
    Member

    I can confirm this behavior changed after CL 471400 moved the Clone implementation to the runtime.

  6. randall77 commented on Nov 30, 2023

    @randall77
    Contributor
  7. mauri870 commented on Nov 30, 2023

    @mauri870
    Member

    The bug triggers this branch of mapclone2 https://go.googlesource.com/go/+/refs/heads/master/src/runtime/map.go#1512

    	if src.B == 0 {
    		dst.buckets = newobject(t.Bucket)
    		dst.count = src.count
    		typedmemmove(t.Bucket, dst.buckets, src.buckets)
    		return dst
    	}
  8. added this to the Go1.22 milestone on Nov 30, 2023
  9. gopherbot commented on Dec 1, 2023

    @gopherbot
    Contributor

    Change https://go.dev/cl/546515 mentions this issue: maps: fix aliasing problems with Clone

  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 Dec 1, 2023
  11. moved this to In Progress in Release Blockerson Dec 1, 2023
  12. moved this from In Progress to Done in Release Blockerson Dec 5, 2023
  13. added a commit that references this issue on Jan 25, 2024
    16d3040
  14. added a commit that references this issue on Feb 18, 2024
    f766a95
  15. locked and limited conversation to collaborators on Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.compiler/runtimeIssues related to the Go compiler and/or runtime.release-blocker

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions