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