1 use std::fmt;
2
3 use either::{Either, Left, Right};
4
5 use rustc_apfloat::{
6 ieee::{Double, Single},
7 Float,
8 };
9 use rustc_macros::HashStable;
10 use rustc_target::abi::{HasDataLayout, Size};
11
12 use crate::ty::{ParamEnv, ScalarInt, Ty, TyCtxt};
13
14 use super::{
15 AllocId, AllocRange, ConstAllocation, InterpResult, Pointer, PointerArithmetic, Provenance,
16 ScalarSizeMismatch,
17 };
18
19 /// Represents the result of const evaluation via the `eval_to_allocation` query.
20 #[derive(Copy, Clone, HashStable, TyEncodable, TyDecodable, Debug, Hash, Eq, PartialEq)]
21 pub struct ConstAlloc<'tcx> {
22 /// The value lives here, at offset 0, and that allocation definitely is an `AllocKind::Memory`
23 /// (so you can use `AllocMap::unwrap_memory`).
24 pub alloc_id: AllocId,
25 pub ty: Ty<'tcx>,
26 }
27
28 /// Represents a constant value in Rust. `Scalar` and `Slice` are optimizations for
29 /// array length computations, enum discriminants and the pattern matching logic.
30 #[derive(Copy, Clone, Debug, Eq, PartialEq, TyEncodable, TyDecodable, Hash)]
31 #[derive(HashStable, Lift)]
32 pub enum ConstValue<'tcx> {
33 /// Used only for types with `layout::abi::Scalar` ABI.
34 ///
35 /// Not using the enum `Value` to encode that this must not be `Uninit`.
36 Scalar(Scalar),
37
38 /// Only used for ZSTs.
39 ZeroSized,
40
41 /// Used only for `&[u8]` and `&str`
42 Slice { data: ConstAllocation<'tcx>, start: usize, end: usize },
43
44 /// A value not represented/representable by `Scalar` or `Slice`
45 ByRef {
46 /// The backing memory of the value, may contain more memory than needed for just the value
47 /// in order to share `ConstAllocation`s between values
48 alloc: ConstAllocation<'tcx>,
49 /// Offset into `alloc`
50 offset: Size,
51 },
52 }
53
54 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
55 static_assert_size!(ConstValue<'_>, 32);
56
57 impl<'tcx> ConstValue<'tcx> {
58 #[inline]
try_to_scalar(&self) -> Option<Scalar<AllocId>>59 pub fn try_to_scalar(&self) -> Option<Scalar<AllocId>> {
60 match *self {
61 ConstValue::ByRef { .. } | ConstValue::Slice { .. } | ConstValue::ZeroSized => None,
62 ConstValue::Scalar(val) => Some(val),
63 }
64 }
65
try_to_scalar_int(&self) -> Option<ScalarInt>66 pub fn try_to_scalar_int(&self) -> Option<ScalarInt> {
67 self.try_to_scalar()?.try_to_int().ok()
68 }
69
try_to_bits(&self, size: Size) -> Option<u128>70 pub fn try_to_bits(&self, size: Size) -> Option<u128> {
71 self.try_to_scalar_int()?.to_bits(size).ok()
72 }
73
try_to_bool(&self) -> Option<bool>74 pub fn try_to_bool(&self) -> Option<bool> {
75 self.try_to_scalar_int()?.try_into().ok()
76 }
77
try_to_target_usize(&self, tcx: TyCtxt<'tcx>) -> Option<u64>78 pub fn try_to_target_usize(&self, tcx: TyCtxt<'tcx>) -> Option<u64> {
79 self.try_to_scalar_int()?.try_to_target_usize(tcx).ok()
80 }
81
try_to_bits_for_ty( &self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>, ty: Ty<'tcx>, ) -> Option<u128>82 pub fn try_to_bits_for_ty(
83 &self,
84 tcx: TyCtxt<'tcx>,
85 param_env: ParamEnv<'tcx>,
86 ty: Ty<'tcx>,
87 ) -> Option<u128> {
88 let size = tcx.layout_of(param_env.with_reveal_all_normalized(tcx).and(ty)).ok()?.size;
89 self.try_to_bits(size)
90 }
91
from_bool(b: bool) -> Self92 pub fn from_bool(b: bool) -> Self {
93 ConstValue::Scalar(Scalar::from_bool(b))
94 }
95
from_u64(i: u64) -> Self96 pub fn from_u64(i: u64) -> Self {
97 ConstValue::Scalar(Scalar::from_u64(i))
98 }
99
from_u128(i: u128) -> Self100 pub fn from_u128(i: u128) -> Self {
101 ConstValue::Scalar(Scalar::from_u128(i))
102 }
103
from_target_usize(i: u64, cx: &impl HasDataLayout) -> Self104 pub fn from_target_usize(i: u64, cx: &impl HasDataLayout) -> Self {
105 ConstValue::Scalar(Scalar::from_target_usize(i, cx))
106 }
107 }
108
109 /// A `Scalar` represents an immediate, primitive value existing outside of a
110 /// `memory::Allocation`. It is in many ways like a small chunk of an `Allocation`, up to 16 bytes in
111 /// size. Like a range of bytes in an `Allocation`, a `Scalar` can either represent the raw bytes
112 /// of a simple value or a pointer into another `Allocation`
113 ///
114 /// These variants would be private if there was a convenient way to achieve that in Rust.
115 /// Do *not* match on a `Scalar`! Use the various `to_*` methods instead.
116 #[derive(Clone, Copy, Eq, PartialEq, TyEncodable, TyDecodable, Hash)]
117 #[derive(HashStable)]
118 pub enum Scalar<Prov = AllocId> {
119 /// The raw bytes of a simple value.
120 Int(ScalarInt),
121
122 /// A pointer.
123 ///
124 /// We also store the size of the pointer, such that a `Scalar` always knows how big it is.
125 /// The size is always the pointer size of the current target, but this is not information
126 /// that we always have readily available.
127 Ptr(Pointer<Prov>, u8),
128 }
129
130 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
131 static_assert_size!(Scalar, 24);
132
133 // We want the `Debug` output to be readable as it is used by `derive(Debug)` for
134 // all the Miri types.
135 impl<Prov: Provenance> fmt::Debug for Scalar<Prov> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result136 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
137 match self {
138 Scalar::Ptr(ptr, _size) => write!(f, "{:?}", ptr),
139 Scalar::Int(int) => write!(f, "{:?}", int),
140 }
141 }
142 }
143
144 impl<Prov: Provenance> fmt::Display for Scalar<Prov> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result145 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
146 match self {
147 Scalar::Ptr(ptr, _size) => write!(f, "pointer to {:?}", ptr),
148 Scalar::Int(int) => write!(f, "{}", int),
149 }
150 }
151 }
152
153 impl<Prov: Provenance> fmt::LowerHex for Scalar<Prov> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result154 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
155 match self {
156 Scalar::Ptr(ptr, _size) => write!(f, "pointer to {:?}", ptr),
157 Scalar::Int(int) => write!(f, "{:#x}", int),
158 }
159 }
160 }
161
162 impl<Prov> From<Single> for Scalar<Prov> {
163 #[inline(always)]
from(f: Single) -> Self164 fn from(f: Single) -> Self {
165 Scalar::from_f32(f)
166 }
167 }
168
169 impl<Prov> From<Double> for Scalar<Prov> {
170 #[inline(always)]
from(f: Double) -> Self171 fn from(f: Double) -> Self {
172 Scalar::from_f64(f)
173 }
174 }
175
176 impl<Prov> From<ScalarInt> for Scalar<Prov> {
177 #[inline(always)]
from(ptr: ScalarInt) -> Self178 fn from(ptr: ScalarInt) -> Self {
179 Scalar::Int(ptr)
180 }
181 }
182
183 impl<Prov> Scalar<Prov> {
184 #[inline(always)]
from_pointer(ptr: Pointer<Prov>, cx: &impl HasDataLayout) -> Self185 pub fn from_pointer(ptr: Pointer<Prov>, cx: &impl HasDataLayout) -> Self {
186 Scalar::Ptr(ptr, u8::try_from(cx.pointer_size().bytes()).unwrap())
187 }
188
189 /// Create a Scalar from a pointer with an `Option<_>` provenance (where `None` represents a
190 /// plain integer / "invalid" pointer).
from_maybe_pointer(ptr: Pointer<Option<Prov>>, cx: &impl HasDataLayout) -> Self191 pub fn from_maybe_pointer(ptr: Pointer<Option<Prov>>, cx: &impl HasDataLayout) -> Self {
192 match ptr.into_parts() {
193 (Some(prov), offset) => Scalar::from_pointer(Pointer::new(prov, offset), cx),
194 (None, offset) => {
195 Scalar::Int(ScalarInt::try_from_uint(offset.bytes(), cx.pointer_size()).unwrap())
196 }
197 }
198 }
199
200 #[inline]
null_ptr(cx: &impl HasDataLayout) -> Self201 pub fn null_ptr(cx: &impl HasDataLayout) -> Self {
202 Scalar::Int(ScalarInt::null(cx.pointer_size()))
203 }
204
205 #[inline]
from_bool(b: bool) -> Self206 pub fn from_bool(b: bool) -> Self {
207 Scalar::Int(b.into())
208 }
209
210 #[inline]
from_char(c: char) -> Self211 pub fn from_char(c: char) -> Self {
212 Scalar::Int(c.into())
213 }
214
215 #[inline]
try_from_uint(i: impl Into<u128>, size: Size) -> Option<Self>216 pub fn try_from_uint(i: impl Into<u128>, size: Size) -> Option<Self> {
217 ScalarInt::try_from_uint(i, size).map(Scalar::Int)
218 }
219
220 #[inline]
from_uint(i: impl Into<u128>, size: Size) -> Self221 pub fn from_uint(i: impl Into<u128>, size: Size) -> Self {
222 let i = i.into();
223 Self::try_from_uint(i, size)
224 .unwrap_or_else(|| bug!("Unsigned value {:#x} does not fit in {} bits", i, size.bits()))
225 }
226
227 #[inline]
from_u8(i: u8) -> Self228 pub fn from_u8(i: u8) -> Self {
229 Scalar::Int(i.into())
230 }
231
232 #[inline]
from_u16(i: u16) -> Self233 pub fn from_u16(i: u16) -> Self {
234 Scalar::Int(i.into())
235 }
236
237 #[inline]
from_u32(i: u32) -> Self238 pub fn from_u32(i: u32) -> Self {
239 Scalar::Int(i.into())
240 }
241
242 #[inline]
from_u64(i: u64) -> Self243 pub fn from_u64(i: u64) -> Self {
244 Scalar::Int(i.into())
245 }
246
247 #[inline]
from_u128(i: u128) -> Self248 pub fn from_u128(i: u128) -> Self {
249 Scalar::Int(i.into())
250 }
251
252 #[inline]
from_target_usize(i: u64, cx: &impl HasDataLayout) -> Self253 pub fn from_target_usize(i: u64, cx: &impl HasDataLayout) -> Self {
254 Self::from_uint(i, cx.data_layout().pointer_size)
255 }
256
257 #[inline]
try_from_int(i: impl Into<i128>, size: Size) -> Option<Self>258 pub fn try_from_int(i: impl Into<i128>, size: Size) -> Option<Self> {
259 ScalarInt::try_from_int(i, size).map(Scalar::Int)
260 }
261
262 #[inline]
from_int(i: impl Into<i128>, size: Size) -> Self263 pub fn from_int(i: impl Into<i128>, size: Size) -> Self {
264 let i = i.into();
265 Self::try_from_int(i, size)
266 .unwrap_or_else(|| bug!("Signed value {:#x} does not fit in {} bits", i, size.bits()))
267 }
268
269 #[inline]
from_i32(i: i32) -> Self270 pub fn from_i32(i: i32) -> Self {
271 Self::from_int(i, Size::from_bits(32))
272 }
273
274 #[inline]
from_i64(i: i64) -> Self275 pub fn from_i64(i: i64) -> Self {
276 Self::from_int(i, Size::from_bits(64))
277 }
278
279 #[inline]
from_target_isize(i: i64, cx: &impl HasDataLayout) -> Self280 pub fn from_target_isize(i: i64, cx: &impl HasDataLayout) -> Self {
281 Self::from_int(i, cx.data_layout().pointer_size)
282 }
283
284 #[inline]
from_f32(f: Single) -> Self285 pub fn from_f32(f: Single) -> Self {
286 Scalar::Int(f.into())
287 }
288
289 #[inline]
from_f64(f: Double) -> Self290 pub fn from_f64(f: Double) -> Self {
291 Scalar::Int(f.into())
292 }
293
294 /// This is almost certainly not the method you want! You should dispatch on the type
295 /// and use `to_{u8,u16,...}`/`scalar_to_ptr` to perform ptr-to-int / int-to-ptr casts as needed.
296 ///
297 /// This method only exists for the benefit of low-level operations that truly need to treat the
298 /// scalar in whatever form it is.
299 ///
300 /// This throws UB (instead of ICEing) on a size mismatch since size mismatches can arise in
301 /// Miri when someone declares a function that we shim (such as `malloc`) with a wrong type.
302 #[inline]
to_bits_or_ptr_internal( self, target_size: Size, ) -> Result<Either<u128, Pointer<Prov>>, ScalarSizeMismatch>303 pub fn to_bits_or_ptr_internal(
304 self,
305 target_size: Size,
306 ) -> Result<Either<u128, Pointer<Prov>>, ScalarSizeMismatch> {
307 assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
308 Ok(match self {
309 Scalar::Int(int) => Left(int.to_bits(target_size).map_err(|size| {
310 ScalarSizeMismatch { target_size: target_size.bytes(), data_size: size.bytes() }
311 })?),
312 Scalar::Ptr(ptr, sz) => {
313 if target_size.bytes() != u64::from(sz) {
314 return Err(ScalarSizeMismatch {
315 target_size: target_size.bytes(),
316 data_size: sz.into(),
317 });
318 }
319 Right(ptr)
320 }
321 })
322 }
323 }
324
325 impl<'tcx, Prov: Provenance> Scalar<Prov> {
to_pointer(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, Pointer<Option<Prov>>>326 pub fn to_pointer(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, Pointer<Option<Prov>>> {
327 match self
328 .to_bits_or_ptr_internal(cx.pointer_size())
329 .map_err(|s| err_ub!(ScalarSizeMismatch(s)))?
330 {
331 Right(ptr) => Ok(ptr.into()),
332 Left(bits) => {
333 let addr = u64::try_from(bits).unwrap();
334 Ok(Pointer::from_addr_invalid(addr))
335 }
336 }
337 }
338
339 /// Fundamental scalar-to-int (cast) operation. Many convenience wrappers exist below, that you
340 /// likely want to use instead.
341 ///
342 /// Will perform ptr-to-int casts if needed and possible.
343 /// If that fails, we know the offset is relative, so we return an "erased" Scalar
344 /// (which is useful for error messages but not much else).
345 #[inline]
try_to_int(self) -> Result<ScalarInt, Scalar<AllocId>>346 pub fn try_to_int(self) -> Result<ScalarInt, Scalar<AllocId>> {
347 match self {
348 Scalar::Int(int) => Ok(int),
349 Scalar::Ptr(ptr, sz) => {
350 if Prov::OFFSET_IS_ADDR {
351 Ok(ScalarInt::try_from_uint(ptr.offset.bytes(), Size::from_bytes(sz)).unwrap())
352 } else {
353 // We know `offset` is relative, since `OFFSET_IS_ADDR == false`.
354 let (prov, offset) = ptr.into_parts();
355 // Because `OFFSET_IS_ADDR == false`, this unwrap can never fail.
356 Err(Scalar::Ptr(Pointer::new(prov.get_alloc_id().unwrap(), offset), sz))
357 }
358 }
359 }
360 }
361
362 #[inline(always)]
363 #[cfg_attr(debug_assertions, track_caller)] // only in debug builds due to perf (see #98980)
assert_int(self) -> ScalarInt364 pub fn assert_int(self) -> ScalarInt {
365 self.try_to_int().unwrap()
366 }
367
368 /// This throws UB (instead of ICEing) on a size mismatch since size mismatches can arise in
369 /// Miri when someone declares a function that we shim (such as `malloc`) with a wrong type.
370 #[inline]
to_bits(self, target_size: Size) -> InterpResult<'tcx, u128>371 pub fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
372 assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
373 self.try_to_int().map_err(|_| err_unsup!(ReadPointerAsBytes))?.to_bits(target_size).map_err(
374 |size| {
375 err_ub!(ScalarSizeMismatch(ScalarSizeMismatch {
376 target_size: target_size.bytes(),
377 data_size: size.bytes(),
378 }))
379 .into()
380 },
381 )
382 }
383
384 #[inline(always)]
385 #[cfg_attr(debug_assertions, track_caller)] // only in debug builds due to perf (see #98980)
assert_bits(self, target_size: Size) -> u128386 pub fn assert_bits(self, target_size: Size) -> u128 {
387 self.to_bits(target_size)
388 .unwrap_or_else(|_| panic!("assertion failed: {self:?} fits {target_size:?}"))
389 }
390
to_bool(self) -> InterpResult<'tcx, bool>391 pub fn to_bool(self) -> InterpResult<'tcx, bool> {
392 let val = self.to_u8()?;
393 match val {
394 0 => Ok(false),
395 1 => Ok(true),
396 _ => throw_ub!(InvalidBool(val)),
397 }
398 }
399
to_char(self) -> InterpResult<'tcx, char>400 pub fn to_char(self) -> InterpResult<'tcx, char> {
401 let val = self.to_u32()?;
402 match std::char::from_u32(val) {
403 Some(c) => Ok(c),
404 None => throw_ub!(InvalidChar(val)),
405 }
406 }
407
408 /// Converts the scalar to produce an unsigned integer of the given size.
409 /// Fails if the scalar is a pointer.
410 #[inline]
to_uint(self, size: Size) -> InterpResult<'tcx, u128>411 pub fn to_uint(self, size: Size) -> InterpResult<'tcx, u128> {
412 self.to_bits(size)
413 }
414
415 /// Converts the scalar to produce a `u8`. Fails if the scalar is a pointer.
to_u8(self) -> InterpResult<'tcx, u8>416 pub fn to_u8(self) -> InterpResult<'tcx, u8> {
417 self.to_uint(Size::from_bits(8)).map(|v| u8::try_from(v).unwrap())
418 }
419
420 /// Converts the scalar to produce a `u16`. Fails if the scalar is a pointer.
to_u16(self) -> InterpResult<'tcx, u16>421 pub fn to_u16(self) -> InterpResult<'tcx, u16> {
422 self.to_uint(Size::from_bits(16)).map(|v| u16::try_from(v).unwrap())
423 }
424
425 /// Converts the scalar to produce a `u32`. Fails if the scalar is a pointer.
to_u32(self) -> InterpResult<'tcx, u32>426 pub fn to_u32(self) -> InterpResult<'tcx, u32> {
427 self.to_uint(Size::from_bits(32)).map(|v| u32::try_from(v).unwrap())
428 }
429
430 /// Converts the scalar to produce a `u64`. Fails if the scalar is a pointer.
to_u64(self) -> InterpResult<'tcx, u64>431 pub fn to_u64(self) -> InterpResult<'tcx, u64> {
432 self.to_uint(Size::from_bits(64)).map(|v| u64::try_from(v).unwrap())
433 }
434
435 /// Converts the scalar to produce a `u128`. Fails if the scalar is a pointer.
to_u128(self) -> InterpResult<'tcx, u128>436 pub fn to_u128(self) -> InterpResult<'tcx, u128> {
437 self.to_uint(Size::from_bits(128))
438 }
439
440 /// Converts the scalar to produce a machine-pointer-sized unsigned integer.
441 /// Fails if the scalar is a pointer.
to_target_usize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, u64>442 pub fn to_target_usize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, u64> {
443 let b = self.to_uint(cx.data_layout().pointer_size)?;
444 Ok(u64::try_from(b).unwrap())
445 }
446
447 /// Converts the scalar to produce a signed integer of the given size.
448 /// Fails if the scalar is a pointer.
449 #[inline]
to_int(self, size: Size) -> InterpResult<'tcx, i128>450 pub fn to_int(self, size: Size) -> InterpResult<'tcx, i128> {
451 let b = self.to_bits(size)?;
452 Ok(size.sign_extend(b) as i128)
453 }
454
455 /// Converts the scalar to produce an `i8`. Fails if the scalar is a pointer.
to_i8(self) -> InterpResult<'tcx, i8>456 pub fn to_i8(self) -> InterpResult<'tcx, i8> {
457 self.to_int(Size::from_bits(8)).map(|v| i8::try_from(v).unwrap())
458 }
459
460 /// Converts the scalar to produce an `i16`. Fails if the scalar is a pointer.
to_i16(self) -> InterpResult<'tcx, i16>461 pub fn to_i16(self) -> InterpResult<'tcx, i16> {
462 self.to_int(Size::from_bits(16)).map(|v| i16::try_from(v).unwrap())
463 }
464
465 /// Converts the scalar to produce an `i32`. Fails if the scalar is a pointer.
to_i32(self) -> InterpResult<'tcx, i32>466 pub fn to_i32(self) -> InterpResult<'tcx, i32> {
467 self.to_int(Size::from_bits(32)).map(|v| i32::try_from(v).unwrap())
468 }
469
470 /// Converts the scalar to produce an `i64`. Fails if the scalar is a pointer.
to_i64(self) -> InterpResult<'tcx, i64>471 pub fn to_i64(self) -> InterpResult<'tcx, i64> {
472 self.to_int(Size::from_bits(64)).map(|v| i64::try_from(v).unwrap())
473 }
474
475 /// Converts the scalar to produce an `i128`. Fails if the scalar is a pointer.
to_i128(self) -> InterpResult<'tcx, i128>476 pub fn to_i128(self) -> InterpResult<'tcx, i128> {
477 self.to_int(Size::from_bits(128))
478 }
479
480 /// Converts the scalar to produce a machine-pointer-sized signed integer.
481 /// Fails if the scalar is a pointer.
to_target_isize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, i64>482 pub fn to_target_isize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, i64> {
483 let b = self.to_int(cx.data_layout().pointer_size)?;
484 Ok(i64::try_from(b).unwrap())
485 }
486
487 #[inline]
to_f32(self) -> InterpResult<'tcx, Single>488 pub fn to_f32(self) -> InterpResult<'tcx, Single> {
489 // Going through `u32` to check size and truncation.
490 Ok(Single::from_bits(self.to_u32()?.into()))
491 }
492
493 #[inline]
to_f64(self) -> InterpResult<'tcx, Double>494 pub fn to_f64(self) -> InterpResult<'tcx, Double> {
495 // Going through `u64` to check size and truncation.
496 Ok(Double::from_bits(self.to_u64()?.into()))
497 }
498 }
499
500 /// Gets the bytes of a constant slice value.
get_slice_bytes<'tcx>(cx: &impl HasDataLayout, val: ConstValue<'tcx>) -> &'tcx [u8]501 pub fn get_slice_bytes<'tcx>(cx: &impl HasDataLayout, val: ConstValue<'tcx>) -> &'tcx [u8] {
502 if let ConstValue::Slice { data, start, end } = val {
503 let len = end - start;
504 data.inner()
505 .get_bytes_strip_provenance(
506 cx,
507 AllocRange { start: Size::from_bytes(start), size: Size::from_bytes(len) },
508 )
509 .unwrap_or_else(|err| bug!("const slice is invalid: {:?}", err))
510 } else {
511 bug!("expected const slice, but found another const value");
512 }
513 }
514