rust/src/shims/mod.rs

93 lines
3.3 KiB
Rust
Raw Normal View History

2019-09-22 21:39:17 -05:00
pub mod dlsym;
pub mod env;
pub mod foreign_items;
pub mod intrinsics;
2019-07-03 16:12:44 -05:00
pub mod tls;
2019-10-11 01:49:28 -05:00
pub mod fs;
pub mod time;
2019-09-22 21:39:17 -05:00
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(
&mut self,
instance: ty::Instance<'tcx>,
args: &[OpTy<'tcx, Tag>],
dest: Option<PlaceTy<'tcx, Tag>>,
ret: Option<mir::BasicBlock>,
) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
let this = self.eval_context_mut();
2019-09-22 21:39:17 -05:00
trace!(
"eval_fn_call: {:#?}, {:?}",
instance,
dest.map(|place| *place)
);
// First, run the common hooks also supported by CTFE.
if this.hook_panic_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();
2019-09-22 21:39:17 -05:00
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);
}
// 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);
}
// Otherwise, load the MIR.
2019-08-27 01:32:31 -05:00
Ok(Some(this.load_mir(instance.def, None)?))
}
2019-09-16 13:46:37 -05:00
fn align_offset(
&mut self,
ptr_op: OpTy<'tcx, Tag>,
2019-09-17 13:26:12 -05:00
align_op: OpTy<'tcx, Tag>,
2019-09-22 21:39:17 -05:00
) -> InterpResult<'tcx, Option<u128>> {
2019-09-16 13:46:37 -05:00
let this = self.eval_context_mut();
let req_align = this.force_bits(
this.read_scalar(align_op)?.not_undef()?,
2019-09-22 21:39:17 -05:00
this.pointer_size(),
2019-09-16 13:46:37 -05:00
)? as usize;
// FIXME: This should actually panic in the interpreted program
if !req_align.is_power_of_two() {
throw_unsup_format!("Required alignment should always be a power of two")
}
2019-09-16 13:46:37 -05:00
let ptr_scalar = this.read_scalar(ptr_op)?.not_undef()?;
2019-09-22 21:39:17 -05:00
if let Ok(ptr) = this.force_ptr(ptr_scalar) {
2019-11-08 15:07:52 -06:00
let cur_align = this.memory.get_size_and_align(ptr.alloc_id, AllocCheck::MaybeDead)?.1.bytes() as usize;
2019-09-22 21:39:17 -05:00
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,
));
2019-09-16 13:46:37 -05:00
}
}
2019-09-22 21:39:17 -05:00
// If the allocation alignment is smaller than then required alignment or the pointer was
// actually an integer, we return `None`
Ok(None)
2019-09-16 13:46:37 -05:00
}
}