blob: 212a7d6631f3f73b15956006be25836e697de4a0 [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#ifndef ART_RUNTIME_CLASS_TABLE_H_
18#define ART_RUNTIME_CLASS_TABLE_H_
19
20#include <string>
21#include <utility>
22#include <vector>
23
24#include "base/allocator.h"
25#include "base/hash_set.h"
26#include "base/macros.h"
27#include "base/mutex.h"
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070028#include "gc_root.h"
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070029#include "obj_ptr.h"
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070030
31namespace art {
32
Vladimir Markoaad75c62016-10-03 08:46:48 +000033class OatFile;
34
Vladimir Marko74527972016-11-29 15:57:32 +000035namespace linker {
36class ImageWriter;
37} // namespace linker
38
39namespace linker {
40class OatWriter;
41} // namespace linker
42
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070043namespace mirror {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080044class Class;
45class ClassLoader;
46class Object;
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070047} // namespace mirror
48
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070049// Each loader has a ClassTable
50class ClassTable {
51 public:
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080052 class TableSlot {
53 public:
54 TableSlot() : data_(0u) {}
55
Orion Hodson88591fe2018-03-06 13:35:43 +000056 TableSlot(const TableSlot& copy) : data_(copy.data_.load(std::memory_order_relaxed)) {}
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080057
58 explicit TableSlot(ObjPtr<mirror::Class> klass);
59
60 TableSlot(ObjPtr<mirror::Class> klass, uint32_t descriptor_hash);
61
62 TableSlot& operator=(const TableSlot& copy) {
Orion Hodson88591fe2018-03-06 13:35:43 +000063 data_.store(copy.data_.load(std::memory_order_relaxed), std::memory_order_relaxed);
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080064 return *this;
65 }
66
Vladimir Markoc6934e32019-04-10 11:40:01 +010067 bool IsNull() const REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080068
69 uint32_t Hash() const {
Orion Hodson88591fe2018-03-06 13:35:43 +000070 return MaskHash(data_.load(std::memory_order_relaxed));
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080071 }
72
73 static uint32_t MaskHash(uint32_t hash) {
74 return hash & kHashMask;
75 }
76
77 bool MaskedHashEquals(uint32_t other) const {
78 return MaskHash(other) == Hash();
79 }
80
81 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Vladimir Marko1fe58392019-04-10 16:14:56 +010082 ObjPtr<mirror::Class> Read() const REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080083
84 // NO_THREAD_SAFETY_ANALYSIS since the visitor may require heap bitmap lock.
85 template<typename Visitor>
86 void VisitRoot(const Visitor& visitor) const NO_THREAD_SAFETY_ANALYSIS;
87
88 private:
89 // Extract a raw pointer from an address.
90 static ObjPtr<mirror::Class> ExtractPtr(uint32_t data)
91 REQUIRES_SHARED(Locks::mutator_lock_);
92
93 static uint32_t Encode(ObjPtr<mirror::Class> klass, uint32_t hash_bits)
94 REQUIRES_SHARED(Locks::mutator_lock_);
95
96 // Data contains the class pointer GcRoot as well as the low bits of the descriptor hash.
97 mutable Atomic<uint32_t> data_;
Stelios Ioannoud9696b72021-06-14 15:49:26 +010098 static constexpr uint32_t kHashMask = kObjectAlignment - 1;
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080099 };
100
101 using DescriptorHashPair = std::pair<const char*, uint32_t>;
102
Vladimir Markoeae6a712021-02-05 13:33:28 +0000103 class ClassDescriptorHash {
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800104 public:
105 // uint32_t for cross compilation.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800106 uint32_t operator()(const TableSlot& slot) const NO_THREAD_SAFETY_ANALYSIS;
Vladimir Markoeae6a712021-02-05 13:33:28 +0000107 // uint32_t for cross compilation.
108 uint32_t operator()(const DescriptorHashPair& pair) const NO_THREAD_SAFETY_ANALYSIS;
109 };
110
111 class ClassDescriptorEquals {
112 public:
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800113 // Same class loader and descriptor.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800114 bool operator()(const TableSlot& a, const TableSlot& b) const
Mathieu Chartier6beced42016-11-15 15:51:31 -0800115 NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800116 // Same descriptor.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800117 bool operator()(const TableSlot& a, const DescriptorHashPair& b) const
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800118 NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800119 };
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800120
121 class TableSlotEmptyFn {
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800122 public:
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800123 void MakeEmpty(TableSlot& item) const NO_THREAD_SAFETY_ANALYSIS {
124 item = TableSlot();
125 DCHECK(IsEmpty(item));
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800126 }
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800127 bool IsEmpty(const TableSlot& item) const NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800128 return item.IsNull();
129 }
130 };
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800131
132 // Hash set that hashes class descriptor, and compares descriptors and class loaders. Results
133 // should be compared for a matching class descriptor and class loader.
Vladimir Marko4f990712021-07-14 12:45:13 +0100134 using ClassSet = HashSet<TableSlot,
135 TableSlotEmptyFn,
136 ClassDescriptorHash,
137 ClassDescriptorEquals,
138 TrackingAllocator<TableSlot, kAllocatorTagClassTable>>;
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800139
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700140 ClassTable();
141
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700142 // Freeze the current class tables by allocating a new table and never updating or modifying the
143 // existing table. This helps prevents dirty pages after caused by inserting after zygote fork.
144 void FreezeSnapshot()
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700145 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700146 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700147
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000148 // Returns the number of classes in previous snapshots defined by `defining_loader`.
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000149 size_t NumZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const
150 REQUIRES(!lock_)
151 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700152
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000153 // Returns all off the classes in the lastest snapshot defined by `defining_loader`.
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000154 size_t NumNonZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const
155 REQUIRES(!lock_)
156 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700157
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000158 // Returns the number of classes in previous snapshots no matter the defining loader.
159 size_t NumReferencedZygoteClasses() const
160 REQUIRES(!lock_)
161 REQUIRES_SHARED(Locks::mutator_lock_);
162
163 // Returns all off the classes in the lastest snapshot no matter the defining loader.
164 size_t NumReferencedNonZygoteClasses() const
165 REQUIRES(!lock_)
166 REQUIRES_SHARED(Locks::mutator_lock_);
167
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700168 // Update a class in the table with the new class. Returns the existing class which was replaced.
Vladimir Marko1fe58392019-04-10 16:14:56 +0100169 ObjPtr<mirror::Class> UpdateClass(const char* descriptor,
170 ObjPtr<mirror::Class> new_klass,
171 size_t hash)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700172 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700173 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700174
Mathieu Chartiere4275c02015-08-06 15:34:15 -0700175 // NO_THREAD_SAFETY_ANALYSIS for object marking requiring heap bitmap lock.
176 template<class Visitor>
177 void VisitRoots(Visitor& visitor)
Mathieu Chartier00310e02015-10-17 12:46:42 -0700178 NO_THREAD_SAFETY_ANALYSIS
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700179 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700180 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700181
Mathieu Chartiere4275c02015-08-06 15:34:15 -0700182 template<class Visitor>
183 void VisitRoots(const Visitor& visitor)
Mathieu Chartier00310e02015-10-17 12:46:42 -0700184 NO_THREAD_SAFETY_ANALYSIS
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700185 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700186 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700187
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -0800188 // Stops visit if the visitor returns false.
Vladimir Marko5f958f62022-02-08 12:01:07 +0000189 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier, typename Visitor>
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -0800190 bool Visit(Visitor& visitor)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700191 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700192 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko5f958f62022-02-08 12:01:07 +0000193 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier, typename Visitor>
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800194 bool Visit(const Visitor& visitor)
195 REQUIRES(!lock_)
196 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700197
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800198 // Return the first class that matches the descriptor. Returns null if there are none.
Vladimir Marko1fe58392019-04-10 16:14:56 +0100199 ObjPtr<mirror::Class> Lookup(const char* descriptor, size_t hash)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700200 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700201 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700202
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800203 // Return the first class that matches the descriptor of klass. Returns null if there are none.
Vladimir Marko1dab5752021-02-25 15:41:33 +0000204 // Used for tests and debug-build checks.
Vladimir Marko1fe58392019-04-10 16:14:56 +0100205 ObjPtr<mirror::Class> LookupByDescriptor(ObjPtr<mirror::Class> klass)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700206 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700207 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800208
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700209 void Insert(ObjPtr<mirror::Class> klass)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700210 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700211 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700212
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700213 void InsertWithHash(ObjPtr<mirror::Class> klass, size_t hash)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700214 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700215 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700216
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700217 // Return true if we inserted the strong root, false if it already exists.
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700218 bool InsertStrongRoot(ObjPtr<mirror::Object> obj)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700219 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700220 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700221
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000222 // Return true if we inserted the oat file, false if it already exists.
223 bool InsertOatFile(const OatFile* oat_file)
224 REQUIRES(!lock_)
225 REQUIRES_SHARED(Locks::mutator_lock_);
226
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800227 // Read a table from ptr and put it at the front of the class set.
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800228 size_t ReadFromMemory(uint8_t* ptr)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700229 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700230 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800231
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800232 // Add a class set to the front of classes.
233 void AddClassSet(ClassSet&& set)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700234 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700235 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700236
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700237 // Clear strong roots (other than classes themselves).
238 void ClearStrongRoots()
239 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700240 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700241
Mathieu Chartier72041a02017-07-14 18:23:25 -0700242 // Filter strong roots (other than classes themselves).
243 template <typename Filter>
244 void RemoveStrongRoots(const Filter& filter)
245 REQUIRES(!lock_)
246 REQUIRES_SHARED(Locks::mutator_lock_);
247
Mathieu Chartier92091bd2016-05-10 18:13:20 -0700248 ReaderWriterMutex& GetLock() {
249 return lock_;
250 }
251
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800252 private:
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000253 size_t CountDefiningLoaderClasses(ObjPtr<mirror::ClassLoader> defining_loader,
254 const ClassSet& set) const
255 REQUIRES(lock_)
256 REQUIRES_SHARED(Locks::mutator_lock_);
257
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000258 // Return true if we inserted the oat file, false if it already exists.
259 bool InsertOatFileLocked(const OatFile* oat_file)
260 REQUIRES(lock_)
261 REQUIRES_SHARED(Locks::mutator_lock_);
262
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700263 // Lock to guard inserting and removing.
264 mutable ReaderWriterMutex lock_;
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700265 // We have a vector to help prevent dirty pages after the zygote forks by calling FreezeSnapshot.
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700266 std::vector<ClassSet> classes_ GUARDED_BY(lock_);
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700267 // Extra strong roots that can be either dex files or dex caches. Dex files used by the class
268 // loader which may not be owned by the class loader must be held strongly live. Also dex caches
269 // are held live to prevent them being unloading once they have classes in them.
270 std::vector<GcRoot<mirror::Object>> strong_roots_ GUARDED_BY(lock_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000271 // Keep track of oat files with GC roots associated with dex caches in `strong_roots_`.
272 std::vector<const OatFile*> oat_files_ GUARDED_BY(lock_);
Mathieu Chartier496577f2016-09-20 15:33:31 -0700273
Vladimir Marko74527972016-11-29 15:57:32 +0000274 friend class linker::ImageWriter; // for InsertWithoutLocks.
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700275};
276
277} // namespace art
278
279#endif // ART_RUNTIME_CLASS_TABLE_H_