1
2
3 // Copyright 2020 the V8 project authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
6
7 #include "src/heap/cppgc-js/unified-heap-marking-verifier.h"
8
9 #include "include/v8-cppgc.h"
10 #include "src/heap/cppgc/marking-verifier.h"
11
12 namespace v8 {
13 namespace internal {
14
15 namespace {
16
17 class UnifiedHeapVerificationVisitor final : public JSVisitor {
18 public:
UnifiedHeapVerificationVisitor(cppgc::internal::VerificationState & state)19 explicit UnifiedHeapVerificationVisitor(
20 cppgc::internal::VerificationState& state)
21 : JSVisitor(cppgc::internal::VisitorFactory::CreateKey()),
22 state_(state) {}
23
Visit(const void *,cppgc::TraceDescriptor desc)24 void Visit(const void*, cppgc::TraceDescriptor desc) final {
25 state_.VerifyMarked(desc.base_object_payload);
26 }
27
VisitWeak(const void *,cppgc::TraceDescriptor desc,cppgc::WeakCallback,const void *)28 void VisitWeak(const void*, cppgc::TraceDescriptor desc, cppgc::WeakCallback,
29 const void*) final {
30 // Weak objects should have been cleared at this point. As a consequence,
31 // all objects found through weak references have to point to live objects
32 // at this point.
33 state_.VerifyMarked(desc.base_object_payload);
34 }
35
VisitWeakContainer(const void * object,cppgc::TraceDescriptor,cppgc::TraceDescriptor weak_desc,cppgc::WeakCallback,const void *)36 void VisitWeakContainer(const void* object, cppgc::TraceDescriptor,
37 cppgc::TraceDescriptor weak_desc, cppgc::WeakCallback,
38 const void*) final {
39 if (!object) return;
40
41 // Contents of weak containers are found themselves through page iteration
42 // and are treated strongly, similar to how they are treated strongly when
43 // found through stack scanning. The verification here only makes sure that
44 // the container itself is properly marked.
45 state_.VerifyMarked(weak_desc.base_object_payload);
46 }
47
Visit(const TracedReferenceBase & ref)48 void Visit(const TracedReferenceBase& ref) final {
49 // TODO(chromium:1056170): Verify V8 object is indeed marked.
50 }
51
52 private:
53 cppgc::internal::VerificationState& state_;
54 };
55
56 } // namespace
57
UnifiedHeapMarkingVerifier(cppgc::internal::HeapBase & heap_base,cppgc::internal::Heap::Config::CollectionType collection_type)58 UnifiedHeapMarkingVerifier::UnifiedHeapMarkingVerifier(
59 cppgc::internal::HeapBase& heap_base,
60 cppgc::internal::Heap::Config::CollectionType collection_type)
61 : MarkingVerifierBase(
62 heap_base, collection_type, state_,
63 std::make_unique<UnifiedHeapVerificationVisitor>(state_)) {}
64
65 } // namespace internal
66 } // namespace v8
67