]> git.lizzy.rs Git - rust.git/blob - miri/tls.rs
Address comments
[rust.git] / miri / tls.rs
1 use rustc::{ty, mir};
2
3 use super::{
4     TlsKey, TlsEntry,
5     EvalResult, EvalError,
6     Pointer,
7     Memory,
8     Evaluator,
9     Lvalue,
10     StackPopCleanup, EvalContext,
11 };
12
13 pub trait MemoryExt<'tcx> {
14     fn create_tls_key(&mut self, dtor: Option<ty::Instance<'tcx>>) -> TlsKey;
15     fn delete_tls_key(&mut self, key: TlsKey) -> EvalResult<'tcx>;
16     fn load_tls(&mut self, key: TlsKey) -> EvalResult<'tcx, Pointer>;
17     fn store_tls(&mut self, key: TlsKey, new_data: Pointer) -> EvalResult<'tcx>;
18     fn fetch_tls_dtor(&mut self, key: Option<TlsKey>) -> EvalResult<'tcx, Option<(ty::Instance<'tcx>, Pointer, TlsKey)>>;
19 }
20
21 pub trait EvalContextExt<'tcx> {
22     fn run_tls_dtors(&mut self) -> EvalResult<'tcx>;
23 }
24
25 impl<'a, 'tcx: 'a> MemoryExt<'tcx> for Memory<'a, 'tcx, Evaluator> {
26     fn create_tls_key(&mut self, dtor: Option<ty::Instance<'tcx>>) -> TlsKey {
27         let new_key = self.data.next_thread_local;
28         self.data.next_thread_local += 1;
29         self.data.thread_local.insert(new_key, TlsEntry { data: Pointer::null(), dtor });
30         trace!("New TLS key allocated: {} with dtor {:?}", new_key, dtor);
31         return new_key;
32     }
33
34     fn delete_tls_key(&mut self, key: TlsKey) -> EvalResult<'tcx> {
35         return match self.data.thread_local.remove(&key) {
36             Some(_) => {
37                 trace!("TLS key {} removed", key);
38                 Ok(())
39             },
40             None => Err(EvalError::TlsOutOfBounds)
41         }
42     }
43
44     fn load_tls(&mut self, key: TlsKey) -> EvalResult<'tcx, Pointer> {
45         return match self.data.thread_local.get(&key) {
46             Some(&TlsEntry { data, .. }) => {
47                 trace!("TLS key {} loaded: {:?}", key, data);
48                 Ok(data)
49             },
50             None => Err(EvalError::TlsOutOfBounds)
51         }
52     }
53
54     fn store_tls(&mut self, key: TlsKey, new_data: Pointer) -> EvalResult<'tcx> {
55         return match self.data.thread_local.get_mut(&key) {
56             Some(&mut TlsEntry { ref mut data, .. }) => {
57                 trace!("TLS key {} stored: {:?}", key, new_data);
58                 *data = new_data;
59                 Ok(())
60             },
61             None => Err(EvalError::TlsOutOfBounds)
62         }
63     }
64     
65     /// Returns a dtor, its argument and its index, if one is supposed to run
66     ///
67     /// An optional destructor function may be associated with each key value.
68     /// At thread exit, if a key value has a non-NULL destructor pointer,
69     /// and the thread has a non-NULL value associated with that key,
70     /// the value of the key is set to NULL, and then the function pointed
71     /// to is called with the previously associated value as its sole argument.
72     /// The order of destructor calls is unspecified if more than one destructor
73     /// exists for a thread when it exits.
74     ///
75     /// If, after all the destructors have been called for all non-NULL values
76     /// with associated destructors, there are still some non-NULL values with
77     /// associated destructors, then the process is repeated.
78     /// If, after at least {PTHREAD_DESTRUCTOR_ITERATIONS} iterations of destructor
79     /// calls for outstanding non-NULL values, there are still some non-NULL values
80     /// with associated destructors, implementations may stop calling destructors,
81     /// or they may continue calling destructors until no non-NULL values with
82     /// associated destructors exist, even though this might result in an infinite loop.
83     fn fetch_tls_dtor(&mut self, key: Option<TlsKey>) -> EvalResult<'tcx, Option<(ty::Instance<'tcx>, Pointer, TlsKey)>> {
84         use std::collections::Bound::*;
85         let start = match key {
86             Some(key) => Excluded(key),
87             None => Unbounded,
88         };
89         for (&key, &mut TlsEntry { ref mut data, dtor }) in self.data.thread_local.range_mut((start, Unbounded)) {
90             if !data.is_null()? {
91                 if let Some(dtor) = dtor {
92                     let ret = Some((dtor, *data, key));
93                     *data = Pointer::null();
94                     return Ok(ret);
95                 }
96             }
97         }
98         return Ok(None);
99     }
100 }
101
102 impl<'a, 'tcx: 'a> EvalContextExt<'tcx> for EvalContext<'a, 'tcx, Evaluator> {
103     fn run_tls_dtors(&mut self) -> EvalResult<'tcx> {
104         let mut dtor = self.memory.fetch_tls_dtor(None)?;
105         // FIXME: replace loop by some structure that works with stepping
106         while let Some((instance, ptr, key)) = dtor {
107             trace!("Running TLS dtor {:?} on {:?}", instance, ptr);
108             // TODO: Potentially, this has to support all the other possible instances?
109             // See eval_fn_call in interpret/terminator/mod.rs
110             let mir = self.load_mir(instance.def)?;
111             self.push_stack_frame(
112                 instance,
113                 mir.span,
114                 mir,
115                 Lvalue::undef(),
116                 StackPopCleanup::None,
117             )?;
118             let arg_local = self.frame().mir.args_iter().next().ok_or(EvalError::AbiViolation("TLS dtor does not take enough arguments.".to_owned()))?;
119             let dest = self.eval_lvalue(&mir::Lvalue::Local(arg_local))?;
120             let ty = self.tcx.mk_mut_ptr(self.tcx.types.u8);
121             self.write_ptr(dest, ptr, ty)?;
122
123             // step until out of stackframes
124             while self.step()? {}
125
126             dtor = match self.memory.fetch_tls_dtor(Some(key))? {
127                 dtor @ Some(_) => dtor,
128                 None => self.memory.fetch_tls_dtor(None)?,
129             };
130         }
131         Ok(())
132     }
133 }