1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ECMASCRIPT_MEM_HEAP_VERIFICATION_H
17 #define ECMASCRIPT_MEM_HEAP_VERIFICATION_H
18 
19 #include <cstdint>
20 
21 #include "ecmascript/js_tagged_value.h"
22 #include "ecmascript/mem/heap.h"
23 #include "ecmascript/mem/object_xray.h"
24 #include "ecmascript/mem/mem.h"
25 #include "ecmascript/mem/slots.h"
26 
27 namespace panda::ecmascript {
28 static constexpr uint32_t INVALID_THRESHOLD = 0x40000;
29 
30 class VerifyScope {
31 public:
VerifyScope(BaseHeap *heap)32     VerifyScope(BaseHeap *heap) : heap_(heap)
33     {
34         heap_->SetVerifying(true);
35     }
36 
~VerifyScope()37     ~VerifyScope()
38     {
39         heap_->SetVerifying(false);
40     }
41 private:
42     BaseHeap *heap_ {nullptr};
43 };
44 
45 // Verify the object body
46 // NOLINTNEXTLINE(cppcoreguidelines-special-member-functions, hicpp-special-member-functions)
47 class VerifyObjectVisitor {
48 public:
49     // Only used for verify InactiveSemiSpace
50     static void VerifyInactiveSemiSpaceMarkedObject(const BaseHeap *heap, void *addr);
51 
VerifyObjectVisitor(const BaseHeap *heap, size_t *failCount, VerifyKind verifyKind = VerifyKind::VERIFY_PRE_GC)52     VerifyObjectVisitor(const BaseHeap *heap, size_t *failCount,
53                         VerifyKind verifyKind = VerifyKind::VERIFY_PRE_GC)
54         : heap_(heap), failCount_(failCount), verifyKind_(verifyKind)
55     {
56     }
57     ~VerifyObjectVisitor() = default;
58 
operator ()(TaggedObject *obj)59     void operator()(TaggedObject *obj)
60     {
61         VisitAllObjects(obj);
62     }
63 
64     void operator()(TaggedObject *obj, JSTaggedValue value);
65 
GetFailedCount() const66     size_t GetFailedCount() const
67     {
68         return *failCount_;
69     }
70 
71 private:
72     void VisitAllObjects(TaggedObject *obj);
73     void VerifyObjectSlotLegal(ObjectSlot slot, TaggedObject *obj) const;
74     void VerifyHeapObjectSlotLegal(ObjectSlot slot, JSTaggedValue value, TaggedObject *obj) const;
75     void VerifyMarkEden(TaggedObject *obj, ObjectSlot slot, TaggedObject *value) const;
76     void VerifyEvacuateEden(TaggedObject *obj, ObjectSlot slot, TaggedObject *value) const;
77     void VerifyMarkYoung(TaggedObject *obj, ObjectSlot slot, TaggedObject *value) const;
78     void VerifyEvacuateYoung(TaggedObject *obj, ObjectSlot slot, TaggedObject *value) const;
79     void VerifyMarkFull(TaggedObject *obj, ObjectSlot slot, TaggedObject *value) const;
80     void VerifyEvacuateOld(TaggedObject *obj, ObjectSlot slot, TaggedObject *value) const;
81     void VerifyEvacuateFull(TaggedObject *obj, ObjectSlot slot, TaggedObject *value) const;
82     void VerifySharedRSetPostFullGC(TaggedObject *obj, ObjectSlot slot, TaggedObject *value) const;
83     void VerifySharedObjectReference(TaggedObject *obj, ObjectSlot slot, TaggedObject *value) const;
84 
85     const BaseHeap* const heap_ {nullptr};
86     size_t* const failCount_ {nullptr};
87     VerifyKind verifyKind_;
88 };
89 
90 class Verification {
91 public:
Verification(Heap *heap, VerifyKind verifyKind = VerifyKind::VERIFY_PRE_GC)92     explicit Verification(Heap *heap, VerifyKind verifyKind = VerifyKind::VERIFY_PRE_GC)
93         : heap_(heap), verifyKind_(verifyKind) {}
94     ~Verification() = default;
95 
96     static void VerifyMark(Heap *heap);
97     static void VerifyEvacuate(Heap *heap);
98     void VerifyAll() const;
99 
100     size_t VerifyRoot() const;
101     size_t VerifyHeap() const;
102     size_t VerifyOldToNewRSet() const;
103 private:
104     void VerifyObjectSlot(const ObjectSlot &slot, size_t *failCount) const;
105 
106     NO_COPY_SEMANTIC(Verification);
107     NO_MOVE_SEMANTIC(Verification);
108 
109     Heap *heap_ {nullptr};
110     VerifyKind verifyKind_;
111 };
112 
113 class SharedHeapVerification {
114 public:
SharedHeapVerification(SharedHeap *heap, VerifyKind verifyKind)115     explicit SharedHeapVerification(SharedHeap *heap, VerifyKind verifyKind)
116         : sHeap_(heap), verifyKind_(verifyKind) {}
117     ~SharedHeapVerification() = default;
118 
119     void VerifyAll() const;
120     void VerifyMark(bool cm) const;
121     void VerifySweep(bool cm) const;
122 
123     size_t VerifyRoot() const;
124     size_t VerifyHeap() const;
125 private:
126     void VerifyObjectSlot(const ObjectSlot &slot, size_t *failCount) const;
127 
128     NO_COPY_SEMANTIC(SharedHeapVerification);
129     NO_MOVE_SEMANTIC(SharedHeapVerification);
130 
131     SharedHeap *sHeap_ {nullptr};
132     VerifyKind verifyKind_;
133 };
134 }  // namespace panda::ecmascript
135 
136 #endif  // ECMASCRIPT_MEM_HEAP_VERIFICATION_H
137