about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>2018-01-23 16:37:39 +0100
committerOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>2018-03-08 08:34:08 +0100
commitd3e2f48c8cb3ee32fecf5a7a525fa51d10ab4035 (patch)
tree4d9a2a4389f57305d5f6c1c3e2e47ff2c2a708f1 /src
parentf14e746b2fb11c638904247ef6764f83448de641 (diff)
downloadrust-d3e2f48c8cb3ee32fecf5a7a525fa51d10ab4035.tar.gz
rust-d3e2f48c8cb3ee32fecf5a7a525fa51d10ab4035.zip
More const eval sanity checks (invalid slice fat pointers)
Diffstat (limited to 'src')
-rw-r--r--src/librustc_mir/build/matches/test.rs36
-rw-r--r--src/librustc_mir/interpret/eval_context.rs13
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 067041b14f5..3b9fbc5c867 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 7dafe846a33..dc66365bccb 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),
             }
         }