From 79e6f35e80d9e9b51cc590df24cb74bb8a0435ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marja=20H=C3=B6ltt=C3=A4?= Date: Mon, 19 Mar 2018 14:53:31 +0100 Subject: [PATCH] [in-place weak refs] Move weak object tag away from v8.h. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Exposing it inside Internals was a hack. The downside of this CL is that heap object tagging is in two places now (v8.h and globals.h). BUG=v8:7308 Cq-Include-Trybots: luci.chromium.try:linux_chromium_rel_ng Change-Id: Ic7115ab20d67109dd2b62c772d52eeb84fa7d9f7 Reviewed-on: https://chromium-review.googlesource.com/968423 Reviewed-by: Ulan Degenbaev Reviewed-by: Yang Guo Reviewed-by: Hannes Payer Commit-Queue: Marja Hölttä Cr-Commit-Position: refs/heads/master@{#52093} --- include/v8.h | 40 ----------------------------- src/globals.h | 41 +++++++++++++++++++++++++++--- src/heap/concurrent-marking.cc | 4 +-- src/heap/heap-inl.h | 10 ++++---- src/heap/heap.cc | 4 +-- src/heap/incremental-marking-inl.h | 4 +-- src/heap/mark-compact.cc | 10 ++++---- src/heap/remembered-set.h | 6 ++--- src/heap/scavenger.cc | 6 ++--- src/objects/maybe-object-inl.h | 13 +++++----- src/objects/maybe-object.h | 16 ++++++------ src/snapshot/deserializer.cc | 2 +- 12 files changed, 74 insertions(+), 82 deletions(-) diff --git a/include/v8.h b/include/v8.h index 05d64f4771..7d04ca54dd 100644 --- a/include/v8.h +++ b/include/v8.h @@ -145,9 +145,7 @@ class Arguments; class DeferredHandles; class Heap; class HeapObject; -class HeapObjectReference; class Isolate; -class MaybeObject; class Object; struct ScriptStreamingData; template class CustomArguments; @@ -8869,9 +8867,6 @@ const int kWeakHeapObjectTag = 3; const int kHeapObjectTagSize = 2; const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1; -const intptr_t kWeakHeapObjectMask = 1 << 1; -const intptr_t kClearedWeakHeapObject = 3; - // Tag information for Smi. const int kSmiTag = 0; const int kSmiTagSize = 1; @@ -9013,41 +9008,6 @@ class Internals { kHeapObjectTag); } - V8_INLINE static bool HasWeakHeapObjectTag( - const internal::MaybeObject* value) { - return ((reinterpret_cast(value) & kHeapObjectTagMask) == - kWeakHeapObjectTag); - } - - // Object* should never have the weak tag; this variant is for overzealous - // checking. - V8_INLINE static bool HasWeakHeapObjectTag(const internal::Object* value) { - return ((reinterpret_cast(value) & kHeapObjectTagMask) == - kWeakHeapObjectTag); - } - - V8_INLINE static bool IsClearedWeakHeapObject(internal::MaybeObject* value) { - return reinterpret_cast(value) == kClearedWeakHeapObject; - } - - V8_INLINE static internal::HeapObject* RemoveWeakHeapObjectMask( - internal::HeapObjectReference* value) { - return reinterpret_cast(reinterpret_cast(value) & - ~kWeakHeapObjectMask); - } - - V8_INLINE static internal::HeapObjectReference* AddWeakHeapObjectMask( - internal::HeapObject* value) { - return reinterpret_cast( - reinterpret_cast(value) | kWeakHeapObjectMask); - } - - V8_INLINE static internal::MaybeObject* AddWeakHeapObjectMask( - internal::MaybeObject* value) { - return reinterpret_cast(reinterpret_cast(value) | - kWeakHeapObjectMask); - } - V8_INLINE static int SmiValue(const internal::Object* value) { return PlatformSmiTagging::SmiToInt(value); } diff --git a/src/globals.h b/src/globals.h index 1f6586c36f..ff79fabf76 100644 --- a/src/globals.h +++ b/src/globals.h @@ -11,6 +11,7 @@ #include #include +#include "include/v8.h" #include "src/base/build_config.h" #include "src/base/flags.h" #include "src/base/logging.h" @@ -413,10 +414,8 @@ constexpr int kCodeAlignmentBits = 5; constexpr intptr_t kCodeAlignment = 1 << kCodeAlignmentBits; constexpr intptr_t kCodeAlignmentMask = kCodeAlignment - 1; -// Weak references are tagged using the second bit in a pointer. -constexpr int kWeakReferenceTag = 3; -constexpr int kWeakReferenceTagSize = 2; -constexpr intptr_t kWeakReferenceTagMask = (1 << kWeakReferenceTagSize) - 1; +const intptr_t kWeakHeapObjectMask = 1 << 1; +const intptr_t kClearedWeakHeapObject = 3; // Zap-value: The value used for zapping dead objects. // Should be a recognizable hex value tagged as a failure. @@ -477,6 +476,7 @@ template class MaybeHandle; template class Handle; class Heap; class HeapObject; +class HeapObjectReference; class IC; class InterceptorInfo; class Isolate; @@ -489,6 +489,7 @@ class MacroAssembler; class Map; class MapSpace; class MarkCompactCollector; +class MaybeObject; class NewSpace; class Object; class OldSpace; @@ -1472,6 +1473,38 @@ enum IsolateAddressId { kIsolateAddressCount }; +V8_INLINE static bool HasWeakHeapObjectTag(const internal::MaybeObject* value) { + return ((reinterpret_cast(value) & kHeapObjectTagMask) == + kWeakHeapObjectTag); +} + +// Object* should never have the weak tag; this variant is for overzealous +// checking. +V8_INLINE static bool HasWeakHeapObjectTag(const Object* value) { + return ((reinterpret_cast(value) & kHeapObjectTagMask) == + kWeakHeapObjectTag); +} + +V8_INLINE static bool IsClearedWeakHeapObject(MaybeObject* value) { + return reinterpret_cast(value) == kClearedWeakHeapObject; +} + +V8_INLINE static HeapObject* RemoveWeakHeapObjectMask( + HeapObjectReference* value) { + return reinterpret_cast(reinterpret_cast(value) & + ~kWeakHeapObjectMask); +} + +V8_INLINE static HeapObjectReference* AddWeakHeapObjectMask(HeapObject* value) { + return reinterpret_cast( + reinterpret_cast(value) | kWeakHeapObjectMask); +} + +V8_INLINE static MaybeObject* AddWeakHeapObjectMask(MaybeObject* value) { + return reinterpret_cast(reinterpret_cast(value) | + kWeakHeapObjectMask); +} + enum class HeapObjectReferenceType { WEAK, STRONG, diff --git a/src/heap/concurrent-marking.cc b/src/heap/concurrent-marking.cc index 4ba72314cb..82b4115b2b 100644 --- a/src/heap/concurrent-marking.cc +++ b/src/heap/concurrent-marking.cc @@ -129,7 +129,7 @@ class ConcurrentMarkingVisitor final void VisitPointers(HeapObject* host, Object** start, Object** end) override { for (Object** slot = start; slot < end; slot++) { Object* object = base::AsAtomicPointer::Relaxed_Load(slot); - DCHECK(!Internals::HasWeakHeapObjectTag(object)); + DCHECK(!HasWeakHeapObjectTag(object)); if (object->IsHeapObject()) { ProcessStrongHeapObject(host, slot, HeapObject::cast(object)); } @@ -158,7 +158,7 @@ class ConcurrentMarkingVisitor final for (int i = 0; i < snapshot.number_of_slots(); i++) { Object** slot = snapshot.slot(i); Object* object = snapshot.value(i); - DCHECK(!Internals::HasWeakHeapObjectTag(object)); + DCHECK(!HasWeakHeapObjectTag(object)); if (!object->IsHeapObject()) continue; MarkObject(HeapObject::cast(object)); MarkCompactCollector::RecordSlot(host, slot, object); diff --git a/src/heap/heap-inl.h b/src/heap/heap-inl.h index 6519e58cb5..72eda445b7 100644 --- a/src/heap/heap-inl.h +++ b/src/heap/heap-inl.h @@ -461,17 +461,17 @@ void Heap::FinalizeExternalString(String* string) { Address Heap::NewSpaceTop() { return new_space_->top(); } bool Heap::InNewSpace(Object* object) { - DCHECK(!Internals::HasWeakHeapObjectTag(object)); + DCHECK(!HasWeakHeapObjectTag(object)); return InNewSpace(MaybeObject::FromObject(object)); } bool Heap::InFromSpace(Object* object) { - DCHECK(!Internals::HasWeakHeapObjectTag(object)); + DCHECK(!HasWeakHeapObjectTag(object)); return InFromSpace(MaybeObject::FromObject(object)); } bool Heap::InToSpace(Object* object) { - DCHECK(!Internals::HasWeakHeapObjectTag(object)); + DCHECK(!HasWeakHeapObjectTag(object)); return InToSpace(MaybeObject::FromObject(object)); } @@ -518,8 +518,8 @@ bool Heap::ShouldBePromoted(Address old_address) { } void Heap::RecordWrite(Object* object, Object** slot, Object* value) { - DCHECK(!Internals::HasWeakHeapObjectTag(*slot)); - DCHECK(!Internals::HasWeakHeapObjectTag(value)); + DCHECK(!HasWeakHeapObjectTag(*slot)); + DCHECK(!HasWeakHeapObjectTag(value)); RecordWrite(object, reinterpret_cast(slot), reinterpret_cast(value)); } diff --git a/src/heap/heap.cc b/src/heap/heap.cc index c90e29b563..cb9cd0cac4 100644 --- a/src/heap/heap.cc +++ b/src/heap/heap.cc @@ -1450,7 +1450,7 @@ class StringTableVerifier : public ObjectVisitor { void VisitPointers(HeapObject* host, Object** start, Object** end) override { // Visit all HeapObject pointers in [start, end). for (Object** p = start; p < end; p++) { - DCHECK(!Internals::HasWeakHeapObjectTag(*p)); + DCHECK(!HasWeakHeapObjectTag(*p)); if ((*p)->IsHeapObject()) { HeapObject* object = HeapObject::cast(*p); Isolate* isolate = object->GetIsolate(); @@ -4919,7 +4919,7 @@ class SlotVerifyingVisitor : public ObjectVisitor { void VisitPointers(HeapObject* host, Object** start, Object** end) override { #ifdef DEBUG for (Object** slot = start; slot < end; slot++) { - DCHECK(!Internals::HasWeakHeapObjectTag(*slot)); + DCHECK(!HasWeakHeapObjectTag(*slot)); } #endif // DEBUG VisitPointers(host, reinterpret_cast(start), diff --git a/src/heap/incremental-marking-inl.h b/src/heap/incremental-marking-inl.h index 0f792a1494..b64c203a8d 100644 --- a/src/heap/incremental-marking-inl.h +++ b/src/heap/incremental-marking-inl.h @@ -15,8 +15,8 @@ namespace internal { void IncrementalMarking::RecordWrite(HeapObject* obj, Object** slot, Object* value) { - DCHECK_IMPLIES(slot != nullptr, !Internals::HasWeakHeapObjectTag(*slot)); - DCHECK(!Internals::HasWeakHeapObjectTag(value)); + DCHECK_IMPLIES(slot != nullptr, !HasWeakHeapObjectTag(*slot)); + DCHECK(!HasWeakHeapObjectTag(value)); RecordMaybeWeakWrite(obj, reinterpret_cast(slot), reinterpret_cast(value)); } diff --git a/src/heap/mark-compact.cc b/src/heap/mark-compact.cc index 7f621da77f..ec748dd5a5 100644 --- a/src/heap/mark-compact.cc +++ b/src/heap/mark-compact.cc @@ -235,7 +235,7 @@ class YoungGenerationMarkingVerifier : public MarkingVerifier { void VerifyPointers(Object** start, Object** end) override { for (Object** current = start; current < end; current++) { - DCHECK(!Internals::HasWeakHeapObjectTag(*current)); + DCHECK(!HasWeakHeapObjectTag(*current)); if ((*current)->IsHeapObject()) { HeapObject* object = HeapObject::cast(*current); if (!heap_->InNewSpace(object)) return; @@ -1061,7 +1061,7 @@ class MarkCompactCollector::CustomRootBodyMarkingVisitor final void VisitPointers(HeapObject* host, Object** start, Object** end) final { for (Object** p = start; p < end; p++) { - DCHECK(!Internals::HasWeakHeapObjectTag(*p)); + DCHECK(!HasWeakHeapObjectTag(*p)); MarkObject(host, *p); } } @@ -1255,7 +1255,7 @@ class RecordMigratedSlotVisitor : public ObjectVisitor { : collector_(collector) {} inline void VisitPointer(HeapObject* host, Object** p) final { - DCHECK(!Internals::HasWeakHeapObjectTag(*p)); + DCHECK(!HasWeakHeapObjectTag(*p)); RecordMigratedSlot(host, reinterpret_cast(*p), reinterpret_cast
(p)); } @@ -1829,7 +1829,7 @@ class YoungGenerationMarkingVisitor final V8_INLINE void VisitPointer(HeapObject* host, Object** slot) final { Object* target = *slot; - DCHECK(!Internals::HasWeakHeapObjectTag(target)); + DCHECK(!HasWeakHeapObjectTag(target)); if (heap_->InNewSpace(target)) { HeapObject* target_object = HeapObject::cast(target); MarkObjectViaMarkingWorklist(target_object); @@ -3005,7 +3005,7 @@ class PointersUpdatingVisitor : public ObjectVisitor, public RootVisitor { } static inline SlotCallbackResult UpdateStrongSlotInternal(Object** slot) { - DCHECK(!Internals::HasWeakHeapObjectTag(*slot)); + DCHECK(!HasWeakHeapObjectTag(*slot)); return UpdateStrongSlot( reinterpret_cast(slot)); } diff --git a/src/heap/remembered-set.h b/src/heap/remembered-set.h index bafbe06efd..7ede5da713 100644 --- a/src/heap/remembered-set.h +++ b/src/heap/remembered-set.h @@ -282,7 +282,7 @@ class UpdateTypedSlotHelper { Object* old_code = code; SlotCallbackResult result = callback(reinterpret_cast(&code)); - DCHECK(!Internals::HasWeakHeapObjectTag(code)); + DCHECK(!HasWeakHeapObjectTag(code)); if (code != old_code) { Memory::Address_at(entry_address) = reinterpret_cast(code)->entry(); @@ -300,7 +300,7 @@ class UpdateTypedSlotHelper { Object* new_target = old_target; SlotCallbackResult result = callback(reinterpret_cast(&new_target)); - DCHECK(!Internals::HasWeakHeapObjectTag(new_target)); + DCHECK(!HasWeakHeapObjectTag(new_target)); if (new_target != old_target) { rinfo->set_target_address(Code::cast(new_target)->instruction_start()); } @@ -317,7 +317,7 @@ class UpdateTypedSlotHelper { Object* new_target = old_target; SlotCallbackResult result = callback(reinterpret_cast(&new_target)); - DCHECK(!Internals::HasWeakHeapObjectTag(new_target)); + DCHECK(!HasWeakHeapObjectTag(new_target)); if (new_target != old_target) { rinfo->set_target_object(HeapObject::cast(new_target)); } diff --git a/src/heap/scavenger.cc b/src/heap/scavenger.cc index 71e7b082f8..ffd78a64c0 100644 --- a/src/heap/scavenger.cc +++ b/src/heap/scavenger.cc @@ -25,7 +25,7 @@ class IterateAndScavengePromotedObjectsVisitor final : public ObjectVisitor { Object** end) final { for (Object** slot = start; slot < end; ++slot) { Object* target = *slot; - DCHECK(!Internals::HasWeakHeapObjectTag(target)); + DCHECK(!HasWeakHeapObjectTag(target)); if (target->IsHeapObject()) { HandleSlot(host, reinterpret_cast
(slot), HeapObject::cast(target)); @@ -183,7 +183,7 @@ void Scavenger::Finalize() { void RootScavengeVisitor::VisitRootPointer(Root root, const char* description, Object** p) { - DCHECK(!Internals::HasWeakHeapObjectTag(*p)); + DCHECK(!HasWeakHeapObjectTag(*p)); ScavengePointer(p); } @@ -195,7 +195,7 @@ void RootScavengeVisitor::VisitRootPointers(Root root, const char* description, void RootScavengeVisitor::ScavengePointer(Object** p) { Object* object = *p; - DCHECK(!Internals::HasWeakHeapObjectTag(object)); + DCHECK(!HasWeakHeapObjectTag(object)); if (!heap_->InNewSpace(object)) return; scavenger_->ScavengeObject(reinterpret_cast(p), diff --git a/src/objects/maybe-object-inl.h b/src/objects/maybe-object-inl.h index 66d872f7db..290d5c9090 100644 --- a/src/objects/maybe-object-inl.h +++ b/src/objects/maybe-object-inl.h @@ -39,7 +39,7 @@ bool MaybeObject::ToStrongOrWeakHeapObject( if (IsSmi() || IsClearedWeakHeapObject()) { return false; } - *reference_type = Internals::HasWeakHeapObjectTag(this) + *reference_type = HasWeakHeapObjectTag(this) ? HeapObjectReferenceType::WEAK : HeapObjectReferenceType::STRONG; *result = GetHeapObject(); @@ -47,11 +47,11 @@ bool MaybeObject::ToStrongOrWeakHeapObject( } bool MaybeObject::IsStrongHeapObject() { - return !Internals::HasWeakHeapObjectTag(this) && !IsSmi(); + return !HasWeakHeapObjectTag(this) && !IsSmi(); } bool MaybeObject::ToStrongHeapObject(HeapObject** result) { - if (!Internals::HasWeakHeapObjectTag(this) && !IsSmi()) { + if (!HasWeakHeapObjectTag(this) && !IsSmi()) { *result = reinterpret_cast(this); return true; } @@ -59,11 +59,11 @@ bool MaybeObject::ToStrongHeapObject(HeapObject** result) { } bool MaybeObject::IsWeakHeapObject() { - return Internals::HasWeakHeapObjectTag(this) && !IsClearedWeakHeapObject(); + return HasWeakHeapObjectTag(this) && !IsClearedWeakHeapObject(); } bool MaybeObject::ToWeakHeapObject(HeapObject** result) { - if (Internals::HasWeakHeapObjectTag(this) && !IsClearedWeakHeapObject()) { + if (HasWeakHeapObjectTag(this) && !IsClearedWeakHeapObject()) { *result = GetHeapObject(); return true; } @@ -73,8 +73,7 @@ bool MaybeObject::ToWeakHeapObject(HeapObject** result) { HeapObject* MaybeObject::GetHeapObject() { DCHECK(!IsSmi()); DCHECK(!IsClearedWeakHeapObject()); - return Internals::RemoveWeakHeapObjectMask( - reinterpret_cast(this)); + return RemoveWeakHeapObjectMask(reinterpret_cast(this)); } } // namespace internal diff --git a/src/objects/maybe-object.h b/src/objects/maybe-object.h index e22b2bfa95..26d73af368 100644 --- a/src/objects/maybe-object.h +++ b/src/objects/maybe-object.h @@ -23,7 +23,7 @@ class MaybeObject { inline bool IsSmi(Smi** value); bool IsClearedWeakHeapObject() { - return Internals::IsClearedWeakHeapObject(this); + return ::v8::internal::IsClearedWeakHeapObject(this); } inline bool IsStrongOrWeakHeapObject(); @@ -43,13 +43,13 @@ class MaybeObject { } static MaybeObject* FromObject(Object* object) { - DCHECK(!Internals::HasWeakHeapObjectTag(object)); + DCHECK(!HasWeakHeapObjectTag(object)); return reinterpret_cast(object); } static MaybeObject* MakeWeak(MaybeObject* object) { DCHECK(object->IsStrongOrWeakHeapObject()); - return Internals::AddWeakHeapObjectMask(object); + return AddWeakHeapObjectMask(object); } #ifdef VERIFY_HEAP @@ -65,13 +65,13 @@ class MaybeObject { class HeapObjectReference : public MaybeObject { public: static HeapObjectReference* Strong(HeapObject* object) { - DCHECK(!Internals::HasWeakHeapObjectTag(object)); + DCHECK(!HasWeakHeapObjectTag(object)); return reinterpret_cast(object); } static HeapObjectReference* Weak(HeapObject* object) { - DCHECK(!Internals::HasWeakHeapObjectTag(object)); - return Internals::AddWeakHeapObjectMask(object); + DCHECK(!HasWeakHeapObjectTag(object)); + return AddWeakHeapObjectMask(object); } static HeapObjectReference* ClearedValue() { @@ -83,7 +83,7 @@ class HeapObjectReference : public MaybeObject { DCHECK(Internals::HasHeapObjectTag(value)); #ifdef DEBUG - bool weak_before = Internals::HasWeakHeapObjectTag(*slot); + bool weak_before = HasWeakHeapObjectTag(*slot); #endif *slot = reinterpret_cast( @@ -91,7 +91,7 @@ class HeapObjectReference : public MaybeObject { (reinterpret_cast(*slot) & kWeakHeapObjectMask)); #ifdef DEBUG - bool weak_after = Internals::HasWeakHeapObjectTag(*slot); + bool weak_after = HasWeakHeapObjectTag(*slot); DCHECK_EQ(weak_before, weak_after); #endif } diff --git a/src/snapshot/deserializer.cc b/src/snapshot/deserializer.cc index 7254533acc..6ba4b26790 100644 --- a/src/snapshot/deserializer.cc +++ b/src/snapshot/deserializer.cc @@ -292,7 +292,7 @@ HeapObject* Deserializer::GetBackReferencedObject(int space) { } hot_objects_.Add(obj); - DCHECK(!Internals::HasWeakHeapObjectTag(obj)); + DCHECK(!HasWeakHeapObjectTag(obj)); return obj; }