Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cgo: memcpy to &a[0] does not change array #30065

Closed
cclerget opened this issue Feb 2, 2019 · 2 comments

Comments

@cclerget
Copy link

commented Feb 2, 2019

What version of Go are you using (go version)?

$ go version
go version go1.12beta2 linux/amd64

Does this issue reproduce with the latest release?

No

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

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="off"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/ced/go"
GOPROXY=""
GORACE=""
GOROOT="/tmp/go"
GOTMPDIR=""
GOTOOLDIR="/tmp/go/pkg/tool/linux_amd64"
GCCGO="/usr/bin/gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build422388811=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Run the following snippet : https://play.golang.org/p/hQMcxNvQ29Y

What did you expect to see?

$ go run /tmp/repro.go
Each line should display: works
1: works
2: works

Each line should display: works too
3: works too
4: works too

Works with 1.9, 1.10, 1.11 but fails since 1.12

What did you see instead?

$ go run /tmp/repro.go
Each line should display: works
1: 
2: works

Each line should display: works too
3: works
4: works too
@randall77

This comment has been minimized.

Copy link
Contributor

commented Feb 2, 2019

@ianlancetaylor
Looks like something weird is going on with cgo.

The anonymous function created for the first C.memcpy call makes a copy of the config array, and then uses that as the destination of the copy.

func() _cgo_unsafe.Pointer{
	_cgoIndex0 := /*line :15:27*/config
	_cgo0 := /*line :15:11*/unsafe.Pointer(&_cgoIndex0[0])
	_cgo1 := func() _cgo_unsafe.Pointer{
		_cgo0 := /*line :15:48*/d
		_cgoCheckPointer(_cgo0)
		return _Cfunc_CBytes(_cgo0)
	}()
	_cgo2 := /*line :15:52*/l
	_cgoCheckPointer(_cgo0, _cgoIndex0)
	_cgoCheckPointer(_cgo1)
	return _Cfunc_memcpy(_cgo0, _cgo1, _cgo2)
}()

That doesn't look right. Go 1.11 did:

func(_cgo0 _cgo_unsafe.Pointer, _cgo1 _cgo_unsafe.Pointer, _cgo2 _Ctype_size_t) _cgo_unsafe.Pointer {
	_cgoCheckPointer(_cgo0, config)
        _cgoCheckPointer(_cgo1)
        return (_Cfunc_memcpy)(_cgo0, _cgo1, _cgo2)
}(unsafe.Pointer(&config[0]), func(_cgo0 []byte) _cgo_unsafe.Pointer {
	_cgoCheckPointer(_cgo0)
        return (_Cfunc_CBytes)(_cgo0)
}(d), l)

Which looks correct.

@ianlancetaylor ianlancetaylor changed the title Unexpected execution result depending of syntax cmd/cgo: memcpy to &a[0] does not change array Feb 2, 2019

@ianlancetaylor ianlancetaylor self-assigned this Feb 2, 2019

@ianlancetaylor ianlancetaylor added this to the Go1.12 milestone Feb 2, 2019

@gopherbot

This comment has been minimized.

Copy link

commented Feb 3, 2019

Change https://golang.org/cl/160830 mentions this issue: cmd/cgo: don't copy a simple variable x in &x[0]

@gopherbot gopherbot closed this in 3fc276c Feb 4, 2019

nebulabox added a commit to nebulabox/go that referenced this issue Feb 18, 2019
cmd/cgo: don't copy a simple variable x in &x[0]
Fixes golang#30065

Change-Id: I3d0fb03bab397548653d5f3b386cfe2980ac1030
Reviewed-on: https://go-review.googlesource.com/c/160830
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
nebulabox added a commit to nebulabox/go that referenced this issue Feb 20, 2019
cmd/cgo: don't copy a simple variable x in &x[0]
Fixes golang#30065

Change-Id: I3d0fb03bab397548653d5f3b386cfe2980ac1030
Reviewed-on: https://go-review.googlesource.com/c/160830
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.