1 #![warn(clippy::integer_arithmetic)]
4 #[cfg(target_os = "linux")]
18 // End module management, begin local code
22 use rustc_middle::{mir, ty};
23 use rustc_target::spec::abi::Abi;
26 use helpers::check_arg_count;
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(
32 instance: ty::Instance<'tcx>,
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);
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)? {
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
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);
61 // Otherwise, load the MIR.
62 Ok(Some((this.load_mir(instance.def, None)?, instance)))
65 /// Returns `true` if the computation was performed, and `false` if we should just evaluate
66 /// the actual MIR of `align_offset`.
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();
78 if this.machine.check_alignment != AlignmentCheck::Symbolic {
79 // Just use actual implementation.
83 let req_align = this.read_scalar(align_op)?.to_machine_usize(this)?;
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
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.
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.
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);