Skip to content

Commit 64ad036

Browse files
committed
Auto merge of #114333 - RalfJung:dangling-ptr-offset, r=oli-obk
Miri: fix error on dangling pointer inbounds offset We used to claim that the pointer was "dereferenced", but that is just not true. Can be reviewed commit-by-commit. The first commit is an unrelated rename that didn't seem worth splitting into its own PR. r? `@oli-obk`
2 parents 5cbfee5 + 8496292 commit 64ad036

File tree

69 files changed

+214
-207
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+214
-207
lines changed

compiler/rustc_const_eval/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ const_eval_pointer_out_of_bounds =
282282
*[many] bytes
283283
} starting at offset {$ptr_offset} is out-of-bounds
284284
const_eval_pointer_use_after_free =
285-
pointer to {$allocation} was dereferenced after this allocation got freed
285+
{$bad_pointer_message}: {$alloc_id} has been freed, so this pointer is dangling
286286
const_eval_ptr_as_bytes_1 =
287287
this code performed an operation that depends on the underlying bytes representing a pointer
288288
const_eval_ptr_as_bytes_2 =

compiler/rustc_const_eval/src/const_eval/machine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ impl<'mir, 'tcx: 'mir> CompileTimeEvalContext<'mir, 'tcx> {
214214
// &str or &&str
215215
assert!(args.len() == 1);
216216

217-
let mut msg_place = self.deref_operand(&args[0])?;
217+
let mut msg_place = self.deref_pointer(&args[0])?;
218218
while msg_place.layout.ty.is_ref() {
219-
msg_place = self.deref_operand(&msg_place)?;
219+
msg_place = self.deref_pointer(&msg_place)?;
220220
}
221221

222222
let msg = Symbol::intern(self.read_str(&msg_place)?);

compiler/rustc_const_eval/src/const_eval/valtrees.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub(crate) fn const_to_valtree_inner<'tcx>(
102102
ty::FnPtr(_) | ty::RawPtr(_) => Err(ValTreeCreationError::NonSupportedType),
103103

104104
ty::Ref(_, _, _) => {
105-
let Ok(derefd_place)= ecx.deref_operand(place) else {
105+
let Ok(derefd_place)= ecx.deref_pointer(place) else {
106106
return Err(ValTreeCreationError::Other);
107107
};
108108
debug!(?derefd_place);

compiler/rustc_const_eval/src/errors.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ impl<'a> ReportErrorExt for UndefinedBehaviorInfo<'a> {
492492
InvalidMeta(InvalidMetaKind::SliceTooBig) => const_eval_invalid_meta_slice,
493493
InvalidMeta(InvalidMetaKind::TooBig) => const_eval_invalid_meta,
494494
UnterminatedCString(_) => const_eval_unterminated_c_string,
495-
PointerUseAfterFree(_) => const_eval_pointer_use_after_free,
495+
PointerUseAfterFree(_, _) => const_eval_pointer_use_after_free,
496496
PointerOutOfBounds { ptr_size: Size::ZERO, .. } => const_eval_zst_pointer_out_of_bounds,
497497
PointerOutOfBounds { .. } => const_eval_pointer_out_of_bounds,
498498
DanglingIntPointer(0, _) => const_eval_dangling_null_pointer,
@@ -545,8 +545,10 @@ impl<'a> ReportErrorExt for UndefinedBehaviorInfo<'a> {
545545
UnterminatedCString(ptr) | InvalidFunctionPointer(ptr) | InvalidVTablePointer(ptr) => {
546546
builder.set_arg("pointer", ptr);
547547
}
548-
PointerUseAfterFree(allocation) => {
549-
builder.set_arg("allocation", allocation);
548+
PointerUseAfterFree(alloc_id, msg) => {
549+
builder
550+
.set_arg("alloc_id", alloc_id)
551+
.set_arg("bad_pointer_message", bad_pointer_message(msg, handler));
550552
}
551553
PointerOutOfBounds { alloc_id, alloc_size, ptr_offset, ptr_size, msg } => {
552554
builder

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
144144
}
145145

146146
sym::min_align_of_val | sym::size_of_val => {
147-
// Avoid `deref_operand` -- this is not a deref, the ptr does not have to be
147+
// Avoid `deref_pointer` -- this is not a deref, the ptr does not have to be
148148
// dereferenceable!
149149
let place = self.ref_to_mplace(&self.read_immediate(&args[0])?)?;
150150
let (size, align) = self
@@ -225,7 +225,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
225225
self.write_scalar(val, dest)?;
226226
}
227227
sym::discriminant_value => {
228-
let place = self.deref_operand(&args[0])?;
228+
let place = self.deref_pointer(&args[0])?;
229229
let variant = self.read_discriminant(&place)?;
230230
let discr = self.discriminant_for_variant(place.layout, variant)?;
231231
self.write_scalar(discr, dest)?;

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
317317
kind = "static_mem"
318318
)
319319
}
320-
None => err_ub!(PointerUseAfterFree(alloc_id)),
320+
None => err_ub!(PointerUseAfterFree(alloc_id, CheckInAllocMsg::MemoryAccessTest)),
321321
}
322322
.into());
323323
};
@@ -380,7 +380,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
380380
M::enforce_alignment(self),
381381
CheckInAllocMsg::MemoryAccessTest,
382382
|alloc_id, offset, prov| {
383-
let (size, align) = self.get_live_alloc_size_and_align(alloc_id)?;
383+
let (size, align) = self
384+
.get_live_alloc_size_and_align(alloc_id, CheckInAllocMsg::MemoryAccessTest)?;
384385
Ok((size, align, (alloc_id, offset, prov)))
385386
},
386387
)
@@ -404,7 +405,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
404405
CheckAlignment::Error,
405406
msg,
406407
|alloc_id, _, _| {
407-
let (size, align) = self.get_live_alloc_size_and_align(alloc_id)?;
408+
let (size, align) = self.get_live_alloc_size_and_align(alloc_id, msg)?;
408409
Ok((size, align, ()))
409410
},
410411
)?;
@@ -414,7 +415,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
414415
/// Low-level helper function to check if a ptr is in-bounds and potentially return a reference
415416
/// to the allocation it points to. Supports both shared and mutable references, as the actual
416417
/// checking is offloaded to a helper closure. `align` defines whether and which alignment check
417-
/// is done. Returns `None` for size 0, and otherwise `Some` of what `alloc_size` returned.
418+
/// is done.
419+
///
420+
/// If this returns `None`, the size is 0; it can however return `Some` even for size 0.
418421
fn check_and_deref_ptr<T>(
419422
&self,
420423
ptr: Pointer<Option<M::Provenance>>,
@@ -515,7 +518,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
515518
}
516519
Some(GlobalAlloc::Function(..)) => throw_ub!(DerefFunctionPointer(id)),
517520
Some(GlobalAlloc::VTable(..)) => throw_ub!(DerefVTablePointer(id)),
518-
None => throw_ub!(PointerUseAfterFree(id)),
521+
None => throw_ub!(PointerUseAfterFree(id, CheckInAllocMsg::MemoryAccessTest)),
519522
Some(GlobalAlloc::Static(def_id)) => {
520523
assert!(self.tcx.is_static(def_id));
521524
assert!(!self.tcx.is_thread_local_static(def_id));
@@ -761,11 +764,15 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
761764
}
762765
}
763766

