• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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_MIRROR_ARRAY_INL_H_
18 #define ART_RUNTIME_MIRROR_ARRAY_INL_H_
19 
20 #include "array.h"
21 
22 #include <android-base/logging.h>
23 
24 #include "base/bit_utils.h"
25 #include "base/casts.h"
26 #include "class.h"
27 #include "obj_ptr-inl.h"
28 #include "runtime.h"
29 #include "thread-current-inl.h"
30 
31 namespace art {
32 namespace mirror {
33 
ClassSize(PointerSize pointer_size)34 inline uint32_t Array::ClassSize(PointerSize pointer_size) {
35   uint32_t vtable_entries = Object::kVTableLength;
36   return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 0, 0, pointer_size);
37 }
38 
39 template <VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption, bool kIsObjArray>
SizeOf()40 inline size_t Array::SizeOf() {
41   // When we are certain that this is a object array, then don't fetch shift
42   // from component_type_ as that doesn't work well with userfaultfd GC as the
43   // component-type class may be allocated at a higher address than the array.
44   size_t component_size_shift = kIsObjArray ?
45                                     Primitive::ComponentSizeShift(Primitive::kPrimNot) :
46                                     GetClass<kVerifyFlags, kReadBarrierOption>()
47                                         ->template GetComponentSizeShift<kReadBarrierOption>();
48   // Don't need to check this since we already check this in GetClass.
49   int32_t component_count =
50       GetLength<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>();
51   // This is safe from overflow because the array was already allocated.
52   size_t header_size = DataOffset(1U << component_size_shift).SizeValue();
53   size_t data_size = component_count << component_size_shift;
54   return header_size + data_size;
55 }
56 
57 template<VerifyObjectFlags kVerifyFlags>
CheckIsValidIndex(int32_t index)58 inline bool Array::CheckIsValidIndex(int32_t index) {
59   if (UNLIKELY(static_cast<uint32_t>(index) >=
60                static_cast<uint32_t>(GetLength<kVerifyFlags>()))) {
61     ThrowArrayIndexOutOfBoundsException(index);
62     return false;
63   }
64   return true;
65 }
66 
67 template<typename T>
Get(int32_t i)68 inline T PrimitiveArray<T>::Get(int32_t i) {
69   if (!CheckIsValidIndex(i)) {
70     DCHECK(Thread::Current()->IsExceptionPending());
71     return T(0);
72   }
73   return GetWithoutChecks(i);
74 }
75 
76 template<typename T>
Set(int32_t i,T value)77 inline void PrimitiveArray<T>::Set(int32_t i, T value) {
78   if (Runtime::Current()->IsActiveTransaction()) {
79     Set<true>(i, value);
80   } else {
81     Set<false>(i, value);
82   }
83 }
84 
85 template<typename T>
86 template<bool kTransactionActive, bool kCheckTransaction>
Set(int32_t i,T value)87 inline void PrimitiveArray<T>::Set(int32_t i, T value) {
88   if (CheckIsValidIndex(i)) {
89     SetWithoutChecks<kTransactionActive, kCheckTransaction>(i, value);
90   } else {
91     DCHECK(Thread::Current()->IsExceptionPending());
92   }
93 }
94 
95 template<typename T>
96 template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
SetWithoutChecks(int32_t i,T value)97 inline void PrimitiveArray<T>::SetWithoutChecks(int32_t i, T value) {
98   if (kCheckTransaction) {
99     DCHECK_EQ(kTransactionActive, Runtime::Current()->IsActiveTransaction());
100   }
101   if (kTransactionActive) {
102     Runtime::Current()->RecordWriteArray(this, i, GetWithoutChecks(i));
103   }
104   DCHECK(CheckIsValidIndex<kVerifyFlags>(i)) << i << " " << GetLength<kVerifyFlags>();
105   GetData()[i] = value;
106 }
107 // Backward copy where elements are of aligned appropriately for T. Count is in T sized units.
108 // Copies are guaranteed not to tear when the sizeof T is less-than 64bit.
109 template<typename T>
ArrayBackwardCopy(T * d,const T * s,int32_t count)110 static inline void ArrayBackwardCopy(T* d, const T* s, int32_t count) {
111   d += count;
112   s += count;
113   for (int32_t i = 0; i < count; ++i) {
114     d--;
115     s--;
116     *d = *s;
117   }
118 }
119 
120 // Forward copy where elements are of aligned appropriately for T. Count is in T sized units.
121 // Copies are guaranteed not to tear when the sizeof T is less-than 64bit.
122 template<typename T>
ArrayForwardCopy(T * d,const T * s,int32_t count)123 static inline void ArrayForwardCopy(T* d, const T* s, int32_t count) {
124   for (int32_t i = 0; i < count; ++i) {
125     *d = *s;
126     d++;
127     s++;
128   }
129 }
130 
131 template<class T>
Memmove(int32_t dst_pos,ObjPtr<PrimitiveArray<T>> src,int32_t src_pos,int32_t count)132 inline void PrimitiveArray<T>::Memmove(int32_t dst_pos,
133                                        ObjPtr<PrimitiveArray<T>> src,
134                                        int32_t src_pos,
135                                        int32_t count) {
136   if (UNLIKELY(count == 0)) {
137     return;
138   }
139   DCHECK_GE(dst_pos, 0);
140   DCHECK_GE(src_pos, 0);
141   DCHECK_GT(count, 0);
142   DCHECK(src != nullptr);
143   DCHECK_LT(dst_pos, GetLength());
144   DCHECK_LE(dst_pos, GetLength() - count);
145   DCHECK_LT(src_pos, src->GetLength());
146   DCHECK_LE(src_pos, src->GetLength() - count);
147 
148   // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
149   // in our implementation, because they may copy byte-by-byte.
150   if (LIKELY(src != this)) {
151     // Memcpy ok for guaranteed non-overlapping distinct arrays.
152     Memcpy(dst_pos, src, src_pos, count);
153   } else {
154     // Handle copies within the same array using the appropriate direction copy.
155     void* dst_raw = GetRawData(sizeof(T), dst_pos);
156     const void* src_raw = src->GetRawData(sizeof(T), src_pos);
157     if (sizeof(T) == sizeof(uint8_t)) {
158       uint8_t* d = reinterpret_cast<uint8_t*>(dst_raw);
159       const uint8_t* s = reinterpret_cast<const uint8_t*>(src_raw);
160       memmove(d, s, count);
161     } else {
162       const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= count);
163       if (sizeof(T) == sizeof(uint16_t)) {
164         uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
165         const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
166         if (copy_forward) {
167           ArrayForwardCopy<uint16_t>(d, s, count);
168         } else {
169           ArrayBackwardCopy<uint16_t>(d, s, count);
170         }
171       } else if (sizeof(T) == sizeof(uint32_t)) {
172         uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
173         const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
174         if (copy_forward) {
175           ArrayForwardCopy<uint32_t>(d, s, count);
176         } else {
177           ArrayBackwardCopy<uint32_t>(d, s, count);
178         }
179       } else {
180         DCHECK_EQ(sizeof(T), sizeof(uint64_t));
181         uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
182         const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
183         if (copy_forward) {
184           ArrayForwardCopy<uint64_t>(d, s, count);
185         } else {
186           ArrayBackwardCopy<uint64_t>(d, s, count);
187         }
188       }
189     }
190   }
191 }
192 
193 template<class T>
Memcpy(int32_t dst_pos,ObjPtr<PrimitiveArray<T>> src,int32_t src_pos,int32_t count)194 inline void PrimitiveArray<T>::Memcpy(int32_t dst_pos,
195                                       ObjPtr<PrimitiveArray<T>> src,
196                                       int32_t src_pos,
197                                       int32_t count) {
198   if (UNLIKELY(count == 0)) {
199     return;
200   }
201   DCHECK_GE(dst_pos, 0);
202   DCHECK_GE(src_pos, 0);
203   DCHECK_GT(count, 0);
204   DCHECK(src != nullptr);
205   DCHECK_LT(dst_pos, GetLength());
206   DCHECK_LE(dst_pos, GetLength() - count);
207   DCHECK_LT(src_pos, src->GetLength());
208   DCHECK_LE(src_pos, src->GetLength() - count);
209 
210   // Note for non-byte copies we can't rely on standard libc functions like memcpy(3) and memmove(3)
211   // in our implementation, because they may copy byte-by-byte.
212   void* dst_raw = GetRawData(sizeof(T), dst_pos);
213   const void* src_raw = src->GetRawData(sizeof(T), src_pos);
214   if (sizeof(T) == sizeof(uint8_t)) {
215     memcpy(dst_raw, src_raw, count);
216   } else if (sizeof(T) == sizeof(uint16_t)) {
217     uint16_t* d = reinterpret_cast<uint16_t*>(dst_raw);
218     const uint16_t* s = reinterpret_cast<const uint16_t*>(src_raw);
219     ArrayForwardCopy<uint16_t>(d, s, count);
220   } else if (sizeof(T) == sizeof(uint32_t)) {
221     uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
222     const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
223     ArrayForwardCopy<uint32_t>(d, s, count);
224   } else {
225     DCHECK_EQ(sizeof(T), sizeof(uint64_t));
226     uint64_t* d = reinterpret_cast<uint64_t*>(dst_raw);
227     const uint64_t* s = reinterpret_cast<const uint64_t*>(src_raw);
228     ArrayForwardCopy<uint64_t>(d, s, count);
229   }
230 }
231 
232 template<typename T, PointerSize kPointerSize, VerifyObjectFlags kVerifyFlags>
GetElementPtrSize(uint32_t idx)233 inline T PointerArray::GetElementPtrSize(uint32_t idx) {
234   if (kPointerSize == PointerSize::k64) {
235     DCHECK(IsLongArray<kVerifyFlags>());
236   } else {
237     DCHECK(IsIntArray<kVerifyFlags>());
238   }
239   return GetElementPtrSizeUnchecked<T, kPointerSize, kVerifyFlags>(idx);
240 }
241 
242 template<typename T, PointerSize kPointerSize, VerifyObjectFlags kVerifyFlags>
GetElementPtrSizeUnchecked(uint32_t idx)243 inline T PointerArray::GetElementPtrSizeUnchecked(uint32_t idx) {
244   // C style casts here since we sometimes have T be a pointer, or sometimes an integer
245   // (for stack traces).
246   using ConversionType = typename std::conditional_t<std::is_pointer_v<T>, uintptr_t, T>;
247   // Note: we cast the array directly when unchecked as this code gets called by
248   // runtime_image, which can pass a 64bit pointer and therefore cannot be held
249   // by an ObjPtr.
250   if (kPointerSize == PointerSize::k64) {
251     uint64_t value =
252         static_cast<uint64_t>(reinterpret_cast<LongArray*>(this)->GetWithoutChecks(idx));
253     return (T) dchecked_integral_cast<ConversionType>(value);
254   } else {
255     uint32_t value =
256         static_cast<uint32_t>(reinterpret_cast<IntArray*>(this)->GetWithoutChecks(idx));
257     return (T) dchecked_integral_cast<ConversionType>(value);
258   }
259 }
260 
261 template<typename T, VerifyObjectFlags kVerifyFlags>
GetElementPtrSize(uint32_t idx,PointerSize ptr_size)262 inline T PointerArray::GetElementPtrSize(uint32_t idx, PointerSize ptr_size) {
263   if (ptr_size == PointerSize::k64) {
264     return GetElementPtrSize<T, PointerSize::k64, kVerifyFlags>(idx);
265   }
266   return GetElementPtrSize<T, PointerSize::k32, kVerifyFlags>(idx);
267 }
268 
269 template<bool kTransactionActive, bool kCheckTransaction, bool kUnchecked>
SetElementPtrSize(uint32_t idx,uint64_t element,PointerSize ptr_size)270 inline void PointerArray::SetElementPtrSize(uint32_t idx, uint64_t element, PointerSize ptr_size) {
271   // Note: we cast the array directly when unchecked as this code gets called by
272   // runtime_image, which can pass a 64bit pointer and therefore cannot be held
273   // by an ObjPtr.
274   if (ptr_size == PointerSize::k64) {
275     (kUnchecked ? reinterpret_cast<LongArray*>(this) : AsLongArray().Ptr())->
276         SetWithoutChecks<kTransactionActive, kCheckTransaction>(idx, element);
277   } else {
278     uint32_t element32 = dchecked_integral_cast<uint32_t>(element);
279     (kUnchecked ? reinterpret_cast<IntArray*>(this) : AsIntArray().Ptr())
280         ->SetWithoutChecks<kTransactionActive, kCheckTransaction>(idx, element32);
281   }
282 }
283 
284 template<bool kTransactionActive, bool kCheckTransaction, bool kUnchecked, typename T>
SetElementPtrSize(uint32_t idx,T * element,PointerSize ptr_size)285 inline void PointerArray::SetElementPtrSize(uint32_t idx, T* element, PointerSize ptr_size) {
286   SetElementPtrSize<kTransactionActive, kCheckTransaction, kUnchecked>(
287       idx, reinterpret_cast<uintptr_t>(element), ptr_size);
288 }
289 
290 template <VerifyObjectFlags kVerifyFlags, typename Visitor>
Fixup(mirror::PointerArray * dest,PointerSize pointer_size,const Visitor & visitor)291 inline void PointerArray::Fixup(mirror::PointerArray* dest,
292                                 PointerSize pointer_size,
293                                 const Visitor& visitor) {
294   for (size_t i = 0, count = GetLength(); i < count; ++i) {
295     void* ptr = GetElementPtrSize<void*, kVerifyFlags>(i, pointer_size);
296     void* new_ptr = visitor(ptr);
297     if (ptr != new_ptr) {
298       dest->SetElementPtrSize</*kActiveTransaction=*/ false,
299                               /*kCheckTransaction=*/ true,
300                               /*kUnchecked=*/ true>(i, new_ptr, pointer_size);
301     }
302   }
303 }
304 
305 template<bool kUnchecked>
Memcpy(int32_t dst_pos,ObjPtr<PointerArray> src,int32_t src_pos,int32_t count,PointerSize ptr_size)306 void PointerArray::Memcpy(int32_t dst_pos,
307                           ObjPtr<PointerArray> src,
308                           int32_t src_pos,
309                           int32_t count,
310                           PointerSize ptr_size) {
311   DCHECK(!Runtime::Current()->IsActiveTransaction());
312   DCHECK(!src.IsNull());
313   if (ptr_size == PointerSize::k64) {
314     ObjPtr<LongArray> l_this = (kUnchecked ? ObjPtr<LongArray>::DownCast(ObjPtr<Object>(this))
315                                            : AsLongArray());
316     ObjPtr<LongArray> l_src = (kUnchecked ? ObjPtr<LongArray>::DownCast(ObjPtr<Object>(src))
317                                           : src->AsLongArray());
318     l_this->Memcpy(dst_pos, l_src, src_pos, count);
319   } else {
320     ObjPtr<IntArray> i_this = (kUnchecked ? ObjPtr<IntArray>::DownCast(ObjPtr<Object>(this))
321                                           : AsIntArray());
322     ObjPtr<IntArray> i_src = (kUnchecked ? ObjPtr<IntArray>::DownCast(ObjPtr<Object>(src.Ptr()))
323                                          : src->AsIntArray());
324     i_this->Memcpy(dst_pos, i_src, src_pos, count);
325   }
326 }
327 
328 }  // namespace mirror
329 }  // namespace art
330 
331 #endif  // ART_RUNTIME_MIRROR_ARRAY_INL_H_
332