blob: dba4ecc567fcad444c8d3f115280df4e8cf362b4 [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,
35 bool kFinalize,
36 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);
43 if (kUseTlabFastPath && !kInstrumented && allocator_type == gc::kAllocatorTypeTLAB) {
Vladimir Marko8e110652019-07-30 10:14:41 +010044 // The "object size alloc fast path" is set when the class is
45 // visibly initialized, objects are fixed size and non-finalizable.
46 // Otherwise, the value is too large for the size check to succeed.
47 size_t byte_count = klass->GetObjectSizeAllocFastPath();
48 if (LIKELY(byte_count < self->TlabSize())) {
49 static_assert(kObjectAlignment == gc::space::BumpPointerSpace::kAlignment, "Alignment check");
50 DCHECK_ALIGNED(byte_count, gc::space::BumpPointerSpace::kAlignment);
51 mirror::Object* obj = self->AllocTlab(byte_count);
52 DCHECK(obj != nullptr) << "AllocTlab can't fail";
53 obj->SetClass(klass);
54 if (kUseBakerReadBarrier) {
55 obj->AssertReadBarrierState();
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000056 }
Vladimir Marko8e110652019-07-30 10:14:41 +010057 QuasiAtomic::ThreadFenceForConstructor();
58 return obj;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000059 }
60 }
61 if (kInitialized) {
Vladimir Marko4bb2af52019-03-22 11:09:19 +000062 return AllocObjectFromCodeInitialized<kInstrumented>(klass, self, allocator_type).Ptr();
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000063 } else if (!kFinalize) {
Vladimir Marko4bb2af52019-03-22 11:09:19 +000064 return AllocObjectFromCodeResolved<kInstrumented>(klass, self, allocator_type).Ptr();
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000065 } else {
Vladimir Marko4bb2af52019-03-22 11:09:19 +000066 return AllocObjectFromCode<kInstrumented>(klass, self, allocator_type).Ptr();
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000067 }
68}
69
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080070#define GENERATE_ENTRYPOINTS_FOR_ALLOCATOR_INST(suffix, suffix2, instrumented_bool, allocator_type) \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000071extern "C" mirror::Object* artAllocObjectFromCodeWithChecks##suffix##suffix2( \
72 mirror::Class* klass, Thread* self) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070073 REQUIRES_SHARED(Locks::mutator_lock_) { \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000074 return artAllocObjectFromCode<false, true, instrumented_bool, allocator_type>(klass, self); \
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080075} \
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080076extern "C" mirror::Object* artAllocObjectFromCodeResolved##suffix##suffix2( \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000077 mirror::Class* klass, Thread* self) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070078 REQUIRES_SHARED(Locks::mutator_lock_) { \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000079 return artAllocObjectFromCode<false, false, instrumented_bool, allocator_type>(klass, self); \
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080080} \
81extern "C" mirror::Object* artAllocObjectFromCodeInitialized##suffix##suffix2( \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000082 mirror::Class* klass, Thread* self) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070083 REQUIRES_SHARED(Locks::mutator_lock_) { \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000084 return artAllocObjectFromCode<true, false, instrumented_bool, allocator_type>(klass, self); \
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080085} \
Alex Lightd109e302018-06-27 10:25:41 -070086extern "C" mirror::String* artAllocStringObject##suffix##suffix2( \
87 mirror::Class* klass, Thread* self) \
88 REQUIRES_SHARED(Locks::mutator_lock_) { \
89 /* The klass arg is so it matches the ABI of the other object alloc callbacks. */ \
90 DCHECK(klass->IsStringClass()) << klass->PrettyClass(); \
Vladimir Marko179b7c62019-03-22 13:38:57 +000091 return mirror::String::AllocEmptyString<instrumented_bool>(self, allocator_type).Ptr(); \
Alex Lightd109e302018-06-27 10:25:41 -070092} \
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -080093extern "C" mirror::Array* artAllocArrayFromCodeResolved##suffix##suffix2( \
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +000094 mirror::Class* klass, int32_t component_count, Thread* self) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070095 REQUIRES_SHARED(Locks::mutator_lock_) { \
Ian Rogers1d8cdbc2014-09-22 22:51:09 -070096 ScopedQuickEntrypointChecks sqec(self); \
Vladimir Marko4bb2af52019-03-22 11:09:19 +000097 return AllocArrayFromCodeResolved<instrumented_bool>( \
98 klass, component_count, self, allocator_type).Ptr(); \
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -080099} \
Jeff Hao848f70a2014-01-15 13:49:50 -0800100extern "C" mirror::String* artAllocStringFromBytesFromCode##suffix##suffix2( \
101 mirror::ByteArray* byte_array, int32_t high, int32_t offset, int32_t byte_count, \
102 Thread* self) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700103 REQUIRES_SHARED(Locks::mutator_lock_) { \
Jeff Hao848f70a2014-01-15 13:49:50 -0800104 ScopedQuickEntrypointChecks sqec(self); \
105 StackHandleScope<1> hs(self); \
106 Handle<mirror::ByteArray> handle_array(hs.NewHandle(byte_array)); \
Vladimir Marko179b7c62019-03-22 13:38:57 +0000107 return mirror::String::AllocFromByteArray<instrumented_bool>( \
108 self, byte_count, handle_array, offset, high, allocator_type).Ptr(); \
Jeff Hao848f70a2014-01-15 13:49:50 -0800109} \
110extern "C" mirror::String* artAllocStringFromCharsFromCode##suffix##suffix2( \
111 int32_t offset, int32_t char_count, mirror::CharArray* char_array, Thread* self) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700112 REQUIRES_SHARED(Locks::mutator_lock_) { \
Jeff Hao848f70a2014-01-15 13:49:50 -0800113 StackHandleScope<1> hs(self); \
114 Handle<mirror::CharArray> handle_array(hs.NewHandle(char_array)); \
Vladimir Marko179b7c62019-03-22 13:38:57 +0000115 return mirror::String::AllocFromCharArray<instrumented_bool>( \
116 self, char_count, handle_array, offset, allocator_type).Ptr(); \
Jeff Hao848f70a2014-01-15 13:49:50 -0800117} \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700118extern "C" mirror::String* artAllocStringFromStringFromCode##suffix##suffix2( /* NOLINT */ \
Jeff Hao848f70a2014-01-15 13:49:50 -0800119 mirror::String* string, Thread* self) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700120 REQUIRES_SHARED(Locks::mutator_lock_) { \
Jeff Hao848f70a2014-01-15 13:49:50 -0800121 StackHandleScope<1> hs(self); \
122 Handle<mirror::String> handle_string(hs.NewHandle(string)); \
Vladimir Marko179b7c62019-03-22 13:38:57 +0000123 return mirror::String::AllocFromString<instrumented_bool>( \
124 self, handle_string->GetLength(), handle_string, 0, allocator_type).Ptr(); \
Ian Rogers57b86d42012-03-27 16:05:41 -0700125}
126
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800127#define GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(suffix, allocator_type) \
128 GENERATE_ENTRYPOINTS_FOR_ALLOCATOR_INST(suffix, Instrumented, true, allocator_type) \
129 GENERATE_ENTRYPOINTS_FOR_ALLOCATOR_INST(suffix, , false, allocator_type)
Ian Rogers57b86d42012-03-27 16:05:41 -0700130
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800131GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(DlMalloc, gc::kAllocatorTypeDlMalloc)
132GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(RosAlloc, gc::kAllocatorTypeRosAlloc)
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800133GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(BumpPointer, gc::kAllocatorTypeBumpPointer)
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800134GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(TLAB, gc::kAllocatorTypeTLAB)
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800135GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(Region, gc::kAllocatorTypeRegion)
136GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(RegionTLAB, gc::kAllocatorTypeRegionTLAB)
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700137
Mathieu Chartierd8891782014-03-02 13:28:37 -0800138#define GENERATE_ENTRYPOINTS(suffix) \
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000139extern "C" void* art_quick_alloc_array_resolved##suffix(mirror::Class* klass, int32_t); \
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +0000140extern "C" void* art_quick_alloc_array_resolved8##suffix(mirror::Class* klass, int32_t); \
141extern "C" void* art_quick_alloc_array_resolved16##suffix(mirror::Class* klass, int32_t); \
142extern "C" void* art_quick_alloc_array_resolved32##suffix(mirror::Class* klass, int32_t); \
143extern "C" void* art_quick_alloc_array_resolved64##suffix(mirror::Class* klass, int32_t); \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000144extern "C" void* art_quick_alloc_object_resolved##suffix(mirror::Class* klass); \
145extern "C" void* art_quick_alloc_object_initialized##suffix(mirror::Class* klass); \
146extern "C" void* art_quick_alloc_object_with_checks##suffix(mirror::Class* klass); \
Alex Lightd109e302018-06-27 10:25:41 -0700147extern "C" void* art_quick_alloc_string_object##suffix(mirror::Class* klass); \
Jeff Hao848f70a2014-01-15 13:49:50 -0800148extern "C" void* art_quick_alloc_string_from_bytes##suffix(void*, int32_t, int32_t, int32_t); \
149extern "C" void* art_quick_alloc_string_from_chars##suffix(int32_t, int32_t, void*); \
150extern "C" void* art_quick_alloc_string_from_string##suffix(void*); \
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000151extern "C" void* art_quick_alloc_array_resolved##suffix##_instrumented(mirror::Class* klass, int32_t); \
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +0000152extern "C" void* art_quick_alloc_array_resolved8##suffix##_instrumented(mirror::Class* klass, int32_t); \
153extern "C" void* art_quick_alloc_array_resolved16##suffix##_instrumented(mirror::Class* klass, int32_t); \
154extern "C" void* art_quick_alloc_array_resolved32##suffix##_instrumented(mirror::Class* klass, int32_t); \
155extern "C" void* art_quick_alloc_array_resolved64##suffix##_instrumented(mirror::Class* klass, int32_t); \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000156extern "C" void* art_quick_alloc_object_resolved##suffix##_instrumented(mirror::Class* klass); \
157extern "C" void* art_quick_alloc_object_initialized##suffix##_instrumented(mirror::Class* klass); \
158extern "C" void* art_quick_alloc_object_with_checks##suffix##_instrumented(mirror::Class* klass); \
Alex Lightd109e302018-06-27 10:25:41 -0700159extern "C" void* art_quick_alloc_string_object##suffix##_instrumented(mirror::Class* klass); \
Jeff Hao848f70a2014-01-15 13:49:50 -0800160extern "C" void* art_quick_alloc_string_from_bytes##suffix##_instrumented(void*, int32_t, int32_t, int32_t); \
161extern "C" void* art_quick_alloc_string_from_chars##suffix##_instrumented(int32_t, int32_t, void*); \
162extern "C" void* art_quick_alloc_string_from_string##suffix##_instrumented(void*); \
Mathieu Chartierd8891782014-03-02 13:28:37 -0800163void SetQuickAllocEntryPoints##suffix(QuickEntryPoints* qpoints, bool instrumented) { \
164 if (instrumented) { \
Mathieu Chartierd8891782014-03-02 13:28:37 -0800165 qpoints->pAllocArrayResolved = art_quick_alloc_array_resolved##suffix##_instrumented; \
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +0000166 qpoints->pAllocArrayResolved8 = art_quick_alloc_array_resolved8##suffix##_instrumented; \
167 qpoints->pAllocArrayResolved16 = art_quick_alloc_array_resolved16##suffix##_instrumented; \
168 qpoints->pAllocArrayResolved32 = art_quick_alloc_array_resolved32##suffix##_instrumented; \
169 qpoints->pAllocArrayResolved64 = art_quick_alloc_array_resolved64##suffix##_instrumented; \
Mathieu Chartierd8891782014-03-02 13:28:37 -0800170 qpoints->pAllocObjectResolved = art_quick_alloc_object_resolved##suffix##_instrumented; \
171 qpoints->pAllocObjectInitialized = art_quick_alloc_object_initialized##suffix##_instrumented; \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000172 qpoints->pAllocObjectWithChecks = art_quick_alloc_object_with_checks##suffix##_instrumented; \
Alex Lightd109e302018-06-27 10:25:41 -0700173 qpoints->pAllocStringObject = art_quick_alloc_string_object##suffix##_instrumented; \
Jeff Hao848f70a2014-01-15 13:49:50 -0800174 qpoints->pAllocStringFromBytes = art_quick_alloc_string_from_bytes##suffix##_instrumented; \
175 qpoints->pAllocStringFromChars = art_quick_alloc_string_from_chars##suffix##_instrumented; \
176 qpoints->pAllocStringFromString = art_quick_alloc_string_from_string##suffix##_instrumented; \
Mathieu Chartierd8891782014-03-02 13:28:37 -0800177 } else { \
Mathieu Chartierd8891782014-03-02 13:28:37 -0800178 qpoints->pAllocArrayResolved = art_quick_alloc_array_resolved##suffix; \
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +0000179 qpoints->pAllocArrayResolved8 = art_quick_alloc_array_resolved8##suffix; \
180 qpoints->pAllocArrayResolved16 = art_quick_alloc_array_resolved16##suffix; \
181 qpoints->pAllocArrayResolved32 = art_quick_alloc_array_resolved32##suffix; \
182 qpoints->pAllocArrayResolved64 = art_quick_alloc_array_resolved64##suffix; \
Mathieu Chartierd8891782014-03-02 13:28:37 -0800183 qpoints->pAllocObjectResolved = art_quick_alloc_object_resolved##suffix; \
184 qpoints->pAllocObjectInitialized = art_quick_alloc_object_initialized##suffix; \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000185 qpoints->pAllocObjectWithChecks = art_quick_alloc_object_with_checks##suffix; \
Alex Lightd109e302018-06-27 10:25:41 -0700186 qpoints->pAllocStringObject = art_quick_alloc_string_object##suffix; \
Jeff Hao848f70a2014-01-15 13:49:50 -0800187 qpoints->pAllocStringFromBytes = art_quick_alloc_string_from_bytes##suffix; \
188 qpoints->pAllocStringFromChars = art_quick_alloc_string_from_chars##suffix; \
189 qpoints->pAllocStringFromString = art_quick_alloc_string_from_string##suffix; \
Mathieu Chartierd8891782014-03-02 13:28:37 -0800190 } \
191}
192
193// Generate the entrypoint functions.
Ian Rogersc3ccc102014-06-25 11:52:14 -0700194#if !defined(__APPLE__) || !defined(__LP64__)
Andreas Gampec8ccf682014-09-29 20:07:43 -0700195GENERATE_ENTRYPOINTS(_dlmalloc)
196GENERATE_ENTRYPOINTS(_rosalloc)
197GENERATE_ENTRYPOINTS(_bump_pointer)
198GENERATE_ENTRYPOINTS(_tlab)
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800199GENERATE_ENTRYPOINTS(_region)
200GENERATE_ENTRYPOINTS(_region_tlab)
Ian Rogersc3ccc102014-06-25 11:52:14 -0700201#endif
Mathieu Chartierd8891782014-03-02 13:28:37 -0800202
203static bool entry_points_instrumented = false;
204static gc::AllocatorType entry_points_allocator = gc::kAllocatorTypeDlMalloc;
205
206void SetQuickAllocEntryPointsAllocator(gc::AllocatorType allocator) {
207 entry_points_allocator = allocator;
208}
209
210void SetQuickAllocEntryPointsInstrumented(bool instrumented) {
211 entry_points_instrumented = instrumented;
212}
213
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800214void ResetQuickAllocEntryPoints(QuickEntryPoints* qpoints, bool is_marking) {
Andreas Gampe48cc32c2015-04-07 02:53:04 +0000215#if !defined(__APPLE__) || !defined(__LP64__)
Ian Rogersde2db522014-11-04 14:43:18 -0800216 switch (entry_points_allocator) {
Mathieu Chartierd8891782014-03-02 13:28:37 -0800217 case gc::kAllocatorTypeDlMalloc: {
218 SetQuickAllocEntryPoints_dlmalloc(qpoints, entry_points_instrumented);
Ian Rogers7dc9c812014-11-04 15:10:55 -0800219 return;
Mathieu Chartierd8891782014-03-02 13:28:37 -0800220 }
221 case gc::kAllocatorTypeRosAlloc: {
222 SetQuickAllocEntryPoints_rosalloc(qpoints, entry_points_instrumented);
Ian Rogers7dc9c812014-11-04 15:10:55 -0800223 return;
Mathieu Chartierd8891782014-03-02 13:28:37 -0800224 }
225 case gc::kAllocatorTypeBumpPointer: {
226 CHECK(kMovingCollector);
227 SetQuickAllocEntryPoints_bump_pointer(qpoints, entry_points_instrumented);
Ian Rogers7dc9c812014-11-04 15:10:55 -0800228 return;
Mathieu Chartierd8891782014-03-02 13:28:37 -0800229 }
230 case gc::kAllocatorTypeTLAB: {
231 CHECK(kMovingCollector);
232 SetQuickAllocEntryPoints_tlab(qpoints, entry_points_instrumented);
Ian Rogers7dc9c812014-11-04 15:10:55 -0800233 return;
Mathieu Chartierd8891782014-03-02 13:28:37 -0800234 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800235 case gc::kAllocatorTypeRegion: {
236 CHECK(kMovingCollector);
237 SetQuickAllocEntryPoints_region(qpoints, entry_points_instrumented);
238 return;
239 }
240 case gc::kAllocatorTypeRegionTLAB: {
241 CHECK(kMovingCollector);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800242 if (is_marking) {
243 SetQuickAllocEntryPoints_region_tlab(qpoints, entry_points_instrumented);
244 } else {
245 // Not marking means we need no read barriers and can just use the normal TLAB case.
246 SetQuickAllocEntryPoints_tlab(qpoints, entry_points_instrumented);
247 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800248 return;
249 }
Andreas Gampe48cc32c2015-04-07 02:53:04 +0000250 default:
251 break;
Mathieu Chartierd8891782014-03-02 13:28:37 -0800252 }
Andreas Gampe48cc32c2015-04-07 02:53:04 +0000253#else
254 UNUSED(qpoints);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800255 UNUSED(is_marking);
Andreas Gampe48cc32c2015-04-07 02:53:04 +0000256#endif
257 UNIMPLEMENTED(FATAL);
Ian Rogersde2db522014-11-04 14:43:18 -0800258 UNREACHABLE();
Mathieu Chartierd8891782014-03-02 13:28:37 -0800259}
260
Ian Rogers57b86d42012-03-27 16:05:41 -0700261} // namespace art