]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_const_eval/src/interpret/terminator.rs
Tweak move error
[rust.git] / compiler / rustc_const_eval / src / interpret / terminator.rs
1 use std::borrow::Cow;
2 use std::convert::TryFrom;
3
4 use rustc_middle::ty::layout::{FnAbiOf, LayoutOf};
5 use rustc_middle::ty::Instance;
6 use rustc_middle::{
7     mir,
8     ty::{self, Ty},
9 };
10 use rustc_target::abi;
11 use rustc_target::abi::call::{ArgAbi, ArgAttribute, ArgAttributes, FnAbi, PassMode};
12 use rustc_target::spec::abi::Abi;
13
14 use super::{
15     FnVal, ImmTy, InterpCx, InterpResult, MPlaceTy, Machine, OpTy, PlaceTy, Scalar,
16     StackPopCleanup, StackPopUnwind,
17 };
18
19 impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
20     pub(super) fn eval_terminator(
21         &mut self,
22         terminator: &mir::Terminator<'tcx>,
23     ) -> InterpResult<'tcx> {
24         use rustc_middle::mir::TerminatorKind::*;
25         match terminator.kind {
26             Return => {
27                 self.pop_stack_frame(/* unwinding */ false)?
28             }
29
30             Goto { target } => self.go_to_block(target),
31
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);
36
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();
40
41                 for (const_int, target) in targets.iter() {
42                     // Compare using binary_op, to also support pointer values
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 { 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)?;
64
65                 let fn_sig_binder = func.layout.ty.fn_sig(*self.tcx);
66                 let fn_sig =
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));
70
71                 let (fn_val, fn_abi, with_caller_location) = match *func.layout.ty.kind() {
72                     ty::FnPtr(_sig) => {
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)
76                     }
77                     ty::FnDef(def_id, substs) => {
78                         let instance =
79                             self.resolve(ty::WithOptConstParam::unknown(def_id), substs)?;
80                         (
81                             FnVal::Instance(instance),
82                             self.fn_abi_of_instance(instance, extra_args)?,
83                             instance.def.requires_caller_location(*self.tcx),
84                         )
85                     }
86                     _ => span_bug!(
87                         terminator.source_info.span,
88                         "invalid callee of type {:?}",
89                         func.layout.ty
90                     ),
91                 };
92
93                 let dest_place;
94                 let ret = match destination {
95                     Some((dest, ret)) => {
96                         dest_place = self.eval_place(dest)?;
97                         Some((&dest_place, ret))
98                     }
99                     None => None,
100                 };
101                 self.eval_fn_call(
102                     fn_val,
103                     (fn_sig.abi, fn_abi),
104                     &args,
105                     with_caller_location,
106                     ret,
107                     match (cleanup, fn_abi.can_unwind) {
108                         (Some(cleanup), true) => StackPopUnwind::Cleanup(*cleanup),
109                         (None, true) => StackPopUnwind::Skip,
110                         (_, false) => StackPopUnwind::NotAllowed,
111                     },
112                 )?;
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");
117                 }
118             }
119
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);
124
125                 let instance = Instance::resolve_drop_in_place(*self.tcx, ty);
126                 self.drop_in_place(&place, instance, target, unwind)?;
127             }
128
129             Assert { ref cond, expected, ref msg, target, cleanup } => {
130                 let cond_val =
131                     self.read_immediate(&self.eval_operand(cond, None)?)?.to_scalar()?.to_bool()?;
132                 if expected == cond_val {
133                     self.go_to_block(target);
134                 } else {
135                     M::assert_panic(self, msg, cleanup)?;
136                 }
137             }
138
139             Abort => {
140                 M::abort(self, "the program aborted execution".to_owned())?;
141             }
142
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
146             Resume => {
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)?;
151                 return Ok(());
152             }
153
154             // It is UB to ever encounter this.
155             Unreachable => throw_ub!(Unreachable),
156
157             // These should never occur for MIR we actually run.
158             DropAndReplace { .. }
159             | FalseEdge { .. }
160             | FalseUnwind { .. }
161             | Yield { .. }
162             | GeneratorDrop => span_bug!(
163                 terminator.source_info.span,
164                 "{:#?} should have been eliminated by MIR pass",
165                 terminator.kind
166             ),
167
168             // Inline assembly can't be interpreted.
169             InlineAsm { .. } => throw_unsup_format!("inline assembly is not supported"),
170         }
171
172         Ok(())
173     }
174
175     fn check_argument_compat(
176         caller_abi: &ArgAbi<'tcx, Ty<'tcx>>,
177         callee_abi: &ArgAbi<'tcx, Ty<'tcx>>,
178     ) -> bool {
179         // Heuristic for type comparison.
180         let layout_compat = || {
181             if caller_abi.layout.ty == callee_abi.layout.ty {
182                 // No question
183                 return true;
184             }
185             // Compare layout
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
192                 }
193                 (
194                     abi::Abi::ScalarPair(caller1, caller2),
195                     abi::Abi::ScalarPair(callee1, callee2),
196                 ) => caller1.value == callee1.value && caller2.value == callee2.value,
197                 // Be conservative
198                 _ => false,
199             }
200         };
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)
209             {
210                 return false;
211             }
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 {
215                 return false;
216             }
217             return true;
218         };
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)
224             }
225             (PassMode::Cast(c1), PassMode::Cast(c2)) => c1 == c2,
226             (
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,
230             (
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,
234             _ => false,
235         };
236
237         if layout_compat() && pad_compat() && mode_compat() {
238             return true;
239         }
240         trace!(
241             "check_argument_compat: incompatible ABIs:\ncaller: {:?}\ncallee: {:?}",
242             caller_abi,
243             callee_abi
244         );
245         return false;
246     }
247
248     /// Initialize a single callee argument, checking the types for compatibility.
249     fn pass_argument<'x, 'y>(
250         &mut self,
251         caller_args: &mut impl Iterator<
252             Item = (&'x OpTy<'tcx, M::PointerTag>, &'y ArgAbi<'tcx, Ty<'tcx>>),
253         >,
254         callee_abi: &ArgAbi<'tcx, Ty<'tcx>>,
255         callee_arg: &PlaceTy<'tcx, M::PointerTag>,
256     ) -> InterpResult<'tcx>
257     where
258         'tcx: 'x,
259         'tcx: 'y,
260     {
261         if matches!(callee_abi.mode, PassMode::Ignore) {
262             // This one is skipped.
263             return Ok(());
264         }
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")
268         })?;
269         // Now, check
270         if !Self::check_argument_compat(caller_abi, callee_abi) {
271             throw_ub_format!(
272                 "calling a function with argument of type {:?} passing data of type {:?}",
273                 callee_arg.layout.ty,
274                 caller_arg.layout.ty
275             )
276         }
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
280         // specifically.)
281         self.copy_op_transmute(&caller_arg, callee_arg)
282     }
283
284     /// Call this function -- pushing the stack frame and initializing the arguments.
285     ///
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.
288     ///
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(
293         &mut self,
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);
302
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);
307             }
308         };
309
310         match instance.def {
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)
315             }
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 {
326                         return Ok(());
327                     };
328
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())?;
333
334                 if callee_fn_abi.c_variadic != caller_fn_abi.c_variadic {
335                     throw_ub_format!(
336                         "calling a c-variadic function via a non-variadic call site, or vice versa"
337                     );
338                 }
339                 if callee_fn_abi.c_variadic {
340                     throw_unsup_format!("calling a c-variadic function is not supported");
341                 }
342
343                 if M::enforce_abi(self) {
344                     if caller_fn_abi.conv != callee_fn_abi.conv {
345                         throw_ub_format!(
346                             "calling a function with calling convention {:?} using calling convention {:?}",
347                             callee_fn_abi.conv,
348                             caller_fn_abi.conv
349                         )
350                     }
351                 }
352
353                 if !matches!(unwind, StackPopUnwind::NotAllowed) && !callee_fn_abi.can_unwind {
354                     // The callee cannot unwind.
355                     unwind = StackPopUnwind::NotAllowed;
356                 }
357
358                 self.push_stack_frame(
359                     instance,
360                     body,
361                     ret.map(|p| p.0),
362                     StackPopCleanup::Goto { ret: ret.map(|p| p.1), unwind },
363                 )?;
364
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 {
368                     trace!(
369                         "caller ABI: {:?}, args: {:#?}",
370                         caller_abi,
371                         args.iter()
372                             .map(|arg| (arg.layout.ty, format!("{:?}", **arg)))
373                             .collect::<Vec<_>>()
374                     );
375                     trace!(
376                         "spread_arg: {:?}, locals: {:#?}",
377                         body.spread_arg,
378                         body.args_iter()
379                             .map(|local| (
380                                 local,
381                                 self.layout_of_local(self.frame(), local, None).unwrap().ty
382                             ))
383                             .collect::<Vec<_>>()
384                     );
385
386                     // In principle, we have two iterators: Where the arguments come from, and where
387                     // they go to.
388
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() {
395                             // Untuple
396                             let (untuple_arg, args) = args.split_last().unwrap();
397                             trace!("eval_fn_call: Will pass last argument by untupling");
398                             Cow::from(
399                                 args.iter()
400                                     .map(|&a| Ok(a))
401                                     .chain(
402                                         (0..untuple_arg.layout.fields.count())
403                                             .map(|i| self.operand_field(untuple_arg, i)),
404                                     )
405                                     .collect::<InterpResult<'_, Vec<OpTy<'tcx, M::PointerTag>>>>(
406                                     )?,
407                             )
408                         } else {
409                             // Plain arg passing
410                             Cow::from(args)
411                         };
412                     // If `with_caller_location` is set we pretend there is an extra argument (that
413                     // we will not pass).
414                     assert_eq!(
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",
418                     );
419                     let mut caller_args = caller_args
420                         .iter()
421                         .zip(caller_fn_abi.args.iter())
422                         .filter(|arg_and_abi| !matches!(arg_and_abi.1.mode, PassMode::Ignore));
423
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 {
433                             // Must be a tuple
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)?;
438                             }
439                         } else {
440                             // Normal argument
441                             let callee_abi = callee_args_abis.next().unwrap();
442                             self.pass_argument(&mut caller_args, callee_abi, &dest)?;
443                         }
444                     }
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();
448                     }
449                     // Now we should have no more caller args or callee arg ABIs
450                     assert!(
451                         callee_args_abis.next().is_none(),
452                         "mismatch between callee ABI and callee body arguments"
453                     );
454                     if caller_args.next().is_some() {
455                         throw_ub_format!("calling a function with more arguments than it expected")
456                     }
457                     // Don't forget to check the return type!
458                     if !Self::check_argument_compat(&caller_fn_abi.ret, &callee_fn_abi.ret) {
459                         throw_ub_format!(
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,
464                         )
465                     }
466                 };
467                 match res {
468                     Err(err) => {
469                         self.stack_mut().pop();
470                         Err(err)
471                     }
472                     Ok(()) => Ok(()),
473                 }
474             }
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) {
483                     Some(_) => {
484                         // Built-in pointer.
485                         self.deref_operand(&args[0])?
486                     }
487                     None => {
488                         // Unsized self.
489                         args[0].assert_mem_place()
490                     }
491                 };
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())?;
495
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
498                 // a thin pointer.
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(),
505                     this_receiver_ptr,
506                 ));
507                 trace!("Patched self operand to {:#?}", args[0]);
508                 // recurse with concrete function
509                 self.eval_fn_call(
510                     fn_val,
511                     (caller_abi, caller_fn_abi),
512                     &args,
513                     with_caller_location,
514                     ret,
515                     unwind,
516                 )
517             }
518         }
519     }
520
521     fn drop_in_place(
522         &mut self,
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)?;
533
534         let (instance, place) = match place.layout.ty.kind() {
535             ty::Dynamic(..) => {
536                 // Dropping a trait object.
537                 self.unpack_dyn_trait(&place)?
538             }
539             _ => (instance, place),
540         };
541         let fn_abi = self.fn_abi_of_instance(instance, ty::List::empty())?;
542
543         let arg = ImmTy::from_immediate(
544             place.to_ref(self),
545             self.layout_of(self.tcx.mk_mut_ptr(place.layout.ty))?,
546         );
547
548         let ty = self.tcx.mk_unit(); // return type is ()
549         let dest = MPlaceTy::dangling(self.layout_of(ty)?);
550
551         self.eval_fn_call(
552             FnVal::Instance(instance),
553             (Abi::Rust, fn_abi),
554             &[arg.into()],
555             false,
556             Some((&dest.into(), target)),
557             match unwind {
558                 Some(cleanup) => StackPopUnwind::Cleanup(cleanup),
559                 None => StackPopUnwind::Skip,
560             },
561         )
562     }
563 }