1 use std::convert::TryFrom;
5 ieee::{Double, Single},
8 use rustc_macros::HashStable;
9 use rustc_target::abi::{HasDataLayout, Size, TargetDataLayout};
11 use crate::ty::{ParamEnv, Ty, TyCtxt};
13 use super::{sign_extend, truncate, AllocId, Allocation, InterpResult, Pointer, PointerArithmetic};
15 /// Represents the result of a raw const operation, pre-validation.
16 #[derive(Clone, HashStable)]
17 pub struct RawConst<'tcx> {
18 // the value lives here, at offset 0, and that allocation definitely is a `AllocKind::Memory`
19 // (so you can use `AllocMap::unwrap_memory`).
20 pub alloc_id: AllocId,
24 /// Represents a constant value in Rust. `Scalar` and `Slice` are optimizations for
25 /// array length computations, enum discriminants and the pattern matching logic.
26 #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, RustcEncodable, RustcDecodable, Hash)]
28 pub enum ConstValue<'tcx> {
29 /// Used only for types with `layout::abi::Scalar` ABI and ZSTs.
31 /// Not using the enum `Value` to encode that this must not be `Uninit`.
34 /// Used only for `&[u8]` and `&str`
35 Slice { data: &'tcx Allocation, start: usize, end: usize },
37 /// A value not represented/representable by `Scalar` or `Slice`
39 /// The backing memory of the value, may contain more memory than needed for just the value
40 /// in order to share `Allocation`s between values
41 alloc: &'tcx Allocation,
42 /// Offset into `alloc`
47 #[cfg(target_arch = "x86_64")]
48 static_assert_size!(ConstValue<'_>, 32);
50 impl<'tcx> ConstValue<'tcx> {
52 pub fn try_to_scalar(&self) -> Option<Scalar> {
54 ConstValue::ByRef { .. } | ConstValue::Slice { .. } => None,
55 ConstValue::Scalar(val) => Some(val),
59 pub fn try_to_str_slice(&self) -> Option<&'tcx str> {
60 if let ConstValue::Slice { data, start, end } = *self {
61 ::std::str::from_utf8(data.inspect_with_undef_and_ptr_outside_interpreter(start..end))
68 pub fn try_to_bits(&self, size: Size) -> Option<u128> {
69 self.try_to_scalar()?.to_bits(size).ok()
72 pub fn try_to_bool(&self) -> Option<bool> {
73 match self.try_to_bits(Size::from_bytes(1))? {
80 pub fn try_to_machine_usize(&self, tcx: TyCtxt<'tcx>) -> Option<u64> {
81 Some(self.try_to_bits(tcx.data_layout.pointer_size)? as u64)
84 pub fn try_to_bits_for_ty(
87 param_env: ParamEnv<'tcx>,
90 let size = tcx.layout_of(param_env.with_reveal_all_normalized(tcx).and(ty)).ok()?.size;
91 self.try_to_bits(size)
94 pub fn from_bool(b: bool) -> Self {
95 ConstValue::Scalar(Scalar::from_bool(b))
98 pub fn from_u64(i: u64) -> Self {
99 ConstValue::Scalar(Scalar::from_u64(i))
102 pub fn from_machine_usize(i: u64, cx: &impl HasDataLayout) -> Self {
103 ConstValue::Scalar(Scalar::from_machine_usize(i, cx))
107 /// A `Scalar` represents an immediate, primitive value existing outside of a
108 /// `memory::Allocation`. It is in many ways like a small chunk of a `Allocation`, up to 8 bytes in
109 /// size. Like a range of bytes in an `Allocation`, a `Scalar` can either represent the raw bytes
110 /// of a simple value or a pointer into another `Allocation`
111 #[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, RustcEncodable, RustcDecodable, Hash)]
112 #[derive(HashStable)]
113 pub enum Scalar<Tag = ()> {
114 /// The raw bytes of a simple value.
116 /// The first `size` bytes of `data` are the value.
117 /// Do not try to read less or more bytes than that. The remaining bytes must be 0.
122 /// A pointer into an `Allocation`. An `Allocation` in the `memory` module has a list of
123 /// relocations, but a `Scalar` is only large enough to contain one, so we just represent the
124 /// relocation and its associated offset together as a `Pointer` here.
128 #[cfg(target_arch = "x86_64")]
129 static_assert_size!(Scalar, 24);
131 // We want the `Debug` output to be readable as it is used by `derive(Debug)` for
132 // all the Miri types.
133 impl<Tag: fmt::Debug> fmt::Debug for Scalar<Tag> {
134 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
136 Scalar::Ptr(ptr) => write!(f, "{:?}", ptr),
137 &Scalar::Raw { data, size } => {
138 Scalar::check_data(data, size);
142 // Format as hex number wide enough to fit any value of the given `size`.
143 // So data=20, size=1 will be "0x14", but with size=4 it'll be "0x00000014".
144 write!(f, "0x{:>0width$x}", data, width = (size * 2) as usize)
151 impl<Tag: fmt::Debug> fmt::Display for Scalar<Tag> {
152 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
154 Scalar::Ptr(ptr) => write!(f, "pointer to {}", ptr),
155 Scalar::Raw { .. } => fmt::Debug::fmt(self, f),
160 impl<Tag> From<Single> for Scalar<Tag> {
162 fn from(f: Single) -> Self {
167 impl<Tag> From<Double> for Scalar<Tag> {
169 fn from(f: Double) -> Self {
175 /// Make sure the `data` fits in `size`.
176 /// This is guaranteed by all constructors here, but since the enum variants are public,
177 /// it could still be violated (even though no code outside this file should
178 /// construct `Scalar`s).
180 fn check_data(data: u128, size: u8) {
182 truncate(data, Size::from_bytes(u64::from(size))),
184 "Scalar value {:#x} exceeds size of {} bytes",
190 /// Tag this scalar with `new_tag` if it is a pointer, leave it unchanged otherwise.
192 /// Used by `MemPlace::replace_tag`.
194 pub fn with_tag<Tag>(self, new_tag: Tag) -> Scalar<Tag> {
196 Scalar::Ptr(ptr) => Scalar::Ptr(ptr.with_tag(new_tag)),
197 Scalar::Raw { data, size } => Scalar::Raw { data, size },
202 impl<'tcx, Tag> Scalar<Tag> {
203 /// Erase the tag from the scalar, if any.
205 /// Used by error reporting code to avoid having the error type depend on `Tag`.
207 pub fn erase_tag(self) -> Scalar {
209 Scalar::Ptr(ptr) => Scalar::Ptr(ptr.erase_tag()),
210 Scalar::Raw { data, size } => Scalar::Raw { data, size },
215 pub fn null_ptr(cx: &impl HasDataLayout) -> Self {
216 Scalar::Raw { data: 0, size: cx.data_layout().pointer_size.bytes() as u8 }
220 pub fn zst() -> Self {
221 Scalar::Raw { data: 0, size: 0 }
227 dl: &TargetDataLayout,
228 f_int: impl FnOnce(u64) -> InterpResult<'tcx, u64>,
229 f_ptr: impl FnOnce(Pointer<Tag>) -> InterpResult<'tcx, Pointer<Tag>>,
230 ) -> InterpResult<'tcx, Self> {
232 Scalar::Raw { data, size } => {
233 assert_eq!(u64::from(size), dl.pointer_size.bytes());
234 Ok(Scalar::Raw { data: u128::from(f_int(u64::try_from(data).unwrap())?), size })
236 Scalar::Ptr(ptr) => Ok(Scalar::Ptr(f_ptr(ptr)?)),
241 pub fn ptr_offset(self, i: Size, cx: &impl HasDataLayout) -> InterpResult<'tcx, Self> {
242 let dl = cx.data_layout();
243 self.ptr_op(dl, |int| dl.offset(int, i.bytes()), |ptr| ptr.offset(i, dl))
247 pub fn ptr_wrapping_offset(self, i: Size, cx: &impl HasDataLayout) -> Self {
248 let dl = cx.data_layout();
251 |int| Ok(dl.overflowing_offset(int, i.bytes()).0),
252 |ptr| Ok(ptr.wrapping_offset(i, dl)),
258 pub fn ptr_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> InterpResult<'tcx, Self> {
259 let dl = cx.data_layout();
260 self.ptr_op(dl, |int| dl.signed_offset(int, i), |ptr| ptr.signed_offset(i, dl))
264 pub fn ptr_wrapping_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> Self {
265 let dl = cx.data_layout();
268 |int| Ok(dl.overflowing_signed_offset(int, i).0),
269 |ptr| Ok(ptr.wrapping_signed_offset(i, dl)),
275 pub fn from_bool(b: bool) -> Self {
276 // Guaranteed to be truncated and does not need sign extension.
277 Scalar::Raw { data: b as u128, size: 1 }
281 pub fn from_char(c: char) -> Self {
282 // Guaranteed to be truncated and does not need sign extension.
283 Scalar::Raw { data: c as u128, size: 4 }
287 pub fn try_from_uint(i: impl Into<u128>, size: Size) -> Option<Self> {
289 if truncate(i, size) == i {
290 Some(Scalar::Raw { data: i, size: size.bytes() as u8 })
297 pub fn from_uint(i: impl Into<u128>, size: Size) -> Self {
299 Self::try_from_uint(i, size)
300 .unwrap_or_else(|| bug!("Unsigned value {:#x} does not fit in {} bits", i, size.bits()))
304 pub fn from_u8(i: u8) -> Self {
305 // Guaranteed to be truncated and does not need sign extension.
306 Scalar::Raw { data: i.into(), size: 1 }
310 pub fn from_u16(i: u16) -> Self {
311 // Guaranteed to be truncated and does not need sign extension.
312 Scalar::Raw { data: i.into(), size: 2 }
316 pub fn from_u32(i: u32) -> Self {
317 // Guaranteed to be truncated and does not need sign extension.
318 Scalar::Raw { data: i.into(), size: 4 }
322 pub fn from_u64(i: u64) -> Self {
323 // Guaranteed to be truncated and does not need sign extension.
324 Scalar::Raw { data: i.into(), size: 8 }
328 pub fn from_machine_usize(i: u64, cx: &impl HasDataLayout) -> Self {
329 Self::from_uint(i, cx.data_layout().pointer_size)
333 pub fn try_from_int(i: impl Into<i128>, size: Size) -> Option<Self> {
335 // `into` performed sign extension, we have to truncate
336 let truncated = truncate(i as u128, size);
337 if sign_extend(truncated, size) as i128 == i {
338 Some(Scalar::Raw { data: truncated, size: size.bytes() as u8 })
345 pub fn from_int(i: impl Into<i128>, size: Size) -> Self {
347 Self::try_from_int(i, size)
348 .unwrap_or_else(|| bug!("Signed value {:#x} does not fit in {} bits", i, size.bits()))
352 pub fn from_i8(i: i8) -> Self {
353 Self::from_int(i, Size::from_bits(8))
357 pub fn from_i16(i: i16) -> Self {
358 Self::from_int(i, Size::from_bits(16))
362 pub fn from_i32(i: i32) -> Self {
363 Self::from_int(i, Size::from_bits(32))
367 pub fn from_i64(i: i64) -> Self {
368 Self::from_int(i, Size::from_bits(64))
372 pub fn from_machine_isize(i: i64, cx: &impl HasDataLayout) -> Self {
373 Self::from_int(i, cx.data_layout().pointer_size)
377 pub fn from_f32(f: Single) -> Self {
378 // We trust apfloat to give us properly truncated data.
379 Scalar::Raw { data: f.to_bits(), size: 4 }
383 pub fn from_f64(f: Double) -> Self {
384 // We trust apfloat to give us properly truncated data.
385 Scalar::Raw { data: f.to_bits(), size: 8 }
388 /// This is very rarely the method you want! You should dispatch on the type
389 /// and use `force_bits`/`assert_bits`/`force_ptr`/`assert_ptr`.
390 /// This method only exists for the benefit of low-level memory operations
391 /// as well as the implementation of the `force_*` methods.
393 pub fn to_bits_or_ptr(
396 cx: &impl HasDataLayout,
397 ) -> Result<u128, Pointer<Tag>> {
398 assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
400 Scalar::Raw { data, size } => {
401 assert_eq!(target_size.bytes(), u64::from(size));
402 Scalar::check_data(data, size);
405 Scalar::Ptr(ptr) => {
406 assert_eq!(target_size, cx.data_layout().pointer_size);
412 /// This method is intentionally private!
413 /// It is just a helper for other methods in this file.
415 fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
416 assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
418 Scalar::Raw { data, size } => {
419 if target_size.bytes() != u64::from(size) {
420 throw_ub!(ScalarSizeMismatch {
421 target_size: target_size.bytes(),
422 data_size: u64::from(size),
425 Scalar::check_data(data, size);
428 Scalar::Ptr(_) => throw_unsup!(ReadPointerAsBytes),
433 pub fn assert_bits(self, target_size: Size) -> u128 {
434 self.to_bits(target_size).expect("expected Raw bits but got a Pointer")
438 pub fn assert_ptr(self) -> Pointer<Tag> {
441 Scalar::Raw { .. } => bug!("expected a Pointer but got Raw bits"),
445 /// Do not call this method! Dispatch based on the type instead.
447 pub fn is_bits(self) -> bool {
449 Scalar::Raw { .. } => true,
454 /// Do not call this method! Dispatch based on the type instead.
456 pub fn is_ptr(self) -> bool {
458 Scalar::Ptr(_) => true,
463 pub fn to_bool(self) -> InterpResult<'tcx, bool> {
464 let val = self.to_u8()?;
468 _ => throw_ub!(InvalidBool(val)),
472 pub fn to_char(self) -> InterpResult<'tcx, char> {
473 let val = self.to_u32()?;
474 match ::std::char::from_u32(val) {
476 None => throw_ub!(InvalidChar(val)),
481 fn to_unsigned_with_bit_width(self, bits: u64) -> InterpResult<'static, u128> {
482 let sz = Size::from_bits(bits);
486 /// Converts the scalar to produce an `u8`. Fails if the scalar is a pointer.
487 pub fn to_u8(self) -> InterpResult<'static, u8> {
488 self.to_unsigned_with_bit_width(8).map(|v| u8::try_from(v).unwrap())
491 /// Converts the scalar to produce an `u16`. Fails if the scalar is a pointer.
492 pub fn to_u16(self) -> InterpResult<'static, u16> {
493 self.to_unsigned_with_bit_width(16).map(|v| u16::try_from(v).unwrap())
496 /// Converts the scalar to produce an `u32`. Fails if the scalar is a pointer.
497 pub fn to_u32(self) -> InterpResult<'static, u32> {
498 self.to_unsigned_with_bit_width(32).map(|v| u32::try_from(v).unwrap())
501 /// Converts the scalar to produce an `u64`. Fails if the scalar is a pointer.
502 pub fn to_u64(self) -> InterpResult<'static, u64> {
503 self.to_unsigned_with_bit_width(64).map(|v| u64::try_from(v).unwrap())
506 pub fn to_machine_usize(self, cx: &impl HasDataLayout) -> InterpResult<'static, u64> {
507 let b = self.to_bits(cx.data_layout().pointer_size)?;
508 Ok(u64::try_from(b).unwrap())
512 fn to_signed_with_bit_width(self, bits: u64) -> InterpResult<'static, i128> {
513 let sz = Size::from_bits(bits);
514 let b = self.to_bits(sz)?;
515 Ok(sign_extend(b, sz) as i128)
518 /// Converts the scalar to produce an `i8`. Fails if the scalar is a pointer.
519 pub fn to_i8(self) -> InterpResult<'static, i8> {
520 self.to_signed_with_bit_width(8).map(|v| i8::try_from(v).unwrap())
523 /// Converts the scalar to produce an `i16`. Fails if the scalar is a pointer.
524 pub fn to_i16(self) -> InterpResult<'static, i16> {
525 self.to_signed_with_bit_width(16).map(|v| i16::try_from(v).unwrap())
528 /// Converts the scalar to produce an `i32`. Fails if the scalar is a pointer.
529 pub fn to_i32(self) -> InterpResult<'static, i32> {
530 self.to_signed_with_bit_width(32).map(|v| i32::try_from(v).unwrap())
533 /// Converts the scalar to produce an `i64`. Fails if the scalar is a pointer.
534 pub fn to_i64(self) -> InterpResult<'static, i64> {
535 self.to_signed_with_bit_width(64).map(|v| i64::try_from(v).unwrap())
538 pub fn to_machine_isize(self, cx: &impl HasDataLayout) -> InterpResult<'static, i64> {
539 let sz = cx.data_layout().pointer_size;
540 let b = self.to_bits(sz)?;
541 let b = sign_extend(b, sz) as i128;
542 Ok(i64::try_from(b).unwrap())
546 pub fn to_f32(self) -> InterpResult<'static, Single> {
547 // Going through `u32` to check size and truncation.
548 Ok(Single::from_bits(self.to_u32()?.into()))
552 pub fn to_f64(self) -> InterpResult<'static, Double> {
553 // Going through `u64` to check size and truncation.
554 Ok(Double::from_bits(self.to_u64()?.into()))
558 impl<Tag> From<Pointer<Tag>> for Scalar<Tag> {
560 fn from(ptr: Pointer<Tag>) -> Self {
565 #[derive(Clone, Copy, Eq, PartialEq, RustcEncodable, RustcDecodable, HashStable, Hash)]
566 pub enum ScalarMaybeUninit<Tag = ()> {
571 impl<Tag> From<Scalar<Tag>> for ScalarMaybeUninit<Tag> {
573 fn from(s: Scalar<Tag>) -> Self {
574 ScalarMaybeUninit::Scalar(s)
578 impl<Tag> From<Pointer<Tag>> for ScalarMaybeUninit<Tag> {
580 fn from(s: Pointer<Tag>) -> Self {
581 ScalarMaybeUninit::Scalar(s.into())
585 // We want the `Debug` output to be readable as it is used by `derive(Debug)` for
586 // all the Miri types.
587 impl<Tag: fmt::Debug> fmt::Debug for ScalarMaybeUninit<Tag> {
588 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
590 ScalarMaybeUninit::Uninit => write!(f, "<uninitialized>"),
591 ScalarMaybeUninit::Scalar(s) => write!(f, "{:?}", s),
596 impl<Tag: fmt::Debug> fmt::Display for ScalarMaybeUninit<Tag> {
597 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
599 ScalarMaybeUninit::Uninit => write!(f, "uninitialized bytes"),
600 ScalarMaybeUninit::Scalar(s) => write!(f, "{}", s),
605 impl<'tcx, Tag> ScalarMaybeUninit<Tag> {
606 /// Erase the tag from the scalar, if any.
608 /// Used by error reporting code to avoid having the error type depend on `Tag`.
610 pub fn erase_tag(self) -> ScalarMaybeUninit {
612 ScalarMaybeUninit::Scalar(s) => ScalarMaybeUninit::Scalar(s.erase_tag()),
613 ScalarMaybeUninit::Uninit => ScalarMaybeUninit::Uninit,
618 pub fn check_init(self) -> InterpResult<'static, Scalar<Tag>> {
620 ScalarMaybeUninit::Scalar(scalar) => Ok(scalar),
621 ScalarMaybeUninit::Uninit => throw_ub!(InvalidUninitBytes(None)),
626 pub fn to_bool(self) -> InterpResult<'tcx, bool> {
627 self.check_init()?.to_bool()
631 pub fn to_char(self) -> InterpResult<'tcx, char> {
632 self.check_init()?.to_char()
636 pub fn to_f32(self) -> InterpResult<'tcx, Single> {
637 self.check_init()?.to_f32()
641 pub fn to_f64(self) -> InterpResult<'tcx, Double> {
642 self.check_init()?.to_f64()
646 pub fn to_u8(self) -> InterpResult<'tcx, u8> {
647 self.check_init()?.to_u8()
651 pub fn to_u16(self) -> InterpResult<'tcx, u16> {
652 self.check_init()?.to_u16()
656 pub fn to_u32(self) -> InterpResult<'tcx, u32> {
657 self.check_init()?.to_u32()
661 pub fn to_u64(self) -> InterpResult<'tcx, u64> {
662 self.check_init()?.to_u64()
666 pub fn to_machine_usize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, u64> {
667 self.check_init()?.to_machine_usize(cx)
671 pub fn to_i8(self) -> InterpResult<'tcx, i8> {
672 self.check_init()?.to_i8()
676 pub fn to_i16(self) -> InterpResult<'tcx, i16> {
677 self.check_init()?.to_i16()
681 pub fn to_i32(self) -> InterpResult<'tcx, i32> {
682 self.check_init()?.to_i32()
686 pub fn to_i64(self) -> InterpResult<'tcx, i64> {
687 self.check_init()?.to_i64()
691 pub fn to_machine_isize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, i64> {
692 self.check_init()?.to_machine_isize(cx)
696 /// Gets the bytes of a constant slice value.
697 pub fn get_slice_bytes<'tcx>(cx: &impl HasDataLayout, val: ConstValue<'tcx>) -> &'tcx [u8] {
698 if let ConstValue::Slice { data, start, end } = val {
699 let len = end - start;
702 // invent a pointer, only the offset is relevant anyway
703 Pointer::new(AllocId(0), Size::from_bytes(start)),
704 Size::from_bytes(len),
706 .unwrap_or_else(|err| bug!("const slice is invalid: {:?}", err))
708 bug!("expected const slice, but found another const value");