2018-08-23 19:04:33 +02:00
|
|
|
//! Intrinsics and other functions that the miri engine executes without
|
2019-02-08 14:53:55 +01:00
|
|
|
//! looking at their MIR. Intrinsics/functions supported here are shared by CTFE
|
2018-08-23 19:04:33 +02:00
|
|
|
//! and miri.
|
|
|
|
|
|
2020-05-25 13:40:01 -07:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_hir::def_id::DefId;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::{
|
2019-06-14 16:55:36 +03:00
|
|
|
self,
|
2022-10-31 11:04:03 +01:00
|
|
|
interpret::{
|
|
|
|
|
Allocation, ConstAllocation, ConstValue, GlobalId, InterpResult, PointerArithmetic, Scalar,
|
|
|
|
|
},
|
2022-08-31 15:23:41 +00:00
|
|
|
BinOp, NonDivergingIntrinsic,
|
2019-10-24 08:03:57 -07:00
|
|
|
};
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty;
|
2021-08-30 17:38:27 +03:00
|
|
|
use rustc_middle::ty::layout::LayoutOf as _;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::subst::SubstsRef;
|
2020-07-24 13:16:54 +01:00
|
|
|
use rustc_middle::ty::{Ty, TyCtxt};
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::symbol::{sym, Symbol};
|
2022-07-14 22:42:47 +01:00
|
|
|
use rustc_target::abi::{Abi, Align, Primitive, Size};
|
2018-08-23 19:04:33 +02:00
|
|
|
|
2020-07-24 13:16:54 +01:00
|
|
|
use super::{
|
|
|
|
|
util::ensure_monomorphic_enough, CheckInAllocMsg, ImmTy, InterpCx, Machine, OpTy, PlaceTy,
|
2021-07-12 18:22:15 +02:00
|
|
|
Pointer,
|
2020-07-24 13:16:54 +01:00
|
|
|
};
|
2018-08-23 19:04:33 +02:00
|
|
|
|
2019-10-24 08:03:57 -07:00
|
|
|
mod caller_location;
|
2019-04-22 13:53:52 +02:00
|
|
|
|
2022-07-18 18:47:31 -04:00
|
|
|
fn numeric_intrinsic<Prov>(name: Symbol, bits: u128, kind: Primitive) -> Scalar<Prov> {
|
2018-08-23 19:04:33 +02:00
|
|
|
let size = match kind {
|
|
|
|
|
Primitive::Int(integer, _) => integer.size(),
|
|
|
|
|
_ => bug!("invalid `{}` argument: {:?}", name, bits),
|
|
|
|
|
};
|
2020-03-21 13:49:02 +01:00
|
|
|
let extra = 128 - u128::from(size.bits());
|
2018-08-23 19:04:33 +02:00
|
|
|
let bits_out = match name {
|
2020-03-21 13:49:02 +01:00
|
|
|
sym::ctpop => u128::from(bits.count_ones()),
|
|
|
|
|
sym::ctlz => u128::from(bits.leading_zeros()) - extra,
|
|
|
|
|
sym::cttz => u128::from((bits << extra).trailing_zeros()) - extra,
|
2019-06-14 16:55:36 +03:00
|
|
|
sym::bswap => (bits << extra).swap_bytes(),
|
|
|
|
|
sym::bitreverse => (bits << extra).reverse_bits(),
|
2018-08-23 19:04:33 +02:00
|
|
|
_ => bug!("not a numeric intrinsic: {}", name),
|
|
|
|
|
};
|
2021-02-21 13:44:16 +01:00
|
|
|
Scalar::from_uint(bits_out, size)
|
2018-08-23 19:04:33 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-31 11:04:03 +01:00
|
|
|
/// Directly returns an `Allocation` containing an absolute path representation of the given type.
|
|
|
|
|
pub(crate) fn alloc_type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ConstAllocation<'tcx> {
|
|
|
|
|
let path = crate::util::type_name(tcx, ty);
|
|
|
|
|
let alloc = Allocation::from_bytes_byte_aligned_immutable(path.into_bytes());
|
|
|
|
|
tcx.intern_const_alloc(alloc)
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-07 19:22:42 +02:00
|
|
|
/// The logic for all nullary intrinsics is implemented here. These intrinsics don't get evaluated
|
|
|
|
|
/// inside an `InterpCx` and instead have their value computed directly from rustc internal info.
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn eval_nullary_intrinsic<'tcx>(
|
2019-06-07 19:22:42 +02:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
|
def_id: DefId,
|
|
|
|
|
substs: SubstsRef<'tcx>,
|
2020-02-15 11:56:23 +13:00
|
|
|
) -> InterpResult<'tcx, ConstValue<'tcx>> {
|
2019-06-07 19:22:42 +02:00
|
|
|
let tp_ty = substs.type_at(0);
|
2019-06-14 16:55:36 +03:00
|
|
|
let name = tcx.item_name(def_id);
|
2019-06-07 19:22:42 +02:00
|
|
|
Ok(match name {
|
2019-06-14 16:55:36 +03:00
|
|
|
sym::type_name => {
|
2020-07-24 13:16:54 +01:00
|
|
|
ensure_monomorphic_enough(tcx, tp_ty)?;
|
2022-10-31 11:04:03 +01:00
|
|
|
let alloc = alloc_type_name(tcx, tp_ty);
|
Introduce `ConstAllocation`.
Currently some `Allocation`s are interned, some are not, and it's very
hard to tell at a use point which is which.
This commit introduces `ConstAllocation` for the known-interned ones,
which makes the division much clearer. `ConstAllocation::inner()` is
used to get the underlying `Allocation`.
In some places it's natural to use an `Allocation`, in some it's natural
to use a `ConstAllocation`, and in some places there's no clear choice.
I've tried to make things look as nice as possible, while generally
favouring `ConstAllocation`, which is the type that embodies more
information. This does require quite a few calls to `inner()`.
The commit also tweaks how `PartialOrd` works for `Interned`. The
previous code was too clever by half, building on `T: Ord` to make the
code shorter. That caused problems with deriving `PartialOrd` and `Ord`
for `ConstAllocation`, so I changed it to build on `T: PartialOrd`,
which is slightly more verbose but much more standard and avoided the
problems.
2022-03-02 07:15:04 +11:00
|
|
|
ConstValue::Slice { data: alloc, start: 0, end: alloc.inner().len() }
|
2019-06-07 19:22:42 +02:00
|
|
|
}
|
2021-06-04 00:00:00 +00:00
|
|
|
sym::needs_drop => {
|
|
|
|
|
ensure_monomorphic_enough(tcx, tp_ty)?;
|
|
|
|
|
ConstValue::from_bool(tp_ty.needs_drop(tcx, param_env))
|
|
|
|
|
}
|
2021-09-14 00:00:00 +00:00
|
|
|
sym::pref_align_of => {
|
2021-06-05 00:00:00 +00:00
|
|
|
// Correctly handles non-monomorphic calls, so there is no need for ensure_monomorphic_enough.
|
2019-06-07 19:22:42 +02:00
|
|
|
let layout = tcx.layout_of(param_env.and(tp_ty)).map_err(|e| err_inval!(Layout(e)))?;
|
2021-09-14 00:00:00 +00:00
|
|
|
ConstValue::from_machine_usize(layout.align.pref.bytes(), &tcx)
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2020-07-04 18:41:30 +01:00
|
|
|
sym::type_id => {
|
2020-07-24 13:16:54 +01:00
|
|
|
ensure_monomorphic_enough(tcx, tp_ty)?;
|
2020-07-04 18:41:30 +01:00
|
|
|
ConstValue::from_u64(tcx.type_id_hash(tp_ty))
|
|
|
|
|
}
|
2020-11-20 21:49:49 +08:00
|
|
|
sym::variant_count => match tp_ty.kind() {
|
2021-06-05 00:00:00 +00:00
|
|
|
// Correctly handles non-monomorphic calls, so there is no need for ensure_monomorphic_enough.
|
2022-03-05 07:28:41 +11:00
|
|
|
ty::Adt(ref adt, _) => {
|
|
|
|
|
ConstValue::from_machine_usize(adt.variants().len() as u64, &tcx)
|
|
|
|
|
}
|
2020-11-20 21:49:49 +08:00
|
|
|
ty::Projection(_)
|
|
|
|
|
| ty::Opaque(_, _)
|
|
|
|
|
| ty::Param(_)
|
|
|
|
|
| ty::Placeholder(_)
|
|
|
|
|
| ty::Infer(_) => throw_inval!(TooGeneric),
|
2022-07-04 19:13:53 +02:00
|
|
|
ty::Bound(_, _) => bug!("bound ty during ctfe"),
|
2020-11-21 13:45:59 +08:00
|
|
|
ty::Bool
|
|
|
|
|
| ty::Char
|
|
|
|
|
| ty::Int(_)
|
|
|
|
|
| ty::Uint(_)
|
|
|
|
|
| ty::Float(_)
|
|
|
|
|
| ty::Foreign(_)
|
|
|
|
|
| ty::Str
|
|
|
|
|
| ty::Array(_, _)
|
|
|
|
|
| ty::Slice(_)
|
|
|
|
|
| ty::RawPtr(_)
|
|
|
|
|
| ty::Ref(_, _, _)
|
|
|
|
|
| ty::FnDef(_, _)
|
|
|
|
|
| ty::FnPtr(_)
|
2022-04-13 16:11:28 -07:00
|
|
|
| ty::Dynamic(_, _, _)
|
2020-11-21 13:45:59 +08:00
|
|
|
| ty::Closure(_, _)
|
|
|
|
|
| ty::Generator(_, _, _)
|
|
|
|
|
| ty::GeneratorWitness(_)
|
|
|
|
|
| ty::Never
|
|
|
|
|
| ty::Tuple(_)
|
|
|
|
|
| ty::Error(_) => ConstValue::from_machine_usize(0u64, &tcx),
|
2020-11-20 21:49:49 +08:00
|
|
|
},
|
2019-06-07 19:22:42 +02:00
|
|
|
other => bug!("`{}` is not a zero arg intrinsic", other),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-16 15:12:42 -07:00
|
|
|
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Returns `true` if emulation happened.
|
2020-09-12 10:10:13 +02:00
|
|
|
/// Here we implement the intrinsics that are common to all Miri instances; individual machines can add their own
|
|
|
|
|
/// intrinsic handling.
|
2018-08-23 19:04:33 +02:00
|
|
|
pub fn emulate_intrinsic(
|
|
|
|
|
&mut self,
|
|
|
|
|
instance: ty::Instance<'tcx>,
|
2022-07-18 18:47:31 -04:00
|
|
|
args: &[OpTy<'tcx, M::Provenance>],
|
|
|
|
|
dest: &PlaceTy<'tcx, M::Provenance>,
|
2022-04-16 09:27:54 -04:00
|
|
|
ret: Option<mir::BasicBlock>,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx, bool> {
|
2018-08-23 19:04:33 +02:00
|
|
|
let substs = instance.substs;
|
2019-06-14 16:55:36 +03:00
|
|
|
let intrinsic_name = self.tcx.item_name(instance.def_id());
|
2018-08-23 19:04:33 +02:00
|
|
|
|
2020-03-12 21:22:22 +01:00
|
|
|
// First handle intrinsics without return place.
|
2022-04-16 09:27:54 -04:00
|
|
|
let ret = match ret {
|
2019-11-25 22:37:31 +01:00
|
|
|
None => match intrinsic_name {
|
2020-03-12 21:22:22 +01:00
|
|
|
sym::transmute => throw_ub_format!("transmuting to uninhabited type"),
|
2020-12-06 20:25:13 +01:00
|
|
|
sym::abort => M::abort(self, "the program aborted execution".to_owned())?,
|
2020-03-12 21:22:22 +01:00
|
|
|
// Unsupported diverging intrinsic.
|
2019-11-25 22:37:31 +01:00
|
|
|
_ => return Ok(false),
|
|
|
|
|
},
|
2020-03-12 21:22:22 +01:00
|
|
|
Some(p) => p,
|
2019-04-16 21:04:54 -04:00
|
|
|
};
|
|
|
|
|
|
2018-08-23 19:04:33 +02:00
|
|
|
match intrinsic_name {
|
2019-06-14 16:55:36 +03:00
|
|
|
sym::caller_location => {
|
2020-03-30 22:54:15 +02:00
|
|
|
let span = self.find_closest_untracked_caller_location();
|
2019-11-29 11:29:30 +01:00
|
|
|
let location = self.alloc_caller_location_for_span(span);
|
2021-07-12 18:22:15 +02:00
|
|
|
self.write_immediate(location.to_ref(self), dest)?;
|
2019-10-24 08:03:57 -07:00
|
|
|
}
|
|
|
|
|
|
2020-07-29 14:56:58 -07:00
|
|
|
sym::min_align_of_val | sym::size_of_val => {
|
2020-12-29 22:46:17 +01:00
|
|
|
// Avoid `deref_operand` -- this is not a deref, the ptr does not have to be
|
2021-12-12 00:23:06 +01:00
|
|
|
// dereferenceable!
|
2021-02-15 00:00:00 +00:00
|
|
|
let place = self.ref_to_mplace(&self.read_immediate(&args[0])?)?;
|
2020-07-29 14:56:58 -07:00
|
|
|
let (size, align) = self
|
2021-02-15 00:00:00 +00:00
|
|
|
.size_and_align_of_mplace(&place)?
|
2020-07-29 14:56:58 -07:00
|
|
|
.ok_or_else(|| err_unsup_format!("`extern type` does not have known layout"))?;
|
|
|
|
|
|
|
|
|
|
let result = match intrinsic_name {
|
|
|
|
|
sym::min_align_of_val => align.bytes(),
|
|
|
|
|
sym::size_of_val => size.bytes(),
|
|
|
|
|
_ => bug!(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.write_scalar(Scalar::from_machine_usize(result, self), dest)?;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-07 16:06:07 +01:00
|
|
|
sym::pref_align_of
|
2019-06-14 16:55:36 +03:00
|
|
|
| sym::needs_drop
|
|
|
|
|
| sym::type_id
|
2020-06-23 16:46:24 +01:00
|
|
|
| sym::type_name
|
|
|
|
|
| sym::variant_count => {
|
2019-12-22 20:56:01 +01:00
|
|
|
let gid = GlobalId { instance, promoted: None };
|
2020-02-16 11:39:04 +13:00
|
|
|
let ty = match intrinsic_name {
|
2021-09-07 16:06:07 +01:00
|
|
|
sym::pref_align_of | sym::variant_count => self.tcx.types.usize,
|
2020-02-16 11:39:04 +13:00
|
|
|
sym::needs_drop => self.tcx.types.bool,
|
|
|
|
|
sym::type_id => self.tcx.types.u64,
|
|
|
|
|
sym::type_name => self.tcx.mk_static_str(),
|
2022-06-04 11:15:36 -04:00
|
|
|
_ => bug!(),
|
2020-02-16 11:39:04 +13:00
|
|
|
};
|
2022-11-15 12:06:20 +01:00
|
|
|
let val = self.ctfe_query(None, |tcx| {
|
|
|
|
|
tcx.const_eval_global_id(self.param_env, gid, Some(tcx.span))
|
|
|
|
|
})?;
|
2021-03-30 16:08:53 +00:00
|
|
|
let val = self.const_val_to_op(val, ty, Some(dest.layout))?;
|
2022-07-02 20:40:41 -04:00
|
|
|
self.copy_op(&val, dest, /*allow_transmute*/ false)?;
|
2019-06-03 14:38:39 -04:00
|
|
|
}
|
|
|
|
|
|
2019-06-14 16:55:36 +03:00
|
|
|
sym::ctpop
|
|
|
|
|
| sym::cttz
|
|
|
|
|
| sym::cttz_nonzero
|
|
|
|
|
| sym::ctlz
|
|
|
|
|
| sym::ctlz_nonzero
|
|
|
|
|
| sym::bswap
|
|
|
|
|
| sym::bitreverse => {
|
2018-08-23 19:04:33 +02:00
|
|
|
let ty = substs.type_at(0);
|
|
|
|
|
let layout_of = self.layout_of(ty)?;
|
2022-08-01 19:05:20 -04:00
|
|
|
let val = self.read_scalar(&args[0])?;
|
2021-07-12 18:22:15 +02:00
|
|
|
let bits = val.to_bits(layout_of.size)?;
|
2018-08-23 19:04:33 +02:00
|
|
|
let kind = match layout_of.abi {
|
2022-03-03 12:02:12 +00:00
|
|
|
Abi::Scalar(scalar) => scalar.primitive(),
|
2020-06-21 16:13:31 +02:00
|
|
|
_ => span_bug!(
|
|
|
|
|
self.cur_span(),
|
|
|
|
|
"{} called on invalid type {:?}",
|
|
|
|
|
intrinsic_name,
|
|
|
|
|
ty
|
|
|
|
|
),
|
2018-08-23 19:04:33 +02:00
|
|
|
};
|
2019-06-14 16:55:36 +03:00
|
|
|
let (nonzero, intrinsic_name) = match intrinsic_name {
|
|
|
|
|
sym::cttz_nonzero => (true, sym::cttz),
|
|
|
|
|
sym::ctlz_nonzero => (true, sym::ctlz),
|
|
|
|
|
other => (false, other),
|
2018-08-23 19:04:33 +02:00
|
|
|
};
|
2019-06-14 16:55:36 +03:00
|
|
|
if nonzero && bits == 0 {
|
|
|
|
|
throw_ub_format!("`{}_nonzero` called on 0", intrinsic_name);
|
|
|
|
|
}
|
2021-02-21 13:44:16 +01:00
|
|
|
let out_val = numeric_intrinsic(intrinsic_name, bits, kind);
|
2018-08-23 19:04:33 +02:00
|
|
|
self.write_scalar(out_val, dest)?;
|
|
|
|
|
}
|
2020-12-15 00:00:00 +00:00
|
|
|
sym::add_with_overflow | sym::sub_with_overflow | sym::mul_with_overflow => {
|
2021-02-15 00:00:00 +00:00
|
|
|
let lhs = self.read_immediate(&args[0])?;
|
|
|
|
|
let rhs = self.read_immediate(&args[1])?;
|
2020-12-15 00:00:00 +00:00
|
|
|
let bin_op = match intrinsic_name {
|
|
|
|
|
sym::add_with_overflow => BinOp::Add,
|
|
|
|
|
sym::sub_with_overflow => BinOp::Sub,
|
|
|
|
|
sym::mul_with_overflow => BinOp::Mul,
|
2022-06-04 11:15:36 -04:00
|
|
|
_ => bug!(),
|
2018-09-01 15:05:55 +02:00
|
|
|
};
|
2022-07-04 08:57:10 -04:00
|
|
|
self.binop_with_overflow(
|
|
|
|
|
bin_op, /*force_overflow_checks*/ true, &lhs, &rhs, dest,
|
|
|
|
|
)?;
|
2018-09-01 15:05:55 +02:00
|
|
|
}
|
2019-06-14 16:55:36 +03:00
|
|
|
sym::saturating_add | sym::saturating_sub => {
|
2021-02-15 00:00:00 +00:00
|
|
|
let l = self.read_immediate(&args[0])?;
|
|
|
|
|
let r = self.read_immediate(&args[1])?;
|
2022-03-06 19:09:22 -05:00
|
|
|
let val = self.saturating_arith(
|
|
|
|
|
if intrinsic_name == sym::saturating_add { BinOp::Add } else { BinOp::Sub },
|
2021-02-15 00:00:00 +00:00
|
|
|
&l,
|
|
|
|
|
&r,
|
|
|
|
|
)?;
|
2019-02-07 13:12:17 -05:00
|
|
|
self.write_scalar(val, dest)?;
|
2019-02-06 16:15:17 -05:00
|
|
|
}
|
2020-03-08 14:24:32 +01:00
|
|
|
sym::discriminant_value => {
|
2021-02-15 00:00:00 +00:00
|
|
|
let place = self.deref_operand(&args[0])?;
|
|
|
|
|
let discr_val = self.read_discriminant(&place.into())?.0;
|
2020-04-14 12:49:15 +02:00
|
|
|
self.write_scalar(discr_val, dest)?;
|
2020-03-08 14:24:32 +01:00
|
|
|
}
|
2020-02-04 17:09:22 -08:00
|
|
|
sym::unchecked_shl
|
|
|
|
|
| sym::unchecked_shr
|
|
|
|
|
| sym::unchecked_add
|
|
|
|
|
| sym::unchecked_sub
|
|
|
|
|
| sym::unchecked_mul
|
|
|
|
|
| sym::unchecked_div
|
|
|
|
|
| sym::unchecked_rem => {
|
2021-02-15 00:00:00 +00:00
|
|
|
let l = self.read_immediate(&args[0])?;
|
|
|
|
|
let r = self.read_immediate(&args[1])?;
|
2018-09-06 13:13:07 +02:00
|
|
|
let bin_op = match intrinsic_name {
|
2019-06-14 16:55:36 +03:00
|
|
|
sym::unchecked_shl => BinOp::Shl,
|
|
|
|
|
sym::unchecked_shr => BinOp::Shr,
|
2020-02-04 17:09:22 -08:00
|
|
|
sym::unchecked_add => BinOp::Add,
|
|
|
|
|
sym::unchecked_sub => BinOp::Sub,
|
|
|
|
|
sym::unchecked_mul => BinOp::Mul,
|
|
|
|
|
sym::unchecked_div => BinOp::Div,
|
|
|
|
|
sym::unchecked_rem => BinOp::Rem,
|
2022-06-04 11:15:36 -04:00
|
|
|
_ => bug!(),
|
2018-09-06 13:13:07 +02:00
|
|
|
};
|
2021-02-15 00:00:00 +00:00
|
|
|
let (val, overflowed, _ty) = self.overflowing_binary_op(bin_op, &l, &r)?;
|
2018-09-06 15:11:56 +02:00
|
|
|
if overflowed {
|
|
|
|
|
let layout = self.layout_of(substs.type_at(0))?;
|
2022-08-01 19:05:20 -04:00
|
|
|
let r_val = r.to_scalar().to_bits(layout.size)?;
|
2020-02-04 17:09:22 -08:00
|
|
|
if let sym::unchecked_shl | sym::unchecked_shr = intrinsic_name {
|
2020-03-09 10:17:06 +01:00
|
|
|
throw_ub_format!("overflowing shift by {} in `{}`", r_val, intrinsic_name);
|
2020-02-04 17:09:22 -08:00
|
|
|
} else {
|
2020-03-09 10:17:06 +01:00
|
|
|
throw_ub_format!("overflow executing `{}`", intrinsic_name);
|
2020-02-04 17:09:22 -08:00
|
|
|
}
|
2018-09-06 15:11:56 +02:00
|
|
|
}
|
|
|
|
|
self.write_scalar(val, dest)?;
|
2018-09-06 13:13:07 +02:00
|
|
|
}
|
2019-06-14 16:55:36 +03:00
|
|
|
sym::rotate_left | sym::rotate_right => {
|
2018-11-03 15:48:29 +01:00
|
|
|
// rotate_left: (X << (S % BW)) | (X >> ((BW - S) % BW))
|
|
|
|
|
// rotate_right: (X << ((BW - S) % BW)) | (X >> (S % BW))
|
|
|
|
|
let layout = self.layout_of(substs.type_at(0))?;
|
2022-08-01 19:05:20 -04:00
|
|
|
let val = self.read_scalar(&args[0])?;
|
2021-07-12 18:22:15 +02:00
|
|
|
let val_bits = val.to_bits(layout.size)?;
|
2022-08-01 19:05:20 -04:00
|
|
|
let raw_shift = self.read_scalar(&args[1])?;
|
2021-07-12 18:22:15 +02:00
|
|
|
let raw_shift_bits = raw_shift.to_bits(layout.size)?;
|
2020-03-21 13:49:02 +01:00
|
|
|
let width_bits = u128::from(layout.size.bits());
|
2018-11-03 15:48:29 +01:00
|
|
|
let shift_bits = raw_shift_bits % width_bits;
|
2019-06-02 16:51:51 +00:00
|
|
|
let inv_shift_bits = (width_bits - shift_bits) % width_bits;
|
2019-06-14 16:55:36 +03:00
|
|
|
let result_bits = if intrinsic_name == sym::rotate_left {
|
2018-11-03 15:48:29 +01:00
|
|
|
(val_bits << shift_bits) | (val_bits >> inv_shift_bits)
|
|
|
|
|
} else {
|
|
|
|
|
(val_bits >> shift_bits) | (val_bits << inv_shift_bits)
|
|
|
|
|
};
|
|
|
|
|
let truncated_bits = self.truncate(result_bits, layout);
|
|
|
|
|
let result = Scalar::from_uint(truncated_bits, layout.size);
|
|
|
|
|
self.write_scalar(result, dest)?;
|
|
|
|
|
}
|
2021-01-23 08:57:04 +00:00
|
|
|
sym::copy => {
|
2021-05-04 13:43:50 +02:00
|
|
|
self.copy_intrinsic(&args[0], &args[1], &args[2], /*nonoverlapping*/ false)?;
|
2020-12-26 02:22:29 +01:00
|
|
|
}
|
2021-11-24 13:05:26 +09:00
|
|
|
sym::write_bytes => {
|
|
|
|
|
self.write_bytes_intrinsic(&args[0], &args[1], &args[2])?;
|
|
|
|
|
}
|
2020-04-24 00:17:29 -07:00
|
|
|
sym::offset => {
|
2021-07-12 18:22:15 +02:00
|
|
|
let ptr = self.read_pointer(&args[0])?;
|
2021-02-15 00:00:00 +00:00
|
|
|
let offset_count = self.read_scalar(&args[1])?.to_machine_isize(self)?;
|
2020-04-24 00:17:29 -07:00
|
|
|
let pointee_ty = substs.type_at(0);
|
2019-08-20 17:51:54 +02:00
|
|
|
|
2020-04-24 00:17:29 -07:00
|
|
|
let offset_ptr = self.ptr_offset_inbounds(ptr, pointee_ty, offset_count)?;
|
2021-07-14 22:10:17 +02:00
|
|
|
self.write_pointer(offset_ptr, dest)?;
|
2020-04-24 00:17:29 -07:00
|
|
|
}
|
|
|
|
|
sym::arith_offset => {
|
2021-07-14 22:10:17 +02:00
|
|
|
let ptr = self.read_pointer(&args[0])?;
|
2021-02-15 00:00:00 +00:00
|
|
|
let offset_count = self.read_scalar(&args[1])?.to_machine_isize(self)?;
|
2020-04-24 00:17:29 -07:00
|
|
|
let pointee_ty = substs.type_at(0);
|
|
|
|
|
|
|
|
|
|
let pointee_size = i64::try_from(self.layout_of(pointee_ty)?.size.bytes()).unwrap();
|
|
|
|
|
let offset_bytes = offset_count.wrapping_mul(pointee_size);
|
2021-07-14 22:10:17 +02:00
|
|
|
let offset_ptr = ptr.wrapping_signed_offset(offset_bytes, self);
|
|
|
|
|
self.write_pointer(offset_ptr, dest)?;
|
2020-04-24 00:17:29 -07:00
|
|
|
}
|
2022-04-09 01:27:47 -07:00
|
|
|
sym::ptr_offset_from | sym::ptr_offset_from_unsigned => {
|
2022-03-10 18:30:32 -05:00
|
|
|
let a = self.read_pointer(&args[0])?;
|
|
|
|
|
let b = self.read_pointer(&args[1])?;
|
2019-11-04 13:24:27 +01:00
|
|
|
|
2022-06-09 20:47:06 -04:00
|
|
|
let usize_layout = self.layout_of(self.tcx.types.usize)?;
|
|
|
|
|
let isize_layout = self.layout_of(self.tcx.types.isize)?;
|
2019-11-04 13:24:27 +01:00
|
|
|
|
2022-06-09 20:47:06 -04:00
|
|
|
// Get offsets for both that are at least relative to the same base.
|
|
|
|
|
let (a_offset, b_offset) =
|
|
|
|
|
match (self.ptr_try_get_alloc_id(a), self.ptr_try_get_alloc_id(b)) {
|
|
|
|
|
(Err(a), Err(b)) => {
|
2022-08-18 10:13:37 +08:00
|
|
|
// Neither pointer points to an allocation.
|
2022-06-09 20:47:06 -04:00
|
|
|
// If these are inequal or null, this *will* fail the deref check below.
|
|
|
|
|
(a, b)
|
|
|
|
|
}
|
|
|
|
|
(Err(_), _) | (_, Err(_)) => {
|
|
|
|
|
// We managed to find a valid allocation for one pointer, but not the other.
|
|
|
|
|
// That means they are definitely not pointing to the same allocation.
|
2022-04-09 01:27:47 -07:00
|
|
|
throw_ub_format!(
|
2022-07-24 16:01:47 -04:00
|
|
|
"`{}` called on pointers into different allocations",
|
2022-06-09 20:47:06 -04:00
|
|
|
intrinsic_name
|
2022-04-09 01:27:47 -07:00
|
|
|
);
|
|
|
|
|
}
|
2022-06-09 20:47:06 -04:00
|
|
|
(Ok((a_alloc_id, a_offset, _)), Ok((b_alloc_id, b_offset, _))) => {
|
|
|
|
|
// Found allocation for both. They must be into the same allocation.
|
|
|
|
|
if a_alloc_id != b_alloc_id {
|
|
|
|
|
throw_ub_format!(
|
2022-07-24 16:01:47 -04:00
|
|
|
"`{}` called on pointers into different allocations",
|
2022-06-09 20:47:06 -04:00
|
|
|
intrinsic_name
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
// Use these offsets for distance calculation.
|
|
|
|
|
(a_offset.bytes(), b_offset.bytes())
|
2022-04-09 16:29:39 -07:00
|
|
|
}
|
2022-06-09 20:47:06 -04:00
|
|
|
};
|
2022-04-09 16:29:39 -07:00
|
|
|
|
2022-06-09 20:47:06 -04:00
|
|
|
// Compute distance.
|
2022-07-24 16:01:47 -04:00
|
|
|
let dist = {
|
|
|
|
|
// Addresses are unsigned, so this is a `usize` computation. We have to do the
|
|
|
|
|
// overflow check separately anyway.
|
|
|
|
|
let (val, overflowed, _ty) = {
|
|
|
|
|
let a_offset = ImmTy::from_uint(a_offset, usize_layout);
|
|
|
|
|
let b_offset = ImmTy::from_uint(b_offset, usize_layout);
|
|
|
|
|
self.overflowing_binary_op(BinOp::Sub, &a_offset, &b_offset)?
|
|
|
|
|
};
|
2022-06-09 20:47:06 -04:00
|
|
|
if overflowed {
|
2022-07-24 16:01:47 -04:00
|
|
|
// a < b
|
|
|
|
|
if intrinsic_name == sym::ptr_offset_from_unsigned {
|
|
|
|
|
throw_ub_format!(
|
|
|
|
|
"`{}` called when first pointer has smaller offset than second: {} < {}",
|
|
|
|
|
intrinsic_name,
|
|
|
|
|
a_offset,
|
|
|
|
|
b_offset,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
// The signed form of the intrinsic allows this. If we interpret the
|
|
|
|
|
// difference as isize, we'll get the proper signed difference. If that
|
|
|
|
|
// seems *positive*, they were more than isize::MAX apart.
|
|
|
|
|
let dist = val.to_machine_isize(self)?;
|
|
|
|
|
if dist >= 0 {
|
|
|
|
|
throw_ub_format!(
|
|
|
|
|
"`{}` called when first pointer is too far before second",
|
|
|
|
|
intrinsic_name
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
dist
|
|
|
|
|
} else {
|
|
|
|
|
// b >= a
|
|
|
|
|
let dist = val.to_machine_isize(self)?;
|
|
|
|
|
// If converting to isize produced a *negative* result, we had an overflow
|
|
|
|
|
// because they were more than isize::MAX apart.
|
|
|
|
|
if dist < 0 {
|
|
|
|
|
throw_ub_format!(
|
|
|
|
|
"`{}` called when first pointer is too far ahead of second",
|
|
|
|
|
intrinsic_name
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
dist
|
2019-11-25 22:00:58 +01:00
|
|
|
}
|
2022-06-09 20:47:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Check that the range between them is dereferenceable ("in-bounds or one past the
|
|
|
|
|
// end of the same allocation"). This is like the check in ptr_offset_inbounds.
|
2022-07-24 16:01:47 -04:00
|
|
|
let min_ptr = if dist >= 0 { b } else { a };
|
2022-06-09 20:47:06 -04:00
|
|
|
self.check_ptr_access_align(
|
|
|
|
|
min_ptr,
|
2022-07-24 16:01:47 -04:00
|
|
|
Size::from_bytes(dist.unsigned_abs()),
|
2022-06-09 20:47:06 -04:00
|
|
|
Align::ONE,
|
|
|
|
|
CheckInAllocMsg::OffsetFromTest,
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
// Perform division by size to compute return value.
|
|
|
|
|
let ret_layout = if intrinsic_name == sym::ptr_offset_from_unsigned {
|
2022-07-24 16:01:47 -04:00
|
|
|
assert!(0 <= dist && dist <= self.machine_isize_max());
|
2022-06-09 20:47:06 -04:00
|
|
|
usize_layout
|
|
|
|
|
} else {
|
2022-07-24 16:01:47 -04:00
|
|
|
assert!(self.machine_isize_min() <= dist && dist <= self.machine_isize_max());
|
2022-06-09 20:47:06 -04:00
|
|
|
isize_layout
|
|
|
|
|
};
|
|
|
|
|
let pointee_layout = self.layout_of(substs.type_at(0))?;
|
|
|
|
|
// If ret_layout is unsigned, we checked that so is the distance, so we are good.
|
2022-07-24 16:01:47 -04:00
|
|
|
let val = ImmTy::from_int(dist, ret_layout);
|
2022-06-09 20:47:06 -04:00
|
|
|
let size = ImmTy::from_int(pointee_layout.size.bytes(), ret_layout);
|
|
|
|
|
self.exact_div(&val, &size, dest)?;
|
2019-08-20 17:51:54 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-14 16:55:36 +03:00
|
|
|
sym::transmute => {
|
2022-07-02 20:40:41 -04:00
|
|
|
self.copy_op(&args[0], dest, /*allow_transmute*/ true)?;
|
2018-08-20 19:51:48 +01:00
|
|
|
}
|
2021-11-28 13:00:37 -05:00
|
|
|
sym::assert_inhabited | sym::assert_zero_valid | sym::assert_uninit_valid => {
|
2020-12-02 02:32:10 +01:00
|
|
|
let ty = instance.substs.type_at(0);
|
|
|
|
|
let layout = self.layout_of(ty)?;
|
|
|
|
|
|
2021-11-28 13:00:37 -05:00
|
|
|
// For *all* intrinsics we first check `is_uninhabited` to give a more specific
|
|
|
|
|
// error message.
|
2020-12-02 02:32:10 +01:00
|
|
|
if layout.abi.is_uninhabited() {
|
2020-12-07 18:59:10 +01:00
|
|
|
// The run-time intrinsic panics just to get a good backtrace; here we abort
|
|
|
|
|
// since there is no problem showing a backtrace even for aborts.
|
2020-12-10 19:42:01 +01:00
|
|
|
M::abort(
|
|
|
|
|
self,
|
|
|
|
|
format!(
|
|
|
|
|
"aborted execution: attempted to instantiate uninhabited type `{}`",
|
|
|
|
|
ty
|
|
|
|
|
),
|
|
|
|
|
)?;
|
2020-12-02 02:32:10 +01:00
|
|
|
}
|
2022-07-14 22:42:47 +01:00
|
|
|
|
|
|
|
|
if intrinsic_name == sym::assert_zero_valid {
|
|
|
|
|
let should_panic = !self.tcx.permits_zero_init(layout);
|
|
|
|
|
|
|
|
|
|
if should_panic {
|
|
|
|
|
M::abort(
|
|
|
|
|
self,
|
|
|
|
|
format!(
|
|
|
|
|
"aborted execution: attempted to zero-initialize type `{}`, which is invalid",
|
|
|
|
|
ty
|
|
|
|
|
),
|
|
|
|
|
)?;
|
|
|
|
|
}
|
2021-11-28 13:00:37 -05:00
|
|
|
}
|
2022-07-14 22:42:47 +01:00
|
|
|
|
|
|
|
|
if intrinsic_name == sym::assert_uninit_valid {
|
|
|
|
|
let should_panic = !self.tcx.permits_uninit_init(layout);
|
|
|
|
|
|
|
|
|
|
if should_panic {
|
|
|
|
|
M::abort(
|
|
|
|
|
self,
|
|
|
|
|
format!(
|
|
|
|
|
"aborted execution: attempted to leave type `{}` uninitialized, which is invalid",
|
|
|
|
|
ty
|
|
|
|
|
),
|
|
|
|
|
)?;
|
|
|
|
|
}
|
2021-11-28 13:00:37 -05:00
|
|
|
}
|
2020-12-02 02:32:10 +01:00
|
|
|
}
|
2019-06-14 16:55:36 +03:00
|
|
|
sym::simd_insert => {
|
2021-02-15 00:00:00 +00:00
|
|
|
let index = u64::from(self.read_scalar(&args[1])?.to_u32()?);
|
|
|
|
|
let elem = &args[2];
|
2021-11-17 22:29:21 -05:00
|
|
|
let (input, input_len) = self.operand_to_simd(&args[0])?;
|
|
|
|
|
let (dest, dest_len) = self.place_to_simd(dest)?;
|
|
|
|
|
assert_eq!(input_len, dest_len, "Return vector length must match input length");
|
2019-09-25 12:54:23 +02:00
|
|
|
assert!(
|
2021-11-17 22:29:21 -05:00
|
|
|
index < dest_len,
|
|
|
|
|
"Index `{}` must be in bounds of vector with length {}`",
|
2019-09-25 12:54:23 +02:00
|
|
|
index,
|
2021-11-17 22:29:21 -05:00
|
|
|
dest_len
|
2019-09-25 12:54:23 +02:00
|
|
|
);
|
|
|
|
|
|
2021-11-17 22:29:21 -05:00
|
|
|
for i in 0..dest_len {
|
|
|
|
|
let place = self.mplace_index(&dest, i)?;
|
2022-07-15 22:58:20 -04:00
|
|
|
let value = if i == index {
|
|
|
|
|
elem.clone()
|
|
|
|
|
} else {
|
|
|
|
|
self.mplace_index(&input, i)?.into()
|
|
|
|
|
};
|
2022-07-02 20:40:41 -04:00
|
|
|
self.copy_op(&value, &place.into(), /*allow_transmute*/ false)?;
|
2019-09-24 16:14:43 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-06-14 16:55:36 +03:00
|
|
|
sym::simd_extract => {
|
2021-02-15 00:00:00 +00:00
|
|
|
let index = u64::from(self.read_scalar(&args[1])?.to_u32()?);
|
2021-11-17 22:29:21 -05:00
|
|
|
let (input, input_len) = self.operand_to_simd(&args[0])?;
|
2019-09-25 12:54:23 +02:00
|
|
|
assert!(
|
2021-11-17 22:29:21 -05:00
|
|
|
index < input_len,
|
|
|
|
|
"index `{}` must be in bounds of vector with length `{}`",
|
2019-09-25 12:54:23 +02:00
|
|
|
index,
|
2021-11-17 22:29:21 -05:00
|
|
|
input_len
|
2019-09-25 12:54:23 +02:00
|
|
|
);
|
2022-07-02 20:40:41 -04:00
|
|
|
self.copy_op(
|
|
|
|
|
&self.mplace_index(&input, index)?.into(),
|
|
|
|
|
dest,
|
|
|
|
|
/*allow_transmute*/ false,
|
|
|
|
|
)?;
|
2019-09-24 16:14:43 +02:00
|
|
|
}
|
2021-08-10 11:50:33 +01:00
|
|
|
sym::likely | sym::unlikely | sym::black_box => {
|
2020-06-26 13:19:50 +01:00
|
|
|
// These just return their argument
|
2022-07-02 20:40:41 -04:00
|
|
|
self.copy_op(&args[0], dest, /*allow_transmute*/ false)?;
|
2020-06-26 13:19:50 +01:00
|
|
|
}
|
2021-05-30 10:25:41 -07:00
|
|
|
sym::raw_eq => {
|
|
|
|
|
let result = self.raw_eq_intrinsic(&args[0], &args[1])?;
|
|
|
|
|
self.write_scalar(result, dest)?;
|
|
|
|
|
}
|
2022-07-18 09:48:20 -04:00
|
|
|
|
|
|
|
|
sym::vtable_size => {
|
|
|
|
|
let ptr = self.read_pointer(&args[0])?;
|
|
|
|
|
let (size, _align) = self.get_vtable_size_and_align(ptr)?;
|
|
|
|
|
self.write_scalar(Scalar::from_machine_usize(size.bytes(), self), dest)?;
|
|
|
|
|
}
|
|
|
|
|
sym::vtable_align => {
|
|
|
|
|
let ptr = self.read_pointer(&args[0])?;
|
|
|
|
|
let (_size, align) = self.get_vtable_size_and_align(ptr)?;
|
|
|
|
|
self.write_scalar(Scalar::from_machine_usize(align.bytes(), self), dest)?;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-23 19:04:33 +02:00
|
|
|
_ => return Ok(false),
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-15 00:00:00 +00:00
|
|
|
trace!("{:?}", self.dump_place(**dest));
|
2019-11-25 22:00:58 +01:00
|
|
|
self.go_to_block(ret);
|
2018-08-23 19:04:33 +02:00
|
|
|
Ok(true)
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-31 15:23:41 +00:00
|
|
|
pub(super) fn emulate_nondiverging_intrinsic(
|
|
|
|
|
&mut self,
|
|
|
|
|
intrinsic: &NonDivergingIntrinsic<'tcx>,
|
|
|
|
|
) -> InterpResult<'tcx> {
|
|
|
|
|
match intrinsic {
|
|
|
|
|
NonDivergingIntrinsic::Assume(op) => {
|
|
|
|
|
let op = self.eval_operand(op, None)?;
|
|
|
|
|
let cond = self.read_scalar(&op)?.to_bool()?;
|
|
|
|
|
if !cond {
|
|
|
|
|
throw_ub_format!("`assume` called with `false`");
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
NonDivergingIntrinsic::CopyNonOverlapping(mir::CopyNonOverlapping {
|
|
|
|
|
count,
|
|
|
|
|
src,
|
|
|
|
|
dst,
|
|
|
|
|
}) => {
|
|
|
|
|
let src = self.eval_operand(src, None)?;
|
|
|
|
|
let dst = self.eval_operand(dst, None)?;
|
|
|
|
|
let count = self.eval_operand(count, None)?;
|
|
|
|
|
self.copy_intrinsic(&src, &dst, &count, /* nonoverlapping */ true)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-20 17:51:54 +02:00
|
|
|
pub fn exact_div(
|
|
|
|
|
&mut self,
|
2022-07-18 18:47:31 -04:00
|
|
|
a: &ImmTy<'tcx, M::Provenance>,
|
|
|
|
|
b: &ImmTy<'tcx, M::Provenance>,
|
|
|
|
|
dest: &PlaceTy<'tcx, M::Provenance>,
|
2019-08-20 17:51:54 +02:00
|
|
|
) -> InterpResult<'tcx> {
|
|
|
|
|
// Performs an exact division, resulting in undefined behavior where
|
2020-03-04 13:12:04 +01:00
|
|
|
// `x % y != 0` or `y == 0` or `x == T::MIN && y == -1`.
|
2020-02-13 11:16:54 +01:00
|
|
|
// First, check x % y != 0 (or if that computation overflows).
|
2021-02-15 00:00:00 +00:00
|
|
|
let (res, overflow, _ty) = self.overflowing_binary_op(BinOp::Rem, &a, &b)?;
|
2022-03-01 20:02:59 -05:00
|
|
|
assert!(!overflow); // All overflow is UB, so this should never return on overflow.
|
|
|
|
|
if res.assert_bits(a.layout.size) != 0 {
|
|
|
|
|
throw_ub_format!("exact_div: {} cannot be divided by {} without remainder", a, b)
|
2019-08-20 17:51:54 +02:00
|
|
|
}
|
2020-02-13 11:16:54 +01:00
|
|
|
// `Rem` says this is all right, so we can let `Div` do its job.
|
2021-02-15 00:00:00 +00:00
|
|
|
self.binop_ignore_overflow(BinOp::Div, &a, &b, dest)
|
2019-08-20 17:51:54 +02:00
|
|
|
}
|
2020-04-24 00:17:29 -07:00
|
|
|
|
2022-03-06 19:09:22 -05:00
|
|
|
pub fn saturating_arith(
|
|
|
|
|
&self,
|
|
|
|
|
mir_op: BinOp,
|
2022-07-18 18:47:31 -04:00
|
|
|
l: &ImmTy<'tcx, M::Provenance>,
|
|
|
|
|
r: &ImmTy<'tcx, M::Provenance>,
|
|
|
|
|
) -> InterpResult<'tcx, Scalar<M::Provenance>> {
|
2022-03-06 19:09:22 -05:00
|
|
|
assert!(matches!(mir_op, BinOp::Add | BinOp::Sub));
|
|
|
|
|
let (val, overflowed, _ty) = self.overflowing_binary_op(mir_op, l, r)?;
|
|
|
|
|
Ok(if overflowed {
|
|
|
|
|
let size = l.layout.size;
|
|
|
|
|
let num_bits = size.bits();
|
|
|
|
|
if l.layout.abi.is_signed() {
|
|
|
|
|
// For signed ints the saturated value depends on the sign of the first
|
|
|
|
|
// term since the sign of the second term can be inferred from this and
|
|
|
|
|
// the fact that the operation has overflowed (if either is 0 no
|
|
|
|
|
// overflow can occur)
|
2022-08-01 19:05:20 -04:00
|
|
|
let first_term: u128 = l.to_scalar().to_bits(l.layout.size)?;
|
2022-03-06 19:09:22 -05:00
|
|
|
let first_term_positive = first_term & (1 << (num_bits - 1)) == 0;
|
|
|
|
|
if first_term_positive {
|
|
|
|
|
// Negative overflow not possible since the positive first term
|
|
|
|
|
// can only increase an (in range) negative term for addition
|
|
|
|
|
// or corresponding negated positive term for subtraction
|
2022-03-06 19:11:31 -05:00
|
|
|
Scalar::from_int(size.signed_int_max(), size)
|
2022-03-06 19:09:22 -05:00
|
|
|
} else {
|
|
|
|
|
// Positive overflow not possible for similar reason
|
|
|
|
|
// max negative
|
2022-03-06 19:11:31 -05:00
|
|
|
Scalar::from_int(size.signed_int_min(), size)
|
2022-03-06 19:09:22 -05:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// unsigned
|
|
|
|
|
if matches!(mir_op, BinOp::Add) {
|
|
|
|
|
// max unsigned
|
2022-03-06 19:11:31 -05:00
|
|
|
Scalar::from_uint(size.unsigned_int_max(), size)
|
2022-03-06 19:09:22 -05:00
|
|
|
} else {
|
|
|
|
|
// underflow to 0
|
2022-03-06 19:11:31 -05:00
|
|
|
Scalar::from_uint(0u128, size)
|
2022-03-06 19:09:22 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
val
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 00:17:29 -07:00
|
|
|
/// Offsets a pointer by some multiple of its type, returning an error if the pointer leaves its
|
|
|
|
|
/// allocation. For integer pointers, we consider each of them their own tiny allocation of size
|
2021-05-02 15:55:22 -06:00
|
|
|
/// 0, so offset-by-0 (and only 0) is okay -- except that null cannot be offset by _any_ value.
|
2020-04-24 00:17:29 -07:00
|
|
|
pub fn ptr_offset_inbounds(
|
|
|
|
|
&self,
|
2022-07-18 18:47:31 -04:00
|
|
|
ptr: Pointer<Option<M::Provenance>>,
|
2020-04-24 00:17:29 -07:00
|
|
|
pointee_ty: Ty<'tcx>,
|
|
|
|
|
offset_count: i64,
|
2022-07-18 18:47:31 -04:00
|
|
|
) -> InterpResult<'tcx, Pointer<Option<M::Provenance>>> {
|
2020-05-26 02:00:02 -07:00
|
|
|
// We cannot overflow i64 as a type's size must be <= isize::MAX.
|
2020-04-24 00:17:29 -07:00
|
|
|
let pointee_size = i64::try_from(self.layout_of(pointee_ty)?.size.bytes()).unwrap();
|
2022-03-27 19:49:52 -04:00
|
|
|
// The computed offset, in bytes, must not overflow an isize.
|
|
|
|
|
// `checked_mul` enforces a too small bound, but no actual allocation can be big enough for
|
|
|
|
|
// the difference to be noticeable.
|
2020-05-26 23:14:55 -07:00
|
|
|
let offset_bytes =
|
|
|
|
|
offset_count.checked_mul(pointee_size).ok_or(err_ub!(PointerArithOverflow))?;
|
2020-04-24 00:17:29 -07:00
|
|
|
// The offset being in bounds cannot rely on "wrapping around" the address space.
|
|
|
|
|
// So, first rule out overflows in the pointer arithmetic.
|
2021-07-12 18:22:15 +02:00
|
|
|
let offset_ptr = ptr.signed_offset(offset_bytes, self)?;
|
2020-04-24 00:17:29 -07:00
|
|
|
// ptr and offset_ptr must be in bounds of the same allocated object. This means all of the
|
|
|
|
|
// memory between these pointers must be accessible. Note that we do not require the
|
|
|
|
|
// pointers to be properly aligned (unlike a read/write operation).
|
|
|
|
|
let min_ptr = if offset_bytes >= 0 { ptr } else { offset_ptr };
|
2021-05-02 15:55:22 -06:00
|
|
|
// This call handles checking for integer/null pointers.
|
2022-04-03 13:05:49 -04:00
|
|
|
self.check_ptr_access_align(
|
2020-04-24 00:17:29 -07:00
|
|
|
min_ptr,
|
2022-06-09 20:47:06 -04:00
|
|
|
Size::from_bytes(offset_bytes.unsigned_abs()),
|
2021-05-17 13:08:12 +02:00
|
|
|
Align::ONE,
|
2021-05-06 00:16:27 +02:00
|
|
|
CheckInAllocMsg::PointerArithmeticTest,
|
2020-04-24 00:17:29 -07:00
|
|
|
)?;
|
|
|
|
|
Ok(offset_ptr)
|
|
|
|
|
}
|
2021-05-04 13:43:50 +02:00
|
|
|
|
|
|
|
|
/// Copy `count*size_of::<T>()` many bytes from `*src` to `*dst`.
|
|
|
|
|
pub(crate) fn copy_intrinsic(
|
|
|
|
|
&mut self,
|
2022-07-18 18:47:31 -04:00
|
|
|
src: &OpTy<'tcx, <M as Machine<'mir, 'tcx>>::Provenance>,
|
|
|
|
|
dst: &OpTy<'tcx, <M as Machine<'mir, 'tcx>>::Provenance>,
|
|
|
|
|
count: &OpTy<'tcx, <M as Machine<'mir, 'tcx>>::Provenance>,
|
2021-05-04 13:43:50 +02:00
|
|
|
nonoverlapping: bool,
|
|
|
|
|
) -> InterpResult<'tcx> {
|
|
|
|
|
let count = self.read_scalar(&count)?.to_machine_usize(self)?;
|
|
|
|
|
let layout = self.layout_of(src.layout.ty.builtin_deref(true).unwrap().ty)?;
|
|
|
|
|
let (size, align) = (layout.size, layout.align.abi);
|
2022-03-27 19:49:52 -04:00
|
|
|
// `checked_mul` enforces a too small bound (the correct one would probably be machine_isize_max),
|
|
|
|
|
// but no actual allocation can be big enough for the difference to be noticeable.
|
2021-05-04 13:43:50 +02:00
|
|
|
let size = size.checked_mul(count, self).ok_or_else(|| {
|
|
|
|
|
err_ub_format!(
|
|
|
|
|
"overflow computing total size of `{}`",
|
|
|
|
|
if nonoverlapping { "copy_nonoverlapping" } else { "copy" }
|
|
|
|
|
)
|
|
|
|
|
})?;
|
|
|
|
|
|
2021-07-12 18:22:15 +02:00
|
|
|
let src = self.read_pointer(&src)?;
|
|
|
|
|
let dst = self.read_pointer(&dst)?;
|
2021-05-04 13:43:50 +02:00
|
|
|
|
2022-04-03 13:05:49 -04:00
|
|
|
self.mem_copy(src, align, dst, align, size, nonoverlapping)
|
2021-05-04 13:43:50 +02:00
|
|
|
}
|
2021-05-30 10:25:41 -07:00
|
|
|
|
2021-11-24 13:05:26 +09:00
|
|
|
pub(crate) fn write_bytes_intrinsic(
|
|
|
|
|
&mut self,
|
2022-07-18 18:47:31 -04:00
|
|
|
dst: &OpTy<'tcx, <M as Machine<'mir, 'tcx>>::Provenance>,
|
|
|
|
|
byte: &OpTy<'tcx, <M as Machine<'mir, 'tcx>>::Provenance>,
|
|
|
|
|
count: &OpTy<'tcx, <M as Machine<'mir, 'tcx>>::Provenance>,
|
2021-11-24 13:05:26 +09:00
|
|
|
) -> InterpResult<'tcx> {
|
|
|
|
|
let layout = self.layout_of(dst.layout.ty.builtin_deref(true).unwrap().ty)?;
|
|
|
|
|
|
|
|
|
|
let dst = self.read_pointer(&dst)?;
|
|
|
|
|
let byte = self.read_scalar(&byte)?.to_u8()?;
|
|
|
|
|
let count = self.read_scalar(&count)?.to_machine_usize(self)?;
|
|
|
|
|
|
2022-03-27 19:49:52 -04:00
|
|
|
// `checked_mul` enforces a too small bound (the correct one would probably be machine_isize_max),
|
|
|
|
|
// but no actual allocation can be big enough for the difference to be noticeable.
|
2021-11-24 13:05:26 +09:00
|
|
|
let len = layout
|
|
|
|
|
.size
|
|
|
|
|
.checked_mul(count, self)
|
|
|
|
|
.ok_or_else(|| err_ub_format!("overflow computing total size of `write_bytes`"))?;
|
|
|
|
|
|
|
|
|
|
let bytes = std::iter::repeat(byte).take(len.bytes_usize());
|
2022-04-03 13:05:49 -04:00
|
|
|
self.write_bytes_ptr(dst, bytes)
|
2021-11-24 13:05:26 +09:00
|
|
|
}
|
|
|
|
|
|
2021-05-30 10:25:41 -07:00
|
|
|
pub(crate) fn raw_eq_intrinsic(
|
|
|
|
|
&mut self,
|
2022-07-18 18:47:31 -04:00
|
|
|
lhs: &OpTy<'tcx, <M as Machine<'mir, 'tcx>>::Provenance>,
|
|
|
|
|
rhs: &OpTy<'tcx, <M as Machine<'mir, 'tcx>>::Provenance>,
|
|
|
|
|
) -> InterpResult<'tcx, Scalar<M::Provenance>> {
|
2021-05-30 10:25:41 -07:00
|
|
|
let layout = self.layout_of(lhs.layout.ty.builtin_deref(true).unwrap().ty)?;
|
2022-11-13 12:14:59 +01:00
|
|
|
assert!(layout.is_sized());
|
2021-05-30 10:25:41 -07:00
|
|
|
|
2022-08-27 14:54:02 -04:00
|
|
|
let get_bytes = |this: &InterpCx<'mir, 'tcx, M>,
|
|
|
|
|
op: &OpTy<'tcx, <M as Machine<'mir, 'tcx>>::Provenance>,
|
|
|
|
|
size|
|
|
|
|
|
-> InterpResult<'tcx, &[u8]> {
|
|
|
|
|
let ptr = this.read_pointer(op)?;
|
|
|
|
|
let Some(alloc_ref) = self.get_ptr_alloc(ptr, size, Align::ONE)? else {
|
|
|
|
|
// zero-sized access
|
|
|
|
|
return Ok(&[]);
|
|
|
|
|
};
|
|
|
|
|
if alloc_ref.has_provenance() {
|
|
|
|
|
throw_ub_format!("`raw_eq` on bytes with provenance");
|
|
|
|
|
}
|
|
|
|
|
alloc_ref.get_bytes_strip_provenance()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let lhs_bytes = get_bytes(self, lhs, layout.size)?;
|
|
|
|
|
let rhs_bytes = get_bytes(self, rhs, layout.size)?;
|
2021-05-30 11:31:56 -07:00
|
|
|
Ok(Scalar::from_bool(lhs_bytes == rhs_bytes))
|
2021-05-30 10:25:41 -07:00
|
|
|
}
|
2018-08-23 19:04:33 +02:00
|
|
|
}
|