Skip to content

Commit 7672e4e

Browse files
committed
interpret: fix TypeId pointers being considered data pointers
1 parent 1079c5e commit 7672e4e

File tree

6 files changed

+28
-11
lines changed

6 files changed

+28
-11
lines changed

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ pub enum AllocKind {
6767
LiveData,
6868
/// A function allocation (that fn ptrs point to).
6969
Function,
70-
/// A (symbolic) vtable allocation.
71-
VTable,
70+
/// A "virtual" allocation, used for vtables and TypeId.
71+
Virtual,
7272
/// A dead allocation.
7373
Dead,
7474
}
@@ -950,11 +950,9 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
950950
let (size, align) = global_alloc.size_and_align(*self.tcx, self.typing_env);
951951
let mutbl = global_alloc.mutability(*self.tcx, self.typing_env);
952952
let kind = match global_alloc {
953-
GlobalAlloc::TypeId { .. }
954-
| GlobalAlloc::Static { .. }
955-
| GlobalAlloc::Memory { .. } => AllocKind::LiveData,
953+
GlobalAlloc::Static { .. } | GlobalAlloc::Memory { .. } => AllocKind::LiveData,
956954
GlobalAlloc::Function { .. } => bug!("We already checked function pointers above"),
957-
GlobalAlloc::VTable { .. } => AllocKind::VTable,
955+
GlobalAlloc::VTable { .. } | GlobalAlloc::TypeId { .. } => AllocKind::Virtual,
958956
};
959957
return AllocInfo::new(size, align, kind, mutbl);
960958
}

library/core/src/any.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ impl TypeId {
783783

784784
// This is a provenance-stripping memcpy.
785785
for (i, chunk) in self.data.iter().copied().enumerate() {
786-
let chunk = chunk.expose_provenance().to_ne_bytes();
786+
let chunk = chunk.addr().to_ne_bytes();
787787
let start = i * chunk.len();
788788
bytes[start..(start + chunk.len())].copy_from_slice(&chunk);
789789
}

src/tools/miri/src/alloc_addresses/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
157157
this.get_alloc_bytes_unchecked_raw(alloc_id)?
158158
}
159159
}
160-
AllocKind::Function | AllocKind::VTable => {
160+
AllocKind::Function | AllocKind::Virtual => {
161161
// Allocate some dummy memory to get a unique address for this function/vtable.
162162
let alloc_bytes = MiriAllocBytes::from_bytes(
163163
&[0u8; 1],

src/tools/miri/src/borrow_tracker/stacked_borrows/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ trait EvalContextPrivExt<'tcx, 'ecx>: crate::MiriInterpCxExt<'tcx> {
650650
dcx.log_protector();
651651
}
652652
},
653-
AllocKind::Function | AllocKind::VTable | AllocKind::Dead => {
653+
AllocKind::Function | AllocKind::Virtual | AllocKind::Dead => {
654654
// No stacked borrows on these allocations.
655655
}
656656
}
@@ -1021,7 +1021,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
10211021
trace!("Stacked Borrows tag {tag:?} exposed in {alloc_id:?}");
10221022
alloc_extra.borrow_tracker_sb().borrow_mut().exposed_tags.insert(tag);
10231023
}
1024-
AllocKind::Function | AllocKind::VTable | AllocKind::Dead => {
1024+
AllocKind::Function | AllocKind::Virtual | AllocKind::Dead => {
10251025
// No stacked borrows on these allocations.
10261026
}
10271027
}

src/tools/miri/src/borrow_tracker/tree_borrows/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
673673
trace!("Tree Borrows tag {tag:?} exposed in {alloc_id:?}");
674674
alloc_extra.borrow_tracker_tb().borrow_mut().expose_tag(tag);
675675
}
676-
AllocKind::Function | AllocKind::VTable | AllocKind::Dead => {
676+
AllocKind::Function | AllocKind::Virtual | AllocKind::Dead => {
677677
// No tree borrows on these allocations.
678678
}
679679
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use std::any::{Any, TypeId};
2+
3+
fn main() {
4+
let t1 = TypeId::of::<u64>();
5+
let t2 = TypeId::of::<u64>();
6+
assert_eq!(t1, t2);
7+
let t3 = TypeId::of::<usize>();
8+
assert_ne!(t1, t3);
9+
10+
let _ = format!("{t1:?}"); // test that we can debug-print
11+
12+
let b = Box::new(0u64) as Box<dyn Any>;
13+
assert_eq!(*b.downcast_ref::<u64>().unwrap(), 0);
14+
assert!(b.downcast_ref::<usize>().is_none());
15+
16+
// Get the first pointer chunk and try to make it a ZST ref.
17+
// This used to trigger an error because TypeId allocs got misclassified as "LiveData".
18+
let _raw_chunk = unsafe { (&raw const t1).cast::<&()>().read() };
19+
}

0 commit comments

Comments
 (0)