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