rustc_middle/mir/interpret/
mod.rs1#[macro_use]
4mod error;
5
6mod allocation;
7mod pointer;
8mod queries;
9mod value;
10
11use std::io::{Read, Write};
12use std::num::NonZero;
13use std::{fmt, io};
14
15use rustc_abi::{AddressSpace, Align, Endian, HasDataLayout, Size};
16use rustc_ast::{LitKind, Mutability};
17use rustc_data_structures::fx::FxHashMap;
18use rustc_data_structures::sharded::ShardedHashMap;
19use rustc_data_structures::sync::{AtomicU64, Lock};
20use rustc_hir::def::DefKind;
21use rustc_hir::def_id::{DefId, LocalDefId};
22use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
23use rustc_serialize::{Decodable, Encodable};
24use tracing::{debug, trace};
25pub use {
27 err_exhaust, err_inval, err_machine_stop, err_ub, err_ub_custom, err_ub_format, err_unsup,
28 err_unsup_format, throw_exhaust, throw_inval, throw_machine_stop, throw_ub, throw_ub_custom,
29 throw_ub_format, throw_unsup, throw_unsup_format,
30};
31
32pub use self::allocation::{
33 AllocBytes, AllocError, AllocInit, AllocRange, AllocResult, Allocation, ConstAllocation,
34 InitChunk, InitChunkIter, alloc_range,
35};
36pub use self::error::{
37 BadBytesAccess, CheckAlignMsg, CheckInAllocMsg, ErrorHandled, EvalStaticInitializerRawResult,
38 EvalToAllocationRawResult, EvalToConstValueResult, EvalToValTreeResult, ExpectedKind,
39 InterpErrorInfo, InterpErrorKind, InterpResult, InvalidMetaKind, InvalidProgramInfo,
40 MachineStopType, Misalignment, PointerKind, ReportedErrorInfo, ResourceExhaustionInfo,
41 ScalarSizeMismatch, UndefinedBehaviorInfo, UnsupportedOpInfo, ValTreeCreationError,
42 ValidationErrorInfo, ValidationErrorKind, interp_ok,
43};
44pub use self::pointer::{CtfeProvenance, Pointer, PointerArithmetic, Provenance};
45pub use self::value::Scalar;
46use crate::mir;
47use crate::ty::codec::{TyDecoder, TyEncoder};
48use crate::ty::print::with_no_trimmed_paths;
49use crate::ty::{self, Instance, Ty, TyCtxt};
50
51#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, TyEncodable, TyDecodable)]
55#[derive(HashStable, TypeFoldable, TypeVisitable)]
56pub struct GlobalId<'tcx> {
57 pub instance: ty::Instance<'tcx>,
60
61 pub promoted: Option<mir::Promoted>,
63}
64
65impl<'tcx> GlobalId<'tcx> {
66 pub fn display(self, tcx: TyCtxt<'tcx>) -> String {
67 let instance_name = with_no_trimmed_paths!(tcx.def_path_str(self.instance.def.def_id()));
68 if let Some(promoted) = self.promoted {
69 format!("{instance_name}::{promoted:?}")
70 } else {
71 instance_name
72 }
73 }
74}
75
76#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, HashStable)]
78pub struct LitToConstInput<'tcx> {
79 pub lit: LitKind,
81 pub ty: Ty<'tcx>,
83 pub neg: bool,
85}
86
87#[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
88pub struct AllocId(pub NonZero<u64>);
89
90impl fmt::Debug for AllocId {
93 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94 if f.alternate() { write!(f, "a{}", self.0) } else { write!(f, "alloc{}", self.0) }
95 }
96}
97
98#[derive(TyDecodable, TyEncodable)]
101enum AllocDiscriminant {
102 Alloc,
103 Fn,
104 VTable,
105 Static,
106 Type,
107}
108
109pub fn specialized_encode_alloc_id<'tcx, E: TyEncoder<'tcx>>(
110 encoder: &mut E,
111 tcx: TyCtxt<'tcx>,
112 alloc_id: AllocId,
113) {
114 match tcx.global_alloc(alloc_id) {
115 GlobalAlloc::Memory(alloc) => {
116 trace!("encoding {:?} with {:#?}", alloc_id, alloc);
117 AllocDiscriminant::Alloc.encode(encoder);
118 alloc.encode(encoder);
119 }
120 GlobalAlloc::Function { instance } => {
121 trace!("encoding {:?} with {:#?}", alloc_id, instance);
122 AllocDiscriminant::Fn.encode(encoder);
123 instance.encode(encoder);
124 }
125 GlobalAlloc::VTable(ty, poly_trait_ref) => {
126 trace!("encoding {:?} with {ty:#?}, {poly_trait_ref:#?}", alloc_id);
127 AllocDiscriminant::VTable.encode(encoder);
128 ty.encode(encoder);
129 poly_trait_ref.encode(encoder);
130 }
131 GlobalAlloc::TypeId { ty } => {
132 trace!("encoding {alloc_id:?} with {ty:#?}");
133 AllocDiscriminant::Type.encode(encoder);
134 ty.encode(encoder);
135 }
136 GlobalAlloc::Static(did) => {
137 assert!(!tcx.is_thread_local_static(did));
138 AllocDiscriminant::Static.encode(encoder);
141 Encodable::<E>::encode(&did, encoder);
144 }
145 }
146}
147
148#[derive(Clone)]
149enum State {
150 Empty,
151 Done(AllocId),
152}
153
154pub struct AllocDecodingState {
155 decoding_state: Vec<Lock<State>>,
157 data_offsets: Vec<u64>,
159}
160
161impl AllocDecodingState {
162 #[inline]
163 pub fn new_decoding_session(&self) -> AllocDecodingSession<'_> {
164 AllocDecodingSession { state: self }
165 }
166
167 pub fn new(data_offsets: Vec<u64>) -> Self {
168 let decoding_state =
169 std::iter::repeat_with(|| Lock::new(State::Empty)).take(data_offsets.len()).collect();
170
171 Self { decoding_state, data_offsets }
172 }
173}
174
175#[derive(Copy, Clone)]
176pub struct AllocDecodingSession<'s> {
177 state: &'s AllocDecodingState,
178}
179
180impl<'s> AllocDecodingSession<'s> {
181 pub fn decode_alloc_id<'tcx, D>(&self, decoder: &mut D) -> AllocId
183 where
184 D: TyDecoder<'tcx>,
185 {
186 let idx = usize::try_from(decoder.read_u32()).unwrap();
188 let pos = usize::try_from(self.state.data_offsets[idx]).unwrap();
189
190 let (alloc_kind, pos) = decoder.with_position(pos, |decoder| {
193 let alloc_kind = AllocDiscriminant::decode(decoder);
194 (alloc_kind, decoder.position())
195 });
196
197 let mut entry = self.state.decoding_state[idx].lock();
210 if let State::Done(alloc_id) = *entry {
213 return alloc_id;
214 }
215
216 let alloc_id = decoder.with_position(pos, |decoder| match alloc_kind {
218 AllocDiscriminant::Alloc => {
219 trace!("creating memory alloc ID");
220 let alloc = <ConstAllocation<'tcx> as Decodable<_>>::decode(decoder);
221 trace!("decoded alloc {:?}", alloc);
222 decoder.interner().reserve_and_set_memory_alloc(alloc)
223 }
224 AllocDiscriminant::Fn => {
225 trace!("creating fn alloc ID");
226 let instance = ty::Instance::decode(decoder);
227 trace!("decoded fn alloc instance: {:?}", instance);
228 decoder.interner().reserve_and_set_fn_alloc(instance, CTFE_ALLOC_SALT)
229 }
230 AllocDiscriminant::VTable => {
231 trace!("creating vtable alloc ID");
232 let ty = Decodable::decode(decoder);
233 let poly_trait_ref = Decodable::decode(decoder);
234 trace!("decoded vtable alloc instance: {ty:?}, {poly_trait_ref:?}");
235 decoder.interner().reserve_and_set_vtable_alloc(ty, poly_trait_ref, CTFE_ALLOC_SALT)
236 }
237 AllocDiscriminant::Type => {
238 trace!("creating typeid alloc ID");
239 let ty = Decodable::decode(decoder);
240 trace!("decoded typid: {ty:?}");
241 decoder.interner().reserve_and_set_type_id_alloc(ty)
242 }
243 AllocDiscriminant::Static => {
244 trace!("creating extern static alloc ID");
245 let did = <DefId as Decodable<D>>::decode(decoder);
246 trace!("decoded static def-ID: {:?}", did);
247 decoder.interner().reserve_and_set_static_alloc(did)
248 }
249 });
250
251 *entry = State::Done(alloc_id);
252
253 alloc_id
254 }
255}
256
257#[derive(Debug, Clone, Eq, PartialEq, Hash, TyDecodable, TyEncodable, HashStable)]
260pub enum GlobalAlloc<'tcx> {
261 Function { instance: Instance<'tcx> },
263 VTable(Ty<'tcx>, &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>),
268 Static(DefId),
271 Memory(ConstAllocation<'tcx>),
273 TypeId { ty: Ty<'tcx> },
276}
277
278impl<'tcx> GlobalAlloc<'tcx> {
279 #[track_caller]
281 #[inline]
282 pub fn unwrap_memory(&self) -> ConstAllocation<'tcx> {
283 match *self {
284 GlobalAlloc::Memory(mem) => mem,
285 _ => bug!("expected memory, got {:?}", self),
286 }
287 }
288
289 #[track_caller]
291 #[inline]
292 pub fn unwrap_fn(&self) -> Instance<'tcx> {
293 match *self {
294 GlobalAlloc::Function { instance, .. } => instance,
295 _ => bug!("expected function, got {:?}", self),
296 }
297 }
298
299 #[track_caller]
301 #[inline]
302 pub fn unwrap_vtable(&self) -> (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>) {
303 match *self {
304 GlobalAlloc::VTable(ty, dyn_ty) => (ty, dyn_ty.principal()),
305 _ => bug!("expected vtable, got {:?}", self),
306 }
307 }
308
309 #[inline]
311 pub fn address_space(&self, cx: &impl HasDataLayout) -> AddressSpace {
312 match self {
313 GlobalAlloc::Function { .. } => cx.data_layout().instruction_address_space,
314 GlobalAlloc::TypeId { .. }
315 | GlobalAlloc::Static(..)
316 | GlobalAlloc::Memory(..)
317 | GlobalAlloc::VTable(..) => AddressSpace::ZERO,
318 }
319 }
320
321 pub fn mutability(&self, tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>) -> Mutability {
322 match self {
324 GlobalAlloc::Static(did) => {
325 let DefKind::Static { safety: _, mutability, nested } = tcx.def_kind(did) else {
326 bug!()
327 };
328 if nested {
329 if cfg!(debug_assertions) {
332 let alloc = tcx.eval_static_initializer(did).unwrap();
333 assert_eq!(alloc.0.mutability, mutability);
334 }
335 mutability
336 } else {
337 let mutability = match mutability {
338 Mutability::Not
339 if !tcx
340 .type_of(did)
341 .no_bound_vars()
342 .expect("statics should not have generic parameters")
343 .is_freeze(tcx, typing_env) =>
344 {
345 Mutability::Mut
346 }
347 _ => mutability,
348 };
349 mutability
350 }
351 }
352 GlobalAlloc::Memory(alloc) => alloc.inner().mutability,
353 GlobalAlloc::TypeId { .. } | GlobalAlloc::Function { .. } | GlobalAlloc::VTable(..) => {
354 Mutability::Not
356 }
357 }
358 }
359
360 pub fn size_and_align(
361 &self,
362 tcx: TyCtxt<'tcx>,
363 typing_env: ty::TypingEnv<'tcx>,
364 ) -> (Size, Align) {
365 match self {
366 GlobalAlloc::Static(def_id) => {
367 let DefKind::Static { nested, .. } = tcx.def_kind(def_id) else {
368 bug!("GlobalAlloc::Static is not a static")
369 };
370
371 if nested {
372 let alloc = tcx.eval_static_initializer(def_id).unwrap();
377 (alloc.0.size(), alloc.0.align)
378 } else {
379 let ty = tcx
384 .type_of(def_id)
385 .no_bound_vars()
386 .expect("statics should not have generic parameters");
387 let layout = tcx.layout_of(typing_env.as_query_input(ty)).unwrap();
388 assert!(layout.is_sized());
389 (layout.size, layout.align.abi)
390 }
391 }
392 GlobalAlloc::Memory(alloc) => {
393 let alloc = alloc.inner();
394 (alloc.size(), alloc.align)
395 }
396 GlobalAlloc::Function { .. } => (Size::ZERO, Align::ONE),
397 GlobalAlloc::VTable(..) => {
398 (Size::ZERO, tcx.data_layout.pointer_align().abi)
400 }
401 GlobalAlloc::TypeId { .. } => (Size::ZERO, Align::ONE),
403 }
404 }
405}
406
407pub const CTFE_ALLOC_SALT: usize = 0;
408
409pub(crate) struct AllocMap<'tcx> {
410 to_alloc: ShardedHashMap<AllocId, GlobalAlloc<'tcx>>,
418
419 dedup: Lock<FxHashMap<(GlobalAlloc<'tcx>, usize), AllocId>>,
424
425 next_id: AtomicU64,
428}
429
430impl<'tcx> AllocMap<'tcx> {
431 pub(crate) fn new() -> Self {
432 AllocMap {
433 to_alloc: Default::default(),
434 dedup: Default::default(),
435 next_id: AtomicU64::new(1),
436 }
437 }
438 fn reserve(&self) -> AllocId {
439 let next_id = self.next_id.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
443 AllocId(NonZero::new(next_id).unwrap())
444 }
445}
446
447impl<'tcx> TyCtxt<'tcx> {
448 pub fn reserve_alloc_id(self) -> AllocId {
454 self.alloc_map.reserve()
455 }
456
457 fn reserve_and_set_dedup(self, alloc: GlobalAlloc<'tcx>, salt: usize) -> AllocId {
460 if let GlobalAlloc::Memory(mem) = alloc {
461 if mem.inner().mutability.is_mut() {
462 bug!("trying to dedup-reserve mutable memory");
463 }
464 }
465 let alloc_salt = (alloc, salt);
466 let mut dedup = self.alloc_map.dedup.lock();
468 if let Some(&alloc_id) = dedup.get(&alloc_salt) {
469 return alloc_id;
470 }
471 let id = self.alloc_map.reserve();
472 debug!("creating alloc {:?} with id {id:?}", alloc_salt.0);
473 let had_previous = self.alloc_map.to_alloc.insert(id, alloc_salt.0.clone()).is_some();
474 assert!(!had_previous);
476 dedup.insert(alloc_salt, id);
477 id
478 }
479
480 pub fn reserve_and_set_memory_dedup(self, mem: ConstAllocation<'tcx>, salt: usize) -> AllocId {
483 self.reserve_and_set_dedup(GlobalAlloc::Memory(mem), salt)
484 }
485
486 pub fn reserve_and_set_static_alloc(self, static_id: DefId) -> AllocId {
489 let salt = 0; self.reserve_and_set_dedup(GlobalAlloc::Static(static_id), salt)
491 }
492
493 pub fn reserve_and_set_fn_alloc(self, instance: Instance<'tcx>, salt: usize) -> AllocId {
495 self.reserve_and_set_dedup(GlobalAlloc::Function { instance }, salt)
496 }
497
498 pub fn reserve_and_set_vtable_alloc(
500 self,
501 ty: Ty<'tcx>,
502 dyn_ty: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
503 salt: usize,
504 ) -> AllocId {
505 self.reserve_and_set_dedup(GlobalAlloc::VTable(ty, dyn_ty), salt)
506 }
507
508 pub fn reserve_and_set_type_id_alloc(self, ty: Ty<'tcx>) -> AllocId {
510 self.reserve_and_set_dedup(GlobalAlloc::TypeId { ty }, 0)
511 }
512
513 pub fn reserve_and_set_memory_alloc(self, mem: ConstAllocation<'tcx>) -> AllocId {
519 let id = self.reserve_alloc_id();
520 self.set_alloc_id_memory(id, mem);
521 id
522 }
523
524 #[inline]
530 pub fn try_get_global_alloc(self, id: AllocId) -> Option<GlobalAlloc<'tcx>> {
531 self.alloc_map.to_alloc.get(&id)
532 }
533
534 #[inline]
535 #[track_caller]
536 pub fn global_alloc(self, id: AllocId) -> GlobalAlloc<'tcx> {
541 match self.try_get_global_alloc(id) {
542 Some(alloc) => alloc,
543 None => bug!("could not find allocation for {id:?}"),
544 }
545 }
546
547 pub fn set_alloc_id_memory(self, id: AllocId, mem: ConstAllocation<'tcx>) {
550 if let Some(old) = self.alloc_map.to_alloc.insert(id, GlobalAlloc::Memory(mem)) {
551 bug!("tried to set allocation ID {id:?}, but it was already existing as {old:#?}");
552 }
553 }
554
555 pub fn set_nested_alloc_id_static(self, id: AllocId, def_id: LocalDefId) {
558 if let Some(old) =
559 self.alloc_map.to_alloc.insert(id, GlobalAlloc::Static(def_id.to_def_id()))
560 {
561 bug!("tried to set allocation ID {id:?}, but it was already existing as {old:#?}");
562 }
563 }
564}
565
566#[inline]
571pub fn write_target_uint(
572 endianness: Endian,
573 mut target: &mut [u8],
574 data: u128,
575) -> Result<(), io::Error> {
576 match endianness {
579 Endian::Little => target.write(&data.to_le_bytes())?,
580 Endian::Big => target.write(&data.to_be_bytes()[16 - target.len()..])?,
581 };
582 debug_assert!(target.len() == 0); Ok(())
584}
585
586#[inline]
587pub fn read_target_uint(endianness: Endian, mut source: &[u8]) -> Result<u128, io::Error> {
588 let mut buf = [0u8; size_of::<u128>()];
590 let uint = match endianness {
592 Endian::Little => {
593 source.read_exact(&mut buf[..source.len()])?;
594 Ok(u128::from_le_bytes(buf))
595 }
596 Endian::Big => {
597 source.read_exact(&mut buf[16 - source.len()..])?;
598 Ok(u128::from_be_bytes(buf))
599 }
600 };
601 debug_assert!(source.len() == 0); uint
603}