2020-09-24 17:52:17 -05:00
|
|
|
mod backtrace;
|
2019-06-30 08:35:28 -05:00
|
|
|
pub mod foreign_items;
|
2019-12-23 05:56:23 -06:00
|
|
|
pub mod intrinsics;
|
2020-06-27 06:19:35 -05:00
|
|
|
pub mod posix;
|
|
|
|
pub mod windows;
|
|
|
|
|
|
|
|
pub mod dlsym;
|
|
|
|
pub mod env;
|
2020-03-28 09:43:47 -05:00
|
|
|
pub mod os_str;
|
2019-04-14 20:02:55 -05:00
|
|
|
pub mod panic;
|
2019-12-23 05:56:23 -06:00
|
|
|
pub mod time;
|
|
|
|
pub mod tls;
|
2019-06-30 16:24:08 -05:00
|
|
|
|
2020-06-27 06:19:35 -05:00
|
|
|
// End module management, begin local code
|
|
|
|
|
2020-03-30 04:07:32 -05:00
|
|
|
use log::trace;
|
|
|
|
|
|
|
|
use rustc_middle::{mir, ty};
|
2021-01-21 20:45:39 -06:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2019-06-30 16:24:08 -05:00
|
|
|
|
2020-03-17 09:18:53 -05:00
|
|
|
use crate::*;
|
2020-04-02 16:09:20 -05:00
|
|
|
use helpers::check_arg_count;
|
2020-03-17 09:18:53 -05:00
|
|
|
|
2020-04-01 18:55:52 -05:00
|
|
|
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
|
2019-06-30 16:24:08 -05:00
|
|
|
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
|
2019-12-04 16:31:39 -06:00
|
|
|
fn find_mir_or_eval_fn(
|
2019-06-30 16:24:08 -05:00
|
|
|
&mut self,
|
|
|
|
instance: ty::Instance<'tcx>,
|
2021-01-21 20:45:39 -06:00
|
|
|
abi: Abi,
|
2019-06-30 16:24:08 -05:00
|
|
|
args: &[OpTy<'tcx, Tag>],
|
2021-02-19 18:00:00 -06:00
|
|
|
ret: Option<(&PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
|
2021-05-22 09:45:00 -05:00
|
|
|
unwind: StackPopUnwind,
|
2021-11-29 11:48:04 -06:00
|
|
|
) -> InterpResult<'tcx, Option<(&'mir mir::Body<'tcx>, ty::Instance<'tcx>)>> {
|
2019-06-30 16:24:08 -05:00
|
|
|
let this = self.eval_context_mut();
|
2021-02-19 18:00:00 -06:00
|
|
|
trace!("eval_fn_call: {:#?}, {:?}", instance, ret.map(|p| p.0));
|
2019-06-30 16:24:08 -05:00
|
|
|
|
|
|
|
// 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()) {
|
2021-02-19 18:00:00 -06:00
|
|
|
let &[ref ptr, ref align] = check_arg_count(args)?;
|
2020-08-16 11:16:34 -05:00
|
|
|
if this.align_offset(ptr, align, ret, unwind)? {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
2019-06-30 16:24:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to see if we can do something about foreign items.
|
|
|
|
if this.tcx.is_foreign_item(instance.def_id()) {
|
2019-04-14 20:02:55 -05:00
|
|
|
// 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`.
|
2021-01-21 20:45:39 -06:00
|
|
|
return this.emulate_foreign_item(instance.def_id(), abi, args, ret, unwind);
|
2019-06-30 16:24:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, load the MIR.
|
2021-11-29 11:48:04 -06:00
|
|
|
Ok(Some((&*this.load_mir(instance.def, None)?, instance)))
|
2019-06-30 16:24:08 -05:00
|
|
|
}
|
2019-09-16 13:46:37 -05:00
|
|
|
|
2020-08-16 11:16:34 -05:00
|
|
|
/// Returns `true` if the computation was performed, and `false` if we should just evaluate
|
|
|
|
/// the actual MIR of `align_offset`.
|
2019-09-16 13:46:37 -05:00
|
|
|
fn align_offset(
|
|
|
|
&mut self,
|
2021-02-19 18:00:00 -06:00
|
|
|
ptr_op: &OpTy<'tcx, Tag>,
|
|
|
|
align_op: &OpTy<'tcx, Tag>,
|
|
|
|
ret: Option<(&PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
|
2021-05-22 09:45:00 -05:00
|
|
|
unwind: StackPopUnwind,
|
2020-08-16 11:16:34 -05:00
|
|
|
) -> InterpResult<'tcx, bool> {
|
2019-09-16 13:46:37 -05:00
|
|
|
let this = self.eval_context_mut();
|
2020-03-08 14:00:40 -05:00
|
|
|
let (dest, ret) = ret.unwrap();
|
2019-09-16 13:46:37 -05:00
|
|
|
|
2020-08-16 11:16:34 -05:00
|
|
|
if this.memory.extra.check_alignment != AlignmentCheck::Symbolic {
|
|
|
|
// Just use actual implementation.
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
|
2021-07-15 13:33:08 -05:00
|
|
|
let req_align = this.read_scalar(align_op)?.to_machine_usize(this)?;
|
2019-09-16 13:46:37 -05:00
|
|
|
|
2020-03-08 14:00:40 -05:00
|
|
|
// Stop if the alignment is not a power of two.
|
2019-09-17 13:42:04 -05:00
|
|
|
if !req_align.is_power_of_two() {
|
2020-08-16 11:16:34 -05:00
|
|
|
this.start_panic("align_offset: align is not a power-of-two", unwind)?;
|
|
|
|
return Ok(true); // nothing left to do
|
2019-09-17 13:42:04 -05:00
|
|
|
}
|
|
|
|
|
2021-07-15 13:33:08 -05:00
|
|
|
let ptr = this.read_pointer(ptr_op)?;
|
|
|
|
if let Ok(ptr) = ptr.into_pointer_or_addr() {
|
2020-03-08 14:00:40 -05:00
|
|
|
// Only do anything if we can identify the allocation this goes to.
|
2021-07-15 13:33:08 -05:00
|
|
|
let (_, cur_align) =
|
|
|
|
this.memory.get_size_and_align(ptr.provenance.alloc_id, AllocCheck::MaybeDead)?;
|
|
|
|
if cur_align.bytes() >= req_align {
|
2020-03-08 14:00:40 -05:00
|
|
|
// If the allocation alignment is at least the required alignment we use the
|
2020-08-16 11:16:34 -05:00
|
|
|
// real implementation.
|
|
|
|
return Ok(false);
|
2019-09-16 13:46:37 -05:00
|
|
|
}
|
|
|
|
}
|
2020-03-08 14:00:40 -05:00
|
|
|
|
2020-08-16 11:16:34 -05:00
|
|
|
// Return error result (usize::MAX), and jump to caller.
|
|
|
|
this.write_scalar(Scalar::from_machine_usize(this.machine_usize_max(), this), dest)?;
|
2020-03-08 14:00:40 -05:00
|
|
|
this.go_to_block(ret);
|
2020-08-16 11:16:34 -05:00
|
|
|
Ok(true)
|
2019-09-16 13:46:37 -05:00
|
|
|
}
|
2019-06-30 16:24:08 -05:00
|
|
|
}
|