From 394a57fc22d061c943985c741cca6cb5694586c5 Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Fri, 15 May 2020 01:44:41 -0700 Subject: [PATCH] Remove pointer arithmetic intrinsics These are now implemented in rustc's mir interpreter Signed-off-by: Joe Richey --- src/operator.rs | 46 +---------------------------------------- src/shims/intrinsics.rs | 20 ------------------ 2 files changed, 1 insertion(+), 65 deletions(-) diff --git a/src/operator.rs b/src/operator.rs index a28a0098e92..bfc8e908dc1 100644 --- a/src/operator.rs +++ b/src/operator.rs @@ -1,9 +1,6 @@ -use std::convert::TryFrom; - use log::trace; use rustc_middle::{mir, ty::Ty}; -use rustc_target::abi::{LayoutOf, Size}; use crate::*; @@ -16,13 +13,6 @@ fn binary_ptr_op( ) -> InterpResult<'tcx, (Scalar, bool, Ty<'tcx>)>; fn ptr_eq(&self, left: Scalar, right: Scalar) -> InterpResult<'tcx, bool>; - - fn pointer_offset_inbounds( - &self, - ptr: Scalar, - pointee_ty: Ty<'tcx>, - offset: i64, - ) -> InterpResult<'tcx, Scalar>; } impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> { @@ -71,7 +61,7 @@ fn binary_ptr_op( Offset => { let pointee_ty = left.layout.ty.builtin_deref(true).expect("Offset called on non-ptr type").ty; - let ptr = self.pointer_offset_inbounds( + let ptr = self.ptr_offset_inbounds( left.to_scalar()?, pointee_ty, right.to_scalar()?.to_machine_isize(self)?, @@ -91,38 +81,4 @@ fn ptr_eq(&self, left: Scalar, right: Scalar) -> InterpResult<'tcx, bo let right = self.force_bits(right, size)?; Ok(left == right) } - - /// Raises an error if the offset moves the pointer outside of its allocation. - /// For integers, we consider each of them their own tiny allocation of size 0, - /// so offset-by-0 is okay for them -- except for NULL, which we rule out entirely. - fn pointer_offset_inbounds( - &self, - ptr: Scalar, - pointee_ty: Ty<'tcx>, - offset: i64, - ) -> InterpResult<'tcx, Scalar> { - let pointee_size = i64::try_from(self.layout_of(pointee_ty)?.size.bytes()).unwrap(); - let offset = offset.checked_mul(pointee_size).ok_or_else(|| { - err_ub_format!("overflow during offset comutation for inbounds pointer arithmetic") - })?; - // We do this first, to rule out overflows. - let offset_ptr = ptr.ptr_signed_offset(offset, self)?; - // What we need to check is that starting at `min(ptr, offset_ptr)`, - // we could do an access of size `abs(offset)`. Alignment does not matter. - let (min_ptr, abs_offset) = if offset >= 0 { - (ptr, u64::try_from(offset).unwrap()) - } else { - // Negative offset. - // If the negation overflows, the result will be negative so the try_from will fail. - (offset_ptr, u64::try_from(-offset).unwrap()) - }; - self.memory.check_ptr_access_align( - min_ptr, - Size::from_bytes(abs_offset), - None, - CheckInAllocMsg::InboundsTest, - )?; - // That's it! - Ok(offset_ptr) - } } diff --git a/src/shims/intrinsics.rs b/src/shims/intrinsics.rs index 1a8c9163211..ec86e878ec6 100644 --- a/src/shims/intrinsics.rs +++ b/src/shims/intrinsics.rs @@ -101,26 +101,6 @@ fn call_intrinsic( .write_bytes(ptr, iter::repeat(val_byte).take(byte_count.bytes() as usize))?; } - // Pointer arithmetic - "arith_offset" => { - let &[ptr, offset] = check_arg_count(args)?; - let ptr = this.read_scalar(ptr)?.not_undef()?; - let offset = this.read_scalar(offset)?.to_machine_isize(this)?; - - let pointee_ty = substs.type_at(0); - let pointee_size = i64::try_from(this.layout_of(pointee_ty)?.size.bytes()).unwrap(); - let offset = offset.overflowing_mul(pointee_size).0; - let result_ptr = ptr.ptr_wrapping_signed_offset(offset, this); - this.write_scalar(result_ptr, dest)?; - } - "offset" => { - let &[ptr, offset] = check_arg_count(args)?; - let ptr = this.read_scalar(ptr)?.not_undef()?; - let offset = this.read_scalar(offset)?.to_machine_isize(this)?; - let result_ptr = this.pointer_offset_inbounds(ptr, substs.type_at(0), offset)?; - this.write_scalar(result_ptr, dest)?; - } - // Floating-point operations #[rustfmt::skip] | "sinf32" -- 2.44.0