Line data Source code
1 : // Copyright 2013 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_ASSERT_SCOPE_H_
6 : #define V8_ASSERT_SCOPE_H_
7 :
8 : #include <stdint.h>
9 :
10 : #include "src/base/macros.h"
11 : #include "src/globals.h"
12 : #include "src/pointer-with-payload.h"
13 :
14 : namespace v8 {
15 : namespace internal {
16 :
17 : // Forward declarations.
18 : class Isolate;
19 : class PerThreadAssertData;
20 :
21 : template <>
22 : struct PointerWithPayloadTraits<PerThreadAssertData> {
23 : static constexpr int value = 1;
24 : };
25 :
26 : enum PerThreadAssertType {
27 : HEAP_ALLOCATION_ASSERT,
28 : HANDLE_ALLOCATION_ASSERT,
29 : HANDLE_DEREFERENCE_ASSERT,
30 : DEFERRED_HANDLE_DEREFERENCE_ASSERT,
31 : CODE_DEPENDENCY_CHANGE_ASSERT,
32 : LAST_PER_THREAD_ASSERT_TYPE
33 : };
34 :
35 : enum PerIsolateAssertType {
36 : JAVASCRIPT_EXECUTION_ASSERT,
37 : JAVASCRIPT_EXECUTION_THROWS,
38 : JAVASCRIPT_EXECUTION_DUMP,
39 : DEOPTIMIZATION_ASSERT,
40 : COMPILATION_ASSERT,
41 : NO_EXCEPTION_ASSERT
42 : };
43 :
44 : template <PerThreadAssertType kType, bool kAllow>
45 : class PerThreadAssertScope {
46 : public:
47 : V8_EXPORT_PRIVATE PerThreadAssertScope();
48 : V8_EXPORT_PRIVATE ~PerThreadAssertScope();
49 :
50 : V8_EXPORT_PRIVATE static bool IsAllowed();
51 :
52 : void Release();
53 :
54 : private:
55 : PointerWithPayload<PerThreadAssertData, bool, 1> data_and_old_state_;
56 :
57 0 : V8_INLINE void set_data(PerThreadAssertData* data) {
58 : data_and_old_state_.SetPointer(data);
59 0 : }
60 :
61 0 : V8_INLINE PerThreadAssertData* data() const {
62 0 : return data_and_old_state_.GetPointer();
63 : }
64 :
65 0 : V8_INLINE void set_old_state(bool old_state) {
66 0 : return data_and_old_state_.SetPayload(old_state);
67 : }
68 :
69 0 : V8_INLINE bool old_state() const { return data_and_old_state_.GetPayload(); }
70 :
71 : DISALLOW_COPY_AND_ASSIGN(PerThreadAssertScope);
72 : };
73 :
74 :
75 : template <PerIsolateAssertType type, bool allow>
76 : class PerIsolateAssertScope {
77 : public:
78 : explicit PerIsolateAssertScope(Isolate* isolate);
79 : ~PerIsolateAssertScope();
80 :
81 : static bool IsAllowed(Isolate* isolate);
82 :
83 : private:
84 : class DataBit;
85 :
86 : Isolate* isolate_;
87 : uint32_t old_data_;
88 :
89 : DISALLOW_COPY_AND_ASSIGN(PerIsolateAssertScope);
90 : };
91 :
92 :
93 : template <PerThreadAssertType type, bool allow>
94 : #ifdef DEBUG
95 : class PerThreadAssertScopeDebugOnly : public
96 : PerThreadAssertScope<type, allow> {
97 : #else
98 : class PerThreadAssertScopeDebugOnly {
99 : public:
100 : PerThreadAssertScopeDebugOnly() { // NOLINT (modernize-use-equals-default)
101 : // Define a constructor to avoid unused variable warnings.
102 : }
103 : void Release() {}
104 : #endif
105 : };
106 :
107 :
108 : template <PerIsolateAssertType type, bool allow>
109 : #ifdef DEBUG
110 : class PerIsolateAssertScopeDebugOnly : public
111 : PerIsolateAssertScope<type, allow> {
112 : public:
113 : explicit PerIsolateAssertScopeDebugOnly(Isolate* isolate)
114 : : PerIsolateAssertScope<type, allow>(isolate) { }
115 : #else
116 : class PerIsolateAssertScopeDebugOnly {
117 : public:
118 : explicit PerIsolateAssertScopeDebugOnly(Isolate* isolate) { }
119 : #endif
120 : };
121 :
122 : // Per-thread assert scopes.
123 :
124 : // Scope to document where we do not expect handles to be created.
125 : typedef PerThreadAssertScopeDebugOnly<HANDLE_ALLOCATION_ASSERT, false>
126 : DisallowHandleAllocation;
127 :
128 : // Scope to introduce an exception to DisallowHandleAllocation.
129 : typedef PerThreadAssertScopeDebugOnly<HANDLE_ALLOCATION_ASSERT, true>
130 : AllowHandleAllocation;
131 :
132 : // Scope to document where we do not expect any allocation and GC.
133 : typedef PerThreadAssertScopeDebugOnly<HEAP_ALLOCATION_ASSERT, false>
134 : DisallowHeapAllocation;
135 : #ifdef DEBUG
136 : #define DISALLOW_HEAP_ALLOCATION(name) DisallowHeapAllocation name
137 : #define DISALLOW_HEAP_ALLOCATION_REF(name) const DisallowHeapAllocation& name
138 : #else
139 : #define DISALLOW_HEAP_ALLOCATION(name)
140 : #define DISALLOW_HEAP_ALLOCATION_REF(name)
141 : #endif
142 :
143 : // Scope to introduce an exception to DisallowHeapAllocation.
144 : typedef PerThreadAssertScopeDebugOnly<HEAP_ALLOCATION_ASSERT, true>
145 : AllowHeapAllocation;
146 :
147 : // Scope to document where we do not expect any handle dereferences.
148 : typedef PerThreadAssertScopeDebugOnly<HANDLE_DEREFERENCE_ASSERT, false>
149 : DisallowHandleDereference;
150 :
151 : // Scope to introduce an exception to DisallowHandleDereference.
152 : typedef PerThreadAssertScopeDebugOnly<HANDLE_DEREFERENCE_ASSERT, true>
153 : AllowHandleDereference;
154 :
155 : // Scope to document where we do not expect deferred handles to be dereferenced.
156 : typedef PerThreadAssertScopeDebugOnly<DEFERRED_HANDLE_DEREFERENCE_ASSERT, false>
157 : DisallowDeferredHandleDereference;
158 :
159 : // Scope to introduce an exception to DisallowDeferredHandleDereference.
160 : typedef PerThreadAssertScopeDebugOnly<DEFERRED_HANDLE_DEREFERENCE_ASSERT, true>
161 : AllowDeferredHandleDereference;
162 :
163 : // Scope to document where we do not expect deferred handles to be dereferenced.
164 : typedef PerThreadAssertScopeDebugOnly<CODE_DEPENDENCY_CHANGE_ASSERT, false>
165 : DisallowCodeDependencyChange;
166 :
167 : // Scope to introduce an exception to DisallowDeferredHandleDereference.
168 : typedef PerThreadAssertScopeDebugOnly<CODE_DEPENDENCY_CHANGE_ASSERT, true>
169 : AllowCodeDependencyChange;
170 :
171 : class DisallowHeapAccess {
172 : DisallowHeapAllocation no_heap_allocation_;
173 : DisallowHandleAllocation no_handle_allocation_;
174 : DisallowHandleDereference no_handle_dereference_;
175 : DisallowCodeDependencyChange no_dependency_change_;
176 : };
177 :
178 : // Per-isolate assert scopes.
179 :
180 : // Scope to document where we do not expect javascript execution.
181 : typedef PerIsolateAssertScope<JAVASCRIPT_EXECUTION_ASSERT, false>
182 : DisallowJavascriptExecution;
183 :
184 : // Scope to introduce an exception to DisallowJavascriptExecution.
185 : typedef PerIsolateAssertScope<JAVASCRIPT_EXECUTION_ASSERT, true>
186 : AllowJavascriptExecution;
187 :
188 : // Scope to document where we do not expect javascript execution (debug only)
189 : typedef PerIsolateAssertScopeDebugOnly<JAVASCRIPT_EXECUTION_ASSERT, false>
190 : DisallowJavascriptExecutionDebugOnly;
191 :
192 : // Scope to introduce an exception to DisallowJavascriptExecutionDebugOnly.
193 : typedef PerIsolateAssertScopeDebugOnly<JAVASCRIPT_EXECUTION_ASSERT, true>
194 : AllowJavascriptExecutionDebugOnly;
195 :
196 : // Scope in which javascript execution leads to exception being thrown.
197 : typedef PerIsolateAssertScope<JAVASCRIPT_EXECUTION_THROWS, false>
198 : ThrowOnJavascriptExecution;
199 :
200 : // Scope to introduce an exception to ThrowOnJavascriptExecution.
201 : typedef PerIsolateAssertScope<JAVASCRIPT_EXECUTION_THROWS, true>
202 : NoThrowOnJavascriptExecution;
203 :
204 : // Scope in which javascript execution causes dumps.
205 : typedef PerIsolateAssertScope<JAVASCRIPT_EXECUTION_DUMP, false>
206 : DumpOnJavascriptExecution;
207 :
208 : // Scope in which javascript execution causes dumps.
209 : typedef PerIsolateAssertScope<JAVASCRIPT_EXECUTION_DUMP, true>
210 : NoDumpOnJavascriptExecution;
211 :
212 : // Scope to document where we do not expect deoptimization.
213 : typedef PerIsolateAssertScopeDebugOnly<DEOPTIMIZATION_ASSERT, false>
214 : DisallowDeoptimization;
215 :
216 : // Scope to introduce an exception to DisallowDeoptimization.
217 : typedef PerIsolateAssertScopeDebugOnly<DEOPTIMIZATION_ASSERT, true>
218 : AllowDeoptimization;
219 :
220 : // Scope to document where we do not expect deoptimization.
221 : typedef PerIsolateAssertScopeDebugOnly<COMPILATION_ASSERT, false>
222 : DisallowCompilation;
223 :
224 : // Scope to introduce an exception to DisallowDeoptimization.
225 : typedef PerIsolateAssertScopeDebugOnly<COMPILATION_ASSERT, true>
226 : AllowCompilation;
227 :
228 : // Scope to document where we do not expect exceptions.
229 : typedef PerIsolateAssertScopeDebugOnly<NO_EXCEPTION_ASSERT, false>
230 : DisallowExceptions;
231 :
232 : // Scope to introduce an exception to DisallowExceptions.
233 : typedef PerIsolateAssertScopeDebugOnly<NO_EXCEPTION_ASSERT, true>
234 : AllowExceptions;
235 : } // namespace internal
236 : } // namespace v8
237 :
238 : #endif // V8_ASSERT_SCOPE_H_
|