diff --git a/android/CMakeLists.txt b/android/CMakeLists.txt index 64b18a51..d38348de 100644 --- a/android/CMakeLists.txt +++ b/android/CMakeLists.txt @@ -79,11 +79,6 @@ if(USE_SE_V8) ) endif() - add_library(v8_inspector STATIC IMPORTED GLOBAL) - set_target_properties(v8_inspector PROPERTIES - IMPORTED_LOCATION ${platform_spec_path}/v8/libinspector.a - INTERFACE_INCLUDE_DIRECTORIES ${platform_spec_path}/include/v8 - ) set(se_libs_name v8_monolith) set(se_libs_include ${platform_spec_path}/include/v8) endif() @@ -96,12 +91,6 @@ if(USE_WEBSOCKET_SERVER) ) endif() -if(USE_SE_V8 AND USE_V8_DEBUGGER ) - list(APPEND CC_EXTERNAL_LIBS - v8_inspector - ) -endif() - ############################# glslang ############################# set(glslang_libs_name glslang glslang-default-resource-limits MachineIndependent OGLCompiler OSDependent SPIRV SPIRV-Tools-opt SPIRV-Tools GenericCodeGen) foreach(lib IN LISTS glslang_libs_name) diff --git a/android/arm64-v8a/include/v8/OWNERS b/android/arm64-v8a/include/v8/OWNERS index 7d538da1..535040c5 100644 --- a/android/arm64-v8a/include/v8/OWNERS +++ b/android/arm64-v8a/include/v8/OWNERS @@ -2,15 +2,20 @@ adamk@chromium.org cbruni@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=file:../src/inspector/OWNERS -per-file v8-inspector-protocol.h=file:../src/inspector/OWNERS + +per-file v8-debug.h=file:../src/debug/OWNERS + per-file js_protocol.pdl=file:../src/inspector/OWNERS +per-file v8-inspector*=file:../src/inspector/OWNERS +per-file v8-inspector*=file:../src/inspector/OWNERS + +# Needed by the auto_tag builder +per-file v8-version.h=v8-ci-autoroll-builder@chops-service-accounts.iam.gserviceaccount.com # For branch updates: per-file v8-version.h=file:../INFRA_OWNERS diff --git a/android/arm64-v8a/include/v8/cppgc/DEPS b/android/arm64-v8a/include/v8/cppgc/DEPS index 861d1187..2ec7ebbd 100644 --- a/android/arm64-v8a/include/v8/cppgc/DEPS +++ b/android/arm64-v8a/include/v8/cppgc/DEPS @@ -2,6 +2,7 @@ include_rules = [ "-include", "+v8config.h", "+v8-platform.h", + "+v8-source-location.h", "+cppgc", "-src", "+libplatform/libplatform.h", diff --git a/android/arm64-v8a/include/v8/cppgc/README.md b/android/arm64-v8a/include/v8/cppgc/README.md index 3a2db6df..d825ea5b 100644 --- a/android/arm64-v8a/include/v8/cppgc/README.md +++ b/android/arm64-v8a/include/v8/cppgc/README.md @@ -1,5 +1,135 @@ -# C++ Garbage Collection +# Oilpan: C++ Garbage Collection -This directory provides an open-source garbage collection library for C++. +Oilpan is an open-source garbage collection library for C++ that can be used stand-alone or in collaboration with V8's JavaScript garbage collector. +Oilpan implements mark-and-sweep garbage collection (GC) with limited compaction (for a subset of objects). -The library is under construction, meaning that *all APIs in this directory are incomplete and considered unstable and should not be used*. \ No newline at end of file +**Key properties** + +- Trace-based garbage collection; +- Incremental and concurrent marking; +- Incremental and concurrent sweeping; +- Precise on-heap memory layout; +- Conservative on-stack memory layout; +- Allows for collection with and without considering stack; +- Non-incremental and non-concurrent compaction for selected spaces; + +See the [Hello World](https://chromium.googlesource.com/v8/v8/+/main/samples/cppgc/hello-world.cc) example on how to get started using Oilpan to manage C++ code. + +Oilpan follows V8's project organization, see e.g. on how we accept [contributions](https://v8.dev/docs/contribute) and [provide a stable API](https://v8.dev/docs/api). + +## Threading model + +Oilpan features thread-local garbage collection and assumes heaps are not shared among threads. +In other words, objects are accessed and ultimately reclaimed by the garbage collector on the same thread that allocates them. +This allows Oilpan to run garbage collection in parallel with mutators running in other threads. + +References to objects belonging to another thread's heap are modeled using cross-thread roots. +This is even true for on-heap to on-heap references. + +Oilpan heaps may generally not be accessed from different threads unless otherwise noted. + +## Heap partitioning + +Oilpan's heaps are partitioned into spaces. +The space for an object is chosen depending on a number of criteria, e.g.: + +- Objects over 64KiB are allocated in a large object space +- Objects can be assigned to a dedicated custom space. + Custom spaces can also be marked as compactable. +- Other objects are allocated in one of the normal page spaces bucketed depending on their size. + +## Precise and conservative garbage collection + +Oilpan supports two kinds of GCs: + +1. **Conservative GC.** +A GC is called conservative when it is executed while the regular native stack is not empty. +In this case, the native stack might contain references to objects in Oilpan's heap, which should be kept alive. +The GC scans the native stack and treats the pointers discovered via the native stack as part of the root set. +This kind of GC is considered imprecise because values on stack other than references may accidentally appear as references to on-heap object, which means these objects will be kept alive despite being in practice unreachable from the application as an actual reference. + +2. **Precise GC.** +A precise GC is triggered at the end of an event loop, which is controlled by an embedder via a platform. +At this point, it is guaranteed that there are no on-stack references pointing to Oilpan's heap. +This means there is no risk of confusing other value types with references. +Oilpan has precise knowledge of on-heap object layouts, and so it knows exactly where pointers lie in memory. +Oilpan can just start marking from the regular root set and collect all garbage precisely. + +## Atomic, incremental and concurrent garbage collection + +Oilpan has three modes of operation: + +1. **Atomic GC.** +The entire GC cycle, including all its phases (e.g. see [Marking](#Marking-phase) and [Sweeping](#Sweeping-phase)), are executed back to back in a single pause. +This mode of operation is also known as Stop-The-World (STW) garbage collection. +It results in the most jank (due to a single long pause), but is overall the most efficient (e.g. no need for write barriers). + +2. **Incremental GC.** +Garbage collection work is split up into multiple steps which are interleaved with the mutator, i.e. user code chunked into tasks. +Each step is a small chunk of work that is executed either as dedicated tasks between mutator tasks or, as needed, during mutator tasks. +Using incremental GC introduces the need for write barriers that record changes to the object graph so that a consistent state is observed and no objects are accidentally considered dead and reclaimed. +The incremental steps are followed by a smaller atomic pause to finalize garbage collection. +The smaller pause times, due to smaller chunks of work, helps with reducing jank. + +3. **Concurrent GC.** +This is the most common type of GC. +It builds on top of incremental GC and offloads much of the garbage collection work away from the mutator thread and on to background threads. +Using concurrent GC allows the mutator thread to spend less time on GC and more on the actual mutator. + +## Marking phase + +The marking phase consists of the following steps: + +1. Mark all objects in the root set. + +2. Mark all objects transitively reachable from the root set by calling `Trace()` methods defined on each object. + +3. Clear out all weak handles to unreachable objects and run weak callbacks. + +The marking phase can be executed atomically in a stop-the-world manner, in which all 3 steps are executed one after the other. + +Alternatively, it can also be executed incrementally/concurrently. +With incremental/concurrent marking, step 1 is executed in a short pause after which the mutator regains control. +Step 2 is repeatedly executed in an interleaved manner with the mutator. +When the GC is ready to finalize, i.e. step 2 is (almost) finished, another short pause is triggered in which step 2 is finished and step 3 is performed. + +To prevent a user-after-free (UAF) issues it is required for Oilpan to know about all edges in the object graph. +This means that all pointers except on-stack pointers must be wrapped with Oilpan's handles (i.e., Persistent<>, Member<>, WeakMember<>). +Raw pointers to on-heap objects create an edge that Oilpan cannot observe and cause UAF issues +Thus, raw pointers shall not be used to reference on-heap objects (except for raw pointers on native stacks). + +## Sweeping phase + +The sweeping phase consists of the following steps: + +1. Invoke pre-finalizers. +At this point, no destructors have been invoked and no memory has been reclaimed. +Pre-finalizers are allowed to access any other on-heap objects, even those that may get destructed. + +2. Sweeping invokes destructors of the dead (unreachable) objects and reclaims memory to be reused by future allocations. + +Assumptions should not be made about the order and the timing of their execution. +There is no guarantee on the order in which the destructors are invoked. +That's why destructors must not access any other on-heap objects (which might have already been destructed). +If some destructor unavoidably needs to access other on-heap objects, it will have to be converted to a pre-finalizer. +The pre-finalizer is allowed to access other on-heap objects. + +The mutator is resumed before all destructors have ran. +For example, imagine a case where X is a client of Y, and Y holds a list of clients. +If the code relies on X's destructor removing X from the list, there is a risk that Y iterates the list and calls some method of X which may touch other on-heap objects. +This causes a use-after-free. +Care must be taken to make sure that X is explicitly removed from the list before the mutator resumes its execution in a way that doesn't rely on X's destructor (e.g. a pre-finalizer). + +Similar to marking, sweeping can be executed in either an atomic stop-the-world manner or incrementally/concurrently. +With incremental/concurrent sweeping, step 2 is interleaved with mutator. +Incremental/concurrent sweeping can be atomically finalized in case it is needed to trigger another GC cycle. +Even with concurrent sweeping, destructors are guaranteed to run on the thread the object has been allocated on to preserve C++ semantics. + +Notes: + +* Weak processing runs only when the holder object of the WeakMember outlives the pointed object. +If the holder object and the pointed object die at the same time, weak processing doesn't run. +It is wrong to write code assuming that the weak processing always runs. + +* Pre-finalizers are heavy because the thread needs to scan all pre-finalizers at each sweeping phase to determine which pre-finalizers should be invoked (the thread needs to invoke pre-finalizers of dead objects). +Adding pre-finalizers to frequently created objects should be avoided. diff --git a/android/arm64-v8a/include/v8/cppgc/allocation.h b/android/arm64-v8a/include/v8/cppgc/allocation.h index f4f0e72b..69883fb3 100644 --- a/android/arm64-v8a/include/v8/cppgc/allocation.h +++ b/android/arm64-v8a/include/v8/cppgc/allocation.h @@ -5,24 +5,38 @@ #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) + +#if defined(__has_attribute) +#if __has_attribute(assume_aligned) +#define CPPGC_DEFAULT_ALIGNED \ + __attribute__((assume_aligned(api_constants::kDefaultAlignment))) +#define CPPGC_DOUBLE_WORD_ALIGNED \ + __attribute__((assume_aligned(2 * api_constants::kDefaultAlignment))) +#endif // __has_attribute(assume_aligned) +#endif // defined(__has_attribute) + +#if !defined(CPPGC_DEFAULT_ALIGNED) +#define CPPGC_DEFAULT_ALIGNED +#endif + +#if !defined(CPPGC_DOUBLE_WORD_ALIGNED) +#define CPPGC_DOUBLE_WORD_ALIGNED +#endif namespace cppgc { -template -class MakeGarbageCollectedTraitBase; - -namespace internal { -class ObjectAllocator; -} // namespace internal - /** * AllocationHandle is used to allocate garbage-collected objects. */ @@ -30,6 +44,9 @@ class AllocationHandle; namespace internal { +// Similar to C++17 std::align_val_t; +enum class AlignVal : size_t {}; + class V8_EXPORT MakeGarbageCollectedTraitInternal { protected: static inline void MarkObjectAsFullyConstructed(const void* payload) { @@ -39,36 +56,81 @@ class V8_EXPORT MakeGarbageCollectedTraitInternal { const_cast(reinterpret_cast( reinterpret_cast(payload) - api_constants::kFullyConstructedBitFieldOffsetFromPayload))); - atomic_mutable_bitfield->fetch_or(api_constants::kFullyConstructedBitMask, - std::memory_order_release); + // It's safe to split use load+store here (instead of a read-modify-write + // operation), since it's guaranteed that this 16-bit bitfield is only + // modified by a single thread. This is cheaper in terms of code bloat (on + // ARM) and performance. + uint16_t value = atomic_mutable_bitfield->load(std::memory_order_relaxed); + value |= api_constants::kFullyConstructedBitMask; + atomic_mutable_bitfield->store(value, std::memory_order_release); } - template - struct SpacePolicy { - static void* Allocate(AllocationHandle& handle, size_t size) { - // Custom space. + // Dispatch based on compile-time information. + // + // Default implementation is for a custom space with >`kDefaultAlignment` byte + // alignment. + template + struct AllocationDispatcher final { + static void* Invoke(AllocationHandle& handle, size_t size) { + static_assert(std::is_base_of::value, + "Custom space must inherit from CustomSpaceBase."); + static_assert( + !CustomSpace::kSupportsCompaction, + "Custom spaces that support compaction do not support allocating " + "objects with non-default (i.e. word-sized) alignment."); + return MakeGarbageCollectedTraitInternal::Allocate( + handle, size, static_cast(alignment), + internal::GCInfoTrait::Index(), CustomSpace::kSpaceIndex); + } + }; + + // Fast path for regular allocations for the default space with + // `kDefaultAlignment` byte alignment. + template + struct AllocationDispatcher + final { + static void* Invoke(AllocationHandle& handle, size_t size) { + return MakeGarbageCollectedTraitInternal::Allocate( + handle, size, internal::GCInfoTrait::Index()); + } + }; + + // Default space with >`kDefaultAlignment` byte alignment. + template + struct AllocationDispatcher final { + static void* Invoke(AllocationHandle& handle, size_t size) { + return MakeGarbageCollectedTraitInternal::Allocate( + handle, size, static_cast(alignment), + internal::GCInfoTrait::Index()); + } + }; + + // Custom space with `kDefaultAlignment` byte alignment. + template + struct AllocationDispatcher + final { + static void* Invoke(AllocationHandle& handle, size_t size) { static_assert(std::is_base_of::value, "Custom space must inherit from CustomSpaceBase."); return MakeGarbageCollectedTraitInternal::Allocate( - handle, size, internal::GCInfoTrait::Index(), + 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, - GCInfoIndex index, CustomSpaceIndex space_index); + static void* CPPGC_DEFAULT_ALIGNED Allocate(cppgc::AllocationHandle&, size_t, + GCInfoIndex); + static void* CPPGC_DOUBLE_WORD_ALIGNED Allocate(cppgc::AllocationHandle&, + size_t, AlignVal, + GCInfoIndex); + static void* CPPGC_DEFAULT_ALIGNED Allocate(cppgc::AllocationHandle&, size_t, + GCInfoIndex, CustomSpaceIndex); + static void* CPPGC_DOUBLE_WORD_ALIGNED Allocate(cppgc::AllocationHandle&, + size_t, AlignVal, GCInfoIndex, + CustomSpaceIndex); friend class HeapObjectHeader; }; @@ -103,10 +165,22 @@ class MakeGarbageCollectedTraitBase * \returns the memory to construct an object of type T on. */ V8_INLINE static void* Allocate(AllocationHandle& handle, size_t size) { - return SpacePolicy< + static_assert( + std::is_base_of::value, + "U of GarbageCollected must be a base of T. Check " + "GarbageCollected base class inheritance."); + static constexpr size_t kWantedAlignment = + alignof(T) < internal::api_constants::kDefaultAlignment + ? internal::api_constants::kDefaultAlignment + : alignof(T); + static_assert( + kWantedAlignment <= internal::api_constants::kMaxSupportedAlignment, + "Requested alignment larger than alignof(std::max_align_t) bytes. " + "Please file a bug to possibly get this restriction lifted."); + return AllocationDispatcher< typename internal::GCInfoFolding< T, typename T::ParentMostGarbageCollectedType>::ResultType, - typename SpaceTrait::Space>::Allocate(handle, size); + typename SpaceTrait::Space, kWantedAlignment>::Invoke(handle, size); } /** @@ -201,7 +275,7 @@ struct PostConstructionCallbackTrait { * \returns an instance of type T. */ template -T* MakeGarbageCollected(AllocationHandle& handle, Args&&... args) { +V8_INLINE T* MakeGarbageCollected(AllocationHandle& handle, Args&&... args) { T* object = MakeGarbageCollectedTrait::Call(handle, std::forward(args)...); PostConstructionCallbackTrait::Call(object); @@ -219,8 +293,9 @@ T* MakeGarbageCollected(AllocationHandle& handle, Args&&... args) { * \returns an instance of type T. */ template -T* MakeGarbageCollected(AllocationHandle& handle, - AdditionalBytes additional_bytes, Args&&... args) { +V8_INLINE T* MakeGarbageCollected(AllocationHandle& handle, + AdditionalBytes additional_bytes, + Args&&... args) { T* object = MakeGarbageCollectedTrait::Call(handle, additional_bytes, std::forward(args)...); PostConstructionCallbackTrait::Call(object); @@ -229,4 +304,7 @@ T* MakeGarbageCollected(AllocationHandle& handle, } // namespace cppgc +#undef CPPGC_DEFAULT_ALIGNED +#undef CPPGC_DOUBLE_WORD_ALIGNED + #endif // INCLUDE_CPPGC_ALLOCATION_H_ diff --git a/android/arm64-v8a/include/v8/cppgc/common.h b/android/arm64-v8a/include/v8/cppgc/common.h index b6dbff3d..96103836 100644 --- a/android/arm64-v8a/include/v8/cppgc/common.h +++ b/android/arm64-v8a/include/v8/cppgc/common.h @@ -5,7 +5,6 @@ #ifndef INCLUDE_CPPGC_COMMON_H_ #define INCLUDE_CPPGC_COMMON_H_ -// TODO(chromium:1056170): Remove dependency on v8. #include "v8config.h" // NOLINT(build/include_directory) namespace cppgc { diff --git a/android/arm64-v8a/include/v8/cppgc/cross-thread-persistent.h b/android/arm64-v8a/include/v8/cppgc/cross-thread-persistent.h index 9cfcd23f..a5f8bac0 100644 --- a/android/arm64-v8a/include/v8/cppgc/cross-thread-persistent.h +++ b/android/arm64-v8a/include/v8/cppgc/cross-thread-persistent.h @@ -13,12 +13,62 @@ #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; + SetNodeSafe(nullptr); + } + + // GetNodeSafe() can be used for a thread-safe IsValid() check in a + // double-checked locking pattern. See ~BasicCrossThreadPersistent. + PersistentNode* GetNodeSafe() const { + return reinterpret_cast*>(&node_)->load( + std::memory_order_acquire); + } + + // The GC writes using SetNodeSafe() while holding the lock. + V8_CLANG_NO_SANITIZE("address") + void SetNodeSafe(PersistentNode* value) const { +#if defined(__has_feature) +#if __has_feature(address_sanitizer) +#define V8_IS_ASAN 1 +#endif +#endif + +#ifdef V8_IS_ASAN + __atomic_store(&node_, &value, __ATOMIC_RELEASE); +#else // !V8_IS_ASAN + // Non-ASAN builds can use atomics. This also covers MSVC which does not + // have the __atomic_store intrinsic. + reinterpret_cast*>(&node_)->store( + value, std::memory_order_release); +#endif // !V8_IS_ASAN + +#undef V8_IS_ASAN + } +}; + template -class BasicCrossThreadPersistent final : public PersistentBase, +class BasicCrossThreadPersistent final : public CrossThreadPersistentBase, public LocationPolicy, private WeaknessPolicy, private CheckingPolicy { @@ -26,27 +76,51 @@ class BasicCrossThreadPersistent final : public PersistentBase, using typename WeaknessPolicy::IsStrongPersistent; using PointeeType = T; - ~BasicCrossThreadPersistent() { Clear(); } + ~BasicCrossThreadPersistent() { + // This implements fast path for destroying empty/sentinel. + // + // Simplified version of `AssignUnsafe()` to allow calling without a + // complete type `T`. Uses double-checked locking with a simple thread-safe + // check for a valid handle based on a node. + if (GetNodeSafe()) { + PersistentRegionLock guard; + const void* old_value = GetValue(); + // The fast path check (GetNodeSafe()) does not acquire the lock. Recheck + // validity while holding the lock 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()); + } + } + // No need to call SetValue() as the handle is not used anymore. This can + // leave behind stale sentinel values but will always destroy the underlying + // node. + } - 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; PersistentRegionLock guard; CrossThreadPersistentRegion& region = this->GetPersistentRegion(raw); - SetNode(region.AllocateNode(this, &Trace)); + SetNode(region.AllocateNode(this, &TraceAsRoot)); this->CheckPointer(raw); } @@ -58,26 +132,27 @@ class BasicCrossThreadPersistent final : public PersistentBase, friend class BasicCrossThreadPersistent; }; - BasicCrossThreadPersistent( // NOLINT + BasicCrossThreadPersistent( UnsafeCtorTag, T* raw, const SourceLocation& loc = SourceLocation::Current()) - : PersistentBase(raw), LocationPolicy(loc) { + : CrossThreadPersistentBase(raw), LocationPolicy(loc) { if (!IsValid(raw)) return; CrossThreadPersistentRegion& region = this->GetPersistentRegion(raw); - SetNode(region.AllocateNode(this, &Trace)); + SetNode(region.AllocateNode(this, &TraceAsRoot)); this->CheckPointer(raw); } - BasicCrossThreadPersistent( // NOLINT + BasicCrossThreadPersistent( T& raw, const SourceLocation& loc = SourceLocation::Current()) : BasicCrossThreadPersistent(&raw, loc) {} template ::value>> - BasicCrossThreadPersistent( // NOLINT + BasicCrossThreadPersistent( internal::BasicMember + MemberCheckingPolicy, MemberStorageType> member, const SourceLocation& loc = SourceLocation::Current()) : BasicCrossThreadPersistent(member.Get(), loc) {} @@ -94,7 +169,7 @@ class BasicCrossThreadPersistent final : public PersistentBase, template ::value>> - BasicCrossThreadPersistent( // NOLINT + BasicCrossThreadPersistent( const BasicCrossThreadPersistent& other, @@ -113,7 +188,7 @@ class BasicCrossThreadPersistent final : public PersistentBase, BasicCrossThreadPersistent& operator=( const BasicCrossThreadPersistent& other) { PersistentRegionLock guard; - AssignUnsafe(other.Get()); + AssignSafe(guard, other.Get()); return *this; } @@ -125,7 +200,7 @@ class BasicCrossThreadPersistent final : public PersistentBase, OtherLocationPolicy, OtherCheckingPolicy>& other) { PersistentRegionLock guard; - AssignUnsafe(other.Get()); + AssignSafe(guard, other.Get()); return *this; } @@ -139,33 +214,50 @@ class BasicCrossThreadPersistent final : public PersistentBase, GetNode()->UpdateOwner(this); other.SetValue(nullptr); other.SetNode(nullptr); - this->CheckPointer(GetValue()); + this->CheckPointer(Get()); return *this; } + /** + * Assigns a raw pointer. + * + * Note: **Not thread-safe.** + */ BasicCrossThreadPersistent& operator=(T* other) { - Assign(other); + AssignUnsafe(other); return *this; } // Assignment from member. template ::value>> BasicCrossThreadPersistent& operator=( internal::BasicMember + MemberCheckingPolicy, MemberStorageType> member) { return operator=(member.Get()); } + /** + * Assigns a nullptr. + * + * \returns the handle. + */ BasicCrossThreadPersistent& operator=(std::nullptr_t) { Clear(); return *this; } + /** + * Assigns the sentinel pointer. + * + * \returns the handle. + */ BasicCrossThreadPersistent& operator=(SentinelPointer s) { - Assign(s); + PersistentRegionLock guard; + AssignSafe(guard, s); return *this; } @@ -187,24 +279,8 @@ class BasicCrossThreadPersistent final : public PersistentBase, * Clears the stored object. */ void Clear() { - // Simplified version of `Assign()` to allow calling without a complete type - // `T`. - const void* old_value = GetValue(); - if (IsValid(old_value)) { - PersistentRegionLock guard; - 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); + PersistentRegionLock guard; + AssignSafe(guard, nullptr); } /** @@ -236,7 +312,7 @@ class BasicCrossThreadPersistent final : public PersistentBase, * * \returns the object. */ - operator T*() const { return Get(); } // NOLINT + operator T*() const { return Get(); } /** * Dereferences the stored object. @@ -275,12 +351,11 @@ class BasicCrossThreadPersistent final : public PersistentBase, return ptr && ptr != kSentinelPointer; } - static void Trace(Visitor* v, const void* ptr) { - const auto* handle = static_cast(ptr); - v->TraceRoot(*handle, handle->Location()); + static void TraceAsRoot(RootVisitor& root_visitor, const void* ptr) { + root_visitor.Trace(*static_cast(ptr)); } - void Assign(T* ptr) { + void AssignUnsafe(T* ptr) { const void* old_value = GetValue(); if (IsValid(old_value)) { PersistentRegionLock guard; @@ -304,11 +379,11 @@ class BasicCrossThreadPersistent final : public PersistentBase, SetValue(ptr); if (!IsValid(ptr)) return; PersistentRegionLock guard; - SetNode(this->GetPersistentRegion(ptr).AllocateNode(this, &Trace)); + SetNode(this->GetPersistentRegion(ptr).AllocateNode(this, &TraceAsRoot)); this->CheckPointer(ptr); } - void AssignUnsafe(T* ptr) { + void AssignSafe(PersistentRegionLock&, T* ptr) { PersistentRegionLock::AssertLocked(); const void* old_value = GetValue(); if (IsValid(old_value)) { @@ -324,18 +399,25 @@ class BasicCrossThreadPersistent final : public PersistentBase, } SetValue(ptr); if (!IsValid(ptr)) return; - SetNode(this->GetPersistentRegion(ptr).AllocateNode(this, &Trace)); + SetNode(this->GetPersistentRegion(ptr).AllocateNode(this, &TraceAsRoot)); this->CheckPointer(ptr); } void ClearFromGC() const { - if (IsValid(GetValue())) { - WeaknessPolicy::GetPersistentRegion(GetValue()).FreeNode(GetNode()); - PersistentBase::ClearFromGC(); + if (IsValid(GetValueFromGC())) { + WeaknessPolicy::GetPersistentRegion(GetValueFromGC()) + .FreeNode(GetNodeFromGC()); + CrossThreadPersistentBase::ClearFromGC(); } } - friend class cppgc::Visitor; + // See Get() for details. + V8_CLANG_NO_SANITIZE("cfi-unrelated-cast") + T* GetFromGC() const { + return static_cast(const_cast(GetValueFromGC())); + } + + friend class internal::RootVisitor; }; template diff --git a/android/arm64-v8a/include/v8/cppgc/default-platform.h b/android/arm64-v8a/include/v8/cppgc/default-platform.h index 2ccdeddd..a27871cc 100644 --- a/android/arm64-v8a/include/v8/cppgc/default-platform.h +++ b/android/arm64-v8a/include/v8/cppgc/default-platform.h @@ -6,7 +6,6 @@ #define INCLUDE_CPPGC_DEFAULT_PLATFORM_H_ #include -#include #include "cppgc/platform.h" #include "libplatform/libplatform.h" @@ -20,15 +19,6 @@ namespace cppgc { */ class V8_EXPORT DefaultPlatform : public Platform { public: - /** - * Use this method instead of 'cppgc::InitializeProcess' when using - * 'cppgc::DefaultPlatform'. 'cppgc::DefaultPlatform::InitializeProcess' - * will initialize cppgc and v8 if needed (for non-standalone builds). - * - * \param platform DefaultPlatform instance used to initialize cppgc/v8. - */ - static void InitializeProcess(DefaultPlatform* platform); - using IdleTaskSupport = v8::platform::IdleTaskSupport; explicit DefaultPlatform( int thread_pool_size = 0, @@ -64,6 +54,8 @@ class V8_EXPORT DefaultPlatform : public Platform { return v8_platform_->GetTracingController(); } + v8::Platform* GetV8Platform() const { return v8_platform_.get(); } + protected: static constexpr v8::Isolate* kNoIsolate = nullptr; diff --git a/android/arm64-v8a/include/v8/cppgc/explicit-management.h b/android/arm64-v8a/include/v8/cppgc/explicit-management.h index 8fb321c0..0290328d 100644 --- a/android/arm64-v8a/include/v8/cppgc/explicit-management.h +++ b/android/arm64-v8a/include/v8/cppgc/explicit-management.h @@ -12,11 +12,30 @@ #include "cppgc/type-traits.h" namespace cppgc { + +class HeapHandle; + +namespace subtle { + +template +void FreeUnreferencedObject(HeapHandle& heap_handle, T& object); +template +bool Resize(T& object, AdditionalBytes additional_bytes); + +} // namespace subtle + namespace internal { -V8_EXPORT void FreeUnreferencedObject(void*); -V8_EXPORT bool Resize(void*, size_t); +class ExplicitManagementImpl final { + private: + V8_EXPORT static void FreeUnreferencedObject(HeapHandle&, void*); + V8_EXPORT static bool Resize(void*, size_t); + template + friend void subtle::FreeUnreferencedObject(HeapHandle&, T&); + template + friend bool subtle::Resize(T&, AdditionalBytes); +}; } // namespace internal namespace subtle { @@ -30,15 +49,20 @@ namespace subtle { * 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(T* object) { +void FreeUnreferencedObject(HeapHandle& heap_handle, T& object) { static_assert(IsGarbageCollectedTypeV, "Object must be of type GarbageCollected."); - if (!object) return; - internal::FreeUnreferencedObject(object); + internal::ExplicitManagementImpl::FreeUnreferencedObject(heap_handle, + &object); } /** @@ -53,6 +77,8 @@ void FreeUnreferencedObject(T* object) { * 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 @@ -64,7 +90,8 @@ 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); + return internal::ExplicitManagementImpl::Resize( + &object, sizeof(T) + additional_bytes.value); } } // namespace subtle diff --git a/android/arm64-v8a/include/v8/cppgc/garbage-collected.h b/android/arm64-v8a/include/v8/cppgc/garbage-collected.h index a3839e1b..6737c8be 100644 --- a/android/arm64-v8a/include/v8/cppgc/garbage-collected.h +++ b/android/arm64-v8a/include/v8/cppgc/garbage-collected.h @@ -5,8 +5,6 @@ #ifndef INCLUDE_CPPGC_GARBAGE_COLLECTED_H_ #define INCLUDE_CPPGC_GARBAGE_COLLECTED_H_ -#include - #include "cppgc/internal/api-constants.h" #include "cppgc/platform.h" #include "cppgc/trace-trait.h" @@ -16,28 +14,6 @@ namespace cppgc { class Visitor; -namespace internal { - -class GarbageCollectedBase { - public: - // Must use MakeGarbageCollected. - void* operator new(size_t) = delete; - void* operator new[](size_t) = delete; - // The garbage collector is taking care of reclaiming the object. Also, - // virtual destructor requires an unambiguous, accessible 'operator delete'. - void operator delete(void*) { -#ifdef V8_ENABLE_CHECKS - internal::Abort(); -#endif // V8_ENABLE_CHECKS - } - void operator delete[](void*) = delete; - - protected: - GarbageCollectedBase() = default; -}; - -} // namespace internal - /** * Base class for managed objects. Only descendent types of `GarbageCollected` * can be constructed using `MakeGarbageCollected()`. Must be inherited from as @@ -74,11 +50,24 @@ class GarbageCollectedBase { * \endcode */ template -class GarbageCollected : public internal::GarbageCollectedBase { +class GarbageCollected { public: using IsGarbageCollectedTypeMarker = void; using ParentMostGarbageCollectedType = T; + // Must use MakeGarbageCollected. + void* operator new(size_t) = delete; + void* operator new[](size_t) = delete; + // The garbage collector is taking care of reclaiming the object. Also, + // virtual destructor requires an unambiguous, accessible 'operator delete'. + void operator delete(void*) { +#ifdef V8_ENABLE_CHECKS + internal::Fatal( + "Manually deleting a garbage collected object is not allowed"); +#endif // V8_ENABLE_CHECKS + } + void operator delete[](void*) = delete; + protected: GarbageCollected() = default; }; @@ -101,7 +90,7 @@ class GarbageCollected : public internal::GarbageCollectedBase { * }; * \endcode */ -class GarbageCollectedMixin : public internal::GarbageCollectedBase { +class GarbageCollectedMixin { public: using IsGarbageCollectedMixinTypeMarker = void; diff --git a/android/arm64-v8a/include/v8/cppgc/heap-consistency.h b/android/arm64-v8a/include/v8/cppgc/heap-consistency.h index 47caea18..eb7fdaee 100644 --- a/android/arm64-v8a/include/v8/cppgc/heap-consistency.h +++ b/android/arm64-v8a/include/v8/cppgc/heap-consistency.h @@ -9,6 +9,7 @@ #include "cppgc/internal/write-barrier.h" #include "cppgc/macros.h" +#include "cppgc/member.h" #include "cppgc/trace-trait.h" #include "v8config.h" // NOLINT(build/include_directory) @@ -47,6 +48,29 @@ class HeapConsistency final { return internal::WriteBarrier::GetWriteBarrierType(slot, value, params); } + /** + * Gets the required write barrier type for a specific write. This override is + * only used for all the BasicMember types. + * + * \param slot Slot containing the pointer to the object. The slot itself + * must reside in an object that has been allocated using + * `MakeGarbageCollected()`. + * \param value The pointer to the object held via `BasicMember`. + * \param params Parameters that may be used for actual write barrier calls. + * Only filled if return value indicates that a write barrier is needed. The + * contents of the `params` are an implementation detail. + * \returns whether a write barrier is needed and which barrier to invoke. + */ + template + static V8_INLINE WriteBarrierType GetWriteBarrierType( + const internal::BasicMember& value, + WriteBarrierParams& params) { + return internal::WriteBarrier::GetWriteBarrierType( + value.GetRawSlot(), value.GetRawStorage(), params); + } + /** * Gets the required write barrier type for a specific write. * @@ -68,6 +92,23 @@ class HeapConsistency final { return internal::WriteBarrier::GetWriteBarrierType(slot, params, callback); } + /** + * Gets the required write barrier type for a specific write. + * This version is meant to be used in conjunction with with a marking write + * barrier barrier which doesn't consider the slot. + * + * \param value The pointer to the object. May be an interior pointer to an + * interface of the actual object. + * \param params Parameters that may be used for actual write barrier calls. + * Only filled if return value indicates that a write barrier is needed. The + * contents of the `params` are an implementation detail. + * \returns whether a write barrier is needed and which barrier to invoke. + */ + static V8_INLINE WriteBarrierType + GetWriteBarrierType(const void* value, WriteBarrierParams& params) { + return internal::WriteBarrier::GetWriteBarrierType(value, params); + } + /** * Conservative Dijkstra-style write barrier that processes an object if it * has not yet been processed. @@ -129,7 +170,39 @@ class HeapConsistency final { */ static V8_INLINE void GenerationalBarrier(const WriteBarrierParams& params, const void* slot) { - internal::WriteBarrier::GenerationalBarrier(params, slot); + internal::WriteBarrier::GenerationalBarrier< + internal::WriteBarrier::GenerationalBarrierType::kPreciseSlot>(params, + slot); + } + + /** + * Generational barrier for maintaining consistency when running with multiple + * generations. This version is used when slot contains uncompressed pointer. + * + * \param params The parameters retrieved from `GetWriteBarrierType()`. + * \param slot Uncompressed slot containing the direct pointer to the object. + * The slot itself must reside in an object that has been allocated using + * `MakeGarbageCollected()`. + */ + static V8_INLINE void GenerationalBarrierForUncompressedSlot( + const WriteBarrierParams& params, const void* uncompressed_slot) { + internal::WriteBarrier::GenerationalBarrier< + internal::WriteBarrier::GenerationalBarrierType:: + kPreciseUncompressedSlot>(params, uncompressed_slot); + } + + /** + * Generational barrier for source object that may contain outgoing pointers + * to objects in young generation. + * + * \param params The parameters retrieved from `GetWriteBarrierType()`. + * \param inner_pointer Pointer to the source object. + */ + static V8_INLINE void GenerationalBarrierForSourceObject( + const WriteBarrierParams& params, const void* inner_pointer) { + internal::WriteBarrier::GenerationalBarrier< + internal::WriteBarrier::GenerationalBarrierType::kImpreciseSlot>( + params, inner_pointer); } private: diff --git a/android/arm64-v8a/include/v8/cppgc/heap-handle.h b/android/arm64-v8a/include/v8/cppgc/heap-handle.h new file mode 100644 index 00000000..0d1d21e6 --- /dev/null +++ b/android/arm64-v8a/include/v8/cppgc/heap-handle.h @@ -0,0 +1,48 @@ +// Copyright 2022 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_HEAP_HANDLE_H_ +#define INCLUDE_CPPGC_HEAP_HANDLE_H_ + +#include "v8config.h" // NOLINT(build/include_directory) + +namespace cppgc { + +namespace internal { +class HeapBase; +class WriteBarrierTypeForCagedHeapPolicy; +class WriteBarrierTypeForNonCagedHeapPolicy; +} // namespace internal + +/** + * Opaque handle used for additional heap APIs. + */ +class HeapHandle { + public: + // Deleted copy ctor to avoid treating the type by value. + HeapHandle(const HeapHandle&) = delete; + HeapHandle& operator=(const HeapHandle&) = delete; + + private: + HeapHandle() = default; + + V8_INLINE bool is_incremental_marking_in_progress() const { + return is_incremental_marking_in_progress_; + } + + V8_INLINE bool is_young_generation_enabled() const { + return is_young_generation_enabled_; + } + + bool is_incremental_marking_in_progress_ = false; + bool is_young_generation_enabled_ = false; + + friend class internal::HeapBase; + friend class internal::WriteBarrierTypeForCagedHeapPolicy; + friend class internal::WriteBarrierTypeForNonCagedHeapPolicy; +}; + +} // namespace cppgc + +#endif // INCLUDE_CPPGC_HEAP_HANDLE_H_ diff --git a/android/arm64-v8a/include/v8/cppgc/heap-state.h b/android/arm64-v8a/include/v8/cppgc/heap-state.h index 3fd6b54a..28212589 100644 --- a/android/arm64-v8a/include/v8/cppgc/heap-state.h +++ b/android/arm64-v8a/include/v8/cppgc/heap-state.h @@ -38,6 +38,18 @@ class V8_EXPORT HeapState final { */ static bool IsSweeping(const HeapHandle& heap_handle); + /* + * Returns whether the garbage collector is currently sweeping on the thread + * owning this heap. This API allows the caller to determine whether it has + * been called from a destructor of a managed object. This API is experimental + * and may be removed in future. + * + * \param heap_handle The corresponding heap. + * \returns true if the garbage collector is currently sweeping on this + * thread, and false otherwise. + */ + static bool IsSweepingOnOwningThread(const HeapHandle& heap_handle); + /** * Returns whether the garbage collector is in the atomic pause, i.e., the * mutator is stopped from running. This API is experimental and is expected diff --git a/android/arm64-v8a/include/v8/cppgc/heap-statistics.h b/android/arm64-v8a/include/v8/cppgc/heap-statistics.h index cf8d6633..5e389874 100644 --- a/android/arm64-v8a/include/v8/cppgc/heap-statistics.h +++ b/android/arm64-v8a/include/v8/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::SupportsCppClassNamesAsObjectNames() is true. */ + 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 held 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/android/arm64-v8a/include/v8/cppgc/heap.h b/android/arm64-v8a/include/v8/cppgc/heap.h index fd0512f1..02ee12ea 100644 --- a/android/arm64-v8a/include/v8/cppgc/heap.h +++ b/android/arm64-v8a/include/v8/cppgc/heap.h @@ -5,6 +5,8 @@ #ifndef INCLUDE_CPPGC_HEAP_H_ #define INCLUDE_CPPGC_HEAP_H_ +#include +#include #include #include @@ -19,6 +21,7 @@ namespace cppgc { class AllocationHandle; +class HeapHandle; /** * Implementation details of cppgc. Those details are considered internal and @@ -29,11 +32,6 @@ namespace internal { class Heap; } // namespace internal -/** - * Used for additional heap APIs. - */ -class HeapHandle; - class V8_EXPORT Heap { public: /** @@ -57,7 +55,7 @@ class V8_EXPORT Heap { }; /** - * Specifies supported marking types + * Specifies supported marking types. */ enum class MarkingType : uint8_t { /** @@ -66,8 +64,8 @@ class V8_EXPORT Heap { */ kAtomic, /** - * Incremental marking, i.e. interleave marking is the rest of the - * application on the same thread. + * Incremental marking interleaves marking with the rest of the application + * workload on the same thread. */ kIncremental, /** @@ -77,13 +75,18 @@ class V8_EXPORT Heap { }; /** - * Specifies supported sweeping types + * Specifies supported sweeping types. */ enum class SweepingType : uint8_t { /** * Atomic stop-the-world sweeping. All of sweeping is performed at once. */ kAtomic, + /** + * Incremental sweeping interleaves sweeping with the rest of the + * application workload on the same thread. + */ + kIncremental, /** * Incremental and concurrent sweeping. Sweeping is split and interleaved * with the rest of the application. diff --git a/android/arm64-v8a/include/v8/cppgc/internal/api-constants.h b/android/arm64-v8a/include/v8/cppgc/internal/api-constants.h index a70f0071..4e2a637e 100644 --- a/android/arm64-v8a/include/v8/cppgc/internal/api-constants.h +++ b/android/arm64-v8a/include/v8/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) @@ -32,12 +32,52 @@ static constexpr uint16_t kFullyConstructedBitMask = uint16_t{1}; static constexpr size_t kPageSize = size_t{1} << 17; +#if defined(V8_TARGET_ARCH_ARM64) && defined(V8_OS_DARWIN) +constexpr size_t kGuardPageSize = 0; +#else +constexpr size_t kGuardPageSize = 4096; +#endif + static constexpr size_t kLargeObjectSizeThreshold = kPageSize / 2; +#if defined(CPPGC_POINTER_COMPRESSION) +#if defined(CPPGC_ENABLE_LARGER_CAGE) +constexpr unsigned kPointerCompressionShift = 3; +#else // !defined(CPPGC_ENABLE_LARGER_CAGE) +constexpr unsigned kPointerCompressionShift = 1; +#endif // !defined(CPPGC_ENABLE_LARGER_CAGE) +#endif // !defined(CPPGC_POINTER_COMPRESSION) + #if defined(CPPGC_CAGED_HEAP) -constexpr size_t kCagedHeapReservationSize = static_cast(4) * kGB; -constexpr size_t kCagedHeapReservationAlignment = kCagedHeapReservationSize; -#endif +#if defined(CPPGC_2GB_CAGE) +constexpr size_t kCagedHeapDefaultReservationSize = + static_cast(2) * kGB; +constexpr size_t kCagedHeapMaxReservationSize = + kCagedHeapDefaultReservationSize; +#else // !defined(CPPGC_2GB_CAGE) +constexpr size_t kCagedHeapDefaultReservationSize = + static_cast(4) * kGB; +#if defined(CPPGC_POINTER_COMPRESSION) +constexpr size_t kCagedHeapMaxReservationSize = + size_t{1} << (31 + kPointerCompressionShift); +#else // !defined(CPPGC_POINTER_COMPRESSION) +constexpr size_t kCagedHeapMaxReservationSize = + kCagedHeapDefaultReservationSize; +#endif // !defined(CPPGC_POINTER_COMPRESSION) +#endif // !defined(CPPGC_2GB_CAGE) +constexpr size_t kCagedHeapReservationAlignment = kCagedHeapMaxReservationSize; +#endif // defined(CPPGC_CAGED_HEAP) + +static constexpr size_t kDefaultAlignment = sizeof(void*); + +// Maximum support alignment for a type as in `alignof(T)`. +static constexpr size_t kMaxSupportedAlignment = 2 * kDefaultAlignment; + +// Granularity of heap allocations. +constexpr size_t kAllocationGranularity = sizeof(void*); + +// Default cacheline size. +constexpr size_t kCachelineSize = 64; } // namespace api_constants diff --git a/android/arm64-v8a/include/v8/cppgc/internal/base-page-handle.h b/android/arm64-v8a/include/v8/cppgc/internal/base-page-handle.h new file mode 100644 index 00000000..9c690755 --- /dev/null +++ b/android/arm64-v8a/include/v8/cppgc/internal/base-page-handle.h @@ -0,0 +1,45 @@ +// Copyright 2022 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_INTERNAL_BASE_PAGE_HANDLE_H_ +#define INCLUDE_CPPGC_INTERNAL_BASE_PAGE_HANDLE_H_ + +#include "cppgc/heap-handle.h" +#include "cppgc/internal/api-constants.h" +#include "cppgc/internal/logging.h" +#include "v8config.h" // NOLINT(build/include_directory) + +namespace cppgc { +namespace internal { + +// The class is needed in the header to allow for fast access to HeapHandle in +// the write barrier. +class BasePageHandle { + public: + static V8_INLINE BasePageHandle* FromPayload(void* payload) { + return reinterpret_cast( + (reinterpret_cast(payload) & + ~(api_constants::kPageSize - 1)) + + api_constants::kGuardPageSize); + } + static V8_INLINE const BasePageHandle* FromPayload(const void* payload) { + return FromPayload(const_cast(payload)); + } + + HeapHandle& heap_handle() { return heap_handle_; } + const HeapHandle& heap_handle() const { return heap_handle_; } + + protected: + explicit BasePageHandle(HeapHandle& heap_handle) : heap_handle_(heap_handle) { + CPPGC_DCHECK(reinterpret_cast(this) % api_constants::kPageSize == + api_constants::kGuardPageSize); + } + + HeapHandle& heap_handle_; +}; + +} // namespace internal +} // namespace cppgc + +#endif // INCLUDE_CPPGC_INTERNAL_BASE_PAGE_HANDLE_H_ diff --git a/android/arm64-v8a/include/v8/cppgc/internal/caged-heap-local-data.h b/android/arm64-v8a/include/v8/cppgc/internal/caged-heap-local-data.h index 1fa60b69..1eb87dfb 100644 --- a/android/arm64-v8a/include/v8/cppgc/internal/caged-heap-local-data.h +++ b/android/arm64-v8a/include/v8/cppgc/internal/caged-heap-local-data.h @@ -6,57 +6,108 @@ #define INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_LOCAL_DATA_H_ #include +#include +#include #include "cppgc/internal/api-constants.h" +#include "cppgc/internal/caged-heap.h" #include "cppgc/internal/logging.h" #include "cppgc/platform.h" #include "v8config.h" // NOLINT(build/include_directory) +#if __cpp_lib_bitopts +#include +#endif // __cpp_lib_bitopts + +#if defined(CPPGC_CAGED_HEAP) + namespace cppgc { namespace internal { class HeapBase; +class HeapBaseHandle; #if defined(CPPGC_YOUNG_GENERATION) -// AgeTable contains entries that correspond to 4KB memory regions. Each entry -// can be in one of three states: kOld, kYoung or kUnknown. -class AgeTable final { - static constexpr size_t kGranularityBits = 12; // 4KiB per byte. +// AgeTable is the bytemap needed for the fast generation check in the write +// barrier. AgeTable contains entries that correspond to 4096 bytes memory +// regions (cards). Each entry in the table represents generation of the objects +// that reside on the corresponding card (young, old or mixed). +class V8_EXPORT AgeTable final { + static constexpr size_t kRequiredSize = 1 * api_constants::kMB; + static constexpr size_t kAllocationGranularity = + api_constants::kAllocationGranularity; public: - enum class Age : uint8_t { kOld, kYoung, kUnknown }; + // Represents age of the objects living on a single card. + enum class Age : uint8_t { kOld, kYoung, kMixed }; + // When setting age for a range, consider or ignore ages of the adjacent + // cards. + enum class AdjacentCardsPolicy : uint8_t { kConsider, kIgnore }; - static constexpr size_t kEntrySizeInBytes = 1 << kGranularityBits; + static constexpr size_t kCardSizeInBytes = + api_constants::kCagedHeapDefaultReservationSize / kRequiredSize; - Age& operator[](uintptr_t offset) { return table_[entry(offset)]; } - Age operator[](uintptr_t offset) const { return table_[entry(offset)]; } + static constexpr size_t CalculateAgeTableSizeForHeapSize(size_t heap_size) { + return heap_size / kCardSizeInBytes; + } - void Reset(PageAllocator* allocator); + void SetAge(uintptr_t cage_offset, Age age) { + table_[card(cage_offset)] = age; + } + + V8_INLINE Age GetAge(uintptr_t cage_offset) const { + return table_[card(cage_offset)]; + } + + void SetAgeForRange(uintptr_t cage_offset_begin, uintptr_t cage_offset_end, + Age age, AdjacentCardsPolicy adjacent_cards_policy); + + Age GetAgeForRange(uintptr_t cage_offset_begin, + uintptr_t cage_offset_end) const; + + void ResetForTesting(); private: - static constexpr size_t kAgeTableSize = - api_constants::kCagedHeapReservationSize >> kGranularityBits; - - size_t entry(uintptr_t offset) const { + V8_INLINE size_t card(uintptr_t offset) const { + constexpr size_t kGranularityBits = +#if __cpp_lib_bitopts + std::countr_zero(static_cast(kCardSizeInBytes)); +#elif V8_HAS_BUILTIN_CTZ + __builtin_ctz(static_cast(kCardSizeInBytes)); +#else //! V8_HAS_BUILTIN_CTZ + // Hardcode and check with assert. +#if defined(CPPGC_2GB_CAGE) + 11; +#else // !defined(CPPGC_2GB_CAGE) + 12; +#endif // !defined(CPPGC_2GB_CAGE) +#endif // !V8_HAS_BUILTIN_CTZ + static_assert((1 << kGranularityBits) == kCardSizeInBytes); const size_t entry = offset >> kGranularityBits; - CPPGC_DCHECK(table_.size() > entry); + CPPGC_DCHECK(CagedHeapBase::GetAgeTableSize() > entry); return entry; } - std::array table_; +#if defined(V8_CC_GNU) + // gcc disallows flexible arrays in otherwise empty classes. + Age table_[0]; +#else // !defined(V8_CC_GNU) + Age table_[]; +#endif // !defined(V8_CC_GNU) }; -static_assert(sizeof(AgeTable) == 1 * api_constants::kMB, - "Size of AgeTable is 1MB"); - #endif // CPPGC_YOUNG_GENERATION struct CagedHeapLocalData final { - explicit CagedHeapLocalData(HeapBase* heap_base) : heap_base(heap_base) {} + V8_INLINE static CagedHeapLocalData& Get() { + return *reinterpret_cast(CagedHeapBase::GetBase()); + } + + static constexpr size_t CalculateLocalDataSizeForHeapSize(size_t heap_size) { + return AgeTable::CalculateAgeTableSizeForHeapSize(heap_size); + } - bool is_incremental_marking_in_progress = false; - HeapBase* heap_base = nullptr; #if defined(CPPGC_YOUNG_GENERATION) AgeTable age_table; #endif @@ -65,4 +116,6 @@ struct CagedHeapLocalData final { } // namespace internal } // namespace cppgc +#endif // defined(CPPGC_CAGED_HEAP) + #endif // INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_LOCAL_DATA_H_ diff --git a/android/arm64-v8a/include/v8/cppgc/internal/caged-heap.h b/android/arm64-v8a/include/v8/cppgc/internal/caged-heap.h new file mode 100644 index 00000000..0c987a95 --- /dev/null +++ b/android/arm64-v8a/include/v8/cppgc/internal/caged-heap.h @@ -0,0 +1,68 @@ +// Copyright 2022 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_INTERNAL_CAGED_HEAP_H_ +#define INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_H_ + +#include +#include + +#include "cppgc/internal/api-constants.h" +#include "cppgc/internal/base-page-handle.h" +#include "v8config.h" // NOLINT(build/include_directory) + +#if defined(CPPGC_CAGED_HEAP) + +namespace cppgc { +namespace internal { + +class V8_EXPORT CagedHeapBase { + public: + V8_INLINE static uintptr_t OffsetFromAddress(const void* address) { + return reinterpret_cast(address) & + (api_constants::kCagedHeapReservationAlignment - 1); + } + + V8_INLINE static bool IsWithinCage(const void* address) { + CPPGC_DCHECK(g_heap_base_); + return (reinterpret_cast(address) & + ~(api_constants::kCagedHeapReservationAlignment - 1)) == + g_heap_base_; + } + + V8_INLINE static bool AreWithinCage(const void* addr1, const void* addr2) { +#if defined(CPPGC_2GB_CAGE) + static constexpr size_t kHeapBaseShift = sizeof(uint32_t) * CHAR_BIT - 1; +#else //! defined(CPPGC_2GB_CAGE) +#if defined(CPPGC_POINTER_COMPRESSION) + static constexpr size_t kHeapBaseShift = + 31 + api_constants::kPointerCompressionShift; +#else // !defined(CPPGC_POINTER_COMPRESSION) + static constexpr size_t kHeapBaseShift = sizeof(uint32_t) * CHAR_BIT; +#endif // !defined(CPPGC_POINTER_COMPRESSION) +#endif //! defined(CPPGC_2GB_CAGE) + static_assert((static_cast(1) << kHeapBaseShift) == + api_constants::kCagedHeapMaxReservationSize); + CPPGC_DCHECK(g_heap_base_); + return !(((reinterpret_cast(addr1) ^ g_heap_base_) | + (reinterpret_cast(addr2) ^ g_heap_base_)) >> + kHeapBaseShift); + } + + V8_INLINE static uintptr_t GetBase() { return g_heap_base_; } + V8_INLINE static size_t GetAgeTableSize() { return g_age_table_size_; } + + private: + friend class CagedHeap; + + static uintptr_t g_heap_base_; + static size_t g_age_table_size_; +}; + +} // namespace internal +} // namespace cppgc + +#endif // defined(CPPGC_CAGED_HEAP) + +#endif // INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_H_ diff --git a/android/arm64-v8a/include/v8/cppgc/internal/compiler-specific.h b/android/arm64-v8a/include/v8/cppgc/internal/compiler-specific.h index c580894b..595b6398 100644 --- a/android/arm64-v8a/include/v8/cppgc/internal/compiler-specific.h +++ b/android/arm64-v8a/include/v8/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/android/arm64-v8a/include/v8/cppgc/internal/finalizer-trait.h b/android/arm64-v8a/include/v8/cppgc/internal/finalizer-trait.h index a9512659..ab49af87 100644 --- a/android/arm64-v8a/include/v8/cppgc/internal/finalizer-trait.h +++ b/android/arm64-v8a/include/v8/cppgc/internal/finalizer-trait.h @@ -19,7 +19,8 @@ struct HasFinalizeGarbageCollectedObject : std::false_type {}; template struct HasFinalizeGarbageCollectedObject< - T, void_t().FinalizeGarbageCollectedObject())>> + T, + std::void_t().FinalizeGarbageCollectedObject())>> : std::true_type {}; // The FinalizerTraitImpl specifies how to finalize objects. @@ -76,6 +77,8 @@ struct FinalizerTrait { } public: + static constexpr bool HasFinalizer() { return kNonTrivialFinalizer; } + // The callback used to finalize an object of type T. static constexpr FinalizationCallback kCallback = kNonTrivialFinalizer ? Finalize : nullptr; diff --git a/android/arm64-v8a/include/v8/cppgc/internal/gc-info.h b/android/arm64-v8a/include/v8/cppgc/internal/gc-info.h index b9074b1a..c8cb99ac 100644 --- a/android/arm64-v8a/include/v8/cppgc/internal/gc-info.h +++ b/android/arm64-v8a/include/v8/cppgc/internal/gc-info.h @@ -7,8 +7,10 @@ #include #include +#include #include "cppgc/internal/finalizer-trait.h" +#include "cppgc/internal/logging.h" #include "cppgc/internal/name-trait.h" #include "cppgc/trace-trait.h" #include "v8config.h" // NOLINT(build/include_directory) @@ -18,17 +20,94 @@ namespace internal { using GCInfoIndex = uint16_t; -// 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); +struct V8_EXPORT EnsureGCInfoIndexTrait final { + // Acquires a new GC info object and updates `registered_index` with the index + // that identifies that new info accordingly. + template + V8_INLINE static GCInfoIndex EnsureIndex( + std::atomic& registered_index) { + return EnsureGCInfoIndexTraitDispatch{}(registered_index); + } + + private: + template ::HasFinalizer(), + bool = NameTrait::HasNonHiddenName()> + struct EnsureGCInfoIndexTraitDispatch; + + static GCInfoIndex V8_PRESERVE_MOST + EnsureGCInfoIndex(std::atomic&, TraceCallback, + FinalizationCallback, NameCallback); + static GCInfoIndex V8_PRESERVE_MOST EnsureGCInfoIndex( + std::atomic&, TraceCallback, FinalizationCallback); + static GCInfoIndex V8_PRESERVE_MOST + EnsureGCInfoIndex(std::atomic&, TraceCallback, NameCallback); + static GCInfoIndex V8_PRESERVE_MOST + EnsureGCInfoIndex(std::atomic&, TraceCallback); +}; + +#define DISPATCH(has_finalizer, has_non_hidden_name, function) \ + template \ + struct EnsureGCInfoIndexTrait::EnsureGCInfoIndexTraitDispatch< \ + T, has_finalizer, has_non_hidden_name> { \ + V8_INLINE GCInfoIndex \ + operator()(std::atomic& registered_index) { \ + return function; \ + } \ + }; + +// ------------------------------------------------------- // +// DISPATCH(has_finalizer, has_non_hidden_name, function) // +// ------------------------------------------------------- // +DISPATCH(true, true, // + EnsureGCInfoIndex(registered_index, // + TraceTrait::Trace, // + FinalizerTrait::kCallback, // + NameTrait::GetName)) // +DISPATCH(true, false, // + EnsureGCInfoIndex(registered_index, // + TraceTrait::Trace, // + FinalizerTrait::kCallback)) // +DISPATCH(false, true, // + EnsureGCInfoIndex(registered_index, // + TraceTrait::Trace, // + NameTrait::GetName)) // +DISPATCH(false, false, // + EnsureGCInfoIndex(registered_index, // + TraceTrait::Trace)) // + +#undef DISPATCH + +// Trait determines how the garbage collector treats objects wrt. to traversing, +// finalization, and naming. +template +struct GCInfoTrait final { + V8_INLINE static GCInfoIndex Index() { + static_assert(sizeof(T), "T must be fully defined"); + static std::atomic + registered_index; // Uses zero initialization. + GCInfoIndex index = registered_index.load(std::memory_order_acquire); + if (V8_UNLIKELY(!index)) { + index = EnsureGCInfoIndexTrait::EnsureIndex(registered_index); + CPPGC_DCHECK(index != 0); + CPPGC_DCHECK(index == registered_index.load(std::memory_order_acquire)); + } + return index; + } + + static constexpr bool CheckCallbacksAreDefined() { + // No USE() macro available. + (void)static_cast(TraceTrait::Trace); + (void)static_cast(FinalizerTrait::kCallback); + (void)static_cast(NameTrait::GetName); + return true; + } +}; // 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 { +struct GCInfoFolding final { static constexpr bool kHasVirtualDestructorAtBase = std::has_virtual_destructor::value; static constexpr bool kBothTypesAreTriviallyDestructible = @@ -43,30 +122,24 @@ struct GCInfoFolding { 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>; -}; + // Always true. Forces the compiler to resolve callbacks which ensures that + // both modes don't break without requiring compiling a separate + // configuration. Only a single GCInfo (for `ResultType` below) will actually + // be instantiated but existence (and well-formedness) of all callbacks is + // checked. + static constexpr bool kCheckTypeGuardAlwaysTrue = + GCInfoTrait::CheckCallbacksAreDefined() && + GCInfoTrait::CheckCallbacksAreDefined(); -// Trait determines how the garbage collector treats objects wrt. to traversing, -// finalization, and naming. -template -struct GCInfoTrait final { - static GCInfoIndex Index() { - static_assert(sizeof(T), "T must be fully defined"); - 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); - } + // Folding would regress 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; }; } // namespace internal diff --git a/android/arm64-v8a/include/v8/cppgc/internal/logging.h b/android/arm64-v8a/include/v8/cppgc/internal/logging.h index 79beaef7..3a279fe0 100644 --- a/android/arm64-v8a/include/v8/cppgc/internal/logging.h +++ b/android/arm64-v8a/include/v8/cppgc/internal/logging.h @@ -20,18 +20,18 @@ FatalImpl(const char*, const SourceLocation& = SourceLocation::Current()); template struct EatParams {}; -#if DEBUG +#if defined(DEBUG) #define CPPGC_DCHECK_MSG(condition, message) \ do { \ if (V8_UNLIKELY(!(condition))) { \ ::cppgc::internal::DCheckImpl(message); \ } \ } while (false) -#else +#else // !defined(DEBUG) #define CPPGC_DCHECK_MSG(condition, message) \ (static_cast(::cppgc::internal::EatParams(condition), message)>{})) -#endif +#endif // !defined(DEBUG) #define CPPGC_DCHECK(condition) CPPGC_DCHECK_MSG(condition, #condition) diff --git a/android/arm64-v8a/include/v8/cppgc/internal/member-storage.h b/android/arm64-v8a/include/v8/cppgc/internal/member-storage.h new file mode 100644 index 00000000..61b255ba --- /dev/null +++ b/android/arm64-v8a/include/v8/cppgc/internal/member-storage.h @@ -0,0 +1,256 @@ +// Copyright 2022 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_INTERNAL_MEMBER_STORAGE_H_ +#define INCLUDE_CPPGC_INTERNAL_MEMBER_STORAGE_H_ + +#include +#include +#include + +#include "cppgc/internal/api-constants.h" +#include "cppgc/internal/logging.h" +#include "cppgc/sentinel-pointer.h" +#include "v8config.h" // NOLINT(build/include_directory) + +namespace cppgc { +namespace internal { + +enum class WriteBarrierSlotType { + kCompressed, + kUncompressed, +}; + +#if defined(CPPGC_POINTER_COMPRESSION) + +#if defined(__clang__) +// Attribute const allows the compiler to assume that CageBaseGlobal::g_base_ +// doesn't change (e.g. across calls) and thereby avoid redundant loads. +#define CPPGC_CONST __attribute__((const)) +#define CPPGC_REQUIRE_CONSTANT_INIT \ + __attribute__((require_constant_initialization)) +#else // defined(__clang__) +#define CPPGC_CONST +#define CPPGC_REQUIRE_CONSTANT_INIT +#endif // defined(__clang__) + +class V8_EXPORT CageBaseGlobal final { + public: + V8_INLINE CPPGC_CONST static uintptr_t Get() { + CPPGC_DCHECK(IsBaseConsistent()); + return g_base_.base; + } + + V8_INLINE CPPGC_CONST static bool IsSet() { + CPPGC_DCHECK(IsBaseConsistent()); + return (g_base_.base & ~kLowerHalfWordMask) != 0; + } + + private: + // We keep the lower halfword as ones to speed up decompression. + static constexpr uintptr_t kLowerHalfWordMask = + (api_constants::kCagedHeapReservationAlignment - 1); + + static union alignas(api_constants::kCachelineSize) Base { + uintptr_t base; + char cache_line[api_constants::kCachelineSize]; + } g_base_ CPPGC_REQUIRE_CONSTANT_INIT; + + CageBaseGlobal() = delete; + + V8_INLINE static bool IsBaseConsistent() { + return kLowerHalfWordMask == (g_base_.base & kLowerHalfWordMask); + } + + friend class CageBaseGlobalUpdater; +}; + +#undef CPPGC_REQUIRE_CONSTANT_INIT +#undef CPPGC_CONST + +class V8_TRIVIAL_ABI CompressedPointer final { + public: + using IntegralType = uint32_t; + static constexpr auto kWriteBarrierSlotType = + WriteBarrierSlotType::kCompressed; + + V8_INLINE CompressedPointer() : value_(0u) {} + V8_INLINE explicit CompressedPointer(const void* ptr) + : value_(Compress(ptr)) {} + V8_INLINE explicit CompressedPointer(std::nullptr_t) : value_(0u) {} + V8_INLINE explicit CompressedPointer(SentinelPointer) + : value_(kCompressedSentinel) {} + + V8_INLINE const void* Load() const { return Decompress(value_); } + V8_INLINE const void* LoadAtomic() const { + return Decompress( + reinterpret_cast&>(value_).load( + std::memory_order_relaxed)); + } + + V8_INLINE void Store(const void* ptr) { value_ = Compress(ptr); } + V8_INLINE void StoreAtomic(const void* value) { + reinterpret_cast&>(value_).store( + Compress(value), std::memory_order_relaxed); + } + + V8_INLINE void Clear() { value_ = 0u; } + V8_INLINE bool IsCleared() const { return !value_; } + + V8_INLINE bool IsSentinel() const { return value_ == kCompressedSentinel; } + + V8_INLINE uint32_t GetAsInteger() const { return value_; } + + V8_INLINE friend bool operator==(CompressedPointer a, CompressedPointer b) { + return a.value_ == b.value_; + } + V8_INLINE friend bool operator!=(CompressedPointer a, CompressedPointer b) { + return a.value_ != b.value_; + } + V8_INLINE friend bool operator<(CompressedPointer a, CompressedPointer b) { + return a.value_ < b.value_; + } + V8_INLINE friend bool operator<=(CompressedPointer a, CompressedPointer b) { + return a.value_ <= b.value_; + } + V8_INLINE friend bool operator>(CompressedPointer a, CompressedPointer b) { + return a.value_ > b.value_; + } + V8_INLINE friend bool operator>=(CompressedPointer a, CompressedPointer b) { + return a.value_ >= b.value_; + } + + static V8_INLINE IntegralType Compress(const void* ptr) { + static_assert(SentinelPointer::kSentinelValue == + 1 << api_constants::kPointerCompressionShift, + "The compression scheme relies on the sentinel encoded as 1 " + "<< kPointerCompressionShift"); + static constexpr size_t kGigaCageMask = + ~(api_constants::kCagedHeapReservationAlignment - 1); + static constexpr size_t kPointerCompressionShiftMask = + (1 << api_constants::kPointerCompressionShift) - 1; + + CPPGC_DCHECK(CageBaseGlobal::IsSet()); + const uintptr_t base = CageBaseGlobal::Get(); + CPPGC_DCHECK(!ptr || ptr == kSentinelPointer || + (base & kGigaCageMask) == + (reinterpret_cast(ptr) & kGigaCageMask)); + CPPGC_DCHECK( + (reinterpret_cast(ptr) & kPointerCompressionShiftMask) == 0); + +#if defined(CPPGC_2GB_CAGE) + // Truncate the pointer. + auto compressed = + static_cast(reinterpret_cast(ptr)); +#else // !defined(CPPGC_2GB_CAGE) + const auto uptr = reinterpret_cast(ptr); + // Shift the pointer and truncate. + auto compressed = static_cast( + uptr >> api_constants::kPointerCompressionShift); +#endif // !defined(CPPGC_2GB_CAGE) + // Normal compressed pointers must have the MSB set. + CPPGC_DCHECK((!compressed || compressed == kCompressedSentinel) || + (compressed & (1 << 31))); + return compressed; + } + + static V8_INLINE void* Decompress(IntegralType ptr) { + CPPGC_DCHECK(CageBaseGlobal::IsSet()); + const uintptr_t base = CageBaseGlobal::Get(); + // Treat compressed pointer as signed and cast it to uint64_t, which will + // sign-extend it. +#if defined(CPPGC_2GB_CAGE) + const uint64_t mask = static_cast(static_cast(ptr)); +#else // !defined(CPPGC_2GB_CAGE) + // Then, shift the result. It's important to shift the unsigned + // value, as otherwise it would result in undefined behavior. + const uint64_t mask = static_cast(static_cast(ptr)) + << api_constants::kPointerCompressionShift; +#endif // !defined(CPPGC_2GB_CAGE) + return reinterpret_cast(mask & base); + } + + private: +#if defined(CPPGC_2GB_CAGE) + static constexpr IntegralType kCompressedSentinel = + SentinelPointer::kSentinelValue; +#else // !defined(CPPGC_2GB_CAGE) + static constexpr IntegralType kCompressedSentinel = + SentinelPointer::kSentinelValue >> + api_constants::kPointerCompressionShift; +#endif // !defined(CPPGC_2GB_CAGE) + // All constructors initialize `value_`. Do not add a default value here as it + // results in a non-atomic write on some builds, even when the atomic version + // of the constructor is used. + IntegralType value_; +}; + +#endif // defined(CPPGC_POINTER_COMPRESSION) + +class V8_TRIVIAL_ABI RawPointer final { + public: + using IntegralType = uintptr_t; + static constexpr auto kWriteBarrierSlotType = + WriteBarrierSlotType::kUncompressed; + + V8_INLINE RawPointer() : ptr_(nullptr) {} + V8_INLINE explicit RawPointer(const void* ptr) : ptr_(ptr) {} + + V8_INLINE const void* Load() const { return ptr_; } + V8_INLINE const void* LoadAtomic() const { + return reinterpret_cast&>(ptr_).load( + std::memory_order_relaxed); + } + + V8_INLINE void Store(const void* ptr) { ptr_ = ptr; } + V8_INLINE void StoreAtomic(const void* ptr) { + reinterpret_cast&>(ptr_).store( + ptr, std::memory_order_relaxed); + } + + V8_INLINE void Clear() { ptr_ = nullptr; } + V8_INLINE bool IsCleared() const { return !ptr_; } + + V8_INLINE bool IsSentinel() const { return ptr_ == kSentinelPointer; } + + V8_INLINE uintptr_t GetAsInteger() const { + return reinterpret_cast(ptr_); + } + + V8_INLINE friend bool operator==(RawPointer a, RawPointer b) { + return a.ptr_ == b.ptr_; + } + V8_INLINE friend bool operator!=(RawPointer a, RawPointer b) { + return a.ptr_ != b.ptr_; + } + V8_INLINE friend bool operator<(RawPointer a, RawPointer b) { + return a.ptr_ < b.ptr_; + } + V8_INLINE friend bool operator<=(RawPointer a, RawPointer b) { + return a.ptr_ <= b.ptr_; + } + V8_INLINE friend bool operator>(RawPointer a, RawPointer b) { + return a.ptr_ > b.ptr_; + } + V8_INLINE friend bool operator>=(RawPointer a, RawPointer b) { + return a.ptr_ >= b.ptr_; + } + + private: + // All constructors initialize `ptr_`. Do not add a default value here as it + // results in a non-atomic write on some builds, even when the atomic version + // of the constructor is used. + const void* ptr_; +}; + +#if defined(CPPGC_POINTER_COMPRESSION) +using DefaultMemberStorage = CompressedPointer; +#else // !defined(CPPGC_POINTER_COMPRESSION) +using DefaultMemberStorage = RawPointer; +#endif // !defined(CPPGC_POINTER_COMPRESSION) + +} // namespace internal +} // namespace cppgc + +#endif // INCLUDE_CPPGC_INTERNAL_MEMBER_STORAGE_H_ diff --git a/android/arm64-v8a/include/v8/cppgc/internal/name-trait.h b/android/arm64-v8a/include/v8/cppgc/internal/name-trait.h index ae99d41c..1d927a9d 100644 --- a/android/arm64-v8a/include/v8/cppgc/internal/name-trait.h +++ b/android/arm64-v8a/include/v8/cppgc/internal/name-trait.h @@ -6,6 +6,8 @@ #define INCLUDE_CPPGC_INTERNAL_NAME_TRAIT_H_ #include +#include +#include #include "cppgc/name-provider.h" #include "v8config.h" // NOLINT(build/include_directory) @@ -57,6 +59,11 @@ struct HeapObjectName { bool name_was_hidden; }; +enum class HeapObjectNameForUnnamedObject : uint8_t { + kUseClassNameIfSupported, + kUseHiddenName, +}; + class V8_EXPORT NameTraitBase { protected: static HeapObjectName GetNameFromTypeSignature(const char*); @@ -67,16 +74,34 @@ class V8_EXPORT NameTraitBase { template class NameTrait final : public NameTraitBase { public: - static HeapObjectName GetName(const void* obj) { - return GetNameFor(static_cast(obj)); + static constexpr bool HasNonHiddenName() { +#if CPPGC_SUPPORTS_COMPILE_TIME_TYPENAME + return true; +#elif CPPGC_SUPPORTS_OBJECT_NAMES + return true; +#else // !CPPGC_SUPPORTS_OBJECT_NAMES + return std::is_base_of::value; +#endif // !CPPGC_SUPPORTS_OBJECT_NAMES + } + + static HeapObjectName GetName( + const void* obj, HeapObjectNameForUnnamedObject name_retrieval_mode) { + return GetNameFor(static_cast(obj), name_retrieval_mode); } private: - static HeapObjectName GetNameFor(const NameProvider* name_provider) { - return {name_provider->GetName(), false}; + static HeapObjectName GetNameFor(const NameProvider* name_provider, + HeapObjectNameForUnnamedObject) { + // Objects inheriting from `NameProvider` are not considered unnamed as + // users already provided a name for them. + return {name_provider->GetHumanReadableName(), false}; } - static HeapObjectName GetNameFor(...) { + static HeapObjectName GetNameFor( + const void*, HeapObjectNameForUnnamedObject name_retrieval_mode) { + if (name_retrieval_mode == HeapObjectNameForUnnamedObject::kUseHiddenName) + return {NameProvider::kHiddenName, true}; + #if CPPGC_SUPPORTS_COMPILE_TIME_TYPENAME return {GetTypename(), false}; #elif CPPGC_SUPPORTS_OBJECT_NAMES @@ -101,7 +126,8 @@ class NameTrait final : public NameTraitBase { } }; -using NameCallback = HeapObjectName (*)(const void*); +using NameCallback = HeapObjectName (*)(const void*, + HeapObjectNameForUnnamedObject); } // namespace internal } // namespace cppgc diff --git a/android/arm64-v8a/include/v8/cppgc/internal/persistent-node.h b/android/arm64-v8a/include/v8/cppgc/internal/persistent-node.h index 5626b178..d22692a7 100644 --- a/android/arm64-v8a/include/v8/cppgc/internal/persistent-node.h +++ b/android/arm64-v8a/include/v8/cppgc/internal/persistent-node.h @@ -14,12 +14,11 @@ #include "v8config.h" // NOLINT(build/include_directory) namespace cppgc { - -class Visitor; - namespace internal { class CrossThreadPersistentRegion; +class FatalOutOfMemoryHandler; +class RootVisitor; // PersistentNode represents a variant of two states: // 1) traceable node with a back pointer to the Persistent object; @@ -31,7 +30,7 @@ class PersistentNode final { PersistentNode(const PersistentNode&) = delete; PersistentNode& operator=(const PersistentNode&) = delete; - void InitializeAsUsedNode(void* owner, TraceCallback trace) { + void InitializeAsUsedNode(void* owner, TraceRootCallback trace) { CPPGC_DCHECK(trace); owner_ = owner; trace_ = trace; @@ -52,9 +51,9 @@ class PersistentNode final { return next_; } - void Trace(Visitor* visitor) const { + void Trace(RootVisitor& root_visitor) const { CPPGC_DCHECK(IsUsed()); - trace_(visitor, owner_); + trace_(root_visitor, owner_); } bool IsUsed() const { return trace_; } @@ -72,29 +71,38 @@ class PersistentNode final { void* owner_ = nullptr; PersistentNode* next_; }; - TraceCallback trace_ = nullptr; + TraceRootCallback trace_ = nullptr; }; -class V8_EXPORT PersistentRegion final { +class V8_EXPORT PersistentRegionBase { using PersistentNodeSlots = std::array; public: - PersistentRegion() = default; // Clears Persistent fields to avoid stale pointers after heap teardown. - ~PersistentRegion(); + ~PersistentRegionBase(); - PersistentRegion(const PersistentRegion&) = delete; - PersistentRegion& operator=(const PersistentRegion&) = delete; + PersistentRegionBase(const PersistentRegionBase&) = delete; + PersistentRegionBase& operator=(const PersistentRegionBase&) = delete; - PersistentNode* AllocateNode(void* owner, TraceCallback trace) { - if (!free_list_head_) { - EnsureNodeSlots(); + void Iterate(RootVisitor&); + + size_t NodesInUse() const; + + void ClearAllUsedNodes(); + + protected: + explicit PersistentRegionBase(const FatalOutOfMemoryHandler& oom_handler); + + PersistentNode* TryAllocateNodeFromFreeList(void* owner, + TraceRootCallback trace) { + PersistentNode* node = nullptr; + if (V8_LIKELY(free_list_head_)) { + node = free_list_head_; + free_list_head_ = free_list_head_->FreeListNext(); + CPPGC_DCHECK(!node->IsUsed()); + node->InitializeAsUsedNode(owner, trace); + nodes_in_use_++; } - 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; } @@ -107,24 +115,57 @@ class V8_EXPORT PersistentRegion final { nodes_in_use_--; } - void Trace(Visitor*); - - size_t NodesInUse() const; - - void ClearAllUsedNodes(); + PersistentNode* RefillFreeListAndAllocateNode(void* owner, + TraceRootCallback trace); private: - void EnsureNodeSlots(); + template + void ClearAllUsedNodes(); + + void RefillFreeList(); std::vector> nodes_; PersistentNode* free_list_head_ = nullptr; size_t nodes_in_use_ = 0; + const FatalOutOfMemoryHandler& oom_handler_; friend class CrossThreadPersistentRegion; }; -// CrossThreadPersistent uses PersistentRegion but protects it using this lock -// when needed. +// Variant of PersistentRegionBase that checks whether the allocation and +// freeing happens only on the thread that created the region. +class V8_EXPORT PersistentRegion final : public PersistentRegionBase { + public: + explicit PersistentRegion(const FatalOutOfMemoryHandler&); + // Clears Persistent fields to avoid stale pointers after heap teardown. + ~PersistentRegion() = default; + + PersistentRegion(const PersistentRegion&) = delete; + PersistentRegion& operator=(const PersistentRegion&) = delete; + + V8_INLINE PersistentNode* AllocateNode(void* owner, TraceRootCallback trace) { + CPPGC_DCHECK(IsCreationThread()); + auto* node = TryAllocateNodeFromFreeList(owner, trace); + if (V8_LIKELY(node)) return node; + + // Slow path allocation allows for checking thread correspondence. + CPPGC_CHECK(IsCreationThread()); + return RefillFreeListAndAllocateNode(owner, trace); + } + + V8_INLINE void FreeNode(PersistentNode* node) { + CPPGC_DCHECK(IsCreationThread()); + PersistentRegionBase::FreeNode(node); + } + + private: + bool IsCreationThread(); + + int creation_thread_id_; +}; + +// CrossThreadPersistent uses PersistentRegionBase but protects it using this +// lock when needed. class V8_EXPORT PersistentRegionLock final { public: PersistentRegionLock(); @@ -133,11 +174,12 @@ class V8_EXPORT PersistentRegionLock final { static void AssertLocked(); }; -// Variant of PersistentRegion that checks whether the PersistentRegionLock is -// locked. -class V8_EXPORT CrossThreadPersistentRegion final { +// Variant of PersistentRegionBase that checks whether the PersistentRegionLock +// is locked. +class V8_EXPORT CrossThreadPersistentRegion final + : protected PersistentRegionBase { public: - CrossThreadPersistentRegion() = default; + explicit CrossThreadPersistentRegion(const FatalOutOfMemoryHandler&); // Clears Persistent fields to avoid stale pointers after heap teardown. ~CrossThreadPersistentRegion(); @@ -145,24 +187,24 @@ class V8_EXPORT CrossThreadPersistentRegion final { CrossThreadPersistentRegion& operator=(const CrossThreadPersistentRegion&) = delete; - V8_INLINE PersistentNode* AllocateNode(void* owner, TraceCallback trace) { + V8_INLINE PersistentNode* AllocateNode(void* owner, TraceRootCallback trace) { PersistentRegionLock::AssertLocked(); - return persistent_region_.AllocateNode(owner, trace); + auto* node = TryAllocateNodeFromFreeList(owner, trace); + if (V8_LIKELY(node)) return node; + + return RefillFreeListAndAllocateNode(owner, trace); } V8_INLINE void FreeNode(PersistentNode* node) { PersistentRegionLock::AssertLocked(); - persistent_region_.FreeNode(node); + PersistentRegionBase::FreeNode(node); } - void Trace(Visitor*); + void Iterate(RootVisitor&); size_t NodesInUse() const; void ClearAllUsedNodes(); - - private: - PersistentRegion persistent_region_; }; } // namespace internal diff --git a/android/arm64-v8a/include/v8/cppgc/internal/pointer-policies.h b/android/arm64-v8a/include/v8/cppgc/internal/pointer-policies.h index ceb002f0..06fa884f 100644 --- a/android/arm64-v8a/include/v8/cppgc/internal/pointer-policies.h +++ b/android/arm64-v8a/include/v8/cppgc/internal/pointer-policies.h @@ -8,13 +8,17 @@ #include #include +#include "cppgc/internal/member-storage.h" #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; @@ -24,15 +28,67 @@ class WeakMemberTag; class UntracedMemberTag; struct DijkstraWriteBarrierPolicy { - static void InitializingBarrier(const void*, const void*) { + V8_INLINE static void InitializingBarrier(const void*, const void*) { // Since in initializing writes the source object is always white, having no // barrier doesn't break the tri-color invariant. } - static void AssigningBarrier(const void* slot, const void* value) { + + template + V8_INLINE static void AssigningBarrier(const void* slot, const void* value) { +#ifdef CPPGC_SLIM_WRITE_BARRIER + if (V8_UNLIKELY(WriteBarrier::IsEnabled())) + WriteBarrier::CombinedWriteBarrierSlow(slot); +#else // !CPPGC_SLIM_WRITE_BARRIER WriteBarrier::Params params; - switch (WriteBarrier::GetWriteBarrierType(slot, value, params)) { + const WriteBarrier::Type type = + WriteBarrier::GetWriteBarrierType(slot, value, params); + WriteBarrier(type, params, slot, value); +#endif // !CPPGC_SLIM_WRITE_BARRIER + } + + template + V8_INLINE static void AssigningBarrier(const void* slot, RawPointer storage) { + static_assert( + SlotType == WriteBarrierSlotType::kUncompressed, + "Assigning storages of Member and UncompressedMember is not supported"); +#ifdef CPPGC_SLIM_WRITE_BARRIER + if (V8_UNLIKELY(WriteBarrier::IsEnabled())) + WriteBarrier::CombinedWriteBarrierSlow(slot); +#else // !CPPGC_SLIM_WRITE_BARRIER + WriteBarrier::Params params; + const WriteBarrier::Type type = + WriteBarrier::GetWriteBarrierType(slot, storage, params); + WriteBarrier(type, params, slot, storage.Load()); +#endif // !CPPGC_SLIM_WRITE_BARRIER + } + +#if defined(CPPGC_POINTER_COMPRESSION) + template + V8_INLINE static void AssigningBarrier(const void* slot, + CompressedPointer storage) { + static_assert( + SlotType == WriteBarrierSlotType::kCompressed, + "Assigning storages of Member and UncompressedMember is not supported"); +#ifdef CPPGC_SLIM_WRITE_BARRIER + if (V8_UNLIKELY(WriteBarrier::IsEnabled())) + WriteBarrier::CombinedWriteBarrierSlow(slot); +#else // !CPPGC_SLIM_WRITE_BARRIER + WriteBarrier::Params params; + const WriteBarrier::Type type = + WriteBarrier::GetWriteBarrierType(slot, storage, params); + WriteBarrier(type, params, slot, storage.Load()); +#endif // !CPPGC_SLIM_WRITE_BARRIER + } +#endif // defined(CPPGC_POINTER_COMPRESSION) + + private: + V8_INLINE static void WriteBarrier(WriteBarrier::Type type, + const WriteBarrier::Params& params, + const void* slot, const void* value) { + switch (type) { case WriteBarrier::Type::kGenerational: - WriteBarrier::GenerationalBarrier(params, slot); + WriteBarrier::GenerationalBarrier< + WriteBarrier::GenerationalBarrierType::kPreciseSlot>(params, slot); break; case WriteBarrier::Type::kMarking: WriteBarrier::DijkstraMarkingBarrier(params, value); @@ -44,29 +100,69 @@ struct DijkstraWriteBarrierPolicy { }; struct NoWriteBarrierPolicy { - static void InitializingBarrier(const void*, const void*) {} - static void AssigningBarrier(const void*, const void*) {} + V8_INLINE static void InitializingBarrier(const void*, const void*) {} + template + V8_INLINE static void AssigningBarrier(const void*, const void*) {} + template + V8_INLINE static void AssigningBarrier(const void*, MemberStorage) {} }; -class V8_EXPORT EnabledCheckingPolicy { +class V8_EXPORT SameThreadEnabledCheckingPolicyBase { protected: - EnabledCheckingPolicy(); - void CheckPointer(const void* ptr); + void CheckPointerImpl(const void* ptr, bool points_to_payload, + bool check_off_heap_assignments); + + const HeapBase* heap_ = nullptr; +}; + +template +class V8_EXPORT SameThreadEnabledCheckingPolicy + : private SameThreadEnabledCheckingPolicyBase { + protected: + template + void CheckPointer(const T* ptr) { + if (!ptr || (kSentinelPointer == ptr)) return; + + CheckPointersImplTrampoline::Call(this, ptr); + } private: - void* impl_; + template > + struct CheckPointersImplTrampoline { + static void Call(SameThreadEnabledCheckingPolicy* policy, const T* ptr) { + policy->CheckPointerImpl(ptr, false, kCheckOffHeapAssignments); + } + }; + + template + struct CheckPointersImplTrampoline { + static void Call(SameThreadEnabledCheckingPolicy* policy, const T* ptr) { + policy->CheckPointerImpl(ptr, IsGarbageCollectedTypeV, + kCheckOffHeapAssignments); + } + }; }; class DisabledCheckingPolicy { protected: - void CheckPointer(const void* raw) {} + V8_INLINE void CheckPointer(const void*) {} }; -#if V8_ENABLE_CHECKS -using DefaultCheckingPolicy = EnabledCheckingPolicy; -#else -using DefaultCheckingPolicy = DisabledCheckingPolicy; -#endif +#ifdef DEBUG +// Off heap members are not connected to object graph and thus cannot ressurect +// dead objects. +using DefaultMemberCheckingPolicy = + SameThreadEnabledCheckingPolicy; +using DefaultPersistentCheckingPolicy = + SameThreadEnabledCheckingPolicy; +#else // !DEBUG +using DefaultMemberCheckingPolicy = DisabledCheckingPolicy; +using DefaultPersistentCheckingPolicy = DisabledCheckingPolicy; +#endif // !DEBUG +// For CT(W)P neither marking information (for value), nor objectstart bitmap +// (for slot) are guaranteed to be present because there's no synchronization +// between heaps after marking. +using DefaultCrossThreadPersistentCheckingPolicy = DisabledCheckingPolicy; class KeepLocationPolicy { public: @@ -129,14 +225,15 @@ struct WeakCrossThreadPersistentPolicy { // Forward declarations setting up the default policies. template + typename CheckingPolicy = DefaultCrossThreadPersistentCheckingPolicy> class BasicCrossThreadPersistent; template + typename CheckingPolicy = DefaultPersistentCheckingPolicy> class BasicPersistent; template + typename CheckingPolicy = DefaultMemberCheckingPolicy, + typename StorageType = DefaultMemberStorage> class BasicMember; } // namespace internal diff --git a/android/arm64-v8a/include/v8/cppgc/internal/prefinalizer-handler.h b/android/arm64-v8a/include/v8/cppgc/internal/prefinalizer-handler.h deleted file mode 100644 index 64b07ec9..00000000 --- a/android/arm64-v8a/include/v8/cppgc/internal/prefinalizer-handler.h +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2020 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_INTERNAL_PREFINALIZER_HANDLER_H_ -#define INCLUDE_CPPGC_INTERNAL_PREFINALIZER_HANDLER_H_ - -#include "cppgc/heap.h" -#include "cppgc/liveness-broker.h" - -namespace cppgc { -namespace internal { - -class V8_EXPORT PreFinalizerRegistrationDispatcher final { - public: - using PreFinalizerCallback = bool (*)(const LivenessBroker&, void*); - struct PreFinalizer { - void* object; - PreFinalizerCallback callback; - - bool operator==(const PreFinalizer& other) const; - }; - - static void RegisterPrefinalizer(PreFinalizer pre_finalizer); -}; - -} // namespace internal -} // namespace cppgc - -#endif // INCLUDE_CPPGC_INTERNAL_PREFINALIZER_HANDLER_H_ diff --git a/android/arm64-v8a/include/v8/cppgc/internal/write-barrier.h b/android/arm64-v8a/include/v8/cppgc/internal/write-barrier.h index f3aaedb1..566724d3 100644 --- a/android/arm64-v8a/include/v8/cppgc/internal/write-barrier.h +++ b/android/arm64-v8a/include/v8/cppgc/internal/write-barrier.h @@ -5,15 +5,23 @@ #ifndef INCLUDE_CPPGC_INTERNAL_WRITE_BARRIER_H_ #define INCLUDE_CPPGC_INTERNAL_WRITE_BARRIER_H_ +#include +#include + +#include "cppgc/heap-handle.h" #include "cppgc/heap-state.h" #include "cppgc/internal/api-constants.h" #include "cppgc/internal/atomic-entry-flag.h" +#include "cppgc/internal/base-page-handle.h" +#include "cppgc/internal/member-storage.h" +#include "cppgc/platform.h" #include "cppgc/sentinel-pointer.h" #include "cppgc/trace-trait.h" #include "v8config.h" // NOLINT(build/include_directory) #if defined(CPPGC_CAGED_HEAP) #include "cppgc/internal/caged-heap-local-data.h" +#include "cppgc/internal/caged-heap.h" #endif namespace cppgc { @@ -22,8 +30,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: @@ -33,16 +44,18 @@ class V8_EXPORT WriteBarrier final { kGenerational, }; + enum class GenerationalBarrierType : uint8_t { + kPreciseSlot, + kPreciseUncompressedSlot, + kImpreciseSlot, + }; + struct Params { HeapHandle* heap = nullptr; #if V8_ENABLE_CHECKS Type type = Type::kNone; #endif // !V8_ENABLE_CHECKS #if defined(CPPGC_CAGED_HEAP) - uintptr_t start = 0; - CagedHeapLocalData& caged_heap() const { - return *reinterpret_cast(start); - } uintptr_t slot_offset = 0; uintptr_t value_offset = 0; #endif // CPPGC_CAGED_HEAP @@ -56,14 +69,25 @@ class V8_EXPORT WriteBarrier final { // Returns the required write barrier for a given `slot` and `value`. static V8_INLINE Type GetWriteBarrierType(const void* slot, const void* value, Params& params); + // Returns the required write barrier for a given `slot` and `value`. + template + static V8_INLINE Type GetWriteBarrierType(const void* slot, MemberStorage, + Params& params); // Returns the required write barrier for a given `slot`. template static V8_INLINE Type GetWriteBarrierType(const void* slot, Params& params, HeapHandleCallback callback); + // Returns the required write barrier for a given `value`. + static V8_INLINE Type GetWriteBarrierType(const void* value, Params& params); - template - static V8_INLINE Type GetWriteBarrierTypeForExternallyReferencedObject( - const void* value, Params& params, HeapHandleCallback callback); +#ifdef CPPGC_SLIM_WRITE_BARRIER + // A write barrier that combines `GenerationalBarrier()` and + // `DijkstraMarkingBarrier()`. We only pass a single parameter here to clobber + // as few registers as possible. + template + static V8_NOINLINE void V8_PRESERVE_MOST + CombinedWriteBarrierSlow(const void* slot); +#endif // CPPGC_SLIM_WRITE_BARRIER static V8_INLINE void DijkstraMarkingBarrier(const Params& params, const void* object); @@ -73,11 +97,13 @@ class V8_EXPORT WriteBarrier final { static V8_INLINE void SteeleMarkingBarrier(const Params& params, const void* object); #if defined(CPPGC_YOUNG_GENERATION) + template static V8_INLINE void GenerationalBarrier(const Params& params, const void* slot); -#else // !CPPGC_YOUNG_GENERATION +#else // !CPPGC_YOUNG_GENERATION + template static V8_INLINE void GenerationalBarrier(const Params& params, - const void* slot) {} + const void* slot){} #endif // CPPGC_YOUNG_GENERATION #if V8_ENABLE_CHECKS @@ -86,12 +112,10 @@ class V8_EXPORT WriteBarrier final { static void CheckParams(Type expected_type, const Params& params) {} #endif // !V8_ENABLE_CHECKS - // The IncrementalOrConcurrentUpdater class allows cppgc internal to update - // |incremental_or_concurrent_marking_flag_|. - class IncrementalOrConcurrentMarkingFlagUpdater; - static bool IsAnyIncrementalOrConcurrentMarking() { - return incremental_or_concurrent_marking_flag_.MightBeEntered(); - } + // The FlagUpdater class allows cppgc internal to update + // |write_barrier_enabled_|. + class FlagUpdater; + static bool IsEnabled() { return write_barrier_enabled_.MightBeEntered(); } private: WriteBarrier() = delete; @@ -115,16 +139,24 @@ class V8_EXPORT WriteBarrier final { #if defined(CPPGC_YOUNG_GENERATION) static CagedHeapLocalData& GetLocalData(HeapHandle&); static void GenerationalBarrierSlow(const CagedHeapLocalData& local_data, - const AgeTable& ageTable, - const void* slot, uintptr_t value_offset); + const AgeTable& age_table, + const void* slot, uintptr_t value_offset, + HeapHandle* heap_handle); + static void GenerationalBarrierForUncompressedSlotSlow( + const CagedHeapLocalData& local_data, const AgeTable& age_table, + const void* slot, uintptr_t value_offset, HeapHandle* heap_handle); + static void GenerationalBarrierForSourceObjectSlow( + const CagedHeapLocalData& local_data, const void* object, + HeapHandle* heap_handle); #endif // CPPGC_YOUNG_GENERATION - static AtomicEntryFlag incremental_or_concurrent_marking_flag_; + static AtomicEntryFlag write_barrier_enabled_; }; template V8_INLINE WriteBarrier::Type SetAndReturnType(WriteBarrier::Params& params) { - if (type == WriteBarrier::Type::kNone) return WriteBarrier::Type::kNone; + if constexpr (type == WriteBarrier::Type::kNone) + return WriteBarrier::Type::kNone; #if V8_ENABLE_CHECKS params.type = type; #endif // !V8_ENABLE_CHECKS @@ -141,67 +173,99 @@ class V8_EXPORT WriteBarrierTypeForCagedHeapPolicy final { return ValueModeDispatch::Get(slot, value, params, callback); } - template - static V8_INLINE WriteBarrier::Type GetForExternallyReferenced( - const void* value, WriteBarrier::Params& params, HeapHandleCallback) { - if (!TryGetCagedHeap(value, value, params)) { - return WriteBarrier::Type::kNone; - } - if (V8_UNLIKELY(params.caged_heap().is_incremental_marking_in_progress)) { - return SetAndReturnType(params); - } - return SetAndReturnType(params); + template + static V8_INLINE WriteBarrier::Type Get(const void* slot, MemberStorage value, + WriteBarrier::Params& params, + HeapHandleCallback callback) { + return ValueModeDispatch::Get(slot, value, params, callback); + } + + template + static V8_INLINE WriteBarrier::Type Get(const void* value, + WriteBarrier::Params& params, + HeapHandleCallback callback) { + return GetNoSlot(value, params, callback); } private: WriteBarrierTypeForCagedHeapPolicy() = delete; - template - struct ValueModeDispatch; + template + static V8_INLINE WriteBarrier::Type GetNoSlot(const void* value, + WriteBarrier::Params& params, + HeapHandleCallback) { + const bool within_cage = CagedHeapBase::IsWithinCage(value); + if (!within_cage) return WriteBarrier::Type::kNone; - static V8_INLINE bool TryGetCagedHeap(const void* slot, const void* value, - WriteBarrier::Params& params) { - params.start = reinterpret_cast(value) & - ~(api_constants::kCagedHeapReservationAlignment - 1); - const uintptr_t slot_offset = - reinterpret_cast(slot) - params.start; - if (slot_offset > api_constants::kCagedHeapReservationSize) { - // Check if slot is on stack or value is sentinel or nullptr. This relies - // on the fact that kSentinelPointer is encoded as 0x1. - return false; + // We know that |value| points either within the normal page or to the + // beginning of large-page, so extract the page header by bitmasking. + BasePageHandle* page = + BasePageHandle::FromPayload(const_cast(value)); + + HeapHandle& heap_handle = page->heap_handle(); + if (V8_UNLIKELY(heap_handle.is_incremental_marking_in_progress())) { + return SetAndReturnType(params); } - return true; + + return SetAndReturnType(params); } - // Returns whether marking is in progress. If marking is not in progress - // sets the start of the cage accordingly. - // - // TODO(chromium:1056170): Create fast path on API. - static bool IsMarking(const HeapHandle&, WriteBarrier::Params&); + template + struct ValueModeDispatch; }; template <> struct WriteBarrierTypeForCagedHeapPolicy::ValueModeDispatch< WriteBarrier::ValueMode::kValuePresent> { + template + static V8_INLINE WriteBarrier::Type Get(const void* slot, + MemberStorage storage, + WriteBarrier::Params& params, + HeapHandleCallback) { + if (V8_LIKELY(!WriteBarrier::IsEnabled())) + return SetAndReturnType(params); + + return BarrierEnabledGet(slot, storage.Load(), params); + } + template static V8_INLINE WriteBarrier::Type Get(const void* slot, const void* value, WriteBarrier::Params& params, HeapHandleCallback) { - bool within_cage = TryGetCagedHeap(slot, value, params); - if (!within_cage) { - return WriteBarrier::Type::kNone; - } - if (V8_LIKELY(!params.caged_heap().is_incremental_marking_in_progress)) { + if (V8_LIKELY(!WriteBarrier::IsEnabled())) + return SetAndReturnType(params); + + return BarrierEnabledGet(slot, value, params); + } + + private: + static V8_INLINE WriteBarrier::Type BarrierEnabledGet( + const void* slot, const void* value, WriteBarrier::Params& params) { + const bool within_cage = CagedHeapBase::AreWithinCage(slot, value); + if (!within_cage) return WriteBarrier::Type::kNone; + + // We know that |value| points either within the normal page or to the + // beginning of large-page, so extract the page header by bitmasking. + BasePageHandle* page = + BasePageHandle::FromPayload(const_cast(value)); + + HeapHandle& heap_handle = page->heap_handle(); + if (V8_LIKELY(!heap_handle.is_incremental_marking_in_progress())) { #if defined(CPPGC_YOUNG_GENERATION) - params.heap = reinterpret_cast(params.start); - params.slot_offset = reinterpret_cast(slot) - params.start; - params.value_offset = reinterpret_cast(value) - params.start; + if (!heap_handle.is_young_generation_enabled()) + return WriteBarrier::Type::kNone; + params.heap = &heap_handle; + params.slot_offset = CagedHeapBase::OffsetFromAddress(slot); + params.value_offset = CagedHeapBase::OffsetFromAddress(value); return SetAndReturnType(params); #else // !CPPGC_YOUNG_GENERATION return SetAndReturnType(params); #endif // !CPPGC_YOUNG_GENERATION } - params.heap = reinterpret_cast(params.start); + + // Use marking barrier. + params.heap = &heap_handle; return SetAndReturnType(params); } }; @@ -213,28 +277,28 @@ struct WriteBarrierTypeForCagedHeapPolicy::ValueModeDispatch< static V8_INLINE WriteBarrier::Type Get(const void* slot, const void*, WriteBarrier::Params& params, HeapHandleCallback callback) { -#if defined(CPPGC_YOUNG_GENERATION) + if (V8_LIKELY(!WriteBarrier::IsEnabled())) + return SetAndReturnType(params); + HeapHandle& handle = callback(); - if (V8_LIKELY(!IsMarking(handle, params))) { - // params.start is populated by IsMarking(). +#if defined(CPPGC_YOUNG_GENERATION) + if (V8_LIKELY(!handle.is_incremental_marking_in_progress())) { + if (!handle.is_young_generation_enabled()) { + return WriteBarrier::Type::kNone; + } params.heap = &handle; - params.slot_offset = reinterpret_cast(slot) - params.start; - // params.value_offset stays 0. - if (params.slot_offset > api_constants::kCagedHeapReservationSize) { - // Check if slot is on stack. + // Check if slot is on stack. + if (V8_UNLIKELY(!CagedHeapBase::IsWithinCage(slot))) { return SetAndReturnType(params); } + params.slot_offset = CagedHeapBase::OffsetFromAddress(slot); return SetAndReturnType(params); } -#else // !CPPGC_YOUNG_GENERATION - if (V8_LIKELY(!WriteBarrier::IsAnyIncrementalOrConcurrentMarking())) { +#else // !defined(CPPGC_YOUNG_GENERATION) + if (V8_UNLIKELY(!handle.is_incremental_marking_in_progress())) { return SetAndReturnType(params); } - HeapHandle& handle = callback(); - if (V8_UNLIKELY(!subtle::HeapState::IsMarking(handle))) { - return SetAndReturnType(params); - } -#endif // !CPPGC_YOUNG_GENERATION +#endif // !defined(CPPGC_YOUNG_GENERATION) params.heap = &handle; return SetAndReturnType(params); } @@ -251,10 +315,18 @@ class V8_EXPORT WriteBarrierTypeForNonCagedHeapPolicy final { return ValueModeDispatch::Get(slot, value, params, callback); } - template - static V8_INLINE WriteBarrier::Type GetForExternallyReferenced( - const void* value, WriteBarrier::Params& params, - HeapHandleCallback callback) { + template + static V8_INLINE WriteBarrier::Type Get(const void* slot, RawPointer value, + WriteBarrier::Params& params, + HeapHandleCallback callback) { + return ValueModeDispatch::Get(slot, value.Load(), params, + callback); + } + + template + static V8_INLINE WriteBarrier::Type Get(const void* value, + WriteBarrier::Params& params, + HeapHandleCallback callback) { // The slot will never be used in `Get()` below. return Get(nullptr, value, params, callback); @@ -264,11 +336,6 @@ class V8_EXPORT WriteBarrierTypeForNonCagedHeapPolicy final { template struct ValueModeDispatch; - // TODO(chromium:1056170): Create fast path on API. - static bool IsMarking(const void*, HeapHandle**); - // TODO(chromium:1056170): Create fast path on API. - static bool IsMarking(HeapHandle&); - WriteBarrierTypeForNonCagedHeapPolicy() = delete; }; @@ -281,9 +348,18 @@ struct WriteBarrierTypeForNonCagedHeapPolicy::ValueModeDispatch< HeapHandleCallback callback) { // The following check covers nullptr as well as sentinel pointer. if (object <= static_cast(kSentinelPointer)) { - return WriteBarrier::Type::kNone; + return SetAndReturnType(params); } - if (IsMarking(object, ¶ms.heap)) { + if (V8_LIKELY(!WriteBarrier::IsEnabled())) { + return SetAndReturnType(params); + } + // We know that |object| is within the normal page or in the beginning of a + // large page, so extract the page header by bitmasking. + BasePageHandle* page = + BasePageHandle::FromPayload(const_cast(object)); + + HeapHandle& heap_handle = page->heap_handle(); + if (V8_LIKELY(heap_handle.is_incremental_marking_in_progress())) { return SetAndReturnType(params); } return SetAndReturnType(params); @@ -297,9 +373,9 @@ struct WriteBarrierTypeForNonCagedHeapPolicy::ValueModeDispatch< static V8_INLINE WriteBarrier::Type Get(const void*, const void*, WriteBarrier::Params& params, HeapHandleCallback callback) { - if (V8_UNLIKELY(WriteBarrier::IsAnyIncrementalOrConcurrentMarking())) { + if (V8_UNLIKELY(WriteBarrier::IsEnabled())) { HeapHandle& handle = callback(); - if (IsMarking(handle)) { + if (V8_LIKELY(handle.is_incremental_marking_in_progress())) { params.heap = &handle; return SetAndReturnType(params); } @@ -315,6 +391,14 @@ WriteBarrier::Type WriteBarrier::GetWriteBarrierType( params, []() {}); } +// static +template +WriteBarrier::Type WriteBarrier::GetWriteBarrierType( + const void* slot, MemberStorage value, WriteBarrier::Params& params) { + return WriteBarrierTypePolicy::Get(slot, value, + params, []() {}); +} + // static template WriteBarrier::Type WriteBarrier::GetWriteBarrierType( @@ -325,12 +409,10 @@ WriteBarrier::Type WriteBarrier::GetWriteBarrierType( } // static -template -WriteBarrier::Type -WriteBarrier::GetWriteBarrierTypeForExternallyReferencedObject( - const void* value, Params& params, HeapHandleCallback callback) { - return WriteBarrierTypePolicy::GetForExternallyReferenced(value, params, - callback); +WriteBarrier::Type WriteBarrier::GetWriteBarrierType( + const void* value, WriteBarrier::Params& params) { + return WriteBarrierTypePolicy::Get(value, params, + []() {}); } // static @@ -369,17 +451,32 @@ void WriteBarrier::SteeleMarkingBarrier(const Params& params, } #if defined(CPPGC_YOUNG_GENERATION) + // static +template void WriteBarrier::GenerationalBarrier(const Params& params, const void* slot) { CheckParams(Type::kGenerational, params); - const CagedHeapLocalData& local_data = params.caged_heap(); + const CagedHeapLocalData& local_data = CagedHeapLocalData::Get(); const AgeTable& age_table = local_data.age_table; - // Bail out if the slot is in young generation. - if (V8_LIKELY(age_table[params.slot_offset] == AgeTable::Age::kYoung)) return; + // Bail out if the slot (precise or imprecise) is in young generation. + if (V8_LIKELY(age_table.GetAge(params.slot_offset) == AgeTable::Age::kYoung)) + return; - GenerationalBarrierSlow(local_data, age_table, slot, params.value_offset); + // Dispatch between different types of barriers. + // TODO(chromium:1029379): Consider reload local_data in the slow path to + // reduce register pressure. + if constexpr (type == GenerationalBarrierType::kPreciseSlot) { + GenerationalBarrierSlow(local_data, age_table, slot, params.value_offset, + params.heap); + } else if constexpr (type == + GenerationalBarrierType::kPreciseUncompressedSlot) { + GenerationalBarrierForUncompressedSlotSlow( + local_data, age_table, slot, params.value_offset, params.heap); + } else { + GenerationalBarrierForSourceObjectSlow(local_data, slot, params.heap); + } } #endif // !CPPGC_YOUNG_GENERATION diff --git a/android/arm64-v8a/include/v8/cppgc/liveness-broker.h b/android/arm64-v8a/include/v8/cppgc/liveness-broker.h index e4490912..2c94f1c0 100644 --- a/android/arm64-v8a/include/v8/cppgc/liveness-broker.h +++ b/android/arm64-v8a/include/v8/cppgc/liveness-broker.h @@ -7,6 +7,7 @@ #include "cppgc/heap.h" #include "cppgc/member.h" +#include "cppgc/sentinel-pointer.h" #include "cppgc/trace-trait.h" #include "v8config.h" // NOLINT(build/include_directory) @@ -44,21 +45,24 @@ 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 collections could not be strongified on + // stack. + // - Sentinel pointers are also preserved in weakness and not cleared. + return !object || object == kSentinelPointer || IsHeapObjectAliveImpl( TraceTrait::GetTraceDescriptor(object).base_object_payload); } template bool IsHeapObjectAlive(const WeakMember& weak_member) const { - return (weak_member != kSentinelPointer) && - IsHeapObjectAlive(weak_member.Get()); + return IsHeapObjectAlive(weak_member.Get()); } template bool IsHeapObjectAlive(const UntracedMember& untraced_member) const { - return (untraced_member != kSentinelPointer) && - IsHeapObjectAlive(untraced_member.Get()); + return IsHeapObjectAlive(untraced_member.Get()); } private: diff --git a/android/arm64-v8a/include/v8/cppgc/macros.h b/android/arm64-v8a/include/v8/cppgc/macros.h index 70ab44c6..a9ac22d7 100644 --- a/android/arm64-v8a/include/v8/cppgc/macros.h +++ b/android/arm64-v8a/include/v8/cppgc/macros.h @@ -5,13 +5,16 @@ #ifndef INCLUDE_CPPGC_MACROS_H_ #define INCLUDE_CPPGC_MACROS_H_ -#include +#include #include "cppgc/internal/compiler-specific.h" namespace cppgc { -// Use if the object is only stack allocated. +// Use CPPGC_STACK_ALLOCATED if the object is only stack allocated. +// Add the CPPGC_STACK_ALLOCATED_IGNORE annotation on a case-by-case basis when +// enforcement of CPPGC_STACK_ALLOCATED should be suppressed. +#if defined(__clang__) #define CPPGC_STACK_ALLOCATED() \ public: \ using IsStackAllocatedTypeMarker CPPGC_UNUSED = int; \ @@ -20,6 +23,12 @@ namespace cppgc { void* operator new(size_t) = delete; \ void* operator new(size_t, void*) = delete; \ static_assert(true, "Force semicolon.") +#define CPPGC_STACK_ALLOCATED_IGNORE(bug_or_reason) \ + __attribute__((annotate("stack_allocated_ignore"))) +#else // !defined(__clang__) +#define CPPGC_STACK_ALLOCATED() static_assert(true, "Force semicolon.") +#define CPPGC_STACK_ALLOCATED_IGNORE(bug_or_reason) +#endif // !defined(__clang__) } // namespace cppgc diff --git a/android/arm64-v8a/include/v8/cppgc/member.h b/android/arm64-v8a/include/v8/cppgc/member.h index 7b76bc4f..457f163b 100644 --- a/android/arm64-v8a/include/v8/cppgc/member.h +++ b/android/arm64-v8a/include/v8/cppgc/member.h @@ -9,6 +9,8 @@ #include #include +#include "cppgc/internal/api-constants.h" +#include "cppgc/internal/member-storage.h" #include "cppgc/internal/pointer-policies.h" #include "cppgc/sentinel-pointer.h" #include "cppgc/type-traits.h" @@ -16,221 +18,536 @@ namespace cppgc { +namespace subtle { +class HeapConsistency; +} // namespace subtle + class Visitor; namespace internal { // MemberBase always refers to the object as const object and defers to // BasicMember on casting to the right type as needed. -class MemberBase { +template +class V8_TRIVIAL_ABI MemberBase { + public: + using RawStorage = StorageType; + protected: - MemberBase() = default; - explicit MemberBase(const void* value) : raw_(value) {} + struct AtomicInitializerTag {}; - const void** GetRawSlot() const { return &raw_; } - const void* GetRaw() const { return raw_; } - void SetRaw(void* value) { raw_ = value; } - - const void* GetRawAtomic() const { - return reinterpret_cast*>(&raw_)->load( - std::memory_order_relaxed); - } - void SetRawAtomic(const void* value) { - reinterpret_cast*>(&raw_)->store( - value, std::memory_order_relaxed); + V8_INLINE MemberBase() = default; + V8_INLINE explicit MemberBase(const void* value) : raw_(value) {} + V8_INLINE MemberBase(const void* value, AtomicInitializerTag) { + SetRawAtomic(value); } - void ClearFromGC() const { raw_ = nullptr; } + V8_INLINE explicit MemberBase(RawStorage raw) : raw_(raw) {} + V8_INLINE explicit MemberBase(std::nullptr_t) : raw_(nullptr) {} + V8_INLINE explicit MemberBase(SentinelPointer s) : raw_(s) {} + + V8_INLINE const void** GetRawSlot() const { + return reinterpret_cast(const_cast(this)); + } + V8_INLINE const void* GetRaw() const { return raw_.Load(); } + V8_INLINE void SetRaw(void* value) { raw_.Store(value); } + + V8_INLINE const void* GetRawAtomic() const { return raw_.LoadAtomic(); } + V8_INLINE void SetRawAtomic(const void* value) { raw_.StoreAtomic(value); } + + V8_INLINE RawStorage GetRawStorage() const { return raw_; } + V8_INLINE void SetRawStorageAtomic(RawStorage other) { + reinterpret_cast&>(raw_).store( + other, std::memory_order_relaxed); + } + + V8_INLINE bool IsCleared() const { return raw_.IsCleared(); } + + V8_INLINE void ClearFromGC() const { raw_.Clear(); } private: - mutable const void* raw_ = nullptr; + friend class MemberDebugHelper; + + mutable RawStorage raw_; }; // The basic class from which all Member classes are 'generated'. template -class BasicMember final : private MemberBase, private CheckingPolicy { + typename CheckingPolicy, typename StorageType> +class V8_TRIVIAL_ABI BasicMember final : private MemberBase, + private CheckingPolicy { + using Base = MemberBase; + public: using PointeeType = T; + using RawStorage = typename Base::RawStorage; - constexpr BasicMember() = default; - constexpr BasicMember(std::nullptr_t) {} // NOLINT - BasicMember(SentinelPointer s) : MemberBase(s) {} // NOLINT - BasicMember(T* raw) : MemberBase(raw) { // NOLINT - InitializingWriteBarrier(); + V8_INLINE constexpr BasicMember() = default; + V8_INLINE constexpr BasicMember(std::nullptr_t) {} // NOLINT + V8_INLINE BasicMember(SentinelPointer s) : Base(s) {} // NOLINT + V8_INLINE BasicMember(T* raw) : Base(raw) { // NOLINT + InitializingWriteBarrier(raw); this->CheckPointer(Get()); } - BasicMember(T& raw) : BasicMember(&raw) {} // NOLINT + V8_INLINE BasicMember(T& raw) // NOLINT + : BasicMember(&raw) {} + + // Atomic ctor. Using the AtomicInitializerTag forces BasicMember to + // initialize using atomic assignments. This is required for preventing + // data races with concurrent marking. + using AtomicInitializerTag = typename Base::AtomicInitializerTag; + V8_INLINE BasicMember(std::nullptr_t, AtomicInitializerTag atomic) + : Base(nullptr, atomic) {} + V8_INLINE BasicMember(SentinelPointer s, AtomicInitializerTag atomic) + : Base(s, atomic) {} + V8_INLINE BasicMember(T* raw, AtomicInitializerTag atomic) + : Base(raw, atomic) { + InitializingWriteBarrier(raw); + this->CheckPointer(Get()); + } + V8_INLINE BasicMember(T& raw, AtomicInitializerTag atomic) + : BasicMember(&raw, atomic) {} + // Copy ctor. - BasicMember(const BasicMember& other) : BasicMember(other.Get()) {} - // Allow heterogeneous construction. + V8_INLINE BasicMember(const BasicMember& other) + : BasicMember(other.GetRawStorage()) {} + + // Heterogeneous copy constructors. When the source pointer have a different + // type, perform a compress-decompress round, because the source pointer may + // need to be adjusted. template ::value>> - BasicMember( // NOLINT + std::enable_if_t>* = nullptr> + V8_INLINE BasicMember( // NOLINT const BasicMember& other) + OtherCheckingPolicy, StorageType>& other) + : BasicMember(other.GetRawStorage()) {} + + template >* = nullptr> + V8_INLINE BasicMember( // NOLINT + const BasicMember& other) : BasicMember(other.Get()) {} + // Move ctor. - BasicMember(BasicMember&& other) noexcept : BasicMember(other.Get()) { + V8_INLINE BasicMember(BasicMember&& other) noexcept + : BasicMember(other.GetRawStorage()) { other.Clear(); } - // Allow heterogeneous move construction. + + // Heterogeneous move constructors. When the source pointer have a different + // type, perform a compress-decompress round, because the source pointer may + // need to be adjusted. template ::value>> - BasicMember( // NOLINT - BasicMember&& other) noexcept + std::enable_if_t>* = nullptr> + V8_INLINE BasicMember( + BasicMember&& other) noexcept + : BasicMember(other.GetRawStorage()) { + other.Clear(); + } + + template >* = nullptr> + V8_INLINE BasicMember( + BasicMember&& other) noexcept : BasicMember(other.Get()) { other.Clear(); } + // Construction from Persistent. template ::value>> - BasicMember( // NOLINT - const BasicPersistent& - p) + V8_INLINE BasicMember(const BasicPersistent& p) : BasicMember(p.Get()) {} // Copy assignment. - BasicMember& operator=(const BasicMember& other) { - return operator=(other.Get()); + V8_INLINE BasicMember& operator=(const BasicMember& other) { + return operator=(other.GetRawStorage()); } - // Allow heterogeneous copy assignment. + + // Heterogeneous copy assignment. When the source pointer have a different + // type, perform a compress-decompress round, because the source pointer may + // need to be adjusted. template ::value>> - BasicMember& operator=( + typename OtherCheckingPolicy> + V8_INLINE BasicMember& operator=( const BasicMember& other) { - return operator=(other.Get()); + OtherCheckingPolicy, StorageType>& other) { + if constexpr (internal::IsDecayedSameV) { + return operator=(other.GetRawStorage()); + } else { + static_assert(internal::IsStrictlyBaseOfV); + return operator=(other.Get()); + } } + // Move assignment. - BasicMember& operator=(BasicMember&& other) noexcept { - operator=(other.Get()); + V8_INLINE BasicMember& operator=(BasicMember&& other) noexcept { + operator=(other.GetRawStorage()); other.Clear(); return *this; } - // Heterogeneous move assignment. + + // Heterogeneous move assignment. When the source pointer have a different + // type, perform a compress-decompress round, because the source pointer may + // need to be adjusted. template ::value>> - BasicMember& operator=(BasicMember&& other) noexcept { - operator=(other.Get()); + typename OtherCheckingPolicy> + V8_INLINE BasicMember& operator=( + BasicMember&& other) noexcept { + if constexpr (internal::IsDecayedSameV) { + operator=(other.GetRawStorage()); + } else { + static_assert(internal::IsStrictlyBaseOfV); + operator=(other.Get()); + } other.Clear(); return *this; } + // Assignment from Persistent. template ::value>> - BasicMember& operator=( + V8_INLINE BasicMember& operator=( const BasicPersistent& other) { return operator=(other.Get()); } - BasicMember& operator=(T* other) { - SetRawAtomic(other); - AssigningWriteBarrier(); + + V8_INLINE BasicMember& operator=(T* other) { + Base::SetRawAtomic(other); + AssigningWriteBarrier(other); this->CheckPointer(Get()); return *this; } - BasicMember& operator=(std::nullptr_t) { + + V8_INLINE BasicMember& operator=(std::nullptr_t) { Clear(); return *this; } - BasicMember& operator=(SentinelPointer s) { - SetRawAtomic(s); + V8_INLINE BasicMember& operator=(SentinelPointer s) { + Base::SetRawAtomic(s); return *this; } template - void Swap(BasicMember& other) { - T* tmp = Get(); + V8_INLINE void Swap(BasicMember& other) { + auto tmp = GetRawStorage(); *this = other; other = tmp; } - explicit operator bool() const { return Get(); } - operator T*() const { return Get(); } // NOLINT - T* operator->() const { return Get(); } - T& operator*() const { return *Get(); } + V8_INLINE explicit operator bool() const { return !Base::IsCleared(); } + V8_INLINE operator T*() const { return Get(); } + V8_INLINE T* operator->() const { return Get(); } + V8_INLINE T& operator*() const { return *Get(); } // CFI cast exemption to allow passing SentinelPointer through T* and support // heterogeneous assignments between different Member and Persistent handles // based on their actual types. - V8_CLANG_NO_SANITIZE("cfi-unrelated-cast") T* Get() const { + V8_INLINE V8_CLANG_NO_SANITIZE("cfi-unrelated-cast") T* Get() const { // Executed by the mutator, hence non atomic load. // // The const_cast below removes the constness from MemberBase storage. The // following static_cast re-adds any constness if specified through the // user-visible template parameter T. - return static_cast(const_cast(MemberBase::GetRaw())); + return static_cast(const_cast(Base::GetRaw())); } - void Clear() { SetRawAtomic(nullptr); } + V8_INLINE void Clear() { + Base::SetRawStorageAtomic(RawStorage{}); + } - T* Release() { + V8_INLINE T* Release() { T* result = Get(); Clear(); return result; } - const T** GetSlotForTesting() const { - return reinterpret_cast(GetRawSlot()); + V8_INLINE const T** GetSlotForTesting() const { + return reinterpret_cast(Base::GetRawSlot()); + } + + V8_INLINE RawStorage GetRawStorage() const { + return Base::GetRawStorage(); } private: - const T* GetRawAtomic() const { - return static_cast(MemberBase::GetRawAtomic()); + V8_INLINE explicit BasicMember(RawStorage raw) : Base(raw) { + InitializingWriteBarrier(Get()); + this->CheckPointer(Get()); } - void InitializingWriteBarrier() const { - WriteBarrierPolicy::InitializingBarrier(GetRawSlot(), GetRaw()); - } - void AssigningWriteBarrier() const { - WriteBarrierPolicy::AssigningBarrier(GetRawSlot(), GetRaw()); + V8_INLINE BasicMember& operator=(RawStorage other) { + Base::SetRawStorageAtomic(other); + AssigningWriteBarrier(); + this->CheckPointer(Get()); + return *this; } - void ClearFromGC() const { MemberBase::ClearFromGC(); } + V8_INLINE const T* GetRawAtomic() const { + return static_cast(Base::GetRawAtomic()); + } + V8_INLINE void InitializingWriteBarrier(T* value) const { + WriteBarrierPolicy::InitializingBarrier(Base::GetRawSlot(), value); + } + V8_INLINE void AssigningWriteBarrier(T* value) const { + WriteBarrierPolicy::template AssigningBarrier< + StorageType::kWriteBarrierSlotType>(Base::GetRawSlot(), value); + } + V8_INLINE void AssigningWriteBarrier() const { + WriteBarrierPolicy::template AssigningBarrier< + StorageType::kWriteBarrierSlotType>(Base::GetRawSlot(), + Base::GetRawStorage()); + } + + V8_INLINE void ClearFromGC() const { Base::ClearFromGC(); } + + V8_INLINE T* GetFromGC() const { return Get(); } + + friend class cppgc::subtle::HeapConsistency; friend class cppgc::Visitor; template friend struct cppgc::TraceTrait; + template + friend class BasicMember; }; +// Member equality operators. template -bool operator==( - BasicMember member1, - BasicMember - member2) { - return member1.Get() == member2.Get(); + typename WriteBarrierPolicy2, typename CheckingPolicy2, + typename StorageType> +V8_INLINE bool operator==( + const BasicMember& member1, + const BasicMember& member2) { + if constexpr (internal::IsDecayedSameV) { + // Check compressed pointers if types are the same. + return member1.GetRawStorage() == member2.GetRawStorage(); + } else { + static_assert(internal::IsStrictlyBaseOfV || + internal::IsStrictlyBaseOfV); + // Otherwise, check decompressed pointers. + return member1.Get() == member2.Get(); + } } template -bool operator!=( - BasicMember member1, - BasicMember - member2) { + typename WriteBarrierPolicy2, typename CheckingPolicy2, + typename StorageType> +V8_INLINE bool operator!=( + const BasicMember& member1, + const BasicMember& member2) { return !(member1 == member2); } -template -struct IsWeak< - internal::BasicMember> +// Equality with raw pointers. +template +V8_INLINE bool operator==( + const BasicMember& member, + U* raw) { + // Never allow comparison with erased pointers. + static_assert(!internal::IsDecayedSameV); + + if constexpr (internal::IsDecayedSameV) { + // Check compressed pointers if types are the same. + return member.GetRawStorage() == StorageType(raw); + } else if constexpr (internal::IsStrictlyBaseOfV) { + // Cast the raw pointer to T, which may adjust the pointer. + return member.GetRawStorage() == StorageType(static_cast(raw)); + } else { + // Otherwise, decompressed the member. + return member.Get() == raw; + } +} + +template +V8_INLINE bool operator!=( + const BasicMember& member, + U* raw) { + return !(member == raw); +} + +template +V8_INLINE bool operator==( + T* raw, const BasicMember& member) { + return member == raw; +} + +template +V8_INLINE bool operator!=( + T* raw, const BasicMember& member) { + return !(raw == member); +} + +// Equality with sentinel. +template +V8_INLINE bool operator==( + const BasicMember& member, + SentinelPointer) { + return member.GetRawStorage().IsSentinel(); +} + +template +V8_INLINE bool operator!=( + const BasicMember& member, + SentinelPointer s) { + return !(member == s); +} + +template +V8_INLINE bool operator==( + SentinelPointer s, const BasicMember& member) { + return member == s; +} + +template +V8_INLINE bool operator!=( + SentinelPointer s, const BasicMember& member) { + return !(s == member); +} + +// Equality with nullptr. +template +V8_INLINE bool operator==( + const BasicMember& member, + std::nullptr_t) { + return !static_cast(member); +} + +template +V8_INLINE bool operator!=( + const BasicMember& member, + std::nullptr_t n) { + return !(member == n); +} + +template +V8_INLINE bool operator==( + std::nullptr_t n, const BasicMember& member) { + return member == n; +} + +template +V8_INLINE bool operator!=( + std::nullptr_t n, const BasicMember& member) { + return !(n == member); +} + +// Relational operators. +template +V8_INLINE bool operator<( + const BasicMember& member1, + const BasicMember& member2) { + static_assert( + internal::IsDecayedSameV, + "Comparison works only for same pointer type modulo cv-qualifiers"); + return member1.GetRawStorage() < member2.GetRawStorage(); +} + +template +V8_INLINE bool operator<=( + const BasicMember& member1, + const BasicMember& member2) { + static_assert( + internal::IsDecayedSameV, + "Comparison works only for same pointer type modulo cv-qualifiers"); + return member1.GetRawStorage() <= member2.GetRawStorage(); +} + +template +V8_INLINE bool operator>( + const BasicMember& member1, + const BasicMember& member2) { + static_assert( + internal::IsDecayedSameV, + "Comparison works only for same pointer type modulo cv-qualifiers"); + return member1.GetRawStorage() > member2.GetRawStorage(); +} + +template +V8_INLINE bool operator>=( + const BasicMember& member1, + const BasicMember& member2) { + static_assert( + internal::IsDecayedSameV, + "Comparison works only for same pointer type modulo cv-qualifiers"); + return member1.GetRawStorage() >= member2.GetRawStorage(); +} + +template +struct IsWeak> : std::true_type {}; } // namespace internal @@ -241,8 +558,9 @@ struct IsWeak< * trace method. */ template -using Member = internal::BasicMember; +using Member = internal::BasicMember< + T, internal::StrongMemberTag, internal::DijkstraWriteBarrierPolicy, + internal::DefaultMemberCheckingPolicy, internal::DefaultMemberStorage>; /** * WeakMember is similar to Member in that it is used to point to other garbage @@ -253,8 +571,9 @@ using Member = internal::BasicMember -using WeakMember = internal::BasicMember; +using WeakMember = internal::BasicMember< + T, internal::WeakMemberTag, internal::DijkstraWriteBarrierPolicy, + internal::DefaultMemberCheckingPolicy, internal::DefaultMemberStorage>; /** * UntracedMember is a pointer to an on-heap object that is not traced for some @@ -263,8 +582,47 @@ using WeakMember = internal::BasicMember -using UntracedMember = internal::BasicMember; +using UntracedMember = internal::BasicMember< + T, internal::UntracedMemberTag, internal::NoWriteBarrierPolicy, + internal::DefaultMemberCheckingPolicy, internal::DefaultMemberStorage>; + +namespace subtle { + +/** + * UncompressedMember. Use with care in hot paths that would otherwise cause + * many decompression cycles. + */ +template +using UncompressedMember = internal::BasicMember< + T, internal::StrongMemberTag, internal::DijkstraWriteBarrierPolicy, + internal::DefaultMemberCheckingPolicy, internal::RawPointer>; + +#if defined(CPPGC_POINTER_COMPRESSION) +/** + * CompressedMember. Default implementation of cppgc::Member on builds with + * pointer compression. + */ +template +using CompressedMember = internal::BasicMember< + T, internal::StrongMemberTag, internal::DijkstraWriteBarrierPolicy, + internal::DefaultMemberCheckingPolicy, internal::CompressedPointer>; +#endif // defined(CPPGC_POINTER_COMPRESSION) + +} // namespace subtle + +namespace internal { + +struct Dummy; + +static constexpr size_t kSizeOfMember = sizeof(Member); +static constexpr size_t kSizeOfUncompressedMember = + sizeof(subtle::UncompressedMember); +#if defined(CPPGC_POINTER_COMPRESSION) +static constexpr size_t kSizeofCompressedMember = + sizeof(subtle::CompressedMember); +#endif // defined(CPPGC_POINTER_COMPRESSION) + +} // namespace internal } // namespace cppgc diff --git a/android/arm64-v8a/include/v8/cppgc/name-provider.h b/android/arm64-v8a/include/v8/cppgc/name-provider.h index 8b70b8ea..216f6098 100644 --- a/android/arm64-v8a/include/v8/cppgc/name-provider.h +++ b/android/arm64-v8a/include/v8/cppgc/name-provider.h @@ -37,15 +37,15 @@ class V8_EXPORT NameProvider { static constexpr const char kNoNameDeducible[] = ""; /** - * Indicating whether internal names are hidden or not. + * Indicating whether the build supports extracting C++ names as object names. * * @returns true if C++ names should be hidden and represented by kHiddenName. */ - static constexpr bool HideInternalNames() { + static constexpr bool SupportsCppClassNamesAsObjectNames() { #if CPPGC_SUPPORTS_OBJECT_NAMES - return false; -#else // !CPPGC_SUPPORTS_OBJECT_NAMES return true; +#else // !CPPGC_SUPPORTS_OBJECT_NAMES + return false; #endif // !CPPGC_SUPPORTS_OBJECT_NAMES } @@ -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/android/arm64-v8a/include/v8/cppgc/persistent.h b/android/arm64-v8a/include/v8/cppgc/persistent.h index d7aac723..709f3fd6 100644 --- a/android/arm64-v8a/include/v8/cppgc/persistent.h +++ b/android/arm64-v8a/include/v8/cppgc/persistent.h @@ -16,9 +16,6 @@ #include "v8config.h" // NOLINT(build/include_directory) namespace cppgc { - -class Visitor; - namespace internal { // PersistentBase always refers to the object as const object and defers to @@ -41,11 +38,11 @@ class PersistentBase { node_ = nullptr; } - private: + protected: mutable const void* raw_ = nullptr; mutable PersistentNode* node_ = nullptr; - friend class PersistentRegion; + friend class PersistentRegionBase; }; // The basic class from which all Persistent classes are generated. @@ -78,7 +75,7 @@ class BasicPersistent final : public PersistentBase, : PersistentBase(raw), LocationPolicy(loc) { if (!IsValid()) return; SetNode(WeaknessPolicy::GetPersistentRegion(GetValue()) - .AllocateNode(this, &BasicPersistent::Trace)); + .AllocateNode(this, &TraceAsRoot)); this->CheckPointer(Get()); } @@ -95,7 +92,7 @@ class BasicPersistent final : public PersistentBase, template ::value>> - BasicPersistent( // NOLINT + BasicPersistent( const BasicPersistent& other, const SourceLocation& loc = SourceLocation::Current()) @@ -117,10 +114,11 @@ class BasicPersistent final : public PersistentBase, // Constructor from member. template ::value>> - BasicPersistent(internal::BasicMember - member, + BasicPersistent(const internal::BasicMember< + U, MemberBarrierPolicy, MemberWeaknessTag, + MemberCheckingPolicy, MemberStorageType>& member, const SourceLocation& loc = SourceLocation::Current()) : BasicPersistent(member.Get(), loc) {} @@ -141,7 +139,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)); @@ -157,10 +155,11 @@ class BasicPersistent final : public PersistentBase, // Assignment from member. template ::value>> BasicPersistent& operator=( - internal::BasicMember + const internal::BasicMember& member) { return operator=(member.Get()); } @@ -181,7 +180,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(); } @@ -222,9 +221,8 @@ class BasicPersistent final : public PersistentBase, } private: - static void Trace(Visitor* v, const void* ptr) { - const auto* persistent = static_cast(ptr); - v->TraceRoot(*persistent, persistent->Location()); + static void TraceAsRoot(RootVisitor& root_visitor, const void* ptr) { + root_visitor.Trace(*static_cast(ptr)); } bool IsValid() const { @@ -248,7 +246,7 @@ class BasicPersistent final : public PersistentBase, SetValue(ptr); if (!IsValid()) return; SetNode(WeaknessPolicy::GetPersistentRegion(GetValue()) - .AllocateNode(this, &BasicPersistent::Trace)); + .AllocateNode(this, &TraceAsRoot)); this->CheckPointer(Get()); } @@ -259,7 +257,13 @@ class BasicPersistent final : public PersistentBase, } } - friend class cppgc::Visitor; + // Set Get() for details. + V8_CLANG_NO_SANITIZE("cfi-unrelated-cast") + T* GetFromGC() const { + return static_cast(const_cast(GetValue())); + } + + friend class internal::RootVisitor; }; template -bool operator==(const BasicPersistent& p, - BasicMember - m) { + typename MemberWeaknessTag, typename MemberCheckingPolicy, + typename MemberStorageType> +bool operator==( + const BasicPersistent& + p, + const BasicMember& m) { return p.Get() == m.Get(); } template -bool operator!=(const BasicPersistent& p, - BasicMember - m) { + typename MemberWeaknessTag, typename MemberCheckingPolicy, + typename MemberStorageType> +bool operator!=( + const BasicPersistent& + p, + const BasicMember& m) { return !(p == m); } template -bool operator==(BasicMember - m, - const BasicPersistent& p) { + typename MemberStorageType, typename T2, + typename PersistentWeaknessPolicy, typename PersistentLocationPolicy, + typename PersistentCheckingPolicy> +bool operator==( + const BasicMember& m, + const BasicPersistent& + p) { return m.Get() == p.Get(); } template -bool operator!=(BasicMember - m, - const BasicPersistent& p) { + typename MemberStorageType, typename T2, + typename PersistentWeaknessPolicy, typename PersistentLocationPolicy, + typename PersistentCheckingPolicy> +bool operator!=( + const BasicMember& m, + const BasicPersistent& + p) { return !(m == p); } diff --git a/android/arm64-v8a/include/v8/cppgc/platform.h b/android/arm64-v8a/include/v8/cppgc/platform.h index 0d737766..ae96579d 100644 --- a/android/arm64-v8a/include/v8/cppgc/platform.h +++ b/android/arm64-v8a/include/v8/cppgc/platform.h @@ -5,6 +5,9 @@ #ifndef INCLUDE_CPPGC_PLATFORM_H_ #define INCLUDE_CPPGC_PLATFORM_H_ +#include + +#include "cppgc/source-location.h" #include "v8-platform.h" // NOLINT(build/include_directory) #include "v8config.h" // NOLINT(build/include_directory) @@ -30,8 +33,9 @@ class V8_EXPORT Platform { virtual ~Platform() = default; /** - * Returns the allocator used by cppgc to allocate its heap and various - * support structures. + * \returns the allocator used by cppgc to allocate its heap and various + * support structures. Returning nullptr results in using the `PageAllocator` + * provided by `cppgc::InitializeProcess()` instead. */ virtual PageAllocator* GetPageAllocator() = 0; @@ -129,10 +133,16 @@ class V8_EXPORT Platform { * * Can be called multiple times when paired with `ShutdownProcess()`. * - * \param page_allocator The allocator used for maintaining meta data. Must not - * change between multiple calls to InitializeProcess. + * \param page_allocator The allocator used for maintaining meta data. Must stay + * always alive and not change between multiple calls to InitializeProcess. If + * no allocator is provided, a default internal version will be used. + * \param desired_heap_size Desired amount of virtual address space to reserve + * for the heap, in bytes. Actual size will be clamped to minimum and maximum + * values based on compile-time settings and may be rounded up. If this + * parameter is zero, a default value will be used. */ -V8_EXPORT void InitializeProcess(PageAllocator* page_allocator); +V8_EXPORT void InitializeProcess(PageAllocator* page_allocator = nullptr, + size_t desired_heap_size = 0); /** * Must be called after destroying the last used heap. Some process-global @@ -143,9 +153,11 @@ V8_EXPORT void ShutdownProcess(); namespace internal { -V8_EXPORT void Abort(); +V8_EXPORT void Fatal(const std::string& reason = std::string(), + const SourceLocation& = SourceLocation::Current()); } // namespace internal + } // namespace cppgc #endif // INCLUDE_CPPGC_PLATFORM_H_ diff --git a/android/arm64-v8a/include/v8/cppgc/prefinalizer.h b/android/arm64-v8a/include/v8/cppgc/prefinalizer.h index 29b18bef..51f2eac8 100644 --- a/android/arm64-v8a/include/v8/cppgc/prefinalizer.h +++ b/android/arm64-v8a/include/v8/cppgc/prefinalizer.h @@ -6,23 +6,17 @@ #define INCLUDE_CPPGC_PREFINALIZER_H_ #include "cppgc/internal/compiler-specific.h" -#include "cppgc/internal/prefinalizer-handler.h" #include "cppgc/liveness-broker.h" namespace cppgc { namespace internal { -template -class PrefinalizerRegistration final { +class V8_EXPORT PrefinalizerRegistration final { public: - explicit PrefinalizerRegistration(T* self) { - static_assert(sizeof(&T::InvokePreFinalizer) > 0, - "USING_PRE_FINALIZER(T) must be defined."); + using Callback = bool (*)(const cppgc::LivenessBroker&, void*); - cppgc::internal::PreFinalizerRegistrationDispatcher::RegisterPrefinalizer( - {self, T::InvokePreFinalizer}); - } + PrefinalizerRegistration(void*, Callback); void* operator new(size_t, void* location) = delete; void* operator new(size_t) = delete; @@ -30,6 +24,35 @@ class PrefinalizerRegistration final { } // namespace internal +/** + * Macro must be used in the private section of `Class` and registers a + * prefinalization callback `void Class::PreFinalizer()`. The callback is + * invoked on garbage collection after the collector has found an object to be + * dead. + * + * Callback properties: + * - The callback is invoked before a possible destructor for the corresponding + * object. + * - The callback may access the whole object graph, irrespective of whether + * objects are considered dead or alive. + * - The callback is invoked on the same thread as the object was created on. + * + * Example: + * \code + * class WithPrefinalizer : public GarbageCollected { + * CPPGC_USING_PRE_FINALIZER(WithPrefinalizer, Dispose); + * + * public: + * void Trace(Visitor*) const {} + * void Dispose() { prefinalizer_called = true; } + * ~WithPrefinalizer() { + * // prefinalizer_called == true + * } + * private: + * bool prefinalizer_called = false; + * }; + * \endcode + */ #define CPPGC_USING_PRE_FINALIZER(Class, PreFinalizer) \ public: \ static bool InvokePreFinalizer(const cppgc::LivenessBroker& liveness_broker, \ @@ -38,13 +61,13 @@ class PrefinalizerRegistration final { "Only garbage collected objects can have prefinalizers"); \ Class* self = static_cast(object); \ if (liveness_broker.IsHeapObjectAlive(self)) return false; \ - self->Class::PreFinalizer(); \ + self->PreFinalizer(); \ return true; \ } \ \ private: \ - CPPGC_NO_UNIQUE_ADDRESS cppgc::internal::PrefinalizerRegistration \ - prefinalizer_dummy_{this}; \ + CPPGC_NO_UNIQUE_ADDRESS cppgc::internal::PrefinalizerRegistration \ + prefinalizer_dummy_{this, Class::InvokePreFinalizer}; \ static_assert(true, "Force semicolon.") } // namespace cppgc diff --git a/android/arm64-v8a/include/v8/cppgc/sentinel-pointer.h b/android/arm64-v8a/include/v8/cppgc/sentinel-pointer.h index f7915834..bee96c77 100644 --- a/android/arm64-v8a/include/v8/cppgc/sentinel-pointer.h +++ b/android/arm64-v8a/include/v8/cppgc/sentinel-pointer.h @@ -7,15 +7,22 @@ #include +#include "cppgc/internal/api-constants.h" + namespace cppgc { namespace internal { // Special tag type used to denote some sentinel member. The semantics of the // sentinel is defined by the embedder. struct SentinelPointer { +#if defined(CPPGC_POINTER_COMPRESSION) + static constexpr intptr_t kSentinelValue = + 1 << api_constants::kPointerCompressionShift; +#else // !defined(CPPGC_POINTER_COMPRESSION) + static constexpr intptr_t kSentinelValue = 0b10; +#endif // !defined(CPPGC_POINTER_COMPRESSION) template - operator T*() const { // NOLINT - static constexpr intptr_t kSentinelValue = 1; + operator T*() const { return reinterpret_cast(kSentinelValue); } // Hidden friends. diff --git a/android/arm64-v8a/include/v8/cppgc/source-location.h b/android/arm64-v8a/include/v8/cppgc/source-location.h index 29d69b0a..0dc28aed 100644 --- a/android/arm64-v8a/include/v8/cppgc/source-location.h +++ b/android/arm64-v8a/include/v8/cppgc/source-location.h @@ -5,86 +5,11 @@ #ifndef INCLUDE_CPPGC_SOURCE_LOCATION_H_ #define INCLUDE_CPPGC_SOURCE_LOCATION_H_ -#include - -#include "v8config.h" // NOLINT(build/include_directory) - -#if defined(__has_builtin) -#define CPPGC_SUPPORTS_SOURCE_LOCATION \ - (__has_builtin(__builtin_FUNCTION) && __has_builtin(__builtin_FILE) && \ - __has_builtin(__builtin_LINE)) // NOLINT -#elif defined(V8_CC_GNU) && __GNUC__ >= 7 -#define CPPGC_SUPPORTS_SOURCE_LOCATION 1 -#elif defined(V8_CC_INTEL) && __ICC >= 1800 -#define CPPGC_SUPPORTS_SOURCE_LOCATION 1 -#else -#define CPPGC_SUPPORTS_SOURCE_LOCATION 0 -#endif +#include "v8-source-location.h" namespace cppgc { -/** - * Encapsulates source location information. Mimics C++20's - * `std::source_location`. - */ -class V8_EXPORT SourceLocation final { - public: - /** - * Construct source location information corresponding to the location of the - * call site. - */ -#if CPPGC_SUPPORTS_SOURCE_LOCATION - static constexpr SourceLocation Current( - const char* function = __builtin_FUNCTION(), - const char* file = __builtin_FILE(), size_t line = __builtin_LINE()) { - return SourceLocation(function, file, line); - } -#else - static constexpr SourceLocation Current() { return SourceLocation(); } -#endif // CPPGC_SUPPORTS_SOURCE_LOCATION - - /** - * Constructs unspecified source location information. - */ - constexpr SourceLocation() = default; - - /** - * Returns the name of the function associated with the position represented - * by this object, if any. - * - * \returns the function name as cstring. - */ - constexpr const char* Function() const { return function_; } - - /** - * Returns the name of the current source file represented by this object. - * - * \returns the file name as cstring. - */ - constexpr const char* FileName() const { return file_; } - - /** - * Returns the line number represented by this object. - * - * \returns the line number. - */ - constexpr size_t Line() const { return line_; } - - /** - * Returns a human-readable string representing this object. - * - * \returns a human-readable string representing source location information. - */ - std::string ToString() const; - - private: - constexpr SourceLocation(const char* function, const char* file, size_t line) - : function_(function), file_(file), line_(line) {} - - const char* function_ = nullptr; - const char* file_ = nullptr; - size_t line_ = 0u; -}; +using SourceLocation = v8::SourceLocation; } // namespace cppgc diff --git a/android/arm64-v8a/include/v8/cppgc/testing.h b/android/arm64-v8a/include/v8/cppgc/testing.h index 229ce140..bddd1fc1 100644 --- a/android/arm64-v8a/include/v8/cppgc/testing.h +++ b/android/arm64-v8a/include/v8/cppgc/testing.h @@ -19,8 +19,13 @@ class HeapHandle; namespace testing { /** - * Overrides the state of the stack with the provided value. Takes precedence - * over other parameters that set the stack state. Must no be nested. + * Overrides the state of the stack with the provided value. Parameters passed + * to explicit garbage collection calls still take precedence. Must not be + * nested. + * + * This scope is useful to make the garbage collector consider the stack when + * tasks that invoke garbage collection (through the provided platform) contain + * interesting pointers on its stack. */ class V8_EXPORT V8_NODISCARD OverrideEmbedderStackStateScope final { CPPGC_STACK_ALLOCATED(); @@ -93,6 +98,8 @@ class V8_EXPORT StandaloneTestingHeap final { HeapHandle& heap_handle_; }; +V8_EXPORT bool IsHeapObjectOld(void*); + } // namespace testing } // namespace cppgc diff --git a/android/arm64-v8a/include/v8/cppgc/trace-trait.h b/android/arm64-v8a/include/v8/cppgc/trace-trait.h index 83619b1d..5fc863d2 100644 --- a/android/arm64-v8a/include/v8/cppgc/trace-trait.h +++ b/android/arm64-v8a/include/v8/cppgc/trace-trait.h @@ -16,6 +16,10 @@ class Visitor; namespace internal { +class RootVisitor; + +using TraceRootCallback = void (*)(RootVisitor&, const void* object); + // Implementation of the default TraceTrait handling GarbageCollected and // GarbageCollectedMixin. template #include namespace cppgc { @@ -15,7 +16,7 @@ class Visitor; namespace internal { template + typename CheckingPolicy, typename StorageType> class BasicMember; struct DijkstraWriteBarrierPolicy; struct NoWriteBarrierPolicy; @@ -23,14 +24,6 @@ class StrongMemberTag; class UntracedMemberTag; class WeakMemberTag; -// Pre-C++17 custom implementation of std::void_t. -template -struct make_void { - typedef void type; -}; -template -using void_t = typename make_void::type; - // Not supposed to be specialized by the user. template struct IsWeak : std::false_type {}; @@ -41,7 +34,7 @@ template struct IsTraceMethodConst : std::false_type {}; template -struct IsTraceMethodConst().Trace( +struct IsTraceMethodConst().Trace( std::declval()))>> : std::true_type { }; @@ -52,7 +45,7 @@ struct IsTraceable : std::false_type { template struct IsTraceable< - T, void_t().Trace(std::declval()))>> + T, std::void_t().Trace(std::declval()))>> : std::true_type { // All Trace methods should be marked as const. If an object of type // 'T' is traceable then any object of type 'const T' should also @@ -71,8 +64,8 @@ struct HasGarbageCollectedMixinTypeMarker : std::false_type { template struct HasGarbageCollectedMixinTypeMarker< - T, - void_t::IsGarbageCollectedMixinTypeMarker>> + T, std::void_t< + typename std::remove_const_t::IsGarbageCollectedMixinTypeMarker>> : std::true_type { static_assert(sizeof(T), "T must be fully defined"); }; @@ -84,7 +77,8 @@ struct HasGarbageCollectedTypeMarker : std::false_type { template struct HasGarbageCollectedTypeMarker< - T, void_t::IsGarbageCollectedTypeMarker>> + T, + std::void_t::IsGarbageCollectedTypeMarker>> : std::true_type { static_assert(sizeof(T), "T must be fully defined"); }; @@ -132,9 +126,10 @@ template struct IsSubclassOfBasicMemberTemplate { private: - template + template static std::true_type SubclassCheck( - BasicMember*); + BasicMember*); static std::false_type SubclassCheck(...); public: @@ -164,6 +159,27 @@ 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; +}; + +template +constexpr bool IsDecayedSameV = + std::is_same_v, std::decay_t>; + +template +constexpr bool IsStrictlyBaseOfV = + std::is_base_of_v, std::decay_t> && + !IsDecayedSameV; + } // namespace internal /** @@ -223,6 +239,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/android/arm64-v8a/include/v8/cppgc/visitor.h b/android/arm64-v8a/include/v8/cppgc/visitor.h index 98de9957..1d6b39a1 100644 --- a/android/arm64-v8a/include/v8/cppgc/visitor.h +++ b/android/arm64-v8a/include/v8/cppgc/visitor.h @@ -5,13 +5,17 @@ #ifndef INCLUDE_CPPGC_VISITOR_H_ #define INCLUDE_CPPGC_VISITOR_H_ +#include + #include "cppgc/custom-space.h" #include "cppgc/ephemeron-pair.h" #include "cppgc/garbage-collected.h" #include "cppgc/internal/logging.h" +#include "cppgc/internal/member-storage.h" #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" @@ -61,22 +65,6 @@ class V8_EXPORT Visitor { virtual ~Visitor() = default; - /** - * Trace method for raw pointers. Prefer the versions for managed pointers. - * - * \param member Reference retaining an object. - */ - template - void Trace(const T* t) { - static_assert(sizeof(T), "Pointee type must be fully defined."); - static_assert(internal::IsGarbageCollectedOrMixinType::value, - "T must be GarbageCollected or GarbageCollectedMixin type"); - if (!t) { - return; - } - Visit(t, TraceTrait::GetTraceDescriptor(t)); - } - /** * Trace method for Member. * @@ -86,7 +74,7 @@ class V8_EXPORT Visitor { void Trace(const Member& member) { const T* value = member.GetRawAtomic(); CPPGC_DCHECK(value != kSentinelPointer); - Trace(value); + TraceImpl(value); } /** @@ -114,6 +102,44 @@ class V8_EXPORT Visitor { &HandleWeak>, &weak_member); } +#if defined(CPPGC_POINTER_COMPRESSION) + /** + * Trace method for UncompressedMember. + * + * \param member UncompressedMember reference retaining an object. + */ + template + void Trace(const subtle::UncompressedMember& member) { + const T* value = member.GetRawAtomic(); + CPPGC_DCHECK(value != kSentinelPointer); + TraceImpl(value); + } +#endif // defined(CPPGC_POINTER_COMPRESSION) + + template + void TraceMultiple(const subtle::UncompressedMember* start, size_t len) { + static_assert(sizeof(T), "Pointee type must be fully defined."); + static_assert(internal::IsGarbageCollectedOrMixinType::value, + "T must be GarbageCollected or GarbageCollectedMixin type"); + VisitMultipleUncompressedMember(start, len, + &TraceTrait::GetTraceDescriptor); + } + + template , subtle::UncompressedMember>>* = nullptr> + void TraceMultiple(const Member* start, size_t len) { + static_assert(sizeof(T), "Pointee type must be fully defined."); + static_assert(internal::IsGarbageCollectedOrMixinType::value, + "T must be GarbageCollected or GarbageCollectedMixin type"); +#if defined(CPPGC_POINTER_COMPRESSION) + static_assert(std::is_same_v, subtle::CompressedMember>, + "Member and CompressedMember must be the same."); + VisitMultipleCompressedMember(start, len, + &TraceTrait::GetTraceDescriptor); +#endif // defined(CPPGC_POINTER_COMPRESSION) + } + /** * Trace method for inlined objects that are not allocated themselves but * otherwise follow managed heap layout and have a Trace() method. @@ -132,6 +158,26 @@ class V8_EXPORT Visitor { TraceTrait::Trace(this, &object); } + template + void TraceMultiple(const T* start, size_t len) { +#if V8_ENABLE_CHECKS + // This object is embedded in potentially multiple nested objects. The + // outermost object must not be in construction as such objects are (a) not + // processed immediately, and (b) only processed conservatively if not + // otherwise possible. + CheckObjectNotInConstruction(start); +#endif // V8_ENABLE_CHECKS + for (size_t i = 0; i < len; ++i) { + const T* object = &start[i]; + if constexpr (std::is_polymorphic_v) { + // The object's vtable may be uninitialized in which case the object is + // not traced. + if (*reinterpret_cast(object) == 0) continue; + } + TraceTrait::Trace(this, object); + } + } + /** * Registers a weak callback method on the object of type T. See * LivenessBroker for an usage example. @@ -230,23 +276,34 @@ class V8_EXPORT Visitor { void TraceStrongly(const WeakMember& weak_member) { const T* value = weak_member.GetRawAtomic(); CPPGC_DCHECK(value != kSentinelPointer); - Trace(value); + TraceImpl(value); } /** - * Trace method for weak containers. + * Trace method for retaining containers strongly. * - * \param object reference of the weak container. + * \param object reference to the container. + */ + template + void TraceStrongContainer(const T* object) { + TraceImpl(object); + } + + /** + * Trace method for retaining containers weakly. Note that weak containers + * should emit write barriers. + * + * \param object reference to the container. * \param callback to be invoked. - * \param data custom data that is passed to the callback. + * \param callback_data custom data that is passed to the callback. */ template void TraceWeakContainer(const T* object, WeakCallback callback, - const void* data) { + const void* callback_data) { if (!object) return; VisitWeakContainer(object, TraceTrait::GetTraceDescriptor(object), TraceTrait::GetWeakTraceDescriptor(object), callback, - data); + callback_data); } /** @@ -254,6 +311,7 @@ class V8_EXPORT Visitor { * compactable space. Such references maybe be arbitrarily moved by the GC. * * \param slot location of reference to object that might be moved by the GC. + * The slot must contain an uncompressed pointer. */ template void RegisterMovableReference(const T** slot) { @@ -296,9 +354,6 @@ class V8_EXPORT Visitor { virtual void Visit(const void* self, TraceDescriptor) {} virtual void VisitWeak(const void* self, TraceDescriptor, WeakCallback, const void* weak_member) {} - virtual void VisitRoot(const void*, TraceDescriptor, const SourceLocation&) {} - virtual void VisitWeakRoot(const void* self, TraceDescriptor, WeakCallback, - const void* weak_root, const SourceLocation&) {} virtual void VisitEphemeron(const void* key, const void* value, TraceDescriptor value_desc) {} virtual void VisitWeakContainer(const void* self, TraceDescriptor strong_desc, @@ -306,6 +361,39 @@ class V8_EXPORT Visitor { WeakCallback callback, const void* data) {} virtual void HandleMovableReference(const void**) {} + virtual void VisitMultipleUncompressedMember( + const void* start, size_t len, + TraceDescriptorCallback get_trace_descriptor) { + // Default implementation merely delegates to Visit(). + const char* it = static_cast(start); + const char* end = it + len * internal::kSizeOfUncompressedMember; + for (; it < end; it += internal::kSizeOfUncompressedMember) { + const auto* current = reinterpret_cast(it); + const void* object = current->LoadAtomic(); + if (!object) continue; + + Visit(object, get_trace_descriptor(object)); + } + } + +#if defined(CPPGC_POINTER_COMPRESSION) + virtual void VisitMultipleCompressedMember( + const void* start, size_t len, + TraceDescriptorCallback get_trace_descriptor) { + // Default implementation merely delegates to Visit(). + const char* it = static_cast(start); + const char* end = it + len * internal::kSizeofCompressedMember; + for (; it < end; it += internal::kSizeofCompressedMember) { + const auto* current = + reinterpret_cast(it); + const void* object = current->LoadAtomic(); + if (!object) continue; + + Visit(object, get_trace_descriptor(object)); + } + } +#endif // defined(CPPGC_POINTER_COMPRESSION) + private: template static void WeakCallbackMethodDelegate(const LivenessBroker& info, @@ -318,44 +406,20 @@ class V8_EXPORT Visitor { template static void HandleWeak(const LivenessBroker& info, const void* object) { const PointerType* weak = static_cast(object); - // Sentinel values are preserved for weak pointers. - if (*weak == kSentinelPointer) return; - const auto* raw = weak->Get(); - if (!info.IsHeapObjectAlive(raw)) { + if (!info.IsHeapObjectAlive(weak->GetFromGC())) { weak->ClearFromGC(); } } - template * = nullptr> - void TraceRoot(const Persistent& p, const SourceLocation& loc) { - using PointeeType = typename Persistent::PointeeType; - static_assert(sizeof(PointeeType), - "Persistent's pointee type must be fully defined"); - static_assert(internal::IsGarbageCollectedOrMixinType::value, - "Persistent's pointee type must be GarbageCollected or " - "GarbageCollectedMixin"); - if (!p.Get()) { + template + void TraceImpl(const T* t) { + static_assert(sizeof(T), "Pointee type must be fully defined."); + static_assert(internal::IsGarbageCollectedOrMixinType::value, + "T must be GarbageCollected or GarbageCollectedMixin type"); + if (!t) { return; } - VisitRoot(p.Get(), TraceTrait::GetTraceDescriptor(p.Get()), - loc); - } - - template < - typename WeakPersistent, - std::enable_if_t* = nullptr> - void TraceRoot(const WeakPersistent& p, const SourceLocation& loc) { - using PointeeType = typename WeakPersistent::PointeeType; - static_assert(sizeof(PointeeType), - "Persistent's pointee type must be fully defined"); - static_assert(internal::IsGarbageCollectedOrMixinType::value, - "Persistent's pointee type must be GarbageCollected or " - "GarbageCollectedMixin"); - static_assert(!internal::IsAllocatedOnCompactableSpace::value, - "Weak references to compactable objects are not allowed"); - VisitWeakRoot(p.Get(), TraceTrait::GetTraceDescriptor(p.Get()), - &HandleWeak, &p, loc); + Visit(t, TraceTrait::GetTraceDescriptor(t)); } #if V8_ENABLE_CHECKS @@ -372,6 +436,69 @@ class V8_EXPORT Visitor { friend class internal::VisitorBase; }; +namespace internal { + +class V8_EXPORT RootVisitor { + public: + explicit RootVisitor(Visitor::Key) {} + + virtual ~RootVisitor() = default; + + template * = nullptr> + void Trace(const AnyStrongPersistentType& p) { + using PointeeType = typename AnyStrongPersistentType::PointeeType; + const void* object = Extract(p); + if (!object) { + return; + } + VisitRoot(object, TraceTrait::GetTraceDescriptor(object), + p.Location()); + } + + template * = nullptr> + void Trace(const AnyWeakPersistentType& p) { + using PointeeType = typename AnyWeakPersistentType::PointeeType; + static_assert(!internal::IsAllocatedOnCompactableSpace::value, + "Weak references to compactable objects are not allowed"); + const void* object = Extract(p); + if (!object) { + return; + } + VisitWeakRoot(object, TraceTrait::GetTraceDescriptor(object), + &HandleWeak, &p, p.Location()); + } + + protected: + virtual void VisitRoot(const void*, TraceDescriptor, const SourceLocation&) {} + virtual void VisitWeakRoot(const void* self, TraceDescriptor, WeakCallback, + const void* weak_root, const SourceLocation&) {} + + private: + template + static const void* Extract(AnyPersistentType& p) { + using PointeeType = typename AnyPersistentType::PointeeType; + static_assert(sizeof(PointeeType), + "Persistent's pointee type must be fully defined"); + static_assert(internal::IsGarbageCollectedOrMixinType::value, + "Persistent's pointee type must be GarbageCollected or " + "GarbageCollectedMixin"); + return p.GetFromGC(); + } + + template + static void HandleWeak(const LivenessBroker& info, const void* object) { + const PointerType* weak = static_cast(object); + if (!info.IsHeapObjectAlive(weak->GetFromGC())) { + weak->ClearFromGC(); + } + } +}; + +} // namespace internal } // namespace cppgc #endif // INCLUDE_CPPGC_VISITOR_H_ diff --git a/android/arm64-v8a/include/v8/js_protocol-1.3.json b/android/arm64-v8a/include/v8/js_protocol-1.3.json index ea573d11..a998d461 100644 --- a/android/arm64-v8a/include/v8/js_protocol-1.3.json +++ b/android/arm64-v8a/include/v8/js_protocol-1.3.json @@ -946,34 +946,6 @@ { "name": "url", "type": "string", "description": "JavaScript script name or url." }, { "name": "functions", "type": "array", "items": { "$ref": "FunctionCoverage" }, "description": "Functions contained in the script that has coverage data." } ] - }, - { "id": "TypeObject", - "type": "object", - "description": "Describes a type collected during runtime.", - "properties": [ - { "name": "name", "type": "string", "description": "Name of a type collected with type profiling." } - ], - "experimental": true - }, - { "id": "TypeProfileEntry", - "type": "object", - "description": "Source offset and types for a parameter or return value.", - "properties": [ - { "name": "offset", "type": "integer", "description": "Source offset of the parameter or end of function for return values." }, - { "name": "types", "type": "array", "items": {"$ref": "TypeObject"}, "description": "The types for this parameter or return value."} - ], - "experimental": true - }, - { - "id": "ScriptTypeProfile", - "type": "object", - "description": "Type profile data collected during runtime for a JavaScript script.", - "properties": [ - { "name": "scriptId", "$ref": "Runtime.ScriptId", "description": "JavaScript script id." }, - { "name": "url", "type": "string", "description": "JavaScript script name or url." }, - { "name": "entries", "type": "array", "items": { "$ref": "TypeProfileEntry" }, "description": "Type profile entries for parameters and return values of the functions in the script." } - ], - "experimental": true } ], "commands": [ @@ -1024,24 +996,6 @@ { "name": "result", "type": "array", "items": { "$ref": "ScriptCoverage" }, "description": "Coverage data for the current isolate." } ], "description": "Collect coverage data for the current isolate. The coverage data may be incomplete due to garbage collection." - }, - { - "name": "startTypeProfile", - "description": "Enable type profile.", - "experimental": true - }, - { - "name": "stopTypeProfile", - "description": "Disable type profile. Disabling releases type profile data collected so far.", - "experimental": true - }, - { - "name": "takeTypeProfile", - "returns": [ - { "name": "result", "type": "array", "items": { "$ref": "ScriptTypeProfile" }, "description": "Type profile for all scripts since startTypeProfile() was turned on." } - ], - "description": "Collect type profile.", - "experimental": true } ], "events": [ diff --git a/android/arm64-v8a/include/v8/js_protocol.pdl b/android/arm64-v8a/include/v8/js_protocol.pdl index 666952f2..7a3c772c 100644 --- a/android/arm64-v8a/include/v8/js_protocol.pdl +++ b/android/arm64-v8a/include/v8/js_protocol.pdl @@ -104,13 +104,20 @@ domain Debugger # Location in the source code. Location location # JavaScript script name or url. - string url + # Deprecated in favor of using the `location.scriptId` to resolve the URL via a previously + # sent `Debugger.scriptParsed` event. + deprecated string url # Scope chain for this call frame. array of Scope scopeChain # `this` object for this call frame. Runtime.RemoteObject this # The value being returned, if the function is at return point. optional Runtime.RemoteObject returnValue + # Valid only while the VM is paused and indicates whether this frame + # can be restarted or not. Note that a `true` value here does not + # guarantee that Debugger#restartFrame with this CallFrameId will be + # successful, but it is very likely. + experimental optional boolean canBeRestarted # Scope description. type Scope extends object @@ -175,7 +182,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. @@ -237,6 +244,40 @@ domain Debugger # Wasm bytecode. optional binary bytecode + experimental type WasmDisassemblyChunk extends object + properties + # The next chunk of disassembled lines. + array of string lines + # The bytecode offsets describing the start of each line. + array of integer bytecodeOffsets + + experimental command disassembleWasmModule + parameters + # Id of the script to disassemble + Runtime.ScriptId scriptId + returns + # For large modules, return a stream from which additional chunks of + # disassembly can be read successively. + optional string streamId + # The total number of lines in the disassembly text. + integer totalNumberOfLines + # The offsets of all function bodies, in the format [start1, end1, + # start2, end2, ...] where all ends are exclusive. + array of integer functionBodyOffsets + # The first chunk of disassembly. + WasmDisassemblyChunk chunk + + # Disassemble the next chunk of lines for the module corresponding to the + # stream. If disassembly is complete, this API will invalidate the streamId + # and return an empty chunk. Any subsequent calls for the now invalid stream + # will return errors. + experimental command nextWasmDisassemblyChunk + parameters + string streamId + returns + # The next chunk of disassembly. + WasmDisassemblyChunk chunk + # This command is deprecated. Use getScriptSource instead. deprecated command getWasmBytecode parameters @@ -266,18 +307,35 @@ domain Debugger parameters BreakpointId breakpointId - # Restarts particular call frame from the beginning. + # Restarts particular call frame from the beginning. The old, deprecated + # behavior of `restartFrame` is to stay paused and allow further CDP commands + # after a restart was scheduled. This can cause problems with restarting, so + # we now continue execution immediatly after it has been scheduled until we + # reach the beginning of the restarted frame. + # + # To stay back-wards compatible, `restartFrame` now expects a `mode` + # parameter to be present. If the `mode` parameter is missing, `restartFrame` + # errors out. + # + # The various return values are deprecated and `callFrames` is always empty. + # Use the call frames from the `Debugger#paused` events instead, that fires + # once V8 pauses at the beginning of the restarted function. command restartFrame parameters # Call frame identifier to evaluate on. CallFrameId callFrameId + # The `mode` parameter must be present and set to 'StepInto', otherwise + # `restartFrame` will error out. + experimental optional enum mode + # Pause at the beginning of the restarted function + StepInto returns # New stack trace. - array of CallFrame callFrames + deprecated array of CallFrame callFrames # Async stack trace, if any. - optional Runtime.StackTrace asyncStackTrace + deprecated optional Runtime.StackTrace asyncStackTrace # Async stack trace, if any. - experimental optional Runtime.StackTraceId asyncStackTraceId + deprecated optional Runtime.StackTraceId asyncStackTraceId # Resumes JavaScript execution. command resume @@ -400,13 +458,14 @@ domain Debugger # New value for breakpoints active state. boolean active - # Defines pause on exceptions state. Can be set to stop on all exceptions, uncaught exceptions or - # no exceptions. Initial pause on exceptions state is `none`. + # Defines pause on exceptions state. Can be set to stop on all exceptions, uncaught exceptions, + # or caught exceptions, no exceptions. Initial pause on exceptions state is `none`. command setPauseOnExceptions parameters # Pause on exceptions mode. enum state none + caught uncaught all @@ -417,6 +476,12 @@ domain Debugger Runtime.CallArgument newValue # Edits JavaScript source live. + # + # In general, functions that are currently on the stack can not be edited with + # a single exception: If the edited function is the top-most stack frame and + # that is the only activation of that function on the stack. In this case + # the live edit will be successful and a `Debugger.restartFrame` for the + # top-most function is automatically triggered. command setScriptSource parameters # Id of the script to edit. @@ -426,16 +491,28 @@ domain Debugger # If true the change will not actually be applied. Dry run may be used to get result # description without actually modifying the code. optional boolean dryRun + # If true, then `scriptSource` is allowed to change the function on top of the stack + # as long as the top-most stack frame is the only activation of that function. + experimental optional boolean allowTopFrameEditing returns # New stack trace in case editing has happened while VM was stopped. - optional array of CallFrame callFrames + deprecated optional array of CallFrame callFrames # Whether current call stack was modified after applying the changes. - optional boolean stackChanged + deprecated optional boolean stackChanged # Async stack trace, if any. - optional Runtime.StackTrace asyncStackTrace + deprecated optional Runtime.StackTrace asyncStackTrace # Async stack trace, if any. - experimental optional Runtime.StackTraceId asyncStackTraceId - # Exception details if any. + deprecated optional Runtime.StackTraceId asyncStackTraceId + # Whether the operation was successful or not. Only `Ok` denotes a + # successful live edit while the other enum variants denote why + # the live edit failed. + experimental enum status + Ok + CompileError + BlockedByActiveGenerator + BlockedByActiveFunction + BlockedByTopLevelEsModuleChange + # Exception details if any. Only present when `status` is `CompileError`. optional Runtime.ExceptionDetails exceptionDetails # Makes page not interrupt on any pauses (breakpoint, exception, dom exception etc). @@ -503,6 +580,7 @@ domain Debugger other promiseRejection XHR + step # Object containing break-specific auxiliary properties. optional object data # Hit breakpoints IDs @@ -552,9 +630,9 @@ domain Debugger integer endColumn # Specifies script creation context. Runtime.ExecutionContextId executionContextId - # Content hash of the script. + # Content hash of the script, SHA-256. string hash - # Embedder-specific auxiliary data. + # Embedder-specific auxiliary data likely matching {isDefault: boolean, type: 'default'|'isolated'|'worker', frameId: string} optional object executionContextAuxData # URL of source map associated with script (if any). optional string sourceMapURL @@ -591,9 +669,9 @@ domain Debugger integer endColumn # Specifies script creation context. Runtime.ExecutionContextId executionContextId - # Content hash of the script. + # Content hash of the script, SHA-256. string hash - # Embedder-specific auxiliary data. + # Embedder-specific auxiliary data likely matching {isDefault: boolean, type: 'default'|'isolated'|'worker', frameId: string} optional object executionContextAuxData # True, if this script is generated as a result of the live edit operation. experimental optional boolean isLiveEdit @@ -691,6 +769,22 @@ experimental domain HeapProfiler # Average sample interval in bytes. Poisson distribution is used for the intervals. The # default value is 32768 bytes. optional number samplingInterval + # By default, the sampling heap profiler reports only objects which are + # still alive when the profile is returned via getSamplingProfile or + # stopSampling, which is useful for determining what functions contribute + # the most to steady-state memory usage. This flag instructs the sampling + # heap profiler to also include information about objects discarded by + # major GC, which will show which functions cause large temporary memory + # usage or long GC pauses. + optional boolean includeObjectsCollectedByMajorGC + # By default, the sampling heap profiler reports only objects which are + # still alive when the profile is returned via getSamplingProfile or + # stopSampling, which is useful for determining what functions contribute + # the most to steady-state memory usage. This flag instructs the sampling + # heap profiler to also include information about objects discarded by + # minor GC, which is useful when tuning a latency-sensitive application + # for minimal GC activity. + optional boolean includeObjectsCollectedByMinorGC command startTrackingHeapObjects parameters @@ -706,14 +800,24 @@ experimental domain HeapProfiler # If true 'reportHeapSnapshotProgress' events will be generated while snapshot is being taken # when the tracking is stopped. optional boolean reportProgress - optional boolean treatGlobalObjectsAsRoots + # Deprecated in favor of `exposeInternals`. + deprecated optional boolean treatGlobalObjectsAsRoots + # If true, numerical values are included in the snapshot + optional boolean captureNumericValue + # If true, exposes internals of the snapshot. + experimental optional boolean exposeInternals 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 - optional boolean treatGlobalObjectsAsRoots + # If true, a raw snapshot without artificial roots will be generated. + # Deprecated in favor of `exposeInternals`. + deprecated optional boolean treatGlobalObjectsAsRoots + # If true, numerical values are included in the snapshot + optional boolean captureNumericValue + # If true, exposes internals of the snapshot. + experimental optional boolean exposeInternals event addHeapSnapshotChunk parameters @@ -817,48 +921,6 @@ domain Profiler # Functions contained in the script that has coverage data. array of FunctionCoverage functions - # Describes a type collected during runtime. - experimental type TypeObject extends object - properties - # Name of a type collected with type profiling. - string name - - # Source offset and types for a parameter or return value. - experimental type TypeProfileEntry extends object - properties - # Source offset of the parameter or end of function for return values. - integer offset - # The types for this parameter or return value. - array of TypeObject types - - # Type profile data collected during runtime for a JavaScript script. - experimental type ScriptTypeProfile extends object - properties - # JavaScript script id. - Runtime.ScriptId scriptId - # JavaScript script name or url. - string url - # Type profile entries for parameters and return values of the functions in the script. - array of TypeProfileEntry entries - - # Collected counter information. - experimental type CounterInfo extends object - properties - # Counter name. - string name - # Counter value. - integer value - - # Runtime call counter information. - experimental type RuntimeCallCounterInfo extends object - properties - # Counter name. - string name - # Counter value. - number value - # Counter time in seconds. - number time - command disable command enable @@ -893,9 +955,6 @@ domain Profiler # Monotonically increasing time (in seconds) when the coverage update was taken in the backend. number timestamp - # Enable type profile. - experimental command startTypeProfile - command stop returns # Recorded profile. @@ -905,9 +964,6 @@ domain Profiler # executing optimized code. command stopPreciseCoverage - # Disable type profile. Disabling releases type profile data collected so far. - experimental command stopTypeProfile - # Collect coverage data for the current isolate, and resets execution counters. Precise code # coverage needs to have started. command takePreciseCoverage @@ -917,36 +973,6 @@ domain Profiler # Monotonically increasing time (in seconds) when the coverage update was taken in the backend. number timestamp - # Collect type profile. - experimental command takeTypeProfile - returns - # Type profile for all scripts since startTypeProfile() was turned on. - array of ScriptTypeProfile result - - # Enable counters collection. - experimental command enableCounters - - # Disable counters collection. - experimental command disableCounters - - # Retrieve counters. - experimental command getCounters - returns - # Collected counters information. - array of CounterInfo result - - # Enable run time call stats collection. - experimental command enableRuntimeCallStats - - # Disable run time call stats collection. - experimental command disableRuntimeCallStats - - # Retrieve run time call stats. - experimental command getRuntimeCallStats - returns - # Collected runtime call counter information. - array of RuntimeCallCounterInfo result - event consoleProfileFinished parameters string id @@ -968,13 +994,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 @@ -988,6 +1014,60 @@ domain Runtime # Unique script identifier. type ScriptId extends string + # Represents options for serialization. Overrides `generatePreview`, `returnByValue` and + # `generateWebDriverValue`. + type SerializationOptions extends object + properties + enum serialization + # Whether the result should be deep-serialized. The result is put into + # `deepSerializedValue` and `ObjectId` is provided. + deep + # Whether the result is expected to be a JSON object which should be sent by value. + # The result is put either into `value` or into `unserializableValue`. Synonym of + # `returnByValue: true`. Overrides `returnByValue`. + json + # Only remote object id is put in the result. Same bahaviour as if no + # `serializationOptions`, `generatePreview`, `returnByValue` nor `generateWebDriverValue` + # are provided. + idOnly + + # Deep serialization depth. Default is full depth. Respected only in `deep` serialization mode. + optional integer maxDepth + + # Represents deep serialized value. + type DeepSerializedValue extends object + properties + enum type + undefined + null + string + number + boolean + bigint + regexp + date + symbol + array + object + function + map + set + weakmap + weakset + error + proxy + promise + typedarray + arraybuffer + node + window + optional any value + optional string objectId + # Set if value reference met more then once during serialization. In such + # case, value is provided only to one of the serialized values. Unique + # per value in the scope of one CDP call. + optional integer weakLocalObjectReference + # Unique object identifier. type RemoteObjectId extends string @@ -1040,6 +1120,10 @@ domain Runtime optional UnserializableValue unserializableValue # String representation of the object. optional string description + # Deprecated. Use `deepSerializedValue` instead. WebDriver BiDi representation of the value. + deprecated optional DeepSerializedValue webDriverValue + # Deep serialized value. + experimental optional DeepSerializedValue deepSerializedValue # Unique object identifier (for non-primitive values). optional RemoteObjectId objectId # Preview containing abbreviated property values. Specified for `object` type values only. @@ -1221,11 +1305,11 @@ 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 - # Embedder-specific auxiliary data. + # Embedder-specific auxiliary data likely matching {isDefault: boolean, type: 'default'|'isolated'|'worker', frameId: string} optional object auxData # Detailed information about exception (or error) that was thrown during script compilation or @@ -1250,6 +1334,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 @@ -1325,6 +1413,7 @@ domain Runtime # execution. Overrides `setPauseOnException` state. optional boolean silent # Whether the result is expected to be a JSON object which should be sent by value. + # Can be overriden by `serializationOptions`. optional boolean returnByValue # Whether preview should be generated for the result. experimental optional boolean generatePreview @@ -1339,6 +1428,24 @@ 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 + # An alternative way to specify the execution context to call function on. + # Compared to contextId that may be reused across processes, this is guaranteed to be + # system-unique, so it can be used to prevent accidental function call + # in context different than intended (e.g. as a result of navigation across process + # boundaries). + # This is mutually exclusive with `executionContextId`. + experimental optional string uniqueContextId + # Deprecated. Use `serializationOptions: {serialization:"deep"}` instead. + # Whether the result should contain `webDriverValue`, serialized according to + # https://w3c.github.io/webdriver-bidi. This is mutually exclusive with `returnByValue`, but + # resulting `objectId` is still provided. + deprecated optional boolean generateWebDriverValue + # Specifies the result serialization. If provided, overrides + # `generatePreview`, `returnByValue` and `generateWebDriverValue`. + experimental optional SerializationOptions serializationOptions + returns # Call result. RemoteObject result @@ -1418,12 +1525,21 @@ 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 + # Deprecated. Use `serializationOptions: {serialization:"deep"}` instead. + # Whether the result should contain `webDriverValue`, serialized + # according to + # https://w3c.github.io/webdriver-bidi. This is mutually exclusive with `returnByValue`, but + # resulting `objectId` is still provided. + deprecated optional boolean generateWebDriverValue + # Specifies the result serialization. If provided, overrides + # `generatePreview`, `returnByValue` and `generateWebDriverValue`. + experimental optional SerializationOptions serializationOptions returns # Evaluation result. RemoteObject result @@ -1459,6 +1575,8 @@ domain Runtime experimental optional boolean accessorPropertiesOnly # Whether preview should be generated for the results. experimental optional boolean generatePreview + # If true, returns non-indexed properties only. + experimental optional boolean nonIndexedPropertiesOnly returns # Object properties. array of PropertyDescriptor result @@ -1563,7 +1681,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 @@ -1577,6 +1698,18 @@ domain Runtime parameters string name + # This method tries to lookup and populate exception details for a + # JavaScript Error object. + # Note that the stackTrace portion of the resulting exceptionDetails will + # only be populated if the Runtime domain was enabled at the time when the + # Error was thrown. + experimental command getExceptionDetails + parameters + # The error object for which to resolve the exception details. + RemoteObjectId errorObjectId + returns + optional ExceptionDetails exceptionDetails + # Notification is issued every time when binding is called. experimental event bindingCalled parameters @@ -1648,7 +1781,9 @@ domain Runtime event executionContextDestroyed parameters # Id of the destroyed context - ExecutionContextId executionContextId + deprecated ExecutionContextId executionContextId + # Unique Id of the destroyed context + experimental string executionContextUniqueId # Issued when all executionContexts were cleared in browser event executionContextsCleared @@ -1659,6 +1794,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/android/arm64-v8a/include/v8/libplatform/libplatform.h b/android/arm64-v8a/include/v8/libplatform/libplatform.h index 00de81df..9ec60c04 100644 --- a/android/arm64-v8a/include/v8/libplatform/libplatform.h +++ b/android/arm64-v8a/include/v8/libplatform/libplatform.h @@ -89,17 +89,6 @@ V8_PLATFORM_EXPORT void RunIdleTasks(v8::Platform* platform, v8::Isolate* isolate, double idle_time_in_seconds); -/** - * Attempts to set the tracing controller for the given platform. - * - * The |platform| has to be created using |NewDefaultPlatform|. - * - */ -V8_DEPRECATE_SOON("Access the DefaultPlatform directly") -V8_PLATFORM_EXPORT void SetTracingController( - v8::Platform* platform, - v8::platform::tracing::TracingController* tracing_controller); - /** * Notifies the given platform about the Isolate getting deleted soon. Has to be * called for all Isolates which are deleted - unless we're shutting down the diff --git a/android/arm64-v8a/include/v8/libplatform/v8-tracing.h b/android/arm64-v8a/include/v8/libplatform/v8-tracing.h index c7a5c4f9..6039a9c5 100644 --- a/android/arm64-v8a/include/v8/libplatform/v8-tracing.h +++ b/android/arm64-v8a/include/v8/libplatform/v8-tracing.h @@ -37,7 +37,6 @@ const int kTraceMaxNumArgs = 2; class V8_PLATFORM_EXPORT TraceObject { public: union ArgValue { - V8_DEPRECATED("use as_uint ? true : false") bool as_bool; uint64_t as_uint; int64_t as_int; double as_double; @@ -283,12 +282,12 @@ class V8_PLATFORM_EXPORT TracingController const char* name, uint64_t handle) override; static const char* GetCategoryGroupName(const uint8_t* category_enabled_flag); -#endif // !defined(V8_USE_PERFETTO) void AddTraceStateObserver( v8::TracingController::TraceStateObserver* observer) override; void RemoveTraceStateObserver( v8::TracingController::TraceStateObserver* observer) override; +#endif // !defined(V8_USE_PERFETTO) void StartTracing(TraceConfig* trace_config); void StopTracing(); @@ -308,7 +307,6 @@ class V8_PLATFORM_EXPORT TracingController std::unique_ptr mutex_; std::unique_ptr trace_config_; std::atomic_bool recording_{false}; - std::unordered_set observers_; #if defined(V8_USE_PERFETTO) std::ostream* output_stream_ = nullptr; @@ -317,6 +315,7 @@ class V8_PLATFORM_EXPORT TracingController TraceEventListener* listener_for_testing_ = nullptr; std::unique_ptr tracing_session_; #else // !defined(V8_USE_PERFETTO) + std::unordered_set observers_; std::unique_ptr trace_buffer_; #endif // !defined(V8_USE_PERFETTO) diff --git a/android/arm64-v8a/include/v8/v8-array-buffer.h b/android/arm64-v8a/include/v8/v8-array-buffer.h new file mode 100644 index 00000000..804fc42c --- /dev/null +++ b/android/arm64-v8a/include/v8/v8-array-buffer.h @@ -0,0 +1,512 @@ +// 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_V8_ARRAY_BUFFER_H_ +#define INCLUDE_V8_ARRAY_BUFFER_H_ + +#include + +#include + +#include "v8-local-handle.h" // NOLINT(build/include_directory) +#include "v8-object.h" // NOLINT(build/include_directory) +#include "v8config.h" // NOLINT(build/include_directory) + +namespace v8 { + +class SharedArrayBuffer; + +#ifndef V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT +// The number of required internal fields can be defined by embedder. +#define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT 2 +#endif + +enum class ArrayBufferCreationMode { kInternalized, kExternalized }; + +/** + * A wrapper around the backing store (i.e. the raw memory) of an array buffer. + * See a document linked in http://crbug.com/v8/9908 for more information. + * + * The allocation and destruction of backing stores is generally managed by + * V8. Clients should always use standard C++ memory ownership types (i.e. + * std::unique_ptr and std::shared_ptr) to manage lifetimes of backing stores + * properly, since V8 internal objects may alias backing stores. + * + * This object does not keep the underlying |ArrayBuffer::Allocator| alive by + * default. Use Isolate::CreateParams::array_buffer_allocator_shared when + * creating the Isolate to make it hold a reference to the allocator itself. + */ +class V8_EXPORT BackingStore : public v8::internal::BackingStoreBase { + public: + ~BackingStore(); + + /** + * Return a pointer to the beginning of the memory block for this backing + * store. The pointer is only valid as long as this backing store object + * lives. + */ + void* Data() const; + + /** + * The length (in bytes) of this backing store. + */ + size_t ByteLength() const; + + /** + * The maximum length (in bytes) that this backing store may grow to. + * + * If this backing store was created for a resizable ArrayBuffer or a growable + * SharedArrayBuffer, it is >= ByteLength(). Otherwise it is == + * ByteLength(). + */ + size_t MaxByteLength() const; + + /** + * Indicates whether the backing store was created for an ArrayBuffer or + * a SharedArrayBuffer. + */ + bool IsShared() const; + + /** + * Indicates whether the backing store was created for a resizable ArrayBuffer + * or a growable SharedArrayBuffer, and thus may be resized by user JavaScript + * code. + */ + bool IsResizableByUserJavaScript() const; + + /** + * Prevent implicit instantiation of operator delete with size_t argument. + * The size_t argument would be incorrect because ptr points to the + * internal BackingStore object. + */ + void operator delete(void* ptr) { ::operator delete(ptr); } + + /** + * Wrapper around ArrayBuffer::Allocator::Reallocate that preserves IsShared. + * Assumes that the backing_store was allocated by the ArrayBuffer allocator + * of the given isolate. + */ + static std::unique_ptr Reallocate( + v8::Isolate* isolate, std::unique_ptr backing_store, + size_t byte_length); + + /** + * This callback is used only if the memory block for a BackingStore cannot be + * allocated with an ArrayBuffer::Allocator. In such cases the destructor of + * the BackingStore invokes the callback to free the memory block. + */ + using DeleterCallback = void (*)(void* data, size_t length, + void* deleter_data); + + /** + * If the memory block of a BackingStore is static or is managed manually, + * then this empty deleter along with nullptr deleter_data can be passed to + * ArrayBuffer::NewBackingStore to indicate that. + * + * The manually managed case should be used with caution and only when it + * is guaranteed that the memory block freeing happens after detaching its + * ArrayBuffer. + */ + static void EmptyDeleter(void* data, size_t length, void* deleter_data); + + private: + /** + * See [Shared]ArrayBuffer::GetBackingStore and + * [Shared]ArrayBuffer::NewBackingStore. + */ + BackingStore(); +}; + +#if !defined(V8_IMMINENT_DEPRECATION_WARNINGS) +// Use v8::BackingStore::DeleterCallback instead. +using BackingStoreDeleterCallback = void (*)(void* data, size_t length, + void* deleter_data); + +#endif + +/** + * An instance of the built-in ArrayBuffer constructor (ES6 draft 15.13.5). + */ +class V8_EXPORT ArrayBuffer : public Object { + public: + /** + * A thread-safe allocator that V8 uses to allocate |ArrayBuffer|'s memory. + * The allocator is a global V8 setting. It has to be set via + * Isolate::CreateParams. + * + * Memory allocated through this allocator by V8 is accounted for as external + * memory by V8. Note that V8 keeps track of the memory for all internalized + * |ArrayBuffer|s. Responsibility for tracking external memory (using + * Isolate::AdjustAmountOfExternalAllocatedMemory) is handed over to the + * embedder upon externalization and taken over upon internalization (creating + * an internalized buffer from an existing buffer). + * + * Note that it is unsafe to call back into V8 from any of the allocator + * functions. + */ + class V8_EXPORT Allocator { + public: + virtual ~Allocator() = default; + + /** + * Allocate |length| bytes. Return nullptr if allocation is not successful. + * Memory should be initialized to zeroes. + */ + virtual void* Allocate(size_t length) = 0; + + /** + * Allocate |length| bytes. Return nullptr if allocation is not successful. + * Memory does not have to be initialized. + */ + virtual void* AllocateUninitialized(size_t length) = 0; + + /** + * Free the memory block of size |length|, pointed to by |data|. + * That memory is guaranteed to be previously allocated by |Allocate|. + */ + virtual void Free(void* data, size_t length) = 0; + + /** + * Reallocate the memory block of size |old_length| to a memory block of + * size |new_length| by expanding, contracting, or copying the existing + * memory block. If |new_length| > |old_length|, then the new part of + * the memory must be initialized to zeros. Return nullptr if reallocation + * is not successful. + * + * The caller guarantees that the memory block was previously allocated + * using Allocate or AllocateUninitialized. + * + * The default implementation allocates a new block and copies data. + */ + virtual void* Reallocate(void* data, size_t old_length, size_t new_length); + + /** + * ArrayBuffer allocation mode. kNormal is a malloc/free style allocation, + * while kReservation is for larger allocations with the ability to set + * access permissions. + */ + enum class AllocationMode { kNormal, kReservation }; + + /** + * Convenience allocator. + * + * When the sandbox is enabled, this allocator will allocate its backing + * memory inside the sandbox. Otherwise, it will rely on malloc/free. + * + * Caller takes ownership, i.e. the returned object needs to be freed using + * |delete allocator| once it is no longer in use. + */ + static Allocator* NewDefaultAllocator(); + }; + + /** + * Data length in bytes. + */ + size_t ByteLength() const; + + /** + * Maximum length in bytes. + */ + size_t MaxByteLength() const; + + /** + * Create a new ArrayBuffer. Allocate |byte_length| bytes. + * Allocated memory will be owned by a created ArrayBuffer and + * will be deallocated when it is garbage-collected, + * unless the object is externalized. + */ + static Local New(Isolate* isolate, size_t byte_length); + + /** + * Create a new ArrayBuffer with an existing backing store. + * The created array keeps a reference to the backing store until the array + * is garbage collected. Note that the IsExternal bit does not affect this + * reference from the array to the backing store. + * + * In future IsExternal bit will be removed. Until then the bit is set as + * follows. If the backing store does not own the underlying buffer, then + * the array is created in externalized state. Otherwise, the array is created + * in internalized state. In the latter case the array can be transitioned + * to the externalized state using Externalize(backing_store). + */ + static Local New(Isolate* isolate, + std::shared_ptr backing_store); + + /** + * Returns a new standalone BackingStore that is allocated using the array + * buffer allocator of the isolate. The result can be later passed to + * ArrayBuffer::New. + * + * If the allocator returns nullptr, then the function may cause GCs in the + * given isolate and re-try the allocation. If GCs do not help, then the + * function will crash with an out-of-memory error. + */ + static std::unique_ptr NewBackingStore(Isolate* isolate, + size_t byte_length); + /** + * Returns a new standalone BackingStore that takes over the ownership of + * the given buffer. The destructor of the BackingStore invokes the given + * deleter callback. + * + * The result can be later passed to ArrayBuffer::New. The raw pointer + * to the buffer must not be passed again to any V8 API function. + */ + static std::unique_ptr NewBackingStore( + void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter, + void* deleter_data); + + /** + * Returns a new resizable standalone BackingStore that is allocated using the + * array buffer allocator of the isolate. The result can be later passed to + * ArrayBuffer::New. + * + * |byte_length| must be <= |max_byte_length|. + * + * This function is usable without an isolate. Unlike |NewBackingStore| calls + * with an isolate, GCs cannot be triggered, and there are no + * retries. Allocation failure will cause the function to crash with an + * out-of-memory error. + */ + static std::unique_ptr NewResizableBackingStore( + size_t byte_length, size_t max_byte_length); + + /** + * Returns true if this ArrayBuffer may be detached. + */ + bool IsDetachable() const; + + /** + * Returns true if this ArrayBuffer has been detached. + */ + bool WasDetached() const; + + /** + * Detaches this ArrayBuffer and all its views (typed arrays). + * Detaching sets the byte length of the buffer and all typed arrays to zero, + * preventing JavaScript from ever accessing underlying backing store. + * ArrayBuffer should have been externalized and must be detachable. + */ + V8_DEPRECATE_SOON( + "Use the version which takes a key parameter (passing a null handle is " + "ok).") + void Detach(); + + /** + * Detaches this ArrayBuffer and all its views (typed arrays). + * Detaching sets the byte length of the buffer and all typed arrays to zero, + * preventing JavaScript from ever accessing underlying backing store. + * ArrayBuffer should have been externalized and must be detachable. Returns + * Nothing if the key didn't pass the [[ArrayBufferDetachKey]] check, + * Just(true) otherwise. + */ + V8_WARN_UNUSED_RESULT Maybe Detach(v8::Local key); + + /** + * Sets the ArrayBufferDetachKey. + */ + void SetDetachKey(v8::Local key); + + /** + * Get a shared pointer to the backing store of this array buffer. This + * pointer coordinates the lifetime management of the internal storage + * with any live ArrayBuffers on the heap, even across isolates. The embedder + * should not attempt to manage lifetime of the storage through other means. + * + * The returned shared pointer will not be empty, even if the ArrayBuffer has + * been detached. Use |WasDetached| to tell if it has been detached instead. + */ + std::shared_ptr GetBackingStore(); + + /** + * More efficient shortcut for GetBackingStore()->Data(). The returned pointer + * is valid as long as the ArrayBuffer is alive. + */ + void* Data() const; + + V8_INLINE static ArrayBuffer* Cast(Value* value) { +#ifdef V8_ENABLE_CHECKS + CheckCast(value); +#endif + return static_cast(value); + } + + static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT; + static const int kEmbedderFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT; + + private: + ArrayBuffer(); + static void CheckCast(Value* obj); +}; + +#ifndef V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT +// The number of required internal fields can be defined by embedder. +#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT 2 +#endif + +/** + * A base class for an instance of one of "views" over ArrayBuffer, + * including TypedArrays and DataView (ES6 draft 15.13). + */ +class V8_EXPORT ArrayBufferView : public Object { + public: + /** + * Returns underlying ArrayBuffer. + */ + Local Buffer(); + /** + * Byte offset in |Buffer|. + */ + size_t ByteOffset(); + /** + * Size of a view in bytes. + */ + size_t ByteLength(); + + /** + * Copy the contents of the ArrayBufferView's buffer to an embedder defined + * memory without additional overhead that calling ArrayBufferView::Buffer + * might incur. + * + * Will write at most min(|byte_length|, ByteLength) bytes starting at + * ByteOffset of the underlying buffer to the memory starting at |dest|. + * Returns the number of bytes actually written. + */ + size_t CopyContents(void* dest, size_t byte_length); + + /** + * Returns true if ArrayBufferView's backing ArrayBuffer has already been + * allocated. + */ + bool HasBuffer() const; + + V8_INLINE static ArrayBufferView* Cast(Value* value) { +#ifdef V8_ENABLE_CHECKS + CheckCast(value); +#endif + return static_cast(value); + } + + static const int kInternalFieldCount = + V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT; + static const int kEmbedderFieldCount = + V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT; + + private: + ArrayBufferView(); + static void CheckCast(Value* obj); +}; + +/** + * An instance of DataView constructor (ES6 draft 15.13.7). + */ +class V8_EXPORT DataView : public ArrayBufferView { + public: + static Local New(Local array_buffer, + size_t byte_offset, size_t length); + static Local New(Local shared_array_buffer, + size_t byte_offset, size_t length); + V8_INLINE static DataView* Cast(Value* value) { +#ifdef V8_ENABLE_CHECKS + CheckCast(value); +#endif + return static_cast(value); + } + + private: + DataView(); + static void CheckCast(Value* obj); +}; + +/** + * An instance of the built-in SharedArrayBuffer constructor. + */ +class V8_EXPORT SharedArrayBuffer : public Object { + public: + /** + * Data length in bytes. + */ + size_t ByteLength() const; + + /** + * Maximum length in bytes. + */ + size_t MaxByteLength() const; + + /** + * Create a new SharedArrayBuffer. Allocate |byte_length| bytes. + * Allocated memory will be owned by a created SharedArrayBuffer and + * will be deallocated when it is garbage-collected, + * unless the object is externalized. + */ + static Local New(Isolate* isolate, size_t byte_length); + + /** + * Create a new SharedArrayBuffer with an existing backing store. + * The created array keeps a reference to the backing store until the array + * is garbage collected. Note that the IsExternal bit does not affect this + * reference from the array to the backing store. + * + * In future IsExternal bit will be removed. Until then the bit is set as + * follows. If the backing store does not own the underlying buffer, then + * the array is created in externalized state. Otherwise, the array is created + * in internalized state. In the latter case the array can be transitioned + * to the externalized state using Externalize(backing_store). + */ + static Local New( + Isolate* isolate, std::shared_ptr backing_store); + + /** + * Returns a new standalone BackingStore that is allocated using the array + * buffer allocator of the isolate. The result can be later passed to + * SharedArrayBuffer::New. + * + * If the allocator returns nullptr, then the function may cause GCs in the + * given isolate and re-try the allocation. If GCs do not help, then the + * function will crash with an out-of-memory error. + */ + static std::unique_ptr NewBackingStore(Isolate* isolate, + size_t byte_length); + /** + * Returns a new standalone BackingStore that takes over the ownership of + * the given buffer. The destructor of the BackingStore invokes the given + * deleter callback. + * + * The result can be later passed to SharedArrayBuffer::New. The raw pointer + * to the buffer must not be passed again to any V8 functions. + */ + static std::unique_ptr NewBackingStore( + void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter, + void* deleter_data); + + /** + * Get a shared pointer to the backing store of this array buffer. This + * pointer coordinates the lifetime management of the internal storage + * with any live ArrayBuffers on the heap, even across isolates. The embedder + * should not attempt to manage lifetime of the storage through other means. + */ + std::shared_ptr GetBackingStore(); + + /** + * More efficient shortcut for GetBackingStore()->Data(). The returned pointer + * is valid as long as the ArrayBuffer is alive. + */ + void* Data() const; + + V8_INLINE static SharedArrayBuffer* Cast(Value* value) { +#ifdef V8_ENABLE_CHECKS + CheckCast(value); +#endif + return static_cast(value); + } + + static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT; + + private: + SharedArrayBuffer(); + static void CheckCast(Value* obj); +}; + +} // namespace v8 + +#endif // INCLUDE_V8_ARRAY_BUFFER_H_ diff --git a/android/arm64-v8a/include/v8/v8-callbacks.h b/android/arm64-v8a/include/v8/v8-callbacks.h new file mode 100644 index 00000000..12588c6c --- /dev/null +++ b/android/arm64-v8a/include/v8/v8-callbacks.h @@ -0,0 +1,422 @@ +// 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_V8_ISOLATE_CALLBACKS_H_ +#define INCLUDE_V8_ISOLATE_CALLBACKS_H_ + +#include + +#include +#include + +#include "cppgc/common.h" +#include "v8-data.h" // NOLINT(build/include_directory) +#include "v8-local-handle.h" // NOLINT(build/include_directory) +#include "v8-promise.h" // NOLINT(build/include_directory) +#include "v8config.h" // NOLINT(build/include_directory) + +#if defined(V8_OS_WIN) +struct _EXCEPTION_POINTERS; +#endif + +namespace v8 { + +template +class FunctionCallbackInfo; +class Isolate; +class Message; +class Module; +class Object; +class Promise; +class ScriptOrModule; +class String; +class UnboundScript; +class Value; + +/** + * A JIT code event is issued each time code is added, moved or removed. + * + * \note removal events are not currently issued. + */ +struct JitCodeEvent { + enum EventType { + CODE_ADDED, + CODE_MOVED, + CODE_REMOVED, + CODE_ADD_LINE_POS_INFO, + CODE_START_LINE_INFO_RECORDING, + CODE_END_LINE_INFO_RECORDING + }; + // Definition of the code position type. The "POSITION" type means the place + // in the source code which are of interest when making stack traces to + // pin-point the source location of a stack frame as close as possible. + // The "STATEMENT_POSITION" means the place at the beginning of each + // statement, and is used to indicate possible break locations. + enum PositionType { POSITION, STATEMENT_POSITION }; + + // There are three different kinds of CodeType, one for JIT code generated + // by the optimizing compiler, one for byte code generated for the + // interpreter, and one for code generated from Wasm. For JIT_CODE and + // WASM_CODE, |code_start| points to the beginning of jitted assembly code, + // while for BYTE_CODE events, |code_start| points to the first bytecode of + // the interpreted function. + enum CodeType { BYTE_CODE, JIT_CODE, WASM_CODE }; + + // Type of event. + EventType type; + CodeType code_type; + // Start of the instructions. + void* code_start; + // Size of the instructions. + size_t code_len; + // Script info for CODE_ADDED event. + Local script; + // User-defined data for *_LINE_INFO_* event. It's used to hold the source + // code line information which is returned from the + // CODE_START_LINE_INFO_RECORDING event. And it's passed to subsequent + // CODE_ADD_LINE_POS_INFO and CODE_END_LINE_INFO_RECORDING events. + void* user_data; + + struct name_t { + // Name of the object associated with the code, note that the string is not + // zero-terminated. + const char* str; + // Number of chars in str. + size_t len; + }; + + struct line_info_t { + // PC offset + size_t offset; + // Code position + size_t pos; + // The position type. + PositionType position_type; + }; + + struct wasm_source_info_t { + // Source file name. + const char* filename; + // Length of filename. + size_t filename_size; + // Line number table, which maps offsets of JITted code to line numbers of + // source file. + const line_info_t* line_number_table; + // Number of entries in the line number table. + size_t line_number_table_size; + }; + + wasm_source_info_t* wasm_source_info = nullptr; + + union { + // Only valid for CODE_ADDED. + struct name_t name; + + // Only valid for CODE_ADD_LINE_POS_INFO + struct line_info_t line_info; + + // New location of instructions. Only valid for CODE_MOVED. + void* new_code_start; + }; + + Isolate* isolate; +}; + +/** + * Option flags passed to the SetJitCodeEventHandler function. + */ +enum JitCodeEventOptions { + kJitCodeEventDefault = 0, + // Generate callbacks for already existent code. + kJitCodeEventEnumExisting = 1 +}; + +/** + * Callback function passed to SetJitCodeEventHandler. + * + * \param event code add, move or removal event. + */ +using JitCodeEventHandler = void (*)(const JitCodeEvent* event); + +// --- Garbage Collection Callbacks --- + +/** + * Applications can register callback functions which will be called before and + * after certain garbage collection operations. Allocations are not allowed in + * the callback functions, you therefore cannot manipulate objects (set or + * delete properties for example) since it is possible such operations will + * result in the allocation of objects. + */ +enum GCType { + kGCTypeScavenge = 1 << 0, + kGCTypeMinorMarkCompact = 1 << 1, + kGCTypeMarkSweepCompact = 1 << 2, + kGCTypeIncrementalMarking = 1 << 3, + kGCTypeProcessWeakCallbacks = 1 << 4, + kGCTypeAll = kGCTypeScavenge | kGCTypeMinorMarkCompact | + kGCTypeMarkSweepCompact | kGCTypeIncrementalMarking | + kGCTypeProcessWeakCallbacks +}; + +/** + * GCCallbackFlags is used to notify additional information about the GC + * callback. + * - kGCCallbackFlagConstructRetainedObjectInfos: The GC callback is for + * constructing retained object infos. + * - kGCCallbackFlagForced: The GC callback is for a forced GC for testing. + * - kGCCallbackFlagSynchronousPhantomCallbackProcessing: The GC callback + * is called synchronously without getting posted to an idle task. + * - kGCCallbackFlagCollectAllAvailableGarbage: The GC callback is called + * in a phase where V8 is trying to collect all available garbage + * (e.g., handling a low memory notification). + * - kGCCallbackScheduleIdleGarbageCollection: The GC callback is called to + * trigger an idle garbage collection. + */ +enum GCCallbackFlags { + kNoGCCallbackFlags = 0, + kGCCallbackFlagConstructRetainedObjectInfos = 1 << 1, + kGCCallbackFlagForced = 1 << 2, + kGCCallbackFlagSynchronousPhantomCallbackProcessing = 1 << 3, + kGCCallbackFlagCollectAllAvailableGarbage = 1 << 4, + kGCCallbackFlagCollectAllExternalMemory = 1 << 5, + kGCCallbackScheduleIdleGarbageCollection = 1 << 6, +}; + +using GCCallback = void (*)(GCType type, GCCallbackFlags flags); + +using InterruptCallback = void (*)(Isolate* isolate, void* data); + +/** + * This callback is invoked when the heap size is close to the heap limit and + * V8 is likely to abort with out-of-memory error. + * The callback can extend the heap limit by returning a value that is greater + * than the current_heap_limit. The initial heap limit is the limit that was + * set after heap setup. + */ +using NearHeapLimitCallback = size_t (*)(void* data, size_t current_heap_limit, + size_t initial_heap_limit); + +/** + * Callback function passed to SetUnhandledExceptionCallback. + */ +#if defined(V8_OS_WIN) +using UnhandledExceptionCallback = + int (*)(_EXCEPTION_POINTERS* exception_pointers); +#endif + +// --- Counters Callbacks --- + +using CounterLookupCallback = int* (*)(const char* name); + +using CreateHistogramCallback = void* (*)(const char* name, int min, int max, + size_t buckets); + +using AddHistogramSampleCallback = void (*)(void* histogram, int sample); + +// --- Exceptions --- + +using FatalErrorCallback = void (*)(const char* location, const char* message); + +struct OOMDetails { + bool is_heap_oom = false; + const char* detail = nullptr; +}; + +using OOMErrorCallback = void (*)(const char* location, + const OOMDetails& details); + +using MessageCallback = void (*)(Local message, Local data); + +// --- Tracing --- + +enum LogEventStatus : int { kStart = 0, kEnd = 1, kStamp = 2 }; +using LogEventCallback = void (*)(const char* name, + int /* LogEventStatus */ status); + +// --- Crashkeys Callback --- +enum class CrashKeyId { + kIsolateAddress, + kReadonlySpaceFirstPageAddress, + kMapSpaceFirstPageAddress V8_ENUM_DEPRECATE_SOON("Map space got removed"), + kOldSpaceFirstPageAddress, + kCodeRangeBaseAddress, + kCodeSpaceFirstPageAddress, + kDumpType, + kSnapshotChecksumCalculated, + kSnapshotChecksumExpected, +}; + +using AddCrashKeyCallback = void (*)(CrashKeyId id, const std::string& value); + +// --- Enter/Leave Script Callback --- +using BeforeCallEnteredCallback = void (*)(Isolate*); +using CallCompletedCallback = void (*)(Isolate*); + +// --- AllowCodeGenerationFromStrings callbacks --- + +/** + * Callback to check if code generation from strings is allowed. See + * Context::AllowCodeGenerationFromStrings. + */ +using AllowCodeGenerationFromStringsCallback = bool (*)(Local context, + Local source); + +struct ModifyCodeGenerationFromStringsResult { + // If true, proceed with the codegen algorithm. Otherwise, block it. + bool codegen_allowed = false; + // Overwrite the original source with this string, if present. + // Use the original source if empty. + // This field is considered only if codegen_allowed is true. + MaybeLocal modified_source; +}; + +/** + * Access type specification. + */ +enum AccessType { + ACCESS_GET, + ACCESS_SET, + ACCESS_HAS, + ACCESS_DELETE, + ACCESS_KEYS +}; + +// --- Failed Access Check Callback --- + +using FailedAccessCheckCallback = void (*)(Local target, + AccessType type, Local data); + +/** + * Callback to check if codegen is allowed from a source object, and convert + * the source to string if necessary. See: ModifyCodeGenerationFromStrings. + */ +using ModifyCodeGenerationFromStringsCallback = + ModifyCodeGenerationFromStringsResult (*)(Local context, + Local source); +using ModifyCodeGenerationFromStringsCallback2 = + ModifyCodeGenerationFromStringsResult (*)(Local context, + Local source, + bool is_code_like); + +// --- WebAssembly compilation callbacks --- +using ExtensionCallback = bool (*)(const FunctionCallbackInfo&); + +using AllowWasmCodeGenerationCallback = bool (*)(Local context, + Local source); + +// --- Callback for APIs defined on v8-supported objects, but implemented +// by the embedder. Example: WebAssembly.{compile|instantiate}Streaming --- +using ApiImplementationCallback = void (*)(const FunctionCallbackInfo&); + +// --- Callback for WebAssembly.compileStreaming --- +using WasmStreamingCallback = void (*)(const FunctionCallbackInfo&); + +enum class WasmAsyncSuccess { kSuccess, kFail }; + +// --- Callback called when async WebAssembly operations finish --- +using WasmAsyncResolvePromiseCallback = void (*)( + Isolate* isolate, Local context, Local resolver, + Local result, WasmAsyncSuccess success); + +// --- Callback for loading source map file for Wasm profiling support +using WasmLoadSourceMapCallback = Local (*)(Isolate* isolate, + const char* name); + +// --- Callback for checking if WebAssembly GC is enabled --- +// If the callback returns true, it will also enable Wasm stringrefs. +using WasmGCEnabledCallback = bool (*)(Local context); + +// --- Callback for checking if the SharedArrayBuffer constructor is enabled --- +using SharedArrayBufferConstructorEnabledCallback = + bool (*)(Local context); + +// --- Callback for checking if the compile hints magic comments are enabled --- +using JavaScriptCompileHintsMagicEnabledCallback = + bool (*)(Local context); + +/** + * HostImportModuleDynamicallyCallback is called when we + * require the embedder to load a module. This is used as part of the dynamic + * import syntax. + * + * The referrer contains metadata about the script/module that calls + * import. + * + * The specifier is the name of the module that should be imported. + * + * The import_assertions are import assertions for this request in the form: + * [key1, value1, key2, value2, ...] where the keys and values are of type + * v8::String. Note, unlike the FixedArray passed to ResolveModuleCallback and + * returned from ModuleRequest::GetImportAssertions(), this array does not + * contain the source Locations of the assertions. + * + * The embedder must compile, instantiate, evaluate the Module, and + * obtain its namespace object. + * + * The Promise returned from this function is forwarded to userland + * JavaScript. The embedder must resolve this promise with the module + * namespace object. In case of an exception, the embedder must reject + * this promise with the exception. If the promise creation itself + * fails (e.g. due to stack overflow), the embedder must propagate + * that exception by returning an empty MaybeLocal. + */ +using HostImportModuleDynamicallyWithImportAssertionsCallback = + MaybeLocal (*)(Local context, + Local referrer, + Local specifier, + Local import_assertions); +using HostImportModuleDynamicallyCallback = MaybeLocal (*)( + Local context, Local host_defined_options, + Local resource_name, Local specifier, + Local import_assertions); + +/** + * Callback for requesting a compile hint for a function from the embedder. The + * first parameter is the position of the function in source code and the second + * parameter is embedder data to be passed back. + */ +using CompileHintCallback = bool (*)(int, void*); + +/** + * HostInitializeImportMetaObjectCallback is called the first time import.meta + * is accessed for a module. Subsequent access will reuse the same value. + * + * The method combines two implementation-defined abstract operations into one: + * HostGetImportMetaProperties and HostFinalizeImportMeta. + * + * The embedder should use v8::Object::CreateDataProperty to add properties on + * the meta object. + */ +using HostInitializeImportMetaObjectCallback = void (*)(Local context, + Local module, + Local meta); + +/** + * HostCreateShadowRealmContextCallback is called each time a ShadowRealm is + * being constructed in the initiator_context. + * + * The method combines Context creation and implementation defined abstract + * operation HostInitializeShadowRealm into one. + * + * The embedder should use v8::Context::New or v8::Context:NewFromSnapshot to + * create a new context. If the creation fails, the embedder must propagate + * that exception by returning an empty MaybeLocal. + */ +using HostCreateShadowRealmContextCallback = + MaybeLocal (*)(Local initiator_context); + +/** + * PrepareStackTraceCallback is called when the stack property of an error is + * first accessed. The return value will be used as the stack value. If this + * callback is registed, the |Error.prepareStackTrace| API will be disabled. + * |sites| is an array of call sites, specified in + * https://v8.dev/docs/stack-trace-api + */ +using PrepareStackTraceCallback = MaybeLocal (*)(Local context, + Local error, + Local sites); + +} // namespace v8 + +#endif // INCLUDE_V8_ISOLATE_CALLBACKS_H_ diff --git a/android/arm64-v8a/include/v8/v8-container.h b/android/arm64-v8a/include/v8/v8-container.h new file mode 100644 index 00000000..ce068603 --- /dev/null +++ b/android/arm64-v8a/include/v8/v8-container.h @@ -0,0 +1,129 @@ +// 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_V8_CONTAINER_H_ +#define INCLUDE_V8_CONTAINER_H_ + +#include +#include + +#include "v8-local-handle.h" // NOLINT(build/include_directory) +#include "v8-object.h" // NOLINT(build/include_directory) +#include "v8config.h" // NOLINT(build/include_directory) + +namespace v8 { + +class Context; +class Isolate; + +/** + * An instance of the built-in array constructor (ECMA-262, 15.4.2). + */ +class V8_EXPORT Array : public Object { + public: + uint32_t Length() const; + + /** + * Creates a JavaScript array with the given length. If the length + * is negative the returned array will have length 0. + */ + static Local New(Isolate* isolate, int length = 0); + + /** + * Creates a JavaScript array out of a Local array in C++ + * with a known length. + */ + static Local New(Isolate* isolate, Local* elements, + size_t length); + V8_INLINE static Array* Cast(Value* value) { +#ifdef V8_ENABLE_CHECKS + CheckCast(value); +#endif + return static_cast(value); + } + + private: + Array(); + static void CheckCast(Value* obj); +}; + +/** + * An instance of the built-in Map constructor (ECMA-262, 6th Edition, 23.1.1). + */ +class V8_EXPORT Map : public Object { + public: + size_t Size() const; + void Clear(); + V8_WARN_UNUSED_RESULT MaybeLocal Get(Local context, + Local key); + V8_WARN_UNUSED_RESULT MaybeLocal Set(Local context, + Local key, + Local value); + V8_WARN_UNUSED_RESULT Maybe Has(Local context, + Local key); + V8_WARN_UNUSED_RESULT Maybe Delete(Local context, + Local key); + + /** + * Returns an array of length Size() * 2, where index N is the Nth key and + * index N + 1 is the Nth value. + */ + Local AsArray() const; + + /** + * Creates a new empty Map. + */ + static Local New(Isolate* isolate); + + V8_INLINE static Map* Cast(Value* value) { +#ifdef V8_ENABLE_CHECKS + CheckCast(value); +#endif + return static_cast(value); + } + + private: + Map(); + static void CheckCast(Value* obj); +}; + +/** + * An instance of the built-in Set constructor (ECMA-262, 6th Edition, 23.2.1). + */ +class V8_EXPORT Set : public Object { + public: + size_t Size() const; + void Clear(); + V8_WARN_UNUSED_RESULT MaybeLocal Add(Local context, + Local key); + V8_WARN_UNUSED_RESULT Maybe Has(Local context, + Local key); + V8_WARN_UNUSED_RESULT Maybe Delete(Local context, + Local key); + + /** + * Returns an array of the keys in this Set. + */ + Local AsArray() const; + + /** + * Creates a new empty Set. + */ + static Local New(Isolate* isolate); + + V8_INLINE static Set* Cast(Value* value) { +#ifdef V8_ENABLE_CHECKS + CheckCast(value); +#endif + return static_cast(value); + } + + private: + Set(); + static void CheckCast(Value* obj); +}; + +} // namespace v8 + +#endif // INCLUDE_V8_CONTAINER_H_ diff --git a/android/arm64-v8a/include/v8/v8-context.h b/android/arm64-v8a/include/v8/v8-context.h new file mode 100644 index 00000000..36cd43cb --- /dev/null +++ b/android/arm64-v8a/include/v8/v8-context.h @@ -0,0 +1,455 @@ +// 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_V8_CONTEXT_H_ +#define INCLUDE_V8_CONTEXT_H_ + +#include + +#include + +#include "v8-data.h" // NOLINT(build/include_directory) +#include "v8-local-handle.h" // NOLINT(build/include_directory) +#include "v8-maybe.h" // NOLINT(build/include_directory) +#include "v8-snapshot.h" // NOLINT(build/include_directory) +#include "v8config.h" // NOLINT(build/include_directory) + +namespace v8 { + +class Function; +class MicrotaskQueue; +class Object; +class ObjectTemplate; +class Value; +class String; + +/** + * A container for extension names. + */ +class V8_EXPORT ExtensionConfiguration { + public: + ExtensionConfiguration() : name_count_(0), names_(nullptr) {} + ExtensionConfiguration(int name_count, const char* names[]) + : name_count_(name_count), names_(names) {} + + const char** begin() const { return &names_[0]; } + const char** end() const { return &names_[name_count_]; } + + private: + const int name_count_; + const char** names_; +}; + +/** + * A sandboxed execution context with its own set of built-in objects + * and functions. + */ +class V8_EXPORT Context : public Data { + public: + /** + * Returns the global proxy object. + * + * Global proxy object is a thin wrapper whose prototype points to actual + * context's global object with the properties like Object, etc. This is done + * that way for security reasons (for more details see + * https://wiki.mozilla.org/Gecko:SplitWindow). + * + * Please note that changes to global proxy object prototype most probably + * would break VM---v8 expects only global object as a prototype of global + * proxy object. + */ + Local Global(); + + /** + * Detaches the global object from its context before + * the global object can be reused to create a new context. + */ + void DetachGlobal(); + + /** + * Creates a new context and returns a handle to the newly allocated + * context. + * + * \param isolate The isolate in which to create the context. + * + * \param extensions An optional extension configuration containing + * the extensions to be installed in the newly created context. + * + * \param global_template An optional object template from which the + * global object for the newly created context will be created. + * + * \param global_object An optional global object to be reused for + * the newly created context. This global object must have been + * created by a previous call to Context::New with the same global + * template. The state of the global object will be completely reset + * and only object identify will remain. + */ + static Local New( + Isolate* isolate, ExtensionConfiguration* extensions = nullptr, + MaybeLocal global_template = MaybeLocal(), + MaybeLocal global_object = MaybeLocal(), + DeserializeInternalFieldsCallback internal_fields_deserializer = + DeserializeInternalFieldsCallback(), + MicrotaskQueue* microtask_queue = nullptr); + + /** + * Create a new context from a (non-default) context snapshot. There + * is no way to provide a global object template since we do not create + * a new global object from template, but we can reuse a global object. + * + * \param isolate See v8::Context::New. + * + * \param context_snapshot_index The index of the context snapshot to + * deserialize from. Use v8::Context::New for the default snapshot. + * + * \param embedder_fields_deserializer Optional callback to deserialize + * internal fields. It should match the SerializeInternalFieldCallback used + * to serialize. + * + * \param extensions See v8::Context::New. + * + * \param global_object See v8::Context::New. + */ + static MaybeLocal FromSnapshot( + Isolate* isolate, size_t context_snapshot_index, + DeserializeInternalFieldsCallback embedder_fields_deserializer = + DeserializeInternalFieldsCallback(), + ExtensionConfiguration* extensions = nullptr, + MaybeLocal global_object = MaybeLocal(), + MicrotaskQueue* microtask_queue = nullptr); + + /** + * Returns an global object that isn't backed by an actual context. + * + * The global template needs to have access checks with handlers installed. + * If an existing global object is passed in, the global object is detached + * from its context. + * + * Note that this is different from a detached context where all accesses to + * the global proxy will fail. Instead, the access check handlers are invoked. + * + * It is also not possible to detach an object returned by this method. + * Instead, the access check handlers need to return nothing to achieve the + * same effect. + * + * It is possible, however, to create a new context from the global object + * returned by this method. + */ + static MaybeLocal NewRemoteContext( + Isolate* isolate, Local global_template, + MaybeLocal global_object = MaybeLocal()); + + /** + * Sets the security token for the context. To access an object in + * another context, the security tokens must match. + */ + void SetSecurityToken(Local token); + + /** Restores the security token to the default value. */ + void UseDefaultSecurityToken(); + + /** Returns the security token of this context.*/ + Local GetSecurityToken(); + + /** + * Enter this context. After entering a context, all code compiled + * and run is compiled and run in this context. If another context + * is already entered, this old context is saved so it can be + * restored when the new context is exited. + */ + void Enter(); + + /** + * Exit this context. Exiting the current context restores the + * context that was in place when entering the current context. + */ + void Exit(); + + /** + * Delegate to help with Deep freezing embedder-specific objects (such as + * JSApiObjects) that can not be frozen natively. + */ + class DeepFreezeDelegate { + public: + /** + * Performs embedder-specific operations to freeze the provided embedder + * object. The provided object *will* be frozen by DeepFreeze after this + * function returns, so only embedder-specific objects need to be frozen. + * This function *may not* create new JS objects or perform JS allocations. + * Any v8 objects reachable from the provided embedder object that should + * also be considered for freezing should be added to the children_out + * parameter. Returns true if the operation completed successfully. + */ + virtual bool FreezeEmbedderObjectAndGetChildren( + Local obj, std::vector>& children_out) = 0; + }; + + /** + * Attempts to recursively freeze all objects reachable from this context. + * Some objects (generators, iterators, non-const closures) can not be frozen + * and will cause this method to throw an error. An optional delegate can be + * provided to help freeze embedder-specific objects. + * + * Freezing occurs in two steps: + * 1. "Marking" where we iterate through all objects reachable by this + * context, accumulating a list of objects that need to be frozen and + * looking for objects that can't be frozen. This step is separated because + * it is more efficient when we can assume there is no garbage collection. + * 2. "Freezing" where we go through the list of objects and freezing them. + * This effectively requires copying them so it may trigger garbage + * collection. + */ + Maybe DeepFreeze(DeepFreezeDelegate* delegate = nullptr); + + /** Returns the isolate associated with a current context. */ + Isolate* GetIsolate(); + + /** Returns the microtask queue associated with a current context. */ + MicrotaskQueue* GetMicrotaskQueue(); + + /** Sets the microtask queue associated with the current context. */ + void SetMicrotaskQueue(MicrotaskQueue* queue); + + /** + * The field at kDebugIdIndex used to be reserved for the inspector. + * It now serves no purpose. + */ + enum EmbedderDataFields { kDebugIdIndex = 0 }; + + /** + * Return the number of fields allocated for embedder data. + */ + uint32_t GetNumberOfEmbedderDataFields(); + + /** + * Gets the embedder data with the given index, which must have been set by a + * previous call to SetEmbedderData with the same index. + */ + V8_INLINE Local GetEmbedderData(int index); + + /** + * Gets the binding object used by V8 extras. Extra natives get a reference + * to this object and can use it to "export" functionality by adding + * properties. Extra natives can also "import" functionality by accessing + * properties added by the embedder using the V8 API. + */ + Local GetExtrasBindingObject(); + + /** + * Sets the embedder data with the given index, growing the data as + * needed. Note that index 0 currently has a special meaning for Chrome's + * debugger. + */ + void SetEmbedderData(int index, Local value); + + /** + * Gets a 2-byte-aligned native pointer from the embedder data with the given + * index, which must have been set by a previous call to + * SetAlignedPointerInEmbedderData with the same index. Note that index 0 + * currently has a special meaning for Chrome's debugger. + */ + V8_INLINE void* GetAlignedPointerFromEmbedderData(int index); + + /** + * Sets a 2-byte-aligned native pointer in the embedder data with the given + * index, growing the data as needed. Note that index 0 currently has a + * special meaning for Chrome's debugger. + */ + void SetAlignedPointerInEmbedderData(int index, void* value); + + /** + * Control whether code generation from strings is allowed. Calling + * this method with false will disable 'eval' and the 'Function' + * constructor for code running in this context. If 'eval' or the + * 'Function' constructor are used an exception will be thrown. + * + * If code generation from strings is not allowed the + * V8::AllowCodeGenerationFromStrings callback will be invoked if + * set before blocking the call to 'eval' or the 'Function' + * constructor. If that callback returns true, the call will be + * allowed, otherwise an exception will be thrown. If no callback is + * set an exception will be thrown. + */ + void AllowCodeGenerationFromStrings(bool allow); + + /** + * Returns true if code generation from strings is allowed for the context. + * For more details see AllowCodeGenerationFromStrings(bool) documentation. + */ + bool IsCodeGenerationFromStringsAllowed() const; + + /** + * Sets the error description for the exception that is thrown when + * code generation from strings is not allowed and 'eval' or the 'Function' + * constructor are called. + */ + void SetErrorMessageForCodeGenerationFromStrings(Local message); + + /** + * Sets the error description for the exception that is thrown when + * wasm code generation is not allowed. + */ + void SetErrorMessageForWasmCodeGeneration(Local message); + + /** + * Return data that was previously attached to the context snapshot via + * SnapshotCreator, and removes the reference to it. + * Repeated call with the same index returns an empty MaybeLocal. + */ + template + V8_INLINE MaybeLocal GetDataFromSnapshotOnce(size_t index); + + /** + * If callback is set, abort any attempt to execute JavaScript in this + * context, call the specified callback, and throw an exception. + * To unset abort, pass nullptr as callback. + */ + using AbortScriptExecutionCallback = void (*)(Isolate* isolate, + Local context); + void SetAbortScriptExecution(AbortScriptExecutionCallback callback); + + /** + * Returns the value that was set or restored by + * SetContinuationPreservedEmbedderData(), if any. + */ + Local GetContinuationPreservedEmbedderData() const; + + /** + * Sets a value that will be stored on continuations and reset while the + * continuation runs. + */ + void SetContinuationPreservedEmbedderData(Local context); + + /** + * Set or clear hooks to be invoked for promise lifecycle operations. + * To clear a hook, set it to an empty v8::Function. Each function will + * receive the observed promise as the first argument. If a chaining + * operation is used on a promise, the init will additionally receive + * the parent promise as the second argument. + */ + void SetPromiseHooks(Local init_hook, Local before_hook, + Local after_hook, + Local resolve_hook); + + bool HasTemplateLiteralObject(Local object); + /** + * Stack-allocated class which sets the execution context for all + * operations executed within a local scope. + */ + class V8_NODISCARD Scope { + public: + explicit V8_INLINE Scope(Local context) : context_(context) { + context_->Enter(); + } + V8_INLINE ~Scope() { context_->Exit(); } + + private: + Local context_; + }; + + /** + * Stack-allocated class to support the backup incumbent settings object + * stack. + * https://html.spec.whatwg.org/multipage/webappapis.html#backup-incumbent-settings-object-stack + */ + class V8_EXPORT V8_NODISCARD BackupIncumbentScope final { + public: + /** + * |backup_incumbent_context| is pushed onto the backup incumbent settings + * object stack. + */ + explicit BackupIncumbentScope(Local backup_incumbent_context); + ~BackupIncumbentScope(); + + private: + friend class internal::Isolate; + + uintptr_t JSStackComparableAddressPrivate() const { + return js_stack_comparable_address_; + } + + Local backup_incumbent_context_; + uintptr_t js_stack_comparable_address_ = 0; + const BackupIncumbentScope* prev_ = nullptr; + }; + + V8_INLINE static Context* Cast(Data* data); + + private: + friend class Value; + friend class Script; + friend class Object; + friend class Function; + + static void CheckCast(Data* obj); + + internal::Address* GetDataFromSnapshotOnce(size_t index); + Local SlowGetEmbedderData(int index); + void* SlowGetAlignedPointerFromEmbedderData(int index); +}; + +// --- Implementation --- + +Local Context::GetEmbedderData(int index) { +#ifndef V8_ENABLE_CHECKS + using A = internal::Address; + using I = internal::Internals; + A ctx = internal::ValueHelper::ValueAsAddress(this); + A embedder_data = + I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset); + int value_offset = + I::kEmbedderDataArrayHeaderSize + (I::kEmbedderDataSlotSize * index); + A value = I::ReadRawField(embedder_data, value_offset); +#ifdef V8_COMPRESS_POINTERS + // We read the full pointer value and then decompress it in order to avoid + // dealing with potential endiannes issues. + value = I::DecompressTaggedField(embedder_data, static_cast(value)); +#endif + + auto isolate = reinterpret_cast( + internal::IsolateFromNeverReadOnlySpaceObject(ctx)); + return Local::New(isolate, value); +#else + return SlowGetEmbedderData(index); +#endif +} + +void* Context::GetAlignedPointerFromEmbedderData(int index) { +#if !defined(V8_ENABLE_CHECKS) + using A = internal::Address; + using I = internal::Internals; + A ctx = internal::ValueHelper::ValueAsAddress(this); + A embedder_data = + I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset); + int value_offset = I::kEmbedderDataArrayHeaderSize + + (I::kEmbedderDataSlotSize * index) + + I::kEmbedderDataSlotExternalPointerOffset; + Isolate* isolate = I::GetIsolateForSandbox(ctx); + return reinterpret_cast( + I::ReadExternalPointerField( + isolate, embedder_data, value_offset)); +#else + return SlowGetAlignedPointerFromEmbedderData(index); +#endif +} + +template +MaybeLocal Context::GetDataFromSnapshotOnce(size_t index) { + auto slot = GetDataFromSnapshotOnce(index); + if (slot) { + internal::PerformCastCheck(internal::ValueHelper::SlotAsValue(slot)); + } + return Local::FromSlot(slot); +} + +Context* Context::Cast(v8::Data* data) { +#ifdef V8_ENABLE_CHECKS + CheckCast(data); +#endif + return static_cast(data); +} + +} // namespace v8 + +#endif // INCLUDE_V8_CONTEXT_H_ diff --git a/android/arm64-v8a/include/v8/v8-cppgc.h b/android/arm64-v8a/include/v8/v8-cppgc.h index fba35f71..4a457027 100644 --- a/android/arm64-v8a/include/v8/v8-cppgc.h +++ b/android/arm64-v8a/include/v8/v8-cppgc.h @@ -12,10 +12,10 @@ #include "cppgc/common.h" #include "cppgc/custom-space.h" #include "cppgc/heap-statistics.h" -#include "cppgc/internal/write-barrier.h" #include "cppgc/visitor.h" -#include "v8-internal.h" // NOLINT(build/include_directory) -#include "v8.h" // NOLINT(build/include_directory) +#include "v8-internal.h" // NOLINT(build/include_directory) +#include "v8-platform.h" // NOLINT(build/include_directory) +#include "v8-traced-handle.h" // NOLINT(build/include_directory) namespace cppgc { class AllocationHandle; @@ -24,10 +24,14 @@ class HeapHandle; namespace v8 { +class Object; + namespace internal { class CppHeap; } // namespace internal +class CustomSpaceStatisticsReceiver; + /** * Describes how V8 wrapper objects maintain references to garbage-collected C++ * objects. @@ -73,15 +77,37 @@ struct WrapperDescriptor final { }; struct V8_EXPORT CppHeapCreateParams { + CppHeapCreateParams( + std::vector> custom_spaces, + WrapperDescriptor wrapper_descriptor) + : custom_spaces(std::move(custom_spaces)), + wrapper_descriptor(wrapper_descriptor) {} + CppHeapCreateParams(const CppHeapCreateParams&) = delete; CppHeapCreateParams& operator=(const CppHeapCreateParams&) = delete; std::vector> custom_spaces; WrapperDescriptor wrapper_descriptor; + /** + * Specifies which kind of marking are supported by the heap. The type may be + * further reduced via runtime flags when attaching the heap to an Isolate. + */ + cppgc::Heap::MarkingType marking_support = + cppgc::Heap::MarkingType::kIncrementalAndConcurrent; + /** + * Specifies which kind of sweeping is supported by the heap. The type may be + * further reduced via runtime flags when attaching the heap to an Isolate. + */ + cppgc::Heap::SweepingType sweeping_support = + cppgc::Heap::SweepingType::kIncrementalAndConcurrent; }; /** * A heap for allocating managed C++ objects. + * + * Similar to v8::Isolate, the heap may only be accessed from one thread at a + * time. The heap may be used from different threads using the + * v8::Locker/v8::Unlocker APIs which is different from generic Oilpan. */ class V8_EXPORT CppHeap { public: @@ -119,6 +145,16 @@ 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 @@ -133,6 +169,14 @@ class V8_EXPORT CppHeap { */ void CollectGarbageForTesting(cppgc::EmbedderStackState stack_state); + /** + * Performs a stop-the-world minor garbage collection for testing purposes. + * + * \param stack_state The stack state to assume for the garbage collection. + */ + void CollectGarbageInYoungGenerationForTesting( + cppgc::EmbedderStackState stack_state); + private: CppHeap() = default; @@ -142,6 +186,7 @@ class V8_EXPORT CppHeap { class JSVisitor : public cppgc::Visitor { public: explicit JSVisitor(cppgc::Visitor::Key key) : cppgc::Visitor(key) {} + ~JSVisitor() override = default; void Trace(const TracedReferenceBase& ref) { if (ref.IsEmptyThreadSafe()) return; @@ -155,126 +200,23 @@ class JSVisitor : public cppgc::Visitor { }; /** - * **DO NOT USE: Use the appropriate managed types.** + * Provided as input to `CppHeap::CollectCustomSpaceStatisticsAtLastGC()`. * - * Consistency helpers that aid in maintaining a consistent internal state of - * the garbage collector. + * Its method is invoked with the results of the statistic collection. */ -class V8_EXPORT JSHeapConsistency final { +class CustomSpaceStatisticsReceiver { public: - using WriteBarrierParams = cppgc::internal::WriteBarrier::Params; - using WriteBarrierType = cppgc::internal::WriteBarrier::Type; - + virtual ~CustomSpaceStatisticsReceiver() = default; /** - * Gets the required write barrier type for a specific write. + * Reports the size of a space at the last GC. It is called for each space + * that was requested in `CollectCustomSpaceStatisticsAtLastGC()`. * - * Note: Handling for C++ to JS references. - * - * \param ref The reference being written to. - * \param params Parameters that may be used for actual write barrier calls. - * Only filled if return value indicates that a write barrier is needed. The - * contents of the `params` are an implementation detail. - * \param callback Callback returning the corresponding heap handle. The - * callback is only invoked if the heap cannot otherwise be figured out. The - * callback must not allocate. - * \returns whether a write barrier is needed and which barrier to invoke. + * \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. */ - template - static V8_INLINE WriteBarrierType - GetWriteBarrierType(const TracedReferenceBase& ref, - WriteBarrierParams& params, HeapHandleCallback callback) { - if (ref.IsEmpty()) return WriteBarrierType::kNone; - - if (V8_LIKELY(!cppgc::internal::WriteBarrier:: - IsAnyIncrementalOrConcurrentMarking())) { - return cppgc::internal::WriteBarrier::Type::kNone; - } - cppgc::HeapHandle& handle = callback(); - if (!cppgc::subtle::HeapState::IsMarking(handle)) { - return cppgc::internal::WriteBarrier::Type::kNone; - } - params.heap = &handle; -#if V8_ENABLE_CHECKS - params.type = cppgc::internal::WriteBarrier::Type::kMarking; -#endif // !V8_ENABLE_CHECKS - return cppgc::internal::WriteBarrier::Type::kMarking; - } - - /** - * Gets the required write barrier type for a specific write. - * - * Note: Handling for JS to C++ references. - * - * \param wrapper The wrapper that has been written into. - * \param wrapper_index The wrapper index in `wrapper` that has been written - * into. - * \param wrappable The value that was written. - * \param params Parameters that may be used for actual write barrier calls. - * Only filled if return value indicates that a write barrier is needed. The - * contents of the `params` are an implementation detail. - * \param callback Callback returning the corresponding heap handle. The - * callback is only invoked if the heap cannot otherwise be figured out. The - * callback must not allocate. - * \returns whether a write barrier is needed and which barrier to invoke. - */ - template - static V8_INLINE WriteBarrierType GetWriteBarrierType( - v8::Local& wrapper, int wrapper_index, const void* wrappable, - WriteBarrierParams& params, HeapHandleCallback callback) { -#if V8_ENABLE_CHECKS - CheckWrapper(wrapper, wrapper_index, wrappable); -#endif // V8_ENABLE_CHECKS - return cppgc::internal::WriteBarrier:: - GetWriteBarrierTypeForExternallyReferencedObject(wrappable, params, - callback); - } - - /** - * Conservative Dijkstra-style write barrier that processes an object if it - * has not yet been processed. - * - * \param params The parameters retrieved from `GetWriteBarrierType()`. - * \param ref The reference being written to. - */ - static V8_INLINE void DijkstraMarkingBarrier(const WriteBarrierParams& params, - cppgc::HeapHandle& heap_handle, - const TracedReferenceBase& ref) { - cppgc::internal::WriteBarrier::CheckParams(WriteBarrierType::kMarking, - params); - DijkstraMarkingBarrierSlow(heap_handle, ref); - } - - /** - * Conservative Dijkstra-style write barrier that processes an object if it - * has not yet been processed. - * - * \param params The parameters retrieved from `GetWriteBarrierType()`. - * \param object The pointer to the object. May be an interior pointer to a - * an interface of the actual object. - */ - static V8_INLINE void DijkstraMarkingBarrier(const WriteBarrierParams& params, - cppgc::HeapHandle& heap_handle, - const void* object) { - cppgc::internal::WriteBarrier::DijkstraMarkingBarrier(params, object); - } - - /** - * Generational barrier for maintaining consistency when running with multiple - * generations. - * - * \param params The parameters retrieved from `GetWriteBarrierType()`. - * \param ref The reference being written to. - */ - static V8_INLINE void GenerationalBarrier(const WriteBarrierParams& params, - const TracedReferenceBase& ref) {} - - private: - JSHeapConsistency() = delete; - - static void CheckWrapper(v8::Local&, int, const void*); - - static void DijkstraMarkingBarrierSlow(cppgc::HeapHandle&, - const TracedReferenceBase& ref); + virtual void AllocatedBytes(cppgc::CustomSpaceIndex space_index, + size_t bytes) = 0; }; } // namespace v8 @@ -283,8 +225,13 @@ namespace cppgc { template struct TraceTrait> { - static void Trace(Visitor* visitor, const v8::TracedReference* self) { - static_cast(visitor)->Trace(*self); + static cppgc::TraceDescriptor GetTraceDescriptor(const void* self) { + return {nullptr, Trace}; + } + + static void Trace(Visitor* visitor, const void* self) { + static_cast(visitor)->Trace( + *static_cast*>(self)); } }; diff --git a/android/arm64-v8a/include/v8/v8-data.h b/android/arm64-v8a/include/v8/v8-data.h new file mode 100644 index 00000000..fc4dea92 --- /dev/null +++ b/android/arm64-v8a/include/v8/v8-data.h @@ -0,0 +1,80 @@ +// 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_V8_DATA_H_ +#define INCLUDE_V8_DATA_H_ + +#include "v8-local-handle.h" // NOLINT(build/include_directory) +#include "v8config.h" // NOLINT(build/include_directory) + +namespace v8 { + +class Context; + +/** + * The superclass of objects that can reside on V8's heap. + */ +class V8_EXPORT Data { + public: + /** + * Returns true if this data is a |v8::Value|. + */ + bool IsValue() const; + + /** + * Returns true if this data is a |v8::Module|. + */ + bool IsModule() const; + + /** + * Returns tru if this data is a |v8::FixedArray| + */ + bool IsFixedArray() const; + + /** + * Returns true if this data is a |v8::Private|. + */ + bool IsPrivate() const; + + /** + * Returns true if this data is a |v8::ObjectTemplate|. + */ + bool IsObjectTemplate() const; + + /** + * Returns true if this data is a |v8::FunctionTemplate|. + */ + bool IsFunctionTemplate() const; + + /** + * Returns true if this data is a |v8::Context|. + */ + bool IsContext() const; + + private: + Data() = delete; +}; + +/** + * A fixed-sized array with elements of type Data. + */ +class V8_EXPORT FixedArray : public Data { + public: + int Length() const; + Local Get(Local context, int i) const; + + V8_INLINE static FixedArray* Cast(Data* data) { +#ifdef V8_ENABLE_CHECKS + CheckCast(data); +#endif + return reinterpret_cast(data); + } + + private: + static void CheckCast(Data* obj); +}; + +} // namespace v8 + +#endif // INCLUDE_V8_DATA_H_ diff --git a/android/arm64-v8a/include/v8/v8-date.h b/android/arm64-v8a/include/v8/v8-date.h new file mode 100644 index 00000000..8d82ccc9 --- /dev/null +++ b/android/arm64-v8a/include/v8/v8-date.h @@ -0,0 +1,48 @@ +// 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_V8_DATE_H_ +#define INCLUDE_V8_DATE_H_ + +#include "v8-local-handle.h" // NOLINT(build/include_directory) +#include "v8-object.h" // NOLINT(build/include_directory) +#include "v8config.h" // NOLINT(build/include_directory) + +namespace v8 { + +class Context; + +/** + * An instance of the built-in Date constructor (ECMA-262, 15.9). + */ +class V8_EXPORT Date : public Object { + public: + static V8_WARN_UNUSED_RESULT MaybeLocal New(Local context, + double time); + + /** + * A specialization of Value::NumberValue that is more efficient + * because we know the structure of this object. + */ + double ValueOf() const; + + /** + * Generates ISO string representation. + */ + v8::Local ToISOString() const; + + V8_INLINE static Date* Cast(Value* value) { +#ifdef V8_ENABLE_CHECKS + CheckCast(value); +#endif + return static_cast(value); + } + + private: + static void CheckCast(Value* obj); +}; + +} // namespace v8 + +#endif // INCLUDE_V8_DATE_H_ diff --git a/android/arm64-v8a/include/v8/v8-debug.h b/android/arm64-v8a/include/v8/v8-debug.h new file mode 100644 index 00000000..52255f37 --- /dev/null +++ b/android/arm64-v8a/include/v8/v8-debug.h @@ -0,0 +1,168 @@ +// 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_V8_DEBUG_H_ +#define INCLUDE_V8_DEBUG_H_ + +#include + +#include "v8-script.h" // NOLINT(build/include_directory) +#include "v8config.h" // NOLINT(build/include_directory) + +namespace v8 { + +class Isolate; +class String; + +/** + * A single JavaScript stack frame. + */ +class V8_EXPORT StackFrame { + public: + /** + * Returns the source location, 0-based, for the associated function call. + */ + Location GetLocation() const; + + /** + * Returns the number, 1-based, of the line for the associate function call. + * This method will return Message::kNoLineNumberInfo if it is unable to + * retrieve the line number, or if kLineNumber was not passed as an option + * when capturing the StackTrace. + */ + int GetLineNumber() const { return GetLocation().GetLineNumber() + 1; } + + /** + * Returns the 1-based column offset on the line for the associated function + * call. + * This method will return Message::kNoColumnInfo if it is unable to retrieve + * the column number, or if kColumnOffset was not passed as an option when + * capturing the StackTrace. + */ + int GetColumn() const { return GetLocation().GetColumnNumber() + 1; } + + /** + * Returns the id of the script for the function for this StackFrame. + * This method will return Message::kNoScriptIdInfo if it is unable to + * retrieve the script id, or if kScriptId was not passed as an option when + * capturing the StackTrace. + */ + int GetScriptId() const; + + /** + * Returns the name of the resource that contains the script for the + * function for this StackFrame. + */ + Local GetScriptName() const; + + /** + * Returns the name of the resource that contains the script for the + * function for this StackFrame or sourceURL value if the script name + * is undefined and its source ends with //# sourceURL=... string or + * deprecated //@ sourceURL=... string. + */ + Local GetScriptNameOrSourceURL() const; + + /** + * Returns the source of the script for the function for this StackFrame. + */ + Local GetScriptSource() const; + + /** + * Returns the source mapping URL (if one is present) of the script for + * the function for this StackFrame. + */ + Local GetScriptSourceMappingURL() const; + + /** + * Returns the name of the function associated with this stack frame. + */ + Local GetFunctionName() const; + + /** + * Returns whether or not the associated function is compiled via a call to + * eval(). + */ + bool IsEval() const; + + /** + * Returns whether or not the associated function is called as a + * constructor via "new". + */ + bool IsConstructor() const; + + /** + * Returns whether or not the associated functions is defined in wasm. + */ + bool IsWasm() const; + + /** + * Returns whether or not the associated function is defined by the user. + */ + bool IsUserJavaScript() const; +}; + +/** + * Representation of a JavaScript stack trace. The information collected is a + * snapshot of the execution stack and the information remains valid after + * execution continues. + */ +class V8_EXPORT StackTrace { + public: + /** + * Flags that determine what information is placed captured for each + * StackFrame when grabbing the current stack trace. + * Note: these options are deprecated and we always collect all available + * information (kDetailed). + */ + enum StackTraceOptions { + kLineNumber = 1, + kColumnOffset = 1 << 1 | kLineNumber, + kScriptName = 1 << 2, + kFunctionName = 1 << 3, + kIsEval = 1 << 4, + kIsConstructor = 1 << 5, + kScriptNameOrSourceURL = 1 << 6, + kScriptId = 1 << 7, + kExposeFramesAcrossSecurityOrigins = 1 << 8, + kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName, + kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL + }; + + /** + * Returns a StackFrame at a particular index. + */ + Local GetFrame(Isolate* isolate, uint32_t index) const; + + /** + * Returns the number of StackFrames. + */ + int GetFrameCount() const; + + /** + * Grab a snapshot of the current JavaScript execution stack. + * + * \param frame_limit The maximum number of stack frames we want to capture. + * \param options Enumerates the set of things we will capture for each + * StackFrame. + */ + static Local CurrentStackTrace( + Isolate* isolate, int frame_limit, StackTraceOptions options = kDetailed); + + /** + * Returns the first valid script name or source URL starting at the top of + * the JS stack. The returned string is either an empty handle if no script + * name/url was found or a non-zero-length string. + * + * This method is equivalent to calling StackTrace::CurrentStackTrace and + * walking the resulting frames from the beginning until a non-empty script + * name/url is found. The difference is that this method won't allocate + * a stack trace. + */ + static Local CurrentScriptNameOrSourceURL(Isolate* isolate); +}; + +} // namespace v8 + +#endif // INCLUDE_V8_DEBUG_H_ diff --git a/android/arm64-v8a/include/v8/v8-embedder-heap.h b/android/arm64-v8a/include/v8/v8-embedder-heap.h new file mode 100644 index 00000000..c37dadf7 --- /dev/null +++ b/android/arm64-v8a/include/v8/v8-embedder-heap.h @@ -0,0 +1,66 @@ +// 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_V8_EMBEDDER_HEAP_H_ +#define INCLUDE_V8_EMBEDDER_HEAP_H_ + +#include "v8-traced-handle.h" // NOLINT(build/include_directory) +#include "v8config.h" // NOLINT(build/include_directory) + +namespace v8 { + +class Isolate; +class Value; + +/** + * Handler for embedder roots on non-unified heap garbage collections. + */ +class V8_EXPORT EmbedderRootsHandler { + public: + virtual ~EmbedderRootsHandler() = default; + + /** + * Returns true if the |TracedReference| handle should be considered as root + * for the currently running non-tracing garbage collection and false + * otherwise. The default implementation will keep all |TracedReference| + * references as roots. + * + * If this returns false, then V8 may decide that the object referred to by + * such a handle is reclaimed. In that case, V8 calls |ResetRoot()| for the + * |TracedReference|. + * + * Note that the `handle` is different from the handle that the embedder holds + * for retaining the object. The embedder may use |WrapperClassId()| to + * distinguish cases where it wants handles to be treated as roots from not + * being treated as roots. + * + * The concrete implementations must be thread-safe. + */ + virtual bool IsRoot(const v8::TracedReference& handle) = 0; + + /** + * Used in combination with |IsRoot|. Called by V8 when an + * object that is backed by a handle is reclaimed by a non-tracing garbage + * collection. It is up to the embedder to reset the original handle. + * + * Note that the |handle| is different from the handle that the embedder holds + * for retaining the object. It is up to the embedder to find the original + * handle via the object or class id. + */ + virtual void ResetRoot(const v8::TracedReference& handle) = 0; + + /** + * Similar to |ResetRoot()|, but opportunistic. The function is called in + * parallel for different handles and as such must be thread-safe. In case, + * |false| is returned, |ResetRoot()| will be recalled for the same handle. + */ + virtual bool TryResetRoot(const v8::TracedReference& handle) { + ResetRoot(handle); + return true; + } +}; + +} // namespace v8 + +#endif // INCLUDE_V8_EMBEDDER_HEAP_H_ diff --git a/android/arm64-v8a/include/v8/v8-embedder-state-scope.h b/android/arm64-v8a/include/v8/v8-embedder-state-scope.h new file mode 100644 index 00000000..d8a3b08d --- /dev/null +++ b/android/arm64-v8a/include/v8/v8-embedder-state-scope.h @@ -0,0 +1,51 @@ +// 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_V8_EMBEDDER_STATE_SCOPE_H_ +#define INCLUDE_V8_EMBEDDER_STATE_SCOPE_H_ + +#include + +#include "v8-context.h" // NOLINT(build/include_directory) +#include "v8-internal.h" // NOLINT(build/include_directory) +#include "v8-local-handle.h" // NOLINT(build/include_directory) + +namespace v8 { + +namespace internal { +class EmbedderState; +} // namespace internal + +// A StateTag represents a possible state of the embedder. +enum class EmbedderStateTag : uint8_t { + // reserved + EMPTY = 0, + OTHER = 1, + // embedder can define any state after +}; + +// A stack-allocated class that manages an embedder state on the isolate. +// After an EmbedderState scope has been created, a new embedder state will be +// pushed on the isolate stack. +class V8_EXPORT EmbedderStateScope { + public: + EmbedderStateScope(Isolate* isolate, Local context, + EmbedderStateTag tag); + + ~EmbedderStateScope(); + + private: + // Declaring operator new and delete as deleted is not spec compliant. + // Therefore declare them private instead to disable dynamic alloc + void* operator new(size_t size); + void* operator new[](size_t size); + void operator delete(void*, size_t); + void operator delete[](void*, size_t); + + std::unique_ptr embedder_state_; +}; + +} // namespace v8 + +#endif // INCLUDE_V8_EMBEDDER_STATE_SCOPE_H_ diff --git a/android/arm64-v8a/include/v8/v8-exception.h b/android/arm64-v8a/include/v8/v8-exception.h new file mode 100644 index 00000000..bc058e3f --- /dev/null +++ b/android/arm64-v8a/include/v8/v8-exception.h @@ -0,0 +1,217 @@ +// 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_V8_EXCEPTION_H_ +#define INCLUDE_V8_EXCEPTION_H_ + +#include + +#include "v8-local-handle.h" // NOLINT(build/include_directory) +#include "v8config.h" // NOLINT(build/include_directory) + +namespace v8 { + +class Context; +class Isolate; +class Message; +class StackTrace; +class String; +class Value; + +namespace internal { +class Isolate; +class ThreadLocalTop; +} // namespace internal + +/** + * Create new error objects by calling the corresponding error object + * constructor with the message. + */ +class V8_EXPORT Exception { + public: + static Local RangeError(Local message); + static Local ReferenceError(Local message); + static Local SyntaxError(Local message); + static Local TypeError(Local message); + static Local WasmCompileError(Local message); + static Local WasmLinkError(Local message); + static Local WasmRuntimeError(Local message); + static Local Error(Local message); + + /** + * Creates an error message for the given exception. + * Will try to reconstruct the original stack trace from the exception value, + * or capture the current stack trace if not available. + */ + static Local CreateMessage(Isolate* isolate, Local exception); + + /** + * Returns the original stack trace that was captured at the creation time + * of a given exception, or an empty handle if not available. + */ + static Local GetStackTrace(Local exception); +}; + +/** + * An external exception handler. + */ +class V8_EXPORT TryCatch { + public: + /** + * Creates a new try/catch block and registers it with v8. Note that + * all TryCatch blocks should be stack allocated because the memory + * location itself is compared against JavaScript try/catch blocks. + */ + explicit TryCatch(Isolate* isolate); + + /** + * Unregisters and deletes this try/catch block. + */ + ~TryCatch(); + + /** + * Returns true if an exception has been caught by this try/catch block. + */ + bool HasCaught() const; + + /** + * For certain types of exceptions, it makes no sense to continue execution. + * + * If CanContinue returns false, the correct action is to perform any C++ + * cleanup needed and then return. If CanContinue returns false and + * HasTerminated returns true, it is possible to call + * CancelTerminateExecution in order to continue calling into the engine. + */ + bool CanContinue() const; + + /** + * Returns true if an exception has been caught due to script execution + * being terminated. + * + * There is no JavaScript representation of an execution termination + * exception. Such exceptions are thrown when the TerminateExecution + * methods are called to terminate a long-running script. + * + * If such an exception has been thrown, HasTerminated will return true, + * indicating that it is possible to call CancelTerminateExecution in order + * to continue calling into the engine. + */ + bool HasTerminated() const; + + /** + * Throws the exception caught by this TryCatch in a way that avoids + * it being caught again by this same TryCatch. As with ThrowException + * it is illegal to execute any JavaScript operations after calling + * ReThrow; the caller must return immediately to where the exception + * is caught. + */ + Local ReThrow(); + + /** + * Returns the exception caught by this try/catch block. If no exception has + * been caught an empty handle is returned. + */ + Local Exception() const; + + /** + * Returns the .stack property of an object. If no .stack + * property is present an empty handle is returned. + */ + V8_WARN_UNUSED_RESULT static MaybeLocal StackTrace( + Local context, Local exception); + + /** + * Returns the .stack property of the thrown object. If no .stack property is + * present or if this try/catch block has not caught an exception, an empty + * handle is returned. + */ + V8_WARN_UNUSED_RESULT MaybeLocal StackTrace( + Local context) const; + + /** + * Returns the message associated with this exception. If there is + * no message associated an empty handle is returned. + */ + Local Message() const; + + /** + * Clears any exceptions that may have been caught by this try/catch block. + * After this method has been called, HasCaught() will return false. Cancels + * the scheduled exception if it is caught and ReThrow() is not called before. + * + * It is not necessary to clear a try/catch block before using it again; if + * another exception is thrown the previously caught exception will just be + * overwritten. However, it is often a good idea since it makes it easier + * to determine which operation threw a given exception. + */ + void Reset(); + + /** + * Set verbosity of the external exception handler. + * + * By default, exceptions that are caught by an external exception + * handler are not reported. Call SetVerbose with true on an + * external exception handler to have exceptions caught by the + * handler reported as if they were not caught. + */ + void SetVerbose(bool value); + + /** + * Returns true if verbosity is enabled. + */ + bool IsVerbose() const; + + /** + * Set whether or not this TryCatch should capture a Message object + * which holds source information about where the exception + * occurred. True by default. + */ + void SetCaptureMessage(bool value); + + TryCatch(const TryCatch&) = delete; + void operator=(const TryCatch&) = delete; + + private: + // Declaring operator new and delete as deleted is not spec compliant. + // Therefore declare them private instead to disable dynamic alloc + void* operator new(size_t size); + void* operator new[](size_t size); + void operator delete(void*, size_t); + void operator delete[](void*, size_t); + + /** + * There are cases when the raw address of C++ TryCatch object cannot be + * used for comparisons with addresses into the JS stack. The cases are: + * 1) ARM, ARM64 and MIPS simulators which have separate JS stack. + * 2) Address sanitizer allocates local C++ object in the heap when + * UseAfterReturn mode is enabled. + * This method returns address that can be used for comparisons with + * addresses into the JS stack. When neither simulator nor ASAN's + * UseAfterReturn is enabled, then the address returned will be the address + * of the C++ try catch handler itself. + */ + internal::Address JSStackComparableAddressPrivate() { + return js_stack_comparable_address_; + } + + void ResetInternal(); + + internal::Isolate* i_isolate_; + TryCatch* next_; + void* exception_; + void* message_obj_; + internal::Address js_stack_comparable_address_; + bool is_verbose_ : 1; + bool can_continue_ : 1; + bool capture_message_ : 1; + bool rethrow_ : 1; + bool has_terminated_ : 1; + + friend class internal::Isolate; + friend class internal::ThreadLocalTop; +}; + +} // namespace v8 + +#endif // INCLUDE_V8_EXCEPTION_H_ diff --git a/android/arm64-v8a/include/v8/v8-extension.h b/android/arm64-v8a/include/v8/v8-extension.h new file mode 100644 index 00000000..0705e2af --- /dev/null +++ b/android/arm64-v8a/include/v8/v8-extension.h @@ -0,0 +1,62 @@ +// 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_V8_EXTENSION_H_ +#define INCLUDE_V8_EXTENSION_H_ + +#include + +#include "v8-local-handle.h" // NOLINT(build/include_directory) +#include "v8-primitive.h" // NOLINT(build/include_directory) +#include "v8config.h" // NOLINT(build/include_directory) + +namespace v8 { + +class FunctionTemplate; + +// --- Extensions --- + +/** + * Ignore + */ +class V8_EXPORT Extension { + public: + // Note that the strings passed into this constructor must live as long + // as the Extension itself. + Extension(const char* name, const char* source = nullptr, int dep_count = 0, + const char** deps = nullptr, int source_length = -1); + virtual ~Extension() { delete source_; } + virtual Local GetNativeFunctionTemplate( + Isolate* isolate, Local name) { + return Local(); + } + + const char* name() const { return name_; } + size_t source_length() const { return source_length_; } + const String::ExternalOneByteStringResource* source() const { + return source_; + } + int dependency_count() const { return dep_count_; } + const char** dependencies() const { return deps_; } + void set_auto_enable(bool value) { auto_enable_ = value; } + bool auto_enable() { return auto_enable_; } + + // Disallow copying and assigning. + Extension(const Extension&) = delete; + void operator=(const Extension&) = delete; + + private: + const char* name_; + size_t source_length_; // expected to initialize before source_ + String::ExternalOneByteStringResource* source_; + int dep_count_; + const char** deps_; + bool auto_enable_; +}; + +void V8_EXPORT RegisterExtension(std::unique_ptr); + +} // namespace v8 + +#endif // INCLUDE_V8_EXTENSION_H_ diff --git a/android/arm64-v8a/include/v8/v8-external.h b/android/arm64-v8a/include/v8/v8-external.h new file mode 100644 index 00000000..2e245036 --- /dev/null +++ b/android/arm64-v8a/include/v8/v8-external.h @@ -0,0 +1,37 @@ +// 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_V8_EXTERNAL_H_ +#define INCLUDE_V8_EXTERNAL_H_ + +#include "v8-value.h" // NOLINT(build/include_directory) +#include "v8config.h" // NOLINT(build/include_directory) + +namespace v8 { + +class Isolate; + +/** + * A JavaScript value that wraps a C++ void*. This type of value is mainly used + * to associate C++ data structures with JavaScript objects. + */ +class V8_EXPORT External : public Value { + public: + static Local New(Isolate* isolate, void* value); + V8_INLINE static External* Cast(Value* value) { +#ifdef V8_ENABLE_CHECKS + CheckCast(value); +#endif + return static_cast(value); + } + + void* Value() const; + + private: + static void CheckCast(v8::Value* obj); +}; + +} // namespace v8 + +#endif // INCLUDE_V8_EXTERNAL_H_ diff --git a/android/arm64-v8a/include/v8/v8-fast-api-calls.h b/android/arm64-v8a/include/v8/v8-fast-api-calls.h index f8b5acb0..e40f1068 100644 --- a/android/arm64-v8a/include/v8/v8-fast-api-calls.h +++ b/android/arm64-v8a/include/v8/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_ @@ -190,22 +225,42 @@ #include #include -#include "v8config.h" // NOLINT(build/include_directory) +#include "v8-internal.h" // NOLINT(build/include_directory) +#include "v8-local-handle.h" // NOLINT(build/include_directory) +#include "v8-typed-array.h" // NOLINT(build/include_directory) +#include "v8-value.h" // NOLINT(build/include_directory) +#include "v8config.h" // NOLINT(build/include_directory) namespace v8 { +class Isolate; + class CTypeInfo { public: enum class Type : uint8_t { kVoid, kBool, + kUint8, kInt32, kUint32, kInt64, kUint64, kFloat32, kFloat64, + kPointer, kV8Value, + kSeqOneByteString, + kApiObject, // This will be deprecated once all users have + // migrated from v8::ApiObject to v8::Local. + kAny, // This is added to enable untyped representation of fast + // call arguments for test purposes. It can represent any of + // the other types stored in the same memory as a union (see + // the AnyCType struct declared below). This allows for + // uniform passing of arguments w.r.t. their location + // (in a register or on the stack), independent of their + // actual type. It's currently used by the arm64 simulator + // and can be added to the other simulators as well when fast + // calls having both GP and FP params need to be supported. }; // kCallbackOptionsType is not part of the Type enum @@ -213,31 +268,139 @@ class CTypeInfo { // than any valid Type enum. static constexpr Type kCallbackOptionsType = Type(255); - enum class Flags : uint8_t { - kNone = 0, + enum class SequenceType : uint8_t { + kScalar, + kIsSequence, // sequence + kIsTypedArray, // TypedArray of T or any ArrayBufferView if T + // is void + kIsArrayBuffer // ArrayBuffer }; - explicit constexpr CTypeInfo(Type type, Flags flags = Flags::kNone) - : type_(type), flags_(flags) {} + 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, SequenceType sequence_type = SequenceType::kScalar, + Flags flags = Flags::kNone) + : type_(type), sequence_type_(sequence_type), flags_(flags) {} + + typedef uint32_t Identifier; + explicit constexpr CTypeInfo(Identifier identifier) + : CTypeInfo(static_cast(identifier >> 16), + static_cast((identifier >> 8) & 255), + static_cast(identifier & 255)) {} + constexpr Identifier GetId() const { + return static_cast(type_) << 16 | + static_cast(sequence_type_) << 8 | + static_cast(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::kUint8 || type == Type::kInt32 || + type == Type::kUint32 || type == Type::kInt64 || + type == Type::kUint64; + } + + static constexpr bool IsFloatingPointType(Type type) { + return type == Type::kFloat32 || type == Type::kFloat64; + } + + static constexpr bool IsPrimitive(Type type) { + return IsIntegralType(type) || IsFloatingPointType(type) || + type == Type::kBool; + } + private: Type type_; + SequenceType sequence_type_; Flags flags_; }; +struct FastApiTypedArrayBase { + public: + // 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; + } + + bool getStorageIfAligned(T** elements) const { + if (reinterpret_cast(data_) % alignof(T) != 0) { + return false; + } + *elements = reinterpret_cast(data_); + return true; + } + + 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 FastOneByteString { + const char* data; + uint32_t length; +}; + class V8_EXPORT CFunctionInfo { public: + enum class Int64Representation : uint8_t { + kNumber = 0, // Use numbers to represent 64 bit integers. + kBigInt = 1, // Use BigInts to represent 64 bit integers. + }; + // 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* arg_info, + Int64Representation repr = Int64Representation::kNumber); const CTypeInfo& ReturnInfo() const { return return_info_; } @@ -247,6 +410,8 @@ class V8_EXPORT CFunctionInfo { return HasOptions() ? arg_count_ - 1 : arg_count_; } + Int64Representation GetInt64Representation() const { return repr_; } + // |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 @@ -261,10 +426,45 @@ class V8_EXPORT CFunctionInfo { private: const CTypeInfo return_info_; + const Int64Representation repr_; const unsigned int arg_count_; const CTypeInfo* arg_info_; }; +struct FastApiCallbackOptions; + +// Provided for testing. +struct AnyCType { + AnyCType() : int64_value(0) {} + + union { + bool bool_value; + int32_t int32_value; + uint32_t uint32_value; + int64_t int64_value; + uint64_t uint64_value; + float float_value; + double double_value; + void* pointer_value; + Local object_value; + Local sequence_value; + const FastApiTypedArray* uint8_ta_value; + const FastApiTypedArray* int32_ta_value; + const FastApiTypedArray* uint32_ta_value; + const FastApiTypedArray* int64_ta_value; + const FastApiTypedArray* uint64_ta_value; + const FastApiTypedArray* float_ta_value; + const FastApiTypedArray* double_ta_value; + const FastOneByteString* string_value; + FastApiCallbackOptions* options_value; + }; +}; + +static_assert( + sizeof(AnyCType) == 8, + "The AnyCType struct should have size == 64 bits, as this is assumed " + "by EffectControlLinearizer."); + class V8_EXPORT CFunction { public: constexpr CFunction() : address_(nullptr), type_info_(nullptr) {} @@ -278,17 +478,63 @@ class V8_EXPORT CFunction { unsigned int ArgumentCount() const { return type_info_->ArgumentCount(); } const void* GetAddress() const { return address_; } + CFunctionInfo::Int64Representation GetInt64Representation() const { + return type_info_->GetInt64Representation(); + } 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); + // Provided for testing purposes. + template + static CFunction Make(R (*func)(Args...), + R_Patch (*patching_func)(Args_Patch...)) { + CFunction c_func = ArgUnwrap::Make(func); + static_assert( + sizeof...(Args_Patch) == sizeof...(Args), + "The patching function must have the same number of arguments."); + c_func.address_ = reinterpret_cast(patching_func); + return c_func; } CFunction(const void* address, const CFunctionInfo* type_info); @@ -310,10 +556,6 @@ class V8_EXPORT CFunction { }; }; -struct ApiObject { - uintptr_t address; -}; - /** * A struct which may be passed to a fast call callback, like so: * \code @@ -321,6 +563,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}, nullptr}; + } + /** * 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 @@ -336,8 +586,17 @@ 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::Local data; + }; + + /** + * When called from WebAssembly, a view of the calling module's memory. + */ + FastApiTypedArray* const wasm_memory; }; namespace internal { @@ -351,7 +610,8 @@ struct count template struct count : count {}; -template +template class CFunctionInfoImpl : public CFunctionInfo { static constexpr int kOptionsArgCount = count(); @@ -366,16 +626,20 @@ class CFunctionInfoImpl : public CFunctionInfo { public: constexpr CFunctionInfoImpl() : CFunctionInfo(RetBuilder::Build(), sizeof...(ArgBuilders), - arg_info_storage_), + arg_info_storage_, Representation), 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::kInt64 || + kReturnType == CTypeInfo::Type::kUint64 || kReturnType == CTypeInfo::Type::kFloat32 || - kReturnType == CTypeInfo::Type::kFloat64, - "64-bit int and api object values are not currently " + kReturnType == CTypeInfo::Type::kFloat64 || + kReturnType == CTypeInfo::Type::kPointer || + kReturnType == CTypeInfo::Type::kAny, + "String and api object values are not currently " "supported return types."); } @@ -396,22 +660,94 @@ struct TypeInfoHelper { } \ \ static constexpr CTypeInfo::Type Type() { return CTypeInfo::Type::Enum; } \ + static constexpr CTypeInfo::SequenceType SequenceType() { \ + return CTypeInfo::SequenceType::kScalar; \ + } \ }; -#define BASIC_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) +template +struct CTypeInfoTraits {}; -BASIC_C_TYPES(SPECIALIZE_GET_TYPE_INFO_HELPER_FOR) +#define DEFINE_TYPE_INFO_TRAITS(CType, Enum) \ + template <> \ + struct CTypeInfoTraits { \ + using ctype = CType; \ + }; -#undef BASIC_C_TYPES +#define PRIMITIVE_C_TYPES(V) \ + V(bool, kBool) \ + V(uint8_t, kUint8) \ + V(int32_t, kInt32) \ + V(uint32_t, kUint32) \ + V(int64_t, kInt64) \ + V(uint64_t, kUint64) \ + V(float, kFloat32) \ + V(double, kFloat64) \ + V(void*, kPointer) + +// 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(AnyCType, kAny) + +// 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(uint8_t, kUint8) \ + 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; + } +}; + +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; + } +}; template <> struct TypeInfoHelper { @@ -420,28 +756,80 @@ struct TypeInfoHelper { static constexpr CTypeInfo::Type Type() { return CTypeInfo::kCallbackOptionsType; } + static constexpr CTypeInfo::SequenceType SequenceType() { + return CTypeInfo::SequenceType::kScalar; + } }; +template <> +struct TypeInfoHelper { + static constexpr CTypeInfo::Flags Flags() { return CTypeInfo::Flags::kNone; } + + static constexpr CTypeInfo::Type Type() { + return CTypeInfo::Type::kSeqOneByteString; + } + static constexpr CTypeInfo::SequenceType SequenceType() { + return CTypeInfo::SequenceType::kScalar; + } +}; + +#define STATIC_ASSERT_IMPLIES(COND, ASSERTION, MSG) \ + static_assert(((COND) == 0) || (ASSERTION), MSG) + +} // namespace internal + template -class CTypeInfoBuilder { +class V8_EXPORT CTypeInfoBuilder { public: using BaseType = T; static constexpr CTypeInfo Build() { - // Get the flags and merge in any additional flags. - uint8_t flags = uint8_t(TypeInfoHelper::Flags()); - int unused[] = {0, (flags |= uint8_t(Flags), 0)...}; - // With C++17, we could use a "..." fold expression over a parameter pack. - // Since we're still using C++14, we have to evaluate an OR expresion while - // constructing an unused list of 0's. This applies the binary operator - // for each value in Flags. - (void)unused; + constexpr CTypeInfo::Flags kFlags = + MergeFlags(internal::TypeInfoHelper::Flags(), Flags...); + constexpr CTypeInfo::Type kType = internal::TypeInfoHelper::Type(); + constexpr CTypeInfo::SequenceType kSequenceType = + internal::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(), CTypeInfo::Flags(flags)); + return CTypeInfo(internal::TypeInfoHelper::Type(), + internal::TypeInfoHelper::SequenceType(), kFlags); } + + 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); } }; +namespace internal { template class CFunctionBuilderWithFunction { public: @@ -462,8 +850,21 @@ class CFunctionBuilderWithFunction { std::make_index_sequence()); } + // Provided for testing purposes. + template + auto Patch(Ret (*patching_func)(Args...)) { + static_assert( + sizeof...(Args) == sizeof...(ArgBuilders), + "The patching function must have the same number of arguments."); + fn_ = reinterpret_cast(patching_func); + return *this; + } + + template auto Build() { - static CFunctionInfoImpl instance; + static CFunctionInfoImpl + instance; return CFunction(fn_, &instance); } @@ -491,8 +892,9 @@ class CFunctionBuilderWithFunction { Flags...>; }; - // Return a copy of the CFunctionBuilder, but merges the Flags on ArgBuilder - // index N with the new Flags passed in the template parameter pack. + // 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< @@ -524,6 +926,50 @@ CFunction CFunction::ArgUnwrap::Make(R (*func)(Args...)) { 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 TryToCopyAndConvertArrayToCppBuffer( + Local src, T* dst, uint32_t max_length); + +template <> +bool V8_EXPORT V8_WARN_UNUSED_RESULT +TryToCopyAndConvertArrayToCppBuffer::Build().GetId(), + int32_t>(Local src, int32_t* dst, + uint32_t max_length); + +template <> +bool V8_EXPORT V8_WARN_UNUSED_RESULT +TryToCopyAndConvertArrayToCppBuffer::Build().GetId(), + uint32_t>(Local src, uint32_t* dst, + uint32_t max_length); + +template <> +bool V8_EXPORT V8_WARN_UNUSED_RESULT +TryToCopyAndConvertArrayToCppBuffer::Build().GetId(), + float>(Local src, float* dst, + uint32_t max_length); + +template <> +bool V8_EXPORT V8_WARN_UNUSED_RESULT +TryToCopyAndConvertArrayToCppBuffer::Build().GetId(), + double>(Local src, double* dst, + uint32_t max_length); + } // namespace v8 #endif // INCLUDE_V8_FAST_API_CALLS_H_ diff --git a/android/arm64-v8a/include/v8/v8-forward.h b/android/arm64-v8a/include/v8/v8-forward.h new file mode 100644 index 00000000..db3a2017 --- /dev/null +++ b/android/arm64-v8a/include/v8/v8-forward.h @@ -0,0 +1,81 @@ +// 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_V8_FORWARD_H_ +#define INCLUDE_V8_FORWARD_H_ + +// This header is intended to be used by headers that pass around V8 types, +// either by pointer or using Local. The full definitions can be included +// either via v8.h or the more fine-grained headers. + +#include "v8-local-handle.h" // NOLINT(build/include_directory) + +namespace v8 { + +class AccessorSignature; +class Array; +class ArrayBuffer; +class ArrayBufferView; +class BigInt; +class BigInt64Array; +class BigIntObject; +class BigUint64Array; +class Boolean; +class BooleanObject; +class Context; +class DataView; +class Data; +class Date; +class Extension; +class External; +class FixedArray; +class Float32Array; +class Float64Array; +class Function; +template +class FunctionCallbackInfo; +class FunctionTemplate; +class Int16Array; +class Int32; +class Int32Array; +class Int8Array; +class Integer; +class Isolate; +class Map; +class Module; +class Name; +class Number; +class NumberObject; +class Object; +class ObjectTemplate; +class Platform; +class Primitive; +class Private; +class Promise; +class Proxy; +class RegExp; +class Script; +class Set; +class SharedArrayBuffer; +class Signature; +class String; +class StringObject; +class Symbol; +class SymbolObject; +class Template; +class TryCatch; +class TypedArray; +class Uint16Array; +class Uint32; +class Uint32Array; +class Uint8Array; +class Uint8ClampedArray; +class UnboundModuleScript; +class Value; +class WasmMemoryObject; +class WasmModuleObject; + +} // namespace v8 + +#endif // INCLUDE_V8_FORWARD_H_ diff --git a/android/arm64-v8a/include/v8/v8-function-callback.h b/android/arm64-v8a/include/v8/v8-function-callback.h new file mode 100644 index 00000000..49c102bc --- /dev/null +++ b/android/arm64-v8a/include/v8/v8-function-callback.h @@ -0,0 +1,499 @@ +// 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_V8_FUNCTION_CALLBACK_H_ +#define INCLUDE_V8_FUNCTION_CALLBACK_H_ + +#include "v8-local-handle.h" // NOLINT(build/include_directory) +#include "v8-primitive.h" // NOLINT(build/include_directory) +#include "v8config.h" // NOLINT(build/include_directory) + +namespace v8 { + +template +class BasicTracedReference; +template +class Global; +class Object; +class Value; + +namespace internal { +class FunctionCallbackArguments; +class PropertyCallbackArguments; +class Builtins; +} // namespace internal + +namespace debug { +class ConsoleCallArguments; +} // namespace debug + +template +class ReturnValue { + public: + template + V8_INLINE ReturnValue(const ReturnValue& that) : value_(that.value_) { + static_assert(std::is_base_of::value, "type check"); + } + // Local setters + template + V8_INLINE void Set(const Global& handle); + template + V8_INLINE void Set(const BasicTracedReference& handle); + template + V8_INLINE void Set(const Local handle); + // Fast primitive setters + V8_INLINE void Set(bool value); + V8_INLINE void Set(double i); + V8_INLINE void Set(int32_t i); + V8_INLINE void Set(uint32_t i); + // Fast JS primitive setters + V8_INLINE void SetNull(); + V8_INLINE void SetUndefined(); + V8_INLINE void SetEmptyString(); + // Convenience getter for Isolate + V8_INLINE Isolate* GetIsolate() const; + + // Pointer setter: Uncompilable to prevent inadvertent misuse. + template + V8_INLINE void Set(S* whatever); + + // Getter. Creates a new Local<> so it comes with a certain performance + // hit. If the ReturnValue was not yet set, this will return the undefined + // value. + V8_INLINE Local Get() const; + + private: + template + friend class ReturnValue; + template + friend class FunctionCallbackInfo; + template + friend class PropertyCallbackInfo; + template + friend class PersistentValueMapBase; + V8_INLINE void SetInternal(internal::Address value) { *value_ = value; } + V8_INLINE internal::Address GetDefaultValue(); + V8_INLINE explicit ReturnValue(internal::Address* slot); + + // See FunctionCallbackInfo. + static constexpr int kIsolateValueIndex = -2; + + internal::Address* value_; +}; + +/** + * The argument information given to function call callbacks. This + * class provides access to information about the context of the call, + * including the receiver, the number and values of arguments, and + * the holder of the function. + */ +template +class FunctionCallbackInfo { + public: + /** The number of available arguments. */ + V8_INLINE int Length() const; + /** + * Accessor for the available arguments. Returns `undefined` if the index + * is out of bounds. + */ + V8_INLINE Local operator[](int i) const; + /** Returns the receiver. This corresponds to the "this" value. */ + V8_INLINE Local This() const; + /** + * If the callback was created without a Signature, this is the same + * value as This(). If there is a signature, and the signature didn't match + * This() but one of its hidden prototypes, this will be the respective + * hidden prototype. + * + * Note that this is not the prototype of This() on which the accessor + * referencing this callback was found (which in V8 internally is often + * referred to as holder [sic]). + */ + V8_INLINE Local Holder() const; + /** For construct calls, this returns the "new.target" value. */ + V8_INLINE Local NewTarget() const; + /** Indicates whether this is a regular call or a construct call. */ + V8_INLINE bool IsConstructCall() const; + /** The data argument specified when creating the callback. */ + V8_INLINE Local Data() const; + /** The current Isolate. */ + V8_INLINE Isolate* GetIsolate() const; + /** The ReturnValue for the call. */ + V8_INLINE ReturnValue GetReturnValue() const; + + private: + friend class internal::FunctionCallbackArguments; + friend class internal::CustomArguments; + friend class debug::ConsoleCallArguments; + friend class internal::Builtins; + + static constexpr int kHolderIndex = 0; + static constexpr int kIsolateIndex = 1; + static constexpr int kUnusedIndex = 2; + static constexpr int kReturnValueIndex = 3; + static constexpr int kDataIndex = 4; + static constexpr int kNewTargetIndex = 5; + static constexpr int kArgsLength = 6; + + static constexpr int kArgsLengthWithReceiver = kArgsLength + 1; + + // Codegen constants: + static constexpr int kSize = 3 * internal::kApiSystemPointerSize; + static constexpr int kImplicitArgsOffset = 0; + static constexpr int kValuesOffset = + kImplicitArgsOffset + internal::kApiSystemPointerSize; + static constexpr int kLengthOffset = + kValuesOffset + internal::kApiSystemPointerSize; + + static constexpr int kThisValuesIndex = -1; + static_assert(ReturnValue::kIsolateValueIndex == + kIsolateIndex - kReturnValueIndex); + + V8_INLINE FunctionCallbackInfo(internal::Address* implicit_args, + internal::Address* values, int length); + internal::Address* implicit_args_; + internal::Address* values_; + int length_; +}; + +/** + * The information passed to a property callback about the context + * of the property access. + */ +template +class PropertyCallbackInfo { + public: + /** + * \return The isolate of the property access. + */ + V8_INLINE Isolate* GetIsolate() const; + + /** + * \return The data set in the configuration, i.e., in + * `NamedPropertyHandlerConfiguration` or + * `IndexedPropertyHandlerConfiguration.` + */ + V8_INLINE Local Data() const; + + /** + * \return The receiver. In many cases, this is the object on which the + * property access was intercepted. When using + * `Reflect.get`, `Function.prototype.call`, or similar functions, it is the + * object passed in as receiver or thisArg. + * + * \code + * void GetterCallback(Local name, + * const v8::PropertyCallbackInfo& info) { + * auto context = info.GetIsolate()->GetCurrentContext(); + * + * v8::Local a_this = + * info.This() + * ->GetRealNamedProperty(context, v8_str("a")) + * .ToLocalChecked(); + * v8::Local a_holder = + * info.Holder() + * ->GetRealNamedProperty(context, v8_str("a")) + * .ToLocalChecked(); + * + * CHECK(v8_str("r")->Equals(context, a_this).FromJust()); + * CHECK(v8_str("obj")->Equals(context, a_holder).FromJust()); + * + * info.GetReturnValue().Set(name); + * } + * + * v8::Local templ = + * v8::FunctionTemplate::New(isolate); + * templ->InstanceTemplate()->SetHandler( + * v8::NamedPropertyHandlerConfiguration(GetterCallback)); + * LocalContext env; + * env->Global() + * ->Set(env.local(), v8_str("obj"), templ->GetFunction(env.local()) + * .ToLocalChecked() + * ->NewInstance(env.local()) + * .ToLocalChecked()) + * .FromJust(); + * + * CompileRun("obj.a = 'obj'; var r = {a: 'r'}; Reflect.get(obj, 'x', r)"); + * \endcode + */ + V8_INLINE Local This() const; + + /** + * \return The object in the prototype chain of the receiver that has the + * interceptor. Suppose you have `x` and its prototype is `y`, and `y` + * has an interceptor. Then `info.This()` is `x` and `info.Holder()` is `y`. + * The Holder() could be a hidden object (the global object, rather + * than the global proxy). + * + * \note For security reasons, do not pass the object back into the runtime. + */ + V8_INLINE Local Holder() const; + + /** + * \return The return value of the callback. + * Can be changed by calling Set(). + * \code + * info.GetReturnValue().Set(...) + * \endcode + * + */ + V8_INLINE ReturnValue GetReturnValue() const; + + /** + * \return True if the intercepted function should throw if an error occurs. + * Usually, `true` corresponds to `'use strict'`. + * + * \note Always `false` when intercepting `Reflect.set()` + * independent of the language mode. + */ + V8_INLINE bool ShouldThrowOnError() const; + + private: + friend class MacroAssembler; + friend class internal::PropertyCallbackArguments; + friend class internal::CustomArguments; + static constexpr int kShouldThrowOnErrorIndex = 0; + static constexpr int kHolderIndex = 1; + static constexpr int kIsolateIndex = 2; + static constexpr int kUnusedIndex = 3; + static constexpr int kReturnValueIndex = 4; + static constexpr int kDataIndex = 5; + static constexpr int kThisIndex = 6; + static constexpr int kArgsLength = 7; + + static constexpr int kSize = 1 * internal::kApiSystemPointerSize; + + V8_INLINE explicit PropertyCallbackInfo(internal::Address* args) + : args_(args) {} + + internal::Address* args_; +}; + +using FunctionCallback = void (*)(const FunctionCallbackInfo& info); + +// --- Implementation --- + +template +ReturnValue::ReturnValue(internal::Address* slot) : value_(slot) {} + +template +template +void ReturnValue::Set(const Global& handle) { + static_assert(std::is_base_of::value, "type check"); + if (V8_UNLIKELY(handle.IsEmpty())) { + *value_ = GetDefaultValue(); + } else { + *value_ = handle.ptr(); + } +} + +template +template +void ReturnValue::Set(const BasicTracedReference& handle) { + static_assert(std::is_base_of::value, "type check"); + if (V8_UNLIKELY(handle.IsEmpty())) { + *value_ = GetDefaultValue(); + } else { + *value_ = handle.ptr(); + } +} + +template +template +void ReturnValue::Set(const Local handle) { + static_assert(std::is_void::value || std::is_base_of::value, + "type check"); + if (V8_UNLIKELY(handle.IsEmpty())) { + *value_ = GetDefaultValue(); + } else { + *value_ = handle.ptr(); + } +} + +template +void ReturnValue::Set(double i) { + static_assert(std::is_base_of::value, "type check"); + Set(Number::New(GetIsolate(), i)); +} + +template +void ReturnValue::Set(int32_t i) { + static_assert(std::is_base_of::value, "type check"); + using I = internal::Internals; + if (V8_LIKELY(I::IsValidSmi(i))) { + *value_ = I::IntToSmi(i); + return; + } + Set(Integer::New(GetIsolate(), i)); +} + +template +void ReturnValue::Set(uint32_t i) { + static_assert(std::is_base_of::value, "type check"); + // Can't simply use INT32_MAX here for whatever reason. + bool fits_into_int32_t = (i & (1U << 31)) == 0; + if (V8_LIKELY(fits_into_int32_t)) { + Set(static_cast(i)); + return; + } + Set(Integer::NewFromUnsigned(GetIsolate(), i)); +} + +template +void ReturnValue::Set(bool value) { + static_assert(std::is_base_of::value, "type check"); + using I = internal::Internals; + int root_index; + if (value) { + root_index = I::kTrueValueRootIndex; + } else { + root_index = I::kFalseValueRootIndex; + } + *value_ = I::GetRoot(GetIsolate(), root_index); +} + +template +void ReturnValue::SetNull() { + static_assert(std::is_base_of::value, "type check"); + using I = internal::Internals; + *value_ = I::GetRoot(GetIsolate(), I::kNullValueRootIndex); +} + +template +void ReturnValue::SetUndefined() { + static_assert(std::is_base_of::value, "type check"); + using I = internal::Internals; + *value_ = I::GetRoot(GetIsolate(), I::kUndefinedValueRootIndex); +} + +template +void ReturnValue::SetEmptyString() { + static_assert(std::is_base_of::value, "type check"); + using I = internal::Internals; + *value_ = I::GetRoot(GetIsolate(), I::kEmptyStringRootIndex); +} + +template +Isolate* ReturnValue::GetIsolate() const { + return *reinterpret_cast(&value_[kIsolateValueIndex]); +} + +template +Local ReturnValue::Get() const { + using I = internal::Internals; +#if V8_STATIC_ROOTS_BOOL + if (I::is_identical(*value_, I::StaticReadOnlyRoot::kTheHoleValue)) { +#else + if (*value_ == I::GetRoot(GetIsolate(), I::kTheHoleValueRootIndex)) { +#endif + return Undefined(GetIsolate()); + } + return Local::New(GetIsolate(), reinterpret_cast(value_)); +} + +template +template +void ReturnValue::Set(S* whatever) { + static_assert(sizeof(S) < 0, "incompilable to prevent inadvertent misuse"); +} + +template +internal::Address ReturnValue::GetDefaultValue() { + using I = internal::Internals; + return I::GetRoot(GetIsolate(), I::kTheHoleValueRootIndex); +} + +template +FunctionCallbackInfo::FunctionCallbackInfo(internal::Address* implicit_args, + internal::Address* values, + int length) + : implicit_args_(implicit_args), values_(values), length_(length) {} + +template +Local FunctionCallbackInfo::operator[](int i) const { + // values_ points to the first argument (not the receiver). + if (i < 0 || length_ <= i) return Undefined(GetIsolate()); + return Local::FromSlot(values_ + i); +} + +template +Local FunctionCallbackInfo::This() const { + // values_ points to the first argument (not the receiver). + return Local::FromSlot(values_ + kThisValuesIndex); +} + +template +Local FunctionCallbackInfo::Holder() const { + return Local::FromSlot(&implicit_args_[kHolderIndex]); +} + +template +Local FunctionCallbackInfo::NewTarget() const { + return Local::FromSlot(&implicit_args_[kNewTargetIndex]); +} + +template +Local FunctionCallbackInfo::Data() const { + return Local::FromSlot(&implicit_args_[kDataIndex]); +} + +template +Isolate* FunctionCallbackInfo::GetIsolate() const { + return *reinterpret_cast(&implicit_args_[kIsolateIndex]); +} + +template +ReturnValue FunctionCallbackInfo::GetReturnValue() const { + return ReturnValue(&implicit_args_[kReturnValueIndex]); +} + +template +bool FunctionCallbackInfo::IsConstructCall() const { + return !NewTarget()->IsUndefined(); +} + +template +int FunctionCallbackInfo::Length() const { + return length_; +} + +template +Isolate* PropertyCallbackInfo::GetIsolate() const { + return *reinterpret_cast(&args_[kIsolateIndex]); +} + +template +Local PropertyCallbackInfo::Data() const { + return Local::FromSlot(&args_[kDataIndex]); +} + +template +Local PropertyCallbackInfo::This() const { + return Local::FromSlot(&args_[kThisIndex]); +} + +template +Local PropertyCallbackInfo::Holder() const { + return Local::FromSlot(&args_[kHolderIndex]); +} + +template +ReturnValue PropertyCallbackInfo::GetReturnValue() const { + return ReturnValue(&args_[kReturnValueIndex]); +} + +template +bool PropertyCallbackInfo::ShouldThrowOnError() const { + using I = internal::Internals; + if (args_[kShouldThrowOnErrorIndex] != + I::IntToSmi(I::kInferShouldThrowMode)) { + return args_[kShouldThrowOnErrorIndex] != I::IntToSmi(I::kDontThrow); + } + return v8::internal::ShouldThrowOnError( + reinterpret_cast(GetIsolate())); +} + +} // namespace v8 + +#endif // INCLUDE_V8_FUNCTION_CALLBACK_H_ diff --git a/android/arm64-v8a/include/v8/v8-function.h b/android/arm64-v8a/include/v8/v8-function.h new file mode 100644 index 00000000..1e35bfc8 --- /dev/null +++ b/android/arm64-v8a/include/v8/v8-function.h @@ -0,0 +1,134 @@ +// 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_V8_FUNCTION_H_ +#define INCLUDE_V8_FUNCTION_H_ + +#include +#include + +#include "v8-function-callback.h" // NOLINT(build/include_directory) +#include "v8-local-handle.h" // NOLINT(build/include_directory) +#include "v8-message.h" // NOLINT(build/include_directory) +#include "v8-object.h" // NOLINT(build/include_directory) +#include "v8-template.h" // NOLINT(build/include_directory) +#include "v8config.h" // NOLINT(build/include_directory) + +namespace v8 { + +class Context; +class UnboundScript; + +/** + * A JavaScript function object (ECMA-262, 15.3). + */ +class V8_EXPORT Function : public Object { + public: + /** + * Create a function in the current execution context + * for a given FunctionCallback. + */ + static MaybeLocal New( + Local context, FunctionCallback callback, + Local data = Local(), int length = 0, + ConstructorBehavior behavior = ConstructorBehavior::kAllow, + SideEffectType side_effect_type = SideEffectType::kHasSideEffect); + + V8_WARN_UNUSED_RESULT MaybeLocal NewInstance( + Local context, int argc, Local argv[]) const; + + V8_WARN_UNUSED_RESULT MaybeLocal NewInstance( + Local context) const { + return NewInstance(context, 0, nullptr); + } + + /** + * When side effect checks are enabled, passing kHasNoSideEffect allows the + * constructor to be invoked without throwing. Calls made within the + * constructor are still checked. + */ + V8_WARN_UNUSED_RESULT MaybeLocal NewInstanceWithSideEffectType( + Local context, int argc, Local argv[], + SideEffectType side_effect_type = SideEffectType::kHasSideEffect) const; + + V8_WARN_UNUSED_RESULT MaybeLocal Call(Local context, + Local recv, int argc, + Local argv[]); + + void SetName(Local name); + Local GetName() const; + + V8_DEPRECATED("No direct replacement") + MaybeLocal GetUnboundScript() const; + + /** + * Name inferred from variable or property assignment of this function. + * Used to facilitate debugging and profiling of JavaScript code written + * in an OO style, where many functions are anonymous but are assigned + * to object properties. + */ + Local GetInferredName() const; + + /** + * displayName if it is set, otherwise name if it is configured, otherwise + * function name, otherwise inferred name. + */ + Local GetDebugName() const; + + /** + * Returns zero based line number of function body and + * kLineOffsetNotFound if no information available. + */ + int GetScriptLineNumber() const; + /** + * Returns zero based column number of function body and + * kLineOffsetNotFound if no information available. + */ + int GetScriptColumnNumber() const; + + /** + * Returns scriptId. + */ + int ScriptId() const; + + /** + * Returns the original function if this function is bound, else returns + * v8::Undefined. + */ + Local GetBoundFunction() const; + + /** + * Calls builtin Function.prototype.toString on this function. + * This is different from Value::ToString() that may call a user-defined + * toString() function, and different than Object::ObjectProtoToString() which + * always serializes "[object Function]". + */ + V8_WARN_UNUSED_RESULT MaybeLocal FunctionProtoToString( + Local context); + + /** + * Returns true if the function does nothing. + * The function returns false on error. + * Note that this function is experimental. Embedders should not rely on + * this existing. We may remove this function in the future. + */ + V8_WARN_UNUSED_RESULT bool Experimental_IsNopFunction() const; + + ScriptOrigin GetScriptOrigin() const; + V8_INLINE static Function* Cast(Value* value) { +#ifdef V8_ENABLE_CHECKS + CheckCast(value); +#endif + return static_cast(value); + } + + static const int kLineOffsetNotFound; + + private: + Function(); + static void CheckCast(Value* obj); +}; +} // namespace v8 + +#endif // INCLUDE_V8_FUNCTION_H_ diff --git a/android/arm64-v8a/include/v8/v8-handle-base.h b/android/arm64-v8a/include/v8/v8-handle-base.h new file mode 100644 index 00000000..da19db77 --- /dev/null +++ b/android/arm64-v8a/include/v8/v8-handle-base.h @@ -0,0 +1,180 @@ +// Copyright 2023 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_V8_HANDLE_BASE_H_ +#define INCLUDE_V8_HANDLE_BASE_H_ + +#include "v8-internal.h" // NOLINT(build/include_directory) + +namespace v8 { + +namespace internal { + +// Helper functions about values contained in handles. +// A value is either an indirect pointer or a direct pointer, depending on +// whether direct local support is enabled. +class ValueHelper final { + public: +#ifdef V8_ENABLE_DIRECT_LOCAL + static constexpr Address kTaggedNullAddress = 1; + static constexpr Address kEmpty = kTaggedNullAddress; +#else + static constexpr Address kEmpty = kNullAddress; +#endif // V8_ENABLE_DIRECT_LOCAL + + template + V8_INLINE static bool IsEmpty(T* value) { + return reinterpret_cast
(value) == kEmpty; + } + + // Returns a handle's "value" for all kinds of abstract handles. For Local, + // it is equivalent to `*handle`. The variadic parameters support handle + // types with extra type parameters, like `Persistent`. + template