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