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(Heap * heap)32 VerifyScope(Heap *heap) : heap_(heap) 33 { 34 heap_->SetVerifying(true); 35 } 36 ~VerifyScope()37 ~VerifyScope() 38 { 39 heap_->SetVerifying(false); 40 } 41 private: 42 Heap *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 Heap *heap, void *addr); 51 52 VerifyObjectVisitor(const Heap *heap, size_t *failCount, 53 VerifyKind verifyKind = VerifyKind::VERIFY_PRE_GC) heap_(heap)54 : heap_(heap), failCount_(failCount), objXRay_(heap->GetEcmaVM()), verifyKind_(verifyKind) 55 { 56 } 57 ~VerifyObjectVisitor() = default; 58 operator()59 void operator()(TaggedObject *obj) 60 { 61 VisitAllObjects(obj); 62 } 63 64 void operator()(TaggedObject *obj, JSTaggedValue value); 65 GetFailedCount()66 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 VerifyMarkYoung(TaggedObject *obj, ObjectSlot slot, TaggedObject *value) const; 75 void VerifyEvacuateYoung(TaggedObject *obj, ObjectSlot slot, TaggedObject *value) const; 76 void VerifyMarkFull(TaggedObject *obj, ObjectSlot slot, TaggedObject *value) const; 77 void VerifyEvacuateOld(TaggedObject *obj, ObjectSlot slot, TaggedObject *value) const; 78 void VerifyEvacuateFull(TaggedObject *obj, ObjectSlot slot, TaggedObject *value) const; 79 80 const Heap* const heap_ {nullptr}; 81 size_t* const failCount_ {nullptr}; 82 ObjectXRay objXRay_; 83 VerifyKind verifyKind_; 84 }; 85 86 class Verification { 87 public: 88 explicit Verification(Heap *heap, VerifyKind verifyKind = VerifyKind::VERIFY_PRE_GC) heap_(heap)89 : heap_(heap), objXRay_(heap->GetEcmaVM()), verifyKind_(verifyKind) {} 90 ~Verification() = default; 91 92 void VerifyAll() const; 93 94 size_t VerifyRoot() const; 95 size_t VerifyHeap() const; 96 size_t VerifyOldToNewRSet() const; 97 private: 98 void VerifyObjectSlot(const ObjectSlot &slot, size_t *failCount) const; 99 100 NO_COPY_SEMANTIC(Verification); 101 NO_MOVE_SEMANTIC(Verification); 102 103 Heap *heap_ {nullptr}; 104 ObjectXRay objXRay_; 105 VerifyKind verifyKind_; 106 }; 107 } // namespace panda::ecmascript 108 109 #endif // ECMASCRIPT_MEM_HEAP_VERIFICATION_H 110