10 StackPopCleanup, EvalContext,
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)>>;
21 pub trait EvalContextExt<'tcx> {
22 fn run_tls_dtors(&mut self) -> EvalResult<'tcx>;
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);
34 fn delete_tls_key(&mut self, key: TlsKey) -> EvalResult<'tcx> {
35 return match self.data.thread_local.remove(&key) {
37 trace!("TLS key {} removed", key);
40 None => Err(EvalError::TlsOutOfBounds)
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);
50 None => Err(EvalError::TlsOutOfBounds)
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);
61 None => Err(EvalError::TlsOutOfBounds)
65 /// Returns a dtor, its argument and its index, if one is supposed to run
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.
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),
89 for (&key, &mut TlsEntry { ref mut data, dtor }) in self.data.thread_local.range_mut((start, Unbounded)) {
91 if let Some(dtor) = dtor {
92 let ret = Some((dtor, *data, key));
93 *data = Pointer::null();
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(
116 StackPopCleanup::None,
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)?;
123 // step until out of stackframes
124 while self.step()? {}
126 dtor = match self.memory.fetch_tls_dtor(Some(key))? {
127 dtor @ Some(_) => dtor,
128 None => self.memory.fetch_tls_dtor(None)?,