rustc_middle/mir/interpret/
value.rs

1use std::fmt;
2
3use either::{Either, Left, Right};
4use rustc_abi::{HasDataLayout, Size};
5use rustc_apfloat::Float;
6use rustc_apfloat::ieee::{Double, Half, Quad, Single};
7use rustc_macros::{HashStable, TyDecodable, TyEncodable};
8
9use super::{
10    AllocId, CtfeProvenance, InterpResult, Pointer, PointerArithmetic, Provenance,
11    ScalarSizeMismatch, interp_ok,
12};
13use crate::ty::ScalarInt;
14
15/// A `Scalar` represents an immediate, primitive value existing outside of a
16/// `memory::Allocation`. It is in many ways like a small chunk of an `Allocation`, up to 16 bytes in
17/// size. Like a range of bytes in an `Allocation`, a `Scalar` can either represent the raw bytes
18/// of a simple value or a pointer into another `Allocation`
19///
20/// These variants would be private if there was a convenient way to achieve that in Rust.
21/// Do *not* match on a `Scalar`! Use the various `to_*` methods instead.
22#[derive(Clone, Copy, Eq, PartialEq, TyEncodable, TyDecodable, Hash)]
23#[derive(HashStable)]
24pub enum Scalar<Prov = CtfeProvenance> {
25    /// The raw bytes of a simple value.
26    Int(ScalarInt),
27
28    /// A pointer.
29    ///
30    /// We also store the size of the pointer, such that a `Scalar` always knows how big it is.
31    /// The size is always the pointer size of the current target, but this is not information
32    /// that we always have readily available.
33    Ptr(Pointer<Prov>, u8),
34}
35
36#[cfg(target_pointer_width = "64")]
37rustc_data_structures::static_assert_size!(Scalar, 24);
38
39// We want the `Debug` output to be readable as it is used by `derive(Debug)` for
40// all the Miri types.
41impl<Prov: Provenance> fmt::Debug for Scalar<Prov> {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        match self {
44            Scalar::Ptr(ptr, _size) => write!(f, "{ptr:?}"),
45            Scalar::Int(int) => write!(f, "{int:?}"),
46        }
47    }
48}
49
50impl<Prov: Provenance> fmt::Display for Scalar<Prov> {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        match self {
53            Scalar::Ptr(ptr, _size) => write!(f, "pointer to {ptr:?}"),
54            Scalar::Int(int) => write!(f, "{int}"),
55        }
56    }
57}
58
59impl<Prov: Provenance> fmt::LowerHex for Scalar<Prov> {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        match self {
62            Scalar::Ptr(ptr, _size) => write!(f, "pointer to {ptr:?}"),
63            Scalar::Int(int) => write!(f, "{int:#x}"),
64        }
65    }
66}
67
68impl<Prov> From<Half> for Scalar<Prov> {
69    #[inline(always)]
70    fn from(f: Half) -> Self {
71        Scalar::from_f16(f)
72    }
73}
74
75impl<Prov> From<Single> for Scalar<Prov> {
76    #[inline(always)]
77    fn from(f: Single) -> Self {
78        Scalar::from_f32(f)
79    }
80}
81
82impl<Prov> From<Double> for Scalar<Prov> {
83    #[inline(always)]
84    fn from(f: Double) -> Self {
85        Scalar::from_f64(f)
86    }
87}
88
89impl<Prov> From<Quad> for Scalar<Prov> {
90    #[inline(always)]
91    fn from(f: Quad) -> Self {
92        Scalar::from_f128(f)
93    }
94}
95
96impl<Prov> From<ScalarInt> for Scalar<Prov> {
97    #[inline(always)]
98    fn from(ptr: ScalarInt) -> Self {
99        Scalar::Int(ptr)
100    }
101}
102
103impl<Prov> Scalar<Prov> {
104    #[inline(always)]
105    pub fn from_pointer(ptr: Pointer<Prov>, cx: &impl HasDataLayout) -> Self {
106        Scalar::Ptr(ptr, u8::try_from(cx.pointer_size().bytes()).unwrap())
107    }
108
109    /// Create a Scalar from a pointer with an `Option<_>` provenance (where `None` represents a
110    /// plain integer / "invalid" pointer).
111    pub fn from_maybe_pointer(ptr: Pointer<Option<Prov>>, cx: &impl HasDataLayout) -> Self {
112        match ptr.into_parts() {
113            (Some(prov), offset) => Scalar::from_pointer(Pointer::new(prov, offset), cx),
114            (None, offset) => {
115                Scalar::Int(ScalarInt::try_from_uint(offset.bytes(), cx.pointer_size()).unwrap())
116            }
117        }
118    }
119
120    #[inline]
121    pub fn null_ptr(cx: &impl HasDataLayout) -> Self {
122        Scalar::Int(ScalarInt::null(cx.pointer_size()))
123    }
124
125    #[inline]
126    pub fn from_bool(b: bool) -> Self {
127        Scalar::Int(b.into())
128    }
129
130    #[inline]
131    pub fn from_char(c: char) -> Self {
132        Scalar::Int(c.into())
133    }
134
135    #[inline]
136    pub fn from_uint(i: impl Into<u128>, size: Size) -> Self {
137        let i = i.into();
138        ScalarInt::try_from_uint(i, size)
139            .unwrap_or_else(|| bug!("Unsigned value {:#x} does not fit in {} bits", i, size.bits()))
140            .into()
141    }
142
143    #[inline]
144    pub fn from_u8(i: u8) -> Self {
145        Scalar::Int(i.into())
146    }
147
148    #[inline]
149    pub fn from_u16(i: u16) -> Self {
150        Scalar::Int(i.into())
151    }
152
153    #[inline]
154    pub fn from_u32(i: u32) -> Self {
155        Scalar::Int(i.into())
156    }
157
158    #[inline]
159    pub fn from_u64(i: u64) -> Self {
160        Scalar::Int(i.into())
161    }
162
163    #[inline]
164    pub fn from_u128(i: u128) -> Self {
165        Scalar::Int(i.into())
166    }
167
168    #[inline]
169    pub fn from_target_usize(i: u64, cx: &impl HasDataLayout) -> Self {
170        Self::from_uint(i, cx.data_layout().pointer_size)
171    }
172
173    #[inline]
174    pub fn from_int(i: impl Into<i128>, size: Size) -> Self {
175        let i = i.into();
176        ScalarInt::try_from_int(i, size)
177            .unwrap_or_else(|| bug!("Signed value {:#x} does not fit in {} bits", i, size.bits()))
178            .into()
179    }
180
181    #[inline]
182    pub fn from_i8(i: i8) -> Self {
183        Self::from_int(i, Size::from_bits(8))
184    }
185
186    #[inline]
187    pub fn from_i16(i: i16) -> Self {
188        Self::from_int(i, Size::from_bits(16))
189    }
190
191    #[inline]
192    pub fn from_i32(i: i32) -> Self {
193        Self::from_int(i, Size::from_bits(32))
194    }
195
196    #[inline]
197    pub fn from_i64(i: i64) -> Self {
198        Self::from_int(i, Size::from_bits(64))
199    }
200
201    #[inline]
202    pub fn from_i128(i: i128) -> Self {
203        Self::from_int(i, Size::from_bits(128))
204    }
205
206    #[inline]
207    pub fn from_target_isize(i: i64, cx: &impl HasDataLayout) -> Self {
208        Self::from_int(i, cx.data_layout().pointer_size)
209    }
210
211    #[inline]
212    pub fn from_f16(f: Half) -> Self {
213        Scalar::Int(f.into())
214    }
215
216    #[inline]
217    pub fn from_f32(f: Single) -> Self {
218        Scalar::Int(f.into())
219    }
220
221    #[inline]
222    pub fn from_f64(f: Double) -> Self {
223        Scalar::Int(f.into())
224    }
225
226    #[inline]
227    pub fn from_f128(f: Quad) -> Self {
228        Scalar::Int(f.into())
229    }
230
231    /// This is almost certainly not the method you want!  You should dispatch on the type
232    /// and use `to_{u8,u16,...}`/`to_pointer` to perform ptr-to-int / int-to-ptr casts as needed.
233    ///
234    /// This method only exists for the benefit of low-level operations that truly need to treat the
235    /// scalar in whatever form it is.
236    ///
237    /// This throws UB (instead of ICEing) on a size mismatch since size mismatches can arise in
238    /// Miri when someone declares a function that we shim (such as `malloc`) with a wrong type.
239    #[inline]
240    pub fn to_bits_or_ptr_internal(
241        self,
242        target_size: Size,
243    ) -> Result<Either<u128, Pointer<Prov>>, ScalarSizeMismatch> {
244        assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
245        Ok(match self {
246            Scalar::Int(int) => Left(int.try_to_bits(target_size).map_err(|size| {
247                ScalarSizeMismatch { target_size: target_size.bytes(), data_size: size.bytes() }
248            })?),
249            Scalar::Ptr(ptr, sz) => {
250                if target_size.bytes() != u64::from(sz) {
251                    return Err(ScalarSizeMismatch {
252                        target_size: target_size.bytes(),
253                        data_size: sz.into(),
254                    });
255                }
256                Right(ptr)
257            }
258        })
259    }
260
261    #[inline]
262    pub fn size(self) -> Size {
263        match self {
264            Scalar::Int(int) => int.size(),
265            Scalar::Ptr(_ptr, sz) => Size::from_bytes(sz),
266        }
267    }
268}
269
270impl<'tcx, Prov: Provenance> Scalar<Prov> {
271    pub fn to_pointer(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, Pointer<Option<Prov>>> {
272        match self
273            .to_bits_or_ptr_internal(cx.pointer_size())
274            .map_err(|s| err_ub!(ScalarSizeMismatch(s)))?
275        {
276            Right(ptr) => interp_ok(ptr.into()),
277            Left(bits) => {
278                let addr = u64::try_from(bits).unwrap();
279                interp_ok(Pointer::from_addr_invalid(addr))
280            }
281        }
282    }
283
284    /// Fundamental scalar-to-int (cast) operation. Many convenience wrappers exist below, that you
285    /// likely want to use instead.
286    ///
287    /// Will perform ptr-to-int casts if needed and possible.
288    /// If that fails, we know the offset is relative, so we return an "erased" Scalar
289    /// (which is useful for error messages but not much else).
290    ///
291    /// The error type is `AllocId`, not `CtfeProvenance`, since `AllocId` is the "minimal"
292    /// component all provenance types must have.
293    #[inline]
294    pub fn try_to_scalar_int(self) -> Result<ScalarInt, Scalar<AllocId>> {
295        match self {
296            Scalar::Int(int) => Ok(int),
297            Scalar::Ptr(ptr, sz) => {
298                if Prov::OFFSET_IS_ADDR {
299                    Ok(ScalarInt::try_from_uint(ptr.offset.bytes(), Size::from_bytes(sz)).unwrap())
300                } else {
301                    // We know `offset` is relative, since `OFFSET_IS_ADDR == false`.
302                    let (prov, offset) = ptr.into_parts();
303                    // Because `OFFSET_IS_ADDR == false`, this unwrap can never fail.
304                    Err(Scalar::Ptr(Pointer::new(prov.get_alloc_id().unwrap(), offset), sz))
305                }
306            }
307        }
308    }
309
310    pub fn clear_provenance(&mut self) -> InterpResult<'tcx> {
311        if matches!(self, Scalar::Ptr(..)) {
312            *self = self.to_scalar_int()?.into();
313        }
314        interp_ok(())
315    }
316
317    #[inline(always)]
318    pub fn to_scalar_int(self) -> InterpResult<'tcx, ScalarInt> {
319        self.try_to_scalar_int().map_err(|_| err_unsup!(ReadPointerAsInt(None))).into()
320    }
321
322    #[inline(always)]
323    #[cfg_attr(debug_assertions, track_caller)] // only in debug builds due to perf (see #98980)
324    pub fn assert_scalar_int(self) -> ScalarInt {
325        self.try_to_scalar_int().expect("got a pointer where a ScalarInt was expected")
326    }
327
328    /// This throws UB (instead of ICEing) on a size mismatch since size mismatches can arise in
329    /// Miri when someone declares a function that we shim (such as `malloc`) with a wrong type.
330    #[inline]
331    pub fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
332        assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
333        self.to_scalar_int()?
334            .try_to_bits(target_size)
335            .map_err(|size| {
336                err_ub!(ScalarSizeMismatch(ScalarSizeMismatch {
337                    target_size: target_size.bytes(),
338                    data_size: size.bytes(),
339                }))
340            })
341            .into()
342    }
343
344    pub fn to_bool(self) -> InterpResult<'tcx, bool> {
345        let val = self.to_u8()?;
346        match val {
347            0 => interp_ok(false),
348            1 => interp_ok(true),
349            _ => throw_ub!(InvalidBool(val)),
350        }
351    }
352
353    pub fn to_char(self) -> InterpResult<'tcx, char> {
354        let val = self.to_u32()?;
355        match std::char::from_u32(val) {
356            Some(c) => interp_ok(c),
357            None => throw_ub!(InvalidChar(val)),
358        }
359    }
360
361    /// Converts the scalar to produce an unsigned integer of the given size.
362    /// Fails if the scalar is a pointer.
363    #[inline]
364    pub fn to_uint(self, size: Size) -> InterpResult<'tcx, u128> {
365        self.to_bits(size)
366    }
367
368    /// Converts the scalar to produce a `u8`. Fails if the scalar is a pointer.
369    pub fn to_u8(self) -> InterpResult<'tcx, u8> {
370        self.to_uint(Size::from_bits(8)).map(|v| u8::try_from(v).unwrap())
371    }
372
373    /// Converts the scalar to produce a `u16`. Fails if the scalar is a pointer.
374    pub fn to_u16(self) -> InterpResult<'tcx, u16> {
375        self.to_uint(Size::from_bits(16)).map(|v| u16::try_from(v).unwrap())
376    }
377
378    /// Converts the scalar to produce a `u32`. Fails if the scalar is a pointer.
379    pub fn to_u32(self) -> InterpResult<'tcx, u32> {
380        self.to_uint(Size::from_bits(32)).map(|v| u32::try_from(v).unwrap())
381    }
382
383    /// Converts the scalar to produce a `u64`. Fails if the scalar is a pointer.
384    pub fn to_u64(self) -> InterpResult<'tcx, u64> {
385        self.to_uint(Size::from_bits(64)).map(|v| u64::try_from(v).unwrap())
386    }
387
388    /// Converts the scalar to produce a `u128`. Fails if the scalar is a pointer.
389    pub fn to_u128(self) -> InterpResult<'tcx, u128> {
390        self.to_uint(Size::from_bits(128))
391    }
392
393    /// Converts the scalar to produce a machine-pointer-sized unsigned integer.
394    /// Fails if the scalar is a pointer.
395    pub fn to_target_usize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, u64> {
396        let b = self.to_uint(cx.data_layout().pointer_size)?;
397        interp_ok(u64::try_from(b).unwrap())
398    }
399
400    /// Converts the scalar to produce a signed integer of the given size.
401    /// Fails if the scalar is a pointer.
402    #[inline]
403    pub fn to_int(self, size: Size) -> InterpResult<'tcx, i128> {
404        let b = self.to_bits(size)?;
405        interp_ok(size.sign_extend(b))
406    }
407
408    /// Converts the scalar to produce an `i8`. Fails if the scalar is a pointer.
409    pub fn to_i8(self) -> InterpResult<'tcx, i8> {
410        self.to_int(Size::from_bits(8)).map(|v| i8::try_from(v).unwrap())
411    }
412
413    /// Converts the scalar to produce an `i16`. Fails if the scalar is a pointer.
414    pub fn to_i16(self) -> InterpResult<'tcx, i16> {
415        self.to_int(Size::from_bits(16)).map(|v| i16::try_from(v).unwrap())
416    }
417
418    /// Converts the scalar to produce an `i32`. Fails if the scalar is a pointer.
419    pub fn to_i32(self) -> InterpResult<'tcx, i32> {
420        self.to_int(Size::from_bits(32)).map(|v| i32::try_from(v).unwrap())
421    }
422
423    /// Converts the scalar to produce an `i64`. Fails if the scalar is a pointer.
424    pub fn to_i64(self) -> InterpResult<'tcx, i64> {
425        self.to_int(Size::from_bits(64)).map(|v| i64::try_from(v).unwrap())
426    }
427
428    /// Converts the scalar to produce an `i128`. Fails if the scalar is a pointer.
429    pub fn to_i128(self) -> InterpResult<'tcx, i128> {
430        self.to_int(Size::from_bits(128))
431    }
432
433    /// Converts the scalar to produce a machine-pointer-sized signed integer.
434    /// Fails if the scalar is a pointer.
435    pub fn to_target_isize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, i64> {
436        let b = self.to_int(cx.data_layout().pointer_size)?;
437        interp_ok(i64::try_from(b).unwrap())
438    }
439
440    #[inline]
441    pub fn to_float<F: Float>(self) -> InterpResult<'tcx, F> {
442        // Going through `to_bits` to check size and truncation.
443        interp_ok(F::from_bits(self.to_bits(Size::from_bits(F::BITS))?))
444    }
445
446    #[inline]
447    pub fn to_f16(self) -> InterpResult<'tcx, Half> {
448        self.to_float()
449    }
450
451    #[inline]
452    pub fn to_f32(self) -> InterpResult<'tcx, Single> {
453        self.to_float()
454    }
455
456    #[inline]
457    pub fn to_f64(self) -> InterpResult<'tcx, Double> {
458        self.to_float()
459    }
460
461    #[inline]
462    pub fn to_f128(self) -> InterpResult<'tcx, Quad> {
463        self.to_float()
464    }
465}