blob: 0f985c6424f85d5fda8e190dafe020016681af0d [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
Pirama Arumuga Nainar813b9c42016-08-25 23:42:50 -070058// Bug: http://b/31104323 Ignore -Wunreachable-code from the for loop below
59#pragma clang diagnostic push
60#pragma clang diagnostic ignored "-Wunreachable-code"
61
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070062mirror::Class* ClassTable::UpdateClass(const char* descriptor, mirror::Class* klass, size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070063 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070064 // Should only be updating latest table.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080065 DescriptorHashPair pair(descriptor, hash);
66 auto existing_it = classes_.back().FindWithHash(pair, hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070067 if (kIsDebugBuild && existing_it == classes_.back().end()) {
68 for (const ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080069 if (class_set.FindWithHash(pair, hash) != class_set.end()) {
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070070 LOG(FATAL) << "Updating class found in frozen table " << descriptor;
71 }
72 }
73 LOG(FATAL) << "Updating class not found " << descriptor;
74 }
75 mirror::Class* const existing = existing_it->Read();
76 CHECK_NE(existing, klass) << descriptor;
77 CHECK(!existing->IsResolved()) << descriptor;
78 CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusResolving) << descriptor;
79 CHECK(!klass->IsTemp()) << descriptor;
80 VerifyObject(klass);
81 // Update the element in the hash set with the new class. This is safe to do since the descriptor
82 // doesn't change.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080083 *existing_it = TableSlot(klass, hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070084 return existing;
85}
86
Pirama Arumuga Nainar813b9c42016-08-25 23:42:50 -070087#pragma clang diagnostic pop // http://b/31104323
88
Vladimir Markoc5798bf2016-12-09 10:20:54 +000089size_t ClassTable::CountDefiningLoaderClasses(ObjPtr<mirror::ClassLoader> defining_loader,
90 const ClassSet& set) const {
91 size_t count = 0;
92 for (const TableSlot& root : set) {
93 if (root.Read()->GetClassLoader() == defining_loader) {
94 ++count;
95 }
96 }
97 return count;
98}
99
100size_t ClassTable::NumZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700101 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700102 size_t sum = 0;
103 for (size_t i = 0; i < classes_.size() - 1; ++i) {
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000104 sum += CountDefiningLoaderClasses(defining_loader, classes_[i]);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700105 }
106 return sum;
107}
108
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000109size_t ClassTable::NumNonZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700110 ReaderMutexLock mu(Thread::Current(), lock_);
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000111 return CountDefiningLoaderClasses(defining_loader, classes_.back());
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700112}
113
114mirror::Class* ClassTable::Lookup(const char* descriptor, size_t hash) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800115 DescriptorHashPair pair(descriptor, hash);
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800116 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700117 for (ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800118 auto it = class_set.FindWithHash(pair, hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700119 if (it != class_set.end()) {
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000120 return it->Read();
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700121 }
122 }
123 return nullptr;
124}
125
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700126void ClassTable::Insert(ObjPtr<mirror::Class> klass) {
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800127 const uint32_t hash = TableSlot::HashDescriptor(klass);
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700128 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800129 classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700130}
131
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700132void ClassTable::InsertWithoutLocks(ObjPtr<mirror::Class> klass) {
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800133 const uint32_t hash = TableSlot::HashDescriptor(klass);
134 classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
Mathieu Chartier496577f2016-09-20 15:33:31 -0700135}
136
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700137void ClassTable::InsertWithHash(ObjPtr<mirror::Class> klass, size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700138 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800139 classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700140}
141
142bool ClassTable::Remove(const char* descriptor) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800143 DescriptorHashPair pair(descriptor, ComputeModifiedUtf8Hash(descriptor));
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800144 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700145 for (ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800146 auto it = class_set.Find(pair);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700147 if (it != class_set.end()) {
148 class_set.Erase(it);
149 return true;
150 }
151 }
152 return false;
153}
154
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800155uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& slot)
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700156 const {
157 std::string temp;
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800158 return ComputeModifiedUtf8Hash(slot.Read()->GetDescriptor(&temp));
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700159}
160
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800161bool ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& a,
162 const TableSlot& b) const {
163 if (a.Hash() != b.Hash()) {
164 std::string temp;
165 DCHECK(!a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp)));
166 return false;
167 }
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700168 std::string temp;
169 return a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp));
170}
171
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800172bool ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& a,
173 const DescriptorHashPair& b) const {
174 if (!a.MaskedHashEquals(b.second)) {
175 DCHECK(!a.Read()->DescriptorEquals(b.first));
176 return false;
177 }
178 return a.Read()->DescriptorEquals(b.first);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700179}
180
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800181uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const DescriptorHashPair& pair) const {
182 return ComputeModifiedUtf8Hash(pair.first);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700183}
184
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700185bool ClassTable::InsertStrongRoot(ObjPtr<mirror::Object> obj) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700186 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700187 DCHECK(obj != nullptr);
188 for (GcRoot<mirror::Object>& root : strong_roots_) {
189 if (root.Read() == obj) {
Mathieu Chartier00310e02015-10-17 12:46:42 -0700190 return false;
191 }
192 }
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700193 strong_roots_.push_back(GcRoot<mirror::Object>(obj));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000194 // If `obj` is a dex cache associated with a new oat file with GC roots, add it to oat_files_.
195 if (obj->IsDexCache()) {
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700196 const DexFile* dex_file = ObjPtr<mirror::DexCache>::DownCast(obj)->GetDexFile();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000197 if (dex_file != nullptr && dex_file->GetOatDexFile() != nullptr) {
198 const OatFile* oat_file = dex_file->GetOatDexFile()->GetOatFile();
Mathieu Chartier1b868492016-11-16 16:22:37 -0800199 if (oat_file != nullptr && !oat_file->GetBssGcRoots().empty()) {
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000200 InsertOatFileLocked(oat_file); // Ignore return value.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000201 }
202 }
203 }
Mathieu Chartier00310e02015-10-17 12:46:42 -0700204 return true;
205}
206
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000207bool ClassTable::InsertOatFile(const OatFile* oat_file) {
208 WriterMutexLock mu(Thread::Current(), lock_);
209 return InsertOatFileLocked(oat_file);
210}
211
212bool ClassTable::InsertOatFileLocked(const OatFile* oat_file) {
213 if (ContainsElement(oat_files_, oat_file)) {
214 return false;
215 }
216 oat_files_.push_back(oat_file);
217 return true;
218}
219
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800220size_t ClassTable::WriteToMemory(uint8_t* ptr) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700221 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800222 ClassSet combined;
223 // Combine all the class sets in case there are multiple, also adjusts load factor back to
224 // default in case classes were pruned.
225 for (const ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800226 for (const TableSlot& root : class_set) {
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800227 combined.Insert(root);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800228 }
229 }
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800230 const size_t ret = combined.WriteToMemory(ptr);
231 // Sanity check.
232 if (kIsDebugBuild && ptr != nullptr) {
233 size_t read_count;
234 ClassSet class_set(ptr, /*make copy*/false, &read_count);
235 class_set.Verify();
236 }
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800237 return ret;
238}
239
240size_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