1 //! Implement thread-local storage.
3 use std::collections::BTreeMap;
4 use std::collections::btree_map::Entry;
5 use std::collections::HashSet;
10 use rustc_target::abi::{Size, HasDataLayout};
13 HelpersEvalContextExt, InterpResult, MPlaceTy, Scalar, StackPopCleanup, Tag, ThreadId,
14 ThreadsEvalContextExt,
17 pub type TlsKey = u128;
19 #[derive(Clone, Debug)]
20 pub struct TlsEntry<'tcx> {
21 /// The data for this key. None is used to represent NULL.
22 /// (We normalize this early to avoid having to do a NULL-ptr-test each time we access the data.)
23 /// Will eventually become a map from thread IDs to `Scalar`s, if we ever support more than one thread.
24 data: BTreeMap<ThreadId, Scalar<Tag>>,
25 dtor: Option<ty::Instance<'tcx>>,
29 pub struct TlsData<'tcx> {
30 /// The Key to use for the next thread-local allocation.
33 /// pthreads-style thread-local storage.
34 keys: BTreeMap<TlsKey, TlsEntry<'tcx>>,
36 /// A single global per thread dtor (that's how things work on macOS) with a data argument.
37 global_dtors: BTreeMap<ThreadId, (ty::Instance<'tcx>, Scalar<Tag>)>,
39 /// Whether we are in the "destruct" phase, during which some operations are UB.
40 dtors_running: HashSet<ThreadId>,
42 /// The last TlsKey used to retrieve a TLS destructor.
43 last_dtor_key: BTreeMap<ThreadId, TlsKey>,
46 impl<'tcx> Default for TlsData<'tcx> {
47 fn default() -> Self {
49 next_key: 1, // start with 1 as we must not use 0 on Windows
50 keys: Default::default(),
51 global_dtors: Default::default(),
52 dtors_running: Default::default(),
53 last_dtor_key: Default::default(),
58 impl<'tcx> TlsData<'tcx> {
59 /// Generate a new TLS key with the given destructor.
60 /// `max_size` determines the integer size the key has to fit in.
61 pub fn create_tls_key(&mut self, dtor: Option<ty::Instance<'tcx>>, max_size: Size) -> InterpResult<'tcx, TlsKey> {
62 let new_key = self.next_key;
64 self.keys.insert(new_key, TlsEntry { data: Default::default(), dtor }).unwrap_none();
65 trace!("New TLS key allocated: {} with dtor {:?}", new_key, dtor);
67 if max_size.bits() < 128 && new_key >= (1u128 << max_size.bits() as u128) {
68 throw_unsup_format!("we ran out of TLS key space");
73 pub fn delete_tls_key(&mut self, key: TlsKey) -> InterpResult<'tcx> {
74 match self.keys.remove(&key) {
76 trace!("TLS key {} removed", key);
79 None => throw_ub_format!("removing a non-existig TLS key: {}", key),
87 cx: &impl HasDataLayout,
88 ) -> InterpResult<'tcx, Scalar<Tag>> {
89 match self.keys.get(&key) {
90 Some(TlsEntry { data, .. }) => {
91 let value = data.get(&thread_id).cloned();
92 trace!("TLS key {} for thread {:?} loaded: {:?}", key, thread_id, value);
93 Ok(value.unwrap_or_else(|| Scalar::null_ptr(cx).into()))
95 None => throw_ub_format!("loading from a non-existing TLS key: {}", key),
101 key: TlsKey, thread_id: ThreadId, new_data: Option<Scalar<Tag>>) -> InterpResult<'tcx> {
102 match self.keys.get_mut(&key) {
103 Some(TlsEntry { data, .. }) => {
106 trace!("TLS key {} for thread {:?} stored: {:?}", key, thread_id, ptr);
107 data.insert(thread_id, ptr);
110 trace!("TLS key {} for thread {:?} removed", key, thread_id);
111 data.remove(&thread_id);
116 None => throw_ub_format!("storing to a non-existing TLS key: {}", key),
120 /// Set global dtor for the given thread. This function is used to implement
121 /// `_tlv_atexit` shim on MacOS.
123 /// Global destructors are available only on MacOS and (potentially
124 /// confusingly) they seem to be still per thread as can be guessed from the
125 /// following comment in the [`_tlv_atexit`
126 /// implementation](https://github.com/opensource-apple/dyld/blob/195030646877261f0c8c7ad8b001f52d6a26f514/src/threadLocalVariables.c#L389):
128 /// // NOTE: this does not need locks because it only operates on current thread data
129 pub fn set_thread_global_dtor(
132 dtor: ty::Instance<'tcx>,
134 ) -> InterpResult<'tcx> {
135 if self.dtors_running.contains(&thread) {
136 // UB, according to libstd docs.
137 throw_ub_format!("setting global destructor while destructors are already running");
139 if self.global_dtors.insert(thread, (dtor, data)).is_some() {
140 throw_unsup_format!("setting more than one global destructor for the same thread is not supported");
145 /// Returns a dtor, its argument and its index, if one is supposed to run.
146 /// `key` is the last dtors that was run; we return the *next* one after that.
148 /// An optional destructor function may be associated with each key value.
149 /// At thread exit, if a key value has a non-NULL destructor pointer,
150 /// and the thread has a non-NULL value associated with that key,
151 /// the value of the key is set to NULL, and then the function pointed
152 /// to is called with the previously associated value as its sole argument.
153 /// The order of destructor calls is unspecified if more than one destructor
154 /// exists for a thread when it exits.
156 /// If, after all the destructors have been called for all non-NULL values
157 /// with associated destructors, there are still some non-NULL values with
158 /// associated destructors, then the process is repeated.
159 /// If, after at least {PTHREAD_DESTRUCTOR_ITERATIONS} iterations of destructor
160 /// calls for outstanding non-NULL values, there are still some non-NULL values
161 /// with associated destructors, implementations may stop calling destructors,
162 /// or they may continue calling destructors until no non-NULL values with
163 /// associated destructors exist, even though this might result in an infinite loop.
168 ) -> Option<(ty::Instance<'tcx>, Scalar<Tag>, TlsKey)> {
169 use std::collections::Bound::*;
171 let thread_local = &mut self.keys;
172 let start = match key {
173 Some(key) => Excluded(key),
176 for (&key, TlsEntry { data, dtor }) in
177 thread_local.range_mut((start, Unbounded))
179 match data.entry(thread_id) {
180 Entry::Occupied(entry) => {
181 let data_scalar = entry.remove();
182 if let Some(dtor) = dtor {
183 let ret = Some((*dtor, data_scalar, key));
187 Entry::Vacant(_) => {}
194 impl<'mir, 'tcx: 'mir> EvalContextPrivExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
195 trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
196 /// Schedule TLS destructors for the main thread on Windows. The
197 /// implementation assumes that we do not support concurrency on Windows
199 fn schedule_windows_tls_dtors(&mut self) -> InterpResult<'tcx> {
200 let this = self.eval_context_mut();
201 let active_thread = this.get_active_thread()?;
202 assert_eq!(this.get_total_thread_count()?, 1, "concurrency on Windows not supported");
203 this.machine.tls.dtors_running.insert(active_thread);
204 // Windows has a special magic linker section that is run on certain events.
205 // Instead of searching for that section and supporting arbitrary hooks in there
206 // (that would be basically https://github.com/rust-lang/miri/issues/450),
207 // we specifically look up the static in libstd that we know is placed
209 let thread_callback = this.eval_path_scalar(&["std", "sys", "windows", "thread_local", "p_thread_callback"])?;
210 let thread_callback = this.memory.get_fn(thread_callback.not_undef()?)?.as_instance()?;
212 // The signature of this function is `unsafe extern "system" fn(h: c::LPVOID, dwReason: c::DWORD, pv: c::LPVOID)`.
213 let reason = this.eval_path_scalar(&["std", "sys", "windows", "c", "DLL_PROCESS_DETACH"])?;
214 let ret_place = MPlaceTy::dangling(this.machine.layouts.unit, this).into();
217 &[Scalar::null_ptr(this).into(), reason.into(), Scalar::null_ptr(this).into()],
219 StackPopCleanup::None { cleanup: true },
222 this.enable_thread(active_thread)?;
226 /// Schedule the MacOS global dtor to be executed.
228 /// Note: It is safe to call this function also on other Unixes.
229 fn schedule_macos_global_tls_dtors(&mut self) -> InterpResult<'tcx> {
230 let this = self.eval_context_mut();
231 let thread_id = this.get_active_thread()?;
232 // The macOS global dtor runs "before any TLS slots get freed", so do that first.
233 if let Some((instance, data)) = this.machine.tls.global_dtors.remove(&thread_id) {
234 trace!("Running global dtor {:?} on {:?} at {:?}", instance, data, thread_id);
236 let ret_place = MPlaceTy::dangling(this.machine.layouts.unit, this).into();
241 StackPopCleanup::None { cleanup: true },
244 // Enable the thread so that it steps through the destructor which
245 // we just scheduled. Since we deleted the destructor, it is
246 // guaranteed that we will schedule it again. The `dtors_running`
247 // flag will prevent the code from adding the destructor again.
248 this.enable_thread(thread_id)?;
253 /// Schedule a pthread TLS destructor.
254 fn schedule_pthread_tls_dtors(&mut self) -> InterpResult<'tcx> {
255 let this = self.eval_context_mut();
256 let active_thread = this.get_active_thread()?;
258 assert!(this.has_terminated(active_thread)?, "running TLS dtors for non-terminated thread");
259 // Fetch next dtor after `key`.
260 let last_key = this.machine.tls.last_dtor_key.get(&active_thread).cloned();
261 let dtor = match this.machine.tls.fetch_tls_dtor(last_key, active_thread) {
262 dtor @ Some(_) => dtor,
263 // We ran each dtor once, start over from the beginning.
265 this.machine.tls.fetch_tls_dtor(None, active_thread)
268 if let Some((instance, ptr, key)) = dtor {
269 this.machine.tls.last_dtor_key.insert(active_thread, key);
270 trace!("Running TLS dtor {:?} on {:?} at {:?}", instance, ptr, active_thread);
271 assert!(!this.is_null(ptr).unwrap(), "Data can't be NULL when dtor is called!");
273 let ret_place = MPlaceTy::dangling(this.machine.layouts.unit, this).into();
278 StackPopCleanup::None { cleanup: true },
281 this.enable_thread(active_thread)?;
284 this.machine.tls.last_dtor_key.remove(&active_thread);
290 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
291 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
293 /// Schedule an active thread's TLS destructor to run on the active thread.
294 /// Note that this function does not run the destructors itself, it just
295 /// schedules them one by one each time it is called.
297 /// FIXME: we do not support yet deallocation of thread local statics.
298 fn schedule_tls_dtors_for_active_thread(&mut self) -> InterpResult<'tcx> {
299 let this = self.eval_context_mut();
300 let active_thread = this.get_active_thread()?;
302 if this.tcx.sess.target.target.target_os == "windows" {
303 if !this.machine.tls.dtors_running.contains(&active_thread) {
304 this.machine.tls.dtors_running.insert(active_thread);
305 this.schedule_windows_tls_dtors()?;
308 this.machine.tls.dtors_running.insert(active_thread);
309 this.schedule_macos_global_tls_dtors()?;
310 this.schedule_pthread_tls_dtors()?;