operand.rs 30.5 KB
Newer Older
R
Ralf Jung 已提交
1 2 3
//! Functions concerning immediate values and operands, and reading from operands.
//! All high-level functions to read from memory work on operands as sources.

4
use std::convert::TryFrom;
5
use std::fmt::Write;
R
Ralf Jung 已提交
6

7
use rustc_errors::ErrorReported;
M
Mazdak Farrokhzad 已提交
8 9
use rustc_hir::def::Namespace;
use rustc_macros::HashStable;
10
use rustc_middle::ty::layout::{PrimitiveExt, TyAndLayout};
M
Mazdak Farrokhzad 已提交
11 12 13
use rustc_middle::ty::print::{FmtPrinter, PrettyPrinter, Printer};
use rustc_middle::ty::Ty;
use rustc_middle::{mir, ty};
14
use rustc_target::abi::{Abi, DiscriminantKind, HasDataLayout, LayoutOf, Size};
15
use rustc_target::abi::{VariantIdx, Variants};
16 17

use super::{
R
Ralf Jung 已提交
18
    from_known_layout, mir_assign_valid_types, ConstValue, GlobalId, InterpCx, InterpResult,
19
    MPlaceTy, Machine, MemPlace, Place, PlaceTy, Pointer, Scalar, ScalarMaybeUninit,
20
};
21

22
/// An `Immediate` represents a single immediate self-contained Rust value.
R
Ralf Jung 已提交
23 24 25
///
/// For optimization of a few very common cases, there is also a representation for a pair of
/// primitive values (`ScalarPair`). It allows Miri to avoid making allocations for checked binary
R
Ralf Jung 已提交
26
/// operations and wide pointers. This idea was taken from rustc's codegen.
R
Ralf Jung 已提交
27
/// In particular, thanks to `ScalarPair`, arithmetic operations and casts can be entirely
R
Ralf Jung 已提交
28
/// defined on `Immediate`, and do not have to work with a `Place`.
29
#[derive(Copy, Clone, Debug, PartialEq, Eq, HashStable, Hash)]
30
pub enum Immediate<Tag = ()> {
31 32
    Scalar(ScalarMaybeUninit<Tag>),
    ScalarPair(ScalarMaybeUninit<Tag>, ScalarMaybeUninit<Tag>),
R
Ralf Jung 已提交
33 34
}

35
impl<Tag> From<ScalarMaybeUninit<Tag>> for Immediate<Tag> {
36
    #[inline(always)]
37
    fn from(val: ScalarMaybeUninit<Tag>) -> Self {
38 39 40 41 42 43 44 45 46 47 48
        Immediate::Scalar(val)
    }
}

impl<Tag> From<Scalar<Tag>> for Immediate<Tag> {
    #[inline(always)]
    fn from(val: Scalar<Tag>) -> Self {
        Immediate::Scalar(val.into())
    }
}

49 50 51 52 53 54 55
impl<Tag> From<Pointer<Tag>> for Immediate<Tag> {
    #[inline(always)]
    fn from(val: Pointer<Tag>) -> Self {
        Immediate::Scalar(Scalar::from(val).into())
    }
}

56
impl<'tcx, Tag> Immediate<Tag> {
M
Mark Rousskov 已提交
57
    pub fn new_slice(val: Scalar<Tag>, len: u64, cx: &impl HasDataLayout) -> Self {
R
refmt  
Ralf Jung 已提交
58
        Immediate::ScalarPair(val.into(), Scalar::from_machine_usize(len, cx).into())
R
Ralf Jung 已提交
59 60
    }

61
    pub fn new_dyn_trait(val: Scalar<Tag>, vtable: Pointer<Tag>) -> Self {
62
        Immediate::ScalarPair(val.into(), vtable.into())
R
Ralf Jung 已提交
63 64
    }

65
    #[inline]
66
    pub fn to_scalar_or_undef(self) -> ScalarMaybeUninit<Tag> {
R
Ralf Jung 已提交
67
        match self {
68
            Immediate::Scalar(val) => val,
R
Ralf Jung 已提交
69
            Immediate::ScalarPair(..) => bug!("Got a wide pointer where a scalar was expected"),
R
Ralf Jung 已提交
70 71 72
        }
    }

73
    #[inline]
74
    pub fn to_scalar(self) -> InterpResult<'tcx, Scalar<Tag>> {
R
Ralf Jung 已提交
75 76 77
        self.to_scalar_or_undef().not_undef()
    }

78
    #[inline]
79
    pub fn to_scalar_pair(self) -> InterpResult<'tcx, (Scalar<Tag>, Scalar<Tag>)> {
80
        match self {
81
            Immediate::Scalar(..) => bug!("Got a thin pointer where a scalar pair was expected"),
M
Mark Rousskov 已提交
82
            Immediate::ScalarPair(a, b) => Ok((a.not_undef()?, b.not_undef()?)),
83 84
        }
    }
R
Ralf Jung 已提交
85 86
}

