2019-06-29 07:15:05 -05:00
|
|
|
//! Implement thread-local storage.
|
|
|
|
|
2018-09-19 18:00:59 -05:00
|
|
|
use std::collections::BTreeMap;
|
2017-07-31 06:30:44 -05:00
|
|
|
|
2020-03-30 04:07:32 -05:00
|
|
|
use log::trace;
|
|
|
|
|
2020-04-02 17:05:35 -05:00
|
|
|
use rustc_middle::ty;
|
|
|
|
use rustc_target::abi::{LayoutOf, Size, HasDataLayout};
|
2018-09-19 18:00:59 -05:00
|
|
|
|
2019-12-23 05:56:23 -06:00
|
|
|
use crate::{HelpersEvalContextExt, InterpResult, MPlaceTy, Scalar, StackPopCleanup, Tag};
|
2017-07-31 06:30:44 -05:00
|
|
|
|
2018-09-19 18:00:59 -05:00
|
|
|
pub type TlsKey = u128;
|
|
|
|
|
2018-10-16 11:01:50 -05:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2018-09-19 18:00:59 -05:00
|
|
|
pub struct TlsEntry<'tcx> {
|
2019-07-05 02:56:42 -05:00
|
|
|
/// The data for this key. None is used to represent NULL.
|
|
|
|
/// (We normalize this early to avoid having to do a NULL-ptr-test each time we access the data.)
|
|
|
|
/// Will eventually become a map from thread IDs to `Scalar`s, if we ever support more than one thread.
|
2019-08-13 02:29:01 -05:00
|
|
|
data: Option<Scalar<Tag>>,
|
|
|
|
dtor: Option<ty::Instance<'tcx>>,
|
2018-09-19 18:00:59 -05:00
|
|
|
}
|
|
|
|
|
2018-10-16 11:01:50 -05:00
|
|
|
#[derive(Debug)]
|
2018-09-19 18:00:59 -05:00
|
|
|
pub struct TlsData<'tcx> {
|
|
|
|
/// The Key to use for the next thread-local allocation.
|
2019-08-13 02:29:01 -05:00
|
|
|
next_key: TlsKey,
|
2018-09-19 18:00:59 -05:00
|
|
|
|
|
|
|
/// pthreads-style thread-local storage.
|
2019-08-13 02:29:01 -05:00
|
|
|
keys: BTreeMap<TlsKey, TlsEntry<'tcx>>,
|
2020-03-27 13:39:00 -05:00
|
|
|
|
|
|
|
/// A single global dtor (that's how things work on macOS) with a data argument.
|
|
|
|
global_dtor: Option<(ty::Instance<'tcx>, Scalar<Tag>)>,
|
|
|
|
|
|
|
|
/// Whether we are in the "destruct" phase, during which some operations are UB.
|
|
|
|
dtors_running: bool,
|
2018-09-19 18:00:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Default for TlsData<'tcx> {
|
|
|
|
fn default() -> Self {
|
|
|
|
TlsData {
|
|
|
|
next_key: 1, // start with 1 as we must not use 0 on Windows
|
|
|
|
keys: Default::default(),
|
2020-03-27 13:39:00 -05:00
|
|
|
global_dtor: None,
|
|
|
|
dtors_running: false,
|
2018-09-19 18:00:59 -05:00
|
|
|
}
|
|
|
|
}
|
2017-07-31 06:30:44 -05:00
|
|
|
}
|
|
|
|
|
2018-09-19 18:00:59 -05:00
|
|
|
impl<'tcx> TlsData<'tcx> {
|
2020-03-08 17:34:54 -05:00
|
|
|
/// Generate a new TLS key with the given destructor.
|
|
|
|
/// `max_size` determines the integer size the key has to fit in.
|
|
|
|
pub fn create_tls_key(&mut self, dtor: Option<ty::Instance<'tcx>>, max_size: Size) -> InterpResult<'tcx, TlsKey> {
|
2018-09-19 18:00:59 -05:00
|
|
|
let new_key = self.next_key;
|
|
|
|
self.next_key += 1;
|
2019-12-23 05:56:23 -06:00
|
|
|
self.keys.insert(new_key, TlsEntry { data: None, dtor }).unwrap_none();
|
2017-07-31 06:30:44 -05:00
|
|
|
trace!("New TLS key allocated: {} with dtor {:?}", new_key, dtor);
|
2020-03-08 17:34:54 -05:00
|
|
|
|
|
|
|
if max_size.bits() < 128 && new_key >= (1u128 << max_size.bits() as u128) {
|
|
|
|
throw_unsup_format!("we ran out of TLS key space");
|
|
|
|
}
|
|
|
|
Ok(new_key)
|
2017-07-31 06:30:44 -05:00
|
|
|
}
|
|
|
|
|
2019-06-08 15:14:47 -05:00
|
|
|
pub fn delete_tls_key(&mut self, key: TlsKey) -> InterpResult<'tcx> {
|
2018-09-19 18:00:59 -05:00
|
|
|
match self.keys.remove(&key) {
|
2017-07-31 06:30:44 -05:00
|
|
|
Some(_) => {
|
|
|
|
trace!("TLS key {} removed", key);
|
|
|
|
Ok(())
|
2017-08-10 10:48:38 -05:00
|
|
|
}
|
2020-03-08 17:34:54 -05:00
|
|
|
None => throw_ub_format!("removing a non-existig TLS key: {}", key),
|
2018-07-10 10:32:38 -05:00
|
|
|
}
|
2017-07-31 06:30:44 -05:00
|
|
|
}
|
|
|
|
|
2019-07-05 02:56:42 -05:00
|
|
|
pub fn load_tls(
|
2020-03-28 03:50:24 -05:00
|
|
|
&self,
|
2019-07-05 02:56:42 -05:00
|
|
|
key: TlsKey,
|
|
|
|
cx: &impl HasDataLayout,
|
|
|
|
) -> InterpResult<'tcx, Scalar<Tag>> {
|
2018-09-19 18:00:59 -05:00
|
|
|
match self.keys.get(&key) {
|
2017-07-31 06:30:44 -05:00
|
|
|
Some(&TlsEntry { data, .. }) => {
|
|
|
|
trace!("TLS key {} loaded: {:?}", key, data);
|
2020-03-29 03:01:31 -05:00
|
|
|
Ok(data.unwrap_or_else(|| Scalar::null_ptr(cx).into()))
|
2017-08-10 10:48:38 -05:00
|
|
|
}
|
2020-03-08 17:34:54 -05:00
|
|
|
None => throw_ub_format!("loading from a non-existing TLS key: {}", key),
|
2018-07-10 10:32:38 -05:00
|
|
|
}
|
2017-07-31 06:30:44 -05:00
|
|
|
}
|
|
|
|
|
2019-07-05 02:56:42 -05:00
|
|
|
pub fn store_tls(&mut self, key: TlsKey, new_data: Option<Scalar<Tag>>) -> InterpResult<'tcx> {
|
2018-09-19 18:00:59 -05:00
|
|
|
match self.keys.get_mut(&key) {
|
2020-04-12 03:08:12 -05:00
|
|
|
Some(TlsEntry { data, .. }) => {
|
2017-07-31 06:30:44 -05:00
|
|
|
trace!("TLS key {} stored: {:?}", key, new_data);
|
|
|
|
*data = new_data;
|
|
|
|
Ok(())
|
2017-08-10 10:48:38 -05:00
|
|
|
}
|
2020-03-08 17:34:54 -05:00
|
|
|
None => throw_ub_format!("storing to a non-existing TLS key: {}", key),
|
2018-07-10 10:32:38 -05:00
|
|
|
}
|
2017-07-31 06:30:44 -05:00
|
|
|
}
|
2017-08-10 10:48:38 -05:00
|
|
|
|
2020-03-27 13:39:00 -05:00
|
|
|
pub fn set_global_dtor(&mut self, dtor: ty::Instance<'tcx>, data: Scalar<Tag>) -> InterpResult<'tcx> {
|
|
|
|
if self.dtors_running {
|
|
|
|
// UB, according to libstd docs.
|
|
|
|
throw_ub_format!("setting global destructor while destructors are already running");
|
|
|
|
}
|
|
|
|
if self.global_dtor.is_some() {
|
|
|
|
throw_unsup_format!("setting more than one global destructor is not supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
self.global_dtor = Some((dtor, data));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-03-28 03:50:24 -05:00
|
|
|
/// Returns a dtor, its argument and its index, if one is supposed to run.
|
|
|
|
/// `key` is the last dtors that was run; we return the *next* one after that.
|
2017-07-31 06:30:44 -05:00
|
|
|
///
|
|
|
|
/// An optional destructor function may be associated with each key value.
|
|
|
|
/// At thread exit, if a key value has a non-NULL destructor pointer,
|
|
|
|
/// and the thread has a non-NULL value associated with that key,
|
|
|
|
/// the value of the key is set to NULL, and then the function pointed
|
|
|
|
/// to is called with the previously associated value as its sole argument.
|
|
|
|
/// The order of destructor calls is unspecified if more than one destructor
|
|
|
|
/// exists for a thread when it exits.
|
|
|
|
///
|
|
|
|
/// If, after all the destructors have been called for all non-NULL values
|
|
|
|
/// with associated destructors, there are still some non-NULL values with
|
|
|
|
/// associated destructors, then the process is repeated.
|
|
|
|
/// If, after at least {PTHREAD_DESTRUCTOR_ITERATIONS} iterations of destructor
|
|
|
|
/// calls for outstanding non-NULL values, there are still some non-NULL values
|
|
|
|
/// with associated destructors, implementations may stop calling destructors,
|
|
|
|
/// or they may continue calling destructors until no non-NULL values with
|
|
|
|
/// associated destructors exist, even though this might result in an infinite loop.
|
2017-08-10 10:48:38 -05:00
|
|
|
fn fetch_tls_dtor(
|
|
|
|
&mut self,
|
|
|
|
key: Option<TlsKey>,
|
2019-04-15 08:36:09 -05:00
|
|
|
) -> Option<(ty::Instance<'tcx>, Scalar<Tag>, TlsKey)> {
|
2017-07-31 06:30:44 -05:00
|
|
|
use std::collections::Bound::*;
|
2018-05-26 10:07:34 -05:00
|
|
|
|
2018-09-19 18:00:59 -05:00
|
|
|
let thread_local = &mut self.keys;
|
2017-07-31 06:30:44 -05:00
|
|
|
let start = match key {
|
|
|
|
Some(key) => Excluded(key),
|
|
|
|
None => Unbounded,
|
|
|
|
};
|
2020-04-12 03:08:12 -05:00
|
|
|
for (&key, TlsEntry { data, dtor }) in
|
2018-05-26 10:07:34 -05:00
|
|
|
thread_local.range_mut((start, Unbounded))
|
2017-08-10 10:48:38 -05:00
|
|
|
{
|
2019-07-05 03:08:57 -05:00
|
|
|
if let Some(data_scalar) = *data {
|
2017-07-31 06:30:44 -05:00
|
|
|
if let Some(dtor) = dtor {
|
2020-04-12 03:08:12 -05:00
|
|
|
let ret = Some((*dtor, data_scalar, key));
|
2019-07-05 03:08:57 -05:00
|
|
|
*data = None;
|
2018-08-07 08:22:11 -05:00
|
|
|
return ret;
|
2017-07-31 06:30:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-07 08:22:11 -05:00
|
|
|
None
|
2017-07-31 06:30:44 -05:00
|
|
|
}
|
|
|
|
}
|
2017-08-01 04:11:57 -05:00
|
|
|
|
2019-06-13 01:52:04 -05:00
|
|
|
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
|
|
|
|
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
|
2019-06-08 15:14:47 -05:00
|
|
|
fn run_tls_dtors(&mut self) -> InterpResult<'tcx> {
|
2018-12-11 07:16:58 -06:00
|
|
|
let this = self.eval_context_mut();
|
2020-03-27 13:39:00 -05:00
|
|
|
assert!(!this.machine.tls.dtors_running, "running TLS dtors twice");
|
|
|
|
this.machine.tls.dtors_running = true;
|
|
|
|
|
2020-03-28 04:07:23 -05:00
|
|
|
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,
|
2020-03-29 03:01:31 -05:00
|
|
|
&[Scalar::null_ptr(this).into(), reason.into(), Scalar::null_ptr(this).into()],
|
2020-03-28 04:07:23 -05:00
|
|
|
Some(ret_place),
|
|
|
|
StackPopCleanup::None { cleanup: true },
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// step until out of stackframes
|
|
|
|
this.run()?;
|
|
|
|
|
|
|
|
// Windows doesn't have other destructors.
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2020-03-27 13:39:00 -05:00
|
|
|
// 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);
|
|
|
|
|
|
|
|
let ret_place = MPlaceTy::dangling(this.layout_of(this.tcx.mk_unit())?, this).into();
|
|
|
|
this.call_function(
|
|
|
|
instance,
|
|
|
|
&[data.into()],
|
|
|
|
Some(ret_place),
|
|
|
|
StackPopCleanup::None { cleanup: true },
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// step until out of stackframes
|
|
|
|
this.run()?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now run the "keyed" destructors.
|
2019-07-05 03:08:57 -05:00
|
|
|
let mut dtor = this.machine.tls.fetch_tls_dtor(None);
|
2017-08-01 04:11:57 -05:00
|
|
|
while let Some((instance, ptr, key)) = dtor {
|
|
|
|
trace!("Running TLS dtor {:?} on {:?}", instance, ptr);
|
2020-03-27 13:39:00 -05:00
|
|
|
assert!(!this.is_null(ptr).unwrap(), "data can't be NULL when dtor is called!");
|
2019-11-28 16:42:10 -06:00
|
|
|
|
2018-12-11 07:16:58 -06:00
|
|
|
let ret_place = MPlaceTy::dangling(this.layout_of(this.tcx.mk_unit())?, this).into();
|
2019-11-28 16:42:10 -06:00
|
|
|
this.call_function(
|
2017-08-01 04:11:57 -05:00
|
|
|
instance,
|
2019-11-29 04:17:44 -06:00
|
|
|
&[ptr.into()],
|
2018-10-09 15:03:14 -05:00
|
|
|
Some(ret_place),
|
2018-08-24 10:44:04 -05:00
|
|
|
StackPopCleanup::None { cleanup: true },
|
2017-08-01 04:11:57 -05:00
|
|
|
)?;
|
|
|
|
|
|
|
|
// step until out of stackframes
|
2018-12-11 07:16:58 -06:00
|
|
|
this.run()?;
|
2017-08-01 04:11:57 -05:00
|
|
|
|
2020-03-28 03:50:24 -05:00
|
|
|
// Fetch next dtor after `key`.
|
2019-07-05 03:08:57 -05:00
|
|
|
dtor = match this.machine.tls.fetch_tls_dtor(Some(key)) {
|
2017-08-01 04:11:57 -05:00
|
|
|
dtor @ Some(_) => dtor,
|
2020-03-28 03:50:24 -05:00
|
|
|
// We ran each dtor once, start over from the beginning.
|
2019-07-05 03:08:57 -05:00
|
|
|
None => this.machine.tls.fetch_tls_dtor(None),
|
2017-08-01 04:11:57 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|