]> git.lizzy.rs Git - rust.git/blob - src/shims/mod.rs
Fix merge conflicts
[rust.git] / src / shims / mod.rs
1 pub mod dlsym;
2 pub mod env;
3 pub mod foreign_items;
4 pub mod intrinsics;
5 pub mod tls;
6 pub mod fs;
7 pub mod time;
8
9 use rustc::{mir, ty};
10
11 use crate::*;
12
13 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
14 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
15     fn find_fn(
16         &mut self,
17         instance: ty::Instance<'tcx>,
18         args: &[OpTy<'tcx, Tag>],
19         dest: Option<PlaceTy<'tcx, Tag>>,
20         ret: Option<mir::BasicBlock>,
21     ) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
22         let this = self.eval_context_mut();
23         trace!(
24             "eval_fn_call: {:#?}, {:?}",
25             instance,
26             dest.map(|place| *place)
27         );
28
29         // First, run the common hooks also supported by CTFE.
30         if this.hook_fn(instance, args, dest)? {
31             this.goto_block(ret)?;
32             return Ok(None);
33         }
34         // There are some more lang items we want to hook that CTFE does not hook (yet).
35         if this.tcx.lang_items().align_offset_fn() == Some(instance.def.def_id()) {
36             let dest = dest.unwrap();
37             let n = this
38                 .align_offset(args[0], args[1])?
39                 .unwrap_or_else(|| this.truncate(u128::max_value(), dest.layout));
40             this.write_scalar(Scalar::from_uint(n, dest.layout.size), dest)?;
41             this.goto_block(ret)?;
42             return Ok(None);
43         }
44
45         // Try to see if we can do something about foreign items.
46         if this.tcx.is_foreign_item(instance.def_id()) {
47             // An external function that we cannot find MIR for, but we can still run enough
48             // of them to make miri viable.
49             this.emulate_foreign_item(instance.def_id(), args, dest, ret)?;
50             // `goto_block` already handled.
51             return Ok(None);
52         }
53
54         // Otherwise, load the MIR.
55         Ok(Some(this.load_mir(instance.def, None)?))
56     }
57
58     fn align_offset(
59         &mut self,
60         ptr_op: OpTy<'tcx, Tag>,
61         align_op: OpTy<'tcx, Tag>,
62     ) -> InterpResult<'tcx, Option<u128>> {
63         let this = self.eval_context_mut();
64
65         let req_align = this.force_bits(
66             this.read_scalar(align_op)?.not_undef()?,
67             this.pointer_size(),
68         )? as usize;
69
70         // FIXME: This should actually panic in the interpreted program
71         if !req_align.is_power_of_two() {
72             throw_unsup_format!("Required alignment should always be a power of two")
73         }
74
75         let ptr_scalar = this.read_scalar(ptr_op)?.not_undef()?;
76
77         if let Ok(ptr) = this.force_ptr(ptr_scalar) {
78             let cur_align = this.memory.get_size_and_align(ptr.alloc_id, AllocCheck::MaybeDead)?.1.bytes() as usize;
79             if cur_align >= req_align {
80                 // if the allocation alignment is at least the required alignment we use the
81                 // libcore implementation
82                 return Ok(Some(
83                     (this.force_bits(ptr_scalar, this.pointer_size())? as *const i8)
84                         .align_offset(req_align) as u128,
85                 ));
86             }
87         }
88         // If the allocation alignment is smaller than then required alignment or the pointer was
89         // actually an integer, we return `None`
90         Ok(None)
91     }
92 }