87
// ScalarPair needs a type to interpret, so we often have an immediate and a type together
R
Ralf Jung 已提交
88 89
// as input for binary and cast operations.
#[derive(Copy, Clone, Debug)]
M
Mark Rousskov 已提交
90
pub struct ImmTy<'tcx, Tag = ()> {
91
    imm: Immediate<Tag>,
92
    pub layout: TyAndLayout<'tcx>,
R
Ralf Jung 已提交
93 94
}

95
impl<Tag: Copy> std::fmt::Display for ImmTy<'tcx, Tag> {
96 97 98
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        /// Helper function for printing a scalar to a FmtPrinter
        fn p<'a, 'tcx, F: std::fmt::Write, Tag>(
99
            cx: FmtPrinter<'a, 'tcx, F>,
100
            s: ScalarMaybeUninit<Tag>,
101 102 103
            ty: Ty<'tcx>,
        ) -> Result<FmtPrinter<'a, 'tcx, F>, std::fmt::Error> {
            match s {
104
                ScalarMaybeUninit::Scalar(s) => {
105 106
                    cx.pretty_print_const_scalar(s.erase_tag(), ty, true)
                }
107
                ScalarMaybeUninit::Uninit => cx.typed_value(
108 109 110 111 112 113 114
                    |mut this| {
                        this.write_str("{undef ")?;
                        Ok(this)
                    },
                    |this| this.print_type(ty),
                    " ",
                ),
115
            }
116 117 118 119 120 121 122 123
        }
        ty::tls::with(|tcx| {
            match self.imm {
                Immediate::Scalar(s) => {
                    if let Some(ty) = tcx.lift(&self.layout.ty) {
                        let cx = FmtPrinter::new(tcx, f, Namespace::ValueNS);
                        p(cx, s, ty)?;
                        return Ok(());
124
                    }
125
                    write!(f, "{}: {}", s.erase_tag(), self.layout.ty)
126 127 128
                }
                Immediate::ScalarPair(a, b) => {
                    // FIXME(oli-obk): at least print tuples and slices nicely
129
                    write!(f, "({}, {}): {}", a.erase_tag(), b.erase_tag(), self.layout.ty,)
M
Mark Rousskov 已提交
130
                }
131
            }
132
        })
133 134 135
    }
}

136 137
impl<'tcx, Tag> ::std::ops::Deref for ImmTy<'tcx, Tag> {
    type Target = Immediate<Tag>;
138
    #[inline(always)]
139
    fn deref(&self) -> &Immediate<Tag> {
140
        &self.imm
R
Ralf Jung 已提交
141 142 143 144
    }
}

/// An `Operand` is the result of computing a `mir::Operand`. It can be immediate,
A
Alexander Regueiro 已提交
145
/// or still in memory. The latter is an optimization, to delay reading that chunk of
R
Ralf Jung 已提交
146
/// memory and to avoid having to store arbitrary-sized data here.
147
#[derive(Copy, Clone, Debug, PartialEq, Eq, HashStable, Hash)]
148 149 150
pub enum Operand<Tag = ()> {
    Immediate(Immediate<Tag>),
    Indirect(MemPlace<Tag>),
R
Ralf Jung 已提交
151 152
}

153
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
M
Mark Rousskov 已提交
154
pub struct OpTy<'tcx, Tag = ()> {
155
    op: Operand<Tag>, // Keep this private; it helps enforce invariants.
156
    pub layout: TyAndLayout<'tcx>,
R
Ralf Jung 已提交
157 158
}

159 160
impl<'tcx, Tag> ::std::ops::Deref for OpTy<'tcx, Tag> {
    type Target = Operand<Tag>;
161
    #[inline(always)]
162
    fn deref(&self) -> &Operand<Tag> {
R
Ralf Jung 已提交
163 164 165 166
        &self.op
    }
}

167
impl<'tcx, Tag: Copy> From<MPlaceTy<'tcx, Tag>> for OpTy<'tcx, Tag> {
168
    #[inline(always)]
169
    fn from(mplace: MPlaceTy<'tcx, Tag>) -> Self {
M
Mark Rousskov 已提交
170
        OpTy { op: Operand::Indirect(*mplace), layout: mplace.layout }
R
Ralf Jung 已提交
171 172 173
    }
}

174
impl<'tcx, Tag> From<ImmTy<'tcx, Tag>> for OpTy<'tcx, Tag> {
175
    #[inline(always)]
176
    fn from(val: ImmTy<'tcx, Tag>) -> Self {
M
Mark Rousskov 已提交
177
        OpTy { op: Operand::Immediate(val.imm), layout: val.layout }
R
Ralf Jung 已提交
178 179 180
    }
}

