• 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 PANDA_RUNTIME_MEM_HEAP_VERIFIER_H_
17 #define PANDA_RUNTIME_MEM_HEAP_VERIFIER_H_
18 
19 #include "libpandabase/utils/bit_utils.h"
20 #include "libpandabase/utils/logger.h"
21 #include "runtime/mem/object_helpers.h"
22 #include "runtime/mem/rendezvous.h"
23 
24 namespace panda::mem {
25 
26 class HeapManager;
27 class GCRoot;
28 
29 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
30 #define LOG_HEAP_VERIFIER(level) LOG(level, RUNTIME) << "HEAP_VERIFIER: "
31 
32 /**
33  * HeapReferenceVerifier checks reference checks if the referent is with the heap and it is live.
34  */
35 class HeapReferenceVerifier {
36 public:
HeapReferenceVerifier(HeapManager * heap,size_t * count)37     explicit HeapReferenceVerifier(HeapManager *heap, size_t *count) : HEAP(heap), FAIL_COUNT(count) {}
38     ~HeapReferenceVerifier() = default;
39     DEFAULT_MOVE_CTOR(HeapReferenceVerifier)
40     DEFAULT_COPY_CTOR(HeapReferenceVerifier)
41     NO_MOVE_OPERATOR(HeapReferenceVerifier);
42     NO_COPY_OPERATOR(HeapReferenceVerifier);
43 
44     void operator()(ObjectHeader *object_header, ObjectHeader *referent);
45 
46     void operator()(const GCRoot &root);
47 
48 private:
49     HeapManager *const HEAP {nullptr};
50     size_t *const FAIL_COUNT {nullptr};
51 };
52 
53 /**
54  * HeapObjectVerifier iterates over HeapManager's allocated objects. If an object contains reference, it checks if the
55  * referent is with the heap and it is live.
56  */
57 template <LangTypeT LangType = LANG_TYPE_STATIC>
58 class HeapObjectVerifier {
59 public:
60     HeapObjectVerifier() = delete;
61 
HeapObjectVerifier(HeapManager * heap,size_t * count)62     HeapObjectVerifier(HeapManager *heap, size_t *count) : HEAP(heap), FAIL_COUNT(count) {}
63 
64     void operator()(ObjectHeader *obj);
65 
GetFailCount()66     size_t GetFailCount() const
67     {
68         return *FAIL_COUNT;
69     }
70 
71 private:
72     HeapManager *const HEAP {nullptr};
73     size_t *const FAIL_COUNT {nullptr};
74 };
75 
76 /**
77  * A class to query address validity.
78  */
79 template <class LanguageConfig>
80 class HeapVerifier {
81 public:
HeapVerifier(HeapManager * heap)82     explicit HeapVerifier(HeapManager *heap) : heap_(heap) {}
83     ~HeapVerifier() = default;
84     DEFAULT_MOVE_SEMANTIC(HeapVerifier);
85     DEFAULT_COPY_SEMANTIC(HeapVerifier);
86 
87     bool IsValidObjectAddress(void *addr) const;
88 
89     bool IsHeapAddress(void *addr) const;
90 
91     size_t VerifyRoot() const;
92 
93     size_t VerifyHeap() const;
94 
VerifyAll()95     size_t VerifyAll() const
96     {
97         return VerifyRoot() + VerifyHeap();
98     }
99 
100     size_t VerifyAllPaused() const;
101 
102 private:
103     HeapManager *heap_;
104 };
105 
106 }  // namespace panda::mem
107 
108 #endif  // PANDA_RUNTIME_MEM_HEAP_VERIFIER_H_
109