]> git.lizzy.rs Git - rust.git/blob - src/shims/mod.rs
Amend experimental thread support warnings
[rust.git] / src / shims / mod.rs
1 mod backtrace;
2 pub mod foreign_items;
3 pub mod intrinsics;
4 pub mod unix;
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 log::trace;
17
18 use rustc_middle::{mir, ty};
19 use rustc_target::spec::abi::Abi;
20
21 use crate::*;
22 use helpers::check_arg_count;
23
24 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
25 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
26     fn find_mir_or_eval_fn(
27         &mut self,
28         instance: ty::Instance<'tcx>,
29         abi: Abi,
30         args: &[OpTy<'tcx, Tag>],
31         dest: &PlaceTy<'tcx, Tag>,
32         ret: Option<mir::BasicBlock>,
33         unwind: StackPopUnwind,
34     ) -> InterpResult<'tcx, Option<(&'mir mir::Body<'tcx>, ty::Instance<'tcx>)>> {
35         let this = self.eval_context_mut();
36         trace!("eval_fn_call: {:#?}, {:?}", instance, dest);
37
38         // There are some more lang items we want to hook that CTFE does not hook (yet).
39         if this.tcx.lang_items().align_offset_fn() == Some(instance.def.def_id()) {
40             let [ptr, align] = check_arg_count(args)?;
41             if this.align_offset(ptr, align, dest, ret, unwind)? {
42                 return Ok(None);
43             }
44         }
45
46         // Try to see if we can do something about foreign items.
47         if this.tcx.is_foreign_item(instance.def_id()) {
48             // An external function call that does not have a MIR body. We either find MIR elsewhere
49             // or emulate its effect.
50             // This will be Ok(None) if we're emulating the intrinsic entirely within Miri (no need
51             // to run extra MIR), and Ok(Some(body)) if we found MIR to run for the
52             // foreign function
53             // Any needed call to `goto_block` will be performed by `emulate_foreign_item`.
54             return this.emulate_foreign_item(instance.def_id(), abi, args, dest, ret, unwind);
55         }
56
57         // Otherwise, load the MIR.
58         Ok(Some((&*this.load_mir(instance.def, None)?, instance)))
59     }
60
61     /// Returns `true` if the computation was performed, and `false` if we should just evaluate
62     /// the actual MIR of `align_offset`.
63     fn align_offset(
64         &mut self,
65         ptr_op: &OpTy<'tcx, Tag>,
66         align_op: &OpTy<'tcx, Tag>,
67         dest: &PlaceTy<'tcx, Tag>,
68         ret: Option<mir::BasicBlock>,
69         unwind: StackPopUnwind,
70     ) -> InterpResult<'tcx, bool> {
71         let this = self.eval_context_mut();
72         let ret = ret.unwrap();
73
74         if this.machine.check_alignment != AlignmentCheck::Symbolic {
75             // Just use actual implementation.
76             return Ok(false);
77         }
78
79         let req_align = this.read_scalar(align_op)?.to_machine_usize(this)?;
80
81         // Stop if the alignment is not a power of two.
82         if !req_align.is_power_of_two() {
83             this.start_panic("align_offset: align is not a power-of-two", unwind)?;
84             return Ok(true); // nothing left to do
85         }
86
87         let ptr = this.read_pointer(ptr_op)?;
88         if let Ok((alloc_id, _offset, _)) = this.ptr_try_get_alloc_id(ptr) {
89             // Only do anything if we can identify the allocation this goes to.
90             let (_, cur_align) = this.get_alloc_size_and_align(alloc_id, AllocCheck::MaybeDead)?;
91             if cur_align.bytes() >= req_align {
92                 // If the allocation alignment is at least the required alignment we use the
93                 // real implementation.
94                 return Ok(false);
95             }
96         }
97
98         // Return error result (usize::MAX), and jump to caller.
99         this.write_scalar(Scalar::from_machine_usize(this.machine_usize_max(), this), dest)?;
100         this.go_to_block(ret);
101         Ok(true)
102     }
103 }