]> git.lizzy.rs Git - rust.git/blob - src/shims/mod.rs
71ff6024ec6fe5e1dc057450f1a45448f1ae77e1
[rust.git] / src / shims / mod.rs
1 pub mod dlsym;
2 pub mod env;
3 pub mod foreign_items;
4 pub mod fs;
5 pub mod intrinsics;
6 pub mod os_str;
7 pub mod panic;
8 pub mod sync;
9 pub mod time;
10 pub mod tls;
11
12 use std::convert::TryFrom;
13
14 use log::trace;
15
16 use rustc_middle::{mir, ty};
17
18 use crate::*;
19
20 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
21 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
22     fn find_mir_or_eval_fn(
23         &mut self,
24         instance: ty::Instance<'tcx>,
25         args: &[OpTy<'tcx, Tag>],
26         ret: Option<(PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
27         unwind: Option<mir::BasicBlock>,
28     ) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
29         let this = self.eval_context_mut();
30         trace!("eval_fn_call: {:#?}, {:?}", instance, ret.map(|p| *p.0));
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             this.align_offset(args[0], args[1], ret, unwind)?;
35             return Ok(None);
36         }
37
38         // Try to see if we can do something about foreign items.
39         if this.tcx.is_foreign_item(instance.def_id()) {
40             // An external function call that does not have a MIR body. We either find MIR elsewhere
41             // or emulate its effect.
42             // This will be Ok(None) if we're emulating the intrinsic entirely within Miri (no need
43             // to run extra MIR), and Ok(Some(body)) if we found MIR to run for the
44             // foreign function
45             // Any needed call to `goto_block` will be performed by `emulate_foreign_item`.
46             return this.emulate_foreign_item(instance.def_id(), args, ret, unwind);
47         }
48
49         // Better error message for panics on Windows.
50         let def_id = instance.def_id();
51         if Some(def_id) == this.tcx.lang_items().begin_panic_fn() ||
52             Some(def_id) == this.tcx.lang_items().panic_impl()
53         {
54             this.check_panic_supported()?;
55         }
56
57         // Otherwise, load the MIR.
58         Ok(Some(&*this.load_mir(instance.def, None)?))
59     }
60
61     fn align_offset(
62         &mut self,
63         ptr_op: OpTy<'tcx, Tag>,
64         align_op: OpTy<'tcx, Tag>,
65         ret: Option<(PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
66         unwind: Option<mir::BasicBlock>,
67     ) -> InterpResult<'tcx> {
68         let this = self.eval_context_mut();
69         let (dest, ret) = ret.unwrap();
70
71         let req_align = this
72             .force_bits(this.read_scalar(align_op)?.not_undef()?, this.pointer_size())?;
73
74         // Stop if the alignment is not a power of two.
75         if !req_align.is_power_of_two() {
76             return this.start_panic("align_offset: align is not a power-of-two", unwind);
77         }
78
79         let ptr_scalar = this.read_scalar(ptr_op)?.not_undef()?;
80
81         // Default: no result.
82         let mut result = this.machine_usize_max();
83         if let Ok(ptr) = this.force_ptr(ptr_scalar) {
84             // Only do anything if we can identify the allocation this goes to.
85             let cur_align =
86                 this.memory.get_size_and_align(ptr.alloc_id, AllocCheck::MaybeDead)?.1.bytes();
87             if u128::from(cur_align) >= req_align {
88                 // If the allocation alignment is at least the required alignment we use the
89                 // libcore implementation.
90                 // FIXME: is this correct in case of truncation?
91                 result = u64::try_from(
92                     (this.force_bits(ptr_scalar, this.pointer_size())? as *const i8)
93                         .align_offset(usize::try_from(req_align).unwrap())
94                 ).unwrap();
95             }
96         }
97
98         // Return result, and jump to caller.
99         this.write_scalar(Scalar::from_machine_usize(result, this), dest)?;
100         this.go_to_block(ret);
101         Ok(())
102     }
103 }