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