Skip to content

Commit b25ee64

Browse files
committed
Auto merge of #61781 - christianpoveda:intptrcast-model, r=oli-obk,RalfJung
prepare for Intptrcast model #61668 done right (I hope so). r? @RalfJung @oli-obk
2 parents b01a257 + 1e38870 commit b25ee64

File tree

8 files changed

+67
-10
lines changed

8 files changed

+67
-10
lines changed

Diff for: src/librustc_mir/interpret/eval_context.rs

+17
Original file line numberDiff line numberDiff line change
@@ -765,4 +765,21 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpretCx<'mir, 'tcx, M> {
765765
pub fn truncate(&self, value: u128, ty: TyLayout<'_>) -> u128 {
766766
truncate(value, ty.size)
767767
}
768+
769+
#[inline(always)]
770+
pub fn force_ptr(
771+
&self,
772+
scalar: Scalar<M::PointerTag>,
773+
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
774+
self.memory.force_ptr(scalar)
775+
}
776+
777+
#[inline(always)]
778+
pub fn force_bits(
779+
&self,
780+
scalar: Scalar<M::PointerTag>,
781+
size: Size
782+
) -> InterpResult<'tcx, u128> {
783+
self.memory.force_bits(scalar, size)
784+
}
768785
}

Diff for: src/librustc_mir/interpret/machine.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use rustc::ty::{self, query::TyCtxtAt};
1111

1212
use super::{
1313
Allocation, AllocId, InterpResult, Scalar, AllocationExtra,
14-
InterpretCx, PlaceTy, OpTy, ImmTy, MemoryKind,
14+
InterpretCx, PlaceTy, OpTy, ImmTy, MemoryKind, Pointer,
15+
InterpErrorInfo, InterpError
1516
};
1617

1718
/// Whether this kind of memory is allowed to leak
@@ -208,4 +209,22 @@ pub trait Machine<'mir, 'tcx>: Sized {
208209
ecx: &mut InterpretCx<'mir, 'tcx, Self>,
209210
extra: Self::FrameExtra,
210211
) -> InterpResult<'tcx>;
212+
213+
fn int_to_ptr(
214+
int: u64,
215+
_extra: &Self::MemoryExtra,
216+
) -> InterpResult<'tcx, Pointer<Self::PointerTag>> {
217+
if int == 0 {
218+
Err(InterpErrorInfo::from(InterpError::InvalidNullPointerUsage))
219+
} else {
220+
Err(InterpErrorInfo::from(InterpError::ReadBytesAsPointer))
221+
}
222+
}
223+
224+
fn ptr_to_int(
225+
_ptr: Pointer<Self::PointerTag>,
226+
_extra: &Self::MemoryExtra,
227+
) -> InterpResult<'tcx, u64> {
228+
Err(InterpErrorInfo::from(InterpError::ReadPointerAsBytes))
229+
}
211230
}

Diff for: src/librustc_mir/interpret/memory.rs

+24-3
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
627627
if size.bytes() == 0 {
628628
Ok(&[])
629629
} else {
630-
let ptr = ptr.to_ptr()?;
630+
let ptr = self.force_ptr(ptr)?;
631631
self.get(ptr.alloc_id)?.get_bytes(self, ptr, size)
632632
}
633633
}
@@ -714,8 +714,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
714714
// non-NULLness which already happened.
715715
return Ok(());
716716
}
717-
let src = src.to_ptr()?;
718-
let dest = dest.to_ptr()?;
717+
let src = self.force_ptr(src)?;
718+
let dest = self.force_ptr(dest)?;
719719

720720
// first copy the relocations to a temporary buffer, because
721721
// `get_bytes_mut` will clear the relocations, which is correct,
@@ -874,4 +874,25 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
874874
}
875875
Ok(())
876876
}
877+
878+
pub fn force_ptr(
879+
&self,
880+
scalar: Scalar<M::PointerTag>,
881+
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
882+
match scalar {
883+
Scalar::Ptr(ptr) => Ok(ptr),
884+
_ => M::int_to_ptr(scalar.to_usize(self)?, &self.extra)
885+
}
886+
}
887+
888+
pub fn force_bits(
889+
&self,
890+
scalar: Scalar<M::PointerTag>,
891+
size: Size
892+
) -> InterpResult<'tcx, u128> {
893+
match scalar.to_bits_or_ptr(size, self) {
894+
Ok(bits) => Ok(bits),
895+
Err(ptr) => Ok(M::ptr_to_int(ptr, &self.extra)? as u128)
896+
}
897+
}
877898
}

Diff for: src/librustc_mir/interpret/operand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpretCx<'mir, 'tcx, M> {
232232
}
233233

234234
// check for integer pointers before alignment to report better errors
235-
let ptr = ptr.to_ptr()?;
235+
let ptr = self.force_ptr(ptr)?;
236236
self.memory.check_align(ptr.into(), ptr_align)?;
237237
match mplace.layout.abi {
238238
layout::Abi::Scalar(..) => {

Diff for: src/librustc_mir/interpret/operator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpretCx<'mir, 'tcx, M> {
347347
}
348348
_ => {
349349
assert!(layout.ty.is_integral());
350-
let val = val.to_bits(layout.size)?;
350+
let val = self.force_bits(val, layout.size)?;
351351
let res = match un_op {
352352
Not => !val,
353353
Neg => {

Diff for: src/librustc_mir/interpret/place.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ where
473473
let layout = self.layout_of(self.tcx.types.usize)?;
474474
let n = self.access_local(self.frame(), local, Some(layout))?;
475475
let n = self.read_scalar(n)?;
476-
let n = n.to_bits(self.tcx.data_layout.pointer_size)?;
476+
let n = self.force_bits(n.not_undef()?, self.tcx.data_layout.pointer_size)?;
477477
self.mplace_field(base, u64::try_from(n).unwrap())?
478478
}
479479

@@ -753,7 +753,7 @@ where
753753
}
754754

755755
// check for integer pointers before alignment to report better errors
756-
let ptr = ptr.to_ptr()?;
756+
let ptr = self.force_ptr(ptr)?;
757757
self.memory.check_align(ptr.into(), ptr_align)?;
758758
let tcx = &*self.tcx;
759759
// FIXME: We should check that there are dest.layout.size many bytes available in

Diff for: src/librustc_mir/interpret/terminator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpretCx<'mir, 'tcx, M> {
7979
let (fn_def, abi) = match func.layout.ty.sty {
8080
ty::FnPtr(sig) => {
8181
let caller_abi = sig.abi();
82-
let fn_ptr = self.read_scalar(func)?.to_ptr()?;
82+
let fn_ptr = self.force_ptr(self.read_scalar(func)?.not_undef()?)?;
8383
let instance = self.memory.get_fn(fn_ptr)?;
8484
(instance, caller_abi)
8585
}

Diff for: src/librustc_mir/interpret/validity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
559559
// This is the size in bytes of the whole array.
560560
let size = ty_size * len;
561561

562-
let ptr = mplace.ptr.to_ptr()?;
562+
let ptr = self.ecx.force_ptr(mplace.ptr)?;
563563

564564
// NOTE: Keep this in sync with the handling of integer and float
565565
// types above, in `visit_primitive`.

0 commit comments

Comments
 (0)