]> git.lizzy.rs Git - rust.git/blob - src/shims/mod.rs
Auto merge of #693 - Aaron1011:feature/panic_unwind_final, r=oli-obk
[rust.git] / src / shims / mod.rs
1 pub mod dlsym;
2 pub mod env;
3 pub mod foreign_items;
4 pub mod intrinsics;
5 pub mod tls;
6 pub mod fs;
7 pub mod time;
8 pub mod panic;
9
10 use rustc::{mir, ty};
11 use crate::*;
12
13 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
14 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
15     fn find_fn(
16         &mut self,
17         instance: ty::Instance<'tcx>,
18         args: &[OpTy<'tcx, Tag>],
19         dest: Option<PlaceTy<'tcx, Tag>>,
20         ret: Option<mir::BasicBlock>,
21         unwind: Option<mir::BasicBlock>
22     ) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
23         let this = self.eval_context_mut();
24         trace!(
25             "eval_fn_call: {:#?}, {:?}",
26             instance,
27             dest.map(|place| *place)
28         );
29
30         // There are some more lang items we want to hook that CTFE does not hook (yet).
31         if this.tcx.lang_items().align_offset_fn() == Some(instance.def.def_id()) {
32             let dest = dest.unwrap();
33             let n = this
34                 .align_offset(args[0], args[1])?
35                 .unwrap_or_else(|| this.truncate(u128::max_value(), dest.layout));
36             this.write_scalar(Scalar::from_uint(n, dest.layout.size), dest)?;
37             this.goto_block(ret)?;
38             return Ok(None);
39         }
40
41         // Try to see if we can do something about foreign items.
42         if this.tcx.is_foreign_item(instance.def_id()) {
43             // An external function call that does not have a MIR body. We either find MIR elsewhere
44             // or emulate its effect.
45             // This will be Ok(None) if we're emulating the intrinsic entirely within Miri (no need
46             // to run extra MIR), and Ok(Some(body)) if we found MIR to run for the
47             // foreign function
48             // Any needed call to `goto_block` will be performed by `emulate_foreign_item`.
49             return this.emulate_foreign_item(instance.def_id(), args, dest, ret, unwind);
50         }
51
52         // Otherwise, load the MIR.
53         Ok(Some(this.load_mir(instance.def, None)?))
54     }
55
56     fn align_offset(
57         &mut self,
58         ptr_op: OpTy<'tcx, Tag>,
59         align_op: OpTy<'tcx, Tag>,
60     ) -> InterpResult<'tcx, Option<u128>> {
61         let this = self.eval_context_mut();
62
63         let req_align = this.force_bits(
64             this.read_scalar(align_op)?.not_undef()?,
65             this.pointer_size(),
66         )? as usize;
67
68         // FIXME: This should actually panic in the interpreted program
69         if !req_align.is_power_of_two() {
70             throw_unsup_format!("Required alignment should always be a power of two")
71         }
72
73         let ptr_scalar = this.read_scalar(ptr_op)?.not_undef()?;
74
75         if let Ok(ptr) = this.force_ptr(ptr_scalar) {
76             let cur_align = this.memory.get_size_and_align(ptr.alloc_id, AllocCheck::MaybeDead)?.1.bytes() as usize;
77             if cur_align >= req_align {
78                 // if the allocation alignment is at least the required alignment we use the
79                 // libcore implementation
80                 return Ok(Some(
81                     (this.force_bits(ptr_scalar, this.pointer_size())? as *const i8)
82                         .align_offset(req_align) as u128,
83                 ));
84             }
85         }
86         // If the allocation alignment is smaller than then required alignment or the pointer was
87         // actually an integer, we return `None`
88         Ok(None)
89     }
90 }