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