rust/src/shims/mod.rs

107 lines
3.9 KiB
Rust
Raw Normal View History

2019-09-22 21:39:17 -05:00
pub mod dlsym;
pub mod env;
pub mod foreign_items;
2019-10-11 01:49:28 -05:00
pub mod fs;
2019-12-23 05:56:23 -06:00
pub mod intrinsics;
2020-03-28 09:43:47 -05:00
pub mod os_str;
pub mod panic;
2020-02-17 21:30:24 -06:00
pub mod sync;
pub mod thread;
2019-12-23 05:56:23 -06:00
pub mod time;
pub mod tls;
use std::convert::TryFrom;
use log::trace;
use rustc_middle::{mir, ty};
use crate::*;
use helpers::check_arg_count;
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
2019-12-04 16:31:39 -06:00
fn find_mir_or_eval_fn(
&mut self,
instance: ty::Instance<'tcx>,
args: &[OpTy<'tcx, Tag>],
2019-11-25 15:48:31 -06:00
ret: Option<(PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
2019-12-23 05:56:23 -06:00
unwind: Option<mir::BasicBlock>,
) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
let this = self.eval_context_mut();
2019-12-23 05:56:23 -06:00
trace!("eval_fn_call: {:#?}, {:?}", instance, ret.map(|p| *p.0));
// 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 &[ptr, align] = check_arg_count(args)?;
this.align_offset(ptr, align, 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 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`.
2019-11-25 15:48:31 -06:00
return this.emulate_foreign_item(instance.def_id(), args, ret, unwind);
}
2020-03-21 04:16:47 -05:00
// Better error message for panics on Windows.
let def_id = instance.def_id();
if Some(def_id) == this.tcx.lang_items().begin_panic_fn() ||
Some(def_id) == this.tcx.lang_items().panic_impl()
{
this.check_panic_supported()?;
}
// Otherwise, load the MIR.
2019-12-08 03:32:50 -06: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>,
ret: Option<(PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
unwind: Option<mir::BasicBlock>,
) -> InterpResult<'tcx> {
2019-09-16 13:46:37 -05:00
let this = self.eval_context_mut();
let (dest, ret) = ret.unwrap();
2019-09-16 13:46:37 -05:00
2019-12-23 05:56:23 -06:00
let req_align = this
.force_bits(this.read_scalar(align_op)?.not_undef()?, this.pointer_size())?;
2019-09-16 13:46:37 -05:00
// 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);
}
2019-09-16 13:46:37 -05:00
let ptr_scalar = this.read_scalar(ptr_op)?.not_undef()?;
// Default: no result.
2020-03-29 03:01:31 -05:00
let mut result = this.machine_usize_max();
2019-09-22 21:39:17 -05:00
if let Ok(ptr) = this.force_ptr(ptr_scalar) {
// Only do anything if we can identify the allocation this goes to.
2019-12-23 05:56:23 -06:00
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 = u64::try_from(
(this.force_bits(ptr_scalar, this.pointer_size())? as *const i8)
.align_offset(usize::try_from(req_align).unwrap())
).unwrap();
2019-09-16 13:46:37 -05:00
}
}
// Return result, and jump to caller.
this.write_scalar(Scalar::from_machine_usize(result, this), dest)?;
this.go_to_block(ret);
Ok(())
2019-09-16 13:46:37 -05:00
}
}