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