• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 #ifndef ART_RUNTIME_INDIRECT_REFERENCE_TABLE_H_
18 #define ART_RUNTIME_INDIRECT_REFERENCE_TABLE_H_
19 
20 #include <stdint.h>
21 
22 #include <iosfwd>
23 #include <limits>
24 #include <string>
25 
26 #include <android-base/logging.h>
27 
28 #include "base/bit_utils.h"
29 #include "base/locks.h"
30 #include "base/macros.h"
31 #include "base/mem_map.h"
32 #include "base/mutex.h"
33 #include "gc_root.h"
34 #include "obj_ptr.h"
35 #include "offsets.h"
36 #include "read_barrier_option.h"
37 
38 namespace art {
39 
40 class RootInfo;
41 
42 namespace mirror {
43 class Object;
44 }  // namespace mirror
45 
46 // 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.
79 
80 // 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.
89 using IndirectRef = void*;
90 
91 // Indirect reference kind, used as the two low bits of IndirectRef.
92 //
93 // For convenience these match up with enum jobjectRefType from jni.h.
94 enum IndirectRefKind {
95   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
100 };
101 std::ostream& operator<<(std::ostream& os, IndirectRefKind rhs);
102 const char* GetIndirectRefKindString(const IndirectRefKind& kind);
103 
104 // 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.
143 
144 // The state of the current segment. We only store the index. Splitting it for index and hole
145 // count restricts the range too much.
146 struct IRTSegmentState {
147   uint32_t top_index;
148 };
149 
150 // Use as initial value for "cookie", and when table has only one segment.
151 static constexpr IRTSegmentState kIRTFirstSegment = { 0 };
152 
153 // We associate a few bits of serial number with each reference, for error checking.
154 static constexpr unsigned int kIRTSerialBits = 3;
155 static constexpr uint32_t kIRTMaxSerial = ((1 << kIRTSerialBits) - 1);
156 
157 class IrtEntry {
158  public:
159   void Add(ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_);
160 
GetReference()161   GcRoot<mirror::Object>* GetReference() {
162     DCHECK_LE(serial_, kIRTMaxSerial);
163     return &reference_;
164   }
165 
GetReference()166   const GcRoot<mirror::Object>* GetReference() const {
167     DCHECK_LE(serial_, kIRTMaxSerial);
168     return &reference_;
169   }
170 
GetSerial()171   uint32_t GetSerial() const {
172     return serial_;
173   }
174 
175   void SetReference(ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_);
176 
177  private:
178   uint32_t serial_;  // Incremented for each reuse; checked against reference.
179   GcRoot<mirror::Object> reference_;
180 };
181 static_assert(sizeof(IrtEntry) == 2 * sizeof(uint32_t), "Unexpected sizeof(IrtEntry)");
182 static_assert(IsPowerOfTwo(sizeof(IrtEntry)), "Unexpected sizeof(IrtEntry)");
183 
184 class IrtIterator {
185  public:
IrtIterator(IrtEntry * table,size_t i,size_t capacity)186   IrtIterator(IrtEntry* table, size_t i, size_t capacity) REQUIRES_SHARED(Locks::mutator_lock_)
187       : table_(table), i_(i), capacity_(capacity) {
188     // capacity_ is used in some target; has warning with unused attribute.
189     UNUSED(capacity_);
190   }
191 
192   IrtIterator& operator++() REQUIRES_SHARED(Locks::mutator_lock_) {
193     ++i_;
194     return *this;
195   }
196 
REQUIRES_SHARED(Locks::mutator_lock_)197   GcRoot<mirror::Object>* operator*() REQUIRES_SHARED(Locks::mutator_lock_) {
198     // This does not have a read barrier as this is used to visit roots.
199     return table_[i_].GetReference();
200   }
201 
equals(const IrtIterator & rhs)202   bool equals(const IrtIterator& rhs) const {
203     return (i_ == rhs.i_ && table_ == rhs.table_);
204   }
205 
206  private:
207   IrtEntry* const table_;
208   size_t i_;
209   const size_t capacity_;
210 };
211 
212 bool inline operator==(const IrtIterator& lhs, const IrtIterator& rhs) {
213   return lhs.equals(rhs);
214 }
215 
216 bool inline operator!=(const IrtIterator& lhs, const IrtIterator& rhs) {
217   return !lhs.equals(rhs);
218 }
219 
220 // 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.
227 constexpr size_t kInitialIrtBytes = 512;  // Number of bytes in an initial local table.
228 constexpr size_t kSmallIrtEntries = kInitialIrtBytes / sizeof(IrtEntry);
229 static_assert(kPageSize % kInitialIrtBytes == 0);
230 static_assert(kInitialIrtBytes % sizeof(IrtEntry) == 0);
231 static_assert(kInitialIrtBytes % sizeof(void *) == 0);
232 
233 // A minimal stopgap allocator for initial small local IRT tables.
234 class 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 
250   Mutex lock_;  // Level kGenericBottomLock; acquired before mem_map_lock_, which is a C++ mutex.
251 };
252 
253 class IndirectReferenceTable {
254  public:
255   enum class ResizableCapacity {
256     kNo,
257     kYes
258   };
259 
260   // 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.
265   // 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.
267   IndirectReferenceTable(size_t max_count,
268                          IndirectRefKind kind,
269                          ResizableCapacity resizable,
270                          std::string* error_msg);
271 
272   ~IndirectReferenceTable();
273 
274   /*
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    */
281   bool IsValid() const;
282 
283   // Add a new entry. "obj" must be a valid non-null object reference. This function will
284   // 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)
288       REQUIRES_SHARED(Locks::mutator_lock_);
289 
290   // Given an IndirectRef in the table, return the Object it refers to.
291   //
292   // This function may abort under error conditions.
293   template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
294   ObjPtr<mirror::Object> Get(IndirectRef iref) const REQUIRES_SHARED(Locks::mutator_lock_)
295       ALWAYS_INLINE;
296 
297   // Synchronized get which reads a reference, acquiring a lock if necessary.
298   template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
SynchronizedGet(IndirectRef iref)299   ObjPtr<mirror::Object> SynchronizedGet(IndirectRef iref) const
300       REQUIRES_SHARED(Locks::mutator_lock_) {
301     return Get<kReadBarrierOption>(iref);
302   }
303 
304   // Updates an existing indirect reference to point to a new object.
305   void Update(IndirectRef iref, ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_);
306 
307   // 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);
315 
316   void AssertEmpty() REQUIRES_SHARED(Locks::mutator_lock_);
317 
318   void Dump(std::ostream& os) const
319       REQUIRES_SHARED(Locks::mutator_lock_)
320       REQUIRES(!Locks::alloc_tracker_lock_);
321 
GetKind()322   IndirectRefKind GetKind() const {
323     return kind_;
324   }
325 
326   // Return the #of entries in the entire table.  This includes holes, and
327   // so may be larger than the actual number of "live" entries.
Capacity()328   size_t Capacity() const {
329     return segment_state_.top_index;
330   }
331 
332   // Return the number of non-null entries in the table. Only reliable for a
333   // single segment table.
NEntriesForGlobal()334   int32_t NEntriesForGlobal() {
335     return segment_state_.top_index - current_num_holes_;
336   }
337 
338   // Ensure that at least free_capacity elements are available, or return false.
339   // Caller ensures free_capacity > 0.
340   bool EnsureFreeCapacity(size_t free_capacity, std::string* error_msg)
341       REQUIRES_SHARED(Locks::mutator_lock_);
342   // See implementation of EnsureFreeCapacity. We'll only state here how much is trivially free,
343   // without recovering holes. Thus this is a conservative estimate.
344   size_t FreeCapacity() const;
345 
346   // Note IrtIterator does not have a read barrier as it's used to visit roots.
begin()347   IrtIterator begin() {
348     return IrtIterator(table_, 0, Capacity());
349   }
350 
end()351   IrtIterator end() {
352     return IrtIterator(table_, Capacity(), Capacity());
353   }
354 
355   void VisitRoots(RootVisitor* visitor, const RootInfo& root_info)
356       REQUIRES_SHARED(Locks::mutator_lock_);
357 
GetSegmentState()358   IRTSegmentState GetSegmentState() const {
359     return segment_state_;
360   }
361 
362   void SetSegmentState(IRTSegmentState new_state);
363 
SegmentStateOffset(size_t pointer_size ATTRIBUTE_UNUSED)364   static Offset SegmentStateOffset(size_t pointer_size ATTRIBUTE_UNUSED) {
365     // Note: Currently segment_state_ is at offset 0. We're testing the expected value in
366     //       jni_internal_test to make sure it stays correct. It is not OFFSETOF_MEMBER, as that
367     //       is not pointer-size-safe.
368     return Offset(0);
369   }
370 
371   // Release pages past the end of the table that may have previously held references.
372   void Trim() REQUIRES_SHARED(Locks::mutator_lock_);
373 
374   // Determine what kind of indirect reference this is. Opposite of EncodeIndirectRefKind.
GetIndirectRefKind(IndirectRef iref)375   ALWAYS_INLINE static inline IndirectRefKind GetIndirectRefKind(IndirectRef iref) {
376     return DecodeIndirectRefKind(reinterpret_cast<uintptr_t>(iref));
377   }
378 
379   /* Reference validation for CheckJNI. */
380   bool IsValidReference(IndirectRef, /*out*/std::string* error_msg) const
381       REQUIRES_SHARED(Locks::mutator_lock_);
382 
383  private:
384   static constexpr uint32_t kShiftedSerialMask = (1u << kIRTSerialBits) - 1;
385 
386   static constexpr size_t kKindBits = MinimumBitsToStore(
387       static_cast<uint32_t>(IndirectRefKind::kLastKind));
388   static constexpr uint32_t kKindMask = (1u << kKindBits) - 1;
389 
EncodeIndex(uint32_t table_index)390   static constexpr uintptr_t EncodeIndex(uint32_t table_index) {
391     static_assert(sizeof(IndirectRef) == sizeof(uintptr_t), "Unexpected IndirectRef size");
392     DCHECK_LE(MinimumBitsToStore(table_index), BitSizeOf<uintptr_t>() - kIRTSerialBits - kKindBits);
393     return (static_cast<uintptr_t>(table_index) << kKindBits << kIRTSerialBits);
394   }
DecodeIndex(uintptr_t uref)395   static constexpr uint32_t DecodeIndex(uintptr_t uref) {
396     return static_cast<uint32_t>((uref >> kKindBits) >> kIRTSerialBits);
397   }
398 
EncodeIndirectRefKind(IndirectRefKind kind)399   static constexpr uintptr_t EncodeIndirectRefKind(IndirectRefKind kind) {
400     return static_cast<uintptr_t>(kind);
401   }
DecodeIndirectRefKind(uintptr_t uref)402   static constexpr IndirectRefKind DecodeIndirectRefKind(uintptr_t uref) {
403     return static_cast<IndirectRefKind>(uref & kKindMask);
404   }
405 
EncodeSerial(uint32_t serial)406   static constexpr uintptr_t EncodeSerial(uint32_t serial) {
407     DCHECK_LE(MinimumBitsToStore(serial), kIRTSerialBits);
408     return serial << kKindBits;
409   }
DecodeSerial(uintptr_t uref)410   static constexpr uint32_t DecodeSerial(uintptr_t uref) {
411     return static_cast<uint32_t>(uref >> kKindBits) & kShiftedSerialMask;
412   }
413 
EncodeIndirectRef(uint32_t table_index,uint32_t serial)414   constexpr uintptr_t EncodeIndirectRef(uint32_t table_index, uint32_t serial) const {
415     DCHECK_LT(table_index, max_entries_);
416     return EncodeIndex(table_index) | EncodeSerial(serial) | EncodeIndirectRefKind(kind_);
417   }
418 
419   static void ConstexprChecks();
420 
421   // Extract the table index from an indirect reference.
ExtractIndex(IndirectRef iref)422   ALWAYS_INLINE static uint32_t ExtractIndex(IndirectRef iref) {
423     return DecodeIndex(reinterpret_cast<uintptr_t>(iref));
424   }
425 
ToIndirectRef(uint32_t table_index)426   IndirectRef ToIndirectRef(uint32_t table_index) const {
427     DCHECK_LT(table_index, max_entries_);
428     uint32_t serial = table_[table_index].GetSerial();
429     return reinterpret_cast<IndirectRef>(EncodeIndirectRef(table_index, serial));
430   }
431 
432   // Resize the backing table to be at least new_size elements long. Currently
433   // must be larger than the current size. After return max_entries_ >= new_size.
434   bool Resize(size_t new_size, std::string* error_msg);
435 
436   void RecoverHoles(IRTSegmentState from);
437 
438   // Abort if check_jni is not enabled. Otherwise, just log as an error.
439   static void AbortIfNoCheckJNI(const std::string& msg);
440 
441   /* extra debugging checks */
442   bool CheckEntry(const char*, IndirectRef, uint32_t) const;
443 
444   /// semi-public - read/write by jni down calls.
445   IRTSegmentState segment_state_;
446 
447   // Mem map where we store the indirect refs. If it's invalid, and table_ is non-null, then
448   // table_ is valid, but was allocated via allocSmallIRT();
449   MemMap table_mem_map_;
450   // bottom of the stack. Do not directly access the object references
451   // in this as they are roots. Use Get() that has a read barrier.
452   IrtEntry* table_;
453   // bit mask, ORed into all irefs.
454   const IndirectRefKind kind_;
455 
456   // max #of entries allowed (modulo resizing).
457   size_t max_entries_;
458 
459   // Some values to retain old behavior with holes. Description of the algorithm is in the .cc
460   // file.
461   // TODO: Consider other data structures for compact tables, e.g., free lists.
462   size_t current_num_holes_;  // Number of holes in the current / top segment.
463   IRTSegmentState last_known_previous_state_;
464 
465   // Whether the table's capacity may be resized. As there are no locks used, it is the caller's
466   // responsibility to ensure thread-safety.
467   ResizableCapacity resizable_;
468 };
469 
470 }  // namespace art
471 
472 #endif  // ART_RUNTIME_INDIRECT_REFERENCE_TABLE_H_
473