14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_MEM_HEAP_VERIFICATION_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_MEM_HEAP_VERIFICATION_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include <cstdint>
204514f5e3Sopenharmony_ci
214514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value.h"
224514f5e3Sopenharmony_ci#include "ecmascript/mem/heap.h"
234514f5e3Sopenharmony_ci#include "ecmascript/mem/object_xray.h"
244514f5e3Sopenharmony_ci#include "ecmascript/mem/mem.h"
254514f5e3Sopenharmony_ci#include "ecmascript/mem/slots.h"
264514f5e3Sopenharmony_ci
274514f5e3Sopenharmony_cinamespace panda::ecmascript {
284514f5e3Sopenharmony_cistatic constexpr uint32_t INVALID_THRESHOLD = 0x40000;
294514f5e3Sopenharmony_ci
304514f5e3Sopenharmony_ciclass VerifyScope {
314514f5e3Sopenharmony_cipublic:
324514f5e3Sopenharmony_ci    VerifyScope(BaseHeap *heap) : heap_(heap)
334514f5e3Sopenharmony_ci    {
344514f5e3Sopenharmony_ci        heap_->SetVerifying(true);
354514f5e3Sopenharmony_ci    }
364514f5e3Sopenharmony_ci
374514f5e3Sopenharmony_ci    ~VerifyScope()
384514f5e3Sopenharmony_ci    {
394514f5e3Sopenharmony_ci        heap_->SetVerifying(false);
404514f5e3Sopenharmony_ci    }
414514f5e3Sopenharmony_ciprivate:
424514f5e3Sopenharmony_ci    BaseHeap *heap_ {nullptr};
434514f5e3Sopenharmony_ci};
444514f5e3Sopenharmony_ci
454514f5e3Sopenharmony_ci// Verify the object body
464514f5e3Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions, hicpp-special-member-functions)
474514f5e3Sopenharmony_ciclass VerifyObjectVisitor {
484514f5e3Sopenharmony_cipublic:
494514f5e3Sopenharmony_ci    // Only used for verify InactiveSemiSpace
504514f5e3Sopenharmony_ci    static void VerifyInactiveSemiSpaceMarkedObject(const BaseHeap *heap, void *addr);
514514f5e3Sopenharmony_ci
524514f5e3Sopenharmony_ci    VerifyObjectVisitor(const BaseHeap *heap, size_t *failCount,
534514f5e3Sopenharmony_ci                        VerifyKind verifyKind = VerifyKind::VERIFY_PRE_GC)
544514f5e3Sopenharmony_ci        : heap_(heap), failCount_(failCount), verifyKind_(verifyKind)
554514f5e3Sopenharmony_ci    {
564514f5e3Sopenharmony_ci    }
574514f5e3Sopenharmony_ci    ~VerifyObjectVisitor() = default;
584514f5e3Sopenharmony_ci
594514f5e3Sopenharmony_ci    void operator()(TaggedObject *obj)
604514f5e3Sopenharmony_ci    {
614514f5e3Sopenharmony_ci        VisitAllObjects(obj);
624514f5e3Sopenharmony_ci    }
634514f5e3Sopenharmony_ci
644514f5e3Sopenharmony_ci    void operator()(TaggedObject *obj, JSTaggedValue value);
654514f5e3Sopenharmony_ci
664514f5e3Sopenharmony_ci    size_t GetFailedCount() const
674514f5e3Sopenharmony_ci    {
684514f5e3Sopenharmony_ci        return *failCount_;
694514f5e3Sopenharmony_ci    }
704514f5e3Sopenharmony_ci
714514f5e3Sopenharmony_ciprivate:
724514f5e3Sopenharmony_ci    void VisitAllObjects(TaggedObject *obj);
734514f5e3Sopenharmony_ci    void VerifyObjectSlotLegal(ObjectSlot slot, TaggedObject *obj) const;
744514f5e3Sopenharmony_ci    void VerifyHeapObjectSlotLegal(ObjectSlot slot, JSTaggedValue value, TaggedObject *obj) const;
754514f5e3Sopenharmony_ci    void VerifyMarkEden(TaggedObject *obj, ObjectSlot slot, TaggedObject *value) const;
764514f5e3Sopenharmony_ci    void VerifyEvacuateEden(TaggedObject *obj, ObjectSlot slot, TaggedObject *value) const;
774514f5e3Sopenharmony_ci    void VerifyMarkYoung(TaggedObject *obj, ObjectSlot slot, TaggedObject *value) const;
784514f5e3Sopenharmony_ci    void VerifyEvacuateYoung(TaggedObject *obj, ObjectSlot slot, TaggedObject *value) const;
794514f5e3Sopenharmony_ci    void VerifyMarkFull(TaggedObject *obj, ObjectSlot slot, TaggedObject *value) const;
804514f5e3Sopenharmony_ci    void VerifyEvacuateOld(TaggedObject *obj, ObjectSlot slot, TaggedObject *value) const;
814514f5e3Sopenharmony_ci    void VerifyEvacuateFull(TaggedObject *obj, ObjectSlot slot, TaggedObject *value) const;
824514f5e3Sopenharmony_ci    void VerifySharedRSetPostFullGC(TaggedObject *obj, ObjectSlot slot, TaggedObject *value) const;
834514f5e3Sopenharmony_ci    void VerifySharedObjectReference(TaggedObject *obj, ObjectSlot slot, TaggedObject *value) const;
844514f5e3Sopenharmony_ci
854514f5e3Sopenharmony_ci    const BaseHeap* const heap_ {nullptr};
864514f5e3Sopenharmony_ci    size_t* const failCount_ {nullptr};
874514f5e3Sopenharmony_ci    VerifyKind verifyKind_;
884514f5e3Sopenharmony_ci};
894514f5e3Sopenharmony_ci
904514f5e3Sopenharmony_ciclass Verification {
914514f5e3Sopenharmony_cipublic:
924514f5e3Sopenharmony_ci    explicit Verification(Heap *heap, VerifyKind verifyKind = VerifyKind::VERIFY_PRE_GC)
934514f5e3Sopenharmony_ci        : heap_(heap), verifyKind_(verifyKind) {}
944514f5e3Sopenharmony_ci    ~Verification() = default;
954514f5e3Sopenharmony_ci
964514f5e3Sopenharmony_ci    static void VerifyMark(Heap *heap);
974514f5e3Sopenharmony_ci    static void VerifyEvacuate(Heap *heap);
984514f5e3Sopenharmony_ci    void VerifyAll() const;
994514f5e3Sopenharmony_ci
1004514f5e3Sopenharmony_ci    size_t VerifyRoot() const;
1014514f5e3Sopenharmony_ci    size_t VerifyHeap() const;
1024514f5e3Sopenharmony_ci    size_t VerifyOldToNewRSet() const;
1034514f5e3Sopenharmony_ciprivate:
1044514f5e3Sopenharmony_ci    void VerifyObjectSlot(const ObjectSlot &slot, size_t *failCount) const;
1054514f5e3Sopenharmony_ci
1064514f5e3Sopenharmony_ci    NO_COPY_SEMANTIC(Verification);
1074514f5e3Sopenharmony_ci    NO_MOVE_SEMANTIC(Verification);
1084514f5e3Sopenharmony_ci
1094514f5e3Sopenharmony_ci    Heap *heap_ {nullptr};
1104514f5e3Sopenharmony_ci    VerifyKind verifyKind_;
1114514f5e3Sopenharmony_ci};
1124514f5e3Sopenharmony_ci
1134514f5e3Sopenharmony_ciclass SharedHeapVerification {
1144514f5e3Sopenharmony_cipublic:
1154514f5e3Sopenharmony_ci    explicit SharedHeapVerification(SharedHeap *heap, VerifyKind verifyKind)
1164514f5e3Sopenharmony_ci        : sHeap_(heap), verifyKind_(verifyKind) {}
1174514f5e3Sopenharmony_ci    ~SharedHeapVerification() = default;
1184514f5e3Sopenharmony_ci
1194514f5e3Sopenharmony_ci    void VerifyAll() const;
1204514f5e3Sopenharmony_ci    void VerifyMark(bool cm) const;
1214514f5e3Sopenharmony_ci    void VerifySweep(bool cm) const;
1224514f5e3Sopenharmony_ci
1234514f5e3Sopenharmony_ci    size_t VerifyRoot() const;
1244514f5e3Sopenharmony_ci    size_t VerifyHeap() const;
1254514f5e3Sopenharmony_ciprivate:
1264514f5e3Sopenharmony_ci    void VerifyObjectSlot(const ObjectSlot &slot, size_t *failCount) const;
1274514f5e3Sopenharmony_ci
1284514f5e3Sopenharmony_ci    NO_COPY_SEMANTIC(SharedHeapVerification);
1294514f5e3Sopenharmony_ci    NO_MOVE_SEMANTIC(SharedHeapVerification);
1304514f5e3Sopenharmony_ci
1314514f5e3Sopenharmony_ci    SharedHeap *sHeap_ {nullptr};
1324514f5e3Sopenharmony_ci    VerifyKind verifyKind_;
1334514f5e3Sopenharmony_ci};
1344514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
1354514f5e3Sopenharmony_ci
1364514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_MEM_HEAP_VERIFICATION_H
137