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
I'm afraid this code is type-punning uintptr and unsafe.Pointer in ways that are not allowed.
In particular, the write in (*[2]uintptr)(unsafe.Pointer(&t))[1] = stringTypePointer is writing a uintptr to a slot that is typed as an unsafe.Pointer. That is not allowed. Not the cause of this issue, but doing it this way means a required write barrier is missing, which could cause all sorts of GC corruption later on. For this issue, the compiler assumes that a uintptr and an unsafe.Pointer can never have the same address. That allows us to reorder this write with a subsequent read.
This code works:
package main
import (
"reflect"
"unsafe"
)
var stringTypePointer = reflect.ValueOf(reflect.TypeFor[string]()).UnsafePointer()
func main() {
t := reflect.TypeOf(0)
(*[2]unsafe.Pointer)(unsafe.Pointer(&t))[1] = stringTypePointer
println(t.String())
}
Not that I would recommend that either. You are reaching into the internals of interfaces. That could break at any time.
Reacted by Mateusz Poliwczak, Xinyu Liu, Alexander Musman and Andrey Bokhanko
Unfortunately, we simply can't guarantee the behavior of invalid code across Go releases. If we start doing that, we will soon be unable to make any changes at all.
Activity
[-]cmd/compile: incorrect optimization[/-][+]cmd/compile: incorrect optimization (go1.25rc2)[/+]mateusz834 commentedon Jul 10, 2025
Not sure how to judge the use of unsafe here, but:
I believe that the compiler should not generate the
v109 LEAQ.It seems to happen in
late opt:cuonglm commentedon Jul 10, 2025
git bisect points to https://go-review.googlesource.com/c/go/+/632176.
cc @randall77 @andreybokhanko
randall77 commentedon Jul 10, 2025
I'm afraid this code is type-punning
uintptrandunsafe.Pointerin ways that are not allowed.In particular, the write in
(*[2]uintptr)(unsafe.Pointer(&t))[1] = stringTypePointeris writing auintptrto a slot that is typed as anunsafe.Pointer. That is not allowed. Not the cause of this issue, but doing it this way means a required write barrier is missing, which could cause all sorts of GC corruption later on. For this issue, the compiler assumes that auintptrand anunsafe.Pointercan never have the same address. That allows us to reorder this write with a subsequent read.This code works:
Not that I would recommend that either. You are reaching into the internals of interfaces. That could break at any time.
AlphaLxy commentedon Jul 10, 2025
Although
StringHeaderhas been deprecated, another type of error can be constructed for the reasons described above.ianlancetaylor commentedon Jul 10, 2025
As the docs say,
reflect.StringHeadercannot be used safely. This program is an example of why that is true.AlphaLxy commentedon Jul 11, 2025
In the first example, the
uintptrvalues point to memory regions that are guaranteed not to be garbage collected.Although this usage is incorrect, it has been working fine in previous versions (<= 1.24). It seems to be a breaking change.
ianlancetaylor commentedon Jul 12, 2025
Unfortunately, we simply can't guarantee the behavior of invalid code across Go releases. If we start doing that, we will soon be unable to make any changes at all.