Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 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 | |
Andreas Gampe | 2ff3b97 | 2017-06-05 18:14:53 -0700 | [diff] [blame] | 17 | #include "class_table-inl.h" |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 18 | |
Andreas Gampe | 5678db5 | 2017-06-08 14:11:18 -0700 | [diff] [blame] | 19 | #include "base/stl_util.h" |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 20 | #include "mirror/class-inl.h" |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 21 | #include "oat_file.h" |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 25 | ClassTable::ClassTable() : lock_("Class loader classes", kClassLoaderClassesLock) { |
Mathieu Chartier | 32cc9ee | 2015-10-15 09:19:15 -0700 | [diff] [blame] | 26 | Runtime* const runtime = Runtime::Current(); |
| 27 | classes_.push_back(ClassSet(runtime->GetHashTableMinLoadFactor(), |
| 28 | runtime->GetHashTableMaxLoadFactor())); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | void ClassTable::FreezeSnapshot() { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 32 | WriterMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 33 | classes_.push_back(ClassSet()); |
| 34 | } |
| 35 | |
Vladimir Marko | 1fe5839 | 2019-04-10 16:14:56 +0100 | [diff] [blame] | 36 | ObjPtr<mirror::Class> ClassTable::UpdateClass(const char* descriptor, |
| 37 | ObjPtr<mirror::Class> klass, |
| 38 | size_t hash) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 39 | WriterMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 40 | // Should only be updating latest table. |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 41 | DescriptorHashPair pair(descriptor, hash); |
| 42 | auto existing_it = classes_.back().FindWithHash(pair, hash); |
Mathieu Chartier | 1a8d83b | 2020-10-12 15:43:23 -0700 | [diff] [blame] | 43 | if (existing_it == classes_.back().end()) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 44 | for (const ClassSet& class_set : classes_) { |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 45 | if (class_set.FindWithHash(pair, hash) != class_set.end()) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 46 | LOG(FATAL) << "Updating class found in frozen table " << descriptor; |
| 47 | } |
| 48 | } |
| 49 | LOG(FATAL) << "Updating class not found " << descriptor; |
| 50 | } |
Vladimir Marko | 1fe5839 | 2019-04-10 16:14:56 +0100 | [diff] [blame] | 51 | const ObjPtr<mirror::Class> existing = existing_it->Read(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 52 | CHECK_NE(existing, klass) << descriptor; |
| 53 | CHECK(!existing->IsResolved()) << descriptor; |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 54 | CHECK_EQ(klass->GetStatus(), ClassStatus::kResolving) << descriptor; |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 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. |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 59 | *existing_it = TableSlot(klass, hash); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 60 | return existing; |
| 61 | } |
| 62 | |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 63 | 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 | |
| 74 | size_t ClassTable::NumZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 75 | ReaderMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 76 | size_t sum = 0; |
| 77 | for (size_t i = 0; i < classes_.size() - 1; ++i) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 78 | sum += CountDefiningLoaderClasses(defining_loader, classes_[i]); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 79 | } |
| 80 | return sum; |
| 81 | } |
| 82 | |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 83 | size_t ClassTable::NumNonZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 84 | ReaderMutexLock mu(Thread::Current(), lock_); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 85 | return CountDefiningLoaderClasses(defining_loader, classes_.back()); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 88 | 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) { |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 92 | sum += classes_[i].size(); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 93 | } |
| 94 | return sum; |
| 95 | } |
| 96 | |
| 97 | size_t ClassTable::NumReferencedNonZygoteClasses() const { |
| 98 | ReaderMutexLock mu(Thread::Current(), lock_); |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 99 | return classes_.back().size(); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Vladimir Marko | 1fe5839 | 2019-04-10 16:14:56 +0100 | [diff] [blame] | 102 | ObjPtr<mirror::Class> ClassTable::Lookup(const char* descriptor, size_t hash) { |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 103 | DescriptorHashPair pair(descriptor, hash); |
Mathieu Chartier | db70ce5 | 2016-12-12 11:06:59 -0800 | [diff] [blame] | 104 | ReaderMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 105 | for (ClassSet& class_set : classes_) { |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 106 | auto it = class_set.FindWithHash(pair, hash); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 107 | if (it != class_set.end()) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 108 | return it->Read(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | return nullptr; |
| 112 | } |
| 113 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 114 | void ClassTable::Insert(ObjPtr<mirror::Class> klass) { |
Vladimir Marko | 1dab575 | 2021-02-25 15:41:33 +0000 | [diff] [blame] | 115 | InsertWithHash(klass, TableSlot::HashDescriptor(klass)); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 118 | void ClassTable::InsertWithHash(ObjPtr<mirror::Class> klass, size_t hash) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 119 | WriterMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 120 | classes_.back().InsertWithHash(TableSlot(klass, hash), hash); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | bool ClassTable::Remove(const char* descriptor) { |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 124 | DescriptorHashPair pair(descriptor, ComputeModifiedUtf8Hash(descriptor)); |
Mathieu Chartier | db70ce5 | 2016-12-12 11:06:59 -0800 | [diff] [blame] | 125 | WriterMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 126 | for (ClassSet& class_set : classes_) { |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 127 | auto it = class_set.find(pair); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 128 | if (it != class_set.end()) { |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 129 | class_set.erase(it); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 130 | return true; |
| 131 | } |
| 132 | } |
| 133 | return false; |
| 134 | } |
| 135 | |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 136 | bool ClassTable::InsertStrongRoot(ObjPtr<mirror::Object> obj) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 137 | WriterMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 138 | DCHECK(obj != nullptr); |
| 139 | for (GcRoot<mirror::Object>& root : strong_roots_) { |
| 140 | if (root.Read() == obj) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 141 | return false; |
| 142 | } |
| 143 | } |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 144 | strong_roots_.push_back(GcRoot<mirror::Object>(obj)); |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 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()) { |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 147 | const DexFile* dex_file = ObjPtr<mirror::DexCache>::DownCast(obj)->GetDexFile(); |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 148 | if (dex_file != nullptr && dex_file->GetOatDexFile() != nullptr) { |
| 149 | const OatFile* oat_file = dex_file->GetOatDexFile()->GetOatFile(); |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 150 | if (oat_file != nullptr && !oat_file->GetBssGcRoots().empty()) { |
Vladimir Marko | 1bc4b17 | 2016-10-24 16:53:39 +0000 | [diff] [blame] | 151 | InsertOatFileLocked(oat_file); // Ignore return value. |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | } |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 155 | return true; |
| 156 | } |
| 157 | |
Vladimir Marko | 1bc4b17 | 2016-10-24 16:53:39 +0000 | [diff] [blame] | 158 | bool ClassTable::InsertOatFile(const OatFile* oat_file) { |
| 159 | WriterMutexLock mu(Thread::Current(), lock_); |
| 160 | return InsertOatFileLocked(oat_file); |
| 161 | } |
| 162 | |
| 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 | |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 171 | size_t ClassTable::ReadFromMemory(uint8_t* ptr) { |
| 172 | size_t read_count = 0; |
Mathieu Chartier | 88027bd | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 173 | AddClassSet(ClassSet(ptr, /*make copy*/false, &read_count)); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 174 | return read_count; |
| 175 | } |
| 176 | |
Mathieu Chartier | 88027bd | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 177 | void ClassTable::AddClassSet(ClassSet&& set) { |
Mathieu Chartier | 1609e3a | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 178 | WriterMutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | 88027bd | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 179 | classes_.insert(classes_.begin(), std::move(set)); |
| 180 | } |
| 181 | |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 182 | void ClassTable::ClearStrongRoots() { |
| 183 | WriterMutexLock mu(Thread::Current(), lock_); |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 184 | oat_files_.clear(); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 185 | strong_roots_.clear(); |
| 186 | } |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 187 | |
Mathieu Chartier | db70ce5 | 2016-12-12 11:06:59 -0800 | [diff] [blame] | 188 | ClassTable::TableSlot::TableSlot(ObjPtr<mirror::Class> klass) |
| 189 | : TableSlot(klass, HashDescriptor(klass)) {} |
| 190 | |
| 191 | uint32_t ClassTable::TableSlot::HashDescriptor(ObjPtr<mirror::Class> klass) { |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 192 | std::string temp; |
Mathieu Chartier | db70ce5 | 2016-12-12 11:06:59 -0800 | [diff] [blame] | 193 | return ComputeModifiedUtf8Hash(klass->GetDescriptor(&temp)); |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 196 | } // namespace art |