]> git.lizzy.rs Git - rust.git/blob - src/shims/mod.rs
Auto merge of #1094 - Aaron1011:rustup-body, r=RalfJung
[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         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!(
24             "eval_fn_call: {:#?}, {:?}",
25             instance,
26             ret.map(|p| *p.0)
27         );
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             let (dest, ret) = ret.unwrap();
32             let n = this
33                 .align_offset(args[0], args[1])?
34                 .unwrap_or_else(|| this.truncate(u128::max_value(), dest.layout));
35             this.write_scalar(Scalar::from_uint(n, dest.layout.size), dest)?;
36             this.go_to_block(ret);
37             return Ok(None);
38         }
39
40         // Try to see if we can do something about foreign items.
41         if this.tcx.is_foreign_item(instance.def_id()) {
42             // An external function call that does not have a MIR body. We either find MIR elsewhere
43             // or emulate its effect.
44             // This will be Ok(None) if we're emulating the intrinsic entirely within Miri (no need
45             // to run extra MIR), and Ok(Some(body)) if we found MIR to run for the
46             // foreign function
47             // Any needed call to `goto_block` will be performed by `emulate_foreign_item`.
48             return this.emulate_foreign_item(instance.def_id(), args, ret, unwind);
49         }
50
51         // Otherwise, load the MIR.
52         Ok(Some(this.load_mir(instance.def, None)?.body()))
53     }
54
55     fn align_offset(
56         &mut self,
57         ptr_op: OpTy<'tcx, Tag>,
58         align_op: OpTy<'tcx, Tag>,
59     ) -> InterpResult<'tcx, Option<u128>> {
60         let this = self.eval_context_mut();
61
62         let req_align = this.force_bits(
63             this.read_scalar(align_op)?.not_undef()?,
64             this.pointer_size(),
65         )? as usize;
66
67         // FIXME: This should actually panic in the interpreted program
68         if !req_align.is_power_of_two() {
69             throw_unsup_format!("Required alignment should always be a power of two")
70         }
71
72         let ptr_scalar = this.read_scalar(ptr_op)?.not_undef()?;
73
74         if let Ok(ptr) = this.force_ptr(ptr_scalar) {
75             let cur_align = this.memory.get_size_and_align(ptr.alloc_id, AllocCheck::MaybeDead)?.1.bytes() as usize;
76             if cur_align >= req_align {
77                 // if the allocation alignment is at least the required alignment we use the
78                 // libcore implementation
79                 return Ok(Some(
80                     (this.force_bits(ptr_scalar, this.pointer_size())? as *const i8)
81                         .align_offset(req_align) as u128,
82                 ));
83             }
84         }
85         // If the allocation alignment is smaller than then required alignment or the pointer was
86         // actually an integer, we return `None`
87         Ok(None)
88     }
89 }