1 /*
2 * Copyright (C) 2012 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 #include "entrypoints/quick/quick_alloc_entrypoints.h"
18
19 #include "art_method-inl.h"
20 #include "base/enums.h"
21 #include "base/quasi_atomic.h"
22 #include "callee_save_frame.h"
23 #include "dex/dex_file_types.h"
24 #include "entrypoints/entrypoint_utils-inl.h"
25 #include "mirror/class-inl.h"
26 #include "mirror/object-inl.h"
27 #include "mirror/object_array-inl.h"
28 #include "mirror/string-alloc-inl.h"
29
30 namespace art {
31
32 static constexpr bool kUseTlabFastPath = true;
33
34 template <bool kInitialized,
35 bool kWithChecks,
36 bool kInstrumented,
37 gc::AllocatorType allocator_type>
artAllocObjectFromCode(mirror::Class * klass,Thread * self)38 static ALWAYS_INLINE inline mirror::Object* artAllocObjectFromCode(
39 mirror::Class* klass,
40 Thread* self) REQUIRES_SHARED(Locks::mutator_lock_) {
41 ScopedQuickEntrypointChecks sqec(self);
42 DCHECK(klass != nullptr);
43 if (kUseTlabFastPath &&
44 !kWithChecks &&
45 !kInstrumented &&
46 allocator_type == gc::kAllocatorTypeTLAB) {
47 // The "object size alloc fast path" is set when the class is
48 // visibly initialized, objects are fixed size and non-finalizable.
49 // Otherwise, the value is too large for the size check to succeed.
50 size_t byte_count = klass->GetObjectSizeAllocFastPath();
51 if (LIKELY(byte_count < self->TlabSize())) {
52 static_assert(kObjectAlignment == gc::space::BumpPointerSpace::kAlignment, "Alignment check");
53 DCHECK_ALIGNED(byte_count, gc::space::BumpPointerSpace::kAlignment);
54 mirror::Object* obj = self->AllocTlab(byte_count);
55 DCHECK(obj != nullptr) << "AllocTlab can't fail";
56 obj->SetClass(klass);
57 if (kUseBakerReadBarrier) {
58 obj->AssertReadBarrierState();
59 }
60 QuasiAtomic::ThreadFenceForConstructor();
61 return obj;
62 }
63 }
64 if (kInitialized) {
65 return AllocObjectFromCodeInitialized<kInstrumented>(klass, self, allocator_type).Ptr();
66 } else if (!kWithChecks) {
67 return AllocObjectFromCodeResolved<kInstrumented>(klass, self, allocator_type).Ptr();
68 } else {
69 return AllocObjectFromCode<kInstrumented>(klass, self, allocator_type).Ptr();
70 }
71 }
72
73 #define GENERATE_ENTRYPOINTS_FOR_ALLOCATOR_INST(suffix, suffix2, instrumented_bool, allocator_type) \
74 extern "C" mirror::Object* artAllocObjectFromCodeWithChecks##suffix##suffix2( \
75 mirror::Class* klass, Thread* self) \
76 REQUIRES_SHARED(Locks::mutator_lock_) { \
77 return artAllocObjectFromCode<false, true, instrumented_bool, allocator_type>(klass, self); \
78 } \
79 extern "C" mirror::Object* artAllocObjectFromCodeResolved##suffix##suffix2( \
80 mirror::Class* klass, Thread* self) \
81 REQUIRES_SHARED(Locks::mutator_lock_) { \
82 return artAllocObjectFromCode<false, false, instrumented_bool, allocator_type>(klass, self); \
83 } \
84 extern "C" mirror::Object* artAllocObjectFromCodeInitialized##suffix##suffix2( \
85 mirror::Class* klass, Thread* self) \
86 REQUIRES_SHARED(Locks::mutator_lock_) { \
87 return artAllocObjectFromCode<true, false, instrumented_bool, allocator_type>(klass, self); \
88 } \
89 extern "C" mirror::String* artAllocStringObject##suffix##suffix2( \
90 mirror::Class* klass, Thread* self) \
91 REQUIRES_SHARED(Locks::mutator_lock_) { \
92 /* The klass arg is so it matches the ABI of the other object alloc callbacks. */ \
93 DCHECK(klass->IsStringClass()) << klass->PrettyClass(); \
94 return mirror::String::AllocEmptyString<instrumented_bool>(self, allocator_type).Ptr(); \
95 } \
96 extern "C" mirror::Array* artAllocArrayFromCodeResolved##suffix##suffix2( \
97 mirror::Class* klass, int32_t component_count, Thread* self) \
98 REQUIRES_SHARED(Locks::mutator_lock_) { \
99 ScopedQuickEntrypointChecks sqec(self); \
100 return AllocArrayFromCodeResolved<instrumented_bool>( \
101 klass, component_count, self, allocator_type).Ptr(); \
102 } \
103 extern "C" mirror::String* artAllocStringFromBytesFromCode##suffix##suffix2( \
104 mirror::ByteArray* byte_array, int32_t high, int32_t offset, int32_t byte_count, \
105 Thread* self) \
106 REQUIRES_SHARED(Locks::mutator_lock_) { \
107 ScopedQuickEntrypointChecks sqec(self); \
108 StackHandleScope<1> hs(self); \
109 Handle<mirror::ByteArray> handle_array(hs.NewHandle(byte_array)); \
110 return mirror::String::AllocFromByteArray<instrumented_bool>( \
111 self, byte_count, handle_array, offset, high, allocator_type).Ptr(); \
112 } \
113 extern "C" mirror::String* artAllocStringFromCharsFromCode##suffix##suffix2( \
114 int32_t offset, int32_t char_count, mirror::CharArray* char_array, Thread* self) \
115 REQUIRES_SHARED(Locks::mutator_lock_) { \
116 StackHandleScope<1> hs(self); \
117 Handle<mirror::CharArray> handle_array(hs.NewHandle(char_array)); \
118 return mirror::String::AllocFromCharArray<instrumented_bool>( \
119 self, char_count, handle_array, offset, allocator_type).Ptr(); \
120 } \
121 extern "C" mirror::String* artAllocStringFromStringFromCode##suffix##suffix2( /* NOLINT */ \
122 mirror::String* string, Thread* self) \
123 REQUIRES_SHARED(Locks::mutator_lock_) { \
124 StackHandleScope<1> hs(self); \
125 Handle<mirror::String> handle_string(hs.NewHandle(string)); \
126 return mirror::String::AllocFromString<instrumented_bool>( \
127 self, handle_string->GetLength(), handle_string, 0, allocator_type).Ptr(); \
128 }
129
130 #define GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(suffix, allocator_type) \
131 GENERATE_ENTRYPOINTS_FOR_ALLOCATOR_INST(suffix, Instrumented, true, allocator_type) \
132 GENERATE_ENTRYPOINTS_FOR_ALLOCATOR_INST(suffix, , false, allocator_type)
133
134 GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(DlMalloc, gc::kAllocatorTypeDlMalloc)
135 GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(RosAlloc, gc::kAllocatorTypeRosAlloc)
136 GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(BumpPointer, gc::kAllocatorTypeBumpPointer)
137 GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(TLAB, gc::kAllocatorTypeTLAB)
138 GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(Region, gc::kAllocatorTypeRegion)
139 GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(RegionTLAB, gc::kAllocatorTypeRegionTLAB)
140
141 #define GENERATE_ENTRYPOINTS(suffix) \
142 extern "C" void* art_quick_alloc_array_resolved##suffix(mirror::Class* klass, int32_t); \
143 extern "C" void* art_quick_alloc_array_resolved8##suffix(mirror::Class* klass, int32_t); \
144 extern "C" void* art_quick_alloc_array_resolved16##suffix(mirror::Class* klass, int32_t); \
145 extern "C" void* art_quick_alloc_array_resolved32##suffix(mirror::Class* klass, int32_t); \
146 extern "C" void* art_quick_alloc_array_resolved64##suffix(mirror::Class* klass, int32_t); \
147 extern "C" void* art_quick_alloc_object_resolved##suffix(mirror::Class* klass); \
148 extern "C" void* art_quick_alloc_object_initialized##suffix(mirror::Class* klass); \
149 extern "C" void* art_quick_alloc_object_with_checks##suffix(mirror::Class* klass); \
150 extern "C" void* art_quick_alloc_string_object##suffix(mirror::Class* klass); \
151 extern "C" void* art_quick_alloc_string_from_bytes##suffix(void*, int32_t, int32_t, int32_t); \
152 extern "C" void* art_quick_alloc_string_from_chars##suffix(int32_t, int32_t, void*); \
153 extern "C" void* art_quick_alloc_string_from_string##suffix(void*); \
154 extern "C" void* art_quick_alloc_array_resolved##suffix##_instrumented(mirror::Class* klass, int32_t); \
155 extern "C" void* art_quick_alloc_array_resolved8##suffix##_instrumented(mirror::Class* klass, int32_t); \
156 extern "C" void* art_quick_alloc_array_resolved16##suffix##_instrumented(mirror::Class* klass, int32_t); \
157 extern "C" void* art_quick_alloc_array_resolved32##suffix##_instrumented(mirror::Class* klass, int32_t); \
158 extern "C" void* art_quick_alloc_array_resolved64##suffix##_instrumented(mirror::Class* klass, int32_t); \
159 extern "C" void* art_quick_alloc_object_resolved##suffix##_instrumented(mirror::Class* klass); \
160 extern "C" void* art_quick_alloc_object_initialized##suffix##_instrumented(mirror::Class* klass); \
161 extern "C" void* art_quick_alloc_object_with_checks##suffix##_instrumented(mirror::Class* klass); \
162 extern "C" void* art_quick_alloc_string_object##suffix##_instrumented(mirror::Class* klass); \
163 extern "C" void* art_quick_alloc_string_from_bytes##suffix##_instrumented(void*, int32_t, int32_t, int32_t); \
164 extern "C" void* art_quick_alloc_string_from_chars##suffix##_instrumented(int32_t, int32_t, void*); \
165 extern "C" void* art_quick_alloc_string_from_string##suffix##_instrumented(void*); \
166 void SetQuickAllocEntryPoints##suffix(QuickEntryPoints* qpoints, bool instrumented) { \
167 if (instrumented) { \
168 qpoints->pAllocArrayResolved = art_quick_alloc_array_resolved##suffix##_instrumented; \
169 qpoints->pAllocArrayResolved8 = art_quick_alloc_array_resolved8##suffix##_instrumented; \
170 qpoints->pAllocArrayResolved16 = art_quick_alloc_array_resolved16##suffix##_instrumented; \
171 qpoints->pAllocArrayResolved32 = art_quick_alloc_array_resolved32##suffix##_instrumented; \
172 qpoints->pAllocArrayResolved64 = art_quick_alloc_array_resolved64##suffix##_instrumented; \
173 qpoints->pAllocObjectResolved = art_quick_alloc_object_resolved##suffix##_instrumented; \
174 qpoints->pAllocObjectInitialized = art_quick_alloc_object_initialized##suffix##_instrumented; \
175 qpoints->pAllocObjectWithChecks = art_quick_alloc_object_with_checks##suffix##_instrumented; \
176 qpoints->pAllocStringObject = art_quick_alloc_string_object##suffix##_instrumented; \
177 qpoints->pAllocStringFromBytes = art_quick_alloc_string_from_bytes##suffix##_instrumented; \
178 qpoints->pAllocStringFromChars = art_quick_alloc_string_from_chars##suffix##_instrumented; \
179 qpoints->pAllocStringFromString = art_quick_alloc_string_from_string##suffix##_instrumented; \
180 } else { \
181 qpoints->pAllocArrayResolved = art_quick_alloc_array_resolved##suffix; \
182 qpoints->pAllocArrayResolved8 = art_quick_alloc_array_resolved8##suffix; \
183 qpoints->pAllocArrayResolved16 = art_quick_alloc_array_resolved16##suffix; \
184 qpoints->pAllocArrayResolved32 = art_quick_alloc_array_resolved32##suffix; \
185 qpoints->pAllocArrayResolved64 = art_quick_alloc_array_resolved64##suffix; \
186 qpoints->pAllocObjectResolved = art_quick_alloc_object_resolved##suffix; \
187 qpoints->pAllocObjectInitialized = art_quick_alloc_object_initialized##suffix; \
188 qpoints->pAllocObjectWithChecks = art_quick_alloc_object_with_checks##suffix; \
189 qpoints->pAllocStringObject = art_quick_alloc_string_object##suffix; \
190 qpoints->pAllocStringFromBytes = art_quick_alloc_string_from_bytes##suffix; \
191 qpoints->pAllocStringFromChars = art_quick_alloc_string_from_chars##suffix; \
192 qpoints->pAllocStringFromString = art_quick_alloc_string_from_string##suffix; \
193 } \
194 }
195
196 // Generate the entrypoint functions.
197 #if !defined(__APPLE__) || !defined(__LP64__)
198 GENERATE_ENTRYPOINTS(_dlmalloc)
199 GENERATE_ENTRYPOINTS(_rosalloc)
200 GENERATE_ENTRYPOINTS(_bump_pointer)
201 GENERATE_ENTRYPOINTS(_tlab)
202 GENERATE_ENTRYPOINTS(_region)
203 GENERATE_ENTRYPOINTS(_region_tlab)
204 #endif
205
206 static bool entry_points_instrumented = false;
207 static gc::AllocatorType entry_points_allocator = gc::kAllocatorTypeDlMalloc;
208
SetQuickAllocEntryPointsAllocator(gc::AllocatorType allocator)209 void SetQuickAllocEntryPointsAllocator(gc::AllocatorType allocator) {
210 entry_points_allocator = allocator;
211 }
212
SetQuickAllocEntryPointsInstrumented(bool instrumented)213 void SetQuickAllocEntryPointsInstrumented(bool instrumented) {
214 entry_points_instrumented = instrumented;
215 }
216
ResetQuickAllocEntryPoints(QuickEntryPoints * qpoints)217 void ResetQuickAllocEntryPoints(QuickEntryPoints* qpoints) {
218 #if !defined(__APPLE__) || !defined(__LP64__)
219 switch (entry_points_allocator) {
220 case gc::kAllocatorTypeDlMalloc: {
221 SetQuickAllocEntryPoints_dlmalloc(qpoints, entry_points_instrumented);
222 return;
223 }
224 case gc::kAllocatorTypeRosAlloc: {
225 SetQuickAllocEntryPoints_rosalloc(qpoints, entry_points_instrumented);
226 return;
227 }
228 case gc::kAllocatorTypeBumpPointer: {
229 CHECK(kMovingCollector);
230 SetQuickAllocEntryPoints_bump_pointer(qpoints, entry_points_instrumented);
231 return;
232 }
233 case gc::kAllocatorTypeTLAB: {
234 CHECK(kMovingCollector);
235 SetQuickAllocEntryPoints_tlab(qpoints, entry_points_instrumented);
236 return;
237 }
238 case gc::kAllocatorTypeRegion: {
239 CHECK(kMovingCollector);
240 SetQuickAllocEntryPoints_region(qpoints, entry_points_instrumented);
241 return;
242 }
243 case gc::kAllocatorTypeRegionTLAB: {
244 CHECK(kMovingCollector);
245 SetQuickAllocEntryPoints_region_tlab(qpoints, entry_points_instrumented);
246 return;
247 }
248 default:
249 break;
250 }
251 #else
252 UNUSED(qpoints);
253 #endif
254 UNIMPLEMENTED(FATAL);
255 UNREACHABLE();
256 }
257
258 } // namespace art
259