blob: 26cd3ec2499a263493ac919831ebb8d6b269a7e1 [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
Mathieu Chartierdb70ce52016-12-12 11:06:59 -080081 static uint32_t HashDescriptor(ObjPtr<mirror::Class> klass)
82 REQUIRES_SHARED(Locks::mutator_lock_);
83
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -080084 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
85 mirror::Class* Read() const REQUIRES_SHARED(Locks::mutator_lock_);
86
87 // NO_THREAD_SAFETY_ANALYSIS since the visitor may require heap bitmap lock.
88 template<typename Visitor>
89 void VisitRoot(const Visitor& visitor) const NO_THREAD_SAFETY_ANALYSIS;
90
91 private:
92 // Extract a raw pointer from an address.
93 static ObjPtr<mirror::Class> ExtractPtr(uint32_t data)
94 REQUIRES_SHARED(Locks::mutator_lock_);
95
96 static uint32_t Encode(ObjPtr<mirror::Class> klass, uint32_t hash_bits)
97 REQUIRES_SHARED(Locks::mutator_lock_);
98
99 // Data contains the class pointer GcRoot as well as the low bits of the descriptor hash.
100 mutable Atomic<uint32_t> data_;
101 static const uint32_t kHashMask = kObjectAlignment - 1;
102 };
103
104 using DescriptorHashPair = std::pair<const char*, uint32_t>;
105
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800106 class ClassDescriptorHashEquals {
107 public:
108 // uint32_t for cross compilation.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800109 uint32_t operator()(const TableSlot& slot) const NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800110 // Same class loader and descriptor.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800111 bool operator()(const TableSlot& a, const TableSlot& b) const
Mathieu Chartier6beced42016-11-15 15:51:31 -0800112 NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800113 // Same descriptor.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800114 bool operator()(const TableSlot& a, const DescriptorHashPair& b) const
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800115 NO_THREAD_SAFETY_ANALYSIS;
116 // uint32_t for cross compilation.
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800117 uint32_t operator()(const DescriptorHashPair& pair) const NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800118 };
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800119
120 class TableSlotEmptyFn {
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800121 public:
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800122 void MakeEmpty(TableSlot& item) const NO_THREAD_SAFETY_ANALYSIS {
123 item = TableSlot();
124 DCHECK(IsEmpty(item));
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800125 }
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800126 bool IsEmpty(const TableSlot& item) const NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800127 return item.IsNull();
128 }
129 };
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800130
131 // Hash set that hashes class descriptor, and compares descriptors and class loaders. Results
132 // should be compared for a matching class descriptor and class loader.
133 typedef HashSet<TableSlot,
134 TableSlotEmptyFn,
135 ClassDescriptorHashEquals,
136 ClassDescriptorHashEquals,
137 TrackingAllocator<TableSlot, kAllocatorTagClassTable>> ClassSet;
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800138
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700139 ClassTable();
140
141 // Used by image writer for checking.
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700142 bool Contains(ObjPtr<mirror::Class> klass)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700143 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700144 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700145
146 // Freeze the current class tables by allocating a new table and never updating or modifying the
147 // existing table. This helps prevents dirty pages after caused by inserting after zygote fork.
148 void FreezeSnapshot()
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700149 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700150 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700151
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000152 // Returns the number of classes in previous snapshots defined by `defining_loader`.
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000153 size_t NumZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const
154 REQUIRES(!lock_)
155 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700156
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000157 // Returns all off the classes in the lastest snapshot defined by `defining_loader`.
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000158 size_t NumNonZygoteClasses(ObjPtr<mirror::ClassLoader> defining_loader) const
159 REQUIRES(!lock_)
160 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700161
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000162 // Returns the number of classes in previous snapshots no matter the defining loader.
163 size_t NumReferencedZygoteClasses() const
164 REQUIRES(!lock_)
165 REQUIRES_SHARED(Locks::mutator_lock_);
166
167 // Returns all off the classes in the lastest snapshot no matter the defining loader.
168 size_t NumReferencedNonZygoteClasses() const
169 REQUIRES(!lock_)
170 REQUIRES_SHARED(Locks::mutator_lock_);
171
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700172 // Update a class in the table with the new class. Returns the existing class which was replaced.
173 mirror::Class* UpdateClass(const char* descriptor, mirror::Class* new_klass, size_t hash)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700174 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700175 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700176
Mathieu Chartiere4275c02015-08-06 15:34:15 -0700177 // NO_THREAD_SAFETY_ANALYSIS for object marking requiring heap bitmap lock.
178 template<class Visitor>
179 void VisitRoots(Visitor& visitor)
Mathieu Chartier00310e02015-10-17 12:46:42 -0700180 NO_THREAD_SAFETY_ANALYSIS
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700181 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700182 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700183
Mathieu Chartiere4275c02015-08-06 15:34:15 -0700184 template<class Visitor>
185 void VisitRoots(const Visitor& visitor)
Mathieu Chartier00310e02015-10-17 12:46:42 -0700186 NO_THREAD_SAFETY_ANALYSIS
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700187 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700188 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700189
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -0800190 // Stops visit if the visitor returns false.
Alexey Grebenkinbe4c2bd2018-02-01 19:09:59 +0300191 template <typename Visitor, ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -0800192 bool Visit(Visitor& visitor)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700193 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700194 REQUIRES_SHARED(Locks::mutator_lock_);
Alexey Grebenkinbe4c2bd2018-02-01 19:09:59 +0300195 template <typename Visitor, ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Mathieu Chartierdb70ce52016-12-12 11:06:59 -0800196 bool Visit(const Visitor& visitor)
197 REQUIRES(!lock_)
198 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700199
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800200 // Return the first class that matches the descriptor. Returns null if there are none.
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700201 mirror::Class* Lookup(const char* descriptor, size_t hash)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700202 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700203 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700204
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800205 // Return the first class that matches the descriptor of klass. Returns null if there are none.
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700206 mirror::Class* LookupByDescriptor(ObjPtr<mirror::Class> klass)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700207 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700208 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800209
Vladimir Markocd556b02017-02-03 11:47:34 +0000210 // Try to insert a class and return the inserted class if successful. If another class
211 // with the same descriptor is already in the table, return the existing entry.
212 ObjPtr<mirror::Class> TryInsert(ObjPtr<mirror::Class> klass)
213 REQUIRES(!lock_)
214 REQUIRES_SHARED(Locks::mutator_lock_);
215
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700216 void Insert(ObjPtr<mirror::Class> klass)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700217 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700218 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700219
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700220 void InsertWithHash(ObjPtr<mirror::Class> klass, size_t hash)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700221 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700222 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700223
224 // Returns true if the class was found and removed, false otherwise.
225 bool Remove(const char* descriptor)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700226 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700227 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier00310e02015-10-17 12:46:42 -0700228
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700229 // Return true if we inserted the strong root, false if it already exists.
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700230 bool InsertStrongRoot(ObjPtr<mirror::Object> obj)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700231 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700232 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700233
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000234 // Return true if we inserted the oat file, false if it already exists.
235 bool InsertOatFile(const OatFile* oat_file)
236 REQUIRES(!lock_)
237 REQUIRES_SHARED(Locks::mutator_lock_);
238
Mathieu Chartier41dc8ce2015-12-04 15:07:48 -0800239 // Combines all of the tables into one class set.
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800240 size_t WriteToMemory(uint8_t* ptr) const
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700241 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700242 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800243
244 // Read a table from ptr and put it at the front of the class set.
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800245 size_t ReadFromMemory(uint8_t* ptr)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700246 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700247 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800248
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800249 // Add a class set to the front of classes.
250 void AddClassSet(ClassSet&& set)
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700251 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700252 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700253
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700254 // Clear strong roots (other than classes themselves).
255 void ClearStrongRoots()
256 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700257 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700258
Mathieu Chartier72041a02017-07-14 18:23:25 -0700259 // Filter strong roots (other than classes themselves).
260 template <typename Filter>
261 void RemoveStrongRoots(const Filter& filter)
262 REQUIRES(!lock_)
263 REQUIRES_SHARED(Locks::mutator_lock_);
264
Mathieu Chartier92091bd2016-05-10 18:13:20 -0700265 ReaderWriterMutex& GetLock() {
266 return lock_;
267 }
268
Mathieu Chartier88027bd2016-03-02 16:08:31 -0800269 private:
Mathieu Chartier8c19d242017-03-06 12:35:10 -0800270 // Only copies classes.
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +0000271 void CopyWithoutLocks(const ClassTable& source_table) NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700272 void InsertWithoutLocks(ObjPtr<mirror::Class> klass) NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier496577f2016-09-20 15:33:31 -0700273
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000274 size_t CountDefiningLoaderClasses(ObjPtr<mirror::ClassLoader> defining_loader,
275 const ClassSet& set) const
276 REQUIRES(lock_)
277 REQUIRES_SHARED(Locks::mutator_lock_);
278
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000279 // Return true if we inserted the oat file, false if it already exists.
280 bool InsertOatFileLocked(const OatFile* oat_file)
281 REQUIRES(lock_)
282 REQUIRES_SHARED(Locks::mutator_lock_);
283
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700284 // Lock to guard inserting and removing.
285 mutable ReaderWriterMutex lock_;
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700286 // We have a vector to help prevent dirty pages after the zygote forks by calling FreezeSnapshot.
Mathieu Chartier1609e3a2016-04-05 14:36:57 -0700287 std::vector<ClassSet> classes_ GUARDED_BY(lock_);
Mathieu Chartierc9dbb1d2016-06-03 17:47:32 -0700288 // Extra strong roots that can be either dex files or dex caches. Dex files used by the class
289 // loader which may not be owned by the class loader must be held strongly live. Also dex caches
290 // are held live to prevent them being unloading once they have classes in them.
291 std::vector<GcRoot<mirror::Object>> strong_roots_ GUARDED_BY(lock_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000292 // Keep track of oat files with GC roots associated with dex caches in `strong_roots_`.
293 std::vector<const OatFile*> oat_files_ GUARDED_BY(lock_);
Mathieu Chartier496577f2016-09-20 15:33:31 -0700294
Vladimir Marko74527972016-11-29 15:57:32 +0000295 friend class linker::ImageWriter; // for InsertWithoutLocks.
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700296};
297
298} // namespace art
299
300#endif // ART_RUNTIME_CLASS_TABLE_H_