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