14 // End module management, begin local code
18 use rustc_middle::{mir, ty};
21 use helpers::check_arg_count;
23 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
24 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
25 fn find_mir_or_eval_fn(
27 instance: ty::Instance<'tcx>,
28 args: &[OpTy<'tcx, Tag>],
29 ret: Option<(PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
30 unwind: Option<mir::BasicBlock>,
31 ) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
32 let this = self.eval_context_mut();
33 trace!("eval_fn_call: {:#?}, {:?}", instance, ret.map(|p| *p.0));
35 // There are some more lang items we want to hook that CTFE does not hook (yet).
36 if this.tcx.lang_items().align_offset_fn() == Some(instance.def.def_id()) {
37 let &[ptr, align] = check_arg_count(args)?;
38 if this.align_offset(ptr, align, ret, unwind)? {
43 // Try to see if we can do something about foreign items.
44 if this.tcx.is_foreign_item(instance.def_id()) {
45 // An external function call that does not have a MIR body. We either find MIR elsewhere
46 // or emulate its effect.
47 // This will be Ok(None) if we're emulating the intrinsic entirely within Miri (no need
48 // to run extra MIR), and Ok(Some(body)) if we found MIR to run for the
50 // Any needed call to `goto_block` will be performed by `emulate_foreign_item`.
51 return this.emulate_foreign_item(instance.def_id(), args, ret, unwind);
54 // Otherwise, load the MIR.
55 Ok(Some(&*this.load_mir(instance.def, None)?))
58 /// Returns `true` if the computation was performed, and `false` if we should just evaluate
59 /// the actual MIR of `align_offset`.
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, bool> {
67 let this = self.eval_context_mut();
68 let (dest, ret) = ret.unwrap();
70 if this.memory.extra.check_alignment != AlignmentCheck::Symbolic {
71 // Just use actual implementation.
76 .force_bits(this.read_scalar(align_op)?.check_init()?, this.pointer_size())?;
78 // Stop if the alignment is not a power of two.
79 if !req_align.is_power_of_two() {
80 this.start_panic("align_offset: align is not a power-of-two", unwind)?;
81 return Ok(true); // nothing left to do
84 let ptr_scalar = this.read_scalar(ptr_op)?.check_init()?;
86 if let Ok(ptr) = this.force_ptr(ptr_scalar) {
87 // Only do anything if we can identify the allocation this goes to.
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 // real implementation.
97 // Return error result (usize::MAX), and jump to caller.
98 this.write_scalar(Scalar::from_machine_usize(this.machine_usize_max(), this), dest)?;
99 this.go_to_block(ret);