diff --git a/src/eval.rs b/src/eval.rs index 7f07fc1a7db..512b4176df8 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -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 { - // 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, diff --git a/src/helpers.rs b/src/helpers.rs index 3e89fdf6f3c..aa327b468bf 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -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", diff --git a/src/machine.rs b/src/machine.rs index 73e7ce665b8..4bf3d0d7f44 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -102,6 +102,20 @@ impl MemoryExtra { } } + fn add_extern_static<'tcx, 'mir>( + this: &mut MiriEvalContext<'mir, 'tcx>, + name: &str, + ptr: Scalar, + ) { + 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 } diff --git a/src/shims/foreign_items.rs b/src/shims/foreign_items.rs index 0bed688187b..ecf24e2f203 100644 --- a/src/shims/foreign_items.rs +++ b/src/shims/foreign_items.rs @@ -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), diff --git a/src/shims/tls.rs b/src/shims/tls.rs index 461b5b3eed6..c753689f4c2 100644 --- a/src/shims/tls.rs +++ b/src/shims/tls.rs @@ -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(()) } }