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