run Windows TLS dtor function

This commit is contained in:
Ralf Jung 2020-03-28 10:07:23 +01:00
parent 02897e03cf
commit 876bded2e8
5 changed files with 55 additions and 17 deletions

View File

@ -184,10 +184,9 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
/// Returns `Some(return_code)` if program executed completed.
/// Returns `None` if an evaluation error occured.
pub fn eval_main<'tcx>(tcx: TyCtxt<'tcx>, main_id: DefId, config: MiriConfig) -> Option<i64> {
// FIXME: We always ignore leaks on some OSs where we do not
// correctly implement TLS destructors.
let target_os = &tcx.sess.target.target.target_os;
let ignore_leaks = config.ignore_leaks || target_os == "windows";
// FIXME: on Windows, locks and TLS dtor management allocate and leave that memory in `static`s.
// So we need https://github.com/rust-lang/miri/issues/940 to fix the leaks there.
let ignore_leaks = config.ignore_leaks || tcx.sess.target.target.target_os == "windows";
let (mut ecx, ret_place) = match create_ecx(tcx, main_id, config) {
Ok(v) => v,

View File

@ -413,7 +413,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
fn set_last_error_from_io_error(&mut self, e: std::io::Error) -> InterpResult<'tcx> {
use std::io::ErrorKind::*;
let this = self.eval_context_mut();
let target = &this.tcx.tcx.sess.target.target;
let target = &this.tcx.sess.target.target;
let last_error = if target.options.target_family == Some("unix".to_owned()) {
this.eval_libc(match e.kind() {
ConnectionRefused => "ECONNREFUSED",

View File

@ -102,6 +102,20 @@ impl MemoryExtra {
}
}
fn add_extern_static<'tcx, 'mir>(
this: &mut MiriEvalContext<'mir, 'tcx>,
name: &str,
ptr: Scalar<Tag>,
) {
let ptr = ptr.assert_ptr();
assert_eq!(ptr.offset, Size::ZERO);
this.memory
.extra
.extern_statics
.insert(Symbol::intern(name), ptr.alloc_id)
.unwrap_none();
}
/// Sets up the "extern statics" for this machine.
pub fn init_extern_statics<'tcx, 'mir>(
this: &mut MiriEvalContext<'mir, 'tcx>,
@ -113,17 +127,17 @@ impl MemoryExtra {
let layout = this.layout_of(this.tcx.types.usize)?;
let place = this.allocate(layout, MiriMemoryKind::Machine.into());
this.write_scalar(Scalar::from_machine_usize(0, &*this.tcx), place.into())?;
this.memory
.extra
.extern_statics
.insert(Symbol::intern("__cxa_thread_atexit_impl"), place.ptr.assert_ptr().alloc_id)
.unwrap_none();
Self::add_extern_static(this, "__cxa_thread_atexit_impl", place.ptr);
// "environ"
this.memory
.extra
.extern_statics
.insert(Symbol::intern("environ"), this.machine.env_vars.environ.unwrap().ptr.assert_ptr().alloc_id)
.unwrap_none();
Self::add_extern_static(this, "environ", this.machine.env_vars.environ.unwrap().ptr);
}
"windows" => {
// "_tls_used"
// This is some obscure hack that is part of the Windows TLS story. It's a `u8`.
let layout = this.layout_of(this.tcx.types.u8)?;
let place = this.allocate(layout, MiriMemoryKind::Machine.into());
this.write_scalar(Scalar::from_u8(0), place.into())?;
Self::add_extern_static(this, "_tls_used", place.ptr);
}
_ => {} // No "extern statics" supported on this target
}

View File

@ -19,7 +19,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
fn min_align(&self, size: u64, kind: MiriMemoryKind) -> Align {
let this = self.eval_context_ref();
// List taken from `libstd/sys_common/alloc.rs`.
let min_align = match this.tcx.tcx.sess.target.target.arch.as_str() {
let min_align = match this.tcx.sess.target.target.arch.as_str() {
"x86" | "arm" | "mips" | "powerpc" | "powerpc64" | "asmjs" | "wasm32" => 8,
"x86_64" | "aarch64" | "mips64" | "s390x" | "sparc64" => 16,
arch => bug!("Unsupported target architecture: {}", arch),

View File

@ -159,6 +159,32 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
assert!(!this.machine.tls.dtors_running, "running TLS dtors twice");
this.machine.tls.dtors_running = true;
if this.tcx.sess.target.target.target_os == "windows" {
// Windows has a special magic linker section that is run on certain events.
// Instead of searching for that section and supporting arbitrary hooks in there
// (that would be basically https://github.com/rust-lang/miri/issues/450),
// we specifically look up the static in libstd that we know is placed
// in that section.
let thread_callback = this.eval_path_scalar(&["std", "sys", "windows", "thread_local", "p_thread_callback"])?;
let thread_callback = this.memory.get_fn(thread_callback.not_undef()?)?.as_instance()?;
// The signature of this function is `unsafe extern "system" fn(h: c::LPVOID, dwReason: c::DWORD, pv: c::LPVOID)`.
let reason = this.eval_path_scalar(&["std", "sys", "windows", "c", "DLL_PROCESS_DETACH"])?;
let ret_place = MPlaceTy::dangling(this.layout_of(this.tcx.mk_unit())?, this).into();
this.call_function(
thread_callback,
&[Scalar::ptr_null(this).into(), reason.into(), Scalar::ptr_null(this).into()],
Some(ret_place),
StackPopCleanup::None { cleanup: true },
)?;
// step until out of stackframes
this.run()?;
// Windows doesn't have other destructors.
return Ok(());
}
// The macOS global dtor runs "before any TLS slots get freed", so do that first.
if let Some((instance, data)) = this.machine.tls.global_dtor {
trace!("Running global dtor {:?} on {:?}", instance, data);
@ -199,7 +225,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
None => this.machine.tls.fetch_tls_dtor(None),
};
}
// FIXME: On a windows target, call `unsafe extern "system" fn on_tls_callback`.
Ok(())
}
}