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/base/optional.h" 11 #include "src/heap/base/stack.h" 12 #include "src/heap/cppgc/heap-object-header.h" 13 #include "src/heap/cppgc/heap-visitor.h" 14 #include "src/heap/cppgc/heap.h" 15 #include "src/heap/cppgc/visitor.h" 16 17 namespace cppgc { 18 namespace internal { 19 20 class VerificationState { 21 public: 22 void VerifyMarked(const void*) const; SetCurrentParent(const HeapObjectHeader * header)23 void SetCurrentParent(const HeapObjectHeader* header) { parent_ = header; } 24 25 // No parent means parent was on stack. IsParentOnStack()26 bool IsParentOnStack() const { return !parent_; } 27 28 private: 29 const HeapObjectHeader* parent_ = nullptr; 30 }; 31 32 class V8_EXPORT_PRIVATE MarkingVerifierBase 33 : private HeapVisitor<MarkingVerifierBase>, 34 public ConservativeTracingVisitor, 35 public heap::base::StackVisitor { 36 friend class HeapVisitor<MarkingVerifierBase>; 37 38 public: 39 ~MarkingVerifierBase() override = default; 40 41 MarkingVerifierBase(const MarkingVerifierBase&) = delete; 42 MarkingVerifierBase& operator=(const MarkingVerifierBase&) = delete; 43 44 void Run(Heap::Config::StackState, uintptr_t, v8::base::Optional<size_t>); 45 46 protected: 47 MarkingVerifierBase(HeapBase&, Heap::Config::CollectionType, 48 VerificationState&, std::unique_ptr<cppgc::Visitor>); 49 50 private: 51 void VisitInConstructionConservatively(HeapObjectHeader&, 52 TraceConservativelyCallback) final; 53 void VisitPointer(const void*) final; 54 55 bool VisitHeapObjectHeader(HeapObjectHeader&); 56 57 VerificationState& verification_state_; 58 std::unique_ptr<cppgc::Visitor> visitor_; 59 60 std::unordered_set<const HeapObjectHeader*> in_construction_objects_heap_; 61 std::unordered_set<const HeapObjectHeader*> in_construction_objects_stack_; 62 std::unordered_set<const HeapObjectHeader*>* in_construction_objects_ = 63 &in_construction_objects_heap_; 64 size_t verifier_found_marked_bytes_ = 0; 65 bool verifier_found_marked_bytes_are_exact_ = true; 66 Heap::Config::CollectionType collection_type_; 67 }; 68 69 class V8_EXPORT_PRIVATE MarkingVerifier final : public MarkingVerifierBase { 70 public: 71 MarkingVerifier(HeapBase&, Heap::Config::CollectionType); 72 ~MarkingVerifier() final = default; 73 74 private: 75 VerificationState state_; 76 }; 77 78 } // namespace internal 79 } // namespace cppgc 80 81 #endif // V8_HEAP_CPPGC_MARKING_VERIFIER_H_ 82