]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_const_eval/src/interpret/terminator.rs
Auto merge of #99182 - RalfJung:mitigate-uninit, r=scottmcm
[rust.git] / compiler / rustc_const_eval / src / interpret / terminator.rs
1 use std::borrow::Cow;
2
3 use rustc_middle::ty::layout::{FnAbiOf, LayoutOf};
4 use rustc_middle::ty::Instance;
5 use rustc_middle::{
6     mir,
7     ty::{self, Ty},
8 };
9 use rustc_target::abi;
10 use rustc_target::abi::call::{ArgAbi, ArgAttribute, ArgAttributes, FnAbi, PassMode};
11 use rustc_target::spec::abi::Abi;
12
13 use super::{
14     FnVal, ImmTy, Immediate, InterpCx, InterpResult, MPlaceTy, Machine, MemoryKind, OpTy, Operand,
15     PlaceTy, Scalar, StackPopCleanup, StackPopUnwind,
16 };
17
18 impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
19     pub(super) fn eval_terminator(
20         &mut self,
21         terminator: &mir::Terminator<'tcx>,
22     ) -> InterpResult<'tcx> {
23         use rustc_middle::mir::TerminatorKind::*;
24         match terminator.kind {
25             Return => {
26                 self.pop_stack_frame(/* unwinding */ false)?
27             }
28
29             Goto { target } => self.go_to_block(target),
30
31             SwitchInt { ref discr, ref targets, switch_ty } => {
32                 let discr = self.read_immediate(&self.eval_operand(discr, None)?)?;
33                 trace!("SwitchInt({:?})", *discr);
34                 assert_eq!(discr.layout.ty, switch_ty);
35
36                 // Branch to the `otherwise` case by default, if no match is found.
37                 assert!(!targets.iter().is_empty());
38                 let mut target_block = targets.otherwise();
39
40                 for (const_int, target) in targets.iter() {
41                     // Compare using MIR BinOp::Eq, to also support pointer values.
42                     // (Avoiding `self.binary_op` as that does some redundant layout computation.)
43                     let res = self
44                         .overflowing_binary_op(
45                             mir::BinOp::Eq,
46                             &discr,
47                             &ImmTy::from_uint(const_int, discr.layout),
48                         )?
49                         .0;
50                     if res.to_bool()? {
51                         target_block = target;
52                         break;
53                     }
54                 }
55
56                 self.go_to_block(target_block);
57             }
58
59             Call {
60                 ref func,
61                 ref args,
62                 destination,
63                 target,
64                 ref cleanup,
65                 from_hir_call: _,
66                 fn_span: _,
67             } => {
68                 let old_stack = self.frame_idx();
69                 let old_loc = self.frame().loc;
70                 let func = self.eval_operand(func, None)?;
71                 let args = self.eval_operands(args)?;
72
73                 let fn_sig_binder = func.layout.ty.fn_sig(*self.tcx);
74                 let fn_sig =
75                     self.tcx.normalize_erasing_late_bound_regions(self.param_env, fn_sig_binder);
76                 let extra_args = &args[fn_sig.inputs().len()..];
77                 let extra_args = self.tcx.mk_type_list(extra_args.iter().map(|arg| arg.layout.ty));
78
79                 let (fn_val, fn_abi, with_caller_location) = match *func.layout.ty.kind() {
80                     ty::FnPtr(_sig) => {
81                         let fn_ptr = self.read_pointer(&func)?;
82                         let fn_val = self.get_ptr_fn(fn_ptr)?;
83                         (fn_val, self.fn_abi_of_fn_ptr(fn_sig_binder, extra_args)?, false)
84                     }
85                     ty::FnDef(def_id, substs) => {
86                         let instance =
87                             self.resolve(ty::WithOptConstParam::unknown(def_id), substs)?;
88                         (
89                             FnVal::Instance(instance),
90                             self.fn_abi_of_instance(instance, extra_args)?,
91                             instance.def.requires_caller_location(*self.tcx),
92                         )
93                     }
94                     _ => span_bug!(
95                         terminator.source_info.span,
96                         "invalid callee of type {:?}",
97                         func.layout.ty
98                     ),
99                 };
100
101                 let destination = self.eval_place(destination)?;
102                 self.eval_fn_call(
103                     fn_val,
104                     (fn_sig.abi, fn_abi),
105                     &args,
106                     with_caller_location,
107                     &destination,
108                     target,
109                     match (cleanup, fn_abi.can_unwind) {
110                         (Some(cleanup), true) => StackPopUnwind::Cleanup(*cleanup),
111                         (None, true) => StackPopUnwind::Skip,
112                         (_, false) => StackPopUnwind::NotAllowed,
113                     },
114                 )?;
115                 // Sanity-check that `eval_fn_call` either pushed a new frame or
116                 // did a jump to another block.
117                 if self.frame_idx() == old_stack && self.frame().loc == old_loc {
118                     span_bug!(terminator.source_info.span, "evaluating this call made no progress");
119                 }
120             }
121
122             Drop { place, target, unwind } => {
123                 let place = self.eval_place(place)?;
124                 let ty = place.layout.ty;
125                 trace!("TerminatorKind::drop: {:?}, type {}", place, ty);
126
127                 let instance = Instance::resolve_drop_in_place(*self.tcx, ty);
128                 self.drop_in_place(&place, instance, target, unwind)?;
129             }
130
131             Assert { ref cond, expected, ref msg, target, cleanup } => {
132                 let cond_val =
133                     self.read_immediate(&self.eval_operand(cond, None)?)?.to_scalar()?.to_bool()?;
134                 if expected == cond_val {
135                     self.go_to_block(target);
136                 } else {
137                     M::assert_panic(self, msg, cleanup)?;
138                 }
139             }
140
141             Abort => {
142                 M::abort(self, "the program aborted execution".to_owned())?;
143             }
144
145             // When we encounter Resume, we've finished unwinding
146             // cleanup for the current stack frame. We pop it in order
147             // to continue unwinding the next frame
148             Resume => {
149                 trace!("unwinding: resuming from cleanup");
150                 // By definition, a Resume terminator means
151                 // that we're unwinding
152                 self.pop_stack_frame(/* unwinding */ true)?;
153                 return Ok(());
154             }
155
156             // It is UB to ever encounter this.
157             Unreachable => throw_ub!(Unreachable),
158
159             // These should never occur for MIR we actually run.
160             DropAndReplace { .. }
161             | FalseEdge { .. }
162             | FalseUnwind { .. }
163             | Yield { .. }
164             | GeneratorDrop => span_bug!(
165                 terminator.source_info.span,
166                 "{:#?} should have been eliminated by MIR pass",
167                 terminator.kind
168             ),
169
170             // Inline assembly can't be interpreted.
171             InlineAsm { .. } => throw_unsup_format!("inline assembly is not supported"),
172         }
173
174         Ok(())
175     }
176
177     fn check_argument_compat(
178         caller_abi: &ArgAbi<'tcx, Ty<'tcx>>,
179         callee_abi: &ArgAbi<'tcx, Ty<'tcx>>,
180     ) -> bool {
181         // Heuristic for type comparison.
182         let layout_compat = || {
183             if caller_abi.layout.ty == callee_abi.layout.ty {
184                 // No question
185                 return true;
186             }
187             if caller_abi.layout.is_unsized() || callee_abi.layout.is_unsized() {
188                 // No, no, no. We require the types to *exactly* match for unsized arguments. If
189                 // these are somehow unsized "in a different way" (say, `dyn Trait` vs `[i32]`),
190                 // then who knows what happens.
191                 return false;
192             }
193             if caller_abi.layout.size != callee_abi.layout.size
194                 || caller_abi.layout.align.abi != callee_abi.layout.align.abi
195             {
196                 // This cannot go well...
197                 return false;
198             }
199             // The rest *should* be okay, but we are extra conservative.
200             match (caller_abi.layout.abi, callee_abi.layout.abi) {
201                 // Different valid ranges are okay (once we enforce validity,
202                 // that will take care to make it UB to leave the range, just
203                 // like for transmute).
204                 (abi::Abi::Scalar(caller), abi::Abi::Scalar(callee)) => {
205                     caller.primitive() == callee.primitive()
206                 }
207                 (
208                     abi::Abi::ScalarPair(caller1, caller2),
209                     abi::Abi::ScalarPair(callee1, callee2),
210                 ) => {
211                     caller1.primitive() == callee1.primitive()
212                         && caller2.primitive() == callee2.primitive()
213                 }
214                 // Be conservative
215                 _ => false,
216             }
217         };
218         // Padding must be fully equal.
219         let pad_compat = || caller_abi.pad == callee_abi.pad;
220         // When comparing the PassMode, we have to be smart about comparing the attributes.
221         let arg_attr_compat = |a1: ArgAttributes, a2: ArgAttributes| {
222             // There's only one regular attribute that matters for the call ABI: InReg.
223             // Everything else is things like noalias, dereferencable, nonnull, ...
224             // (This also applies to pointee_size, pointee_align.)
225             if a1.regular.contains(ArgAttribute::InReg) != a2.regular.contains(ArgAttribute::InReg)
226             {
227                 return false;
228             }
229             // We also compare the sign extension mode -- this could let the callee make assumptions
230             // about bits that conceptually were not even passed.
231             if a1.arg_ext != a2.arg_ext {
232                 return false;
233             }
234             return true;
235         };
236         let mode_compat = || match (caller_abi.mode, callee_abi.mode) {
237             (PassMode::Ignore, PassMode::Ignore) => true,
238             (PassMode::Direct(a1), PassMode::Direct(a2)) => arg_attr_compat(a1, a2),
239             (PassMode::Pair(a1, b1), PassMode::Pair(a2, b2)) => {
240                 arg_attr_compat(a1, a2) && arg_attr_compat(b1, b2)
241             }
242             (PassMode::Cast(c1), PassMode::Cast(c2)) => c1 == c2,
243             (
244                 PassMode::Indirect { attrs: a1, extra_attrs: None, on_stack: s1 },
245                 PassMode::Indirect { attrs: a2, extra_attrs: None, on_stack: s2 },
246             ) => arg_attr_compat(a1, a2) && s1 == s2,
247             (
248                 PassMode::Indirect { attrs: a1, extra_attrs: Some(e1), on_stack: s1 },
249                 PassMode::Indirect { attrs: a2, extra_attrs: Some(e2), on_stack: s2 },
250             ) => arg_attr_compat(a1, a2) && arg_attr_compat(e1, e2) && s1 == s2,
251             _ => false,
252         };
253
254         if layout_compat() && pad_compat() && mode_compat() {
255             return true;
256         }
257         trace!(
258             "check_argument_compat: incompatible ABIs:\ncaller: {:?}\ncallee: {:?}",
259             caller_abi,
260             callee_abi
261         );
262         return false;
263     }
264
265     /// Initialize a single callee argument, checking the types for compatibility.
266     fn pass_argument<'x, 'y>(
267         &mut self,
268         caller_args: &mut impl Iterator<
269             Item = (&'x OpTy<'tcx, M::Provenance>, &'y ArgAbi<'tcx, Ty<'tcx>>),
270         >,
271         callee_abi: &ArgAbi<'tcx, Ty<'tcx>>,
272         callee_arg: &PlaceTy<'tcx, M::Provenance>,
273     ) -> InterpResult<'tcx>
274     where
275         'tcx: 'x,
276         'tcx: 'y,
277     {
278         if matches!(callee_abi.mode, PassMode::Ignore) {
279             // This one is skipped.
280             return Ok(());
281         }
282         // Find next caller arg.
283         let (caller_arg, caller_abi) = caller_args.next().ok_or_else(|| {
284             err_ub_format!("calling a function with fewer arguments than it requires")
285         })?;
286         // Now, check
287         if !Self::check_argument_compat(caller_abi, callee_abi) {
288             throw_ub_format!(
289                 "calling a function with argument of type {:?} passing data of type {:?}",
290                 callee_arg.layout.ty,
291                 caller_arg.layout.ty
292             )
293         }
294         // Special handling for unsized parameters.
295         if caller_arg.layout.is_unsized() {
296             // `check_argument_compat` ensures that both have the same type, so we know they will use the metadata the same way.
297             assert_eq!(caller_arg.layout.ty, callee_arg.layout.ty);
298             // We have to properly pre-allocate the memory for the callee.
299             // So let's tear down some wrappers.
300             // This all has to be in memory, there are no immediate unsized values.
301             let src = caller_arg.assert_mem_place();
302             // The destination cannot be one of these "spread args".
303             let (dest_frame, dest_local) = callee_arg.assert_local();
304             // We are just initializing things, so there can't be anything here yet.
305             assert!(matches!(
306                 *self.local_to_op(&self.stack()[dest_frame], dest_local, None)?,
307                 Operand::Immediate(Immediate::Uninit)
308             ));
309             // Allocate enough memory to hold `src`.
310             let Some((size, align)) = self.size_and_align_of_mplace(&src)? else {
311                 span_bug!(self.cur_span(), "unsized fn arg with `extern` type tail should not be allowed")
312             };
313             let ptr = self.allocate_ptr(size, align, MemoryKind::Stack)?;
314             let dest_place =
315                 MPlaceTy::from_aligned_ptr_with_meta(ptr.into(), callee_arg.layout, src.meta);
316             // Update the local to be that new place.
317             *M::access_local_mut(self, dest_frame, dest_local)? = Operand::Indirect(*dest_place);
318         }
319         // We allow some transmutes here.
320         // FIXME: Depending on the PassMode, this should reset some padding to uninitialized. (This
321         // is true for all `copy_op`, but there are a lot of special cases for argument passing
322         // specifically.)
323         self.copy_op(&caller_arg, callee_arg, /*allow_transmute*/ true)
324     }
325
326     /// Call this function -- pushing the stack frame and initializing the arguments.
327     ///
328     /// `caller_fn_abi` is used to determine if all the arguments are passed the proper way.
329     /// However, we also need `caller_abi` to determine if we need to do untupling of arguments.
330     ///
331     /// `with_caller_location` indicates whether the caller passed a caller location. Miri
332     /// implements caller locations without argument passing, but to match `FnAbi` we need to know
333     /// when those arguments are present.
334     pub(crate) fn eval_fn_call(
335         &mut self,
336         fn_val: FnVal<'tcx, M::ExtraFnVal>,
337         (caller_abi, caller_fn_abi): (Abi, &FnAbi<'tcx, Ty<'tcx>>),
338         args: &[OpTy<'tcx, M::Provenance>],
339         with_caller_location: bool,
340         destination: &PlaceTy<'tcx, M::Provenance>,
341         target: Option<mir::BasicBlock>,
342         mut unwind: StackPopUnwind,
343     ) -> InterpResult<'tcx> {
344         trace!("eval_fn_call: {:#?}", fn_val);
345
346         let instance = match fn_val {
347             FnVal::Instance(instance) => instance,
348             FnVal::Other(extra) => {
349                 return M::call_extra_fn(
350                     self,
351                     extra,
352                     caller_abi,
353                     args,
354                     destination,
355                     target,
356                     unwind,
357                 );
358             }
359         };
360
361         match instance.def {
362             ty::InstanceDef::Intrinsic(def_id) => {
363                 assert!(self.tcx.is_intrinsic(def_id));
364                 // caller_fn_abi is not relevant here, we interpret the arguments directly for each intrinsic.
365                 M::call_intrinsic(self, instance, args, destination, target, unwind)
366             }
367             ty::InstanceDef::VTableShim(..)
368             | ty::InstanceDef::ReifyShim(..)
369             | ty::InstanceDef::ClosureOnceShim { .. }
370             | ty::InstanceDef::FnPtrShim(..)
371             | ty::InstanceDef::DropGlue(..)
372             | ty::InstanceDef::CloneShim(..)
373             | ty::InstanceDef::Item(_) => {
374                 // We need MIR for this fn
375                 let Some((body, instance)) =
376                     M::find_mir_or_eval_fn(self, instance, caller_abi, args, destination, target, unwind)? else {
377                         return Ok(());
378                     };
379
380                 // Compute callee information using the `instance` returned by
381                 // `find_mir_or_eval_fn`.
382                 // FIXME: for variadic support, do we have to somehow determine callee's extra_args?
383                 let callee_fn_abi = self.fn_abi_of_instance(instance, ty::List::empty())?;
384
385                 if callee_fn_abi.c_variadic || caller_fn_abi.c_variadic {
386                     throw_unsup_format!("calling a c-variadic function is not supported");
387                 }
388
389                 if M::enforce_abi(self) {
390                     if caller_fn_abi.conv != callee_fn_abi.conv {
391                         throw_ub_format!(
392                             "calling a function with calling convention {:?} using calling convention {:?}",
393                             callee_fn_abi.conv,
394                             caller_fn_abi.conv
395                         )
396                     }
397                 }
398
399                 if !matches!(unwind, StackPopUnwind::NotAllowed) && !callee_fn_abi.can_unwind {
400                     // The callee cannot unwind.
401                     unwind = StackPopUnwind::NotAllowed;
402                 }
403
404                 self.push_stack_frame(
405                     instance,
406                     body,
407                     destination,
408                     StackPopCleanup::Goto { ret: target, unwind },
409                 )?;
410
411                 // If an error is raised here, pop the frame again to get an accurate backtrace.
412                 // To this end, we wrap it all in a `try` block.
413                 let res: InterpResult<'tcx> = try {
414                     trace!(
415                         "caller ABI: {:?}, args: {:#?}",
416                         caller_abi,
417                         args.iter()
418                             .map(|arg| (arg.layout.ty, format!("{:?}", **arg)))
419                             .collect::<Vec<_>>()
420                     );
421                     trace!(
422                         "spread_arg: {:?}, locals: {:#?}",
423                         body.spread_arg,
424                         body.args_iter()
425                             .map(|local| (
426                                 local,
427                                 self.layout_of_local(self.frame(), local, None).unwrap().ty
428                             ))
429                             .collect::<Vec<_>>()
430                     );
431
432                     // In principle, we have two iterators: Where the arguments come from, and where
433                     // they go to.
434
435                     // For where they come from: If the ABI is RustCall, we untuple the
436                     // last incoming argument.  These two iterators do not have the same type,
437                     // so to keep the code paths uniform we accept an allocation
438                     // (for RustCall ABI only).
439                     let caller_args: Cow<'_, [OpTy<'tcx, M::Provenance>]> =
440                         if caller_abi == Abi::RustCall && !args.is_empty() {
441                             // Untuple
442                             let (untuple_arg, args) = args.split_last().unwrap();
443                             trace!("eval_fn_call: Will pass last argument by untupling");
444                             Cow::from(
445                                 args.iter()
446                                     .map(|a| Ok(a.clone()))
447                                     .chain(
448                                         (0..untuple_arg.layout.fields.count())
449                                             .map(|i| self.operand_field(untuple_arg, i)),
450                                     )
451                                     .collect::<InterpResult<'_, Vec<OpTy<'tcx, M::Provenance>>>>(
452                                     )?,
453                             )
454                         } else {
455                             // Plain arg passing
456                             Cow::from(args)
457                         };
458                     // If `with_caller_location` is set we pretend there is an extra argument (that
459                     // we will not pass).
460                     assert_eq!(
461                         caller_args.len() + if with_caller_location { 1 } else { 0 },
462                         caller_fn_abi.args.len(),
463                         "mismatch between caller ABI and caller arguments",
464                     );
465                     let mut caller_args = caller_args
466                         .iter()
467                         .zip(caller_fn_abi.args.iter())
468                         .filter(|arg_and_abi| !matches!(arg_and_abi.1.mode, PassMode::Ignore));
469
470                     // Now we have to spread them out across the callee's locals,
471                     // taking into account the `spread_arg`.  If we could write
472                     // this is a single iterator (that handles `spread_arg`), then
473                     // `pass_argument` would be the loop body. It takes care to
474                     // not advance `caller_iter` for ZSTs.
475                     let mut callee_args_abis = callee_fn_abi.args.iter();
476                     for local in body.args_iter() {
477                         let dest = self.eval_place(mir::Place::from(local))?;
478                         if Some(local) == body.spread_arg {
479                             // Must be a tuple
480                             for i in 0..dest.layout.fields.count() {
481                                 let dest = self.place_field(&dest, i)?;
482                                 let callee_abi = callee_args_abis.next().unwrap();
483                                 self.pass_argument(&mut caller_args, callee_abi, &dest)?;
484                             }
485                         } else {
486                             // Normal argument
487                             let callee_abi = callee_args_abis.next().unwrap();
488                             self.pass_argument(&mut caller_args, callee_abi, &dest)?;
489                         }
490                     }
491                     // If the callee needs a caller location, pretend we consume one more argument from the ABI.
492                     if instance.def.requires_caller_location(*self.tcx) {
493                         callee_args_abis.next().unwrap();
494                     }
495                     // Now we should have no more caller args or callee arg ABIs
496                     assert!(
497                         callee_args_abis.next().is_none(),
498                         "mismatch between callee ABI and callee body arguments"
499                     );
500                     if caller_args.next().is_some() {
501                         throw_ub_format!("calling a function with more arguments than it expected")
502                     }
503                     // Don't forget to check the return type!
504                     if !Self::check_argument_compat(&caller_fn_abi.ret, &callee_fn_abi.ret) {
505                         throw_ub_format!(
506                             "calling a function with return type {:?} passing \
507                                     return place of type {:?}",
508                             callee_fn_abi.ret.layout.ty,
509                             caller_fn_abi.ret.layout.ty,
510                         )
511                     }
512                 };
513                 match res {
514                     Err(err) => {
515                         self.stack_mut().pop();
516                         Err(err)
517                     }
518                     Ok(()) => Ok(()),
519                 }
520             }
521             // cannot use the shim here, because that will only result in infinite recursion
522             ty::InstanceDef::Virtual(def_id, idx) => {
523                 let mut args = args.to_vec();
524                 // We have to implement all "object safe receivers". So we have to go search for a
525                 // pointer or `dyn Trait` type, but it could be wrapped in newtypes. So recursively
526                 // unwrap those newtypes until we are there.
527                 let mut receiver = args[0].clone();
528                 let receiver_place = loop {
529                     match receiver.layout.ty.kind() {
530                         ty::Ref(..) | ty::RawPtr(..) => break self.deref_operand(&receiver)?,
531                         ty::Dynamic(..) => break receiver.assert_mem_place(), // no immediate unsized values
532                         _ => {
533                             // Not there yet, search for the only non-ZST field.
534                             let mut non_zst_field = None;
535                             for i in 0..receiver.layout.fields.count() {
536                                 let field = self.operand_field(&receiver, i)?;
537                                 if !field.layout.is_zst() {
538                                     assert!(
539                                         non_zst_field.is_none(),
540                                         "multiple non-ZST fields in dyn receiver type {}",
541                                         receiver.layout.ty
542                                     );
543                                     non_zst_field = Some(field);
544                                 }
545                             }
546                             receiver = non_zst_field.unwrap_or_else(|| {
547                                 panic!(
548                                     "no non-ZST fields in dyn receiver type {}",
549                                     receiver.layout.ty
550                                 )
551                             });
552                         }
553                     }
554                 };
555                 // Obtain the underlying trait we are working on.
556                 let receiver_tail = self
557                     .tcx
558                     .struct_tail_erasing_lifetimes(receiver_place.layout.ty, self.param_env);
559                 let ty::Dynamic(data, ..) = receiver_tail.kind() else {
560                     span_bug!(self.cur_span(), "dyanmic call on non-`dyn` type {}", receiver_tail)
561                 };
562
563                 // Get the required information from the vtable.
564                 let vptr = receiver_place.meta.unwrap_meta().to_pointer(self)?;
565                 let (dyn_ty, dyn_trait) = self.get_ptr_vtable(vptr)?;
566                 if dyn_trait != data.principal() {
567                     throw_ub_format!(
568                         "`dyn` call on a pointer whose vtable does not match its type"
569                     );
570                 }
571
572                 // Now determine the actual method to call. We can do that in two different ways and
573                 // compare them to ensure everything fits.
574                 let Some(ty::VtblEntry::Method(fn_inst)) = self.get_vtable_entries(vptr)?.get(idx).copied() else {
575                     throw_ub_format!("`dyn` call trying to call something that is not a method")
576                 };
577                 if cfg!(debug_assertions) {
578                     let tcx = *self.tcx;
579
580                     let trait_def_id = tcx.trait_of_item(def_id).unwrap();
581                     let virtual_trait_ref =
582                         ty::TraitRef::from_method(tcx, trait_def_id, instance.substs);
583                     assert_eq!(
584                         receiver_tail,
585                         virtual_trait_ref.self_ty(),
586                         "mismatch in underlying dyn trait computation within Miri and MIR building",
587                     );
588                     let existential_trait_ref =
589                         ty::ExistentialTraitRef::erase_self_ty(tcx, virtual_trait_ref);
590                     let concrete_trait_ref = existential_trait_ref.with_self_ty(tcx, dyn_ty);
591
592                     let concrete_method = Instance::resolve_for_vtable(
593                         tcx,
594                         self.param_env,
595                         def_id,
596                         instance.substs.rebase_onto(tcx, trait_def_id, concrete_trait_ref.substs),
597                     )
598                     .unwrap();
599                     assert_eq!(fn_inst, concrete_method);
600                 }
601
602                 // `*mut receiver_place.layout.ty` is almost the layout that we
603                 // want for args[0]: We have to project to field 0 because we want
604                 // a thin pointer.
605                 assert!(receiver_place.layout.is_unsized());
606                 let receiver_ptr_ty = self.tcx.mk_mut_ptr(receiver_place.layout.ty);
607                 let this_receiver_ptr = self.layout_of(receiver_ptr_ty)?.field(self, 0);
608                 // Adjust receiver argument.
609                 args[0] = OpTy::from(ImmTy::from_immediate(
610                     Scalar::from_maybe_pointer(receiver_place.ptr, self).into(),
611                     this_receiver_ptr,
612                 ));
613                 trace!("Patched receiver operand to {:#?}", args[0]);
614                 // recurse with concrete function
615                 self.eval_fn_call(
616                     FnVal::Instance(fn_inst),
617                     (caller_abi, caller_fn_abi),
618                     &args,
619                     with_caller_location,
620                     destination,
621                     target,
622                     unwind,
623                 )
624             }
625         }
626     }
627
628     fn drop_in_place(
629         &mut self,
630         place: &PlaceTy<'tcx, M::Provenance>,
631         instance: ty::Instance<'tcx>,
632         target: mir::BasicBlock,
633         unwind: Option<mir::BasicBlock>,
634     ) -> InterpResult<'tcx> {
635         trace!("drop_in_place: {:?},\n  {:?}, {:?}", *place, place.layout.ty, instance);
636         // We take the address of the object.  This may well be unaligned, which is fine
637         // for us here.  However, unaligned accesses will probably make the actual drop
638         // implementation fail -- a problem shared by rustc.
639         let place = self.force_allocation(place)?;
640
641         let (instance, place) = match place.layout.ty.kind() {
642             ty::Dynamic(..) => {
643                 // Dropping a trait object. Need to find actual drop fn.
644                 let place = self.unpack_dyn_trait(&place)?;
645                 let instance = ty::Instance::resolve_drop_in_place(*self.tcx, place.layout.ty);
646                 (instance, place)
647             }
648             _ => (instance, place),
649         };
650         let fn_abi = self.fn_abi_of_instance(instance, ty::List::empty())?;
651
652         let arg = ImmTy::from_immediate(
653             place.to_ref(self),
654             self.layout_of(self.tcx.mk_mut_ptr(place.layout.ty))?,
655         );
656         let ret = MPlaceTy::fake_alloc_zst(self.layout_of(self.tcx.types.unit)?);
657
658         self.eval_fn_call(
659             FnVal::Instance(instance),
660             (Abi::Rust, fn_abi),
661             &[arg.into()],
662             false,
663             &ret.into(),
664             Some(target),
665             match unwind {
666                 Some(cleanup) => StackPopUnwind::Cleanup(cleanup),
667                 None => StackPopUnwind::Skip,
668             },
669         )
670     }
671 }