Skip to content
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

Fix const prop miscompilations of references with projections. #68002

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions src/librustc_mir/transform/const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc::mir::visit::{
};
use rustc::mir::{
read_only, AggregateKind, BasicBlock, BinOp, Body, BodyAndCache, ClearCrossCrate, Constant,
Local, LocalDecl, LocalKind, Location, Operand, Place, ReadOnlyBodyAndCache, Rvalue,
Local, LocalDecl, LocalKind, Location, Operand, Place, PlaceRef, ReadOnlyBodyAndCache, Rvalue,
SourceInfo, SourceScope, SourceScopeData, Statement, StatementKind, Terminator, TerminatorKind,
UnOp, RETURN_PLACE,
};
Expand Down Expand Up @@ -602,20 +602,24 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
// (e.g. for CTFE) it can never happen. But here in const_prop
// unknown data is uninitialized, so if e.g. a function argument is unsized
// and has a reference taken, we get an ICE.
//
// Additionally, to evaluate a Ref into a place to const prop, we must ensure that the
// underlying base data is initialized before we evaluate the rvalue, or we will end up
// propagating an allocation which will never be initialized.
Rvalue::Ref(_, _, place_ref) => {
trace!("checking Ref({:?})", place_ref);

if let Some(local) = place_ref.as_local() {
let alive = if let LocalValue::Live(_) = self.ecx.frame().locals[local].value {
true
} else {
false
};
let PlaceRef { local, .. } = place_ref.as_ref();

if !alive {
trace!("skipping Ref({:?}) to uninitialized local", place);
return None;
}
let alive = if let LocalValue::Live(_) = self.ecx.frame().locals[*local].value {
true
} else {
false
};

if !alive {
trace!("skipping Ref({:?}) to uninitialized local", place);
return None;
}
}

Expand Down Expand Up @@ -643,7 +647,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
value: Const<'tcx>,
source_info: SourceInfo,
) {
trace!("attepting to replace {:?} with {:?}", rval, value);
trace!("attempting to replace {:?} with {:?}", rval, value);
if let Err(e) = self.ecx.validate_operand(
value,
vec![],
Expand All @@ -654,8 +658,9 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
return;
}

// FIXME> figure out what tho do when try_read_immediate fails
// FIXME: figure out what tho do when try_read_immediate fails
let imm = self.use_ecx(source_info, |this| this.ecx.try_read_immediate(value));
debug!("Read value {:?} as immediate {:?}", value, imm);

if let Some(Ok(imm)) = imm {
match *imm {
Expand All @@ -670,7 +675,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
ScalarMaybeUndef::Scalar(one),
ScalarMaybeUndef::Scalar(two),
) => {
// Found a value represented as a pair. For now only do cont-prop if type of
// Found a value represented as a pair. For now only do const-prop if type of
// Rvalue is also a pair with two scalars. The more general case is more
// complicated to implement so we'll do it later.
let ty = &value.layout.ty.kind;
Expand Down
14 changes: 14 additions & 0 deletions src/test/ui/mir/issue67529.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Tests for miscompilation due to const propagation, as described in #67529. This used to result
// in an assertion error, because d.a was replaced with a new allocation that was never
// initialized.
//
// run-pass
// compile-flags: -Zmir-opt-level=2
struct Baz<T: ?Sized> {
a: T,
}

fn main() {
let d: Baz<[i32; 4]> = Baz { a: [1, 2, 3, 4] };
assert_eq!([1, 2, 3, 4], d.a);
}