-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
-Zmir-opt-level=2
misoptimises valid code under Tree Borrows
#110947
Comments
Here's a pure-surface Rust reproduction pub fn fn0() -> bool {
let mut pair = (1, false);
let ptr = core::ptr::addr_of_mut!(pair.1);
let mut ret = pair.1 <= unsafe { *ptr };
pair = (1, false);
unsafe {
*ptr = ret | ret;
}
ret = !pair.1;
return ret;
}
pub fn main() {
println!("{}", fn0());
} |
@JakobDegen any guess which MIR opt might be the culprit here? |
Even more minimized reproducer: pub fn fn0() -> bool {
let mut pair = (1, false);
let ptr = core::ptr::addr_of_mut!(pair.1);
pair = (1, false);
unsafe {
*ptr = true;
}
let ret = !pair.1;
return ret;
}
pub fn main() {
println!("{}", fn0());
} My first guess would be that some MIR analysis assumes that Cc @cjgillot |
Even stranger, with Rust 1.69 we get |
Does this look like the bad optimization? -// MIR for `fn0` before ConstProp
+// MIR for `fn0` after ConstProp
fn fn0() -> bool {
let mut _0: bool; // return place in scope 0 at src/main.rs:1:17: 1:21
@@ -19,13 +19,13 @@ fn fn0() -> bool {
}
bb0: {
- _1 = (const 1_i32, const false); // scope 0 at src/main.rs:2:20: 2:30
+ _1 = const (1_i32, false); // scope 0 at src/main.rs:2:20: 2:30
_2 = &raw mut (_1.1: bool); // scope 1 at /rustc/1a6ae3d692cfb52b21d0f45ba50b659486e53d6c/library/core/src/ptr/mod.rs:2192:5: 2192:20
- _1 = (const 1_i32, const false); // scope 2 at src/main.rs:4:5: 4:22
+ _1 = const (1_i32, false); // scope 2 at src/main.rs:4:5: 4:22
(*_2) = const true; // scope 3 at src/main.rs:6:9: 6:20
- _4 = (_1.1: bool); // scope 2 at src/main.rs:8:16: 8:22
- _3 = Not(move _4); // scope 2 at src/main.rs:8:15: 8:22
- _0 = _3; // scope 4 at src/main.rs:9:12: 9:15
+ _4 = const false; // scope 2 at src/main.rs:8:16: 8:22
+ _3 = const true; // scope 2 at src/main.rs:8:15: 8:22
+ _0 = const true; // scope 4 at src/main.rs:9:12: 9:15
return; // scope 0 at src/main.rs:10:2: 10:2
}
} |
@saethlin yes, very much so. Specifically the Did ConstProp never learn that locals that have their address taken cannot be propagated? |
I'm about to be busy for a few hours, so if someone else wants to bisect what changed with |
I've been suspecting such a bug without managing to reproduce it for a few months. The bug is in the |
@cbeuw I'm curious, how did you come up with this example? |
@saethlin I'm making a fuzzer targeting custom MIR :D It's still quite incomplete and currently hosted on an ETH Zürich private GitLab instance https://gitlab.inf.ethz.ch/ou-plf/rustlantis. I guess I should make a public mirror on GitHub soon... |
That is awesome! This is exactly the kind of stuff I was hoping to find with one. |
Ah so should we report these? I think I read somewhere that custom mir is likely to get wrong and would always cause crashes then or something which is why I have ignored all of the |
This isn't an ICE, this is a change in behavior due to enabling an optimization. |
ah right sorry, I was confused with #110902 |
This code has UB under Stacked Borrows in Miri, but is fine with
-Zmiri-tree-borrows
, and it should printfalse
However, under
-Zmir-opt-level=2
and above, it printstrue
Meta
rustc --version --verbose
:cc @Vanille-N @RalfJung
The text was updated successfully, but these errors were encountered: