11 use std::convert::TryFrom;
15 use rustc_middle::{mir, ty};
19 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
20 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
21 fn find_mir_or_eval_fn(
23 instance: ty::Instance<'tcx>,
24 args: &[OpTy<'tcx, Tag>],
25 ret: Option<(PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
26 unwind: Option<mir::BasicBlock>,
27 ) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
28 let this = self.eval_context_mut();
29 trace!("eval_fn_call: {:#?}, {:?}", instance, ret.map(|p| *p.0));
31 // There are some more lang items we want to hook that CTFE does not hook (yet).
32 if this.tcx.lang_items().align_offset_fn() == Some(instance.def.def_id()) {
33 this.align_offset(args[0], args[1], ret, unwind)?;
37 // Try to see if we can do something about foreign items.
38 if this.tcx.is_foreign_item(instance.def_id()) {
39 // An external function call that does not have a MIR body. We either find MIR elsewhere
40 // or emulate its effect.
41 // This will be Ok(None) if we're emulating the intrinsic entirely within Miri (no need
42 // to run extra MIR), and Ok(Some(body)) if we found MIR to run for the
44 // Any needed call to `goto_block` will be performed by `emulate_foreign_item`.
45 return this.emulate_foreign_item(instance.def_id(), args, ret, unwind);
48 // Better error message for panics on Windows.
49 let def_id = instance.def_id();
50 if Some(def_id) == this.tcx.lang_items().begin_panic_fn() ||
51 Some(def_id) == this.tcx.lang_items().panic_impl()
53 this.check_panic_supported()?;
56 // Otherwise, load the MIR.
57 Ok(Some(&*this.load_mir(instance.def, None)?))
62 ptr_op: OpTy<'tcx, Tag>,
63 align_op: OpTy<'tcx, Tag>,
64 ret: Option<(PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
65 unwind: Option<mir::BasicBlock>,
66 ) -> InterpResult<'tcx> {
67 let this = self.eval_context_mut();
68 let (dest, ret) = ret.unwrap();
71 .force_bits(this.read_scalar(align_op)?.not_undef()?, this.pointer_size())?;
73 // Stop if the alignment is not a power of two.
74 if !req_align.is_power_of_two() {
75 return this.start_panic("align_offset: align is not a power-of-two", unwind);
78 let ptr_scalar = this.read_scalar(ptr_op)?.not_undef()?;
80 // Default: no result.
81 let mut result = this.machine_usize_max();
82 if let Ok(ptr) = this.force_ptr(ptr_scalar) {
83 // Only do anything if we can identify the allocation this goes to.
85 this.memory.get_size_and_align(ptr.alloc_id, AllocCheck::MaybeDead)?.1.bytes();
86 if u128::from(cur_align) >= req_align {
87 // If the allocation alignment is at least the required alignment we use the
88 // libcore implementation.
89 // FIXME: is this correct in case of truncation?
90 result = u64::try_from(
91 (this.force_bits(ptr_scalar, this.pointer_size())? as *const i8)
92 .align_offset(usize::try_from(req_align).unwrap())
97 // Return result, and jump to caller.
98 this.write_scalar(Scalar::from_machine_usize(result, this), dest)?;
99 this.go_to_block(ret);