miri/shims/
extern_static.rs

1//! Provides the `extern static` that this platform expects.
2
3use rustc_target::spec::Os;
4
5use crate::*;
6
7impl<'tcx> MiriMachine<'tcx> {
8    fn alloc_extern_static(
9        ecx: &mut MiriInterpCx<'tcx>,
10        name: &str,
11        val: ImmTy<'tcx>,
12    ) -> InterpResult<'tcx> {
13        let place = ecx.allocate(val.layout, MiriMemoryKind::ExternStatic.into())?;
14        ecx.write_immediate(*val, &place)?;
15        Self::add_extern_static(ecx, name, place.ptr());
16        interp_ok(())
17    }
18
19    /// Zero-initialized pointer-sized extern statics are pretty common.
20    /// Most of them are for weak symbols, which we all set to null (indicating that the
21    /// symbol is not supported, and triggering fallback code which ends up calling
22    /// some other shim that we do support).
23    fn null_ptr_extern_statics(ecx: &mut MiriInterpCx<'tcx>, names: &[&str]) -> InterpResult<'tcx> {
24        for name in names {
25            let val = ImmTy::from_int(0, ecx.machine.layouts.usize);
26            Self::alloc_extern_static(ecx, name, val)?;
27        }
28        interp_ok(())
29    }
30
31    /// Extern statics that are initialized with function pointers to the symbols of the same name.
32    fn weak_symbol_extern_statics(
33        ecx: &mut MiriInterpCx<'tcx>,
34        names: &[&str],
35    ) -> InterpResult<'tcx> {
36        for name in names {
37            assert!(ecx.is_dyn_sym(name), "{name} is not a dynamic symbol");
38            let layout = ecx.machine.layouts.const_raw_ptr;
39            let ptr = ecx.fn_ptr(FnVal::Other(DynSym::from_str(name)));
40            let val = ImmTy::from_scalar(Scalar::from_pointer(ptr, ecx), layout);
41            Self::alloc_extern_static(ecx, name, val)?;
42        }
43        interp_ok(())
44    }
45
46    /// Sets up the "extern statics" for this machine.
47    pub fn init_extern_statics(ecx: &mut MiriInterpCx<'tcx>) -> InterpResult<'tcx> {
48        if ecx.target_os_is_unix() {
49            // "environ" is mandated by POSIX.
50            let environ = ecx.machine.env_vars.unix().environ();
51            Self::add_extern_static(ecx, "environ", environ);
52        }
53
54        match &ecx.tcx.sess.target.os {
55            Os::Linux => {
56                Self::null_ptr_extern_statics(
57                    ecx,
58                    &["__cxa_thread_atexit_impl", "__clock_gettime64"],
59                )?;
60                Self::weak_symbol_extern_statics(ecx, &["getrandom", "gettid", "statx"])?;
61            }
62            Os::FreeBsd => {
63                Self::null_ptr_extern_statics(ecx, &["__cxa_thread_atexit_impl"])?;
64            }
65            Os::Android => {
66                Self::null_ptr_extern_statics(ecx, &["bsd_signal"])?;
67                Self::weak_symbol_extern_statics(ecx, &["signal", "getrandom", "gettid"])?;
68            }
69            Os::Windows => {
70                // "_tls_used"
71                // This is some obscure hack that is part of the Windows TLS story. It's a `u8`.
72                let val = ImmTy::from_int(0, ecx.machine.layouts.u8);
73                Self::alloc_extern_static(ecx, "_tls_used", val)?;
74            }
75            Os::Illumos | Os::Solaris => {
76                Self::weak_symbol_extern_statics(ecx, &["pthread_setname_np"])?;
77            }
78            _ => {} // No "extern statics" supported on this target
79        }
80        interp_ok(())
81    }
82}