1 //! This file implements "place projections"; basically a symmetric API for 3 types: MPlaceTy, OpTy, PlaceTy.
3 //! OpTy and PlaceTy genrally work by "let's see if we are actually an MPlaceTy, and do something custom if not".
4 //! For PlaceTy, the custom thing is basically always to call `force_allocation` and then use the MPlaceTy logic anyway.
5 //! For OpTy, the custom thing on field pojections has to be pretty clever (since `Operand::Immediate` can have fields),
6 //! but for array/slice operations it only has to worry about `Operand::Uninit`. That makes the value part trivial,
7 //! but we still need to do bounds checking and adjust the layout. To not duplicate that with MPlaceTy, we actually
8 //! implement the logic on OpTy, and MPlaceTy calls that.
12 use rustc_middle::mir;
14 use rustc_middle::ty::layout::LayoutOf;
15 use rustc_target::abi::{self, Abi, VariantIdx};
18 ImmTy, Immediate, InterpCx, InterpResult, MPlaceTy, Machine, MemPlaceMeta, OpTy, PlaceTy,
22 // FIXME: Working around https://github.com/rust-lang/rust/issues/54385
23 impl<'mir, 'tcx: 'mir, Tag, M> InterpCx<'mir, 'tcx, M>
25 Tag: Provenance + Eq + Hash + 'static,
26 M: Machine<'mir, 'tcx, PointerTag = Tag>,
30 /// Offset a pointer to project to a field of a struct/union. Unlike `place_field`, this is
31 /// always possible without allocating, so it can take `&self`. Also return the field's layout.
32 /// This supports both struct and array fields.
34 /// This also works for arrays, but then the `usize` index type is restricting.
35 /// For indexing into arrays, use `mplace_index`.
38 base: &MPlaceTy<'tcx, M::PointerTag>,
40 ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
41 let offset = base.layout.fields.offset(field);
42 let field_layout = base.layout.field(self, field);
44 // Offset may need adjustment for unsized fields.
45 let (meta, offset) = if field_layout.is_unsized() {
46 // Re-use parent metadata to determine dynamic field layout.
47 // With custom DSTS, this *will* execute user-defined code, but the same
48 // happens at run-time so that's okay.
49 match self.size_and_align_of(&base.meta, &field_layout)? {
50 Some((_, align)) => (base.meta, offset.align_to(align)),
52 // For unsized types with an extern type tail we perform no adjustments.
53 // NOTE: keep this in sync with `PlaceRef::project_field` in the codegen backend.
54 assert!(matches!(base.meta, MemPlaceMeta::None));
59 // base.meta could be present; we might be accessing a sized field of an unsized
61 (MemPlaceMeta::None, offset)
64 // We do not look at `base.layout.align` nor `field_layout.align`, unlike
65 // codegen -- mostly to see if we can get away with that
66 base.offset_with_meta(offset, meta, field_layout, self)
69 /// Gets the place of a field inside the place, and also the field's type.
70 /// Just a convenience function, but used quite a bit.
71 /// This is the only projection that might have a side-effect: We cannot project
72 /// into the field of a local `ScalarPair`, we have to first allocate it.
75 base: &PlaceTy<'tcx, M::PointerTag>,
77 ) -> InterpResult<'tcx, PlaceTy<'tcx, M::PointerTag>> {
78 // FIXME: We could try to be smarter and avoid allocation for fields that span the
80 let base = self.force_allocation(base)?;
81 Ok(self.mplace_field(&base, field)?.into())
86 base: &OpTy<'tcx, M::PointerTag>,
88 ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
89 let base = match base.try_as_mplace() {
91 // We can reuse the mplace field computation logic for indirect operands.
92 let field = self.mplace_field(mplace, field)?;
93 return Ok(field.into());
98 let field_layout = base.layout.field(self, field);
99 let offset = base.layout.fields.offset(field);
100 // This makes several assumptions about what layouts we will encounter; we match what
101 // codegen does as good as we can (see `extract_field` in `rustc_codegen_ssa/src/mir/operand.rs`).
102 let field_val: Immediate<_> = match (*base, base.layout.abi) {
103 // the field contains no information, can be left uninit
104 _ if field_layout.is_zst() => Immediate::Uninit,
105 // the field covers the entire type
106 _ if field_layout.size == base.layout.size => {
107 assert!(match (base.layout.abi, field_layout.abi) {
108 (Abi::Scalar(..), Abi::Scalar(..)) => true,
109 (Abi::ScalarPair(..), Abi::ScalarPair(..)) => true,
112 assert!(offset.bytes() == 0);
115 // extract fields from types with `ScalarPair` ABI
116 (Immediate::ScalarPair(a_val, b_val), Abi::ScalarPair(a, b)) => {
117 assert!(matches!(field_layout.abi, Abi::Scalar(..)));
118 Immediate::from(if offset.bytes() == 0 {
119 debug_assert_eq!(field_layout.size, a.size(self));
122 debug_assert_eq!(offset, a.size(self).align_to(b.align(self).abi));
123 debug_assert_eq!(field_layout.size, b.size(self));
129 "invalid field access on immediate {}, layout {:#?}",
135 Ok(ImmTy::from_immediate(field_val, field_layout).into())
140 pub fn mplace_downcast(
142 base: &MPlaceTy<'tcx, M::PointerTag>,
144 ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
145 // Downcasts only change the layout.
146 // (In particular, no check about whether this is even the active variant -- that's by design,
147 // see https://github.com/rust-lang/rust/issues/93688#issuecomment-1032929496.)
148 assert!(!base.meta.has_meta());
149 let mut base = *base;
150 base.layout = base.layout.for_variant(self, variant);
154 pub fn place_downcast(
156 base: &PlaceTy<'tcx, M::PointerTag>,
158 ) -> InterpResult<'tcx, PlaceTy<'tcx, M::PointerTag>> {
159 // Downcast just changes the layout
160 let mut base = base.clone();
161 base.layout = base.layout.for_variant(self, variant);
165 pub fn operand_downcast(
167 base: &OpTy<'tcx, M::PointerTag>,
169 ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
170 // Downcast just changes the layout
171 let mut base = base.clone();
172 base.layout = base.layout.for_variant(self, variant);
179 pub fn operand_index(
181 base: &OpTy<'tcx, M::PointerTag>,
183 ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
184 // Not using the layout method because we want to compute on u64
185 match base.layout.fields {
186 abi::FieldsShape::Array { stride, count: _ } => {
187 // `count` is nonsense for slices, use the dynamic length instead.
188 let len = base.len(self)?;
190 // This can only be reached in ConstProp and non-rustc-MIR.
191 throw_ub!(BoundsCheckFailed { len, index });
193 let offset = stride * index; // `Size` multiplication
194 // All fields have the same layout.
195 let field_layout = base.layout.field(self, 0);
196 base.offset(offset, field_layout, self)
200 "`mplace_index` called on non-array type {:?}",
206 // Iterates over all fields of an array. Much more efficient than doing the
207 // same by repeatedly calling `operand_index`.
208 pub fn operand_array_fields<'a>(
210 base: &'a OpTy<'tcx, Tag>,
211 ) -> InterpResult<'tcx, impl Iterator<Item = InterpResult<'tcx, OpTy<'tcx, Tag>>> + 'a> {
212 let len = base.len(self)?; // also asserts that we have a type where this makes sense
213 let abi::FieldsShape::Array { stride, .. } = base.layout.fields else {
214 span_bug!(self.cur_span(), "operand_array_fields: expected an array layout");
216 let field_layout = base.layout.field(self, 0);
217 let dl = &self.tcx.data_layout;
218 // `Size` multiplication
219 Ok((0..len).map(move |i| base.offset(stride * i, field_layout, dl)))
222 /// Index into an array.
225 base: &MPlaceTy<'tcx, M::PointerTag>,
227 ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
228 Ok(self.operand_index(&base.into(), index)?.assert_mem_place())
233 base: &PlaceTy<'tcx, M::PointerTag>,
235 ) -> InterpResult<'tcx, PlaceTy<'tcx, M::PointerTag>> {
236 // There's not a lot we can do here, since we cannot have a place to a part of a local. If
237 // we are accessing the only element of a 1-element array, it's still the entire local...
238 // that doesn't seem worth it.
239 let base = self.force_allocation(base)?;
240 Ok(self.mplace_index(&base, index)?.into())
243 //# ConstantIndex support
245 fn operand_constant_index(
247 base: &OpTy<'tcx, M::PointerTag>,
251 ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
252 let n = base.len(self)?;
254 // This can only be reached in ConstProp and non-rustc-MIR.
255 throw_ub!(BoundsCheckFailed { len: min_length, index: n });
258 let index = if from_end {
259 assert!(0 < offset && offset <= min_length);
260 n.checked_sub(offset).unwrap()
262 assert!(offset < min_length);
266 self.operand_index(base, index)
269 fn place_constant_index(
271 base: &PlaceTy<'tcx, M::PointerTag>,
275 ) -> InterpResult<'tcx, PlaceTy<'tcx, M::PointerTag>> {
276 let base = self.force_allocation(base)?;
278 .operand_constant_index(&base.into(), offset, min_length, from_end)?
287 base: &OpTy<'tcx, M::PointerTag>,
291 ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
292 let len = base.len(self)?; // also asserts that we have a type where this makes sense
293 let actual_to = if from_end {
294 if from.checked_add(to).map_or(true, |to| to > len) {
295 // This can only be reached in ConstProp and non-rustc-MIR.
296 throw_ub!(BoundsCheckFailed { len: len, index: from.saturating_add(to) });
298 len.checked_sub(to).unwrap()
303 // Not using layout method because that works with usize, and does not work with slices
304 // (that have count 0 in their layout).
305 let from_offset = match base.layout.fields {
306 abi::FieldsShape::Array { stride, .. } => stride * from, // `Size` multiplication is checked
308 span_bug!(self.cur_span(), "unexpected layout of index access: {:#?}", base.layout)
312 // Compute meta and new layout
313 let inner_len = actual_to.checked_sub(from).unwrap();
314 let (meta, ty) = match base.layout.ty.kind() {
315 // It is not nice to match on the type, but that seems to be the only way to
317 ty::Array(inner, _) => (MemPlaceMeta::None, self.tcx.mk_array(*inner, inner_len)),
319 let len = Scalar::from_machine_usize(inner_len, self);
320 (MemPlaceMeta::Meta(len), base.layout.ty)
323 span_bug!(self.cur_span(), "cannot subslice non-array type: `{:?}`", base.layout.ty)
326 let layout = self.layout_of(ty)?;
327 base.offset_with_meta(from_offset, meta, layout, self)
330 pub fn place_subslice(
332 base: &PlaceTy<'tcx, M::PointerTag>,
336 ) -> InterpResult<'tcx, PlaceTy<'tcx, M::PointerTag>> {
337 let base = self.force_allocation(base)?;
338 Ok(self.operand_subslice(&base.into(), from, to, from_end)?.assert_mem_place().into())
341 //# Applying a general projection
343 /// Projects into a place.
344 #[instrument(skip(self), level = "trace")]
345 pub fn place_projection(
347 base: &PlaceTy<'tcx, M::PointerTag>,
348 proj_elem: mir::PlaceElem<'tcx>,
349 ) -> InterpResult<'tcx, PlaceTy<'tcx, M::PointerTag>> {
350 use rustc_middle::mir::ProjectionElem::*;
353 let mut place = base.clone();
354 place.layout = self.layout_of(ty)?;
357 Field(field, _) => self.place_field(base, field.index())?,
358 Downcast(_, variant) => self.place_downcast(base, variant)?,
359 Deref => self.deref_operand(&self.place_to_op(base)?)?.into(),
361 let layout = self.layout_of(self.tcx.types.usize)?;
362 let n = self.local_to_op(self.frame(), local, Some(layout))?;
363 let n = self.read_scalar(&n)?.to_machine_usize(self)?;
364 self.place_index(base, n)?
366 ConstantIndex { offset, min_length, from_end } => {
367 self.place_constant_index(base, offset, min_length, from_end)?
369 Subslice { from, to, from_end } => self.place_subslice(base, from, to, from_end)?,
373 #[instrument(skip(self), level = "trace")]
374 pub fn operand_projection(
376 base: &OpTy<'tcx, M::PointerTag>,
377 proj_elem: mir::PlaceElem<'tcx>,
378 ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
379 use rustc_middle::mir::ProjectionElem::*;
382 let mut op = base.clone();
383 op.layout = self.layout_of(ty)?;
386 Field(field, _) => self.operand_field(base, field.index())?,
387 Downcast(_, variant) => self.operand_downcast(base, variant)?,
388 Deref => self.deref_operand(base)?.into(),
390 let layout = self.layout_of(self.tcx.types.usize)?;
391 let n = self.local_to_op(self.frame(), local, Some(layout))?;
392 let n = self.read_scalar(&n)?.to_machine_usize(self)?;
393 self.operand_index(base, n)?
395 ConstantIndex { offset, min_length, from_end } => {
396 self.operand_constant_index(base, offset, min_length, from_end)?
398 Subslice { from, to, from_end } => self.operand_subslice(base, from, to, from_end)?,