-
Notifications
You must be signed in to change notification settings - Fork 349
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
Reference to uninitialized memory is not caught #2518
Comments
When checking validity for references, the referenced memory is not checked for validity. This is by design I believe, but I can't quite explain why. |
Is it miri's design or Rust's one? As it states here:
And also:
As I understand creating a reference means producing it, and it is invalid because it is a reference that points to an invalid value which is uninitialized memory. |
We're not sure if this is UB or not (see: rust-lang/unsafe-code-guidelines#77). But it probably won't be UB because it's difficult to check this (Every time you move a &[u8; 1_000_000], you have to check the whole of the array there Just In Case any of it has been invalidated) Not to mention recursive types. These aren't impossible problems, but it would make the job of miri harder. |
I do not think the performance arguments are compelling. I might be convinced, if someone would implement validity-behind-references, then give me time to try to make it faster. But in any case, I think the much bigger concern is how surprising this would be for unsafe programmers, and how much existing code it would make invalid. Basically: rust-lang/unsafe-code-guidelines#346 |
This is indeed a deliberate difference between Miri and the Reference, and documented prominently in the README:
A final decision for Rust semantics has not been made, so the reference is conservative and says this is forbidden. Miri does not guarantee to detect all UB and hence, for now, does not do these checks. I also hope we will officially allow such references to invalid data in the future; most people seem to be in favor of that. If someone wanted to implement them we could add an opt-in flag for these checks. However, notice that references to uninhabited types are considered UB even by Miri, as this example shows. |
I am going to close this, since Miri behaves as intended and as documented. Feel free to ask any further questions that you have, though! |
An intermediary reference to uninitialized memory is not caught up. It is produced when taking that reference and then converting it to a pointer.
Rust playground permalink: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=811c791ba18d604f1553f1b2dd27108e
The text was updated successfully, but these errors were encountered: