1 use std::convert::{TryFrom, TryInto};
3 use std::num::NonZeroUsize;
4 use std::time::Duration;
8 use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
10 use rustc_middle::ty::{self, layout::TyAndLayout, List, TyCtxt};
11 use rustc_target::abi::{FieldsShape, LayoutOf, Size, Variants};
12 use rustc_target::spec::abi::Abi;
18 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
20 /// Gets an instance for a path.
21 fn try_resolve_did<'mir, 'tcx>(tcx: TyCtxt<'tcx>, path: &[&str]) -> Option<DefId> {
22 tcx.crates().iter().find(|&&krate| tcx.original_crate_name(krate).as_str() == path[0]).and_then(
24 let krate = DefId { krate: *krate, index: CRATE_DEF_INDEX };
25 let mut items = tcx.item_children(krate);
26 let mut path_it = path.iter().skip(1).peekable();
28 while let Some(segment) = path_it.next() {
29 for item in mem::replace(&mut items, Default::default()).iter() {
30 if item.ident.name.as_str() == *segment {
31 if path_it.peek().is_none() {
32 return Some(item.res.def_id());
35 items = tcx.item_children(item.res.def_id());
45 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
46 /// Gets an instance for a path.
47 fn resolve_path(&self, path: &[&str]) -> ty::Instance<'tcx> {
48 let did = try_resolve_did(self.eval_context_ref().tcx.tcx, path)
49 .unwrap_or_else(|| panic!("failed to find required Rust item: {:?}", path));
50 ty::Instance::mono(self.eval_context_ref().tcx.tcx, did)
53 /// Evaluates the scalar at the specified path. Returns Some(val)
54 /// if the path could be resolved, and None otherwise
55 fn eval_path_scalar(&mut self, path: &[&str]) -> InterpResult<'tcx, ScalarMaybeUninit<Tag>> {
56 let this = self.eval_context_mut();
57 let instance = this.resolve_path(path);
58 let cid = GlobalId { instance, promoted: None };
59 let const_val = this.eval_to_allocation(cid)?;
60 let const_val = this.read_scalar(&const_val.into())?;
64 /// Helper function to get a `libc` constant as a `Scalar`.
65 fn eval_libc(&mut self, name: &str) -> InterpResult<'tcx, Scalar<Tag>> {
66 self.eval_context_mut().eval_path_scalar(&["libc", name])?.check_init()
69 /// Helper function to get a `libc` constant as an `i32`.
70 fn eval_libc_i32(&mut self, name: &str) -> InterpResult<'tcx, i32> {
71 // TODO: Cache the result.
72 self.eval_libc(name)?.to_i32()
75 /// Helper function to get a `windows` constant as a `Scalar`.
76 fn eval_windows(&mut self, module: &str, name: &str) -> InterpResult<'tcx, Scalar<Tag>> {
77 self.eval_context_mut()
78 .eval_path_scalar(&["std", "sys", "windows", module, name])?
82 /// Helper function to get a `windows` constant as an `u64`.
83 fn eval_windows_u64(&mut self, module: &str, name: &str) -> InterpResult<'tcx, u64> {
84 // TODO: Cache the result.
85 self.eval_windows(module, name)?.to_u64()
88 /// Helper function to get the `TyAndLayout` of a `libc` type
89 fn libc_ty_layout(&mut self, name: &str) -> InterpResult<'tcx, TyAndLayout<'tcx>> {
90 let this = self.eval_context_mut();
91 let ty = this.resolve_path(&["libc", name]).ty(*this.tcx, ty::ParamEnv::reveal_all());
95 /// Helper function to get the `TyAndLayout` of a `windows` type
96 fn windows_ty_layout(&mut self, name: &str) -> InterpResult<'tcx, TyAndLayout<'tcx>> {
97 let this = self.eval_context_mut();
99 .resolve_path(&["std", "sys", "windows", "c", name])
100 .ty(*this.tcx, ty::ParamEnv::reveal_all());
104 /// Write a 0 of the appropriate size to `dest`.
105 fn write_null(&mut self, dest: &PlaceTy<'tcx, Tag>) -> InterpResult<'tcx> {
106 self.eval_context_mut().write_scalar(Scalar::from_int(0, dest.layout.size), dest)
109 /// Test if this immediate equals 0.
110 fn is_null(&self, val: Scalar<Tag>) -> InterpResult<'tcx, bool> {
111 let this = self.eval_context_ref();
112 let null = Scalar::null_ptr(this);
113 this.ptr_eq(val, null)
116 /// Turn a Scalar into an Option<NonNullScalar>
117 fn test_null(&self, val: Scalar<Tag>) -> InterpResult<'tcx, Option<Scalar<Tag>>> {
118 let this = self.eval_context_ref();
119 Ok(if this.is_null(val)? { None } else { Some(val) })
122 /// Get the `Place` for a local
123 fn local_place(&mut self, local: mir::Local) -> InterpResult<'tcx, PlaceTy<'tcx, Tag>> {
124 let this = self.eval_context_mut();
125 let place = mir::Place { local: local, projection: List::empty() };
126 this.eval_place(place)
129 /// Generate some random bytes, and write them to `dest`.
130 fn gen_random(&mut self, ptr: Scalar<Tag>, len: u64) -> InterpResult<'tcx> {
131 // Some programs pass in a null pointer and a length of 0
132 // to their platform's random-generation function (e.g. getrandom())
133 // on Linux. For compatibility with these programs, we don't perform
134 // any additional checks - it's okay if the pointer is invalid,
135 // since we wouldn't actually be writing to it.
139 let this = self.eval_context_mut();
141 let mut data = vec![0; usize::try_from(len).unwrap()];
143 if this.machine.communicate {
144 // Fill the buffer using the host's rng.
145 getrandom::getrandom(&mut data)
146 .map_err(|err| err_unsup_format!("host getrandom failed: {}", err))?;
148 let rng = this.memory.extra.rng.get_mut();
149 rng.fill_bytes(&mut data);
152 this.memory.write_bytes(ptr, data.iter().copied())
155 /// Call a function: Push the stack frame and pass the arguments.
156 /// For now, arguments must be scalars (so that the caller does not have to know the layout).
159 f: ty::Instance<'tcx>,
161 args: &[Immediate<Tag>],
162 dest: Option<&PlaceTy<'tcx, Tag>>,
163 stack_pop: StackPopCleanup,
164 ) -> InterpResult<'tcx> {
165 let this = self.eval_context_mut();
166 let param_env = ty::ParamEnv::reveal_all(); // in Miri this is always the param_env we use... and this.param_env is private.
167 let callee_abi = f.ty(*this.tcx, param_env).fn_sig(*this.tcx).abi();
168 if callee_abi != caller_abi {
170 "calling a function with ABI {} using caller ABI {}",
177 let mir = &*this.load_mir(f.def, None)?;
178 this.push_stack_frame(f, mir, dest, stack_pop)?;
180 // Initialize arguments.
181 let mut callee_args = this.frame().body.args_iter();
183 let callee_arg = this.local_place(
186 .ok_or_else(|| err_ub_format!("callee has fewer arguments than expected"))?,
188 this.write_immediate(*arg, &callee_arg)?;
190 if callee_args.next().is_some() {
191 throw_ub_format!("callee has more arguments than expected");
197 /// Visits the memory covered by `place`, sensitive to freezing: the 3rd parameter
198 /// will be true if this is frozen, false if this is in an `UnsafeCell`.
199 fn visit_freeze_sensitive(
201 place: &MPlaceTy<'tcx, Tag>,
203 mut action: impl FnMut(Pointer<Tag>, Size, bool) -> InterpResult<'tcx>,
204 ) -> InterpResult<'tcx> {
205 let this = self.eval_context_ref();
206 trace!("visit_frozen(place={:?}, size={:?})", *place, size);
209 this.size_and_align_of_mplace(place)?
210 .map(|(size, _)| size)
211 .unwrap_or_else(|| place.layout.size)
213 // Store how far we proceeded into the place so far. Everything to the left of
214 // this offset has already been handled, in the sense that the frozen parts
215 // have had `action` called on them.
216 let mut end_ptr = place.ptr.assert_ptr();
217 // Called when we detected an `UnsafeCell` at the given offset and size.
218 // Calls `action` and advances `end_ptr`.
219 let mut unsafe_cell_action = |unsafe_cell_ptr: Scalar<Tag>, unsafe_cell_size: Size| {
220 let unsafe_cell_ptr = unsafe_cell_ptr.assert_ptr();
221 debug_assert_eq!(unsafe_cell_ptr.alloc_id, end_ptr.alloc_id);
222 debug_assert_eq!(unsafe_cell_ptr.tag, end_ptr.tag);
223 // We assume that we are given the fields in increasing offset order,
224 // and nothing else changes.
225 let unsafe_cell_offset = unsafe_cell_ptr.offset;
226 let end_offset = end_ptr.offset;
227 assert!(unsafe_cell_offset >= end_offset);
228 let frozen_size = unsafe_cell_offset - end_offset;
229 // Everything between the end_ptr and this `UnsafeCell` is frozen.
230 if frozen_size != Size::ZERO {
231 action(end_ptr, frozen_size, /*frozen*/ true)?;
233 // This `UnsafeCell` is NOT frozen.
234 if unsafe_cell_size != Size::ZERO {
235 action(unsafe_cell_ptr, unsafe_cell_size, /*frozen*/ false)?;
237 // Update end end_ptr.
238 end_ptr = unsafe_cell_ptr.wrapping_offset(unsafe_cell_size, this);
244 let mut visitor = UnsafeCellVisitor {
246 unsafe_cell_action: |place| {
247 trace!("unsafe_cell_action on {:?}", place.ptr);
248 // We need a size to go on.
249 let unsafe_cell_size = this
250 .size_and_align_of_mplace(&place)?
251 .map(|(size, _)| size)
252 // for extern types, just cover what we can
253 .unwrap_or_else(|| place.layout.size);
254 // Now handle this `UnsafeCell`, unless it is empty.
255 if unsafe_cell_size != Size::ZERO {
256 unsafe_cell_action(place.ptr, unsafe_cell_size)
262 visitor.visit_value(place)?;
264 // The part between the end_ptr and the end of the place is also frozen.
265 // So pretend there is a 0-sized `UnsafeCell` at the end.
266 unsafe_cell_action(place.ptr.ptr_wrapping_offset(size, this), Size::ZERO)?;
270 /// Visiting the memory covered by a `MemPlace`, being aware of
271 /// whether we are inside an `UnsafeCell` or not.
272 struct UnsafeCellVisitor<'ecx, 'mir, 'tcx, F>
274 F: FnMut(&MPlaceTy<'tcx, Tag>) -> InterpResult<'tcx>,
276 ecx: &'ecx MiriEvalContext<'mir, 'tcx>,
277 unsafe_cell_action: F,
280 impl<'ecx, 'mir, 'tcx: 'mir, F> ValueVisitor<'mir, 'tcx, Evaluator<'mir, 'tcx>>
281 for UnsafeCellVisitor<'ecx, 'mir, 'tcx, F>
283 F: FnMut(&MPlaceTy<'tcx, Tag>) -> InterpResult<'tcx>,
285 type V = MPlaceTy<'tcx, Tag>;
288 fn ecx(&self) -> &MiriEvalContext<'mir, 'tcx> {
292 // Hook to detect `UnsafeCell`.
293 fn visit_value(&mut self, v: &MPlaceTy<'tcx, Tag>) -> InterpResult<'tcx> {
294 trace!("UnsafeCellVisitor: {:?} {:?}", *v, v.layout.ty);
295 let is_unsafe_cell = match v.layout.ty.kind() {
297 Some(adt.did) == self.ecx.tcx.lang_items().unsafe_cell_type(),
301 // We do not have to recurse further, this is an `UnsafeCell`.
302 (self.unsafe_cell_action)(v)
303 } else if self.ecx.type_is_freeze(v.layout.ty) {
304 // This is `Freeze`, there cannot be an `UnsafeCell`
306 } else if matches!(v.layout.fields, FieldsShape::Union(..)) {
307 // A (non-frozen) union. We fall back to whatever the type says.
308 (self.unsafe_cell_action)(v)
310 // We want to not actually read from memory for this visit. So, before
311 // walking this value, we have to make sure it is not a
312 // `Variants::Multiple`.
313 match v.layout.variants {
314 Variants::Multiple { .. } => {
315 // A multi-variant enum, or generator, or so.
316 // Treat this like a union: without reading from memory,
317 // we cannot determine the variant we are in. Reading from
318 // memory would be subject to Stacked Borrows rules, leading
319 // to all sorts of "funny" recursion.
320 // We only end up here if the type is *not* freeze, so we just call the
321 // `UnsafeCell` action.
322 (self.unsafe_cell_action)(v)
324 Variants::Single { .. } => {
325 // Proceed further, try to find where exactly that `UnsafeCell`
333 // Make sure we visit aggregrates in increasing offset order.
336 place: &MPlaceTy<'tcx, Tag>,
337 fields: impl Iterator<Item = InterpResult<'tcx, MPlaceTy<'tcx, Tag>>>,
338 ) -> InterpResult<'tcx> {
339 match place.layout.fields {
340 FieldsShape::Array { .. } => {
341 // For the array layout, we know the iterator will yield sorted elements so
342 // we can avoid the allocation.
343 self.walk_aggregate(place, fields)
345 FieldsShape::Arbitrary { .. } => {
346 // Gather the subplaces and sort them before visiting.
348 fields.collect::<InterpResult<'tcx, Vec<MPlaceTy<'tcx, Tag>>>>()?;
349 places.sort_by_key(|place| place.ptr.assert_ptr().offset);
350 self.walk_aggregate(place, places.into_iter().map(Ok))
352 FieldsShape::Union { .. } | FieldsShape::Primitive => {
354 bug!("unions/primitives are not aggregates we should ever visit")
361 _v: &MPlaceTy<'tcx, Tag>,
362 _fields: NonZeroUsize,
363 ) -> InterpResult<'tcx> {
364 bug!("we should have already handled unions in `visit_value`")
369 // Writes several `ImmTy`s contiguously into memory. This is useful when you have to pack
370 // different values into a struct.
371 fn write_packed_immediates(
373 place: &MPlaceTy<'tcx, Tag>,
374 imms: &[ImmTy<'tcx, Tag>],
375 ) -> InterpResult<'tcx> {
376 let this = self.eval_context_mut();
378 let mut offset = Size::from_bytes(0);
381 this.write_immediate_to_mplace(
383 &place.offset(offset, MemPlaceMeta::None, imm.layout, &*this.tcx)?,
385 offset += imm.layout.size;
390 /// Helper function used inside the shims of foreign functions to check that isolation is
391 /// disabled. It returns an error using the `name` of the foreign function if this is not the
393 fn check_no_isolation(&self, name: &str) -> InterpResult<'tcx> {
394 if !self.eval_context_ref().machine.communicate {
395 isolation_error(name)?;
400 /// Helper function used inside the shims of foreign functions to assert that the target OS
401 /// is `target_os`. It panics showing a message with the `name` of the foreign function
402 /// if this is not the case.
403 fn assert_target_os(&self, target_os: &str, name: &str) {
405 self.eval_context_ref().tcx.sess.target.os,
407 "`{}` is only available on the `{}` target OS",
413 /// Get last error variable as a place, lazily allocating thread-local storage for it if
415 fn last_error_place(&mut self) -> InterpResult<'tcx, MPlaceTy<'tcx, Tag>> {
416 let this = self.eval_context_mut();
417 if let Some(errno_place) = this.active_thread_ref().last_error {
420 // Allocate new place, set initial value to 0.
421 let errno_layout = this.machine.layouts.u32;
422 let errno_place = this.allocate(errno_layout, MiriMemoryKind::Machine.into());
423 this.write_scalar(Scalar::from_u32(0), &errno_place.into())?;
424 this.active_thread_mut().last_error = Some(errno_place);
429 /// Sets the last error variable.
430 fn set_last_error(&mut self, scalar: Scalar<Tag>) -> InterpResult<'tcx> {
431 let this = self.eval_context_mut();
432 let errno_place = this.last_error_place()?;
433 this.write_scalar(scalar, &errno_place.into())
436 /// Gets the last error variable.
437 fn get_last_error(&mut self) -> InterpResult<'tcx, Scalar<Tag>> {
438 let this = self.eval_context_mut();
439 let errno_place = this.last_error_place()?;
440 this.read_scalar(&errno_place.into())?.check_init()
443 /// Sets the last OS error using a `std::io::Error`. This function tries to produce the most
444 /// similar OS error from the `std::io::ErrorKind` and sets it as the last OS error.
445 fn set_last_error_from_io_error(&mut self, e: std::io::Error) -> InterpResult<'tcx> {
446 use std::io::ErrorKind::*;
447 let this = self.eval_context_mut();
448 let target = &this.tcx.sess.target;
449 let target_os = &target.os;
450 let last_error = if target.families.contains(&"unix".to_owned()) {
451 this.eval_libc(match e.kind() {
452 ConnectionRefused => "ECONNREFUSED",
453 ConnectionReset => "ECONNRESET",
454 PermissionDenied => "EPERM",
455 BrokenPipe => "EPIPE",
456 NotConnected => "ENOTCONN",
457 ConnectionAborted => "ECONNABORTED",
458 AddrNotAvailable => "EADDRNOTAVAIL",
459 AddrInUse => "EADDRINUSE",
460 NotFound => "ENOENT",
461 Interrupted => "EINTR",
462 InvalidInput => "EINVAL",
463 TimedOut => "ETIMEDOUT",
464 AlreadyExists => "EEXIST",
465 WouldBlock => "EWOULDBLOCK",
467 throw_unsup_format!("io error {} cannot be transformed into a raw os error", e)
470 } else if target.families.contains(&"windows".to_owned()) {
471 // FIXME: we have to finish implementing the Windows equivalent of this.
475 NotFound => "ERROR_FILE_NOT_FOUND",
476 _ => throw_unsup_format!(
477 "io error {} cannot be transformed into a raw os error",
484 "setting the last OS error from an io::Error is unsupported for {}.",
488 this.set_last_error(last_error)
491 /// Helper function that consumes an `std::io::Result<T>` and returns an
492 /// `InterpResult<'tcx,T>::Ok` instead. In case the result is an error, this function returns
493 /// `Ok(-1)` and sets the last OS error accordingly.
495 /// This function uses `T: From<i32>` instead of `i32` directly because some IO related
496 /// functions return different integer types (like `read`, that returns an `i64`).
497 fn try_unwrap_io_result<T: From<i32>>(
499 result: std::io::Result<T>,
500 ) -> InterpResult<'tcx, T> {
504 self.eval_context_mut().set_last_error_from_io_error(e)?;
510 fn read_scalar_at_offset(
512 op: &OpTy<'tcx, Tag>,
514 layout: TyAndLayout<'tcx>,
515 ) -> InterpResult<'tcx, ScalarMaybeUninit<Tag>> {
516 let this = self.eval_context_ref();
517 let op_place = this.deref_operand(op)?;
518 let offset = Size::from_bytes(offset);
519 // Ensure that the following read at an offset is within bounds
520 assert!(op_place.layout.size >= offset + layout.size);
521 let value_place = op_place.offset(offset, MemPlaceMeta::None, layout, this)?;
522 this.read_scalar(&value_place.into())
525 fn write_scalar_at_offset(
527 op: &OpTy<'tcx, Tag>,
529 value: impl Into<ScalarMaybeUninit<Tag>>,
530 layout: TyAndLayout<'tcx>,
531 ) -> InterpResult<'tcx, ()> {
532 let this = self.eval_context_mut();
533 let op_place = this.deref_operand(op)?;
534 let offset = Size::from_bytes(offset);
535 // Ensure that the following read at an offset is within bounds
536 assert!(op_place.layout.size >= offset + layout.size);
537 let value_place = op_place.offset(offset, MemPlaceMeta::None, layout, this)?;
538 this.write_scalar(value, &value_place.into())
541 /// Parse a `timespec` struct and return it as a `std::time::Duration`. It returns `None`
542 /// if the value in the `timespec` struct is invalid. Some libc functions will return
543 /// `EINVAL` in this case.
546 timespec_ptr_op: &OpTy<'tcx, Tag>,
547 ) -> InterpResult<'tcx, Option<Duration>> {
548 let this = self.eval_context_mut();
549 let tp = this.deref_operand(timespec_ptr_op)?;
550 let seconds_place = this.mplace_field(&tp, 0)?;
551 let seconds_scalar = this.read_scalar(&seconds_place.into())?;
552 let seconds = seconds_scalar.to_machine_isize(this)?;
553 let nanoseconds_place = this.mplace_field(&tp, 1)?;
554 let nanoseconds_scalar = this.read_scalar(&nanoseconds_place.into())?;
555 let nanoseconds = nanoseconds_scalar.to_machine_isize(this)?;
558 // tv_sec must be non-negative.
559 let seconds: u64 = seconds.try_into().ok()?;
560 // tv_nsec must be non-negative.
561 let nanoseconds: u32 = nanoseconds.try_into().ok()?;
562 if nanoseconds >= 1_000_000_000 {
563 // tv_nsec must not be greater than 999,999,999.
566 Duration::new(seconds, nanoseconds)
570 fn read_c_str<'a>(&'a self, sptr: Scalar<Tag>) -> InterpResult<'tcx, &'a [u8]>
575 let this = self.eval_context_ref();
576 let size1 = Size::from_bytes(1);
577 let ptr = this.force_ptr(sptr)?; // We need to read at least 1 byte, so we can eagerly get a ptr.
579 // Step 1: determine the length.
580 let alloc = this.memory.get_raw(ptr.alloc_id)?;
581 let mut len = Size::ZERO;
583 let byte = alloc.read_scalar(this, ptr.offset(len, this)?, size1)?.to_u8()?;
591 // Step 2: get the bytes.
592 this.memory.read_bytes(ptr.into(), len)
595 fn read_wide_str(&self, sptr: Scalar<Tag>) -> InterpResult<'tcx, Vec<u16>> {
596 let this = self.eval_context_ref();
597 let size2 = Size::from_bytes(2);
599 let mut ptr = this.force_ptr(sptr)?; // We need to read at least 1 wchar, so we can eagerly get a ptr.
600 let mut wchars = Vec::new();
601 let alloc = this.memory.get_raw(ptr.alloc_id)?;
603 let wchar = alloc.read_scalar(this, ptr, size2)?.to_u16()?;
608 ptr = ptr.offset(size2, this)?;
616 /// Check that the number of args is what we expect.
617 pub fn check_arg_count<'a, 'tcx, const N: usize>(
618 args: &'a [OpTy<'tcx, Tag>],
619 ) -> InterpResult<'tcx, &'a [OpTy<'tcx, Tag>; N]>
621 &'a [OpTy<'tcx, Tag>; N]: TryFrom<&'a [OpTy<'tcx, Tag>]>,
623 if let Ok(ops) = args.try_into() {
626 throw_ub_format!("incorrect number of arguments: got {}, expected {}", args.len(), N)
629 /// Check that the ABI is what we expect.
630 pub fn check_abi<'a>(abi: Abi, exp_abi: Abi) -> InterpResult<'a, ()> {
635 "calling a function with ABI {} using caller ABI {}",
642 pub fn isolation_error(name: &str) -> InterpResult<'static> {
643 throw_machine_stop!(TerminationInfo::UnsupportedInIsolation(format!(
644 "{} not available when isolation is enabled",
649 pub fn immty_from_int_checked<'tcx>(
650 int: impl Into<i128>,
651 layout: TyAndLayout<'tcx>,
652 ) -> InterpResult<'tcx, ImmTy<'tcx, Tag>> {
653 let int = int.into();
654 Ok(ImmTy::try_from_int(int, layout).ok_or_else(|| {
655 err_unsup_format!("signed value {:#x} does not fit in {} bits", int, layout.size.bits())
659 pub fn immty_from_uint_checked<'tcx>(
660 int: impl Into<u128>,
661 layout: TyAndLayout<'tcx>,
662 ) -> InterpResult<'tcx, ImmTy<'tcx, Tag>> {
663 let int = int.into();
664 Ok(ImmTy::try_from_uint(int, layout).ok_or_else(|| {
665 err_unsup_format!("unsigned value {:#x} does not fit in {} bits", int, layout.size.bits())