OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 12 matching lines...) Expand all Loading... |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #ifndef V8_ASSERT_SCOPE_H_ | 28 #ifndef V8_ASSERT_SCOPE_H_ |
29 #define V8_ASSERT_SCOPE_H_ | 29 #define V8_ASSERT_SCOPE_H_ |
30 | 30 |
31 #include "allocation.h" | 31 #include "allocation.h" |
32 #include "platform.h" | 32 #include "platform.h" |
| 33 #include "utils.h" |
33 | 34 |
34 namespace v8 { | 35 namespace v8 { |
35 namespace internal { | 36 namespace internal { |
36 | 37 |
37 class Isolate; | 38 class Isolate; |
38 | 39 |
39 enum PerThreadAssertType { | 40 enum PerThreadAssertType { |
40 HEAP_ALLOCATION_ASSERT, | 41 HEAP_ALLOCATION_ASSERT, |
41 HANDLE_ALLOCATION_ASSERT, | 42 HANDLE_ALLOCATION_ASSERT, |
42 HANDLE_DEREFERENCE_ASSERT, | 43 HANDLE_DEREFERENCE_ASSERT, |
43 DEFERRED_HANDLE_DEREFERENCE_ASSERT, | 44 DEFERRED_HANDLE_DEREFERENCE_ASSERT, |
44 CODE_DEPENDENCY_CHANGE_ASSERT, | 45 CODE_DEPENDENCY_CHANGE_ASSERT, |
45 LAST_PER_THREAD_ASSERT_TYPE | 46 LAST_PER_THREAD_ASSERT_TYPE |
46 }; | 47 }; |
47 | 48 |
48 | 49 |
49 #ifdef DEBUG | 50 enum PerIsolateAssertType { |
| 51 JAVASCRIPT_EXECUTION_ASSERT, |
| 52 ALLOCATION_FAILURE_ASSERT |
| 53 }; |
| 54 |
| 55 |
50 class PerThreadAssertData { | 56 class PerThreadAssertData { |
51 public: | 57 public: |
52 PerThreadAssertData() : nesting_level_(0) { | 58 PerThreadAssertData() : nesting_level_(0) { |
53 for (int i = 0; i < LAST_PER_THREAD_ASSERT_TYPE; i++) { | 59 for (int i = 0; i < LAST_PER_THREAD_ASSERT_TYPE; i++) { |
54 assert_states_[i] = true; | 60 assert_states_[i] = true; |
55 } | 61 } |
56 } | 62 } |
57 | 63 |
58 void set(PerThreadAssertType type, bool allow) { | 64 void set(PerThreadAssertType type, bool allow) { |
59 assert_states_[type] = allow; | 65 assert_states_[type] = allow; |
60 } | 66 } |
61 | 67 |
62 bool get(PerThreadAssertType type) const { | 68 bool get(PerThreadAssertType type) const { |
63 return assert_states_[type]; | 69 return assert_states_[type]; |
64 } | 70 } |
65 | 71 |
66 void increment_level() { ++nesting_level_; } | 72 void increment_level() { ++nesting_level_; } |
67 bool decrement_level() { return --nesting_level_ == 0; } | 73 bool decrement_level() { return --nesting_level_ == 0; } |
68 | 74 |
69 private: | 75 private: |
70 bool assert_states_[LAST_PER_THREAD_ASSERT_TYPE]; | 76 bool assert_states_[LAST_PER_THREAD_ASSERT_TYPE]; |
71 int nesting_level_; | 77 int nesting_level_; |
72 | 78 |
73 DISALLOW_COPY_AND_ASSIGN(PerThreadAssertData); | 79 DISALLOW_COPY_AND_ASSIGN(PerThreadAssertData); |
74 }; | 80 }; |
75 #endif // DEBUG | |
76 | 81 |
77 | 82 |
78 class PerThreadAssertScopeBase { | 83 class PerThreadAssertScopeBase { |
79 #ifdef DEBUG | |
80 | |
81 protected: | 84 protected: |
82 PerThreadAssertScopeBase() { | 85 PerThreadAssertScopeBase() { |
83 data_ = GetAssertData(); | 86 data_ = GetAssertData(); |
84 if (data_ == NULL) { | 87 if (data_ == NULL) { |
85 data_ = new PerThreadAssertData(); | 88 data_ = new PerThreadAssertData(); |
86 SetThreadLocalData(data_); | 89 SetThreadLocalData(data_); |
87 } | 90 } |
88 data_->increment_level(); | 91 data_->increment_level(); |
89 } | 92 } |
90 | 93 |
(...skipping 12 matching lines...) Expand all Loading... |
103 } | 106 } |
104 | 107 |
105 static Thread::LocalStorageKey thread_local_key; | 108 static Thread::LocalStorageKey thread_local_key; |
106 PerThreadAssertData* data_; | 109 PerThreadAssertData* data_; |
107 friend class Isolate; | 110 friend class Isolate; |
108 | 111 |
109 private: | 112 private: |
110 static void SetThreadLocalData(PerThreadAssertData* data) { | 113 static void SetThreadLocalData(PerThreadAssertData* data) { |
111 Thread::SetThreadLocal(thread_local_key, data); | 114 Thread::SetThreadLocal(thread_local_key, data); |
112 } | 115 } |
113 #endif // DEBUG | |
114 }; | 116 }; |
115 | 117 |
116 | 118 |
117 | |
118 template <PerThreadAssertType type, bool allow> | 119 template <PerThreadAssertType type, bool allow> |
119 class PerThreadAssertScope : public PerThreadAssertScopeBase { | 120 class PerThreadAssertScope : public PerThreadAssertScopeBase { |
120 public: | 121 public: |
121 #ifndef DEBUG | |
122 PerThreadAssertScope() { } | |
123 static void SetIsAllowed(bool is_allowed) { } | |
124 #else | |
125 PerThreadAssertScope() { | 122 PerThreadAssertScope() { |
126 old_state_ = data_->get(type); | 123 old_state_ = data_->get(type); |
127 data_->set(type, allow); | 124 data_->set(type, allow); |
128 } | 125 } |
129 | 126 |
130 ~PerThreadAssertScope() { data_->set(type, old_state_); } | 127 ~PerThreadAssertScope() { data_->set(type, old_state_); } |
131 | 128 |
132 static bool IsAllowed() { | 129 static bool IsAllowed() { |
133 PerThreadAssertData* data = GetAssertData(); | 130 PerThreadAssertData* data = GetAssertData(); |
134 return data == NULL || data->get(type); | 131 return data == NULL || data->get(type); |
135 } | 132 } |
136 | 133 |
137 private: | 134 private: |
138 bool old_state_; | 135 bool old_state_; |
139 #endif | |
140 }; | 136 }; |
141 | 137 |
| 138 |
| 139 class PerIsolateAssertBase { |
| 140 protected: |
| 141 static uint32_t GetData(Isolate* isolate); |
| 142 static void SetData(Isolate* isolate, uint32_t data); |
| 143 }; |
| 144 |
| 145 |
| 146 template <PerIsolateAssertType type, bool allow> |
| 147 class PerIsolateAssertScope : public PerIsolateAssertBase { |
| 148 public: |
| 149 explicit PerIsolateAssertScope(Isolate* isolate) : isolate_(isolate) { |
| 150 STATIC_ASSERT(type < 32); |
| 151 old_data_ = GetData(isolate_); |
| 152 SetData(isolate_, DataBit::update(old_data_, allow)); |
| 153 } |
| 154 |
| 155 ~PerIsolateAssertScope() { |
| 156 SetData(isolate_, old_data_); |
| 157 } |
| 158 |
| 159 static bool IsAllowed(Isolate* isolate) { |
| 160 return DataBit::decode(GetData(isolate)); |
| 161 } |
| 162 |
| 163 private: |
| 164 typedef BitField<bool, type, 1> DataBit; |
| 165 |
| 166 uint32_t old_data_; |
| 167 Isolate* isolate_; |
| 168 }; |
| 169 |
| 170 |
| 171 template <PerThreadAssertType type, bool allow> |
| 172 #ifdef DEBUG |
| 173 class PerThreadAssertScopeDebugOnly : public |
| 174 PerThreadAssertScope<type, allow> { |
| 175 }; |
| 176 #else |
| 177 class PerThreadAssertScopeDebugOnly { |
| 178 public: |
| 179 PerThreadAssertScopeDebugOnly() { } |
| 180 }; |
| 181 #endif |
| 182 |
| 183 |
| 184 template <PerIsolateAssertType type, bool allow> |
| 185 #ifdef DEBUG |
| 186 class PerIsolateAssertScopeDebugOnly : public |
| 187 PerIsolateAssertScope<type, allow> { |
| 188 public: |
| 189 explicit PerIsolateAssertScopeDebugOnly(Isolate* isolate) |
| 190 : PerIsolateAssertScope<type, allow>(isolate) { } |
| 191 }; |
| 192 #else |
| 193 class PerIsolateAssertScopeDebugOnly { |
| 194 public: |
| 195 explicit PerIsolateAssertScopeDebugOnly(Isolate* isolate) { } |
| 196 }; |
| 197 #endif |
| 198 |
| 199 |
| 200 // Per-thread assert scopes. |
| 201 |
142 // Scope to document where we do not expect handles to be created. | 202 // Scope to document where we do not expect handles to be created. |
143 typedef PerThreadAssertScope<HANDLE_ALLOCATION_ASSERT, false> | 203 typedef PerThreadAssertScopeDebugOnly<HANDLE_ALLOCATION_ASSERT, false> |
144 DisallowHandleAllocation; | 204 DisallowHandleAllocation; |
145 | 205 |
146 // Scope to introduce an exception to DisallowHandleAllocation. | 206 // Scope to introduce an exception to DisallowHandleAllocation. |
147 typedef PerThreadAssertScope<HANDLE_ALLOCATION_ASSERT, true> | 207 typedef PerThreadAssertScopeDebugOnly<HANDLE_ALLOCATION_ASSERT, true> |
148 AllowHandleAllocation; | 208 AllowHandleAllocation; |
149 | 209 |
150 // Scope to document where we do not expect any allocation and GC. | 210 // Scope to document where we do not expect any allocation and GC. |
151 typedef PerThreadAssertScope<HEAP_ALLOCATION_ASSERT, false> | 211 typedef PerThreadAssertScopeDebugOnly<HEAP_ALLOCATION_ASSERT, false> |
152 DisallowHeapAllocation; | 212 DisallowHeapAllocation; |
153 | 213 |
154 // Scope to introduce an exception to DisallowHeapAllocation. | 214 // Scope to introduce an exception to DisallowHeapAllocation. |
155 typedef PerThreadAssertScope<HEAP_ALLOCATION_ASSERT, true> | 215 typedef PerThreadAssertScopeDebugOnly<HEAP_ALLOCATION_ASSERT, true> |
156 AllowHeapAllocation; | 216 AllowHeapAllocation; |
157 | 217 |
158 // Scope to document where we do not expect any handle dereferences. | 218 // Scope to document where we do not expect any handle dereferences. |
159 typedef PerThreadAssertScope<HANDLE_DEREFERENCE_ASSERT, false> | 219 typedef PerThreadAssertScopeDebugOnly<HANDLE_DEREFERENCE_ASSERT, false> |
160 DisallowHandleDereference; | 220 DisallowHandleDereference; |
161 | 221 |
162 // Scope to introduce an exception to DisallowHandleDereference. | 222 // Scope to introduce an exception to DisallowHandleDereference. |
163 typedef PerThreadAssertScope<HANDLE_DEREFERENCE_ASSERT, true> | 223 typedef PerThreadAssertScopeDebugOnly<HANDLE_DEREFERENCE_ASSERT, true> |
164 AllowHandleDereference; | 224 AllowHandleDereference; |
165 | 225 |
166 // Scope to document where we do not expect deferred handles to be dereferenced. | 226 // Scope to document where we do not expect deferred handles to be dereferenced. |
167 typedef PerThreadAssertScope<DEFERRED_HANDLE_DEREFERENCE_ASSERT, false> | 227 typedef PerThreadAssertScopeDebugOnly<DEFERRED_HANDLE_DEREFERENCE_ASSERT, false> |
168 DisallowDeferredHandleDereference; | 228 DisallowDeferredHandleDereference; |
169 | 229 |
170 // Scope to introduce an exception to DisallowDeferredHandleDereference. | 230 // Scope to introduce an exception to DisallowDeferredHandleDereference. |
171 typedef PerThreadAssertScope<DEFERRED_HANDLE_DEREFERENCE_ASSERT, true> | 231 typedef PerThreadAssertScopeDebugOnly<DEFERRED_HANDLE_DEREFERENCE_ASSERT, true> |
172 AllowDeferredHandleDereference; | 232 AllowDeferredHandleDereference; |
173 | 233 |
174 // Scope to document where we do not expect deferred handles to be dereferenced. | 234 // Scope to document where we do not expect deferred handles to be dereferenced. |
175 typedef PerThreadAssertScope<CODE_DEPENDENCY_CHANGE_ASSERT, false> | 235 typedef PerThreadAssertScopeDebugOnly<CODE_DEPENDENCY_CHANGE_ASSERT, false> |
176 DisallowCodeDependencyChange; | 236 DisallowCodeDependencyChange; |
177 | 237 |
178 // Scope to introduce an exception to DisallowDeferredHandleDereference. | 238 // Scope to introduce an exception to DisallowDeferredHandleDereference. |
179 typedef PerThreadAssertScope<CODE_DEPENDENCY_CHANGE_ASSERT, true> | 239 typedef PerThreadAssertScopeDebugOnly<CODE_DEPENDENCY_CHANGE_ASSERT, true> |
180 AllowCodeDependencyChange; | 240 AllowCodeDependencyChange; |
181 | 241 |
| 242 |
| 243 // Per-isolate assert scopes. |
| 244 |
| 245 // Scope to document where we do not expect javascript execution. |
| 246 typedef PerIsolateAssertScope<JAVASCRIPT_EXECUTION_ASSERT, false> |
| 247 DisallowJavascriptExecution; |
| 248 |
| 249 // Scope to introduce an exception to DisallowJavascriptExecution. |
| 250 typedef PerIsolateAssertScope<JAVASCRIPT_EXECUTION_ASSERT, true> |
| 251 AllowJavascriptExecution; |
| 252 |
| 253 // Scope to document where we do not expect an allocation failure. |
| 254 typedef PerIsolateAssertScopeDebugOnly<ALLOCATION_FAILURE_ASSERT, false> |
| 255 DisallowAllocationFailure; |
| 256 |
| 257 // Scope to introduce an exception to DisallowAllocationFailure. |
| 258 typedef PerIsolateAssertScopeDebugOnly<ALLOCATION_FAILURE_ASSERT, true> |
| 259 AllowAllocationFailure; |
| 260 |
182 } } // namespace v8::internal | 261 } } // namespace v8::internal |
183 | 262 |
184 #endif // V8_ASSERT_SCOPE_H_ | 263 #endif // V8_ASSERT_SCOPE_H_ |
OLD | NEW |