-
Notifications
You must be signed in to change notification settings - Fork 353
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
Detect instant UB caused by creating a reference to an invalid value #1638
Comments
Note that Miri correctly detects the case of creating an owned invalid value (and rustc produces a warning as well): enum Void {}
fn main() {
let x: Void = unsafe { std::mem::transmute(()) };
} stderr:
|
The rules for validity of reference are not finally decided yet, but still being discussed at rust-lang/unsafe-code-guidelines#77. To not preempt the conclusion of this discussion, the docs declare as much UB as is possible -- we can always relax UB later, but introducing more UB is a breaking change. That's one reason why Miri does not complain about this kind of UB. The other reason is that it is really expensive to recursively traverse references all the time. However, one special case should be easy and cheap to check: references to uninhabited types (i.e., the case you used as an example). That one does not actually require looking at the value behind the reference. |
Could this be done as an opt-in flag? I wanted to demonstrate where (I understand if it's not a priority, just thought opt-in wouldn't slow down everyone, and would allow demonstrating "what if" questions wrt that kind of model) |
Not sure what you mean by this, or by "this" in your first sentence -- the uninhabited-pointee case, or the general case? Both could be implemented, of course, but I am don't think we should do the general recursive case. |
According to rust-lang/rust#78123 (comment) and the Reference, producing an
&
-reference (not sure about raw pointers) to an invalid value is instant UB:For example, if I understand correctly, this program has UB, but Miri does not report any errors:
The text was updated successfully, but these errors were encountered: