miri/shims/
extern_static.rs1use crate::*;
4
5impl<'tcx> MiriMachine<'tcx> {
6 fn alloc_extern_static(
7 ecx: &mut MiriInterpCx<'tcx>,
8 name: &str,
9 val: ImmTy<'tcx>,
10 ) -> InterpResult<'tcx> {
11 let place = ecx.allocate(val.layout, MiriMemoryKind::ExternStatic.into())?;
12 ecx.write_immediate(*val, &place)?;
13 Self::add_extern_static(ecx, name, place.ptr());
14 interp_ok(())
15 }
16
17 fn null_ptr_extern_statics(ecx: &mut MiriInterpCx<'tcx>, names: &[&str]) -> InterpResult<'tcx> {
22 for name in names {
23 let val = ImmTy::from_int(0, ecx.machine.layouts.usize);
24 Self::alloc_extern_static(ecx, name, val)?;
25 }
26 interp_ok(())
27 }
28
29 fn weak_symbol_extern_statics(
31 ecx: &mut MiriInterpCx<'tcx>,
32 names: &[&str],
33 ) -> InterpResult<'tcx> {
34 for name in names {
35 assert!(ecx.is_dyn_sym(name), "{name} is not a dynamic symbol");
36 let layout = ecx.machine.layouts.const_raw_ptr;
37 let ptr = ecx.fn_ptr(FnVal::Other(DynSym::from_str(name)));
38 let val = ImmTy::from_scalar(Scalar::from_pointer(ptr, ecx), layout);
39 Self::alloc_extern_static(ecx, name, val)?;
40 }
41 interp_ok(())
42 }
43
44 pub fn init_extern_statics(ecx: &mut MiriInterpCx<'tcx>) -> InterpResult<'tcx> {
46 let val = ImmTy::from_int(0, ecx.machine.layouts.u8); Self::alloc_extern_static(ecx, "__rust_no_alloc_shim_is_unstable", val)?;
49
50 let val = ecx.tcx.sess.opts.unstable_opts.oom.should_panic();
52 let val = ImmTy::from_int(val, ecx.machine.layouts.u8);
53 Self::alloc_extern_static(ecx, "__rust_alloc_error_handler_should_panic", val)?;
54
55 if ecx.target_os_is_unix() {
56 let environ = ecx.machine.env_vars.unix().environ();
58 Self::add_extern_static(ecx, "environ", environ);
59 }
60
61 match ecx.tcx.sess.target.os.as_ref() {
62 "linux" => {
63 Self::null_ptr_extern_statics(
64 ecx,
65 &["__cxa_thread_atexit_impl", "__clock_gettime64"],
66 )?;
67 Self::weak_symbol_extern_statics(ecx, &["getrandom", "statx"])?;
68 }
69 "freebsd" => {
70 Self::null_ptr_extern_statics(ecx, &["__cxa_thread_atexit_impl"])?;
71 }
72 "android" => {
73 Self::null_ptr_extern_statics(ecx, &["bsd_signal"])?;
74 Self::weak_symbol_extern_statics(ecx, &["signal", "getrandom"])?;
75 }
76 "windows" => {
77 let val = ImmTy::from_int(0, ecx.machine.layouts.u8);
80 Self::alloc_extern_static(ecx, "_tls_used", val)?;
81 }
82 "illumos" | "solaris" => {
83 Self::weak_symbol_extern_statics(ecx, &["pthread_setname_np"])?;
84 }
85 _ => {} }
87 interp_ok(())
88 }
89}