blob: 58f367480482903c1bbd71d839bacd9b315200d7 [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
Mathieu Chartier28357fa2016-10-18 16:27:40 -070040mirror::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
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070052mirror::Class* ClassTable::UpdateClass(const char* descriptor, mirror::Class* klass, size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070053 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070054 // Should only be updating latest table.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080055 DescriptorHashPair pair(descriptor, hash);
56 auto existing_it = classes_.back().FindWithHash(pair, hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070057 if (kIsDebugBuild && existing_it == classes_.back().end()) {
58 for (const ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080059 if (class_set.FindWithHash(pair, hash) != class_set.end()) {
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070060 LOG(FATAL) << "Updating class found in frozen table " << descriptor;
61 }
62 }
63 LOG(FATAL) << "Updating class not found " << descriptor;
64 }
65 mirror::Class* const existing = existing_it->Read();
66 CHECK_NE(existing, klass) << descriptor;
67 CHECK(!existing->IsResolved()) << descriptor;
Vladimir Marko2c64a832018-01-04 11:31:56 +000068 CHECK_EQ(klass->GetStatus(), ClassStatus::kResolving) << descriptor;
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070069 CHECK(!klass->IsTemp()) << descriptor;
70 VerifyObject(klass);
71 // Update the element in the hash set with the new class. This is safe to do since the descriptor
72 // doesn't change.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080073 *existing_it = TableSlot(klass, hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070074 return existing;
75}
76
Vladimir Markoc5798bf2016-12-09 10:20:54 +000077size_t ClassTable::CountDefiningLoaderClasses(ObjPtr<mirror::ClassLoader> defining_loader,
78 const ClassSet& set) const {
79 size_t count = 0;
80 for (const TableSlot& root : set) {
81 if (root.Read()->GetClassLoader() == defining_loader) {
82 ++count;
83 }
84 }
85 return count;
86}
87
88size_t ClassTable::NumZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070089 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070090 size_t sum = 0;
91 for (size_t i = 0; i < classes_.size() - 1; ++i) {
Vladimir Markoc5798bf2016-12-09 10:20:54 +000092 sum += CountDefiningLoaderClasses(defining_loader, classes_[i]);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070093 }
94 return sum;
95}
96
Vladimir Markoc5798bf2016-12-09 10:20:54 +000097size_t ClassTable::NumNonZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070098 ReaderMutexLock mu(Thread::Current(), lock_);
Vladimir Markoc5798bf2016-12-09 10:20:54 +000099 return CountDefiningLoaderClasses(defining_loader, classes_.back());
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700100}
101
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000102size_t ClassTable::NumReferencedZygoteClasses() const {
103 ReaderMutexLock mu(Thread::Current(), lock_);
104 size_t sum = 0;
105 for (size_t i = 0; i < classes_.size() - 1; ++i) {
Vladimir Marko54159c62018-06-20 14:30:08 +0100106 sum += classes_[i].size();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000107 }
108 return sum;
109}
110
111size_t ClassTable::NumReferencedNonZygoteClasses() const {
112 ReaderMutexLock mu(Thread::Current(), lock_);
Vladimir Marko54159c62018-06-20 14:30:08 +0100113 return classes_.back().size();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000114}
115
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700116mirror::Class* ClassTable::Lookup(const char* descriptor, size_t hash) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800117 DescriptorHashPair pair(descriptor, hash);
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800118 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700119 for (ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800120 auto it = class_set.FindWithHash(pair, hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700121 if (it != class_set.end()) {
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000122 return it->Read();
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700123 }
124 }
125 return nullptr;
126}
127
Vladimir Markocd556b02017-02-03 11:47:34 +0000128ObjPtr<mirror::Class> ClassTable::TryInsert(ObjPtr<mirror::Class> klass) {
129 TableSlot slot(klass);
130 WriterMutexLock mu(Thread::Current(), lock_);
131 for (ClassSet& class_set : classes_) {
Vladimir Marko54159c62018-06-20 14:30:08 +0100132 auto it = class_set.find(slot);
Vladimir Markocd556b02017-02-03 11:47:34 +0000133 if (it != class_set.end()) {
134 return it->Read();
135 }
136 }
Vladimir Marko54159c62018-06-20 14:30:08 +0100137 classes_.back().insert(slot);
Vladimir Markocd556b02017-02-03 11:47:34 +0000138 return klass;
139}
140
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700141void ClassTable::Insert(ObjPtr<mirror::Class> klass) {
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800142 const uint32_t hash = TableSlot::HashDescriptor(klass);
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700143 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800144 classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700145}
146
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +0000147void ClassTable::CopyWithoutLocks(const ClassTable& source_table) {
148 if (kIsDebugBuild) {
149 for (ClassSet& class_set : classes_) {
Vladimir Marko54159c62018-06-20 14:30:08 +0100150 CHECK(class_set.empty());
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +0000151 }
152 }
153 for (const ClassSet& class_set : source_table.classes_) {
154 for (const TableSlot& slot : class_set) {
Vladimir Marko54159c62018-06-20 14:30:08 +0100155 classes_.back().insert(slot);
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +0000156 }
157 }
158}
159
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700160void ClassTable::InsertWithoutLocks(ObjPtr<mirror::Class> klass) {
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800161 const uint32_t hash = TableSlot::HashDescriptor(klass);
162 classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
Mathieu Chartier496577f2016-09-20 15:33:31 -0700163}
164
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700165void ClassTable::InsertWithHash(ObjPtr<mirror::Class> klass, size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700166 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800167 classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700168}
169
170bool ClassTable::Remove(const char* descriptor) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800171 DescriptorHashPair pair(descriptor, ComputeModifiedUtf8Hash(descriptor));
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800172 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700173 for (ClassSet& class_set : classes_) {
Vladimir Marko54159c62018-06-20 14:30:08 +0100174 auto it = class_set.find(pair);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700175 if (it != class_set.end()) {
Vladimir Marko54159c62018-06-20 14:30:08 +0100176 class_set.erase(it);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700177 return true;
178 }
179 }
180 return false;
181}
182
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800183uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& slot)
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700184 const {
185 std::string temp;
Vladimir Markoc6934e32019-04-10 11:40:01 +0100186 // No read barrier needed, we're reading a chain of constant references for comparison
187 // with null and retrieval of constant primitive data. See ReadBarrierOption.
188 return ComputeModifiedUtf8Hash(slot.Read<kWithoutReadBarrier>()->GetDescriptor(&temp));
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700189}
190
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800191bool ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& a,
192 const TableSlot& b) const {
Vladimir Markoc6934e32019-04-10 11:40:01 +0100193 // No read barrier needed, we're reading a chain of constant references for comparison
194 // with null and retrieval of constant primitive data. See ReadBarrierOption.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800195 if (a.Hash() != b.Hash()) {
196 std::string temp;
Vladimir Markoc6934e32019-04-10 11:40:01 +0100197 DCHECK(!a.Read<kWithoutReadBarrier>()->DescriptorEquals(
198 b.Read<kWithoutReadBarrier>()->GetDescriptor(&temp)));
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800199 return false;
200 }
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700201 std::string temp;
Vladimir Markoc6934e32019-04-10 11:40:01 +0100202 return a.Read<kWithoutReadBarrier>()->DescriptorEquals(
203 b.Read<kWithoutReadBarrier>()->GetDescriptor(&temp));
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700204}
205
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800206bool ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& a,
207 const DescriptorHashPair& b) const {
Vladimir Markoc6934e32019-04-10 11:40:01 +0100208 // No read barrier needed, we're reading a chain of constant references for comparison
209 // with null and retrieval of constant primitive data. See ReadBarrierOption.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800210 if (!a.MaskedHashEquals(b.second)) {
Vladimir Markoc6934e32019-04-10 11:40:01 +0100211 DCHECK(!a.Read<kWithoutReadBarrier>()->DescriptorEquals(b.first));
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800212 return false;
213 }
Vladimir Markoc6934e32019-04-10 11:40:01 +0100214 return a.Read<kWithoutReadBarrier>()->DescriptorEquals(b.first);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700215}
216
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800217uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const DescriptorHashPair& pair) const {
218 return ComputeModifiedUtf8Hash(pair.first);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700219}
220
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700221bool ClassTable::InsertStrongRoot(ObjPtr<mirror::Object> obj) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700222 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700223 DCHECK(obj != nullptr);
224 for (GcRoot<mirror::Object>& root : strong_roots_) {
225 if (root.Read() == obj) {
Mathieu Chartier00310e02015-10-17 12:46:42 -0700226 return false;
227 }
228 }
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700229 strong_roots_.push_back(GcRoot<mirror::Object>(obj));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000230 // If `obj` is a dex cache associated with a new oat file with GC roots, add it to oat_files_.
231 if (obj->IsDexCache()) {
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700232 const DexFile* dex_file = ObjPtr<mirror::DexCache>::DownCast(obj)->GetDexFile();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000233 if (dex_file != nullptr && dex_file->GetOatDexFile() != nullptr) {
234 const OatFile* oat_file = dex_file->GetOatDexFile()->GetOatFile();
Mathieu Chartier1b868492016-11-16 16:22:37 -0800235 if (oat_file != nullptr && !oat_file->GetBssGcRoots().empty()) {
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000236 InsertOatFileLocked(oat_file); // Ignore return value.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000237 }
238 }
239 }
Mathieu Chartier00310e02015-10-17 12:46:42 -0700240 return true;
241}
242
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000243bool ClassTable::InsertOatFile(const OatFile* oat_file) {
244 WriterMutexLock mu(Thread::Current(), lock_);
245 return InsertOatFileLocked(oat_file);
246}
247
248bool 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
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800256size_t ClassTable::WriteToMemory(uint8_t* ptr) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700257 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800258 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_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800262 for (const TableSlot& root : class_set) {
Vladimir Marko54159c62018-06-20 14:30:08 +0100263 combined.insert(root);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800264 }
265 }
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800266 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 }
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800273 return ret;
274}
275
276size_t ClassTable::ReadFromMemory(uint8_t* ptr) {
277 size_t read_count = 0;
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800278 AddClassSet(ClassSet(ptr, /*make copy*/false, &read_count));
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800279 return read_count;
280}
281
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800282void ClassTable::AddClassSet(ClassSet&& set) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700283 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800284 classes_.insert(classes_.begin(), std::move(set));
285}
286
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700287void ClassTable::ClearStrongRoots() {
288 WriterMutexLock mu(Thread::Current(), lock_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000289 oat_files_.clear();
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700290 strong_roots_.clear();
291}
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800292
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800293ClassTable::TableSlot::TableSlot(ObjPtr<mirror::Class> klass)
294 : TableSlot(klass, HashDescriptor(klass)) {}
295
296uint32_t ClassTable::TableSlot::HashDescriptor(ObjPtr<mirror::Class> klass) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800297 std::string temp;
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800298 return ComputeModifiedUtf8Hash(klass->GetDescriptor(&temp));
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800299}
300
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700301} // namespace art