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