1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "class_table-inl.h"
18
19 #include "base/stl_util.h"
20 #include "mirror/class-inl.h"
21 #include "oat_file.h"
22
23 namespace art {
24
ClassTable()25 ClassTable::ClassTable() : lock_("Class loader classes", kClassLoaderClassesLock) {
26 Runtime* const runtime = Runtime::Current();
27 classes_.push_back(ClassSet(runtime->GetHashTableMinLoadFactor(),
28 runtime->GetHashTableMaxLoadFactor()));
29 }
30
FreezeSnapshot()31 void ClassTable::FreezeSnapshot() {
32 WriterMutexLock mu(Thread::Current(), lock_);
33 classes_.push_back(ClassSet());
34 }
35
UpdateClass(const char * descriptor,ObjPtr<mirror::Class> klass,size_t hash)36 ObjPtr<mirror::Class> ClassTable::UpdateClass(const char* descriptor,
37 ObjPtr<mirror::Class> klass,
38 size_t hash) {
39 WriterMutexLock mu(Thread::Current(), lock_);
40 // Should only be updating latest table.
41 DescriptorHashPair pair(descriptor, hash);
42 auto existing_it = classes_.back().FindWithHash(pair, hash);
43 if (existing_it == classes_.back().end()) {
44 for (const ClassSet& class_set : classes_) {
45 if (class_set.FindWithHash(pair, hash) != class_set.end()) {
46 LOG(FATAL) << "Updating class found in frozen table " << descriptor;
47 }
48 }
49 LOG(FATAL) << "Updating class not found " << descriptor;
50 }
51 const ObjPtr<mirror::Class> existing = existing_it->Read();
52 CHECK_NE(existing, klass) << descriptor;
53 CHECK(!existing->IsResolved()) << descriptor;
54 CHECK_EQ(klass->GetStatus(), ClassStatus::kResolving) << descriptor;
55 CHECK(!klass->IsTemp()) << descriptor;
56 VerifyObject(klass);
57 // Update the element in the hash set with the new class. This is safe to do since the descriptor
58 // doesn't change.
59 *existing_it = TableSlot(klass, hash);
60 return existing;
61 }
62
CountDefiningLoaderClasses(ObjPtr<mirror::ClassLoader> defining_loader,const ClassSet & set) const63 size_t ClassTable::CountDefiningLoaderClasses(ObjPtr<mirror::ClassLoader> defining_loader,
64 const ClassSet& set) const {
65 size_t count = 0;
66 for (const TableSlot& root : set) {
67 if (root.Read()->GetClassLoader() == defining_loader) {
68 ++count;
69 }
70 }
71 return count;
72 }
73
NumZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const74 size_t ClassTable::NumZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
75 ReaderMutexLock mu(Thread::Current(), lock_);
76 size_t sum = 0;
77 for (size_t i = 0; i < classes_.size() - 1; ++i) {
78 sum += CountDefiningLoaderClasses(defining_loader, classes_[i]);
79 }
80 return sum;
81 }
82
NumNonZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const83 size_t ClassTable::NumNonZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
84 ReaderMutexLock mu(Thread::Current(), lock_);
85 return CountDefiningLoaderClasses(defining_loader, classes_.back());
86 }
87
NumReferencedZygoteClasses() const88 size_t ClassTable::NumReferencedZygoteClasses() const {
89 ReaderMutexLock mu(Thread::Current(), lock_);
90 size_t sum = 0;
91 for (size_t i = 0; i < classes_.size() - 1; ++i) {
92 sum += classes_[i].size();
93 }
94 return sum;
95 }
96
NumReferencedNonZygoteClasses() const97 size_t ClassTable::NumReferencedNonZygoteClasses() const {
98 ReaderMutexLock mu(Thread::Current(), lock_);
99 return classes_.back().size();
100 }
101
Lookup(const char * descriptor,size_t hash)102 ObjPtr<mirror::Class> ClassTable::Lookup(const char* descriptor, size_t hash) {
103 DescriptorHashPair pair(descriptor, hash);
104 ReaderMutexLock mu(Thread::Current(), lock_);
105 for (ClassSet& class_set : classes_) {
106 auto it = class_set.FindWithHash(pair, hash);
107 if (it != class_set.end()) {
108 return it->Read();
109 }
110 }
111 return nullptr;
112 }
113
Insert(ObjPtr<mirror::Class> klass)114 void ClassTable::Insert(ObjPtr<mirror::Class> klass) {
115 InsertWithHash(klass, TableSlot::HashDescriptor(klass));
116 }
117
InsertWithHash(ObjPtr<mirror::Class> klass,size_t hash)118 void ClassTable::InsertWithHash(ObjPtr<mirror::Class> klass, size_t hash) {
119 WriterMutexLock mu(Thread::Current(), lock_);
120 classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
121 }
122
Remove(const char * descriptor)123 bool ClassTable::Remove(const char* descriptor) {
124 DescriptorHashPair pair(descriptor, ComputeModifiedUtf8Hash(descriptor));
125 WriterMutexLock mu(Thread::Current(), lock_);
126 for (ClassSet& class_set : classes_) {
127 auto it = class_set.find(pair);
128 if (it != class_set.end()) {
129 class_set.erase(it);
130 return true;
131 }
132 }
133 return false;
134 }
135
InsertStrongRoot(ObjPtr<mirror::Object> obj)136 bool ClassTable::InsertStrongRoot(ObjPtr<mirror::Object> obj) {
137 WriterMutexLock mu(Thread::Current(), lock_);
138 DCHECK(obj != nullptr);
139 for (GcRoot<mirror::Object>& root : strong_roots_) {
140 if (root.Read() == obj) {
141 return false;
142 }
143 }
144 strong_roots_.push_back(GcRoot<mirror::Object>(obj));
145 // If `obj` is a dex cache associated with a new oat file with GC roots, add it to oat_files_.
146 if (obj->IsDexCache()) {
147 const DexFile* dex_file = ObjPtr<mirror::DexCache>::DownCast(obj)->GetDexFile();
148 if (dex_file != nullptr && dex_file->GetOatDexFile() != nullptr) {
149 const OatFile* oat_file = dex_file->GetOatDexFile()->GetOatFile();
150 if (oat_file != nullptr && !oat_file->GetBssGcRoots().empty()) {
151 InsertOatFileLocked(oat_file); // Ignore return value.
152 }
153 }
154 }
155 return true;
156 }
157
InsertOatFile(const OatFile * oat_file)158 bool ClassTable::InsertOatFile(const OatFile* oat_file) {
159 WriterMutexLock mu(Thread::Current(), lock_);
160 return InsertOatFileLocked(oat_file);
161 }
162
InsertOatFileLocked(const OatFile * oat_file)163 bool ClassTable::InsertOatFileLocked(const OatFile* oat_file) {
164 if (ContainsElement(oat_files_, oat_file)) {
165 return false;
166 }
167 oat_files_.push_back(oat_file);
168 return true;
169 }
170
ReadFromMemory(uint8_t * ptr)171 size_t ClassTable::ReadFromMemory(uint8_t* ptr) {
172 size_t read_count = 0;
173 AddClassSet(ClassSet(ptr, /*make copy*/false, &read_count));
174 return read_count;
175 }
176
AddClassSet(ClassSet && set)177 void ClassTable::AddClassSet(ClassSet&& set) {
178 WriterMutexLock mu(Thread::Current(), lock_);
179 classes_.insert(classes_.begin(), std::move(set));
180 }
181
ClearStrongRoots()182 void ClassTable::ClearStrongRoots() {
183 WriterMutexLock mu(Thread::Current(), lock_);
184 oat_files_.clear();
185 strong_roots_.clear();
186 }
187
TableSlot(ObjPtr<mirror::Class> klass)188 ClassTable::TableSlot::TableSlot(ObjPtr<mirror::Class> klass)
189 : TableSlot(klass, HashDescriptor(klass)) {}
190
HashDescriptor(ObjPtr<mirror::Class> klass)191 uint32_t ClassTable::TableSlot::HashDescriptor(ObjPtr<mirror::Class> klass) {
192 std::string temp;
193 return ComputeModifiedUtf8Hash(klass->GetDescriptor(&temp));
194 }
195
196 } // namespace art
197