Skip to content

Commit 654e334

Browse files
committed
Change miri to use tcx allocated allocations.
1 parent a5b7511 commit 654e334

File tree

5 files changed

+36
-29
lines changed

5 files changed

+36
-29
lines changed

compiler/rustc_mir/src/interpret/eval_context.rs

-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::cell::Cell;
22
use std::fmt;
33
use std::mem;
44

5-
use rustc_data_structures::fx::FxHashMap;
65
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
76
use rustc_hir::{self as hir, def_id::DefId, definitions::DefPathData};
87
use rustc_index::vec::IndexVec;
@@ -40,10 +39,6 @@ pub struct InterpCx<'mir, 'tcx, M: Machine<'mir, 'tcx>> {
4039

4140
/// The virtual memory system.
4241
pub memory: Memory<'mir, 'tcx, M>,
43-
44-
/// A cache for deduplicating vtables
45-
pub(super) vtables:
46-
FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), Pointer<M::PointerTag>>,
4742
}
4843

4944
// The Phantomdata exists to prevent this type from being `Send`. If it were sent across a thread
@@ -393,7 +388,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
393388
tcx: tcx.at(root_span),
394389
param_env,
395390
memory: Memory::new(tcx, memory_extra),
396-
vtables: FxHashMap::default(),
397391
}
398392
}
399393

compiler/rustc_mir/src/interpret/intern.rs

-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ fn intern_shallow<'rt, 'mir, 'tcx, M: CompileTimeMachine<'mir, 'tcx, const_eval:
107107
match kind {
108108
MemoryKind::Stack
109109
| MemoryKind::Machine(const_eval::MemoryKind::Heap)
110-
| MemoryKind::Vtable
111110
| MemoryKind::CallerLocation => {}
112111
}
113112
// Set allocation mutability as appropriate. This is used by LLVM to put things into

compiler/rustc_mir/src/interpret/memory.rs

-4
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ use crate::util::pretty;
2727
pub enum MemoryKind<T> {
2828
/// Stack memory. Error if deallocated except during a stack pop.
2929
Stack,
30-
/// Memory backing vtables. Error if ever deallocated.
31-
Vtable,
3230
/// Memory allocated by `caller_location` intrinsic. Error if ever deallocated.
3331
CallerLocation,
3432
/// Additional memory kinds a machine wishes to distinguish from the builtin ones.
@@ -40,7 +38,6 @@ impl<T: MayLeak> MayLeak for MemoryKind<T> {
4038
fn may_leak(self) -> bool {
4139
match self {
4240
MemoryKind::Stack => false,
43-
MemoryKind::Vtable => true,
4441
MemoryKind::CallerLocation => true,
4542
MemoryKind::Machine(k) => k.may_leak(),
4643
}
@@ -51,7 +48,6 @@ impl<T: fmt::Display> fmt::Display for MemoryKind<T> {
5148
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5249
match self {
5350
MemoryKind::Stack => write!(f, "stack variable"),
54-
MemoryKind::Vtable => write!(f, "vtable"),
5551
MemoryKind::CallerLocation => write!(f, "caller location"),
5652
MemoryKind::Machine(m) => write!(f, "{}", m),
5753
}

compiler/rustc_mir/src/interpret/terminator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
459459
};
460460
// Find and consult vtable
461461
let vtable = receiver_place.vtable();
462-
let drop_fn = self.get_vtable_slot(vtable, u64::try_from(idx).unwrap())?;
462+
let fn_val = self.get_vtable_slot(vtable, u64::try_from(idx).unwrap())?;
463463

464464
// `*mut receiver_place.layout.ty` is almost the layout that we
465465
// want for args[0]: We have to project to field 0 because we want
@@ -472,7 +472,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
472472
OpTy::from(ImmTy::from_immediate(receiver_place.ptr.into(), this_receiver_ptr));
473473
trace!("Patched self operand to {:#?}", args[0]);
474474
// recurse with concrete function
475-
self.eval_fn_call(drop_fn, caller_abi, &args, ret, unwind)
475+
self.eval_fn_call(fn_val, caller_abi, &args, ret, unwind)
476476
}
477477
}
478478
}

compiler/rustc_mir/src/interpret/traits.rs

+34-16
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
use std::convert::TryFrom;
22

3-
use rustc_middle::mir::interpret::{InterpResult, Pointer, PointerArithmetic, Scalar};
3+
use rustc_middle::mir::interpret::{
4+
AllocError, InterpError, InterpResult, Pointer, PointerArithmetic, Scalar,
5+
UndefinedBehaviorInfo, UnsupportedOpInfo,
6+
};
47
use rustc_middle::ty::{
58
self, Instance, Ty, VtblEntry, COMMON_VTABLE_ENTRIES, COMMON_VTABLE_ENTRIES_ALIGN,
69
COMMON_VTABLE_ENTRIES_DROPINPLACE, COMMON_VTABLE_ENTRIES_SIZE,
710
};
811
use rustc_target::abi::{Align, LayoutOf, Size};
912