R
Ralf Jung 已提交
181
impl<'tcx, Tag: Copy> ImmTy<'tcx, Tag> {
182
    #[inline]
183
    pub fn from_scalar(val: Scalar<Tag>, layout: TyAndLayout<'tcx>) -> Self {
184
        ImmTy { imm: val.into(), layout }
185 186
    }

187 188 189 190 191
    #[inline]
    pub fn from_immediate(imm: Immediate<Tag>, layout: TyAndLayout<'tcx>) -> Self {
        ImmTy { imm, layout }
    }

192
    #[inline]
193
    pub fn try_from_uint(i: impl Into<u128>, layout: TyAndLayout<'tcx>) -> Option<Self> {
C
Christian Poveda 已提交
194
        Some(Self::from_scalar(Scalar::try_from_uint(i, layout.size)?, layout))
195
    }
R
Ralf Jung 已提交
196
    #[inline]
197
    pub fn from_uint(i: impl Into<u128>, layout: TyAndLayout<'tcx>) -> Self {
R
Ralf Jung 已提交
198 199 200
        Self::from_scalar(Scalar::from_uint(i, layout.size), layout)
    }

201
    #[inline]
202
    pub fn try_from_int(i: impl Into<i128>, layout: TyAndLayout<'tcx>) -> Option<Self> {
C
Christian Poveda 已提交
203
        Some(Self::from_scalar(Scalar::try_from_int(i, layout.size)?, layout))
R
Ralf Jung 已提交
204 205 206
    }

    #[inline]
207
    pub fn from_int(i: impl Into<i128>, layout: TyAndLayout<'tcx>) -> Self {
R
Ralf Jung 已提交
208 209
        Self::from_scalar(Scalar::from_int(i, layout.size), layout)
    }
210 211
}

212
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
213 214 215
    /// Normalice `place.ptr` to a `Pointer` if this is a place and not a ZST.
    /// Can be helpful to avoid lots of `force_ptr` calls later, if this place is used a lot.
    #[inline]
R
Ralf Jung 已提交
216
    pub fn force_op_ptr(
217 218 219
        &self,
        op: OpTy<'tcx, M::PointerTag>,
    ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
220
        match op.try_as_mplace(self) {
R
Ralf Jung 已提交
221 222
            Ok(mplace) => Ok(self.force_mplace_ptr(mplace)?.into()),
            Err(imm) => Ok(imm.into()), // Nothing to cast/force
223 224 225
        }
    }

226
    /// Try reading an immediate in memory; this is interesting particularly for `ScalarPair`.
A
Alexander Regueiro 已提交
227
    /// Returns `None` if the layout does not permit loading this as a value.
228
    fn try_read_immediate_from_mplace(
R
Ralf Jung 已提交
229
        &self,
230
        mplace: MPlaceTy<'tcx, M::PointerTag>,
231
    ) -> InterpResult<'tcx, Option<ImmTy<'tcx, M::PointerTag>>> {
R
Ralf Jung 已提交
232
        if mplace.layout.is_unsized() {
M
Matthias Krüger 已提交
233
            // Don't touch unsized
234 235
            return Ok(None);
        }
R
Ralf Jung 已提交
236

M
Mark Rousskov 已提交
237 238
        let ptr = match self
            .check_mplace_access(mplace, None)
239 240
            .expect("places should be checked on creation")
        {
241
            Some(ptr) => ptr,
M
Mark Rousskov 已提交
242
            None => {
243 244 245 246
                if let Scalar::Ptr(ptr) = mplace.ptr {
                    // We may be reading from a static.
                    // In order to ensure that `static FOO: Type = FOO;` causes a cycle error
                    // instead of magically pulling *any* ZST value from the ether, we need to
247
                    // actually access the referenced allocation.
248 249
                    self.memory.get_raw(ptr.alloc_id)?;
                }
M
Mark Rousskov 已提交
250 251 252 253 254 255
                return Ok(Some(ImmTy {
                    // zero-sized type
                    imm: Scalar::zst().into(),
                    layout: mplace.layout,
                }));
            }
256
        };
R
Ralf Jung 已提交
257

258 259
        let alloc = self.memory.get_raw(ptr.alloc_id)?;

260
        match mplace.layout.abi {
261
            Abi::Scalar(..) => {
262
                let scalar = alloc.read_scalar(self, ptr, mplace.layout.size)?;
M
Mark Rousskov 已提交
263
                Ok(Some(ImmTy { imm: scalar.into(), layout: mplace.layout }))
R
Ralf Jung 已提交
264
            }
265
            Abi::ScalarPair(ref a, ref b) => {
266 267 268
                // We checked `ptr_align` above, so all fields will have the alignment they need.
                // We would anyway check against `ptr_align.restrict_for_offset(b_offset)`,
                // which `ptr.offset(b_offset)` cannot possibly fail to satisfy.
R
Ralf Jung 已提交
269 270 271
                let (a, b) = (&a.value, &b.value);
                let (a_size, b_size) = (a.size(self), b.size(self));
                let a_ptr = ptr;
272
                let b_offset = a_size.align_to(b.align(self).abi);
273
                assert!(b_offset.bytes() > 0); // we later use the offset to tell apart the fields
274
                let b_ptr = ptr.offset(b_offset, self)?;
275 276
                let a_val = alloc.read_scalar(self, a_ptr, a_size)?;
                let b_val = alloc.read_scalar(self, b_ptr, b_size)?;
M
Mark Rousskov 已提交
277
                Ok(Some(ImmTy { imm: Immediate::ScalarPair(a_val, b_val), layout: mplace.layout }))
R
Ralf Jung 已提交
278 279 280 281 282
            }
            _ => Ok(None),
        }
    }

