From d3e2f48c8cb3ee32fecf5a7a525fa51d10ab4035 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Tue, 23 Jan 2018 16:37:39 +0100 Subject: [PATCH] More const eval sanity checks (invalid slice fat pointers) --- src/librustc_mir/build/matches/test.rs | 36 +--------------------- src/librustc_mir/interpret/eval_context.rs | 13 ++++++-- 2 files changed, 11 insertions(+), 38 deletions(-) diff --git a/src/librustc_mir/build/matches/test.rs b/src/librustc_mir/build/matches/test.rs index 067041b14f58..3b9fbc5c867a 100644 --- a/src/librustc_mir/build/matches/test.rs +++ b/src/librustc_mir/build/matches/test.rs @@ -173,39 +173,6 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { } } - /// Convert a byte array or byte slice to a byte slice. - fn to_slice_operand(&mut self, - block: BasicBlock, - source_info: SourceInfo, - operand: Operand<'tcx>) - -> Operand<'tcx> - { - let tcx = self.hir.tcx(); - let ty = operand.ty(&self.local_decls, tcx); - debug!("to_slice_operand({:?}, {:?}: {:?})", block, operand, ty); - match ty.sty { - ty::TyRef(region, mt) => match mt.ty.sty { - ty::TyArray(ety, _) => { - let ty = tcx.mk_imm_ref(region, tcx.mk_slice(ety)); - let temp = self.temp(ty, source_info.span); - self.cfg.push_assign(block, source_info, &temp, - Rvalue::Cast(CastKind::Unsize, operand, ty)); - Operand::Move(temp) - } - ty::TySlice(_) => operand, - _ => { - span_bug!(source_info.span, - "bad operand {:?}: {:?} to `to_slice_operand`", operand, ty) - } - } - _ => { - span_bug!(source_info.span, - "bad operand {:?}: {:?} to `to_slice_operand`", operand, ty) - } - } - - } - /// Generates the code to perform a test. pub fn perform_test(&mut self, block: BasicBlock, @@ -292,8 +259,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { ret } - TestKind::Eq { value, ty } => { - let tcx = self.hir.tcx(); + TestKind::Eq { value, mut ty } => { let mut val = Operand::Copy(place.clone()); let mut expect = self.literal_operand(test.span, ty, Literal::Value { value diff --git a/src/librustc_mir/interpret/eval_context.rs b/src/librustc_mir/interpret/eval_context.rs index 7dafe846a334..dc66365bccb5 100644 --- a/src/librustc_mir/interpret/eval_context.rs +++ b/src/librustc_mir/interpret/eval_context.rs @@ -1263,9 +1263,16 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M ty::TyDynamic(..) => Ok(p.to_value_with_vtable( self.memory.read_ptr_sized_unsigned(extra, ptr_align)?.to_ptr()?, )), - ty::TySlice(..) | ty::TyStr => Ok( - p.to_value_with_len(self.memory.read_ptr_sized_unsigned(extra, ptr_align)?.to_bytes()? as u64), - ), + ty::TySlice(..) | ty::TyStr => { + match p.primval { + PrimVal::Bytes(b) => bug!("slice ptr: {:x}", b), + PrimVal::Undef => bug!("undef slice ptr"), + _ => {}, + } + Ok( + p.to_value_with_len(self.memory.read_ptr_sized_unsigned(extra, ptr_align)?.to_bytes()? as u64), + ) + }, _ => bug!("unsized primval ptr read from {:?}", pointee_ty), } }