1 use std::convert::{TryFrom, TryInto};
5 ieee::{Double, Single},
8 use rustc_macros::HashStable;
9 use rustc_target::abi::{HasDataLayout, Size};
11 use crate::ty::{Lift, ParamEnv, ScalarInt, Ty, TyCtxt};
14 AllocId, AllocRange, ConstAllocation, InterpResult, Pointer, PointerArithmetic, Provenance,
17 /// Represents the result of const evaluation via the `eval_to_allocation` query.
18 #[derive(Copy, Clone, HashStable, TyEncodable, TyDecodable, Debug, Hash, Eq, PartialEq)]
19 pub struct ConstAlloc<'tcx> {
20 // the value lives here, at offset 0, and that allocation definitely is an `AllocKind::Memory`
21 // (so you can use `AllocMap::unwrap_memory`).
22 pub alloc_id: AllocId,
26 /// Represents a constant value in Rust. `Scalar` and `Slice` are optimizations for
27 /// array length computations, enum discriminants and the pattern matching logic.
28 #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Hash)]
30 pub enum ConstValue<'tcx> {
31 /// Used only for types with `layout::abi::Scalar` ABI and ZSTs.
33 /// Not using the enum `Value` to encode that this must not be `Uninit`.
36 /// Used only for `&[u8]` and `&str`
37 Slice { data: ConstAllocation<'tcx>, start: usize, end: usize },
39 /// A value not represented/representable by `Scalar` or `Slice`
41 /// The backing memory of the value, may contain more memory than needed for just the value
42 /// in order to share `ConstAllocation`s between values
43 alloc: ConstAllocation<'tcx>,
44 /// Offset into `alloc`
49 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
50 static_assert_size!(ConstValue<'_>, 32);
52 impl<'a, 'tcx> Lift<'tcx> for ConstValue<'a> {
53 type Lifted = ConstValue<'tcx>;
54 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ConstValue<'tcx>> {
56 ConstValue::Scalar(s) => ConstValue::Scalar(s),
57 ConstValue::Slice { data, start, end } => {
58 ConstValue::Slice { data: tcx.lift(data)?, start, end }
60 ConstValue::ByRef { alloc, offset } => {
61 ConstValue::ByRef { alloc: tcx.lift(alloc)?, offset }
67 impl<'tcx> ConstValue<'tcx> {
69 pub fn try_to_scalar(&self) -> Option<Scalar<AllocId>> {
71 ConstValue::ByRef { .. } | ConstValue::Slice { .. } => None,
72 ConstValue::Scalar(val) => Some(val),
76 pub fn try_to_scalar_int(&self) -> Option<ScalarInt> {
77 Some(self.try_to_scalar()?.assert_int())
80 pub fn try_to_bits(&self, size: Size) -> Option<u128> {
81 self.try_to_scalar_int()?.to_bits(size).ok()
84 pub fn try_to_bool(&self) -> Option<bool> {
85 self.try_to_scalar_int()?.try_into().ok()
88 pub fn try_to_machine_usize(&self, tcx: TyCtxt<'tcx>) -> Option<u64> {
89 self.try_to_scalar_int()?.try_to_machine_usize(tcx).ok()
92 pub fn try_to_bits_for_ty(
95 param_env: ParamEnv<'tcx>,
98 let size = tcx.layout_of(param_env.with_reveal_all_normalized(tcx).and(ty)).ok()?.size;
99 self.try_to_bits(size)
102 pub fn from_bool(b: bool) -> Self {
103 ConstValue::Scalar(Scalar::from_bool(b))
106 pub fn from_u64(i: u64) -> Self {
107 ConstValue::Scalar(Scalar::from_u64(i))
110 pub fn from_machine_usize(i: u64, cx: &impl HasDataLayout) -> Self {
111 ConstValue::Scalar(Scalar::from_machine_usize(i, cx))
115 /// A `Scalar` represents an immediate, primitive value existing outside of a
116 /// `memory::Allocation`. It is in many ways like a small chunk of an `Allocation`, up to 16 bytes in
117 /// size. Like a range of bytes in an `Allocation`, a `Scalar` can either represent the raw bytes
118 /// of a simple value or a pointer into another `Allocation`
120 /// These variants would be private if there was a convenient way to achieve that in Rust.
121 /// Do *not* match on a `Scalar`! Use the various `to_*` methods instead.
122 #[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, TyEncodable, TyDecodable, Hash)]
123 #[derive(HashStable)]
124 pub enum Scalar<Tag = AllocId> {
125 /// The raw bytes of a simple value.
128 /// A pointer into an `Allocation`. An `Allocation` in the `memory` module has a list of
129 /// relocations, but a `Scalar` is only large enough to contain one, so we just represent the
130 /// relocation and its associated offset together as a `Pointer` here.
132 /// We also store the size of the pointer, such that a `Scalar` always knows how big it is.
133 /// The size is always the pointer size of the current target, but this is not information
134 /// that we always have readily available.
135 Ptr(Pointer<Tag>, u8),
138 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
139 static_assert_size!(Scalar, 24);
141 // We want the `Debug` output to be readable as it is used by `derive(Debug)` for
142 // all the Miri types.
143 impl<Tag: Provenance> fmt::Debug for Scalar<Tag> {
144 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
146 Scalar::Ptr(ptr, _size) => write!(f, "{:?}", ptr),
147 Scalar::Int(int) => write!(f, "{:?}", int),
152 impl<Tag: Provenance> fmt::Display for Scalar<Tag> {
153 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
155 Scalar::Ptr(ptr, _size) => write!(f, "pointer to {:?}", ptr),
156 Scalar::Int(int) => write!(f, "{}", int),
161 impl<Tag: Provenance> fmt::LowerHex for Scalar<Tag> {
162 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
164 Scalar::Ptr(ptr, _size) => write!(f, "pointer to {:?}", ptr),
165 Scalar::Int(int) => write!(f, "0x{:x}", int),
170 impl<Tag> From<Single> for Scalar<Tag> {
172 fn from(f: Single) -> Self {
177 impl<Tag> From<Double> for Scalar<Tag> {
179 fn from(f: Double) -> Self {
184 impl<Tag> From<ScalarInt> for Scalar<Tag> {
186 fn from(ptr: ScalarInt) -> Self {
191 impl<Tag> Scalar<Tag> {
192 pub const ZST: Self = Scalar::Int(ScalarInt::ZST);
195 pub fn from_pointer(ptr: Pointer<Tag>, cx: &impl HasDataLayout) -> Self {
196 Scalar::Ptr(ptr, u8::try_from(cx.pointer_size().bytes()).unwrap())
199 /// Create a Scalar from a pointer with an `Option<_>` tag (where `None` represents a plain integer).
200 pub fn from_maybe_pointer(ptr: Pointer<Option<Tag>>, cx: &impl HasDataLayout) -> Self {
201 match ptr.into_parts() {
202 (Some(tag), offset) => Scalar::from_pointer(Pointer::new(tag, offset), cx),
204 Scalar::Int(ScalarInt::try_from_uint(offset.bytes(), cx.pointer_size()).unwrap())
210 pub fn null_ptr(cx: &impl HasDataLayout) -> Self {
211 Scalar::Int(ScalarInt::null(cx.pointer_size()))
215 pub fn from_bool(b: bool) -> Self {
216 Scalar::Int(b.into())
220 pub fn from_char(c: char) -> Self {
221 Scalar::Int(c.into())
225 pub fn try_from_uint(i: impl Into<u128>, size: Size) -> Option<Self> {
226 ScalarInt::try_from_uint(i, size).map(Scalar::Int)
230 pub fn from_uint(i: impl Into<u128>, size: Size) -> Self {
232 Self::try_from_uint(i, size)
233 .unwrap_or_else(|| bug!("Unsigned value {:#x} does not fit in {} bits", i, size.bits()))
237 pub fn from_u8(i: u8) -> Self {
238 Scalar::Int(i.into())
242 pub fn from_u16(i: u16) -> Self {
243 Scalar::Int(i.into())
247 pub fn from_u32(i: u32) -> Self {
248 Scalar::Int(i.into())
252 pub fn from_u64(i: u64) -> Self {
253 Scalar::Int(i.into())
257 pub fn from_machine_usize(i: u64, cx: &impl HasDataLayout) -> Self {
258 Self::from_uint(i, cx.data_layout().pointer_size)
262 pub fn try_from_int(i: impl Into<i128>, size: Size) -> Option<Self> {
263 ScalarInt::try_from_int(i, size).map(Scalar::Int)
267 pub fn from_int(i: impl Into<i128>, size: Size) -> Self {
269 Self::try_from_int(i, size)
270 .unwrap_or_else(|| bug!("Signed value {:#x} does not fit in {} bits", i, size.bits()))
274 pub fn from_i32(i: i32) -> Self {
275 Self::from_int(i, Size::from_bits(32))
279 pub fn from_i64(i: i64) -> Self {
280 Self::from_int(i, Size::from_bits(64))
284 pub fn from_machine_isize(i: i64, cx: &impl HasDataLayout) -> Self {
285 Self::from_int(i, cx.data_layout().pointer_size)
289 pub fn from_f32(f: Single) -> Self {
290 Scalar::Int(f.into())
294 pub fn from_f64(f: Double) -> Self {
295 Scalar::Int(f.into())
298 /// This is almost certainly not the method you want! You should dispatch on the type
299 /// and use `to_{u8,u16,...}`/`scalar_to_ptr` to perform ptr-to-int / int-to-ptr casts as needed.
301 /// This method only exists for the benefit of low-level operations that truly need to treat the
302 /// scalar in whatever form it is.
304 pub fn to_bits_or_ptr_internal(self, target_size: Size) -> Result<u128, Pointer<Tag>> {
305 assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
307 Scalar::Int(int) => Ok(int.assert_bits(target_size)),
308 Scalar::Ptr(ptr, sz) => {
309 assert_eq!(target_size.bytes(), u64::from(sz));
316 impl<'tcx, Tag: Provenance> Scalar<Tag> {
317 /// Fundamental scalar-to-int (cast) operation. Many convenience wrappers exist below, that you
318 /// likely want to use instead.
320 /// Will perform ptr-to-int casts if needed and possible.
321 /// If that fails, we know the offset is relative, so we return an "erased" Scalar
322 /// (which is useful for error messages but not much else).
324 pub fn try_to_int(self) -> Result<ScalarInt, Scalar<AllocId>> {
326 Scalar::Int(int) => Ok(int),
327 Scalar::Ptr(ptr, sz) => {
328 if Tag::OFFSET_IS_ADDR {
329 Ok(ScalarInt::try_from_uint(ptr.offset.bytes(), Size::from_bytes(sz)).unwrap())
331 // We know `offset` is relative, since `OFFSET_IS_ADDR == false`.
332 let (tag, offset) = ptr.into_parts();
333 Err(Scalar::Ptr(Pointer::new(tag.get_alloc_id(), offset), sz))
340 pub fn assert_int(self) -> ScalarInt {
341 self.try_to_int().unwrap()
344 /// This throws UB (instead of ICEing) on a size mismatch since size mismatches can arise in
345 /// Miri when someone declares a function that we shim (such as `malloc`) with a wrong type.
347 pub fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
348 assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
349 self.try_to_int().map_err(|_| err_unsup!(ReadPointerAsBytes))?.to_bits(target_size).map_err(
351 err_ub!(ScalarSizeMismatch {
352 target_size: target_size.bytes(),
353 data_size: size.bytes(),
361 pub fn assert_bits(self, target_size: Size) -> u128 {
362 self.to_bits(target_size).unwrap()
365 pub fn to_bool(self) -> InterpResult<'tcx, bool> {
366 let val = self.to_u8()?;
370 _ => throw_ub!(InvalidBool(val)),
374 pub fn to_char(self) -> InterpResult<'tcx, char> {
375 let val = self.to_u32()?;
376 match std::char::from_u32(val) {
378 None => throw_ub!(InvalidChar(val)),
382 /// Converts the scalar to produce an unsigned integer of the given size.
383 /// Fails if the scalar is a pointer.
385 pub fn to_uint(self, size: Size) -> InterpResult<'static, u128> {
389 /// Converts the scalar to produce a `u8`. Fails if the scalar is a pointer.
390 pub fn to_u8(self) -> InterpResult<'static, u8> {
391 self.to_uint(Size::from_bits(8)).map(|v| u8::try_from(v).unwrap())
394 /// Converts the scalar to produce a `u16`. Fails if the scalar is a pointer.
395 pub fn to_u16(self) -> InterpResult<'static, u16> {
396 self.to_uint(Size::from_bits(16)).map(|v| u16::try_from(v).unwrap())
399 /// Converts the scalar to produce a `u32`. Fails if the scalar is a pointer.
400 pub fn to_u32(self) -> InterpResult<'static, u32> {
401 self.to_uint(Size::from_bits(32)).map(|v| u32::try_from(v).unwrap())
404 /// Converts the scalar to produce a `u64`. Fails if the scalar is a pointer.
405 pub fn to_u64(self) -> InterpResult<'static, u64> {
406 self.to_uint(Size::from_bits(64)).map(|v| u64::try_from(v).unwrap())
409 /// Converts the scalar to produce a `u128`. Fails if the scalar is a pointer.
410 pub fn to_u128(self) -> InterpResult<'static, u128> {
411 self.to_uint(Size::from_bits(128))
414 /// Converts the scalar to produce a machine-pointer-sized unsigned integer.
415 /// Fails if the scalar is a pointer.
416 pub fn to_machine_usize(self, cx: &impl HasDataLayout) -> InterpResult<'static, u64> {
417 let b = self.to_uint(cx.data_layout().pointer_size)?;
418 Ok(u64::try_from(b).unwrap())
421 /// Converts the scalar to produce a signed integer of the given size.
422 /// Fails if the scalar is a pointer.
424 pub fn to_int(self, size: Size) -> InterpResult<'static, i128> {
425 let b = self.to_bits(size)?;
426 Ok(size.sign_extend(b) as i128)
429 /// Converts the scalar to produce an `i8`. Fails if the scalar is a pointer.
430 pub fn to_i8(self) -> InterpResult<'static, i8> {
431 self.to_int(Size::from_bits(8)).map(|v| i8::try_from(v).unwrap())
434 /// Converts the scalar to produce an `i16`. Fails if the scalar is a pointer.
435 pub fn to_i16(self) -> InterpResult<'static, i16> {
436 self.to_int(Size::from_bits(16)).map(|v| i16::try_from(v).unwrap())
439 /// Converts the scalar to produce an `i32`. Fails if the scalar is a pointer.
440 pub fn to_i32(self) -> InterpResult<'static, i32> {
441 self.to_int(Size::from_bits(32)).map(|v| i32::try_from(v).unwrap())
444 /// Converts the scalar to produce an `i64`. Fails if the scalar is a pointer.
445 pub fn to_i64(self) -> InterpResult<'static, i64> {
446 self.to_int(Size::from_bits(64)).map(|v| i64::try_from(v).unwrap())
449 /// Converts the scalar to produce an `i128`. Fails if the scalar is a pointer.
450 pub fn to_i128(self) -> InterpResult<'static, i128> {
451 self.to_int(Size::from_bits(128))
454 /// Converts the scalar to produce a machine-pointer-sized signed integer.
455 /// Fails if the scalar is a pointer.
456 pub fn to_machine_isize(self, cx: &impl HasDataLayout) -> InterpResult<'static, i64> {
457 let b = self.to_int(cx.data_layout().pointer_size)?;
458 Ok(i64::try_from(b).unwrap())
462 pub fn to_f32(self) -> InterpResult<'static, Single> {
463 // Going through `u32` to check size and truncation.
464 Ok(Single::from_bits(self.to_u32()?.into()))
468 pub fn to_f64(self) -> InterpResult<'static, Double> {
469 // Going through `u64` to check size and truncation.
470 Ok(Double::from_bits(self.to_u64()?.into()))
474 #[derive(Clone, Copy, Eq, PartialEq, TyEncodable, TyDecodable, HashStable, Hash)]
475 pub enum ScalarMaybeUninit<Tag = AllocId> {
480 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
481 static_assert_size!(ScalarMaybeUninit, 24);
483 impl<Tag> From<Scalar<Tag>> for ScalarMaybeUninit<Tag> {
485 fn from(s: Scalar<Tag>) -> Self {
486 ScalarMaybeUninit::Scalar(s)
490 // We want the `Debug` output to be readable as it is used by `derive(Debug)` for
491 // all the Miri types.
492 impl<Tag: Provenance> fmt::Debug for ScalarMaybeUninit<Tag> {
493 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
495 ScalarMaybeUninit::Uninit => write!(f, "<uninitialized>"),
496 ScalarMaybeUninit::Scalar(s) => write!(f, "{:?}", s),
501 impl<Tag: Provenance> fmt::LowerHex for ScalarMaybeUninit<Tag> {
502 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
504 ScalarMaybeUninit::Uninit => write!(f, "uninitialized bytes"),
505 ScalarMaybeUninit::Scalar(s) => write!(f, "{:x}", s),
510 impl<Tag> ScalarMaybeUninit<Tag> {
512 pub fn from_pointer(ptr: Pointer<Tag>, cx: &impl HasDataLayout) -> Self {
513 ScalarMaybeUninit::Scalar(Scalar::from_pointer(ptr, cx))
517 pub fn from_maybe_pointer(ptr: Pointer<Option<Tag>>, cx: &impl HasDataLayout) -> Self {
518 ScalarMaybeUninit::Scalar(Scalar::from_maybe_pointer(ptr, cx))
522 pub fn check_init(self) -> InterpResult<'static, Scalar<Tag>> {
524 ScalarMaybeUninit::Scalar(scalar) => Ok(scalar),
525 ScalarMaybeUninit::Uninit => throw_ub!(InvalidUninitBytes(None)),
530 impl<'tcx, Tag: Provenance> ScalarMaybeUninit<Tag> {
532 pub fn to_bool(self) -> InterpResult<'tcx, bool> {
533 self.check_init()?.to_bool()
537 pub fn to_char(self) -> InterpResult<'tcx, char> {
538 self.check_init()?.to_char()
542 pub fn to_f32(self) -> InterpResult<'tcx, Single> {
543 self.check_init()?.to_f32()
547 pub fn to_f64(self) -> InterpResult<'tcx, Double> {
548 self.check_init()?.to_f64()
552 pub fn to_u8(self) -> InterpResult<'tcx, u8> {
553 self.check_init()?.to_u8()
557 pub fn to_u16(self) -> InterpResult<'tcx, u16> {
558 self.check_init()?.to_u16()
562 pub fn to_u32(self) -> InterpResult<'tcx, u32> {
563 self.check_init()?.to_u32()
567 pub fn to_u64(self) -> InterpResult<'tcx, u64> {
568 self.check_init()?.to_u64()
572 pub fn to_machine_usize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, u64> {
573 self.check_init()?.to_machine_usize(cx)
577 pub fn to_i8(self) -> InterpResult<'tcx, i8> {
578 self.check_init()?.to_i8()
582 pub fn to_i16(self) -> InterpResult<'tcx, i16> {
583 self.check_init()?.to_i16()
587 pub fn to_i32(self) -> InterpResult<'tcx, i32> {
588 self.check_init()?.to_i32()
592 pub fn to_i64(self) -> InterpResult<'tcx, i64> {
593 self.check_init()?.to_i64()
597 pub fn to_machine_isize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, i64> {
598 self.check_init()?.to_machine_isize(cx)
602 /// Gets the bytes of a constant slice value.
603 pub fn get_slice_bytes<'tcx>(cx: &impl HasDataLayout, val: ConstValue<'tcx>) -> &'tcx [u8] {
604 if let ConstValue::Slice { data, start, end } = val {
605 let len = end - start;
609 AllocRange { start: Size::from_bytes(start), size: Size::from_bytes(len) },
611 .unwrap_or_else(|err| bug!("const slice is invalid: {:?}", err))
613 bug!("expected const slice, but found another const value");