283 284
    /// Try returning an immediate for the operand.
    /// If the layout does not permit loading this as an immediate, return where in memory
R
Ralf Jung 已提交
285 286 287
    /// we can find the data.
    /// Note that for a given layout, this operation will either always fail or always
    /// succeed!  Whether it succeeds depends on whether the layout can be represented
288
    /// in a `Immediate`, not on which data is stored there currently.
289
    pub(crate) fn try_read_immediate(
R
Ralf Jung 已提交
290
        &self,
291
        src: OpTy<'tcx, M::PointerTag>,
292
    ) -> InterpResult<'tcx, Result<ImmTy<'tcx, M::PointerTag>, MPlaceTy<'tcx, M::PointerTag>>> {
293
        Ok(match src.try_as_mplace(self) {
294
            Ok(mplace) => {
295
                if let Some(val) = self.try_read_immediate_from_mplace(mplace)? {
296 297
                    Ok(val)
                } else {
298
                    Err(mplace)
R
Ralf Jung 已提交
299
                }
M
Mark Rousskov 已提交
300
            }
301 302
            Err(val) => Ok(val),
        })
R
Ralf Jung 已提交
303 304
    }

305
    /// Read an immediate from a place, asserting that that is possible with the given layout.
R
Ralf Jung 已提交
306
    #[inline(always)]
307
    pub fn read_immediate(
308
        &self,
M
Mark Rousskov 已提交
309
        op: OpTy<'tcx, M::PointerTag>,
310
    ) -> InterpResult<'tcx, ImmTy<'tcx, M::PointerTag>> {
311
        if let Ok(imm) = self.try_read_immediate(op)? {
312
            Ok(imm)
R
Ralf Jung 已提交
313 314 315 316 317 318
        } else {
            bug!("primitive read failed for type: {:?}", op.layout.ty);
        }
    }

    /// Read a scalar from a place
319 320
    pub fn read_scalar(
        &self,
M
Mark Rousskov 已提交
321
        op: OpTy<'tcx, M::PointerTag>,
322
    ) -> InterpResult<'tcx, ScalarMaybeUninit<M::PointerTag>> {
323
        Ok(self.read_immediate(op)?.to_scalar_or_undef())
R
Ralf Jung 已提交
324 325
    }

R
Ralf Jung 已提交
326
    // Turn the wide MPlace into a string (must already be dereferenced!)
