blob: fc2640c2cca3a6bc204005921484f506f15c5ef0 [file] [log] [blame]
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -07001/*
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 Gampe2ff3b972017-06-05 18:14:53 -070017#include "class_table-inl.h"
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070018
Andreas Gampe5678db52017-06-08 14:11:18 -070019#include "base/stl_util.h"
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070020#include "mirror/class-inl.h"
Andreas Gampec15a2f42017-04-21 12:09:39 -070021#include "oat_file.h"
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070022
23namespace art {
24
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070025ClassTable::ClassTable() : lock_("Class loader classes", kClassLoaderClassesLock) {
Mathieu Chartier32cc9ee2015-10-15 09:19:15 -070026 Runtime* const runtime = Runtime::Current();
27 classes_.push_back(ClassSet(runtime->GetHashTableMinLoadFactor(),
28 runtime->GetHashTableMaxLoadFactor()));
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070029}
30
31void ClassTable::FreezeSnapshot() {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070032 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070033 classes_.push_back(ClassSet());
34}
35
Mathieu Chartier28357fa2016-10-18 16:27:40 -070036bool ClassTable::Contains(ObjPtr<mirror::Class> klass) {
Vladimir Markoc6934e32019-04-10 11:40:01 +010037 return LookupByDescriptor(klass) == klass;
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070038}
39
Vladimir Marko1fe58392019-04-10 16:14:56 +010040ObjPtr<mirror::Class> ClassTable::LookupByDescriptor(ObjPtr<mirror::Class> klass) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070041 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartierdb70ce52016-12-12 11:06:59 -080042 TableSlot slot(klass);
Mathieu Chartierfbc31082016-01-24 11:59:56 -080043 for (ClassSet& class_set : classes_) {
Vladimir Marko54159c62018-06-20 14:30:08 +010044 auto it = class_set.find(slot);
Mathieu Chartierfbc31082016-01-24 11:59:56 -080045 if (it != class_set.end()) {
46 return it->Read();
47 }
48 }
49 return nullptr;
50}
51
Vladimir Marko1fe58392019-04-10 16:14:56 +010052ObjPtr<mirror::Class> ClassTable::UpdateClass(const char* descriptor,
53 ObjPtr<mirror::Class> klass,
54 size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070055 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070056 // Should only be updating latest table.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080057 DescriptorHashPair pair(descriptor, hash);
58 auto existing_it = classes_.back().FindWithHash(pair, hash);
Mathieu Chartier1a8d83b2020-10-12 15:43:23 -070059 if (existing_it == classes_.back().end()) {
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070060 for (const ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080061 if (class_set.FindWithHash(pair, hash) != class_set.end()) {
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070062 LOG(FATAL) << "Updating class found in frozen table " << descriptor;
63 }
64 }
65 LOG(FATAL) << "Updating class not found " << descriptor;
66 }
Vladimir Marko1fe58392019-04-10 16:14:56 +010067 const ObjPtr<mirror::Class> existing = existing_it->Read();
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070068 CHECK_NE(existing, klass) << descriptor;
69 CHECK(!existing->IsResolved()) << descriptor;
Vladimir Marko2c64a832018-01-04 11:31:56 +000070 CHECK_EQ(klass->GetStatus(), ClassStatus::kResolving) << descriptor;
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070071 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.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080075 *existing_it = TableSlot(klass, hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070076 return existing;
77}
78
Vladimir Markoc5798bf2016-12-09 10:20:54 +000079size_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
90size_t ClassTable::NumZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070091 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070092 size_t sum = 0;
93 for (size_t i = 0; i < classes_.size() - 1; ++i) {
Vladimir Markoc5798bf2016-12-09 10:20:54 +000094 sum += CountDefiningLoaderClasses(defining_loader, classes_[i]);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070095 }
96 return sum;
97}
98
Vladimir Markoc5798bf2016-12-09 10:20:54 +000099size_t ClassTable::NumNonZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700100 ReaderMutexLock mu(Thread::Current(), lock_);
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000101 return CountDefiningLoaderClasses(defining_loader, classes_.back());
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700102}
103
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000104size_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) {
Vladimir Marko54159c62018-06-20 14:30:08 +0100108 sum += classes_[i].size();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000109 }
110 return sum;
111}
112
113size_t ClassTable::NumReferencedNonZygoteClasses() const {
114 ReaderMutexLock mu(Thread::Current(), lock_);
Vladimir Marko54159c62018-06-20 14:30:08 +0100115 return classes_.back().size();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000116}
117
Vladimir Marko1fe58392019-04-10 16:14:56 +0100118ObjPtr<mirror::Class> ClassTable::Lookup(const char* descriptor, size_t hash) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800119 DescriptorHashPair pair(descriptor, hash);
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800120 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700121 for (ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800122 auto it = class_set.FindWithHash(pair, hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700123 if (it != class_set.end()) {
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000124 return it->Read();
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700125 }
126 }
127 return nullptr;
128}
129
Vladimir Markocd556b02017-02-03 11:47:34 +0000130ObjPtr<mirror::Class> ClassTable::TryInsert(ObjPtr<mirror::Class> klass) {
131 TableSlot slot(klass);
132 WriterMutexLock mu(Thread::Current(), lock_);
133 for (ClassSet& class_set : classes_) {
Vladimir Marko54159c62018-06-20 14:30:08 +0100134 auto it = class_set.find(slot);
Vladimir Markocd556b02017-02-03 11:47:34 +0000135 if (it != class_set.end()) {
136 return it->Read();
137 }
138 }
Vladimir Marko54159c62018-06-20 14:30:08 +0100139 classes_.back().insert(slot);
Vladimir Markocd556b02017-02-03 11:47:34 +0000140 return klass;
141}
142
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700143void ClassTable::Insert(ObjPtr<mirror::Class> klass) {
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800144 const uint32_t hash = TableSlot::HashDescriptor(klass);
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700145 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800146 classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700147}
148
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700149void ClassTable::InsertWithHash(ObjPtr<mirror::Class> klass, size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700150 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800151 classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700152}
153
154bool ClassTable::Remove(const char* descriptor) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800155 DescriptorHashPair pair(descriptor, ComputeModifiedUtf8Hash(descriptor));
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800156 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700157 for (ClassSet& class_set : classes_) {
Vladimir Marko54159c62018-06-20 14:30:08 +0100158 auto it = class_set.find(pair);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700159 if (it != class_set.end()) {
Vladimir Marko54159c62018-06-20 14:30:08 +0100160 class_set.erase(it);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700161 return true;
162 }
163 }
164 return false;
165}
166
Vladimir Markoeae6a712021-02-05 13:33:28 +0000167uint32_t ClassTable::ClassDescriptorHash::operator()(const TableSlot& slot)
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700168 const {
169 std::string temp;
Vladimir Markoc6934e32019-04-10 11:40:01 +0100170 // No read barrier needed, we're reading a chain of constant references for comparison
171 // with null and retrieval of constant primitive data. See ReadBarrierOption.
172 return ComputeModifiedUtf8Hash(slot.Read<kWithoutReadBarrier>()->GetDescriptor(&temp));
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700173}
174
Vladimir Markoeae6a712021-02-05 13:33:28 +0000175uint32_t ClassTable::ClassDescriptorHash::operator()(const DescriptorHashPair& pair) const {
176 DCHECK_EQ(ComputeModifiedUtf8Hash(pair.first), pair.second);
177 return pair.second;
178}
179
180bool ClassTable::ClassDescriptorEquals::operator()(const TableSlot& a, const TableSlot& b) const {
Vladimir Markoc6934e32019-04-10 11:40:01 +0100181 // No read barrier needed, we're reading a chain of constant references for comparison
182 // with null and retrieval of constant primitive data. See ReadBarrierOption.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800183 if (a.Hash() != b.Hash()) {
184 std::string temp;
Vladimir Markoc6934e32019-04-10 11:40:01 +0100185 DCHECK(!a.Read<kWithoutReadBarrier>()->DescriptorEquals(
186 b.Read<kWithoutReadBarrier>()->GetDescriptor(&temp)));
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800187 return false;
188 }
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700189 std::string temp;
Vladimir Markoc6934e32019-04-10 11:40:01 +0100190 return a.Read<kWithoutReadBarrier>()->DescriptorEquals(
191 b.Read<kWithoutReadBarrier>()->GetDescriptor(&temp));
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700192}
193
Vladimir Markoeae6a712021-02-05 13:33:28 +0000194bool ClassTable::ClassDescriptorEquals::operator()(const TableSlot& a,
195 const DescriptorHashPair& b) const {
Vladimir Markoc6934e32019-04-10 11:40:01 +0100196 // No read barrier needed, we're reading a chain of constant references for comparison
197 // with null and retrieval of constant primitive data. See ReadBarrierOption.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800198 if (!a.MaskedHashEquals(b.second)) {
Vladimir Markoc6934e32019-04-10 11:40:01 +0100199 DCHECK(!a.Read<kWithoutReadBarrier>()->DescriptorEquals(b.first));
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800200 return false;
201 }
Vladimir Markoc6934e32019-04-10 11:40:01 +0100202 return a.Read<kWithoutReadBarrier>()->DescriptorEquals(b.first);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700203}
204
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700205bool ClassTable::InsertStrongRoot(ObjPtr<mirror::Object> obj) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700206 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700207 DCHECK(obj != nullptr);
208 for (GcRoot<mirror::Object>& root : strong_roots_) {
209 if (root.Read() == obj) {
Mathieu Chartier00310e02015-10-17 12:46:42 -0700210 return false;
211 }
212 }
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700213 strong_roots_.push_back(GcRoot<mirror::Object>(obj));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000214 // If `obj` is a dex cache associated with a new oat file with GC roots, add it to oat_files_.
215 if (obj->IsDexCache()) {
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700216 const DexFile* dex_file = ObjPtr<mirror::DexCache>::DownCast(obj)->GetDexFile();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000217 if (dex_file != nullptr && dex_file->GetOatDexFile() != nullptr) {
218 const OatFile* oat_file = dex_file->GetOatDexFile()->GetOatFile();
Mathieu Chartier1b868492016-11-16 16:22:37 -0800219 if (oat_file != nullptr && !oat_file->GetBssGcRoots().empty()) {
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000220 InsertOatFileLocked(oat_file); // Ignore return value.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000221 }
222 }
223 }
Mathieu Chartier00310e02015-10-17 12:46:42 -0700224 return true;
225}
226
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000227bool ClassTable::InsertOatFile(const OatFile* oat_file) {
228 WriterMutexLock mu(Thread::Current(), lock_);
229 return InsertOatFileLocked(oat_file);
230}
231
232bool ClassTable::InsertOatFileLocked(const OatFile* oat_file) {
233 if (ContainsElement(oat_files_, oat_file)) {
234 return false;
235 }
236 oat_files_.push_back(oat_file);
237 return true;
238}
239
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800240size_t ClassTable::ReadFromMemory(uint8_t* ptr) {
241 size_t read_count = 0;
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800242 AddClassSet(ClassSet(ptr, /*make copy*/false, &read_count));
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800243 return read_count;
244}
245
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800246void ClassTable::AddClassSet(ClassSet&& set) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700247 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800248 classes_.insert(classes_.begin(), std::move(set));
249}
250
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700251void ClassTable::ClearStrongRoots() {
252 WriterMutexLock mu(Thread::Current(), lock_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000253 oat_files_.clear();
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700254 strong_roots_.clear();
255}
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800256
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800257ClassTable::TableSlot::TableSlot(ObjPtr<mirror::Class> klass)
258 : TableSlot(klass, HashDescriptor(klass)) {}
259
260uint32_t ClassTable::TableSlot::HashDescriptor(ObjPtr<mirror::Class> klass) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800261 std::string temp;
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800262 return ComputeModifiedUtf8Hash(klass->GetDescriptor(&temp));
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800263}
264
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700265} // namespace art