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