• 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_ALLOC_INL_H_
18 #define ART_RUNTIME_MIRROR_ARRAY_ALLOC_INL_H_
19 
20 #include "array-inl.h"
21 
22 #include <android-base/logging.h>
23 #include <android-base/stringprintf.h>
24 
25 #include "base/bit_utils.h"
26 #include "base/casts.h"
27 #include "class.h"
28 #include "gc/heap-inl.h"
29 #include "obj_ptr-inl.h"
30 #include "runtime.h"
31 
32 namespace art {
33 namespace mirror {
34 
ComputeArraySize(int32_t component_count,size_t component_size_shift)35 static inline size_t ComputeArraySize(int32_t component_count, size_t component_size_shift) {
36   DCHECK_GE(component_count, 0);
37 
38   size_t component_size = 1U << component_size_shift;
39   size_t header_size = Array::DataOffset(component_size).SizeValue();
40   size_t data_size = static_cast<size_t>(component_count) << component_size_shift;
41   size_t size = header_size + data_size;
42 
43   // Check for size_t overflow if this was an unreasonable request
44   // but let the caller throw OutOfMemoryError.
45 #ifdef __LP64__
46   // 64-bit. No overflow as component_count is 32-bit and the maximum
47   // component size is 8.
48   DCHECK_LE((1U << component_size_shift), 8U);
49 #else
50   // 32-bit.
51   DCHECK_NE(header_size, 0U);
52   DCHECK_EQ(RoundUp(header_size, component_size), header_size);
53   // The array length limit (exclusive).
54   const size_t length_limit = (0U - header_size) >> component_size_shift;
55   if (UNLIKELY(length_limit <= static_cast<size_t>(component_count))) {
56     return 0;  // failure
57   }
58 #endif
59   return size;
60 }
61 
62 // Used for setting the array length in the allocation code path to ensure it is guarded by a
63 // StoreStore fence.
64 class SetLengthVisitor {
65  public:
SetLengthVisitor(int32_t length)66   explicit SetLengthVisitor(int32_t length) : length_(length) {
67   }
68 
operator()69   void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
70       REQUIRES_SHARED(Locks::mutator_lock_) {
71     // Avoid AsArray as object is not yet in live bitmap or allocation stack.
72     ObjPtr<Array> array = ObjPtr<Array>::DownCast(obj);
73     // DCHECK(array->IsArrayInstance());
74     array->SetLength(length_);
75   }
76 
77  private:
78   const int32_t length_;
79 
80   DISALLOW_COPY_AND_ASSIGN(SetLengthVisitor);
81 };
82 
83 // Similar to SetLengthVisitor, used for setting the array length to fill the usable size of an
84 // array.
85 class SetLengthToUsableSizeVisitor {
86  public:
SetLengthToUsableSizeVisitor(int32_t min_length,size_t header_size,size_t component_size_shift)87   SetLengthToUsableSizeVisitor(int32_t min_length, size_t header_size,
88                                size_t component_size_shift) :
89       minimum_length_(min_length), header_size_(header_size),
90       component_size_shift_(component_size_shift) {
91   }
92 
operator()93   void operator()(ObjPtr<Object> obj, size_t usable_size) const
94       REQUIRES_SHARED(Locks::mutator_lock_) {
95     // Avoid AsArray as object is not yet in live bitmap or allocation stack.
96     ObjPtr<Array> array = ObjPtr<Array>::DownCast(obj);
97     // DCHECK(array->IsArrayInstance());
98     int32_t length = (usable_size - header_size_) >> component_size_shift_;
99     DCHECK_GE(length, minimum_length_);
100     uint8_t* old_end = reinterpret_cast<uint8_t*>(array->GetRawData(1U << component_size_shift_,
101                                                                     minimum_length_));
102     uint8_t* new_end = reinterpret_cast<uint8_t*>(array->GetRawData(1U << component_size_shift_,
103                                                                     length));
104     // Ensure space beyond original allocation is zeroed.
105     memset(old_end, 0, new_end - old_end);
106     array->SetLength(length);
107   }
108 
109  private:
110   const int32_t minimum_length_;
111   const size_t header_size_;
112   const size_t component_size_shift_;
113 
114   DISALLOW_COPY_AND_ASSIGN(SetLengthToUsableSizeVisitor);
115 };
116 
117 template <bool kIsInstrumented, bool kFillUsable>
Alloc(Thread * self,ObjPtr<Class> array_class,int32_t component_count,size_t component_size_shift,gc::AllocatorType allocator_type)118 inline ObjPtr<Array> Array::Alloc(Thread* self,
119                                   ObjPtr<Class> array_class,
120                                   int32_t component_count,
121                                   size_t component_size_shift,
122                                   gc::AllocatorType allocator_type) {
123   DCHECK(allocator_type != gc::kAllocatorTypeLOS);
124   DCHECK(array_class != nullptr);
125   DCHECK(array_class->IsArrayClass());
126   DCHECK_EQ(array_class->GetComponentSizeShift(), component_size_shift);
127   DCHECK_EQ(array_class->GetComponentSize(), (1U << component_size_shift));
128   size_t size = ComputeArraySize(component_count, component_size_shift);
129 #ifdef __LP64__
130   // 64-bit. No size_t overflow.
131   DCHECK_NE(size, 0U);
132 #else
133   // 32-bit.
134   if (UNLIKELY(size == 0)) {
135     self->ThrowOutOfMemoryError(android::base::StringPrintf("%s of length %d would overflow",
136                                                             array_class->PrettyDescriptor().c_str(),
137                                                             component_count).c_str());
138     return nullptr;
139   }
140 #endif
141   gc::Heap* heap = Runtime::Current()->GetHeap();
142   ObjPtr<Array> result;
143   if (!kFillUsable) {
144     SetLengthVisitor visitor(component_count);
145     result = ObjPtr<Array>::DownCast(
146         heap->AllocObjectWithAllocator<kIsInstrumented, true>(
147             self, array_class, size, allocator_type, visitor));
148   } else {
149     SetLengthToUsableSizeVisitor visitor(component_count,
150                                          DataOffset(1U << component_size_shift).SizeValue(),
151                                          component_size_shift);
152     result = ObjPtr<Array>::DownCast(
153         heap->AllocObjectWithAllocator<kIsInstrumented, true>(
154             self, array_class, size, allocator_type, visitor));
155   }
156   if (kIsDebugBuild && result != nullptr && Runtime::Current()->IsStarted()) {
157     array_class = result->GetClass();  // In case the array class moved.
158     CHECK_EQ(array_class->GetComponentSize(), 1U << component_size_shift);
159     if (!kFillUsable) {
160       CHECK_EQ(result->SizeOf(), size);
161     } else {
162       CHECK_GE(result->SizeOf(), size);
163     }
164   }
165   return result;
166 }
167 
168 template<typename T>
AllocateAndFill(Thread * self,const T * data,size_t length)169 inline ObjPtr<PrimitiveArray<T>> PrimitiveArray<T>::AllocateAndFill(Thread* self,
170                                                                    const T* data,
171                                                                    size_t length) {
172   StackHandleScope<1> hs(self);
173   Handle<PrimitiveArray<T>> arr(hs.NewHandle(PrimitiveArray<T>::Alloc(self, length)));
174   if (!arr.IsNull()) {
175     // Copy it in. Just skip if it's null
176     memcpy(arr->GetData(), data, sizeof(T) * length);
177   }
178   return arr.Get();
179 }
180 
181 }  // namespace mirror
182 }  // namespace art
183 
184 #endif  // ART_RUNTIME_MIRROR_ARRAY_ALLOC_INL_H_
185