// Copyright 2013 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_COMMON_ASSERT_SCOPE_H_ #define V8_COMMON_ASSERT_SCOPE_H_ #include #include "src/base/macros.h" #include "src/base/optional.h" #include "src/common/globals.h" #include "src/utils/pointer-with-payload.h" namespace v8 { namespace internal { // Forward declarations. class Isolate; class PerThreadAssertData; template <> struct PointerWithPayloadTraits { static constexpr int value = 1; }; enum PerThreadAssertType { HEAP_ALLOCATION_ASSERT, HANDLE_ALLOCATION_ASSERT, HANDLE_DEREFERENCE_ASSERT, CODE_DEPENDENCY_CHANGE_ASSERT, LAST_PER_THREAD_ASSERT_TYPE }; enum PerIsolateAssertType { JAVASCRIPT_EXECUTION_ASSERT, JAVASCRIPT_EXECUTION_THROWS, JAVASCRIPT_EXECUTION_DUMP, DEOPTIMIZATION_ASSERT, COMPILATION_ASSERT, NO_EXCEPTION_ASSERT }; template class PerThreadAssertScope { public: V8_EXPORT_PRIVATE PerThreadAssertScope(); V8_EXPORT_PRIVATE ~PerThreadAssertScope(); V8_EXPORT_PRIVATE static bool IsAllowed(); void Release(); private: PointerWithPayload data_and_old_state_; V8_INLINE void set_data(PerThreadAssertData* data) { data_and_old_state_.SetPointer(data); } V8_INLINE PerThreadAssertData* data() const { return data_and_old_state_.GetPointer(); } V8_INLINE void set_old_state(bool old_state) { return data_and_old_state_.SetPayload(old_state); } V8_INLINE bool old_state() const { return data_and_old_state_.GetPayload(); } DISALLOW_COPY_AND_ASSIGN(PerThreadAssertScope); }; template class PerIsolateAssertScope { public: V8_EXPORT_PRIVATE explicit PerIsolateAssertScope(Isolate* isolate); V8_EXPORT_PRIVATE ~PerIsolateAssertScope(); static bool IsAllowed(Isolate* isolate); private: Isolate* isolate_; uint32_t old_data_; DISALLOW_COPY_AND_ASSIGN(PerIsolateAssertScope); }; template #ifdef DEBUG class PerThreadAssertScopeDebugOnly : public PerThreadAssertScope { #else class PerThreadAssertScopeDebugOnly { public: PerThreadAssertScopeDebugOnly() { // NOLINT (modernize-use-equals-default) // Define a constructor to avoid unused variable warnings. } void Release() {} #endif }; template #ifdef DEBUG class PerIsolateAssertScopeDebugOnly : public PerIsolateAssertScope { public: explicit PerIsolateAssertScopeDebugOnly(Isolate* isolate) : PerIsolateAssertScope(isolate) {} #else class PerIsolateAssertScopeDebugOnly { public: explicit PerIsolateAssertScopeDebugOnly(Isolate* isolate) {} #endif }; // Per-thread assert scopes. // Scope to document where we do not expect handles to be created. using DisallowHandleAllocation = PerThreadAssertScopeDebugOnly; // Scope to introduce an exception to DisallowHandleAllocation. using AllowHandleAllocation = PerThreadAssertScopeDebugOnly; // Scope to document where we do not expect any allocation and GC. using DisallowHeapAllocation = PerThreadAssertScopeDebugOnly; #ifdef DEBUG #define DISALLOW_HEAP_ALLOCATION(name) DisallowHeapAllocation name; #else #define DISALLOW_HEAP_ALLOCATION(name) #endif // Scope to introduce an exception to DisallowHeapAllocation. using AllowHeapAllocation = PerThreadAssertScopeDebugOnly; // Scope to document where we do not expect any handle dereferences. using DisallowHandleDereference = PerThreadAssertScopeDebugOnly; // Scope to introduce an exception to DisallowHandleDereference. using AllowHandleDereference = PerThreadAssertScopeDebugOnly; // Scope to document where we do not expect code dependencies to change. using DisallowCodeDependencyChange = PerThreadAssertScopeDebugOnly; // Scope to introduce an exception to DisallowCodeDependencyChange. using AllowCodeDependencyChange = PerThreadAssertScopeDebugOnly; class DisallowHeapAccess { DisallowCodeDependencyChange no_dependency_change_; DisallowHandleAllocation no_handle_allocation_; DisallowHandleDereference no_handle_dereference_; DisallowHeapAllocation no_heap_allocation_; }; class DisallowHeapAccessIf { public: explicit DisallowHeapAccessIf(bool condition) { if (condition) maybe_disallow_.emplace(); } private: base::Optional maybe_disallow_; }; // Per-isolate assert scopes. // Scope to document where we do not expect javascript execution. using DisallowJavascriptExecution = PerIsolateAssertScope; // Scope to introduce an exception to DisallowJavascriptExecution. using AllowJavascriptExecution = PerIsolateAssertScope; // Scope to document where we do not expect javascript execution (debug only) using DisallowJavascriptExecutionDebugOnly = PerIsolateAssertScopeDebugOnly; // Scope to introduce an exception to DisallowJavascriptExecutionDebugOnly. using AllowJavascriptExecutionDebugOnly = PerIsolateAssertScopeDebugOnly; // Scope in which javascript execution leads to exception being thrown. using ThrowOnJavascriptExecution = PerIsolateAssertScope; // Scope to introduce an exception to ThrowOnJavascriptExecution. using NoThrowOnJavascriptExecution = PerIsolateAssertScope; // Scope in which javascript execution causes dumps. using DumpOnJavascriptExecution = PerIsolateAssertScope; // Scope in which javascript execution causes dumps. using NoDumpOnJavascriptExecution = PerIsolateAssertScope; // Scope to document where we do not expect deoptimization. using DisallowDeoptimization = PerIsolateAssertScopeDebugOnly; // Scope to introduce an exception to DisallowDeoptimization. using AllowDeoptimization = PerIsolateAssertScopeDebugOnly; // Scope to document where we do not expect deoptimization. using DisallowCompilation = PerIsolateAssertScopeDebugOnly; // Scope to introduce an exception to DisallowDeoptimization. using AllowCompilation = PerIsolateAssertScopeDebugOnly; // Scope to document where we do not expect exceptions. using DisallowExceptions = PerIsolateAssertScopeDebugOnly; // Scope to introduce an exception to DisallowExceptions. using AllowExceptions = PerIsolateAssertScopeDebugOnly; // Explicit instantiation declarations. extern template class PerThreadAssertScope; extern template class PerThreadAssertScope; extern template class PerThreadAssertScope; extern template class PerThreadAssertScope; extern template class PerThreadAssertScope; extern template class PerThreadAssertScope; extern template class PerThreadAssertScope; extern template class PerThreadAssertScope; extern template class PerIsolateAssertScope; extern template class PerIsolateAssertScope; extern template class PerIsolateAssertScope; extern template class PerIsolateAssertScope; extern template class PerIsolateAssertScope; extern template class PerIsolateAssertScope; extern template class PerIsolateAssertScope; extern template class PerIsolateAssertScope; extern template class PerIsolateAssertScope; extern template class PerIsolateAssertScope; extern template class PerIsolateAssertScope; extern template class PerIsolateAssertScope; } // namespace internal } // namespace v8 #endif // V8_COMMON_ASSERT_SCOPE_H_