Skip to content

Commit

Permalink
Auto merge of #42369 - RalfJung:drop-glue, r=eddyb
Browse files Browse the repository at this point in the history
array drop glue: avoid using out-of-bounds index lvalues

Avoid using out-of-bounds index lvalues. Miri doesn't like them, and they seem fishy in general.
Instead, use a pointer cast to compute the address of the beginning, and Offset to compute the end.

Cc @arielb1 @nagisa
  • Loading branch information
bors committed Jun 3, 2017
2 parents a8dc246 + 763ef81 commit 0453cf5
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/librustc_mir/util/elaborate_drops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ impl<'l, 'b, 'tcx, D> DropCtxt<'l, 'b, 'tcx, D>
debug!("drop_loop_pair({:?}, {:?})", ety, ptr_based);
let tcx = self.tcx();
let iter_ty = if ptr_based {
tcx.mk_ptr(ty::TypeAndMut { ty: ety, mutbl: hir::Mutability::MutMutable })
tcx.mk_mut_ptr(ety)
} else {
tcx.types.usize
};
Expand Down Expand Up @@ -708,15 +708,20 @@ impl<'l, 'b, 'tcx, D> DropCtxt<'l, 'b, 'tcx, D>
let mut drop_block_stmts = vec![];
drop_block_stmts.push(self.assign(&length, Rvalue::Len(self.lvalue.clone())));
if ptr_based {
// cur = &LV[0];
// end = &LV[len];
drop_block_stmts.push(self.assign(&cur, Rvalue::Ref(
tcx.types.re_erased, BorrowKind::Mut,
self.lvalue.clone().index(zero.clone())
let tmp_ty = tcx.mk_mut_ptr(self.lvalue_ty(self.lvalue));
let tmp = Lvalue::Local(self.new_temp(tmp_ty));
// tmp = &LV;
// cur = tmp as *mut T;
// end = Offset(cur, len);
drop_block_stmts.push(self.assign(&tmp, Rvalue::Ref(
tcx.types.re_erased, BorrowKind::Mut, self.lvalue.clone()
)));
drop_block_stmts.push(self.assign(&length_or_end, Rvalue::Ref(
tcx.types.re_erased, BorrowKind::Mut,
self.lvalue.clone().index(Operand::Consume(length.clone()))
drop_block_stmts.push(self.assign(&cur, Rvalue::Cast(
CastKind::Misc, Operand::Consume(tmp.clone()), iter_ty
)));
drop_block_stmts.push(self.assign(&length_or_end,
Rvalue::BinaryOp(BinOp::Offset,
Operand::Consume(cur.clone()), Operand::Consume(length.clone())
)));
} else {
// index = 0 (length already pushed)
Expand Down

0 comments on commit 0453cf5

Please sign in to comment.