]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/src/shims/tls.rs
Auto merge of #94487 - oli-obk:stable_hash_ty, r=fee1-dead
[rust.git] / src / tools / miri / src / shims / tls.rs
1 //! Implement thread-local storage.
2
3 use std::collections::btree_map::Entry as BTreeEntry;
4 use std::collections::hash_map::Entry as HashMapEntry;
5 use std::collections::BTreeMap;
6
7 use log::trace;
8
9 use rustc_data_structures::fx::FxHashMap;
10 use rustc_middle::ty;
11 use rustc_target::abi::{HasDataLayout, Size};
12 use rustc_target::spec::abi::Abi;
13
14 use crate::*;
15
16 pub type TlsKey = u128;
17
18 #[derive(Clone, Debug)]
19 pub struct TlsEntry<'tcx> {
20     /// The data for this key. None is used to represent NULL.
21     /// (We normalize this early to avoid having to do a NULL-ptr-test each time we access the data.)
22     data: BTreeMap<ThreadId, Scalar<Provenance>>,
23     dtor: Option<ty::Instance<'tcx>>,
24 }
25
26 #[derive(Clone, Debug)]
27 struct RunningDtorsState {
28     /// The last TlsKey used to retrieve a TLS destructor. `None` means that we
29     /// have not tried to retrieve a TLS destructor yet or that we already tried
30     /// all keys.
31     last_dtor_key: Option<TlsKey>,
32 }
33
34 #[derive(Debug)]
35 pub struct TlsData<'tcx> {
36     /// The Key to use for the next thread-local allocation.
37     next_key: TlsKey,
38
39     /// pthreads-style thread-local storage.
40     keys: BTreeMap<TlsKey, TlsEntry<'tcx>>,
41
42     /// A single per thread destructor of the thread local storage (that's how
43     /// things work on macOS) with a data argument.
44     macos_thread_dtors: BTreeMap<ThreadId, (ty::Instance<'tcx>, Scalar<Provenance>)>,
45
46     /// State for currently running TLS dtors. If this map contains a key for a
47     /// specific thread, it means that we are in the "destruct" phase, during
48     /// which some operations are UB.
49     dtors_running: FxHashMap<ThreadId, RunningDtorsState>,
50 }
51
52 impl<'tcx> Default for TlsData<'tcx> {
53     fn default() -> Self {
54         TlsData {
55             next_key: 1, // start with 1 as we must not use 0 on Windows
56             keys: Default::default(),
57             macos_thread_dtors: Default::default(),
58             dtors_running: Default::default(),
59         }
60     }
61 }
62
63 impl<'tcx> TlsData<'tcx> {
64     /// Generate a new TLS key with the given destructor.
65     /// `max_size` determines the integer size the key has to fit in.
66     #[allow(clippy::integer_arithmetic)]
67     pub fn create_tls_key(
68         &mut self,
69         dtor: Option<ty::Instance<'tcx>>,
70         max_size: Size,
71     ) -> InterpResult<'tcx, TlsKey> {
72         let new_key = self.next_key;
73         self.next_key += 1;
74         self.keys.try_insert(new_key, TlsEntry { data: Default::default(), dtor }).unwrap();
75         trace!("New TLS key allocated: {} with dtor {:?}", new_key, dtor);
76
77         if max_size.bits() < 128 && new_key >= (1u128 << max_size.bits()) {
78             throw_unsup_format!("we ran out of TLS key space");
79         }
80         Ok(new_key)
81     }
82
83     pub fn delete_tls_key(&mut self, key: TlsKey) -> InterpResult<'tcx> {
84         match self.keys.remove(&key) {
85             Some(_) => {
86                 trace!("TLS key {} removed", key);
87                 Ok(())
88             }
89             None => throw_ub_format!("removing a non-existig TLS key: {}", key),
90         }
91     }
92
93     pub fn load_tls(
94         &self,
95         key: TlsKey,
96         thread_id: ThreadId,
97         cx: &impl HasDataLayout,
98     ) -> InterpResult<'tcx, Scalar<Provenance>> {
99         match self.keys.get(&key) {
100             Some(TlsEntry { data, .. }) => {
101                 let value = data.get(&thread_id).copied();
102                 trace!("TLS key {} for thread {:?} loaded: {:?}", key, thread_id, value);
103                 Ok(value.unwrap_or_else(|| Scalar::null_ptr(cx)))
104             }
105             None => throw_ub_format!("loading from a non-existing TLS key: {}", key),
106         }
107     }
108
109     pub fn store_tls(
110         &mut self,
111         key: TlsKey,
112         thread_id: ThreadId,
113         new_data: Scalar<Provenance>,
114         cx: &impl HasDataLayout,
115     ) -> InterpResult<'tcx> {
116         match self.keys.get_mut(&key) {
117             Some(TlsEntry { data, .. }) => {
118                 if new_data.to_machine_usize(cx)? != 0 {
119                     trace!("TLS key {} for thread {:?} stored: {:?}", key, thread_id, new_data);
120                     data.insert(thread_id, new_data);
121                 } else {
122                     trace!("TLS key {} for thread {:?} removed", key, thread_id);
123                     data.remove(&thread_id);
124                 }
125                 Ok(())
126             }
127             None => throw_ub_format!("storing to a non-existing TLS key: {}", key),
128         }
129     }
130
131     /// Set the thread wide destructor of the thread local storage for the given
132     /// thread. This function is used to implement `_tlv_atexit` shim on MacOS.
133     ///
134     /// Thread wide dtors are available only on MacOS. There is one destructor
135     /// per thread as can be guessed from the following comment in the
136     /// [`_tlv_atexit`
137     /// implementation](https://github.com/opensource-apple/dyld/blob/195030646877261f0c8c7ad8b001f52d6a26f514/src/threadLocalVariables.c#L389):
138     ///
139     /// NOTE: this does not need locks because it only operates on current thread data
140     pub fn set_macos_thread_dtor(
141         &mut self,
142         thread: ThreadId,
143         dtor: ty::Instance<'tcx>,
144         data: Scalar<Provenance>,
145     ) -> InterpResult<'tcx> {
146         if self.dtors_running.contains_key(&thread) {
147             // UB, according to libstd docs.
148             throw_ub_format!(
149                 "setting thread's local storage destructor while destructors are already running"
150             );
151         }
152         if self.macos_thread_dtors.insert(thread, (dtor, data)).is_some() {
153             throw_unsup_format!(
154                 "setting more than one thread local storage destructor for the same thread is not supported"
155             );
156         }
157         Ok(())
158     }
159
160     /// Returns a dtor, its argument and its index, if one is supposed to run.
161     /// `key` is the last dtors that was run; we return the *next* one after that.
162     ///
163     /// An optional destructor function may be associated with each key value.
164     /// At thread exit, if a key value has a non-NULL destructor pointer,
165     /// and the thread has a non-NULL value associated with that key,
166     /// the value of the key is set to NULL, and then the function pointed
167     /// to is called with the previously associated value as its sole argument.
168     /// **The order of destructor calls is unspecified if more than one destructor
169     /// exists for a thread when it exits.**
170     ///
171     /// If, after all the destructors have been called for all non-NULL values
172     /// with associated destructors, there are still some non-NULL values with
173     /// associated destructors, then the process is repeated.
174     /// If, after at least {PTHREAD_DESTRUCTOR_ITERATIONS} iterations of destructor
175     /// calls for outstanding non-NULL values, there are still some non-NULL values
176     /// with associated destructors, implementations may stop calling destructors,
177     /// or they may continue calling destructors until no non-NULL values with
178     /// associated destructors exist, even though this might result in an infinite loop.
179     fn fetch_tls_dtor(
180         &mut self,
181         key: Option<TlsKey>,
182         thread_id: ThreadId,
183     ) -> Option<(ty::Instance<'tcx>, Scalar<Provenance>, TlsKey)> {
184         use std::ops::Bound::*;
185
186         let thread_local = &mut self.keys;
187         let start = match key {
188             Some(key) => Excluded(key),
189             None => Unbounded,
190         };
191         // We interpret the documentaion above (taken from POSIX) as saying that we need to iterate
192         // over all keys and run each destructor at least once before running any destructor a 2nd
193         // time. That's why we have `key` to indicate how far we got in the current iteration. If we
194         // return `None`, `schedule_next_pthread_tls_dtor` will re-try with `ket` set to `None` to
195         // start the next round.
196         // TODO: In the future, we might consider randomizing destructor order, but we still have to
197         // uphold this requirement.
198         for (&key, TlsEntry { data, dtor }) in thread_local.range_mut((start, Unbounded)) {
199             match data.entry(thread_id) {
200                 BTreeEntry::Occupied(entry) => {
201                     if let Some(dtor) = dtor {
202                         // Set TLS data to NULL, and call dtor with old value.
203                         let data_scalar = entry.remove();
204                         let ret = Some((*dtor, data_scalar, key));
205                         return ret;
206                     }
207                 }
208                 BTreeEntry::Vacant(_) => {}
209             }
210         }
211         None
212     }
213
214     /// Set that dtors are running for `thread`. It is guaranteed not to change
215     /// the existing values stored in `dtors_running` for this thread. Returns
216     /// `true` if dtors for `thread` are already running.
217     fn set_dtors_running_for_thread(&mut self, thread: ThreadId) -> bool {
218         match self.dtors_running.entry(thread) {
219             HashMapEntry::Occupied(_) => true,
220             HashMapEntry::Vacant(entry) => {
221                 // We cannot just do `self.dtors_running.insert` because that
222                 // would overwrite `last_dtor_key` with `None`.
223                 entry.insert(RunningDtorsState { last_dtor_key: None });
224                 false
225             }
226         }
227     }
228
229     /// Delete all TLS entries for the given thread. This function should be
230     /// called after all TLS destructors have already finished.
231     fn delete_all_thread_tls(&mut self, thread_id: ThreadId) {
232         for TlsEntry { data, .. } in self.keys.values_mut() {
233             data.remove(&thread_id);
234         }
235     }
236 }
237
238 impl VisitTags for TlsData<'_> {
239     fn visit_tags(&self, visit: &mut dyn FnMut(SbTag)) {
240         let TlsData { keys, macos_thread_dtors, next_key: _, dtors_running: _ } = self;
241
242         for scalar in keys.values().flat_map(|v| v.data.values()) {
243             scalar.visit_tags(visit);
244         }
245         for (_, scalar) in macos_thread_dtors.values() {
246             scalar.visit_tags(visit);
247         }
248     }
249 }
250
251 impl<'mir, 'tcx: 'mir> EvalContextPrivExt<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {}
252 trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
253     /// Schedule TLS destructors for Windows.
254     /// On windows, TLS destructors are managed by std.
255     fn schedule_windows_tls_dtors(&mut self) -> InterpResult<'tcx> {
256         let this = self.eval_context_mut();
257         let active_thread = this.get_active_thread();
258
259         // Windows has a special magic linker section that is run on certain events.
260         // Instead of searching for that section and supporting arbitrary hooks in there
261         // (that would be basically https://github.com/rust-lang/miri/issues/450),
262         // we specifically look up the static in libstd that we know is placed
263         // in that section.
264         if !this.have_module(&["std"]) {
265             // Looks like we are running in a `no_std` crate.
266             // That also means no TLS dtors callback to call.
267             return Ok(());
268         }
269         let thread_callback =
270             this.eval_windows("thread_local_key", "p_thread_callback")?.to_pointer(this)?;
271         let thread_callback = this.get_ptr_fn(thread_callback)?.as_instance()?;
272
273         // FIXME: Technically, the reason should be `DLL_PROCESS_DETACH` when the main thread exits
274         // but std treats both the same.
275         let reason = this.eval_windows("c", "DLL_THREAD_DETACH")?;
276
277         // The signature of this function is `unsafe extern "system" fn(h: c::LPVOID, dwReason: c::DWORD, pv: c::LPVOID)`.
278         // FIXME: `h` should be a handle to the current module and what `pv` should be is unknown
279         // but both are ignored by std
280         this.call_function(
281             thread_callback,
282             Abi::System { unwind: false },
283             &[Scalar::null_ptr(this).into(), reason.into(), Scalar::null_ptr(this).into()],
284             None,
285             StackPopCleanup::Root { cleanup: true },
286         )?;
287
288         this.enable_thread(active_thread);
289         Ok(())
290     }
291
292     /// Schedule the MacOS thread destructor of the thread local storage to be
293     /// executed. Returns `true` if scheduled.
294     ///
295     /// Note: It is safe to call this function also on other Unixes.
296     fn schedule_macos_tls_dtor(&mut self) -> InterpResult<'tcx, bool> {
297         let this = self.eval_context_mut();
298         let thread_id = this.get_active_thread();
299         if let Some((instance, data)) = this.machine.tls.macos_thread_dtors.remove(&thread_id) {
300             trace!("Running macos dtor {:?} on {:?} at {:?}", instance, data, thread_id);
301
302             this.call_function(
303                 instance,
304                 Abi::C { unwind: false },
305                 &[data.into()],
306                 None,
307                 StackPopCleanup::Root { cleanup: true },
308             )?;
309
310             // Enable the thread so that it steps through the destructor which
311             // we just scheduled. Since we deleted the destructor, it is
312             // guaranteed that we will schedule it again. The `dtors_running`
313             // flag will prevent the code from adding the destructor again.
314             this.enable_thread(thread_id);
315             Ok(true)
316         } else {
317             Ok(false)
318         }
319     }
320
321     /// Schedule a pthread TLS destructor. Returns `true` if found
322     /// a destructor to schedule, and `false` otherwise.
323     fn schedule_next_pthread_tls_dtor(&mut self) -> InterpResult<'tcx, bool> {
324         let this = self.eval_context_mut();
325         let active_thread = this.get_active_thread();
326
327         assert!(this.has_terminated(active_thread), "running TLS dtors for non-terminated thread");
328         // Fetch next dtor after `key`.
329         let last_key = this.machine.tls.dtors_running[&active_thread].last_dtor_key;
330         let dtor = match this.machine.tls.fetch_tls_dtor(last_key, active_thread) {
331             dtor @ Some(_) => dtor,
332             // We ran each dtor once, start over from the beginning.
333             None => this.machine.tls.fetch_tls_dtor(None, active_thread),
334         };
335         if let Some((instance, ptr, key)) = dtor {
336             this.machine.tls.dtors_running.get_mut(&active_thread).unwrap().last_dtor_key =
337                 Some(key);
338             trace!("Running TLS dtor {:?} on {:?} at {:?}", instance, ptr, active_thread);
339             assert!(
340                 !ptr.to_machine_usize(this).unwrap() != 0,
341                 "data can't be NULL when dtor is called!"
342             );
343
344             this.call_function(
345                 instance,
346                 Abi::C { unwind: false },
347                 &[ptr.into()],
348                 None,
349                 StackPopCleanup::Root { cleanup: true },
350             )?;
351
352             this.enable_thread(active_thread);
353             return Ok(true);
354         }
355         this.machine.tls.dtors_running.get_mut(&active_thread).unwrap().last_dtor_key = None;
356
357         Ok(false)
358     }
359 }
360
361 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {}
362 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
363     /// Schedule an active thread's TLS destructor to run on the active thread.
364     /// Note that this function does not run the destructors itself, it just
365     /// schedules them one by one each time it is called and reenables the
366     /// thread so that it can be executed normally by the main execution loop.
367     ///
368     /// Note: we consistently run TLS destructors for all threads, including the
369     /// main thread. However, it is not clear that we should run the TLS
370     /// destructors for the main thread. See issue:
371     /// <https://github.com/rust-lang/rust/issues/28129>.
372     fn schedule_next_tls_dtor_for_active_thread(&mut self) -> InterpResult<'tcx> {
373         let this = self.eval_context_mut();
374         let active_thread = this.get_active_thread();
375         trace!("schedule_next_tls_dtor_for_active_thread on thread {:?}", active_thread);
376
377         if !this.machine.tls.set_dtors_running_for_thread(active_thread) {
378             // This is the first time we got asked to schedule a destructor. The
379             // Windows schedule destructor function must be called exactly once,
380             // this is why it is in this block.
381             if this.tcx.sess.target.os == "windows" {
382                 // On Windows, we signal that the thread quit by starting the
383                 // relevant function, reenabling the thread, and going back to
384                 // the scheduler.
385                 this.schedule_windows_tls_dtors()?;
386                 return Ok(());
387             }
388         }
389         // The remaining dtors make some progress each time around the scheduler loop,
390         // until they return `false` to indicate that they are done.
391
392         // The macOS thread wide destructor runs "before any TLS slots get
393         // freed", so do that first.
394         if this.schedule_macos_tls_dtor()? {
395             // We have scheduled a MacOS dtor to run on the thread. Execute it
396             // to completion and come back here. Scheduling a destructor
397             // destroys it, so we will not enter this branch again.
398             return Ok(());
399         }
400         if this.schedule_next_pthread_tls_dtor()? {
401             // We have scheduled a pthread destructor and removed it from the
402             // destructors list. Run it to completion and come back here.
403             return Ok(());
404         }
405
406         // All dtors done!
407         this.machine.tls.delete_all_thread_tls(active_thread);
408         this.thread_terminated()?;
409
410         Ok(())
411     }
412 }