• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2024 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 ark {
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 = 0;
75     uid = GetHash32String(descriptor);
76     constexpr uint64_t HALF = 32ULL;
77     constexpr uint64_t NO_FILE = 0xFFFFFFFFULL << HALF;
78     uid = NO_FILE | uid;
79     return uid;
80 }
81 
CalcUniqId() const82 Class::UniqId Class::CalcUniqId() const
83 {
84     if (pandaFile_ != nullptr) {
85         return CalcUniqId(pandaFile_, fileId_);
86     }
87     return CalcUniqId(descriptor_);
88 }
89 
90 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
Class(const uint8_t * descriptor,panda_file::SourceLang lang,uint32_t vtableSize,uint32_t imtSize,uint32_t size)91 Class::Class(const uint8_t *descriptor, panda_file::SourceLang lang, uint32_t vtableSize, uint32_t imtSize,
92              uint32_t size)
93     : BaseClass(lang), descriptor_(descriptor), vtableSize_(vtableSize), imtSize_(imtSize), classSize_(size)
94 {
95     state_ = State::INITIAL;
96     // Initializa all static fields with 0 value.
97     auto staticsOffset = GetStaticFieldsOffset();
98     auto sp = GetClassSpan();
99     ASSERT(sp.size() >= staticsOffset);
100     auto sizeToSet = sp.size() - staticsOffset;
101     if (sizeToSet > 0) {
102         memset_s(&sp[staticsOffset], sizeToSet, 0, sizeToSet);
103     }
104 }
SetState(Class::State state)105 void Class::SetState(Class::State state)
106 {
107     if (state_ == State::ERRONEOUS || state < state_ || (state_ == State::LOADED && state == State::INITIALIZED)) {
108         LOG(FATAL, RUNTIME) << "Invalid class state transition " << state_ << " -> " << state;
109     }
110 
111     state_ = state;
112 }
113 
GetName() const114 std::string Class::GetName() const
115 {
116     return ClassHelper::GetName(descriptor_);
117 }
118 
DumpClass(std::ostream & os,size_t flags)119 void Class::DumpClass(std::ostream &os, size_t flags)
120 {
121     if ((flags & DUMPCLASSFULLDETAILS) == 0) {
122         os << GetDescriptor();
123         if ((flags & DUMPCLASSCLASSLODER) != 0) {
124             LOG(INFO, RUNTIME) << " Panda can't get classloader at now\n";
125         }
126         if ((flags & DUMPCLASSINITIALIZED) != 0) {
127             LOG(INFO, RUNTIME) << " There is no status structure of class in Panda at now\n";
128         }
129         os << "\n";
130         return;
131     }
132     os << "\n";
133     os << "----- " << (IsInterface() ? "interface" : "class") << " "
134        << "'" << GetDescriptor() << "' -----\n";
135     os << "  objectSize=" << BaseClass::GetObjectSize() << " \n";
136     os << "  accessFlags=" << GetAccessFlags() << " \n";
137     if (IsArrayClass()) {
138         os << "  componentType=" << GetComponentType() << "\n";
139     }
140     size_t numDirectInterfaces = this->numIfaces_;
141     if (numDirectInterfaces > 0) {
142         os << "  interfaces (" << numDirectInterfaces << "):\n";
143     }
144     if (!IsLoaded()) {
145         os << "  class not yet loaded";
146     } else {
147         os << "  vtable (" << this->GetVTable().size() << " entries)\n";
148         if (this->numSfields_ > 0) {
149             os << "  static fields (" << this->numSfields_ << " entries)\n";
150         }
151         if (this->numFields_ - this->numSfields_ > 0) {
152             os << "  instance fields (" << this->numFields_ - this->numSfields_ << " entries)\n";
153         }
154     }
155 }
156 
FromClassObject(const ObjectHeader * obj)157 Class *Class::FromClassObject(const ObjectHeader *obj)
158 {
159     return Runtime::GetCurrent()->GetClassLinker()->ObjectToClass(obj);
160 }
161 
GetClassObjectSizeFromClass(Class * cls,panda_file::SourceLang lang)162 size_t Class::GetClassObjectSizeFromClass(Class *cls, panda_file::SourceLang lang)
163 {
164     return Runtime::GetCurrent()->GetClassLinker()->GetClassObjectSize(cls, lang);
165 }
166 
167 }  // namespace ark
168