]> git.lizzy.rs Git - rust.git/blob - src/shims/mod.rs
Auto merge of #826 - RalfJung:uninit, r=RalfJung
[rust.git] / src / shims / mod.rs
1 pub mod foreign_items;
2 pub mod intrinsics;
3 pub mod tls;
4 pub mod dlsym;
5
6 use rustc::{ty, mir};
7
8 use crate::*;
9
10 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
11 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
12     fn find_fn(
13         &mut self,
14         instance: ty::Instance<'tcx>,
15         args: &[OpTy<'tcx, Tag>],
16         dest: Option<PlaceTy<'tcx, Tag>>,
17         ret: Option<mir::BasicBlock>,
18     ) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
19         let this = self.eval_context_mut();
20         trace!("eval_fn_call: {:#?}, {:?}", instance, dest.map(|place| *place));
21
22         // First, run the common hooks also supported by CTFE.
23         if this.hook_fn(instance, args, dest)? {
24             this.goto_block(ret)?;
25             return Ok(None);
26         }
27         // There are some more lang items we want to hook that CTFE does not hook (yet).
28         if this.tcx.lang_items().align_offset_fn() == Some(instance.def.def_id()) {
29             // FIXME: return a real value in case the target allocation has an
30             // alignment bigger than the one requested.
31             let n = u128::max_value();
32             let dest = dest.unwrap();
33             let n = this.truncate(n, dest.layout);
34             this.write_scalar(Scalar::from_uint(n, dest.layout.size), dest)?;
35             this.goto_block(ret)?;
36             return Ok(None);
37         }
38
39         // Try to see if we can do something about foreign items.
40         if this.tcx.is_foreign_item(instance.def_id()) {
41             // An external function that we cannot find MIR for, but we can still run enough
42             // of them to make miri viable.
43             this.emulate_foreign_item(instance.def_id(), args, dest, ret)?;
44             // `goto_block` already handled.
45             return Ok(None);
46         }
47
48         // Otherwise, load the MIR.
49         Ok(Some(this.load_mir(instance.def)?))
50     }
51 }