Skip to content

Commit 6ec9b90

Browse files
authored
Rollup merge of rust-lang#97763 - RalfJung:fallible-cast, r=lcnr
Allow ptr_from_addr_cast to fail This is needed for rust-lang/miri#2133: I would like to have an option in Miri to error when a int2ptr cast is executed.
2 parents 16e60cd + e1f0736 commit 6ec9b90

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

compiler/rustc_const_eval/src/interpret/cast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
221221
let addr = addr.to_machine_usize(self)?;
222222

223223
// Then turn address into pointer.
224-
let ptr = M::ptr_from_addr_cast(&self, addr);
224+
let ptr = M::ptr_from_addr_cast(&self, addr)?;
225225
Ok(Scalar::from_maybe_pointer(ptr, self).into())
226226
}
227227

compiler/rustc_const_eval/src/interpret/machine.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,10 @@ pub trait Machine<'mir, 'tcx>: Sized {
294294
fn ptr_from_addr_cast(
295295
ecx: &InterpCx<'mir, 'tcx, Self>,
296296
addr: u64,
297-
) -> Pointer<Option<Self::PointerTag>>;
297+
) -> InterpResult<'tcx, Pointer<Option<Self::PointerTag>>>;
298298

299-
// FIXME: Transmuting an integer to a pointer should just always return a `None`
300-
// provenance, but that causes problems with function pointers in Miri.
301299
/// Hook for returning a pointer from a transmute-like operation on an addr.
300+
/// This is only needed to support Miri's (unsound) "allow-ptr-int-transmute" flag.
302301
fn ptr_from_addr_transmute(
303302
ecx: &InterpCx<'mir, 'tcx, Self>,
304303
addr: u64,
@@ -519,8 +518,10 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
519518
fn ptr_from_addr_cast(
520519
_ecx: &InterpCx<$mir, $tcx, Self>,
521520
addr: u64,
522-
) -> Pointer<Option<AllocId>> {
523-
Pointer::new(None, Size::from_bytes(addr))
521+
) -> InterpResult<$tcx, Pointer<Option<AllocId>>> {
522+
// Allow these casts, but make the pointer not dereferenceable.
523+
// (I.e., they behave like transmutation.)
524+
Ok(Pointer::new(None, Size::from_bytes(addr)))
524525
}
525526

526527
#[inline(always)]

0 commit comments

Comments
 (0)