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