]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_const_eval/src/interpret/operand.rs
Rollup merge of #103439 - Nilstrieb:help-me-with-my-macro, r=estebank
[rust.git] / compiler / rustc_const_eval / src / interpret / operand.rs
1 //! Functions concerning immediate values and operands, and reading from operands.
2 //! All high-level functions to read from memory work on operands as sources.
3
4 use rustc_hir::def::Namespace;
5 use rustc_middle::ty::layout::{LayoutOf, PrimitiveExt, TyAndLayout};
6 use rustc_middle::ty::print::{FmtPrinter, PrettyPrinter};
7 use rustc_middle::ty::{ConstInt, Ty};
8 use rustc_middle::{mir, ty};
9 use rustc_target::abi::{self, Abi, Align, HasDataLayout, Size, TagEncoding};
10 use rustc_target::abi::{VariantIdx, Variants};
11
12 use super::{
13     alloc_range, from_known_layout, mir_assign_valid_types, AllocId, ConstValue, Frame, GlobalId,
14     InterpCx, InterpResult, MPlaceTy, Machine, MemPlace, MemPlaceMeta, Place, PlaceTy, Pointer,
15     Provenance, Scalar,
16 };
17
18 /// An `Immediate` represents a single immediate self-contained Rust value.
19 ///
20 /// For optimization of a few very common cases, there is also a representation for a pair of
21 /// primitive values (`ScalarPair`). It allows Miri to avoid making allocations for checked binary
22 /// operations and wide pointers. This idea was taken from rustc's codegen.
23 /// In particular, thanks to `ScalarPair`, arithmetic operations and casts can be entirely
24 /// defined on `Immediate`, and do not have to work with a `Place`.
25 #[derive(Copy, Clone, Debug)]
26 pub enum Immediate<Prov: Provenance = AllocId> {
27     /// A single scalar value (must have *initialized* `Scalar` ABI).
28     Scalar(Scalar<Prov>),
29     /// A pair of two scalar value (must have `ScalarPair` ABI where both fields are
30     /// `Scalar::Initialized`).
31     ScalarPair(Scalar<Prov>, Scalar<Prov>),
32     /// A value of fully uninitialized memory. Can have and size and layout.
33     Uninit,
34 }
35
36 impl<Prov: Provenance> From<Scalar<Prov>> for Immediate<Prov> {
37     #[inline(always)]
38     fn from(val: Scalar<Prov>) -> Self {
39         Immediate::Scalar(val.into())
40     }
41 }
42
43 impl<Prov: Provenance> Immediate<Prov> {
44     pub fn from_pointer(p: Pointer<Prov>, cx: &impl HasDataLayout) -> Self {
45         Immediate::Scalar(Scalar::from_pointer(p, cx))
46     }
47
48     pub fn from_maybe_pointer(p: Pointer<Option<Prov>>, cx: &impl HasDataLayout) -> Self {
49         Immediate::Scalar(Scalar::from_maybe_pointer(p, cx))
50     }
51
52     pub fn new_slice(val: Scalar<Prov>, len: u64, cx: &impl HasDataLayout) -> Self {
53         Immediate::ScalarPair(val.into(), Scalar::from_machine_usize(len, cx).into())
54     }
55
56     pub fn new_dyn_trait(
57         val: Scalar<Prov>,
58         vtable: Pointer<Option<Prov>>,
59         cx: &impl HasDataLayout,
60     ) -> Self {
61         Immediate::ScalarPair(val.into(), Scalar::from_maybe_pointer(vtable, cx))
62     }
63
64     #[inline]
65     #[cfg_attr(debug_assertions, track_caller)] // only in debug builds due to perf (see #98980)
66     pub fn to_scalar(self) -> Scalar<Prov> {
67         match self {
68             Immediate::Scalar(val) => val,
69             Immediate::ScalarPair(..) => bug!("Got a scalar pair where a scalar was expected"),
70             Immediate::Uninit => bug!("Got uninit where a scalar was expected"),
71         }
72     }
73
74     #[inline]
75     #[cfg_attr(debug_assertions, track_caller)] // only in debug builds due to perf (see #98980)
76     pub fn to_scalar_pair(self) -> (Scalar<Prov>, Scalar<Prov>) {
77         match self {
78             Immediate::ScalarPair(val1, val2) => (val1, val2),
79             Immediate::Scalar(..) => bug!("Got a scalar where a scalar pair was expected"),
80             Immediate::Uninit => bug!("Got uninit where a scalar pair was expected"),
81         }
82     }
83 }
84
85 // ScalarPair needs a type to interpret, so we often have an immediate and a type together
86 // as input for binary and cast operations.
87 #[derive(Clone, Debug)]
88 pub struct ImmTy<'tcx, Prov: Provenance = AllocId> {
89     imm: Immediate<Prov>,
90     pub layout: TyAndLayout<'tcx>,
91 }
92
93 impl<Prov: Provenance> std::fmt::Display for ImmTy<'_, Prov> {
94     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
95         /// Helper function for printing a scalar to a FmtPrinter
96         fn p<'a, 'tcx, Prov: Provenance>(
97             cx: FmtPrinter<'a, 'tcx>,
98             s: Scalar<Prov>,
99             ty: Ty<'tcx>,
100         ) -> Result<FmtPrinter<'a, 'tcx>, std::fmt::Error> {
101             match s {
102                 Scalar::Int(int) => cx.pretty_print_const_scalar_int(int, ty, true),
103                 Scalar::Ptr(ptr, _sz) => {
104                     // Just print the ptr value. `pretty_print_const_scalar_ptr` would also try to
105                     // print what is points to, which would fail since it has no access to the local
106                     // memory.
107                     cx.pretty_print_const_pointer(ptr, ty, true)
108                 }
109             }
110         }
111         ty::tls::with(|tcx| {
112             match self.imm {
113                 Immediate::Scalar(s) => {
114                     if let Some(ty) = tcx.lift(self.layout.ty) {
115                         let cx = FmtPrinter::new(tcx, Namespace::ValueNS);
116                         f.write_str(&p(cx, s, ty)?.into_buffer())?;
117                         return Ok(());
118                     }
119                     write!(f, "{:x}: {}", s, self.layout.ty)
120                 }
121                 Immediate::ScalarPair(a, b) => {
122                     // FIXME(oli-obk): at least print tuples and slices nicely
123                     write!(f, "({:x}, {:x}): {}", a, b, self.layout.ty)
124                 }
125                 Immediate::Uninit => {
126                     write!(f, "uninit: {}", self.layout.ty)
127                 }
128             }
129         })
130     }
131 }
132
133 impl<'tcx, Prov: Provenance> std::ops::Deref for ImmTy<'tcx, Prov> {
134     type Target = Immediate<Prov>;
135     #[inline(always)]
136     fn deref(&self) -> &Immediate<Prov> {
137         &self.imm
138     }
139 }
140
141 /// An `Operand` is the result of computing a `mir::Operand`. It can be immediate,
142 /// or still in memory. The latter is an optimization, to delay reading that chunk of
143 /// memory and to avoid having to store arbitrary-sized data here.
144 #[derive(Copy, Clone, Debug)]
145 pub enum Operand<Prov: Provenance = AllocId> {
146     Immediate(Immediate<Prov>),
147     Indirect(MemPlace<Prov>),
148 }
149
150 #[derive(Clone, Debug)]
151 pub struct OpTy<'tcx, Prov: Provenance = AllocId> {
152     op: Operand<Prov>, // Keep this private; it helps enforce invariants.
153     pub layout: TyAndLayout<'tcx>,
154     /// rustc does not have a proper way to represent the type of a field of a `repr(packed)` struct:
155     /// it needs to have a different alignment than the field type would usually have.
156     /// So we represent this here with a separate field that "overwrites" `layout.align`.
157     /// This means `layout.align` should never be used for an `OpTy`!
158     /// `None` means "alignment does not matter since this is a by-value operand"
159     /// (`Operand::Immediate`); this field is only relevant for `Operand::Indirect`.
160     /// Also CTFE ignores alignment anyway, so this is for Miri only.
161     pub align: Option<Align>,
162 }
163
164 impl<'tcx, Prov: Provenance> std::ops::Deref for OpTy<'tcx, Prov> {
165     type Target = Operand<Prov>;
166     #[inline(always)]
167     fn deref(&self) -> &Operand<Prov> {
168         &self.op
169     }
170 }
171
172 impl<'tcx, Prov: Provenance> From<MPlaceTy<'tcx, Prov>> for OpTy<'tcx, Prov> {
173     #[inline(always)]
174     fn from(mplace: MPlaceTy<'tcx, Prov>) -> Self {
175         OpTy { op: Operand::Indirect(*mplace), layout: mplace.layout, align: Some(mplace.align) }
176     }
177 }
178
179 impl<'tcx, Prov: Provenance> From<&'_ MPlaceTy<'tcx, Prov>> for OpTy<'tcx, Prov> {
180     #[inline(always)]
181     fn from(mplace: &MPlaceTy<'tcx, Prov>) -> Self {
182         OpTy { op: Operand::Indirect(**mplace), layout: mplace.layout, align: Some(mplace.align) }
183     }
184 }
185
186 impl<'tcx, Prov: Provenance> From<&'_ mut MPlaceTy<'tcx, Prov>> for OpTy<'tcx, Prov> {
187     #[inline(always)]
188     fn from(mplace: &mut MPlaceTy<'tcx, Prov>) -> Self {
189         OpTy { op: Operand::Indirect(**mplace), layout: mplace.layout, align: Some(mplace.align) }
190     }
191 }
192
193 impl<'tcx, Prov: Provenance> From<ImmTy<'tcx, Prov>> for OpTy<'tcx, Prov> {
194     #[inline(always)]
195     fn from(val: ImmTy<'tcx, Prov>) -> Self {
196         OpTy { op: Operand::Immediate(val.imm), layout: val.layout, align: None }
197     }
198 }
199
200 impl<'tcx, Prov: Provenance> ImmTy<'tcx, Prov> {
201     #[inline]
202     pub fn from_scalar(val: Scalar<Prov>, layout: TyAndLayout<'tcx>) -> Self {
203         ImmTy { imm: val.into(), layout }
204     }
205
206     #[inline]
207     pub fn from_immediate(imm: Immediate<Prov>, layout: TyAndLayout<'tcx>) -> Self {
208         ImmTy { imm, layout }
209     }
210
211     #[inline]
212     pub fn uninit(layout: TyAndLayout<'tcx>) -> Self {
213         ImmTy { imm: Immediate::Uninit, layout }
214     }
215
216     #[inline]
217     pub fn try_from_uint(i: impl Into<u128>, layout: TyAndLayout<'tcx>) -> Option<Self> {
218         Some(Self::from_scalar(Scalar::try_from_uint(i, layout.size)?, layout))
219     }
220     #[inline]
221     pub fn from_uint(i: impl Into<u128>, layout: TyAndLayout<'tcx>) -> Self {
222         Self::from_scalar(Scalar::from_uint(i, layout.size), layout)
223     }
224
225     #[inline]
226     pub fn try_from_int(i: impl Into<i128>, layout: TyAndLayout<'tcx>) -> Option<Self> {
227         Some(Self::from_scalar(Scalar::try_from_int(i, layout.size)?, layout))
228     }
229
230     #[inline]
231     pub fn from_int(i: impl Into<i128>, layout: TyAndLayout<'tcx>) -> Self {
232         Self::from_scalar(Scalar::from_int(i, layout.size), layout)
233     }
234
235     #[inline]
236     pub fn to_const_int(self) -> ConstInt {
237         assert!(self.layout.ty.is_integral());
238         let int = self.to_scalar().assert_int();
239         ConstInt::new(int, self.layout.ty.is_signed(), self.layout.ty.is_ptr_sized_integral())
240     }
241 }
242
243 impl<'tcx, Prov: Provenance> OpTy<'tcx, Prov> {
244     pub fn len(&self, cx: &impl HasDataLayout) -> InterpResult<'tcx, u64> {
245         if self.layout.is_unsized() {
246             // There are no unsized immediates.
247             self.assert_mem_place().len(cx)
248         } else {
249             match self.layout.fields {
250                 abi::FieldsShape::Array { count, .. } => Ok(count),
251                 _ => bug!("len not supported on sized type {:?}", self.layout.ty),
252             }
253         }
254     }
255
256     pub fn offset_with_meta(
257         &self,
258         offset: Size,
259         meta: MemPlaceMeta<Prov>,
260         layout: TyAndLayout<'tcx>,
261         cx: &impl HasDataLayout,
262     ) -> InterpResult<'tcx, Self> {
263         match self.try_as_mplace() {
264             Ok(mplace) => Ok(mplace.offset_with_meta(offset, meta, layout, cx)?.into()),
265             Err(imm) => {
266                 assert!(
267                     matches!(*imm, Immediate::Uninit),
268                     "Scalar/ScalarPair cannot be offset into"
269                 );
270                 assert!(!meta.has_meta()); // no place to store metadata here
271                 // Every part of an uninit is uninit.
272                 Ok(ImmTy::uninit(layout).into())
273             }
274         }
275     }
276
277     pub fn offset(
278         &self,
279         offset: Size,
280         layout: TyAndLayout<'tcx>,
281         cx: &impl HasDataLayout,
282     ) -> InterpResult<'tcx, Self> {
283         assert!(layout.is_sized());
284         self.offset_with_meta(offset, MemPlaceMeta::None, layout, cx)
285     }
286 }
287
288 impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
289     /// Try reading an immediate in memory; this is interesting particularly for `ScalarPair`.
290     /// Returns `None` if the layout does not permit loading this as a value.
291     ///
292     /// This is an internal function; call `read_immediate` instead.
293     fn read_immediate_from_mplace_raw(
294         &self,
295         mplace: &MPlaceTy<'tcx, M::Provenance>,
296     ) -> InterpResult<'tcx, Option<ImmTy<'tcx, M::Provenance>>> {
297         if mplace.layout.is_unsized() {
298             // Don't touch unsized
299             return Ok(None);
300         }
301
302         let Some(alloc) = self.get_place_alloc(mplace)? else {
303             // zero-sized type can be left uninit
304             return Ok(Some(ImmTy::uninit(mplace.layout)));
305         };
306
307         // It may seem like all types with `Scalar` or `ScalarPair` ABI are fair game at this point.
308         // However, `MaybeUninit<u64>` is considered a `Scalar` as far as its layout is concerned --
309         // and yet cannot be represented by an interpreter `Scalar`, since we have to handle the
310         // case where some of the bytes are initialized and others are not. So, we need an extra
311         // check that walks over the type of `mplace` to make sure it is truly correct to treat this
312         // like a `Scalar` (or `ScalarPair`).
313         Ok(match mplace.layout.abi {
314             Abi::Scalar(abi::Scalar::Initialized { value: s, .. }) => {
315                 let size = s.size(self);
316                 assert_eq!(size, mplace.layout.size, "abi::Scalar size does not match layout size");
317                 let scalar = alloc.read_scalar(
318                     alloc_range(Size::ZERO, size),
319                     /*read_provenance*/ s.is_ptr(),
320                 )?;
321                 Some(ImmTy { imm: scalar.into(), layout: mplace.layout })
322             }
323             Abi::ScalarPair(
324                 abi::Scalar::Initialized { value: a, .. },
325                 abi::Scalar::Initialized { value: b, .. },
326             ) => {
327                 // We checked `ptr_align` above, so all fields will have the alignment they need.
328                 // We would anyway check against `ptr_align.restrict_for_offset(b_offset)`,
329                 // which `ptr.offset(b_offset)` cannot possibly fail to satisfy.
330                 let (a_size, b_size) = (a.size(self), b.size(self));
331                 let b_offset = a_size.align_to(b.align(self).abi);
332                 assert!(b_offset.bytes() > 0); // in `operand_field` we use the offset to tell apart the fields
333                 let a_val = alloc.read_scalar(
334                     alloc_range(Size::ZERO, a_size),
335                     /*read_provenance*/ a.is_ptr(),
336                 )?;
337                 let b_val = alloc.read_scalar(
338                     alloc_range(b_offset, b_size),
339                     /*read_provenance*/ b.is_ptr(),
340                 )?;
341                 Some(ImmTy {
342                     imm: Immediate::ScalarPair(a_val.into(), b_val.into()),
343                     layout: mplace.layout,
344                 })
345             }
346             _ => {
347                 // Neither a scalar nor scalar pair.
348                 None
349             }
350         })
351     }
352
353     /// Try returning an immediate for the operand. If the layout does not permit loading this as an
354     /// immediate, return where in memory we can find the data.
355     /// Note that for a given layout, this operation will either always fail or always
356     /// succeed!  Whether it succeeds depends on whether the layout can be represented
357     /// in an `Immediate`, not on which data is stored there currently.
358     ///
359     /// This is an internal function that should not usually be used; call `read_immediate` instead.
360     /// ConstProp needs it, though.
361     pub fn read_immediate_raw(
362         &self,
363         src: &OpTy<'tcx, M::Provenance>,
364     ) -> InterpResult<'tcx, Result<ImmTy<'tcx, M::Provenance>, MPlaceTy<'tcx, M::Provenance>>> {
365         Ok(match src.try_as_mplace() {
366             Ok(ref mplace) => {
367                 if let Some(val) = self.read_immediate_from_mplace_raw(mplace)? {
368                     Ok(val)
369                 } else {
370                     Err(*mplace)
371                 }
372             }
373             Err(val) => Ok(val),
374         })
375     }
376
377     /// Read an immediate from a place, asserting that that is possible with the given layout.
378     ///
379     /// If this succeeds, the `ImmTy` is never `Uninit`.
380     #[inline(always)]
381     pub fn read_immediate(
382         &self,
383         op: &OpTy<'tcx, M::Provenance>,
384     ) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>> {
385         if !matches!(
386             op.layout.abi,
387             Abi::Scalar(abi::Scalar::Initialized { .. })
388                 | Abi::ScalarPair(abi::Scalar::Initialized { .. }, abi::Scalar::Initialized { .. })
389         ) {
390             span_bug!(self.cur_span(), "primitive read not possible for type: {:?}", op.layout.ty);
391         }
392         let imm = self.read_immediate_raw(op)?.unwrap();
393         if matches!(*imm, Immediate::Uninit) {
394             throw_ub!(InvalidUninitBytes(None));
395         }
396         Ok(imm)
397     }
398
399     /// Read a scalar from a place
400     pub fn read_scalar(
401         &self,
402         op: &OpTy<'tcx, M::Provenance>,
403     ) -> InterpResult<'tcx, Scalar<M::Provenance>> {
404         Ok(self.read_immediate(op)?.to_scalar())
405     }
406
407     /// Read a pointer from a place.
408     pub fn read_pointer(
409         &self,
410         op: &OpTy<'tcx, M::Provenance>,
411     ) -> InterpResult<'tcx, Pointer<Option<M::Provenance>>> {
412         self.read_scalar(op)?.to_pointer(self)
413     }
414
415     /// Turn the wide MPlace into a string (must already be dereferenced!)
416     pub fn read_str(&self, mplace: &MPlaceTy<'tcx, M::Provenance>) -> InterpResult<'tcx, &str> {
417         let len = mplace.len(self)?;
418         let bytes = self.read_bytes_ptr_strip_provenance(mplace.ptr, Size::from_bytes(len))?;
419         let str = std::str::from_utf8(bytes).map_err(|err| err_ub!(InvalidStr(err)))?;
420         Ok(str)
421     }
422
423     /// Converts a repr(simd) operand into an operand where `place_index` accesses the SIMD elements.
424     /// Also returns the number of elements.
425     ///
426     /// Can (but does not always) trigger UB if `op` is uninitialized.
427     pub fn operand_to_simd(
428         &self,
429         op: &OpTy<'tcx, M::Provenance>,
430     ) -> InterpResult<'tcx, (MPlaceTy<'tcx, M::Provenance>, u64)> {
431         // Basically we just transmute this place into an array following simd_size_and_type.
432         // This only works in memory, but repr(simd) types should never be immediates anyway.
433         assert!(op.layout.ty.is_simd());
434         match op.try_as_mplace() {
435             Ok(mplace) => self.mplace_to_simd(&mplace),
436             Err(imm) => match *imm {
437                 Immediate::Uninit => {
438                     throw_ub!(InvalidUninitBytes(None))
439                 }
440                 Immediate::Scalar(..) | Immediate::ScalarPair(..) => {
441                     bug!("arrays/slices can never have Scalar/ScalarPair layout")
442                 }
443             },
444         }
445     }
446
447     /// Read from a local.
448     /// Will not access memory, instead an indirect `Operand` is returned.
449     ///
450     /// This is public because it is used by [priroda](https://github.com/oli-obk/priroda) to get an
451     /// OpTy from a local.
452     pub fn local_to_op(
453         &self,
454         frame: &Frame<'mir, 'tcx, M::Provenance, M::FrameExtra>,
455         local: mir::Local,
456         layout: Option<TyAndLayout<'tcx>>,
457     ) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
458         let layout = self.layout_of_local(frame, local, layout)?;
459         let op = *frame.locals[local].access()?;
460         Ok(OpTy { op, layout, align: Some(layout.align.abi) })
461     }
462
463     /// Every place can be read from, so we can turn them into an operand.
464     /// This will definitely return `Indirect` if the place is a `Ptr`, i.e., this
465     /// will never actually read from memory.
466     #[inline(always)]
467     pub fn place_to_op(
468         &self,
469         place: &PlaceTy<'tcx, M::Provenance>,
470     ) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
471         let op = match **place {
472             Place::Ptr(mplace) => Operand::Indirect(mplace),
473             Place::Local { frame, local } => {
474                 *self.local_to_op(&self.stack()[frame], local, None)?
475             }
476         };
477         Ok(OpTy { op, layout: place.layout, align: Some(place.align) })
478     }
479
480     /// Evaluate a place with the goal of reading from it.  This lets us sometimes
481     /// avoid allocations.
482     pub fn eval_place_to_op(
483         &self,
484         mir_place: mir::Place<'tcx>,
485         layout: Option<TyAndLayout<'tcx>>,
486     ) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
487         // Do not use the layout passed in as argument if the base we are looking at
488         // here is not the entire place.
489         let layout = if mir_place.projection.is_empty() { layout } else { None };
490
491         let mut op = self.local_to_op(self.frame(), mir_place.local, layout)?;
492         // Using `try_fold` turned out to be bad for performance, hence the loop.
493         for elem in mir_place.projection.iter() {
494             op = self.operand_projection(&op, elem)?
495         }
496
497         trace!("eval_place_to_op: got {:?}", *op);
498         // Sanity-check the type we ended up with.
499         debug_assert!(
500             mir_assign_valid_types(
501                 *self.tcx,
502                 self.param_env,
503                 self.layout_of(self.subst_from_current_frame_and_normalize_erasing_regions(
504                     mir_place.ty(&self.frame().body.local_decls, *self.tcx).ty
505                 )?)?,
506                 op.layout,
507             ),
508             "eval_place of a MIR place with type {:?} produced an interpreter operand with type {:?}",
509             mir_place.ty(&self.frame().body.local_decls, *self.tcx).ty,
510             op.layout.ty,
511         );
512         Ok(op)
513     }
514
515     /// Evaluate the operand, returning a place where you can then find the data.
516     /// If you already know the layout, you can save two table lookups
517     /// by passing it in here.
518     #[inline]
519     pub fn eval_operand(
520         &self,
521         mir_op: &mir::Operand<'tcx>,
522         layout: Option<TyAndLayout<'tcx>>,
523     ) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
524         use rustc_middle::mir::Operand::*;
525         let op = match *mir_op {
526             // FIXME: do some more logic on `move` to invalidate the old location
527             Copy(place) | Move(place) => self.eval_place_to_op(place, layout)?,
528
529             Constant(ref constant) => {
530                 let val =
531                     self.subst_from_current_frame_and_normalize_erasing_regions(constant.literal)?;
532
533                 // This can still fail:
534                 // * During ConstProp, with `TooGeneric` or since the `required_consts` were not all
535                 //   checked yet.
536                 // * During CTFE, since promoteds in `const`/`static` initializer bodies can fail.
537                 self.const_to_op(&val, layout)?
538             }
539         };
540         trace!("{:?}: {:?}", mir_op, *op);
541         Ok(op)
542     }
543
544     /// Evaluate a bunch of operands at once
545     pub(super) fn eval_operands(
546         &self,
547         ops: &[mir::Operand<'tcx>],
548     ) -> InterpResult<'tcx, Vec<OpTy<'tcx, M::Provenance>>> {
549         ops.iter().map(|op| self.eval_operand(op, None)).collect()
550     }
551
552     pub fn const_to_op(
553         &self,
554         val: &mir::ConstantKind<'tcx>,
555         layout: Option<TyAndLayout<'tcx>>,
556     ) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
557         // FIXME(const_prop): normalization needed b/c const prop lint in
558         // `mir_drops_elaborated_and_const_checked`, which happens before
559         // optimized MIR. Only after optimizing the MIR can we guarantee
560         // that the `RevealAll` pass has happened and that the body's consts
561         // are normalized, so any call to resolve before that needs to be
562         // manually normalized.
563         let val = self.tcx.normalize_erasing_regions(self.param_env, *val);
564         match val {
565             mir::ConstantKind::Ty(ct) => {
566                 match ct.kind() {
567                     ty::ConstKind::Param(_) | ty::ConstKind::Placeholder(..) => {
568                         throw_inval!(TooGeneric)
569                     }
570                     ty::ConstKind::Error(reported) => {
571                         throw_inval!(AlreadyReported(reported))
572                     }
573                     ty::ConstKind::Unevaluated(uv) => {
574                         // NOTE: We evaluate to a `ValTree` here as a check to ensure
575                         // we're working with valid constants, even though we never need it.
576                         let instance = self.resolve(uv.def, uv.substs)?;
577                         let cid = GlobalId { instance, promoted: None };
578                         let _valtree = self
579                             .tcx
580                             .eval_to_valtree(self.param_env.and(cid))?
581                             .unwrap_or_else(|| bug!("unable to create ValTree for {uv:?}"));
582
583                         Ok(self.eval_to_allocation(cid)?.into())
584                     }
585                     ty::ConstKind::Bound(..) | ty::ConstKind::Infer(..) => {
586                         span_bug!(self.cur_span(), "unexpected ConstKind in ctfe: {ct:?}")
587                     }
588                     ty::ConstKind::Value(valtree) => {
589                         let ty = ct.ty();
590                         let const_val = self.tcx.valtree_to_const_val((ty, valtree));
591                         self.const_val_to_op(const_val, ty, layout)
592                     }
593                 }
594             }
595             mir::ConstantKind::Val(val, ty) => self.const_val_to_op(val, ty, layout),
596             mir::ConstantKind::Unevaluated(uv, _) => {
597                 let instance = self.resolve(uv.def, uv.substs)?;
598                 Ok(self.eval_to_allocation(GlobalId { instance, promoted: uv.promoted })?.into())
599             }
600         }
601     }
602
603     pub(crate) fn const_val_to_op(
604         &self,
605         val_val: ConstValue<'tcx>,
606         ty: Ty<'tcx>,
607         layout: Option<TyAndLayout<'tcx>>,
608     ) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
609         // Other cases need layout.
610         let adjust_scalar = |scalar| -> InterpResult<'tcx, _> {
611             Ok(match scalar {
612                 Scalar::Ptr(ptr, size) => Scalar::Ptr(self.global_base_pointer(ptr)?, size),
613                 Scalar::Int(int) => Scalar::Int(int),
614             })
615         };
616         let layout = from_known_layout(self.tcx, self.param_env, layout, || self.layout_of(ty))?;
617         let op = match val_val {
618             ConstValue::ByRef { alloc, offset } => {
619                 let id = self.tcx.create_memory_alloc(alloc);
620                 // We rely on mutability being set correctly in that allocation to prevent writes
621                 // where none should happen.
622                 let ptr = self.global_base_pointer(Pointer::new(id, offset))?;
623                 Operand::Indirect(MemPlace::from_ptr(ptr.into()))
624             }
625             ConstValue::Scalar(x) => Operand::Immediate(adjust_scalar(x)?.into()),
626             ConstValue::ZeroSized => Operand::Immediate(Immediate::Uninit),
627             ConstValue::Slice { data, start, end } => {
628                 // We rely on mutability being set correctly in `data` to prevent writes
629                 // where none should happen.
630                 let ptr = Pointer::new(
631                     self.tcx.create_memory_alloc(data),
632                     Size::from_bytes(start), // offset: `start`
633                 );
634                 Operand::Immediate(Immediate::new_slice(
635                     Scalar::from_pointer(self.global_base_pointer(ptr)?, &*self.tcx),
636                     u64::try_from(end.checked_sub(start).unwrap()).unwrap(), // len: `end - start`
637                     self,
638                 ))
639             }
640         };
641         Ok(OpTy { op, layout, align: Some(layout.align.abi) })
642     }
643
644     /// Read discriminant, return the runtime value as well as the variant index.
645     /// Can also legally be called on non-enums (e.g. through the discriminant_value intrinsic)!
646     pub fn read_discriminant(
647         &self,
648         op: &OpTy<'tcx, M::Provenance>,
649     ) -> InterpResult<'tcx, (Scalar<M::Provenance>, VariantIdx)> {
650         trace!("read_discriminant_value {:#?}", op.layout);
651         // Get type and layout of the discriminant.
652         let discr_layout = self.layout_of(op.layout.ty.discriminant_ty(*self.tcx))?;
653         trace!("discriminant type: {:?}", discr_layout.ty);
654
655         // We use "discriminant" to refer to the value associated with a particular enum variant.
656         // This is not to be confused with its "variant index", which is just determining its position in the
657         // declared list of variants -- they can differ with explicitly assigned discriminants.
658         // We use "tag" to refer to how the discriminant is encoded in memory, which can be either
659         // straight-forward (`TagEncoding::Direct`) or with a niche (`TagEncoding::Niche`).
660         let (tag_scalar_layout, tag_encoding, tag_field) = match op.layout.variants {
661             Variants::Single { index } => {
662                 let discr = match op.layout.ty.discriminant_for_variant(*self.tcx, index) {
663                     Some(discr) => {
664                         // This type actually has discriminants.
665                         assert_eq!(discr.ty, discr_layout.ty);
666                         Scalar::from_uint(discr.val, discr_layout.size)
667                     }
668                     None => {
669                         // On a type without actual discriminants, variant is 0.
670                         assert_eq!(index.as_u32(), 0);
671                         Scalar::from_uint(index.as_u32(), discr_layout.size)
672                     }
673                 };
674                 return Ok((discr, index));
675             }
676             Variants::Multiple { tag, ref tag_encoding, tag_field, .. } => {
677                 (tag, tag_encoding, tag_field)
678             }
679         };
680
681         // There are *three* layouts that come into play here:
682         // - The discriminant has a type for typechecking. This is `discr_layout`, and is used for
683         //   the `Scalar` we return.
684         // - The tag (encoded discriminant) has layout `tag_layout`. This is always an integer type,
685         //   and used to interpret the value we read from the tag field.
686         //   For the return value, a cast to `discr_layout` is performed.
687         // - The field storing the tag has a layout, which is very similar to `tag_layout` but
688         //   may be a pointer. This is `tag_val.layout`; we just use it for sanity checks.
689
690         // Get layout for tag.
691         let tag_layout = self.layout_of(tag_scalar_layout.primitive().to_int_ty(*self.tcx))?;
692
693         // Read tag and sanity-check `tag_layout`.
694         let tag_val = self.read_immediate(&self.operand_field(op, tag_field)?)?;
695         assert_eq!(tag_layout.size, tag_val.layout.size);
696         assert_eq!(tag_layout.abi.is_signed(), tag_val.layout.abi.is_signed());
697         trace!("tag value: {}", tag_val);
698
699         // Figure out which discriminant and variant this corresponds to.
700         Ok(match *tag_encoding {
701             TagEncoding::Direct => {
702                 let scalar = tag_val.to_scalar();
703                 // Generate a specific error if `tag_val` is not an integer.
704                 // (`tag_bits` itself is only used for error messages below.)
705                 let tag_bits = scalar
706                     .try_to_int()
707                     .map_err(|dbg_val| err_ub!(InvalidTag(dbg_val)))?
708                     .assert_bits(tag_layout.size);
709                 // Cast bits from tag layout to discriminant layout.
710                 // After the checks we did above, this cannot fail, as
711                 // discriminants are int-like.
712                 let discr_val =
713                     self.cast_from_int_like(scalar, tag_val.layout, discr_layout.ty).unwrap();
714                 let discr_bits = discr_val.assert_bits(discr_layout.size);
715                 // Convert discriminant to variant index, and catch invalid discriminants.
716                 let index = match *op.layout.ty.kind() {
717                     ty::Adt(adt, _) => {
718                         adt.discriminants(*self.tcx).find(|(_, var)| var.val == discr_bits)
719                     }
720                     ty::Generator(def_id, substs, _) => {
721                         let substs = substs.as_generator();
722                         substs
723                             .discriminants(def_id, *self.tcx)
724                             .find(|(_, var)| var.val == discr_bits)
725                     }
726                     _ => span_bug!(self.cur_span(), "tagged layout for non-adt non-generator"),
727                 }
728                 .ok_or_else(|| err_ub!(InvalidTag(Scalar::from_uint(tag_bits, tag_layout.size))))?;
729                 // Return the cast value, and the index.
730                 (discr_val, index.0)
731             }
732             TagEncoding::Niche { untagged_variant, ref niche_variants, niche_start } => {
733                 let tag_val = tag_val.to_scalar();
734                 // Compute the variant this niche value/"tag" corresponds to. With niche layout,
735                 // discriminant (encoded in niche/tag) and variant index are the same.
736                 let variants_start = niche_variants.start().as_u32();
737                 let variants_end = niche_variants.end().as_u32();
738                 let variant = match tag_val.try_to_int() {
739                     Err(dbg_val) => {
740                         // So this is a pointer then, and casting to an int failed.
741                         // Can only happen during CTFE.
742                         // The niche must be just 0, and the ptr not null, then we know this is
743                         // okay. Everything else, we conservatively reject.
744                         let ptr_valid = niche_start == 0
745                             && variants_start == variants_end
746                             && !self.scalar_may_be_null(tag_val)?;
747                         if !ptr_valid {
748                             throw_ub!(InvalidTag(dbg_val))
749                         }
750                         untagged_variant
751                     }
752                     Ok(tag_bits) => {
753                         let tag_bits = tag_bits.assert_bits(tag_layout.size);
754                         // We need to use machine arithmetic to get the relative variant idx:
755                         // variant_index_relative = tag_val - niche_start_val
756                         let tag_val = ImmTy::from_uint(tag_bits, tag_layout);
757                         let niche_start_val = ImmTy::from_uint(niche_start, tag_layout);
758                         let variant_index_relative_val =
759                             self.binary_op(mir::BinOp::Sub, &tag_val, &niche_start_val)?;
760                         let variant_index_relative =
761                             variant_index_relative_val.to_scalar().assert_bits(tag_val.layout.size);
762                         // Check if this is in the range that indicates an actual discriminant.
763                         if variant_index_relative <= u128::from(variants_end - variants_start) {
764                             let variant_index_relative = u32::try_from(variant_index_relative)
765                                 .expect("we checked that this fits into a u32");
766                             // Then computing the absolute variant idx should not overflow any more.
767                             let variant_index = variants_start
768                                 .checked_add(variant_index_relative)
769                                 .expect("overflow computing absolute variant idx");
770                             let variants_len = op
771                                 .layout
772                                 .ty
773                                 .ty_adt_def()
774                                 .expect("tagged layout for non adt")
775                                 .variants()
776                                 .len();
777                             assert!(usize::try_from(variant_index).unwrap() < variants_len);
778                             VariantIdx::from_u32(variant_index)
779                         } else {
780                             untagged_variant
781                         }
782                     }
783                 };
784                 // Compute the size of the scalar we need to return.
785                 // No need to cast, because the variant index directly serves as discriminant and is
786                 // encoded in the tag.
787                 (Scalar::from_uint(variant.as_u32(), discr_layout.size), variant)
788             }
789         })
790     }
791 }
792
793 // Some nodes are used a lot. Make sure they don't unintentionally get bigger.
794 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
795 mod size_asserts {
796     use super::*;
797     use rustc_data_structures::static_assert_size;
798     // tidy-alphabetical-start
799     static_assert_size!(Immediate, 48);
800     static_assert_size!(ImmTy<'_>, 64);
801     static_assert_size!(Operand, 56);
802     static_assert_size!(OpTy<'_>, 80);
803     // tidy-alphabetical-end
804 }