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 56 : PerThreadAssertScopeDebugOnly() { // NOLINT (modernize-use-equals-default)
101 : // Define a constructor to avoid unused variable warnings.
102 56 : }
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 : #else
138 : #define DISALLOW_HEAP_ALLOCATION(name)
139 : #endif
140 :
141 : // Scope to introduce an exception to DisallowHeapAllocation.
142 : typedef PerThreadAssertScopeDebugOnly<HEAP_ALLOCATION_ASSERT, true>
143 : AllowHeapAllocation;
144 :
145 : // Scope to document where we do not expect any handle dereferences.
146 : typedef PerThreadAssertScopeDebugOnly<HANDLE_DEREFERENCE_ASSERT, false>
147 : DisallowHandleDereference;
148 :
149 : // Scope to introduce an exception to DisallowHandleDereference.
150 : typedef PerThreadAssertScopeDebugOnly<HANDLE_DEREFERENCE_ASSERT, true>
151 : AllowHandleDereference;
152 :
153 : // Scope to document where we do not expect deferred handles to be dereferenced.
154 : typedef PerThreadAssertScopeDebugOnly<DEFERRED_HANDLE_DEREFERENCE_ASSERT, false>
155 : DisallowDeferredHandleDereference;
156 :
157 : // Scope to introduce an exception to DisallowDeferredHandleDereference.
158 : typedef PerThreadAssertScopeDebugOnly<DEFERRED_HANDLE_DEREFERENCE_ASSERT, true>
159 : AllowDeferredHandleDereference;
160 :
161 : // Scope to document where we do not expect deferred handles to be dereferenced.
162 : typedef PerThreadAssertScopeDebugOnly<CODE_DEPENDENCY_CHANGE_ASSERT, false>
163 : DisallowCodeDependencyChange;
164 :
165 : // Scope to introduce an exception to DisallowDeferredHandleDereference.
166 : typedef PerThreadAssertScopeDebugOnly<CODE_DEPENDENCY_CHANGE_ASSERT, true>
167 : AllowCodeDependencyChange;
168 :
169 : class DisallowHeapAccess {
170 : DisallowHeapAllocation no_heap_allocation_;
171 : DisallowHandleAllocation no_handle_allocation_;
172 : DisallowHandleDereference no_handle_dereference_;
173 : DisallowCodeDependencyChange no_dependency_change_;
174 : };
175 :
176 : // Per-isolate assert scopes.
177 :
178 : // Scope to document where we do not expect javascript execution.
179 : typedef PerIsolateAssertScope<JAVASCRIPT_EXECUTION_ASSERT, false>
180 : DisallowJavascriptExecution;
181 :
182 : // Scope to introduce an exception to DisallowJavascriptExecution.
183 : typedef PerIsolateAssertScope<JAVASCRIPT_EXECUTION_ASSERT, true>
184 : AllowJavascriptExecution;
185 :
186 : // Scope to document where we do not expect javascript execution (debug only)
187 : typedef PerIsolateAssertScopeDebugOnly<JAVASCRIPT_EXECUTION_ASSERT, false>
188 : DisallowJavascriptExecutionDebugOnly;
189 :
190 : // Scope to introduce an exception to DisallowJavascriptExecutionDebugOnly.
191 : typedef PerIsolateAssertScopeDebugOnly<JAVASCRIPT_EXECUTION_ASSERT, true>
192 : AllowJavascriptExecutionDebugOnly;
193 :
194 : // Scope in which javascript execution leads to exception being thrown.
195 : typedef PerIsolateAssertScope<JAVASCRIPT_EXECUTION_THROWS, false>
196 : ThrowOnJavascriptExecution;
197 :
198 : // Scope to introduce an exception to ThrowOnJavascriptExecution.
199 : typedef PerIsolateAssertScope<JAVASCRIPT_EXECUTION_THROWS, true>
200 : NoThrowOnJavascriptExecution;
201 :
202 : // Scope in which javascript execution causes dumps.
203 : typedef PerIsolateAssertScope<JAVASCRIPT_EXECUTION_DUMP, false>
204 : DumpOnJavascriptExecution;
205 :
206 : // Scope in which javascript execution causes dumps.
207 : typedef PerIsolateAssertScope<JAVASCRIPT_EXECUTION_DUMP, true>
208 : NoDumpOnJavascriptExecution;
209 :
210 : // Scope to document where we do not expect deoptimization.
211 : typedef PerIsolateAssertScopeDebugOnly<DEOPTIMIZATION_ASSERT, false>
212 : DisallowDeoptimization;
213 :
214 : // Scope to introduce an exception to DisallowDeoptimization.
215 : typedef PerIsolateAssertScopeDebugOnly<DEOPTIMIZATION_ASSERT, true>
216 : AllowDeoptimization;
217 :
218 : // Scope to document where we do not expect deoptimization.
219 : typedef PerIsolateAssertScopeDebugOnly<COMPILATION_ASSERT, false>
220 : DisallowCompilation;
221 :
222 : // Scope to introduce an exception to DisallowDeoptimization.
223 : typedef PerIsolateAssertScopeDebugOnly<COMPILATION_ASSERT, true>
224 : AllowCompilation;
225 :
226 : // Scope to document where we do not expect exceptions.
227 : typedef PerIsolateAssertScopeDebugOnly<NO_EXCEPTION_ASSERT, false>
228 : DisallowExceptions;
229 :
230 : // Scope to introduce an exception to DisallowExceptions.
231 : typedef PerIsolateAssertScopeDebugOnly<NO_EXCEPTION_ASSERT, true>
232 : AllowExceptions;
233 : } // namespace internal
234 : } // namespace v8
235 :
236 : #endif // V8_ASSERT_SCOPE_H_
|