blob: b7d37e2e37f05300665e30f5974441eaae5cbe04 [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
Vladimir Marko1fe58392019-04-10 16:14:56 +010040ObjPtr<mirror::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
Vladimir Marko1fe58392019-04-10 16:14:56 +010052ObjPtr<mirror::Class> ClassTable::UpdateClass(const char* descriptor,
53 ObjPtr<mirror::Class> klass,
54 size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070055 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070056 // Should only be updating latest table.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080057 DescriptorHashPair pair(descriptor, hash);
58 auto existing_it = classes_.back().FindWithHash(pair, hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070059 if (kIsDebugBuild && existing_it == classes_.back().end()) {
60 for (const ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080061 if (class_set.FindWithHash(pair, hash) != class_set.end()) {
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070062 LOG(FATAL) << "Updating class found in frozen table " << descriptor;
63 }
64 }
65 LOG(FATAL) << "Updating class not found " << descriptor;
66 }
Vladimir Marko1fe58392019-04-10 16:14:56 +010067 const ObjPtr<mirror::Class> existing = existing_it->Read();
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070068 CHECK_NE(existing, klass) << descriptor;
69 CHECK(!existing->IsResolved()) << descriptor;
Vladimir Marko2c64a832018-01-04 11:31:56 +000070 CHECK_EQ(klass->GetStatus(), ClassStatus::kResolving) << descriptor;
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070071 CHECK(!klass->IsTemp()) << descriptor;
72 VerifyObject(klass);
73 // Update the element in the hash set with the new class. This is safe to do since the descriptor
74 // doesn't change.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080075 *existing_it = TableSlot(klass, hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070076 return existing;
77}
78
Vladimir Markoc5798bf2016-12-09 10:20:54 +000079size_t ClassTable::CountDefiningLoaderClasses(ObjPtr<mirror::ClassLoader> defining_loader,
80 const ClassSet& set) const {
81 size_t count = 0;
82 for (const TableSlot& root : set) {
83 if (root.Read()->GetClassLoader() == defining_loader) {
84 ++count;
85 }
86 }
87 return count;
88}
89
90size_t ClassTable::NumZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -070091 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070092 size_t sum = 0;
93 for (size_t i = 0; i < classes_.size() - 1; ++i) {
Vladimir Markoc5798bf2016-12-09 10:20:54 +000094 sum += CountDefiningLoaderClasses(defining_loader, classes_[i]);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070095 }
96 return sum;
97}
98
Vladimir Markoc5798bf2016-12-09 10:20:54 +000099size_t ClassTable::NumNonZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700100 ReaderMutexLock mu(Thread::Current(), lock_);
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000101 return CountDefiningLoaderClasses(defining_loader, classes_.back());
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700102}
103
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000104size_t ClassTable::NumReferencedZygoteClasses() const {
105 ReaderMutexLock mu(Thread::Current(), lock_);
106 size_t sum = 0;
107 for (size_t i = 0; i < classes_.size() - 1; ++i) {
Vladimir Marko54159c62018-06-20 14:30:08 +0100108 sum += classes_[i].size();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000109 }
110 return sum;
111}
112
113size_t ClassTable::NumReferencedNonZygoteClasses() const {
114 ReaderMutexLock mu(Thread::Current(), lock_);
Vladimir Marko54159c62018-06-20 14:30:08 +0100115 return classes_.back().size();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000116}
117
Vladimir Marko1fe58392019-04-10 16:14:56 +0100118ObjPtr<mirror::Class> ClassTable::Lookup(const char* descriptor, size_t hash) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800119 DescriptorHashPair pair(descriptor, hash);
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800120 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700121 for (ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800122 auto it = class_set.FindWithHash(pair, hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700123 if (it != class_set.end()) {
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000124 return it->Read();
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700125 }
126 }
127 return nullptr;
128}
129
Vladimir Markocd556b02017-02-03 11:47:34 +0000130ObjPtr<mirror::Class> ClassTable::TryInsert(ObjPtr<mirror::Class> klass) {
131 TableSlot slot(klass);
132 WriterMutexLock mu(Thread::Current(), lock_);
133 for (ClassSet& class_set : classes_) {
Vladimir Marko54159c62018-06-20 14:30:08 +0100134 auto it = class_set.find(slot);
Vladimir Markocd556b02017-02-03 11:47:34 +0000135 if (it != class_set.end()) {
136 return it->Read();
137 }
138 }
Vladimir Marko54159c62018-06-20 14:30:08 +0100139 classes_.back().insert(slot);
Vladimir Markocd556b02017-02-03 11:47:34 +0000140 return klass;
141}
142
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700143void ClassTable::Insert(ObjPtr<mirror::Class> klass) {
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800144 const uint32_t hash = TableSlot::HashDescriptor(klass);
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700145 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800146 classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700147}
148
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +0000149void ClassTable::CopyWithoutLocks(const ClassTable& source_table) {
150 if (kIsDebugBuild) {
151 for (ClassSet& class_set : classes_) {
Vladimir Marko54159c62018-06-20 14:30:08 +0100152 CHECK(class_set.empty());
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +0000153 }
154 }
155 for (const ClassSet& class_set : source_table.classes_) {
156 for (const TableSlot& slot : class_set) {
Vladimir Marko54159c62018-06-20 14:30:08 +0100157 classes_.back().insert(slot);
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +0000158 }
159 }
160}
161
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700162void ClassTable::InsertWithoutLocks(ObjPtr<mirror::Class> klass) {
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800163 const uint32_t hash = TableSlot::HashDescriptor(klass);
164 classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
Mathieu Chartier496577f2016-09-20 15:33:31 -0700165}
166
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700167void ClassTable::InsertWithHash(ObjPtr<mirror::Class> klass, size_t hash) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700168 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800169 classes_.back().InsertWithHash(TableSlot(klass, hash), hash);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700170}
171
172bool ClassTable::Remove(const char* descriptor) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800173 DescriptorHashPair pair(descriptor, ComputeModifiedUtf8Hash(descriptor));
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800174 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700175 for (ClassSet& class_set : classes_) {
Vladimir Marko54159c62018-06-20 14:30:08 +0100176 auto it = class_set.find(pair);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700177 if (it != class_set.end()) {
Vladimir Marko54159c62018-06-20 14:30:08 +0100178 class_set.erase(it);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700179 return true;
180 }
181 }
182 return false;
183}
184
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800185uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& slot)
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700186 const {
187 std::string temp;
Vladimir Markoc6934e32019-04-10 11:40:01 +0100188 // No read barrier needed, we're reading a chain of constant references for comparison
189 // with null and retrieval of constant primitive data. See ReadBarrierOption.
190 return ComputeModifiedUtf8Hash(slot.Read<kWithoutReadBarrier>()->GetDescriptor(&temp));
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700191}
192
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800193bool ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& a,
194 const TableSlot& b) const {
Vladimir Markoc6934e32019-04-10 11:40:01 +0100195 // No read barrier needed, we're reading a chain of constant references for comparison
196 // with null and retrieval of constant primitive data. See ReadBarrierOption.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800197 if (a.Hash() != b.Hash()) {
198 std::string temp;
Vladimir Markoc6934e32019-04-10 11:40:01 +0100199 DCHECK(!a.Read<kWithoutReadBarrier>()->DescriptorEquals(
200 b.Read<kWithoutReadBarrier>()->GetDescriptor(&temp)));
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800201 return false;
202 }
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700203 std::string temp;
Vladimir Markoc6934e32019-04-10 11:40:01 +0100204 return a.Read<kWithoutReadBarrier>()->DescriptorEquals(
205 b.Read<kWithoutReadBarrier>()->GetDescriptor(&temp));
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700206}
207
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800208bool ClassTable::ClassDescriptorHashEquals::operator()(const TableSlot& a,
209 const DescriptorHashPair& b) const {
Vladimir Markoc6934e32019-04-10 11:40:01 +0100210 // No read barrier needed, we're reading a chain of constant references for comparison
211 // with null and retrieval of constant primitive data. See ReadBarrierOption.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800212 if (!a.MaskedHashEquals(b.second)) {
Vladimir Markoc6934e32019-04-10 11:40:01 +0100213 DCHECK(!a.Read<kWithoutReadBarrier>()->DescriptorEquals(b.first));
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800214 return false;
215 }
Vladimir Markoc6934e32019-04-10 11:40:01 +0100216 return a.Read<kWithoutReadBarrier>()->DescriptorEquals(b.first);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700217}
218
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800219uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const DescriptorHashPair& pair) const {
220 return ComputeModifiedUtf8Hash(pair.first);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700221}
222
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700223bool ClassTable::InsertStrongRoot(ObjPtr<mirror::Object> obj) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700224 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700225 DCHECK(obj != nullptr);
226 for (GcRoot<mirror::Object>& root : strong_roots_) {
227 if (root.Read() == obj) {
Mathieu Chartier00310e02015-10-17 12:46:42 -0700228 return false;
229 }
230 }
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700231 strong_roots_.push_back(GcRoot<mirror::Object>(obj));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000232 // If `obj` is a dex cache associated with a new oat file with GC roots, add it to oat_files_.
233 if (obj->IsDexCache()) {
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700234 const DexFile* dex_file = ObjPtr<mirror::DexCache>::DownCast(obj)->GetDexFile();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000235 if (dex_file != nullptr && dex_file->GetOatDexFile() != nullptr) {
236 const OatFile* oat_file = dex_file->GetOatDexFile()->GetOatFile();
Mathieu Chartier1b868492016-11-16 16:22:37 -0800237 if (oat_file != nullptr && !oat_file->GetBssGcRoots().empty()) {
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000238 InsertOatFileLocked(oat_file); // Ignore return value.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000239 }
240 }
241 }
Mathieu Chartier00310e02015-10-17 12:46:42 -0700242 return true;
243}
244
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000245bool ClassTable::InsertOatFile(const OatFile* oat_file) {
246 WriterMutexLock mu(Thread::Current(), lock_);
247 return InsertOatFileLocked(oat_file);
248}
249
250bool ClassTable::InsertOatFileLocked(const OatFile* oat_file) {
251 if (ContainsElement(oat_files_, oat_file)) {
252 return false;
253 }
254 oat_files_.push_back(oat_file);
255 return true;
256}
257
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800258size_t ClassTable::WriteToMemory(uint8_t* ptr) const {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700259 ReaderMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800260 ClassSet combined;
261 // Combine all the class sets in case there are multiple, also adjusts load factor back to
262 // default in case classes were pruned.
263 for (const ClassSet& class_set : classes_) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800264 for (const TableSlot& root : class_set) {
Vladimir Marko54159c62018-06-20 14:30:08 +0100265 combined.insert(root);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800266 }
267 }
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800268 const size_t ret = combined.WriteToMemory(ptr);
269 // Sanity check.
270 if (kIsDebugBuild && ptr != nullptr) {
271 size_t read_count;
272 ClassSet class_set(ptr, /*make copy*/false, &read_count);
273 class_set.Verify();
274 }
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800275 return ret;
276}
277
278size_t ClassTable::ReadFromMemory(uint8_t* ptr) {
279 size_t read_count = 0;
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800280 AddClassSet(ClassSet(ptr, /*make copy*/false, &read_count));
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800281 return read_count;
282}
283
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800284void ClassTable::AddClassSet(ClassSet&& set) {
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700285 WriterMutexLock mu(Thread::Current(), lock_);
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800286 classes_.insert(classes_.begin(), std::move(set));
287}
288
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700289void ClassTable::ClearStrongRoots() {
290 WriterMutexLock mu(Thread::Current(), lock_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000291 oat_files_.clear();
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700292 strong_roots_.clear();
293}
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800294
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800295ClassTable::TableSlot::TableSlot(ObjPtr<mirror::Class> klass)
296 : TableSlot(klass, HashDescriptor(klass)) {}
297
298uint32_t ClassTable::TableSlot::HashDescriptor(ObjPtr<mirror::Class> klass) {
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800299 std::string temp;
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800300 return ComputeModifiedUtf8Hash(klass->GetDescriptor(&temp));
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800301}
302
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700303} // namespace art