rustc_const_eval/interpret/
intrinsics.rs

1//! Intrinsics and other functions that the interpreter executes without
2//! looking at their MIR. Intrinsics/functions supported here are shared by CTFE
3//! and miri.
4
5use std::assert_matches::assert_matches;
6
7use rustc_abi::{FieldIdx, HasDataLayout, Size};
8use rustc_apfloat::ieee::{Double, Half, Quad, Single};
9use rustc_middle::mir::interpret::{CTFE_ALLOC_SALT, read_target_uint, write_target_uint};
10use rustc_middle::mir::{self, BinOp, ConstValue, NonDivergingIntrinsic};
11use rustc_middle::ty::layout::TyAndLayout;
12use rustc_middle::ty::{Ty, TyCtxt};
13use rustc_middle::{bug, ty};
14use rustc_span::{Symbol, sym};
15use tracing::trace;
16
17use super::memory::MemoryKind;
18use super::util::ensure_monomorphic_enough;
19use super::{
20    AllocId, CheckInAllocMsg, ImmTy, InterpCx, InterpResult, Machine, OpTy, PlaceTy, Pointer,
21    PointerArithmetic, Provenance, Scalar, err_ub_custom, err_unsup_format, interp_ok, throw_inval,
22    throw_ub_custom, throw_ub_format,
23};
24use crate::fluent_generated as fluent;
25
26/// Directly returns an `Allocation` containing an absolute path representation of the given type.
27pub(crate) fn alloc_type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> (AllocId, u64) {
28    let path = crate::util::type_name(tcx, ty);
29    let bytes = path.into_bytes();
30    let len = bytes.len().try_into().unwrap();
31    (tcx.allocate_bytes_dedup(bytes, CTFE_ALLOC_SALT), len)
32}
33impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
34    /// Generates a value of `TypeId` for `ty` in-place.
35    fn write_type_id(
36        &mut self,
37        ty: Ty<'tcx>,
38        dest: &PlaceTy<'tcx, M::Provenance>,
39    ) -> InterpResult<'tcx, ()> {
40        let tcx = self.tcx;
41        let type_id_hash = tcx.type_id_hash(ty).as_u128();
42        let op = self.const_val_to_op(
43            ConstValue::Scalar(Scalar::from_u128(type_id_hash)),
44            tcx.types.u128,
45            None,
46        )?;
47        self.copy_op_allow_transmute(&op, dest)?;
48
49        // Give the each pointer-sized chunk provenance that knows about the type id.
50        // Here we rely on `TypeId` being a newtype around an array of pointers, so we
51        // first project to its only field and then the array elements.
52        let alloc_id = tcx.reserve_and_set_type_id_alloc(ty);
53        let arr = self.project_field(dest, FieldIdx::ZERO)?;
54        let mut elem_iter = self.project_array_fields(&arr)?;
55        while let Some((_, elem)) = elem_iter.next(self)? {
56            // Decorate this part of the hash with provenance; leave the integer part unchanged.
57            let hash_fragment = self.read_scalar(&elem)?.to_target_usize(&tcx)?;
58            let ptr = Pointer::new(alloc_id.into(), Size::from_bytes(hash_fragment));
59            let ptr = self.global_root_pointer(ptr)?;
60            let val = Scalar::from_pointer(ptr, &tcx);
61            self.write_scalar(val, &elem)?;
62        }
63        interp_ok(())
64    }
65
66    /// Read a value of type `TypeId`, returning the type it represents.
67    pub(crate) fn read_type_id(
68        &self,
69        op: &OpTy<'tcx, M::Provenance>,
70    ) -> InterpResult<'tcx, Ty<'tcx>> {
71        // `TypeId` is a newtype around an array of pointers. All pointers must have the same
72        // provenance, and that provenance represents the type.
73        let ptr_size = self.pointer_size().bytes_usize();
74        let arr = self.project_field(op, FieldIdx::ZERO)?;
75
76        let mut ty_and_hash = None;
77        let mut elem_iter = self.project_array_fields(&arr)?;
78        while let Some((idx, elem)) = elem_iter.next(self)? {
79            let elem = self.read_pointer(&elem)?;
80            let (elem_ty, elem_hash) = self.get_ptr_type_id(elem)?;
81            // If this is the first element, remember the type and its hash.
82            // If this is not the first element, ensure it is consistent with the previous ones.
83            let full_hash = match ty_and_hash {
84                None => {
85                    let hash = self.tcx.type_id_hash(elem_ty).as_u128();
86                    let mut hash_bytes = [0u8; 16];
87                    write_target_uint(self.data_layout().endian, &mut hash_bytes, hash).unwrap();
88                    ty_and_hash = Some((elem_ty, hash_bytes));
89                    hash_bytes
90                }
91                Some((ty, hash_bytes)) => {
92                    if ty != elem_ty {
93                        throw_ub_format!(
94                            "invalid `TypeId` value: not all bytes carry the same type id metadata"
95                        );
96                    }
97                    hash_bytes
98                }
99            };
100            // Ensure the elem_hash matches the corresponding part of the full hash.
101            let hash_frag = &full_hash[(idx as usize) * ptr_size..][..ptr_size];
102            if read_target_uint(self.data_layout().endian, hash_frag).unwrap() != elem_hash.into() {
103                throw_ub_format!(
104                    "invalid `TypeId` value: the hash does not match the type id metadata"
105                );
106            }
107        }
108
109        interp_ok(ty_and_hash.unwrap().0)
110    }
111
112    /// Returns `true` if emulation happened.
113    /// Here we implement the intrinsics that are common to all Miri instances; individual machines can add their own
114    /// intrinsic handling.
115    pub fn eval_intrinsic(
116        &mut self,
117        instance: ty::Instance<'tcx>,
118        args: &[OpTy<'tcx, M::Provenance>],
119        dest: &PlaceTy<'tcx, M::Provenance>,
120        ret: Option<mir::BasicBlock>,
121    ) -> InterpResult<'tcx, bool> {
122        let instance_args = instance.args;
123        let intrinsic_name = self.tcx.item_name(instance.def_id());
124        let tcx = self.tcx.tcx;
125
126        match intrinsic_name {
127            sym::type_name => {
128                let tp_ty = instance.args.type_at(0);
129                ensure_monomorphic_enough(tcx, tp_ty)?;
130                let (alloc_id, meta) = alloc_type_name(tcx, tp_ty);
131                let val = ConstValue::Slice { alloc_id, meta };
132                let val = self.const_val_to_op(val, dest.layout.ty, Some(dest.layout))?;
133                self.copy_op(&val, dest)?;
134            }
135            sym::needs_drop => {
136                let tp_ty = instance.args.type_at(0);
137                ensure_monomorphic_enough(tcx, tp_ty)?;
138                let val = ConstValue::from_bool(tp_ty.needs_drop(tcx, self.typing_env));
139                let val = self.const_val_to_op(val, tcx.types.bool, Some(dest.layout))?;
140                self.copy_op(&val, dest)?;
141            }
142            sym::type_id => {
143                let tp_ty = instance.args.type_at(0);
144                ensure_monomorphic_enough(tcx, tp_ty)?;
145                self.write_type_id(tp_ty, dest)?;
146            }
147            sym::type_id_eq => {
148                let a_ty = self.read_type_id(&args[0])?;
149                let b_ty = self.read_type_id(&args[1])?;
150                self.write_scalar(Scalar::from_bool(a_ty == b_ty), dest)?;
151            }
152            sym::variant_count => {
153                let tp_ty = instance.args.type_at(0);
154                let ty = match tp_ty.kind() {
155                    // Pattern types have the same number of variants as their base type.
156                    // Even if we restrict e.g. which variants are valid, the variants are essentially just uninhabited.
157                    // And `Result<(), !>` still has two variants according to `variant_count`.
158                    ty::Pat(base, _) => *base,
159                    _ => tp_ty,
160                };
161                let val = match ty.kind() {
162                    // Correctly handles non-monomorphic calls, so there is no need for ensure_monomorphic_enough.
163                    ty::Adt(adt, _) => {
164                        ConstValue::from_target_usize(adt.variants().len() as u64, &tcx)
165                    }
166                    ty::Alias(..) | ty::Param(_) | ty::Placeholder(_) | ty::Infer(_) => {
167                        throw_inval!(TooGeneric)
168                    }
169                    ty::Pat(..) => unreachable!(),
170                    ty::Bound(_, _) => bug!("bound ty during ctfe"),
171                    ty::Bool
172                    | ty::Char
173                    | ty::Int(_)
174                    | ty::Uint(_)
175                    | ty::Float(_)
176                    | ty::Foreign(_)
177                    | ty::Str
178                    | ty::Array(_, _)
179                    | ty::Slice(_)
180                    | ty::RawPtr(_, _)
181                    | ty::Ref(_, _, _)
182                    | ty::FnDef(_, _)
183                    | ty::FnPtr(..)
184                    | ty::Dynamic(_, _, _)
185                    | ty::Closure(_, _)
186                    | ty::CoroutineClosure(_, _)
187                    | ty::Coroutine(_, _)
188                    | ty::CoroutineWitness(..)
189                    | ty::UnsafeBinder(_)
190                    | ty::Never
191                    | ty::Tuple(_)
192                    | ty::Error(_) => ConstValue::from_target_usize(0u64, &tcx),
193                };
194                let val = self.const_val_to_op(val, dest.layout.ty, Some(dest.layout))?;
195                self.copy_op(&val, dest)?;
196            }
197
198            sym::caller_location => {
199                let span = self.find_closest_untracked_caller_location();
200                let val = self.tcx.span_as_caller_location(span);
201                let val =
202                    self.const_val_to_op(val, self.tcx.caller_location_ty(), Some(dest.layout))?;
203                self.copy_op(&val, dest)?;
204            }
205
206            sym::align_of_val | sym::size_of_val => {
207                // Avoid `deref_pointer` -- this is not a deref, the ptr does not have to be
208                // dereferenceable!
209                let place = self.ref_to_mplace(&self.read_immediate(&args[0])?)?;
210                let (size, align) = self
211                    .size_and_align_of_val(&place)?
212                    .ok_or_else(|| err_unsup_format!("`extern type` does not have known layout"))?;
213
214                let result = match intrinsic_name {
215                    sym::align_of_val => align.bytes(),
216                    sym::size_of_val => size.bytes(),
217                    _ => bug!(),
218                };
219
220                self.write_scalar(Scalar::from_target_usize(result, self), dest)?;
221            }
222
223            sym::fadd_algebraic
224            | sym::fsub_algebraic
225            | sym::fmul_algebraic
226            | sym::fdiv_algebraic
227            | sym::frem_algebraic => {
228                let a = self.read_immediate(&args[0])?;
229                let b = self.read_immediate(&args[1])?;
230
231                let op = match intrinsic_name {
232                    sym::fadd_algebraic => BinOp::Add,
233                    sym::fsub_algebraic => BinOp::Sub,
234                    sym::fmul_algebraic => BinOp::Mul,
235                    sym::fdiv_algebraic => BinOp::Div,
236                    sym::frem_algebraic => BinOp::Rem,
237
238                    _ => bug!(),
239                };
240
241                let res = self.binary_op(op, &a, &b)?;
242                // `binary_op` already called `generate_nan` if needed.
243                let res = M::apply_float_nondet(self, res)?;
244                self.write_immediate(*res, dest)?;
245            }
246
247            sym::ctpop
248            | sym::cttz
249            | sym::cttz_nonzero
250            | sym::ctlz
251            | sym::ctlz_nonzero
252            | sym::bswap
253            | sym::bitreverse => {
254                let ty = instance_args.type_at(0);
255                let layout = self.layout_of(ty)?;
256                let val = self.read_scalar(&args[0])?;
257
258                let out_val = self.numeric_intrinsic(intrinsic_name, val, layout, dest.layout)?;
259                self.write_scalar(out_val, dest)?;
260            }
261            sym::saturating_add | sym::saturating_sub => {
262                let l = self.read_immediate(&args[0])?;
263                let r = self.read_immediate(&args[1])?;
264                let val = self.saturating_arith(
265                    if intrinsic_name == sym::saturating_add { BinOp::Add } else { BinOp::Sub },
266                    &l,
267                    &r,
268                )?;
269                self.write_scalar(val, dest)?;
270            }
271            sym::discriminant_value => {
272                let place = self.deref_pointer(&args[0])?;
273                let variant = self.read_discriminant(&place)?;
274                let discr = self.discriminant_for_variant(place.layout.ty, variant)?;
275                self.write_immediate(*discr, dest)?;
276            }
277            sym::exact_div => {
278                let l = self.read_immediate(&args[0])?;
279                let r = self.read_immediate(&args[1])?;
280                self.exact_div(&l, &r, dest)?;
281            }
282            sym::rotate_left | sym::rotate_right => {
283                // rotate_left: (X << (S % BW)) | (X >> ((BW - S) % BW))
284                // rotate_right: (X << ((BW - S) % BW)) | (X >> (S % BW))
285                let layout_val = self.layout_of(instance_args.type_at(0))?;
286                let val = self.read_scalar(&args[0])?;
287                let val_bits = val.to_bits(layout_val.size)?; // sign is ignored here
288
289                let layout_raw_shift = self.layout_of(self.tcx.types.u32)?;
290                let raw_shift = self.read_scalar(&args[1])?;
291                let raw_shift_bits = raw_shift.to_bits(layout_raw_shift.size)?;
292
293                let width_bits = u128::from(layout_val.size.bits());
294                let shift_bits = raw_shift_bits % width_bits;
295                let inv_shift_bits = (width_bits - shift_bits) % width_bits;
296                let result_bits = if intrinsic_name == sym::rotate_left {
297                    (val_bits << shift_bits) | (val_bits >> inv_shift_bits)
298                } else {
299                    (val_bits >> shift_bits) | (val_bits << inv_shift_bits)
300                };
301                let truncated_bits = layout_val.size.truncate(result_bits);
302                let result = Scalar::from_uint(truncated_bits, layout_val.size);
303                self.write_scalar(result, dest)?;
304            }
305            sym::copy => {
306                self.copy_intrinsic(&args[0], &args[1], &args[2], /*nonoverlapping*/ false)?;
307            }
308            sym::write_bytes => {
309                self.write_bytes_intrinsic(&args[0], &args[1], &args[2], "write_bytes")?;
310            }
311            sym::compare_bytes => {
312                let result = self.compare_bytes_intrinsic(&args[0], &args[1], &args[2])?;
313                self.write_scalar(result, dest)?;
314            }
315            sym::arith_offset => {
316                let ptr = self.read_pointer(&args[0])?;
317                let offset_count = self.read_target_isize(&args[1])?;
318                let pointee_ty = instance_args.type_at(0);
319
320                let pointee_size = i64::try_from(self.layout_of(pointee_ty)?.size.bytes()).unwrap();
321                let offset_bytes = offset_count.wrapping_mul(pointee_size);
322                let offset_ptr = ptr.wrapping_signed_offset(offset_bytes, self);
323                self.write_pointer(offset_ptr, dest)?;
324            }
325            sym::ptr_offset_from | sym::ptr_offset_from_unsigned => {
326                let a = self.read_pointer(&args[0])?;
327                let b = self.read_pointer(&args[1])?;
328
329                let usize_layout = self.layout_of(self.tcx.types.usize)?;
330                let isize_layout = self.layout_of(self.tcx.types.isize)?;
331
332                // Get offsets for both that are at least relative to the same base.
333                // With `OFFSET_IS_ADDR` this is trivial; without it we need either
334                // two integers or two pointers into the same allocation.
335                let (a_offset, b_offset, is_addr) = if M::Provenance::OFFSET_IS_ADDR {
336                    (a.addr().bytes(), b.addr().bytes(), /*is_addr*/ true)
337                } else {
338                    match (self.ptr_try_get_alloc_id(a, 0), self.ptr_try_get_alloc_id(b, 0)) {
339                        (Err(a), Err(b)) => {
340                            // Neither pointer points to an allocation, so they are both absolute.
341                            (a, b, /*is_addr*/ true)
342                        }
343                        (Ok((a_alloc_id, a_offset, _)), Ok((b_alloc_id, b_offset, _)))
344                            if a_alloc_id == b_alloc_id =>
345                        {
346                            // Found allocation for both, and it's the same.
347                            // Use these offsets for distance calculation.
348                            (a_offset.bytes(), b_offset.bytes(), /*is_addr*/ false)
349                        }
350                        _ => {
351                            // Not into the same allocation -- this is UB.
352                            throw_ub_custom!(
353                                fluent::const_eval_offset_from_different_allocations,
354                                name = intrinsic_name,
355                            );
356                        }
357                    }
358                };
359
360                // Compute distance: a - b.
361                let dist = {
362                    // Addresses are unsigned, so this is a `usize` computation. We have to do the
363                    // overflow check separately anyway.
364                    let (val, overflowed) = {
365                        let a_offset = ImmTy::from_uint(a_offset, usize_layout);
366                        let b_offset = ImmTy::from_uint(b_offset, usize_layout);
367                        self.binary_op(BinOp::SubWithOverflow, &a_offset, &b_offset)?
368                            .to_scalar_pair()
369                    };
370                    if overflowed.to_bool()? {
371                        // a < b
372                        if intrinsic_name == sym::ptr_offset_from_unsigned {
373                            throw_ub_custom!(
374                                fluent::const_eval_offset_from_unsigned_overflow,
375                                a_offset = a_offset,
376                                b_offset = b_offset,
377                                is_addr = is_addr,
378                            );
379                        }
380                        // The signed form of the intrinsic allows this. If we interpret the
381                        // difference as isize, we'll get the proper signed difference. If that
382                        // seems *positive* or equal to isize::MIN, they were more than isize::MAX apart.
383                        let dist = val.to_target_isize(self)?;
384                        if dist >= 0 || i128::from(dist) == self.pointer_size().signed_int_min() {
385                            throw_ub_custom!(
386                                fluent::const_eval_offset_from_underflow,
387                                name = intrinsic_name,
388                            );
389                        }
390                        dist
391                    } else {
392                        // b >= a
393                        let dist = val.to_target_isize(self)?;
394                        // If converting to isize produced a *negative* result, we had an overflow
395                        // because they were more than isize::MAX apart.
396                        if dist < 0 {
397                            throw_ub_custom!(
398                                fluent::const_eval_offset_from_overflow,
399                                name = intrinsic_name,
400                            );
401                        }
402                        dist
403                    }
404                };
405
406                // Check that the memory between them is dereferenceable at all, starting from the
407                // origin pointer: `dist` is `a - b`, so it is based on `b`.
408                self.check_ptr_access_signed(b, dist, CheckInAllocMsg::Dereferenceable)
409                    .map_err_kind(|_| {
410                        // This could mean they point to different allocations, or they point to the same allocation
411                        // but not the entire range between the pointers is in-bounds.
412                        if let Ok((a_alloc_id, ..)) = self.ptr_try_get_alloc_id(a, 0)
413                            && let Ok((b_alloc_id, ..)) = self.ptr_try_get_alloc_id(b, 0)
414                            && a_alloc_id == b_alloc_id
415                        {
416                            err_ub_custom!(
417                                fluent::const_eval_offset_from_out_of_bounds,
418                                name = intrinsic_name,
419                            )
420                        } else {
421                            err_ub_custom!(
422                                fluent::const_eval_offset_from_different_allocations,
423                                name = intrinsic_name,
424                            )
425                        }
426                    })?;
427                // Then check that this is also dereferenceable from `a`. This ensures that they are
428                // derived from the same allocation.
429                self.check_ptr_access_signed(
430                    a,
431                    dist.checked_neg().unwrap(), // i64::MIN is impossible as no allocation can be that large
432                    CheckInAllocMsg::Dereferenceable,
433                )
434                .map_err_kind(|_| {
435                    // Make the error more specific.
436                    err_ub_custom!(
437                        fluent::const_eval_offset_from_different_allocations,
438                        name = intrinsic_name,
439                    )
440                })?;
441
442                // Perform division by size to compute return value.
443                let ret_layout = if intrinsic_name == sym::ptr_offset_from_unsigned {
444                    assert!(0 <= dist && dist <= self.target_isize_max());
445                    usize_layout
446                } else {
447                    assert!(self.target_isize_min() <= dist && dist <= self.target_isize_max());
448                    isize_layout
449                };
450                let pointee_layout = self.layout_of(instance_args.type_at(0))?;
451                // If ret_layout is unsigned, we checked that so is the distance, so we are good.
452                let val = ImmTy::from_int(dist, ret_layout);
453                let size = ImmTy::from_int(pointee_layout.size.bytes(), ret_layout);
454                self.exact_div(&val, &size, dest)?;
455            }
456
457            sym::simd_insert => {
458                let index = u64::from(self.read_scalar(&args[1])?.to_u32()?);
459                let elem = &args[2];
460                let (input, input_len) = self.project_to_simd(&args[0])?;
461                let (dest, dest_len) = self.project_to_simd(dest)?;
462                assert_eq!(input_len, dest_len, "Return vector length must match input length");
463                // Bounds are not checked by typeck so we have to do it ourselves.
464                if index >= input_len {
465                    throw_ub_format!(
466                        "`simd_insert` index {index} is out-of-bounds of vector with length {input_len}"
467                    );
468                }
469
470                for i in 0..dest_len {
471                    let place = self.project_index(&dest, i)?;
472                    let value =
473                        if i == index { elem.clone() } else { self.project_index(&input, i)? };
474                    self.copy_op(&value, &place)?;
475                }
476            }
477            sym::simd_extract => {
478                let index = u64::from(self.read_scalar(&args[1])?.to_u32()?);
479                let (input, input_len) = self.project_to_simd(&args[0])?;
480                // Bounds are not checked by typeck so we have to do it ourselves.
481                if index >= input_len {
482                    throw_ub_format!(
483                        "`simd_extract` index {index} is out-of-bounds of vector with length {input_len}"
484                    );
485                }
486                self.copy_op(&self.project_index(&input, index)?, dest)?;
487            }
488            sym::black_box => {
489                // These just return their argument
490                self.copy_op(&args[0], dest)?;
491            }
492            sym::raw_eq => {
493                let result = self.raw_eq_intrinsic(&args[0], &args[1])?;
494                self.write_scalar(result, dest)?;
495            }
496            sym::typed_swap_nonoverlapping => {
497                self.typed_swap_nonoverlapping_intrinsic(&args[0], &args[1])?;
498            }
499
500            sym::vtable_size => {
501                let ptr = self.read_pointer(&args[0])?;
502                // `None` because we don't know which trait to expect here; any vtable is okay.
503                let (size, _align) = self.get_vtable_size_and_align(ptr, None)?;
504                self.write_scalar(Scalar::from_target_usize(size.bytes(), self), dest)?;
505            }
506            sym::vtable_align => {
507                let ptr = self.read_pointer(&args[0])?;
508                // `None` because we don't know which trait to expect here; any vtable is okay.
509                let (_size, align) = self.get_vtable_size_and_align(ptr, None)?;
510                self.write_scalar(Scalar::from_target_usize(align.bytes(), self), dest)?;
511            }
512
513            sym::minnumf16 => self.float_min_intrinsic::<Half>(args, dest)?,
514            sym::minnumf32 => self.float_min_intrinsic::<Single>(args, dest)?,
515            sym::minnumf64 => self.float_min_intrinsic::<Double>(args, dest)?,
516            sym::minnumf128 => self.float_min_intrinsic::<Quad>(args, dest)?,
517
518            sym::minimumf16 => self.float_minimum_intrinsic::<Half>(args, dest)?,
519            sym::minimumf32 => self.float_minimum_intrinsic::<Single>(args, dest)?,
520            sym::minimumf64 => self.float_minimum_intrinsic::<Double>(args, dest)?,
521            sym::minimumf128 => self.float_minimum_intrinsic::<Quad>(args, dest)?,
522
523            sym::maxnumf16 => self.float_max_intrinsic::<Half>(args, dest)?,
524            sym::maxnumf32 => self.float_max_intrinsic::<Single>(args, dest)?,
525            sym::maxnumf64 => self.float_max_intrinsic::<Double>(args, dest)?,
526            sym::maxnumf128 => self.float_max_intrinsic::<Quad>(args, dest)?,
527
528            sym::maximumf16 => self.float_maximum_intrinsic::<Half>(args, dest)?,
529            sym::maximumf32 => self.float_maximum_intrinsic::<Single>(args, dest)?,
530            sym::maximumf64 => self.float_maximum_intrinsic::<Double>(args, dest)?,
531            sym::maximumf128 => self.float_maximum_intrinsic::<Quad>(args, dest)?,
532
533            sym::copysignf16 => self.float_copysign_intrinsic::<Half>(args, dest)?,
534            sym::copysignf32 => self.float_copysign_intrinsic::<Single>(args, dest)?,
535            sym::copysignf64 => self.float_copysign_intrinsic::<Double>(args, dest)?,
536            sym::copysignf128 => self.float_copysign_intrinsic::<Quad>(args, dest)?,
537
538            sym::fabsf16 => self.float_abs_intrinsic::<Half>(args, dest)?,
539            sym::fabsf32 => self.float_abs_intrinsic::<Single>(args, dest)?,
540            sym::fabsf64 => self.float_abs_intrinsic::<Double>(args, dest)?,
541            sym::fabsf128 => self.float_abs_intrinsic::<Quad>(args, dest)?,
542
543            sym::floorf16 => self.float_round_intrinsic::<Half>(
544                args,
545                dest,
546                rustc_apfloat::Round::TowardNegative,
547            )?,
548            sym::floorf32 => self.float_round_intrinsic::<Single>(
549                args,
550                dest,
551                rustc_apfloat::Round::TowardNegative,
552            )?,
553            sym::floorf64 => self.float_round_intrinsic::<Double>(
554                args,
555                dest,
556                rustc_apfloat::Round::TowardNegative,
557            )?,
558            sym::floorf128 => self.float_round_intrinsic::<Quad>(
559                args,
560                dest,
561                rustc_apfloat::Round::TowardNegative,
562            )?,
563
564            sym::ceilf16 => self.float_round_intrinsic::<Half>(
565                args,
566                dest,
567                rustc_apfloat::Round::TowardPositive,
568            )?,
569            sym::ceilf32 => self.float_round_intrinsic::<Single>(
570                args,
571                dest,
572                rustc_apfloat::Round::TowardPositive,
573            )?,
574            sym::ceilf64 => self.float_round_intrinsic::<Double>(
575                args,
576                dest,
577                rustc_apfloat::Round::TowardPositive,
578            )?,
579            sym::ceilf128 => self.float_round_intrinsic::<Quad>(
580                args,
581                dest,
582                rustc_apfloat::Round::TowardPositive,
583            )?,
584
585            sym::truncf16 => {
586                self.float_round_intrinsic::<Half>(args, dest, rustc_apfloat::Round::TowardZero)?
587            }
588            sym::truncf32 => {
589                self.float_round_intrinsic::<Single>(args, dest, rustc_apfloat::Round::TowardZero)?
590            }
591            sym::truncf64 => {
592                self.float_round_intrinsic::<Double>(args, dest, rustc_apfloat::Round::TowardZero)?
593            }
594            sym::truncf128 => {
595                self.float_round_intrinsic::<Quad>(args, dest, rustc_apfloat::Round::TowardZero)?
596            }
597
598            sym::roundf16 => self.float_round_intrinsic::<Half>(
599                args,
600                dest,
601                rustc_apfloat::Round::NearestTiesToAway,
602            )?,
603            sym::roundf32 => self.float_round_intrinsic::<Single>(
604                args,
605                dest,
606                rustc_apfloat::Round::NearestTiesToAway,
607            )?,
608            sym::roundf64 => self.float_round_intrinsic::<Double>(
609                args,
610                dest,
611                rustc_apfloat::Round::NearestTiesToAway,
612            )?,
613            sym::roundf128 => self.float_round_intrinsic::<Quad>(
614                args,
615                dest,
616                rustc_apfloat::Round::NearestTiesToAway,
617            )?,
618
619            sym::round_ties_even_f16 => self.float_round_intrinsic::<Half>(
620                args,
621                dest,
622                rustc_apfloat::Round::NearestTiesToEven,
623            )?,
624            sym::round_ties_even_f32 => self.float_round_intrinsic::<Single>(
625                args,
626                dest,
627                rustc_apfloat::Round::NearestTiesToEven,
628            )?,
629            sym::round_ties_even_f64 => self.float_round_intrinsic::<Double>(
630                args,
631                dest,
632                rustc_apfloat::Round::NearestTiesToEven,
633            )?,
634            sym::round_ties_even_f128 => self.float_round_intrinsic::<Quad>(
635                args,
636                dest,
637                rustc_apfloat::Round::NearestTiesToEven,
638            )?,
639
640            // Unsupported intrinsic: skip the return_to_block below.
641            _ => return interp_ok(false),
642        }
643
644        trace!("{:?}", self.dump_place(&dest.clone().into()));
645        self.return_to_block(ret)?;
646        interp_ok(true)
647    }
648
649    pub(super) fn eval_nondiverging_intrinsic(
650        &mut self,
651        intrinsic: &NonDivergingIntrinsic<'tcx>,
652    ) -> InterpResult<'tcx> {
653        match intrinsic {
654            NonDivergingIntrinsic::Assume(op) => {
655                let op = self.eval_operand(op, None)?;
656                let cond = self.read_scalar(&op)?.to_bool()?;
657                if !cond {
658                    throw_ub_custom!(fluent::const_eval_assume_false);
659                }
660                interp_ok(())
661            }
662            NonDivergingIntrinsic::CopyNonOverlapping(mir::CopyNonOverlapping {
663                count,
664                src,
665                dst,
666            }) => {
667                let src = self.eval_operand(src, None)?;
668                let dst = self.eval_operand(dst, None)?;
669                let count = self.eval_operand(count, None)?;
670                self.copy_intrinsic(&src, &dst, &count, /* nonoverlapping */ true)
671            }
672        }
673    }
674
675    pub fn numeric_intrinsic(
676        &self,
677        name: Symbol,
678        val: Scalar<M::Provenance>,
679        layout: TyAndLayout<'tcx>,
680        ret_layout: TyAndLayout<'tcx>,
681    ) -> InterpResult<'tcx, Scalar<M::Provenance>> {
682        assert!(layout.ty.is_integral(), "invalid type for numeric intrinsic: {}", layout.ty);
683        let bits = val.to_bits(layout.size)?; // these operations all ignore the sign
684        let extra = 128 - u128::from(layout.size.bits());
685        let bits_out = match name {
686            sym::ctpop => u128::from(bits.count_ones()),
687            sym::ctlz_nonzero | sym::cttz_nonzero if bits == 0 => {
688                throw_ub_custom!(fluent::const_eval_call_nonzero_intrinsic, name = name,);
689            }
690            sym::ctlz | sym::ctlz_nonzero => u128::from(bits.leading_zeros()) - extra,
691            sym::cttz | sym::cttz_nonzero => u128::from((bits << extra).trailing_zeros()) - extra,
692            sym::bswap => {
693                assert_eq!(layout, ret_layout);
694                (bits << extra).swap_bytes()
695            }
696            sym::bitreverse => {
697                assert_eq!(layout, ret_layout);
698                (bits << extra).reverse_bits()
699            }
700            _ => bug!("not a numeric intrinsic: {}", name),
701        };
702        interp_ok(Scalar::from_uint(bits_out, ret_layout.size))
703    }
704
705    pub fn exact_div(
706        &mut self,
707        a: &ImmTy<'tcx, M::Provenance>,
708        b: &ImmTy<'tcx, M::Provenance>,
709        dest: &PlaceTy<'tcx, M::Provenance>,
710    ) -> InterpResult<'tcx> {
711        assert_eq!(a.layout.ty, b.layout.ty);
712        assert_matches!(a.layout.ty.kind(), ty::Int(..) | ty::Uint(..));
713
714        // Performs an exact division, resulting in undefined behavior where
715        // `x % y != 0` or `y == 0` or `x == T::MIN && y == -1`.
716        // First, check x % y != 0 (or if that computation overflows).
717        let rem = self.binary_op(BinOp::Rem, a, b)?;
718        // sign does not matter for 0 test, so `to_bits` is fine
719        if rem.to_scalar().to_bits(a.layout.size)? != 0 {
720            throw_ub_custom!(
721                fluent::const_eval_exact_div_has_remainder,
722                a = format!("{a}"),
723                b = format!("{b}")
724            )
725        }
726        // `Rem` says this is all right, so we can let `Div` do its job.
727        let res = self.binary_op(BinOp::Div, a, b)?;
728        self.write_immediate(*res, dest)
729    }
730
731    pub fn saturating_arith(
732        &self,
733        mir_op: BinOp,
734        l: &ImmTy<'tcx, M::Provenance>,
735        r: &ImmTy<'tcx, M::Provenance>,
736    ) -> InterpResult<'tcx, Scalar<M::Provenance>> {
737        assert_eq!(l.layout.ty, r.layout.ty);
738        assert_matches!(l.layout.ty.kind(), ty::Int(..) | ty::Uint(..));
739        assert_matches!(mir_op, BinOp::Add | BinOp::Sub);
740
741        let (val, overflowed) =
742            self.binary_op(mir_op.wrapping_to_overflowing().unwrap(), l, r)?.to_scalar_pair();
743        interp_ok(if overflowed.to_bool()? {
744            let size = l.layout.size;
745            if l.layout.backend_repr.is_signed() {
746                // For signed ints the saturated value depends on the sign of the first
747                // term since the sign of the second term can be inferred from this and
748                // the fact that the operation has overflowed (if either is 0 no
749                // overflow can occur)
750                let first_term: i128 = l.to_scalar().to_int(l.layout.size)?;
751                if first_term >= 0 {
752                    // Negative overflow not possible since the positive first term
753                    // can only increase an (in range) negative term for addition
754                    // or corresponding negated positive term for subtraction.
755                    Scalar::from_int(size.signed_int_max(), size)
756                } else {
757                    // Positive overflow not possible for similar reason.
758                    Scalar::from_int(size.signed_int_min(), size)
759                }
760            } else {
761                // unsigned
762                if matches!(mir_op, BinOp::Add) {
763                    // max unsigned
764                    Scalar::from_uint(size.unsigned_int_max(), size)
765                } else {
766                    // underflow to 0
767                    Scalar::from_uint(0u128, size)
768                }
769            }
770        } else {
771            val
772        })
773    }
774
775    /// Offsets a pointer by some multiple of its type, returning an error if the pointer leaves its
776    /// allocation.
777    pub fn ptr_offset_inbounds(
778        &self,
779        ptr: Pointer<Option<M::Provenance>>,
780        offset_bytes: i64,
781    ) -> InterpResult<'tcx, Pointer<Option<M::Provenance>>> {
782        // The offset must be in bounds starting from `ptr`.
783        self.check_ptr_access_signed(
784            ptr,
785            offset_bytes,
786            CheckInAllocMsg::InboundsPointerArithmetic,
787        )?;
788        // This also implies that there is no overflow, so we are done.
789        interp_ok(ptr.wrapping_signed_offset(offset_bytes, self))
790    }
791
792    /// Copy `count*size_of::<T>()` many bytes from `*src` to `*dst`.
793    pub(crate) fn copy_intrinsic(
794        &mut self,
795        src: &OpTy<'tcx, <M as Machine<'tcx>>::Provenance>,
796        dst: &OpTy<'tcx, <M as Machine<'tcx>>::Provenance>,
797        count: &OpTy<'tcx, <M as Machine<'tcx>>::Provenance>,
798        nonoverlapping: bool,
799    ) -> InterpResult<'tcx> {
800        let count = self.read_target_usize(count)?;
801        let layout = self.layout_of(src.layout.ty.builtin_deref(true).unwrap())?;
802        let (size, align) = (layout.size, layout.align.abi);
803
804        let size = self.compute_size_in_bytes(size, count).ok_or_else(|| {
805            err_ub_custom!(
806                fluent::const_eval_size_overflow,
807                name = if nonoverlapping { "copy_nonoverlapping" } else { "copy" }
808            )
809        })?;
810
811        let src = self.read_pointer(src)?;
812        let dst = self.read_pointer(dst)?;
813
814        self.check_ptr_align(src, align)?;
815        self.check_ptr_align(dst, align)?;
816
817        self.mem_copy(src, dst, size, nonoverlapping)
818    }
819
820    /// Does a *typed* swap of `*left` and `*right`.
821    fn typed_swap_nonoverlapping_intrinsic(
822        &mut self,
823        left: &OpTy<'tcx, <M as Machine<'tcx>>::Provenance>,
824        right: &OpTy<'tcx, <M as Machine<'tcx>>::Provenance>,
825    ) -> InterpResult<'tcx> {
826        let left = self.deref_pointer(left)?;
827        let right = self.deref_pointer(right)?;
828        assert_eq!(left.layout, right.layout);
829        assert!(left.layout.is_sized());
830        let kind = MemoryKind::Stack;
831        let temp = self.allocate(left.layout, kind)?;
832        self.copy_op(&left, &temp)?; // checks alignment of `left`
833
834        // We want to always enforce non-overlapping, even if this is a scalar type.
835        // Therefore we directly use the underlying `mem_copy` here.
836        self.mem_copy(right.ptr(), left.ptr(), left.layout.size, /*nonoverlapping*/ true)?;
837        // This means we also need to do the validation of the value that used to be in `right`
838        // ourselves. This value is now in `left.` The one that started out in `left` already got
839        // validated by the copy above.
840        if M::enforce_validity(self, left.layout) {
841            self.validate_operand(
842                &left.clone().into(),
843                M::enforce_validity_recursively(self, left.layout),
844                /*reset_provenance_and_padding*/ true,
845            )?;
846        }
847
848        self.copy_op(&temp, &right)?; // checks alignment of `right`
849
850        self.deallocate_ptr(temp.ptr(), None, kind)?;
851        interp_ok(())
852    }
853
854    pub fn write_bytes_intrinsic(
855        &mut self,
856        dst: &OpTy<'tcx, <M as Machine<'tcx>>::Provenance>,
857        byte: &OpTy<'tcx, <M as Machine<'tcx>>::Provenance>,
858        count: &OpTy<'tcx, <M as Machine<'tcx>>::Provenance>,
859        name: &'static str,
860    ) -> InterpResult<'tcx> {
861        let layout = self.layout_of(dst.layout.ty.builtin_deref(true).unwrap())?;
862
863        let dst = self.read_pointer(dst)?;
864        let byte = self.read_scalar(byte)?.to_u8()?;
865        let count = self.read_target_usize(count)?;
866
867        // `checked_mul` enforces a too small bound (the correct one would probably be target_isize_max),
868        // but no actual allocation can be big enough for the difference to be noticeable.
869        let len = self
870            .compute_size_in_bytes(layout.size, count)
871            .ok_or_else(|| err_ub_custom!(fluent::const_eval_size_overflow, name = name))?;
872
873        let bytes = std::iter::repeat(byte).take(len.bytes_usize());
874        self.write_bytes_ptr(dst, bytes)
875    }
876
877    pub(crate) fn compare_bytes_intrinsic(
878        &mut self,
879        left: &OpTy<'tcx, <M as Machine<'tcx>>::Provenance>,
880        right: &OpTy<'tcx, <M as Machine<'tcx>>::Provenance>,
881        byte_count: &OpTy<'tcx, <M as Machine<'tcx>>::Provenance>,
882    ) -> InterpResult<'tcx, Scalar<M::Provenance>> {
883        let left = self.read_pointer(left)?;
884        let right = self.read_pointer(right)?;
885        let n = Size::from_bytes(self.read_target_usize(byte_count)?);
886
887        let left_bytes = self.read_bytes_ptr_strip_provenance(left, n)?;
888        let right_bytes = self.read_bytes_ptr_strip_provenance(right, n)?;
889
890        // `Ordering`'s discriminants are -1/0/+1, so casting does the right thing.
891        let result = Ord::cmp(left_bytes, right_bytes) as i32;
892        interp_ok(Scalar::from_i32(result))
893    }
894
895    pub(crate) fn raw_eq_intrinsic(
896        &mut self,
897        lhs: &OpTy<'tcx, <M as Machine<'tcx>>::Provenance>,
898        rhs: &OpTy<'tcx, <M as Machine<'tcx>>::Provenance>,
899    ) -> InterpResult<'tcx, Scalar<M::Provenance>> {
900        let layout = self.layout_of(lhs.layout.ty.builtin_deref(true).unwrap())?;
901        assert!(layout.is_sized());
902
903        let get_bytes = |this: &InterpCx<'tcx, M>,
904                         op: &OpTy<'tcx, <M as Machine<'tcx>>::Provenance>|
905         -> InterpResult<'tcx, &[u8]> {
906            let ptr = this.read_pointer(op)?;
907            this.check_ptr_align(ptr, layout.align.abi)?;
908            let Some(alloc_ref) = self.get_ptr_alloc(ptr, layout.size)? else {
909                // zero-sized access
910                return interp_ok(&[]);
911            };
912            alloc_ref.get_bytes_strip_provenance()
913        };
914
915        let lhs_bytes = get_bytes(self, lhs)?;
916        let rhs_bytes = get_bytes(self, rhs)?;
917        interp_ok(Scalar::from_bool(lhs_bytes == rhs_bytes))
918    }
919
920    fn float_min_intrinsic<F>(
921        &mut self,
922        args: &[OpTy<'tcx, M::Provenance>],
923        dest: &PlaceTy<'tcx, M::Provenance>,
924    ) -> InterpResult<'tcx, ()>
925    where
926        F: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F> + Into<Scalar<M::Provenance>>,
927    {
928        let a: F = self.read_scalar(&args[0])?.to_float()?;
929        let b: F = self.read_scalar(&args[1])?.to_float()?;
930        let res = if a == b {
931            // They are definitely not NaN (those are never equal), but they could be `+0` and `-0`.
932            // Let the machine decide which one to return.
933            M::equal_float_min_max(self, a, b)
934        } else {
935            self.adjust_nan(a.min(b), &[a, b])
936        };
937        self.write_scalar(res, dest)?;
938        interp_ok(())
939    }
940
941    fn float_max_intrinsic<F>(
942        &mut self,
943        args: &[OpTy<'tcx, M::Provenance>],
944        dest: &PlaceTy<'tcx, M::Provenance>,
945    ) -> InterpResult<'tcx, ()>
946    where
947        F: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F> + Into<Scalar<M::Provenance>>,
948    {
949        let a: F = self.read_scalar(&args[0])?.to_float()?;
950        let b: F = self.read_scalar(&args[1])?.to_float()?;
951        let res = if a == b {
952            // They are definitely not NaN (those are never equal), but they could be `+0` and `-0`.
953            // Let the machine decide which one to return.
954            M::equal_float_min_max(self, a, b)
955        } else {
956            self.adjust_nan(a.max(b), &[a, b])
957        };
958        self.write_scalar(res, dest)?;
959        interp_ok(())
960    }
961
962    fn float_minimum_intrinsic<F>(
963        &mut self,
964        args: &[OpTy<'tcx, M::Provenance>],
965        dest: &PlaceTy<'tcx, M::Provenance>,
966    ) -> InterpResult<'tcx, ()>
967    where
968        F: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F> + Into<Scalar<M::Provenance>>,
969    {
970        let a: F = self.read_scalar(&args[0])?.to_float()?;
971        let b: F = self.read_scalar(&args[1])?.to_float()?;
972        let res = a.minimum(b);
973        let res = self.adjust_nan(res, &[a, b]);
974        self.write_scalar(res, dest)?;
975        interp_ok(())
976    }
977
978    fn float_maximum_intrinsic<F>(
979        &mut self,
980        args: &[OpTy<'tcx, M::Provenance>],
981        dest: &PlaceTy<'tcx, M::Provenance>,
982    ) -> InterpResult<'tcx, ()>
983    where
984        F: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F> + Into<Scalar<M::Provenance>>,
985    {
986        let a: F = self.read_scalar(&args[0])?.to_float()?;
987        let b: F = self.read_scalar(&args[1])?.to_float()?;
988        let res = a.maximum(b);
989        let res = self.adjust_nan(res, &[a, b]);
990        self.write_scalar(res, dest)?;
991        interp_ok(())
992    }
993
994    fn float_copysign_intrinsic<F>(
995        &mut self,
996        args: &[OpTy<'tcx, M::Provenance>],
997        dest: &PlaceTy<'tcx, M::Provenance>,
998    ) -> InterpResult<'tcx, ()>
999    where
1000        F: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F> + Into<Scalar<M::Provenance>>,
1001    {
1002        let a: F = self.read_scalar(&args[0])?.to_float()?;
1003        let b: F = self.read_scalar(&args[1])?.to_float()?;
1004        // bitwise, no NaN adjustments
1005        self.write_scalar(a.copy_sign(b), dest)?;
1006        interp_ok(())
1007    }
1008
1009    fn float_abs_intrinsic<F>(
1010        &mut self,
1011        args: &[OpTy<'tcx, M::Provenance>],
1012        dest: &PlaceTy<'tcx, M::Provenance>,
1013    ) -> InterpResult<'tcx, ()>
1014    where
1015        F: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F> + Into<Scalar<M::Provenance>>,
1016    {
1017        let x: F = self.read_scalar(&args[0])?.to_float()?;
1018        // bitwise, no NaN adjustments
1019        self.write_scalar(x.abs(), dest)?;
1020        interp_ok(())
1021    }
1022
1023    fn float_round_intrinsic<F>(
1024        &mut self,
1025        args: &[OpTy<'tcx, M::Provenance>],
1026        dest: &PlaceTy<'tcx, M::Provenance>,
1027        mode: rustc_apfloat::Round,
1028    ) -> InterpResult<'tcx, ()>
1029    where
1030        F: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F> + Into<Scalar<M::Provenance>>,
1031    {
1032        let x: F = self.read_scalar(&args[0])?.to_float()?;
1033        let res = x.round_to_integral(mode).value;
1034        let res = self.adjust_nan(res, &[x]);
1035        self.write_scalar(res, dest)?;
1036        interp_ok(())
1037    }
1038}