-
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
What *exactly* is the behavior of reading from padding data? #395
Comments
I believe that the current position is that a typed copy of
Footnotes
|
Because you're working on learning materials: I think it is important to lay out and be very clear that Rust has (or the current position is that Rust has) typed reads/writes, and does not have typed memory. I think your initial confusion/uncertainty on these questions is a good demonstration of this. In my experience, newcomers to this subject really want to reason about the rules here as if Rust has typed memory, and it can be challenging to get out of that line of thinking. |
Should be uninitialized state, but otherwise I agree.
👍 |
There are cases in windows APIs where we need to perform reads of padding bytes. In particular, these usually involve a tail So certainly typed memory preventing padding bytes from being accessed would be bad here. |
... what kind of typo had me write "initialized" instead of "uninitialized" 🙃 |
Yeah this is my understanding, but I've heard previously that padding is weird. I suspect it's just weird because it's one of the situations where this really gets tricky.
Sorry, the UCG team has made its ruling, everything is initialized now. |
It sounds like as a UCG issue this is resolved, but I'll leave this issue open so I can improve the glossary. |
Also I assume |
For those situations you're not going to do copies at that struct type though? That would obviously fail to copy the flexible array part. So I don't see how there's a problem with padding here. FWIW, padding is similarly reset during struct assignments in C. |
I've been working under the assumption that "padding bytes are uninit" and while you can write to them just fine, reading from them is always uninit.
I'm getting this in part from the glossary:
@djkoloski has another plausible-sounding read of the situation:
His contention is that if you know what bytes have been written to the padding area, it's safe to read them, for example in this code.
While trying to figure out how to teach uninitialized values in Rust, we need to know more precise semantics.
I suspect the best way to tease this apart might be to pose a bunch of scenarios, and fish for a Yes/No/Maybe/Depends answer ("Maybe" = "UCG hasn't ruled on specifics here, see <issue>", "Depends" = "not enough information in the example to give a single answer").
Let's say that I have a type
Foo
, for whichmem::zeroed()
produces a valid representation (i.e. all of its non-padding bytes have 0 as a valid representation). It also has padding. It could be something likestruct Foo(u8, u32)
, I don't care too much.Also, let's have the functions:
Scenario 1
Is this print statement UB due to reading from uninitialized memory?
Scenario 2
Is this print statement UB due to reading from uninitialized memory?
Scenario 3
The print will just print 42, yes?
Scenario 4
Is this print UB? Does it print 42?
Scenario 5
Is this print statement UB due to reading from uninitialized memory?
(Probably not?)
Scenario 6
Is this print statement UB due to reading from uninitialized memory? (ooh, tricky)
The text was updated successfully, but these errors were encountered: