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