2018-08-25 21:22:00 +02:00
|
|
|
use std::borrow::Cow;
|
2020-03-21 13:49:02 +01:00
|
|
|
use std::convert::TryFrom;
|
2018-08-25 21:22:00 +02:00
|
|
|
|
2020-03-31 18:16:47 +02:00
|
|
|
use rustc_middle::ty::layout::TyAndLayout;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::Instance;
|
2021-05-09 02:42:43 +08:00
|
|
|
use rustc_middle::{
|
|
|
|
mir,
|
|
|
|
ty::{self, Ty},
|
|
|
|
};
|
2020-03-31 18:16:47 +02:00
|
|
|
use rustc_target::abi::{self, LayoutOf as _};
|
2018-04-25 19:30:39 +03:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2016-06-23 01:03:58 -06:00
|
|
|
|
2018-08-24 14:40:55 +02:00
|
|
|
use super::{
|
2019-12-25 01:08:39 +01:00
|
|
|
FnVal, ImmTy, InterpCx, InterpResult, MPlaceTy, Machine, OpTy, PlaceTy, StackPopCleanup,
|
2018-08-24 14:40:55 +02:00
|
|
|
};
|
2016-06-23 01:03:58 -06:00
|
|
|
|
2020-03-16 15:12:42 -07:00
|
|
|
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
2016-06-23 01:03:58 -06:00
|
|
|
pub(super) fn eval_terminator(
|
|
|
|
&mut self,
|
|
|
|
terminator: &mir::Terminator<'tcx>,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx> {
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::TerminatorKind::*;
|
2016-06-23 01:03:58 -06:00
|
|
|
match terminator.kind {
|
2016-11-26 19:13:22 -08:00
|
|
|
Return => {
|
2019-04-16 21:04:54 -04:00
|
|
|
self.pop_stack_frame(/* unwinding */ false)?
|
2016-11-26 19:13:22 -08:00
|
|
|
}
|
2016-06-23 01:03:58 -06:00
|
|
|
|
2019-11-22 22:17:15 +01:00
|
|
|
Goto { target } => self.go_to_block(target),
|
2016-06-23 01:03:58 -06:00
|
|
|
|
2020-10-10 17:36:04 +02:00
|
|
|
SwitchInt { ref discr, ref targets, switch_ty } => {
|
2021-02-15 00:00:00 +00:00
|
|
|
let discr = self.read_immediate(&self.eval_operand(discr, None)?)?;
|
2018-08-15 20:18:40 +02:00
|
|
|
trace!("SwitchInt({:?})", *discr);
|
2020-06-21 18:22:30 +02:00
|
|
|
assert_eq!(discr.layout.ty, switch_ty);
|
2016-06-23 01:03:58 -06:00
|
|
|
|
|
|
|
// Branch to the `otherwise` case by default, if no match is found.
|
2020-10-10 17:36:04 +02:00
|
|
|
assert!(!targets.iter().is_empty());
|
|
|
|
let mut target_block = targets.otherwise();
|
2016-06-23 01:03:58 -06:00
|
|
|
|
2020-10-10 17:36:04 +02:00
|
|
|
for (const_int, target) in targets.iter() {
|
2018-08-27 13:34:35 +02:00
|
|
|
// Compare using binary_op, to also support pointer values
|
2019-12-22 17:42:04 -05:00
|
|
|
let res = self
|
|
|
|
.overflowing_binary_op(
|
|
|
|
mir::BinOp::Eq,
|
2021-02-15 00:00:00 +00:00
|
|
|
&discr,
|
|
|
|
&ImmTy::from_uint(const_int, discr.layout),
|
2019-12-22 17:42:04 -05:00
|
|
|
)?
|
|
|
|
.0;
|
2018-08-13 16:14:22 +02:00
|
|
|
if res.to_bool()? {
|
2020-10-10 17:36:04 +02:00
|
|
|
target_block = target;
|
2016-06-23 01:03:58 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 22:17:15 +01:00
|
|
|
self.go_to_block(target_block);
|
2016-06-23 01:03:58 -06:00
|
|
|
}
|
|
|
|
|
2020-06-21 19:35:57 +02:00
|
|
|
Call { ref func, ref args, destination, ref cleanup, from_hir_call: _, fn_span: _ } => {
|
2020-04-14 14:40:08 -07:00
|
|
|
let old_stack = self.frame_idx();
|
2020-04-23 19:40:41 +02:00
|
|
|
let old_loc = self.frame().loc;
|
2018-08-20 15:21:04 +02:00
|
|
|
let func = self.eval_operand(func, None)?;
|
2020-08-03 00:49:11 +02:00
|
|
|
let (fn_val, abi) = match *func.layout.ty.kind() {
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::FnPtr(sig) => {
|
2018-08-27 13:34:35 +02:00
|
|
|
let caller_abi = sig.abi();
|
2021-02-15 00:00:00 +00:00
|
|
|
let fn_ptr = self.read_scalar(&func)?.check_init()?;
|
2019-06-30 13:51:18 +02:00
|
|
|
let fn_val = self.memory.get_fn(fn_ptr)?;
|
|
|
|
(fn_val, caller_abi)
|
2017-08-10 08:48:38 -07:00
|
|
|
}
|
2018-08-24 14:40:55 +02:00
|
|
|
ty::FnDef(def_id, substs) => {
|
|
|
|
let sig = func.layout.ty.fn_sig(*self.tcx);
|
2020-09-09 09:43:18 +02:00
|
|
|
(
|
|
|
|
FnVal::Instance(
|
|
|
|
self.resolve(ty::WithOptConstParam::unknown(def_id), substs)?,
|
|
|
|
),
|
|
|
|
sig.abi(),
|
|
|
|
)
|
2016-11-26 22:58:01 -08:00
|
|
|
}
|
2020-04-05 08:35:31 +02:00
|
|
|
_ => span_bug!(
|
|
|
|
terminator.source_info.span,
|
|
|
|
"invalid callee of type {:?}",
|
|
|
|
func.layout.ty
|
|
|
|
),
|
2017-02-28 12:35:00 +01:00
|
|
|
};
|
2018-08-13 16:14:22 +02:00
|
|
|
let args = self.eval_operands(args)?;
|
2021-02-15 00:00:00 +00:00
|
|
|
let dest_place;
|
2019-11-25 22:00:58 +01:00
|
|
|
let ret = match destination {
|
2021-02-15 00:00:00 +00:00
|
|
|
Some((dest, ret)) => {
|
|
|
|
dest_place = self.eval_place(dest)?;
|
|
|
|
Some((&dest_place, ret))
|
2021-02-15 00:00:00 +00:00
|
|
|
}
|
2019-11-25 22:00:58 +01:00
|
|
|
None => None,
|
|
|
|
};
|
2020-03-30 22:54:15 +02:00
|
|
|
self.eval_fn_call(fn_val, abi, &args[..], ret, *cleanup)?;
|
2020-04-04 15:53:47 +02:00
|
|
|
// Sanity-check that `eval_fn_call` either pushed a new frame or
|
|
|
|
// did a jump to another block.
|
2020-04-23 19:40:41 +02:00
|
|
|
if self.frame_idx() == old_stack && self.frame().loc == old_loc {
|
2020-04-05 08:35:31 +02:00
|
|
|
span_bug!(terminator.source_info.span, "evaluating this call made no progress");
|
|
|
|
}
|
2016-06-23 01:03:58 -06:00
|
|
|
}
|
|
|
|
|
2020-06-10 09:56:54 +02:00
|
|
|
Drop { place, target, unwind } => {
|
|
|
|
let place = self.eval_place(place)?;
|
2018-08-13 16:14:22 +02:00
|
|
|
let ty = place.layout.ty;
|
2020-06-10 09:56:54 +02:00
|
|
|
trace!("TerminatorKind::drop: {:?}, type {}", place, ty);
|
2017-03-22 13:13:52 +01:00
|
|
|
|
2019-05-23 12:45:22 -05:00
|
|
|
let instance = Instance::resolve_drop_in_place(*self.tcx, ty);
|
2021-02-15 00:00:00 +00:00
|
|
|
self.drop_in_place(&place, instance, target, unwind)?;
|
2017-03-22 13:13:52 +01:00
|
|
|
}
|
2016-06-23 01:03:58 -06:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
Assert { ref cond, expected, ref msg, target, cleanup } => {
|
|
|
|
let cond_val =
|
2021-02-15 00:00:00 +00:00
|
|
|
self.read_immediate(&self.eval_operand(cond, None)?)?.to_scalar()?.to_bool()?;
|
2016-09-19 02:19:31 -06:00
|
|
|
if expected == cond_val {
|
2019-11-22 22:17:15 +01:00
|
|
|
self.go_to_block(target);
|
2016-06-23 01:03:58 -06:00
|
|
|
} else {
|
2020-03-09 19:58:58 +01:00
|
|
|
M::assert_panic(self, msg, cleanup)?;
|
2016-06-23 01:03:58 -06:00
|
|
|
}
|
2017-08-10 08:48:38 -07:00
|
|
|
}
|
2016-06-23 01:03:58 -06:00
|
|
|
|
2020-03-09 10:45:20 +01:00
|
|
|
Abort => {
|
2020-12-06 20:25:13 +01:00
|
|
|
M::abort(self, "the program aborted execution".to_owned())?;
|
2020-03-09 10:45:20 +01:00
|
|
|
}
|
|
|
|
|
2019-04-16 21:04:54 -04:00
|
|
|
// When we encounter Resume, we've finished unwinding
|
|
|
|
// cleanup for the current stack frame. We pop it in order
|
|
|
|
// to continue unwinding the next frame
|
|
|
|
Resume => {
|
|
|
|
trace!("unwinding: resuming from cleanup");
|
2019-09-19 13:47:59 -04:00
|
|
|
// By definition, a Resume terminator means
|
2019-04-16 21:04:54 -04:00
|
|
|
// that we're unwinding
|
|
|
|
self.pop_stack_frame(/* unwinding */ true)?;
|
2019-12-22 17:42:04 -05:00
|
|
|
return Ok(());
|
|
|
|
}
|
2019-04-16 21:04:54 -04:00
|
|
|
|
2019-11-29 09:59:52 +01:00
|
|
|
// It is UB to ever encounter this.
|
|
|
|
Unreachable => throw_ub!(Unreachable),
|
|
|
|
|
|
|
|
// These should never occur for MIR we actually run.
|
2020-03-08 19:05:18 +01:00
|
|
|
DropAndReplace { .. }
|
2020-06-02 09:15:24 +02:00
|
|
|
| FalseEdge { .. }
|
2020-03-08 19:05:18 +01:00
|
|
|
| FalseUnwind { .. }
|
|
|
|
| Yield { .. }
|
2020-04-05 08:35:31 +02:00
|
|
|
| GeneratorDrop => span_bug!(
|
|
|
|
terminator.source_info.span,
|
|
|
|
"{:#?} should have been eliminated by MIR pass",
|
|
|
|
terminator.kind
|
|
|
|
),
|
2020-02-14 18:17:50 +00:00
|
|
|
|
|
|
|
// Inline assembly can't be interpreted.
|
|
|
|
InlineAsm { .. } => throw_unsup_format!("inline assembly is not supported"),
|
2016-06-23 01:03:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-08-27 13:34:35 +02:00
|
|
|
fn check_argument_compat(
|
2018-11-22 17:38:59 +01:00
|
|
|
rust_abi: bool,
|
2020-03-04 14:50:21 +00:00
|
|
|
caller: TyAndLayout<'tcx>,
|
|
|
|
callee: TyAndLayout<'tcx>,
|
2018-08-27 13:34:35 +02:00
|
|
|
) -> bool {
|
|
|
|
if caller.ty == callee.ty {
|
|
|
|
// No question
|
|
|
|
return true;
|
2017-05-26 20:02:51 -07:00
|
|
|
}
|
2018-11-22 17:38:59 +01:00
|
|
|
if !rust_abi {
|
|
|
|
// Don't risk anything
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-27 13:34:35 +02:00
|
|
|
// Compare layout
|
|
|
|
match (&caller.abi, &callee.abi) {
|
2018-11-22 17:38:59 +01:00
|
|
|
// Different valid ranges are okay (once we enforce validity,
|
|
|
|
// that will take care to make it UB to leave the range, just
|
|
|
|
// like for transmute).
|
2020-03-31 18:16:47 +02:00
|
|
|
(abi::Abi::Scalar(ref caller), abi::Abi::Scalar(ref callee)) => {
|
2019-12-22 17:42:04 -05:00
|
|
|
caller.value == callee.value
|
|
|
|
}
|
|
|
|
(
|
2020-03-31 18:16:47 +02:00
|
|
|
abi::Abi::ScalarPair(ref caller1, ref caller2),
|
|
|
|
abi::Abi::ScalarPair(ref callee1, ref callee2),
|
2019-12-22 17:42:04 -05:00
|
|
|
) => caller1.value == callee1.value && caller2.value == callee2.value,
|
2018-08-27 13:34:35 +02:00
|
|
|
// Be conservative
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => false,
|
2017-05-26 20:02:51 -07:00
|
|
|
}
|
2018-08-27 13:34:35 +02:00
|
|
|
}
|
2017-05-26 20:02:51 -07:00
|
|
|
|
2018-08-27 13:34:35 +02:00
|
|
|
/// Pass a single argument, checking the types for compatibility.
|
|
|
|
fn pass_argument(
|
|
|
|
&mut self,
|
2018-11-22 17:38:59 +01:00
|
|
|
rust_abi: bool,
|
2019-12-22 17:42:04 -05:00
|
|
|
caller_arg: &mut impl Iterator<Item = OpTy<'tcx, M::PointerTag>>,
|
2021-02-15 00:00:00 +00:00
|
|
|
callee_arg: &PlaceTy<'tcx, M::PointerTag>,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx> {
|
2018-11-22 17:38:59 +01:00
|
|
|
if rust_abi && callee_arg.layout.is_zst() {
|
2018-08-27 13:34:35 +02:00
|
|
|
// Nothing to do.
|
|
|
|
trace!("Skipping callee ZST");
|
|
|
|
return Ok(());
|
2017-05-26 20:02:51 -07:00
|
|
|
}
|
2020-03-08 18:52:30 +01:00
|
|
|
let caller_arg = caller_arg.next().ok_or_else(|| {
|
2020-03-08 23:28:00 +01:00
|
|
|
err_ub_format!("calling a function with fewer arguments than it requires")
|
2020-03-08 18:52:30 +01:00
|
|
|
})?;
|
2018-11-22 17:38:59 +01:00
|
|
|
if rust_abi {
|
2020-02-28 22:54:10 +01:00
|
|
|
assert!(!caller_arg.layout.is_zst(), "ZSTs must have been already filtered out");
|
2018-08-27 13:34:35 +02:00
|
|
|
}
|
|
|
|
// Now, check
|
2018-11-22 17:38:59 +01:00
|
|
|
if !Self::check_argument_compat(rust_abi, caller_arg.layout, callee_arg.layout) {
|
2020-03-08 18:52:30 +01:00
|
|
|
throw_ub_format!(
|
|
|
|
"calling a function with argument of type {:?} passing data of type {:?}",
|
|
|
|
callee_arg.layout.ty,
|
|
|
|
caller_arg.layout.ty
|
|
|
|
)
|
2018-08-27 13:34:35 +02:00
|
|
|
}
|
2018-10-09 17:06:57 +02:00
|
|
|
// We allow some transmutes here
|
2021-02-15 00:00:00 +00:00
|
|
|
self.copy_op_transmute(&caller_arg, callee_arg)
|
2017-05-26 20:02:51 -07:00
|
|
|
}
|
|
|
|
|
2018-08-24 14:40:55 +02:00
|
|
|
/// Call this function -- pushing the stack frame and initializing the arguments.
|
2016-06-23 01:03:58 -06:00
|
|
|
fn eval_fn_call(
|
|
|
|
&mut self,
|
2019-06-30 13:51:18 +02:00
|
|
|
fn_val: FnVal<'tcx, M::ExtraFnVal>,
|
2018-08-27 13:34:35 +02:00
|
|
|
caller_abi: Abi,
|
2018-09-21 23:32:59 +02:00
|
|
|
args: &[OpTy<'tcx, M::PointerTag>],
|
2021-02-15 00:00:00 +00:00
|
|
|
ret: Option<(&PlaceTy<'tcx, M::PointerTag>, mir::BasicBlock)>,
|
2019-12-22 17:42:04 -05:00
|
|
|
unwind: Option<mir::BasicBlock>,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx> {
|
2019-06-30 13:51:18 +02:00
|
|
|
trace!("eval_fn_call: {:#?}", fn_val);
|
|
|
|
|
2019-06-30 15:06:13 +02:00
|
|
|
let instance = match fn_val {
|
|
|
|
FnVal::Instance(instance) => instance,
|
|
|
|
FnVal::Other(extra) => {
|
2021-01-10 14:31:02 +00:00
|
|
|
return M::call_extra_fn(self, extra, caller_abi, args, ret, unwind);
|
2019-06-30 15:06:13 +02:00
|
|
|
}
|
|
|
|
};
|
2018-08-24 14:40:55 +02:00
|
|
|
|
2019-11-25 16:25:10 +01:00
|
|
|
// ABI check
|
2021-05-09 02:42:43 +08:00
|
|
|
let check_abi = |this: &Self, instance_ty: Ty<'tcx>| -> InterpResult<'tcx> {
|
2021-05-21 22:19:37 +08:00
|
|
|
if M::enforce_abi(this) {
|
|
|
|
let callee_abi = match instance_ty.kind() {
|
|
|
|
ty::FnDef(..) => instance_ty.fn_sig(*this.tcx).abi(),
|
|
|
|
ty::Closure(..) => Abi::RustCall,
|
|
|
|
ty::Generator(..) => Abi::Rust,
|
|
|
|
_ => span_bug!(this.cur_span(), "unexpected callee ty: {:?}", instance_ty),
|
|
|
|
};
|
|
|
|
let normalize_abi = |abi| match abi {
|
|
|
|
Abi::Rust | Abi::RustCall | Abi::RustIntrinsic | Abi::PlatformIntrinsic =>
|
|
|
|
// These are all the same ABI, really.
|
|
|
|
{
|
|
|
|
Abi::Rust
|
|
|
|
}
|
|
|
|
abi => abi,
|
|
|
|
};
|
|
|
|
if normalize_abi(caller_abi) != normalize_abi(callee_abi) {
|
|
|
|
throw_ub_format!(
|
|
|
|
"calling a function with ABI {} using caller ABI {}",
|
|
|
|
callee_abi.name(),
|
|
|
|
caller_abi.name()
|
|
|
|
)
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2019-11-25 16:25:10 +01:00
|
|
|
}
|
2021-05-09 02:42:43 +08:00
|
|
|
Ok(())
|
|
|
|
};
|
2019-11-25 16:25:10 +01:00
|
|
|
|
2017-03-22 13:13:52 +01:00
|
|
|
match instance.def {
|
|
|
|
ty::InstanceDef::Intrinsic(..) => {
|
2021-05-09 02:42:43 +08:00
|
|
|
check_abi(self, instance.ty(*self.tcx, self.param_env))?;
|
2019-11-16 14:10:07 +01:00
|
|
|
assert!(caller_abi == Abi::RustIntrinsic || caller_abi == Abi::PlatformIntrinsic);
|
2020-03-30 22:54:15 +02:00
|
|
|
M::call_intrinsic(self, instance, args, ret, unwind)
|
2017-08-10 08:48:38 -07:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
ty::InstanceDef::VtableShim(..)
|
|
|
|
| ty::InstanceDef::ReifyShim(..)
|
|
|
|
| ty::InstanceDef::ClosureOnceShim { .. }
|
|
|
|
| ty::InstanceDef::FnPtrShim(..)
|
|
|
|
| ty::InstanceDef::DropGlue(..)
|
|
|
|
| ty::InstanceDef::CloneShim(..)
|
|
|
|
| ty::InstanceDef::Item(_) => {
|
2018-08-27 13:34:35 +02:00
|
|
|
// We need MIR for this fn
|
2021-01-10 14:31:02 +00:00
|
|
|
let body =
|
|
|
|
match M::find_mir_or_eval_fn(self, instance, caller_abi, args, ret, unwind)? {
|
|
|
|
Some(body) => body,
|
|
|
|
None => return Ok(()),
|
|
|
|
};
|
2018-08-23 19:04:33 +02:00
|
|
|
|
2021-05-17 20:54:31 +08:00
|
|
|
// Check against the ABI of the MIR body we are calling (not the ABI of `instance`;
|
|
|
|
// these can differ when `find_mir_or_eval_fn` does something clever like resolve
|
|
|
|
// exported symbol names).
|
2021-05-09 02:42:43 +08:00
|
|
|
check_abi(self, self.tcx.type_of(body.source.def_id()))?;
|
|
|
|
|
2018-08-23 19:04:33 +02:00
|
|
|
self.push_stack_frame(
|
|
|
|
instance,
|
2019-06-03 18:26:48 -04:00
|
|
|
body,
|
2019-11-25 22:00:58 +01:00
|
|
|
ret.map(|p| p.0),
|
2019-12-22 17:42:04 -05:00
|
|
|
StackPopCleanup::Goto { ret: ret.map(|p| p.1), unwind },
|
2018-08-23 19:04:33 +02:00
|
|
|
)?;
|
2017-03-23 13:36:13 +01:00
|
|
|
|
2020-03-09 21:43:05 +01:00
|
|
|
// If an error is raised here, pop the frame again to get an accurate backtrace.
|
|
|
|
// To this end, we wrap it all in a `try` block.
|
|
|
|
let res: InterpResult<'tcx> = try {
|
2020-03-08 18:52:30 +01:00
|
|
|
trace!(
|
|
|
|
"caller ABI: {:?}, args: {:#?}",
|
|
|
|
caller_abi,
|
|
|
|
args.iter()
|
|
|
|
.map(|arg| (arg.layout.ty, format!("{:?}", **arg)))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
);
|
|
|
|
trace!(
|
|
|
|
"spread_arg: {:?}, locals: {:#?}",
|
|
|
|
body.spread_arg,
|
|
|
|
body.args_iter()
|
|
|
|
.map(|local| (
|
|
|
|
local,
|
|
|
|
self.layout_of_local(self.frame(), local, None).unwrap().ty
|
|
|
|
))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
);
|
2019-12-22 17:42:04 -05:00
|
|
|
|
2020-03-08 18:52:30 +01:00
|
|
|
// Figure out how to pass which arguments.
|
|
|
|
// The Rust ABI is special: ZST get skipped.
|
|
|
|
let rust_abi = match caller_abi {
|
|
|
|
Abi::Rust | Abi::RustCall => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
// We have two iterators: Where the arguments come from,
|
|
|
|
// and where they go to.
|
2019-12-22 17:42:04 -05:00
|
|
|
|
2020-03-08 18:52:30 +01:00
|
|
|
// For where they come from: If the ABI is RustCall, we untuple the
|
|
|
|
// last incoming argument. These two iterators do not have the same type,
|
|
|
|
// so to keep the code paths uniform we accept an allocation
|
|
|
|
// (for RustCall ABI only).
|
|
|
|
let caller_args: Cow<'_, [OpTy<'tcx, M::PointerTag>]> =
|
|
|
|
if caller_abi == Abi::RustCall && !args.is_empty() {
|
|
|
|
// Untuple
|
2021-02-15 00:00:00 +00:00
|
|
|
let (untuple_arg, args) = args.split_last().unwrap();
|
2020-03-08 18:52:30 +01:00
|
|
|
trace!("eval_fn_call: Will pass last argument by untupling");
|
|
|
|
Cow::from(
|
|
|
|
args.iter()
|
|
|
|
.map(|&a| Ok(a))
|
|
|
|
.chain(
|
|
|
|
(0..untuple_arg.layout.fields.count())
|
2020-03-21 17:17:01 +01:00
|
|
|
.map(|i| self.operand_field(untuple_arg, i)),
|
2020-03-08 18:52:30 +01:00
|
|
|
)
|
|
|
|
.collect::<InterpResult<'_, Vec<OpTy<'tcx, M::PointerTag>>>>(
|
|
|
|
)?,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// Plain arg passing
|
|
|
|
Cow::from(args)
|
|
|
|
};
|
|
|
|
// Skip ZSTs
|
|
|
|
let mut caller_iter =
|
|
|
|
caller_args.iter().filter(|op| !rust_abi || !op.layout.is_zst()).copied();
|
2019-12-22 17:42:04 -05:00
|
|
|
|
2020-03-08 18:52:30 +01:00
|
|
|
// Now we have to spread them out across the callee's locals,
|
|
|
|
// taking into account the `spread_arg`. If we could write
|
|
|
|
// this is a single iterator (that handles `spread_arg`), then
|
|
|
|
// `pass_argument` would be the loop body. It takes care to
|
|
|
|
// not advance `caller_iter` for ZSTs.
|
2020-03-08 23:55:25 +01:00
|
|
|
for local in body.args_iter() {
|
2020-03-31 12:19:29 -03:00
|
|
|
let dest = self.eval_place(mir::Place::from(local))?;
|
2020-03-08 18:52:30 +01:00
|
|
|
if Some(local) == body.spread_arg {
|
|
|
|
// Must be a tuple
|
|
|
|
for i in 0..dest.layout.fields.count() {
|
2021-02-15 00:00:00 +00:00
|
|
|
let dest = self.place_field(&dest, i)?;
|
|
|
|
self.pass_argument(rust_abi, &mut caller_iter, &dest)?;
|
2018-08-27 13:34:35 +02:00
|
|
|
}
|
2020-03-08 18:52:30 +01:00
|
|
|
} else {
|
|
|
|
// Normal argument
|
2021-02-15 00:00:00 +00:00
|
|
|
self.pass_argument(rust_abi, &mut caller_iter, &dest)?;
|
2018-08-24 14:40:55 +02:00
|
|
|
}
|
2020-03-08 18:52:30 +01:00
|
|
|
}
|
|
|
|
// Now we should have no more caller args
|
|
|
|
if caller_iter.next().is_some() {
|
2020-03-08 23:55:25 +01:00
|
|
|
throw_ub_format!("calling a function with more arguments than it expected")
|
2020-03-08 18:52:30 +01:00
|
|
|
}
|
|
|
|
// Don't forget to check the return type!
|
|
|
|
if let Some((caller_ret, _)) = ret {
|
2020-03-31 12:19:29 -03:00
|
|
|
let callee_ret = self.eval_place(mir::Place::return_place())?;
|
2020-03-08 18:52:30 +01:00
|
|
|
if !Self::check_argument_compat(
|
|
|
|
rust_abi,
|
|
|
|
caller_ret.layout,
|
|
|
|
callee_ret.layout,
|
|
|
|
) {
|
|
|
|
throw_ub_format!(
|
|
|
|
"calling a function with return type {:?} passing \
|
|
|
|
return place of type {:?}",
|
|
|
|
callee_ret.layout.ty,
|
|
|
|
caller_ret.layout.ty
|
|
|
|
)
|
2018-10-02 21:16:35 +02:00
|
|
|
}
|
2020-03-08 18:52:30 +01:00
|
|
|
} else {
|
|
|
|
let local = mir::RETURN_PLACE;
|
|
|
|
let callee_layout = self.layout_of_local(self.frame(), local, None)?;
|
|
|
|
if !callee_layout.abi.is_uninhabited() {
|
|
|
|
throw_ub_format!("calling a returning function without a return place")
|
2018-10-09 18:16:27 +02:00
|
|
|
}
|
2020-03-08 18:52:30 +01:00
|
|
|
}
|
2020-03-09 21:43:05 +01:00
|
|
|
};
|
2018-08-27 13:34:35 +02:00
|
|
|
match res {
|
|
|
|
Err(err) => {
|
2020-03-16 15:12:42 -07:00
|
|
|
self.stack_mut().pop();
|
2018-08-27 13:34:35 +02:00
|
|
|
Err(err)
|
|
|
|
}
|
2020-03-21 14:24:57 +01:00
|
|
|
Ok(()) => Ok(()),
|
2017-03-23 13:36:13 +01:00
|
|
|
}
|
2017-08-10 08:48:38 -07:00
|
|
|
}
|
2017-08-23 17:46:36 +02:00
|
|
|
// cannot use the shim here, because that will only result in infinite recursion
|
2017-03-23 14:24:02 +01:00
|
|
|
ty::InstanceDef::Virtual(_, idx) => {
|
2019-04-07 19:54:43 +02:00
|
|
|
let mut args = args.to_vec();
|
|
|
|
// We have to implement all "object safe receivers". Currently we
|
2020-09-20 12:46:06 +00:00
|
|
|
// support built-in pointers `(&, &mut, Box)` as well as unsized-self. We do
|
2019-04-07 19:54:43 +02:00
|
|
|
// not yet support custom self types.
|
2020-09-20 12:46:06 +00:00
|
|
|
// Also see `compiler/rustc_codegen_llvm/src/abi.rs` and `compiler/rustc_codegen_ssa/src/mir/block.rs`.
|
2019-04-07 19:54:43 +02:00
|
|
|
let receiver_place = match args[0].layout.ty.builtin_deref(true) {
|
|
|
|
Some(_) => {
|
|
|
|
// Built-in pointer.
|
2021-02-15 00:00:00 +00:00
|
|
|
self.deref_operand(&args[0])?
|
2019-04-07 19:54:43 +02:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
// Unsized self.
|
2019-12-21 23:55:34 +01:00
|
|
|
args[0].assert_mem_place(self)
|
2019-04-07 19:54:43 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
// Find and consult vtable
|
2019-06-23 14:26:36 +02:00
|
|
|
let vtable = receiver_place.vtable();
|
2020-03-21 13:49:02 +01:00
|
|
|
let drop_fn = self.get_vtable_slot(vtable, u64::try_from(idx).unwrap())?;
|
2018-08-16 10:30:56 +02:00
|
|
|
|
2019-04-07 19:54:43 +02:00
|
|
|
// `*mut receiver_place.layout.ty` is almost the layout that we
|
|
|
|
// want for args[0]: We have to project to field 0 because we want
|
|
|
|
// a thin pointer.
|
|
|
|
assert!(receiver_place.layout.is_unsized());
|
|
|
|
let receiver_ptr_ty = self.tcx.mk_mut_ptr(receiver_place.layout.ty);
|
|
|
|
let this_receiver_ptr = self.layout_of(receiver_ptr_ty)?.field(self, 0)?;
|
|
|
|
// Adjust receiver argument.
|
2019-12-22 17:42:04 -05:00
|
|
|
args[0] =
|
2020-04-13 17:07:54 +02:00
|
|
|
OpTy::from(ImmTy::from_immediate(receiver_place.ptr.into(), this_receiver_ptr));
|
2018-08-16 10:30:56 +02:00
|
|
|
trace!("Patched self operand to {:#?}", args[0]);
|
2017-03-23 14:57:11 +01:00
|
|
|
// recurse with concrete function
|
2020-03-30 22:54:15 +02:00
|
|
|
self.eval_fn_call(drop_fn, caller_abi, &args, ret, unwind)
|
2017-08-10 08:48:38 -07:00
|
|
|
}
|
2017-02-28 12:35:00 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-24 14:44:30 +02:00
|
|
|
|
|
|
|
fn drop_in_place(
|
|
|
|
&mut self,
|
2021-02-15 00:00:00 +00:00
|
|
|
place: &PlaceTy<'tcx, M::PointerTag>,
|
2018-08-24 14:44:30 +02:00
|
|
|
instance: ty::Instance<'tcx>,
|
|
|
|
target: mir::BasicBlock,
|
2019-12-22 17:42:04 -05:00
|
|
|
unwind: Option<mir::BasicBlock>,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx> {
|
2018-08-24 14:44:30 +02:00
|
|
|
trace!("drop_in_place: {:?},\n {:?}, {:?}", *place, place.layout.ty, instance);
|
|
|
|
// We take the address of the object. This may well be unaligned, which is fine
|
|
|
|
// for us here. However, unaligned accesses will probably make the actual drop
|
|
|
|
// implementation fail -- a problem shared by rustc.
|
|
|
|
let place = self.force_allocation(place)?;
|
|
|
|
|
2020-08-03 00:49:11 +02:00
|
|
|
let (instance, place) = match place.layout.ty.kind() {
|
2018-08-24 14:44:30 +02:00
|
|
|
ty::Dynamic(..) => {
|
|
|
|
// Dropping a trait object.
|
2021-02-15 00:00:00 +00:00
|
|
|
self.unpack_dyn_trait(&place)?
|
2018-08-24 14:44:30 +02:00
|
|
|
}
|
|
|
|
_ => (instance, place),
|
|
|
|
};
|
|
|
|
|
2020-04-13 17:07:54 +02:00
|
|
|
let arg = ImmTy::from_immediate(
|
|
|
|
place.to_ref(),
|
|
|
|
self.layout_of(self.tcx.mk_mut_ptr(place.layout.ty))?,
|
|
|
|
);
|
2018-08-24 14:44:30 +02:00
|
|
|
|
2018-09-10 11:07:13 +09:00
|
|
|
let ty = self.tcx.mk_unit(); // return type is ()
|
2018-11-03 22:57:53 +02:00
|
|
|
let dest = MPlaceTy::dangling(self.layout_of(ty)?, self);
|
2018-08-24 14:44:30 +02:00
|
|
|
|
|
|
|
self.eval_fn_call(
|
2019-06-30 13:51:18 +02:00
|
|
|
FnVal::Instance(instance),
|
2018-08-27 13:34:35 +02:00
|
|
|
Abi::Rust,
|
2019-02-08 12:20:55 +01:00
|
|
|
&[arg.into()],
|
2021-02-15 00:00:00 +00:00
|
|
|
Some((&dest.into(), target)),
|
2019-12-22 17:42:04 -05:00
|
|
|
unwind,
|
2018-08-24 14:44:30 +02:00
|
|
|
)
|
|
|
|
}
|
2016-09-20 12:52:01 +02:00
|
|
|
}
|