Skip to content

Commit 60329f7

Browse files
committed
Show offsets and derefs in padding, too
1 parent 80a003f commit 60329f7

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ pub enum PathElem {
121121
ArrayElem(usize),
122122
TupleElem(usize),
123123
Deref,
124+
Offset(Size),
124125
EnumTag,
125126
CoroutineTag,
126127
DynDowncast,
@@ -199,6 +200,7 @@ fn write_path(out: &mut String, path: &[PathElem]) {
199200
// even use the usual syntax because we are just showing the projections,
200201
// not the root.
201202
Deref => write!(out, ".<deref>"),
203+
Offset(offset) => write!(out, ".<offset({:x})>", offset.bytes()),
202204
DynDowncast => write!(out, ".<dyn-downcast>"),
203205
}
204206
.unwrap()
@@ -1047,18 +1049,26 @@ impl<'mir, 'tcx> InterpCx<'mir, 'tcx, crate::const_eval::CompileTimeInterpreter<
10471049
if let Some(prov) = prov
10481050
&& let Some((_, alloc)) = self.memory.alloc_map().get(&prov.alloc_id())
10491051
{
1050-
for (_, prov) in alloc.provenance().ptrs().iter() {
1052+
for &(offset, prov) in alloc.provenance().ptrs().iter() {
10511053
if let AllocKind::Dead = self.get_alloc_info(prov.alloc_id()).2 {
10521054
throw_validation_failure!(
10531055
path,
10541056
DanglingPtrUseAfterFree { ptr_kind: PointerKind::Ref(Mutability::Not) }
10551057
)
10561058
} else {
1057-
let ptr = Pointer::new(Some(*prov), Size::ZERO);
1059+
let ptr = Pointer::new(Some(prov), Size::ZERO);
10581060
let ty = self.tcx.types.unit;
10591061
let layout = self.layout_of(ty).unwrap();
10601062
let op = self.ptr_to_mplace(ptr, layout);
1061-
ref_tracking.track(op, || path.clone())
1063+
ref_tracking.track(op, || {
1064+
// We need to clone the path anyway, make sure it gets created
1065+
// with enough space for the additional `Deref`.
1066+
let mut new_path = Vec::with_capacity(path.len() + 1);
1067+
new_path.extend(path.iter().copied());
1068+
new_path.push(PathElem::Offset(offset));
1069+
new_path.push(PathElem::Deref);
1070+
new_path
1071+
})
10621072
}
10631073
}
10641074
}

tests/ui/consts/dangling_raw_ptr.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ error[E0080]: it is undefined behavior to use this value
2424
--> $DIR/dangling_raw_ptr.rs:20:1
2525
|
2626
LL | const BAZ: Union = {
27-
| ^^^^^^^^^^^^^^^^ constructing invalid value: encountered a dangling reference (use-after-free)
27+
| ^^^^^^^^^^^^^^^^ constructing invalid value at .<offset(0)>.<deref>: encountered a dangling reference (use-after-free)
2828
|
2929
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
3030
= note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) {

0 commit comments

Comments
 (0)