• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Verify the object body
29 // NOLINTNEXTLINE(cppcoreguidelines-special-member-functions, hicpp-special-member-functions)
30 class VerifyObjectVisitor {
31 public:
VerifyObjectVisitor(const Heap * heap,size_t * failCount)32     VerifyObjectVisitor(const Heap *heap, size_t *failCount)
33         : heap_(heap), failCount_(failCount), objXRay_(heap->GetEcmaVM())
34     {
35     }
36     ~VerifyObjectVisitor() = default;
37 
operator()38     void operator()(TaggedObject *obj)
39     {
40         VisitAllObjects(obj);
41     }
42 
43     void operator()(TaggedObject *obj, JSTaggedValue value);
44 
GetFailedCount()45     size_t GetFailedCount() const
46     {
47         return *failCount_;
48     }
49 
50 private:
51     void VisitAllObjects(TaggedObject *obj);
52     void VisitObject(ObjectSlot slot);
53 
54     const Heap* const heap_ {nullptr};
55     size_t* const failCount_ {nullptr};
56     ObjectXRay objXRay_;
57 };
58 
59 class Verification {
60 public:
Verification(const Heap * heap)61     explicit Verification(const Heap *heap) : heap_(heap), objXRay_(heap->GetEcmaVM()) {}
62     ~Verification() = default;
63 
VerifyAll()64     size_t VerifyAll() const
65     {
66         size_t result = VerifyRoot();
67         result += VerifyHeap();
68         return result;
69     }
70 
71     size_t VerifyRoot() const;
72     size_t VerifyHeap() const;
73     size_t VerifyOldToNewRSet() const;
74 private:
75     void VerifyObjectSlot(const ObjectSlot &slot, size_t *failCount) const;
76 
77     NO_COPY_SEMANTIC(Verification);
78     NO_MOVE_SEMANTIC(Verification);
79 
80     const Heap *heap_ {nullptr};
81     ObjectXRay objXRay_;
82 };
83 }  // namespace panda::ecmascript
84 
85 #endif  // ECMASCRIPT_MEM_HEAP_VERIFICATION_H
86