blob: 84e6990b04b353e50d2bda3da589148b91cc02dc [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008#include <stdint.h>
9#include "src/base/macros.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010
11namespace v8 {
12namespace internal {
13
Emily Bernierd0a1eb72015-03-24 16:35:39 -040014// Forward declarations.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015class Isolate;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040016class PerThreadAssertData;
17
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018
19enum PerThreadAssertType {
20 HEAP_ALLOCATION_ASSERT,
21 HANDLE_ALLOCATION_ASSERT,
22 HANDLE_DEREFERENCE_ASSERT,
23 DEFERRED_HANDLE_DEREFERENCE_ASSERT,
24 CODE_DEPENDENCY_CHANGE_ASSERT,
25 LAST_PER_THREAD_ASSERT_TYPE
26};
27
28
29enum PerIsolateAssertType {
30 JAVASCRIPT_EXECUTION_ASSERT,
31 JAVASCRIPT_EXECUTION_THROWS,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032 DEOPTIMIZATION_ASSERT,
33 COMPILATION_ASSERT
34};
35
36
Emily Bernierd0a1eb72015-03-24 16:35:39 -040037template <PerThreadAssertType kType, bool kAllow>
38class PerThreadAssertScope {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000039 public:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040040 PerThreadAssertScope();
41 ~PerThreadAssertScope();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000042
Emily Bernierd0a1eb72015-03-24 16:35:39 -040043 static bool IsAllowed();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000044
45 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000046 PerThreadAssertData* data_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000047 bool old_state_;
48
49 DISALLOW_COPY_AND_ASSIGN(PerThreadAssertScope);
50};
51
52
Ben Murdochb8a8cc12014-11-26 15:28:44 +000053template <PerIsolateAssertType type, bool allow>
Emily Bernierd0a1eb72015-03-24 16:35:39 -040054class PerIsolateAssertScope {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055 public:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040056 explicit PerIsolateAssertScope(Isolate* isolate);
57 ~PerIsolateAssertScope();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058
Emily Bernierd0a1eb72015-03-24 16:35:39 -040059 static bool IsAllowed(Isolate* isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000060
61 private:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040062 class DataBit;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064 Isolate* isolate_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040065 uint32_t old_data_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066
67 DISALLOW_COPY_AND_ASSIGN(PerIsolateAssertScope);
68};
69
70
71template <PerThreadAssertType type, bool allow>
72#ifdef DEBUG
73class PerThreadAssertScopeDebugOnly : public
74 PerThreadAssertScope<type, allow> {
75#else
76class PerThreadAssertScopeDebugOnly {
77 public:
78 PerThreadAssertScopeDebugOnly() { }
79#endif
80};
81
82
83template <PerIsolateAssertType type, bool allow>
84#ifdef DEBUG
85class PerIsolateAssertScopeDebugOnly : public
86 PerIsolateAssertScope<type, allow> {
87 public:
88 explicit PerIsolateAssertScopeDebugOnly(Isolate* isolate)
89 : PerIsolateAssertScope<type, allow>(isolate) { }
90#else
91class PerIsolateAssertScopeDebugOnly {
92 public:
93 explicit PerIsolateAssertScopeDebugOnly(Isolate* isolate) { }
94#endif
95};
96
97// Per-thread assert scopes.
98
99// Scope to document where we do not expect handles to be created.
100typedef PerThreadAssertScopeDebugOnly<HANDLE_ALLOCATION_ASSERT, false>
101 DisallowHandleAllocation;
102
103// Scope to introduce an exception to DisallowHandleAllocation.
104typedef PerThreadAssertScopeDebugOnly<HANDLE_ALLOCATION_ASSERT, true>
105 AllowHandleAllocation;
106
107// Scope to document where we do not expect any allocation and GC.
108typedef PerThreadAssertScopeDebugOnly<HEAP_ALLOCATION_ASSERT, false>
109 DisallowHeapAllocation;
110
111// Scope to introduce an exception to DisallowHeapAllocation.
112typedef PerThreadAssertScopeDebugOnly<HEAP_ALLOCATION_ASSERT, true>
113 AllowHeapAllocation;
114
115// Scope to document where we do not expect any handle dereferences.
116typedef PerThreadAssertScopeDebugOnly<HANDLE_DEREFERENCE_ASSERT, false>
117 DisallowHandleDereference;
118
119// Scope to introduce an exception to DisallowHandleDereference.
120typedef PerThreadAssertScopeDebugOnly<HANDLE_DEREFERENCE_ASSERT, true>
121 AllowHandleDereference;
122
123// Scope to document where we do not expect deferred handles to be dereferenced.
124typedef PerThreadAssertScopeDebugOnly<DEFERRED_HANDLE_DEREFERENCE_ASSERT, false>
125 DisallowDeferredHandleDereference;
126
127// Scope to introduce an exception to DisallowDeferredHandleDereference.
128typedef PerThreadAssertScopeDebugOnly<DEFERRED_HANDLE_DEREFERENCE_ASSERT, true>
129 AllowDeferredHandleDereference;
130
131// Scope to document where we do not expect deferred handles to be dereferenced.
132typedef PerThreadAssertScopeDebugOnly<CODE_DEPENDENCY_CHANGE_ASSERT, false>
133 DisallowCodeDependencyChange;
134
135// Scope to introduce an exception to DisallowDeferredHandleDereference.
136typedef PerThreadAssertScopeDebugOnly<CODE_DEPENDENCY_CHANGE_ASSERT, true>
137 AllowCodeDependencyChange;
138
139
140// Per-isolate assert scopes.
141
142// Scope to document where we do not expect javascript execution.
143typedef PerIsolateAssertScope<JAVASCRIPT_EXECUTION_ASSERT, false>
144 DisallowJavascriptExecution;
145
146// Scope to introduce an exception to DisallowJavascriptExecution.
147typedef PerIsolateAssertScope<JAVASCRIPT_EXECUTION_ASSERT, true>
148 AllowJavascriptExecution;
149
150// Scope in which javascript execution leads to exception being thrown.
151typedef PerIsolateAssertScope<JAVASCRIPT_EXECUTION_THROWS, false>
152 ThrowOnJavascriptExecution;
153
154// Scope to introduce an exception to ThrowOnJavascriptExecution.
155typedef PerIsolateAssertScope<JAVASCRIPT_EXECUTION_THROWS, true>
156 NoThrowOnJavascriptExecution;
157
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000158// Scope to document where we do not expect deoptimization.
159typedef PerIsolateAssertScopeDebugOnly<DEOPTIMIZATION_ASSERT, false>
160 DisallowDeoptimization;
161
162// Scope to introduce an exception to DisallowDeoptimization.
163typedef PerIsolateAssertScopeDebugOnly<DEOPTIMIZATION_ASSERT, true>
164 AllowDeoptimization;
165
166// Scope to document where we do not expect deoptimization.
167typedef PerIsolateAssertScopeDebugOnly<COMPILATION_ASSERT, false>
168 DisallowCompilation;
169
170// Scope to introduce an exception to DisallowDeoptimization.
171typedef PerIsolateAssertScopeDebugOnly<COMPILATION_ASSERT, true>
172 AllowCompilation;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000173} // namespace internal
174} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000175
176#endif // V8_ASSERT_SCOPE_H_