]> git.lizzy.rs Git - rust.git/blob - src/libstd/local_data.rs
Ignore tests broken by failing on ICE
[rust.git] / src / libstd / local_data.rs
1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 /*!
12
13 Task local data management
14
15 Allows storing arbitrary types inside task-local-storage (TLS), to be accessed
16 anywhere within a task, keyed by a global pointer parameterized over the type of
17 the TLS slot.  Useful for dynamic variables, singletons, and interfacing with
18 foreign code with bad callback interfaces.
19
20 To declare a new key for storing local data of a particular type, use the
21 `local_data_key!` macro. This macro will expand to a `static` item appropriately
22 named and annotated. This name is then passed to the functions in this module to
23 modify/read the slot specified by the key.
24
25 ```rust
26 use std::local_data;
27
28 local_data_key!(key_int: int)
29 local_data_key!(key_vector: ~[int])
30
31 local_data::set(key_int, 3);
32 local_data::get(key_int, |opt| assert_eq!(opt.map(|x| *x), Some(3)));
33
34 local_data::set(key_vector, ~[4]);
35 local_data::get(key_vector, |opt| assert_eq!(*opt.unwrap(), ~[4]));
36  ```
37
38 */
39
40 // Casting 'Arcane Sight' reveals an overwhelming aura of Transmutation
41 // magic.
42
43 use cast;
44 use iter::{Iterator};
45 use kinds::Send;
46 use mem::replace;
47 use option::{None, Option, Some};
48 use rt::task::{Task, LocalStorage};
49 use slice::{ImmutableVector, MutableVector};
50 use vec::Vec;
51
52 /**
53  * Indexes a task-local data slot. This pointer is used for comparison to
54  * differentiate keys from one another. The actual type `T` is not used anywhere
55  * as a member of this type, except that it is parameterized with it to define
56  * the type of each key's value.
57  *
58  * The value of each Key is of the singleton enum KeyValue. These also have the
59  * same name as `Key` and their purpose is to take up space in the programs data
60  * sections to ensure that each value of the `Key` type points to a unique
61  * location.
62  */
63 pub type Key<T> = &'static KeyValue<T>;
64
65 #[allow(missing_doc)]
66 pub enum KeyValue<T> { Key }
67
68 #[allow(missing_doc)]
69 trait LocalData {}
70 impl<T: 'static> LocalData for T {}
71
72 // The task-local-map stores all TLS information for the currently running task.
73 // It is stored as an owned pointer into the runtime, and it's only allocated
74 // when TLS is used for the first time. This map must be very carefully
75 // constructed because it has many mutable loans unsoundly handed out on it to
76 // the various invocations of TLS requests.
77 //
78 // One of the most important operations is loaning a value via `get` to a
79 // caller. In doing so, the slot that the TLS entry is occupying cannot be
80 // invalidated because upon returning its loan state must be updated. Currently
81 // the TLS map is a vector, but this is possibly dangerous because the vector
82 // can be reallocated/moved when new values are pushed onto it.
83 //
84 // This problem currently isn't solved in a very elegant way. Inside the `get`
85 // function, it internally "invalidates" all references after the loan is
86 // finished and looks up into the vector again. In theory this will prevent
87 // pointers from being moved under our feet so long as LLVM doesn't go too crazy
88 // with the optimizations.
89 //
90 // n.b. If TLS is used heavily in future, this could be made more efficient with
91 //      a proper map.
92 #[doc(hidden)]
93 pub type Map = Vec<Option<(*u8, TLSValue, LoanState)>>;
94 type TLSValue = ~LocalData:Send;
95
96 // Gets the map from the runtime. Lazily initialises if not done so already.
97 unsafe fn get_local_map() -> &mut Map {
98     use rt::local::Local;
99
100     let task: *mut Task = Local::unsafe_borrow();
101     match &mut (*task).storage {
102         // If the at_exit function is already set, then we just need to take
103         // a loan out on the TLS map stored inside
104         &LocalStorage(Some(ref mut map_ptr)) => {
105             return map_ptr;
106         }
107         // If this is the first time we've accessed TLS, perform similar
108         // actions to the oldsched way of doing things.
109         &LocalStorage(ref mut slot) => {
110             *slot = Some(vec!());
111             match *slot {
112                 Some(ref mut map_ptr) => { return map_ptr }
113                 None => abort()
114             }
115         }
116     }
117 }
118
119 #[deriving(Eq)]
120 enum LoanState {
121     NoLoan, ImmLoan, MutLoan
122 }
123
124 impl LoanState {
125     fn describe(&self) -> &'static str {
126         match *self {
127             NoLoan => "no loan",
128             ImmLoan => "immutable",
129             MutLoan => "mutable"
130         }
131     }
132 }
133
134 fn key_to_key_value<T: 'static>(key: Key<T>) -> *u8 {
135     unsafe { cast::transmute(key) }
136 }
137
138 /// Removes a task-local value from task-local storage. This will return
139 /// Some(value) if the key was present in TLS, otherwise it will return None.
140 ///
141 /// A runtime assertion will be triggered it removal of TLS value is attempted
142 /// while the value is still loaned out via `get` or `get_mut`.
143 pub fn pop<T: 'static>(key: Key<T>) -> Option<T> {
144     let map = unsafe { get_local_map() };
145     let key_value = key_to_key_value(key);
146
147     for entry in map.mut_iter() {
148         match *entry {
149             Some((k, _, loan)) if k == key_value => {
150                 if loan != NoLoan {
151                     fail!("TLS value cannot be removed because it is currently \
152                           borrowed as {}", loan.describe());
153                 }
154                 // Move the data out of the `entry` slot via prelude::replace.
155                 // This is guaranteed to succeed because we already matched
156                 // on `Some` above.
157                 let data = match replace(entry, None) {
158                     Some((_, data, _)) => data,
159                     None => abort()
160                 };
161
162                 // Move `data` into transmute to get out the memory that it
163                 // owns, we must free it manually later.
164                 let (_vtable, alloc): (uint, ~T) = unsafe {
165                     cast::transmute(data)
166                 };
167
168                 // Now that we own `alloc`, we can just move out of it as we
169                 // would with any other data.
170                 return Some(*alloc);
171             }
172             _ => {}
173         }
174     }
175     return None;
176 }
177
178 /// Retrieves a value from TLS. The closure provided is yielded `Some` of a
179 /// reference to the value located in TLS if one exists, or `None` if the key
180 /// provided is not present in TLS currently.
181 ///
182 /// It is considered a runtime error to attempt to get a value which is already
183 /// on loan via the `get_mut` method provided.
184 pub fn get<T: 'static, U>(key: Key<T>, f: |Option<&T>| -> U) -> U {
185     get_with(key, ImmLoan, f)
186 }
187
188 /// Retrieves a mutable value from TLS. The closure provided is yielded `Some`
189 /// of a reference to the mutable value located in TLS if one exists, or `None`
190 /// if the key provided is not present in TLS currently.
191 ///
192 /// It is considered a runtime error to attempt to get a value which is already
193 /// on loan via this or the `get` methods.
194 pub fn get_mut<T: 'static, U>(key: Key<T>, f: |Option<&mut T>| -> U) -> U {
195     get_with(key, MutLoan, |x| {
196         match x {
197             None => f(None),
198             // We're violating a lot of compiler guarantees with this
199             // invocation of `transmute`, but we're doing runtime checks to
200             // ensure that it's always valid (only one at a time).
201             //
202             // there is no need to be upset!
203             Some(x) => { f(Some(unsafe { cast::transmute::<&_, &mut _>(x) })) }
204         }
205     })
206 }
207
208 fn get_with<T:'static,
209             U>(
210             key: Key<T>,
211             state: LoanState,
212             f: |Option<&T>| -> U)
213             -> U {
214     // This function must be extremely careful. Because TLS can store owned
215     // values, and we must have some form of `get` function other than `pop`,
216     // this function has to give a `&` reference back to the caller.
217     //
218     // One option is to return the reference, but this cannot be sound because
219     // the actual lifetime of the object is not known. The slot in TLS could not
220     // be modified until the object goes out of scope, but the TLS code cannot
221     // know when this happens.
222     //
223     // For this reason, the reference is yielded to a specified closure. This
224     // way the TLS code knows exactly what the lifetime of the yielded pointer
225     // is, allowing callers to acquire references to owned data. This is also
226     // sound so long as measures are taken to ensure that while a TLS slot is
227     // loaned out to a caller, it's not modified recursively.
228     let map = unsafe { get_local_map() };
229     let key_value = key_to_key_value(key);
230
231     let pos = map.iter().position(|entry| {
232         match *entry {
233             Some((k, _, _)) if k == key_value => true, _ => false
234         }
235     });
236     match pos {
237         None => { return f(None); }
238         Some(i) => {
239             let ret;
240             let mut return_loan = false;
241             match *map.get_mut(i) {
242                 Some((_, ref data, ref mut loan)) => {
243                     match (state, *loan) {
244                         (_, NoLoan) => {
245                             *loan = state;
246                             return_loan = true;
247                         }
248                         (ImmLoan, ImmLoan) => {}
249                         (want, cur) => {
250                             fail!("TLS slot cannot be borrowed as {} because \
251                                     it is already borrowed as {}",
252                                   want.describe(), cur.describe());
253                         }
254                     }
255                     // data was created with `~T as ~LocalData`, so we extract
256                     // pointer part of the trait, (as ~T), and then use
257                     // compiler coercions to achieve a '&' pointer.
258                     unsafe {
259                         match *cast::transmute::<&TLSValue, &(uint, ~T)>(data){
260                             (_vtable, ref alloc) => {
261                                 let value: &T = *alloc;
262                                 ret = f(Some(value));
263                             }
264                         }
265                     }
266                 }
267                 _ => abort()
268             }
269
270             // n.b. 'data' and 'loans' are both invalid pointers at the point
271             // 'f' returned because `f` could have appended more TLS items which
272             // in turn relocated the vector. Hence we do another lookup here to
273             // fixup the loans.
274             if return_loan {
275                 match *map.get_mut(i) {
276                     Some((_, _, ref mut loan)) => { *loan = NoLoan; }
277                     None => abort()
278                 }
279             }
280             return ret;
281         }
282     }
283 }
284
285 fn abort() -> ! {
286     use intrinsics;
287     unsafe { intrinsics::abort() }
288 }
289
290 /// Inserts a value into task local storage. If the key is already present in
291 /// TLS, then the previous value is removed and replaced with the provided data.
292 ///
293 /// It is considered a runtime error to attempt to set a key which is currently
294 /// on loan via the `get` or `get_mut` methods.
295 pub fn set<T: 'static>(key: Key<T>, data: T) {
296     let map = unsafe { get_local_map() };
297     let keyval = key_to_key_value(key);
298
299     // When the task-local map is destroyed, all the data needs to be cleaned
300     // up. For this reason we can't do some clever tricks to store '~T' as a
301     // '*c_void' or something like that. To solve the problem, we cast
302     // everything to a trait (LocalData) which is then stored inside the map.
303     // Upon destruction of the map, all the objects will be destroyed and the
304     // traits have enough information about them to destroy themselves.
305     let data = box data as ~LocalData:;
306
307     fn insertion_position(map: &mut Map,
308                           key: *u8) -> Option<uint> {
309         // First see if the map contains this key already
310         let curspot = map.iter().position(|entry| {
311             match *entry {
312                 Some((ekey, _, loan)) if key == ekey => {
313                     if loan != NoLoan {
314                         fail!("TLS value cannot be overwritten because it is
315                                already borrowed as {}", loan.describe())
316                     }
317                     true
318                 }
319                 _ => false,
320             }
321         });
322         // If it doesn't contain the key, just find a slot that's None
323         match curspot {
324             Some(i) => Some(i),
325             None => map.iter().position(|entry| entry.is_none())
326         }
327     }
328
329     // The type of the local data map must ascribe to Send, so we do the
330     // transmute here to add the Send bound back on. This doesn't actually
331     // matter because TLS will always own the data (until its moved out) and
332     // we're not actually sending it to other schedulers or anything.
333     let data: ~LocalData:Send = unsafe { cast::transmute(data) };
334     match insertion_position(map, keyval) {
335         Some(i) => { *map.get_mut(i) = Some((keyval, data, NoLoan)); }
336         None => { map.push(Some((keyval, data, NoLoan))); }
337     }
338 }
339
340 /// Modifies a task-local value by temporarily removing it from task-local
341 /// storage and then re-inserting if `Some` is returned from the closure.
342 ///
343 /// This function will have the same runtime errors as generated from `pop` and
344 /// `set` (the key must not currently be on loan
345 pub fn modify<T: 'static>(key: Key<T>, f: |Option<T>| -> Option<T>) {
346     match f(pop(key)) {
347         Some(next) => { set(key, next); }
348         None => {}
349     }
350 }
351
352 #[cfg(test)]
353 mod tests {
354     use prelude::*;
355     use super::*;
356     use task;
357     use str::StrSlice;
358
359     #[test]
360     fn test_tls_multitask() {
361         static my_key: Key<~str> = &Key;
362         set(my_key, "parent data".to_owned());
363         task::spawn(proc() {
364             // TLS shouldn't carry over.
365             assert!(get(my_key, |k| k.map(|k| (*k).clone())).is_none());
366             set(my_key, "child data".to_owned());
367             assert!(get(my_key, |k| k.map(|k| (*k).clone())).unwrap() ==
368                     "child data".to_owned());
369             // should be cleaned up for us
370         });
371         // Must work multiple times
372         assert!(get(my_key, |k| k.map(|k| (*k).clone())).unwrap() == "parent data".to_owned());
373         assert!(get(my_key, |k| k.map(|k| (*k).clone())).unwrap() == "parent data".to_owned());
374         assert!(get(my_key, |k| k.map(|k| (*k).clone())).unwrap() == "parent data".to_owned());
375     }
376
377     #[test]
378     fn test_tls_overwrite() {
379         static my_key: Key<~str> = &Key;
380         set(my_key, "first data".to_owned());
381         set(my_key, "next data".to_owned()); // Shouldn't leak.
382         assert!(get(my_key, |k| k.map(|k| (*k).clone())).unwrap() == "next data".to_owned());
383     }
384
385     #[test]
386     fn test_tls_pop() {
387         static my_key: Key<~str> = &Key;
388         set(my_key, "weasel".to_owned());
389         assert!(pop(my_key).unwrap() == "weasel".to_owned());
390         // Pop must remove the data from the map.
391         assert!(pop(my_key).is_none());
392     }
393
394     #[test]
395     fn test_tls_modify() {
396         static my_key: Key<~str> = &Key;
397         modify(my_key, |data| {
398             match data {
399                 Some(ref val) => fail!("unwelcome value: {}", *val),
400                 None           => Some("first data".to_owned())
401             }
402         });
403         modify(my_key, |data| {
404             match data.as_ref().map(|s| s.as_slice()) {
405                 Some("first data") => Some("next data".to_owned()),
406                 Some(ref val)       => fail!("wrong value: {}", *val),
407                 None                 => fail!("missing value")
408             }
409         });
410         assert!(pop(my_key).unwrap() == "next data".to_owned());
411     }
412
413     #[test]
414     fn test_tls_crust_automorestack_memorial_bug() {
415         // This might result in a stack-canary clobber if the runtime fails to
416         // set sp_limit to 0 when calling the cleanup extern - it might
417         // automatically jump over to the rust stack, which causes next_c_sp
418         // to get recorded as something within a rust stack segment. Then a
419         // subsequent upcall (esp. for logging, think vsnprintf) would run on
420         // a stack smaller than 1 MB.
421         static my_key: Key<~str> = &Key;
422         task::spawn(proc() {
423             set(my_key, "hax".to_owned());
424         });
425     }
426
427     #[test]
428     fn test_tls_multiple_types() {
429         static str_key: Key<~str> = &Key;
430         static box_key: Key<@()> = &Key;
431         static int_key: Key<int> = &Key;
432         task::spawn(proc() {
433             set(str_key, "string data".to_owned());
434             set(box_key, @());
435             set(int_key, 42);
436         });
437     }
438
439     #[test]
440     #[allow(dead_code)]
441     fn test_tls_overwrite_multiple_types() {
442         static str_key: Key<~str> = &Key;
443         static box_key: Key<@()> = &Key;
444         static int_key: Key<int> = &Key;
445         task::spawn(proc() {
446             set(str_key, "string data".to_owned());
447             set(str_key, "string data 2".to_owned());
448             set(box_key, @());
449             set(box_key, @());
450             set(int_key, 42);
451             // This could cause a segfault if overwriting-destruction is done
452             // with the crazy polymorphic transmute rather than the provided
453             // finaliser.
454             set(int_key, 31337);
455         });
456     }
457
458     #[test]
459     #[should_fail]
460     fn test_tls_cleanup_on_failure() {
461         static str_key: Key<~str> = &Key;
462         static box_key: Key<@()> = &Key;
463         static int_key: Key<int> = &Key;
464         set(str_key, "parent data".to_owned());
465         set(box_key, @());
466         task::spawn(proc() {
467             // spawn_linked
468             set(str_key, "string data".to_owned());
469             set(box_key, @());
470             set(int_key, 42);
471             fail!();
472         });
473         // Not quite nondeterministic.
474         set(int_key, 31337);
475         fail!();
476     }
477
478     #[test]
479     fn test_static_pointer() {
480         static key: Key<&'static int> = &Key;
481         static VALUE: int = 0;
482         let v: &'static int = &VALUE;
483         set(key, v);
484     }
485
486     #[test]
487     fn test_owned() {
488         static key: Key<~int> = &Key;
489         set(key, box 1);
490
491         get(key, |v| {
492             get(key, |v| {
493                 get(key, |v| {
494                     assert_eq!(**v.unwrap(), 1);
495                 });
496                 assert_eq!(**v.unwrap(), 1);
497             });
498             assert_eq!(**v.unwrap(), 1);
499         });
500         set(key, box 2);
501         get(key, |v| {
502             assert_eq!(**v.unwrap(), 2);
503         })
504     }
505
506     #[test]
507     fn test_get_mut() {
508         static key: Key<int> = &Key;
509         set(key, 1);
510
511         get_mut(key, |v| {
512             *v.unwrap() = 2;
513         });
514
515         get(key, |v| {
516             assert_eq!(*v.unwrap(), 2);
517         })
518     }
519
520     #[test]
521     fn test_same_key_type() {
522         static key1: Key<int> = &Key;
523         static key2: Key<int> = &Key;
524         static key3: Key<int> = &Key;
525         static key4: Key<int> = &Key;
526         static key5: Key<int> = &Key;
527         set(key1, 1);
528         set(key2, 2);
529         set(key3, 3);
530         set(key4, 4);
531         set(key5, 5);
532
533         get(key1, |x| assert_eq!(*x.unwrap(), 1));
534         get(key2, |x| assert_eq!(*x.unwrap(), 2));
535         get(key3, |x| assert_eq!(*x.unwrap(), 3));
536         get(key4, |x| assert_eq!(*x.unwrap(), 4));
537         get(key5, |x| assert_eq!(*x.unwrap(), 5));
538     }
539
540     #[test]
541     #[should_fail]
542     fn test_nested_get_set1() {
543         static key: Key<int> = &Key;
544         set(key, 4);
545         get(key, |_| {
546             set(key, 4);
547         })
548     }
549
550     #[test]
551     #[should_fail]
552     fn test_nested_get_mut2() {
553         static key: Key<int> = &Key;
554         set(key, 4);
555         get(key, |_| {
556             get_mut(key, |_| {})
557         })
558     }
559
560     #[test]
561     #[should_fail]
562     fn test_nested_get_mut3() {
563         static key: Key<int> = &Key;
564         set(key, 4);
565         get_mut(key, |_| {
566             get(key, |_| {})
567         })
568     }
569
570     #[test]
571     #[should_fail]
572     fn test_nested_get_mut4() {
573         static key: Key<int> = &Key;
574         set(key, 4);
575         get_mut(key, |_| {
576             get_mut(key, |_| {})
577         })
578     }
579 }