Skip to main content

miri/shims/
mod.rs

1#![warn(clippy::arithmetic_side_effects)]
2
3mod alloc;
4mod backtrace;
5mod files;
6mod math;
7#[cfg(all(feature = "native-lib", unix))]
8pub mod native_lib;
9mod unix;
10mod windows;
11
12pub mod cpu_affinity;
13pub mod env;
14pub mod extern_static;
15pub mod foreign_items;
16pub mod global_ctor;
17pub mod io_error;
18pub mod os_str;
19pub mod panic;
20pub mod readiness;
21pub mod sig;
22pub mod time;
23pub mod tls;
24pub mod unwind;
25
26pub use self::cpu_affinity::CpuAffinityMask;
27pub use self::files::{FdId, FdTable, FileDescription, FileDescriptionRef, WeakFileDescriptionRef};
28#[cfg(all(feature = "native-lib", unix))]
29pub use self::native_lib::trace::{init_sv, register_retcode_sv};
30pub use self::unix::DirTable;
31
32/// What needs to be done after emulating an item (a shim or an intrinsic) is done.
33pub enum EmulateItemResult {
34    /// The caller is expected to jump to the return block.
35    NeedsReturn,
36    /// The caller is expected to jump to the unwind block.
37    NeedsUnwind,
38    /// Jumping to the next block has already been taken care of.
39    AlreadyJumped,
40    /// The item is not supported.
41    NotSupported,
42}
43
44impl EmulateItemResult {
45    pub fn jump_to_next_block<'tcx, T: Default>(
46        self,
47        ecx: &mut crate::MiriInterpCx<'tcx>,
48        dest: &crate::PlaceTy<'tcx>,
49        ret: Option<rustc_middle::mir::BasicBlock>,
50        unwind: Option<rustc_middle::mir::UnwindAction>,
51        not_supported: impl FnOnce(&mut crate::MiriInterpCx<'tcx>) -> crate::InterpResult<'tcx, T>,
52    ) -> crate::InterpResult<'tcx, T> {
53        use crate::*;
54
55        match self {
56            EmulateItemResult::NeedsReturn => {
57                trace!("{:?}", ecx.dump_place(dest));
58                ecx.return_to_block(ret)?;
59                interp_ok(T::default())
60            }
61            EmulateItemResult::NeedsUnwind => {
62                // Jump to the unwind block to begin unwinding.
63                ecx.unwind_to_block(unwind.unwrap())?;
64                interp_ok(T::default())
65            }
66            EmulateItemResult::AlreadyJumped => interp_ok(T::default()),
67            EmulateItemResult::NotSupported => not_supported(ecx),
68        }
69    }
70}