blob: 084b534a70832a289c6dc571d5c1aad83fcba339 [file] [log] [blame]
Elliott Hughes6c1a3942011-08-17 15:00:06 -07001/*
2 * Copyright (C) 2009 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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_INDIRECT_REFERENCE_TABLE_H_
18#define ART_RUNTIME_INDIRECT_REFERENCE_TABLE_H_
Elliott Hughes6c1a3942011-08-17 15:00:06 -070019
Elliott Hughes07ed66b2012-12-12 18:34:25 -080020#include <stdint.h>
Elliott Hughes6c1a3942011-08-17 15:00:06 -070021
22#include <iosfwd>
Andreas Gampee03662b2016-10-13 17:12:56 -070023#include <limits>
Elliott Hughes6c1a3942011-08-17 15:00:06 -070024#include <string>
25
Andreas Gampe57943812017-12-06 21:39:13 -080026#include <android-base/logging.h>
27
Andreas Gampedc061d02016-10-24 13:19:37 -070028#include "base/bit_utils.h"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080029#include "base/locks.h"
Andreas Gampe57943812017-12-06 21:39:13 -080030#include "base/macros.h"
Vladimir Markoc34bebf2018-08-16 16:12:49 +010031#include "base/mem_map.h"
Hans Boehm4dcac362021-09-23 12:26:04 -070032#include "base/mutex.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070033#include "gc_root.h"
Mathieu Chartier8778c522016-10-04 19:06:30 -070034#include "obj_ptr.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "offsets.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070036#include "read_barrier_option.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080037
Elliott Hughes6c1a3942011-08-17 15:00:06 -070038namespace art {
Ian Rogers68d8b422014-07-17 11:09:10 -070039
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080040class RootInfo;
41
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042namespace mirror {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070043class Object;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044} // namespace mirror
Elliott Hughes6c1a3942011-08-17 15:00:06 -070045
Andreas Gampee03662b2016-10-13 17:12:56 -070046// Maintain a table of indirect references. Used for local/global JNI references.
47//
48// The table contains object references, where the strong (local/global) references are part of the
49// GC root set (but not the weak global references). When an object is added we return an
50// IndirectRef that is not a valid pointer but can be used to find the original value in O(1) time.
51// Conversions to and from indirect references are performed on upcalls and downcalls, so they need
52// to be very fast.
53//
54// To be efficient for JNI local variable storage, we need to provide operations that allow us to
55// operate on segments of the table, where segments are pushed and popped as if on a stack. For
56// example, deletion of an entry should only succeed if it appears in the current segment, and we
57// want to be able to strip off the current segment quickly when a method returns. Additions to the
58// table must be made in the current segment even if space is available in an earlier area.
59//
60// A new segment is created when we call into native code from interpreted code, or when we handle
61// the JNI PushLocalFrame function.
62//
63// The GC must be able to scan the entire table quickly.
64//
65// In summary, these must be very fast:
66// - adding or removing a segment
67// - adding references to a new segment
68// - converting an indirect reference back to an Object
69// These can be a little slower, but must still be pretty quick:
70// - adding references to a "mature" segment
71// - removing individual references
72// - scanning the entire table straight through
73//
74// If there's more than one segment, we don't guarantee that the table will fill completely before
75// we fail due to lack of space. We do ensure that the current segment will pack tightly, which
76// should satisfy JNI requirements (e.g. EnsureLocalCapacity).
77//
78// Only SynchronizedGet is synchronized.
Elliott Hughes6c1a3942011-08-17 15:00:06 -070079
Andreas Gampee03662b2016-10-13 17:12:56 -070080// Indirect reference definition. This must be interchangeable with JNI's jobject, and it's
81// convenient to let null be null, so we use void*.
82//
83// We need a (potentially) large table index and a 2-bit reference type (global, local, weak
84// global). We also reserve some bits to be used to detect stale indirect references: we put a
85// serial number in the extra bits, and keep a copy of the serial number in the table. This requires
86// more memory and additional memory accesses on add/get, but is moving-GC safe. It will catch
87// additional problems, e.g.: create iref1 for obj, delete iref1, create iref2 for same obj,
88// lookup iref1. A pattern based on object bits will miss this.
Vladimir Marko4f990712021-07-14 12:45:13 +010089using IndirectRef = void*;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070090
Andreas Gampee03662b2016-10-13 17:12:56 -070091// Indirect reference kind, used as the two low bits of IndirectRef.
92//
93// For convenience these match up with enum jobjectRefType from jni.h.
Elliott Hughes6c1a3942011-08-17 15:00:06 -070094enum IndirectRefKind {
Vladimir Markocedec9d2021-02-08 16:16:13 +000095 kJniTransitionOrInvalid = 0, // <<JNI transition frame reference or invalid reference>>
96 kLocal = 1, // <<local reference>>
97 kGlobal = 2, // <<global reference>>
98 kWeakGlobal = 3, // <<weak global reference>>
99 kLastKind = kWeakGlobal
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700100};
Vladimir Marko9974e3c2020-06-10 16:27:06 +0100101std::ostream& operator<<(std::ostream& os, IndirectRefKind rhs);
Andreas Gampef1e86302016-10-03 11:42:31 -0700102const char* GetIndirectRefKindString(const IndirectRefKind& kind);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700103
Andreas Gampee03662b2016-10-13 17:12:56 -0700104// Table definition.
105//
106// For the global reference table, the expected common operations are adding a new entry and
107// removing a recently-added entry (usually the most-recently-added entry). For JNI local
108// references, the common operations are adding a new entry and removing an entire table segment.
109//
110// If we delete entries from the middle of the list, we will be left with "holes". We track the
111// number of holes so that, when adding new elements, we can quickly decide to do a trivial append
112// or go slot-hunting.
113//
114// When the top-most entry is removed, any holes immediately below it are also removed. Thus,
115// deletion of an entry may reduce "top_index" by more than one.
116//
117// To get the desired behavior for JNI locals, we need to know the bottom and top of the current
118// "segment". The top is managed internally, and the bottom is passed in as a function argument.
119// When we call a native method or push a local frame, the current top index gets pushed on, and
120// serves as the new bottom. When we pop a frame off, the value from the stack becomes the new top
121// index, and the value stored in the previous frame becomes the new bottom.
122//
123// Holes are being locally cached for the segment. Otherwise we'd have to pass bottom index and
124// number of holes, which restricts us to 16 bits for the top index. The value is cached within the
125// table. To avoid code in generated JNI transitions, which implicitly form segments, the code for
126// adding and removing references needs to detect the change of a segment. Helper fields are used
127// for this detection.
128//
129// Common alternative implementation: make IndirectRef a pointer to the actual reference slot.
130// Instead of getting a table and doing a lookup, the lookup can be done instantly. Operations like
131// determining the type and deleting the reference are more expensive because the table must be
132// hunted for (i.e. you have to do a pointer comparison to see which table it's in), you can't move
133// the table when expanding it (so realloc() is out), and tricks like serial number checking to
134// detect stale references aren't possible (though we may be able to get similar benefits with other
135// approaches).
136//
137// TODO: consider a "lastDeleteIndex" for quick hole-filling when an add immediately follows a
138// delete; must invalidate after segment pop might be worth only using it for JNI globals.
139//
140// TODO: may want completely different add/remove algorithms for global and local refs to improve
141// performance. A large circular buffer might reduce the amortized cost of adding global
142// references.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700143
Andreas Gampee03662b2016-10-13 17:12:56 -0700144// The state of the current segment. We only store the index. Splitting it for index and hole
145// count restricts the range too much.
146struct IRTSegmentState {
147 uint32_t top_index;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700148};
149
Andreas Gampee03662b2016-10-13 17:12:56 -0700150// Use as initial value for "cookie", and when table has only one segment.
151static constexpr IRTSegmentState kIRTFirstSegment = { 0 };
152
Hans Boehmf8dea012021-09-22 13:28:35 -0700153// We associate a few bits of serial number with each reference, for error checking.
154static constexpr unsigned int kIRTSerialBits = 3;
155static constexpr uint32_t kIRTMaxSerial = ((1 << kIRTSerialBits) - 1);
Andreas Gampedc061d02016-10-24 13:19:37 -0700156
Hiroshi Yamauchi9e47bfa2015-02-23 11:14:40 -0800157class IrtEntry {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700158 public:
Mathieu Chartier8778c522016-10-04 19:06:30 -0700159 void Add(ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_);
160
Mathieu Chartier4838d662014-09-25 15:27:43 -0700161 GcRoot<mirror::Object>* GetReference() {
Hans Boehmf8dea012021-09-22 13:28:35 -0700162 DCHECK_LE(serial_, kIRTMaxSerial);
163 return &reference_;
Mathieu Chartier4838d662014-09-25 15:27:43 -0700164 }
Mathieu Chartier8778c522016-10-04 19:06:30 -0700165
Andreas Gampee03662b2016-10-13 17:12:56 -0700166 const GcRoot<mirror::Object>* GetReference() const {
Hans Boehmf8dea012021-09-22 13:28:35 -0700167 DCHECK_LE(serial_, kIRTMaxSerial);
168 return &reference_;
Andreas Gampee03662b2016-10-13 17:12:56 -0700169 }
170
Mathieu Chartier4838d662014-09-25 15:27:43 -0700171 uint32_t GetSerial() const {
172 return serial_;
173 }
Mathieu Chartier8778c522016-10-04 19:06:30 -0700174
175 void SetReference(ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4838d662014-09-25 15:27:43 -0700176
177 private:
Hans Boehmf8dea012021-09-22 13:28:35 -0700178 uint32_t serial_; // Incremented for each reuse; checked against reference.
179 GcRoot<mirror::Object> reference_;
Mathieu Chartier4838d662014-09-25 15:27:43 -0700180};
Hans Boehmf8dea012021-09-22 13:28:35 -0700181static_assert(sizeof(IrtEntry) == 2 * sizeof(uint32_t), "Unexpected sizeof(IrtEntry)");
Andreas Gampedc061d02016-10-24 13:19:37 -0700182static_assert(IsPowerOfTwo(sizeof(IrtEntry)), "Unexpected sizeof(IrtEntry)");
Mathieu Chartier4838d662014-09-25 15:27:43 -0700183
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700184class IrtIterator {
185 public:
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700186 IrtIterator(IrtEntry* table, size_t i, size_t capacity) REQUIRES_SHARED(Locks::mutator_lock_)
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700187 : table_(table), i_(i), capacity_(capacity) {
Chih-Hung Hsieh4c84ca62018-01-16 10:15:40 -0800188 // capacity_ is used in some target; has warning with unused attribute.
189 UNUSED(capacity_);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700190 }
191
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700192 IrtIterator& operator++() REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700193 ++i_;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700194 return *this;
195 }
196
Mathieu Chartier8778c522016-10-04 19:06:30 -0700197 GcRoot<mirror::Object>* operator*() REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -0700198 // This does not have a read barrier as this is used to visit roots.
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700199 return table_[i_].GetReference();
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700200 }
201
202 bool equals(const IrtIterator& rhs) const {
203 return (i_ == rhs.i_ && table_ == rhs.table_);
204 }
205
206 private:
Mathieu Chartier4838d662014-09-25 15:27:43 -0700207 IrtEntry* const table_;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700208 size_t i_;
Ian Rogersc0542af2014-09-03 16:16:56 -0700209 const size_t capacity_;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700210};
211
Elliott Hughes726079d2011-10-07 18:43:44 -0700212bool inline operator==(const IrtIterator& lhs, const IrtIterator& rhs) {
213 return lhs.equals(rhs);
214}
215
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700216bool inline operator!=(const IrtIterator& lhs, const IrtIterator& rhs) {
217 return !lhs.equals(rhs);
218}
219
Hans Boehm4dcac362021-09-23 12:26:04 -0700220// We initially allocate local reference tables with a very small number of entries, packing
221// multiple tables into a single page. If we need to expand one, we allocate them in units of
222// pages.
223// TODO: We should allocate all IRT tables as nonmovable Java objects, That in turn works better
224// if we break up each table into 2 parallel arrays, one for the Java reference, and one for the
225// serial number. The current scheme page-aligns regions containing IRT tables, and so allows them
226// to be identified and page-protected in the future.
227constexpr size_t kInitialIrtBytes = 512; // Number of bytes in an initial local table.
228constexpr size_t kSmallIrtEntries = kInitialIrtBytes / sizeof(IrtEntry);
229static_assert(kPageSize % kInitialIrtBytes == 0);
230static_assert(kInitialIrtBytes % sizeof(IrtEntry) == 0);
231static_assert(kInitialIrtBytes % sizeof(void *) == 0);
232
233// A minimal stopgap allocator for initial small local IRT tables.
234class SmallIrtAllocator {
235 public:
236 SmallIrtAllocator();
237
238 // Allocate an IRT table for kSmallIrtEntries.
239 IrtEntry* Allocate(std::string* error_msg) REQUIRES(!lock_);
240
241 void Deallocate(IrtEntry* unneeded) REQUIRES(!lock_);
242
243 private:
244 // A free list of kInitialIrtBytes chunks linked through the first word.
245 IrtEntry* small_irt_freelist_;
246
247 // Repository of MemMaps used for small IRT tables.
248 std::vector<MemMap> shared_irt_maps_;
249
Hans Boehm808d8cc2021-10-29 14:57:37 -0700250 Mutex lock_; // Level kGenericBottomLock; acquired before mem_map_lock_, which is a C++ mutex.
Hans Boehm4dcac362021-09-23 12:26:04 -0700251};
252
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700253class IndirectReferenceTable {
254 public:
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700255 enum class ResizableCapacity {
256 kNo,
257 kYes
258 };
259
Andreas Gampee03662b2016-10-13 17:12:56 -0700260 // WARNING: Construction of the IndirectReferenceTable may fail.
261 // error_msg must not be null. If error_msg is set by the constructor, then
262 // construction has failed and the IndirectReferenceTable will be in an
263 // invalid state. Use IsValid to check whether the object is in an invalid
264 // state.
Hans Boehm4dcac362021-09-23 12:26:04 -0700265 // Max_count is the minimum initial capacity (resizable), or minimum total capacity
266 // (not resizable). A value of 1 indicates an implementation-convenient small size.
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700267 IndirectReferenceTable(size_t max_count,
268 IndirectRefKind kind,
269 ResizableCapacity resizable,
270 std::string* error_msg);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700271
272 ~IndirectReferenceTable();
273
Richard Uhlerda0a69e2016-10-11 15:06:38 +0100274 /*
275 * Checks whether construction of the IndirectReferenceTable succeeded.
276 *
277 * This object must only be used if IsValid() returns true. It is safe to
278 * call IsValid from multiple threads without locking or other explicit
279 * synchronization.
280 */
Andreas Gampe3f5881f2015-04-08 10:26:16 -0700281 bool IsValid() const;
282
Andreas Gampee03662b2016-10-13 17:12:56 -0700283 // Add a new entry. "obj" must be a valid non-null object reference. This function will
Andreas Gampe25651122017-09-25 14:50:23 -0700284 // return null if an error happened (with an appropriate error message set).
285 IndirectRef Add(IRTSegmentState previous_state,
286 ObjPtr<mirror::Object> obj,
287 std::string* error_msg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700288 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700289
Andreas Gampee03662b2016-10-13 17:12:56 -0700290 // Given an IndirectRef in the table, return the Object it refers to.
291 //
292 // This function may abort under error conditions.
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -0700293 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Mathieu Chartier8778c522016-10-04 19:06:30 -0700294 ObjPtr<mirror::Object> Get(IndirectRef iref) const REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartierc56057e2014-05-04 13:18:58 -0700295 ALWAYS_INLINE;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700296
Mathieu Chartierc56057e2014-05-04 13:18:58 -0700297 // Synchronized get which reads a reference, acquiring a lock if necessary.
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -0700298 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Mathieu Chartier8778c522016-10-04 19:06:30 -0700299 ObjPtr<mirror::Object> SynchronizedGet(IndirectRef iref) const
300 REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -0700301 return Get<kReadBarrierOption>(iref);
Mathieu Chartierc56057e2014-05-04 13:18:58 -0700302 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700303
Andreas Gampee03662b2016-10-13 17:12:56 -0700304 // Updates an existing indirect reference to point to a new object.
Mathieu Chartier8778c522016-10-04 19:06:30 -0700305 void Update(IndirectRef iref, ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_);
Jeff Hao39b6c242015-05-19 20:30:23 -0700306
Andreas Gampee03662b2016-10-13 17:12:56 -0700307 // Remove an existing entry.
308 //
309 // If the entry is not between the current top index and the bottom index
310 // specified by the cookie, we don't remove anything. This is the behavior
311 // required by JNI's DeleteLocalRef function.
312 //
313 // Returns "false" if nothing was removed.
314 bool Remove(IRTSegmentState previous_state, IndirectRef iref);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700315
Mathieu Chartier8778c522016-10-04 19:06:30 -0700316 void AssertEmpty() REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes726079d2011-10-07 18:43:44 -0700317
Andreas Gampe8a2a1fc2017-09-29 17:53:18 -0700318 void Dump(std::ostream& os) const
319 REQUIRES_SHARED(Locks::mutator_lock_)
320 REQUIRES(!Locks::alloc_tracker_lock_);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700321
Vladimir Marko17491ac2020-12-01 12:02:29 +0000322 IndirectRefKind GetKind() const {
323 return kind_;
324 }
325
Andreas Gampee03662b2016-10-13 17:12:56 -0700326 // Return the #of entries in the entire table. This includes holes, and
327 // so may be larger than the actual number of "live" entries.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700328 size_t Capacity() const {
Andreas Gampee03662b2016-10-13 17:12:56 -0700329 return segment_state_.top_index;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700330 }
331
Hans Boehm592bcea2021-07-02 22:20:52 -0700332 // Return the number of non-null entries in the table. Only reliable for a
333 // single segment table.
334 int32_t NEntriesForGlobal() {
335 return segment_state_.top_index - current_num_holes_;
336 }
337
Andreas Gampe88831082017-05-31 19:46:03 -0700338 // Ensure that at least free_capacity elements are available, or return false.
339 bool EnsureFreeCapacity(size_t free_capacity, std::string* error_msg)
340 REQUIRES_SHARED(Locks::mutator_lock_);
341 // See implementation of EnsureFreeCapacity. We'll only state here how much is trivially free,
342 // without recovering holes. Thus this is a conservative estimate.
Andreas Gampe1b35b462017-09-29 18:52:15 -0700343 size_t FreeCapacity() const;
Andreas Gampe88831082017-05-31 19:46:03 -0700344
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -0700345 // Note IrtIterator does not have a read barrier as it's used to visit roots.
Mathieu Chartier02e25112013-08-14 16:14:24 -0700346 IrtIterator begin() {
347 return IrtIterator(table_, 0, Capacity());
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700348 }
349
Mathieu Chartier02e25112013-08-14 16:14:24 -0700350 IrtIterator end() {
351 return IrtIterator(table_, Capacity(), Capacity());
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700352 }
353
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700354 void VisitRoots(RootVisitor* visitor, const RootInfo& root_info)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700355 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700356
Andreas Gampee03662b2016-10-13 17:12:56 -0700357 IRTSegmentState GetSegmentState() const {
358 return segment_state_;
Ian Rogersad25ac52011-10-04 19:13:33 -0700359 }
360
Andreas Gampee03662b2016-10-13 17:12:56 -0700361 void SetSegmentState(IRTSegmentState new_state);
Ian Rogersad25ac52011-10-04 19:13:33 -0700362
Andreas Gampe4d98c842015-12-09 15:14:04 -0800363 static Offset SegmentStateOffset(size_t pointer_size ATTRIBUTE_UNUSED) {
364 // Note: Currently segment_state_ is at offset 0. We're testing the expected value in
365 // jni_internal_test to make sure it stays correct. It is not OFFSETOF_MEMBER, as that
366 // is not pointer-size-safe.
367 return Offset(0);
Ian Rogersdc51b792011-09-22 20:41:37 -0700368 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800369
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800370 // Release pages past the end of the table that may have previously held references.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700371 void Trim() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800372
Andreas Gampedc061d02016-10-24 13:19:37 -0700373 // Determine what kind of indirect reference this is. Opposite of EncodeIndirectRefKind.
374 ALWAYS_INLINE static inline IndirectRefKind GetIndirectRefKind(IndirectRef iref) {
375 return DecodeIndirectRefKind(reinterpret_cast<uintptr_t>(iref));
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700376 }
377
Vladimir Marko17491ac2020-12-01 12:02:29 +0000378 /* Reference validation for CheckJNI. */
379 bool IsValidReference(IndirectRef, /*out*/std::string* error_msg) const
380 REQUIRES_SHARED(Locks::mutator_lock_);
381
Andreas Gampedc061d02016-10-24 13:19:37 -0700382 private:
Hans Boehmf8dea012021-09-22 13:28:35 -0700383 static constexpr uint32_t kShiftedSerialMask = (1u << kIRTSerialBits) - 1;
Andreas Gampedc061d02016-10-24 13:19:37 -0700384
385 static constexpr size_t kKindBits = MinimumBitsToStore(
386 static_cast<uint32_t>(IndirectRefKind::kLastKind));
387 static constexpr uint32_t kKindMask = (1u << kKindBits) - 1;
388
389 static constexpr uintptr_t EncodeIndex(uint32_t table_index) {
390 static_assert(sizeof(IndirectRef) == sizeof(uintptr_t), "Unexpected IndirectRef size");
Hans Boehmf8dea012021-09-22 13:28:35 -0700391 DCHECK_LE(MinimumBitsToStore(table_index), BitSizeOf<uintptr_t>() - kIRTSerialBits - kKindBits);
392 return (static_cast<uintptr_t>(table_index) << kKindBits << kIRTSerialBits);
Andreas Gampedc061d02016-10-24 13:19:37 -0700393 }
394 static constexpr uint32_t DecodeIndex(uintptr_t uref) {
Hans Boehmf8dea012021-09-22 13:28:35 -0700395 return static_cast<uint32_t>((uref >> kKindBits) >> kIRTSerialBits);
Andreas Gampedc061d02016-10-24 13:19:37 -0700396 }
397
398 static constexpr uintptr_t EncodeIndirectRefKind(IndirectRefKind kind) {
399 return static_cast<uintptr_t>(kind);
400 }
401 static constexpr IndirectRefKind DecodeIndirectRefKind(uintptr_t uref) {
402 return static_cast<IndirectRefKind>(uref & kKindMask);
403 }
404
405 static constexpr uintptr_t EncodeSerial(uint32_t serial) {
Hans Boehmf8dea012021-09-22 13:28:35 -0700406 DCHECK_LE(MinimumBitsToStore(serial), kIRTSerialBits);
Andreas Gampedc061d02016-10-24 13:19:37 -0700407 return serial << kKindBits;
408 }
409 static constexpr uint32_t DecodeSerial(uintptr_t uref) {
410 return static_cast<uint32_t>(uref >> kKindBits) & kShiftedSerialMask;
411 }
412
413 constexpr uintptr_t EncodeIndirectRef(uint32_t table_index, uint32_t serial) const {
414 DCHECK_LT(table_index, max_entries_);
415 return EncodeIndex(table_index) | EncodeSerial(serial) | EncodeIndirectRefKind(kind_);
416 }
417
418 static void ConstexprChecks();
419
420 // Extract the table index from an indirect reference.
421 ALWAYS_INLINE static uint32_t ExtractIndex(IndirectRef iref) {
422 return DecodeIndex(reinterpret_cast<uintptr_t>(iref));
423 }
424
425 IndirectRef ToIndirectRef(uint32_t table_index) const {
426 DCHECK_LT(table_index, max_entries_);
427 uint32_t serial = table_[table_index].GetSerial();
428 return reinterpret_cast<IndirectRef>(EncodeIndirectRef(table_index, serial));
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700429 }
430
Alex Light3d9c0d92020-09-09 14:16:08 -0700431 // Resize the backing table to be at least new_size elements long. Currently
432 // must be larger than the current size. After return max_entries_ >= new_size.
Andreas Gampee03662b2016-10-13 17:12:56 -0700433 bool Resize(size_t new_size, std::string* error_msg);
434
435 void RecoverHoles(IRTSegmentState from);
436
Andreas Gampef1e86302016-10-03 11:42:31 -0700437 // Abort if check_jni is not enabled. Otherwise, just log as an error.
438 static void AbortIfNoCheckJNI(const std::string& msg);
Mathieu Chartierc56057e2014-05-04 13:18:58 -0700439
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700440 /* extra debugging checks */
Andreas Gampee03662b2016-10-13 17:12:56 -0700441 bool CheckEntry(const char*, IndirectRef, uint32_t) const;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700442
Andreas Gampee03662b2016-10-13 17:12:56 -0700443 /// semi-public - read/write by jni down calls.
Ian Rogersdc51b792011-09-22 20:41:37 -0700444 IRTSegmentState segment_state_;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700445
Hans Boehm4dcac362021-09-23 12:26:04 -0700446 // Mem map where we store the indirect refs. If it's invalid, and table_ is non-null, then
447 // table_ is valid, but was allocated via allocSmallIRT();
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100448 MemMap table_mem_map_;
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -0700449 // bottom of the stack. Do not directly access the object references
450 // in this as they are roots. Use Get() that has a read barrier.
Mathieu Chartier4838d662014-09-25 15:27:43 -0700451 IrtEntry* table_;
Andreas Gampee03662b2016-10-13 17:12:56 -0700452 // bit mask, ORed into all irefs.
Mathieu Chartier4838d662014-09-25 15:27:43 -0700453 const IndirectRefKind kind_;
Andreas Gampee03662b2016-10-13 17:12:56 -0700454
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700455 // max #of entries allowed (modulo resizing).
Andreas Gampee03662b2016-10-13 17:12:56 -0700456 size_t max_entries_;
457
458 // Some values to retain old behavior with holes. Description of the algorithm is in the .cc
459 // file.
460 // TODO: Consider other data structures for compact tables, e.g., free lists.
Hans Boehm592bcea2021-07-02 22:20:52 -0700461 size_t current_num_holes_; // Number of holes in the current / top segment.
Andreas Gampee03662b2016-10-13 17:12:56 -0700462 IRTSegmentState last_known_previous_state_;
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700463
464 // Whether the table's capacity may be resized. As there are no locks used, it is the caller's
465 // responsibility to ensure thread-safety.
466 ResizableCapacity resizable_;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700467};
468
469} // namespace art
470
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700471#endif // ART_RUNTIME_INDIRECT_REFERENCE_TABLE_H_