-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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/compile: compiler assumes [2]T
does not alias with struct{a, b T}
which might be allowed by the unsafe
package
#65535
Comments
This happen because the compiler use a non saving copy to handle the copy:
I would guess It seems the compiler assumes structs are either not aliased at all or completely aliased (
I don't know if this could be a bug in unsafe's docs, or in the compiler. I don't know if it possible to manifest this bug without using |
I found this, so it looks like a sentence is needed in go/src/cmd/compile/internal/ssagen/ssa.go Lines 1382 to 1399 in 6076edc
|
This conversion already violates unsafe rules:
This violates rule 1, casting from a smaller to a larger pointed-to type. |
I did thought about that. |
Reinterpreting a |
In triage, we agree that the kind of conversion mentioned in this issue is a bit of a stretch. You could argue that converting [2]T to a struct{a, b T} is incorrectly assuming equivalent memory layouts, especially since the spec (technically) allows for things like reordering struct fields. |
[2]T
does not alias with struct{a, b T}
which might be allowed by the unsafe
package
We should perhaps consider clarifying this case in |
What about this code that treats any as [2]uintptr?(Currently any is equivalent to a structure of two Pointers, thus converted no problem or rely on existing compiler behavior?) |
@qiulaidongfeng That would almost certainly fall under the same non-guarantee. Layouts of multi-word builtin types (interfaces, slices, strings, complex) should not be guaranteed to be interchangeable by unsafe recasting. |
This might need to be reevaluated given #66408 |
Go version
go version devel go1.23-6076edc55c Mon Feb 5 20:59:15 2024 +0000 linux/amd64
Output of
go env
in your module/workspace:What did you do?
While investing #65495 I noticed that the compiler makes assumptions about aliasing of structs which are not documented in
unsafe
.See this piece of code:
What did you see happen?
What did you expect to see?
The text was updated successfully, but these errors were encountered: