Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | // PLEASE READ: Do you really need a singleton? |
michael@0 | 6 | // |
michael@0 | 7 | // Singletons make it hard to determine the lifetime of an object, which can |
michael@0 | 8 | // lead to buggy code and spurious crashes. |
michael@0 | 9 | // |
michael@0 | 10 | // Instead of adding another singleton into the mix, try to identify either: |
michael@0 | 11 | // a) An existing singleton that can manage your object's lifetime |
michael@0 | 12 | // b) Locations where you can deterministically create the object and pass |
michael@0 | 13 | // into other objects |
michael@0 | 14 | // |
michael@0 | 15 | // If you absolutely need a singleton, please keep them as trivial as possible |
michael@0 | 16 | // and ideally a leaf dependency. Singletons get problematic when they attempt |
michael@0 | 17 | // to do too much in their destructor or have circular dependencies. |
michael@0 | 18 | |
michael@0 | 19 | #ifndef BASE_MEMORY_SINGLETON_H_ |
michael@0 | 20 | #define BASE_MEMORY_SINGLETON_H_ |
michael@0 | 21 | |
michael@0 | 22 | #include "base/at_exit.h" |
michael@0 | 23 | #include "base/atomicops.h" |
michael@0 | 24 | #include "base/base_export.h" |
michael@0 | 25 | #include "base/memory/aligned_memory.h" |
michael@0 | 26 | #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
michael@0 | 27 | #include "base/threading/thread_restrictions.h" |
michael@0 | 28 | |
michael@0 | 29 | namespace base { |
michael@0 | 30 | namespace internal { |
michael@0 | 31 | |
michael@0 | 32 | // Our AtomicWord doubles as a spinlock, where a value of |
michael@0 | 33 | // kBeingCreatedMarker means the spinlock is being held for creation. |
michael@0 | 34 | static const subtle::AtomicWord kBeingCreatedMarker = 1; |
michael@0 | 35 | |
michael@0 | 36 | // We pull out some of the functionality into a non-templated function, so that |
michael@0 | 37 | // we can implement the more complicated pieces out of line in the .cc file. |
michael@0 | 38 | BASE_EXPORT subtle::AtomicWord WaitForInstance(subtle::AtomicWord* instance); |
michael@0 | 39 | |
michael@0 | 40 | } // namespace internal |
michael@0 | 41 | } // namespace base |
michael@0 | 42 | |
michael@0 | 43 | // TODO(joth): Move more of this file into namespace base |
michael@0 | 44 | |
michael@0 | 45 | // Default traits for Singleton<Type>. Calls operator new and operator delete on |
michael@0 | 46 | // the object. Registers automatic deletion at process exit. |
michael@0 | 47 | // Overload if you need arguments or another memory allocation function. |
michael@0 | 48 | template<typename Type> |
michael@0 | 49 | struct DefaultSingletonTraits { |
michael@0 | 50 | // Allocates the object. |
michael@0 | 51 | static Type* New() { |
michael@0 | 52 | // The parenthesis is very important here; it forces POD type |
michael@0 | 53 | // initialization. |
michael@0 | 54 | return new Type(); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | // Destroys the object. |
michael@0 | 58 | static void Delete(Type* x) { |
michael@0 | 59 | delete x; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | // Set to true to automatically register deletion of the object on process |
michael@0 | 63 | // exit. See below for the required call that makes this happen. |
michael@0 | 64 | static const bool kRegisterAtExit = true; |
michael@0 | 65 | |
michael@0 | 66 | // Set to false to disallow access on a non-joinable thread. This is |
michael@0 | 67 | // different from kRegisterAtExit because StaticMemorySingletonTraits allows |
michael@0 | 68 | // access on non-joinable threads, and gracefully handles this. |
michael@0 | 69 | static const bool kAllowedToAccessOnNonjoinableThread = false; |
michael@0 | 70 | }; |
michael@0 | 71 | |
michael@0 | 72 | |
michael@0 | 73 | // Alternate traits for use with the Singleton<Type>. Identical to |
michael@0 | 74 | // DefaultSingletonTraits except that the Singleton will not be cleaned up |
michael@0 | 75 | // at exit. |
michael@0 | 76 | template<typename Type> |
michael@0 | 77 | struct LeakySingletonTraits : public DefaultSingletonTraits<Type> { |
michael@0 | 78 | static const bool kRegisterAtExit = false; |
michael@0 | 79 | static const bool kAllowedToAccessOnNonjoinableThread = true; |
michael@0 | 80 | }; |
michael@0 | 81 | |
michael@0 | 82 | |
michael@0 | 83 | // Alternate traits for use with the Singleton<Type>. Allocates memory |
michael@0 | 84 | // for the singleton instance from a static buffer. The singleton will |
michael@0 | 85 | // be cleaned up at exit, but can't be revived after destruction unless |
michael@0 | 86 | // the Resurrect() method is called. |
michael@0 | 87 | // |
michael@0 | 88 | // This is useful for a certain category of things, notably logging and |
michael@0 | 89 | // tracing, where the singleton instance is of a type carefully constructed to |
michael@0 | 90 | // be safe to access post-destruction. |
michael@0 | 91 | // In logging and tracing you'll typically get stray calls at odd times, like |
michael@0 | 92 | // during static destruction, thread teardown and the like, and there's a |
michael@0 | 93 | // termination race on the heap-based singleton - e.g. if one thread calls |
michael@0 | 94 | // get(), but then another thread initiates AtExit processing, the first thread |
michael@0 | 95 | // may call into an object residing in unallocated memory. If the instance is |
michael@0 | 96 | // allocated from the data segment, then this is survivable. |
michael@0 | 97 | // |
michael@0 | 98 | // The destructor is to deallocate system resources, in this case to unregister |
michael@0 | 99 | // a callback the system will invoke when logging levels change. Note that |
michael@0 | 100 | // this is also used in e.g. Chrome Frame, where you have to allow for the |
michael@0 | 101 | // possibility of loading briefly into someone else's process space, and |
michael@0 | 102 | // so leaking is not an option, as that would sabotage the state of your host |
michael@0 | 103 | // process once you've unloaded. |
michael@0 | 104 | template <typename Type> |
michael@0 | 105 | struct StaticMemorySingletonTraits { |
michael@0 | 106 | // WARNING: User has to deal with get() in the singleton class |
michael@0 | 107 | // this is traits for returning NULL. |
michael@0 | 108 | static Type* New() { |
michael@0 | 109 | // Only constructs once and returns pointer; otherwise returns NULL. |
michael@0 | 110 | if (base::subtle::NoBarrier_AtomicExchange(&dead_, 1)) |
michael@0 | 111 | return NULL; |
michael@0 | 112 | |
michael@0 | 113 | return new(buffer_.void_data()) Type(); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | static void Delete(Type* p) { |
michael@0 | 117 | if (p != NULL) |
michael@0 | 118 | p->Type::~Type(); |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | static const bool kRegisterAtExit = true; |
michael@0 | 122 | static const bool kAllowedToAccessOnNonjoinableThread = true; |
michael@0 | 123 | |
michael@0 | 124 | // Exposed for unittesting. |
michael@0 | 125 | static void Resurrect() { |
michael@0 | 126 | base::subtle::NoBarrier_Store(&dead_, 0); |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | private: |
michael@0 | 130 | static base::AlignedMemory<sizeof(Type), ALIGNOF(Type)> buffer_; |
michael@0 | 131 | // Signal the object was already deleted, so it is not revived. |
michael@0 | 132 | static base::subtle::Atomic32 dead_; |
michael@0 | 133 | }; |
michael@0 | 134 | |
michael@0 | 135 | template <typename Type> base::AlignedMemory<sizeof(Type), ALIGNOF(Type)> |
michael@0 | 136 | StaticMemorySingletonTraits<Type>::buffer_; |
michael@0 | 137 | template <typename Type> base::subtle::Atomic32 |
michael@0 | 138 | StaticMemorySingletonTraits<Type>::dead_ = 0; |
michael@0 | 139 | |
michael@0 | 140 | // The Singleton<Type, Traits, DifferentiatingType> class manages a single |
michael@0 | 141 | // instance of Type which will be created on first use and will be destroyed at |
michael@0 | 142 | // normal process exit). The Trait::Delete function will not be called on |
michael@0 | 143 | // abnormal process exit. |
michael@0 | 144 | // |
michael@0 | 145 | // DifferentiatingType is used as a key to differentiate two different |
michael@0 | 146 | // singletons having the same memory allocation functions but serving a |
michael@0 | 147 | // different purpose. This is mainly used for Locks serving different purposes. |
michael@0 | 148 | // |
michael@0 | 149 | // Example usage: |
michael@0 | 150 | // |
michael@0 | 151 | // In your header: |
michael@0 | 152 | // template <typename T> struct DefaultSingletonTraits; |
michael@0 | 153 | // class FooClass { |
michael@0 | 154 | // public: |
michael@0 | 155 | // static FooClass* GetInstance(); <-- See comment below on this. |
michael@0 | 156 | // void Bar() { ... } |
michael@0 | 157 | // private: |
michael@0 | 158 | // FooClass() { ... } |
michael@0 | 159 | // friend struct DefaultSingletonTraits<FooClass>; |
michael@0 | 160 | // |
michael@0 | 161 | // DISALLOW_COPY_AND_ASSIGN(FooClass); |
michael@0 | 162 | // }; |
michael@0 | 163 | // |
michael@0 | 164 | // In your source file: |
michael@0 | 165 | // #include "base/memory/singleton.h" |
michael@0 | 166 | // FooClass* FooClass::GetInstance() { |
michael@0 | 167 | // return Singleton<FooClass>::get(); |
michael@0 | 168 | // } |
michael@0 | 169 | // |
michael@0 | 170 | // And to call methods on FooClass: |
michael@0 | 171 | // FooClass::GetInstance()->Bar(); |
michael@0 | 172 | // |
michael@0 | 173 | // NOTE: The method accessing Singleton<T>::get() has to be named as GetInstance |
michael@0 | 174 | // and it is important that FooClass::GetInstance() is not inlined in the |
michael@0 | 175 | // header. This makes sure that when source files from multiple targets include |
michael@0 | 176 | // this header they don't end up with different copies of the inlined code |
michael@0 | 177 | // creating multiple copies of the singleton. |
michael@0 | 178 | // |
michael@0 | 179 | // Singleton<> has no non-static members and doesn't need to actually be |
michael@0 | 180 | // instantiated. |
michael@0 | 181 | // |
michael@0 | 182 | // This class is itself thread-safe. The underlying Type must of course be |
michael@0 | 183 | // thread-safe if you want to use it concurrently. Two parameters may be tuned |
michael@0 | 184 | // depending on the user's requirements. |
michael@0 | 185 | // |
michael@0 | 186 | // Glossary: |
michael@0 | 187 | // RAE = kRegisterAtExit |
michael@0 | 188 | // |
michael@0 | 189 | // On every platform, if Traits::RAE is true, the singleton will be destroyed at |
michael@0 | 190 | // process exit. More precisely it uses base::AtExitManager which requires an |
michael@0 | 191 | // object of this type to be instantiated. AtExitManager mimics the semantics |
michael@0 | 192 | // of atexit() such as LIFO order but under Windows is safer to call. For more |
michael@0 | 193 | // information see at_exit.h. |
michael@0 | 194 | // |
michael@0 | 195 | // If Traits::RAE is false, the singleton will not be freed at process exit, |
michael@0 | 196 | // thus the singleton will be leaked if it is ever accessed. Traits::RAE |
michael@0 | 197 | // shouldn't be false unless absolutely necessary. Remember that the heap where |
michael@0 | 198 | // the object is allocated may be destroyed by the CRT anyway. |
michael@0 | 199 | // |
michael@0 | 200 | // Caveats: |
michael@0 | 201 | // (a) Every call to get(), operator->() and operator*() incurs some overhead |
michael@0 | 202 | // (16ns on my P4/2.8GHz) to check whether the object has already been |
michael@0 | 203 | // initialized. You may wish to cache the result of get(); it will not |
michael@0 | 204 | // change. |
michael@0 | 205 | // |
michael@0 | 206 | // (b) Your factory function must never throw an exception. This class is not |
michael@0 | 207 | // exception-safe. |
michael@0 | 208 | // |
michael@0 | 209 | template <typename Type, |
michael@0 | 210 | typename Traits = DefaultSingletonTraits<Type>, |
michael@0 | 211 | typename DifferentiatingType = Type> |
michael@0 | 212 | class Singleton { |
michael@0 | 213 | private: |
michael@0 | 214 | // Classes using the Singleton<T> pattern should declare a GetInstance() |
michael@0 | 215 | // method and call Singleton::get() from within that. |
michael@0 | 216 | friend Type* Type::GetInstance(); |
michael@0 | 217 | |
michael@0 | 218 | // Allow TraceLog tests to test tracing after OnExit. |
michael@0 | 219 | friend class DeleteTraceLogForTesting; |
michael@0 | 220 | |
michael@0 | 221 | // This class is safe to be constructed and copy-constructed since it has no |
michael@0 | 222 | // member. |
michael@0 | 223 | |
michael@0 | 224 | // Return a pointer to the one true instance of the class. |
michael@0 | 225 | static Type* get() { |
michael@0 | 226 | #ifndef NDEBUG |
michael@0 | 227 | // Avoid making TLS lookup on release builds. |
michael@0 | 228 | if (!Traits::kAllowedToAccessOnNonjoinableThread) |
michael@0 | 229 | base::ThreadRestrictions::AssertSingletonAllowed(); |
michael@0 | 230 | #endif |
michael@0 | 231 | |
michael@0 | 232 | base::subtle::AtomicWord value = base::subtle::NoBarrier_Load(&instance_); |
michael@0 | 233 | if (value != 0 && value != base::internal::kBeingCreatedMarker) { |
michael@0 | 234 | // See the corresponding HAPPENS_BEFORE below. |
michael@0 | 235 | ANNOTATE_HAPPENS_AFTER(&instance_); |
michael@0 | 236 | return reinterpret_cast<Type*>(value); |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | // Object isn't created yet, maybe we will get to create it, let's try... |
michael@0 | 240 | if (base::subtle::Acquire_CompareAndSwap( |
michael@0 | 241 | &instance_, 0, base::internal::kBeingCreatedMarker) == 0) { |
michael@0 | 242 | // instance_ was NULL and is now kBeingCreatedMarker. Only one thread |
michael@0 | 243 | // will ever get here. Threads might be spinning on us, and they will |
michael@0 | 244 | // stop right after we do this store. |
michael@0 | 245 | Type* newval = Traits::New(); |
michael@0 | 246 | |
michael@0 | 247 | // This annotation helps race detectors recognize correct lock-less |
michael@0 | 248 | // synchronization between different threads calling get(). |
michael@0 | 249 | // See the corresponding HAPPENS_AFTER below and above. |
michael@0 | 250 | ANNOTATE_HAPPENS_BEFORE(&instance_); |
michael@0 | 251 | base::subtle::Release_Store( |
michael@0 | 252 | &instance_, reinterpret_cast<base::subtle::AtomicWord>(newval)); |
michael@0 | 253 | |
michael@0 | 254 | if (newval != NULL && Traits::kRegisterAtExit) |
michael@0 | 255 | base::AtExitManager::RegisterCallback(OnExit, NULL); |
michael@0 | 256 | |
michael@0 | 257 | return newval; |
michael@0 | 258 | } |
michael@0 | 259 | |
michael@0 | 260 | // We hit a race. Wait for the other thread to complete it. |
michael@0 | 261 | value = base::internal::WaitForInstance(&instance_); |
michael@0 | 262 | |
michael@0 | 263 | // See the corresponding HAPPENS_BEFORE above. |
michael@0 | 264 | ANNOTATE_HAPPENS_AFTER(&instance_); |
michael@0 | 265 | return reinterpret_cast<Type*>(value); |
michael@0 | 266 | } |
michael@0 | 267 | |
michael@0 | 268 | // Adapter function for use with AtExit(). This should be called single |
michael@0 | 269 | // threaded, so don't use atomic operations. |
michael@0 | 270 | // Calling OnExit while singleton is in use by other threads is a mistake. |
michael@0 | 271 | static void OnExit(void* /*unused*/) { |
michael@0 | 272 | // AtExit should only ever be register after the singleton instance was |
michael@0 | 273 | // created. We should only ever get here with a valid instance_ pointer. |
michael@0 | 274 | Traits::Delete( |
michael@0 | 275 | reinterpret_cast<Type*>(base::subtle::NoBarrier_Load(&instance_))); |
michael@0 | 276 | instance_ = 0; |
michael@0 | 277 | } |
michael@0 | 278 | static base::subtle::AtomicWord instance_; |
michael@0 | 279 | }; |
michael@0 | 280 | |
michael@0 | 281 | template <typename Type, typename Traits, typename DifferentiatingType> |
michael@0 | 282 | base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: |
michael@0 | 283 | instance_ = 0; |
michael@0 | 284 | |
michael@0 | 285 | #endif // BASE_MEMORY_SINGLETON_H_ |