Repository navigation
Miscompile in DestinationPropagation? #146383
Copy link
Copy link
Closed
Labels
A-mir-optArea: MIR optimizationsArea: MIR optimizationsA-rustlantisArea: A miscompilation found by RustlantisArea: A miscompilation found by RustlantisC-bugCategory: This is a bug.Category: This is a bug.I-miscompileIssue: Correct Rust code lowers to incorrect machine codeIssue: Correct Rust code lowers to incorrect machine codeS-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
Reduced example from rustlantis, which is accepted by Miri using Tree Borrows without optimizations enabled:
#![feature(custom_mir, core_intrinsics)]
#![allow(internal_features)]
use std::intrinsics::mir::*;
fn dump_var<T>(_: T) {}
#[custom_mir(dialect = "runtime")]
fn main() {
mir!{
let _1: (u8,);
let _2: (u8,);
let _3: (u8,);
let _4: (u8,);
let _5: ();
{
_3.0 = 0;
_1 = _3;
_4 = (_1.0, );
_1 = _4;
Call(_5 = dump_var(_1), ReturnTo(bb1), UnwindUnreachable())
}
bb1 = {
Return()
}
}
}I'm sure this is caused by DestinationPropagation, because
cargo +nightly miri run
accepts this code, but
MIRIFLAGS=-Zmir-enable-passes=+DestinationPropagation cargo +nightly miri run
reports UB (ignore the span, it's confusing because the source code that's displayed is the pre-optimization MIR):
error: Undefined Behavior: reading memory at alloc125[0x0..0x1], but memory is uninitialized at [0x0..0x1], and this operation requires initialized memory
--> src/main.rs:19:13
|
19 | _4 = (_1.0, );
| ^^^^^^^^^^^^^ Undefined Behavior occurred here
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
= note: BACKTRACE:
= note: inside `main` at src/main.rs:19:13: 19:26
The errant transformation rewrite the entire body:
-// MIR for `main` before DestinationPropagation
+// MIR for `main` after DestinationPropagation
fn main() -> () {
let mut _0: ();
let mut _1: (u8,);
let mut _2: (u8,);
let mut _3: (u8,);
let mut _4: (u8,);
let mut _5: ();
bb0: {
- (_3.0: u8) = const 0_u8;
- _1 = copy _3;
- _4 = (copy (_1.0: u8),);
- _1 = copy _4;
- _5 = dump_var::<(u8,)>(copy _1) -> [return: bb1, unwind unreachable];
+ (_4.0: u8) = const 0_u8;
+ nop;
+ _4 = (copy (_4.0: u8),);
+ nop;
+ _5 = dump_var::<(u8,)>(copy _4) -> [return: bb1, unwind unreachable];
}
bb1: {
return;
}
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
A-mir-optArea: MIR optimizationsArea: MIR optimizationsA-rustlantisArea: A miscompilation found by RustlantisArea: A miscompilation found by RustlantisC-bugCategory: This is a bug.Category: This is a bug.I-miscompileIssue: Correct Rust code lowers to incorrect machine codeIssue: Correct Rust code lowers to incorrect machine codeS-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Activity
[-]Miscompile in DestinationPropagation[/-][+]Miscompile in DestinationPropagation?[/+]saethlin commentedon Sep 10, 2025
Actually, I'm confused. This program executes UB?
Miri says:
@RalfJung does this UB report make sense to you?
saethlin commentedon Sep 10, 2025
The UB report occurs because of this:
rust/compiler/rustc_const_eval/src/interpret/step.rs
Lines 307 to 313 in 7ad23f4
Miri accepts this code without that
write_uninitcall. But I'm not sure if this self-assignment is supposed to be disallowed because it's an overlappingcopy_nonoverlapping.tmiasko commentedon Sep 10, 2025
I would expect an aggregate assignment with an overlapping memory to have undefined behavior analogously to an assignment. If anything, this looks like a situation where Miri undefined behavior detection is incomplete.
Regardless, this is definitely an issue in destination propagation:
11 remaining items
Rollup merge of rust-lang#146402 - RalfJung:aggregate-init, r=saethlin
Auto merge of #146402 - RalfJung:aggregate-init, r=saethlin
Unrolled build for #146402
Auto merge of #146516 - cjgillot:dest-prop-aggregate, r=Amanieu
Rollup merge of rust-lang#146566 - cjgillot:mir-overlap-lint, r=saethlin
Rollup merge of #146402 - RalfJung:aggregate-init, r=saethlin
Auto merge of #146516 - cjgillot:dest-prop-aggregate, r=Amanieu
Rollup merge of rust-lang#146566 - cjgillot:mir-overlap-lint, r=saethlin
Unrolled build for #146566
Auto merge of #146516 - cjgillot:dest-prop-aggregate, r=Amanieu
Auto merge of rust-lang#146516 - cjgillot:dest-prop-aggregate, r=Amanieu
Auto merge of rust-lang#146516 - cjgillot:dest-prop-aggregate, r=Amanieu
Auto merge of #146516 - cjgillot:dest-prop-aggregate, r=Amanieu
Auto merge of #146516 - cjgillot:dest-prop-aggregate, r=Amanieu