]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/src/shims/mod.rs
make eval_libc functions ICE on any problem
[rust.git] / src / tools / miri / src / shims / mod.rs
1 #![warn(clippy::integer_arithmetic)]
2
3 mod backtrace;
4 #[cfg(target_os = "linux")]
5 pub mod ffi_support;
6 pub mod foreign_items;
7 pub mod intrinsics;
8 pub mod unix;
9 pub mod windows;
10
11 pub mod dlsym;
12 pub mod env;
13 pub mod os_str;
14 pub mod panic;
15 pub mod time;
16 pub mod tls;
17
18 // End module management, begin local code
19
20 use log::trace;
21
22 use rustc_middle::{mir, ty};
23 use rustc_target::spec::abi::Abi;
24
25 use crate::*;
26 use helpers::check_arg_count;
27
28 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {}
29 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
30     fn find_mir_or_eval_fn(
31         &mut self,
32         instance: ty::Instance<'tcx>,
33         abi: Abi,
34         args: &[OpTy<'tcx, Provenance>],
35         dest: &PlaceTy<'tcx, Provenance>,
36         ret: Option<mir::BasicBlock>,
37         unwind: StackPopUnwind,
38     ) -> InterpResult<'tcx, Option<(&'mir mir::Body<'tcx>, ty::Instance<'tcx>)>> {
39         let this = self.eval_context_mut();
40         trace!("eval_fn_call: {:#?}, {:?}", instance, dest);
41
42         // There are some more lang items we want to hook that CTFE does not hook (yet).
43         if this.tcx.lang_items().align_offset_fn() == Some(instance.def.def_id()) {
44             let [ptr, align] = check_arg_count(args)?;
45             if this.align_offset(ptr, align, dest, ret, unwind)? {
46                 return Ok(None);
47             }
48         }
49
50         // Try to see if we can do something about foreign items.
51         if this.tcx.is_foreign_item(instance.def_id()) {
52             // An external function call that does not have a MIR body. We either find MIR elsewhere
53             // or emulate its effect.
54             // This will be Ok(None) if we're emulating the intrinsic entirely within Miri (no need
55             // to run extra MIR), and Ok(Some(body)) if we found MIR to run for the
56             // foreign function
57             // Any needed call to `goto_block` will be performed by `emulate_foreign_item`.
58             return this.emulate_foreign_item(instance.def_id(), abi, args, dest, ret, unwind);
59         }
60
61         // Otherwise, load the MIR.
62         Ok(Some((this.load_mir(instance.def, None)?, instance)))
63     }
64
65     /// Returns `true` if the computation was performed, and `false` if we should just evaluate
66     /// the actual MIR of `align_offset`.
67     fn align_offset(
68         &mut self,
69         ptr_op: &OpTy<'tcx, Provenance>,
70         align_op: &OpTy<'tcx, Provenance>,
71         dest: &PlaceTy<'tcx, Provenance>,
72         ret: Option<mir::BasicBlock>,
73         unwind: StackPopUnwind,
74     ) -> InterpResult<'tcx, bool> {
75         let this = self.eval_context_mut();
76         let ret = ret.unwrap();
77
78         if this.machine.check_alignment != AlignmentCheck::Symbolic {
79             // Just use actual implementation.
80             return Ok(false);
81         }
82
83         let req_align = this.read_scalar(align_op)?.to_machine_usize(this)?;
84
85         // Stop if the alignment is not a power of two.
86         if !req_align.is_power_of_two() {
87             this.start_panic("align_offset: align is not a power-of-two", unwind)?;
88             return Ok(true); // nothing left to do
89         }
90
91         let ptr = this.read_pointer(ptr_op)?;
92         // If this carries no provenance, treat it like an integer.
93         if ptr.provenance.is_none() {
94             // Use actual implementation.
95             return Ok(false);
96         }
97
98         if let Ok((alloc_id, _offset, _)) = this.ptr_try_get_alloc_id(ptr) {
99             // Only do anything if we can identify the allocation this goes to.
100             let (_size, cur_align, _kind) = this.get_alloc_info(alloc_id);
101             if cur_align.bytes() >= req_align {
102                 // If the allocation alignment is at least the required alignment we use the
103                 // real implementation.
104                 return Ok(false);
105             }
106         }
107
108         // Return error result (usize::MAX), and jump to caller.
109         this.write_scalar(Scalar::from_machine_usize(this.machine_usize_max(), this), dest)?;
110         this.go_to_block(ret);
111         Ok(true)
112     }
113 }