-pub mod dlsym;
-pub mod env;
+mod backtrace;
pub mod foreign_items;
pub mod intrinsics;
-pub mod tls;
-pub mod fs;
+pub mod unix;
+pub mod windows;
+
+pub mod dlsym;
+pub mod env;
+pub mod os_str;
+pub mod panic;
pub mod time;
+pub mod tls;
+
+// End module management, begin local code
-use rustc::{mir, ty};
+use log::trace;
+
+use rustc_middle::{mir, ty};
+use rustc_target::spec::abi::Abi;
use crate::*;
+use helpers::check_arg_count;
-impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
+impl<'mir, 'tcx: 'mir> 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>,
+ abi: Abi,
args: &[OpTy<'tcx, Tag>],
- dest: Option<PlaceTy<'tcx, Tag>>,
+ dest: &PlaceTy<'tcx, Tag>,
ret: Option<mir::BasicBlock>,
- ) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
+ unwind: StackPopUnwind,
+ ) -> InterpResult<'tcx, Option<(&'mir mir::Body<'tcx>, ty::Instance<'tcx>)>> {
let this = self.eval_context_mut();
- trace!(
- "eval_fn_call: {:#?}, {:?}",
- instance,
- dest.map(|place| *place)
- );
+ trace!("eval_fn_call: {:#?}, {:?}", instance, dest);
- // 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 dest = dest.unwrap();
- let n = this
- .align_offset(args[0], args[1])?
- .unwrap_or_else(|| this.truncate(u128::max_value(), dest.layout));
- this.write_scalar(Scalar::from_uint(n, dest.layout.size), dest)?;
- this.goto_block(ret)?;
- return Ok(None);
+ let [ptr, align] = check_arg_count(args)?;
+ if this.align_offset(ptr, align, dest, 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(), abi, args, dest, ret, unwind);
}
// Otherwise, load the MIR.
- Ok(Some(this.load_mir(instance.def, None)?))
+ Ok(Some((&*this.load_mir(instance.def, None)?, instance)))
}
+ /// Returns `true` if the computation was performed, and `false` if we should just evaluate
+ /// the actual MIR of `align_offset`.
fn align_offset(
&mut self,
- ptr_op: OpTy<'tcx, Tag>,
- align_op: OpTy<'tcx, Tag>,
- ) -> InterpResult<'tcx, Option<u128>> {
+ ptr_op: &OpTy<'tcx, Tag>,
+ align_op: &OpTy<'tcx, Tag>,
+ dest: &PlaceTy<'tcx, Tag>,
+ ret: Option<mir::BasicBlock>,
+ unwind: StackPopUnwind,
+ ) -> InterpResult<'tcx, bool> {
let this = self.eval_context_mut();
+ let ret = ret.unwrap();
- let req_align = this.force_bits(
- this.read_scalar(align_op)?.not_undef()?,
- this.pointer_size(),
- )? as usize;
+ if this.machine.check_alignment != AlignmentCheck::Symbolic {
+ // Just use actual implementation.
+ return Ok(false);
+ }
+
+ let req_align = this.read_scalar(align_op)?.to_machine_usize(this)?;
- // FIXME: This should actually panic in the interpreted program
+ // Stop if the alignment is not a power of two.
if !req_align.is_power_of_two() {
- throw_unsup_format!("Required alignment should always be a power of two")
+ this.start_panic("align_offset: align is not a power-of-two", unwind)?;
+ return Ok(true); // nothing left to do
}
- let ptr_scalar = this.read_scalar(ptr_op)?.not_undef()?;
-
- if let Ok(ptr) = this.force_ptr(ptr_scalar) {
- let cur_align = this.memory.get_size_and_align(ptr.alloc_id, AllocCheck::MaybeDead)?.1.bytes() as usize;
- if cur_align >= req_align {
- // if the allocation alignment is at least the required alignment we use the
- // libcore implementation
- return Ok(Some(
- (this.force_bits(ptr_scalar, this.pointer_size())? as *const i8)
- .align_offset(req_align) as u128,
- ));
+ let ptr = this.read_pointer(ptr_op)?;
+ if let Ok((alloc_id, _offset, _)) = this.ptr_try_get_alloc_id(ptr) {
+ // Only do anything if we can identify the allocation this goes to.
+ let (_, cur_align) = this.get_alloc_size_and_align(alloc_id, AllocCheck::MaybeDead)?;
+ if cur_align.bytes() >= req_align {
+ // If the allocation alignment is at least the required alignment we use the
+ // real implementation.
+ return Ok(false);
}
}
- // If the allocation alignment is smaller than then required alignment or the pointer was
- // actually an integer, we return `None`
- Ok(None)
+
+ // Return error result (usize::MAX), and jump to caller.
+ this.write_scalar(Scalar::from_machine_usize(this.machine_usize_max(), this), dest)?;
+ this.go_to_block(ret);
+ Ok(true)
}
}