]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/interpret/intern.rs
Don't ICE on mutable zst slices
[rust.git] / src / librustc_mir / interpret / intern.rs
1 //! This module specifies the type based interner for constants.
2 //!
3 //! After a const evaluation has computed a value, before we destroy the const evaluator's session
4 //! memory, we need to extract all memory allocations to the global memory pool so they stay around.
5
6 use rustc::ty::{Ty, TyCtxt, ParamEnv, self};
7 use rustc::mir::interpret::{
8     InterpResult, ErrorHandled,
9 };
10 use rustc::hir;
11 use rustc::hir::def_id::DefId;
12 use super::validity::RefTracking;
13 use rustc_data_structures::fx::FxHashSet;
14
15 use syntax::ast::Mutability;
16 use syntax_pos::Span;
17
18 use super::{
19     ValueVisitor, MemoryKind, Pointer, AllocId, MPlaceTy, InterpError, Scalar,
20 };
21 use crate::const_eval::{CompileTimeInterpreter, CompileTimeEvalContext};
22
23 struct InternVisitor<'rt, 'mir, 'tcx> {
24     /// previously encountered safe references
25     ref_tracking: &'rt mut RefTracking<(MPlaceTy<'tcx>, Mutability, InternMode)>,
26     ecx: &'rt mut CompileTimeEvalContext<'mir, 'tcx>,
27     param_env: ParamEnv<'tcx>,
28     /// The root node of the value that we're looking at. This field is never mutated and only used
29     /// for sanity assertions that will ICE when `const_qualif` screws up.
30     mode: InternMode,
31     /// This field stores the mutability of the value *currently* being checked.
32     /// It is set to mutable when an `UnsafeCell` is encountered
33     /// When recursing across a reference, we don't recurse but store the
34     /// value to be checked in `ref_tracking` together with the mutability at which we are checking
35     /// the value.
36     /// When encountering an immutable reference, we treat everything as immutable that is behind
37     /// it.
38     mutability: Mutability,
39     /// A list of all encountered relocations. After type-based interning, we traverse this list to
40     /// also intern allocations that are only referenced by a raw pointer or inside a union.
41     leftover_relocations: &'rt mut FxHashSet<AllocId>,
42 }
43
44 #[derive(Copy, Clone, Debug, PartialEq, Hash, Eq)]
45 enum InternMode {
46     /// Mutable references must in fact be immutable due to their surrounding immutability in a
47     /// `static`. In a `static mut` we start out as mutable and thus can also contain further `&mut`
48     /// that will actually be treated as mutable.
49     Static,
50     /// UnsafeCell is OK in the value of a constant, but not behind references in a constant
51     ConstBase,
52     /// `UnsafeCell` ICEs
53     Const,
54 }
55
56 /// Signalling data structure to ensure we don't recurse
57 /// into the memory of other constants or statics
58 struct IsStaticOrFn;
59
60 impl<'rt, 'mir, 'tcx> InternVisitor<'rt, 'mir, 'tcx> {
61     /// Intern an allocation without looking at its children
62     fn intern_shallow(
63         &mut self,
64         ptr: Pointer,
65         mutability: Mutability,
66     ) -> InterpResult<'tcx, Option<IsStaticOrFn>> {
67         trace!(
68             "InternVisitor::intern {:?} with {:?}",
69             ptr, mutability,
70         );
71         // remove allocation
72         let tcx = self.ecx.tcx;
73         let memory = self.ecx.memory_mut();
74         let (kind, mut alloc) = match memory.alloc_map.remove(&ptr.alloc_id) {
75             Some(entry) => entry,
76             None => {
77                 // if the pointer is dangling (neither in local nor global memory), we leave it
78                 // to validation to error. The `delay_span_bug` ensures that we don't forget such
79                 // a check in validation.
80                 if tcx.alloc_map.lock().get(ptr.alloc_id).is_none() {
81                     tcx.sess.delay_span_bug(self.ecx.tcx.span, "tried to intern dangling pointer");
82                 }
83                 // treat dangling pointers like other statics
84                 // just to stop trying to recurse into them
85                 return Ok(Some(IsStaticOrFn));
86             },
87         };
88         // This match is just a canary for future changes to `MemoryKind`, which most likely need
89         // changes in this function.
90         match kind {
91             MemoryKind::Stack | MemoryKind::Vtable => {},
92         }
93         // Ensure llvm knows to only put this into immutable memory if the value is immutable either
94         // by being behind a reference or by being part of a static or const without interior
95         // mutability
96         alloc.mutability = mutability;
97         // link the alloc id to the actual allocation
98         let alloc = tcx.intern_const_alloc(alloc);
99         self.leftover_relocations.extend(alloc.relocations.iter().map(|&(_, ((), reloc))| reloc));
100         tcx.alloc_map.lock().set_alloc_id_memory(ptr.alloc_id, alloc);
101         Ok(None)
102     }
103 }
104
105 impl<'rt, 'mir, 'tcx>
106     ValueVisitor<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>>
107 for
108     InternVisitor<'rt, 'mir, 'tcx>
109 {
110     type V = MPlaceTy<'tcx>;
111
112     #[inline(always)]
113     fn ecx(&self) -> &CompileTimeEvalContext<'mir, 'tcx> {
114         &self.ecx
115     }
116
117     fn visit_aggregate(
118         &mut self,
119         mplace: MPlaceTy<'tcx>,
120         fields: impl Iterator<Item=InterpResult<'tcx, Self::V>>,
121     ) -> InterpResult<'tcx> {
122         if let Some(def) = mplace.layout.ty.ty_adt_def() {
123             if Some(def.did) == self.ecx.tcx.lang_items().unsafe_cell_type() {
124                 // We are crossing over an `UnsafeCell`, we can mutate again
125                 let old = std::mem::replace(&mut self.mutability, Mutability::Mutable);
126                 assert_ne!(
127                     self.mode, InternMode::Const,
128                     "UnsafeCells are not allowed behind references in constants. This should have \
129                     been prevented statically by const qualification. If this were allowed one \
130                     would be able to change a constant at one use site and other use sites may \
131                     arbitrarily decide to change, too.",
132                 );
133                 let walked = self.walk_aggregate(mplace, fields);
134                 self.mutability = old;
135                 return walked;
136             }
137         }
138         self.walk_aggregate(mplace, fields)
139     }
140
141     fn visit_primitive(&mut self, mplace: MPlaceTy<'tcx>) -> InterpResult<'tcx> {
142         // Handle Reference types, as these are the only relocations supported by const eval.
143         // Raw pointers (and boxes) are handled by the `leftover_relocations` logic.
144         let ty = mplace.layout.ty;
145         if let ty::Ref(_, referenced_ty, mutability) = ty.sty {
146             let value = self.ecx.read_immediate(mplace.into())?;
147             // Handle trait object vtables
148             if let Ok(meta) = value.to_meta() {
149                 if let ty::Dynamic(..) = self.ecx.tcx.struct_tail(referenced_ty).sty {
150                     if let Ok(vtable) = meta.unwrap().to_ptr() {
151                         // explitly choose `Immutable` here, since vtables are immutable, even
152                         // if the reference of the fat pointer is mutable
153                         self.intern_shallow(vtable, Mutability::Immutable)?;
154                     }
155                 }
156             }
157             let mplace = self.ecx.ref_to_mplace(value)?;
158             // Check if we have encountered this pointer+layout combination before.
159             // Only recurse for allocation-backed pointers.
160             if let Scalar::Ptr(ptr) = mplace.ptr {
161                 // We do not have any `frozen` logic here, because it's essentially equivalent to
162                 // the mutability except for the outermost item. Only `UnsafeCell` can "unfreeze",
163                 // and we check that in `visit_aggregate`.
164                 // This is not an inherent limitation, but one that we know to be true, because
165                 // const qualification enforces it. We can lift it in the future.
166                 match (self.mode, mutability) {
167                     // immutable references are fine everywhere
168                     (_, hir::Mutability::MutImmutable) => {},
169                     // all is "good and well" in the unsoundness of `static mut`
170
171                     // mutable references are ok in `static`. Either they are treated as immutable
172                     // because they are behind an immutable one, or they are behind an `UnsafeCell`
173                     // and thus ok.
174                     (InternMode::Static, hir::Mutability::MutMutable) => {},
175                     // we statically prevent `&mut T` via `const_qualif` and double check this here
176                     (InternMode::ConstBase, hir::Mutability::MutMutable) |
177                     (InternMode::Const, hir::Mutability::MutMutable) => {
178                         match referenced_ty.sty {
179                             ty::Array(_, n) if n.unwrap_usize(self.ecx.tcx.tcx) == 0 => {}
180                             ty::Slice(_)
181                                 if value.to_meta().unwrap().unwrap().to_usize(self.ecx)? == 0 => {}
182                             _ => bug!("const qualif failed to prevent mutable references"),
183                         }
184                     },
185                 }
186                 // Compute the mutability with which we'll start visiting the allocation. This is
187                 // what gets changed when we encounter an `UnsafeCell`
188                 let mutability = match (self.mutability, mutability) {
189                     // The only way a mutable reference actually works as a mutable reference is
190                     // by being in a `static mut` directly or behind another mutable reference.
191                     // If there's an immutable reference or we are inside a static, then our
192                     // mutable reference is equivalent to an immutable one. As an example:
193                     // `&&mut Foo` is semantically equivalent to `&&Foo`
194                     (Mutability::Mutable, hir::Mutability::MutMutable) => Mutability::Mutable,
195                     _ => Mutability::Immutable,
196                 };
197                 // Compute the mutability of the allocation
198                 let intern_mutability = intern_mutability(
199                     self.ecx.tcx.tcx,
200                     self.param_env,
201                     mplace.layout.ty,
202                     self.ecx.tcx.span,
203                     mutability,
204                 );
205                 // Recursing behind references changes the intern mode for constants in order to
206                 // cause assertions to trigger if we encounter any `UnsafeCell`s.
207                 let mode = match self.mode {
208                     InternMode::ConstBase => InternMode::Const,
209                     other => other,
210                 };
211                 match self.intern_shallow(ptr, intern_mutability)? {
212                     // No need to recurse, these are interned already and statics may have
213                     // cycles, so we don't want to recurse there
214                     Some(IsStaticOrFn) => {},
215                     // intern everything referenced by this value. The mutability is taken from the
216                     // reference. It is checked above that mutable references only happen in
217                     // `static mut`
218                     None => self.ref_tracking.track((mplace, mutability, mode), || ()),
219                 }
220             }
221         }
222         Ok(())
223     }
224 }
225
226 /// Figure out the mutability of the allocation.
227 /// Mutable if it has interior mutability *anywhere* in the type.
228 fn intern_mutability<'tcx>(
229     tcx: TyCtxt<'tcx>,
230     param_env: ParamEnv<'tcx>,
231     ty: Ty<'tcx>,
232     span: Span,
233     mutability: Mutability,
234 ) -> Mutability {
235     let has_interior_mutability = !ty.is_freeze(tcx, param_env, span);
236     if has_interior_mutability {
237         Mutability::Mutable
238     } else {
239         mutability
240     }
241 }
242
243 pub fn intern_const_alloc_recursive(
244     ecx: &mut CompileTimeEvalContext<'mir, 'tcx>,
245     def_id: DefId,
246     ret: MPlaceTy<'tcx>,
247     // FIXME(oli-obk): can we scrap the param env? I think we can, the final value of a const eval
248     // must always be monomorphic, right?
249     param_env: ty::ParamEnv<'tcx>,
250 ) -> InterpResult<'tcx> {
251     let tcx = ecx.tcx;
252     // this `mutability` is the mutability of the place, ignoring the type
253     let (mutability, base_intern_mode) = match tcx.static_mutability(def_id) {
254         Some(hir::Mutability::MutImmutable) => (Mutability::Immutable, InternMode::Static),
255         None => (Mutability::Immutable, InternMode::ConstBase),
256         // `static mut` doesn't care about interior mutability, it's mutable anyway
257         Some(hir::Mutability::MutMutable) => (Mutability::Mutable, InternMode::Static),
258     };
259
260     // type based interning
261     let mut ref_tracking = RefTracking::new((ret, mutability, base_intern_mode));
262     let leftover_relocations = &mut FxHashSet::default();
263
264     // This mutability is the combination of the place mutability and the type mutability. If either
265     // is mutable, `alloc_mutability` is mutable. This exists because the entire allocation needs
266     // to be mutable if it contains an `UnsafeCell` anywhere. The other `mutability` exists so that
267     // the visitor does not treat everything outside the `UnsafeCell` as mutable.
268     let alloc_mutability = intern_mutability(
269         tcx.tcx, param_env, ret.layout.ty, tcx.span, mutability,
270     );
271
272     // start with the outermost allocation
273     InternVisitor {
274         ref_tracking: &mut ref_tracking,
275         ecx,
276         mode: base_intern_mode,
277         leftover_relocations,
278         param_env,
279         mutability,
280     }.intern_shallow(ret.ptr.to_ptr()?, alloc_mutability)?;
281
282     while let Some(((mplace, mutability, mode), _)) = ref_tracking.todo.pop() {
283         let interned = InternVisitor {
284             ref_tracking: &mut ref_tracking,
285             ecx,
286             mode,
287             leftover_relocations,
288             param_env,
289             mutability,
290         }.visit_value(mplace);
291         if let Err(error) = interned {
292             // This can happen when e.g. the tag of an enum is not a valid discriminant. We do have
293             // to read enum discriminants in order to find references in enum variant fields.
294             if let InterpError::ValidationFailure(_) = error.kind {
295                 let err = crate::const_eval::error_to_const_error(&ecx, error);
296                 match err.struct_error(ecx.tcx, "it is undefined behavior to use this value") {
297                     Ok(mut diag) => {
298                         diag.note("The rules on what exactly is undefined behavior aren't clear, \
299                             so this check might be overzealous. Please open an issue on the rust \
300                             compiler repository if you believe it should not be considered \
301                             undefined behavior",
302                         );
303                         diag.emit();
304                     }
305                     Err(ErrorHandled::TooGeneric) |
306                     Err(ErrorHandled::Reported) => {},
307                 }
308             }
309         }
310     }
311
312     // Intern the rest of the allocations as mutable. These might be inside unions, padding, raw
313     // pointers, ... So we can't intern them according to their type rules
314
315     let mut todo: Vec<_> = leftover_relocations.iter().cloned().collect();
316     while let Some(alloc_id) = todo.pop() {
317         if let Some((_, alloc)) = ecx.memory_mut().alloc_map.remove(&alloc_id) {
318             // We can't call the `intern` method here, as its logic is tailored to safe references.
319             // So we hand-roll the interning logic here again
320             let alloc = tcx.intern_const_alloc(alloc);
321             tcx.alloc_map.lock().set_alloc_id_memory(alloc_id, alloc);
322             for &(_, ((), reloc)) in alloc.relocations.iter() {
323                 if leftover_relocations.insert(reloc) {
324                     todo.push(reloc);
325                 }
326             }
327         } else if ecx.memory().dead_alloc_map.contains_key(&alloc_id) {
328             // dangling pointer
329             return err!(ValidationFailure(
330                 "encountered dangling pointer in final constant".into(),
331             ))
332         }
333     }
334     Ok(())
335 }