blob: 257cd41bc8ba6eb2e200be0d8ea18a3243f3e4a3 [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"
Vladimir Marko5924a4a2018-05-29 17:40:41 +010028#include "mirror/string-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) {
44 if (kInitialized || klass->IsInitialized()) {
45 if (!kFinalize || !klass->IsFinalizable()) {
46 size_t byte_count = klass->GetObjectSize();
47 byte_count = RoundUp(byte_count, gc::space::BumpPointerSpace::kAlignment);
48 mirror::Object* obj;
49 if (LIKELY(byte_count < self->TlabSize())) {
50 obj = self->AllocTlab(byte_count);
51 DCHECK(obj != nullptr) << "AllocTlab can't fail";
52 obj->SetClass(klass);
53 if (kUseBakerReadBarrier) {
54 obj->AssertReadBarrierState();
55 }
56 QuasiAtomic::ThreadFenceForConstructor();
57 return obj;
58 }
59 }
60 }
61 }
62 if (kInitialized) {
63 return AllocObjectFromCodeInitialized<kInstrumented>(klass, self, allocator_type);
64 } else if (!kFinalize) {
65 return AllocObjectFromCodeResolved<kInstrumented>(klass, self, allocator_type);
66 } else {
67 return AllocObjectFromCode<kInstrumented>(klass, self, allocator_type);
68 }
69}
70
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080071#define GENERATE_ENTRYPOINTS_FOR_ALLOCATOR_INST(suffix, suffix2, instrumented_bool, allocator_type) \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000072extern "C" mirror::Object* artAllocObjectFromCodeWithChecks##suffix##suffix2( \
73 mirror::Class* klass, Thread* self) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070074 REQUIRES_SHARED(Locks::mutator_lock_) { \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000075 return artAllocObjectFromCode<false, true, instrumented_bool, allocator_type>(klass, self); \
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080076} \
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080077extern "C" mirror::Object* artAllocObjectFromCodeResolved##suffix##suffix2( \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000078 mirror::Class* klass, Thread* self) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070079 REQUIRES_SHARED(Locks::mutator_lock_) { \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000080 return artAllocObjectFromCode<false, false, instrumented_bool, allocator_type>(klass, self); \
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080081} \
82extern "C" mirror::Object* artAllocObjectFromCodeInitialized##suffix##suffix2( \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000083 mirror::Class* klass, Thread* self) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070084 REQUIRES_SHARED(Locks::mutator_lock_) { \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000085 return artAllocObjectFromCode<true, false, instrumented_bool, allocator_type>(klass, self); \
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080086} \
Alex Lightd109e302018-06-27 10:25:41 -070087extern "C" mirror::String* artAllocStringObject##suffix##suffix2( \
88 mirror::Class* klass, Thread* self) \
89 REQUIRES_SHARED(Locks::mutator_lock_) { \
90 /* The klass arg is so it matches the ABI of the other object alloc callbacks. */ \
91 DCHECK(klass->IsStringClass()) << klass->PrettyClass(); \
92 return mirror::String::AllocEmptyString<instrumented_bool>(self, allocator_type); \
93} \
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -080094extern "C" mirror::Array* artAllocArrayFromCodeResolved##suffix##suffix2( \
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +000095 mirror::Class* klass, int32_t component_count, Thread* self) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070096 REQUIRES_SHARED(Locks::mutator_lock_) { \
Ian Rogers1d8cdbc2014-09-22 22:51:09 -070097 ScopedQuickEntrypointChecks sqec(self); \
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +000098 return AllocArrayFromCodeResolved<instrumented_bool>(klass, component_count, self, \
99 allocator_type); \
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800100} \
Jeff Hao848f70a2014-01-15 13:49:50 -0800101extern "C" mirror::String* artAllocStringFromBytesFromCode##suffix##suffix2( \
102 mirror::ByteArray* byte_array, int32_t high, int32_t offset, int32_t byte_count, \
103 Thread* self) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700104 REQUIRES_SHARED(Locks::mutator_lock_) { \
Jeff Hao848f70a2014-01-15 13:49:50 -0800105 ScopedQuickEntrypointChecks sqec(self); \
106 StackHandleScope<1> hs(self); \
107 Handle<mirror::ByteArray> handle_array(hs.NewHandle(byte_array)); \
108 return mirror::String::AllocFromByteArray<instrumented_bool>(self, byte_count, handle_array, \
109 offset, high, allocator_type); \
110} \
111extern "C" mirror::String* artAllocStringFromCharsFromCode##suffix##suffix2( \
112 int32_t offset, int32_t char_count, mirror::CharArray* char_array, Thread* self) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700113 REQUIRES_SHARED(Locks::mutator_lock_) { \
Jeff Hao848f70a2014-01-15 13:49:50 -0800114 StackHandleScope<1> hs(self); \
115 Handle<mirror::CharArray> handle_array(hs.NewHandle(char_array)); \
116 return mirror::String::AllocFromCharArray<instrumented_bool>(self, char_count, handle_array, \
117 offset, allocator_type); \
118} \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700119extern "C" mirror::String* artAllocStringFromStringFromCode##suffix##suffix2( /* NOLINT */ \
Jeff Hao848f70a2014-01-15 13:49:50 -0800120 mirror::String* string, Thread* self) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700121 REQUIRES_SHARED(Locks::mutator_lock_) { \
Jeff Hao848f70a2014-01-15 13:49:50 -0800122 StackHandleScope<1> hs(self); \
123 Handle<mirror::String> handle_string(hs.NewHandle(string)); \
124 return mirror::String::AllocFromString<instrumented_bool>(self, handle_string->GetLength(), \
125 handle_string, 0, allocator_type); \
Ian Rogers57b86d42012-03-27 16:05:41 -0700126}
127
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800128#define GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(suffix, allocator_type) \
129 GENERATE_ENTRYPOINTS_FOR_ALLOCATOR_INST(suffix, Instrumented, true, allocator_type) \
130 GENERATE_ENTRYPOINTS_FOR_ALLOCATOR_INST(suffix, , false, allocator_type)
Ian Rogers57b86d42012-03-27 16:05:41 -0700131
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800132GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(DlMalloc, gc::kAllocatorTypeDlMalloc)
133GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(RosAlloc, gc::kAllocatorTypeRosAlloc)
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800134GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(BumpPointer, gc::kAllocatorTypeBumpPointer)
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800135GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(TLAB, gc::kAllocatorTypeTLAB)
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800136GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(Region, gc::kAllocatorTypeRegion)
137GENERATE_ENTRYPOINTS_FOR_ALLOCATOR(RegionTLAB, gc::kAllocatorTypeRegionTLAB)
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700138
Mathieu Chartierd8891782014-03-02 13:28:37 -0800139#define GENERATE_ENTRYPOINTS(suffix) \
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000140extern "C" void* art_quick_alloc_array_resolved##suffix(mirror::Class* klass, int32_t); \
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +0000141extern "C" void* art_quick_alloc_array_resolved8##suffix(mirror::Class* klass, int32_t); \
142extern "C" void* art_quick_alloc_array_resolved16##suffix(mirror::Class* klass, int32_t); \
143extern "C" void* art_quick_alloc_array_resolved32##suffix(mirror::Class* klass, int32_t); \
144extern "C" void* art_quick_alloc_array_resolved64##suffix(mirror::Class* klass, int32_t); \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000145extern "C" void* art_quick_alloc_object_resolved##suffix(mirror::Class* klass); \
146extern "C" void* art_quick_alloc_object_initialized##suffix(mirror::Class* klass); \
147extern "C" void* art_quick_alloc_object_with_checks##suffix(mirror::Class* klass); \
Alex Lightd109e302018-06-27 10:25:41 -0700148extern "C" void* art_quick_alloc_string_object##suffix(mirror::Class* klass); \
Jeff Hao848f70a2014-01-15 13:49:50 -0800149extern "C" void* art_quick_alloc_string_from_bytes##suffix(void*, int32_t, int32_t, int32_t); \
150extern "C" void* art_quick_alloc_string_from_chars##suffix(int32_t, int32_t, void*); \
151extern "C" void* art_quick_alloc_string_from_string##suffix(void*); \
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000152extern "C" void* art_quick_alloc_array_resolved##suffix##_instrumented(mirror::Class* klass, int32_t); \
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +0000153extern "C" void* art_quick_alloc_array_resolved8##suffix##_instrumented(mirror::Class* klass, int32_t); \
154extern "C" void* art_quick_alloc_array_resolved16##suffix##_instrumented(mirror::Class* klass, int32_t); \
155extern "C" void* art_quick_alloc_array_resolved32##suffix##_instrumented(mirror::Class* klass, int32_t); \
156extern "C" void* art_quick_alloc_array_resolved64##suffix##_instrumented(mirror::Class* klass, int32_t); \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000157extern "C" void* art_quick_alloc_object_resolved##suffix##_instrumented(mirror::Class* klass); \
158extern "C" void* art_quick_alloc_object_initialized##suffix##_instrumented(mirror::Class* klass); \
159extern "C" void* art_quick_alloc_object_with_checks##suffix##_instrumented(mirror::Class* klass); \
Alex Lightd109e302018-06-27 10:25:41 -0700160extern "C" void* art_quick_alloc_string_object##suffix##_instrumented(mirror::Class* klass); \
Jeff Hao848f70a2014-01-15 13:49:50 -0800161extern "C" void* art_quick_alloc_string_from_bytes##suffix##_instrumented(void*, int32_t, int32_t, int32_t); \
162extern "C" void* art_quick_alloc_string_from_chars##suffix##_instrumented(int32_t, int32_t, void*); \
163extern "C" void* art_quick_alloc_string_from_string##suffix##_instrumented(void*); \
Mathieu Chartierd8891782014-03-02 13:28:37 -0800164void SetQuickAllocEntryPoints##suffix(QuickEntryPoints* qpoints, bool instrumented) { \
165 if (instrumented) { \
Mathieu Chartierd8891782014-03-02 13:28:37 -0800166 qpoints->pAllocArrayResolved = art_quick_alloc_array_resolved##suffix##_instrumented; \
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +0000167 qpoints->pAllocArrayResolved8 = art_quick_alloc_array_resolved8##suffix##_instrumented; \
168 qpoints->pAllocArrayResolved16 = art_quick_alloc_array_resolved16##suffix##_instrumented; \
169 qpoints->pAllocArrayResolved32 = art_quick_alloc_array_resolved32##suffix##_instrumented; \
170 qpoints->pAllocArrayResolved64 = art_quick_alloc_array_resolved64##suffix##_instrumented; \
Mathieu Chartierd8891782014-03-02 13:28:37 -0800171 qpoints->pAllocObjectResolved = art_quick_alloc_object_resolved##suffix##_instrumented; \
172 qpoints->pAllocObjectInitialized = art_quick_alloc_object_initialized##suffix##_instrumented; \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000173 qpoints->pAllocObjectWithChecks = art_quick_alloc_object_with_checks##suffix##_instrumented; \
Alex Lightd109e302018-06-27 10:25:41 -0700174 qpoints->pAllocStringObject = art_quick_alloc_string_object##suffix##_instrumented; \
Jeff Hao848f70a2014-01-15 13:49:50 -0800175 qpoints->pAllocStringFromBytes = art_quick_alloc_string_from_bytes##suffix##_instrumented; \
176 qpoints->pAllocStringFromChars = art_quick_alloc_string_from_chars##suffix##_instrumented; \
177 qpoints->pAllocStringFromString = art_quick_alloc_string_from_string##suffix##_instrumented; \
Mathieu Chartierd8891782014-03-02 13:28:37 -0800178 } else { \
Mathieu Chartierd8891782014-03-02 13:28:37 -0800179 qpoints->pAllocArrayResolved = art_quick_alloc_array_resolved##suffix; \
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +0000180 qpoints->pAllocArrayResolved8 = art_quick_alloc_array_resolved8##suffix; \
181 qpoints->pAllocArrayResolved16 = art_quick_alloc_array_resolved16##suffix; \
182 qpoints->pAllocArrayResolved32 = art_quick_alloc_array_resolved32##suffix; \
183 qpoints->pAllocArrayResolved64 = art_quick_alloc_array_resolved64##suffix; \
Mathieu Chartierd8891782014-03-02 13:28:37 -0800184 qpoints->pAllocObjectResolved = art_quick_alloc_object_resolved##suffix; \
185 qpoints->pAllocObjectInitialized = art_quick_alloc_object_initialized##suffix; \
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000186 qpoints->pAllocObjectWithChecks = art_quick_alloc_object_with_checks##suffix; \
Alex Lightd109e302018-06-27 10:25:41 -0700187 qpoints->pAllocStringObject = art_quick_alloc_string_object##suffix; \
Jeff Hao848f70a2014-01-15 13:49:50 -0800188 qpoints->pAllocStringFromBytes = art_quick_alloc_string_from_bytes##suffix; \
189 qpoints->pAllocStringFromChars = art_quick_alloc_string_from_chars##suffix; \
190 qpoints->pAllocStringFromString = art_quick_alloc_string_from_string##suffix; \
Mathieu Chartierd8891782014-03-02 13:28:37 -0800191 } \
192}
193
194// Generate the entrypoint functions.
Ian Rogersc3ccc102014-06-25 11:52:14 -0700195#if !defined(__APPLE__) || !defined(__LP64__)
Andreas Gampec8ccf682014-09-29 20:07:43 -0700196GENERATE_ENTRYPOINTS(_dlmalloc)
197GENERATE_ENTRYPOINTS(_rosalloc)
198GENERATE_ENTRYPOINTS(_bump_pointer)
199GENERATE_ENTRYPOINTS(_tlab)
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800200GENERATE_ENTRYPOINTS(_region)
201GENERATE_ENTRYPOINTS(_region_tlab)
Ian Rogersc3ccc102014-06-25 11:52:14 -0700202#endif
Mathieu Chartierd8891782014-03-02 13:28:37 -0800203
204static bool entry_points_instrumented = false;
205static gc::AllocatorType entry_points_allocator = gc::kAllocatorTypeDlMalloc;
206
207void SetQuickAllocEntryPointsAllocator(gc::AllocatorType allocator) {
208 entry_points_allocator = allocator;
209}
210
211void SetQuickAllocEntryPointsInstrumented(bool instrumented) {
212 entry_points_instrumented = instrumented;
213}
214
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800215void ResetQuickAllocEntryPoints(QuickEntryPoints* qpoints, bool is_marking) {
Andreas Gampe48cc32c2015-04-07 02:53:04 +0000216#if !defined(__APPLE__) || !defined(__LP64__)
Ian Rogersde2db522014-11-04 14:43:18 -0800217 switch (entry_points_allocator) {
Mathieu Chartierd8891782014-03-02 13:28:37 -0800218 case gc::kAllocatorTypeDlMalloc: {
219 SetQuickAllocEntryPoints_dlmalloc(qpoints, entry_points_instrumented);
Ian Rogers7dc9c812014-11-04 15:10:55 -0800220 return;
Mathieu Chartierd8891782014-03-02 13:28:37 -0800221 }
222 case gc::kAllocatorTypeRosAlloc: {
223 SetQuickAllocEntryPoints_rosalloc(qpoints, entry_points_instrumented);
Ian Rogers7dc9c812014-11-04 15:10:55 -0800224 return;
Mathieu Chartierd8891782014-03-02 13:28:37 -0800225 }
226 case gc::kAllocatorTypeBumpPointer: {
227 CHECK(kMovingCollector);
228 SetQuickAllocEntryPoints_bump_pointer(qpoints, entry_points_instrumented);
Ian Rogers7dc9c812014-11-04 15:10:55 -0800229 return;
Mathieu Chartierd8891782014-03-02 13:28:37 -0800230 }
231 case gc::kAllocatorTypeTLAB: {
232 CHECK(kMovingCollector);
233 SetQuickAllocEntryPoints_tlab(qpoints, entry_points_instrumented);
Ian Rogers7dc9c812014-11-04 15:10:55 -0800234 return;
Mathieu Chartierd8891782014-03-02 13:28:37 -0800235 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800236 case gc::kAllocatorTypeRegion: {
237 CHECK(kMovingCollector);
238 SetQuickAllocEntryPoints_region(qpoints, entry_points_instrumented);
239 return;
240 }
241 case gc::kAllocatorTypeRegionTLAB: {
242 CHECK(kMovingCollector);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800243 if (is_marking) {
244 SetQuickAllocEntryPoints_region_tlab(qpoints, entry_points_instrumented);
245 } else {
246 // Not marking means we need no read barriers and can just use the normal TLAB case.
247 SetQuickAllocEntryPoints_tlab(qpoints, entry_points_instrumented);
248 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800249 return;
250 }
Andreas Gampe48cc32c2015-04-07 02:53:04 +0000251 default:
252 break;
Mathieu Chartierd8891782014-03-02 13:28:37 -0800253 }
Andreas Gampe48cc32c2015-04-07 02:53:04 +0000254#else
255 UNUSED(qpoints);
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800256 UNUSED(is_marking);
Andreas Gampe48cc32c2015-04-07 02:53:04 +0000257#endif
258 UNIMPLEMENTED(FATAL);
Ian Rogersde2db522014-11-04 14:43:18 -0800259 UNREACHABLE();
Mathieu Chartierd8891782014-03-02 13:28:37 -0800260}
261
Ian Rogers57b86d42012-03-27 16:05:41 -0700262} // namespace art