1 //! An interpreter for MIR used in CTFE and by miri.
4 macro_rules! err_unsup {
6 $crate::mir::interpret::InterpError::Unsupported(
7 $crate::mir::interpret::UnsupportedOpInfo::$($tt)*
13 macro_rules! err_unsup_format {
14 ($($tt:tt)*) => { err_unsup!(Unsupported(format!($($tt)*))) };
18 macro_rules! err_inval {
20 $crate::mir::interpret::InterpError::InvalidProgram(
21 $crate::mir::interpret::InvalidProgramInfo::$($tt)*
29 $crate::mir::interpret::InterpError::UndefinedBehavior(
30 $crate::mir::interpret::UndefinedBehaviorInfo::$($tt)*
36 macro_rules! err_ub_format {
37 ($($tt:tt)*) => { err_ub!(Ub(format!($($tt)*))) };
41 macro_rules! err_panic {
43 $crate::mir::interpret::InterpError::Panic(
44 $crate::mir::interpret::PanicInfo::$($tt)*
50 macro_rules! err_exhaust {
52 $crate::mir::interpret::InterpError::ResourceExhaustion(
53 $crate::mir::interpret::ResourceExhaustionInfo::$($tt)*
59 macro_rules! throw_unsup {
60 ($($tt:tt)*) => { return Err(err_unsup!($($tt)*).into()) };
64 macro_rules! throw_unsup_format {
65 ($($tt:tt)*) => { throw_unsup!(Unsupported(format!($($tt)*))) };
69 macro_rules! throw_inval {
70 ($($tt:tt)*) => { return Err(err_inval!($($tt)*).into()) };
74 macro_rules! throw_ub {
75 ($($tt:tt)*) => { return Err(err_ub!($($tt)*).into()) };
79 macro_rules! throw_ub_format {
80 ($($tt:tt)*) => { throw_ub!(Ub(format!($($tt)*))) };
84 macro_rules! throw_panic {
85 ($($tt:tt)*) => { return Err(err_panic!($($tt)*).into()) };
89 macro_rules! throw_exhaust {
90 ($($tt:tt)*) => { return Err(err_exhaust!($($tt)*).into()) };
94 macro_rules! throw_machine_stop {
96 return Err($crate::mir::interpret::InterpError::MachineStop(Box::new($($tt)*)).into())
106 pub use self::error::{
107 struct_error, AssertMessage, ConstEvalErr, ConstEvalRawResult, ConstEvalResult, ErrorHandled,
108 FrameInfo, InterpError, InterpErrorInfo, InterpResult, InvalidProgramInfo, PanicInfo,
109 ResourceExhaustionInfo, UndefinedBehaviorInfo, UnsupportedOpInfo,
112 pub use self::value::{get_slice_bytes, ConstValue, RawConst, Scalar, ScalarMaybeUndef};
114 pub use self::allocation::{Allocation, AllocationExtra, Relocations, UndefMask};
116 pub use self::pointer::{CheckInAllocMsg, Pointer, PointerArithmetic};
119 use crate::ty::codec::TyDecoder;
120 use crate::ty::layout::{self, Size};
121 use crate::ty::subst::GenericArgKind;
122 use crate::ty::{self, Instance, TyCtxt};
123 use byteorder::{BigEndian, LittleEndian, ReadBytesExt, WriteBytesExt};
124 use rustc_data_structures::fx::FxHashMap;
125 use rustc_data_structures::sync::{HashMapExt, Lock};
126 use rustc_data_structures::tiny_list::TinyList;
127 use rustc_hir::def_id::DefId;
128 use rustc_macros::HashStable;
129 use rustc_serialize::{Decodable, Encodable, Encoder};
132 use std::num::NonZeroU32;
133 use std::sync::atomic::{AtomicU32, Ordering};
135 /// Uniquely identifies one of the following:
138 /// - A const fn where all arguments (if any) are zero-sized types
139 #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, RustcEncodable, RustcDecodable)]
140 #[derive(HashStable, Lift)]
141 pub struct GlobalId<'tcx> {
142 /// For a constant or static, the `Instance` of the item itself.
143 /// For a promoted global, the `Instance` of the function they belong to.
144 pub instance: ty::Instance<'tcx>,
146 /// The index for promoted globals within their function's `mir::Body`.
147 pub promoted: Option<mir::Promoted>,
150 #[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd, Debug)]
151 pub struct AllocId(pub u64);
153 impl rustc_serialize::UseSpecializedEncodable for AllocId {}
154 impl rustc_serialize::UseSpecializedDecodable for AllocId {}
156 #[derive(RustcDecodable, RustcEncodable)]
157 enum AllocDiscriminant {
163 pub fn specialized_encode_alloc_id<'tcx, E: Encoder>(
167 ) -> Result<(), E::Error> {
168 let alloc: GlobalAlloc<'tcx> =
169 tcx.alloc_map.lock().get(alloc_id).expect("no value for given alloc ID");
171 GlobalAlloc::Memory(alloc) => {
172 trace!("encoding {:?} with {:#?}", alloc_id, alloc);
173 AllocDiscriminant::Alloc.encode(encoder)?;
174 alloc.encode(encoder)?;
176 GlobalAlloc::Function(fn_instance) => {
177 trace!("encoding {:?} with {:#?}", alloc_id, fn_instance);
178 AllocDiscriminant::Fn.encode(encoder)?;
179 fn_instance.encode(encoder)?;
181 GlobalAlloc::Static(did) => {
182 // References to statics doesn't need to know about their allocations,
183 // just about its `DefId`.
184 AllocDiscriminant::Static.encode(encoder)?;
185 did.encode(encoder)?;
191 // Used to avoid infinite recursion when decoding cyclic allocations.
192 type DecodingSessionId = NonZeroU32;
197 InProgressNonAlloc(TinyList<DecodingSessionId>),
198 InProgress(TinyList<DecodingSessionId>, AllocId),
202 pub struct AllocDecodingState {
203 // For each `AllocId`, we keep track of which decoding state it's currently in.
204 decoding_state: Vec<Lock<State>>,
205 // The offsets of each allocation in the data stream.
206 data_offsets: Vec<u32>,
209 impl AllocDecodingState {
210 pub fn new_decoding_session(&self) -> AllocDecodingSession<'_> {
211 static DECODER_SESSION_ID: AtomicU32 = AtomicU32::new(0);
212 let counter = DECODER_SESSION_ID.fetch_add(1, Ordering::SeqCst);
214 // Make sure this is never zero.
215 let session_id = DecodingSessionId::new((counter & 0x7FFFFFFF) + 1).unwrap();
217 AllocDecodingSession { state: self, session_id }
220 pub fn new(data_offsets: Vec<u32>) -> Self {
221 let decoding_state = vec![Lock::new(State::Empty); data_offsets.len()];
223 Self { decoding_state, data_offsets }
227 #[derive(Copy, Clone)]
228 pub struct AllocDecodingSession<'s> {
229 state: &'s AllocDecodingState,
230 session_id: DecodingSessionId,
233 impl<'s> AllocDecodingSession<'s> {
234 /// Decodes an `AllocId` in a thread-safe way.
235 pub fn decode_alloc_id<D>(&self, decoder: &mut D) -> Result<AllocId, D::Error>
239 // Read the index of the allocation.
240 let idx = decoder.read_u32()? as usize;
241 let pos = self.state.data_offsets[idx] as usize;
243 // Decode the `AllocDiscriminant` now so that we know if we have to reserve an
245 let (alloc_kind, pos) = decoder.with_position(pos, |decoder| {
246 let alloc_kind = AllocDiscriminant::decode(decoder)?;
247 Ok((alloc_kind, decoder.position()))
250 // Check the decoding state to see if it's already decoded or if we should
253 let mut entry = self.state.decoding_state[idx].lock();
256 State::Done(alloc_id) => {
259 ref mut entry @ State::Empty => {
260 // We are allowed to decode.
262 AllocDiscriminant::Alloc => {
263 // If this is an allocation, we need to reserve an
264 // `AllocId` so we can decode cyclic graphs.
265 let alloc_id = decoder.tcx().alloc_map.lock().reserve();
267 State::InProgress(TinyList::new_single(self.session_id), alloc_id);
270 AllocDiscriminant::Fn | AllocDiscriminant::Static => {
271 // Fns and statics cannot be cyclic, and their `AllocId`
272 // is determined later by interning.
274 State::InProgressNonAlloc(TinyList::new_single(self.session_id));
279 State::InProgressNonAlloc(ref mut sessions) => {
280 if sessions.contains(&self.session_id) {
281 bug!("this should be unreachable");
283 // Start decoding concurrently.
284 sessions.insert(self.session_id);
288 State::InProgress(ref mut sessions, alloc_id) => {
289 if sessions.contains(&self.session_id) {
293 // Start decoding concurrently.
294 sessions.insert(self.session_id);
301 // Now decode the actual data.
302 let alloc_id = decoder.with_position(pos, |decoder| {
304 AllocDiscriminant::Alloc => {
305 let alloc = <&'tcx Allocation as Decodable>::decode(decoder)?;
306 // We already have a reserved `AllocId`.
307 let alloc_id = alloc_id.unwrap();
308 trace!("decoded alloc {:?}: {:#?}", alloc_id, alloc);
309 decoder.tcx().alloc_map.lock().set_alloc_id_same_memory(alloc_id, alloc);
312 AllocDiscriminant::Fn => {
313 assert!(alloc_id.is_none());
314 trace!("creating fn alloc ID");
315 let instance = ty::Instance::decode(decoder)?;
316 trace!("decoded fn alloc instance: {:?}", instance);
317 let alloc_id = decoder.tcx().alloc_map.lock().create_fn_alloc(instance);
320 AllocDiscriminant::Static => {
321 assert!(alloc_id.is_none());
322 trace!("creating extern static alloc ID");
323 let did = DefId::decode(decoder)?;
324 trace!("decoded static def-ID: {:?}", did);
325 let alloc_id = decoder.tcx().alloc_map.lock().create_static_alloc(did);
331 self.state.decoding_state[idx].with_lock(|entry| {
332 *entry = State::Done(alloc_id);
339 impl fmt::Display for AllocId {
340 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
341 write!(f, "{}", self.0)
345 /// An allocation in the global (tcx-managed) memory can be either a function pointer,
346 /// a static, or a "real" allocation with some data in it.
347 #[derive(Debug, Clone, Eq, PartialEq, Hash, RustcDecodable, RustcEncodable, HashStable)]
348 pub enum GlobalAlloc<'tcx> {
349 /// The alloc ID is used as a function pointer.
350 Function(Instance<'tcx>),
351 /// The alloc ID points to a "lazy" static variable that did not get computed (yet).
352 /// This is also used to break the cycle in recursive statics.
354 /// The alloc ID points to memory.
355 Memory(&'tcx Allocation),
358 pub struct AllocMap<'tcx> {
359 /// Maps `AllocId`s to their corresponding allocations.
360 alloc_map: FxHashMap<AllocId, GlobalAlloc<'tcx>>,
362 /// Used to ensure that statics and functions only get one associated `AllocId`.
363 /// Should never contain a `GlobalAlloc::Memory`!
365 // FIXME: Should we just have two separate dedup maps for statics and functions each?
366 dedup: FxHashMap<GlobalAlloc<'tcx>, AllocId>,
368 /// The `AllocId` to assign to the next requested ID.
369 /// Always incremented; never gets smaller.
373 impl<'tcx> AllocMap<'tcx> {
374 pub fn new() -> Self {
375 AllocMap { alloc_map: Default::default(), dedup: Default::default(), next_id: AllocId(0) }
378 /// Obtains a new allocation ID that can be referenced but does not
379 /// yet have an allocation backing it.
381 /// Make sure to call `set_alloc_id_memory` or `set_alloc_id_same_memory` before returning such
382 /// an `AllocId` from a query.
383 pub fn reserve(&mut self) -> AllocId {
384 let next = self.next_id;
385 self.next_id.0 = self.next_id.0.checked_add(1).expect(
386 "You overflowed a u64 by incrementing by 1... \
387 You've just earned yourself a free drink if we ever meet. \
388 Seriously, how did you do that?!",
393 /// Reserves a new ID *if* this allocation has not been dedup-reserved before.
394 /// Should only be used for function pointers and statics, we don't want
395 /// to dedup IDs for "real" memory!
396 fn reserve_and_set_dedup(&mut self, alloc: GlobalAlloc<'tcx>) -> AllocId {
398 GlobalAlloc::Function(..) | GlobalAlloc::Static(..) => {}
399 GlobalAlloc::Memory(..) => bug!("Trying to dedup-reserve memory with real data!"),
401 if let Some(&alloc_id) = self.dedup.get(&alloc) {
404 let id = self.reserve();
405 debug!("creating alloc {:?} with id {}", alloc, id);
406 self.alloc_map.insert(id, alloc.clone());
407 self.dedup.insert(alloc, id);
411 /// Generates an `AllocId` for a static or return a cached one in case this function has been
412 /// called on the same static before.
413 pub fn create_static_alloc(&mut self, static_id: DefId) -> AllocId {
414 self.reserve_and_set_dedup(GlobalAlloc::Static(static_id))
417 /// Generates an `AllocId` for a function. Depending on the function type,
418 /// this might get deduplicated or assigned a new ID each time.
419 pub fn create_fn_alloc(&mut self, instance: Instance<'tcx>) -> AllocId {
420 // Functions cannot be identified by pointers, as asm-equal functions can get deduplicated
421 // by the linker (we set the "unnamed_addr" attribute for LLVM) and functions can be
422 // duplicated across crates.
423 // We thus generate a new `AllocId` for every mention of a function. This means that
424 // `main as fn() == main as fn()` is false, while `let x = main as fn(); x == x` is true.
425 // However, formatting code relies on function identity (see #58320), so we only do
426 // this for generic functions. Lifetime parameters are ignored.
427 let is_generic = instance.substs.into_iter().any(|kind| match kind.unpack() {
428 GenericArgKind::Lifetime(_) => false,
433 let id = self.reserve();
434 self.alloc_map.insert(id, GlobalAlloc::Function(instance));
438 self.reserve_and_set_dedup(GlobalAlloc::Function(instance))
442 /// Interns the `Allocation` and return a new `AllocId`, even if there's already an identical
443 /// `Allocation` with a different `AllocId`.
444 /// Statics with identical content will still point to the same `Allocation`, i.e.,
445 /// their data will be deduplicated through `Allocation` interning -- but they
446 /// are different places in memory and as such need different IDs.
447 pub fn create_memory_alloc(&mut self, mem: &'tcx Allocation) -> AllocId {
448 let id = self.reserve();
449 self.set_alloc_id_memory(id, mem);
453 /// Returns `None` in case the `AllocId` is dangling. An `InterpretCx` can still have a
454 /// local `Allocation` for that `AllocId`, but having such an `AllocId` in a constant is
455 /// illegal and will likely ICE.
456 /// This function exists to allow const eval to detect the difference between evaluation-
457 /// local dangling pointers and allocations in constants/statics.
459 pub fn get(&self, id: AllocId) -> Option<GlobalAlloc<'tcx>> {
460 self.alloc_map.get(&id).cloned()
463 /// Panics if the `AllocId` does not refer to an `Allocation`
464 pub fn unwrap_memory(&self, id: AllocId) -> &'tcx Allocation {
466 Some(GlobalAlloc::Memory(mem)) => mem,
467 _ => bug!("expected allocation ID {} to point to memory", id),
471 /// Panics if the `AllocId` does not refer to a function
472 pub fn unwrap_fn(&self, id: AllocId) -> Instance<'tcx> {
474 Some(GlobalAlloc::Function(instance)) => instance,
475 _ => bug!("expected allocation ID {} to point to a function", id),
479 /// Freezes an `AllocId` created with `reserve` by pointing it at an `Allocation`. Trying to
480 /// call this function twice, even with the same `Allocation` will ICE the compiler.
481 pub fn set_alloc_id_memory(&mut self, id: AllocId, mem: &'tcx Allocation) {
482 if let Some(old) = self.alloc_map.insert(id, GlobalAlloc::Memory(mem)) {
483 bug!("tried to set allocation ID {}, but it was already existing as {:#?}", id, old);
487 /// Freezes an `AllocId` created with `reserve` by pointing it at an `Allocation`. May be called
488 /// twice for the same `(AllocId, Allocation)` pair.
489 fn set_alloc_id_same_memory(&mut self, id: AllocId, mem: &'tcx Allocation) {
490 self.alloc_map.insert_same(id, GlobalAlloc::Memory(mem));
494 ////////////////////////////////////////////////////////////////////////////////
495 // Methods to access integers in the target endianness
496 ////////////////////////////////////////////////////////////////////////////////
499 pub fn write_target_uint(
500 endianness: layout::Endian,
501 mut target: &mut [u8],
503 ) -> Result<(), io::Error> {
504 let len = target.len();
506 layout::Endian::Little => target.write_uint128::<LittleEndian>(data, len),
507 layout::Endian::Big => target.write_uint128::<BigEndian>(data, len),
512 pub fn read_target_uint(endianness: layout::Endian, mut source: &[u8]) -> Result<u128, io::Error> {
514 layout::Endian::Little => source.read_uint128::<LittleEndian>(source.len()),
515 layout::Endian::Big => source.read_uint128::<BigEndian>(source.len()),
519 ////////////////////////////////////////////////////////////////////////////////
520 // Methods to facilitate working with signed integers stored in a u128
521 ////////////////////////////////////////////////////////////////////////////////
523 /// Truncates `value` to `size` bits and then sign-extend it to 128 bits
524 /// (i.e., if it is negative, fill with 1's on the left).
526 pub fn sign_extend(value: u128, size: Size) -> u128 {
527 let size = size.bits();
529 // Truncated until nothing is left.
533 let shift = 128 - size;
534 // Shift the unsigned value to the left, then shift back to the right as signed
535 // (essentially fills with FF on the left).
536 (((value << shift) as i128) >> shift) as u128
539 /// Truncates `value` to `size` bits.
541 pub fn truncate(value: u128, size: Size) -> u128 {
542 let size = size.bits();
544 // Truncated until nothing is left.
547 let shift = 128 - size;
548 // Truncate (shift left to drop out leftover values, shift right to fill with zeroes).
549 (value << shift) >> shift