]> git.lizzy.rs Git - rust.git/blob - src/librustc_middle/mir/interpret/mod.rs
pin docs: add some forward references
[rust.git] / src / librustc_middle / mir / interpret / mod.rs
1 //! An interpreter for MIR used in CTFE and by miri.
2
3 #[macro_export]
4 macro_rules! err_unsup {
5     ($($tt:tt)*) => {
6         $crate::mir::interpret::InterpError::Unsupported(
7             $crate::mir::interpret::UnsupportedOpInfo::$($tt)*
8         )
9     };
10 }
11
12 #[macro_export]
13 macro_rules! err_unsup_format {
14     ($($tt:tt)*) => { err_unsup!(Unsupported(format!($($tt)*))) };
15 }
16
17 #[macro_export]
18 macro_rules! err_inval {
19     ($($tt:tt)*) => {
20         $crate::mir::interpret::InterpError::InvalidProgram(
21             $crate::mir::interpret::InvalidProgramInfo::$($tt)*
22         )
23     };
24 }
25
26 #[macro_export]
27 macro_rules! err_ub {
28     ($($tt:tt)*) => {
29         $crate::mir::interpret::InterpError::UndefinedBehavior(
30             $crate::mir::interpret::UndefinedBehaviorInfo::$($tt)*
31         )
32     };
33 }
34
35 #[macro_export]
36 macro_rules! err_ub_format {
37     ($($tt:tt)*) => { err_ub!(Ub(format!($($tt)*))) };
38 }
39
40 #[macro_export]
41 macro_rules! err_exhaust {
42     ($($tt:tt)*) => {
43         $crate::mir::interpret::InterpError::ResourceExhaustion(
44             $crate::mir::interpret::ResourceExhaustionInfo::$($tt)*
45         )
46     };
47 }
48
49 #[macro_export]
50 macro_rules! err_machine_stop {
51     ($($tt:tt)*) => {
52         $crate::mir::interpret::InterpError::MachineStop(Box::new($($tt)*))
53     };
54 }
55
56 // In the `throw_*` macros, avoid `return` to make them work with `try {}`.
57 #[macro_export]
58 macro_rules! throw_unsup {
59     ($($tt:tt)*) => { Err::<!, _>(err_unsup!($($tt)*))? };
60 }
61
62 #[macro_export]
63 macro_rules! throw_unsup_format {
64     ($($tt:tt)*) => { throw_unsup!(Unsupported(format!($($tt)*))) };
65 }
66
67 #[macro_export]
68 macro_rules! throw_inval {
69     ($($tt:tt)*) => { Err::<!, _>(err_inval!($($tt)*))? };
70 }
71
72 #[macro_export]
73 macro_rules! throw_ub {
74     ($($tt:tt)*) => { Err::<!, _>(err_ub!($($tt)*))? };
75 }
76
77 #[macro_export]
78 macro_rules! throw_ub_format {
79     ($($tt:tt)*) => { throw_ub!(Ub(format!($($tt)*))) };
80 }
81
82 #[macro_export]
83 macro_rules! throw_exhaust {
84     ($($tt:tt)*) => { Err::<!, _>(err_exhaust!($($tt)*))? };
85 }
86
87 #[macro_export]
88 macro_rules! throw_machine_stop {
89     ($($tt:tt)*) => { Err::<!, _>(err_machine_stop!($($tt)*))? };
90 }
91
92 mod allocation;
93 mod error;
94 mod pointer;
95 mod queries;
96 mod value;
97
98 use std::convert::TryFrom;
99 use std::fmt;
100 use std::io;
101 use std::num::NonZeroU32;
102 use std::sync::atomic::{AtomicU32, Ordering};
103
104 use byteorder::{BigEndian, LittleEndian, ReadBytesExt, WriteBytesExt};
105 use rustc_ast::ast::LitKind;
106 use rustc_data_structures::fx::FxHashMap;
107 use rustc_data_structures::sync::{HashMapExt, Lock};
108 use rustc_data_structures::tiny_list::TinyList;
109 use rustc_hir::def_id::DefId;
110 use rustc_macros::HashStable;
111 use rustc_serialize::{Decodable, Encodable, Encoder};
112 use rustc_target::abi::{Endian, Size};
113
114 use crate::mir;
115 use crate::ty::codec::TyDecoder;
116 use crate::ty::subst::GenericArgKind;
117 use crate::ty::{self, Instance, Ty, TyCtxt};
118
119 pub use self::error::{
120     struct_error, CheckInAllocMsg, ConstEvalRawResult, ConstEvalResult, ErrorHandled, InterpError,
121     InterpErrorInfo, InterpResult, InvalidProgramInfo, MachineStopType, ResourceExhaustionInfo,
122     UndefinedBehaviorInfo, UninitBytesAccess, UnsupportedOpInfo,
123 };
124
125 pub use self::value::{get_slice_bytes, ConstValue, RawConst, Scalar, ScalarMaybeUninit};
126
127 pub use self::allocation::{Allocation, AllocationExtra, InitMask, Relocations};
128
129 pub use self::pointer::{Pointer, PointerArithmetic};
130
131 /// Uniquely identifies one of the following:
132 /// - A constant
133 /// - A static
134 /// - A const fn where all arguments (if any) are zero-sized types
135 #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, RustcEncodable, RustcDecodable)]
136 #[derive(HashStable, Lift)]
137 pub struct GlobalId<'tcx> {
138     /// For a constant or static, the `Instance` of the item itself.
139     /// For a promoted global, the `Instance` of the function they belong to.
140     pub instance: ty::Instance<'tcx>,
141
142     /// The index for promoted globals within their function's `mir::Body`.
143     pub promoted: Option<mir::Promoted>,
144 }
145
146 /// Input argument for `tcx.lit_to_const`.
147 #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, HashStable)]
148 pub struct LitToConstInput<'tcx> {
149     /// The absolute value of the resultant constant.
150     pub lit: &'tcx LitKind,
151     /// The type of the constant.
152     pub ty: Ty<'tcx>,
153     /// If the constant is negative.
154     pub neg: bool,
155 }
156
157 /// Error type for `tcx.lit_to_const`.
158 #[derive(Copy, Clone, Debug, Eq, PartialEq, HashStable)]
159 pub enum LitToConstError {
160     /// The literal's inferred type did not match the expected `ty` in the input.
161     /// This is used for graceful error handling (`delay_span_bug`) in
162     /// type checking (`Const::from_anon_const`).
163     TypeError,
164     UnparseableFloat,
165     Reported,
166 }
167
168 #[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
169 pub struct AllocId(pub u64);
170
171 // We want the `Debug` output to be readable as it is used by `derive(Debug)` for
172 // all the Miri types.
173 impl fmt::Debug for AllocId {
174     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
175         if f.alternate() { write!(f, "a{}", self.0) } else { write!(f, "alloc{}", self.0) }
176     }
177 }
178
179 impl fmt::Display for AllocId {
180     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
181         fmt::Debug::fmt(self, f)
182     }
183 }
184
185 impl rustc_serialize::UseSpecializedEncodable for AllocId {}
186 impl rustc_serialize::UseSpecializedDecodable for AllocId {}
187
188 #[derive(RustcDecodable, RustcEncodable)]
189 enum AllocDiscriminant {
190     Alloc,
191     Fn,
192     Static,
193 }
194
195 pub fn specialized_encode_alloc_id<'tcx, E: Encoder>(
196     encoder: &mut E,
197     tcx: TyCtxt<'tcx>,
198     alloc_id: AllocId,
199 ) -> Result<(), E::Error> {
200     match tcx.global_alloc(alloc_id) {
201         GlobalAlloc::Memory(alloc) => {
202             trace!("encoding {:?} with {:#?}", alloc_id, alloc);
203             AllocDiscriminant::Alloc.encode(encoder)?;
204             alloc.encode(encoder)?;
205         }
206         GlobalAlloc::Function(fn_instance) => {
207             trace!("encoding {:?} with {:#?}", alloc_id, fn_instance);
208             AllocDiscriminant::Fn.encode(encoder)?;
209             fn_instance.encode(encoder)?;
210         }
211         GlobalAlloc::Static(did) => {
212             assert!(!tcx.is_thread_local_static(did));
213             // References to statics doesn't need to know about their allocations,
214             // just about its `DefId`.
215             AllocDiscriminant::Static.encode(encoder)?;
216             did.encode(encoder)?;
217         }
218     }
219     Ok(())
220 }
221
222 // Used to avoid infinite recursion when decoding cyclic allocations.
223 type DecodingSessionId = NonZeroU32;
224
225 #[derive(Clone)]
226 enum State {
227     Empty,
228     InProgressNonAlloc(TinyList<DecodingSessionId>),
229     InProgress(TinyList<DecodingSessionId>, AllocId),
230     Done(AllocId),
231 }
232
233 pub struct AllocDecodingState {
234     // For each `AllocId`, we keep track of which decoding state it's currently in.
235     decoding_state: Vec<Lock<State>>,
236     // The offsets of each allocation in the data stream.
237     data_offsets: Vec<u32>,
238 }
239
240 impl AllocDecodingState {
241     pub fn new_decoding_session(&self) -> AllocDecodingSession<'_> {
242         static DECODER_SESSION_ID: AtomicU32 = AtomicU32::new(0);
243         let counter = DECODER_SESSION_ID.fetch_add(1, Ordering::SeqCst);
244
245         // Make sure this is never zero.
246         let session_id = DecodingSessionId::new((counter & 0x7FFFFFFF) + 1).unwrap();
247
248         AllocDecodingSession { state: self, session_id }
249     }
250
251     pub fn new(data_offsets: Vec<u32>) -> Self {
252         let decoding_state = vec![Lock::new(State::Empty); data_offsets.len()];
253
254         Self { decoding_state, data_offsets }
255     }
256 }
257
258 #[derive(Copy, Clone)]
259 pub struct AllocDecodingSession<'s> {
260     state: &'s AllocDecodingState,
261     session_id: DecodingSessionId,
262 }
263
264 impl<'s> AllocDecodingSession<'s> {
265     /// Decodes an `AllocId` in a thread-safe way.
266     pub fn decode_alloc_id<D>(&self, decoder: &mut D) -> Result<AllocId, D::Error>
267     where
268         D: TyDecoder<'tcx>,
269     {
270         // Read the index of the allocation.
271         let idx = usize::try_from(decoder.read_u32()?).unwrap();
272         let pos = usize::try_from(self.state.data_offsets[idx]).unwrap();
273
274         // Decode the `AllocDiscriminant` now so that we know if we have to reserve an
275         // `AllocId`.
276         let (alloc_kind, pos) = decoder.with_position(pos, |decoder| {
277             let alloc_kind = AllocDiscriminant::decode(decoder)?;
278             Ok((alloc_kind, decoder.position()))
279         })?;
280
281         // Check the decoding state to see if it's already decoded or if we should
282         // decode it here.
283         let alloc_id = {
284             let mut entry = self.state.decoding_state[idx].lock();
285
286             match *entry {
287                 State::Done(alloc_id) => {
288                     return Ok(alloc_id);
289                 }
290                 ref mut entry @ State::Empty => {
291                     // We are allowed to decode.
292                     match alloc_kind {
293                         AllocDiscriminant::Alloc => {
294                             // If this is an allocation, we need to reserve an
295                             // `AllocId` so we can decode cyclic graphs.
296                             let alloc_id = decoder.tcx().reserve_alloc_id();
297                             *entry =
298                                 State::InProgress(TinyList::new_single(self.session_id), alloc_id);
299                             Some(alloc_id)
300                         }
301                         AllocDiscriminant::Fn | AllocDiscriminant::Static => {
302                             // Fns and statics cannot be cyclic, and their `AllocId`
303                             // is determined later by interning.
304                             *entry =
305                                 State::InProgressNonAlloc(TinyList::new_single(self.session_id));
306                             None
307                         }
308                     }
309                 }
310                 State::InProgressNonAlloc(ref mut sessions) => {
311                     if sessions.contains(&self.session_id) {
312                         bug!("this should be unreachable");
313                     } else {
314                         // Start decoding concurrently.
315                         sessions.insert(self.session_id);
316                         None
317                     }
318                 }
319                 State::InProgress(ref mut sessions, alloc_id) => {
320                     if sessions.contains(&self.session_id) {
321                         // Don't recurse.
322                         return Ok(alloc_id);
323                     } else {
324                         // Start decoding concurrently.
325                         sessions.insert(self.session_id);
326                         Some(alloc_id)
327                     }
328                 }
329             }
330         };
331
332         // Now decode the actual data.
333         let alloc_id = decoder.with_position(pos, |decoder| {
334             match alloc_kind {
335                 AllocDiscriminant::Alloc => {
336                     let alloc = <&'tcx Allocation as Decodable>::decode(decoder)?;
337                     // We already have a reserved `AllocId`.
338                     let alloc_id = alloc_id.unwrap();
339                     trace!("decoded alloc {:?}: {:#?}", alloc_id, alloc);
340                     decoder.tcx().set_alloc_id_same_memory(alloc_id, alloc);
341                     Ok(alloc_id)
342                 }
343                 AllocDiscriminant::Fn => {
344                     assert!(alloc_id.is_none());
345                     trace!("creating fn alloc ID");
346                     let instance = ty::Instance::decode(decoder)?;
347                     trace!("decoded fn alloc instance: {:?}", instance);
348                     let alloc_id = decoder.tcx().create_fn_alloc(instance);
349                     Ok(alloc_id)
350                 }
351                 AllocDiscriminant::Static => {
352                     assert!(alloc_id.is_none());
353                     trace!("creating extern static alloc ID");
354                     let did = DefId::decode(decoder)?;
355                     trace!("decoded static def-ID: {:?}", did);
356                     let alloc_id = decoder.tcx().create_static_alloc(did);
357                     Ok(alloc_id)
358                 }
359             }
360         })?;
361
362         self.state.decoding_state[idx].with_lock(|entry| {
363             *entry = State::Done(alloc_id);
364         });
365
366         Ok(alloc_id)
367     }
368 }
369
370 /// An allocation in the global (tcx-managed) memory can be either a function pointer,
371 /// a static, or a "real" allocation with some data in it.
372 #[derive(Debug, Clone, Eq, PartialEq, Hash, RustcDecodable, RustcEncodable, HashStable)]
373 pub enum GlobalAlloc<'tcx> {
374     /// The alloc ID is used as a function pointer.
375     Function(Instance<'tcx>),
376     /// The alloc ID points to a "lazy" static variable that did not get computed (yet).
377     /// This is also used to break the cycle in recursive statics.
378     Static(DefId),
379     /// The alloc ID points to memory.
380     Memory(&'tcx Allocation),
381 }
382
383 impl GlobalAlloc<'tcx> {
384     /// Panics if the `GlobalAlloc` does not refer to an `GlobalAlloc::Memory`
385     #[track_caller]
386     #[inline]
387     pub fn unwrap_memory(&self) -> &'tcx Allocation {
388         match *self {
389             GlobalAlloc::Memory(mem) => mem,
390             _ => bug!("expected memory, got {:?}", self),
391         }
392     }
393
394     /// Panics if the `GlobalAlloc` is not `GlobalAlloc::Function`
395     #[track_caller]
396     #[inline]
397     pub fn unwrap_fn(&self) -> Instance<'tcx> {
398         match *self {
399             GlobalAlloc::Function(instance) => instance,
400             _ => bug!("expected function, got {:?}", self),
401         }
402     }
403 }
404
405 crate struct AllocMap<'tcx> {
406     /// Maps `AllocId`s to their corresponding allocations.
407     alloc_map: FxHashMap<AllocId, GlobalAlloc<'tcx>>,
408
409     /// Used to ensure that statics and functions only get one associated `AllocId`.
410     /// Should never contain a `GlobalAlloc::Memory`!
411     //
412     // FIXME: Should we just have two separate dedup maps for statics and functions each?
413     dedup: FxHashMap<GlobalAlloc<'tcx>, AllocId>,
414
415     /// The `AllocId` to assign to the next requested ID.
416     /// Always incremented; never gets smaller.
417     next_id: AllocId,
418 }
419
420 impl<'tcx> AllocMap<'tcx> {
421     crate fn new() -> Self {
422         AllocMap { alloc_map: Default::default(), dedup: Default::default(), next_id: AllocId(0) }
423     }
424     fn reserve(&mut self) -> AllocId {
425         let next = self.next_id;
426         self.next_id.0 = self.next_id.0.checked_add(1).expect(
427             "You overflowed a u64 by incrementing by 1... \
428              You've just earned yourself a free drink if we ever meet. \
429              Seriously, how did you do that?!",
430         );
431         next
432     }
433 }
434
435 impl<'tcx> TyCtxt<'tcx> {
436     /// Obtains a new allocation ID that can be referenced but does not
437     /// yet have an allocation backing it.
438     ///
439     /// Make sure to call `set_alloc_id_memory` or `set_alloc_id_same_memory` before returning such
440     /// an `AllocId` from a query.
441     pub fn reserve_alloc_id(&self) -> AllocId {
442         self.alloc_map.lock().reserve()
443     }
444
445     /// Reserves a new ID *if* this allocation has not been dedup-reserved before.
446     /// Should only be used for function pointers and statics, we don't want
447     /// to dedup IDs for "real" memory!
448     fn reserve_and_set_dedup(&self, alloc: GlobalAlloc<'tcx>) -> AllocId {
449         let mut alloc_map = self.alloc_map.lock();
450         match alloc {
451             GlobalAlloc::Function(..) | GlobalAlloc::Static(..) => {}
452             GlobalAlloc::Memory(..) => bug!("Trying to dedup-reserve memory with real data!"),
453         }
454         if let Some(&alloc_id) = alloc_map.dedup.get(&alloc) {
455             return alloc_id;
456         }
457         let id = alloc_map.reserve();
458         debug!("creating alloc {:?} with id {}", alloc, id);
459         alloc_map.alloc_map.insert(id, alloc.clone());
460         alloc_map.dedup.insert(alloc, id);
461         id
462     }
463
464     /// Generates an `AllocId` for a static or return a cached one in case this function has been
465     /// called on the same static before.
466     pub fn create_static_alloc(&self, static_id: DefId) -> AllocId {
467         self.reserve_and_set_dedup(GlobalAlloc::Static(static_id))
468     }
469
470     /// Generates an `AllocId` for a function.  Depending on the function type,
471     /// this might get deduplicated or assigned a new ID each time.
472     pub fn create_fn_alloc(&self, instance: Instance<'tcx>) -> AllocId {
473         // Functions cannot be identified by pointers, as asm-equal functions can get deduplicated
474         // by the linker (we set the "unnamed_addr" attribute for LLVM) and functions can be
475         // duplicated across crates.
476         // We thus generate a new `AllocId` for every mention of a function. This means that
477         // `main as fn() == main as fn()` is false, while `let x = main as fn(); x == x` is true.
478         // However, formatting code relies on function identity (see #58320), so we only do
479         // this for generic functions.  Lifetime parameters are ignored.
480         let is_generic = instance.substs.into_iter().any(|kind| match kind.unpack() {
481             GenericArgKind::Lifetime(_) => false,
482             _ => true,
483         });
484         if is_generic {
485             // Get a fresh ID.
486             let mut alloc_map = self.alloc_map.lock();
487             let id = alloc_map.reserve();
488             alloc_map.alloc_map.insert(id, GlobalAlloc::Function(instance));
489             id
490         } else {
491             // Deduplicate.
492             self.reserve_and_set_dedup(GlobalAlloc::Function(instance))
493         }
494     }
495
496     /// Interns the `Allocation` and return a new `AllocId`, even if there's already an identical
497     /// `Allocation` with a different `AllocId`.
498     /// Statics with identical content will still point to the same `Allocation`, i.e.,
499     /// their data will be deduplicated through `Allocation` interning -- but they
500     /// are different places in memory and as such need different IDs.
501     pub fn create_memory_alloc(&self, mem: &'tcx Allocation) -> AllocId {
502         let id = self.reserve_alloc_id();
503         self.set_alloc_id_memory(id, mem);
504         id
505     }
506
507     /// Returns `None` in case the `AllocId` is dangling. An `InterpretCx` can still have a
508     /// local `Allocation` for that `AllocId`, but having such an `AllocId` in a constant is
509     /// illegal and will likely ICE.
510     /// This function exists to allow const eval to detect the difference between evaluation-
511     /// local dangling pointers and allocations in constants/statics.
512     #[inline]
513     pub fn get_global_alloc(&self, id: AllocId) -> Option<GlobalAlloc<'tcx>> {
514         self.alloc_map.lock().alloc_map.get(&id).cloned()
515     }
516
517     #[inline]
518     #[track_caller]
519     /// Panics in case the `AllocId` is dangling. Since that is impossible for `AllocId`s in
520     /// constants (as all constants must pass interning and validation that check for dangling
521     /// ids), this function is frequently used throughout rustc, but should not be used within
522     /// the miri engine.
523     pub fn global_alloc(&self, id: AllocId) -> GlobalAlloc<'tcx> {
524         match self.get_global_alloc(id) {
525             Some(alloc) => alloc,
526             None => bug!("could not find allocation for {}", id),
527         }
528     }
529
530     /// Freezes an `AllocId` created with `reserve` by pointing it at an `Allocation`. Trying to
531     /// call this function twice, even with the same `Allocation` will ICE the compiler.
532     pub fn set_alloc_id_memory(&self, id: AllocId, mem: &'tcx Allocation) {
533         if let Some(old) = self.alloc_map.lock().alloc_map.insert(id, GlobalAlloc::Memory(mem)) {
534             bug!("tried to set allocation ID {}, but it was already existing as {:#?}", id, old);
535         }
536     }
537
538     /// Freezes an `AllocId` created with `reserve` by pointing it at an `Allocation`. May be called
539     /// twice for the same `(AllocId, Allocation)` pair.
540     fn set_alloc_id_same_memory(&self, id: AllocId, mem: &'tcx Allocation) {
541         self.alloc_map.lock().alloc_map.insert_same(id, GlobalAlloc::Memory(mem));
542     }
543 }
544
545 ////////////////////////////////////////////////////////////////////////////////
546 // Methods to access integers in the target endianness
547 ////////////////////////////////////////////////////////////////////////////////
548
549 #[inline]
550 pub fn write_target_uint(
551     endianness: Endian,
552     mut target: &mut [u8],
553     data: u128,
554 ) -> Result<(), io::Error> {
555     let len = target.len();
556     match endianness {
557         Endian::Little => target.write_uint128::<LittleEndian>(data, len),
558         Endian::Big => target.write_uint128::<BigEndian>(data, len),
559     }
560 }
561
562 #[inline]
563 pub fn read_target_uint(endianness: Endian, mut source: &[u8]) -> Result<u128, io::Error> {
564     match endianness {
565         Endian::Little => source.read_uint128::<LittleEndian>(source.len()),
566         Endian::Big => source.read_uint128::<BigEndian>(source.len()),
567     }
568 }
569
570 ////////////////////////////////////////////////////////////////////////////////
571 // Methods to facilitate working with signed integers stored in a u128
572 ////////////////////////////////////////////////////////////////////////////////
573
574 /// Truncates `value` to `size` bits and then sign-extend it to 128 bits
575 /// (i.e., if it is negative, fill with 1's on the left).
576 #[inline]
577 pub fn sign_extend(value: u128, size: Size) -> u128 {
578     let size = size.bits();
579     if size == 0 {
580         // Truncated until nothing is left.
581         return 0;
582     }
583     // Sign-extend it.
584     let shift = 128 - size;
585     // Shift the unsigned value to the left, then shift back to the right as signed
586     // (essentially fills with FF on the left).
587     (((value << shift) as i128) >> shift) as u128
588 }
589
590 /// Truncates `value` to `size` bits.
591 #[inline]
592 pub fn truncate(value: u128, size: Size) -> u128 {
593     let size = size.bits();
594     if size == 0 {
595         // Truncated until nothing is left.
596         return 0;
597     }
598     let shift = 128 - size;
599     // Truncate (shift left to drop out leftover values, shift right to fill with zeroes).
600     (value << shift) >> shift
601 }
602
603 /// Computes the unsigned absolute value without wrapping or panicking.
604 #[inline]
605 pub fn uabs(value: i64) -> u64 {
606     // The only tricky part here is if value == i64::MIN. In that case,
607     // wrapping_abs() returns i64::MIN == -2^63. Casting this value to a u64
608     // gives 2^63, the correct value.
609     value.wrapping_abs() as u64
610 }