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_OBJECT_ARRAY_INL_H_
18 #define ART_RUNTIME_MIRROR_OBJECT_ARRAY_INL_H_
19 
20 #include <string>
21 
22 #include "object_array.h"
23 
24 #include "array-inl.h"
25 #include "base/stringprintf.h"
26 #include "gc/heap.h"
27 #include "mirror/class.h"
28 #include "runtime.h"
29 #include "handle_scope-inl.h"
30 #include "thread.h"
31 #include "utils.h"
32 
33 namespace art {
34 namespace mirror {
35 
36 template<class T>
Alloc(Thread * self,Class * object_array_class,int32_t length,gc::AllocatorType allocator_type)37 inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self, Class* object_array_class,
38                                              int32_t length, gc::AllocatorType allocator_type) {
39   Array* array = Array::Alloc<true>(self, object_array_class, length,
40                                     ComponentSizeShiftWidth(sizeof(HeapReference<Object>)),
41                                     allocator_type);
42   if (UNLIKELY(array == nullptr)) {
43     return nullptr;
44   } else {
45     DCHECK_EQ(array->GetClass()->GetComponentSizeShift(),
46               ComponentSizeShiftWidth(sizeof(HeapReference<Object>)));
47     return array->AsObjectArray<T>();
48   }
49 }
50 
51 template<class T>
Alloc(Thread * self,Class * object_array_class,int32_t length)52 inline ObjectArray<T>* ObjectArray<T>::Alloc(Thread* self, Class* object_array_class,
53                                              int32_t length) {
54   return Alloc(self, object_array_class, length,
55                Runtime::Current()->GetHeap()->GetCurrentAllocator());
56 }
57 
58 template<class T> template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
Get(int32_t i)59 inline T* ObjectArray<T>::Get(int32_t i) {
60   if (!CheckIsValidIndex(i)) {
61     DCHECK(Thread::Current()->IsExceptionPending());
62     return nullptr;
63   }
64   return GetFieldObject<T, kVerifyFlags, kReadBarrierOption>(OffsetOfElement(i));
65 }
66 
67 template<class T> template<VerifyObjectFlags kVerifyFlags>
CheckAssignable(T * object)68 inline bool ObjectArray<T>::CheckAssignable(T* object) {
69   if (object != nullptr) {
70     Class* element_class = GetClass<kVerifyFlags>()->GetComponentType();
71     if (UNLIKELY(!object->InstanceOf(element_class))) {
72       ThrowArrayStoreException(object);
73       return false;
74     }
75   }
76   return true;
77 }
78 
79 template<class T>
Set(int32_t i,T * object)80 inline void ObjectArray<T>::Set(int32_t i, T* object) {
81   if (Runtime::Current()->IsActiveTransaction()) {
82     Set<true>(i, object);
83   } else {
84     Set<false>(i, object);
85   }
86 }
87 
88 template<class T>
89 template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
Set(int32_t i,T * object)90 inline void ObjectArray<T>::Set(int32_t i, T* object) {
91   if (CheckIsValidIndex(i) && CheckAssignable<kVerifyFlags>(object)) {
92     SetFieldObject<kTransactionActive, kCheckTransaction, kVerifyFlags>(OffsetOfElement(i), object);
93   } else {
94     DCHECK(Thread::Current()->IsExceptionPending());
95   }
96 }
97 
98 template<class T>
99 template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
SetWithoutChecks(int32_t i,T * object)100 inline void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
101   DCHECK(CheckIsValidIndex<kVerifyFlags>(i));
102   DCHECK(CheckAssignable<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>(object));
103   SetFieldObject<kTransactionActive, kCheckTransaction, kVerifyFlags>(OffsetOfElement(i), object);
104 }
105 
106 template<class T>
107 template<bool kTransactionActive, bool kCheckTransaction, VerifyObjectFlags kVerifyFlags>
SetWithoutChecksAndWriteBarrier(int32_t i,T * object)108 inline void ObjectArray<T>::SetWithoutChecksAndWriteBarrier(int32_t i, T* object) {
109   DCHECK(CheckIsValidIndex<kVerifyFlags>(i));
110   // TODO:  enable this check. It fails when writing the image in ImageWriter::FixupObjectArray.
111   // DCHECK(CheckAssignable(object));
112   SetFieldObjectWithoutWriteBarrier<kTransactionActive, kCheckTransaction, kVerifyFlags>(
113       OffsetOfElement(i), object);
114 }
115 
116 template<class T>
GetWithoutChecks(int32_t i)117 inline T* ObjectArray<T>::GetWithoutChecks(int32_t i) {
118   DCHECK(CheckIsValidIndex(i));
119   return GetFieldObject<T>(OffsetOfElement(i));
120 }
121 
122 template<class T>
AssignableMemmove(int32_t dst_pos,ObjectArray<T> * src,int32_t src_pos,int32_t count)123 inline void ObjectArray<T>::AssignableMemmove(int32_t dst_pos, ObjectArray<T>* src,
124                                               int32_t src_pos, int32_t count) {
125   if (kIsDebugBuild) {
126     for (int i = 0; i < count; ++i) {
127       // The get will perform the VerifyObject.
128       src->GetWithoutChecks(src_pos + i);
129     }
130   }
131   // Perform the memmove using int memmove then perform the write barrier.
132   static_assert(sizeof(HeapReference<T>) == sizeof(uint32_t),
133                 "art::mirror::HeapReference<T> and uint32_t have different sizes.");
134   IntArray* dstAsIntArray = reinterpret_cast<IntArray*>(this);
135   IntArray* srcAsIntArray = reinterpret_cast<IntArray*>(src);
136   if (kUseReadBarrier) {
137     // TODO: Optimize this later?
138     const bool copy_forward = (src != this) || (dst_pos < src_pos) || (dst_pos - src_pos >= count);
139     if (copy_forward) {
140       // Forward copy.
141       for (int i = 0; i < count; ++i) {
142         // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
143         Object* obj = src->GetWithoutChecks(src_pos + i);
144         SetWithoutChecks<false>(dst_pos + i, obj);
145       }
146     } else {
147       // Backward copy.
148       for (int i = count - 1; i >= 0; --i) {
149         // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
150         Object* obj = src->GetWithoutChecks(src_pos + i);
151         SetWithoutChecks<false>(dst_pos + i, obj);
152       }
153     }
154   } else {
155     dstAsIntArray->Memmove(dst_pos, srcAsIntArray, src_pos, count);
156   }
157   Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
158   if (kIsDebugBuild) {
159     for (int i = 0; i < count; ++i) {
160       // The get will perform the VerifyObject.
161       GetWithoutChecks(dst_pos + i);
162     }
163   }
164 }
165 
166 template<class T>
AssignableMemcpy(int32_t dst_pos,ObjectArray<T> * src,int32_t src_pos,int32_t count)167 inline void ObjectArray<T>::AssignableMemcpy(int32_t dst_pos, ObjectArray<T>* src,
168                                              int32_t src_pos, int32_t count) {
169   if (kIsDebugBuild) {
170     for (int i = 0; i < count; ++i) {
171       // The get will perform the VerifyObject.
172       src->GetWithoutChecks(src_pos + i);
173     }
174   }
175   // Perform the memmove using int memcpy then perform the write barrier.
176   static_assert(sizeof(HeapReference<T>) == sizeof(uint32_t),
177                 "art::mirror::HeapReference<T> and uint32_t have different sizes.");
178   IntArray* dstAsIntArray = reinterpret_cast<IntArray*>(this);
179   IntArray* srcAsIntArray = reinterpret_cast<IntArray*>(src);
180   if (kUseReadBarrier) {
181     // TODO: Optimize this later?
182     for (int i = 0; i < count; ++i) {
183       // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
184       T* obj = src->GetWithoutChecks(src_pos + i);
185       SetWithoutChecks<false>(dst_pos + i, obj);
186     }
187   } else {
188     dstAsIntArray->Memcpy(dst_pos, srcAsIntArray, src_pos, count);
189   }
190   Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
191   if (kIsDebugBuild) {
192     for (int i = 0; i < count; ++i) {
193       // The get will perform the VerifyObject.
194       GetWithoutChecks(dst_pos + i);
195     }
196   }
197 }
198 
199 template<class T>
200 template<bool kTransactionActive>
AssignableCheckingMemcpy(int32_t dst_pos,ObjectArray<T> * src,int32_t src_pos,int32_t count,bool throw_exception)201 inline void ObjectArray<T>::AssignableCheckingMemcpy(int32_t dst_pos, ObjectArray<T>* src,
202                                                      int32_t src_pos, int32_t count,
203                                                      bool throw_exception) {
204   DCHECK_NE(this, src)
205       << "This case should be handled with memmove that handles overlaps correctly";
206   // We want to avoid redundant IsAssignableFrom checks where possible, so we cache a class that
207   // we know is assignable to the destination array's component type.
208   Class* dst_class = GetClass()->GetComponentType();
209   Class* lastAssignableElementClass = dst_class;
210 
211   Object* o = nullptr;
212   int i = 0;
213   for (; i < count; ++i) {
214     // The follow get operations force the objects to be verified.
215     // We need a RB here. ObjectArray::GetWithoutChecks() contains a RB.
216     o = src->GetWithoutChecks(src_pos + i);
217     if (o == nullptr) {
218       // Null is always assignable.
219       SetWithoutChecks<kTransactionActive>(dst_pos + i, nullptr);
220     } else {
221       // TODO: use the underlying class reference to avoid uncompression when not necessary.
222       Class* o_class = o->GetClass();
223       if (LIKELY(lastAssignableElementClass == o_class)) {
224         SetWithoutChecks<kTransactionActive>(dst_pos + i, o);
225       } else if (LIKELY(dst_class->IsAssignableFrom(o_class))) {
226         lastAssignableElementClass = o_class;
227         SetWithoutChecks<kTransactionActive>(dst_pos + i, o);
228       } else {
229         // Can't put this element into the array, break to perform write-barrier and throw
230         // exception.
231         break;
232       }
233     }
234   }
235   Runtime::Current()->GetHeap()->WriteBarrierArray(this, dst_pos, count);
236   if (UNLIKELY(i != count)) {
237     std::string actualSrcType(PrettyTypeOf(o));
238     std::string dstType(PrettyTypeOf(this));
239     Thread* self = Thread::Current();
240     if (throw_exception) {
241       self->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
242                                "source[%d] of type %s cannot be stored in destination array of type %s",
243                                src_pos + i, actualSrcType.c_str(), dstType.c_str());
244     } else {
245       LOG(FATAL) << StringPrintf("source[%d] of type %s cannot be stored in destination array of type %s",
246                                  src_pos + i, actualSrcType.c_str(), dstType.c_str());
247     }
248   }
249 }
250 
251 template<class T>
CopyOf(Thread * self,int32_t new_length)252 inline ObjectArray<T>* ObjectArray<T>::CopyOf(Thread* self, int32_t new_length) {
253   DCHECK_GE(new_length, 0);
254   // We may get copied by a compacting GC.
255   StackHandleScope<1> hs(self);
256   Handle<ObjectArray<T>> h_this(hs.NewHandle(this));
257   gc::Heap* heap = Runtime::Current()->GetHeap();
258   gc::AllocatorType allocator_type = heap->IsMovableObject(this) ? heap->GetCurrentAllocator() :
259       heap->GetCurrentNonMovingAllocator();
260   ObjectArray<T>* new_array = Alloc(self, GetClass(), new_length, allocator_type);
261   if (LIKELY(new_array != nullptr)) {
262     new_array->AssignableMemcpy(0, h_this.Get(), 0, std::min(h_this->GetLength(), new_length));
263   }
264   return new_array;
265 }
266 
267 template<class T>
OffsetOfElement(int32_t i)268 inline MemberOffset ObjectArray<T>::OffsetOfElement(int32_t i) {
269   return MemberOffset(DataOffset(sizeof(HeapReference<Object>)).Int32Value() +
270                       (i * sizeof(HeapReference<Object>)));
271 }
272 
273 template<class T> template<typename Visitor>
VisitReferences(const Visitor & visitor)274 inline void ObjectArray<T>::VisitReferences(const Visitor& visitor) {
275   const size_t length = static_cast<size_t>(GetLength());
276   for (size_t i = 0; i < length; ++i) {
277     visitor(this, OffsetOfElement(i), false);
278   }
279 }
280 
281 }  // namespace mirror
282 }  // namespace art
283 
284 #endif  // ART_RUNTIME_MIRROR_OBJECT_ARRAY_INL_H_
285