• 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 #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/class_linker.h"
24 #include "runtime/include/runtime.h"
25 
26 namespace panda {
27 
operator <<(std::ostream & os,const Class::State & state)28 std::ostream &operator<<(std::ostream &os, const Class::State &state)
29 {
30     switch (state) {
31         case Class::State::INITIAL: {
32             os << "INITIAL";
33             break;
34         }
35         case Class::State::LOADED: {
36             os << "LOADED";
37             break;
38         }
39         case Class::State::VERIFIED: {
40             os << "VERIFIED";
41             break;
42         }
43         case Class::State::INITIALIZING: {
44             os << "INITIALIZING";
45             break;
46         }
47         case Class::State::ERRONEOUS: {
48             os << "ERRONEOUS";
49             break;
50         }
51         case Class::State::INITIALIZED: {
52             os << "INITIALIZED";
53             break;
54         }
55         default: {
56             UNREACHABLE();
57             break;
58         }
59     }
60 
61     return os;
62 }
63 
CalcUniqId(const panda_file::File * file,panda_file::File::EntityId file_id)64 Class::UniqId Class::CalcUniqId(const panda_file::File *file, panda_file::File::EntityId file_id)
65 {
66     constexpr uint64_t HALF = 32ULL;
67     uint64_t uid = file->GetUniqId();
68     uid <<= HALF;
69     uid |= file_id.GetOffset();
70     return uid;
71 }
72 
CalcUniqId(const uint8_t * descriptor)73 Class::UniqId Class::CalcUniqId(const uint8_t *descriptor)
74 {
75     uint64_t uid;
76     uid = GetHash32String(descriptor);
77     constexpr uint64_t HALF = 32ULL;
78     constexpr uint64_t NO_FILE = 0xFFFFFFFFULL << HALF;
79     uid = NO_FILE | uid;
80     return uid;
81 }
82 
CalcUniqId() const83 Class::UniqId Class::CalcUniqId() const
84 {
85     if (panda_file_ != nullptr) {
86         return CalcUniqId(panda_file_, file_id_);
87     }
88     return CalcUniqId(descriptor_);
89 }
90 
Class(const uint8_t * descriptor,panda_file::SourceLang lang,uint32_t vtable_size,uint32_t imt_size,uint32_t size)91 Class::Class(const uint8_t *descriptor, panda_file::SourceLang lang, uint32_t vtable_size, uint32_t imt_size,
92              uint32_t size)
93     : BaseClass(lang), descriptor_(descriptor), vtable_size_(vtable_size), imt_size_(imt_size), class_size_(size)
94 {
95     // Initializa all static fields with 0 value.
96     auto statics_offset = GetStaticFieldsOffset();
97     auto sp = GetClassSpan();
98     ASSERT(sp.size() >= statics_offset);
99     auto size_to_set = sp.size() - statics_offset;
100     if (size_to_set > 0) {
101         (void)memset_s(&sp[statics_offset], size_to_set, 0, size_to_set);
102     }
103 }
SetState(Class::State state)104 void Class::SetState(Class::State state)
105 {
106     if (state_ == State::ERRONEOUS || state <= state_) {
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 num_direct_interfaces = this->num_ifaces_;
140     if (num_direct_interfaces > 0) {
141         os << "  interfaces (" << num_direct_interfaces << "):\n";
142     }
143     if (!IsLoaded()) {
144         os << "  class not yet loaded";
145     } else {
146         os << "  vtable (" << this->GetVTable().size() << " entries)\n";
147         if (this->num_sfields_ > 0) {
148             os << "  static fields (" << this->num_sfields_ << " entries)\n";
149         }
150         if (this->num_fields_ - this->num_sfields_ > 0) {
151             os << "  instance fields (" << this->num_fields_ - this->num_sfields_ << " 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)161 size_t Class::GetClassObjectSizeFromClass(Class *cls)
162 {
163     return Runtime::GetCurrent()->GetClassLinker()->GetClassObjectSize(cls);
164 }
165 
166 }  // namespace panda
167