blob: af4f998fdf30f9ea899369e3e1c02dabe05314b0 [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
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070058mirror::Class* ClassTable::UpdateClass(const char* descriptor, mirror::Class* klass, size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070059 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070060 // Should only be updating latest table.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080061 DescriptorHashPair pair(descriptor, hash);
62 auto existing_it = classes_.back().FindWithHash(pair, hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070063 if (kIsDebugBuild && existing_it == classes_.back().end()) {
64 for (const ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080065 if (class_set.FindWithHash(pair, hash) != class_set.end()) {
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070066 LOG(FATAL) << "Updating class found in frozen table " << descriptor;
67 }
68 }
69 LOG(FATAL) << "Updating class not found " << descriptor;
70 }
71 mirror::Class* const existing = existing_it->Read();
72 CHECK_NE(existing, klass) << descriptor;
73 CHECK(!existing->IsResolved()) << descriptor;
74 CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusResolving) << descriptor;
75 CHECK(!klass->IsTemp()) << descriptor;
76 VerifyObject(klass);
77 // Update the element in the hash set with the new class. This is safe to do since the descriptor
78 // doesn't change.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080079 *existing_it = TableSlot(klass, hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070080 return existing;
81}
82
Vladimir Markoc5798bf2016-12-09 10:20:54 +000083size_t ClassTable::CountDefiningLoaderClasses(ObjPtr<mirror::ClassLoader> defining_loader,
84 const ClassSet& set) const {
85 size_t count = 0;
86 for (const TableSlot& root : set) {
87 if (root.Read()->GetClassLoader() == defining_loader) {
88 ++count;
89 }
90 }
91 return count;
92}
93
94size_t ClassTable::NumZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070095 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070096 size_t sum = 0;
97 for (size_t i = 0; i < classes_.size() - 1; ++i) {
Vladimir Markoc5798bf2016-12-09 10:20:54 +000098 sum += CountDefiningLoaderClasses(defining_loader, classes_[i]);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070099 }
100 return sum;
101}
102
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000103size_t ClassTable::NumNonZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700104 ReaderMutexLock mu(Thread::Current(), lock_);
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000105 return CountDefiningLoaderClasses(defining_loader, classes_.back());
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700106}
107
108mirror::Class* ClassTable::Lookup(const char* descriptor, size_t hash) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800109 DescriptorHashPair pair(descriptor, hash);
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800110 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700111 for (ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800112 auto it = class_set.FindWithHash(pair, hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700113 if (it != class_set.end()) {
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000114 return it->Read();
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700115 }
116 }
117 return nullptr;
118}
119
Vladimir Markocd556b02017-02-03 11:47:34 +0000120ObjPtr<mirror::Class> ClassTable::TryInsert(ObjPtr<mirror::Class> klass) {
121 TableSlot slot(klass);
122 WriterMutexLock mu(Thread::Current(), lock_);
123 for (ClassSet& class_set : classes_) {
124 auto it = class_set.Find(slot);
125 if (it != class_set.end()) {
126 return it->Read();
127 }
128 }
129 classes_.back().Insert(slot);
130 return klass;
131}
132
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700133void ClassTable::Insert(ObjPtr<mirror::Class> klass) {
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800134 const uint32_t hash = TableSlot::HashDescriptor(klass);
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700135 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800136 classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700137}
138
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +0000139void ClassTable::CopyWithoutLocks(const ClassTable& source_table) {
140 if (kIsDebugBuild) {
141 for (ClassSet& class_set : classes_) {
142 CHECK(class_set.Empty());
143 }
144 }
145 for (const ClassSet& class_set : source_table.classes_) {
146 for (const TableSlot& slot : class_set) {
147 classes_.back().Insert(slot);
148 }
149 }
150}
151
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700152void ClassTable::InsertWithoutLocks(ObjPtr<mirror::Class> klass) {
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800153 const uint32_t hash = TableSlot::HashDescriptor(klass);
154 classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
Mathieu Chartier496577f2016-09-20 15:33:31 -0700155}
156
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700157void ClassTable::InsertWithHash(ObjPtr<mirror::Class> klass, size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700158 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800159 classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700160}
161
162bool ClassTable::Remove(const char* descriptor) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800163 DescriptorHashPair pair(descriptor, ComputeModifiedUtf8Hash(descriptor));
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800164 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700165 for (ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800166 auto it = class_set.Find(pair);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700167 if (it != class_set.end()) {
168 class_set.Erase(it);
169 return true;
170 }
171 }
172 return false;
173}
174
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800175uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& slot)
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700176 const {
177 std::string temp;
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800178 return ComputeModifiedUtf8Hash(slot.Read()->GetDescriptor(&temp));
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700179}
180
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800181bool ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& a,
182 const TableSlot& b) const {
183 if (a.Hash() != b.Hash()) {
184 std::string temp;
185 DCHECK(!a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp)));
186 return false;
187 }
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700188 std::string temp;
189 return a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp));
190}
191
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800192bool ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& a,
193 const DescriptorHashPair& b) const {
194 if (!a.MaskedHashEquals(b.second)) {
195 DCHECK(!a.Read()->DescriptorEquals(b.first));
196 return false;
197 }
198 return a.Read()->DescriptorEquals(b.first);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700199}
200
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800201uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const DescriptorHashPair& pair) const {
202 return ComputeModifiedUtf8Hash(pair.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::WriteToMemory(uint8_t* ptr) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700241 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800242 ClassSet combined;
243 // Combine all the class sets in case there are multiple, also adjusts load factor back to
244 // default in case classes were pruned.
245 for (const ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800246 for (const TableSlot& root : class_set) {
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800247 combined.Insert(root);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800248 }
249 }
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800250 const size_t ret = combined.WriteToMemory(ptr);
251 // Sanity check.
252 if (kIsDebugBuild && ptr != nullptr) {
253 size_t read_count;
254 ClassSet class_set(ptr, /*make copy*/false, &read_count);
255 class_set.Verify();
256 }
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800257 return ret;
258}
259
260size_t ClassTable::ReadFromMemory(uint8_t* ptr) {
261 size_t read_count = 0;
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800262 AddClassSet(ClassSet(ptr, /*make copy*/false, &read_count));
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800263 return read_count;
264}
265
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800266void ClassTable::AddClassSet(ClassSet&& set) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700267 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800268 classes_.insert(classes_.begin(), std::move(set));
269}
270
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700271void ClassTable::ClearStrongRoots() {
272 WriterMutexLock mu(Thread::Current(), lock_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000273 oat_files_.clear();
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700274 strong_roots_.clear();
275}
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800276
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800277ClassTable::TableSlot::TableSlot(ObjPtr<mirror::Class> klass)
278 : TableSlot(klass, HashDescriptor(klass)) {}
279
280uint32_t ClassTable::TableSlot::HashDescriptor(ObjPtr<mirror::Class> klass) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800281 std::string temp;
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800282 return ComputeModifiedUtf8Hash(klass->GetDescriptor(&temp));
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800283}
284
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700285} // namespace art