• 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 "class_linker.h"
28 #include "obj_ptr-inl.h"
29 #include "runtime.h"
30 #include "thread-current-inl.h"
31 
32 namespace art HIDDEN {
33 namespace mirror {
34 
ClassSize(PointerSize pointer_size)35 inline uint32_t Array::ClassSize(PointerSize pointer_size) {
36   uint32_t vtable_entries = Object::kVTableLength;
37   return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 0, 0, 0, pointer_size);
38 }
39 
40 template <VerifyObjectFlags kVerifyFlags>
SizeOf(size_t component_size_shift)41 inline size_t Array::SizeOf(size_t component_size_shift) {
42   int32_t component_count = GetLength<kVerifyFlags>();
43   // This is safe from overflow because the array was already allocated.
44   size_t header_size = DataOffset(1U << component_size_shift).SizeValue();
45   size_t data_size = component_count << component_size_shift;
46   return header_size + data_size;
47 }
48 
49 template <VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
SizeOf()50 inline size_t Array::SizeOf() {
51   size_t component_size_shift = GetClass<kVerifyFlags, kReadBarrierOption>()
52                                     ->template GetComponentSizeShift<kReadBarrierOption>();
53   // Don't need to check this since we already check this in GetClass.
54   return SizeOf<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>(component_size_shift);
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()->GetClassLinker()->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   static_assert(sizeof(T) == sizeof(uint8_t) || sizeof(T) == sizeof(uint16_t) ||
213                 sizeof(T) == sizeof(uint32_t) || sizeof(T) == sizeof(uint64_t));
214   void* dst_raw = GetRawData(sizeof(T), dst_pos);
215   const void* src_raw = src->GetRawData(sizeof(T), src_pos);
216   if (sizeof(T) == sizeof(uint8_t)) {
217     memcpy(dst_raw, src_raw, count);
218   } else if (sizeof(T) == sizeof(uint32_t)) {
219     // b/392789466 Avoids copy using float registers on aarch64 for better performance.
220     uint32_t* d = reinterpret_cast<uint32_t*>(dst_raw);
221     const uint32_t* s = reinterpret_cast<const uint32_t*>(src_raw);
222     ArrayForwardCopy<uint32_t>(d, s, count);
223   } else {
224     T* d = reinterpret_cast<T*>(dst_raw);
225     const T* s = reinterpret_cast<const T*>(src_raw);
226     ArrayForwardCopy<T>(d, s, count);
227   }
228 }
229 
230 template<typename T, PointerSize kPointerSize, VerifyObjectFlags kVerifyFlags>
GetElementPtrSize(uint32_t idx)231 inline T PointerArray::GetElementPtrSize(uint32_t idx) {
232   if (kPointerSize == PointerSize::k64) {
233     DCHECK(IsLongArray<kVerifyFlags>());
234   } else {
235     DCHECK(IsIntArray<kVerifyFlags>());
236   }
237   return GetElementPtrSizeUnchecked<T, kPointerSize, kVerifyFlags>(idx);
238 }
239 
240 template<typename T, PointerSize kPointerSize, VerifyObjectFlags kVerifyFlags>
GetElementPtrSizeUnchecked(uint32_t idx)241 inline T PointerArray::GetElementPtrSizeUnchecked(uint32_t idx) {
242   // C style casts here since we sometimes have T be a pointer, or sometimes an integer
243   // (for stack traces).
244   using ConversionType = typename std::conditional_t<std::is_pointer_v<T>, uintptr_t, T>;
245   // Note: we cast the array directly when unchecked as this code gets called by
246   // runtime_image, which can pass a 64bit pointer and therefore cannot be held
247   // by an ObjPtr.
248   if (kPointerSize == PointerSize::k64) {
249     uint64_t value =
250         static_cast<uint64_t>(reinterpret_cast<LongArray*>(this)->GetWithoutChecks(idx));
251     return (T) dchecked_integral_cast<ConversionType>(value);
252   } else {
253     uint32_t value =
254         static_cast<uint32_t>(reinterpret_cast<IntArray*>(this)->GetWithoutChecks(idx));
255     return (T) dchecked_integral_cast<ConversionType>(value);
256   }
257 }
258 
259 template<typename T, VerifyObjectFlags kVerifyFlags>
GetElementPtrSize(uint32_t idx,PointerSize ptr_size)260 inline T PointerArray::GetElementPtrSize(uint32_t idx, PointerSize ptr_size) {
261   if (ptr_size == PointerSize::k64) {
262     return GetElementPtrSize<T, PointerSize::k64, kVerifyFlags>(idx);
263   }
264   return GetElementPtrSize<T, PointerSize::k32, kVerifyFlags>(idx);
265 }
266 
267 template<bool kTransactionActive, bool kCheckTransaction, bool kUnchecked>
SetElementPtrSize(uint32_t idx,uint64_t element,PointerSize ptr_size)268 inline void PointerArray::SetElementPtrSize(uint32_t idx, uint64_t element, PointerSize ptr_size) {
269   // Note: we cast the array directly when unchecked as this code gets called by
270   // runtime_image, which can pass a 64bit pointer and therefore cannot be held
271   // by an ObjPtr.
272   if (ptr_size == PointerSize::k64) {
273     (kUnchecked ? reinterpret_cast<LongArray*>(this) : AsLongArray().Ptr())->
274         SetWithoutChecks<kTransactionActive, kCheckTransaction>(idx, element);
275   } else {
276     uint32_t element32 = dchecked_integral_cast<uint32_t>(element);
277     (kUnchecked ? reinterpret_cast<IntArray*>(this) : AsIntArray().Ptr())
278         ->SetWithoutChecks<kTransactionActive, kCheckTransaction>(idx, element32);
279   }
280 }
281 
282 template<bool kTransactionActive, bool kCheckTransaction, bool kUnchecked, typename T>
SetElementPtrSize(uint32_t idx,T * element,PointerSize ptr_size)283 inline void PointerArray::SetElementPtrSize(uint32_t idx, T* element, PointerSize ptr_size) {
284   SetElementPtrSize<kTransactionActive, kCheckTransaction, kUnchecked>(
285       idx, reinterpret_cast<uintptr_t>(element), ptr_size);
286 }
287 
288 template <VerifyObjectFlags kVerifyFlags, typename Visitor>
Fixup(mirror::PointerArray * dest,PointerSize pointer_size,const Visitor & visitor)289 inline void PointerArray::Fixup(mirror::PointerArray* dest,
290                                 PointerSize pointer_size,
291                                 const Visitor& visitor) {
292   for (size_t i = 0, count = GetLength(); i < count; ++i) {
293     void* ptr = GetElementPtrSize<void*, kVerifyFlags>(i, pointer_size);
294     void* new_ptr = visitor(ptr);
295     if (ptr != new_ptr) {
296       dest->SetElementPtrSize</*kActiveTransaction=*/ false,
297                               /*kCheckTransaction=*/ true,
298                               /*kUnchecked=*/ true>(i, new_ptr, pointer_size);
299     }
300   }
301 }
302 
303 template<bool kUnchecked>
Memcpy(int32_t dst_pos,ObjPtr<PointerArray> src,int32_t src_pos,int32_t count,PointerSize ptr_size)304 void PointerArray::Memcpy(int32_t dst_pos,
305                           ObjPtr<PointerArray> src,
306                           int32_t src_pos,
307                           int32_t count,
308                           PointerSize ptr_size) {
309   DCHECK(!Runtime::Current()->IsActiveTransaction());
310   DCHECK(!src.IsNull());
311   if (ptr_size == PointerSize::k64) {
312     ObjPtr<LongArray> l_this = (kUnchecked ? ObjPtr<LongArray>::DownCast(ObjPtr<Object>(this))
313                                            : AsLongArray());
314     ObjPtr<LongArray> l_src = (kUnchecked ? ObjPtr<LongArray>::DownCast(ObjPtr<Object>(src))
315                                           : src->AsLongArray());
316     l_this->Memcpy(dst_pos, l_src, src_pos, count);
317   } else {
318     ObjPtr<IntArray> i_this = (kUnchecked ? ObjPtr<IntArray>::DownCast(ObjPtr<Object>(this))
319                                           : AsIntArray());
320     ObjPtr<IntArray> i_src = (kUnchecked ? ObjPtr<IntArray>::DownCast(ObjPtr<Object>(src.Ptr()))
321                                          : src->AsIntArray());
322     i_this->Memcpy(dst_pos, i_src, src_pos, count);
323   }
324 }
325 
326 }  // namespace mirror
327 }  // namespace art
328 
329 #endif  // ART_RUNTIME_MIRROR_ARRAY_INL_H_
330