]> git.lizzy.rs Git - rust.git/blob - src/shims/mod.rs
0b5bd7ae5cf0dba98450ad355d34042453db0327
[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             let dest = dest.unwrap();
31             let n = this.align_offset(args[0], args[1], 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, None)?))
48     }
49
50     fn align_offset(
51         &mut self,
52         ptr_op: OpTy<'tcx, Tag>,
53         align_op: OpTy<'tcx, Tag>,
54         layout: ty::layout::TyLayout<'tcx>,
55     ) -> InterpResult<'tcx, u128> {
56         let this = self.eval_context_mut();
57
58         let req_align = this.force_bits(
59             this.read_scalar(align_op)?.not_undef()?,
60             this.pointer_size()
61         )? as usize;
62
63         // FIXME: This should actually panic in the interpreted program
64         if !req_align.is_power_of_two() {
65             throw_unsup_format!("Required alignment should always be a power of two")
66         }
67
68         let ptr_scalar = this.read_scalar(ptr_op)?.not_undef()?;
69
70         if let Scalar::Ptr(ptr) = ptr_scalar {
71             let cur_align = this.memory().get(ptr.alloc_id)?.align.bytes() as usize;
72             if cur_align < req_align {
73                 return Ok(this.truncate(u128::max_value(), layout));
74             }
75         }
76
77         // if the allocation alignment is at least the required alignment or if the pointer is an
78         // integer, we use the libcore implementation
79         Ok(
80             (this.force_bits(ptr_scalar, this.pointer_size())? as *const i8)
81             .align_offset(req_align) as u128
82         )
83     }
84 }