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
Contains(ObjPtr<mirror::Class> klass)36 bool ClassTable::Contains(ObjPtr<mirror::Class> klass) {
37 ReaderMutexLock mu(Thread::Current(), lock_);
38 TableSlot slot(klass);
39 for (ClassSet& class_set : classes_) {
40 auto it = class_set.find(slot);
41 if (it != class_set.end()) {
42 return it->Read() == klass;
43 }
44 }
45 return false;
46 }
47
LookupByDescriptor(ObjPtr<mirror::Class> klass)48 mirror::Class* ClassTable::LookupByDescriptor(ObjPtr<mirror::Class> klass) {
49 ReaderMutexLock mu(Thread::Current(), lock_);
50 TableSlot slot(klass);
51 for (ClassSet& class_set : classes_) {
52 auto it = class_set.find(slot);
53 if (it != class_set.end()) {
54 return it->Read();
55 }
56 }
57 return nullptr;
58 }
59
UpdateClass(const char * descriptor,mirror::Class * klass,size_t hash)60 mirror::Class* ClassTable::UpdateClass(const char* descriptor, mirror::Class* klass, size_t hash) {
61 WriterMutexLock mu(Thread::Current(), lock_);
62 // Should only be updating latest table.
63 DescriptorHashPair pair(descriptor, hash);
64 auto existing_it = classes_.back().FindWithHash(pair, hash);
65 if (kIsDebugBuild && existing_it == classes_.back().end()) {
66 for (const ClassSet& class_set : classes_) {
67 if (class_set.FindWithHash(pair, hash) != class_set.end()) {
68 LOG(FATAL) << "Updating class found in frozen table " << descriptor;
69 }
70 }
71 LOG(FATAL) << "Updating class not found " << descriptor;
72 }
73 mirror::Class* const existing = existing_it->Read();
74 CHECK_NE(existing, klass) << descriptor;
75 CHECK(!existing->IsResolved()) << descriptor;
76 CHECK_EQ(klass->GetStatus(), ClassStatus::kResolving) << descriptor;
77 CHECK(!klass->IsTemp()) << descriptor;
78 VerifyObject(klass);
79 // Update the element in the hash set with the new class. This is safe to do since the descriptor
80 // doesn't change.
81 *existing_it = TableSlot(klass, hash);
82 return existing;
83 }
84
CountDefiningLoaderClasses(ObjPtr<mirror::ClassLoader> defining_loader,const ClassSet & set) const85 size_t ClassTable::CountDefiningLoaderClasses(ObjPtr<mirror::ClassLoader> defining_loader,
86 const ClassSet& set) const {
87 size_t count = 0;
88 for (const TableSlot& root : set) {
89 if (root.Read()->GetClassLoader() == defining_loader) {
90 ++count;
91 }
92 }
93 return count;
94 }
95
NumZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const96 size_t ClassTable::NumZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
97 ReaderMutexLock mu(Thread::Current(), lock_);
98 size_t sum = 0;
99 for (size_t i = 0; i < classes_.size() - 1; ++i) {
100 sum += CountDefiningLoaderClasses(defining_loader, classes_[i]);
101 }
102 return sum;
103 }
104
NumNonZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const105 size_t ClassTable::NumNonZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
106 ReaderMutexLock mu(Thread::Current(), lock_);
107 return CountDefiningLoaderClasses(defining_loader, classes_.back());
108 }
109
NumReferencedZygoteClasses() const110 size_t ClassTable::NumReferencedZygoteClasses() const {
111 ReaderMutexLock mu(Thread::Current(), lock_);
112 size_t sum = 0;
113 for (size_t i = 0; i < classes_.size() - 1; ++i) {
114 sum += classes_[i].size();
115 }
116 return sum;
117 }
118
NumReferencedNonZygoteClasses() const119 size_t ClassTable::NumReferencedNonZygoteClasses() const {
120 ReaderMutexLock mu(Thread::Current(), lock_);
121 return classes_.back().size();
122 }
123
Lookup(const char * descriptor,size_t hash)124 mirror::Class* ClassTable::Lookup(const char* descriptor, size_t hash) {
125 DescriptorHashPair pair(descriptor, hash);
126 ReaderMutexLock mu(Thread::Current(), lock_);
127 for (ClassSet& class_set : classes_) {
128 auto it = class_set.FindWithHash(pair, hash);
129 if (it != class_set.end()) {
130 return it->Read();
131 }
132 }
133 return nullptr;
134 }
135
TryInsert(ObjPtr<mirror::Class> klass)136 ObjPtr<mirror::Class> ClassTable::TryInsert(ObjPtr<mirror::Class> klass) {
137 TableSlot slot(klass);
138 WriterMutexLock mu(Thread::Current(), lock_);
139 for (ClassSet& class_set : classes_) {
140 auto it = class_set.find(slot);
141 if (it != class_set.end()) {
142 return it->Read();
143 }
144 }
145 classes_.back().insert(slot);
146 return klass;
147 }
148
Insert(ObjPtr<mirror::Class> klass)149 void ClassTable::Insert(ObjPtr<mirror::Class> klass) {
150 const uint32_t hash = TableSlot::HashDescriptor(klass);
151 WriterMutexLock mu(Thread::Current(), lock_);
152 classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
153 }
154
CopyWithoutLocks(const ClassTable & source_table)155 void ClassTable::CopyWithoutLocks(const ClassTable& source_table) {
156 if (kIsDebugBuild) {
157 for (ClassSet& class_set : classes_) {
158 CHECK(class_set.empty());
159 }
160 }
161 for (const ClassSet& class_set : source_table.classes_) {
162 for (const TableSlot& slot : class_set) {
163 classes_.back().insert(slot);
164 }
165 }
166 }
167
InsertWithoutLocks(ObjPtr<mirror::Class> klass)168 void ClassTable::InsertWithoutLocks(ObjPtr<mirror::Class> klass) {
169 const uint32_t hash = TableSlot::HashDescriptor(klass);
170 classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
171 }
172
InsertWithHash(ObjPtr<mirror::Class> klass,size_t hash)173 void ClassTable::InsertWithHash(ObjPtr<mirror::Class> klass, size_t hash) {
174 WriterMutexLock mu(Thread::Current(), lock_);
175 classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
176 }
177
Remove(const char * descriptor)178 bool ClassTable::Remove(const char* descriptor) {
179 DescriptorHashPair pair(descriptor, ComputeModifiedUtf8Hash(descriptor));
180 WriterMutexLock mu(Thread::Current(), lock_);
181 for (ClassSet& class_set : classes_) {
182 auto it = class_set.find(pair);
183 if (it != class_set.end()) {
184 class_set.erase(it);
185 return true;
186 }
187 }
188 return false;
189 }
190
operator ()(const TableSlot & slot) const191 uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& slot)
192 const {
193 std::string temp;
194 return ComputeModifiedUtf8Hash(slot.Read()->GetDescriptor(&temp));
195 }
196
operator ()(const TableSlot & a,const TableSlot & b) const197 bool ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& a,
198 const TableSlot& b) const {
199 if (a.Hash() != b.Hash()) {
200 std::string temp;
201 DCHECK(!a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp)));
202 return false;
203 }
204 std::string temp;
205 return a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp));
206 }
207
operator ()(const TableSlot & a,const DescriptorHashPair & b) const208 bool ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& a,
209 const DescriptorHashPair& b) const {
210 if (!a.MaskedHashEquals(b.second)) {
211 DCHECK(!a.Read()->DescriptorEquals(b.first));
212 return false;
213 }
214 return a.Read()->DescriptorEquals(b.first);
215 }
216
operator ()(const DescriptorHashPair & pair) const217 uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const DescriptorHashPair& pair) const {
218 return ComputeModifiedUtf8Hash(pair.first);
219 }
220
InsertStrongRoot(ObjPtr<mirror::Object> obj)221 bool ClassTable::InsertStrongRoot(ObjPtr<mirror::Object> obj) {
222 WriterMutexLock mu(Thread::Current(), lock_);
223 DCHECK(obj != nullptr);
224 for (GcRoot<mirror::Object>& root : strong_roots_) {
225 if (root.Read() == obj) {
226 return false;
227 }
228 }
229 strong_roots_.push_back(GcRoot<mirror::Object>(obj));
230 // If `obj` is a dex cache associated with a new oat file with GC roots, add it to oat_files_.
231 if (obj->IsDexCache()) {
232 const DexFile* dex_file = ObjPtr<mirror::DexCache>::DownCast(obj)->GetDexFile();
233 if (dex_file != nullptr && dex_file->GetOatDexFile() != nullptr) {
234 const OatFile* oat_file = dex_file->GetOatDexFile()->GetOatFile();
235 if (oat_file != nullptr && !oat_file->GetBssGcRoots().empty()) {
236 InsertOatFileLocked(oat_file); // Ignore return value.
237 }
238 }
239 }
240 return true;
241 }
242
InsertOatFile(const OatFile * oat_file)243 bool ClassTable::InsertOatFile(const OatFile* oat_file) {
244 WriterMutexLock mu(Thread::Current(), lock_);
245 return InsertOatFileLocked(oat_file);
246 }
247
InsertOatFileLocked(const OatFile * oat_file)248 bool ClassTable::InsertOatFileLocked(const OatFile* oat_file) {
249 if (ContainsElement(oat_files_, oat_file)) {
250 return false;
251 }
252 oat_files_.push_back(oat_file);
253 return true;
254 }
255
WriteToMemory(uint8_t * ptr) const256 size_t ClassTable::WriteToMemory(uint8_t* ptr) const {
257 ReaderMutexLock mu(Thread::Current(), lock_);
258 ClassSet combined;
259 // Combine all the class sets in case there are multiple, also adjusts load factor back to
260 // default in case classes were pruned.
261 for (const ClassSet& class_set : classes_) {
262 for (const TableSlot& root : class_set) {
263 combined.insert(root);
264 }
265 }
266 const size_t ret = combined.WriteToMemory(ptr);
267 // Sanity check.
268 if (kIsDebugBuild && ptr != nullptr) {
269 size_t read_count;
270 ClassSet class_set(ptr, /*make copy*/false, &read_count);
271 class_set.Verify();
272 }
273 return ret;
274 }
275
ReadFromMemory(uint8_t * ptr)276 size_t ClassTable::ReadFromMemory(uint8_t* ptr) {
277 size_t read_count = 0;
278 AddClassSet(ClassSet(ptr, /*make copy*/false, &read_count));
279 return read_count;
280 }
281
AddClassSet(ClassSet && set)282 void ClassTable::AddClassSet(ClassSet&& set) {
283 WriterMutexLock mu(Thread::Current(), lock_);
284 classes_.insert(classes_.begin(), std::move(set));
285 }
286
ClearStrongRoots()287 void ClassTable::ClearStrongRoots() {
288 WriterMutexLock mu(Thread::Current(), lock_);
289 oat_files_.clear();
290 strong_roots_.clear();
291 }
292
TableSlot(ObjPtr<mirror::Class> klass)293 ClassTable::TableSlot::TableSlot(ObjPtr<mirror::Class> klass)
294 : TableSlot(klass, HashDescriptor(klass)) {}
295
HashDescriptor(ObjPtr<mirror::Class> klass)296 uint32_t ClassTable::TableSlot::HashDescriptor(ObjPtr<mirror::Class> klass) {
297 std::string temp;
298 return ComputeModifiedUtf8Hash(klass->GetDescriptor(&temp));
299 }
300
301 } // namespace art
302