1 //! Intrinsics and other functions that the miri engine executes without
2 //! looking at their MIR. Intrinsics/functions supported here are shared by CTFE
5 use std::convert::TryFrom;
7 use rustc_hir::def_id::DefId;
8 use rustc_middle::mir::{
11 Allocation, ConstAllocation, ConstValue, GlobalId, InterpResult, PointerArithmetic, Scalar,
13 BinOp, NonDivergingIntrinsic,
16 use rustc_middle::ty::layout::LayoutOf as _;
17 use rustc_middle::ty::subst::SubstsRef;
18 use rustc_middle::ty::{Ty, TyCtxt};
19 use rustc_span::symbol::{sym, Symbol};
20 use rustc_target::abi::{Abi, Align, Primitive, Size};
23 util::ensure_monomorphic_enough, CheckInAllocMsg, ImmTy, InterpCx, Machine, OpTy, PlaceTy,
29 fn numeric_intrinsic<Prov>(name: Symbol, bits: u128, kind: Primitive) -> Scalar<Prov> {
30 let size = match kind {
31 Primitive::Int(integer, _) => integer.size(),
32 _ => bug!("invalid `{}` argument: {:?}", name, bits),
34 let extra = 128 - u128::from(size.bits());
35 let bits_out = match name {
36 sym::ctpop => u128::from(bits.count_ones()),
37 sym::ctlz => u128::from(bits.leading_zeros()) - extra,
38 sym::cttz => u128::from((bits << extra).trailing_zeros()) - extra,
39 sym::bswap => (bits << extra).swap_bytes(),
40 sym::bitreverse => (bits << extra).reverse_bits(),
41 _ => bug!("not a numeric intrinsic: {}", name),
43 Scalar::from_uint(bits_out, size)
46 /// Directly returns an `Allocation` containing an absolute path representation of the given type.
47 pub(crate) fn alloc_type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ConstAllocation<'tcx> {
48 let path = crate::util::type_name(tcx, ty);
49 let alloc = Allocation::from_bytes_byte_aligned_immutable(path.into_bytes());
50 tcx.intern_const_alloc(alloc)
53 /// The logic for all nullary intrinsics is implemented here. These intrinsics don't get evaluated
54 /// inside an `InterpCx` and instead have their value computed directly from rustc internal info.
55 pub(crate) fn eval_nullary_intrinsic<'tcx>(
57 param_env: ty::ParamEnv<'tcx>,
59 substs: SubstsRef<'tcx>,
60 ) -> InterpResult<'tcx, ConstValue<'tcx>> {
61 let tp_ty = substs.type_at(0);
62 let name = tcx.item_name(def_id);
65 ensure_monomorphic_enough(tcx, tp_ty)?;
66 let alloc = alloc_type_name(tcx, tp_ty);
67 ConstValue::Slice { data: alloc, start: 0, end: alloc.inner().len() }
70 ensure_monomorphic_enough(tcx, tp_ty)?;
71 ConstValue::from_bool(tp_ty.needs_drop(tcx, param_env))
73 sym::pref_align_of => {
74 // Correctly handles non-monomorphic calls, so there is no need for ensure_monomorphic_enough.
75 let layout = tcx.layout_of(param_env.and(tp_ty)).map_err(|e| err_inval!(Layout(e)))?;
76 ConstValue::from_machine_usize(layout.align.pref.bytes(), &tcx)
79 ensure_monomorphic_enough(tcx, tp_ty)?;
80 ConstValue::from_u64(tcx.type_id_hash(tp_ty))
82 sym::variant_count => match tp_ty.kind() {
83 // Correctly handles non-monomorphic calls, so there is no need for ensure_monomorphic_enough.
84 ty::Adt(ref adt, _) => {
85 ConstValue::from_machine_usize(adt.variants().len() as u64, &tcx)
91 | ty::Infer(_) => throw_inval!(TooGeneric),
92 ty::Bound(_, _) => bug!("bound ty during ctfe"),
106 | ty::Dynamic(_, _, _)
108 | ty::Generator(_, _, _)
109 | ty::GeneratorWitness(_)
112 | ty::Error(_) => ConstValue::from_machine_usize(0u64, &tcx),
114 other => bug!("`{}` is not a zero arg intrinsic", other),
118 impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
119 /// Returns `true` if emulation happened.
120 /// Here we implement the intrinsics that are common to all Miri instances; individual machines can add their own
121 /// intrinsic handling.
122 pub fn emulate_intrinsic(
124 instance: ty::Instance<'tcx>,
125 args: &[OpTy<'tcx, M::Provenance>],
126 dest: &PlaceTy<'tcx, M::Provenance>,
127 ret: Option<mir::BasicBlock>,
128 ) -> InterpResult<'tcx, bool> {
129 let substs = instance.substs;
130 let intrinsic_name = self.tcx.item_name(instance.def_id());
132 // First handle intrinsics without return place.
133 let ret = match ret {
134 None => match intrinsic_name {
135 sym::transmute => throw_ub_format!("transmuting to uninhabited type"),
136 sym::abort => M::abort(self, "the program aborted execution".to_owned())?,
137 // Unsupported diverging intrinsic.
138 _ => return Ok(false),
143 match intrinsic_name {
144 sym::caller_location => {
145 let span = self.find_closest_untracked_caller_location();
146 let location = self.alloc_caller_location_for_span(span);
147 self.write_immediate(location.to_ref(self), dest)?;
150 sym::min_align_of_val | sym::size_of_val => {
151 // Avoid `deref_operand` -- this is not a deref, the ptr does not have to be
153 let place = self.ref_to_mplace(&self.read_immediate(&args[0])?)?;
154 let (size, align) = self
155 .size_and_align_of_mplace(&place)?
156 .ok_or_else(|| err_unsup_format!("`extern type` does not have known layout"))?;
158 let result = match intrinsic_name {
159 sym::min_align_of_val => align.bytes(),
160 sym::size_of_val => size.bytes(),
164 self.write_scalar(Scalar::from_machine_usize(result, self), dest)?;
171 | sym::variant_count => {
172 let gid = GlobalId { instance, promoted: None };
173 let ty = match intrinsic_name {
174 sym::pref_align_of | sym::variant_count => self.tcx.types.usize,
175 sym::needs_drop => self.tcx.types.bool,
176 sym::type_id => self.tcx.types.u64,
177 sym::type_name => self.tcx.mk_static_str(),
181 self.tcx.const_eval_global_id(self.param_env, gid, Some(self.tcx.span))?;
182 let val = self.const_val_to_op(val, ty, Some(dest.layout))?;
183 self.copy_op(&val, dest, /*allow_transmute*/ false)?;
192 | sym::bitreverse => {
193 let ty = substs.type_at(0);
194 let layout_of = self.layout_of(ty)?;
195 let val = self.read_scalar(&args[0])?;
196 let bits = val.to_bits(layout_of.size)?;
197 let kind = match layout_of.abi {
198 Abi::Scalar(scalar) => scalar.primitive(),
201 "{} called on invalid type {:?}",
206 let (nonzero, intrinsic_name) = match intrinsic_name {
207 sym::cttz_nonzero => (true, sym::cttz),
208 sym::ctlz_nonzero => (true, sym::ctlz),
209 other => (false, other),
211 if nonzero && bits == 0 {
212 throw_ub_format!("`{}_nonzero` called on 0", intrinsic_name);
214 let out_val = numeric_intrinsic(intrinsic_name, bits, kind);
215 self.write_scalar(out_val, dest)?;
217 sym::add_with_overflow | sym::sub_with_overflow | sym::mul_with_overflow => {
218 let lhs = self.read_immediate(&args[0])?;
219 let rhs = self.read_immediate(&args[1])?;
220 let bin_op = match intrinsic_name {
221 sym::add_with_overflow => BinOp::Add,
222 sym::sub_with_overflow => BinOp::Sub,
223 sym::mul_with_overflow => BinOp::Mul,
226 self.binop_with_overflow(
227 bin_op, /*force_overflow_checks*/ true, &lhs, &rhs, dest,
230 sym::saturating_add | sym::saturating_sub => {
231 let l = self.read_immediate(&args[0])?;
232 let r = self.read_immediate(&args[1])?;
233 let val = self.saturating_arith(
234 if intrinsic_name == sym::saturating_add { BinOp::Add } else { BinOp::Sub },
238 self.write_scalar(val, dest)?;
240 sym::discriminant_value => {
241 let place = self.deref_operand(&args[0])?;
242 let discr_val = self.read_discriminant(&place.into())?.0;
243 self.write_scalar(discr_val, dest)?;
251 | sym::unchecked_rem => {
252 let l = self.read_immediate(&args[0])?;
253 let r = self.read_immediate(&args[1])?;
254 let bin_op = match intrinsic_name {
255 sym::unchecked_shl => BinOp::Shl,
256 sym::unchecked_shr => BinOp::Shr,
257 sym::unchecked_add => BinOp::Add,
258 sym::unchecked_sub => BinOp::Sub,
259 sym::unchecked_mul => BinOp::Mul,
260 sym::unchecked_div => BinOp::Div,
261 sym::unchecked_rem => BinOp::Rem,
264 let (val, overflowed, _ty) = self.overflowing_binary_op(bin_op, &l, &r)?;
266 let layout = self.layout_of(substs.type_at(0))?;
267 let r_val = r.to_scalar().to_bits(layout.size)?;
268 if let sym::unchecked_shl | sym::unchecked_shr = intrinsic_name {
269 throw_ub_format!("overflowing shift by {} in `{}`", r_val, intrinsic_name);
271 throw_ub_format!("overflow executing `{}`", intrinsic_name);
274 self.write_scalar(val, dest)?;
276 sym::rotate_left | sym::rotate_right => {
277 // rotate_left: (X << (S % BW)) | (X >> ((BW - S) % BW))
278 // rotate_right: (X << ((BW - S) % BW)) | (X >> (S % BW))
279 let layout = self.layout_of(substs.type_at(0))?;
280 let val = self.read_scalar(&args[0])?;
281 let val_bits = val.to_bits(layout.size)?;
282 let raw_shift = self.read_scalar(&args[1])?;
283 let raw_shift_bits = raw_shift.to_bits(layout.size)?;
284 let width_bits = u128::from(layout.size.bits());
285 let shift_bits = raw_shift_bits % width_bits;
286 let inv_shift_bits = (width_bits - shift_bits) % width_bits;
287 let result_bits = if intrinsic_name == sym::rotate_left {
288 (val_bits << shift_bits) | (val_bits >> inv_shift_bits)
290 (val_bits >> shift_bits) | (val_bits << inv_shift_bits)
292 let truncated_bits = self.truncate(result_bits, layout);
293 let result = Scalar::from_uint(truncated_bits, layout.size);
294 self.write_scalar(result, dest)?;
297 self.copy_intrinsic(&args[0], &args[1], &args[2], /*nonoverlapping*/ false)?;
299 sym::write_bytes => {
300 self.write_bytes_intrinsic(&args[0], &args[1], &args[2])?;
303 let ptr = self.read_pointer(&args[0])?;
304 let offset_count = self.read_scalar(&args[1])?.to_machine_isize(self)?;
305 let pointee_ty = substs.type_at(0);
307 let offset_ptr = self.ptr_offset_inbounds(ptr, pointee_ty, offset_count)?;
308 self.write_pointer(offset_ptr, dest)?;
310 sym::arith_offset => {
311 let ptr = self.read_pointer(&args[0])?;
312 let offset_count = self.read_scalar(&args[1])?.to_machine_isize(self)?;
313 let pointee_ty = substs.type_at(0);
315 let pointee_size = i64::try_from(self.layout_of(pointee_ty)?.size.bytes()).unwrap();
316 let offset_bytes = offset_count.wrapping_mul(pointee_size);
317 let offset_ptr = ptr.wrapping_signed_offset(offset_bytes, self);
318 self.write_pointer(offset_ptr, dest)?;
320 sym::ptr_offset_from | sym::ptr_offset_from_unsigned => {
321 let a = self.read_pointer(&args[0])?;
322 let b = self.read_pointer(&args[1])?;
324 let usize_layout = self.layout_of(self.tcx.types.usize)?;
325 let isize_layout = self.layout_of(self.tcx.types.isize)?;
327 // Get offsets for both that are at least relative to the same base.
328 let (a_offset, b_offset) =
329 match (self.ptr_try_get_alloc_id(a), self.ptr_try_get_alloc_id(b)) {
330 (Err(a), Err(b)) => {
331 // Neither pointer points to an allocation.
332 // If these are inequal or null, this *will* fail the deref check below.
335 (Err(_), _) | (_, Err(_)) => {
336 // We managed to find a valid allocation for one pointer, but not the other.
337 // That means they are definitely not pointing to the same allocation.
339 "`{}` called on pointers into different allocations",
343 (Ok((a_alloc_id, a_offset, _)), Ok((b_alloc_id, b_offset, _))) => {
344 // Found allocation for both. They must be into the same allocation.
345 if a_alloc_id != b_alloc_id {
347 "`{}` called on pointers into different allocations",
351 // Use these offsets for distance calculation.
352 (a_offset.bytes(), b_offset.bytes())
358 // Addresses are unsigned, so this is a `usize` computation. We have to do the
359 // overflow check separately anyway.
360 let (val, overflowed, _ty) = {
361 let a_offset = ImmTy::from_uint(a_offset, usize_layout);
362 let b_offset = ImmTy::from_uint(b_offset, usize_layout);
363 self.overflowing_binary_op(BinOp::Sub, &a_offset, &b_offset)?
367 if intrinsic_name == sym::ptr_offset_from_unsigned {
369 "`{}` called when first pointer has smaller offset than second: {} < {}",
375 // The signed form of the intrinsic allows this. If we interpret the
376 // difference as isize, we'll get the proper signed difference. If that
377 // seems *positive*, they were more than isize::MAX apart.
378 let dist = val.to_machine_isize(self)?;
381 "`{}` called when first pointer is too far before second",
388 let dist = val.to_machine_isize(self)?;
389 // If converting to isize produced a *negative* result, we had an overflow
390 // because they were more than isize::MAX apart.
393 "`{}` called when first pointer is too far ahead of second",
401 // Check that the range between them is dereferenceable ("in-bounds or one past the
402 // end of the same allocation"). This is like the check in ptr_offset_inbounds.
403 let min_ptr = if dist >= 0 { b } else { a };
404 self.check_ptr_access_align(
406 Size::from_bytes(dist.unsigned_abs()),
408 CheckInAllocMsg::OffsetFromTest,
411 // Perform division by size to compute return value.
412 let ret_layout = if intrinsic_name == sym::ptr_offset_from_unsigned {
413 assert!(0 <= dist && dist <= self.machine_isize_max());
416 assert!(self.machine_isize_min() <= dist && dist <= self.machine_isize_max());
419 let pointee_layout = self.layout_of(substs.type_at(0))?;
420 // If ret_layout is unsigned, we checked that so is the distance, so we are good.
421 let val = ImmTy::from_int(dist, ret_layout);
422 let size = ImmTy::from_int(pointee_layout.size.bytes(), ret_layout);
423 self.exact_div(&val, &size, dest)?;
427 self.copy_op(&args[0], dest, /*allow_transmute*/ true)?;
429 sym::assert_inhabited | sym::assert_zero_valid | sym::assert_uninit_valid => {
430 let ty = instance.substs.type_at(0);
431 let layout = self.layout_of(ty)?;
433 // For *all* intrinsics we first check `is_uninhabited` to give a more specific
435 if layout.abi.is_uninhabited() {
436 // The run-time intrinsic panics just to get a good backtrace; here we abort
437 // since there is no problem showing a backtrace even for aborts.
441 "aborted execution: attempted to instantiate uninhabited type `{}`",
447 if intrinsic_name == sym::assert_zero_valid {
448 let should_panic = !self.tcx.permits_zero_init(layout);
454 "aborted execution: attempted to zero-initialize type `{}`, which is invalid",
461 if intrinsic_name == sym::assert_uninit_valid {
462 let should_panic = !self.tcx.permits_uninit_init(layout);
468 "aborted execution: attempted to leave type `{}` uninitialized, which is invalid",
475 sym::simd_insert => {
476 let index = u64::from(self.read_scalar(&args[1])?.to_u32()?);
478 let (input, input_len) = self.operand_to_simd(&args[0])?;
479 let (dest, dest_len) = self.place_to_simd(dest)?;
480 assert_eq!(input_len, dest_len, "Return vector length must match input length");
483 "Index `{}` must be in bounds of vector with length {}`",
488 for i in 0..dest_len {
489 let place = self.mplace_index(&dest, i)?;
490 let value = if i == index {
493 self.mplace_index(&input, i)?.into()
495 self.copy_op(&value, &place.into(), /*allow_transmute*/ false)?;
498 sym::simd_extract => {
499 let index = u64::from(self.read_scalar(&args[1])?.to_u32()?);
500 let (input, input_len) = self.operand_to_simd(&args[0])?;
503 "index `{}` must be in bounds of vector with length `{}`",
508 &self.mplace_index(&input, index)?.into(),
510 /*allow_transmute*/ false,
513 sym::likely | sym::unlikely | sym::black_box => {
514 // These just return their argument
515 self.copy_op(&args[0], dest, /*allow_transmute*/ false)?;
518 let result = self.raw_eq_intrinsic(&args[0], &args[1])?;
519 self.write_scalar(result, dest)?;
522 sym::vtable_size => {
523 let ptr = self.read_pointer(&args[0])?;
524 let (size, _align) = self.get_vtable_size_and_align(ptr)?;
525 self.write_scalar(Scalar::from_machine_usize(size.bytes(), self), dest)?;
527 sym::vtable_align => {
528 let ptr = self.read_pointer(&args[0])?;
529 let (_size, align) = self.get_vtable_size_and_align(ptr)?;
530 self.write_scalar(Scalar::from_machine_usize(align.bytes(), self), dest)?;
533 _ => return Ok(false),
536 trace!("{:?}", self.dump_place(**dest));
537 self.go_to_block(ret);
541 pub(super) fn emulate_nondiverging_intrinsic(
543 intrinsic: &NonDivergingIntrinsic<'tcx>,
544 ) -> InterpResult<'tcx> {
546 NonDivergingIntrinsic::Assume(op) => {
547 let op = self.eval_operand(op, None)?;
548 let cond = self.read_scalar(&op)?.to_bool()?;
550 throw_ub_format!("`assume` called with `false`");
554 NonDivergingIntrinsic::CopyNonOverlapping(mir::CopyNonOverlapping {
559 let src = self.eval_operand(src, None)?;
560 let dst = self.eval_operand(dst, None)?;
561 let count = self.eval_operand(count, None)?;
562 self.copy_intrinsic(&src, &dst, &count, /* nonoverlapping */ true)
569 a: &ImmTy<'tcx, M::Provenance>,
570 b: &ImmTy<'tcx, M::Provenance>,
571 dest: &PlaceTy<'tcx, M::Provenance>,
572 ) -> InterpResult<'tcx> {
573 // Performs an exact division, resulting in undefined behavior where
574 // `x % y != 0` or `y == 0` or `x == T::MIN && y == -1`.
575 // First, check x % y != 0 (or if that computation overflows).
576 let (res, overflow, _ty) = self.overflowing_binary_op(BinOp::Rem, &a, &b)?;
577 assert!(!overflow); // All overflow is UB, so this should never return on overflow.
578 if res.assert_bits(a.layout.size) != 0 {
579 throw_ub_format!("exact_div: {} cannot be divided by {} without remainder", a, b)
581 // `Rem` says this is all right, so we can let `Div` do its job.
582 self.binop_ignore_overflow(BinOp::Div, &a, &b, dest)
585 pub fn saturating_arith(
588 l: &ImmTy<'tcx, M::Provenance>,
589 r: &ImmTy<'tcx, M::Provenance>,
590 ) -> InterpResult<'tcx, Scalar<M::Provenance>> {
591 assert!(matches!(mir_op, BinOp::Add | BinOp::Sub));
592 let (val, overflowed, _ty) = self.overflowing_binary_op(mir_op, l, r)?;
594 let size = l.layout.size;
595 let num_bits = size.bits();
596 if l.layout.abi.is_signed() {
597 // For signed ints the saturated value depends on the sign of the first
598 // term since the sign of the second term can be inferred from this and
599 // the fact that the operation has overflowed (if either is 0 no
600 // overflow can occur)
601 let first_term: u128 = l.to_scalar().to_bits(l.layout.size)?;
602 let first_term_positive = first_term & (1 << (num_bits - 1)) == 0;
603 if first_term_positive {
604 // Negative overflow not possible since the positive first term
605 // can only increase an (in range) negative term for addition
606 // or corresponding negated positive term for subtraction
607 Scalar::from_int(size.signed_int_max(), size)
609 // Positive overflow not possible for similar reason
611 Scalar::from_int(size.signed_int_min(), size)
615 if matches!(mir_op, BinOp::Add) {
617 Scalar::from_uint(size.unsigned_int_max(), size)
620 Scalar::from_uint(0u128, size)
628 /// Offsets a pointer by some multiple of its type, returning an error if the pointer leaves its
629 /// allocation. For integer pointers, we consider each of them their own tiny allocation of size
630 /// 0, so offset-by-0 (and only 0) is okay -- except that null cannot be offset by _any_ value.
631 pub fn ptr_offset_inbounds(
633 ptr: Pointer<Option<M::Provenance>>,
634 pointee_ty: Ty<'tcx>,
636 ) -> InterpResult<'tcx, Pointer<Option<M::Provenance>>> {
637 // We cannot overflow i64 as a type's size must be <= isize::MAX.
638 let pointee_size = i64::try_from(self.layout_of(pointee_ty)?.size.bytes()).unwrap();
639 // The computed offset, in bytes, must not overflow an isize.
640 // `checked_mul` enforces a too small bound, but no actual allocation can be big enough for
641 // the difference to be noticeable.
643 offset_count.checked_mul(pointee_size).ok_or(err_ub!(PointerArithOverflow))?;
644 // The offset being in bounds cannot rely on "wrapping around" the address space.
645 // So, first rule out overflows in the pointer arithmetic.
646 let offset_ptr = ptr.signed_offset(offset_bytes, self)?;
647 // ptr and offset_ptr must be in bounds of the same allocated object. This means all of the
648 // memory between these pointers must be accessible. Note that we do not require the
649 // pointers to be properly aligned (unlike a read/write operation).
650 let min_ptr = if offset_bytes >= 0 { ptr } else { offset_ptr };
651 // This call handles checking for integer/null pointers.
652 self.check_ptr_access_align(
654 Size::from_bytes(offset_bytes.unsigned_abs()),
656 CheckInAllocMsg::PointerArithmeticTest,
661 /// Copy `count*size_of::<T>()` many bytes from `*src` to `*dst`.
662 pub(crate) fn copy_intrinsic(
664 src: &OpTy<'tcx, <M as Machine<'mir, 'tcx>>::Provenance>,
665 dst: &OpTy<'tcx, <M as Machine<'mir, 'tcx>>::Provenance>,
666 count: &OpTy<'tcx, <M as Machine<'mir, 'tcx>>::Provenance>,
667 nonoverlapping: bool,
668 ) -> InterpResult<'tcx> {
669 let count = self.read_scalar(&count)?.to_machine_usize(self)?;
670 let layout = self.layout_of(src.layout.ty.builtin_deref(true).unwrap().ty)?;
671 let (size, align) = (layout.size, layout.align.abi);
672 // `checked_mul` enforces a too small bound (the correct one would probably be machine_isize_max),
673 // but no actual allocation can be big enough for the difference to be noticeable.
674 let size = size.checked_mul(count, self).ok_or_else(|| {
676 "overflow computing total size of `{}`",
677 if nonoverlapping { "copy_nonoverlapping" } else { "copy" }
681 let src = self.read_pointer(&src)?;
682 let dst = self.read_pointer(&dst)?;
684 self.mem_copy(src, align, dst, align, size, nonoverlapping)
687 pub(crate) fn write_bytes_intrinsic(
689 dst: &OpTy<'tcx, <M as Machine<'mir, 'tcx>>::Provenance>,
690 byte: &OpTy<'tcx, <M as Machine<'mir, 'tcx>>::Provenance>,
691 count: &OpTy<'tcx, <M as Machine<'mir, 'tcx>>::Provenance>,
692 ) -> InterpResult<'tcx> {
693 let layout = self.layout_of(dst.layout.ty.builtin_deref(true).unwrap().ty)?;
695 let dst = self.read_pointer(&dst)?;
696 let byte = self.read_scalar(&byte)?.to_u8()?;
697 let count = self.read_scalar(&count)?.to_machine_usize(self)?;
699 // `checked_mul` enforces a too small bound (the correct one would probably be machine_isize_max),
700 // but no actual allocation can be big enough for the difference to be noticeable.
703 .checked_mul(count, self)
704 .ok_or_else(|| err_ub_format!("overflow computing total size of `write_bytes`"))?;
706 let bytes = std::iter::repeat(byte).take(len.bytes_usize());
707 self.write_bytes_ptr(dst, bytes)
710 pub(crate) fn raw_eq_intrinsic(
712 lhs: &OpTy<'tcx, <M as Machine<'mir, 'tcx>>::Provenance>,
713 rhs: &OpTy<'tcx, <M as Machine<'mir, 'tcx>>::Provenance>,
714 ) -> InterpResult<'tcx, Scalar<M::Provenance>> {
715 let layout = self.layout_of(lhs.layout.ty.builtin_deref(true).unwrap().ty)?;
716 assert!(!layout.is_unsized());
718 let get_bytes = |this: &InterpCx<'mir, 'tcx, M>,
719 op: &OpTy<'tcx, <M as Machine<'mir, 'tcx>>::Provenance>,
721 -> InterpResult<'tcx, &[u8]> {
722 let ptr = this.read_pointer(op)?;
723 let Some(alloc_ref) = self.get_ptr_alloc(ptr, size, Align::ONE)? else {
727 if alloc_ref.has_provenance() {
728 throw_ub_format!("`raw_eq` on bytes with provenance");
730 alloc_ref.get_bytes_strip_provenance()
733 let lhs_bytes = get_bytes(self, lhs, layout.size)?;
734 let rhs_bytes = get_bytes(self, rhs, layout.size)?;
735 Ok(Scalar::from_bool(lhs_bytes == rhs_bytes))