blob: c5b4be5982410b2c42e101e799a2f7dd1a84e7af [file] [log] [blame]
Ian Rogers57b86d42012-03-27 16:05:41 -07001/*
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
Mathieu Chartierd8891782014-03-02 13:28:37 -080017#include "entrypoints/quick/quick_alloc_entrypoints.h"
18
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070020#include "base/enums.h"
David Sehrc431b9d2018-03-02 12:01:51 -080021#include "base/quasi_atomic.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070022#include "callee_save_frame.h"
David Sehr9e734c72018-01-04 17:56:19 -080023#include "dex/dex_file_types.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070024#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/class-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070026#include "mirror/object-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070027#include "mirror/object_array-inl.h"
Andreas Gampefd63bbf2018-10-29 12:55:35 -070028#include "mirror/string-alloc-inl.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070029
30namespace art {
31
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -070032static constexpr bool kUseTlabFastPath = true;
33
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000034template <bool kInitialized,
Nicolas Geoffray4ebb99c2021-01-07 15:13:54 +000035 bool kWithChecks,
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000036 bool kInstrumented,
37 gc::AllocatorType allocator_type>
38static 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);
Nicolas Geoffray4ebb99c2021-01-07 15:13:54 +000043 if (kUseTlabFastPath &&
44 !kWithChecks &&
45 !kInstrumented &&
46 allocator_type == gc::kAllocatorTypeTLAB) {
Vladimir Marko8e110652019-07-30 10:14:41 +010047 // 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();
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000059 }
Vladimir Marko8e110652019-07-30 10:14:41 +010060 QuasiAtomic::ThreadFenceForConstructor();
61 return obj;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000062 }
63 }
64 if (kInitialized) {
Vladimir Marko4bb2af52019-03-22 11:09:19 +000065 return AllocObjectFromCodeInitialized<kInstrumented>(klass, self, allocator_type).Ptr();
Nicolas Geoffray4ebb99c2021-01-07 15:13:54 +000066 } else if (!kWithChecks) {
Vladimir Marko4bb2af52019-03-22 11:09:19 +000067 return AllocObjectFromCodeResolved<kInstrumented>(klass, self, allocator_type).Ptr();
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000068 } else {
Vladimir Marko4bb2af52019-03-22 11:09:19 +000069 return AllocObjectFromCode<kInstrumented>(klass, self, allocator_type).Ptr();
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000070 }
71}
72
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080073#define GENERATE_ENTRYPOINTS_FOR_ALLOCATOR_INST(suffix, suffix2, instrumented_bool, allocator_type) \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000074extern "C" mirror::Object* artAllocObjectFromCodeWithChecks##suffix##suffix2( \
75 mirror::Class* klass, Thread* self) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070076 REQUIRES_SHARED(Locks::mutator_lock_) { \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000077 return artAllocObjectFromCode<false, true, instrumented_bool, allocator_type>(klass, self); \
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080078} \
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080079extern "C" mirror::Object* artAllocObjectFromCodeResolved##suffix##suffix2( \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000080 mirror::Class* klass, Thread* self) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070081 REQUIRES_SHARED(Locks::mutator_lock_) { \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000082 return artAllocObjectFromCode<false, false, instrumented_bool, allocator_type>(klass, self); \
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080083} \
84extern "C" mirror::Object* artAllocObjectFromCodeInitialized##suffix##suffix2( \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000085 mirror::Class* klass, Thread* self) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070086 REQUIRES_SHARED(Locks::mutator_lock_) { \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000087 return artAllocObjectFromCode<true, false, instrumented_bool, allocator_type>(klass, self); \
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080088} \
Alex Lightd109e302018-06-27 10:25:41 -070089extern "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(); \
Vladimir Marko179b7c62019-03-22 13:38:57 +000094 return mirror::String::AllocEmptyString<instrumented_bool>(self, allocator_type).Ptr(); \
Alex Lightd109e302018-06-27 10:25:41 -070095} \
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -080096extern "C" mirror::Array* artAllocArrayFromCodeResolved##suffix##suffix2( \
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +000097 mirror::Class* klass, int32_t component_count, Thread* self) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070098 REQUIRES_SHARED(Locks::mutator_lock_) { \
Ian Rogers1d8cdbc2014-09-22 22:51:09 -070099 ScopedQuickEntrypointChecks sqec(self); \
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000100 return AllocArrayFromCodeResolved<instrumented_bool>( \
101 klass, component_count, self, allocator_type).Ptr(); \
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800102} \
Jeff Hao848f70a2014-01-15 13:49:50 -0800103extern "C" mirror::String* artAllocStringFromBytesFromCode##suffix##suffix2( \
104 mirror::ByteArray* byte_array, int32_t high, int32_t offset, int32_t byte_count, \
105 Thread* self) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700106 REQUIRES_SHARED(Locks::mutator_lock_) { \
Jeff Hao848f70a2014-01-15 13:49:50 -0800107 ScopedQuickEntrypointChecks sqec(self); \
108 StackHandleScope<1> hs(self); \
109 Handle<mirror::ByteArray> handle_array(hs.NewHandle(byte_array)); \
Vladimir Marko179b7c62019-03-22 13:38:57 +0000110 return mirror::String::AllocFromByteArray<instrumented_bool>( \
111 self, byte_count, handle_array, offset, high, allocator_type).Ptr(); \
Jeff Hao848f70a2014-01-15 13:49:50 -0800112} \
113extern "C" mirror::String* artAllocStringFromCharsFromCode##suffix##suffix2( \
114 int32_t offset, int32_t char_count, mirror::CharArray* char_array, Thread* self) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700115 REQUIRES_SHARED(Locks::mutator_lock_) { \
Jeff Hao848f70a2014-01-15 13:49:50 -0800116 StackHandleScope<1> hs(self); \
117 Handle<mirror::CharArray> handle_array(hs.NewHandle(char_array)); \
Vladimir Marko179b7c62019-03-22 13:38:57 +0000118 return mirror::String::AllocFromCharArray<instrumented_bool>( \
119 self, char_count, handle_array, offset, allocator_type).Ptr(); \
Jeff Hao848f70a2014-01-15 13:49:50 -0800120} \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700121extern "C" mirror::String* artAllocStringFromStringFromCode##suffix##suffix2( /* NOLINT */ \
Jeff Hao848f70a2014-01-15 13:49:50 -0800122 mirror::String* string, Thread* self) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700123 REQUIRES_SHARED(Locks::mutator_lock_) { \
Jeff Hao848f70a2014-01-15 13:49:50 -0800124 StackHandleScope<1> hs(self); \
125 Handle<mirror::String> handle_string(hs.NewHandle(string)); \
Vladimir Marko179b7c62019-03-22 13:38:57 +0000126 return mirror::String::AllocFromString<instrumented_bool>( \
127 self, handle_string->GetLength(), handle_string, 0, allocator_type).Ptr(); \
Ian Rogers57b86d42012-03-27 16:05:41 -0700128}
129
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800130#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)
Ian Rogers57b86d42012-03-27 16:05:41 -0700133
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800134GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(DlMalloc, gc::kAllocatorTypeDlMalloc)
135GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(RosAlloc, gc::kAllocatorTypeRosAlloc)
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800136GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(BumpPointer, gc::kAllocatorTypeBumpPointer)
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800137GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(TLAB, gc::kAllocatorTypeTLAB)
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800138GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(Region, gc::kAllocatorTypeRegion)
139GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(RegionTLAB, gc::kAllocatorTypeRegionTLAB)
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700140
Mathieu Chartierd8891782014-03-02 13:28:37 -0800141#define GENERATE_ENTRYPOINTS(suffix) \
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000142extern "C" void* art_quick_alloc_array_resolved##suffix(mirror::Class* klass, int32_t); \
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +0000143extern "C" void* art_quick_alloc_array_resolved8##suffix(mirror::Class* klass, int32_t); \
144extern "C" void* art_quick_alloc_array_resolved16##suffix(mirror::Class* klass, int32_t); \
145extern "C" void* art_quick_alloc_array_resolved32##suffix(mirror::Class* klass, int32_t); \
146extern "C" void* art_quick_alloc_array_resolved64##suffix(mirror::Class* klass, int32_t); \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000147extern "C" void* art_quick_alloc_object_resolved##suffix(mirror::Class* klass); \
148extern "C" void* art_quick_alloc_object_initialized##suffix(mirror::Class* klass); \
149extern "C" void* art_quick_alloc_object_with_checks##suffix(mirror::Class* klass); \
Alex Lightd109e302018-06-27 10:25:41 -0700150extern "C" void* art_quick_alloc_string_object##suffix(mirror::Class* klass); \
Jeff Hao848f70a2014-01-15 13:49:50 -0800151extern "C" void* art_quick_alloc_string_from_bytes##suffix(void*, int32_t, int32_t, int32_t); \
152extern "C" void* art_quick_alloc_string_from_chars##suffix(int32_t, int32_t, void*); \
153extern "C" void* art_quick_alloc_string_from_string##suffix(void*); \
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000154extern "C" void* art_quick_alloc_array_resolved##suffix##_instrumented(mirror::Class* klass, int32_t); \
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +0000155extern "C" void* art_quick_alloc_array_resolved8##suffix##_instrumented(mirror::Class* klass, int32_t); \
156extern "C" void* art_quick_alloc_array_resolved16##suffix##_instrumented(mirror::Class* klass, int32_t); \
157extern "C" void* art_quick_alloc_array_resolved32##suffix##_instrumented(mirror::Class* klass, int32_t); \
158extern "C" void* art_quick_alloc_array_resolved64##suffix##_instrumented(mirror::Class* klass, int32_t); \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000159extern "C" void* art_quick_alloc_object_resolved##suffix##_instrumented(mirror::Class* klass); \
160extern "C" void* art_quick_alloc_object_initialized##suffix##_instrumented(mirror::Class* klass); \
161extern "C" void* art_quick_alloc_object_with_checks##suffix##_instrumented(mirror::Class* klass); \
Alex Lightd109e302018-06-27 10:25:41 -0700162extern "C" void* art_quick_alloc_string_object##suffix##_instrumented(mirror::Class* klass); \
Jeff Hao848f70a2014-01-15 13:49:50 -0800163extern "C" void* art_quick_alloc_string_from_bytes##suffix##_instrumented(void*, int32_t, int32_t, int32_t); \
164extern "C" void* art_quick_alloc_string_from_chars##suffix##_instrumented(int32_t, int32_t, void*); \
165extern "C" void* art_quick_alloc_string_from_string##suffix##_instrumented(void*); \
Mathieu Chartierd8891782014-03-02 13:28:37 -0800166void SetQuickAllocEntryPoints##suffix(QuickEntryPoints* qpoints, bool instrumented) { \
167 if (instrumented) { \
Mathieu Chartierd8891782014-03-02 13:28:37 -0800168 qpoints->pAllocArrayResolved = art_quick_alloc_array_resolved##suffix##_instrumented; \
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +0000169 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; \
Mathieu Chartierd8891782014-03-02 13:28:37 -0800173 qpoints->pAllocObjectResolved = art_quick_alloc_object_resolved##suffix##_instrumented; \
174 qpoints->pAllocObjectInitialized = art_quick_alloc_object_initialized##suffix##_instrumented; \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000175 qpoints->pAllocObjectWithChecks = art_quick_alloc_object_with_checks##suffix##_instrumented; \
Alex Lightd109e302018-06-27 10:25:41 -0700176 qpoints->pAllocStringObject = art_quick_alloc_string_object##suffix##_instrumented; \
Jeff Hao848f70a2014-01-15 13:49:50 -0800177 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; \
Mathieu Chartierd8891782014-03-02 13:28:37 -0800180 } else { \
Mathieu Chartierd8891782014-03-02 13:28:37 -0800181 qpoints->pAllocArrayResolved = art_quick_alloc_array_resolved##suffix; \
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +0000182 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; \
Mathieu Chartierd8891782014-03-02 13:28:37 -0800186 qpoints->pAllocObjectResolved = art_quick_alloc_object_resolved##suffix; \
187 qpoints->pAllocObjectInitialized = art_quick_alloc_object_initialized##suffix; \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000188 qpoints->pAllocObjectWithChecks = art_quick_alloc_object_with_checks##suffix; \
Alex Lightd109e302018-06-27 10:25:41 -0700189 qpoints->pAllocStringObject = art_quick_alloc_string_object##suffix; \
Jeff Hao848f70a2014-01-15 13:49:50 -0800190 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; \
Mathieu Chartierd8891782014-03-02 13:28:37 -0800193 } \
194}
195
196// Generate the entrypoint functions.
Ian Rogersc3ccc102014-06-25 11:52:14 -0700197#if !defined(__APPLE__) || !defined(__LP64__)
Andreas Gampec8ccf682014-09-29 20:07:43 -0700198GENERATE_ENTRYPOINTS(_dlmalloc)
199GENERATE_ENTRYPOINTS(_rosalloc)
200GENERATE_ENTRYPOINTS(_bump_pointer)
201GENERATE_ENTRYPOINTS(_tlab)
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800202GENERATE_ENTRYPOINTS(_region)
203GENERATE_ENTRYPOINTS(_region_tlab)
Ian Rogersc3ccc102014-06-25 11:52:14 -0700204#endif
Mathieu Chartierd8891782014-03-02 13:28:37 -0800205
206static bool entry_points_instrumented = false;
207static gc::AllocatorType entry_points_allocator = gc::kAllocatorTypeDlMalloc;
208
209void SetQuickAllocEntryPointsAllocator(gc::AllocatorType allocator) {
210 entry_points_allocator = allocator;
211}
212
213void SetQuickAllocEntryPointsInstrumented(bool instrumented) {
214 entry_points_instrumented = instrumented;
215}
216
Lokesh Gidra7e678d32020-04-28 16:17:49 -0700217void ResetQuickAllocEntryPoints(QuickEntryPoints* qpoints) {
Andreas Gampe48cc32c2015-04-07 02:53:04 +0000218#if !defined(__APPLE__) || !defined(__LP64__)
Ian Rogersde2db522014-11-04 14:43:18 -0800219 switch (entry_points_allocator) {
Mathieu Chartierd8891782014-03-02 13:28:37 -0800220 case gc::kAllocatorTypeDlMalloc: {
221 SetQuickAllocEntryPoints_dlmalloc(qpoints, entry_points_instrumented);
Ian Rogers7dc9c812014-11-04 15:10:55 -0800222 return;
Mathieu Chartierd8891782014-03-02 13:28:37 -0800223 }
224 case gc::kAllocatorTypeRosAlloc: {
225 SetQuickAllocEntryPoints_rosalloc(qpoints, entry_points_instrumented);
Ian Rogers7dc9c812014-11-04 15:10:55 -0800226 return;
Mathieu Chartierd8891782014-03-02 13:28:37 -0800227 }
228 case gc::kAllocatorTypeBumpPointer: {
229 CHECK(kMovingCollector);
230 SetQuickAllocEntryPoints_bump_pointer(qpoints, entry_points_instrumented);
Ian Rogers7dc9c812014-11-04 15:10:55 -0800231 return;
Mathieu Chartierd8891782014-03-02 13:28:37 -0800232 }
233 case gc::kAllocatorTypeTLAB: {
234 CHECK(kMovingCollector);
235 SetQuickAllocEntryPoints_tlab(qpoints, entry_points_instrumented);
Ian Rogers7dc9c812014-11-04 15:10:55 -0800236 return;
Mathieu Chartierd8891782014-03-02 13:28:37 -0800237 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800238 case gc::kAllocatorTypeRegion: {
239 CHECK(kMovingCollector);
240 SetQuickAllocEntryPoints_region(qpoints, entry_points_instrumented);
241 return;
242 }
243 case gc::kAllocatorTypeRegionTLAB: {
244 CHECK(kMovingCollector);
Lokesh Gidra7e678d32020-04-28 16:17:49 -0700245 SetQuickAllocEntryPoints_region_tlab(qpoints, entry_points_instrumented);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800246 return;
247 }
Andreas Gampe48cc32c2015-04-07 02:53:04 +0000248 default:
249 break;
Mathieu Chartierd8891782014-03-02 13:28:37 -0800250 }
Andreas Gampe48cc32c2015-04-07 02:53:04 +0000251#else
252 UNUSED(qpoints);
253#endif
254 UNIMPLEMENTED(FATAL);
Ian Rogersde2db522014-11-04 14:43:18 -0800255 UNREACHABLE();
Mathieu Chartierd8891782014-03-02 13:28:37 -0800256}
257
Ian Rogers57b86d42012-03-27 16:05:41 -0700258} // namespace art