blob: 7c3ad12b61ad97c9fe7c4eb7185db35bdfbdf9b3 [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
17#include "class_table.h"
18
19#include "mirror/class-inl.h"
20
21namespace art {
22
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070023ClassTable::ClassTable() : lock_("Class loader classes", kClassLoaderClassesLock) {
Mathieu Chartier32cc9ee2015-10-15 09:19:15 -070024 Runtime* const runtime = Runtime::Current();
25 classes_.push_back(ClassSet(runtime->GetHashTableMinLoadFactor(),
26 runtime->GetHashTableMaxLoadFactor()));
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070027}
28
29void ClassTable::FreezeSnapshot() {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070030 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070031 classes_.push_back(ClassSet());
32}
33
Mathieu Chartier28357fa2016-10-18 16:27:40 -070034bool ClassTable::Contains(ObjPtr<mirror::Class> klass) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070035 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartierdb70ce52016-12-12 11:06:59 -080036 TableSlot slot(klass);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070037 for (ClassSet& class_set : classes_) {
Mathieu Chartierdb70ce52016-12-12 11:06:59 -080038 auto it = class_set.Find(slot);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070039 if (it != class_set.end()) {
40 return it->Read() == klass;
41 }
42 }
43 return false;
44}
45
Mathieu Chartier28357fa2016-10-18 16:27:40 -070046mirror::Class* ClassTable::LookupByDescriptor(ObjPtr<mirror::Class> klass) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070047 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartierdb70ce52016-12-12 11:06:59 -080048 TableSlot slot(klass);
Mathieu Chartierfbc31082016-01-24 11:59:56 -080049 for (ClassSet& class_set : classes_) {
Mathieu Chartierdb70ce52016-12-12 11:06:59 -080050 auto it = class_set.Find(slot);
Mathieu Chartierfbc31082016-01-24 11:59:56 -080051 if (it != class_set.end()) {
52 return it->Read();
53 }
54 }
55 return nullptr;
56}
57
Jayant Chowdhary8de12972017-02-28 10:54:20 -080058// To take into account http://b/35845221
59#pragma clang diagnostic push
60#if __clang_major__ < 4
61#pragma clang diagnostic ignored "-Wunreachable-code"
62#endif
63
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070064mirror::Class* ClassTable::UpdateClass(const char* descriptor, mirror::Class* klass, size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070065 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070066 // Should only be updating latest table.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080067 DescriptorHashPair pair(descriptor, hash);
68 auto existing_it = classes_.back().FindWithHash(pair, hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070069 if (kIsDebugBuild && existing_it == classes_.back().end()) {
70 for (const ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080071 if (class_set.FindWithHash(pair, hash) != class_set.end()) {
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070072 LOG(FATAL) << "Updating class found in frozen table " << descriptor;
73 }
74 }
75 LOG(FATAL) << "Updating class not found " << descriptor;
76 }
77 mirror::Class* const existing = existing_it->Read();
78 CHECK_NE(existing, klass) << descriptor;
79 CHECK(!existing->IsResolved()) << descriptor;
80 CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusResolving) << descriptor;
81 CHECK(!klass->IsTemp()) << descriptor;
82 VerifyObject(klass);
83 // Update the element in the hash set with the new class. This is safe to do since the descriptor
84 // doesn't change.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080085 *existing_it = TableSlot(klass, hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070086 return existing;
87}
88
Jayant Chowdhary8de12972017-02-28 10:54:20 -080089#pragma clang diagnostic pop
90
Vladimir Markoc5798bf2016-12-09 10:20:54 +000091size_t ClassTable::CountDefiningLoaderClasses(ObjPtr<mirror::ClassLoader> defining_loader,
92 const ClassSet& set) const {
93 size_t count = 0;
94 for (const TableSlot& root : set) {
95 if (root.Read()->GetClassLoader() == defining_loader) {
96 ++count;
97 }
98 }
99 return count;
100}
101
102size_t ClassTable::NumZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700103 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700104 size_t sum = 0;
105 for (size_t i = 0; i < classes_.size() - 1; ++i) {
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000106 sum += CountDefiningLoaderClasses(defining_loader, classes_[i]);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700107 }
108 return sum;
109}
110
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000111size_t ClassTable::NumNonZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700112 ReaderMutexLock mu(Thread::Current(), lock_);
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000113 return CountDefiningLoaderClasses(defining_loader, classes_.back());
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700114}
115
116mirror::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_) {
132 auto it = class_set.Find(slot);
133 if (it != class_set.end()) {
134 return it->Read();
135 }
136 }
137 classes_.back().Insert(slot);
138 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_) {
150 CHECK(class_set.Empty());
151 }
152 }
153 for (const ClassSet& class_set : source_table.classes_) {
154 for (const TableSlot& slot : class_set) {
155 classes_.back().Insert(slot);
156 }
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_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800174 auto it = class_set.Find(pair);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700175 if (it != class_set.end()) {
176 class_set.Erase(it);
177 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;
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800186 return ComputeModifiedUtf8Hash(slot.Read()->GetDescriptor(&temp));
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700187}
188
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800189bool ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& a,
190 const TableSlot& b) const {
191 if (a.Hash() != b.Hash()) {
192 std::string temp;
193 DCHECK(!a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp)));
194 return false;
195 }
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700196 std::string temp;
197 return a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp));
198}
199
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800200bool ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& a,
201 const DescriptorHashPair& b) const {
202 if (!a.MaskedHashEquals(b.second)) {
203 DCHECK(!a.Read()->DescriptorEquals(b.first));
204 return false;
205 }
206 return a.Read()->DescriptorEquals(b.first);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700207}
208
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800209uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const DescriptorHashPair& pair) const {
210 return ComputeModifiedUtf8Hash(pair.first);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700211}
212
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700213bool ClassTable::InsertStrongRoot(ObjPtr<mirror::Object> obj) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700214 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700215 DCHECK(obj != nullptr);
216 for (GcRoot<mirror::Object>& root : strong_roots_) {
217 if (root.Read() == obj) {
Mathieu Chartier00310e02015-10-17 12:46:42 -0700218 return false;
219 }
220 }
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700221 strong_roots_.push_back(GcRoot<mirror::Object>(obj));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000222 // If `obj` is a dex cache associated with a new oat file with GC roots, add it to oat_files_.
223 if (obj->IsDexCache()) {
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700224 const DexFile* dex_file = ObjPtr<mirror::DexCache>::DownCast(obj)->GetDexFile();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000225 if (dex_file != nullptr && dex_file->GetOatDexFile() != nullptr) {
226 const OatFile* oat_file = dex_file->GetOatDexFile()->GetOatFile();
Mathieu Chartier1b868492016-11-16 16:22:37 -0800227 if (oat_file != nullptr && !oat_file->GetBssGcRoots().empty()) {
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000228 InsertOatFileLocked(oat_file); // Ignore return value.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000229 }
230 }
231 }
Mathieu Chartier00310e02015-10-17 12:46:42 -0700232 return true;
233}
234
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000235bool ClassTable::InsertOatFile(const OatFile* oat_file) {
236 WriterMutexLock mu(Thread::Current(), lock_);
237 return InsertOatFileLocked(oat_file);
238}
239
240bool ClassTable::InsertOatFileLocked(const OatFile* oat_file) {
241 if (ContainsElement(oat_files_, oat_file)) {
242 return false;
243 }
244 oat_files_.push_back(oat_file);
245 return true;
246}
247
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800248size_t ClassTable::WriteToMemory(uint8_t* ptr) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700249 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800250 ClassSet combined;
251 // Combine all the class sets in case there are multiple, also adjusts load factor back to
252 // default in case classes were pruned.
253 for (const ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800254 for (const TableSlot& root : class_set) {
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800255 combined.Insert(root);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800256 }
257 }
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800258 const size_t ret = combined.WriteToMemory(ptr);
259 // Sanity check.
260 if (kIsDebugBuild && ptr != nullptr) {
261 size_t read_count;
262 ClassSet class_set(ptr, /*make copy*/false, &read_count);
263 class_set.Verify();
264 }
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800265 return ret;
266}
267
268size_t ClassTable::ReadFromMemory(uint8_t* ptr) {
269 size_t read_count = 0;
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800270 AddClassSet(ClassSet(ptr, /*make copy*/false, &read_count));
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800271 return read_count;
272}
273
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800274void ClassTable::AddClassSet(ClassSet&& set) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700275 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800276 classes_.insert(classes_.begin(), std::move(set));
277}
278
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700279void ClassTable::ClearStrongRoots() {
280 WriterMutexLock mu(Thread::Current(), lock_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000281 oat_files_.clear();
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700282 strong_roots_.clear();
283}
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800284
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800285ClassTable::TableSlot::TableSlot(ObjPtr<mirror::Class> klass)
286 : TableSlot(klass, HashDescriptor(klass)) {}
287
288uint32_t ClassTable::TableSlot::HashDescriptor(ObjPtr<mirror::Class> klass) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800289 std::string temp;
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800290 return ComputeModifiedUtf8Hash(klass->GetDescriptor(&temp));
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800291}
292
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700293} // namespace art