You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Convert it into temporary (rvalue) string and with unsafe convert it into byte slice again
Then, copy this slice (by reference)
Then, do something with the second slice inside goroutine
Print the pointers before and after
And I have this output on my linux mint laptop with go 1.16:
go test ./pkg -v -count=1
=== RUN TestSomething
0xc000046720 123 0xc000046720 123
0xc000076f20 123 0xc000046721 z
--- PASS: TestSomething (0.84s)
PASS
ok github.com/leviska/go-unsafe-gc/pkg 0.847s
So, the first slice magically changes its address, while the second isn't
If we replace the goroutine with runtime.GC() (and may be play with the code a little bit), we can get the both pointers to change the value (to the same one).
If we change the unsafe cast to just []byte() everything works without changing the addresses. Also, if we change it to the unsafe cast from here https://stackoverflow.com/a/66218124/5516391 everything works as expected.
funcStringToByteUnsafe(strstring) []byte { // this works finevarbuf=*(*[]byte)(unsafe.Pointer(&str))
(*reflect.SliceHeader)(unsafe.Pointer(&buf)).Cap=len(str)
returnbuf
}
I run it with GOGC=off and got the same result. I run it with -race and got no errors.
If you run this as main package with main function, it seems to work correctly. Also if you remove the Convert function. My guess is that compiler optimizes stuff in this cases.
After playing with this code a little bit more, I think, that this can be a compiler bug or strange UB. Can you help me understand what's happening here? If it's not a bug, then
Why and how go runtime magically changes the address of the variable?
Why in concurentless case it can change both addresses, while in concurrent can't?
What's the difference between this unsafe cast and the cast from stackoverflow answer? Why it does work?
What version of Go are you using (go version)?
$ go version
go version go1.16.4 linux/amd64
What operating system and processor architecture are you using (go env)?
The backing store of a is allocated on stack, because it does not escape. And goroutine stacks can move dynamically. b, on the other hand, escapes to heap, because it is passed to another goroutine. In general, we don't assume the address of an object don't change.
This works as intended. Thanks.
If you have further questions, you will get better answers if you ask on a forum rather than on the issue tracker. See https://golang.org/wiki/Questions. Thanks.
This is a copy of this question https://stackoverflow.com/q/68401381/5516391 , because I convinced, that this looks like a compiler bug
I have this function to convert string to slice of bytes without copying
That works fine, but with very specific setup gives very strange behavior:
The setup is here: https://github.com/leviska/go-unsafe-gc/blob/main/pkg/pkg_test.go
What happens:
And I have this output on my linux mint laptop with go 1.16:
go test ./pkg -v -count=1 === RUN TestSomething 0xc000046720 123 0xc000046720 123 0xc000076f20 123 0xc000046721 z --- PASS: TestSomething (0.84s) PASS ok github.com/leviska/go-unsafe-gc/pkg 0.847s
So, the first slice magically changes its address, while the second isn't
If we replace the goroutine with
runtime.GC()
(and may be play with the code a little bit), we can get the both pointers to change the value (to the same one).If we change the unsafe cast to just
[]byte()
everything works without changing the addresses. Also, if we change it to the unsafe cast from here https://stackoverflow.com/a/66218124/5516391 everything works as expected.I run it with
GOGC=off
and got the same result. I run it with-race
and got no errors.If you run this as main package with main function, it seems to work correctly. Also if you remove the
Convert
function. My guess is that compiler optimizes stuff in this cases.After playing with this code a little bit more, I think, that this can be a compiler bug or strange UB. Can you help me understand what's happening here? If it's not a bug, then
What version of Go are you using (
go version
)?What operating system and processor architecture are you using (
go env
)?go env
OutputThe text was updated successfully, but these errors were encountered: