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