13 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
14 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
17 instance: ty::Instance<'tcx>,
18 args: &[OpTy<'tcx, Tag>],
19 dest: Option<PlaceTy<'tcx, Tag>>,
20 ret: Option<mir::BasicBlock>,
21 ) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
22 let this = self.eval_context_mut();
24 "eval_fn_call: {:#?}, {:?}",
26 dest.map(|place| *place)
29 // First, run the common hooks also supported by CTFE.
30 if this.hook_fn(instance, args, dest)? {
31 this.goto_block(ret)?;
34 // There are some more lang items we want to hook that CTFE does not hook (yet).
35 if this.tcx.lang_items().align_offset_fn() == Some(instance.def.def_id()) {
36 let dest = dest.unwrap();
38 .align_offset(args[0], args[1])?
39 .unwrap_or_else(|| this.truncate(u128::max_value(), dest.layout));
40 this.write_scalar(Scalar::from_uint(n, dest.layout.size), dest)?;
41 this.goto_block(ret)?;
45 // Try to see if we can do something about foreign items.
46 if this.tcx.is_foreign_item(instance.def_id()) {
47 // An external function that we cannot find MIR for, but we can still run enough
48 // of them to make miri viable.
49 this.emulate_foreign_item(instance.def_id(), args, dest, ret)?;
50 // `goto_block` already handled.
54 // Otherwise, load the MIR.
55 Ok(Some(this.load_mir(instance.def, None)?))
60 ptr_op: OpTy<'tcx, Tag>,
61 align_op: OpTy<'tcx, Tag>,
62 ) -> InterpResult<'tcx, Option<u128>> {
63 let this = self.eval_context_mut();
65 let req_align = this.force_bits(
66 this.read_scalar(align_op)?.not_undef()?,
70 // FIXME: This should actually panic in the interpreted program
71 if !req_align.is_power_of_two() {
72 throw_unsup_format!("Required alignment should always be a power of two")
75 let ptr_scalar = this.read_scalar(ptr_op)?.not_undef()?;
77 if let Ok(ptr) = this.force_ptr(ptr_scalar) {
78 let cur_align = this.memory.get_size_and_align(ptr.alloc_id, AllocCheck::MaybeDead)?.1.bytes() as usize;
79 if cur_align >= req_align {
80 // if the allocation alignment is at least the required alignment we use the
81 // libcore implementation
83 (this.force_bits(ptr_scalar, this.pointer_size())? as *const i8)
84 .align_offset(req_align) as u128,
88 // If the allocation alignment is smaller than then required alignment or the pointer was
89 // actually an integer, we return `None`