blob: 9857249384d89c84152fda728c60a44f1209041c [file] [log] [blame]
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001/*
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 "interpreter_common.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070018
Andreas Gampef0e128a2015-02-27 20:08:34 -080019#include <cmath>
20
Vladimir Marko78baed52018-10-11 10:44:58 +010021#include "base/casts.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070022#include "base/enums.h"
Vladimir Markoc7aa87e2018-05-24 15:19:52 +010023#include "class_root.h"
Daniel Mihalyieb076692014-08-22 17:33:31 +020024#include "debugger.h"
David Sehr9e734c72018-01-04 17:56:19 -080025#include "dex/dex_file_types.h"
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +000026#include "entrypoints/runtime_asm_entrypoints.h"
Orion Hodson43f0cdb2017-10-10 14:47:32 +010027#include "intrinsics_enum.h"
Tamas Berghammerdd5e5e92016-02-12 16:29:00 +000028#include "jit/jit.h"
Andreas Gampec5b75642018-05-16 15:12:11 -070029#include "jvalue-inl.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010030#include "method_handles-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070031#include "method_handles.h"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070032#include "mirror/array-alloc-inl.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010033#include "mirror/array-inl.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010034#include "mirror/class.h"
Narayan Kamath000e1882016-10-24 17:14:25 +010035#include "mirror/emulated_stack_frame.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080036#include "mirror/method_handle_impl-inl.h"
Orion Hodson928033d2018-02-07 05:30:54 +000037#include "mirror/var_handle.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010038#include "reflection-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070039#include "reflection.h"
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +010040#include "shadow_frame-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070041#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070042#include "thread-inl.h"
Chang Xingbd208d82017-07-12 14:53:17 -070043#include "transaction.h"
Orion Hodson537a4fe2018-05-15 13:57:58 +010044#include "var_handles.h"
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +010045#include "well_known_classes.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020046
47namespace art {
48namespace interpreter {
49
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000050void ThrowNullPointerExceptionFromInterpreter() {
51 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -070052}
53
David Srbecky960327b2018-10-25 10:11:59 +000054bool CheckStackOverflow(Thread* self, size_t frame_size)
55 REQUIRES_SHARED(Locks::mutator_lock_) {
56 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
57 uint8_t* stack_end = self->GetStackEndForInterpreter(implicit_check);
58 if (UNLIKELY(__builtin_frame_address(0) < stack_end + frame_size)) {
59 ThrowStackOverflowError(self);
60 return false;
61 }
62 return true;
63}
64
Chang Xingbd208d82017-07-12 14:53:17 -070065template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
66 bool transaction_active>
Ian Rogers54874942014-06-10 16:31:03 -070067bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
68 uint16_t inst_data) {
69 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
70 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010071 ArtField* f =
72 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
73 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -070074 if (UNLIKELY(f == nullptr)) {
75 CHECK(self->IsExceptionPending());
76 return false;
77 }
Mathieu Chartieref41db72016-10-25 15:08:01 -070078 ObjPtr<mirror::Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -070079 if (is_static) {
80 obj = f->GetDeclaringClass();
Chang Xingbd208d82017-07-12 14:53:17 -070081 if (transaction_active) {
82 if (Runtime::Current()->GetTransaction()->ReadConstraint(obj.Ptr(), f)) {
83 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Can't read static fields of "
84 + obj->PrettyTypeOf() + " since it does not belong to clinit's class.");
85 return false;
86 }
87 }
Ian Rogers54874942014-06-10 16:31:03 -070088 } else {
89 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
90 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000091 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers54874942014-06-10 16:31:03 -070092 return false;
93 }
94 }
Orion Hodson3d617ac2016-10-19 14:00:46 +010095
96 JValue result;
Alex Light084fa372017-06-16 08:58:34 -070097 if (UNLIKELY(!DoFieldGetCommon<field_type>(self, shadow_frame, obj, f, &result))) {
98 // Instrumentation threw an error!
99 CHECK(self->IsExceptionPending());
100 return false;
101 }
Ian Rogers54874942014-06-10 16:31:03 -0700102 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
103 switch (field_type) {
104 case Primitive::kPrimBoolean:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100105 shadow_frame.SetVReg(vregA, result.GetZ());
Ian Rogers54874942014-06-10 16:31:03 -0700106 break;
107 case Primitive::kPrimByte:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100108 shadow_frame.SetVReg(vregA, result.GetB());
Ian Rogers54874942014-06-10 16:31:03 -0700109 break;
110 case Primitive::kPrimChar:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100111 shadow_frame.SetVReg(vregA, result.GetC());
Ian Rogers54874942014-06-10 16:31:03 -0700112 break;
113 case Primitive::kPrimShort:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100114 shadow_frame.SetVReg(vregA, result.GetS());
Ian Rogers54874942014-06-10 16:31:03 -0700115 break;
116 case Primitive::kPrimInt:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100117 shadow_frame.SetVReg(vregA, result.GetI());
Ian Rogers54874942014-06-10 16:31:03 -0700118 break;
119 case Primitive::kPrimLong:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100120 shadow_frame.SetVRegLong(vregA, result.GetJ());
Ian Rogers54874942014-06-10 16:31:03 -0700121 break;
122 case Primitive::kPrimNot:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100123 shadow_frame.SetVRegReference(vregA, result.GetL());
Ian Rogers54874942014-06-10 16:31:03 -0700124 break;
125 default:
126 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700127 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700128 }
129 return true;
130}
131
132// Explicitly instantiate all DoFieldGet functions.
Chang Xingbd208d82017-07-12 14:53:17 -0700133#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
134 template bool DoFieldGet<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
Ian Rogers54874942014-06-10 16:31:03 -0700135 ShadowFrame& shadow_frame, \
136 const Instruction* inst, \
137 uint16_t inst_data)
138
139#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
Chang Xingbd208d82017-07-12 14:53:17 -0700140 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false, true); \
141 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false, false); \
142 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true, true); \
143 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true, false);
Ian Rogers54874942014-06-10 16:31:03 -0700144
145// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700146EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
147EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
148EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
149EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
150EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
151EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
152EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700153
154// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700155EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
156EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
157EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
158EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
159EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
160EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
161EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700162
163#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
164#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
165
166// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
167// Returns true on success, otherwise throws an exception and returns false.
168template<Primitive::Type field_type>
169bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700170 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700171 if (UNLIKELY(obj == nullptr)) {
172 // We lost the reference to the field index so we cannot get a more
173 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000174 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700175 return false;
176 }
177 MemberOffset field_offset(inst->VRegC_22c());
178 // Report this field access to instrumentation if needed. Since we only have the offset of
179 // the field from the base of the object, we need to look for it first.
180 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
181 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
182 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
183 field_offset.Uint32Value());
184 DCHECK(f != nullptr);
185 DCHECK(!f->IsStatic());
Alex Light084fa372017-06-16 08:58:34 -0700186 Thread* self = Thread::Current();
187 StackHandleScope<1> hs(self);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700188 // Save obj in case the instrumentation event has thread suspension.
189 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Alex Light084fa372017-06-16 08:58:34 -0700190 instrumentation->FieldReadEvent(self,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700191 obj.Ptr(),
192 shadow_frame.GetMethod(),
193 shadow_frame.GetDexPC(),
194 f);
Alex Light084fa372017-06-16 08:58:34 -0700195 if (UNLIKELY(self->IsExceptionPending())) {
196 return false;
197 }
Ian Rogers54874942014-06-10 16:31:03 -0700198 }
199 // Note: iget-x-quick instructions are only for non-volatile fields.
200 const uint32_t vregA = inst->VRegA_22c(inst_data);
201 switch (field_type) {
202 case Primitive::kPrimInt:
203 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
204 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800205 case Primitive::kPrimBoolean:
206 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
207 break;
208 case Primitive::kPrimByte:
209 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
210 break;
211 case Primitive::kPrimChar:
212 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
213 break;
214 case Primitive::kPrimShort:
215 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
216 break;
Ian Rogers54874942014-06-10 16:31:03 -0700217 case Primitive::kPrimLong:
218 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
219 break;
220 case Primitive::kPrimNot:
221 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
222 break;
223 default:
224 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700225 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700226 }
227 return true;
228}
229
230// Explicitly instantiate all DoIGetQuick functions.
231#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
232 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
233 uint16_t inst_data)
234
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800235EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
236EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
237EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
238EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
239EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
240EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
241EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700242#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
243
244template<Primitive::Type field_type>
245static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700246 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700247 JValue field_value;
248 switch (field_type) {
249 case Primitive::kPrimBoolean:
250 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
251 break;
252 case Primitive::kPrimByte:
253 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
254 break;
255 case Primitive::kPrimChar:
256 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
257 break;
258 case Primitive::kPrimShort:
259 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
260 break;
261 case Primitive::kPrimInt:
262 field_value.SetI(shadow_frame.GetVReg(vreg));
263 break;
264 case Primitive::kPrimLong:
265 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
266 break;
267 case Primitive::kPrimNot:
268 field_value.SetL(shadow_frame.GetVRegReference(vreg));
269 break;
270 default:
271 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700272 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700273 }
274 return field_value;
275}
276
Orion Hodson3d617ac2016-10-19 14:00:46 +0100277template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
278 bool transaction_active>
279bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
280 uint16_t inst_data) {
281 const bool do_assignability_check = do_access_check;
282 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
283 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
284 ArtField* f =
285 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
286 Primitive::ComponentSize(field_type));
287 if (UNLIKELY(f == nullptr)) {
288 CHECK(self->IsExceptionPending());
289 return false;
290 }
291 ObjPtr<mirror::Object> obj;
292 if (is_static) {
293 obj = f->GetDeclaringClass();
Chang Xingbd208d82017-07-12 14:53:17 -0700294 if (transaction_active) {
295 if (Runtime::Current()->GetTransaction()->WriteConstraint(obj.Ptr(), f)) {
296 Runtime::Current()->AbortTransactionAndThrowAbortError(
297 self, "Can't set fields of " + obj->PrettyTypeOf());
298 return false;
299 }
300 }
301
Orion Hodson3d617ac2016-10-19 14:00:46 +0100302 } else {
303 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
304 if (UNLIKELY(obj == nullptr)) {
305 ThrowNullPointerExceptionForFieldAccess(f, false);
306 return false;
307 }
308 }
309
310 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100311 JValue value = GetFieldValue<field_type>(shadow_frame, vregA);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100312 return DoFieldPutCommon<field_type, do_assignability_check, transaction_active>(self,
313 shadow_frame,
314 obj,
315 f,
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100316 value);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100317}
318
Ian Rogers54874942014-06-10 16:31:03 -0700319// Explicitly instantiate all DoFieldPut functions.
320#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
321 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
322 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
323
324#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
325 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
326 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
327 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
328 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
329
330// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700331EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
332EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
333EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
334EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
335EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
336EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
337EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700338
339// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700340EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
341EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
342EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
343EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
344EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
345EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
346EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700347
348#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
349#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
350
351template<Primitive::Type field_type, bool transaction_active>
352bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700353 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700354 if (UNLIKELY(obj == nullptr)) {
355 // We lost the reference to the field index so we cannot get a more
356 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000357 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700358 return false;
359 }
360 MemberOffset field_offset(inst->VRegC_22c());
361 const uint32_t vregA = inst->VRegA_22c(inst_data);
362 // Report this field modification to instrumentation if needed. Since we only have the offset of
363 // the field from the base of the object, we need to look for it first.
364 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
365 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
366 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
367 field_offset.Uint32Value());
368 DCHECK(f != nullptr);
369 DCHECK(!f->IsStatic());
370 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Alex Light084fa372017-06-16 08:58:34 -0700371 Thread* self = Thread::Current();
372 StackHandleScope<2> hs(self);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700373 // Save obj in case the instrumentation event has thread suspension.
374 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Alex Light084fa372017-06-16 08:58:34 -0700375 mirror::Object* fake_root = nullptr;
376 HandleWrapper<mirror::Object> ret(hs.NewHandleWrapper<mirror::Object>(
377 field_type == Primitive::kPrimNot ? field_value.GetGCRoot() : &fake_root));
378 instrumentation->FieldWriteEvent(self,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700379 obj.Ptr(),
380 shadow_frame.GetMethod(),
381 shadow_frame.GetDexPC(),
382 f,
383 field_value);
Alex Light084fa372017-06-16 08:58:34 -0700384 if (UNLIKELY(self->IsExceptionPending())) {
385 return false;
386 }
Alex Light0aa7a5a2018-10-10 15:58:14 +0000387 if (UNLIKELY(shadow_frame.GetForcePopFrame())) {
388 // Don't actually set the field. The next instruction will force us to pop.
389 DCHECK(Runtime::Current()->AreNonStandardExitsEnabled());
390 DCHECK(PrevFrameWillRetry(self, shadow_frame));
391 return true;
392 }
Ian Rogers54874942014-06-10 16:31:03 -0700393 }
394 // Note: iput-x-quick instructions are only for non-volatile fields.
395 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700396 case Primitive::kPrimBoolean:
397 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
398 break;
399 case Primitive::kPrimByte:
400 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
401 break;
402 case Primitive::kPrimChar:
403 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
404 break;
405 case Primitive::kPrimShort:
406 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
407 break;
Ian Rogers54874942014-06-10 16:31:03 -0700408 case Primitive::kPrimInt:
409 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
410 break;
411 case Primitive::kPrimLong:
412 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
413 break;
414 case Primitive::kPrimNot:
415 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
416 break;
417 default:
418 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700419 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700420 }
421 return true;
422}
423
424// Explicitly instantiate all DoIPutQuick functions.
425#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
426 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
427 const Instruction* inst, \
428 uint16_t inst_data)
429
430#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
431 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
432 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
433
Andreas Gampec8ccf682014-09-29 20:07:43 -0700434EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
435EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
436EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
437EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
438EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
439EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
440EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700441#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
442#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
443
Alex Light9fb1ab12017-09-05 09:32:49 -0700444// We execute any instrumentation events that are triggered by this exception and change the
445// shadow_frame's dex_pc to that of the exception handler if there is one in the current method.
446// Return true if we should continue executing in the current method and false if we need to go up
447// the stack to find an exception handler.
Sebastien Hertz520633b2015-09-08 17:03:36 +0200448// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Alex Light9fb1ab12017-09-05 09:32:49 -0700449// TODO We should have a better way to skip instrumentation reporting or possibly rethink that
450// behavior.
451bool MoveToExceptionHandler(Thread* self,
452 ShadowFrame& shadow_frame,
453 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700454 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700455 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000456 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Alex Light9fb1ab12017-09-05 09:32:49 -0700457 if (instrumentation != nullptr &&
458 instrumentation->HasExceptionThrownListeners() &&
459 self->IsExceptionThrownByCurrentMethod(exception.Get())) {
460 // See b/65049545 for why we don't need to check to see if the exception has changed.
Alex Light6e1607e2017-08-23 10:06:18 -0700461 instrumentation->ExceptionThrownEvent(self, exception.Get());
Alex Light0aa7a5a2018-10-10 15:58:14 +0000462 if (shadow_frame.GetForcePopFrame()) {
463 // We will check in the caller for GetForcePopFrame again. We need to bail out early to
464 // prevent an ExceptionHandledEvent from also being sent before popping.
465 return true;
466 }
Sebastien Hertz9f102032014-05-23 08:59:42 +0200467 }
Ian Rogers54874942014-06-10 16:31:03 -0700468 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700469 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
Alex Light9fb1ab12017-09-05 09:32:49 -0700470 hs.NewHandle(exception->GetClass()), shadow_frame.GetDexPC(), &clear_exception);
Andreas Gampee2abbc62017-09-15 11:59:26 -0700471 if (found_dex_pc == dex::kDexNoIndex) {
Alex Light9fb1ab12017-09-05 09:32:49 -0700472 if (instrumentation != nullptr) {
473 if (shadow_frame.NeedsNotifyPop()) {
474 instrumentation->WatchedFramePopped(self, shadow_frame);
475 }
476 // Exception is not caught by the current method. We will unwind to the
477 // caller. Notify any instrumentation listener.
478 instrumentation->MethodUnwindEvent(self,
479 shadow_frame.GetThisObject(),
480 shadow_frame.GetMethod(),
481 shadow_frame.GetDexPC());
Alex Lighte814f9d2017-07-31 16:14:39 -0700482 }
Alex Light9fb1ab12017-09-05 09:32:49 -0700483 return false;
Ian Rogers54874942014-06-10 16:31:03 -0700484 } else {
Alex Light9fb1ab12017-09-05 09:32:49 -0700485 shadow_frame.SetDexPC(found_dex_pc);
486 if (instrumentation != nullptr && instrumentation->HasExceptionHandledListeners()) {
487 self->ClearException();
488 instrumentation->ExceptionHandledEvent(self, exception.Get());
489 if (UNLIKELY(self->IsExceptionPending())) {
490 // Exception handled event threw an exception. Try to find the handler for this one.
491 return MoveToExceptionHandler(self, shadow_frame, instrumentation);
492 } else if (!clear_exception) {
493 self->SetException(exception.Get());
494 }
495 } else if (clear_exception) {
Ian Rogers54874942014-06-10 16:31:03 -0700496 self->ClearException();
497 }
Alex Light9fb1ab12017-09-05 09:32:49 -0700498 return true;
Ian Rogers54874942014-06-10 16:31:03 -0700499 }
Ian Rogers54874942014-06-10 16:31:03 -0700500}
501
Ian Rogerse94652f2014-12-02 11:13:19 -0800502void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
503 LOG(FATAL) << "Unexpected instruction: "
504 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
505 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700506}
507
Sebastien Hertz45b15972015-04-03 16:07:05 +0200508void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700509 va_list args;
510 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200511 AbortTransactionV(self, fmt, args);
512 va_end(args);
513}
514
515void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
516 CHECK(Runtime::Current()->IsActiveTransaction());
517 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100518 std::string abort_msg;
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800519 android::base::StringAppendV(&abort_msg, fmt, args);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100520 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200521 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700522}
523
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100524// START DECLARATIONS :
525//
526// These additional declarations are required because clang complains
527// about ALWAYS_INLINE (-Werror, -Wgcc-compat) in definitions.
528//
529
Narayan Kamath9823e782016-08-03 12:46:58 +0100530template <bool is_range, bool do_assignability_check>
Vladimir Markod16363a2017-02-01 14:09:13 +0000531static ALWAYS_INLINE bool DoCallCommon(ArtMethod* called_method,
532 Thread* self,
533 ShadowFrame& shadow_frame,
534 JValue* result,
535 uint16_t number_of_inputs,
536 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
537 uint32_t vregC) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100538
539template <bool is_range>
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000540ALWAYS_INLINE void CopyRegisters(ShadowFrame& caller_frame,
541 ShadowFrame* callee_frame,
542 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
543 const size_t first_src_reg,
544 const size_t first_dest_reg,
545 const size_t num_regs) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100546
547// END DECLARATIONS.
548
Siva Chandra05d24152016-01-05 17:43:17 -0800549void ArtInterpreterToCompiledCodeBridge(Thread* self,
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100550 ArtMethod* caller,
Siva Chandra05d24152016-01-05 17:43:17 -0800551 ShadowFrame* shadow_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -0700552 uint16_t arg_offset,
Siva Chandra05d24152016-01-05 17:43:17 -0800553 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700554 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700555 ArtMethod* method = shadow_frame->GetMethod();
556 // Ensure static methods are initialized.
557 if (method->IsStatic()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700558 ObjPtr<mirror::Class> declaringClass = method->GetDeclaringClass();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700559 if (UNLIKELY(!declaringClass->IsInitialized())) {
560 self->PushShadowFrame(shadow_frame);
561 StackHandleScope<1> hs(self);
562 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
Nicolas Geoffray01822292017-03-09 09:03:19 +0000563 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
564 true))) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700565 self->PopShadowFrame();
566 DCHECK(self->IsExceptionPending());
567 return;
568 }
569 self->PopShadowFrame();
570 CHECK(h_class->IsInitializing());
Nicolas Geoffray01822292017-03-09 09:03:19 +0000571 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
572 method = shadow_frame->GetMethod();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700573 }
574 }
Jeff Hao5ea84132017-05-05 16:59:29 -0700575 // Basic checks for the arg_offset. If there's no code item, the arg_offset must be 0. Otherwise,
576 // check that the arg_offset isn't greater than the number of registers. A stronger check is
577 // difficult since the frame may contain space for all the registers in the method, or only enough
578 // space for the arguments.
579 if (kIsDebugBuild) {
580 if (method->GetCodeItem() == nullptr) {
581 DCHECK_EQ(0u, arg_offset) << method->PrettyMethod();
582 } else {
583 DCHECK_LE(arg_offset, shadow_frame->NumberOfVRegs());
584 }
585 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100586 jit::Jit* jit = Runtime::Current()->GetJit();
587 if (jit != nullptr && caller != nullptr) {
588 jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
589 }
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700590 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
591 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
Andreas Gampe542451c2016-07-26 09:02:02 -0700592 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700593}
594
Mingyao Yangffedec52016-05-19 10:48:40 -0700595void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
596 uint16_t this_obj_vreg,
597 JValue result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700598 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700599 ObjPtr<mirror::Object> existing = shadow_frame->GetVRegReference(this_obj_vreg);
Mingyao Yangffedec52016-05-19 10:48:40 -0700600 if (existing == nullptr) {
601 // If it's null, we come from compiled code that was deoptimized. Nothing to do,
602 // as the compiler verified there was no alias.
603 // Set the new string result of the StringFactory.
604 shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
605 return;
606 }
607 // Set the string init result into all aliases.
608 for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
609 if (shadow_frame->GetVRegReference(i) == existing) {
610 DCHECK_EQ(shadow_frame->GetVRegReference(i),
Vladimir Marko78baed52018-10-11 10:44:58 +0100611 reinterpret_cast32<mirror::Object*>(shadow_frame->GetVReg(i)));
Mingyao Yangffedec52016-05-19 10:48:40 -0700612 shadow_frame->SetVRegReference(i, result.GetL());
613 DCHECK_EQ(shadow_frame->GetVRegReference(i),
Vladimir Marko78baed52018-10-11 10:44:58 +0100614 reinterpret_cast32<mirror::Object*>(shadow_frame->GetVReg(i)));
Mingyao Yangffedec52016-05-19 10:48:40 -0700615 }
616 }
617}
618
Orion Hodsonc069a302017-01-18 09:23:12 +0000619template<bool is_range>
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100620static bool DoMethodHandleInvokeCommon(Thread* self,
621 ShadowFrame& shadow_frame,
622 bool invoke_exact,
623 const Instruction* inst,
624 uint16_t inst_data,
625 JValue* result)
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100626 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Light848574c2017-09-25 16:59:39 -0700627 // Make sure to check for async exceptions
628 if (UNLIKELY(self->ObserveAsyncException())) {
629 return false;
630 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100631 // Invoke-polymorphic instructions always take a receiver. i.e, they are never static.
632 const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc();
Orion Hodson3d617ac2016-10-19 14:00:46 +0100633 const int invoke_method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc();
Narayan Kamath9823e782016-08-03 12:46:58 +0100634
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000635 // Initialize |result| to 0 as this is the default return value for
636 // polymorphic invocations of method handle types with void return
637 // and provides sane return result in error cases.
638 result->SetJ(0);
639
Orion Hodson3d617ac2016-10-19 14:00:46 +0100640 // The invoke_method_idx here is the name of the signature polymorphic method that
Narayan Kamath9823e782016-08-03 12:46:58 +0100641 // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact)
642 // and not the method that we'll dispatch to in the end.
Orion Hodsone7732be2017-10-11 14:35:20 +0100643 StackHandleScope<2> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +0000644 Handle<mirror::MethodHandle> method_handle(hs.NewHandle(
645 ObjPtr<mirror::MethodHandle>::DownCast(
Mathieu Chartieref41db72016-10-25 15:08:01 -0700646 MakeObjPtr(shadow_frame.GetVRegReference(vRegC)))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800647 if (UNLIKELY(method_handle == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100648 // Note that the invoke type is kVirtual here because a call to a signature
649 // polymorphic method is shaped like a virtual call at the bytecode level.
Orion Hodson3d617ac2016-10-19 14:00:46 +0100650 ThrowNullPointerExceptionForMethodAccess(invoke_method_idx, InvokeType::kVirtual);
Narayan Kamath9823e782016-08-03 12:46:58 +0100651 return false;
652 }
653
654 // The vRegH value gives the index of the proto_id associated with this
Orion Hodson811bd5f2016-12-07 11:35:37 +0000655 // signature polymorphic call site.
Orion Hodson06d10a72018-05-14 08:53:38 +0100656 const uint16_t vRegH = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
657 const dex::ProtoIndex callsite_proto_id(vRegH);
Narayan Kamath9823e782016-08-03 12:46:58 +0100658
659 // Call through to the classlinker and ask it to resolve the static type associated
660 // with the callsite. This information is stored in the dex cache so it's
661 // guaranteed to be fast after the first resolution.
Narayan Kamath9823e782016-08-03 12:46:58 +0100662 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Orion Hodsone7732be2017-10-11 14:35:20 +0100663 Handle<mirror::MethodType> callsite_type(hs.NewHandle(
664 class_linker->ResolveMethodType(self, callsite_proto_id, shadow_frame.GetMethod())));
Narayan Kamath9823e782016-08-03 12:46:58 +0100665
666 // This implies we couldn't resolve one or more types in this method handle.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800667 if (UNLIKELY(callsite_type == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100668 CHECK(self->IsExceptionPending());
Narayan Kamath9823e782016-08-03 12:46:58 +0100669 return false;
670 }
671
Orion Hodson811bd5f2016-12-07 11:35:37 +0000672 // There is a common dispatch method for method handles that takes
673 // arguments either from a range or an array of arguments depending
674 // on whether the DEX instruction is invoke-polymorphic/range or
675 // invoke-polymorphic. The array here is for the latter.
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100676 if (UNLIKELY(is_range)) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000677 // VRegC is the register holding the method handle. Arguments passed
678 // to the method handle's target do not include the method handle.
Orion Hodson960d4f72017-11-10 15:32:38 +0000679 RangeInstructionOperands operands(inst->VRegC_4rcc() + 1, inst->VRegA_4rcc() - 1);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100680 if (invoke_exact) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000681 return MethodHandleInvokeExact(self,
682 shadow_frame,
683 method_handle,
684 callsite_type,
685 &operands,
686 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100687 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000688 return MethodHandleInvoke(self,
689 shadow_frame,
690 method_handle,
691 callsite_type,
692 &operands,
693 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100694 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100695 } else {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000696 // Get the register arguments for the invoke.
Orion Hodson960d4f72017-11-10 15:32:38 +0000697 uint32_t args[Instruction::kMaxVarArgRegs] = {};
Orion Hodson811bd5f2016-12-07 11:35:37 +0000698 inst->GetVarArgs(args, inst_data);
699 // Drop the first register which is the method handle performing the invoke.
Alexey Frunze8631a462017-01-19 19:07:37 -0800700 memmove(args, args + 1, sizeof(args[0]) * (Instruction::kMaxVarArgRegs - 1));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000701 args[Instruction::kMaxVarArgRegs - 1] = 0;
Orion Hodson960d4f72017-11-10 15:32:38 +0000702 VarArgsInstructionOperands operands(args, inst->VRegA_45cc() - 1);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100703 if (invoke_exact) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000704 return MethodHandleInvokeExact(self,
705 shadow_frame,
706 method_handle,
707 callsite_type,
708 &operands,
709 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100710 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000711 return MethodHandleInvoke(self,
712 shadow_frame,
713 method_handle,
714 callsite_type,
715 &operands,
716 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100717 }
718 }
719}
720
721bool DoMethodHandleInvokeExact(Thread* self,
722 ShadowFrame& shadow_frame,
723 const Instruction* inst,
724 uint16_t inst_data,
725 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
726 if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) {
727 static const bool kIsRange = false;
728 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700729 self, shadow_frame, /* invoke_exact= */ true, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100730 } else {
731 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE);
732 static const bool kIsRange = true;
733 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700734 self, shadow_frame, /* invoke_exact= */ true, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100735 }
736}
737
738bool DoMethodHandleInvoke(Thread* self,
739 ShadowFrame& shadow_frame,
740 const Instruction* inst,
741 uint16_t inst_data,
742 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
743 if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) {
744 static const bool kIsRange = false;
745 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700746 self, shadow_frame, /* invoke_exact= */ false, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100747 } else {
748 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE);
749 static const bool kIsRange = true;
750 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700751 self, shadow_frame, /* invoke_exact= */ false, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100752 }
753}
754
Orion Hodson928033d2018-02-07 05:30:54 +0000755static bool DoVarHandleInvokeCommon(Thread* self,
756 ShadowFrame& shadow_frame,
757 const Instruction* inst,
758 uint16_t inst_data,
759 JValue* result,
760 mirror::VarHandle::AccessMode access_mode)
761 REQUIRES_SHARED(Locks::mutator_lock_) {
762 // Make sure to check for async exceptions
763 if (UNLIKELY(self->ObserveAsyncException())) {
764 return false;
765 }
766
Orion Hodson928033d2018-02-07 05:30:54 +0000767 StackHandleScope<2> hs(self);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100768 bool is_var_args = inst->HasVarArgs();
Orion Hodson06d10a72018-05-14 08:53:38 +0100769 const uint16_t vRegH = is_var_args ? inst->VRegH_45cc() : inst->VRegH_4rcc();
Orion Hodson928033d2018-02-07 05:30:54 +0000770 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
771 Handle<mirror::MethodType> callsite_type(hs.NewHandle(
Orion Hodson06d10a72018-05-14 08:53:38 +0100772 class_linker->ResolveMethodType(self, dex::ProtoIndex(vRegH), shadow_frame.GetMethod())));
Orion Hodson928033d2018-02-07 05:30:54 +0000773 // This implies we couldn't resolve one or more types in this VarHandle.
774 if (UNLIKELY(callsite_type == nullptr)) {
775 CHECK(self->IsExceptionPending());
776 return false;
777 }
778
Orion Hodson537a4fe2018-05-15 13:57:58 +0100779 const uint32_t vRegC = is_var_args ? inst->VRegC_45cc() : inst->VRegC_4rcc();
780 ObjPtr<mirror::Object> receiver(shadow_frame.GetVRegReference(vRegC));
781 Handle<mirror::VarHandle> var_handle(hs.NewHandle(down_cast<mirror::VarHandle*>(receiver.Ptr())));
Orion Hodson928033d2018-02-07 05:30:54 +0000782 if (is_var_args) {
783 uint32_t args[Instruction::kMaxVarArgRegs];
784 inst->GetVarArgs(args, inst_data);
785 VarArgsInstructionOperands all_operands(args, inst->VRegA_45cc());
786 NoReceiverInstructionOperands operands(&all_operands);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100787 return VarHandleInvokeAccessor(self,
788 shadow_frame,
789 var_handle,
790 callsite_type,
791 access_mode,
792 &operands,
793 result);
Orion Hodson928033d2018-02-07 05:30:54 +0000794 } else {
795 RangeInstructionOperands all_operands(inst->VRegC_4rcc(), inst->VRegA_4rcc());
796 NoReceiverInstructionOperands operands(&all_operands);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100797 return VarHandleInvokeAccessor(self,
798 shadow_frame,
799 var_handle,
800 callsite_type,
801 access_mode,
802 &operands,
803 result);
Orion Hodson928033d2018-02-07 05:30:54 +0000804 }
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100805}
806
Orion Hodson928033d2018-02-07 05:30:54 +0000807#define DO_VAR_HANDLE_ACCESSOR(_access_mode) \
808bool DoVarHandle ## _access_mode(Thread* self, \
809 ShadowFrame& shadow_frame, \
810 const Instruction* inst, \
811 uint16_t inst_data, \
812 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { \
813 const auto access_mode = mirror::VarHandle::AccessMode::k ## _access_mode; \
814 return DoVarHandleInvokeCommon(self, shadow_frame, inst, inst_data, result, access_mode); \
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100815}
816
Orion Hodson928033d2018-02-07 05:30:54 +0000817DO_VAR_HANDLE_ACCESSOR(CompareAndExchange)
818DO_VAR_HANDLE_ACCESSOR(CompareAndExchangeAcquire)
819DO_VAR_HANDLE_ACCESSOR(CompareAndExchangeRelease)
820DO_VAR_HANDLE_ACCESSOR(CompareAndSet)
821DO_VAR_HANDLE_ACCESSOR(Get)
822DO_VAR_HANDLE_ACCESSOR(GetAcquire)
823DO_VAR_HANDLE_ACCESSOR(GetAndAdd)
824DO_VAR_HANDLE_ACCESSOR(GetAndAddAcquire)
825DO_VAR_HANDLE_ACCESSOR(GetAndAddRelease)
826DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAnd)
827DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAndAcquire)
828DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAndRelease)
829DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOr)
830DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOrAcquire)
831DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOrRelease)
832DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXor)
833DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXorAcquire)
834DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXorRelease)
835DO_VAR_HANDLE_ACCESSOR(GetAndSet)
836DO_VAR_HANDLE_ACCESSOR(GetAndSetAcquire)
837DO_VAR_HANDLE_ACCESSOR(GetAndSetRelease)
838DO_VAR_HANDLE_ACCESSOR(GetOpaque)
839DO_VAR_HANDLE_ACCESSOR(GetVolatile)
840DO_VAR_HANDLE_ACCESSOR(Set)
841DO_VAR_HANDLE_ACCESSOR(SetOpaque)
842DO_VAR_HANDLE_ACCESSOR(SetRelease)
843DO_VAR_HANDLE_ACCESSOR(SetVolatile)
844DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSet)
845DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetAcquire)
846DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetPlain)
847DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetRelease)
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100848
Orion Hodson928033d2018-02-07 05:30:54 +0000849#undef DO_VAR_HANDLE_ACCESSOR
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100850
851template<bool is_range>
852bool DoInvokePolymorphic(Thread* self,
853 ShadowFrame& shadow_frame,
854 const Instruction* inst,
855 uint16_t inst_data,
856 JValue* result) {
857 const int invoke_method_idx = inst->VRegB();
858 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
859 ArtMethod* invoke_method =
860 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
861 self, invoke_method_idx, shadow_frame.GetMethod(), kVirtual);
862
863 // Ensure intrinsic identifiers are initialized.
864 DCHECK(invoke_method->IsIntrinsic());
865
866 // Dispatch based on intrinsic identifier associated with method.
867 switch (static_cast<art::Intrinsics>(invoke_method->GetIntrinsic())) {
868#define CASE_SIGNATURE_POLYMORPHIC_INTRINSIC(Name, ...) \
869 case Intrinsics::k##Name: \
870 return Do ## Name(self, shadow_frame, inst, inst_data, result);
871#include "intrinsics_list.h"
872 SIGNATURE_POLYMORPHIC_INTRINSICS_LIST(CASE_SIGNATURE_POLYMORPHIC_INTRINSIC)
873#undef INTRINSICS_LIST
874#undef SIGNATURE_POLYMORPHIC_INTRINSICS_LIST
875#undef CASE_SIGNATURE_POLYMORPHIC_INTRINSIC
876 default:
877 LOG(FATAL) << "Unreachable: " << invoke_method->GetIntrinsic();
878 UNREACHABLE();
879 return false;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100880 }
881}
882
Orion Hodsona5dca522018-02-27 12:42:11 +0000883static JValue ConvertScalarBootstrapArgument(jvalue value) {
884 // value either contains a primitive scalar value if it corresponds
885 // to a primitive type, or it contains an integer value if it
886 // corresponds to an object instance reference id (e.g. a string id).
887 return JValue::FromPrimitive(value.j);
888}
889
890static ObjPtr<mirror::Class> GetClassForBootstrapArgument(EncodedArrayValueIterator::ValueType type)
891 REQUIRES_SHARED(Locks::mutator_lock_) {
892 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100893 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = class_linker->GetClassRoots();
Orion Hodsona5dca522018-02-27 12:42:11 +0000894 switch (type) {
895 case EncodedArrayValueIterator::ValueType::kBoolean:
896 case EncodedArrayValueIterator::ValueType::kByte:
897 case EncodedArrayValueIterator::ValueType::kChar:
898 case EncodedArrayValueIterator::ValueType::kShort:
899 // These types are disallowed by JVMS. Treat as integers. This
900 // will result in CCE's being raised if the BSM has one of these
901 // types.
902 case EncodedArrayValueIterator::ValueType::kInt:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100903 return GetClassRoot(ClassRoot::kPrimitiveInt, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000904 case EncodedArrayValueIterator::ValueType::kLong:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100905 return GetClassRoot(ClassRoot::kPrimitiveLong, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000906 case EncodedArrayValueIterator::ValueType::kFloat:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100907 return GetClassRoot(ClassRoot::kPrimitiveFloat, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000908 case EncodedArrayValueIterator::ValueType::kDouble:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100909 return GetClassRoot(ClassRoot::kPrimitiveDouble, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000910 case EncodedArrayValueIterator::ValueType::kMethodType:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100911 return GetClassRoot<mirror::MethodType>(class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000912 case EncodedArrayValueIterator::ValueType::kMethodHandle:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100913 return GetClassRoot<mirror::MethodHandle>(class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000914 case EncodedArrayValueIterator::ValueType::kString:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100915 return GetClassRoot<mirror::String>();
Orion Hodsona5dca522018-02-27 12:42:11 +0000916 case EncodedArrayValueIterator::ValueType::kType:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100917 return GetClassRoot<mirror::Class>();
Orion Hodsona5dca522018-02-27 12:42:11 +0000918 case EncodedArrayValueIterator::ValueType::kField:
919 case EncodedArrayValueIterator::ValueType::kMethod:
920 case EncodedArrayValueIterator::ValueType::kEnum:
921 case EncodedArrayValueIterator::ValueType::kArray:
922 case EncodedArrayValueIterator::ValueType::kAnnotation:
923 case EncodedArrayValueIterator::ValueType::kNull:
924 return nullptr;
925 }
926}
927
928static bool GetArgumentForBootstrapMethod(Thread* self,
929 ArtMethod* referrer,
930 EncodedArrayValueIterator::ValueType type,
931 const JValue* encoded_value,
932 JValue* decoded_value)
933 REQUIRES_SHARED(Locks::mutator_lock_) {
934 // The encoded_value contains either a scalar value (IJDF) or a
935 // scalar DEX file index to a reference type to be materialized.
936 switch (type) {
937 case EncodedArrayValueIterator::ValueType::kInt:
938 case EncodedArrayValueIterator::ValueType::kFloat:
939 decoded_value->SetI(encoded_value->GetI());
940 return true;
941 case EncodedArrayValueIterator::ValueType::kLong:
942 case EncodedArrayValueIterator::ValueType::kDouble:
943 decoded_value->SetJ(encoded_value->GetJ());
944 return true;
945 case EncodedArrayValueIterator::ValueType::kMethodType: {
946 StackHandleScope<2> hs(self);
947 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader()));
948 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
Orion Hodson06d10a72018-05-14 08:53:38 +0100949 dex::ProtoIndex proto_idx(encoded_value->GetC());
Orion Hodsona5dca522018-02-27 12:42:11 +0000950 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Orion Hodson06d10a72018-05-14 08:53:38 +0100951 ObjPtr<mirror::MethodType> o =
952 cl->ResolveMethodType(self, proto_idx, dex_cache, class_loader);
Orion Hodsona5dca522018-02-27 12:42:11 +0000953 if (UNLIKELY(o.IsNull())) {
954 DCHECK(self->IsExceptionPending());
955 return false;
956 }
957 decoded_value->SetL(o);
958 return true;
959 }
960 case EncodedArrayValueIterator::ValueType::kMethodHandle: {
961 uint32_t index = static_cast<uint32_t>(encoded_value->GetI());
962 ClassLinker* cl = Runtime::Current()->GetClassLinker();
963 ObjPtr<mirror::MethodHandle> o = cl->ResolveMethodHandle(self, index, referrer);
964 if (UNLIKELY(o.IsNull())) {
965 DCHECK(self->IsExceptionPending());
966 return false;
967 }
968 decoded_value->SetL(o);
969 return true;
970 }
971 case EncodedArrayValueIterator::ValueType::kString: {
Orion Hodsona5dca522018-02-27 12:42:11 +0000972 dex::StringIndex index(static_cast<uint32_t>(encoded_value->GetI()));
973 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Vladimir Marko18090d12018-06-01 16:53:12 +0100974 ObjPtr<mirror::String> o = cl->ResolveString(index, referrer);
Orion Hodsona5dca522018-02-27 12:42:11 +0000975 if (UNLIKELY(o.IsNull())) {
976 DCHECK(self->IsExceptionPending());
977 return false;
978 }
979 decoded_value->SetL(o);
980 return true;
981 }
982 case EncodedArrayValueIterator::ValueType::kType: {
Orion Hodsona5dca522018-02-27 12:42:11 +0000983 dex::TypeIndex index(static_cast<uint32_t>(encoded_value->GetI()));
984 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100985 ObjPtr<mirror::Class> o = cl->ResolveType(index, referrer);
Orion Hodsona5dca522018-02-27 12:42:11 +0000986 if (UNLIKELY(o.IsNull())) {
987 DCHECK(self->IsExceptionPending());
988 return false;
989 }
990 decoded_value->SetL(o);
991 return true;
992 }
993 case EncodedArrayValueIterator::ValueType::kBoolean:
994 case EncodedArrayValueIterator::ValueType::kByte:
995 case EncodedArrayValueIterator::ValueType::kChar:
996 case EncodedArrayValueIterator::ValueType::kShort:
997 case EncodedArrayValueIterator::ValueType::kField:
998 case EncodedArrayValueIterator::ValueType::kMethod:
999 case EncodedArrayValueIterator::ValueType::kEnum:
1000 case EncodedArrayValueIterator::ValueType::kArray:
1001 case EncodedArrayValueIterator::ValueType::kAnnotation:
1002 case EncodedArrayValueIterator::ValueType::kNull:
1003 // Unreachable - unsupported types that have been checked when
1004 // determining the effect call site type based on the bootstrap
1005 // argument types.
1006 UNREACHABLE();
1007 }
1008}
1009
1010static bool PackArgumentForBootstrapMethod(Thread* self,
1011 ArtMethod* referrer,
1012 CallSiteArrayValueIterator* it,
1013 ShadowFrameSetter* setter)
1014 REQUIRES_SHARED(Locks::mutator_lock_) {
1015 auto type = it->GetValueType();
1016 const JValue encoded_value = ConvertScalarBootstrapArgument(it->GetJavaValue());
1017 JValue decoded_value;
1018 if (!GetArgumentForBootstrapMethod(self, referrer, type, &encoded_value, &decoded_value)) {
1019 return false;
1020 }
1021 switch (it->GetValueType()) {
1022 case EncodedArrayValueIterator::ValueType::kInt:
1023 case EncodedArrayValueIterator::ValueType::kFloat:
1024 setter->Set(static_cast<uint32_t>(decoded_value.GetI()));
1025 return true;
1026 case EncodedArrayValueIterator::ValueType::kLong:
1027 case EncodedArrayValueIterator::ValueType::kDouble:
1028 setter->SetLong(decoded_value.GetJ());
1029 return true;
1030 case EncodedArrayValueIterator::ValueType::kMethodType:
1031 case EncodedArrayValueIterator::ValueType::kMethodHandle:
1032 case EncodedArrayValueIterator::ValueType::kString:
1033 case EncodedArrayValueIterator::ValueType::kType:
1034 setter->SetReference(decoded_value.GetL());
1035 return true;
1036 case EncodedArrayValueIterator::ValueType::kBoolean:
1037 case EncodedArrayValueIterator::ValueType::kByte:
1038 case EncodedArrayValueIterator::ValueType::kChar:
1039 case EncodedArrayValueIterator::ValueType::kShort:
1040 case EncodedArrayValueIterator::ValueType::kField:
1041 case EncodedArrayValueIterator::ValueType::kMethod:
1042 case EncodedArrayValueIterator::ValueType::kEnum:
1043 case EncodedArrayValueIterator::ValueType::kArray:
1044 case EncodedArrayValueIterator::ValueType::kAnnotation:
1045 case EncodedArrayValueIterator::ValueType::kNull:
1046 // Unreachable - unsupported types that have been checked when
1047 // determining the effect call site type based on the bootstrap
1048 // argument types.
1049 UNREACHABLE();
1050 }
1051}
1052
1053static bool PackCollectorArrayForBootstrapMethod(Thread* self,
1054 ArtMethod* referrer,
1055 ObjPtr<mirror::Class> array_type,
1056 int32_t array_length,
1057 CallSiteArrayValueIterator* it,
1058 ShadowFrameSetter* setter)
1059 REQUIRES_SHARED(Locks::mutator_lock_) {
1060 StackHandleScope<1> hs(self);
1061 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1062 JValue decoded_value;
1063
1064#define COLLECT_PRIMITIVE_ARRAY(Descriptor, Type) \
1065 Handle<mirror::Type ## Array> array = \
1066 hs.NewHandle(mirror::Type ## Array::Alloc(self, array_length)); \
1067 if (array.IsNull()) { \
1068 return false; \
1069 } \
1070 for (int32_t i = 0; it->HasNext(); it->Next(), ++i) { \
1071 auto type = it->GetValueType(); \
1072 DCHECK_EQ(type, EncodedArrayValueIterator::ValueType::k ## Type); \
1073 const JValue encoded_value = \
1074 ConvertScalarBootstrapArgument(it->GetJavaValue()); \
1075 GetArgumentForBootstrapMethod(self, \
1076 referrer, \
1077 type, \
1078 &encoded_value, \
1079 &decoded_value); \
1080 array->Set(i, decoded_value.Get ## Descriptor()); \
1081 } \
1082 setter->SetReference(array.Get()); \
1083 return true;
1084
1085#define COLLECT_REFERENCE_ARRAY(T, Type) \
Andreas Gampe584771b2018-10-18 13:22:23 -07001086 Handle<mirror::ObjectArray<T>> array = /* NOLINT */ \
Orion Hodsona5dca522018-02-27 12:42:11 +00001087 hs.NewHandle(mirror::ObjectArray<T>::Alloc(self, \
1088 array_type, \
1089 array_length)); \
1090 if (array.IsNull()) { \
1091 return false; \
1092 } \
1093 for (int32_t i = 0; it->HasNext(); it->Next(), ++i) { \
1094 auto type = it->GetValueType(); \
1095 DCHECK_EQ(type, EncodedArrayValueIterator::ValueType::k ## Type); \
1096 const JValue encoded_value = \
1097 ConvertScalarBootstrapArgument(it->GetJavaValue()); \
1098 if (!GetArgumentForBootstrapMethod(self, \
1099 referrer, \
1100 type, \
1101 &encoded_value, \
1102 &decoded_value)) { \
1103 return false; \
1104 } \
1105 ObjPtr<mirror::Object> o = decoded_value.GetL(); \
1106 if (Runtime::Current()->IsActiveTransaction()) { \
1107 array->Set<true>(i, ObjPtr<T>::DownCast(o)); \
1108 } else { \
1109 array->Set<false>(i, ObjPtr<T>::DownCast(o)); \
1110 } \
1111 } \
1112 setter->SetReference(array.Get()); \
1113 return true;
1114
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001115 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = class_linker->GetClassRoots();
1116 ObjPtr<mirror::Class> component_type = array_type->GetComponentType();
1117 if (component_type == GetClassRoot(ClassRoot::kPrimitiveInt, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001118 COLLECT_PRIMITIVE_ARRAY(I, Int);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001119 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveLong, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001120 COLLECT_PRIMITIVE_ARRAY(J, Long);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001121 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveFloat, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001122 COLLECT_PRIMITIVE_ARRAY(F, Float);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001123 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveDouble, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001124 COLLECT_PRIMITIVE_ARRAY(D, Double);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001125 } else if (component_type == GetClassRoot<mirror::MethodType>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001126 COLLECT_REFERENCE_ARRAY(mirror::MethodType, MethodType);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001127 } else if (component_type == GetClassRoot<mirror::MethodHandle>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001128 COLLECT_REFERENCE_ARRAY(mirror::MethodHandle, MethodHandle);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001129 } else if (component_type == GetClassRoot<mirror::String>(class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001130 COLLECT_REFERENCE_ARRAY(mirror::String, String);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001131 } else if (component_type == GetClassRoot<mirror::Class>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001132 COLLECT_REFERENCE_ARRAY(mirror::Class, Type);
1133 } else {
1134 UNREACHABLE();
1135 }
1136 #undef COLLECT_PRIMITIVE_ARRAY
1137 #undef COLLECT_REFERENCE_ARRAY
1138}
1139
1140static ObjPtr<mirror::MethodType> BuildCallSiteForBootstrapMethod(Thread* self,
1141 const DexFile* dex_file,
1142 uint32_t call_site_idx)
1143 REQUIRES_SHARED(Locks::mutator_lock_) {
1144 const DexFile::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx);
1145 CallSiteArrayValueIterator it(*dex_file, csi);
1146 DCHECK_GE(it.Size(), 1u);
1147
1148 StackHandleScope<2> hs(self);
1149 // Create array for parameter types.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001150 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001151 ObjPtr<mirror::Class> class_array_type =
1152 GetClassRoot<mirror::ObjectArray<mirror::Class>>(class_linker);
Orion Hodsona5dca522018-02-27 12:42:11 +00001153 Handle<mirror::ObjectArray<mirror::Class>> ptypes = hs.NewHandle(
1154 mirror::ObjectArray<mirror::Class>::Alloc(self,
1155 class_array_type,
1156 static_cast<int>(it.Size())));
1157 if (ptypes.IsNull()) {
1158 DCHECK(self->IsExceptionPending());
1159 return nullptr;
1160 }
1161
1162 // Populate the first argument with an instance of j.l.i.MethodHandles.Lookup
1163 // that the runtime will construct.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001164 ptypes->Set(0, GetClassRoot<mirror::MethodHandlesLookup>(class_linker));
Orion Hodsona5dca522018-02-27 12:42:11 +00001165 it.Next();
1166
1167 // The remaining parameter types are derived from the types of
1168 // arguments present in the DEX file.
1169 int index = 1;
1170 while (it.HasNext()) {
1171 ObjPtr<mirror::Class> ptype = GetClassForBootstrapArgument(it.GetValueType());
1172 if (ptype.IsNull()) {
1173 ThrowClassCastException("Unsupported bootstrap argument type");
1174 return nullptr;
1175 }
1176 ptypes->Set(index, ptype);
1177 index++;
1178 it.Next();
1179 }
1180 DCHECK_EQ(static_cast<size_t>(index), it.Size());
1181
1182 // By definition, the return type is always a j.l.i.CallSite.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001183 Handle<mirror::Class> rtype = hs.NewHandle(GetClassRoot<mirror::CallSite>());
Orion Hodsona5dca522018-02-27 12:42:11 +00001184 return mirror::MethodType::Create(self, rtype, ptypes);
1185}
1186
Orion Hodsonc069a302017-01-18 09:23:12 +00001187static ObjPtr<mirror::CallSite> InvokeBootstrapMethod(Thread* self,
1188 ShadowFrame& shadow_frame,
1189 uint32_t call_site_idx)
1190 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001191 StackHandleScope<5> hs(self);
Orion Hodsona5dca522018-02-27 12:42:11 +00001192 // There are three mandatory arguments expected from the call site
1193 // value array in the DEX file: the bootstrap method handle, the
1194 // method name to pass to the bootstrap method, and the method type
1195 // to pass to the bootstrap method.
1196 static constexpr size_t kMandatoryArgumentsCount = 3;
Orion Hodsonc069a302017-01-18 09:23:12 +00001197 ArtMethod* referrer = shadow_frame.GetMethod();
1198 const DexFile* dex_file = referrer->GetDexFile();
1199 const DexFile::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00001200 CallSiteArrayValueIterator it(*dex_file, csi);
Orion Hodsona5dca522018-02-27 12:42:11 +00001201 if (it.Size() < kMandatoryArgumentsCount) {
1202 ThrowBootstrapMethodError("Truncated bootstrap arguments (%zu < %zu)",
1203 it.Size(), kMandatoryArgumentsCount);
1204 return nullptr;
1205 }
1206
1207 if (it.GetValueType() != EncodedArrayValueIterator::ValueType::kMethodHandle) {
1208 ThrowBootstrapMethodError("First bootstrap argument is not a method handle");
1209 return nullptr;
1210 }
1211
1212 uint32_t bsm_index = static_cast<uint32_t>(it.GetJavaValue().i);
1213 it.Next();
1214
Orion Hodsonc069a302017-01-18 09:23:12 +00001215 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Orion Hodsona5dca522018-02-27 12:42:11 +00001216 Handle<mirror::MethodHandle> bsm =
1217 hs.NewHandle(class_linker->ResolveMethodHandle(self, bsm_index, referrer));
1218 if (bsm.IsNull()) {
Orion Hodsonc069a302017-01-18 09:23:12 +00001219 DCHECK(self->IsExceptionPending());
1220 return nullptr;
1221 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001222
Orion Hodsona5dca522018-02-27 12:42:11 +00001223 if (bsm->GetHandleKind() != mirror::MethodHandle::Kind::kInvokeStatic) {
1224 // JLS suggests also accepting constructors. This is currently
1225 // hard as constructor invocations happen via transformers in ART
1226 // today. The constructor would need to be a class derived from java.lang.invoke.CallSite.
1227 ThrowBootstrapMethodError("Unsupported bootstrap method invocation kind");
1228 return nullptr;
1229 }
1230
1231 // Construct the local call site type information based on the 3
1232 // mandatory arguments provided by the runtime and the static arguments
1233 // in the DEX file. We will use these arguments to build a shadow frame.
1234 MutableHandle<mirror::MethodType> call_site_type =
1235 hs.NewHandle(BuildCallSiteForBootstrapMethod(self, dex_file, call_site_idx));
1236 if (call_site_type.IsNull()) {
1237 DCHECK(self->IsExceptionPending());
1238 return nullptr;
1239 }
1240
1241 // Check if this BSM is targeting a variable arity method. If so,
1242 // we'll need to collect the trailing arguments into an array.
1243 Handle<mirror::Array> collector_arguments;
1244 int32_t collector_arguments_length;
1245 if (bsm->GetTargetMethod()->IsVarargs()) {
1246 int number_of_bsm_parameters = bsm->GetMethodType()->GetNumberOfPTypes();
1247 if (number_of_bsm_parameters == 0) {
1248 ThrowBootstrapMethodError("Variable arity BSM does not have any arguments");
1249 return nullptr;
1250 }
1251 Handle<mirror::Class> collector_array_class =
1252 hs.NewHandle(bsm->GetMethodType()->GetPTypes()->Get(number_of_bsm_parameters - 1));
1253 if (!collector_array_class->IsArrayClass()) {
1254 ThrowBootstrapMethodError("Variable arity BSM does not have array as final argument");
1255 return nullptr;
1256 }
1257 // The call site may include no arguments to be collected. In this
1258 // case the number of arguments must be at least the number of BSM
1259 // parameters less the collector array.
1260 if (call_site_type->GetNumberOfPTypes() < number_of_bsm_parameters - 1) {
1261 ThrowWrongMethodTypeException(bsm->GetMethodType(), call_site_type.Get());
1262 return nullptr;
1263 }
1264 // Check all the arguments to be collected match the collector array component type.
1265 for (int i = number_of_bsm_parameters - 1; i < call_site_type->GetNumberOfPTypes(); ++i) {
1266 if (call_site_type->GetPTypes()->Get(i) != collector_array_class->GetComponentType()) {
1267 ThrowClassCastException(collector_array_class->GetComponentType(),
1268 call_site_type->GetPTypes()->Get(i));
1269 return nullptr;
1270 }
1271 }
1272 // Update the call site method type so it now includes the collector array.
1273 int32_t collector_arguments_start = number_of_bsm_parameters - 1;
1274 collector_arguments_length = call_site_type->GetNumberOfPTypes() - number_of_bsm_parameters + 1;
1275 call_site_type.Assign(
1276 mirror::MethodType::CollectTrailingArguments(self,
1277 call_site_type.Get(),
1278 collector_array_class.Get(),
1279 collector_arguments_start));
1280 if (call_site_type.IsNull()) {
1281 DCHECK(self->IsExceptionPending());
1282 return nullptr;
1283 }
1284 } else {
1285 collector_arguments_length = 0;
1286 }
1287
1288 if (call_site_type->GetNumberOfPTypes() != bsm->GetMethodType()->GetNumberOfPTypes()) {
1289 ThrowWrongMethodTypeException(bsm->GetMethodType(), call_site_type.Get());
1290 return nullptr;
1291 }
1292
1293 // BSM invocation has a different set of exceptions that
1294 // j.l.i.MethodHandle.invoke(). Scan arguments looking for CCE
1295 // "opportunities". Unfortunately we cannot just leave this to the
1296 // method handle invocation as this might generate a WMTE.
1297 for (int32_t i = 0; i < call_site_type->GetNumberOfPTypes(); ++i) {
1298 ObjPtr<mirror::Class> from = call_site_type->GetPTypes()->Get(i);
1299 ObjPtr<mirror::Class> to = bsm->GetMethodType()->GetPTypes()->Get(i);
1300 if (!IsParameterTypeConvertible(from, to)) {
1301 ThrowClassCastException(from, to);
1302 return nullptr;
1303 }
1304 }
1305 if (!IsReturnTypeConvertible(call_site_type->GetRType(), bsm->GetMethodType()->GetRType())) {
1306 ThrowClassCastException(bsm->GetMethodType()->GetRType(), call_site_type->GetRType());
1307 return nullptr;
1308 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001309
1310 // Set-up a shadow frame for invoking the bootstrap method handle.
1311 ShadowFrameAllocaUniquePtr bootstrap_frame =
Orion Hodsona5dca522018-02-27 12:42:11 +00001312 CREATE_SHADOW_FRAME(call_site_type->NumberOfVRegs(),
1313 nullptr,
1314 referrer,
1315 shadow_frame.GetDexPC());
Orion Hodsonc069a302017-01-18 09:23:12 +00001316 ScopedStackedShadowFramePusher pusher(
1317 self, bootstrap_frame.get(), StackedShadowFrameType::kShadowFrameUnderConstruction);
Orion Hodsona5dca522018-02-27 12:42:11 +00001318 ShadowFrameSetter setter(bootstrap_frame.get(), 0u);
Orion Hodsonc069a302017-01-18 09:23:12 +00001319
1320 // The first parameter is a MethodHandles lookup instance.
Orion Hodsona5dca522018-02-27 12:42:11 +00001321 Handle<mirror::Class> lookup_class =
1322 hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass());
1323 ObjPtr<mirror::MethodHandlesLookup> lookup =
1324 mirror::MethodHandlesLookup::Create(self, lookup_class);
1325 if (lookup.IsNull()) {
Orion Hodsonc069a302017-01-18 09:23:12 +00001326 DCHECK(self->IsExceptionPending());
1327 return nullptr;
1328 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001329 setter.SetReference(lookup);
Orion Hodsonc069a302017-01-18 09:23:12 +00001330
Orion Hodsona5dca522018-02-27 12:42:11 +00001331 // Pack the remaining arguments into the frame.
1332 int number_of_arguments = call_site_type->GetNumberOfPTypes();
1333 int argument_index;
1334 for (argument_index = 1; argument_index < number_of_arguments; ++argument_index) {
1335 if (argument_index == number_of_arguments - 1 &&
1336 call_site_type->GetPTypes()->Get(argument_index)->IsArrayClass()) {
1337 ObjPtr<mirror::Class> array_type = call_site_type->GetPTypes()->Get(argument_index);
1338 if (!PackCollectorArrayForBootstrapMethod(self,
1339 referrer,
1340 array_type,
1341 collector_arguments_length,
1342 &it,
1343 &setter)) {
1344 DCHECK(self->IsExceptionPending());
1345 return nullptr;
Orion Hodsonc069a302017-01-18 09:23:12 +00001346 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001347 } else if (!PackArgumentForBootstrapMethod(self, referrer, &it, &setter)) {
1348 DCHECK(self->IsExceptionPending());
1349 return nullptr;
Orion Hodsonc069a302017-01-18 09:23:12 +00001350 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001351 it.Next();
1352 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001353 DCHECK(!it.HasNext());
1354 DCHECK(setter.Done());
Orion Hodsonc069a302017-01-18 09:23:12 +00001355
1356 // Invoke the bootstrap method handle.
1357 JValue result;
Orion Hodsona5dca522018-02-27 12:42:11 +00001358 RangeInstructionOperands operands(0, bootstrap_frame->NumberOfVRegs());
1359 bool invoke_success = MethodHandleInvoke(self,
1360 *bootstrap_frame,
1361 bsm,
1362 call_site_type,
1363 &operands,
1364 &result);
Orion Hodsonc069a302017-01-18 09:23:12 +00001365 if (!invoke_success) {
1366 DCHECK(self->IsExceptionPending());
1367 return nullptr;
1368 }
1369
1370 Handle<mirror::Object> object(hs.NewHandle(result.GetL()));
Orion Hodsonc069a302017-01-18 09:23:12 +00001371 if (UNLIKELY(object.IsNull())) {
Orion Hodsonda1cdd02018-01-31 18:08:28 +00001372 // This will typically be for LambdaMetafactory which is not supported.
Orion Hodson76e6adb2018-02-23 13:15:55 +00001373 ThrowClassCastException("Bootstrap method returned null");
Orion Hodsonc069a302017-01-18 09:23:12 +00001374 return nullptr;
1375 }
1376
Orion Hodsona5dca522018-02-27 12:42:11 +00001377 // Check the result type is a subclass of j.l.i.CallSite.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001378 ObjPtr<mirror::Class> call_site_class = GetClassRoot<mirror::CallSite>(class_linker);
1379 if (UNLIKELY(!object->InstanceOf(call_site_class))) {
1380 ThrowClassCastException(object->GetClass(), call_site_class);
Orion Hodsonc069a302017-01-18 09:23:12 +00001381 return nullptr;
1382 }
1383
Orion Hodsona5dca522018-02-27 12:42:11 +00001384 // Check the call site target is not null as we're going to invoke it.
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001385 ObjPtr<mirror::CallSite> call_site =
1386 ObjPtr<mirror::CallSite>::DownCast(ObjPtr<mirror::Object>(result.GetL()));
1387 ObjPtr<mirror::MethodHandle> target = call_site->GetTarget();
1388 if (UNLIKELY(target == nullptr)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001389 ThrowClassCastException("Bootstrap method returned a CallSite with a null target");
Orion Hodsonc069a302017-01-18 09:23:12 +00001390 return nullptr;
1391 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001392 return call_site;
Orion Hodsonc069a302017-01-18 09:23:12 +00001393}
1394
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001395namespace {
1396
1397ObjPtr<mirror::CallSite> DoResolveCallSite(Thread* self,
1398 ShadowFrame& shadow_frame,
1399 uint32_t call_site_idx)
1400 REQUIRES_SHARED(Locks::mutator_lock_) {
1401 StackHandleScope<1> hs(self);
1402 Handle<mirror::DexCache> dex_cache(hs.NewHandle(shadow_frame.GetMethod()->GetDexCache()));
1403
1404 // Get the call site from the DexCache if present.
1405 ObjPtr<mirror::CallSite> call_site = dex_cache->GetResolvedCallSite(call_site_idx);
1406 if (LIKELY(call_site != nullptr)) {
1407 return call_site;
1408 }
1409
1410 // Invoke the bootstrap method to get a candidate call site.
1411 call_site = InvokeBootstrapMethod(self, shadow_frame, call_site_idx);
1412 if (UNLIKELY(call_site == nullptr)) {
1413 if (!self->GetException()->IsError()) {
1414 // Use a BootstrapMethodError if the exception is not an instance of java.lang.Error.
1415 ThrowWrappedBootstrapMethodError("Exception from call site #%u bootstrap method",
1416 call_site_idx);
1417 }
1418 return nullptr;
1419 }
1420
1421 // Attempt to place the candidate call site into the DexCache, return the winning call site.
1422 return dex_cache->SetResolvedCallSite(call_site_idx, call_site);
1423}
1424
1425} // namespace
1426
Orion Hodsonc069a302017-01-18 09:23:12 +00001427bool DoInvokeCustom(Thread* self,
1428 ShadowFrame& shadow_frame,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001429 uint32_t call_site_idx,
1430 const InstructionOperands* operands,
1431 JValue* result) {
Alex Light848574c2017-09-25 16:59:39 -07001432 // Make sure to check for async exceptions
1433 if (UNLIKELY(self->ObserveAsyncException())) {
1434 return false;
1435 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001436
Orion Hodsonc069a302017-01-18 09:23:12 +00001437 // invoke-custom is not supported in transactions. In transactions
1438 // there is a limited set of types supported. invoke-custom allows
1439 // running arbitrary code and instantiating arbitrary types.
1440 CHECK(!Runtime::Current()->IsActiveTransaction());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001441
1442 ObjPtr<mirror::CallSite> call_site = DoResolveCallSite(self, shadow_frame, call_site_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00001443 if (call_site.IsNull()) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001444 DCHECK(self->IsExceptionPending());
1445 return false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001446 }
1447
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001448 StackHandleScope<2> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +00001449 Handle<mirror::MethodHandle> target = hs.NewHandle(call_site->GetTarget());
1450 Handle<mirror::MethodType> target_method_type = hs.NewHandle(target->GetMethodType());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001451 DCHECK_EQ(operands->GetNumberOfOperands(), target_method_type->NumberOfVRegs())
1452 << " call_site_idx" << call_site_idx;
1453 return MethodHandleInvokeExact(self,
1454 shadow_frame,
1455 target,
1456 target_method_type,
1457 operands,
1458 result);
Orion Hodsonc069a302017-01-18 09:23:12 +00001459}
1460
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001461// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
1462static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
1463 size_t dest_reg, size_t src_reg)
1464 REQUIRES_SHARED(Locks::mutator_lock_) {
1465 // Uint required, so that sign extension does not make this wrong on 64b systems
1466 uint32_t src_value = shadow_frame.GetVReg(src_reg);
1467 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
1468
1469 // If both register locations contains the same value, the register probably holds a reference.
1470 // Note: As an optimization, non-moving collectors leave a stale reference value
1471 // in the references array even after the original vreg was overwritten to a non-reference.
Vladimir Marko78baed52018-10-11 10:44:58 +01001472 if (src_value == reinterpret_cast32<uint32_t>(o.Ptr())) {
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001473 new_shadow_frame->SetVRegReference(dest_reg, o);
1474 } else {
1475 new_shadow_frame->SetVReg(dest_reg, src_value);
1476 }
1477}
1478
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001479template <bool is_range>
1480inline void CopyRegisters(ShadowFrame& caller_frame,
1481 ShadowFrame* callee_frame,
1482 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
1483 const size_t first_src_reg,
1484 const size_t first_dest_reg,
1485 const size_t num_regs) {
1486 if (is_range) {
1487 const size_t dest_reg_bound = first_dest_reg + num_regs;
1488 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound;
1489 ++dest_reg, ++src_reg) {
1490 AssignRegister(callee_frame, caller_frame, dest_reg, src_reg);
1491 }
1492 } else {
1493 DCHECK_LE(num_regs, arraysize(arg));
1494
1495 for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) {
1496 AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]);
1497 }
1498 }
1499}
1500
Igor Murashkin6918bf12015-09-27 19:19:06 -07001501template <bool is_range,
Narayan Kamath370423d2016-10-03 16:51:22 +01001502 bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001503static inline bool DoCallCommon(ArtMethod* called_method,
1504 Thread* self,
1505 ShadowFrame& shadow_frame,
1506 JValue* result,
1507 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +01001508 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -07001509 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001510 bool string_init = false;
1511 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001512 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
1513 && called_method->IsConstructor())) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001514 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
Jeff Hao848f70a2014-01-15 13:49:50 -08001515 string_init = true;
1516 }
1517
Alex Lightdaf58c82016-03-16 23:00:49 +00001518 // Compute method information.
David Sehr0225f8e2018-01-31 08:52:24 +00001519 CodeItemDataAccessor accessor(called_method->DexInstructionData());
Igor Murashkin158f35c2015-06-10 15:55:30 -07001520 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001521 uint16_t num_regs;
Jeff Hao5ea84132017-05-05 16:59:29 -07001522 // Test whether to use the interpreter or compiler entrypoint, and save that result to pass to
1523 // PerformCall. A deoptimization could occur at any time, and we shouldn't change which
1524 // entrypoint to use once we start building the shadow frame.
Mathieu Chartier448bbcf2017-07-06 12:05:13 -07001525
1526 // For unstarted runtimes, always use the interpreter entrypoint. This fixes the case where we are
1527 // doing cross compilation. Note that GetEntryPointFromQuickCompiledCode doesn't use the image
1528 // pointer size here and this may case an overflow if it is called from the compiler. b/62402160
1529 const bool use_interpreter_entrypoint = !Runtime::Current()->IsStarted() ||
1530 ClassLinker::ShouldUseInterpreterEntrypoint(
1531 called_method,
1532 called_method->GetEntryPointFromQuickCompiledCode());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001533 if (LIKELY(accessor.HasCodeItem())) {
Jeff Hao5ea84132017-05-05 16:59:29 -07001534 // When transitioning to compiled code, space only needs to be reserved for the input registers.
1535 // The rest of the frame gets discarded. This also prevents accessing the called method's code
1536 // item, saving memory by keeping code items of compiled code untouched.
Mathieu Chartier448bbcf2017-07-06 12:05:13 -07001537 if (!use_interpreter_entrypoint) {
1538 DCHECK(!Runtime::Current()->IsAotCompiler()) << "Compiler should use interpreter entrypoint";
Jeff Hao5ea84132017-05-05 16:59:29 -07001539 num_regs = number_of_inputs;
1540 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001541 num_regs = accessor.RegistersSize();
1542 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, accessor.InsSize());
Jeff Hao5ea84132017-05-05 16:59:29 -07001543 }
Nicolas Geoffray01822292017-03-09 09:03:19 +00001544 } else {
1545 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1546 num_regs = number_of_inputs;
Jeff Haodf79ddb2017-02-27 14:47:06 -08001547 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001548
Igor Murashkin158f35c2015-06-10 15:55:30 -07001549 // Hack for String init:
1550 //
1551 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
1552 // invoke-x StringFactory(a, b, c, ...)
1553 // by effectively dropping the first virtual register from the invoke.
1554 //
1555 // (at this point the ArtMethod has already been replaced,
1556 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +00001557 //
1558 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
1559 // to handle the compiler optimization of replacing `this` with null without
1560 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001561 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -07001562 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001563 DCHECK_GT(num_regs, 0u); // As the method is an instance method, there should be at least 1.
Igor Murashkina06b49b2015-06-25 15:18:12 -07001564
Igor Murashkin158f35c2015-06-10 15:55:30 -07001565 // The new StringFactory call is static and has one fewer argument.
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001566 if (!accessor.HasCodeItem()) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001567 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1568 num_regs--;
1569 } // else ... don't need to change num_regs since it comes up from the string_init's code item
Igor Murashkin158f35c2015-06-10 15:55:30 -07001570 number_of_inputs--;
1571
1572 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -07001573 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001574 arg[i - 1] = arg[i];
1575 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07001576 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001577
1578 // Rewrite the non-var-arg case
1579 vregC++; // Skips the 0th vreg in the range ("this").
1580 }
1581
1582 // Parameter registers go at the end of the shadow frame.
1583 DCHECK_GE(num_regs, number_of_inputs);
1584 size_t first_dest_reg = num_regs - number_of_inputs;
1585 DCHECK_NE(first_dest_reg, (size_t)-1);
1586
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001587 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001588 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -07001589 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -07001590 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -07001591 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001592
Igor Murashkin158f35c2015-06-10 15:55:30 -07001593 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -07001594 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001595 // Slow path.
1596 // We might need to do class loading, which incurs a thread state change to kNative. So
1597 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07001598 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +02001599 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001600 self->EndAssertNoThreadSuspension(old_cause);
1601
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001602 // ArtMethod here is needed to check type information of the call site against the callee.
1603 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
1604 //
1605 // As a special case for proxy methods, which are not dex-backed,
1606 // we have to retrieve type information from the proxy's method
1607 // interface method instead (which is dex backed since proxies are never interfaces).
Andreas Gampe542451c2016-07-26 09:02:02 -07001608 ArtMethod* method =
1609 new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001610
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001611 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001612 // to get the exact type of each reference argument.
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001613 const DexFile::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001614 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001615 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001616
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001617 // Handle receiver apart since it's not part of the shorty.
1618 size_t dest_reg = first_dest_reg;
1619 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001620
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001621 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001622 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001623 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
1624 ++dest_reg;
1625 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -07001626 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001627 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001628
1629 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001630 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001631 // Skip the 0th 'shorty' type since it represents the return type.
1632 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001633 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
1634 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001635 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001636 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001637 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001638 if (do_assignability_check && o != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001639 const dex::TypeIndex type_idx = params->GetTypeItem(shorty_pos).type_idx_;
Vladimir Marko942fd312017-01-16 20:52:19 +00001640 ObjPtr<mirror::Class> arg_type = method->GetDexCache()->GetResolvedType(type_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001641 if (arg_type == nullptr) {
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001642 StackHandleScope<1> hs(self);
1643 // Preserve o since it is used below and GetClassFromTypeIndex may cause thread
1644 // suspension.
1645 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&o);
Vladimir Markob45528c2017-07-27 14:14:28 +01001646 arg_type = method->ResolveClassFromTypeIndex(type_idx);
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001647 if (arg_type == nullptr) {
1648 CHECK(self->IsExceptionPending());
1649 return false;
1650 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001651 }
1652 if (!o->VerifierInstanceOf(arg_type)) {
1653 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -07001654 std::string temp1, temp2;
Orion Hodsonfef06642016-11-25 16:07:11 +00001655 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001656 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -08001657 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -07001658 o->GetClass()->GetDescriptor(&temp1),
1659 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001660 return false;
1661 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001662 }
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001663 new_shadow_frame->SetVRegReference(dest_reg, o);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001664 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -07001665 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001666 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001667 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001668 uint64_t wide_value =
1669 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
1670 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001671 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001672 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001673 ++dest_reg;
1674 ++arg_offset;
1675 break;
1676 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001677 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001678 default:
1679 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
1680 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001681 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001682 }
1683 } else {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001684 if (is_range) {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001685 DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001686 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001687
1688 CopyRegisters<is_range>(shadow_frame,
1689 new_shadow_frame,
1690 arg,
1691 vregC,
1692 first_dest_reg,
1693 number_of_inputs);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001694 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001695 }
1696
Jeff Hao5ea84132017-05-05 16:59:29 -07001697 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001698 accessor,
Jeff Hao5ea84132017-05-05 16:59:29 -07001699 shadow_frame.GetMethod(),
1700 first_dest_reg,
1701 new_shadow_frame,
1702 result,
1703 use_interpreter_entrypoint);
Jeff Hao848f70a2014-01-15 13:49:50 -08001704
1705 if (string_init && !self->IsExceptionPending()) {
Mingyao Yangffedec52016-05-19 10:48:40 -07001706 SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001707 }
1708
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001709 return !self->IsExceptionPending();
1710}
1711
Igor Murashkin158f35c2015-06-10 15:55:30 -07001712template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001713bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
1714 const Instruction* inst, uint16_t inst_data, JValue* result) {
1715 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001716 const uint16_t number_of_inputs =
1717 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001718
1719 // TODO: find a cleaner way to separate non-range and range information without duplicating
1720 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -07001721 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001722 uint32_t vregC = 0;
1723 if (is_range) {
1724 vregC = inst->VRegC_3rc();
1725 } else {
1726 vregC = inst->VRegC_35c();
1727 inst->GetVarArgs(arg, inst_data);
1728 }
1729
1730 return DoCallCommon<is_range, do_assignability_check>(
1731 called_method, self, shadow_frame,
1732 result, number_of_inputs, arg, vregC);
1733}
1734
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001735template <bool is_range, bool do_access_check, bool transaction_active>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001736bool DoFilledNewArray(const Instruction* inst,
1737 const ShadowFrame& shadow_frame,
1738 Thread* self,
1739 JValue* result) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001740 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
1741 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
1742 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
1743 if (!is_range) {
1744 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
1745 CHECK_LE(length, 5);
1746 }
1747 if (UNLIKELY(length < 0)) {
1748 ThrowNegativeArraySizeException(length);
1749 return false;
1750 }
1751 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08001752 ObjPtr<mirror::Class> array_class = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
Mathieu Chartieref41db72016-10-25 15:08:01 -07001753 shadow_frame.GetMethod(),
1754 self,
1755 false,
1756 do_access_check);
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001757 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001758 DCHECK(self->IsExceptionPending());
1759 return false;
1760 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001761 CHECK(array_class->IsArrayClass());
Mathieu Chartieref41db72016-10-25 15:08:01 -07001762 ObjPtr<mirror::Class> component_class = array_class->GetComponentType();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001763 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
1764 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
1765 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001766 ThrowRuntimeException("Bad filled array request for type %s",
David Sehr709b0702016-10-13 09:12:37 -07001767 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001768 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001769 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -08001770 "Found type %s; filled-new-array not implemented for anything but 'int'",
David Sehr709b0702016-10-13 09:12:37 -07001771 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001772 }
1773 return false;
1774 }
Mathieu Chartieref41db72016-10-25 15:08:01 -07001775 ObjPtr<mirror::Object> new_array = mirror::Array::Alloc<true>(
1776 self,
1777 array_class,
1778 length,
1779 array_class->GetComponentSizeShift(),
1780 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001781 if (UNLIKELY(new_array == nullptr)) {
1782 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001783 return false;
1784 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001785 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
1786 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001787 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +01001788 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001789 } else {
Ian Rogers29a26482014-05-02 15:27:29 -07001790 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +01001791 }
Sebastien Hertzabff6432014-01-27 18:01:39 +01001792 for (int32_t i = 0; i < length; ++i) {
1793 size_t src_reg = is_range ? vregC + i : arg[i];
1794 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001795 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
1796 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +01001797 } else {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001798 new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<transaction_active>(
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001799 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001800 }
1801 }
1802
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001803 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001804 return true;
1805}
1806
Mathieu Chartieref41db72016-10-25 15:08:01 -07001807// TODO: Use ObjPtr here.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001808template<typename T>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001809static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array,
1810 int32_t count)
1811 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001812 Runtime* runtime = Runtime::Current();
1813 for (int32_t i = 0; i < count; ++i) {
1814 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
1815 }
1816}
1817
Mathieu Chartieref41db72016-10-25 15:08:01 -07001818void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001819 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001820 DCHECK(Runtime::Current()->IsActiveTransaction());
1821 DCHECK(array != nullptr);
1822 DCHECK_LE(count, array->GetLength());
1823 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
1824 switch (primitive_component_type) {
1825 case Primitive::kPrimBoolean:
1826 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
1827 break;
1828 case Primitive::kPrimByte:
1829 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
1830 break;
1831 case Primitive::kPrimChar:
1832 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
1833 break;
1834 case Primitive::kPrimShort:
1835 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
1836 break;
1837 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001838 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
1839 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001840 case Primitive::kPrimFloat:
1841 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
1842 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001843 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001844 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
1845 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001846 case Primitive::kPrimDouble:
1847 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
1848 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001849 default:
1850 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
1851 << " in fill-array-data";
1852 break;
1853 }
1854}
1855
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001856// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +02001857#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001858 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001859 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
1860 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +02001861 const Instruction* inst, uint16_t inst_data, \
1862 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001863EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
1864EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
1865EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
1866EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
1867#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001868
Orion Hodsonc069a302017-01-18 09:23:12 +00001869// Explicit DoInvokePolymorphic template function declarations.
1870#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range) \
1871 template REQUIRES_SHARED(Locks::mutator_lock_) \
1872 bool DoInvokePolymorphic<_is_range>( \
1873 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1874 uint16_t inst_data, JValue* result)
1875EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false);
1876EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true);
Narayan Kamath9823e782016-08-03 12:46:58 +01001877#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1878
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001879// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001880#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001881 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001882 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
1883 const ShadowFrame& shadow_frame, \
1884 Thread* self, JValue* result)
1885#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
1886 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
1887 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
1888 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
1889 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
1890EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
1891EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
1892#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001893#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
1894
1895} // namespace interpreter
1896} // namespace art