2018-09-19 18:00:59 -05:00
|
|
|
use std::collections::BTreeMap;
|
2017-07-31 06:30:44 -05:00
|
|
|
|
2018-10-09 15:03:14 -05:00
|
|
|
use rustc_target::abi::LayoutOf;
|
2018-09-19 18:00:59 -05:00
|
|
|
use rustc::{ty, ty::layout::HasDataLayout, mir};
|
|
|
|
|
2018-10-09 15:03:14 -05:00
|
|
|
use super::{
|
2018-10-16 11:01:50 -05:00
|
|
|
EvalResult, EvalErrorKind, StackPopCleanup,
|
2018-10-16 04:21:38 -05:00
|
|
|
MPlaceTy, Scalar, Borrow,
|
2018-10-09 15:03:14 -05:00
|
|
|
};
|
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> {
|
2018-10-16 04:21:38 -05:00
|
|
|
pub(crate) data: Scalar<Borrow>, // Will eventually become a map from thread IDs to `Scalar`s, if we ever support more than one thread.
|
2018-09-19 18:00:59 -05:00
|
|
|
pub(crate) dtor: Option<ty::Instance<'tcx>>,
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
pub(crate) next_key: TlsKey,
|
|
|
|
|
|
|
|
/// pthreads-style thread-local storage.
|
|
|
|
pub(crate) keys: BTreeMap<TlsKey, TlsEntry<'tcx>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
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(),
|
|
|
|
}
|
|
|
|
}
|
2017-07-31 06:30:44 -05:00
|
|
|
}
|
|
|
|
|
2017-08-01 04:11:57 -05:00
|
|
|
pub trait EvalContextExt<'tcx> {
|
|
|
|
fn run_tls_dtors(&mut self) -> EvalResult<'tcx>;
|
|
|
|
}
|
|
|
|
|
2018-09-19 18:00:59 -05:00
|
|
|
impl<'tcx> TlsData<'tcx> {
|
|
|
|
pub fn create_tls_key(
|
|
|
|
&mut self,
|
|
|
|
dtor: Option<ty::Instance<'tcx>>,
|
|
|
|
cx: impl HasDataLayout,
|
|
|
|
) -> TlsKey {
|
|
|
|
let new_key = self.next_key;
|
|
|
|
self.next_key += 1;
|
|
|
|
self.keys.insert(
|
2017-08-10 10:48:38 -05:00
|
|
|
new_key,
|
|
|
|
TlsEntry {
|
2018-09-19 18:00:59 -05:00
|
|
|
data: Scalar::ptr_null(cx).into(),
|
2017-08-10 10:48:38 -05:00
|
|
|
dtor,
|
|
|
|
},
|
|
|
|
);
|
2017-07-31 06:30:44 -05:00
|
|
|
trace!("New TLS key allocated: {} with dtor {:?}", new_key, dtor);
|
2018-07-10 10:32:38 -05:00
|
|
|
new_key
|
2017-07-31 06:30:44 -05:00
|
|
|
}
|
|
|
|
|
2018-09-19 18:00:59 -05:00
|
|
|
pub fn delete_tls_key(&mut self, key: TlsKey) -> EvalResult<'tcx> {
|
|
|
|
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
|
|
|
}
|
|
|
|
None => err!(TlsOutOfBounds),
|
2018-07-10 10:32:38 -05:00
|
|
|
}
|
2017-07-31 06:30:44 -05:00
|
|
|
}
|
|
|
|
|
2018-10-16 04:21:38 -05:00
|
|
|
pub fn load_tls(&mut self, key: TlsKey) -> EvalResult<'tcx, Scalar<Borrow>> {
|
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);
|
|
|
|
Ok(data)
|
2017-08-10 10:48:38 -05:00
|
|
|
}
|
|
|
|
None => err!(TlsOutOfBounds),
|
2018-07-10 10:32:38 -05:00
|
|
|
}
|
2017-07-31 06:30:44 -05:00
|
|
|
}
|
|
|
|
|
2018-10-16 04:21:38 -05:00
|
|
|
pub fn store_tls(&mut self, key: TlsKey, new_data: Scalar<Borrow>) -> EvalResult<'tcx> {
|
2018-09-19 18:00:59 -05:00
|
|
|
match self.keys.get_mut(&key) {
|
2017-07-31 06:30:44 -05:00
|
|
|
Some(&mut TlsEntry { ref mut data, .. }) => {
|
|
|
|
trace!("TLS key {} stored: {:?}", key, new_data);
|
|
|
|
*data = new_data;
|
|
|
|
Ok(())
|
2017-08-10 10:48:38 -05:00
|
|
|
}
|
|
|
|
None => err!(TlsOutOfBounds),
|
2018-07-10 10:32:38 -05:00
|
|
|
}
|
2017-07-31 06:30:44 -05:00
|
|
|
}
|
2017-08-10 10:48:38 -05:00
|
|
|
|
2017-07-31 06:30:44 -05:00
|
|
|
/// Returns a dtor, its argument and its index, if one is supposed to run
|
|
|
|
///
|
|
|
|
/// 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>,
|
2018-09-19 18:00:59 -05:00
|
|
|
cx: impl HasDataLayout,
|
2018-10-16 04:21:38 -05:00
|
|
|
) -> Option<(ty::Instance<'tcx>, Scalar<Borrow>, 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,
|
|
|
|
};
|
2017-08-10 10:48:38 -05:00
|
|
|
for (&key, &mut TlsEntry { ref mut 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
|
|
|
{
|
2018-10-25 04:37:42 -05:00
|
|
|
if !data.is_null_ptr(cx) {
|
2017-07-31 06:30:44 -05:00
|
|
|
if let Some(dtor) = dtor {
|
|
|
|
let ret = Some((dtor, *data, key));
|
2018-09-19 18:00:59 -05:00
|
|
|
*data = Scalar::ptr_null(cx);
|
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
|
|
|
|
2018-10-16 11:01:50 -05:00
|
|
|
impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for super::MiriEvalContext<'a, 'mir, 'tcx> {
|
2017-08-01 04:11:57 -05:00
|
|
|
fn run_tls_dtors(&mut self) -> EvalResult<'tcx> {
|
2018-09-19 18:00:59 -05:00
|
|
|
let mut dtor = self.machine.tls.fetch_tls_dtor(None, *self.tcx);
|
2017-08-01 04:11:57 -05:00
|
|
|
// FIXME: replace loop by some structure that works with stepping
|
|
|
|
while let Some((instance, ptr, key)) = dtor {
|
|
|
|
trace!("Running TLS dtor {:?} on {:?}", instance, ptr);
|
|
|
|
// TODO: Potentially, this has to support all the other possible instances?
|
|
|
|
// See eval_fn_call in interpret/terminator/mod.rs
|
|
|
|
let mir = self.load_mir(instance.def)?;
|
2018-10-09 15:03:14 -05:00
|
|
|
let ret_place = MPlaceTy::dangling(self.layout_of(self.tcx.mk_unit())?, &self).into();
|
2017-08-01 04:11:57 -05:00
|
|
|
self.push_stack_frame(
|
|
|
|
instance,
|
|
|
|
mir.span,
|
|
|
|
mir,
|
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
|
|
|
)?;
|
2018-07-10 10:32:38 -05:00
|
|
|
let arg_local = self.frame().mir.args_iter().next().ok_or_else(
|
|
|
|
|| EvalErrorKind::AbiViolation("TLS dtor does not take enough arguments.".to_owned()),
|
2017-08-10 10:48:38 -05:00
|
|
|
)?;
|
2017-12-06 01:39:31 -06:00
|
|
|
let dest = self.eval_place(&mir::Place::Local(arg_local))?;
|
2018-08-15 14:01:40 -05:00
|
|
|
self.write_scalar(ptr, dest)?;
|
2017-08-01 04:11:57 -05:00
|
|
|
|
|
|
|
// step until out of stackframes
|
2018-08-23 12:28:48 -05:00
|
|
|
self.run()?;
|
2017-08-01 04:11:57 -05:00
|
|
|
|
2018-09-19 18:00:59 -05:00
|
|
|
dtor = match self.machine.tls.fetch_tls_dtor(Some(key), *self.tcx) {
|
2017-08-01 04:11:57 -05:00
|
|
|
dtor @ Some(_) => dtor,
|
2018-09-19 18:00:59 -05:00
|
|
|
None => self.machine.tls.fetch_tls_dtor(None, *self.tcx),
|
2017-08-01 04:11:57 -05:00
|
|
|
};
|
|
|
|
}
|
2018-07-16 04:42:46 -05:00
|
|
|
// FIXME: On a windows target, call `unsafe extern "system" fn on_tls_callback`.
|
2017-08-01 04:11:57 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|