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