Skip to content

Commit 508371a

Browse files
committed
Auto merge of #1412 - josephlr:ptr, r=RalfJung
Remove pointer arithmetic intrinsics **Do Not Merge** until rust-lang/rust#71500 is in nightly. As rust-lang/rust#71500 implements `offset` and `arith_offset` in rustc's MIR interpreter, these implementations can now be removed from miri. Also, the `pointer_offset_inbounds` method has been moved to the main MIR engine, so that too can be removed. Signed-off-by: Joe Richey <[email protected]>
2 parents dd1e112 + c77e902 commit 508371a

File tree

5 files changed

+2
-69
lines changed

5 files changed

+2
-69
lines changed

rust-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
255c0338dc0b02f833fb1a816d76febd50c399c4
1+
0e9e4083100aa3ebf09b8f1ace0348cb37475eb9

src/operator.rs

+1-45
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
use std::convert::TryFrom;
2-
31
use log::trace;
42

53
use rustc_middle::{mir, ty::Ty};
6-
use rustc_target::abi::{LayoutOf, Size};
74

85
use crate::*;
96

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

1815
fn ptr_eq(&self, left: Scalar<Tag>, right: Scalar<Tag>) -> InterpResult<'tcx, bool>;
19-
20-
fn pointer_offset_inbounds(
21-
&self,
22-
ptr: Scalar<Tag>,
23-
pointee_ty: Ty<'tcx>,
24-
offset: i64,
25-
) -> InterpResult<'tcx, Scalar<Tag>>;
2616
}
2717

2818
impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
@@ -71,7 +61,7 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
7161
Offset => {
7262
let pointee_ty =
7363
left.layout.ty.builtin_deref(true).expect("Offset called on non-ptr type").ty;
74-
let ptr = self.pointer_offset_inbounds(
64+
let ptr = self.ptr_offset_inbounds(
7565
left.to_scalar()?,
7666
pointee_ty,
7767
right.to_scalar()?.to_machine_isize(self)?,
@@ -91,38 +81,4 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
9181
let right = self.force_bits(right, size)?;
9282
Ok(left == right)
9383
}
94-
95-
/// Raises an error if the offset moves the pointer outside of its allocation.
96-
/// For integers, we consider each of them their own tiny allocation of size 0,
97-
/// so offset-by-0 is okay for them -- except for NULL, which we rule out entirely.
98-
fn pointer_offset_inbounds(
99-
&self,
100-
ptr: Scalar<Tag>,
101-
pointee_ty: Ty<'tcx>,
102-
offset: i64,
103-
) -> InterpResult<'tcx, Scalar<Tag>> {
104-
let pointee_size = i64::try_from(self.layout_of(pointee_ty)?.size.bytes()).unwrap();
105-
let offset = offset.checked_mul(pointee_size).ok_or_else(|| {
106-
err_ub_format!("overflow during offset comutation for inbounds pointer arithmetic")
107-
})?;
108-
// We do this first, to rule out overflows.
109-
let offset_ptr = ptr.ptr_signed_offset(offset, self)?;
110-
// What we need to check is that starting at `min(ptr, offset_ptr)`,
111-
// we could do an access of size `abs(offset)`. Alignment does not matter.
112-
let (min_ptr, abs_offset) = if offset >= 0 {
113-
(ptr, u64::try_from(offset).unwrap())
114-
} else {
115-
// Negative offset.
116-
// If the negation overflows, the result will be negative so the try_from will fail.
117-
(offset_ptr, u64::try_from(-offset).unwrap())
118-
};
119-
self.memory.check_ptr_access_align(
120-
min_ptr,
121-
Size::from_bytes(abs_offset),
122-
None,
123-
CheckInAllocMsg::InboundsTest,
124-
)?;
125-
// That's it!
126-
Ok(offset_ptr)
127-
}
12884
}

src/shims/intrinsics.rs

-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::iter;
2-
use std::convert::TryFrom;
32

43
use rustc_attr as attr;
54
use rustc_ast::ast::FloatTy;
@@ -101,26 +100,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
101100
.write_bytes(ptr, iter::repeat(val_byte).take(byte_count.bytes() as usize))?;
102101
}
103102

104-
// Pointer arithmetic
105-
"arith_offset" => {
106-
let &[ptr, offset] = check_arg_count(args)?;
107-
let ptr = this.read_scalar(ptr)?.not_undef()?;
108-
let offset = this.read_scalar(offset)?.to_machine_isize(this)?;
109-
110-
let pointee_ty = substs.type_at(0);
111-
let pointee_size = i64::try_from(this.layout_of(pointee_ty)?.size.bytes()).unwrap();
112-
let offset = offset.overflowing_mul(pointee_size).0;
113-
let result_ptr = ptr.ptr_wrapping_signed_offset(offset, this);
114-
this.write_scalar(result_ptr, dest)?;
115-
}
116-
"offset" => {
117-
let &[ptr, offset] = check_arg_count(args)?;
118-
let ptr = this.read_scalar(ptr)?.not_undef()?;
119-
let offset = this.read_scalar(offset)?.to_machine_isize(this)?;
120-
let result_ptr = this.pointer_offset_inbounds(ptr, substs.type_at(0), offset)?;
121-
this.write_scalar(result_ptr, dest)?;
122-
}
123-
124103
// Floating-point operations
125104
#[rustfmt::skip]
126105
| "sinf32"

tests/compile-fail/rc_as_ptr.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// This should fail even without validation
22
// compile-flags: -Zmiri-disable-validation
3-
#![feature(weak_into_raw)]
43

54
use std::rc::{Rc, Weak};
65
use std::ptr;

tests/run-pass/rc.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(weak_into_raw)]
21
#![feature(new_uninit)]
32
#![feature(get_mut_unchecked)]
43

0 commit comments

Comments
 (0)