764-
/// Obtain the size and alignment of a live allocation.
765-
pub fn get_live_alloc_size_and_align(&self, id: AllocId) -> InterpResult<'tcx, (Size, Align)> {
767+
/// Obtain the size and alignment of a *live* allocation.
768+
fn get_live_alloc_size_and_align(
769+
&self,
770+
id: AllocId,
771+
msg: CheckInAllocMsg,
772+
) -> InterpResult<'tcx, (Size, Align)> {
766773
let (size, align, kind) = self.get_alloc_info(id);
767774
if matches!(kind, AllocKind::Dead) {
768-
throw_ub!(PointerUseAfterFree(id))
775+
throw_ub!(PointerUseAfterFree(id, msg))
769776
}
770777
Ok((size, align))
771778
}

compiler/rustc_const_eval/src/interpret/place.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ where
419419
///
420420
/// Only call this if you are sure the place is "valid" (aligned and inbounds), or do not
421421
/// want to ever use the place for memory access!
422-
/// Generally prefer `deref_operand`.
422+
/// Generally prefer `deref_pointer`.
423423
pub fn ref_to_mplace(
424424
&self,
425425
val: &ImmTy<'tcx, M::Provenance>,
@@ -439,8 +439,9 @@ where
439439
}
440440

441441
/// Take an operand, representing a pointer, and dereference it to a place.
442+
/// Corresponds to the `*` operator in Rust.
442443
#[instrument(skip(self), level = "debug")]
443-
pub fn deref_operand(
444+
pub fn deref_pointer(
444445
&self,
445446
src: &impl Readable<'tcx, M::Provenance>,
446447
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {

compiler/rustc_const_eval/src/interpret/projection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ where
290290
OpaqueCast(ty) => base.transmute(self.layout_of(ty)?, self)?,
291291
Field(field, _) => self.project_field(base, field.index())?,
292292
Downcast(_, variant) => self.project_downcast(base, variant)?,
293-
Deref => self.deref_operand(&base.to_op(self)?)?.into(),
293+
Deref => self.deref_pointer(&base.to_op(self)?)?.into(),
294294
Index(local) => {
295295
let layout = self.layout_of(self.tcx.types.usize)?;
296296
let n = self.local_to_op(self.frame(), local, Some(layout))?;

compiler/rustc_const_eval/src/interpret/step.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
224224

225225
Len(place) => {
226226
let src = self.eval_place(place)?;
227-
let op = self.place_to_op(&src)?;
228-
let len = op.len(self)?;
227+
let len = src.len(self)?;
229228
self.write_scalar(Scalar::from_target_usize(len, self), &dest)?;
230229
}
231230

compiler/rustc_const_eval/src/interpret/terminator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
661661
let receiver_place = loop {
662662
match receiver.layout.ty.kind() {
663663
ty::Ref(..) | ty::RawPtr(..) => {
664-
// We do *not* use `deref_operand` here: we don't want to conceptually
664+
// We do *not* use `deref_pointer` here: we don't want to conceptually
665665
// create a place that must be dereferenceable, since the receiver might
666666
// be a raw pointer and (for `*const dyn Trait`) we don't need to
667667
// actually access memory to resolve this method.

0 commit comments

Comments
 (0)