1 /**
2 * Copyright (c) 2021-2022 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 <algorithm>
17
18 #include "libpandabase/macros.h"
19 #include "libpandabase/utils/hash.h"
20 #include "libpandabase/utils/logger.h"
21 #include "libpandafile/class_data_accessor.h"
22 #include "runtime/include/class-inl.h"
23 #include "runtime/include/runtime.h"
24
25 namespace panda {
26
operator <<(std::ostream & os,const Class::State & state)27 std::ostream &operator<<(std::ostream &os, const Class::State &state)
28 {
29 switch (state) {
30 case Class::State::INITIAL: {
31 os << "INITIAL";
32 break;
33 }
34 case Class::State::LOADED: {
35 os << "LOADED";
36 break;
37 }
38 case Class::State::VERIFIED: {
39 os << "VERIFIED";
40 break;
41 }
42 case Class::State::INITIALIZING: {
43 os << "INITIALIZING";
44 break;
45 }
46 case Class::State::ERRONEOUS: {
47 os << "ERRONEOUS";
48 break;
49 }
50 case Class::State::INITIALIZED: {
51 os << "INITIALIZED";
52 break;
53 }
54 default: {
55 UNREACHABLE();
56 break;
57 }
58 }
59
60 return os;
61 }
62
CalcUniqId(const panda_file::File * file,panda_file::File::EntityId fileId)63 Class::UniqId Class::CalcUniqId(const panda_file::File *file, panda_file::File::EntityId fileId)
64 {
65 constexpr uint64_t HALF = 32ULL;
66 uint64_t uid = file->GetUniqId();
67 uid <<= HALF;
68 uid |= fileId.GetOffset();
69 return uid;
70 }
71
CalcUniqId(const uint8_t * descriptor)72 Class::UniqId Class::CalcUniqId(const uint8_t *descriptor)
73 {
74 uint64_t uid = GetHash32String(descriptor);
75 constexpr uint64_t HALF = 32ULL;
76 constexpr uint64_t NO_FILE = 0xFFFFFFFFULL << HALF;
77 uid = NO_FILE | uid;
78 return uid;
79 }
80
CalcUniqId() const81 Class::UniqId Class::CalcUniqId() const
82 {
83 if (pandaFile_ != nullptr) {
84 return CalcUniqId(pandaFile_, fileId_);
85 }
86 return CalcUniqId(descriptor_);
87 }
88
89 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
Class(const uint8_t * descriptor,panda_file::SourceLang lang,uint32_t vtableSize,uint32_t imtSize,uint32_t size)90 Class::Class(const uint8_t *descriptor, panda_file::SourceLang lang, uint32_t vtableSize, uint32_t imtSize,
91 uint32_t size)
92 : BaseClass(lang), descriptor_(descriptor), vtableSize_(vtableSize), imtSize_(imtSize), classSize_(size)
93 {
94 state_ = State::INITIAL;
95 // Initializa all static fields with 0 value.
96 auto staticsOffset = GetStaticFieldsOffset();
97 auto sp = GetClassSpan();
98 ASSERT(sp.size() >= staticsOffset);
99 auto sizeToSet = sp.size() - staticsOffset;
100 if (sizeToSet > 0) {
101 memset_s(&sp[staticsOffset], sizeToSet, 0, sizeToSet);
102 }
103 }
SetState(Class::State state)104 void Class::SetState(Class::State state)
105 {
106 if (state_ == State::ERRONEOUS || state < state_ || (state_ == State::LOADED && state == State::INITIALIZED)) {
107 LOG(FATAL, RUNTIME) << "Invalid class state transition " << state_ << " -> " << state;
108 }
109
110 state_ = state;
111 }
112
GetName() const113 std::string Class::GetName() const
114 {
115 return ClassHelper::GetName(descriptor_);
116 }
117
DumpClass(std::ostream & os,size_t flags)118 void Class::DumpClass(std::ostream &os, size_t flags)
119 {
120 if ((flags & DUMPCLASSFULLDETAILS) == 0) {
121 os << GetDescriptor();
122 if ((flags & DUMPCLASSCLASSLODER) != 0) {
123 LOG(INFO, RUNTIME) << " Panda can't get classloader at now\n";
124 }
125 if ((flags & DUMPCLASSINITIALIZED) != 0) {
126 LOG(INFO, RUNTIME) << " There is no status structure of class in Panda at now\n";
127 }
128 os << "\n";
129 return;
130 }
131 os << "\n";
132 os << "----- " << (IsInterface() ? "interface" : "class") << " "
133 << "'" << GetDescriptor() << "' -----\n";
134 os << " objectSize=" << BaseClass::GetObjectSize() << " \n";
135 os << " accessFlags=" << GetAccessFlags() << " \n";
136 if (IsArrayClass()) {
137 os << " componentType=" << GetComponentType() << "\n";
138 }
139 size_t numDirectInterfaces = this->numIfaces_;
140 if (numDirectInterfaces > 0) {
141 os << " interfaces (" << numDirectInterfaces << "):\n";
142 }
143 if (!IsLoaded()) {
144 os << " class not yet loaded";
145 } else {
146 os << " vtable (" << this->GetVTable().size() << " entries)\n";
147 if (this->numSfields_ > 0) {
148 os << " static fields (" << this->numSfields_ << " entries)\n";
149 }
150 if (this->numFields_ - this->numSfields_ > 0) {
151 os << " instance fields (" << this->numFields_ - this->numSfields_ << " entries)\n";
152 }
153 }
154 }
155
FromClassObject(const ObjectHeader * obj)156 Class *Class::FromClassObject(const ObjectHeader *obj)
157 {
158 return Runtime::GetCurrent()->GetClassLinker()->ObjectToClass(obj);
159 }
160
GetClassObjectSizeFromClass(Class * cls,panda_file::SourceLang lang)161 size_t Class::GetClassObjectSizeFromClass(Class *cls, panda_file::SourceLang lang)
162 {
163 return Runtime::GetCurrent()->GetClassLinker()->GetClassObjectSize(cls, lang);
164 }
165
166 } // namespace panda
167