2 use std::convert::TryFrom;
4 use rustc_middle::ty::layout::{FnAbiOf, LayoutOf};
5 use rustc_middle::ty::Instance;
10 use rustc_target::abi;
11 use rustc_target::abi::call::{ArgAbi, ArgAttribute, ArgAttributes, FnAbi, PassMode};
12 use rustc_target::spec::abi::Abi;
15 FnVal, ImmTy, InterpCx, InterpResult, MPlaceTy, Machine, OpTy, PlaceTy, Scalar,
16 StackPopCleanup, StackPopUnwind,
19 impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
20 pub(super) fn eval_terminator(
22 terminator: &mir::Terminator<'tcx>,
23 ) -> InterpResult<'tcx> {
24 use rustc_middle::mir::TerminatorKind::*;
25 match terminator.kind {
27 self.pop_stack_frame(/* unwinding */ false)?
30 Goto { target } => self.go_to_block(target),
32 SwitchInt { ref discr, ref targets, switch_ty } => {
33 let discr = self.read_immediate(&self.eval_operand(discr, None)?)?;
34 trace!("SwitchInt({:?})", *discr);
35 assert_eq!(discr.layout.ty, switch_ty);
37 // Branch to the `otherwise` case by default, if no match is found.
38 assert!(!targets.iter().is_empty());
39 let mut target_block = targets.otherwise();
41 for (const_int, target) in targets.iter() {
42 // Compare using binary_op, to also support pointer values
44 .overflowing_binary_op(
47 &ImmTy::from_uint(const_int, discr.layout),
51 target_block = target;
56 self.go_to_block(target_block);
59 Call { ref func, ref args, destination, ref cleanup, from_hir_call: _, fn_span: _ } => {
60 let old_stack = self.frame_idx();
61 let old_loc = self.frame().loc;
62 let func = self.eval_operand(func, None)?;
63 let args = self.eval_operands(args)?;
65 let fn_sig_binder = func.layout.ty.fn_sig(*self.tcx);
67 self.tcx.normalize_erasing_late_bound_regions(self.param_env, fn_sig_binder);
68 let extra_args = &args[fn_sig.inputs().len()..];
69 let extra_args = self.tcx.mk_type_list(extra_args.iter().map(|arg| arg.layout.ty));
71 let (fn_val, fn_abi, with_caller_location) = match *func.layout.ty.kind() {
73 let fn_ptr = self.read_pointer(&func)?;
74 let fn_val = self.memory.get_fn(fn_ptr)?;
75 (fn_val, self.fn_abi_of_fn_ptr(fn_sig_binder, extra_args)?, false)
77 ty::FnDef(def_id, substs) => {
79 self.resolve(ty::WithOptConstParam::unknown(def_id), substs)?;
81 FnVal::Instance(instance),
82 self.fn_abi_of_instance(instance, extra_args)?,
83 instance.def.requires_caller_location(*self.tcx),
87 terminator.source_info.span,
88 "invalid callee of type {:?}",
94 let ret = match destination {
95 Some((dest, ret)) => {
96 dest_place = self.eval_place(dest)?;
97 Some((&dest_place, ret))
103 (fn_sig.abi, fn_abi),
105 with_caller_location,
107 match (cleanup, fn_abi.can_unwind) {
108 (Some(cleanup), true) => StackPopUnwind::Cleanup(*cleanup),
109 (None, true) => StackPopUnwind::Skip,
110 (_, false) => StackPopUnwind::NotAllowed,
113 // Sanity-check that `eval_fn_call` either pushed a new frame or
114 // did a jump to another block.
115 if self.frame_idx() == old_stack && self.frame().loc == old_loc {
116 span_bug!(terminator.source_info.span, "evaluating this call made no progress");
120 Drop { place, target, unwind } => {
121 let place = self.eval_place(place)?;
122 let ty = place.layout.ty;
123 trace!("TerminatorKind::drop: {:?}, type {}", place, ty);
125 let instance = Instance::resolve_drop_in_place(*self.tcx, ty);
126 self.drop_in_place(&place, instance, target, unwind)?;
129 Assert { ref cond, expected, ref msg, target, cleanup } => {
131 self.read_immediate(&self.eval_operand(cond, None)?)?.to_scalar()?.to_bool()?;
132 if expected == cond_val {
133 self.go_to_block(target);
135 M::assert_panic(self, msg, cleanup)?;
140 M::abort(self, "the program aborted execution".to_owned())?;
143 // When we encounter Resume, we've finished unwinding
144 // cleanup for the current stack frame. We pop it in order
145 // to continue unwinding the next frame
147 trace!("unwinding: resuming from cleanup");
148 // By definition, a Resume terminator means
149 // that we're unwinding
150 self.pop_stack_frame(/* unwinding */ true)?;
154 // It is UB to ever encounter this.
155 Unreachable => throw_ub!(Unreachable),
157 // These should never occur for MIR we actually run.
158 DropAndReplace { .. }
162 | GeneratorDrop => span_bug!(
163 terminator.source_info.span,
164 "{:#?} should have been eliminated by MIR pass",
168 // Inline assembly can't be interpreted.
169 InlineAsm { .. } => throw_unsup_format!("inline assembly is not supported"),
175 fn check_argument_compat(
176 caller_abi: &ArgAbi<'tcx, Ty<'tcx>>,
177 callee_abi: &ArgAbi<'tcx, Ty<'tcx>>,
179 // Heuristic for type comparison.
180 let layout_compat = || {
181 if caller_abi.layout.ty == callee_abi.layout.ty {
186 match (caller_abi.layout.abi, callee_abi.layout.abi) {
187 // Different valid ranges are okay (once we enforce validity,
188 // that will take care to make it UB to leave the range, just
189 // like for transmute).
190 (abi::Abi::Scalar(caller), abi::Abi::Scalar(callee)) => {
191 caller.value == callee.value
194 abi::Abi::ScalarPair(caller1, caller2),
195 abi::Abi::ScalarPair(callee1, callee2),
196 ) => caller1.value == callee1.value && caller2.value == callee2.value,
201 // Padding must be fully equal.
202 let pad_compat = || caller_abi.pad == callee_abi.pad;
203 // When comparing the PassMode, we have to be smart about comparing the attributes.
204 let arg_attr_compat = |a1: ArgAttributes, a2: ArgAttributes| {
205 // There's only one regular attribute that matters for the call ABI: InReg.
206 // Everything else is things like noalias, dereferencable, nonnull, ...
207 // (This also applies to pointee_size, pointee_align.)
208 if a1.regular.contains(ArgAttribute::InReg) != a2.regular.contains(ArgAttribute::InReg)
212 // We also compare the sign extension mode -- this could let the callee make assumptions
213 // about bits that conceptually were not even passed.
214 if a1.arg_ext != a2.arg_ext {
219 let mode_compat = || match (caller_abi.mode, callee_abi.mode) {
220 (PassMode::Ignore, PassMode::Ignore) => true,
221 (PassMode::Direct(a1), PassMode::Direct(a2)) => arg_attr_compat(a1, a2),
222 (PassMode::Pair(a1, b1), PassMode::Pair(a2, b2)) => {
223 arg_attr_compat(a1, a2) && arg_attr_compat(b1, b2)
225 (PassMode::Cast(c1), PassMode::Cast(c2)) => c1 == c2,
227 PassMode::Indirect { attrs: a1, extra_attrs: None, on_stack: s1 },
228 PassMode::Indirect { attrs: a2, extra_attrs: None, on_stack: s2 },
229 ) => arg_attr_compat(a1, a2) && s1 == s2,
231 PassMode::Indirect { attrs: a1, extra_attrs: Some(e1), on_stack: s1 },
232 PassMode::Indirect { attrs: a2, extra_attrs: Some(e2), on_stack: s2 },
233 ) => arg_attr_compat(a1, a2) && arg_attr_compat(e1, e2) && s1 == s2,
237 if layout_compat() && pad_compat() && mode_compat() {
241 "check_argument_compat: incompatible ABIs:\ncaller: {:?}\ncallee: {:?}",
248 /// Initialize a single callee argument, checking the types for compatibility.
249 fn pass_argument<'x, 'y>(
251 caller_args: &mut impl Iterator<
252 Item = (&'x OpTy<'tcx, M::PointerTag>, &'y ArgAbi<'tcx, Ty<'tcx>>),
254 callee_abi: &ArgAbi<'tcx, Ty<'tcx>>,
255 callee_arg: &PlaceTy<'tcx, M::PointerTag>,
256 ) -> InterpResult<'tcx>
261 if matches!(callee_abi.mode, PassMode::Ignore) {
262 // This one is skipped.
265 // Find next caller arg.
266 let (caller_arg, caller_abi) = caller_args.next().ok_or_else(|| {
267 err_ub_format!("calling a function with fewer arguments than it requires")
270 if !Self::check_argument_compat(caller_abi, callee_abi) {
272 "calling a function with argument of type {:?} passing data of type {:?}",
273 callee_arg.layout.ty,
277 // We allow some transmutes here.
278 // FIXME: Depending on the PassMode, this should reset some padding to uninitialized. (This
279 // is true for all `copy_op`, but there are a lot of special cases for argument passing
281 self.copy_op_transmute(&caller_arg, callee_arg)
284 /// Call this function -- pushing the stack frame and initializing the arguments.
286 /// `caller_fn_abi` is used to determine if all the arguments are passed the proper way.
287 /// However, we also need `caller_abi` to determine if we need to do untupling of arguments.
289 /// `with_caller_location` indicates whether the caller passed a caller location. Miri
290 /// implements caller locations without argument passing, but to match `FnAbi` we need to know
291 /// when those arguments are present.
292 pub(crate) fn eval_fn_call(
294 fn_val: FnVal<'tcx, M::ExtraFnVal>,
295 (caller_abi, caller_fn_abi): (Abi, &FnAbi<'tcx, Ty<'tcx>>),
296 args: &[OpTy<'tcx, M::PointerTag>],
297 with_caller_location: bool,
298 ret: Option<(&PlaceTy<'tcx, M::PointerTag>, mir::BasicBlock)>,
299 mut unwind: StackPopUnwind,
300 ) -> InterpResult<'tcx> {
301 trace!("eval_fn_call: {:#?}", fn_val);
303 let instance = match fn_val {
304 FnVal::Instance(instance) => instance,
305 FnVal::Other(extra) => {
306 return M::call_extra_fn(self, extra, caller_abi, args, ret, unwind);
311 ty::InstanceDef::Intrinsic(..) => {
312 assert!(caller_abi == Abi::RustIntrinsic || caller_abi == Abi::PlatformIntrinsic);
313 // caller_fn_abi is not relevant here, we interpret the arguments directly for each intrinsic.
314 M::call_intrinsic(self, instance, args, ret, unwind)
316 ty::InstanceDef::VtableShim(..)
317 | ty::InstanceDef::ReifyShim(..)
318 | ty::InstanceDef::ClosureOnceShim { .. }
319 | ty::InstanceDef::FnPtrShim(..)
320 | ty::InstanceDef::DropGlue(..)
321 | ty::InstanceDef::CloneShim(..)
322 | ty::InstanceDef::Item(_) => {
323 // We need MIR for this fn
324 let Some((body, instance)) =
325 M::find_mir_or_eval_fn(self, instance, caller_abi, args, ret, unwind)? else {
329 // Compute callee information using the `instance` returned by
330 // `find_mir_or_eval_fn`.
331 // FIXME: for variadic support, do we have to somehow determine calle's extra_args?
332 let callee_fn_abi = self.fn_abi_of_instance(instance, ty::List::empty())?;
334 if callee_fn_abi.c_variadic != caller_fn_abi.c_variadic {
336 "calling a c-variadic function via a non-variadic call site, or vice versa"
339 if callee_fn_abi.c_variadic {
340 throw_unsup_format!("calling a c-variadic function is not supported");
343 if M::enforce_abi(self) {
344 if caller_fn_abi.conv != callee_fn_abi.conv {
346 "calling a function with calling convention {:?} using calling convention {:?}",
353 if !matches!(unwind, StackPopUnwind::NotAllowed) && !callee_fn_abi.can_unwind {
354 // The callee cannot unwind.
355 unwind = StackPopUnwind::NotAllowed;
358 self.push_stack_frame(
362 StackPopCleanup::Goto { ret: ret.map(|p| p.1), unwind },
365 // If an error is raised here, pop the frame again to get an accurate backtrace.
366 // To this end, we wrap it all in a `try` block.
367 let res: InterpResult<'tcx> = try {
369 "caller ABI: {:?}, args: {:#?}",
372 .map(|arg| (arg.layout.ty, format!("{:?}", **arg)))
376 "spread_arg: {:?}, locals: {:#?}",
381 self.layout_of_local(self.frame(), local, None).unwrap().ty
386 // In principle, we have two iterators: Where the arguments come from, and where
389 // For where they come from: If the ABI is RustCall, we untuple the
390 // last incoming argument. These two iterators do not have the same type,
391 // so to keep the code paths uniform we accept an allocation
392 // (for RustCall ABI only).
393 let caller_args: Cow<'_, [OpTy<'tcx, M::PointerTag>]> =
394 if caller_abi == Abi::RustCall && !args.is_empty() {
396 let (untuple_arg, args) = args.split_last().unwrap();
397 trace!("eval_fn_call: Will pass last argument by untupling");
402 (0..untuple_arg.layout.fields.count())
403 .map(|i| self.operand_field(untuple_arg, i)),
405 .collect::<InterpResult<'_, Vec<OpTy<'tcx, M::PointerTag>>>>(
412 // If `with_caller_location` is set we pretend there is an extra argument (that
413 // we will not pass).
415 caller_args.len() + if with_caller_location { 1 } else { 0 },
416 caller_fn_abi.args.len(),
417 "mismatch between caller ABI and caller arguments",
419 let mut caller_args = caller_args
421 .zip(caller_fn_abi.args.iter())
422 .filter(|arg_and_abi| !matches!(arg_and_abi.1.mode, PassMode::Ignore));
424 // Now we have to spread them out across the callee's locals,
425 // taking into account the `spread_arg`. If we could write
426 // this is a single iterator (that handles `spread_arg`), then
427 // `pass_argument` would be the loop body. It takes care to
428 // not advance `caller_iter` for ZSTs.
429 let mut callee_args_abis = callee_fn_abi.args.iter();
430 for local in body.args_iter() {
431 let dest = self.eval_place(mir::Place::from(local))?;
432 if Some(local) == body.spread_arg {
434 for i in 0..dest.layout.fields.count() {
435 let dest = self.place_field(&dest, i)?;
436 let callee_abi = callee_args_abis.next().unwrap();
437 self.pass_argument(&mut caller_args, callee_abi, &dest)?;
441 let callee_abi = callee_args_abis.next().unwrap();
442 self.pass_argument(&mut caller_args, callee_abi, &dest)?;
445 // If the callee needs a caller location, pretend we consume one more argument from the ABI.
446 if instance.def.requires_caller_location(*self.tcx) {
447 callee_args_abis.next().unwrap();
449 // Now we should have no more caller args or callee arg ABIs
451 callee_args_abis.next().is_none(),
452 "mismatch between callee ABI and callee body arguments"
454 if caller_args.next().is_some() {
455 throw_ub_format!("calling a function with more arguments than it expected")
457 // Don't forget to check the return type!
458 if !Self::check_argument_compat(&caller_fn_abi.ret, &callee_fn_abi.ret) {
460 "calling a function with return type {:?} passing \
461 return place of type {:?}",
462 callee_fn_abi.ret.layout.ty,
463 caller_fn_abi.ret.layout.ty,
469 self.stack_mut().pop();
475 // cannot use the shim here, because that will only result in infinite recursion
476 ty::InstanceDef::Virtual(_, idx) => {
477 let mut args = args.to_vec();
478 // We have to implement all "object safe receivers". Currently we
479 // support built-in pointers `(&, &mut, Box)` as well as unsized-self. We do
480 // not yet support custom self types.
481 // Also see `compiler/rustc_codegen_llvm/src/abi.rs` and `compiler/rustc_codegen_ssa/src/mir/block.rs`.
482 let receiver_place = match args[0].layout.ty.builtin_deref(true) {
485 self.deref_operand(&args[0])?
489 args[0].assert_mem_place()
492 // Find and consult vtable
493 let vtable = self.scalar_to_ptr(receiver_place.vtable());
494 let fn_val = self.get_vtable_slot(vtable, u64::try_from(idx).unwrap())?;
496 // `*mut receiver_place.layout.ty` is almost the layout that we
497 // want for args[0]: We have to project to field 0 because we want
499 assert!(receiver_place.layout.is_unsized());
500 let receiver_ptr_ty = self.tcx.mk_mut_ptr(receiver_place.layout.ty);
501 let this_receiver_ptr = self.layout_of(receiver_ptr_ty)?.field(self, 0);
502 // Adjust receiver argument.
503 args[0] = OpTy::from(ImmTy::from_immediate(
504 Scalar::from_maybe_pointer(receiver_place.ptr, self).into(),
507 trace!("Patched self operand to {:#?}", args[0]);
508 // recurse with concrete function
511 (caller_abi, caller_fn_abi),
513 with_caller_location,
523 place: &PlaceTy<'tcx, M::PointerTag>,
524 instance: ty::Instance<'tcx>,
525 target: mir::BasicBlock,
526 unwind: Option<mir::BasicBlock>,
527 ) -> InterpResult<'tcx> {
528 trace!("drop_in_place: {:?},\n {:?}, {:?}", *place, place.layout.ty, instance);
529 // We take the address of the object. This may well be unaligned, which is fine
530 // for us here. However, unaligned accesses will probably make the actual drop
531 // implementation fail -- a problem shared by rustc.
532 let place = self.force_allocation(place)?;
534 let (instance, place) = match place.layout.ty.kind() {
536 // Dropping a trait object.
537 self.unpack_dyn_trait(&place)?
539 _ => (instance, place),
541 let fn_abi = self.fn_abi_of_instance(instance, ty::List::empty())?;
543 let arg = ImmTy::from_immediate(
545 self.layout_of(self.tcx.mk_mut_ptr(place.layout.ty))?,
548 let ty = self.tcx.mk_unit(); // return type is ()
549 let dest = MPlaceTy::dangling(self.layout_of(ty)?);
552 FnVal::Instance(instance),
556 Some((&dest.into(), target)),
558 Some(cleanup) => StackPopUnwind::Cleanup(cleanup),
559 None => StackPopUnwind::Skip,