]> git.lizzy.rs Git - rust.git/blob - src/librustc/mir/interpret/mod.rs
964d6c01d74ad282e39b20e92d55b0c4f140328b
[rust.git] / src / librustc / mir / interpret / mod.rs
1 //! An interpreter for MIR used in CTFE and by miri
2
3 #[macro_export]
4 macro_rules! err {
5     ($($tt:tt)*) => { Err($crate::mir::interpret::InterpError::$($tt)*.into()) };
6 }
7
8 mod error;
9 mod value;
10 mod allocation;
11 mod pointer;
12
13 pub use self::error::{
14     EvalError, EvalResult, InterpError, AssertMessage, ConstEvalErr, struct_error,
15     FrameInfo, ConstEvalRawResult, ConstEvalResult, ErrorHandled,
16 };
17
18 pub use self::value::{Scalar, ScalarMaybeUndef, RawConst, ConstValue};
19
20 pub use self::allocation::{
21     InboundsCheck, Allocation, AllocationExtra,
22     Relocations, UndefMask, CheckInAllocMsg,
23 };
24
25 pub use self::pointer::{Pointer, PointerArithmetic};
26
27 use std::fmt;
28 use crate::mir;
29 use crate::hir::def_id::DefId;
30 use crate::ty::{self, TyCtxt, Instance, subst::UnpackedKind};
31 use crate::ty::layout::{self, Size};
32 use std::io;
33 use crate::rustc_serialize::{Encoder, Decodable, Encodable};
34 use rustc_data_structures::fx::FxHashMap;
35 use rustc_data_structures::sync::{Lock as Mutex, HashMapExt};
36 use rustc_data_structures::tiny_list::TinyList;
37 use rustc_macros::HashStable;
38 use byteorder::{WriteBytesExt, ReadBytesExt, LittleEndian, BigEndian};
39 use crate::ty::codec::TyDecoder;
40 use std::sync::atomic::{AtomicU32, Ordering};
41 use std::num::NonZeroU32;
42
43 /// Uniquely identifies a specific constant or static.
44 #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, RustcEncodable, RustcDecodable, HashStable)]
45 pub struct GlobalId<'tcx> {
46     /// For a constant or static, the `Instance` of the item itself.
47     /// For a promoted global, the `Instance` of the function they belong to.
48     pub instance: ty::Instance<'tcx>,
49
50     /// The index for promoted globals within their function's `mir::Body`.
51     pub promoted: Option<mir::Promoted>,
52 }
53
54 #[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd, Debug)]
55 pub struct AllocId(pub u64);
56
57 impl crate::rustc_serialize::UseSpecializedEncodable for AllocId {}
58 impl crate::rustc_serialize::UseSpecializedDecodable for AllocId {}
59
60 #[derive(RustcDecodable, RustcEncodable)]
61 enum AllocDiscriminant {
62     Alloc,
63     Fn,
64     Static,
65 }
66
67 pub fn specialized_encode_alloc_id<
68     'a, 'tcx,
69     E: Encoder,
70 >(
71     encoder: &mut E,
72     tcx: TyCtxt<'a, 'tcx, 'tcx>,
73     alloc_id: AllocId,
74 ) -> Result<(), E::Error> {
75     let alloc: GlobalAlloc<'tcx> =
76         tcx.alloc_map.lock().get(alloc_id).expect("no value for AllocId");
77     match alloc {
78         GlobalAlloc::Memory(alloc) => {
79             trace!("encoding {:?} with {:#?}", alloc_id, alloc);
80             AllocDiscriminant::Alloc.encode(encoder)?;
81             alloc.encode(encoder)?;
82         }
83         GlobalAlloc::Function(fn_instance) => {
84             trace!("encoding {:?} with {:#?}", alloc_id, fn_instance);
85             AllocDiscriminant::Fn.encode(encoder)?;
86             fn_instance.encode(encoder)?;
87         }
88         GlobalAlloc::Static(did) => {
89             // referring to statics doesn't need to know about their allocations,
90             // just about its DefId
91             AllocDiscriminant::Static.encode(encoder)?;
92             did.encode(encoder)?;
93         }
94     }
95     Ok(())
96 }
97
98 // Used to avoid infinite recursion when decoding cyclic allocations.
99 type DecodingSessionId = NonZeroU32;
100
101 #[derive(Clone)]
102 enum State {
103     Empty,
104     InProgressNonAlloc(TinyList<DecodingSessionId>),
105     InProgress(TinyList<DecodingSessionId>, AllocId),
106     Done(AllocId),
107 }
108
109 pub struct AllocDecodingState {
110     // For each AllocId we keep track of which decoding state it's currently in.
111     decoding_state: Vec<Mutex<State>>,
112     // The offsets of each allocation in the data stream.
113     data_offsets: Vec<u32>,
114 }
115
116 impl AllocDecodingState {
117
118     pub fn new_decoding_session(&self) -> AllocDecodingSession<'_> {
119         static DECODER_SESSION_ID: AtomicU32 = AtomicU32::new(0);
120         let counter = DECODER_SESSION_ID.fetch_add(1, Ordering::SeqCst);
121
122         // Make sure this is never zero
123         let session_id = DecodingSessionId::new((counter & 0x7FFFFFFF) + 1).unwrap();
124
125         AllocDecodingSession {
126             state: self,
127             session_id,
128         }
129     }
130
131     pub fn new(data_offsets: Vec<u32>) -> AllocDecodingState {
132         let decoding_state = vec![Mutex::new(State::Empty); data_offsets.len()];
133
134         AllocDecodingState {
135             decoding_state,
136             data_offsets,
137         }
138     }
139 }
140
141 #[derive(Copy, Clone)]
142 pub struct AllocDecodingSession<'s> {
143     state: &'s AllocDecodingState,
144     session_id: DecodingSessionId,
145 }
146
147 impl<'s> AllocDecodingSession<'s> {
148
149     // Decodes an AllocId in a thread-safe way.
150     pub fn decode_alloc_id<'a, 'tcx, D>(&self,
151                                         decoder: &mut D)
152                                         -> Result<AllocId, D::Error>
153         where D: TyDecoder<'a, 'tcx>,
154               'tcx: 'a,
155     {
156         // Read the index of the allocation
157         let idx = decoder.read_u32()? as usize;
158         let pos = self.state.data_offsets[idx] as usize;
159
160         // Decode the AllocDiscriminant now so that we know if we have to reserve an
161         // AllocId.
162         let (alloc_kind, pos) = decoder.with_position(pos, |decoder| {
163             let alloc_kind = AllocDiscriminant::decode(decoder)?;
164             Ok((alloc_kind, decoder.position()))
165         })?;
166
167         // Check the decoding state, see if it's already decoded or if we should
168         // decode it here.
169         let alloc_id = {
170             let mut entry = self.state.decoding_state[idx].lock();
171
172             match *entry {
173                 State::Done(alloc_id) => {
174                     return Ok(alloc_id);
175                 }
176                 ref mut entry @ State::Empty => {
177                     // We are allowed to decode
178                     match alloc_kind {
179                         AllocDiscriminant::Alloc => {
180                             // If this is an allocation, we need to reserve an
181                             // AllocId so we can decode cyclic graphs.
182                             let alloc_id = decoder.tcx().alloc_map.lock().reserve();
183                             *entry = State::InProgress(
184                                 TinyList::new_single(self.session_id),
185                                 alloc_id);
186                             Some(alloc_id)
187                         },
188                         AllocDiscriminant::Fn | AllocDiscriminant::Static => {
189                             // Fns and statics cannot be cyclic and their AllocId
190                             // is determined later by interning
191                             *entry = State::InProgressNonAlloc(
192                                 TinyList::new_single(self.session_id));
193                             None
194                         }
195                     }
196                 }
197                 State::InProgressNonAlloc(ref mut sessions) => {
198                     if sessions.contains(&self.session_id) {
199                         bug!("This should be unreachable")
200                     } else {
201                         // Start decoding concurrently
202                         sessions.insert(self.session_id);
203                         None
204                     }
205                 }
206                 State::InProgress(ref mut sessions, alloc_id) => {
207                     if sessions.contains(&self.session_id) {
208                         // Don't recurse.
209                         return Ok(alloc_id)
210                     } else {
211                         // Start decoding concurrently
212                         sessions.insert(self.session_id);
213                         Some(alloc_id)
214                     }
215                 }
216             }
217         };
218
219         // Now decode the actual data
220         let alloc_id = decoder.with_position(pos, |decoder| {
221             match alloc_kind {
222                 AllocDiscriminant::Alloc => {
223                     let allocation = <&'tcx Allocation as Decodable>::decode(decoder)?;
224                     // We already have a reserved AllocId.
225                     let alloc_id = alloc_id.unwrap();
226                     trace!("decoded alloc {:?} {:#?}", alloc_id, allocation);
227                     decoder.tcx().alloc_map.lock().set_alloc_id_same_memory(alloc_id, allocation);
228                     Ok(alloc_id)
229                 },
230                 AllocDiscriminant::Fn => {
231                     assert!(alloc_id.is_none());
232                     trace!("creating fn alloc id");
233                     let instance = ty::Instance::decode(decoder)?;
234                     trace!("decoded fn alloc instance: {:?}", instance);
235                     let alloc_id = decoder.tcx().alloc_map.lock().create_fn_alloc(instance);
236                     Ok(alloc_id)
237                 },
238                 AllocDiscriminant::Static => {
239                     assert!(alloc_id.is_none());
240                     trace!("creating extern static alloc id at");
241                     let did = DefId::decode(decoder)?;
242                     let alloc_id = decoder.tcx().alloc_map.lock().create_static_alloc(did);
243                     Ok(alloc_id)
244                 }
245             }
246         })?;
247
248         self.state.decoding_state[idx].with_lock(|entry| {
249             *entry = State::Done(alloc_id);
250         });
251
252         Ok(alloc_id)
253     }
254 }
255
256 impl fmt::Display for AllocId {
257     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
258         write!(f, "{}", self.0)
259     }
260 }
261
262 /// An allocation in the global (tcx-managed) memory can be either a function pointer,
263 /// a static, or a "real" allocation with some data in it.
264 #[derive(Debug, Clone, Eq, PartialEq, Hash, RustcDecodable, RustcEncodable, HashStable)]
265 pub enum GlobalAlloc<'tcx> {
266     /// The alloc ID is used as a function pointer
267     Function(Instance<'tcx>),
268     /// The alloc ID points to a "lazy" static variable that did not get computed (yet).
269     /// This is also used to break the cycle in recursive statics.
270     Static(DefId),
271     /// The alloc ID points to memory.
272     Memory(&'tcx Allocation),
273 }
274
275 pub struct AllocMap<'tcx> {
276     /// Lets you know what an `AllocId` refers to.
277     alloc_map: FxHashMap<AllocId, GlobalAlloc<'tcx>>,
278
279     /// Used to ensure that statics and functions only get one associated `AllocId`.
280     /// Should never contain a `GlobalAlloc::Memory`!
281     /// FIXME: Should we just have two separate dedup maps for statics and functions each?
282     dedup: FxHashMap<GlobalAlloc<'tcx>, AllocId>,
283
284     /// The `AllocId` to assign to the next requested ID.
285     /// Always incremented, never gets smaller.
286     next_id: AllocId,
287 }
288
289 impl<'tcx> AllocMap<'tcx> {
290     pub fn new() -> Self {
291         AllocMap {
292             alloc_map: Default::default(),
293             dedup: Default::default(),
294             next_id: AllocId(0),
295         }
296     }
297
298     /// Obtains a new allocation ID that can be referenced but does not
299     /// yet have an allocation backing it.
300     ///
301     /// Make sure to call `set_alloc_id_memory` or `set_alloc_id_same_memory` before returning such
302     /// an `AllocId` from a query.
303     pub fn reserve(
304         &mut self,
305     ) -> AllocId {
306         let next = self.next_id;
307         self.next_id.0 = self.next_id.0
308             .checked_add(1)
309             .expect("You overflowed a u64 by incrementing by 1... \
310                      You've just earned yourself a free drink if we ever meet. \
311                      Seriously, how did you do that?!");
312         next
313     }
314
315     /// Reserve a new ID *if* this allocation has not been dedup-reserved before.
316     /// Should only be used for function pointers and statics, we don't want
317     /// to dedup IDs for "real" memory!
318     fn reserve_and_set_dedup(&mut self, alloc: GlobalAlloc<'tcx>) -> AllocId {
319         match alloc {
320             GlobalAlloc::Function(..) | GlobalAlloc::Static(..) => {},
321             GlobalAlloc::Memory(..) => bug!("Trying to dedup-reserve memory with real data!"),
322         }
323         if let Some(&alloc_id) = self.dedup.get(&alloc) {
324             return alloc_id;
325         }
326         let id = self.reserve();
327         debug!("creating alloc {:?} with id {}", alloc, id);
328         self.alloc_map.insert(id, alloc.clone());
329         self.dedup.insert(alloc, id);
330         id
331     }
332
333     /// Generates an `AllocId` for a static or return a cached one in case this function has been
334     /// called on the same static before.
335     pub fn create_static_alloc(&mut self, static_id: DefId) -> AllocId {
336         self.reserve_and_set_dedup(GlobalAlloc::Static(static_id))
337     }
338
339     /// Generates an `AllocId` for a function.  Depending on the function type,
340     /// this might get deduplicated or assigned a new ID each time.
341     pub fn create_fn_alloc(&mut self, instance: Instance<'tcx>) -> AllocId {
342         // Functions cannot be identified by pointers, as asm-equal functions can get deduplicated
343         // by the linker (we set the "unnamed_addr" attribute for LLVM) and functions can be
344         // duplicated across crates.
345         // We thus generate a new `AllocId` for every mention of a function. This means that
346         // `main as fn() == main as fn()` is false, while `let x = main as fn(); x == x` is true.
347         // However, formatting code relies on function identity (see #58320), so we only do
348         // this for generic functions.  Lifetime parameters are ignored.
349         let is_generic = instance.substs.into_iter().any(|kind| {
350             match kind.unpack() {
351                 UnpackedKind::Lifetime(_) => false,
352                 _ => true,
353             }
354         });
355         if is_generic {
356             // Get a fresh ID
357             let id = self.reserve();
358             self.alloc_map.insert(id, GlobalAlloc::Function(instance));
359             id
360         } else {
361             // Deduplicate
362             self.reserve_and_set_dedup(GlobalAlloc::Function(instance))
363         }
364     }
365
366     /// Intern the `Allocation` and return a new `AllocId`, even if there's already an identical
367     /// `Allocation` with a different `AllocId`.
368     /// Statics with identical content will still point to the same `Allocation`, i.e.,
369     /// their data will be deduplicated through `Allocation` interning -- but they
370     /// are different places in memory and as such need different IDs.
371     pub fn create_memory_alloc(&mut self, mem: &'tcx Allocation) -> AllocId {
372         let id = self.reserve();
373         self.set_alloc_id_memory(id, mem);
374         id
375     }
376
377     /// Returns `None` in case the `AllocId` is dangling. An `InterpretCx` can still have a
378     /// local `Allocation` for that `AllocId`, but having such an `AllocId` in a constant is
379     /// illegal and will likely ICE.
380     /// This function exists to allow const eval to detect the difference between evaluation-
381     /// local dangling pointers and allocations in constants/statics.
382     #[inline]
383     pub fn get(&self, id: AllocId) -> Option<GlobalAlloc<'tcx>> {
384         self.alloc_map.get(&id).cloned()
385     }
386
387     /// Panics if the `AllocId` does not refer to an `Allocation`
388     pub fn unwrap_memory(&self, id: AllocId) -> &'tcx Allocation {
389         match self.get(id) {
390             Some(GlobalAlloc::Memory(mem)) => mem,
391             _ => bug!("expected allocation id {} to point to memory", id),
392         }
393     }
394
395     /// Freeze an `AllocId` created with `reserve` by pointing it at an `Allocation`. Trying to
396     /// call this function twice, even with the same `Allocation` will ICE the compiler.
397     pub fn set_alloc_id_memory(&mut self, id: AllocId, mem: &'tcx Allocation) {
398         if let Some(old) = self.alloc_map.insert(id, GlobalAlloc::Memory(mem)) {
399             bug!("tried to set allocation id {}, but it was already existing as {:#?}", id, old);
400         }
401     }
402
403     /// Freeze an `AllocId` created with `reserve` by pointing it at an `Allocation`. May be called
404     /// twice for the same `(AllocId, Allocation)` pair.
405     fn set_alloc_id_same_memory(&mut self, id: AllocId, mem: &'tcx Allocation) {
406         self.alloc_map.insert_same(id, GlobalAlloc::Memory(mem));
407     }
408 }
409
410 ////////////////////////////////////////////////////////////////////////////////
411 // Methods to access integers in the target endianness
412 ////////////////////////////////////////////////////////////////////////////////
413
414 #[inline]
415 pub fn write_target_uint(
416     endianness: layout::Endian,
417     mut target: &mut [u8],
418     data: u128,
419 ) -> Result<(), io::Error> {
420     let len = target.len();
421     match endianness {
422         layout::Endian::Little => target.write_uint128::<LittleEndian>(data, len),
423         layout::Endian::Big => target.write_uint128::<BigEndian>(data, len),
424     }
425 }
426
427 #[inline]
428 pub fn read_target_uint(endianness: layout::Endian, mut source: &[u8]) -> Result<u128, io::Error> {
429     match endianness {
430         layout::Endian::Little => source.read_uint128::<LittleEndian>(source.len()),
431         layout::Endian::Big => source.read_uint128::<BigEndian>(source.len()),
432     }
433 }
434
435 ////////////////////////////////////////////////////////////////////////////////
436 // Methods to facilitate working with signed integers stored in a u128
437 ////////////////////////////////////////////////////////////////////////////////
438
439 /// Truncate `value` to `size` bits and then sign-extend it to 128 bits
440 /// (i.e., if it is negative, fill with 1's on the left).
441 #[inline]
442 pub fn sign_extend(value: u128, size: Size) -> u128 {
443     let size = size.bits();
444     if size == 0 {
445         // Truncated until nothing is left.
446         return 0;
447     }
448     // sign extend
449     let shift = 128 - size;
450     // shift the unsigned value to the left
451     // and back to the right as signed (essentially fills with FF on the left)
452     (((value << shift) as i128) >> shift) as u128
453 }
454
455 /// Truncate `value` to `size` bits.
456 #[inline]
457 pub fn truncate(value: u128, size: Size) -> u128 {
458     let size = size.bits();
459     if size == 0 {
460         // Truncated until nothing is left.
461         return 0;
462     }
463     let shift = 128 - size;
464     // truncate (shift left to drop out leftover values, shift right to fill with zeroes)
465     (value << shift) >> shift
466 }