-
Notifications
You must be signed in to change notification settings - Fork 14k
const validation: remove check for mutable refs in final value of const #148746
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
base: master
Are you sure you want to change the base?
Conversation
|
r? @davidtwco rustbot has assigned @davidtwco. Use |
All code that this PR newly allows involves |
|
With this PR, Miri detects no UB in this code. Is this intended? use std::ptr;
static mut X: i32 = 1;
const Y: &mut i32 = unsafe { &mut *&raw mut X };
fn main() {
let a = &Y;
let b = &Y;
println!("{a}");
println!("{b}");
unsafe {
*ptr::from_ref(a).read() = 2;
*ptr::from_ref(b).read() = 3;
*ptr::from_ref(a).read() = 4;
*ptr::from_ref(b).read() = 5;
}
} |
|
However, Miri detects UB in the following code: use std::ptr;
static mut X: i32 = 1;
const Y: &mut i32 = unsafe { &mut *&raw mut X };
fn main() {
let a = &std::convert::identity(Y);
let b = &Y;
unsafe {
*ptr::from_ref(b).read() = 3;
*ptr::from_ref(a).read() = 4;
}
} |
That should be the case, yes. The only other thing an
We can't run the aliasing model when evaluating constants, so all pointers coming from constants are "equivalent" to Miri. These The 2nd example passes |
Even if those are two aliasing |
|
If the address they are stored at is "global" memory (i.e. the memory backing a const or static), then yes. All pointers stored in global memory are considered to be "root" pointers for the aliasing model in Miri. |
This check rejects code that is not necessarily UB, e.g. a mutable ref to a
static mutthat is very carefully used correctly. That led to us having to describe it in the Reference, which uncovered just how ad-hoc this check is (rust-lang/reference#2074).Even without this check, we still reject things like
This is rejected by const checking -- the part of the frontend that looks at the source code and says whether it is allowed in const context. In the Reference, this restriction is explained here.
So, the check during validation is just a safety net. And it is already a safety net with gaping holes since we only check
&mut T, not&UnsafeCell<T>, due to the fact that we promote some immutable values that have!Freezetype so&!Freezeactually can occur in the final value of a const.So... it may be time for me to acknowledge that the "mutable ref in final value of const" check is a cure that's worth than the disease. Nobody asked for that check, I just added it because I was worried about soundness issues when we allow mutable references in constants. Originally it was much stricter, but I had to slowly relax it to its current form to prevent t from firing on code we intend to allow. In the end there are only 3 tests left that trigger this error, and they are all just constants containing references to mutable statics -- not the safest code in the world, but also not so bad that we have to spend a lot of time devising a core language limitation and associated Reference wording to prevent it from ever happening.
So... @rust-lang/wg-const-eval @rust-lang/lang I propose that we allow code like this
@theemathas would be great if you could try to poke a hole into this. ;)