13 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
14 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
15 fn find_mir_or_eval_fn(
17 instance: ty::Instance<'tcx>,
18 args: &[OpTy<'tcx, Tag>],
19 ret: Option<(PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
20 unwind: Option<mir::BasicBlock>,
21 ) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
22 let this = self.eval_context_mut();
23 trace!("eval_fn_call: {:#?}, {:?}", instance, ret.map(|p| *p.0));
25 // There are some more lang items we want to hook that CTFE does not hook (yet).
26 if this.tcx.lang_items().align_offset_fn() == Some(instance.def.def_id()) {
27 let (dest, ret) = ret.unwrap();
29 .align_offset(args[0], args[1])?
30 .unwrap_or_else(|| this.truncate(u128::MAX, dest.layout));
31 this.write_scalar(Scalar::from_uint(n, dest.layout.size), dest)?;
32 this.go_to_block(ret);
36 // Try to see if we can do something about foreign items.
37 if this.tcx.is_foreign_item(instance.def_id()) {
38 // An external function call that does not have a MIR body. We either find MIR elsewhere
39 // or emulate its effect.
40 // This will be Ok(None) if we're emulating the intrinsic entirely within Miri (no need
41 // to run extra MIR), and Ok(Some(body)) if we found MIR to run for the
43 // Any needed call to `goto_block` will be performed by `emulate_foreign_item`.
44 return this.emulate_foreign_item(instance.def_id(), args, ret, unwind);
47 // Otherwise, load the MIR.
48 Ok(Some(&*this.load_mir(instance.def, None)?))
53 ptr_op: OpTy<'tcx, Tag>,
54 align_op: OpTy<'tcx, Tag>,
55 ) -> InterpResult<'tcx, Option<u128>> {
56 let this = self.eval_context_mut();
59 .force_bits(this.read_scalar(align_op)?.not_undef()?, this.pointer_size())?
62 // FIXME: This should actually panic in the interpreted program
63 if !req_align.is_power_of_two() {
64 throw_unsup_format!("Required alignment should always be a power of two")
67 let ptr_scalar = this.read_scalar(ptr_op)?.not_undef()?;
69 if let Ok(ptr) = this.force_ptr(ptr_scalar) {
71 this.memory.get_size_and_align(ptr.alloc_id, AllocCheck::MaybeDead)?.1.bytes()
73 if cur_align >= req_align {
74 // if the allocation alignment is at least the required alignment we use the
75 // libcore implementation
77 (this.force_bits(ptr_scalar, this.pointer_size())? as *const i8)
78 .align_offset(req_align) as u128,
82 // If the allocation alignment is smaller than then required alignment or the pointer was
83 // actually an integer, we return `None`