Skip to content

interpret: fix TypeId pointers being considered data pointers #144169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions compiler/rustc_const_eval/src/interpret/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ pub enum AllocKind {
LiveData,
/// A function allocation (that fn ptrs point to).
Function,
/// A (symbolic) vtable allocation.
VTable,
/// A "virtual" allocation, used for vtables and TypeId.
Virtual,
/// A dead allocation.
Dead,
}
Expand Down Expand Up @@ -950,11 +950,9 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
let (size, align) = global_alloc.size_and_align(*self.tcx, self.typing_env);
let mutbl = global_alloc.mutability(*self.tcx, self.typing_env);
let kind = match global_alloc {
GlobalAlloc::TypeId { .. }
| GlobalAlloc::Static { .. }
| GlobalAlloc::Memory { .. } => AllocKind::LiveData,
GlobalAlloc::Static { .. } | GlobalAlloc::Memory { .. } => AllocKind::LiveData,
GlobalAlloc::Function { .. } => bug!("We already checked function pointers above"),
GlobalAlloc::VTable { .. } => AllocKind::VTable,
GlobalAlloc::VTable { .. } | GlobalAlloc::TypeId { .. } => AllocKind::Virtual,
};
return AllocInfo::new(size, align, kind, mutbl);
}
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ impl TypeId {

// This is a provenance-stripping memcpy.
for (i, chunk) in self.data.iter().copied().enumerate() {
let chunk = chunk.expose_provenance().to_ne_bytes();
let chunk = chunk.addr().to_ne_bytes();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not required, but since we want to strip the provenance anyway there's no reason to expose it.

let start = i * chunk.len();
bytes[start..(start + chunk.len())].copy_from_slice(&chunk);
}
Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/src/alloc_addresses/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
this.get_alloc_bytes_unchecked_raw(alloc_id)?
}
}
AllocKind::Function | AllocKind::VTable => {
AllocKind::Function | AllocKind::Virtual => {
// Allocate some dummy memory to get a unique address for this function/vtable.
let alloc_bytes = MiriAllocBytes::from_bytes(
&[0u8; 1],
Expand Down
4 changes: 2 additions & 2 deletions src/tools/miri/src/borrow_tracker/stacked_borrows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ trait EvalContextPrivExt<'tcx, 'ecx>: crate::MiriInterpCxExt<'tcx> {
dcx.log_protector();
}
},
AllocKind::Function | AllocKind::VTable | AllocKind::Dead => {
AllocKind::Function | AllocKind::Virtual | AllocKind::Dead => {
// No stacked borrows on these allocations.
}
}
Expand Down Expand Up @@ -1021,7 +1021,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
trace!("Stacked Borrows tag {tag:?} exposed in {alloc_id:?}");
alloc_extra.borrow_tracker_sb().borrow_mut().exposed_tags.insert(tag);
}
AllocKind::Function | AllocKind::VTable | AllocKind::Dead => {
AllocKind::Function | AllocKind::Virtual | AllocKind::Dead => {
// No stacked borrows on these allocations.
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/src/borrow_tracker/tree_borrows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
trace!("Tree Borrows tag {tag:?} exposed in {alloc_id:?}");
alloc_extra.borrow_tracker_tb().borrow_mut().expose_tag(tag);
}
AllocKind::Function | AllocKind::VTable | AllocKind::Dead => {
AllocKind::Function | AllocKind::Virtual | AllocKind::Dead => {
// No tree borrows on these allocations.
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/tools/miri/tests/pass/intrinsics/type-id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::any::{Any, TypeId};

fn main() {
let t1 = TypeId::of::<u64>();
let t2 = TypeId::of::<u64>();
assert_eq!(t1, t2);
let t3 = TypeId::of::<usize>();
assert_ne!(t1, t3);

let _ = format!("{t1:?}"); // test that we can debug-print

let b = Box::new(0u64) as Box<dyn Any>;
assert_eq!(*b.downcast_ref::<u64>().unwrap(), 0);
assert!(b.downcast_ref::<usize>().is_none());

// Get the first pointer chunk and try to make it a ZST ref.
// This used to trigger an error because TypeId allocs got misclassified as "LiveData".
let _raw_chunk = unsafe { (&raw const t1).cast::<&()>().read() };
}
Loading