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