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
27namespace panda::ecmascript {
28static constexpr uint32_t INVALID_THRESHOLD = 0x40000;
29
30class VerifyScope {
31public:
32    VerifyScope(BaseHeap *heap) : heap_(heap)
33    {
34        heap_->SetVerifying(true);
35    }
36
37    ~VerifyScope()
38    {
39        heap_->SetVerifying(false);
40    }
41private:
42    BaseHeap *heap_ {nullptr};
43};
44
45// Verify the object body
46// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions, hicpp-special-member-functions)
47class VerifyObjectVisitor {
48public:
49    // Only used for verify InactiveSemiSpace
50    static void VerifyInactiveSemiSpaceMarkedObject(const BaseHeap *heap, void *addr);
51
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
59    void operator()(TaggedObject *obj)
60    {
61        VisitAllObjects(obj);
62    }
63
64    void operator()(TaggedObject *obj, JSTaggedValue value);
65
66    size_t GetFailedCount() const
67    {
68        return *failCount_;
69    }
70
71private:
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
90class Verification {
91public:
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;
103private:
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
113class SharedHeapVerification {
114public:
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;
125private:
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