Skip to content

Remove pointer arithmetic intrinsics #1412

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

Merged
merged 2 commits into from
May 30, 2020
Merged
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
2 changes: 1 addition & 1 deletion rust-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
255c0338dc0b02f833fb1a816d76febd50c399c4
0e9e4083100aa3ebf09b8f1ace0348cb37475eb9
46 changes: 1 addition & 45 deletions src/operator.rs
Original file line number Diff line number Diff line change
@@ -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::*;

Expand All @@ -16,13 +13,6 @@ pub trait EvalContextExt<'tcx> {
) -> InterpResult<'tcx, (Scalar<Tag>, bool, Ty<'tcx>)>;

fn ptr_eq(&self, left: Scalar<Tag>, right: Scalar<Tag>) -> InterpResult<'tcx, bool>;

fn pointer_offset_inbounds(
&self,
ptr: Scalar<Tag>,
pointee_ty: Ty<'tcx>,
offset: i64,
) -> InterpResult<'tcx, Scalar<Tag>>;
}

impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
Expand Down Expand Up @@ -71,7 +61,7 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
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)?,
Expand All @@ -91,38 +81,4 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
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<Tag>,
pointee_ty: Ty<'tcx>,
offset: i64,
) -> InterpResult<'tcx, Scalar<Tag>> {
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)
}
}
21 changes: 0 additions & 21 deletions src/shims/intrinsics.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::iter;
use std::convert::TryFrom;

use rustc_attr as attr;
use rustc_ast::ast::FloatTy;
Expand Down Expand Up @@ -101,26 +100,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
.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"
Expand Down
1 change: 0 additions & 1 deletion tests/compile-fail/rc_as_ptr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// This should fail even without validation
// compile-flags: -Zmiri-disable-validation
#![feature(weak_into_raw)]

use std::rc::{Rc, Weak};
use std::ptr;
Expand Down
1 change: 0 additions & 1 deletion tests/run-pass/rc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(weak_into_raw)]
#![feature(new_uninit)]
#![feature(get_mut_unchecked)]

Expand Down