+pub mod dlsym;
+pub mod env;
pub mod foreign_items;
+pub mod fs;
pub mod intrinsics;
+pub mod panic;
+pub mod time;
pub mod tls;
-pub mod dlsym;
-pub mod env;
-use rustc::{ty, mir};
+use std::convert::TryFrom;
+
+use rustc::{mir, ty};
use crate::*;
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
- fn find_fn(
+ fn find_mir_or_eval_fn(
&mut self,
instance: ty::Instance<'tcx>,
args: &[OpTy<'tcx, Tag>],
- dest: Option<PlaceTy<'tcx, Tag>>,
- ret: Option<mir::BasicBlock>,
+ ret: Option<(PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
+ unwind: Option<mir::BasicBlock>,
) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
let this = self.eval_context_mut();
- trace!("eval_fn_call: {:#?}, {:?}", instance, dest.map(|place| *place));
+ trace!("eval_fn_call: {:#?}, {:?}", instance, ret.map(|p| *p.0));
- // First, run the common hooks also supported by CTFE.
- if this.hook_fn(instance, args, dest)? {
- this.goto_block(ret)?;
- return Ok(None);
- }
// There are some more lang items we want to hook that CTFE does not hook (yet).
if this.tcx.lang_items().align_offset_fn() == Some(instance.def.def_id()) {
-
- let n = {
- let ptr = this.force_ptr(this.read_scalar(args[0])?.not_undef()?)?;
- let align = this.force_bits(
- this.read_scalar(args[1])?.not_undef()?,
- this.pointer_size()
- )? as usize;
-
- let stride = this.memory().get(ptr.alloc_id)?.align.bytes() as usize;
- // if the allocation alignment is at least the required alignment, we use the
- // libcore implementation
- if stride >= align {
- ((stride + ptr.offset.bytes() as usize) as *const ())
- .align_offset(align) as u128
- } else {
- u128::max_value()
- }
- };
-
- let dest = dest.unwrap();
- let n = this.truncate(n, dest.layout);
- this.write_scalar(Scalar::from_uint(n, dest.layout.size), dest)?;
- this.goto_block(ret)?;
+ this.align_offset(args[0], args[1], ret, unwind)?;
return Ok(None);
}
// Try to see if we can do something about foreign items.
if this.tcx.is_foreign_item(instance.def_id()) {
- // An external function that we cannot find MIR for, but we can still run enough
- // of them to make miri viable.
- this.emulate_foreign_item(instance.def_id(), args, dest, ret)?;
- // `goto_block` already handled.
- return Ok(None);
+ // An external function call that does not have a MIR body. We either find MIR elsewhere
+ // or emulate its effect.
+ // This will be Ok(None) if we're emulating the intrinsic entirely within Miri (no need
+ // to run extra MIR), and Ok(Some(body)) if we found MIR to run for the
+ // foreign function
+ // Any needed call to `goto_block` will be performed by `emulate_foreign_item`.
+ return this.emulate_foreign_item(instance.def_id(), args, ret, unwind);
}
// Otherwise, load the MIR.
- Ok(Some(this.load_mir(instance.def, None)?))
+ Ok(Some(&*this.load_mir(instance.def, None)?))
+ }
+
+ fn align_offset(
+ &mut self,
+ ptr_op: OpTy<'tcx, Tag>,
+ align_op: OpTy<'tcx, Tag>,
+ ret: Option<(PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
+ unwind: Option<mir::BasicBlock>,
+ ) -> InterpResult<'tcx> {
+ let this = self.eval_context_mut();
+ let (dest, ret) = ret.unwrap();
+
+ let req_align = this
+ .force_bits(this.read_scalar(align_op)?.not_undef()?, this.pointer_size())?;
+
+ // Stop if the alignment is not a power of two.
+ if !req_align.is_power_of_two() {
+ return this.start_panic("align_offset: align is not a power-of-two", unwind);
+ }
+
+ let ptr_scalar = this.read_scalar(ptr_op)?.not_undef()?;
+
+ // Default: no result.
+ let mut result = this.truncate(u128::MAX, dest.layout);
+ if let Ok(ptr) = this.force_ptr(ptr_scalar) {
+ // Only do anything if we can identify the allocation this goes to.
+ let cur_align =
+ this.memory.get_size_and_align(ptr.alloc_id, AllocCheck::MaybeDead)?.1.bytes();
+ if u128::from(cur_align) >= req_align {
+ // If the allocation alignment is at least the required alignment we use the
+ // libcore implementation.
+ // FIXME: is this correct in case of truncation?
+ result = u128::try_from(
+ (this.force_bits(ptr_scalar, this.pointer_size())? as *const i8)
+ .align_offset(usize::try_from(req_align).unwrap())
+ ).unwrap();
+ }
+ }
+
+ // Return result, and jump to caller.
+ this.write_scalar(Scalar::from_uint(result, dest.layout.size), dest)?;
+ this.go_to_block(ret);
+ Ok(())
}
}