blob: ff846a718ec2afd46ad0feb503fb8ac3646ee229 [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
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +0000132void ClassTable::CopyWithoutLocks(const ClassTable& source_table) {
133 if (kIsDebugBuild) {
134 for (ClassSet& class_set : classes_) {
135 CHECK(class_set.Empty());
136 }
137 }
138 for (const ClassSet& class_set : source_table.classes_) {
139 for (const TableSlot& slot : class_set) {
140 classes_.back().Insert(slot);
141 }
142 }
143}
144
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700145void ClassTable::InsertWithoutLocks(ObjPtr<mirror::Class> klass) {
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800146 const uint32_t hash = TableSlot::HashDescriptor(klass);
147 classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
Mathieu Chartier496577f2016-09-20 15:33:31 -0700148}
149
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700150void ClassTable::InsertWithHash(ObjPtr<mirror::Class> klass, size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700151 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800152 classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700153}
154
155bool ClassTable::Remove(const char* descriptor) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800156 DescriptorHashPair pair(descriptor, ComputeModifiedUtf8Hash(descriptor));
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800157 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700158 for (ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800159 auto it = class_set.Find(pair);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700160 if (it != class_set.end()) {
161 class_set.Erase(it);
162 return true;
163 }
164 }
165 return false;
166}
167
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800168uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& slot)
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700169 const {
170 std::string temp;
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800171 return ComputeModifiedUtf8Hash(slot.Read()->GetDescriptor(&temp));
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700172}
173
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800174bool ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& a,
175 const TableSlot& b) const {
176 if (a.Hash() != b.Hash()) {
177 std::string temp;
178 DCHECK(!a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp)));
179 return false;
180 }
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700181 std::string temp;
182 return a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp));
183}
184
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800185bool ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& a,
186 const DescriptorHashPair& b) const {
187 if (!a.MaskedHashEquals(b.second)) {
188 DCHECK(!a.Read()->DescriptorEquals(b.first));
189 return false;
190 }
191 return a.Read()->DescriptorEquals(b.first);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700192}
193
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800194uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const DescriptorHashPair& pair) const {
195 return ComputeModifiedUtf8Hash(pair.first);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700196}
197
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700198bool ClassTable::InsertStrongRoot(ObjPtr<mirror::Object> obj) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700199 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700200 DCHECK(obj != nullptr);
201 for (GcRoot<mirror::Object>& root : strong_roots_) {
202 if (root.Read() == obj) {
Mathieu Chartier00310e02015-10-17 12:46:42 -0700203 return false;
204 }
205 }
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700206 strong_roots_.push_back(GcRoot<mirror::Object>(obj));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000207 // If `obj` is a dex cache associated with a new oat file with GC roots, add it to oat_files_.
208 if (obj->IsDexCache()) {
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700209 const DexFile* dex_file = ObjPtr<mirror::DexCache>::DownCast(obj)->GetDexFile();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000210 if (dex_file != nullptr && dex_file->GetOatDexFile() != nullptr) {
211 const OatFile* oat_file = dex_file->GetOatDexFile()->GetOatFile();
Mathieu Chartier1b868492016-11-16 16:22:37 -0800212 if (oat_file != nullptr && !oat_file->GetBssGcRoots().empty()) {
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000213 InsertOatFileLocked(oat_file); // Ignore return value.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000214 }
215 }
216 }
Mathieu Chartier00310e02015-10-17 12:46:42 -0700217 return true;
218}
219
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000220bool ClassTable::InsertOatFile(const OatFile* oat_file) {
221 WriterMutexLock mu(Thread::Current(), lock_);
222 return InsertOatFileLocked(oat_file);
223}
224
225bool ClassTable::InsertOatFileLocked(const OatFile* oat_file) {
226 if (ContainsElement(oat_files_, oat_file)) {
227 return false;
228 }
229 oat_files_.push_back(oat_file);
230 return true;
231}
232
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800233size_t ClassTable::WriteToMemory(uint8_t* ptr) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700234 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800235 ClassSet combined;
236 // Combine all the class sets in case there are multiple, also adjusts load factor back to
237 // default in case classes were pruned.
238 for (const ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800239 for (const TableSlot& root : class_set) {
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800240 combined.Insert(root);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800241 }
242 }
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800243 const size_t ret = combined.WriteToMemory(ptr);
244 // Sanity check.
245 if (kIsDebugBuild && ptr != nullptr) {
246 size_t read_count;
247 ClassSet class_set(ptr, /*make copy*/false, &read_count);
248 class_set.Verify();
249 }
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800250 return ret;
251}
252
253size_t ClassTable::ReadFromMemory(uint8_t* ptr) {
254 size_t read_count = 0;
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800255 AddClassSet(ClassSet(ptr, /*make copy*/false, &read_count));
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800256 return read_count;
257}
258
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800259void ClassTable::AddClassSet(ClassSet&& set) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700260 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800261 classes_.insert(classes_.begin(), std::move(set));
262}
263
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700264void ClassTable::ClearStrongRoots() {
265 WriterMutexLock mu(Thread::Current(), lock_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000266 oat_files_.clear();
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700267 strong_roots_.clear();
268}
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800269
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800270ClassTable::TableSlot::TableSlot(ObjPtr<mirror::Class> klass)
271 : TableSlot(klass, HashDescriptor(klass)) {}
272
273uint32_t ClassTable::TableSlot::HashDescriptor(ObjPtr<mirror::Class> klass) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800274 std::string temp;
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800275 return ComputeModifiedUtf8Hash(klass->GetDescriptor(&temp));
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800276}
277
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700278} // namespace art