diff --git a/deps/depot_tools b/deps/depot_tools index 1dbd65b18..a6baf70a4 160000 --- a/deps/depot_tools +++ b/deps/depot_tools @@ -1 +1 @@ -Subproject commit 1dbd65b18a35b36d568fae41532013a894ed608a +Subproject commit a6baf70a45b04731ed99f47a0fbfcb5f28a1f03e diff --git a/deps/include/OWNERS b/deps/include/OWNERS index cd5fd0535..d85849d52 100644 --- a/deps/include/OWNERS +++ b/deps/include/OWNERS @@ -1,22 +1,15 @@ adamk@chromium.org cbruni@chromium.org -danno@chromium.org +leszeks@chromium.org mlippautz@chromium.org -ulan@chromium.org verwaest@chromium.org yangguo@chromium.org per-file *DEPS=file:../COMMON_OWNERS per-file v8-internal.h=file:../COMMON_OWNERS -per-file v8-inspector.h=dgozman@chromium.org -per-file v8-inspector.h=pfeldman@chromium.org -per-file v8-inspector.h=kozyatinskiy@chromium.org -per-file v8-inspector-protocol.h=dgozman@chromium.org -per-file v8-inspector-protocol.h=pfeldman@chromium.org -per-file v8-inspector-protocol.h=kozyatinskiy@chromium.org -per-file js_protocol.pdl=dgozman@chromium.org -per-file js_protocol.pdl=pfeldman@chromium.org -per-file js_protocol.pdl=bmeurer@chromium.org +per-file v8-inspector.h=file:../src/inspector/OWNERS +per-file v8-inspector-protocol.h=file:../src/inspector/OWNERS +per-file js_protocol.pdl=file:../src/inspector/OWNERS # For branch updates: per-file v8-version.h=file:../INFRA_OWNERS diff --git a/deps/include/cppgc/allocation.h b/deps/include/cppgc/allocation.h index b6f9d3902..d75f1a972 100644 --- a/deps/include/cppgc/allocation.h +++ b/deps/include/cppgc/allocation.h @@ -5,24 +5,21 @@ #ifndef INCLUDE_CPPGC_ALLOCATION_H_ #define INCLUDE_CPPGC_ALLOCATION_H_ -#include - #include +#include +#include +#include +#include +#include #include "cppgc/custom-space.h" -#include "cppgc/garbage-collected.h" #include "cppgc/internal/api-constants.h" #include "cppgc/internal/gc-info.h" +#include "cppgc/type-traits.h" +#include "v8config.h" // NOLINT(build/include_directory) namespace cppgc { -template -class MakeGarbageCollectedTraitBase; - -namespace internal { -class ObjectAllocator; -} // namespace internal - /** * AllocationHandle is used to allocate garbage-collected objects. */ @@ -43,6 +40,28 @@ class V8_EXPORT MakeGarbageCollectedTraitInternal { std::memory_order_release); } + template + struct SpacePolicy { + static void* Allocate(AllocationHandle& handle, size_t size) { + // Custom space. + static_assert(std::is_base_of::value, + "Custom space must inherit from CustomSpaceBase."); + return MakeGarbageCollectedTraitInternal::Allocate( + handle, size, internal::GCInfoTrait::Index(), + CustomSpace::kSpaceIndex); + } + }; + + template + struct SpacePolicy { + static void* Allocate(AllocationHandle& handle, size_t size) { + // Default space. + return MakeGarbageCollectedTraitInternal::Allocate( + handle, size, internal::GCInfoTrait::Index()); + } + }; + + private: static void* Allocate(cppgc::AllocationHandle& handle, size_t size, GCInfoIndex index); static void* Allocate(cppgc::AllocationHandle& handle, size_t size, @@ -71,27 +90,6 @@ class MakeGarbageCollectedTraitBase internal::api_constants::kLargeObjectSizeThreshold, "GarbageCollectedMixin may not be a large object"); - template - struct SpacePolicy { - static void* Allocate(AllocationHandle& handle, size_t size) { - // Custom space. - static_assert(std::is_base_of::value, - "Custom space must inherit from CustomSpaceBase."); - return internal::MakeGarbageCollectedTraitInternal::Allocate( - handle, size, internal::GCInfoTrait::Index(), - CustomSpace::kSpaceIndex); - } - }; - - template - struct SpacePolicy { - static void* Allocate(AllocationHandle& handle, size_t size) { - // Default space. - return internal::MakeGarbageCollectedTraitInternal::Allocate( - handle, size, internal::GCInfoTrait::Index()); - } - }; - protected: /** * Allocates memory for an object of type T. @@ -101,9 +99,15 @@ class MakeGarbageCollectedTraitBase * \param size The size that should be reserved for the object. * \returns the memory to construct an object of type T on. */ - static void* Allocate(AllocationHandle& handle, size_t size) { - return SpacePolicy::Space>::Allocate(handle, - size); + V8_INLINE static void* Allocate(AllocationHandle& handle, size_t size) { + static_assert( + std::is_base_of::value, + "U of GarbageCollected must be a base of T. Check " + "GarbageCollected base class inheritance."); + return SpacePolicy< + typename internal::GCInfoFolding< + T, typename T::ParentMostGarbageCollectedType>::ResultType, + typename SpaceTrait::Space>::Allocate(handle, size); } /** @@ -112,7 +116,7 @@ class MakeGarbageCollectedTraitBase * * \param payload The base pointer the object is allocated at. */ - static void MarkObjectAsFullyConstructed(const void* payload) { + V8_INLINE static void MarkObjectAsFullyConstructed(const void* payload) { internal::MakeGarbageCollectedTraitInternal::MarkObjectAsFullyConstructed( payload); } diff --git a/deps/include/cppgc/cross-thread-persistent.h b/deps/include/cppgc/cross-thread-persistent.h index 1f509d4b0..0a9afdcd2 100644 --- a/deps/include/cppgc/cross-thread-persistent.h +++ b/deps/include/cppgc/cross-thread-persistent.h @@ -13,12 +13,34 @@ #include "cppgc/visitor.h" namespace cppgc { - namespace internal { +// Wrapper around PersistentBase that allows accessing poisoned memory when +// using ASAN. This is needed as the GC of the heap that owns the value +// of a CTP, may clear it (heap termination, weakness) while the object +// holding the CTP may be poisoned as itself may be deemed dead. +class CrossThreadPersistentBase : public PersistentBase { + public: + CrossThreadPersistentBase() = default; + explicit CrossThreadPersistentBase(const void* raw) : PersistentBase(raw) {} + + V8_CLANG_NO_SANITIZE("address") const void* GetValueFromGC() const { + return raw_; + } + + V8_CLANG_NO_SANITIZE("address") + PersistentNode* GetNodeFromGC() const { return node_; } + + V8_CLANG_NO_SANITIZE("address") + void ClearFromGC() const { + raw_ = nullptr; + node_ = nullptr; + } +}; + template -class BasicCrossThreadPersistent final : public PersistentBase, +class BasicCrossThreadPersistent final : public CrossThreadPersistentBase, public LocationPolicy, private WeaknessPolicy, private CheckingPolicy { @@ -28,35 +50,54 @@ class BasicCrossThreadPersistent final : public PersistentBase, ~BasicCrossThreadPersistent() { Clear(); } - BasicCrossThreadPersistent( // NOLINT + BasicCrossThreadPersistent( const SourceLocation& loc = SourceLocation::Current()) : LocationPolicy(loc) {} - BasicCrossThreadPersistent( // NOLINT + BasicCrossThreadPersistent( std::nullptr_t, const SourceLocation& loc = SourceLocation::Current()) : LocationPolicy(loc) {} - BasicCrossThreadPersistent( // NOLINT + BasicCrossThreadPersistent( SentinelPointer s, const SourceLocation& loc = SourceLocation::Current()) - : PersistentBase(s), LocationPolicy(loc) {} + : CrossThreadPersistentBase(s), LocationPolicy(loc) {} - BasicCrossThreadPersistent( // NOLINT + BasicCrossThreadPersistent( T* raw, const SourceLocation& loc = SourceLocation::Current()) - : PersistentBase(raw), LocationPolicy(loc) { + : CrossThreadPersistentBase(raw), LocationPolicy(loc) { if (!IsValid(raw)) return; - PersistentRegion& region = this->GetPersistentRegion(raw); + PersistentRegionLock guard; + CrossThreadPersistentRegion& region = this->GetPersistentRegion(raw); SetNode(region.AllocateNode(this, &Trace)); this->CheckPointer(raw); } - BasicCrossThreadPersistent( // NOLINT + class UnsafeCtorTag { + private: + UnsafeCtorTag() = default; + template + friend class BasicCrossThreadPersistent; + }; + + BasicCrossThreadPersistent( + UnsafeCtorTag, T* raw, + const SourceLocation& loc = SourceLocation::Current()) + : CrossThreadPersistentBase(raw), LocationPolicy(loc) { + if (!IsValid(raw)) return; + CrossThreadPersistentRegion& region = this->GetPersistentRegion(raw); + SetNode(region.AllocateNode(this, &Trace)); + this->CheckPointer(raw); + } + + BasicCrossThreadPersistent( T& raw, const SourceLocation& loc = SourceLocation::Current()) : BasicCrossThreadPersistent(&raw, loc) {} template ::value>> - BasicCrossThreadPersistent( // NOLINT + BasicCrossThreadPersistent( internal::BasicMember member, @@ -75,7 +116,7 @@ class BasicCrossThreadPersistent final : public PersistentBase, template ::value>> - BasicCrossThreadPersistent( // NOLINT + BasicCrossThreadPersistent( const BasicCrossThreadPersistent& other, @@ -120,7 +161,7 @@ class BasicCrossThreadPersistent final : public PersistentBase, GetNode()->UpdateOwner(this); other.SetValue(nullptr); other.SetNode(nullptr); - this->CheckPointer(GetValue()); + this->CheckPointer(Get()); return *this; } @@ -173,9 +214,17 @@ class BasicCrossThreadPersistent final : public PersistentBase, const void* old_value = GetValue(); if (IsValid(old_value)) { PersistentRegionLock guard; - PersistentRegion& region = this->GetPersistentRegion(old_value); - region.FreeNode(GetNode()); - SetNode(nullptr); + old_value = GetValue(); + // The fast path check (IsValid()) does not acquire the lock. Reload + // the value to ensure the reference has not been cleared. + if (IsValid(old_value)) { + CrossThreadPersistentRegion& region = + this->GetPersistentRegion(old_value); + region.FreeNode(GetNode()); + SetNode(nullptr); + } else { + CPPGC_DCHECK(!GetNode()); + } } SetValue(nullptr); } @@ -209,7 +258,7 @@ class BasicCrossThreadPersistent final : public PersistentBase, * * \returns the object. */ - operator T*() const { return Get(); } // NOLINT + operator T*() const { return Get(); } /** * Dereferences the stored object. @@ -225,9 +274,12 @@ class BasicCrossThreadPersistent final : public PersistentBase, BasicCrossThreadPersistent To() const { + using OtherBasicCrossThreadPersistent = + BasicCrossThreadPersistent; PersistentRegionLock guard; - return BasicCrossThreadPersistent( + return OtherBasicCrossThreadPersistent( + typename OtherBasicCrossThreadPersistent::UnsafeCtorTag(), static_cast(Get())); } @@ -254,14 +306,22 @@ class BasicCrossThreadPersistent final : public PersistentBase, const void* old_value = GetValue(); if (IsValid(old_value)) { PersistentRegionLock guard; - PersistentRegion& region = this->GetPersistentRegion(old_value); - if (IsValid(ptr) && (®ion == &this->GetPersistentRegion(ptr))) { - SetValue(ptr); - this->CheckPointer(ptr); - return; + old_value = GetValue(); + // The fast path check (IsValid()) does not acquire the lock. Reload + // the value to ensure the reference has not been cleared. + if (IsValid(old_value)) { + CrossThreadPersistentRegion& region = + this->GetPersistentRegion(old_value); + if (IsValid(ptr) && (®ion == &this->GetPersistentRegion(ptr))) { + SetValue(ptr); + this->CheckPointer(ptr); + return; + } + region.FreeNode(GetNode()); + SetNode(nullptr); + } else { + CPPGC_DCHECK(!GetNode()); } - region.FreeNode(GetNode()); - SetNode(nullptr); } SetValue(ptr); if (!IsValid(ptr)) return; @@ -274,7 +334,8 @@ class BasicCrossThreadPersistent final : public PersistentBase, PersistentRegionLock::AssertLocked(); const void* old_value = GetValue(); if (IsValid(old_value)) { - PersistentRegion& region = this->GetPersistentRegion(old_value); + CrossThreadPersistentRegion& region = + this->GetPersistentRegion(old_value); if (IsValid(ptr) && (®ion == &this->GetPersistentRegion(ptr))) { SetValue(ptr); this->CheckPointer(ptr); @@ -290,12 +351,19 @@ class BasicCrossThreadPersistent final : public PersistentBase, } void ClearFromGC() const { - if (IsValid(GetValue())) { - WeaknessPolicy::GetPersistentRegion(GetValue()).FreeNode(GetNode()); - PersistentBase::ClearFromGC(); + if (IsValid(GetValueFromGC())) { + WeaknessPolicy::GetPersistentRegion(GetValueFromGC()) + .FreeNode(GetNodeFromGC()); + CrossThreadPersistentBase::ClearFromGC(); } } + // See Get() for details. + V8_CLANG_NO_SANITIZE("cfi-unrelated-cast") + T* GetFromGC() const { + return static_cast(const_cast(GetValueFromGC())); + } + friend class cppgc::Visitor; }; diff --git a/deps/include/cppgc/explicit-management.h b/deps/include/cppgc/explicit-management.h new file mode 100644 index 000000000..cdb6af485 --- /dev/null +++ b/deps/include/cppgc/explicit-management.h @@ -0,0 +1,82 @@ +// Copyright 2021 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 INCLUDE_CPPGC_EXPLICIT_MANAGEMENT_H_ +#define INCLUDE_CPPGC_EXPLICIT_MANAGEMENT_H_ + +#include + +#include "cppgc/allocation.h" +#include "cppgc/internal/logging.h" +#include "cppgc/type-traits.h" + +namespace cppgc { + +class HeapHandle; + +namespace internal { + +V8_EXPORT void FreeUnreferencedObject(HeapHandle&, void*); +V8_EXPORT bool Resize(void*, size_t); + +} // namespace internal + +namespace subtle { + +/** + * Informs the garbage collector that `object` can be immediately reclaimed. The + * destructor may not be invoked immediately but only on next garbage + * collection. + * + * It is up to the embedder to guarantee that no other object holds a reference + * to `object` after calling `FreeUnreferencedObject()`. In case such a + * reference exists, it's use results in a use-after-free. + * + * To aid in using the API, `FreeUnreferencedObject()` may be called from + * destructors on objects that would be reclaimed in the same garbage collection + * cycle. + * + * \param heap_handle The corresponding heap. + * \param object Reference to an object that is of type `GarbageCollected` and + * should be immediately reclaimed. + */ +template +void FreeUnreferencedObject(HeapHandle& heap_handle, T& object) { + static_assert(IsGarbageCollectedTypeV, + "Object must be of type GarbageCollected."); + internal::FreeUnreferencedObject(heap_handle, &object); +} + +/** + * Tries to resize `object` of type `T` with additional bytes on top of + * sizeof(T). Resizing is only useful with trailing inlined storage, see e.g. + * `MakeGarbageCollected(AllocationHandle&, AdditionalBytes)`. + * + * `Resize()` performs growing or shrinking as needed and may skip the operation + * for internal reasons, see return value. + * + * It is up to the embedder to guarantee that in case of shrinking a larger + * object down, the reclaimed area is not used anymore. Any subsequent use + * results in a use-after-free. + * + * The `object` must be live when calling `Resize()`. + * + * \param object Reference to an object that is of type `GarbageCollected` and + * should be resized. + * \param additional_bytes Bytes in addition to sizeof(T) that the object should + * provide. + * \returns true when the operation was successful and the result can be relied + * on, and false otherwise. + */ +template +bool Resize(T& object, AdditionalBytes additional_bytes) { + static_assert(IsGarbageCollectedTypeV, + "Object must be of type GarbageCollected."); + return internal::Resize(&object, sizeof(T) + additional_bytes.value); +} + +} // namespace subtle +} // namespace cppgc + +#endif // INCLUDE_CPPGC_EXPLICIT_MANAGEMENT_H_ diff --git a/deps/include/cppgc/garbage-collected.h b/deps/include/cppgc/garbage-collected.h index d28a39074..a3839e1ba 100644 --- a/deps/include/cppgc/garbage-collected.h +++ b/deps/include/cppgc/garbage-collected.h @@ -73,10 +73,11 @@ class GarbageCollectedBase { * }; * \endcode */ -template +template class GarbageCollected : public internal::GarbageCollectedBase { public: using IsGarbageCollectedTypeMarker = void; + using ParentMostGarbageCollectedType = T; protected: GarbageCollected() = default; diff --git a/deps/include/cppgc/heap-state.h b/deps/include/cppgc/heap-state.h index 0157282a5..3fd6b54a8 100644 --- a/deps/include/cppgc/heap-state.h +++ b/deps/include/cppgc/heap-state.h @@ -49,6 +49,17 @@ class V8_EXPORT HeapState final { */ static bool IsInAtomicPause(const HeapHandle& heap_handle); + /** + * Returns whether the last garbage collection was finalized conservatively + * (i.e., with a non-empty stack). This API is experimental and is expected to + * be removed in future. + * + * \param heap_handle The corresponding heap. + * \returns true if the last garbage collection was finalized conservatively, + * and false otherwise. + */ + static bool PreviousGCWasConservative(const HeapHandle& heap_handle); + private: HeapState() = delete; }; diff --git a/deps/include/cppgc/heap-statistics.h b/deps/include/cppgc/heap-statistics.h index cf8d6633c..8e626596e 100644 --- a/deps/include/cppgc/heap-statistics.h +++ b/deps/include/cppgc/heap-statistics.h @@ -5,7 +5,8 @@ #ifndef INCLUDE_CPPGC_HEAP_STATISTICS_H_ #define INCLUDE_CPPGC_HEAP_STATISTICS_H_ -#include +#include +#include #include #include @@ -30,19 +31,17 @@ struct HeapStatistics final { }; /** - * Statistics of object types. For each type the statistics record its name, - * how many objects of that type were allocated, and the overall size used by - * these objects. + * Object statistics for a single type. */ - struct ObjectStatistics { - /** Number of distinct types in the heap. */ - size_t num_types = 0; - /** Name of each type in the heap. */ - std::vector type_name; - /** Number of allocated objects per each type. */ - std::vector type_count; - /** Overall size of allocated objects per each type. */ - std::vector type_bytes; + struct ObjectStatsEntry { + /** + * Number of allocated bytes. + */ + size_t allocated_bytes; + /** + * Number of allocated objects. + */ + size_t object_count; }; /** @@ -50,14 +49,19 @@ struct HeapStatistics final { * allocated memory size and overall used memory size for the page. */ struct PageStatistics { - /** Overall amount of memory allocated for the page. */ - size_t physical_size_bytes = 0; + /** Overall committed amount of memory for the page. */ + size_t committed_size_bytes = 0; + /** Resident amount of memory held by the page. */ + size_t resident_size_bytes = 0; /** Amount of memory actually used on the page. */ size_t used_size_bytes = 0; + /** Statistics for object allocated on the page. Filled only when + * NameProvider::HideInternalNames() is false. */ + std::vector object_statistics; }; /** - * Stastistics of the freelist (used only in non-large object spaces). For + * Statistics of the freelist (used only in non-large object spaces). For * each bucket in the freelist the statistics record the bucket size, the * number of freelist entries in the bucket, and the overall allocated memory * consumed by these freelist entries. @@ -67,7 +71,7 @@ struct HeapStatistics final { std::vector bucket_size; /** number of freelist entries per bucket. */ std::vector free_count; - /** memory size concumed by freelist entries per size. */ + /** memory size consumed by freelist entries per size. */ std::vector free_size; }; @@ -80,29 +84,35 @@ struct HeapStatistics final { struct SpaceStatistics { /** The space name */ std::string name; - /** Overall amount of memory allocated for the space. */ - size_t physical_size_bytes = 0; + /** Overall committed amount of memory for the heap. */ + size_t committed_size_bytes = 0; + /** Resident amount of memory held by the heap. */ + size_t resident_size_bytes = 0; /** Amount of memory actually used on the space. */ size_t used_size_bytes = 0; /** Statistics for each of the pages in the space. */ std::vector page_stats; /** Statistics for the freelist of the space. */ FreeListStatistics free_list_stats; - /** Statistics for object allocated on the space. Filled only when - * NameProvider::HideInternalNames() is false. */ - ObjectStatistics object_stats; }; - /** Overall amount of memory allocated for the heap. */ - size_t physical_size_bytes = 0; + /** Overall committed amount of memory for the heap. */ + size_t committed_size_bytes = 0; + /** Resident amount of memory help by the heap. */ + size_t resident_size_bytes = 0; /** Amount of memory actually used on the heap. */ size_t used_size_bytes = 0; /** Detail level of this HeapStatistics. */ DetailLevel detail_level; /** Statistics for each of the spaces in the heap. Filled only when - * detail_level is kDetailed. */ + * `detail_level` is `DetailLevel::kDetailed`. */ std::vector space_stats; + + /** + * Vector of `cppgc::GarbageCollected` type names. + */ + std::vector type_names; }; } // namespace cppgc diff --git a/deps/include/cppgc/heap.h b/deps/include/cppgc/heap.h index fd0512f1f..136c4fb44 100644 --- a/deps/include/cppgc/heap.h +++ b/deps/include/cppgc/heap.h @@ -5,6 +5,8 @@ #ifndef INCLUDE_CPPGC_HEAP_H_ #define INCLUDE_CPPGC_HEAP_H_ +#include +#include #include #include diff --git a/deps/include/cppgc/internal/api-constants.h b/deps/include/cppgc/internal/api-constants.h index a70f00710..7253a4708 100644 --- a/deps/include/cppgc/internal/api-constants.h +++ b/deps/include/cppgc/internal/api-constants.h @@ -5,8 +5,8 @@ #ifndef INCLUDE_CPPGC_INTERNAL_API_CONSTANTS_H_ #define INCLUDE_CPPGC_INTERNAL_API_CONSTANTS_H_ -#include -#include +#include +#include #include "v8config.h" // NOLINT(build/include_directory) diff --git a/deps/include/cppgc/internal/compiler-specific.h b/deps/include/cppgc/internal/compiler-specific.h index c580894b3..595b6398c 100644 --- a/deps/include/cppgc/internal/compiler-specific.h +++ b/deps/include/cppgc/internal/compiler-specific.h @@ -21,13 +21,13 @@ namespace cppgc { // [[no_unique_address]] comes in C++20 but supported in clang with -std >= // c++11. -#if CPPGC_HAS_CPP_ATTRIBUTE(no_unique_address) // NOLINTNEXTLINE +#if CPPGC_HAS_CPP_ATTRIBUTE(no_unique_address) #define CPPGC_NO_UNIQUE_ADDRESS [[no_unique_address]] #else #define CPPGC_NO_UNIQUE_ADDRESS #endif -#if CPPGC_HAS_ATTRIBUTE(unused) // NOLINTNEXTLINE +#if CPPGC_HAS_ATTRIBUTE(unused) #define CPPGC_UNUSED __attribute__((unused)) #else #define CPPGC_UNUSED diff --git a/deps/include/cppgc/internal/gc-info.h b/deps/include/cppgc/internal/gc-info.h index 9c26d6aa5..0830b1949 100644 --- a/deps/include/cppgc/internal/gc-info.h +++ b/deps/include/cppgc/internal/gc-info.h @@ -5,7 +5,9 @@ #ifndef INCLUDE_CPPGC_INTERNAL_GC_INFO_H_ #define INCLUDE_CPPGC_INTERNAL_GC_INFO_H_ -#include +#include +#include +#include #include "cppgc/internal/finalizer-trait.h" #include "cppgc/internal/name-trait.h" @@ -17,27 +19,54 @@ namespace internal { using GCInfoIndex = uint16_t; -class V8_EXPORT RegisteredGCInfoIndex final { - public: - RegisteredGCInfoIndex(FinalizationCallback finalization_callback, - TraceCallback trace_callback, - NameCallback name_callback, bool has_v_table); - GCInfoIndex GetIndex() const { return index_; } +// Acquires a new GC info object and returns the index. In addition, also +// updates `registered_index` atomically. +V8_EXPORT GCInfoIndex +EnsureGCInfoIndex(std::atomic& registered_index, + FinalizationCallback, TraceCallback, NameCallback, bool); - private: - const GCInfoIndex index_; +// Fold types based on finalizer behavior. Note that finalizer characteristics +// align with trace behavior, i.e., destructors are virtual when trace methods +// are and vice versa. +template +struct GCInfoFolding { + static constexpr bool kHasVirtualDestructorAtBase = + std::has_virtual_destructor::value; + static constexpr bool kBothTypesAreTriviallyDestructible = + std::is_trivially_destructible::value && + std::is_trivially_destructible::value; + static constexpr bool kHasCustomFinalizerDispatchAtBase = + internal::HasFinalizeGarbageCollectedObject< + ParentMostGarbageCollectedType>::value; +#ifdef CPPGC_SUPPORTS_OBJECT_NAMES + static constexpr bool kWantsDetailedObjectNames = true; +#else // !CPPGC_SUPPORTS_OBJECT_NAMES + static constexpr bool kWantsDetailedObjectNames = false; +#endif // !CPPGC_SUPPORTS_OBJECT_NAMES + + // Folding would regresses name resolution when deriving names from C++ + // class names as it would just folds a name to the base class name. + using ResultType = std::conditional_t<(kHasVirtualDestructorAtBase || + kBothTypesAreTriviallyDestructible || + kHasCustomFinalizerDispatchAtBase) && + !kWantsDetailedObjectNames, + ParentMostGarbageCollectedType, T>; }; // Trait determines how the garbage collector treats objects wrt. to traversing, // finalization, and naming. template -struct GCInfoTrait { +struct GCInfoTrait final { static GCInfoIndex Index() { static_assert(sizeof(T), "T must be fully defined"); - static const RegisteredGCInfoIndex registered_index( - FinalizerTrait::kCallback, TraceTrait::Trace, - NameTrait::GetName, std::is_polymorphic::value); - return registered_index.GetIndex(); + static std::atomic + registered_index; // Uses zero initialization. + const GCInfoIndex index = registered_index.load(std::memory_order_acquire); + return index ? index + : EnsureGCInfoIndex( + registered_index, FinalizerTrait::kCallback, + TraceTrait::Trace, NameTrait::GetName, + std::is_polymorphic::value); } }; diff --git a/deps/include/cppgc/internal/name-trait.h b/deps/include/cppgc/internal/name-trait.h index ae99d41c0..2e2da1eab 100644 --- a/deps/include/cppgc/internal/name-trait.h +++ b/deps/include/cppgc/internal/name-trait.h @@ -73,7 +73,7 @@ class NameTrait final : public NameTraitBase { private: static HeapObjectName GetNameFor(const NameProvider* name_provider) { - return {name_provider->GetName(), false}; + return {name_provider->GetHumanReadableName(), false}; } static HeapObjectName GetNameFor(...) { @@ -91,7 +91,7 @@ class NameTrait final : public NameTraitBase { static const HeapObjectName leaky_name = GetNameFromTypeSignature(PRETTY_FUNCTION_VALUE); - return leaky_name; + return {leaky_name, false}; #undef PRETTY_FUNCTION_VALUE diff --git a/deps/include/cppgc/internal/persistent-node.h b/deps/include/cppgc/internal/persistent-node.h index 6524f326a..b5dba476a 100644 --- a/deps/include/cppgc/internal/persistent-node.h +++ b/deps/include/cppgc/internal/persistent-node.h @@ -19,6 +19,8 @@ class Visitor; namespace internal { +class CrossThreadPersistentRegion; + // PersistentNode represents a variant of two states: // 1) traceable node with a back pointer to the Persistent object; // 2) freelist entry. @@ -30,6 +32,7 @@ class PersistentNode final { PersistentNode& operator=(const PersistentNode&) = delete; void InitializeAsUsedNode(void* owner, TraceCallback trace) { + CPPGC_DCHECK(trace); owner_ = owner; trace_ = trace; } @@ -72,7 +75,7 @@ class PersistentNode final { TraceCallback trace_ = nullptr; }; -class V8_EXPORT PersistentRegion final { +class V8_EXPORT PersistentRegion { using PersistentNodeSlots = std::array; public: @@ -89,12 +92,15 @@ class V8_EXPORT PersistentRegion final { } PersistentNode* node = free_list_head_; free_list_head_ = free_list_head_->FreeListNext(); + CPPGC_DCHECK(!node->IsUsed()); node->InitializeAsUsedNode(owner, trace); nodes_in_use_++; return node; } void FreeNode(PersistentNode* node) { + CPPGC_DCHECK(node); + CPPGC_DCHECK(node->IsUsed()); node->InitializeAsFreeNode(free_list_head_); free_list_head_ = node; CPPGC_DCHECK(nodes_in_use_ > 0); @@ -110,9 +116,14 @@ class V8_EXPORT PersistentRegion final { private: void EnsureNodeSlots(); + template + void ClearAllUsedNodes(); + std::vector> nodes_; PersistentNode* free_list_head_ = nullptr; size_t nodes_in_use_ = 0; + + friend class CrossThreadPersistentRegion; }; // CrossThreadPersistent uses PersistentRegion but protects it using this lock @@ -125,6 +136,35 @@ class V8_EXPORT PersistentRegionLock final { static void AssertLocked(); }; +// Variant of PersistentRegion that checks whether the PersistentRegionLock is +// locked. +class V8_EXPORT CrossThreadPersistentRegion final : protected PersistentRegion { + public: + CrossThreadPersistentRegion() = default; + // Clears Persistent fields to avoid stale pointers after heap teardown. + ~CrossThreadPersistentRegion(); + + CrossThreadPersistentRegion(const CrossThreadPersistentRegion&) = delete; + CrossThreadPersistentRegion& operator=(const CrossThreadPersistentRegion&) = + delete; + + V8_INLINE PersistentNode* AllocateNode(void* owner, TraceCallback trace) { + PersistentRegionLock::AssertLocked(); + return PersistentRegion::AllocateNode(owner, trace); + } + + V8_INLINE void FreeNode(PersistentNode* node) { + PersistentRegionLock::AssertLocked(); + PersistentRegion::FreeNode(node); + } + + void Trace(Visitor*); + + size_t NodesInUse() const; + + void ClearAllUsedNodes(); +}; + } // namespace internal } // namespace cppgc diff --git a/deps/include/cppgc/internal/pointer-policies.h b/deps/include/cppgc/internal/pointer-policies.h index ea86a0a70..cdf0bb693 100644 --- a/deps/include/cppgc/internal/pointer-policies.h +++ b/deps/include/cppgc/internal/pointer-policies.h @@ -9,13 +9,17 @@ #include #include "cppgc/internal/write-barrier.h" +#include "cppgc/sentinel-pointer.h" #include "cppgc/source-location.h" +#include "cppgc/type-traits.h" #include "v8config.h" // NOLINT(build/include_directory) namespace cppgc { namespace internal { +class HeapBase; class PersistentRegion; +class CrossThreadPersistentRegion; // Tags to distinguish between strong and weak member types. class StrongMemberTag; @@ -49,23 +53,49 @@ struct NoWriteBarrierPolicy { class V8_EXPORT EnabledCheckingPolicy { protected: - EnabledCheckingPolicy(); - void CheckPointer(const void* ptr); + template + void CheckPointer(const T* ptr) { + if (!ptr || (kSentinelPointer == ptr)) return; + + CheckPointersImplTrampoline::Call(this, ptr); + } private: - void* impl_; + void CheckPointerImpl(const void* ptr, bool points_to_payload); + + template > + struct CheckPointersImplTrampoline { + static void Call(EnabledCheckingPolicy* policy, const T* ptr) { + policy->CheckPointerImpl(ptr, false); + } + }; + + template + struct CheckPointersImplTrampoline { + static void Call(EnabledCheckingPolicy* policy, const T* ptr) { + policy->CheckPointerImpl(ptr, IsGarbageCollectedTypeV); + } + }; + + const HeapBase* heap_ = nullptr; }; class DisabledCheckingPolicy { protected: - void CheckPointer(const void* raw) {} + void CheckPointer(const void*) {} }; #if V8_ENABLE_CHECKS -using DefaultCheckingPolicy = EnabledCheckingPolicy; +using DefaultMemberCheckingPolicy = EnabledCheckingPolicy; +using DefaultPersistentCheckingPolicy = EnabledCheckingPolicy; #else -using DefaultCheckingPolicy = DisabledCheckingPolicy; +using DefaultMemberCheckingPolicy = DisabledCheckingPolicy; +using DefaultPersistentCheckingPolicy = DisabledCheckingPolicy; #endif +// For CT(W)P neither marking information (for value), nor objectstart bitmap +// (for slot) are guaranteed to be present because there's no synchonization +// between heaps after marking. +using DefaultCrossThreadPersistentCheckingPolicy = DisabledCheckingPolicy; class KeepLocationPolicy { public: @@ -115,25 +145,27 @@ struct WeakPersistentPolicy { struct StrongCrossThreadPersistentPolicy { using IsStrongPersistent = std::true_type; - static V8_EXPORT PersistentRegion& GetPersistentRegion(const void* object); + static V8_EXPORT CrossThreadPersistentRegion& GetPersistentRegion( + const void* object); }; struct WeakCrossThreadPersistentPolicy { using IsStrongPersistent = std::false_type; - static V8_EXPORT PersistentRegion& GetPersistentRegion(const void* object); + static V8_EXPORT CrossThreadPersistentRegion& GetPersistentRegion( + const void* object); }; // Forward declarations setting up the default policies. template + typename CheckingPolicy = DefaultCrossThreadPersistentCheckingPolicy> class BasicCrossThreadPersistent; template + typename CheckingPolicy = DefaultPersistentCheckingPolicy> class BasicPersistent; template + typename CheckingPolicy = DefaultMemberCheckingPolicy> class BasicMember; } // namespace internal diff --git a/deps/include/cppgc/internal/write-barrier.h b/deps/include/cppgc/internal/write-barrier.h index f3aaedb1b..47460ee13 100644 --- a/deps/include/cppgc/internal/write-barrier.h +++ b/deps/include/cppgc/internal/write-barrier.h @@ -5,9 +5,13 @@ #ifndef INCLUDE_CPPGC_INTERNAL_WRITE_BARRIER_H_ #define INCLUDE_CPPGC_INTERNAL_WRITE_BARRIER_H_ +#include +#include + #include "cppgc/heap-state.h" #include "cppgc/internal/api-constants.h" #include "cppgc/internal/atomic-entry-flag.h" +#include "cppgc/platform.h" #include "cppgc/sentinel-pointer.h" #include "cppgc/trace-trait.h" #include "v8config.h" // NOLINT(build/include_directory) @@ -22,8 +26,11 @@ class HeapHandle; namespace internal { +#if defined(CPPGC_CAGED_HEAP) class WriteBarrierTypeForCagedHeapPolicy; +#else // !CPPGC_CAGED_HEAP class WriteBarrierTypeForNonCagedHeapPolicy; +#endif // !CPPGC_CAGED_HEAP class V8_EXPORT WriteBarrier final { public: @@ -161,6 +168,9 @@ class V8_EXPORT WriteBarrierTypeForCagedHeapPolicy final { static V8_INLINE bool TryGetCagedHeap(const void* slot, const void* value, WriteBarrier::Params& params) { + // TODO(chromium:1056170): Check if the null check can be folded in with + // the rest of the write barrier. + if (!value) return false; params.start = reinterpret_cast(value) & ~(api_constants::kCagedHeapReservationAlignment - 1); const uintptr_t slot_offset = diff --git a/deps/include/cppgc/liveness-broker.h b/deps/include/cppgc/liveness-broker.h index e44909128..c94eef0d4 100644 --- a/deps/include/cppgc/liveness-broker.h +++ b/deps/include/cppgc/liveness-broker.h @@ -44,7 +44,10 @@ class V8_EXPORT LivenessBroker final { public: template bool IsHeapObjectAlive(const T* object) const { - return object && + // nullptr objects are considered alive to allow weakness to be used from + // stack while running into a conservative GC. Treating nullptr as dead + // would mean that e.g. custom collectins could not be strongified on stack. + return !object || IsHeapObjectAliveImpl( TraceTrait::GetTraceDescriptor(object).base_object_payload); } diff --git a/deps/include/cppgc/macros.h b/deps/include/cppgc/macros.h index 70ab44c65..030f397e3 100644 --- a/deps/include/cppgc/macros.h +++ b/deps/include/cppgc/macros.h @@ -5,7 +5,7 @@ #ifndef INCLUDE_CPPGC_MACROS_H_ #define INCLUDE_CPPGC_MACROS_H_ -#include +#include #include "cppgc/internal/compiler-specific.h" diff --git a/deps/include/cppgc/member.h b/deps/include/cppgc/member.h index 7b76bc4f7..38105b8e4 100644 --- a/deps/include/cppgc/member.h +++ b/deps/include/cppgc/member.h @@ -24,8 +24,11 @@ namespace internal { // BasicMember on casting to the right type as needed. class MemberBase { protected: + struct AtomicInitializerTag {}; + MemberBase() = default; explicit MemberBase(const void* value) : raw_(value) {} + MemberBase(const void* value, AtomicInitializerTag) { SetRawAtomic(value); } const void** GetRawSlot() const { return &raw_; } const void* GetRaw() const { return raw_; } @@ -61,6 +64,20 @@ class BasicMember final : private MemberBase, private CheckingPolicy { this->CheckPointer(Get()); } BasicMember(T& raw) : BasicMember(&raw) {} // NOLINT + // Atomic ctor. Using the AtomicInitializerTag forces BasicMember to + // initialize using atomic assignments. This is required for preventing + // data races with concurrent marking. + using AtomicInitializerTag = MemberBase::AtomicInitializerTag; + BasicMember(std::nullptr_t, AtomicInitializerTag atomic) + : MemberBase(nullptr, atomic) {} + BasicMember(SentinelPointer s, AtomicInitializerTag atomic) + : MemberBase(s, atomic) {} + BasicMember(T* raw, AtomicInitializerTag atomic) : MemberBase(raw, atomic) { + InitializingWriteBarrier(); + this->CheckPointer(Get()); + } + BasicMember(T& raw, AtomicInitializerTag atomic) + : BasicMember(&raw, atomic) {} // Copy ctor. BasicMember(const BasicMember& other) : BasicMember(other.Get()) {} // Allow heterogeneous construction. @@ -79,9 +96,8 @@ class BasicMember final : private MemberBase, private CheckingPolicy { template ::value>> - BasicMember( // NOLINT - BasicMember&& other) noexcept + BasicMember(BasicMember&& other) noexcept : BasicMember(other.Get()) { other.Clear(); } @@ -90,10 +106,9 @@ class BasicMember final : private MemberBase, private CheckingPolicy { typename PersistentLocationPolicy, typename PersistentCheckingPolicy, typename = std::enable_if_t::value>> - BasicMember( // NOLINT - const BasicPersistent& - p) + BasicMember(const BasicPersistent& p) : BasicMember(p.Get()) {} // Copy assignment. @@ -161,7 +176,7 @@ class BasicMember final : private MemberBase, private CheckingPolicy { } explicit operator bool() const { return Get(); } - operator T*() const { return Get(); } // NOLINT + operator T*() const { return Get(); } T* operator->() const { return Get(); } T& operator*() const { return *Get(); } @@ -203,6 +218,8 @@ class BasicMember final : private MemberBase, private CheckingPolicy { void ClearFromGC() const { MemberBase::ClearFromGC(); } + T* GetFromGC() const { return Get(); } + friend class cppgc::Visitor; template friend struct cppgc::TraceTrait; @@ -211,20 +228,20 @@ class BasicMember final : private MemberBase, private CheckingPolicy { template -bool operator==( - BasicMember member1, - BasicMember - member2) { +bool operator==(const BasicMember& member1, + const BasicMember& member2) { return member1.Get() == member2.Get(); } template -bool operator!=( - BasicMember member1, - BasicMember - member2) { +bool operator!=(const BasicMember& member1, + const BasicMember& member2) { return !(member1 == member2); } diff --git a/deps/include/cppgc/name-provider.h b/deps/include/cppgc/name-provider.h index 8b70b8ea5..224dd4b5d 100644 --- a/deps/include/cppgc/name-provider.h +++ b/deps/include/cppgc/name-provider.h @@ -57,7 +57,7 @@ class V8_EXPORT NameProvider { * * @returns a human readable name for the object. */ - virtual const char* GetName() const = 0; + virtual const char* GetHumanReadableName() const = 0; }; } // namespace cppgc diff --git a/deps/include/cppgc/persistent.h b/deps/include/cppgc/persistent.h index d7aac723c..b83a46457 100644 --- a/deps/include/cppgc/persistent.h +++ b/deps/include/cppgc/persistent.h @@ -41,7 +41,7 @@ class PersistentBase { node_ = nullptr; } - private: + protected: mutable const void* raw_ = nullptr; mutable PersistentNode* node_ = nullptr; @@ -95,7 +95,7 @@ class BasicPersistent final : public PersistentBase, template ::value>> - BasicPersistent( // NOLINT + BasicPersistent( const BasicPersistent& other, const SourceLocation& loc = SourceLocation::Current()) @@ -118,7 +118,7 @@ class BasicPersistent final : public PersistentBase, template ::value>> - BasicPersistent(internal::BasicMember member, const SourceLocation& loc = SourceLocation::Current()) @@ -141,7 +141,7 @@ class BasicPersistent final : public PersistentBase, } // Move assignment. - BasicPersistent& operator=(BasicPersistent&& other) { + BasicPersistent& operator=(BasicPersistent&& other) noexcept { if (this == &other) return *this; Clear(); PersistentBase::operator=(std::move(other)); @@ -181,7 +181,7 @@ class BasicPersistent final : public PersistentBase, } explicit operator bool() const { return Get(); } - operator T*() const { return Get(); } // NOLINT + operator T*() const { return Get(); } T* operator->() const { return Get(); } T& operator*() const { return *Get(); } @@ -259,6 +259,12 @@ class BasicPersistent final : public PersistentBase, } } + // Set Get() for details. + V8_CLANG_NO_SANITIZE("cfi-unrelated-cast") + T* GetFromGC() const { + return static_cast(const_cast(GetValue())); + } + friend class cppgc::Visitor; }; diff --git a/deps/include/cppgc/platform.h b/deps/include/cppgc/platform.h index 0d7377668..3276a26b6 100644 --- a/deps/include/cppgc/platform.h +++ b/deps/include/cppgc/platform.h @@ -5,6 +5,8 @@ #ifndef INCLUDE_CPPGC_PLATFORM_H_ #define INCLUDE_CPPGC_PLATFORM_H_ +#include + #include "v8-platform.h" // NOLINT(build/include_directory) #include "v8config.h" // NOLINT(build/include_directory) @@ -146,6 +148,7 @@ namespace internal { V8_EXPORT void Abort(); } // namespace internal + } // namespace cppgc #endif // INCLUDE_CPPGC_PLATFORM_H_ diff --git a/deps/include/cppgc/sentinel-pointer.h b/deps/include/cppgc/sentinel-pointer.h index f7915834e..b049d1a2b 100644 --- a/deps/include/cppgc/sentinel-pointer.h +++ b/deps/include/cppgc/sentinel-pointer.h @@ -14,7 +14,7 @@ namespace internal { // sentinel is defined by the embedder. struct SentinelPointer { template - operator T*() const { // NOLINT + operator T*() const { static constexpr intptr_t kSentinelValue = 1; return reinterpret_cast(kSentinelValue); } diff --git a/deps/include/cppgc/source-location.h b/deps/include/cppgc/source-location.h index 29d69b0a1..da5a5ede5 100644 --- a/deps/include/cppgc/source-location.h +++ b/deps/include/cppgc/source-location.h @@ -5,6 +5,7 @@ #ifndef INCLUDE_CPPGC_SOURCE_LOCATION_H_ #define INCLUDE_CPPGC_SOURCE_LOCATION_H_ +#include #include #include "v8config.h" // NOLINT(build/include_directory) diff --git a/deps/include/cppgc/testing.h b/deps/include/cppgc/testing.h index f93897a9a..229ce140f 100644 --- a/deps/include/cppgc/testing.h +++ b/deps/include/cppgc/testing.h @@ -44,6 +44,55 @@ class V8_EXPORT V8_NODISCARD OverrideEmbedderStackStateScope final { HeapHandle& heap_handle_; }; +/** + * Testing interface for managed heaps that allows for controlling garbage + * collection timings. Embedders should use this class when testing the + * interaction of their code with incremental/concurrent garbage collection. + */ +class V8_EXPORT StandaloneTestingHeap final { + public: + explicit StandaloneTestingHeap(HeapHandle&); + + /** + * Start an incremental garbage collection. + */ + void StartGarbageCollection(); + + /** + * Perform an incremental step. This will also schedule concurrent steps if + * needed. + * + * \param stack_state The state of the stack during the step. + */ + bool PerformMarkingStep(EmbedderStackState stack_state); + + /** + * Finalize the current garbage collection cycle atomically. + * Assumes that garbage collection is in progress. + * + * \param stack_state The state of the stack for finalizing the garbage + * collection cycle. + */ + void FinalizeGarbageCollection(EmbedderStackState stack_state); + + /** + * Toggle main thread marking on/off. Allows to stress concurrent marking + * (e.g. to better detect data races). + * + * \param should_mark Denotes whether the main thread should contribute to + * marking. Defaults to true. + */ + void ToggleMainThreadMarking(bool should_mark); + + /** + * Force enable compaction for the next garbage collection cycle. + */ + void ForceCompactionForNextGarbageCollection(); + + private: + HeapHandle& heap_handle_; +}; + } // namespace testing } // namespace cppgc diff --git a/deps/include/cppgc/type-traits.h b/deps/include/cppgc/type-traits.h index 2b50a2164..56cd55d61 100644 --- a/deps/include/cppgc/type-traits.h +++ b/deps/include/cppgc/type-traits.h @@ -7,6 +7,7 @@ // This file should stay with minimal dependencies to allow embedder to check // against Oilpan types without including any other parts. +#include #include namespace cppgc { @@ -164,6 +165,18 @@ struct IsUntracedMemberType : std::false_type {}; template struct IsUntracedMemberType : std::true_type {}; +template +struct IsComplete { + private: + template + static std::true_type IsSizeOfKnown(U*); + static std::false_type IsSizeOfKnown(...); + + public: + static constexpr bool value = + decltype(IsSizeOfKnown(std::declval()))::value; +}; + } // namespace internal /** @@ -223,6 +236,12 @@ constexpr bool IsWeakMemberTypeV = internal::IsWeakMemberType::value; template constexpr bool IsWeakV = internal::IsWeak::value; +/** + * Value is true for types that are complete, and false otherwise. + */ +template +constexpr bool IsCompleteV = internal::IsComplete::value; + } // namespace cppgc #endif // INCLUDE_CPPGC_TYPE_TRAITS_H_ diff --git a/deps/include/cppgc/visitor.h b/deps/include/cppgc/visitor.h index 95fd5fc84..57e2ce396 100644 --- a/deps/include/cppgc/visitor.h +++ b/deps/include/cppgc/visitor.h @@ -12,6 +12,7 @@ #include "cppgc/internal/pointer-policies.h" #include "cppgc/liveness-broker.h" #include "cppgc/member.h" +#include "cppgc/sentinel-pointer.h" #include "cppgc/source-location.h" #include "cppgc/trace-trait.h" #include "cppgc/type-traits.h" @@ -158,22 +159,67 @@ class V8_EXPORT Visitor { } /** - * Trace method for ephemerons. Used for tracing raw ephemeron in which the - * key and value are kept separately. + * Trace method for a single ephemeron. Used for tracing a raw ephemeron in + * which the `key` and `value` are kept separately. * - * \param key WeakMember reference weakly retaining a key object. - * \param value Member reference weakly retaining a value object. + * \param weak_member_key WeakMember reference weakly retaining a key object. + * \param member_value Member reference with ephemeron semantics. */ - template - void TraceEphemeron(const WeakMember& key, const V* value) { - const K* k = key.GetRawAtomic(); - if (!k) return; - TraceDescriptor value_desc = TraceTrait::GetTraceDescriptor(value); - // `value` must always be non-null. `value_desc.base_object_payload` may be - // null in the case that value is not a garbage-collected object but only - // traceable. + template + void TraceEphemeron(const WeakMember& weak_member_key, + const Member* member_value) { + const KeyType* key = weak_member_key.GetRawAtomic(); + if (!key) return; + + // `value` must always be non-null. + CPPGC_DCHECK(member_value); + const ValueType* value = member_value->GetRawAtomic(); + if (!value) return; + + // KeyType and ValueType may refer to GarbageCollectedMixin. + TraceDescriptor value_desc = + TraceTrait::GetTraceDescriptor(value); + CPPGC_DCHECK(value_desc.base_object_payload); + const void* key_base_object_payload = + TraceTrait::GetTraceDescriptor(key).base_object_payload; + CPPGC_DCHECK(key_base_object_payload); + + VisitEphemeron(key_base_object_payload, value, value_desc); + } + + /** + * Trace method for a single ephemeron. Used for tracing a raw ephemeron in + * which the `key` and `value` are kept separately. Note that this overload + * is for non-GarbageCollected `value`s that can be traced though. + * + * \param key `WeakMember` reference weakly retaining a key object. + * \param value Reference weakly retaining a value object. Note that + * `ValueType` here should not be `Member`. It is expected that + * `TraceTrait::GetTraceDescriptor(value)` returns a + * `TraceDescriptor` with a null base pointer but a valid trace method. + */ + template + void TraceEphemeron(const WeakMember& weak_member_key, + const ValueType* value) { + static_assert(!IsGarbageCollectedOrMixinTypeV, + "garbage-collected types must use WeakMember and Member"); + const KeyType* key = weak_member_key.GetRawAtomic(); + if (!key) return; + + // `value` must always be non-null. CPPGC_DCHECK(value); - VisitEphemeron(key, value, value_desc); + TraceDescriptor value_desc = + TraceTrait::GetTraceDescriptor(value); + // `value_desc.base_object_payload` must be null as this override is only + // taken for non-garbage-collected values. + CPPGC_DCHECK(!value_desc.base_object_payload); + + // KeyType might be a GarbageCollectedMixin. + const void* key_base_object_payload = + TraceTrait::GetTraceDescriptor(key).base_object_payload; + CPPGC_DCHECK(key_base_object_payload); + + VisitEphemeron(key_base_object_payload, value, value_desc); } /** @@ -273,10 +319,10 @@ class V8_EXPORT Visitor { template static void HandleWeak(const LivenessBroker& info, const void* object) { const PointerType* weak = static_cast(object); + auto* raw_ptr = weak->GetFromGC(); // Sentinel values are preserved for weak pointers. - if (*weak == kSentinelPointer) return; - const auto* raw = weak->Get(); - if (!info.IsHeapObjectAlive(raw)) { + if (raw_ptr == kSentinelPointer) return; + if (!info.IsHeapObjectAlive(raw_ptr)) { weak->ClearFromGC(); } } @@ -290,11 +336,11 @@ class V8_EXPORT Visitor { static_assert(internal::IsGarbageCollectedOrMixinType::value, "Persistent's pointee type must be GarbageCollected or " "GarbageCollectedMixin"); - if (!p.Get()) { + auto* ptr = p.GetFromGC(); + if (!ptr) { return; } - VisitRoot(p.Get(), TraceTrait::GetTraceDescriptor(p.Get()), - loc); + VisitRoot(ptr, TraceTrait::GetTraceDescriptor(ptr), loc); } template < @@ -309,7 +355,8 @@ class V8_EXPORT Visitor { "GarbageCollectedMixin"); static_assert(!internal::IsAllocatedOnCompactableSpace::value, "Weak references to compactable objects are not allowed"); - VisitWeakRoot(p.Get(), TraceTrait::GetTraceDescriptor(p.Get()), + auto* ptr = p.GetFromGC(); + VisitWeakRoot(ptr, TraceTrait::GetTraceDescriptor(ptr), &HandleWeak, &p, loc); } @@ -327,14 +374,6 @@ class V8_EXPORT Visitor { friend class internal::VisitorBase; }; -template -struct TraceTrait> { - static TraceDescriptor GetTraceDescriptor(const void* self) { - return TraceTrait::GetTraceDescriptor( - static_cast*>(self)->GetRawAtomic()); - } -}; - } // namespace cppgc #endif // INCLUDE_CPPGC_VISITOR_H_ diff --git a/deps/include/js_protocol.pdl b/deps/include/js_protocol.pdl index 666952f27..ebf9eb7fe 100644 --- a/deps/include/js_protocol.pdl +++ b/deps/include/js_protocol.pdl @@ -175,7 +175,7 @@ domain Debugger command enable parameters # The maximum size in bytes of collected scripts (not referenced by other heap objects) - # the debugger can hold. Puts no limit if paramter is omitted. + # the debugger can hold. Puts no limit if parameter is omitted. experimental optional number maxScriptsCacheSize returns # Unique identifier of the debugger. @@ -267,7 +267,7 @@ domain Debugger BreakpointId breakpointId # Restarts particular call frame from the beginning. - command restartFrame + deprecated command restartFrame parameters # Call frame identifier to evaluate on. CallFrameId callFrameId @@ -707,13 +707,17 @@ experimental domain HeapProfiler # when the tracking is stopped. optional boolean reportProgress optional boolean treatGlobalObjectsAsRoots + # If true, numerical values are included in the snapshot + optional boolean captureNumericValue command takeHeapSnapshot parameters # If true 'reportHeapSnapshotProgress' events will be generated while snapshot is being taken. optional boolean reportProgress - # If true, a raw snapshot without artifical roots will be generated + # If true, a raw snapshot without artificial roots will be generated optional boolean treatGlobalObjectsAsRoots + # If true, numerical values are included in the snapshot + optional boolean captureNumericValue event addHeapSnapshotChunk parameters @@ -968,13 +972,13 @@ domain Profiler # Reports coverage delta since the last poll (either from an event like this, or from # `takePreciseCoverage` for the current isolate. May only be sent if precise code # coverage has been started. This event can be trigged by the embedder to, for example, - # trigger collection of coverage data immediatelly at a certain point in time. + # trigger collection of coverage data immediately at a certain point in time. experimental event preciseCoverageDeltaUpdate parameters # Monotonically increasing time (in seconds) when the coverage update was taken in the backend. number timestamp # Identifier for distinguishing coverage events. - string occassion + string occasion # Coverage data for the current isolate. array of ScriptCoverage result @@ -1221,7 +1225,7 @@ domain Runtime string origin # Human readable name describing given context. string name - # A system-unique execution context identifier. Unlike the id, this is unique accross + # A system-unique execution context identifier. Unlike the id, this is unique across # multiple processes, so can be reliably used to identify specific context while backend # performs a cross-process navigation. experimental string uniqueId @@ -1250,6 +1254,10 @@ domain Runtime optional RemoteObject exception # Identifier of the context where exception happened. optional ExecutionContextId executionContextId + # Dictionary with entries of meta data that the client associated + # with this exception, such as information about associated network + # requests, etc. + experimental optional object exceptionMetaData # Number of milliseconds since epoch. type Timestamp extends number @@ -1339,6 +1347,8 @@ domain Runtime # Symbolic group name that can be used to release multiple objects. If objectGroup is not # specified and objectId is, objectGroup will be inherited from object. optional string objectGroup + # Whether to throw an exception if side effect cannot be ruled out during evaluation. + experimental optional boolean throwOnSideEffect returns # Call result. RemoteObject result @@ -1418,9 +1428,9 @@ domain Runtime # evaluation and allows unsafe-eval. Defaults to true. experimental optional boolean allowUnsafeEvalBlockedByCSP # An alternative way to specify the execution context to evaluate in. - # Compared to contextId that may be reused accross processes, this is guaranteed to be + # Compared to contextId that may be reused across processes, this is guaranteed to be # system-unique, so it can be used to prevent accidental evaluation of the expression - # in context different than intended (e.g. as a result of navigation accross process + # in context different than intended (e.g. as a result of navigation across process # boundaries). # This is mutually exclusive with `contextId`. experimental optional string uniqueContextId @@ -1563,7 +1573,10 @@ domain Runtime # execution context. If omitted and `executionContextName` is not set, # the binding is exposed to all execution contexts of the target. # This parameter is mutually exclusive with `executionContextName`. - optional ExecutionContextId executionContextId + # Deprecated in favor of `executionContextName` due to an unclear use case + # and bugs in implementation (crbug.com/1169639). `executionContextId` will be + # removed in the future. + deprecated optional ExecutionContextId executionContextId # If specified, the binding is exposed to the executionContext with # matching name, even for contexts created after the binding is added. # See also `ExecutionContext.name` and `worldName` parameter to @@ -1659,6 +1672,8 @@ domain Runtime parameters RemoteObject object object hints + # Identifier of the context where the call was made. + experimental optional ExecutionContextId executionContextId # This domain is deprecated. deprecated domain Schema diff --git a/deps/include/v8-cppgc.h b/deps/include/v8-cppgc.h index 2c2219304..745fb0434 100644 --- a/deps/include/v8-cppgc.h +++ b/deps/include/v8-cppgc.h @@ -9,6 +9,7 @@ #include #include +#include "cppgc/common.h" #include "cppgc/custom-space.h" #include "cppgc/heap-statistics.h" #include "cppgc/internal/write-barrier.h" @@ -27,6 +28,8 @@ namespace internal { class CppHeap; } // namespace internal +class CustomSpaceStatisticsReceiver; + /** * Describes how V8 wrapper objects maintain references to garbage-collected C++ * objects. @@ -118,6 +121,30 @@ class V8_EXPORT CppHeap { cppgc::HeapStatistics CollectStatistics( cppgc::HeapStatistics::DetailLevel detail_level); + /** + * Collects statistics for the given spaces and reports them to the receiver. + * + * \param custom_spaces a collection of custom space indicies. + * \param receiver an object that gets the results. + */ + void CollectCustomSpaceStatisticsAtLastGC( + std::vector custom_spaces, + std::unique_ptr receiver); + + /** + * Enables a detached mode that allows testing garbage collection using + * `cppgc::testing` APIs. Once used, the heap cannot be attached to an + * `Isolate` anymore. + */ + void EnableDetachedGarbageCollectionsForTesting(); + + /** + * Performs a stop-the-world garbage collection for testing purposes. + * + * \param stack_state The stack state to assume for the garbage collection. + */ + void CollectGarbageForTesting(cppgc::EmbedderStackState stack_state); + private: CppHeap() = default; @@ -262,6 +289,26 @@ class V8_EXPORT JSHeapConsistency final { const TracedReferenceBase& ref); }; +/** + * Provided as input to `CppHeap::CollectCustomSpaceStatisticsAtLastGC()`. + * + * Its method is invoked with the results of the statistic collection. + */ +class CustomSpaceStatisticsReceiver { + public: + virtual ~CustomSpaceStatisticsReceiver() = default; + /** + * Reports the size of a space at the last GC. It is called for each space + * that was requested in `CollectCustomSpaceStatisticsAtLastGC()`. + * + * \param space_index The index of the space. + * \param bytes The total size of live objects in the space at the last GC. + * It is zero if there was no GC yet. + */ + virtual void AllocatedBytes(cppgc::CustomSpaceIndex space_index, + size_t bytes) = 0; +}; + } // namespace v8 namespace cppgc { diff --git a/deps/include/v8-fast-api-calls.h b/deps/include/v8-fast-api-calls.h index ca5fc764a..8c9d02769 100644 --- a/deps/include/v8-fast-api-calls.h +++ b/deps/include/v8-fast-api-calls.h @@ -70,8 +70,7 @@ * return GetInternalField(wrapper); * } - * static void FastMethod(v8::ApiObject receiver_obj, int param) { - * v8::Object* v8_object = reinterpret_cast(&api_object); + * static void FastMethod(v8::Local receiver_obj, int param) { * CustomEmbedderType* receiver = static_cast( * receiver_obj->GetAlignedPointerFromInternalField( * kV8EmbedderWrapperObjectIndex)); @@ -157,6 +156,7 @@ * - float64_t * Currently supported argument types: * - pointer to an embedder type + * - JavaScript array of primitive types * - bool * - int32_t * - uint32_t @@ -177,8 +177,43 @@ * passes NaN values as-is, i.e. doesn't normalize them. * * To be supported types: - * - arrays of C types + * - TypedArrays and ArrayBuffers * - arrays of embedder types + * + * + * The API offers a limited support for function overloads: + * + * \code + * void FastMethod_2Args(int param, bool another_param); + * void FastMethod_3Args(int param, bool another_param, int third_param); + * + * v8::CFunction fast_method_2args_c_func = + * MakeV8CFunction(FastMethod_2Args); + * v8::CFunction fast_method_3args_c_func = + * MakeV8CFunction(FastMethod_3Args); + * const v8::CFunction fast_method_overloads[] = {fast_method_2args_c_func, + * fast_method_3args_c_func}; + * Local method_template = + * v8::FunctionTemplate::NewWithCFunctionOverloads( + * isolate, SlowCallback, data, signature, length, + * constructor_behavior, side_effect_type, + * {fast_method_overloads, 2}); + * \endcode + * + * In this example a single FunctionTemplate is associated to multiple C++ + * functions. The overload resolution is currently only based on the number of + * arguments passed in a call. For example, if this method_template is + * registered with a wrapper JS object as described above, a call with two + * arguments: + * obj.method(42, true); + * will result in a fast call to FastMethod_2Args, while a call with three or + * more arguments: + * obj.method(42, true, 11); + * will result in a fast call to FastMethod_3Args. Instead a call with less than + * two arguments, like: + * obj.method(42); + * would not result in a fast call but would fall back to executing the + * associated SlowCallback. */ #ifndef INCLUDE_V8_FAST_API_CALLS_H_ @@ -187,10 +222,17 @@ #include #include -#include "v8config.h" // NOLINT(build/include_directory) +#include +#include + +#include "v8-internal.h" // NOLINT(build/include_directory) +#include "v8.h" // NOLINT(build/include_directory) +#include "v8config.h" // NOLINT(build/include_directory) namespace v8 { +class Isolate; + class CTypeInfo { public: enum class Type : uint8_t { @@ -203,44 +245,226 @@ class CTypeInfo { kFloat32, kFloat64, kV8Value, + kApiObject, // This will be deprecated once all users have + // migrated from v8::ApiObject to v8::Local. }; - // kCallbackOptionsType and kInvalidType are not part of the Type enum - // because they are only used internally. Use values 255 and 254 that - // are larger than any valid Type enum. + // kCallbackOptionsType is not part of the Type enum + // because it is only used internally. Use value 255 that is larger + // than any valid Type enum. static constexpr Type kCallbackOptionsType = Type(255); - static constexpr Type kInvalidType = Type(254); - enum class ArgFlags : uint8_t { + enum class SequenceType : uint8_t { + kScalar, + kIsSequence, // sequence + kIsTypedArray, // TypedArray of T or any ArrayBufferView if T + // is void + kIsArrayBuffer // ArrayBuffer + }; + + enum class Flags : uint8_t { kNone = 0, + kAllowSharedBit = 1 << 0, // Must be an ArrayBuffer or TypedArray + kEnforceRangeBit = 1 << 1, // T must be integral + kClampBit = 1 << 2, // T must be integral + kIsRestrictedBit = 1 << 3, // T must be float or double }; - explicit constexpr CTypeInfo(Type type, ArgFlags flags = ArgFlags::kNone) - : type_(type), flags_(flags) {} + explicit constexpr CTypeInfo( + Type type, SequenceType sequence_type = SequenceType::kScalar, + Flags flags = Flags::kNone) + : type_(type), sequence_type_(sequence_type), flags_(flags) {} constexpr Type GetType() const { return type_; } + constexpr SequenceType GetSequenceType() const { return sequence_type_; } + constexpr Flags GetFlags() const { return flags_; } + + static constexpr bool IsIntegralType(Type type) { + return type == Type::kInt32 || type == Type::kUint32 || + type == Type::kInt64 || type == Type::kUint64; + } - constexpr ArgFlags GetFlags() const { return flags_; } + static constexpr bool IsFloatingPointType(Type type) { + return type == Type::kFloat32 || type == Type::kFloat64; + } - static const CTypeInfo& Invalid() { - static CTypeInfo invalid = CTypeInfo(kInvalidType); - return invalid; + static constexpr bool IsPrimitive(Type type) { + return IsIntegralType(type) || IsFloatingPointType(type) || + type == Type::kBool; } private: Type type_; - ArgFlags flags_; + SequenceType sequence_type_; + Flags flags_; }; -class CFunctionInfo { +struct FastApiTypedArrayBase { public: - virtual const CTypeInfo& ReturnInfo() const = 0; - virtual unsigned int ArgumentCount() const = 0; - virtual const CTypeInfo& ArgumentInfo(unsigned int index) const = 0; - virtual bool HasOptions() const = 0; + // Returns the length in number of elements. + size_t V8_EXPORT length() const { return length_; } + // Checks whether the given index is within the bounds of the collection. + void V8_EXPORT ValidateIndex(size_t index) const; + + protected: + size_t length_ = 0; +}; + +template +struct FastApiTypedArray : public FastApiTypedArrayBase { + public: + V8_INLINE T get(size_t index) const { +#ifdef DEBUG + ValidateIndex(index); +#endif // DEBUG + T tmp; + memcpy(&tmp, reinterpret_cast(data_) + index, sizeof(T)); + return tmp; + } + + private: + // This pointer should include the typed array offset applied. + // It's not guaranteed that it's aligned to sizeof(T), it's only + // guaranteed that it's 4-byte aligned, so for 8-byte types we need to + // provide a special implementation for reading from it, which hides + // the possibly unaligned read in the `get` method. + void* data_; +}; + +// Any TypedArray. It uses kTypedArrayBit with base type void +// Overloaded args of ArrayBufferView and TypedArray are not supported +// (for now) because the generic “any” ArrayBufferView doesn’t have its +// own instance type. It could be supported if we specify that +// TypedArray always has precedence over the generic ArrayBufferView, +// but this complicates overload resolution. +struct FastApiArrayBufferView { + void* data; + size_t byte_length; +}; + +struct FastApiArrayBuffer { + void* data; + size_t byte_length; }; -struct ApiObject { +class V8_EXPORT CFunctionInfo { + public: + // Construct a struct to hold a CFunction's type information. + // |return_info| describes the function's return type. + // |arg_info| is an array of |arg_count| CTypeInfos describing the + // arguments. Only the last argument may be of the special type + // CTypeInfo::kCallbackOptionsType. + CFunctionInfo(const CTypeInfo& return_info, unsigned int arg_count, + const CTypeInfo* arg_info); + + const CTypeInfo& ReturnInfo() const { return return_info_; } + + // The argument count, not including the v8::FastApiCallbackOptions + // if present. + unsigned int ArgumentCount() const { + return HasOptions() ? arg_count_ - 1 : arg_count_; + } + + // |index| must be less than ArgumentCount(). + // Note: if the last argument passed on construction of CFunctionInfo + // has type CTypeInfo::kCallbackOptionsType, it is not included in + // ArgumentCount(). + const CTypeInfo& ArgumentInfo(unsigned int index) const; + + bool HasOptions() const { + // The options arg is always the last one. + return arg_count_ > 0 && arg_info_[arg_count_ - 1].GetType() == + CTypeInfo::kCallbackOptionsType; + } + + private: + const CTypeInfo return_info_; + const unsigned int arg_count_; + const CTypeInfo* arg_info_; +}; + +class V8_EXPORT CFunction { + public: + constexpr CFunction() : address_(nullptr), type_info_(nullptr) {} + + const CTypeInfo& ReturnInfo() const { return type_info_->ReturnInfo(); } + + const CTypeInfo& ArgumentInfo(unsigned int index) const { + return type_info_->ArgumentInfo(index); + } + + unsigned int ArgumentCount() const { return type_info_->ArgumentCount(); } + + const void* GetAddress() const { return address_; } + const CFunctionInfo* GetTypeInfo() const { return type_info_; } + + enum class OverloadResolution { kImpossible, kAtRuntime, kAtCompileTime }; + + // Returns whether an overload between this and the given CFunction can + // be resolved at runtime by the RTTI available for the arguments or at + // compile time for functions with different number of arguments. + OverloadResolution GetOverloadResolution(const CFunction* other) { + // Runtime overload resolution can only deal with functions with the + // same number of arguments. Functions with different arity are handled + // by compile time overload resolution though. + if (ArgumentCount() != other->ArgumentCount()) { + return OverloadResolution::kAtCompileTime; + } + + // The functions can only differ by a single argument position. + int diff_index = -1; + for (unsigned int i = 0; i < ArgumentCount(); ++i) { + if (ArgumentInfo(i).GetSequenceType() != + other->ArgumentInfo(i).GetSequenceType()) { + if (diff_index >= 0) { + return OverloadResolution::kImpossible; + } + diff_index = i; + + // We only support overload resolution between sequence types. + if (ArgumentInfo(i).GetSequenceType() == + CTypeInfo::SequenceType::kScalar || + other->ArgumentInfo(i).GetSequenceType() == + CTypeInfo::SequenceType::kScalar) { + return OverloadResolution::kImpossible; + } + } + } + + return OverloadResolution::kAtRuntime; + } + + template + static CFunction Make(F* func) { + return ArgUnwrap::Make(func); + } + + template + V8_DEPRECATED("Use CFunctionBuilder instead.") + static CFunction MakeWithFallbackSupport(F* func) { + return ArgUnwrap::Make(func); + } + + CFunction(const void* address, const CFunctionInfo* type_info); + + private: + const void* address_; + const CFunctionInfo* type_info_; + + template + class ArgUnwrap { + static_assert(sizeof(F) != sizeof(F), + "CFunction must be created from a function pointer."); + }; + + template + class ArgUnwrap { + public: + static CFunction Make(R (*func)(Args...)); + }; +}; + +struct V8_DEPRECATE_SOON("Use v8::Local instead.") ApiObject { uintptr_t address; }; @@ -251,6 +475,14 @@ struct ApiObject { * \endcode */ struct FastApiCallbackOptions { + /** + * Creates a new instance of FastApiCallbackOptions for testing purpose. The + * returned instance may be filled with mock data. + */ + static FastApiCallbackOptions CreateForTesting(Isolate* isolate) { + return {false, {0}}; + } + /** * If the callback wants to signal an error condition or to perform an * allocation, it must set options.fallback to true and do an early return @@ -266,43 +498,16 @@ struct FastApiCallbackOptions { /** * The `data` passed to the FunctionTemplate constructor, or `undefined`. + * `data_ptr` allows for default constructing FastApiCallbackOptions. */ - const ApiObject data; + union { + uintptr_t data_ptr; + v8::Value data; + }; }; namespace internal { -template -struct GetCType; - -#define SPECIALIZE_GET_C_TYPE_FOR(ctype, ctypeinfo) \ - template <> \ - struct GetCType { \ - static constexpr CTypeInfo Get() { \ - return CTypeInfo(CTypeInfo::Type::ctypeinfo); \ - } \ - }; - -#define SUPPORTED_C_TYPES(V) \ - V(void, kVoid) \ - V(bool, kBool) \ - V(int32_t, kInt32) \ - V(uint32_t, kUint32) \ - V(int64_t, kInt64) \ - V(uint64_t, kUint64) \ - V(float, kFloat32) \ - V(double, kFloat64) \ - V(ApiObject, kV8Value) - -SUPPORTED_C_TYPES(SPECIALIZE_GET_C_TYPE_FOR) - -template <> -struct GetCType { - static constexpr CTypeInfo Get() { - return CTypeInfo(CTypeInfo::kCallbackOptionsType); - } -}; - // Helper to count the number of occurances of `T` in `List` template struct count : std::integral_constant {}; @@ -312,108 +517,320 @@ struct count template struct count : count {}; -template +template class CFunctionInfoImpl : public CFunctionInfo { - public: static constexpr int kOptionsArgCount = - count(); + count(); static constexpr int kReceiverCount = 1; - CFunctionInfoImpl() - : return_info_(internal::GetCType::Get()), - arg_count_(sizeof...(Args) - kOptionsArgCount), - arg_info_{internal::GetCType::Get()...} { - static_assert(kOptionsArgCount == 0 || kOptionsArgCount == 1, - "Only one options parameter is supported."); - static_assert(sizeof...(Args) >= kOptionsArgCount + kReceiverCount, - "The receiver or the fallback argument is missing."); - constexpr CTypeInfo::Type type = internal::GetCType::Get().GetType(); - static_assert(type == CTypeInfo::Type::kVoid || - type == CTypeInfo::Type::kBool || - type == CTypeInfo::Type::kInt32 || - type == CTypeInfo::Type::kUint32 || - type == CTypeInfo::Type::kFloat32 || - type == CTypeInfo::Type::kFloat64, + + static_assert(kOptionsArgCount == 0 || kOptionsArgCount == 1, + "Only one options parameter is supported."); + + static_assert(sizeof...(ArgBuilders) >= kOptionsArgCount + kReceiverCount, + "The receiver or the options argument is missing."); + + public: + constexpr CFunctionInfoImpl() + : CFunctionInfo(RetBuilder::Build(), sizeof...(ArgBuilders), + arg_info_storage_), + arg_info_storage_{ArgBuilders::Build()...} { + constexpr CTypeInfo::Type kReturnType = RetBuilder::Build().GetType(); + static_assert(kReturnType == CTypeInfo::Type::kVoid || + kReturnType == CTypeInfo::Type::kBool || + kReturnType == CTypeInfo::Type::kInt32 || + kReturnType == CTypeInfo::Type::kUint32 || + kReturnType == CTypeInfo::Type::kFloat32 || + kReturnType == CTypeInfo::Type::kFloat64, "64-bit int and api object values are not currently " "supported return types."); } - const CTypeInfo& ReturnInfo() const override { return return_info_; } - unsigned int ArgumentCount() const override { return arg_count_; } - const CTypeInfo& ArgumentInfo(unsigned int index) const override { - if (index >= ArgumentCount()) { - return CTypeInfo::Invalid(); - } - return arg_info_[index]; + private: + const CTypeInfo arg_info_storage_[sizeof...(ArgBuilders)]; +}; + +template +struct TypeInfoHelper { + static_assert(sizeof(T) != sizeof(T), "This type is not supported"); +}; + +#define SPECIALIZE_GET_TYPE_INFO_HELPER_FOR(T, Enum) \ + template <> \ + struct TypeInfoHelper { \ + static constexpr CTypeInfo::Flags Flags() { \ + return CTypeInfo::Flags::kNone; \ + } \ + \ + static constexpr CTypeInfo::Type Type() { return CTypeInfo::Type::Enum; } \ + static constexpr CTypeInfo::SequenceType SequenceType() { \ + return CTypeInfo::SequenceType::kScalar; \ + } \ + }; + +template +struct CTypeInfoTraits {}; + +#define DEFINE_TYPE_INFO_TRAITS(CType, Enum) \ + template <> \ + struct CTypeInfoTraits { \ + using ctype = CType; \ + }; + +#define PRIMITIVE_C_TYPES(V) \ + V(bool, kBool) \ + V(int32_t, kInt32) \ + V(uint32_t, kUint32) \ + V(int64_t, kInt64) \ + V(uint64_t, kUint64) \ + V(float, kFloat32) \ + V(double, kFloat64) + +// Same as above, but includes deprecated types for compatibility. +#define ALL_C_TYPES(V) \ + PRIMITIVE_C_TYPES(V) \ + V(void, kVoid) \ + V(v8::Local, kV8Value) \ + V(v8::Local, kV8Value) \ + V(ApiObject, kApiObject) + +// ApiObject was a temporary solution to wrap the pointer to the v8::Value. +// Please use v8::Local in new code for the arguments and +// v8::Local for the receiver, as ApiObject will be deprecated. + +ALL_C_TYPES(SPECIALIZE_GET_TYPE_INFO_HELPER_FOR) +PRIMITIVE_C_TYPES(DEFINE_TYPE_INFO_TRAITS) + +#undef PRIMITIVE_C_TYPES +#undef ALL_C_TYPES + +#define SPECIALIZE_GET_TYPE_INFO_HELPER_FOR_TA(T, Enum) \ + template <> \ + struct TypeInfoHelper&> { \ + static constexpr CTypeInfo::Flags Flags() { \ + return CTypeInfo::Flags::kNone; \ + } \ + \ + static constexpr CTypeInfo::Type Type() { return CTypeInfo::Type::Enum; } \ + static constexpr CTypeInfo::SequenceType SequenceType() { \ + return CTypeInfo::SequenceType::kIsTypedArray; \ + } \ + }; + +#define TYPED_ARRAY_C_TYPES(V) \ + V(int32_t, kInt32) \ + V(uint32_t, kUint32) \ + V(int64_t, kInt64) \ + V(uint64_t, kUint64) \ + V(float, kFloat32) \ + V(double, kFloat64) + +TYPED_ARRAY_C_TYPES(SPECIALIZE_GET_TYPE_INFO_HELPER_FOR_TA) + +#undef TYPED_ARRAY_C_TYPES + +template <> +struct TypeInfoHelper> { + static constexpr CTypeInfo::Flags Flags() { return CTypeInfo::Flags::kNone; } + + static constexpr CTypeInfo::Type Type() { return CTypeInfo::Type::kVoid; } + static constexpr CTypeInfo::SequenceType SequenceType() { + return CTypeInfo::SequenceType::kIsSequence; } - bool HasOptions() const override { return kOptionsArgCount == 1; } +}; - private: - const CTypeInfo return_info_; - const unsigned int arg_count_; - const CTypeInfo arg_info_[sizeof...(Args)]; +template <> +struct TypeInfoHelper> { + static constexpr CTypeInfo::Flags Flags() { return CTypeInfo::Flags::kNone; } + + static constexpr CTypeInfo::Type Type() { return CTypeInfo::Type::kUint32; } + static constexpr CTypeInfo::SequenceType SequenceType() { + return CTypeInfo::SequenceType::kIsTypedArray; + } }; -} // namespace internal +template <> +struct TypeInfoHelper { + static constexpr CTypeInfo::Flags Flags() { return CTypeInfo::Flags::kNone; } -class V8_EXPORT CFunction { - public: - constexpr CFunction() : address_(nullptr), type_info_(nullptr) {} + static constexpr CTypeInfo::Type Type() { + return CTypeInfo::kCallbackOptionsType; + } + static constexpr CTypeInfo::SequenceType SequenceType() { + return CTypeInfo::SequenceType::kScalar; + } +}; - const CTypeInfo& ReturnInfo() const { return type_info_->ReturnInfo(); } +#define STATIC_ASSERT_IMPLIES(COND, ASSERTION, MSG) \ + static_assert(((COND) == 0) || (ASSERTION), MSG) - const CTypeInfo& ArgumentInfo(unsigned int index) const { - return type_info_->ArgumentInfo(index); +template +class CTypeInfoBuilder { + public: + using BaseType = T; + + static constexpr CTypeInfo Build() { + constexpr CTypeInfo::Flags kFlags = + MergeFlags(TypeInfoHelper::Flags(), Flags...); + constexpr CTypeInfo::Type kType = TypeInfoHelper::Type(); + constexpr CTypeInfo::SequenceType kSequenceType = + TypeInfoHelper::SequenceType(); + + STATIC_ASSERT_IMPLIES( + uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kAllowSharedBit), + (kSequenceType == CTypeInfo::SequenceType::kIsTypedArray || + kSequenceType == CTypeInfo::SequenceType::kIsArrayBuffer), + "kAllowSharedBit is only allowed for TypedArrays and ArrayBuffers."); + STATIC_ASSERT_IMPLIES( + uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kEnforceRangeBit), + CTypeInfo::IsIntegralType(kType), + "kEnforceRangeBit is only allowed for integral types."); + STATIC_ASSERT_IMPLIES( + uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kClampBit), + CTypeInfo::IsIntegralType(kType), + "kClampBit is only allowed for integral types."); + STATIC_ASSERT_IMPLIES( + uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kIsRestrictedBit), + CTypeInfo::IsFloatingPointType(kType), + "kIsRestrictedBit is only allowed for floating point types."); + STATIC_ASSERT_IMPLIES(kSequenceType == CTypeInfo::SequenceType::kIsSequence, + kType == CTypeInfo::Type::kVoid, + "Sequences are only supported from void type."); + STATIC_ASSERT_IMPLIES( + kSequenceType == CTypeInfo::SequenceType::kIsTypedArray, + CTypeInfo::IsPrimitive(kType) || kType == CTypeInfo::Type::kVoid, + "TypedArrays are only supported from primitive types or void."); + + // Return the same type with the merged flags. + return CTypeInfo(TypeInfoHelper::Type(), + TypeInfoHelper::SequenceType(), kFlags); } - unsigned int ArgumentCount() const { return type_info_->ArgumentCount(); } + private: + template + static constexpr CTypeInfo::Flags MergeFlags(CTypeInfo::Flags flags, + Rest... rest) { + return CTypeInfo::Flags(uint8_t(flags) | uint8_t(MergeFlags(rest...))); + } + static constexpr CTypeInfo::Flags MergeFlags() { return CTypeInfo::Flags(0); } +}; - const void* GetAddress() const { return address_; } - const CFunctionInfo* GetTypeInfo() const { return type_info_; } +template +class CFunctionBuilderWithFunction { + public: + explicit constexpr CFunctionBuilderWithFunction(const void* fn) : fn_(fn) {} - template - static CFunction Make(F* func) { - return ArgUnwrap::Make(func); + template + constexpr auto Ret() { + return CFunctionBuilderWithFunction< + CTypeInfoBuilder, + ArgBuilders...>(fn_); } - template - V8_DEPRECATED("Use CFunction::Make instead.") - static CFunction MakeWithFallbackSupport(F* func) { - return ArgUnwrap::Make(func); + template + constexpr auto Arg() { + // Return a copy of the builder with the Nth arg builder merged with + // template parameter pack Flags. + return ArgImpl( + std::make_index_sequence()); } - template - static CFunction Make(F* func, const CFunctionInfo* type_info) { - return CFunction(reinterpret_cast(func), type_info); + auto Build() { + static CFunctionInfoImpl instance; + return CFunction(fn_, &instance); } private: - const void* address_; - const CFunctionInfo* type_info_; + template + struct GetArgBuilder; + + // Returns the same ArgBuilder as the one at index N, including its flags. + // Flags in the template parameter pack are ignored. + template + struct GetArgBuilder { + using type = + typename std::tuple_element>::type; + }; - CFunction(const void* address, const CFunctionInfo* type_info); + // Returns an ArgBuilder with the same base type as the one at index N, + // but merges the flags with the flags in the template parameter pack. + template + struct GetArgBuilder { + using type = CTypeInfoBuilder< + typename std::tuple_element>::type::BaseType, + std::tuple_element>::type::Build() + .GetFlags(), + Flags...>; + }; - template - static CFunctionInfo* GetCFunctionInfo() { - static internal::CFunctionInfoImpl instance; - return &instance; + // Return a copy of the CFunctionBuilder, but merges the Flags on + // ArgBuilder index N with the new Flags passed in the template parameter + // pack. + template + constexpr auto ArgImpl(std::index_sequence) { + return CFunctionBuilderWithFunction< + RetBuilder, typename GetArgBuilder::type...>(fn_); } - template - class ArgUnwrap { - static_assert(sizeof(F) != sizeof(F), - "CFunction must be created from a function pointer."); - }; + const void* fn_; +}; + +class CFunctionBuilder { + public: + constexpr CFunctionBuilder() {} template - class ArgUnwrap { - public: - static CFunction Make(R (*func)(Args...)) { - return CFunction(reinterpret_cast(func), - GetCFunctionInfo()); - } - }; + constexpr auto Fn(R (*fn)(Args...)) { + return CFunctionBuilderWithFunction, + CTypeInfoBuilder...>( + reinterpret_cast(fn)); + } }; +} // namespace internal + +// static +template +CFunction CFunction::ArgUnwrap::Make(R (*func)(Args...)) { + return internal::CFunctionBuilder().Fn(func).Build(); +} + +using CFunctionBuilder = internal::CFunctionBuilder; + +static constexpr CTypeInfo kTypeInfoInt32 = CTypeInfo(CTypeInfo::Type::kInt32); +static constexpr CTypeInfo kTypeInfoFloat64 = + CTypeInfo(CTypeInfo::Type::kFloat64); + +/** + * Copies the contents of this JavaScript array to a C++ buffer with + * a given max_length. A CTypeInfo is passed as an argument, + * instructing different rules for conversion (e.g. restricted float/double). + * The element type T of the destination array must match the C type + * corresponding to the CTypeInfo (specified by CTypeInfoTraits). + * If the array length is larger than max_length or the array is of + * unsupported type, the operation will fail, returning false. Generally, an + * array which contains objects, undefined, null or anything not convertible + * to the requested destination type, is considered unsupported. The operation + * returns true on success. `type_info` will be used for conversions. + */ +template +bool V8_EXPORT V8_WARN_UNUSED_RESULT TryCopyAndConvertArrayToCppBuffer( + Local src, T* dst, uint32_t max_length); + +template <> +inline bool V8_WARN_UNUSED_RESULT +TryCopyAndConvertArrayToCppBuffer<&kTypeInfoInt32, int32_t>( + Local src, int32_t* dst, uint32_t max_length) { + return CopyAndConvertArrayToCppBufferInt32(src, dst, max_length); +} + +template <> +inline bool V8_WARN_UNUSED_RESULT +TryCopyAndConvertArrayToCppBuffer<&kTypeInfoFloat64, double>( + Local src, double* dst, uint32_t max_length) { + return CopyAndConvertArrayToCppBufferFloat64(src, dst, max_length); +} + } // namespace v8 #endif // INCLUDE_V8_FAST_API_CALLS_H_ diff --git a/deps/include/v8-inspector.h b/deps/include/v8-inspector.h index a55518e45..e6621ccd7 100644 --- a/deps/include/v8-inspector.h +++ b/deps/include/v8-inspector.h @@ -105,8 +105,9 @@ class V8_EXPORT V8StackTrace { virtual StringView topSourceURL() const = 0; virtual int topLineNumber() const = 0; virtual int topColumnNumber() const = 0; - virtual StringView topScriptId() const = 0; - virtual int topScriptIdAsInteger() const = 0; + virtual int topScriptId() const = 0; + V8_DEPRECATE_SOON("Use V8::StackTrace::topScriptId() instead.") + int topScriptIdAsInteger() const { return topScriptId(); } virtual StringView topFunctionName() const = 0; virtual ~V8StackTrace() = default; @@ -130,6 +131,10 @@ class V8_EXPORT V8InspectorSession { virtual v8::Local get(v8::Local) = 0; virtual ~Inspectable() = default; }; + class V8_EXPORT CommandLineAPIScope { + public: + virtual ~CommandLineAPIScope() = default; + }; virtual void addInspectedObject(std::unique_ptr) = 0; // Dispatching protocol messages. @@ -139,6 +144,9 @@ class V8_EXPORT V8InspectorSession { virtual std::vector> supportedDomains() = 0; + virtual std::unique_ptr + initializeCommandLineAPIScope(int executionContextId) = 0; + // Debugger actions. virtual void schedulePauseOnNextStatement(StringView breakReason, StringView breakDetails) = 0; @@ -162,7 +170,7 @@ class V8_EXPORT V8InspectorSession { v8::Local*, std::unique_ptr* objectGroup) = 0; virtual void releaseObjectGroup(StringView) = 0; - virtual void triggerPreciseCoverageDeltaUpdate(StringView occassion) = 0; + virtual void triggerPreciseCoverageDeltaUpdate(StringView occasion) = 0; }; class V8_EXPORT V8InspectorClient { @@ -186,9 +194,6 @@ class V8_EXPORT V8InspectorClient { v8::Local, v8::Local) { return nullptr; } - virtual bool formatAccessorsAsProperties(v8::Local) { - return false; - } virtual bool isInspectableHeapObject(v8::Local) { return true; } virtual v8::Local ensureDefaultContextInGroup( @@ -293,6 +298,10 @@ class V8_EXPORT V8Inspector { int scriptId) = 0; virtual void exceptionRevoked(v8::Local, unsigned exceptionId, StringView message) = 0; + virtual bool associateExceptionData(v8::Local, + v8::Local exception, + v8::Local key, + v8::Local value) = 0; // Connection. class V8_EXPORT Channel { diff --git a/deps/include/v8-internal.h b/deps/include/v8-internal.h index 8abbcfb41..0222ab2f7 100644 --- a/deps/include/v8-internal.h +++ b/deps/include/v8-internal.h @@ -15,9 +15,12 @@ namespace v8 { +class Array; class Context; class Data; class Isolate; +template +class Local; namespace internal { @@ -33,6 +36,7 @@ const int kApiSystemPointerSize = sizeof(void*); const int kApiDoubleSize = sizeof(double); const int kApiInt32Size = sizeof(int32_t); const int kApiInt64Size = sizeof(int64_t); +const int kApiSizetSize = sizeof(size_t); // Tag information for HeapObject. const int kHeapObjectTag = 1; @@ -40,6 +44,13 @@ const int kWeakHeapObjectTag = 3; const int kHeapObjectTagSize = 2; const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1; +// Tag information for fowarding pointers stored in object headers. +// 0b00 at the lowest 2 bits in the header indicates that the map word is a +// forwarding pointer. +const int kForwardingTag = 0; +const int kForwardingTagSize = 2; +const intptr_t kForwardingTagMask = (1 << kForwardingTagSize) - 1; + // Tag information for Smi. const int kSmiTag = 0; const int kSmiTagSize = 1; @@ -120,23 +131,29 @@ constexpr bool HeapSandboxIsEnabled() { using ExternalPointer_t = Address; -// If the heap sandbox is enabled, these tag values will be XORed with the +// If the heap sandbox is enabled, these tag values will be ORed with the // external pointers in the external pointer table to prevent use of pointers of -// the wrong type. -enum ExternalPointerTag : Address { - kExternalPointerNullTag = static_cast
(0ULL), - kArrayBufferBackingStoreTag = static_cast
(1ULL << 48), - kTypedArrayExternalPointerTag = static_cast
(2ULL << 48), - kDataViewDataPointerTag = static_cast
(3ULL << 48), - kExternalStringResourceTag = static_cast
(4ULL << 48), - kExternalStringResourceDataTag = static_cast
(5ULL << 48), - kForeignForeignAddressTag = static_cast
(6ULL << 48), - kNativeContextMicrotaskQueueTag = static_cast
(7ULL << 48), - // TODO(v8:10391, saelo): Currently has to be zero so that raw zero values are - // also nullptr - kEmbedderDataSlotPayloadTag = static_cast
(0ULL << 48), +// the wrong type. When a pointer is loaded, it is ANDed with the inverse of the +// expected type's tag. The tags are constructed in a way that guarantees that a +// failed type check will result in one or more of the top bits of the pointer +// to be set, rendering the pointer inacessible. This construction allows +// performing the type check and removing GC marking bits from the pointer at +// the same time. +enum ExternalPointerTag : uint64_t { + kExternalPointerNullTag = 0x0000000000000000, + kArrayBufferBackingStoreTag = 0x00ff000000000000, // 0b000000011111111 + kTypedArrayExternalPointerTag = 0x017f000000000000, // 0b000000101111111 + kDataViewDataPointerTag = 0x01bf000000000000, // 0b000000110111111 + kExternalStringResourceTag = 0x01df000000000000, // 0b000000111011111 + kExternalStringResourceDataTag = 0x01ef000000000000, // 0b000000111101111 + kForeignForeignAddressTag = 0x01f7000000000000, // 0b000000111110111 + kNativeContextMicrotaskQueueTag = 0x01fb000000000000, // 0b000000111111011 + kEmbedderDataSlotPayloadTag = 0x01fd000000000000, // 0b000000111111101 + kCodeEntryPointTag = 0x01fe000000000000, // 0b000000111111110 }; +constexpr uint64_t kExternalPointerTagMask = 0xffff000000000000; + #ifdef V8_31BIT_SMIS_ON_64BIT_ARCH using PlatformSmiTagging = SmiTagging; #else @@ -171,12 +188,22 @@ V8_EXPORT internal::Isolate* IsolateFromNeverReadOnlySpaceObject(Address obj); // language mode is strict. V8_EXPORT bool ShouldThrowOnError(v8::internal::Isolate* isolate); +V8_EXPORT bool CanHaveInternalField(int instance_type); + /** * This class exports constants and functionality from within v8 that * is necessary to implement inline functions in the v8 api. Don't * depend on functions and constants defined here. */ class Internals { +#ifdef V8_MAP_PACKING + V8_INLINE static constexpr internal::Address UnpackMapWord( + internal::Address mapword) { + // TODO(wenyuzhao): Clear header metadata. + return mapword ^ kMapWordXorMask; + } +#endif + public: // These values match non-compiler-dependent values defined within // the implementation of v8. @@ -209,8 +236,12 @@ class Internals { kIsolateFastCCallCallerFpOffset + kApiSystemPointerSize; static const int kIsolateFastApiCallTargetOffset = kIsolateFastCCallCallerPcOffset + kApiSystemPointerSize; - static const int kIsolateStackGuardOffset = + static const int kIsolateCageBaseOffset = kIsolateFastApiCallTargetOffset + kApiSystemPointerSize; + static const int kIsolateLongTaskStatsCounterOffset = + kIsolateCageBaseOffset + kApiSystemPointerSize; + static const int kIsolateStackGuardOffset = + kIsolateLongTaskStatsCounterOffset + kApiSizetSize; static const int kIsolateRootsOffset = kIsolateStackGuardOffset + 7 * kApiSystemPointerSize; @@ -237,8 +268,9 @@ class Internals { static const int kOddballType = 0x43; static const int kForeignType = 0x46; static const int kJSSpecialApiObjectType = 0x410; - static const int kJSApiObjectType = 0x420; static const int kJSObjectType = 0x421; + static const int kFirstJSApiObjectType = 0x422; + static const int kLastJSApiObjectType = 0x80A; static const int kUndefinedOddballKind = 5; static const int kNullOddballKind = 3; @@ -253,6 +285,17 @@ class Internals { // incremental GC once the external memory reaches this limit. static constexpr int kExternalAllocationSoftLimit = 64 * 1024 * 1024; +#ifdef V8_MAP_PACKING + static const uintptr_t kMapWordMetadataMask = 0xffffULL << 48; + // The lowest two bits of mapwords are always `0b10` + static const uintptr_t kMapWordSignature = 0b10; + // XORing a (non-compressed) map with this mask ensures that the two + // low-order bits are 0b10. The 0 at the end makes this look like a Smi, + // although real Smis have all lower 32 bits unset. We only rely on these + // values passing as Smis in very few places. + static const int kMapWordXorMask = 0b11; +#endif + V8_EXPORT static void CheckInitializedImpl(v8::Isolate* isolate); V8_INLINE static void CheckInitialized(v8::Isolate* isolate) { #ifdef V8_ENABLE_CHECKS @@ -279,6 +322,9 @@ class Internals { V8_INLINE static int GetInstanceType(const internal::Address obj) { typedef internal::Address A; A map = ReadTaggedPointerField(obj, kHeapObjectMapOffset); +#ifdef V8_MAP_PACKING + map = UnpackMapWord(map); +#endif return ReadRawField(map, kMapInstanceTypeOffset); } @@ -329,6 +375,12 @@ class Internals { return *reinterpret_cast(addr); } + V8_INLINE static void IncrementLongTasksStatsCounter(v8::Isolate* isolate) { + internal::Address addr = reinterpret_cast(isolate) + + kIsolateLongTaskStatsCounterOffset; + ++(*reinterpret_cast(addr)); + } + V8_INLINE static internal::Address* GetRoot(v8::Isolate* isolate, int index) { internal::Address addr = reinterpret_cast(isolate) + kIsolateRootsOffset + @@ -358,8 +410,9 @@ class Internals { internal::Address heap_object_ptr, int offset) { #ifdef V8_COMPRESS_POINTERS uint32_t value = ReadRawField(heap_object_ptr, offset); - internal::Address root = GetRootFromOnHeapAddress(heap_object_ptr); - return root + static_cast(static_cast(value)); + internal::Address base = + GetPtrComprCageBaseFromOnHeapAddress(heap_object_ptr); + return base + static_cast(static_cast(value)); #else return ReadRawField(heap_object_ptr, offset); #endif @@ -411,18 +464,19 @@ class Internals { #ifdef V8_COMPRESS_POINTERS // See v8:7703 or src/ptr-compr.* for details about pointer compression. - static constexpr size_t kPtrComprHeapReservationSize = size_t{1} << 32; - static constexpr size_t kPtrComprIsolateRootAlignment = size_t{1} << 32; + static constexpr size_t kPtrComprCageReservationSize = size_t{1} << 32; + static constexpr size_t kPtrComprCageBaseAlignment = size_t{1} << 32; - V8_INLINE static internal::Address GetRootFromOnHeapAddress( + V8_INLINE static internal::Address GetPtrComprCageBaseFromOnHeapAddress( internal::Address addr) { - return addr & -static_cast(kPtrComprIsolateRootAlignment); + return addr & -static_cast(kPtrComprCageBaseAlignment); } V8_INLINE static internal::Address DecompressTaggedAnyField( internal::Address heap_object_ptr, uint32_t value) { - internal::Address root = GetRootFromOnHeapAddress(heap_object_ptr); - return root + static_cast(static_cast(value)); + internal::Address base = + GetPtrComprCageBaseFromOnHeapAddress(heap_object_ptr); + return base + static_cast(static_cast(value)); } #endif // V8_COMPRESS_POINTERS @@ -457,6 +511,15 @@ V8_INLINE void PerformCastCheck(T* data) { class BackingStoreBase {}; } // namespace internal + +V8_EXPORT bool CopyAndConvertArrayToCppBufferInt32(Local src, + int32_t* dst, + uint32_t max_length); + +V8_EXPORT bool CopyAndConvertArrayToCppBufferFloat64(Local src, + double* dst, + uint32_t max_length); + } // namespace v8 #endif // INCLUDE_V8_INTERNAL_H_ diff --git a/deps/include/v8-metrics.h b/deps/include/v8-metrics.h index 0217f40d6..a6eea6a86 100644 --- a/deps/include/v8-metrics.h +++ b/deps/include/v8-metrics.h @@ -5,7 +5,8 @@ #ifndef V8_METRICS_H_ #define V8_METRICS_H_ -#include "v8.h" // NOLINT(build/include_directory) +#include "v8-internal.h" // NOLINT(build/include_directory) +#include "v8.h" // NOLINT(build/include_directory) namespace v8 { namespace metrics { @@ -49,11 +50,19 @@ struct GarbageCollectionFullMainThreadIncrementalMark { int64_t cpp_wall_clock_duration_in_us = -1; }; +struct GarbageCollectionFullMainThreadBatchedIncrementalMark { + std::vector events; +}; + struct GarbageCollectionFullMainThreadIncrementalSweep { int64_t wall_clock_duration_in_us = -1; int64_t cpp_wall_clock_duration_in_us = -1; }; +struct GarbageCollectionFullMainThreadBatchedIncrementalSweep { + std::vector events; +}; + struct GarbageCollectionYoungCycle { int64_t total_wall_clock_duration_in_us = -1; int64_t main_thread_wall_clock_duration_in_us = -1; @@ -69,6 +78,7 @@ struct WasmModuleDecoded { size_t module_size_in_bytes = 0; size_t function_count = 0; int64_t wall_clock_duration_in_us = -1; + int64_t cpu_duration_in_us = -1; }; struct WasmModuleCompiled { @@ -81,6 +91,7 @@ struct WasmModuleCompiled { size_t code_size_in_bytes = 0; size_t liftoff_bailout_count = 0; int64_t wall_clock_duration_in_us = -1; + int64_t cpu_duration_in_us = -1; }; struct WasmModuleInstantiated { @@ -94,20 +105,23 @@ struct WasmModuleTieredUp { bool lazy = false; size_t code_size_in_bytes = 0; int64_t wall_clock_duration_in_us = -1; + int64_t cpu_duration_in_us = -1; }; struct WasmModulesPerIsolate { size_t count = 0; }; -#define V8_MAIN_THREAD_METRICS_EVENTS(V) \ - V(GarbageCollectionFullCycle) \ - V(GarbageCollectionFullMainThreadIncrementalMark) \ - V(GarbageCollectionFullMainThreadIncrementalSweep) \ - V(GarbageCollectionYoungCycle) \ - V(WasmModuleDecoded) \ - V(WasmModuleCompiled) \ - V(WasmModuleInstantiated) \ +#define V8_MAIN_THREAD_METRICS_EVENTS(V) \ + V(GarbageCollectionFullCycle) \ + V(GarbageCollectionFullMainThreadIncrementalMark) \ + V(GarbageCollectionFullMainThreadBatchedIncrementalMark) \ + V(GarbageCollectionFullMainThreadIncrementalSweep) \ + V(GarbageCollectionFullMainThreadBatchedIncrementalSweep) \ + V(GarbageCollectionYoungCycle) \ + V(WasmModuleDecoded) \ + V(WasmModuleCompiled) \ + V(WasmModuleInstantiated) \ V(WasmModuleTieredUp) #define V8_THREAD_SAFE_METRICS_EVENTS(V) V(WasmModulesPerIsolate) @@ -183,6 +197,32 @@ class V8_EXPORT Recorder { static ContextId GetContextId(Local context); }; +/** + * Experimental API intended for the LongTasks UKM (crbug.com/1173527). + * The Reset() method should be called at the start of a potential + * long task. The Get() method returns durations of V8 work that + * happened during the task. + * + * This API is experimental and may be removed/changed in the future. + */ +struct V8_EXPORT LongTaskStats { + /** + * Resets durations of V8 work for the new task. + */ + V8_INLINE static void Reset(Isolate* isolate) { + v8::internal::Internals::IncrementLongTasksStatsCounter(isolate); + } + + /** + * Returns durations of V8 work that happened since the last Reset(). + */ + static LongTaskStats Get(Isolate* isolate); + + int64_t gc_full_atomic_wall_clock_duration_us = 0; + int64_t gc_full_incremental_wall_clock_duration_us = 0; + int64_t gc_young_wall_clock_duration_us = 0; +}; + } // namespace metrics } // namespace v8 diff --git a/deps/include/v8-platform.h b/deps/include/v8-platform.h index e27d26cb6..fc9a357fe 100644 --- a/deps/include/v8-platform.h +++ b/deps/include/v8-platform.h @@ -181,9 +181,8 @@ class JobDelegate { /** * Returns true if the current task is called from the thread currently * running JobHandle::Join(). - * TODO(etiennep): Make pure virtual once custom embedders implement it. */ - virtual bool IsJoiningThread() const { return false; } + virtual bool IsJoiningThread() const = 0; }; /** @@ -220,19 +219,14 @@ class JobHandle { * Forces all existing workers to yield ASAP but doesn’t wait for them. * Warning, this is dangerous if the Job's callback is bound to or has access * to state which may be deleted after this call. - * TODO(etiennep): Cleanup once implemented by all embedders. */ - virtual void CancelAndDetach() { Cancel(); } + virtual void CancelAndDetach() = 0; /** * Returns true if there's any work pending or any worker running. */ virtual bool IsActive() = 0; - // TODO(etiennep): Clean up once all overrides are removed. - V8_DEPRECATED("Use !IsActive() instead.") - virtual bool IsCompleted() { return !IsActive(); } - /** * Returns true if associated with a Job and other methods may be called. * Returns false after Join() or Cancel() was called. This may return true @@ -240,10 +234,6 @@ class JobHandle { */ virtual bool IsValid() = 0; - // TODO(etiennep): Clean up once all overrides are removed. - V8_DEPRECATED("Use IsValid() instead.") - virtual bool IsRunning() { return IsValid(); } - /** * Returns true if job priority can be changed. */ @@ -272,10 +262,6 @@ class JobTask { * it must not call back any JobHandle methods. */ virtual size_t GetMaxConcurrency(size_t worker_count) const = 0; - - // TODO(1114823): Clean up once all overrides are removed. - V8_DEPRECATED("Use the version that takes |worker_count|.") - virtual size_t GetMaxConcurrency() const { return 0; } }; /** @@ -408,7 +394,6 @@ class PageAllocator { kNoAccess, kRead, kReadWrite, - // TODO(hpayer): Remove this flag. Memory should never be rwx. kReadWriteExecute, kReadExecute, // Set this when reserving memory that will later require kReadWriteExecute diff --git a/deps/include/v8-profiler.h b/deps/include/v8-profiler.h index 85d3f8a48..9a40cfcf3 100644 --- a/deps/include/v8-profiler.h +++ b/deps/include/v8-profiler.h @@ -289,8 +289,8 @@ class V8_EXPORT CpuProfilingOptions { * interval, set via SetSamplingInterval(). If * zero, the sampling interval will be equal to * the profiler's sampling interval. - * \param filter_context Deprecated option to filter by context, currently a - * no-op. + * \param filter_context If specified, profiles will only contain frames + * using this context. Other frames will be elided. */ CpuProfilingOptions( CpuProfilingMode mode = kLeafNodeLineNumbers, @@ -304,9 +304,13 @@ class V8_EXPORT CpuProfilingOptions { private: friend class internal::CpuProfile; + bool has_filter_context() const { return !filter_context_.IsEmpty(); } + void* raw_filter_context() const; + CpuProfilingMode mode_; unsigned max_samples_; int sampling_interval_us_; + CopyablePersistentTraits::CopyablePersistent filter_context_; }; /** @@ -492,7 +496,7 @@ class V8_EXPORT HeapGraphNode { /** * An interface for exporting data from V8, using "push" model. */ -class V8_EXPORT OutputStream { // NOLINT +class V8_EXPORT OutputStream { public: enum WriteResult { kContinue = 0, @@ -519,7 +523,6 @@ class V8_EXPORT OutputStream { // NOLINT } }; - /** * HeapSnapshots record the state of the JS heap at some moment. */ @@ -586,7 +589,7 @@ class V8_EXPORT HeapSnapshot { * An interface for reporting progress and controlling long-running * activities. */ -class V8_EXPORT ActivityControl { // NOLINT +class V8_EXPORT ActivityControl { public: enum ControlOption { kContinue = 0, @@ -600,7 +603,6 @@ class V8_EXPORT ActivityControl { // NOLINT virtual ControlOption ReportProgressValue(int done, int total) = 0; }; - /** * AllocationProfile is a sampled profile of allocations done by the program. * This is structured as a call-graph. @@ -900,7 +902,8 @@ class V8_EXPORT HeapProfiler { const HeapSnapshot* TakeHeapSnapshot( ActivityControl* control = nullptr, ObjectNameResolver* global_object_name_resolver = nullptr, - bool treat_global_objects_as_roots = true); + bool treat_global_objects_as_roots = true, + bool capture_numeric_value = false); /** * Starts tracking of heap objects population statistics. After calling diff --git a/deps/include/v8-util.h b/deps/include/v8-util.h index 89ec4f6a7..8e4d66153 100644 --- a/deps/include/v8-util.h +++ b/deps/include/v8-util.h @@ -43,7 +43,7 @@ class StdMapTraits { static bool Empty(Impl* impl) { return impl->empty(); } static size_t Size(Impl* impl) { return impl->size(); } - static void Swap(Impl& a, Impl& b) { std::swap(a, b); } // NOLINT + static void Swap(Impl& a, Impl& b) { std::swap(a, b); } static Iterator Begin(Impl* impl) { return impl->begin(); } static Iterator End(Impl* impl) { return impl->end(); } static K Key(Iterator it) { return it->first; } diff --git a/deps/include/v8-version.h b/deps/include/v8-version.h index 0b8ac1f0a..8b88700b5 100644 --- a/deps/include/v8-version.h +++ b/deps/include/v8-version.h @@ -9,9 +9,9 @@ // NOTE these macros are used by some of the tool scripts and the build // system so their names cannot be changed without changing the scripts. #define V8_MAJOR_VERSION 9 -#define V8_MINOR_VERSION 0 -#define V8_BUILD_NUMBER 257 -#define V8_PATCH_LEVEL 18 +#define V8_MINOR_VERSION 4 +#define V8_BUILD_NUMBER 146 +#define V8_PATCH_LEVEL 16 // Use 1 for candidates and 0 otherwise. // (Boolean macro values are not supported by all preprocessors.) diff --git a/deps/include/v8.h b/deps/include/v8.h index 7cb19bbed..78c454e33 100644 --- a/deps/include/v8.h +++ b/deps/include/v8.h @@ -50,6 +50,7 @@ class CFunction; class CallHandlerHelper; class Context; class CppHeap; +class CTypeInfo; class Data; class Date; class EscapableHandleScope; @@ -127,6 +128,7 @@ template class PropertyCallbackInfo; template class ReturnValue; namespace internal { +class BackgroundDeserializeTask; class BasicTracedReferenceExtractor; class ExternalString; class FunctionCallbackArguments; @@ -596,7 +598,7 @@ template class PersistentBase { */ V8_INLINE uint16_t WrapperClassId() const; - PersistentBase(const PersistentBase& other) = delete; // NOLINT + PersistentBase(const PersistentBase& other) = delete; void operator=(const PersistentBase&) = delete; private: @@ -708,7 +710,7 @@ template class Persistent : public PersistentBase { return *this; } template - V8_INLINE Persistent& operator=(const Persistent& that) { // NOLINT + V8_INLINE Persistent& operator=(const Persistent& that) { Copy(that); return *this; } @@ -723,7 +725,7 @@ template class Persistent : public PersistentBase { // TODO(dcarney): this is pretty useless, fix or remove template - V8_INLINE static Persistent& Cast(const Persistent& that) { // NOLINT + V8_INLINE static Persistent& Cast(const Persistent& that) { #ifdef V8_ENABLE_CHECKS // If we're going to perform the type check then we have to check // that the handle isn't empty before doing the checked cast. @@ -734,7 +736,7 @@ template class Persistent : public PersistentBase { // TODO(dcarney): this is pretty useless, fix or remove template - V8_INLINE Persistent& As() const { // NOLINT + V8_INLINE Persistent& As() const { return Persistent::Cast(*this); } @@ -803,7 +805,7 @@ class Global : public PersistentBase { /** * Pass allows returning uniques from functions, etc. */ - Global Pass() { return static_cast(*this); } // NOLINT + Global Pass() { return static_cast(*this); } /* * For compatibility with Chromium's base::Bind (base::Passed). @@ -885,6 +887,8 @@ class TracedReferenceBase { std::memory_order_relaxed); } + V8_EXPORT void CheckValue() const; + // val_ points to a GlobalHandles node. internal::Address* val_ = nullptr; @@ -905,8 +909,8 @@ class TracedReferenceBase { * The exact semantics are: * - Tracing garbage collections use |v8::EmbedderHeapTracer| or cppgc. * - Non-tracing garbage collections refer to - * |v8::EmbedderHeapTracer::IsRootForNonTracingGC()| whether the handle should - * be treated as root or not. + * |v8::EmbedderRootsHandler::IsRoot()| whether the handle should + * be treated as root or not. * * Note that the base class cannot be instantiated itself. Choose from * - TracedGlobal @@ -926,8 +930,18 @@ class BasicTracedReference : public TracedReferenceBase { const_cast&>(*this)); } - T* operator->() const { return reinterpret_cast(val_); } - T* operator*() const { return reinterpret_cast(val_); } + T* operator->() const { +#ifdef V8_ENABLE_CHECKS + CheckValue(); +#endif // V8_ENABLE_CHECKS + return reinterpret_cast(val_); + } + T* operator*() const { +#ifdef V8_ENABLE_CHECKS + CheckValue(); +#endif // V8_ENABLE_CHECKS + return reinterpret_cast(val_); + } private: enum DestructionMode { kWithDestructor, kWithoutDestructor }; @@ -1427,7 +1441,7 @@ class ScriptOriginOptions { */ class ScriptOrigin { public: - V8_DEPRECATE_SOON("Use constructor with primitive C++ types") + V8_DEPRECATED("Use constructor with primitive C++ types") V8_INLINE explicit ScriptOrigin( Local resource_name, Local resource_line_offset, Local resource_column_offset, @@ -1438,7 +1452,7 @@ class ScriptOrigin { Local is_wasm = Local(), Local is_module = Local(), Local host_defined_options = Local()); - V8_DEPRECATE_SOON("Use constructor that takes an isolate") + V8_DEPRECATED("Use constructor that takes an isolate") V8_INLINE explicit ScriptOrigin( Local resource_name, int resource_line_offset = 0, int resource_column_offset = 0, @@ -1457,11 +1471,11 @@ class ScriptOrigin { Local host_defined_options = Local()); V8_INLINE Local ResourceName() const; - V8_DEPRECATE_SOON("Use getter with primitvie C++ types.") + V8_DEPRECATED("Use getter with primitvie C++ types.") V8_INLINE Local ResourceLineOffset() const; - V8_DEPRECATE_SOON("Use getter with primitvie C++ types.") + V8_DEPRECATED("Use getter with primitvie C++ types.") V8_INLINE Local ResourceColumnOffset() const; - V8_DEPRECATE_SOON("Use getter with primitvie C++ types.") + V8_DEPRECATED("Use getter with primitvie C++ types.") V8_INLINE Local ScriptID() const; V8_INLINE int LineOffset() const; V8_INLINE int ColumnOffset() const; @@ -1491,7 +1505,7 @@ class V8_EXPORT UnboundScript { */ Local