-
Notifications
You must be signed in to change notification settings - Fork 59
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
Should char
have the same layout as u32
?
#462
Comments
Yes, I've written plenty of code that relies on this. If we don't write this down anywhere, we should. |
I'm not sure how it couldn't? |
For the purposes of justifying unsafe code, I think it's not tightly worded enough. My interpretation is that |
Note that you are asking two different questions:
However, this is really more of a t-lang thing than a t-opsem thing. It's about making a stable commitment about the layout and ABI of |
Is that true? Which targets is this the case? (Is it just the case due to CFI stuff?) |
I think it was on s390x that u32 gets zero-extended and i32 gets sign-extended, both passed in a larger register. So if a function takes a u32 it can assume that all the high bits are 0, and if the caller passes an i32 then it can violate that assumption, making them not ABI-compatible. See rust-lang/rust#115476 for which types we can actually guarantee are ABI-compatible. |
For this particular issue I would suggest opening a PR which adds a statement to the docs saying explicitly that char is fully layout and ABI-compatible with I didn't add
FWIW latest Miri does not consider u32 and char to be ABI-compatible, it will error in such cases. |
Thanks for clarifying @RalfJung, I understand the difference between layout compatibility and ABI compatibility much better now. I only care about |
I would like |
Yeah that makes sense. For t-opsem there's nothing to do here since the operational semantics already do what you are asking for. You just need to get the lang team to commit to this as a stable guarantee. Filing the PR to make that request should be very easy. :) |
We would also benefit from the alignment guarantee in zerocopy. IIUC, the only gaps are:
Are there any other facts about |
The function call ABI of |
rust-lang/rust#116894 and rust-lang/rust#118032 are in 1.75 and 1.76, respectively, and guarantee layout-compatbility (size and alignment) and function call ABI-compatibility, respectively. "are all of the bytes of |
Yeah this question is settled, char is exactly like u32 but with a more restrictive validity invariant. |
It's documented here. |
I'd like to be able to make the assumption that
char
is ABI compatible withu32
for some validation code in bytecheck. I have actually been making this assumption for a while and never had problems (including with MIRI), so I think this is de-facto the case. I thought thatchar
andu32
were supposed to have the same layout but when I went looking I realized I couldn't find anything. As far as I know, it doesn't really make sense forchar
s to be anything other than au32
with fewer valid bit patterns.For completeness:
u32
andchar
are four bytes.u32
.The text was updated successfully, but these errors were encountered: