use rustc_data_structures::fx::FxHashMap;
use rustc_middle::ty;
use rustc_target::abi::{Size, HasDataLayout};
+use rustc_target::spec::abi::Abi;
-use crate::{
- HelpersEvalContextExt, InterpResult, MPlaceTy, Scalar, StackPopCleanup, Tag, ThreadId,
- ThreadsEvalContextExt,
-};
+use crate::*;
pub type TlsKey = u128;
pub fn create_tls_key(&mut self, dtor: Option<ty::Instance<'tcx>>, max_size: Size) -> InterpResult<'tcx, TlsKey> {
let new_key = self.next_key;
self.next_key += 1;
- self.keys.insert(new_key, TlsEntry { data: Default::default(), dtor }).unwrap_none();
+ self.keys.try_insert(new_key, TlsEntry { data: Default::default(), dtor }).unwrap();
trace!("New TLS key allocated: {} with dtor {:?}", new_key, dtor);
if max_size.bits() < 128 && new_key >= (1u128 << max_size.bits() as u128) {
key: Option<TlsKey>,
thread_id: ThreadId,
) -> Option<(ty::Instance<'tcx>, Scalar<Tag>, TlsKey)> {
- use std::collections::Bound::*;
+ use std::ops::Bound::*;
let thread_local = &mut self.keys;
let start = match key {
fn schedule_windows_tls_dtors(&mut self) -> InterpResult<'tcx> {
let this = self.eval_context_mut();
let active_thread = this.get_active_thread();
- assert_eq!(this.get_total_thread_count(), 1, "concurrency on Windows not supported");
+ assert_eq!(this.get_total_thread_count(), 1, "concurrency on Windows is not supported");
// 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()?;
+ let thread_callback = this.eval_path_scalar(&["std", "sys", "windows", "thread_local_key", "p_thread_callback"])?;
+ let thread_callback = this.memory.get_fn(thread_callback.check_init()?)?.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_THREAD_DETACH"])?;
let ret_place = MPlaceTy::dangling(this.machine.layouts.unit, this).into();
this.call_function(
thread_callback,
+ Abi::System { unwind: false },
&[Scalar::null_ptr(this).into(), reason.into(), Scalar::null_ptr(this).into()],
- Some(ret_place),
+ Some(&ret_place),
StackPopCleanup::None { cleanup: true },
)?;
let ret_place = MPlaceTy::dangling(this.machine.layouts.unit, this).into();
this.call_function(
instance,
+ Abi::C { unwind: false },
&[data.into()],
- Some(ret_place),
+ Some(&ret_place),
StackPopCleanup::None { cleanup: true },
)?;
let ret_place = MPlaceTy::dangling(this.machine.layouts.unit, this).into();
this.call_function(
instance,
+ Abi::C { unwind: false },
&[ptr.into()],
- Some(ret_place),
+ Some(&ret_place),
StackPopCleanup::None { cleanup: true },
)?;
/// schedules them one by one each time it is called and reenables the
/// thread so that it can be executed normally by the main execution loop.
///
- /// FIXME: we do not support yet deallocation of thread local statics.
- /// Issue: https://github.com/rust-lang/miri/issues/1369
- ///
/// Note: we consistently run TLS destructors for all threads, including the
/// main thread. However, it is not clear that we should run the TLS
/// destructors for the main thread. See issue:
// This is the first time we got asked to schedule a destructor. The
// Windows schedule destructor function must be called exactly once,
// this is why it is in this block.
- if this.tcx.sess.target.target.target_os == "windows" {
+ if this.tcx.sess.target.os == "windows" {
// On Windows, we signal that the thread quit by starting the
// relevant function, reenabling the thread, and going back to
// the scheduler.
return Ok(())
}
}
+ // The remaining dtors make some progress each time around the scheduler loop,
+ // until they return `false` to indicate that they are done.
+
// The macOS thread wide destructor runs "before any TLS slots get
// freed", so do that first.
if this.schedule_macos_tls_dtor()? {
// All dtors done!
this.machine.tls.delete_all_thread_tls(active_thread);
+ this.thread_terminated()?;
Ok(())
}