v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
assert-scope.cc
Go to the documentation of this file.
1// Copyright 2014 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
6
7#include "src/base/enum-set.h"
9
10namespace v8 {
11namespace internal {
12
13namespace {
14
15// All asserts are allowed by default except for one, and the cleared bit is not
16// set.
17constexpr PerThreadAsserts kInitialValue =
18 ~PerThreadAsserts{HANDLE_USAGE_ON_ALL_THREADS_ASSERT};
19static_assert(kInitialValue.contains(ASSERT_TYPE_IS_VALID_MARKER));
20
21// The cleared value is the only one where ASSERT_TYPE_IS_VALID_MARKER is not
22// set.
23constexpr PerThreadAsserts kClearedValue = PerThreadAsserts{};
24static_assert(!kClearedValue.contains(ASSERT_TYPE_IS_VALID_MARKER));
25
26// Thread-local storage for assert data.
27thread_local PerThreadAsserts current_per_thread_assert_data(kInitialValue);
28
29} // namespace
30
31template <bool kAllow, PerThreadAssertType... kTypes>
33 : old_data_(current_per_thread_assert_data) {
34 static_assert(((kTypes != ASSERT_TYPE_IS_VALID_MARKER) && ...),
35 "PerThreadAssertScope types should not include the "
36 "ASSERT_TYPE_IS_VALID_MARKER");
38 if (kAllow) {
39 current_per_thread_assert_data = old_data_ | PerThreadAsserts({kTypes...});
40 } else {
41 current_per_thread_assert_data = old_data_ - PerThreadAsserts({kTypes...});
42 }
43}
44
45template <bool kAllow, PerThreadAssertType... kTypes>
49
50template <bool kAllow, PerThreadAssertType... kTypes>
52 if (old_data_ == kClearedValue) return;
53 current_per_thread_assert_data = old_data_;
54 old_data_ = kClearedValue;
55}
56
57// static
58template <bool kAllow, PerThreadAssertType... kTypes>
60 return current_per_thread_assert_data.contains_all({kTypes...});
61}
62
63#define PER_ISOLATE_ASSERT_SCOPE_DEFINITION(ScopeType, field, enable) \
64 ScopeType::ScopeType(Isolate* isolate) \
65 : isolate_(isolate), old_data_(isolate->field()) { \
66 DCHECK_NOT_NULL(isolate); \
67 isolate_->set_##field(enable); \
68 } \
69 \
70 ScopeType::~ScopeType() { isolate_->set_##field(old_data_); } \
71 \
72 /* static */ \
73 bool ScopeType::IsAllowed(Isolate* isolate) { return isolate->field(); } \
74 \
75 /* static */ \
76 void ScopeType::Open(Isolate* isolate, bool* was_execution_allowed) { \
77 DCHECK_NOT_NULL(isolate); \
78 DCHECK_NOT_NULL(was_execution_allowed); \
79 *was_execution_allowed = isolate->field(); \
80 isolate->set_##field(enable); \
81 } \
82 /* static */ \
83 void ScopeType::Close(Isolate* isolate, bool was_execution_allowed) { \
84 DCHECK_NOT_NULL(isolate); \
85 isolate->set_##field(was_execution_allowed); \
86 }
87
88#define PER_ISOLATE_ASSERT_ENABLE_SCOPE_DEFINITION(EnableType, _, field, \
89 enable) \
90 PER_ISOLATE_ASSERT_SCOPE_DEFINITION(EnableType, field, enable)
91
92#define PER_ISOLATE_ASSERT_DISABLE_SCOPE_DEFINITION(_, DisableType, field, \
93 enable) \
94 PER_ISOLATE_ASSERT_SCOPE_DEFINITION(DisableType, field, enable)
95
100
101// -----------------------------------------------------------------------------
102// Instantiations.
103
104template class PerThreadAssertScope<false, HEAP_ALLOCATION_ASSERT>;
105template class PerThreadAssertScope<true, HEAP_ALLOCATION_ASSERT>;
106template class PerThreadAssertScope<false, SAFEPOINTS_ASSERT>;
107template class PerThreadAssertScope<true, SAFEPOINTS_ASSERT>;
108template class PerThreadAssertScope<false, HANDLE_ALLOCATION_ASSERT>;
109template class PerThreadAssertScope<true, HANDLE_ALLOCATION_ASSERT>;
110template class PerThreadAssertScope<false, HANDLE_DEREFERENCE_ASSERT>;
111template class PerThreadAssertScope<true, HANDLE_DEREFERENCE_ASSERT>;
112template class PerThreadAssertScope<true, HANDLE_USAGE_ON_ALL_THREADS_ASSERT>;
113template class PerThreadAssertScope<false, CODE_DEPENDENCY_CHANGE_ASSERT>;
114template class PerThreadAssertScope<true, CODE_DEPENDENCY_CHANGE_ASSERT>;
115template class PerThreadAssertScope<false, CODE_ALLOCATION_ASSERT>;
116template class PerThreadAssertScope<true, CODE_ALLOCATION_ASSERT>;
117template class PerThreadAssertScope<false, GC_MOLE>;
118template class PerThreadAssertScope<false, POSITION_INFO_SLOW_ASSERT>;
119template class PerThreadAssertScope<true, POSITION_INFO_SLOW_ASSERT>;
120template class PerThreadAssertScope<false, SAFEPOINTS_ASSERT,
122template class PerThreadAssertScope<true, SAFEPOINTS_ASSERT,
124template class PerThreadAssertScope<
127template class PerThreadAssertScope<
130
131static_assert(Internals::kDisallowGarbageCollectionAlign ==
133static_assert(Internals::kDisallowGarbageCollectionSize ==
135
136} // namespace internal
137} // namespace v8
#define PER_ISOLATE_ASSERT_ENABLE_SCOPE_DEFINITION(EnableType, _, field, enable)
#define PER_ISOLATE_ASSERT_DISABLE_SCOPE_DEFINITION(_, DisableType, field, enable)
#define PER_ISOLATE_DCHECK_TYPE(V, enable)
#define PER_ISOLATE_CHECK_TYPE(V, enable)
constexpr bool contains(E element) const
Definition enum-set.h:35
static V8_EXPORT_PRIVATE bool IsAllowed()
V8_EXPORT_PRIVATE PerThreadAssertScope()
V8_EXPORT_PRIVATE ~PerThreadAssertScope()
PerThreadAssertScope< false, SAFEPOINTS_ASSERT, HEAP_ALLOCATION_ASSERT > DisallowGarbageCollectionInRelease
@ CODE_DEPENDENCY_CHANGE_ASSERT
@ HANDLE_USAGE_ON_ALL_THREADS_ASSERT
@ POSITION_INFO_SLOW_ASSERT
@ ASSERT_TYPE_IS_VALID_MARKER
@ HANDLE_ALLOCATION_ASSERT
@ HANDLE_DEREFERENCE_ASSERT
base::EnumSet< PerThreadAssertType, uint32_t > PerThreadAsserts
#define DCHECK(condition)
Definition logging.h:482