13+
use super::alloc_range;
1014
use super::util::ensure_monomorphic_enough;
11-
use super::{FnVal, InterpCx, Machine, MemoryKind};
15+
use super::{Allocation, FnVal, InterpCx, Machine};
16+
17+
fn vtable_alloc_error_to_interp_error<'tcx>(error: AllocError) -> InterpError<'tcx> {
18+
match error {
19+
AllocError::ReadPointerAsBytes => {
20+
InterpError::Unsupported(UnsupportedOpInfo::ReadPointerAsBytes)
21+
}
22+
AllocError::InvalidUninitBytes(_info) => {
23+
InterpError::UndefinedBehavior(UndefinedBehaviorInfo::InvalidUninitBytes(None))
24+
}
25+
}
26+
}
1227

1328
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
1429
/// Creates a dynamic vtable for the given type and vtable origin. This is used only for
@@ -60,10 +75,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
6075
// `get_vtable` in `rust_codegen_llvm/meth.rs`.
6176
// /////////////////////////////////////////////////////////////////////////////////////////
6277
let vtable_size = ptr_size * u64::try_from(vtable_entries.len()).unwrap();
63-
let vtable = self.memory.allocate(vtable_size, ptr_align, MemoryKind::Vtable);
64-
65-
let drop = Instance::resolve_drop_in_place(tcx, ty);
66-
let drop = self.memory.create_fn_alloc(FnVal::Instance(drop));
78+
let mut vtable = Allocation::uninit(vtable_size, ptr_align);
6779

6880
// No need to do any alignment checks on the memory accesses below, because we know the
6981
// allocation is correctly aligned as we created it above. Also we're only offsetting by
@@ -72,36 +84,42 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
7284
.iter()
7385
.map(|entry| -> InterpResult<'tcx, _> {
7486
match entry {
75-
VtblEntry::MetadataDropInPlace => Ok(Some(drop.into())),
87+
VtblEntry::MetadataDropInPlace => {
88+
let instance = Instance::resolve_drop_in_place(tcx, ty);
89+
let fn_alloc_id = tcx.create_fn_alloc(instance);
90+
let fn_ptr = Pointer::from(fn_alloc_id);
91+
Ok(Some(fn_ptr.into()))
92+
}
7693
VtblEntry::MetadataSize => Ok(Some(Scalar::from_uint(size, ptr_size).into())),
7794
VtblEntry::MetadataAlign => Ok(Some(Scalar::from_uint(align, ptr_size).into())),
7895
VtblEntry::Vacant => Ok(None),
7996
VtblEntry::Method(def_id, substs) => {
8097
// Prepare the fn ptr we write into the vtable.
8198
let instance =
82-
ty::Instance::resolve_for_vtable(tcx, self.param_env, *def_id, substs)
99+
Instance::resolve_for_vtable(tcx, self.param_env, *def_id, substs)
83100
.ok_or_else(|| err_inval!(TooGeneric))?;
84-
let fn_ptr = self.memory.create_fn_alloc(FnVal::Instance(instance));
101+
let fn_alloc_id = tcx.create_fn_alloc(instance);
102+
let fn_ptr = Pointer::from(fn_alloc_id);
85103
Ok(Some(fn_ptr.into()))
86104
}
87105
}
88106
})
89107
.collect::<Result<Vec<_>, _>>()?;
90-
let mut vtable_alloc =
91-
self.memory.get_mut(vtable.into(), vtable_size, ptr_align)?.expect("not a ZST");
92108
for (idx, scalar) in scalars.into_iter().enumerate() {
93109
if let Some(scalar) = scalar {
94110
let idx: u64 = u64::try_from(idx).unwrap();
95-
vtable_alloc.write_ptr_sized(ptr_size * idx, scalar)?;
111+
vtable
112+
.write_scalar(self, alloc_range(ptr_size * idx, ptr_size), scalar)
113+
.map_err(vtable_alloc_error_to_interp_error)?;
96114
}
97115
}
98116

99-
M::after_static_mem_initialized(self, vtable, vtable_size)?;
117+
let vtable_id = tcx.create_memory_alloc(tcx.intern_const_alloc(vtable));
118+
let vtable_ptr = self.memory.global_base_pointer(Pointer::from(vtable_id))?;
100119

101-
self.memory.mark_immutable(vtable.alloc_id)?;
102-
assert!(self.vtables.insert((ty, poly_trait_ref), vtable).is_none());
120+
assert!(self.vtables.insert((ty, poly_trait_ref), vtable_ptr).is_none());
103121

104-
Ok(vtable)
122+
Ok(vtable_ptr)
105123
}
106124

107125
/// Resolves the function at the specified slot in the provided

0 commit comments

Comments
 (0)