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 #include "runtime/include/language_context.h"
17 #include "runtime/include/class_root.h"
18 #include "runtime/include/runtime.h"
19 #include "runtime/include/panda_vm.h"
20 #include "runtime/mem/gc/gc_root.h"
21 #include "runtime/mem/heap_manager.h"
22 #include "runtime/mem/heap_verifier.h"
23
24 namespace panda::mem {
25
26 // Should be called only with MutatorLock held
27 template <class LanguageConfig>
VerifyAllPaused() const28 size_t HeapVerifier<LanguageConfig>::VerifyAllPaused() const
29 {
30 Rendezvous *rendezvous = Runtime::GetCurrent()->GetPandaVM()->GetRendezvous();
31 rendezvous->SafepointBegin();
32 size_t fail_count = VerifyAll();
33 rendezvous->SafepointEnd();
34 return fail_count;
35 }
36
37 template <LangTypeT LangType>
operator ()(ObjectHeader * obj)38 void HeapObjectVerifier<LangType>::operator()(ObjectHeader *obj)
39 {
40 HeapReferenceVerifier ref_verifier(HEAP, FAIL_COUNT);
41 ObjectHelpers<LangType>::TraverseAllObjects(obj, ref_verifier);
42 }
43
operator ()(ObjectHeader * object_header,ObjectHeader * referent)44 void HeapReferenceVerifier::operator()([[maybe_unused]] ObjectHeader *object_header, ObjectHeader *referent)
45 {
46 auto obj_allocator = HEAP->GetObjectAllocator().AsObjectAllocator();
47 if (!obj_allocator->IsLive(referent)) {
48 LOG_HEAP_VERIFIER(ERROR) << "Heap corruption found! Heap references a dead object at " << referent;
49 ++(*FAIL_COUNT);
50 }
51 }
52
operator ()(const GCRoot & root)53 void HeapReferenceVerifier::operator()(const GCRoot &root)
54 {
55 auto obj_allocator = HEAP->GetObjectAllocator().AsObjectAllocator();
56 auto referent = root.GetObjectHeader();
57 if (!obj_allocator->IsLive(referent)) {
58 LOG_HEAP_VERIFIER(ERROR) << "Heap corruption found! Root references a dead object at " << referent;
59 ++(*FAIL_COUNT);
60 }
61 }
62
63 template <class LanguageConfig>
IsValidObjectAddress(void * addr) const64 bool HeapVerifier<LanguageConfig>::IsValidObjectAddress(void *addr) const
65 {
66 return IsAligned<DEFAULT_ALIGNMENT_IN_BYTES>(ToUintPtr(addr)) && IsHeapAddress(addr);
67 }
68
69 template <class LanguageConfig>
IsHeapAddress(void * addr) const70 bool HeapVerifier<LanguageConfig>::IsHeapAddress(void *addr) const
71 {
72 return heap_->GetObjectAllocator().AsObjectAllocator()->ContainObject(reinterpret_cast<ObjectHeader *>(addr));
73 }
74
75 template <class LanguageConfig>
VerifyHeap() const76 size_t HeapVerifier<LanguageConfig>::VerifyHeap() const
77 {
78 return heap_->VerifyHeapReferences();
79 }
80
81 template <class LanguageConfig>
VerifyRoot() const82 size_t HeapVerifier<LanguageConfig>::VerifyRoot() const
83 {
84 RootManager<LanguageConfig> root_manager;
85 size_t fail_count = 0;
86 root_manager.SetPandaVM(heap_->GetPandaVM());
87 root_manager.VisitNonHeapRoots([this, &fail_count](const GCRoot &root) {
88 if (root.GetType() == RootType::ROOT_FRAME || root.GetType() == RootType::ROOT_THREAD) {
89 auto base_cls = root.GetObjectHeader()->ClassAddr<BaseClass>();
90 if (!(!base_cls->IsDynamicClass() && static_cast<Class *>(base_cls)->IsClassClass())) {
91 HeapReferenceVerifier(heap_, &fail_count)(root);
92 }
93 }
94 });
95
96 return fail_count;
97 }
98
99 template class HeapVerifier<PandaAssemblyLanguageConfig>;
100
101 } // namespace panda::mem
102