]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/interpret/terminator.rs
`find_fn` -> `find_mir_or_eval_fn` rename
[rust.git] / src / librustc_mir / interpret / terminator.rs
1 use std::borrow::Cow;
2
3 use rustc::{mir, ty};
4 use rustc::ty::Instance;
5 use rustc::ty::layout::{self, TyLayout, LayoutOf};
6 use syntax::source_map::Span;
7 use rustc_target::spec::abi::Abi;
8
9 use super::{
10     GlobalId, InterpResult, PointerArithmetic,
11     InterpCx, Machine, OpTy, ImmTy, PlaceTy, MPlaceTy, StackPopCleanup, FnVal,
12 };
13
14 impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
15     pub(super) fn eval_terminator(
16         &mut self,
17         terminator: &mir::Terminator<'tcx>,
18     ) -> InterpResult<'tcx> {
19         use rustc::mir::TerminatorKind::*;
20         match terminator.kind {
21             Return => {
22                 self.frame().return_place.map(|r| self.dump_place(*r));
23                 self.pop_stack_frame(/* unwinding */ false)?
24             }
25
26             Goto { target } => self.go_to_block(target),
27
28             SwitchInt {
29                 ref discr,
30                 ref values,
31                 ref targets,
32                 ..
33             } => {
34                 let discr = self.read_immediate(self.eval_operand(discr, None)?)?;
35                 trace!("SwitchInt({:?})", *discr);
36
37                 // Branch to the `otherwise` case by default, if no match is found.
38                 let mut target_block = targets[targets.len() - 1];
39
40                 for (index, &const_int) in values.iter().enumerate() {
41                     // Compare using binary_op, to also support pointer values
42                     let res = self.overflowing_binary_op(mir::BinOp::Eq,
43                         discr,
44                         ImmTy::from_uint(const_int, discr.layout),
45                     )?.0;
46                     if res.to_bool()? {
47                         target_block = targets[index];
48                         break;
49                     }
50                 }
51
52                 self.go_to_block(target_block);
53             }
54
55             Call {
56                 ref func,
57                 ref args,
58                 ref destination,
59                 ref cleanup,
60                 ..
61             } => {
62                 let func = self.eval_operand(func, None)?;
63                 let (fn_val, abi) = match func.layout.ty.kind {
64                     ty::FnPtr(sig) => {
65                         let caller_abi = sig.abi();
66                         let fn_ptr = self.read_scalar(func)?.not_undef()?;
67                         let fn_val = self.memory.get_fn(fn_ptr)?;
68                         (fn_val, caller_abi)
69                     }
70                     ty::FnDef(def_id, substs) => {
71                         let sig = func.layout.ty.fn_sig(*self.tcx);
72                         (FnVal::Instance(self.resolve(def_id, substs)?), sig.abi())
73                     },
74                     _ => {
75                         bug!("invalid callee of type {:?}", func.layout.ty)
76                     }
77                 };
78                 let args = self.eval_operands(args)?;
79                 let ret = match destination {
80                     Some((dest, ret)) => Some((self.eval_place(dest)?, *ret)),
81                     None => None,
82                 };
83                 self.eval_fn_call(
84                     fn_val,
85                     terminator.source_info.span,
86                     abi,
87                     &args[..],
88                     ret,
89                     *cleanup
90                 )?;
91             }
92
93             Drop {
94                 ref location,
95                 target,
96                 unwind,
97             } => {
98                 // FIXME(CTFE): forbid drop in const eval
99                 let place = self.eval_place(location)?;
100                 let ty = place.layout.ty;
101                 trace!("TerminatorKind::drop: {:?}, type {}", location, ty);
102
103                 let instance = Instance::resolve_drop_in_place(*self.tcx, ty);
104                 self.drop_in_place(
105                     place,
106                     instance,
107                     terminator.source_info.span,
108                     target,
109                     unwind
110                 )?;
111             }
112
113             Assert {
114                 ref cond,
115                 expected,
116                 ref msg,
117                 target,
118                 ..
119             } => {
120                 let cond_val = self.read_immediate(self.eval_operand(cond, None)?)?
121                     .to_scalar()?.to_bool()?;
122                 if expected == cond_val {
123                     self.go_to_block(target);
124                 } else {
125                     // Compute error message
126                     use rustc::mir::interpret::PanicInfo::*;
127                     return Err(match msg {
128                         BoundsCheck { ref len, ref index } => {
129                             let len = self
130                                 .read_immediate(self.eval_operand(len, None)?)
131                                 .expect("can't eval len")
132                                 .to_scalar()?
133                                 .to_bits(self.memory.pointer_size())? as u64;
134                             let index = self
135                                 .read_immediate(self.eval_operand(index, None)?)
136                                 .expect("can't eval index")
137                                 .to_scalar()?
138                                 .to_bits(self.memory.pointer_size())? as u64;
139                             err_panic!(BoundsCheck { len, index })
140                         }
141                         Overflow(op) => err_panic!(Overflow(*op)),
142                         OverflowNeg => err_panic!(OverflowNeg),
143                         DivisionByZero => err_panic!(DivisionByZero),
144                         RemainderByZero => err_panic!(RemainderByZero),
145                         GeneratorResumedAfterReturn => err_panic!(GeneratorResumedAfterReturn),
146                         GeneratorResumedAfterPanic => err_panic!(GeneratorResumedAfterPanic),
147                         Panic { .. } => bug!("`Panic` variant cannot occur in MIR"),
148                     }
149                     .into());
150                 }
151             }
152
153
154             // When we encounter Resume, we've finished unwinding
155             // cleanup for the current stack frame. We pop it in order
156             // to continue unwinding the next frame
157             Resume => {
158                 trace!("unwinding: resuming from cleanup");
159                 // By definition, a Resume terminator means
160                 // that we're unwinding
161                 self.pop_stack_frame(/* unwinding */ true)?;
162                 return Ok(())
163             },
164
165             Yield { .. } |
166             GeneratorDrop |
167             DropAndReplace { .. } |
168             Abort => unimplemented!("{:#?}", terminator.kind),
169             FalseEdges { .. } => bug!("should have been eliminated by\
170                                       `simplify_branches` mir pass"),
171             FalseUnwind { .. } => bug!("should have been eliminated by\
172                                        `simplify_branches` mir pass"),
173             Unreachable => throw_ub!(Unreachable),
174         }
175
176         Ok(())
177     }
178
179     fn check_argument_compat(
180         rust_abi: bool,
181         caller: TyLayout<'tcx>,
182         callee: TyLayout<'tcx>,
183     ) -> bool {
184         if caller.ty == callee.ty {
185             // No question
186             return true;
187         }
188         if !rust_abi {
189             // Don't risk anything
190             return false;
191         }
192         // Compare layout
193         match (&caller.abi, &callee.abi) {
194             // Different valid ranges are okay (once we enforce validity,
195             // that will take care to make it UB to leave the range, just
196             // like for transmute).
197             (layout::Abi::Scalar(ref caller), layout::Abi::Scalar(ref callee)) =>
198                 caller.value == callee.value,
199             (layout::Abi::ScalarPair(ref caller1, ref caller2),
200              layout::Abi::ScalarPair(ref callee1, ref callee2)) =>
201                 caller1.value == callee1.value && caller2.value == callee2.value,
202             // Be conservative
203             _ => false
204         }
205     }
206
207     /// Pass a single argument, checking the types for compatibility.
208     fn pass_argument(
209         &mut self,
210         rust_abi: bool,
211         caller_arg: &mut impl Iterator<Item=OpTy<'tcx, M::PointerTag>>,
212         callee_arg: PlaceTy<'tcx, M::PointerTag>,
213     ) -> InterpResult<'tcx> {
214         if rust_abi && callee_arg.layout.is_zst() {
215             // Nothing to do.
216             trace!("Skipping callee ZST");
217             return Ok(());
218         }
219         let caller_arg = caller_arg.next()
220             .ok_or_else(|| err_unsup!(FunctionArgCountMismatch)) ?;
221         if rust_abi {
222             debug_assert!(!caller_arg.layout.is_zst(), "ZSTs must have been already filtered out");
223         }
224         // Now, check
225         if !Self::check_argument_compat(rust_abi, caller_arg.layout, callee_arg.layout) {
226             throw_unsup!(FunctionArgMismatch(caller_arg.layout.ty, callee_arg.layout.ty))
227         }
228         // We allow some transmutes here
229         self.copy_op_transmute(caller_arg, callee_arg)
230     }
231
232     /// Call this function -- pushing the stack frame and initializing the arguments.
233     fn eval_fn_call(
234         &mut self,
235         fn_val: FnVal<'tcx, M::ExtraFnVal>,
236         span: Span,
237         caller_abi: Abi,
238         args: &[OpTy<'tcx, M::PointerTag>],
239         ret: Option<(PlaceTy<'tcx, M::PointerTag>, mir::BasicBlock)>,
240         unwind: Option<mir::BasicBlock>
241     ) -> InterpResult<'tcx> {
242         trace!("eval_fn_call: {:#?}", fn_val);
243
244         let instance = match fn_val {
245             FnVal::Instance(instance) => instance,
246             FnVal::Other(extra) => {
247                 return M::call_extra_fn(self, extra, args, ret, unwind);
248             }
249         };
250
251         // ABI check
252         {
253             let callee_abi = {
254                 let instance_ty = instance.ty(*self.tcx);
255                 match instance_ty.kind {
256                     ty::FnDef(..) =>
257                         instance_ty.fn_sig(*self.tcx).abi(),
258                     ty::Closure(..) => Abi::RustCall,
259                     ty::Generator(..) => Abi::Rust,
260                     _ => bug!("unexpected callee ty: {:?}", instance_ty),
261                 }
262             };
263             let normalize_abi = |abi| match abi {
264                 Abi::Rust | Abi::RustCall | Abi::RustIntrinsic | Abi::PlatformIntrinsic =>
265                     // These are all the same ABI, really.
266                     Abi::Rust,
267                 abi =>
268                     abi,
269             };
270             if normalize_abi(caller_abi) != normalize_abi(callee_abi) {
271                 throw_unsup!(FunctionAbiMismatch(caller_abi, callee_abi))
272             }
273         }
274
275         match instance.def {
276             ty::InstanceDef::Intrinsic(..) => {
277                 assert!(caller_abi == Abi::RustIntrinsic || caller_abi == Abi::PlatformIntrinsic);
278                 return M::call_intrinsic(self, span, instance, args, ret, unwind);
279             }
280             ty::InstanceDef::VtableShim(..) |
281             ty::InstanceDef::ReifyShim(..) |
282             ty::InstanceDef::ClosureOnceShim { .. } |
283             ty::InstanceDef::FnPtrShim(..) |
284             ty::InstanceDef::DropGlue(..) |
285             ty::InstanceDef::CloneShim(..) |
286             ty::InstanceDef::Item(_) => {
287                 // We need MIR for this fn
288                 let body = match M::find_mir_or_eval_fn(self, instance, args, ret, unwind)? {
289                     Some(body) => body,
290                     None => return Ok(()),
291                 };
292
293                 self.push_stack_frame(
294                     instance,
295                     span,
296                     body,
297                     ret.map(|p| p.0),
298                     StackPopCleanup::Goto { ret: ret.map(|p| p.1), unwind }
299                 )?;
300
301                 // We want to pop this frame again in case there was an error, to put
302                 // the blame in the right location.  Until the 2018 edition is used in
303                 // the compiler, we have to do this with an immediately invoked function.
304                 let res = (||{
305                     trace!(
306                         "caller ABI: {:?}, args: {:#?}",
307                         caller_abi,
308                         args.iter()
309                             .map(|arg| (arg.layout.ty, format!("{:?}", **arg)))
310                             .collect::<Vec<_>>()
311                     );
312                     trace!(
313                         "spread_arg: {:?}, locals: {:#?}",
314                         body.spread_arg,
315                         body.args_iter()
316                             .map(|local|
317                                 (local, self.layout_of_local(self.frame(), local, None).unwrap().ty)
318                             )
319                             .collect::<Vec<_>>()
320                     );
321
322                     // Figure out how to pass which arguments.
323                     // The Rust ABI is special: ZST get skipped.
324                     let rust_abi = match caller_abi {
325                         Abi::Rust | Abi::RustCall => true,
326                         _ => false
327                     };
328                     // We have two iterators: Where the arguments come from,
329                     // and where they go to.
330
331                     // For where they come from: If the ABI is RustCall, we untuple the
332                     // last incoming argument.  These two iterators do not have the same type,
333                     // so to keep the code paths uniform we accept an allocation
334                     // (for RustCall ABI only).
335                     let caller_args : Cow<'_, [OpTy<'tcx, M::PointerTag>]> =
336                         if caller_abi == Abi::RustCall && !args.is_empty() {
337                             // Untuple
338                             let (&untuple_arg, args) = args.split_last().unwrap();
339                             trace!("eval_fn_call: Will pass last argument by untupling");
340                             Cow::from(args.iter().map(|&a| Ok(a))
341                                 .chain((0..untuple_arg.layout.fields.count()).into_iter()
342                                     .map(|i| self.operand_field(untuple_arg, i as u64))
343                                 )
344                                 .collect::<InterpResult<'_, Vec<OpTy<'tcx, M::PointerTag>>>>()?)
345                         } else {
346                             // Plain arg passing
347                             Cow::from(args)
348                         };
349                     // Skip ZSTs
350                     let mut caller_iter = caller_args.iter()
351                         .filter(|op| !rust_abi || !op.layout.is_zst())
352                         .map(|op| *op);
353
354                     // Now we have to spread them out across the callee's locals,
355                     // taking into account the `spread_arg`.  If we could write
356                     // this is a single iterator (that handles `spread_arg`), then
357                     // `pass_argument` would be the loop body. It takes care to
358                     // not advance `caller_iter` for ZSTs.
359                     let mut locals_iter = body.args_iter();
360                     while let Some(local) = locals_iter.next() {
361                         let dest = self.eval_place(
362                             &mir::Place::from(local)
363                         )?;
364                         if Some(local) == body.spread_arg {
365                             // Must be a tuple
366                             for i in 0..dest.layout.fields.count() {
367                                 let dest = self.place_field(dest, i as u64)?;
368                                 self.pass_argument(rust_abi, &mut caller_iter, dest)?;
369                             }
370                         } else {
371                             // Normal argument
372                             self.pass_argument(rust_abi, &mut caller_iter, dest)?;
373                         }
374                     }
375                     // Now we should have no more caller args
376                     if caller_iter.next().is_some() {
377                         trace!("Caller has passed too many args");
378                         throw_unsup!(FunctionArgCountMismatch)
379                     }
380                     // Don't forget to check the return type!
381                     if let Some((caller_ret, _)) = ret {
382                         let callee_ret = self.eval_place(
383                             &mir::Place::return_place()
384                         )?;
385                         if !Self::check_argument_compat(
386                             rust_abi,
387                             caller_ret.layout,
388                             callee_ret.layout,
389                         ) {
390                             throw_unsup!(
391                                 FunctionRetMismatch(caller_ret.layout.ty, callee_ret.layout.ty)
392                             )
393                         }
394                     } else {
395                         let local = mir::RETURN_PLACE;
396                         let callee_layout = self.layout_of_local(self.frame(), local, None)?;
397                         if !callee_layout.abi.is_uninhabited() {
398                             throw_unsup!(FunctionRetMismatch(
399                                 self.tcx.types.never, callee_layout.ty
400                             ))
401                         }
402                     }
403                     Ok(())
404                 })();
405                 match res {
406                     Err(err) => {
407                         self.stack.pop();
408                         Err(err)
409                     }
410                     Ok(v) => Ok(v)
411                 }
412             }
413             // cannot use the shim here, because that will only result in infinite recursion
414             ty::InstanceDef::Virtual(_, idx) => {
415                 let mut args = args.to_vec();
416                 // We have to implement all "object safe receivers".  Currently we
417                 // support built-in pointers (&, &mut, Box) as well as unsized-self.  We do
418                 // not yet support custom self types.
419                 // Also see librustc_codegen_llvm/abi.rs and librustc_codegen_llvm/mir/block.rs.
420                 let receiver_place = match args[0].layout.ty.builtin_deref(true) {
421                     Some(_) => {
422                         // Built-in pointer.
423                         self.deref_operand(args[0])?
424                     }
425                     None => {
426                         // Unsized self.
427                         args[0].assert_mem_place()
428                     }
429                 };
430                 // Find and consult vtable
431                 let vtable = receiver_place.vtable();
432                 let drop_fn = self.get_vtable_slot(vtable, idx)?;
433
434                 // `*mut receiver_place.layout.ty` is almost the layout that we
435                 // want for args[0]: We have to project to field 0 because we want
436                 // a thin pointer.
437                 assert!(receiver_place.layout.is_unsized());
438                 let receiver_ptr_ty = self.tcx.mk_mut_ptr(receiver_place.layout.ty);
439                 let this_receiver_ptr = self.layout_of(receiver_ptr_ty)?.field(self, 0)?;
440                 // Adjust receiver argument.
441                 args[0] = OpTy::from(ImmTy {
442                     layout: this_receiver_ptr,
443                     imm: receiver_place.ptr.into()
444                 });
445                 trace!("Patched self operand to {:#?}", args[0]);
446                 // recurse with concrete function
447                 self.eval_fn_call(drop_fn, span, caller_abi, &args, ret, unwind)
448             }
449         }
450     }
451
452     /// Evaluate a const function where all arguments (if any) are zero-sized types.
453     /// The evaluation is memoized thanks to the query system.
454     pub (crate) fn eval_const_fn_call(
455         &mut self,
456         gid: GlobalId<'tcx>,
457         ret: Option<(PlaceTy<'tcx, M::PointerTag>, mir::BasicBlock)>,
458     ) -> InterpResult<'tcx> {
459         trace!("eval_const_fn_call: {:?}", gid);
460
461         let place = self.const_eval_raw(gid)?;
462         let dest = ret.ok_or_else(|| err_ub!(Unreachable))?.0;
463
464         self.copy_op(place.into(), dest)?;
465
466         self.return_to_block(ret.map(|r| r.1))?;
467         self.dump_place(*dest);
468         return Ok(())
469     }
470
471     fn drop_in_place(
472         &mut self,
473         place: PlaceTy<'tcx, M::PointerTag>,
474         instance: ty::Instance<'tcx>,
475         span: Span,
476         target: mir::BasicBlock,
477         unwind: Option<mir::BasicBlock>
478     ) -> InterpResult<'tcx> {
479         trace!("drop_in_place: {:?},\n  {:?}, {:?}", *place, place.layout.ty, instance);
480         // We take the address of the object.  This may well be unaligned, which is fine
481         // for us here.  However, unaligned accesses will probably make the actual drop
482         // implementation fail -- a problem shared by rustc.
483         let place = self.force_allocation(place)?;
484
485         let (instance, place) = match place.layout.ty.kind {
486             ty::Dynamic(..) => {
487                 // Dropping a trait object.
488                 self.unpack_dyn_trait(place)?
489             }
490             _ => (instance, place),
491         };
492
493         let arg = ImmTy {
494             imm: place.to_ref(),
495             layout: self.layout_of(self.tcx.mk_mut_ptr(place.layout.ty))?,
496         };
497
498         let ty = self.tcx.mk_unit(); // return type is ()
499         let dest = MPlaceTy::dangling(self.layout_of(ty)?, self);
500
501         self.eval_fn_call(
502             FnVal::Instance(instance),
503             span,
504             Abi::Rust,
505             &[arg.into()],
506             Some((dest.into(), target)),
507             unwind
508         )
509     }
510 }