M
Mark Rousskov 已提交
327
    pub fn read_str(&self, mplace: MPlaceTy<'tcx, M::PointerTag>) -> InterpResult<'tcx, &str> {
328
        let len = mplace.len(self)?;
R
Ralf Jung 已提交
329
        let bytes = self.memory.read_bytes(mplace.ptr, Size::from_bytes(len))?;
330
        let str = ::std::str::from_utf8(bytes).map_err(|err| err_ub!(InvalidStr(err)))?;
331
        Ok(str)
R
Ralf Jung 已提交
332 333
    }

R
Ralf Jung 已提交
334 335 336
    /// Projection functions
    pub fn operand_field(
        &self,
337
        op: OpTy<'tcx, M::PointerTag>,
338
        field: usize,
339
    ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
340
        let base = match op.try_as_mplace(self) {
R
Ralf Jung 已提交
341
            Ok(mplace) => {
O
Oliver Scherer 已提交
342
                // We can reuse the mplace field computation logic for indirect operands.
R
Ralf Jung 已提交
343 344
                let field = self.mplace_field(mplace, field)?;
                return Ok(field.into());
M
Mark Rousskov 已提交
345 346
            }
            Err(value) => value,
R
Ralf Jung 已提交
347 348 349
        };

        let field_layout = op.layout.field(self, field)?;
350
        if field_layout.is_zst() {
351
            let immediate = Scalar::zst().into();
352
            return Ok(OpTy { op: Operand::Immediate(immediate), layout: field_layout });
R
Ralf Jung 已提交
353 354
        }
        let offset = op.layout.fields.offset(field);
355
        let immediate = match *base {
R
Ralf Jung 已提交
356
            // the field covers the entire type
357
            _ if offset.bytes() == 0 && field_layout.size == op.layout.size => *base,
R
Ralf Jung 已提交
358
            // extract fields from types with `ScalarPair` ABI
359
            Immediate::ScalarPair(a, b) => {
R
Ralf Jung 已提交
360
                let val = if offset.bytes() == 0 { a } else { b };
361
                Immediate::from(val)
M
Mark Rousskov 已提交
362 363 364 365
            }
            Immediate::Scalar(val) => {
                bug!("field access on non aggregate {:#?}, {:#?}", val, op.layout)
            }
R
Ralf Jung 已提交
366
        };
367
        Ok(OpTy { op: Operand::Immediate(immediate), layout: field_layout })
R
Ralf Jung 已提交
368 369
    }

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
    pub fn operand_index(
        &self,
        op: OpTy<'tcx, M::PointerTag>,
        index: u64,
    ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
        if let Ok(index) = usize::try_from(index) {
            // We can just treat this as a field.
            self.operand_field(op, index)
        } else {
            // Indexing into a big array. This must be an mplace.
            let mplace = op.assert_mem_place(self);
            Ok(self.mplace_index(mplace, index)?.into())
        }
    }

385
    pub fn operand_downcast(
R
Ralf Jung 已提交
386
        &self,
387
        op: OpTy<'tcx, M::PointerTag>,
388
        variant: VariantIdx,
389
    ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
R
Ralf Jung 已提交
390
        // Downcasts only change the layout
391
        Ok(match op.try_as_mplace(self) {
M
Mark Rousskov 已提交
392
            Ok(mplace) => self.mplace_downcast(mplace, variant)?.into(),
R
Ralf Jung 已提交
393 394 395 396 397 398 399 400 401
            Err(..) => {
                let layout = op.layout.for_variant(self, variant);
                OpTy { layout, ..op }
            }
        })
    }

    pub fn operand_projection(
        &self,
402
        base: OpTy<'tcx, M::PointerTag>,
B
Bastian Kauschke 已提交
403
        proj_elem: mir::PlaceElem<'tcx>,
404
    ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
M
Mazdak Farrokhzad 已提交
405
        use rustc_middle::mir::ProjectionElem::*;
B
Bastian Kauschke 已提交
406
        Ok(match proj_elem {
407
            Field(field, _) => self.operand_field(base, field.index())?,
R
Ralf Jung 已提交
408 409
            Downcast(_, variant) => self.operand_downcast(base, variant)?,
            Deref => self.deref_operand(base)?.into(),
M
Mark Rousskov 已提交
410
            Subslice { .. } | ConstantIndex { .. } | Index(_) => {
411 412
                // The rest should only occur as mplace, we do not use Immediates for types
                // allowing such operations.  This matches place_projection forcing an allocation.
413
                let mplace = base.assert_mem_place(self);
R
Ralf Jung 已提交
414 415 416 417 418
                self.mplace_projection(mplace, proj_elem)?.into()
            }
        })
    }

419
    /// This is used by [priroda](https://github.com/oli-obk/priroda) to get an OpTy from a local
O
Oliver Scherer 已提交
420
    pub fn access_local(
421
        &self,
422
        frame: &super::Frame<'mir, 'tcx, M::PointerTag, M::FrameExtra>,
423
        local: mir::Local,
424
        layout: Option<TyAndLayout<'tcx>>,
425
    ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
426
        let layout = self.layout_of_local(frame, local, layout)?;
R
Ralf Jung 已提交
427 428
        let op = if layout.is_zst() {
            // Do not read from ZST, they might not be initialized
429
            Operand::Immediate(Scalar::zst().into())
R
Ralf Jung 已提交
430
        } else {
W
Wesley Wiser 已提交
431
            M::access_local(&self, frame, local)?
R
Ralf Jung 已提交
432
        };
433 434 435
        Ok(OpTy { op, layout })
    }

436 437 438
    /// Every place can be read from, so we can turn them into an operand.
    /// This will definitely return `Indirect` if the place is a `Ptr`, i.e., this
    /// will never actually read from memory.
439 440 441
    #[inline(always)]
    pub fn place_to_op(
        &self,
M
Mark Rousskov 已提交
442
        place: PlaceTy<'tcx, M::PointerTag>,
443
    ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
444
        let op = match *place {
M
Mark Rousskov 已提交
445
            Place::Ptr(mplace) => Operand::Indirect(mplace),
446 447 448
            Place::Local { frame, local } => {
                *self.access_local(&self.stack()[frame], local, None)?
            }
449 450 451 452
        };
        Ok(OpTy { op, layout: place.layout })
    }

R
Ralf Jung 已提交
453
    // Evaluate a place with the goal of reading from it.  This lets us sometimes
454
    // avoid allocations.
455
    pub fn eval_place_to_op(
456
        &self,
457
        place: mir::Place<'tcx>,
458
        layout: Option<TyAndLayout<'tcx>>,
459
    ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
J
Jonas Schievink 已提交
460 461 462
        // Do not use the layout passed in as argument if the base we are looking at
        // here is not the entire place.
        let layout = if place.projection.is_empty() { layout } else { None };
463

J
Jonas Schievink 已提交
464
        let base_op = self.access_local(self.frame(), place.local, layout)?;
465

M
Mark Rousskov 已提交
466 467 468
        let op = place
            .projection
            .iter()
B
Bastian Kauschke 已提交
469
            .try_fold(base_op, |op, elem| self.operand_projection(op, elem))?;
470 471

        trace!("eval_place_to_op: got {:?}", *op);
R
Ralf Jung 已提交
472 473
        // Sanity-check the type we ended up with.
        debug_assert!(mir_assign_valid_types(
R
Ralf Jung 已提交
474
            *self.tcx,
R
Ralf Jung 已提交
475
            self.layout_of(self.subst_from_current_frame_and_normalize_erasing_regions(
R
Ralf Jung 已提交
476
                place.ty(&self.frame().body.local_decls, *self.tcx).ty
R
Ralf Jung 已提交
477 478 479
            ))?,
            op.layout,
        ));
480
        Ok(op)
R
Ralf Jung 已提交
481 482 483
    }

    /// Evaluate the operand, returning a place where you can then find the data.
484
    /// If you already know the layout, you can save two table lookups
485 486
    /// by passing it in here.
    pub fn eval_operand(
487
        &self,
488
        mir_op: &mir::Operand<'tcx>,
489
        layout: Option<TyAndLayout<'tcx>>,
490
    ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
M
Mazdak Farrokhzad 已提交
491
        use rustc_middle::mir::Operand::*;
R
Ralf Jung 已提交
492
        let op = match *mir_op {
R
Ralf Jung 已提交
493
            // FIXME: do some more logic on `move` to invalidate the old location
494
            Copy(place) | Move(place) => self.eval_place_to_op(place, layout)?,
R
Ralf Jung 已提交
495

496
            Constant(ref constant) => {
497 498
                let val =
                    self.subst_from_current_frame_and_normalize_erasing_regions(constant.literal);
499 500
                self.eval_const_to_op(val, layout)?
            }
R
Ralf Jung 已提交
501 502 503
        };
        trace!("{:?}: {:?}", mir_op, *op);
        Ok(op)
R
Ralf Jung 已提交
504 505 506
    }

    /// Evaluate a bunch of operands at once
507
    pub(super) fn eval_operands(
508
        &self,
R
Ralf Jung 已提交
509
        ops: &[mir::Operand<'tcx>],
510
    ) -> InterpResult<'tcx, Vec<OpTy<'tcx, M::PointerTag>>> {
511
        ops.iter().map(|op| self.eval_operand(op, None)).collect()
R
Ralf Jung 已提交
512 513
    }

514 515
    // Used when the miri-engine runs into a constant and for extracting information from constants
    // in patterns via the `const_eval` module
R
Ralf Jung 已提交
516 517
    /// The `val` and `layout` are assumed to already be in our interpreter
    /// "universe" (param_env).
O
Oliver Scherer 已提交
518
    crate fn eval_const_to_op(
519
        &self,
520
        val: &ty::Const<'tcx>,
521
        layout: Option<TyAndLayout<'tcx>>,
522
    ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
523
        let tag_scalar = |scalar| match scalar {
524
            Scalar::Ptr(ptr) => Scalar::Ptr(self.tag_global_base_pointer(ptr)),
525 526 527
            Scalar::Raw { data, size } => Scalar::Raw { data, size },
        };
        // Early-return cases.
C
Camille GILLOT 已提交
528
        let val_val = match val.val {
M
Mark Rousskov 已提交
529
            ty::ConstKind::Param(_) => throw_inval!(TooGeneric),
530
            ty::ConstKind::Error => throw_inval!(TypeckError(ErrorReported)),
531
            ty::ConstKind::Unevaluated(def_id, substs, promoted) => {
O
Oliver Scherer 已提交
532
                let instance = self.resolve(def_id, substs)?;
533 534 535 536
                // We use `const_eval` here and `const_eval_raw` elsewhere in mir interpretation.
                // The reason we use `const_eval_raw` everywhere else is to prevent cycles during
                // validation, because validation automatically reads through any references, thus
                // potentially requiring the current static to be evaluated again. This is not a
O
Oliver Scherer 已提交
537 538
                // problem here, because we are building an operand which means an actual read is
                // happening.
539 540 541 542
                //
                // The machine callback `adjust_global_const` below is guaranteed to
                // be called for all constants because `const_eval` calls
                // `eval_const_to_op` recursively.
543
                return Ok(self.const_eval(GlobalId { instance, promoted }, val.ty)?);
M
Mark Rousskov 已提交
544
            }
O
Oliver Scherer 已提交
545 546 547 548 549
            ty::ConstKind::Infer(..)
            | ty::ConstKind::Bound(..)
            | ty::ConstKind::Placeholder(..) => {
                bug!("eval_const_to_op: Unexpected ConstKind {:?}", val)
            }
R
Ralf Jung 已提交
550
            ty::ConstKind::Value(val_val) => val_val,
C
Camille GILLOT 已提交
551
        };
552 553
        // This call allows the machine to create fresh allocation ids for
        // thread-local statics (see the `adjust_global_const` function
554
        // documentation).
555
        let val_val = M::adjust_global_const(self, val_val)?;
556
        // Other cases need layout.
R
Ralf Jung 已提交
557
        let layout = from_known_layout(self.tcx, layout, || self.layout_of(val.ty))?;
C
Camille GILLOT 已提交
558
        let op = match val_val {
559
            ConstValue::ByRef { alloc, offset } => {
560
                let id = self.tcx.create_memory_alloc(alloc);
561
                // We rely on mutability being set correctly in that allocation to prevent writes
R
Ralf Jung 已提交
562
                // where none should happen.
563
                let ptr = self.tag_global_base_pointer(Pointer::new(id, offset));
564
                Operand::Indirect(MemPlace::from_ptr(ptr, layout.align.abi))
M
Mark Rousskov 已提交
565
            }
566
            ConstValue::Scalar(x) => Operand::Immediate(tag_scalar(x).into()),
567
            ConstValue::Slice { data, start, end } => {
R
Ralf Jung 已提交
568 569
                // We rely on mutability being set correctly in `data` to prevent writes
                // where none should happen.
570
                let ptr = Pointer::new(
571
                    self.tcx.create_memory_alloc(data),
572
                    Size::from_bytes(start), // offset: `start`
573 574
                );
                Operand::Immediate(Immediate::new_slice(
575
                    self.tag_global_base_pointer(ptr).into(),
576
                    u64::try_from(end.checked_sub(start).unwrap()).unwrap(), // len: `end - start`
577 578 579 580 581
                    self,
                ))
            }
        };
        Ok(OpTy { op, layout })
R
Ralf Jung 已提交
582 583
    }

584 585
    /// Read discriminant, return the runtime value as well as the variant index.
    pub fn read_discriminant(
R
Ralf Jung 已提交
586
        &self,
587
        op: OpTy<'tcx, M::PointerTag>,
588
    ) -> InterpResult<'tcx, (Scalar<M::PointerTag>, VariantIdx)> {
589 590 591
        trace!("read_discriminant_value {:#?}", op.layout);

        // Get type and layout of the discriminant.
R
Ralf Jung 已提交
592
        let discr_layout = self.layout_of(op.layout.ty.discriminant_ty(*self.tcx))?;
593
        trace!("discriminant type: {:?}", discr_layout.ty);
R
Ralf Jung 已提交
594

R
Ralf Jung 已提交
595
        // We use "discriminant" to refer to the value associated with a particular enum variant.
R
Ralf Jung 已提交
596 597 598 599 600 601
        // This is not to be confused with its "variant index", which is just determining its position in the
        // declared list of variants -- they can differ with explicitly assigned discriminants.
        // We use "tag" to refer to how the discriminant is encoded in memory, which can be either
        // straight-forward (`DiscriminantKind::Tag`) or with a niche (`DiscriminantKind::Niche`).
        // Unfortunately, the rest of the compiler calls the latter "discriminant", too, which makes things
        // rather confusing.
602
        let (tag_scalar_layout, tag_kind, tag_index) = match op.layout.variants {
603
            Variants::Single { index } => {
R
Ralf Jung 已提交
604
                let discr = match op.layout.ty.discriminant_for_variant(*self.tcx, index) {
605 606
                    Some(discr) => {
                        // This type actually has discriminants.
607
                        assert_eq!(discr.ty, discr_layout.ty);
608 609 610
                        Scalar::from_uint(discr.val, discr_layout.size)
                    }
                    None => {
611
                        // On a type without actual discriminants, variant is 0.
612
                        assert_eq!(index.as_u32(), 0);
613 614 615 616
                        Scalar::from_uint(index.as_u32(), discr_layout.size)
                    }
                };
                return Ok((discr, index));
R
Ralf Jung 已提交
617
            }
618 619
            Variants::Multiple { ref discr, ref discr_kind, discr_index, .. } => {
                (discr, discr_kind, discr_index)
620
            }
621 622
        };

623 624
        // There are *three* layouts that come into play here:
        // - The discriminant has a type for typechecking. This is `discr_layout`, and is used for
R
Ralf Jung 已提交
625
        //   the `Scalar` we return.
626 627 628 629 630
        // - The tag (encoded discriminant) has layout `tag_layout`. This is always an integer type,
        //   and used to interpret the value we read from the tag field.
        //   For the return value, a cast to `discr_layout` is performed.
        // - The field storing the tag has a layout, which is very similar to `tag_layout` but
        //   may be a pointer. This is `tag_val.layout`; we just use it for sanity checks.
631 632

        // Get layout for tag.
R
Ralf Jung 已提交
633
        let tag_layout = self.layout_of(tag_scalar_layout.value.to_int_ty(*self.tcx))?;
634

R
Ralf Jung 已提交
635
        // Read tag and sanity-check `tag_layout`.
636
        let tag_val = self.read_immediate(self.operand_field(op, tag_index)?)?;
R
Ralf Jung 已提交
637 638 639 640
        assert_eq!(tag_layout.size, tag_val.layout.size);
        assert_eq!(tag_layout.abi.is_signed(), tag_val.layout.abi.is_signed());
        let tag_val = tag_val.to_scalar()?;
        trace!("tag value: {:?}", tag_val);
641 642

        // Figure out which discriminant and variant this corresponds to.
R
Ralf Jung 已提交
643
        Ok(match *tag_kind {
644
            DiscriminantKind::Tag => {
R
Ralf Jung 已提交
645 646 647 648
                let tag_bits = self
                    .force_bits(tag_val, tag_layout.size)
                    .map_err(|_| err_ub!(InvalidDiscriminant(tag_val.erase_tag())))?;
                // Cast bits from tag layout to discriminant layout.
649
                let discr_val_cast = self.cast_from_scalar(tag_bits, tag_layout, discr_layout.ty);
R
Ralf Jung 已提交
650 651
                let discr_bits = discr_val_cast.assert_bits(discr_layout.size);
                // Convert discriminant to variant index, and catch invalid discriminants.
652
                let index = match op.layout.ty.kind {
M
Mark Rousskov 已提交
653
                    ty::Adt(adt, _) => {
R
Ralf Jung 已提交
654
                        adt.discriminants(*self.tcx).find(|(_, var)| var.val == discr_bits)
M
Mark Rousskov 已提交
655
                    }
C
csmoe 已提交
656 657 658
                    ty::Generator(def_id, substs, _) => {
                        let substs = substs.as_generator();
                        substs
R
Ralf Jung 已提交
659
                            .discriminants(def_id, *self.tcx)
660
                            .find(|(_, var)| var.val == discr_bits)
C
csmoe 已提交
661
                    }
662
                    _ => bug!("tagged layout for non-adt non-generator"),
M
Mark Rousskov 已提交
663
                }
R
Ralf Jung 已提交
664
                .ok_or_else(|| err_ub!(InvalidDiscriminant(tag_val.erase_tag())))?;
665 666
                // Return the cast value, and the index.
                (discr_val_cast, index.0)
M
Mark Rousskov 已提交
667
            }
668
            DiscriminantKind::Niche { dataful_variant, ref niche_variants, niche_start } => {
R
Ralf Jung 已提交
669 670
                // Compute the variant this niche value/"tag" corresponds to. With niche layout,
                // discriminant (encoded in niche/tag) and variant index are the same.
671 672
                let variants_start = niche_variants.start().as_u32();
                let variants_end = niche_variants.end().as_u32();
R
Ralf Jung 已提交
673
                let variant = match tag_val.to_bits_or_ptr(tag_layout.size, self) {
674
                    Err(ptr) => {
675
                        // The niche must be just 0 (which an inbounds pointer value never is)
M
Mark Rousskov 已提交
676 677 678
                        let ptr_valid = niche_start == 0
                            && variants_start == variants_end
                            && !self.memory.ptr_may_be_null(ptr);
679
                        if !ptr_valid {
R
Ralf Jung 已提交
680
                            throw_ub!(InvalidDiscriminant(tag_val.erase_tag()))
681
                        }
682
                        dataful_variant
M
Mark Rousskov 已提交
683
                    }
R
Ralf Jung 已提交
684
                    Ok(tag_bits) => {
685
                        // We need to use machine arithmetic to get the relative variant idx:
R
Ralf Jung 已提交
686 687 688
                        // variant_index_relative = tag_val - niche_start_val
                        let tag_val = ImmTy::from_uint(tag_bits, tag_layout);
                        let niche_start_val = ImmTy::from_uint(niche_start, tag_layout);
M
Mark Rousskov 已提交
689
                        let variant_index_relative_val =
R
Ralf Jung 已提交
690
                            self.binary_op(mir::BinOp::Sub, tag_val, niche_start_val)?;
691
                        let variant_index_relative = variant_index_relative_val
692
                            .to_scalar()?
R
Ralf Jung 已提交
693
                            .assert_bits(tag_val.layout.size);
694
                        // Check if this is in the range that indicates an actual discriminant.
695 696 697 698 699 700
                        if variant_index_relative <= u128::from(variants_end - variants_start) {
                            let variant_index_relative = u32::try_from(variant_index_relative)
                                .expect("we checked that this fits into a u32");
                            // Then computing the absolute variant idx should not overflow any more.
                            let variant_index = variants_start
                                .checked_add(variant_index_relative)
M
Matthias Krüger 已提交
701
                                .expect("overflow computing absolute variant idx");
702
                            let variants_len = op
R
Ralf Jung 已提交
703 704 705 706 707 708
                                .layout
                                .ty
                                .ty_adt_def()
                                .expect("tagged layout for non adt")
                                .variants
                                .len();
709
                            assert!(usize::try_from(variant_index).unwrap() < variants_len);
710
                            VariantIdx::from_u32(variant_index)
R
Ralf Jung 已提交
711
                        } else {
712
                            dataful_variant
R
Ralf Jung 已提交
713
                        }
M
Mark Rousskov 已提交
714
                    }
715 716
                };
                // Compute the size of the scalar we need to return.
R
Ralf Jung 已提交
717 718
                // No need to cast, because the variant index directly serves as discriminant and is
                // encoded in the tag.
719
                (Scalar::from_uint(variant.as_u32(), discr_layout.size), variant)
R
Ralf Jung 已提交
720 721 722
            }
        })
    }
723
}