miri/shims/
extern_static.rsuse crate::*;
impl<'tcx> MiriMachine<'tcx> {
fn alloc_extern_static(
ecx: &mut MiriInterpCx<'tcx>,
name: &str,
val: ImmTy<'tcx>,
) -> InterpResult<'tcx> {
let place = ecx.allocate(val.layout, MiriMemoryKind::ExternStatic.into())?;
ecx.write_immediate(*val, &place)?;
Self::add_extern_static(ecx, name, place.ptr());
interp_ok(())
}
fn null_ptr_extern_statics(ecx: &mut MiriInterpCx<'tcx>, names: &[&str]) -> InterpResult<'tcx> {
for name in names {
let val = ImmTy::from_int(0, ecx.machine.layouts.usize);
Self::alloc_extern_static(ecx, name, val)?;
}
interp_ok(())
}
fn weak_symbol_extern_statics(
ecx: &mut MiriInterpCx<'tcx>,
names: &[&str],
) -> InterpResult<'tcx> {
for name in names {
assert!(ecx.is_dyn_sym(name), "{name} is not a dynamic symbol");
let layout = ecx.machine.layouts.const_raw_ptr;
let ptr = ecx.fn_ptr(FnVal::Other(DynSym::from_str(name)));
let val = ImmTy::from_scalar(Scalar::from_pointer(ptr, ecx), layout);
Self::alloc_extern_static(ecx, name, val)?;
}
interp_ok(())
}
pub fn init_extern_statics(ecx: &mut MiriInterpCx<'tcx>) -> InterpResult<'tcx> {
let val = ImmTy::from_int(0, ecx.machine.layouts.u8); Self::alloc_extern_static(ecx, "__rust_no_alloc_shim_is_unstable", val)?;
let val = ecx.tcx.sess.opts.unstable_opts.oom.should_panic();
let val = ImmTy::from_int(val, ecx.machine.layouts.u8);
Self::alloc_extern_static(ecx, "__rust_alloc_error_handler_should_panic", val)?;
if ecx.target_os_is_unix() {
let environ = ecx.machine.env_vars.unix().environ();
Self::add_extern_static(ecx, "environ", environ);
}
match ecx.tcx.sess.target.os.as_ref() {
"linux" => {
Self::null_ptr_extern_statics(ecx, &[
"__cxa_thread_atexit_impl",
"__clock_gettime64",
])?;
Self::weak_symbol_extern_statics(ecx, &["getrandom", "statx"])?;
}
"freebsd" => {
Self::null_ptr_extern_statics(ecx, &["__cxa_thread_atexit_impl"])?;
}
"android" => {
Self::null_ptr_extern_statics(ecx, &["bsd_signal"])?;
Self::weak_symbol_extern_statics(ecx, &["signal", "getrandom"])?;
}
"windows" => {
let val = ImmTy::from_int(0, ecx.machine.layouts.u8);
Self::alloc_extern_static(ecx, "_tls_used", val)?;
}
"illumos" | "solaris" => {
Self::weak_symbol_extern_statics(ecx, &["pthread_setname_np"])?;
}
_ => {} }
interp_ok(())
}
}