]> git.lizzy.rs Git - rust.git/blob - src/helpers.rs
Auto merge of #1239 - elichai:2020-03-abort-intrinsics, r=RalfJung
[rust.git] / src / helpers.rs
1 use std::ffi::OsStr;
2 use std::{iter, mem};
3 use std::convert::TryFrom;
4
5 use rustc::mir;
6 use rustc::ty::{
7     self,
8     layout::{self, LayoutOf, Size, TyLayout},
9     List, TyCtxt,
10 };
11 use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
12 use rustc_span::source_map::DUMMY_SP;
13
14 use rand::RngCore;
15
16 use crate::*;
17
18 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
19
20 /// Gets an instance for a path.
21 fn resolve_did<'mir, 'tcx>(tcx: TyCtxt<'tcx>, path: &[&str]) -> InterpResult<'tcx, DefId> {
22     tcx.crates()
23         .iter()
24         .find(|&&krate| tcx.original_crate_name(krate).as_str() == path[0])
25         .and_then(|krate| {
26             let krate = DefId { krate: *krate, index: CRATE_DEF_INDEX };
27             let mut items = tcx.item_children(krate);
28             let mut path_it = path.iter().skip(1).peekable();
29
30             while let Some(segment) = path_it.next() {
31                 for item in mem::replace(&mut items, Default::default()).iter() {
32                     if item.ident.name.as_str() == *segment {
33                         if path_it.peek().is_none() {
34                             return Some(item.res.def_id());
35                         }
36
37                         items = tcx.item_children(item.res.def_id());
38                         break;
39                     }
40                 }
41             }
42             None
43         })
44         .ok_or_else(|| {
45             let path = path.iter().map(|&s| s.to_owned()).collect();
46             err_unsup!(PathNotFound(path)).into()
47         })
48 }
49
50 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
51     /// Gets an instance for a path.
52     fn resolve_path(&self, path: &[&str]) -> InterpResult<'tcx, ty::Instance<'tcx>> {
53         Ok(ty::Instance::mono(
54             self.eval_context_ref().tcx.tcx,
55             resolve_did(self.eval_context_ref().tcx.tcx, path)?,
56         ))
57     }
58
59     /// Write a 0 of the appropriate size to `dest`.
60     fn write_null(&mut self, dest: PlaceTy<'tcx, Tag>) -> InterpResult<'tcx> {
61         self.eval_context_mut().write_scalar(Scalar::from_int(0, dest.layout.size), dest)
62     }
63
64     /// Test if this immediate equals 0.
65     fn is_null(&self, val: Scalar<Tag>) -> InterpResult<'tcx, bool> {
66         let this = self.eval_context_ref();
67         let null = Scalar::from_int(0, this.memory.pointer_size());
68         this.ptr_eq(val, null)
69     }
70
71     /// Turn a Scalar into an Option<NonNullScalar>
72     fn test_null(&self, val: Scalar<Tag>) -> InterpResult<'tcx, Option<Scalar<Tag>>> {
73         let this = self.eval_context_ref();
74         Ok(if this.is_null(val)? { None } else { Some(val) })
75     }
76
77     /// Get the `Place` for a local
78     fn local_place(&mut self, local: mir::Local) -> InterpResult<'tcx, PlaceTy<'tcx, Tag>> {
79         let this = self.eval_context_mut();
80         let place = mir::Place { local: local, projection: List::empty() };
81         this.eval_place(&place)
82     }
83
84     /// Generate some random bytes, and write them to `dest`.
85     fn gen_random(&mut self, ptr: Scalar<Tag>, len: u64) -> InterpResult<'tcx> {
86         // Some programs pass in a null pointer and a length of 0
87         // to their platform's random-generation function (e.g. getrandom())
88         // on Linux. For compatibility with these programs, we don't perform
89         // any additional checks - it's okay if the pointer is invalid,
90         // since we wouldn't actually be writing to it.
91         if len == 0 {
92             return Ok(());
93         }
94         let this = self.eval_context_mut();
95
96         let mut data = vec![0; usize::try_from(len).unwrap()];
97
98         if this.machine.communicate {
99             // Fill the buffer using the host's rng.
100             getrandom::getrandom(&mut data)
101                 .map_err(|err| err_unsup_format!("getrandom failed: {}", err))?;
102         } else {
103             let rng = this.memory.extra.rng.get_mut();
104             rng.fill_bytes(&mut data);
105         }
106
107         this.memory.write_bytes(ptr, data.iter().copied())
108     }
109
110     /// Call a function: Push the stack frame and pass the arguments.
111     /// For now, arguments must be scalars (so that the caller does not have to know the layout).
112     fn call_function(
113         &mut self,
114         f: ty::Instance<'tcx>,
115         args: &[Immediate<Tag>],
116         dest: Option<PlaceTy<'tcx, Tag>>,
117         stack_pop: StackPopCleanup,
118     ) -> InterpResult<'tcx> {
119         let this = self.eval_context_mut();
120
121         // Push frame.
122         let mir = &*this.load_mir(f.def, None)?;
123         let span = this
124             .stack()
125             .last()
126             .and_then(Frame::current_source_info)
127             .map(|si| si.span)
128             .unwrap_or(DUMMY_SP);
129         this.push_stack_frame(f, span, mir, dest, stack_pop)?;
130
131         // Initialize arguments.
132         let mut callee_args = this.frame().body.args_iter();
133         for arg in args {
134             let callee_arg = this.local_place(
135                 callee_args.next().expect("callee has fewer arguments than expected"),
136             )?;
137             this.write_immediate(*arg, callee_arg)?;
138         }
139         callee_args.next().expect_none("callee has more arguments than expected");
140
141         Ok(())
142     }
143
144     /// Visits the memory covered by `place`, sensitive to freezing: the 3rd parameter
145     /// will be true if this is frozen, false if this is in an `UnsafeCell`.
146     fn visit_freeze_sensitive(
147         &self,
148         place: MPlaceTy<'tcx, Tag>,
149         size: Size,
150         mut action: impl FnMut(Pointer<Tag>, Size, bool) -> InterpResult<'tcx>,
151     ) -> InterpResult<'tcx> {
152         let this = self.eval_context_ref();
153         trace!("visit_frozen(place={:?}, size={:?})", *place, size);
154         debug_assert_eq!(
155             size,
156             this.size_and_align_of_mplace(place)?
157                 .map(|(size, _)| size)
158                 .unwrap_or_else(|| place.layout.size)
159         );
160         // Store how far we proceeded into the place so far. Everything to the left of
161         // this offset has already been handled, in the sense that the frozen parts
162         // have had `action` called on them.
163         let mut end_ptr = place.ptr.assert_ptr();
164         // Called when we detected an `UnsafeCell` at the given offset and size.
165         // Calls `action` and advances `end_ptr`.
166         let mut unsafe_cell_action = |unsafe_cell_ptr: Scalar<Tag>, unsafe_cell_size: Size| {
167             let unsafe_cell_ptr = unsafe_cell_ptr.assert_ptr();
168             debug_assert_eq!(unsafe_cell_ptr.alloc_id, end_ptr.alloc_id);
169             debug_assert_eq!(unsafe_cell_ptr.tag, end_ptr.tag);
170             // We assume that we are given the fields in increasing offset order,
171             // and nothing else changes.
172             let unsafe_cell_offset = unsafe_cell_ptr.offset;
173             let end_offset = end_ptr.offset;
174             assert!(unsafe_cell_offset >= end_offset);
175             let frozen_size = unsafe_cell_offset - end_offset;
176             // Everything between the end_ptr and this `UnsafeCell` is frozen.
177             if frozen_size != Size::ZERO {
178                 action(end_ptr, frozen_size, /*frozen*/ true)?;
179             }
180             // This `UnsafeCell` is NOT frozen.
181             if unsafe_cell_size != Size::ZERO {
182                 action(unsafe_cell_ptr, unsafe_cell_size, /*frozen*/ false)?;
183             }
184             // Update end end_ptr.
185             end_ptr = unsafe_cell_ptr.wrapping_offset(unsafe_cell_size, this);
186             // Done
187             Ok(())
188         };
189         // Run a visitor
190         {
191             let mut visitor = UnsafeCellVisitor {
192                 ecx: this,
193                 unsafe_cell_action: |place| {
194                     trace!("unsafe_cell_action on {:?}", place.ptr);
195                     // We need a size to go on.
196                     let unsafe_cell_size = this
197                         .size_and_align_of_mplace(place)?
198                         .map(|(size, _)| size)
199                         // for extern types, just cover what we can
200                         .unwrap_or_else(|| place.layout.size);
201                     // Now handle this `UnsafeCell`, unless it is empty.
202                     if unsafe_cell_size != Size::ZERO {
203                         unsafe_cell_action(place.ptr, unsafe_cell_size)
204                     } else {
205                         Ok(())
206                     }
207                 },
208             };
209             visitor.visit_value(place)?;
210         }
211         // The part between the end_ptr and the end of the place is also frozen.
212         // So pretend there is a 0-sized `UnsafeCell` at the end.
213         unsafe_cell_action(place.ptr.ptr_wrapping_offset(size, this), Size::ZERO)?;
214         // Done!
215         return Ok(());
216
217         /// Visiting the memory covered by a `MemPlace`, being aware of
218         /// whether we are inside an `UnsafeCell` or not.
219         struct UnsafeCellVisitor<'ecx, 'mir, 'tcx, F>
220         where
221             F: FnMut(MPlaceTy<'tcx, Tag>) -> InterpResult<'tcx>,
222         {
223             ecx: &'ecx MiriEvalContext<'mir, 'tcx>,
224             unsafe_cell_action: F,
225         }
226
227         impl<'ecx, 'mir, 'tcx, F> ValueVisitor<'mir, 'tcx, Evaluator<'tcx>>
228             for UnsafeCellVisitor<'ecx, 'mir, 'tcx, F>
229         where
230             F: FnMut(MPlaceTy<'tcx, Tag>) -> InterpResult<'tcx>,
231         {
232             type V = MPlaceTy<'tcx, Tag>;
233
234             #[inline(always)]
235             fn ecx(&self) -> &MiriEvalContext<'mir, 'tcx> {
236                 &self.ecx
237             }
238
239             // Hook to detect `UnsafeCell`.
240             fn visit_value(&mut self, v: MPlaceTy<'tcx, Tag>) -> InterpResult<'tcx> {
241                 trace!("UnsafeCellVisitor: {:?} {:?}", *v, v.layout.ty);
242                 let is_unsafe_cell = match v.layout.ty.kind {
243                     ty::Adt(adt, _) =>
244                         Some(adt.did) == self.ecx.tcx.lang_items().unsafe_cell_type(),
245                     _ => false,
246                 };
247                 if is_unsafe_cell {
248                     // We do not have to recurse further, this is an `UnsafeCell`.
249                     (self.unsafe_cell_action)(v)
250                 } else if self.ecx.type_is_freeze(v.layout.ty) {
251                     // This is `Freeze`, there cannot be an `UnsafeCell`
252                     Ok(())
253                 } else {
254                     // We want to not actually read from memory for this visit. So, before
255                     // walking this value, we have to make sure it is not a
256                     // `Variants::Multiple`.
257                     match v.layout.variants {
258                         layout::Variants::Multiple { .. } => {
259                             // A multi-variant enum, or generator, or so.
260                             // Treat this like a union: without reading from memory,
261                             // we cannot determine the variant we are in. Reading from
262                             // memory would be subject to Stacked Borrows rules, leading
263                             // to all sorts of "funny" recursion.
264                             // We only end up here if the type is *not* freeze, so we just call the
265                             // `UnsafeCell` action.
266                             (self.unsafe_cell_action)(v)
267                         }
268                         layout::Variants::Single { .. } => {
269                             // Proceed further, try to find where exactly that `UnsafeCell`
270                             // is hiding.
271                             self.walk_value(v)
272                         }
273                     }
274                 }
275             }
276
277             // Make sure we visit aggregrates in increasing offset order.
278             fn visit_aggregate(
279                 &mut self,
280                 place: MPlaceTy<'tcx, Tag>,
281                 fields: impl Iterator<Item = InterpResult<'tcx, MPlaceTy<'tcx, Tag>>>,
282             ) -> InterpResult<'tcx> {
283                 match place.layout.fields {
284                     layout::FieldPlacement::Array { .. } => {
285                         // For the array layout, we know the iterator will yield sorted elements so
286                         // we can avoid the allocation.
287                         self.walk_aggregate(place, fields)
288                     }
289                     layout::FieldPlacement::Arbitrary { .. } => {
290                         // Gather the subplaces and sort them before visiting.
291                         let mut places =
292                             fields.collect::<InterpResult<'tcx, Vec<MPlaceTy<'tcx, Tag>>>>()?;
293                         places.sort_by_key(|place| place.ptr.assert_ptr().offset);
294                         self.walk_aggregate(place, places.into_iter().map(Ok))
295                     }
296                     layout::FieldPlacement::Union { .. } => {
297                         // Uh, what?
298                         bug!("a union is not an aggregate we should ever visit")
299                     }
300                 }
301             }
302
303             // We have to do *something* for unions.
304             fn visit_union(&mut self, v: MPlaceTy<'tcx, Tag>, fields: usize) -> InterpResult<'tcx> {
305                 assert!(fields > 0); // we should never reach "pseudo-unions" with 0 fields, like primitives
306
307                 // With unions, we fall back to whatever the type says, to hopefully be consistent
308                 // with LLVM IR.
309                 // FIXME: are we consistent, and is this really the behavior we want?
310                 let frozen = self.ecx.type_is_freeze(v.layout.ty);
311                 if frozen { Ok(()) } else { (self.unsafe_cell_action)(v) }
312             }
313         }
314     }
315
316     /// Helper function to get a `libc` constant as a `Scalar`.
317     fn eval_libc(&mut self, name: &str) -> InterpResult<'tcx, Scalar<Tag>> {
318         self.eval_context_mut()
319             .eval_path_scalar(&["libc", name])?
320             .ok_or_else(|| err_unsup_format!("Path libc::{} cannot be resolved.", name))?
321             .not_undef()
322     }
323
324     /// Helper function to get a `libc` constant as an `i32`.
325     fn eval_libc_i32(&mut self, name: &str) -> InterpResult<'tcx, i32> {
326         self.eval_libc(name)?.to_i32()
327     }
328
329     /// Helper function to get the `TyLayout` of a `libc` type
330     fn libc_ty_layout(&mut self, name: &str) -> InterpResult<'tcx, TyLayout<'tcx>> {
331         let this = self.eval_context_mut();
332         let ty = this.resolve_path(&["libc", name])?.monomorphic_ty(*this.tcx);
333         this.layout_of(ty)
334     }
335
336     // Writes several `ImmTy`s contiguosly into memory. This is useful when you have to pack
337     // different values into a struct.
338     fn write_packed_immediates(
339         &mut self,
340         place: MPlaceTy<'tcx, Tag>,
341         imms: &[ImmTy<'tcx, Tag>],
342     ) -> InterpResult<'tcx> {
343         let this = self.eval_context_mut();
344
345         let mut offset = Size::from_bytes(0);
346
347         for &imm in imms {
348             this.write_immediate_to_mplace(
349                 *imm,
350                 place.offset(offset, MemPlaceMeta::None, imm.layout, &*this.tcx)?,
351             )?;
352             offset += imm.layout.size;
353         }
354         Ok(())
355     }
356
357     /// Helper function used inside the shims of foreign functions to check that isolation is
358     /// disabled. It returns an error using the `name` of the foreign function if this is not the
359     /// case.
360     fn check_no_isolation(&self, name: &str) -> InterpResult<'tcx> {
361         if !self.eval_context_ref().machine.communicate {
362             throw_unsup_format!(
363                 "`{}` not available when isolation is enabled. Pass the flag `-Zmiri-disable-isolation` to disable it.",
364                 name,
365             )
366         }
367         Ok(())
368     }
369     /// Helper function used inside the shims of foreign functions to assert that the target
370     /// platform is `platform`. It panics showing a message with the `name` of the foreign function
371     /// if this is not the case.
372     fn assert_platform(&self, platform: &str, name: &str) {
373         assert_eq!(
374             self.eval_context_ref().tcx.sess.target.target.target_os,
375             platform,
376             "`{}` is only available on the `{}` platform",
377             name,
378             platform,
379         )
380     }
381
382     /// Sets the last error variable.
383     fn set_last_error(&mut self, scalar: Scalar<Tag>) -> InterpResult<'tcx> {
384         let this = self.eval_context_mut();
385         let errno_place = this.machine.last_error.unwrap();
386         this.write_scalar(scalar, errno_place.into())
387     }
388
389     /// Gets the last error variable.
390     fn get_last_error(&self) -> InterpResult<'tcx, Scalar<Tag>> {
391         let this = self.eval_context_ref();
392         let errno_place = this.machine.last_error.unwrap();
393         this.read_scalar(errno_place.into())?.not_undef()
394     }
395
396     /// Sets the last OS error using a `std::io::Error`. This function tries to produce the most
397     /// similar OS error from the `std::io::ErrorKind` and sets it as the last OS error.
398     fn set_last_error_from_io_error(&mut self, e: std::io::Error) -> InterpResult<'tcx> {
399         use std::io::ErrorKind::*;
400         let this = self.eval_context_mut();
401         let target = &this.tcx.tcx.sess.target.target;
402         let last_error = if target.options.target_family == Some("unix".to_owned()) {
403             this.eval_libc(match e.kind() {
404                 ConnectionRefused => "ECONNREFUSED",
405                 ConnectionReset => "ECONNRESET",
406                 PermissionDenied => "EPERM",
407                 BrokenPipe => "EPIPE",
408                 NotConnected => "ENOTCONN",
409                 ConnectionAborted => "ECONNABORTED",
410                 AddrNotAvailable => "EADDRNOTAVAIL",
411                 AddrInUse => "EADDRINUSE",
412                 NotFound => "ENOENT",
413                 Interrupted => "EINTR",
414                 InvalidInput => "EINVAL",
415                 TimedOut => "ETIMEDOUT",
416                 AlreadyExists => "EEXIST",
417                 WouldBlock => "EWOULDBLOCK",
418                 _ => {
419                     throw_unsup_format!("The {} error cannot be transformed into a raw os error", e)
420                 }
421             })?
422         } else {
423             // FIXME: we have to implement the Windows equivalent of this.
424             throw_unsup_format!(
425                 "Setting the last OS error from an io::Error is unsupported for {}.",
426                 target.target_os
427             )
428         };
429         this.set_last_error(last_error)
430     }
431
432     /// Helper function that consumes an `std::io::Result<T>` and returns an
433     /// `InterpResult<'tcx,T>::Ok` instead. In case the result is an error, this function returns
434     /// `Ok(-1)` and sets the last OS error accordingly.
435     ///
436     /// This function uses `T: From<i32>` instead of `i32` directly because some IO related
437     /// functions return different integer types (like `read`, that returns an `i64`).
438     fn try_unwrap_io_result<T: From<i32>>(
439         &mut self,
440         result: std::io::Result<T>,
441     ) -> InterpResult<'tcx, T> {
442         match result {
443             Ok(ok) => Ok(ok),
444             Err(e) => {
445                 self.eval_context_mut().set_last_error_from_io_error(e)?;
446                 Ok((-1).into())
447             }
448         }
449     }
450
451     /// Helper function to read an OsString from a null-terminated sequence of bytes, which is what
452     /// the Unix APIs usually handle.
453     fn read_os_str_from_c_str<'a>(&'a self, scalar: Scalar<Tag>) -> InterpResult<'tcx, &'a OsStr>
454     where
455         'tcx: 'a,
456         'mir: 'a,
457     {
458         #[cfg(target_os = "unix")]
459         fn bytes_to_os_str<'tcx, 'a>(bytes: &'a [u8]) -> InterpResult<'tcx, &'a OsStr> {
460             Ok(std::os::unix::ffi::OsStringExt::from_bytes(bytes))
461         }
462         #[cfg(not(target_os = "unix"))]
463         fn bytes_to_os_str<'tcx, 'a>(bytes: &'a [u8]) -> InterpResult<'tcx, &'a OsStr> {
464             let s = std::str::from_utf8(bytes)
465                 .map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", bytes))?;
466             Ok(&OsStr::new(s))
467         }
468
469         let this = self.eval_context_ref();
470         let bytes = this.memory.read_c_str(scalar)?;
471         bytes_to_os_str(bytes)
472     }
473
474     /// Helper function to write an OsStr as a null-terminated sequence of bytes, which is what
475     /// the Unix APIs usually handle. This function returns `Ok((false, length))` without trying
476     /// to write if `size` is not large enough to fit the contents of `os_string` plus a null
477     /// terminator. It returns `Ok((true, length))` if the writing process was successful. The
478     /// string length returned does not include the null terminator.
479     fn write_os_str_to_c_str(
480         &mut self,
481         os_str: &OsStr,
482         scalar: Scalar<Tag>,
483         size: u64,
484     ) -> InterpResult<'tcx, (bool, u64)> {
485         #[cfg(target_os = "unix")]
486         fn os_str_to_bytes<'tcx, 'a>(os_str: &'a OsStr) -> InterpResult<'tcx, &'a [u8]> {
487             std::os::unix::ffi::OsStringExt::into_bytes(os_str)
488         }
489         #[cfg(not(target_os = "unix"))]
490         fn os_str_to_bytes<'tcx, 'a>(os_str: &'a OsStr) -> InterpResult<'tcx, &'a [u8]> {
491             // On non-unix platforms the best we can do to transform bytes from/to OS strings is to do the
492             // intermediate transformation into strings. Which invalidates non-utf8 paths that are actually
493             // valid.
494             os_str
495                 .to_str()
496                 .map(|s| s.as_bytes())
497                 .ok_or_else(|| err_unsup_format!("{:?} is not a valid utf-8 string", os_str).into())
498         }
499
500         let bytes = os_str_to_bytes(os_str)?;
501         // If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required null
502         // terminator to memory using the `ptr` pointer would cause an out-of-bounds access.
503         let string_length = u64::try_from(bytes.len()).unwrap();
504         if size <= string_length {
505             return Ok((false, string_length));
506         }
507         self.eval_context_mut()
508             .memory
509             .write_bytes(scalar, bytes.iter().copied().chain(iter::once(0u8)))?;
510         Ok((true, string_length))
511     }
512
513     fn alloc_os_str_as_c_str(
514         &mut self,
515         os_str: &OsStr,
516         memkind: MemoryKind<MiriMemoryKind>,
517     ) -> Pointer<Tag> {
518         let size = u64::try_from(os_str.len()).unwrap().checked_add(1).unwrap(); // Make space for `0` terminator.
519         let this = self.eval_context_mut();
520
521         let arg_type = this.tcx.mk_array(this.tcx.types.u8, size);
522         let arg_place = this.allocate(this.layout_of(arg_type).unwrap(), memkind);
523         self.write_os_str_to_c_str(os_str, arg_place.ptr, size).unwrap();
524         arg_place.ptr.assert_ptr()
525     }
526 }
527
528 pub fn immty_from_int_checked<'tcx>(
529     int: impl Into<i128>,
530     layout: TyLayout<'tcx>,
531 ) -> InterpResult<'tcx, ImmTy<'tcx, Tag>> {
532     let int = int.into();
533     Ok(ImmTy::try_from_int(int, layout).ok_or_else(|| {
534         err_unsup_format!("Signed value {:#x} does not fit in {} bits", int, layout.size.bits())
535     })?)
536 }
537
538 pub fn immty_from_uint_checked<'tcx>(
539     int: impl Into<u128>,
540     layout: TyLayout<'tcx>,
541 ) -> InterpResult<'tcx, ImmTy<'tcx, Tag>> {
542     let int = int.into();
543     Ok(ImmTy::try_from_uint(int, layout).ok_or_else(|| {
544         err_unsup_format!("Signed value {:#x} does not fit in {} bits", int, layout.size.bits())
545     })?)
546 }