1 // Copyright 2020 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_HEAP_CPPGC_MARKING_VERIFIER_H_ 6 #define V8_HEAP_CPPGC_MARKING_VERIFIER_H_ 7 8 #include <unordered_set> 9 10 #include "src/heap/base/stack.h" 11 #include "src/heap/cppgc/heap-object-header.h" 12 #include "src/heap/cppgc/heap-visitor.h" 13 #include "src/heap/cppgc/heap.h" 14 #include "src/heap/cppgc/visitor.h" 15 16 namespace cppgc { 17 namespace internal { 18 19 class VerificationState { 20 public: 21 void VerifyMarked(const void*) const; SetCurrentParent(const HeapObjectHeader * header)22 void SetCurrentParent(const HeapObjectHeader* header) { parent_ = header; } 23 24 private: 25 const HeapObjectHeader* parent_ = nullptr; 26 }; 27 28 class V8_EXPORT_PRIVATE MarkingVerifierBase 29 : private HeapVisitor<MarkingVerifierBase>, 30 public ConservativeTracingVisitor, 31 public heap::base::StackVisitor { 32 friend class HeapVisitor<MarkingVerifierBase>; 33 34 public: 35 ~MarkingVerifierBase() override = default; 36 37 MarkingVerifierBase(const MarkingVerifierBase&) = delete; 38 MarkingVerifierBase& operator=(const MarkingVerifierBase&) = delete; 39 40 void Run(Heap::Config::StackState); 41 42 protected: 43 MarkingVerifierBase(HeapBase&, std::unique_ptr<cppgc::Visitor>); 44 45 virtual void SetCurrentParent(const HeapObjectHeader*) = 0; 46 47 private: 48 void VisitInConstructionConservatively(HeapObjectHeader&, 49 TraceConservativelyCallback) final; 50 void VisitPointer(const void*) final; 51 52 bool VisitHeapObjectHeader(HeapObjectHeader*); 53 54 std::unique_ptr<cppgc::Visitor> visitor_; 55 56 std::unordered_set<const HeapObjectHeader*> in_construction_objects_heap_; 57 std::unordered_set<const HeapObjectHeader*> in_construction_objects_stack_; 58 std::unordered_set<const HeapObjectHeader*>* in_construction_objects_ = 59 &in_construction_objects_heap_; 60 }; 61 62 class V8_EXPORT_PRIVATE MarkingVerifier final : public MarkingVerifierBase { 63 public: 64 explicit MarkingVerifier(HeapBase&); 65 ~MarkingVerifier() final = default; 66 67 void SetCurrentParent(const HeapObjectHeader*) final; 68 69 private: 70 VerificationState state_; 71 }; 72 73 } // namespace internal 74 } // namespace cppgc 75 76 #endif // V8_HEAP_CPPGC_MARKING_VERIFIER_H_ 77