]> git.lizzy.rs Git - rust.git/blob - src/shims/mod.rs
728634a7f6f66b2efe683b10274ae1a2febf7579
[rust.git] / src / shims / mod.rs
1 pub mod foreign_items;
2 pub mod intrinsics;
3 pub mod tls;
4 pub mod dlsym;
5 pub mod env;
6
7 use rustc::{ty, mir};
8
9 use crate::*;
10
11 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
12 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
13     fn find_fn(
14         &mut self,
15         instance: ty::Instance<'tcx>,
16         args: &[OpTy<'tcx, Tag>],
17         dest: Option<PlaceTy<'tcx, Tag>>,
18         ret: Option<mir::BasicBlock>,
19     ) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
20         let this = self.eval_context_mut();
21         trace!("eval_fn_call: {:#?}, {:?}", instance, dest.map(|place| *place));
22
23         // First, run the common hooks also supported by CTFE.
24         if this.hook_fn(instance, args, dest)? {
25             this.goto_block(ret)?;
26             return Ok(None);
27         }
28         // There are some more lang items we want to hook that CTFE does not hook (yet).
29         if this.tcx.lang_items().align_offset_fn() == Some(instance.def.def_id()) {
30
31             let n = {
32                 let ptr = this.read_scalar(args[0])?.not_undef()?.assert_ptr();
33                 let align = this.force_bits(
34                     this.read_scalar(args[1])?.not_undef()?,
35                     this.pointer_size()
36                 )? as usize;
37
38                 let stride = this.memory().get(ptr.alloc_id)?.align.bytes() as usize;
39                 // if the allocation alignment is at least the required alignment, we use the
40                 // libcore implementation
41                 if stride >= align {
42                     ((stride + ptr.offset.bytes() as usize) as *const ())
43                         .align_offset(align) as u128
44                 } else {
45                     u128::max_value()
46                 }
47             };
48
49             let dest = dest.unwrap();
50             let n = this.truncate(n, dest.layout);
51             this.write_scalar(Scalar::from_uint(n, dest.layout.size), dest)?;
52             this.goto_block(ret)?;
53             return Ok(None);
54         }
55
56         // Try to see if we can do something about foreign items.
57         if this.tcx.is_foreign_item(instance.def_id()) {
58             // An external function that we cannot find MIR for, but we can still run enough
59             // of them to make miri viable.
60             this.emulate_foreign_item(instance.def_id(), args, dest, ret)?;
61             // `goto_block` already handled.
62             return Ok(None);
63         }
64
65         // Otherwise, load the MIR.
66         Ok(Some(this.load_mir(instance.def, None)?))
67     }
68 }