blob: 60c8ef34b647870aafd29ce2f234a1fafac1738b [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"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010032#include "mirror/array-inl.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010033#include "mirror/class.h"
Narayan Kamath000e1882016-10-24 17:14:25 +010034#include "mirror/emulated_stack_frame.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080035#include "mirror/method_handle_impl-inl.h"
Orion Hodson928033d2018-02-07 05:30:54 +000036#include "mirror/var_handle.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010037#include "reflection-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070038#include "reflection.h"
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +010039#include "shadow_frame-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070040#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070041#include "thread-inl.h"
Chang Xingbd208d82017-07-12 14:53:17 -070042#include "transaction.h"
Orion Hodson537a4fe2018-05-15 13:57:58 +010043#include "var_handles.h"
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +010044#include "well_known_classes.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020045
46namespace art {
47namespace interpreter {
48
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000049void ThrowNullPointerExceptionFromInterpreter() {
50 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -070051}
52
Chang Xingbd208d82017-07-12 14:53:17 -070053template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
54 bool transaction_active>
Ian Rogers54874942014-06-10 16:31:03 -070055bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
56 uint16_t inst_data) {
57 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
58 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010059 ArtField* f =
60 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
61 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -070062 if (UNLIKELY(f == nullptr)) {
63 CHECK(self->IsExceptionPending());
64 return false;
65 }
Mathieu Chartieref41db72016-10-25 15:08:01 -070066 ObjPtr<mirror::Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -070067 if (is_static) {
68 obj = f->GetDeclaringClass();
Chang Xingbd208d82017-07-12 14:53:17 -070069 if (transaction_active) {
70 if (Runtime::Current()->GetTransaction()->ReadConstraint(obj.Ptr(), f)) {
71 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Can't read static fields of "
72 + obj->PrettyTypeOf() + " since it does not belong to clinit's class.");
73 return false;
74 }
75 }
Ian Rogers54874942014-06-10 16:31:03 -070076 } else {
77 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
78 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000079 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers54874942014-06-10 16:31:03 -070080 return false;
81 }
82 }
Orion Hodson3d617ac2016-10-19 14:00:46 +010083
84 JValue result;
Alex Light084fa372017-06-16 08:58:34 -070085 if (UNLIKELY(!DoFieldGetCommon<field_type>(self, shadow_frame, obj, f, &result))) {
86 // Instrumentation threw an error!
87 CHECK(self->IsExceptionPending());
88 return false;
89 }
Ian Rogers54874942014-06-10 16:31:03 -070090 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
91 switch (field_type) {
92 case Primitive::kPrimBoolean:
Orion Hodson3d617ac2016-10-19 14:00:46 +010093 shadow_frame.SetVReg(vregA, result.GetZ());
Ian Rogers54874942014-06-10 16:31:03 -070094 break;
95 case Primitive::kPrimByte:
Orion Hodson3d617ac2016-10-19 14:00:46 +010096 shadow_frame.SetVReg(vregA, result.GetB());
Ian Rogers54874942014-06-10 16:31:03 -070097 break;
98 case Primitive::kPrimChar:
Orion Hodson3d617ac2016-10-19 14:00:46 +010099 shadow_frame.SetVReg(vregA, result.GetC());
Ian Rogers54874942014-06-10 16:31:03 -0700100 break;
101 case Primitive::kPrimShort:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100102 shadow_frame.SetVReg(vregA, result.GetS());
Ian Rogers54874942014-06-10 16:31:03 -0700103 break;
104 case Primitive::kPrimInt:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100105 shadow_frame.SetVReg(vregA, result.GetI());
Ian Rogers54874942014-06-10 16:31:03 -0700106 break;
107 case Primitive::kPrimLong:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100108 shadow_frame.SetVRegLong(vregA, result.GetJ());
Ian Rogers54874942014-06-10 16:31:03 -0700109 break;
110 case Primitive::kPrimNot:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100111 shadow_frame.SetVRegReference(vregA, result.GetL());
Ian Rogers54874942014-06-10 16:31:03 -0700112 break;
113 default:
114 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700115 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700116 }
117 return true;
118}
119
120// Explicitly instantiate all DoFieldGet functions.
Chang Xingbd208d82017-07-12 14:53:17 -0700121#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
122 template bool DoFieldGet<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
Ian Rogers54874942014-06-10 16:31:03 -0700123 ShadowFrame& shadow_frame, \
124 const Instruction* inst, \
125 uint16_t inst_data)
126
127#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
Chang Xingbd208d82017-07-12 14:53:17 -0700128 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false, true); \
129 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false, false); \
130 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true, true); \
131 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true, false);
Ian Rogers54874942014-06-10 16:31:03 -0700132
133// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700134EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
135EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
136EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
137EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
138EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
139EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
140EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700141
142// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700143EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
144EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
145EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
146EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
147EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
148EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
149EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700150
151#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
152#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
153
154// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
155// Returns true on success, otherwise throws an exception and returns false.
156template<Primitive::Type field_type>
157bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700158 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700159 if (UNLIKELY(obj == nullptr)) {
160 // We lost the reference to the field index so we cannot get a more
161 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000162 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700163 return false;
164 }
165 MemberOffset field_offset(inst->VRegC_22c());
166 // Report this field access to instrumentation if needed. Since we only have the offset of
167 // the field from the base of the object, we need to look for it first.
168 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
169 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
170 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
171 field_offset.Uint32Value());
172 DCHECK(f != nullptr);
173 DCHECK(!f->IsStatic());
Alex Light084fa372017-06-16 08:58:34 -0700174 Thread* self = Thread::Current();
175 StackHandleScope<1> hs(self);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700176 // Save obj in case the instrumentation event has thread suspension.
177 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Alex Light084fa372017-06-16 08:58:34 -0700178 instrumentation->FieldReadEvent(self,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700179 obj.Ptr(),
180 shadow_frame.GetMethod(),
181 shadow_frame.GetDexPC(),
182 f);
Alex Light084fa372017-06-16 08:58:34 -0700183 if (UNLIKELY(self->IsExceptionPending())) {
184 return false;
185 }
Ian Rogers54874942014-06-10 16:31:03 -0700186 }
187 // Note: iget-x-quick instructions are only for non-volatile fields.
188 const uint32_t vregA = inst->VRegA_22c(inst_data);
189 switch (field_type) {
190 case Primitive::kPrimInt:
191 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
192 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800193 case Primitive::kPrimBoolean:
194 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
195 break;
196 case Primitive::kPrimByte:
197 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
198 break;
199 case Primitive::kPrimChar:
200 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
201 break;
202 case Primitive::kPrimShort:
203 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
204 break;
Ian Rogers54874942014-06-10 16:31:03 -0700205 case Primitive::kPrimLong:
206 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
207 break;
208 case Primitive::kPrimNot:
209 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
210 break;
211 default:
212 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700213 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700214 }
215 return true;
216}
217
218// Explicitly instantiate all DoIGetQuick functions.
219#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
220 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
221 uint16_t inst_data)
222
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800223EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
224EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
225EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
226EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
227EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
228EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
229EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700230#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
231
232template<Primitive::Type field_type>
233static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700234 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700235 JValue field_value;
236 switch (field_type) {
237 case Primitive::kPrimBoolean:
238 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
239 break;
240 case Primitive::kPrimByte:
241 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
242 break;
243 case Primitive::kPrimChar:
244 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
245 break;
246 case Primitive::kPrimShort:
247 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
248 break;
249 case Primitive::kPrimInt:
250 field_value.SetI(shadow_frame.GetVReg(vreg));
251 break;
252 case Primitive::kPrimLong:
253 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
254 break;
255 case Primitive::kPrimNot:
256 field_value.SetL(shadow_frame.GetVRegReference(vreg));
257 break;
258 default:
259 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700260 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700261 }
262 return field_value;
263}
264
Orion Hodson3d617ac2016-10-19 14:00:46 +0100265template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
266 bool transaction_active>
267bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
268 uint16_t inst_data) {
269 const bool do_assignability_check = do_access_check;
270 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
271 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
272 ArtField* f =
273 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
274 Primitive::ComponentSize(field_type));
275 if (UNLIKELY(f == nullptr)) {
276 CHECK(self->IsExceptionPending());
277 return false;
278 }
279 ObjPtr<mirror::Object> obj;
280 if (is_static) {
281 obj = f->GetDeclaringClass();
Chang Xingbd208d82017-07-12 14:53:17 -0700282 if (transaction_active) {
283 if (Runtime::Current()->GetTransaction()->WriteConstraint(obj.Ptr(), f)) {
284 Runtime::Current()->AbortTransactionAndThrowAbortError(
285 self, "Can't set fields of " + obj->PrettyTypeOf());
286 return false;
287 }
288 }
289
Orion Hodson3d617ac2016-10-19 14:00:46 +0100290 } else {
291 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
292 if (UNLIKELY(obj == nullptr)) {
293 ThrowNullPointerExceptionForFieldAccess(f, false);
294 return false;
295 }
296 }
297
298 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100299 JValue value = GetFieldValue<field_type>(shadow_frame, vregA);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100300 return DoFieldPutCommon<field_type, do_assignability_check, transaction_active>(self,
301 shadow_frame,
302 obj,
303 f,
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100304 value);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100305}
306
Ian Rogers54874942014-06-10 16:31:03 -0700307// Explicitly instantiate all DoFieldPut functions.
308#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
309 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
310 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
311
312#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
313 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
314 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
315 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
316 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
317
318// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700319EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
320EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
321EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
322EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
323EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
324EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
325EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700326
327// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700328EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
329EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
330EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
331EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
332EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
333EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
334EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700335
336#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
337#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
338
339template<Primitive::Type field_type, bool transaction_active>
340bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700341 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700342 if (UNLIKELY(obj == nullptr)) {
343 // We lost the reference to the field index so we cannot get a more
344 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000345 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700346 return false;
347 }
348 MemberOffset field_offset(inst->VRegC_22c());
349 const uint32_t vregA = inst->VRegA_22c(inst_data);
350 // Report this field modification to instrumentation if needed. Since we only have the offset of
351 // the field from the base of the object, we need to look for it first.
352 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
353 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
354 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
355 field_offset.Uint32Value());
356 DCHECK(f != nullptr);
357 DCHECK(!f->IsStatic());
358 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Alex Light084fa372017-06-16 08:58:34 -0700359 Thread* self = Thread::Current();
360 StackHandleScope<2> hs(self);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700361 // Save obj in case the instrumentation event has thread suspension.
362 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Alex Light084fa372017-06-16 08:58:34 -0700363 mirror::Object* fake_root = nullptr;
364 HandleWrapper<mirror::Object> ret(hs.NewHandleWrapper<mirror::Object>(
365 field_type == Primitive::kPrimNot ? field_value.GetGCRoot() : &fake_root));
366 instrumentation->FieldWriteEvent(self,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700367 obj.Ptr(),
368 shadow_frame.GetMethod(),
369 shadow_frame.GetDexPC(),
370 f,
371 field_value);
Alex Light084fa372017-06-16 08:58:34 -0700372 if (UNLIKELY(self->IsExceptionPending())) {
373 return false;
374 }
Ian Rogers54874942014-06-10 16:31:03 -0700375 }
376 // Note: iput-x-quick instructions are only for non-volatile fields.
377 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700378 case Primitive::kPrimBoolean:
379 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
380 break;
381 case Primitive::kPrimByte:
382 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
383 break;
384 case Primitive::kPrimChar:
385 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
386 break;
387 case Primitive::kPrimShort:
388 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
389 break;
Ian Rogers54874942014-06-10 16:31:03 -0700390 case Primitive::kPrimInt:
391 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
392 break;
393 case Primitive::kPrimLong:
394 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
395 break;
396 case Primitive::kPrimNot:
397 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
398 break;
399 default:
400 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700401 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700402 }
403 return true;
404}
405
406// Explicitly instantiate all DoIPutQuick functions.
407#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
408 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
409 const Instruction* inst, \
410 uint16_t inst_data)
411
412#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
413 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
414 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
415
Andreas Gampec8ccf682014-09-29 20:07:43 -0700416EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
417EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
418EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
419EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
420EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
421EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
422EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700423#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
424#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
425
Alex Light9fb1ab12017-09-05 09:32:49 -0700426// We execute any instrumentation events that are triggered by this exception and change the
427// shadow_frame's dex_pc to that of the exception handler if there is one in the current method.
428// Return true if we should continue executing in the current method and false if we need to go up
429// the stack to find an exception handler.
Sebastien Hertz520633b2015-09-08 17:03:36 +0200430// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Alex Light9fb1ab12017-09-05 09:32:49 -0700431// TODO We should have a better way to skip instrumentation reporting or possibly rethink that
432// behavior.
433bool MoveToExceptionHandler(Thread* self,
434 ShadowFrame& shadow_frame,
435 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700436 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700437 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000438 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Alex Light9fb1ab12017-09-05 09:32:49 -0700439 if (instrumentation != nullptr &&
440 instrumentation->HasExceptionThrownListeners() &&
441 self->IsExceptionThrownByCurrentMethod(exception.Get())) {
442 // 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 -0700443 instrumentation->ExceptionThrownEvent(self, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200444 }
Ian Rogers54874942014-06-10 16:31:03 -0700445 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700446 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
Alex Light9fb1ab12017-09-05 09:32:49 -0700447 hs.NewHandle(exception->GetClass()), shadow_frame.GetDexPC(), &clear_exception);
Andreas Gampee2abbc62017-09-15 11:59:26 -0700448 if (found_dex_pc == dex::kDexNoIndex) {
Alex Light9fb1ab12017-09-05 09:32:49 -0700449 if (instrumentation != nullptr) {
450 if (shadow_frame.NeedsNotifyPop()) {
451 instrumentation->WatchedFramePopped(self, shadow_frame);
452 }
453 // Exception is not caught by the current method. We will unwind to the
454 // caller. Notify any instrumentation listener.
455 instrumentation->MethodUnwindEvent(self,
456 shadow_frame.GetThisObject(),
457 shadow_frame.GetMethod(),
458 shadow_frame.GetDexPC());
Alex Lighte814f9d2017-07-31 16:14:39 -0700459 }
Alex Light9fb1ab12017-09-05 09:32:49 -0700460 return false;
Ian Rogers54874942014-06-10 16:31:03 -0700461 } else {
Alex Light9fb1ab12017-09-05 09:32:49 -0700462 shadow_frame.SetDexPC(found_dex_pc);
463 if (instrumentation != nullptr && instrumentation->HasExceptionHandledListeners()) {
464 self->ClearException();
465 instrumentation->ExceptionHandledEvent(self, exception.Get());
466 if (UNLIKELY(self->IsExceptionPending())) {
467 // Exception handled event threw an exception. Try to find the handler for this one.
468 return MoveToExceptionHandler(self, shadow_frame, instrumentation);
469 } else if (!clear_exception) {
470 self->SetException(exception.Get());
471 }
472 } else if (clear_exception) {
Ian Rogers54874942014-06-10 16:31:03 -0700473 self->ClearException();
474 }
Alex Light9fb1ab12017-09-05 09:32:49 -0700475 return true;
Ian Rogers54874942014-06-10 16:31:03 -0700476 }
Ian Rogers54874942014-06-10 16:31:03 -0700477}
478
Ian Rogerse94652f2014-12-02 11:13:19 -0800479void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
480 LOG(FATAL) << "Unexpected instruction: "
481 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
482 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700483}
484
Sebastien Hertz45b15972015-04-03 16:07:05 +0200485void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700486 va_list args;
487 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200488 AbortTransactionV(self, fmt, args);
489 va_end(args);
490}
491
492void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
493 CHECK(Runtime::Current()->IsActiveTransaction());
494 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100495 std::string abort_msg;
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800496 android::base::StringAppendV(&abort_msg, fmt, args);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100497 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200498 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700499}
500
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100501// START DECLARATIONS :
502//
503// These additional declarations are required because clang complains
504// about ALWAYS_INLINE (-Werror, -Wgcc-compat) in definitions.
505//
506
Narayan Kamath9823e782016-08-03 12:46:58 +0100507template <bool is_range, bool do_assignability_check>
Vladimir Markod16363a2017-02-01 14:09:13 +0000508static ALWAYS_INLINE bool DoCallCommon(ArtMethod* called_method,
509 Thread* self,
510 ShadowFrame& shadow_frame,
511 JValue* result,
512 uint16_t number_of_inputs,
513 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
514 uint32_t vregC) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100515
516template <bool is_range>
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000517ALWAYS_INLINE void CopyRegisters(ShadowFrame& caller_frame,
518 ShadowFrame* callee_frame,
519 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
520 const size_t first_src_reg,
521 const size_t first_dest_reg,
522 const size_t num_regs) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100523
524// END DECLARATIONS.
525
Siva Chandra05d24152016-01-05 17:43:17 -0800526void ArtInterpreterToCompiledCodeBridge(Thread* self,
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100527 ArtMethod* caller,
Siva Chandra05d24152016-01-05 17:43:17 -0800528 ShadowFrame* shadow_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -0700529 uint16_t arg_offset,
Siva Chandra05d24152016-01-05 17:43:17 -0800530 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700531 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700532 ArtMethod* method = shadow_frame->GetMethod();
533 // Ensure static methods are initialized.
534 if (method->IsStatic()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700535 ObjPtr<mirror::Class> declaringClass = method->GetDeclaringClass();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700536 if (UNLIKELY(!declaringClass->IsInitialized())) {
537 self->PushShadowFrame(shadow_frame);
538 StackHandleScope<1> hs(self);
539 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
Nicolas Geoffray01822292017-03-09 09:03:19 +0000540 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
541 true))) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700542 self->PopShadowFrame();
543 DCHECK(self->IsExceptionPending());
544 return;
545 }
546 self->PopShadowFrame();
547 CHECK(h_class->IsInitializing());
Nicolas Geoffray01822292017-03-09 09:03:19 +0000548 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
549 method = shadow_frame->GetMethod();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700550 }
551 }
Jeff Hao5ea84132017-05-05 16:59:29 -0700552 // Basic checks for the arg_offset. If there's no code item, the arg_offset must be 0. Otherwise,
553 // check that the arg_offset isn't greater than the number of registers. A stronger check is
554 // difficult since the frame may contain space for all the registers in the method, or only enough
555 // space for the arguments.
556 if (kIsDebugBuild) {
557 if (method->GetCodeItem() == nullptr) {
558 DCHECK_EQ(0u, arg_offset) << method->PrettyMethod();
559 } else {
560 DCHECK_LE(arg_offset, shadow_frame->NumberOfVRegs());
561 }
562 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100563 jit::Jit* jit = Runtime::Current()->GetJit();
564 if (jit != nullptr && caller != nullptr) {
565 jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
566 }
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700567 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
568 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
Andreas Gampe542451c2016-07-26 09:02:02 -0700569 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700570}
571
Mingyao Yangffedec52016-05-19 10:48:40 -0700572void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
573 uint16_t this_obj_vreg,
574 JValue result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700575 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700576 ObjPtr<mirror::Object> existing = shadow_frame->GetVRegReference(this_obj_vreg);
Mingyao Yangffedec52016-05-19 10:48:40 -0700577 if (existing == nullptr) {
578 // If it's null, we come from compiled code that was deoptimized. Nothing to do,
579 // as the compiler verified there was no alias.
580 // Set the new string result of the StringFactory.
581 shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
582 return;
583 }
584 // Set the string init result into all aliases.
585 for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
586 if (shadow_frame->GetVRegReference(i) == existing) {
587 DCHECK_EQ(shadow_frame->GetVRegReference(i),
Vladimir Marko78baed52018-10-11 10:44:58 +0100588 reinterpret_cast32<mirror::Object*>(shadow_frame->GetVReg(i)));
Mingyao Yangffedec52016-05-19 10:48:40 -0700589 shadow_frame->SetVRegReference(i, result.GetL());
590 DCHECK_EQ(shadow_frame->GetVRegReference(i),
Vladimir Marko78baed52018-10-11 10:44:58 +0100591 reinterpret_cast32<mirror::Object*>(shadow_frame->GetVReg(i)));
Mingyao Yangffedec52016-05-19 10:48:40 -0700592 }
593 }
594}
595
Orion Hodsonc069a302017-01-18 09:23:12 +0000596template<bool is_range>
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100597static bool DoMethodHandleInvokeCommon(Thread* self,
598 ShadowFrame& shadow_frame,
599 bool invoke_exact,
600 const Instruction* inst,
601 uint16_t inst_data,
602 JValue* result)
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100603 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Light848574c2017-09-25 16:59:39 -0700604 // Make sure to check for async exceptions
605 if (UNLIKELY(self->ObserveAsyncException())) {
606 return false;
607 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100608 // Invoke-polymorphic instructions always take a receiver. i.e, they are never static.
609 const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc();
Orion Hodson3d617ac2016-10-19 14:00:46 +0100610 const int invoke_method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc();
Narayan Kamath9823e782016-08-03 12:46:58 +0100611
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000612 // Initialize |result| to 0 as this is the default return value for
613 // polymorphic invocations of method handle types with void return
614 // and provides sane return result in error cases.
615 result->SetJ(0);
616
Orion Hodson3d617ac2016-10-19 14:00:46 +0100617 // The invoke_method_idx here is the name of the signature polymorphic method that
Narayan Kamath9823e782016-08-03 12:46:58 +0100618 // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact)
619 // and not the method that we'll dispatch to in the end.
Orion Hodsone7732be2017-10-11 14:35:20 +0100620 StackHandleScope<2> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +0000621 Handle<mirror::MethodHandle> method_handle(hs.NewHandle(
622 ObjPtr<mirror::MethodHandle>::DownCast(
Mathieu Chartieref41db72016-10-25 15:08:01 -0700623 MakeObjPtr(shadow_frame.GetVRegReference(vRegC)))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800624 if (UNLIKELY(method_handle == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100625 // Note that the invoke type is kVirtual here because a call to a signature
626 // polymorphic method is shaped like a virtual call at the bytecode level.
Orion Hodson3d617ac2016-10-19 14:00:46 +0100627 ThrowNullPointerExceptionForMethodAccess(invoke_method_idx, InvokeType::kVirtual);
Narayan Kamath9823e782016-08-03 12:46:58 +0100628 return false;
629 }
630
631 // The vRegH value gives the index of the proto_id associated with this
Orion Hodson811bd5f2016-12-07 11:35:37 +0000632 // signature polymorphic call site.
Orion Hodson06d10a72018-05-14 08:53:38 +0100633 const uint16_t vRegH = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
634 const dex::ProtoIndex callsite_proto_id(vRegH);
Narayan Kamath9823e782016-08-03 12:46:58 +0100635
636 // Call through to the classlinker and ask it to resolve the static type associated
637 // with the callsite. This information is stored in the dex cache so it's
638 // guaranteed to be fast after the first resolution.
Narayan Kamath9823e782016-08-03 12:46:58 +0100639 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Orion Hodsone7732be2017-10-11 14:35:20 +0100640 Handle<mirror::MethodType> callsite_type(hs.NewHandle(
641 class_linker->ResolveMethodType(self, callsite_proto_id, shadow_frame.GetMethod())));
Narayan Kamath9823e782016-08-03 12:46:58 +0100642
643 // This implies we couldn't resolve one or more types in this method handle.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800644 if (UNLIKELY(callsite_type == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100645 CHECK(self->IsExceptionPending());
Narayan Kamath9823e782016-08-03 12:46:58 +0100646 return false;
647 }
648
Orion Hodson811bd5f2016-12-07 11:35:37 +0000649 // There is a common dispatch method for method handles that takes
650 // arguments either from a range or an array of arguments depending
651 // on whether the DEX instruction is invoke-polymorphic/range or
652 // invoke-polymorphic. The array here is for the latter.
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100653 if (UNLIKELY(is_range)) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000654 // VRegC is the register holding the method handle. Arguments passed
655 // to the method handle's target do not include the method handle.
Orion Hodson960d4f72017-11-10 15:32:38 +0000656 RangeInstructionOperands operands(inst->VRegC_4rcc() + 1, inst->VRegA_4rcc() - 1);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100657 if (invoke_exact) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000658 return MethodHandleInvokeExact(self,
659 shadow_frame,
660 method_handle,
661 callsite_type,
662 &operands,
663 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100664 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000665 return MethodHandleInvoke(self,
666 shadow_frame,
667 method_handle,
668 callsite_type,
669 &operands,
670 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100671 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100672 } else {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000673 // Get the register arguments for the invoke.
Orion Hodson960d4f72017-11-10 15:32:38 +0000674 uint32_t args[Instruction::kMaxVarArgRegs] = {};
Orion Hodson811bd5f2016-12-07 11:35:37 +0000675 inst->GetVarArgs(args, inst_data);
676 // Drop the first register which is the method handle performing the invoke.
Alexey Frunze8631a462017-01-19 19:07:37 -0800677 memmove(args, args + 1, sizeof(args[0]) * (Instruction::kMaxVarArgRegs - 1));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000678 args[Instruction::kMaxVarArgRegs - 1] = 0;
Orion Hodson960d4f72017-11-10 15:32:38 +0000679 VarArgsInstructionOperands operands(args, inst->VRegA_45cc() - 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 }
695 }
696}
697
698bool DoMethodHandleInvokeExact(Thread* self,
699 ShadowFrame& shadow_frame,
700 const Instruction* inst,
701 uint16_t inst_data,
702 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
703 if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) {
704 static const bool kIsRange = false;
705 return DoMethodHandleInvokeCommon<kIsRange>(
706 self, shadow_frame, true /* is_exact */, inst, inst_data, result);
707 } else {
708 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE);
709 static const bool kIsRange = true;
710 return DoMethodHandleInvokeCommon<kIsRange>(
711 self, shadow_frame, true /* is_exact */, inst, inst_data, result);
712 }
713}
714
715bool DoMethodHandleInvoke(Thread* self,
716 ShadowFrame& shadow_frame,
717 const Instruction* inst,
718 uint16_t inst_data,
719 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
720 if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) {
721 static const bool kIsRange = false;
722 return DoMethodHandleInvokeCommon<kIsRange>(
723 self, shadow_frame, false /* is_exact */, inst, inst_data, result);
724 } else {
725 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE);
726 static const bool kIsRange = true;
727 return DoMethodHandleInvokeCommon<kIsRange>(
728 self, shadow_frame, false /* is_exact */, inst, inst_data, result);
729 }
730}
731
Orion Hodson928033d2018-02-07 05:30:54 +0000732static bool DoVarHandleInvokeCommon(Thread* self,
733 ShadowFrame& shadow_frame,
734 const Instruction* inst,
735 uint16_t inst_data,
736 JValue* result,
737 mirror::VarHandle::AccessMode access_mode)
738 REQUIRES_SHARED(Locks::mutator_lock_) {
739 // Make sure to check for async exceptions
740 if (UNLIKELY(self->ObserveAsyncException())) {
741 return false;
742 }
743
Orion Hodson928033d2018-02-07 05:30:54 +0000744 StackHandleScope<2> hs(self);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100745 bool is_var_args = inst->HasVarArgs();
Orion Hodson06d10a72018-05-14 08:53:38 +0100746 const uint16_t vRegH = is_var_args ? inst->VRegH_45cc() : inst->VRegH_4rcc();
Orion Hodson928033d2018-02-07 05:30:54 +0000747 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
748 Handle<mirror::MethodType> callsite_type(hs.NewHandle(
Orion Hodson06d10a72018-05-14 08:53:38 +0100749 class_linker->ResolveMethodType(self, dex::ProtoIndex(vRegH), shadow_frame.GetMethod())));
Orion Hodson928033d2018-02-07 05:30:54 +0000750 // This implies we couldn't resolve one or more types in this VarHandle.
751 if (UNLIKELY(callsite_type == nullptr)) {
752 CHECK(self->IsExceptionPending());
753 return false;
754 }
755
Orion Hodson537a4fe2018-05-15 13:57:58 +0100756 const uint32_t vRegC = is_var_args ? inst->VRegC_45cc() : inst->VRegC_4rcc();
757 ObjPtr<mirror::Object> receiver(shadow_frame.GetVRegReference(vRegC));
758 Handle<mirror::VarHandle> var_handle(hs.NewHandle(down_cast<mirror::VarHandle*>(receiver.Ptr())));
Orion Hodson928033d2018-02-07 05:30:54 +0000759 if (is_var_args) {
760 uint32_t args[Instruction::kMaxVarArgRegs];
761 inst->GetVarArgs(args, inst_data);
762 VarArgsInstructionOperands all_operands(args, inst->VRegA_45cc());
763 NoReceiverInstructionOperands operands(&all_operands);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100764 return VarHandleInvokeAccessor(self,
765 shadow_frame,
766 var_handle,
767 callsite_type,
768 access_mode,
769 &operands,
770 result);
Orion Hodson928033d2018-02-07 05:30:54 +0000771 } else {
772 RangeInstructionOperands all_operands(inst->VRegC_4rcc(), inst->VRegA_4rcc());
773 NoReceiverInstructionOperands operands(&all_operands);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100774 return VarHandleInvokeAccessor(self,
775 shadow_frame,
776 var_handle,
777 callsite_type,
778 access_mode,
779 &operands,
780 result);
Orion Hodson928033d2018-02-07 05:30:54 +0000781 }
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100782}
783
Orion Hodson928033d2018-02-07 05:30:54 +0000784#define DO_VAR_HANDLE_ACCESSOR(_access_mode) \
785bool DoVarHandle ## _access_mode(Thread* self, \
786 ShadowFrame& shadow_frame, \
787 const Instruction* inst, \
788 uint16_t inst_data, \
789 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { \
790 const auto access_mode = mirror::VarHandle::AccessMode::k ## _access_mode; \
791 return DoVarHandleInvokeCommon(self, shadow_frame, inst, inst_data, result, access_mode); \
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100792}
793
Orion Hodson928033d2018-02-07 05:30:54 +0000794DO_VAR_HANDLE_ACCESSOR(CompareAndExchange)
795DO_VAR_HANDLE_ACCESSOR(CompareAndExchangeAcquire)
796DO_VAR_HANDLE_ACCESSOR(CompareAndExchangeRelease)
797DO_VAR_HANDLE_ACCESSOR(CompareAndSet)
798DO_VAR_HANDLE_ACCESSOR(Get)
799DO_VAR_HANDLE_ACCESSOR(GetAcquire)
800DO_VAR_HANDLE_ACCESSOR(GetAndAdd)
801DO_VAR_HANDLE_ACCESSOR(GetAndAddAcquire)
802DO_VAR_HANDLE_ACCESSOR(GetAndAddRelease)
803DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAnd)
804DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAndAcquire)
805DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAndRelease)
806DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOr)
807DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOrAcquire)
808DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOrRelease)
809DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXor)
810DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXorAcquire)
811DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXorRelease)
812DO_VAR_HANDLE_ACCESSOR(GetAndSet)
813DO_VAR_HANDLE_ACCESSOR(GetAndSetAcquire)
814DO_VAR_HANDLE_ACCESSOR(GetAndSetRelease)
815DO_VAR_HANDLE_ACCESSOR(GetOpaque)
816DO_VAR_HANDLE_ACCESSOR(GetVolatile)
817DO_VAR_HANDLE_ACCESSOR(Set)
818DO_VAR_HANDLE_ACCESSOR(SetOpaque)
819DO_VAR_HANDLE_ACCESSOR(SetRelease)
820DO_VAR_HANDLE_ACCESSOR(SetVolatile)
821DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSet)
822DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetAcquire)
823DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetPlain)
824DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetRelease)
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100825
Orion Hodson928033d2018-02-07 05:30:54 +0000826#undef DO_VAR_HANDLE_ACCESSOR
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100827
828template<bool is_range>
829bool DoInvokePolymorphic(Thread* self,
830 ShadowFrame& shadow_frame,
831 const Instruction* inst,
832 uint16_t inst_data,
833 JValue* result) {
834 const int invoke_method_idx = inst->VRegB();
835 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
836 ArtMethod* invoke_method =
837 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
838 self, invoke_method_idx, shadow_frame.GetMethod(), kVirtual);
839
840 // Ensure intrinsic identifiers are initialized.
841 DCHECK(invoke_method->IsIntrinsic());
842
843 // Dispatch based on intrinsic identifier associated with method.
844 switch (static_cast<art::Intrinsics>(invoke_method->GetIntrinsic())) {
845#define CASE_SIGNATURE_POLYMORPHIC_INTRINSIC(Name, ...) \
846 case Intrinsics::k##Name: \
847 return Do ## Name(self, shadow_frame, inst, inst_data, result);
848#include "intrinsics_list.h"
849 SIGNATURE_POLYMORPHIC_INTRINSICS_LIST(CASE_SIGNATURE_POLYMORPHIC_INTRINSIC)
850#undef INTRINSICS_LIST
851#undef SIGNATURE_POLYMORPHIC_INTRINSICS_LIST
852#undef CASE_SIGNATURE_POLYMORPHIC_INTRINSIC
853 default:
854 LOG(FATAL) << "Unreachable: " << invoke_method->GetIntrinsic();
855 UNREACHABLE();
856 return false;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100857 }
858}
859
Orion Hodsona5dca522018-02-27 12:42:11 +0000860static JValue ConvertScalarBootstrapArgument(jvalue value) {
861 // value either contains a primitive scalar value if it corresponds
862 // to a primitive type, or it contains an integer value if it
863 // corresponds to an object instance reference id (e.g. a string id).
864 return JValue::FromPrimitive(value.j);
865}
866
867static ObjPtr<mirror::Class> GetClassForBootstrapArgument(EncodedArrayValueIterator::ValueType type)
868 REQUIRES_SHARED(Locks::mutator_lock_) {
869 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100870 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = class_linker->GetClassRoots();
Orion Hodsona5dca522018-02-27 12:42:11 +0000871 switch (type) {
872 case EncodedArrayValueIterator::ValueType::kBoolean:
873 case EncodedArrayValueIterator::ValueType::kByte:
874 case EncodedArrayValueIterator::ValueType::kChar:
875 case EncodedArrayValueIterator::ValueType::kShort:
876 // These types are disallowed by JVMS. Treat as integers. This
877 // will result in CCE's being raised if the BSM has one of these
878 // types.
879 case EncodedArrayValueIterator::ValueType::kInt:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100880 return GetClassRoot(ClassRoot::kPrimitiveInt, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000881 case EncodedArrayValueIterator::ValueType::kLong:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100882 return GetClassRoot(ClassRoot::kPrimitiveLong, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000883 case EncodedArrayValueIterator::ValueType::kFloat:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100884 return GetClassRoot(ClassRoot::kPrimitiveFloat, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000885 case EncodedArrayValueIterator::ValueType::kDouble:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100886 return GetClassRoot(ClassRoot::kPrimitiveDouble, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000887 case EncodedArrayValueIterator::ValueType::kMethodType:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100888 return GetClassRoot<mirror::MethodType>(class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000889 case EncodedArrayValueIterator::ValueType::kMethodHandle:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100890 return GetClassRoot<mirror::MethodHandle>(class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000891 case EncodedArrayValueIterator::ValueType::kString:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100892 return GetClassRoot<mirror::String>();
Orion Hodsona5dca522018-02-27 12:42:11 +0000893 case EncodedArrayValueIterator::ValueType::kType:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100894 return GetClassRoot<mirror::Class>();
Orion Hodsona5dca522018-02-27 12:42:11 +0000895 case EncodedArrayValueIterator::ValueType::kField:
896 case EncodedArrayValueIterator::ValueType::kMethod:
897 case EncodedArrayValueIterator::ValueType::kEnum:
898 case EncodedArrayValueIterator::ValueType::kArray:
899 case EncodedArrayValueIterator::ValueType::kAnnotation:
900 case EncodedArrayValueIterator::ValueType::kNull:
901 return nullptr;
902 }
903}
904
905static bool GetArgumentForBootstrapMethod(Thread* self,
906 ArtMethod* referrer,
907 EncodedArrayValueIterator::ValueType type,
908 const JValue* encoded_value,
909 JValue* decoded_value)
910 REQUIRES_SHARED(Locks::mutator_lock_) {
911 // The encoded_value contains either a scalar value (IJDF) or a
912 // scalar DEX file index to a reference type to be materialized.
913 switch (type) {
914 case EncodedArrayValueIterator::ValueType::kInt:
915 case EncodedArrayValueIterator::ValueType::kFloat:
916 decoded_value->SetI(encoded_value->GetI());
917 return true;
918 case EncodedArrayValueIterator::ValueType::kLong:
919 case EncodedArrayValueIterator::ValueType::kDouble:
920 decoded_value->SetJ(encoded_value->GetJ());
921 return true;
922 case EncodedArrayValueIterator::ValueType::kMethodType: {
923 StackHandleScope<2> hs(self);
924 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader()));
925 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
Orion Hodson06d10a72018-05-14 08:53:38 +0100926 dex::ProtoIndex proto_idx(encoded_value->GetC());
Orion Hodsona5dca522018-02-27 12:42:11 +0000927 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Orion Hodson06d10a72018-05-14 08:53:38 +0100928 ObjPtr<mirror::MethodType> o =
929 cl->ResolveMethodType(self, proto_idx, dex_cache, class_loader);
Orion Hodsona5dca522018-02-27 12:42:11 +0000930 if (UNLIKELY(o.IsNull())) {
931 DCHECK(self->IsExceptionPending());
932 return false;
933 }
934 decoded_value->SetL(o);
935 return true;
936 }
937 case EncodedArrayValueIterator::ValueType::kMethodHandle: {
938 uint32_t index = static_cast<uint32_t>(encoded_value->GetI());
939 ClassLinker* cl = Runtime::Current()->GetClassLinker();
940 ObjPtr<mirror::MethodHandle> o = cl->ResolveMethodHandle(self, index, referrer);
941 if (UNLIKELY(o.IsNull())) {
942 DCHECK(self->IsExceptionPending());
943 return false;
944 }
945 decoded_value->SetL(o);
946 return true;
947 }
948 case EncodedArrayValueIterator::ValueType::kString: {
Orion Hodsona5dca522018-02-27 12:42:11 +0000949 dex::StringIndex index(static_cast<uint32_t>(encoded_value->GetI()));
950 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Vladimir Marko18090d12018-06-01 16:53:12 +0100951 ObjPtr<mirror::String> o = cl->ResolveString(index, referrer);
Orion Hodsona5dca522018-02-27 12:42:11 +0000952 if (UNLIKELY(o.IsNull())) {
953 DCHECK(self->IsExceptionPending());
954 return false;
955 }
956 decoded_value->SetL(o);
957 return true;
958 }
959 case EncodedArrayValueIterator::ValueType::kType: {
Orion Hodsona5dca522018-02-27 12:42:11 +0000960 dex::TypeIndex index(static_cast<uint32_t>(encoded_value->GetI()));
961 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100962 ObjPtr<mirror::Class> o = cl->ResolveType(index, referrer);
Orion Hodsona5dca522018-02-27 12:42:11 +0000963 if (UNLIKELY(o.IsNull())) {
964 DCHECK(self->IsExceptionPending());
965 return false;
966 }
967 decoded_value->SetL(o);
968 return true;
969 }
970 case EncodedArrayValueIterator::ValueType::kBoolean:
971 case EncodedArrayValueIterator::ValueType::kByte:
972 case EncodedArrayValueIterator::ValueType::kChar:
973 case EncodedArrayValueIterator::ValueType::kShort:
974 case EncodedArrayValueIterator::ValueType::kField:
975 case EncodedArrayValueIterator::ValueType::kMethod:
976 case EncodedArrayValueIterator::ValueType::kEnum:
977 case EncodedArrayValueIterator::ValueType::kArray:
978 case EncodedArrayValueIterator::ValueType::kAnnotation:
979 case EncodedArrayValueIterator::ValueType::kNull:
980 // Unreachable - unsupported types that have been checked when
981 // determining the effect call site type based on the bootstrap
982 // argument types.
983 UNREACHABLE();
984 }
985}
986
987static bool PackArgumentForBootstrapMethod(Thread* self,
988 ArtMethod* referrer,
989 CallSiteArrayValueIterator* it,
990 ShadowFrameSetter* setter)
991 REQUIRES_SHARED(Locks::mutator_lock_) {
992 auto type = it->GetValueType();
993 const JValue encoded_value = ConvertScalarBootstrapArgument(it->GetJavaValue());
994 JValue decoded_value;
995 if (!GetArgumentForBootstrapMethod(self, referrer, type, &encoded_value, &decoded_value)) {
996 return false;
997 }
998 switch (it->GetValueType()) {
999 case EncodedArrayValueIterator::ValueType::kInt:
1000 case EncodedArrayValueIterator::ValueType::kFloat:
1001 setter->Set(static_cast<uint32_t>(decoded_value.GetI()));
1002 return true;
1003 case EncodedArrayValueIterator::ValueType::kLong:
1004 case EncodedArrayValueIterator::ValueType::kDouble:
1005 setter->SetLong(decoded_value.GetJ());
1006 return true;
1007 case EncodedArrayValueIterator::ValueType::kMethodType:
1008 case EncodedArrayValueIterator::ValueType::kMethodHandle:
1009 case EncodedArrayValueIterator::ValueType::kString:
1010 case EncodedArrayValueIterator::ValueType::kType:
1011 setter->SetReference(decoded_value.GetL());
1012 return true;
1013 case EncodedArrayValueIterator::ValueType::kBoolean:
1014 case EncodedArrayValueIterator::ValueType::kByte:
1015 case EncodedArrayValueIterator::ValueType::kChar:
1016 case EncodedArrayValueIterator::ValueType::kShort:
1017 case EncodedArrayValueIterator::ValueType::kField:
1018 case EncodedArrayValueIterator::ValueType::kMethod:
1019 case EncodedArrayValueIterator::ValueType::kEnum:
1020 case EncodedArrayValueIterator::ValueType::kArray:
1021 case EncodedArrayValueIterator::ValueType::kAnnotation:
1022 case EncodedArrayValueIterator::ValueType::kNull:
1023 // Unreachable - unsupported types that have been checked when
1024 // determining the effect call site type based on the bootstrap
1025 // argument types.
1026 UNREACHABLE();
1027 }
1028}
1029
1030static bool PackCollectorArrayForBootstrapMethod(Thread* self,
1031 ArtMethod* referrer,
1032 ObjPtr<mirror::Class> array_type,
1033 int32_t array_length,
1034 CallSiteArrayValueIterator* it,
1035 ShadowFrameSetter* setter)
1036 REQUIRES_SHARED(Locks::mutator_lock_) {
1037 StackHandleScope<1> hs(self);
1038 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1039 JValue decoded_value;
1040
1041#define COLLECT_PRIMITIVE_ARRAY(Descriptor, Type) \
1042 Handle<mirror::Type ## Array> array = \
1043 hs.NewHandle(mirror::Type ## Array::Alloc(self, array_length)); \
1044 if (array.IsNull()) { \
1045 return false; \
1046 } \
1047 for (int32_t i = 0; it->HasNext(); it->Next(), ++i) { \
1048 auto type = it->GetValueType(); \
1049 DCHECK_EQ(type, EncodedArrayValueIterator::ValueType::k ## Type); \
1050 const JValue encoded_value = \
1051 ConvertScalarBootstrapArgument(it->GetJavaValue()); \
1052 GetArgumentForBootstrapMethod(self, \
1053 referrer, \
1054 type, \
1055 &encoded_value, \
1056 &decoded_value); \
1057 array->Set(i, decoded_value.Get ## Descriptor()); \
1058 } \
1059 setter->SetReference(array.Get()); \
1060 return true;
1061
1062#define COLLECT_REFERENCE_ARRAY(T, Type) \
1063 Handle<mirror::ObjectArray<T>> array = \
1064 hs.NewHandle(mirror::ObjectArray<T>::Alloc(self, \
1065 array_type, \
1066 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 if (!GetArgumentForBootstrapMethod(self, \
1076 referrer, \
1077 type, \
1078 &encoded_value, \
1079 &decoded_value)) { \
1080 return false; \
1081 } \
1082 ObjPtr<mirror::Object> o = decoded_value.GetL(); \
1083 if (Runtime::Current()->IsActiveTransaction()) { \
1084 array->Set<true>(i, ObjPtr<T>::DownCast(o)); \
1085 } else { \
1086 array->Set<false>(i, ObjPtr<T>::DownCast(o)); \
1087 } \
1088 } \
1089 setter->SetReference(array.Get()); \
1090 return true;
1091
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001092 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = class_linker->GetClassRoots();
1093 ObjPtr<mirror::Class> component_type = array_type->GetComponentType();
1094 if (component_type == GetClassRoot(ClassRoot::kPrimitiveInt, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001095 COLLECT_PRIMITIVE_ARRAY(I, Int);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001096 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveLong, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001097 COLLECT_PRIMITIVE_ARRAY(J, Long);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001098 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveFloat, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001099 COLLECT_PRIMITIVE_ARRAY(F, Float);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001100 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveDouble, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001101 COLLECT_PRIMITIVE_ARRAY(D, Double);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001102 } else if (component_type == GetClassRoot<mirror::MethodType>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001103 COLLECT_REFERENCE_ARRAY(mirror::MethodType, MethodType);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001104 } else if (component_type == GetClassRoot<mirror::MethodHandle>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001105 COLLECT_REFERENCE_ARRAY(mirror::MethodHandle, MethodHandle);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001106 } else if (component_type == GetClassRoot<mirror::String>(class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001107 COLLECT_REFERENCE_ARRAY(mirror::String, String);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001108 } else if (component_type == GetClassRoot<mirror::Class>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001109 COLLECT_REFERENCE_ARRAY(mirror::Class, Type);
1110 } else {
1111 UNREACHABLE();
1112 }
1113 #undef COLLECT_PRIMITIVE_ARRAY
1114 #undef COLLECT_REFERENCE_ARRAY
1115}
1116
1117static ObjPtr<mirror::MethodType> BuildCallSiteForBootstrapMethod(Thread* self,
1118 const DexFile* dex_file,
1119 uint32_t call_site_idx)
1120 REQUIRES_SHARED(Locks::mutator_lock_) {
1121 const DexFile::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx);
1122 CallSiteArrayValueIterator it(*dex_file, csi);
1123 DCHECK_GE(it.Size(), 1u);
1124
1125 StackHandleScope<2> hs(self);
1126 // Create array for parameter types.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001127 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001128 ObjPtr<mirror::Class> class_array_type =
1129 GetClassRoot<mirror::ObjectArray<mirror::Class>>(class_linker);
Orion Hodsona5dca522018-02-27 12:42:11 +00001130 Handle<mirror::ObjectArray<mirror::Class>> ptypes = hs.NewHandle(
1131 mirror::ObjectArray<mirror::Class>::Alloc(self,
1132 class_array_type,
1133 static_cast<int>(it.Size())));
1134 if (ptypes.IsNull()) {
1135 DCHECK(self->IsExceptionPending());
1136 return nullptr;
1137 }
1138
1139 // Populate the first argument with an instance of j.l.i.MethodHandles.Lookup
1140 // that the runtime will construct.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001141 ptypes->Set(0, GetClassRoot<mirror::MethodHandlesLookup>(class_linker));
Orion Hodsona5dca522018-02-27 12:42:11 +00001142 it.Next();
1143
1144 // The remaining parameter types are derived from the types of
1145 // arguments present in the DEX file.
1146 int index = 1;
1147 while (it.HasNext()) {
1148 ObjPtr<mirror::Class> ptype = GetClassForBootstrapArgument(it.GetValueType());
1149 if (ptype.IsNull()) {
1150 ThrowClassCastException("Unsupported bootstrap argument type");
1151 return nullptr;
1152 }
1153 ptypes->Set(index, ptype);
1154 index++;
1155 it.Next();
1156 }
1157 DCHECK_EQ(static_cast<size_t>(index), it.Size());
1158
1159 // By definition, the return type is always a j.l.i.CallSite.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001160 Handle<mirror::Class> rtype = hs.NewHandle(GetClassRoot<mirror::CallSite>());
Orion Hodsona5dca522018-02-27 12:42:11 +00001161 return mirror::MethodType::Create(self, rtype, ptypes);
1162}
1163
Orion Hodsonc069a302017-01-18 09:23:12 +00001164static ObjPtr<mirror::CallSite> InvokeBootstrapMethod(Thread* self,
1165 ShadowFrame& shadow_frame,
1166 uint32_t call_site_idx)
1167 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001168 StackHandleScope<5> hs(self);
Orion Hodsona5dca522018-02-27 12:42:11 +00001169 // There are three mandatory arguments expected from the call site
1170 // value array in the DEX file: the bootstrap method handle, the
1171 // method name to pass to the bootstrap method, and the method type
1172 // to pass to the bootstrap method.
1173 static constexpr size_t kMandatoryArgumentsCount = 3;
Orion Hodsonc069a302017-01-18 09:23:12 +00001174 ArtMethod* referrer = shadow_frame.GetMethod();
1175 const DexFile* dex_file = referrer->GetDexFile();
1176 const DexFile::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00001177 CallSiteArrayValueIterator it(*dex_file, csi);
Orion Hodsona5dca522018-02-27 12:42:11 +00001178 if (it.Size() < kMandatoryArgumentsCount) {
1179 ThrowBootstrapMethodError("Truncated bootstrap arguments (%zu < %zu)",
1180 it.Size(), kMandatoryArgumentsCount);
1181 return nullptr;
1182 }
1183
1184 if (it.GetValueType() != EncodedArrayValueIterator::ValueType::kMethodHandle) {
1185 ThrowBootstrapMethodError("First bootstrap argument is not a method handle");
1186 return nullptr;
1187 }
1188
1189 uint32_t bsm_index = static_cast<uint32_t>(it.GetJavaValue().i);
1190 it.Next();
1191
Orion Hodsonc069a302017-01-18 09:23:12 +00001192 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Orion Hodsona5dca522018-02-27 12:42:11 +00001193 Handle<mirror::MethodHandle> bsm =
1194 hs.NewHandle(class_linker->ResolveMethodHandle(self, bsm_index, referrer));
1195 if (bsm.IsNull()) {
Orion Hodsonc069a302017-01-18 09:23:12 +00001196 DCHECK(self->IsExceptionPending());
1197 return nullptr;
1198 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001199
Orion Hodsona5dca522018-02-27 12:42:11 +00001200 if (bsm->GetHandleKind() != mirror::MethodHandle::Kind::kInvokeStatic) {
1201 // JLS suggests also accepting constructors. This is currently
1202 // hard as constructor invocations happen via transformers in ART
1203 // today. The constructor would need to be a class derived from java.lang.invoke.CallSite.
1204 ThrowBootstrapMethodError("Unsupported bootstrap method invocation kind");
1205 return nullptr;
1206 }
1207
1208 // Construct the local call site type information based on the 3
1209 // mandatory arguments provided by the runtime and the static arguments
1210 // in the DEX file. We will use these arguments to build a shadow frame.
1211 MutableHandle<mirror::MethodType> call_site_type =
1212 hs.NewHandle(BuildCallSiteForBootstrapMethod(self, dex_file, call_site_idx));
1213 if (call_site_type.IsNull()) {
1214 DCHECK(self->IsExceptionPending());
1215 return nullptr;
1216 }
1217
1218 // Check if this BSM is targeting a variable arity method. If so,
1219 // we'll need to collect the trailing arguments into an array.
1220 Handle<mirror::Array> collector_arguments;
1221 int32_t collector_arguments_length;
1222 if (bsm->GetTargetMethod()->IsVarargs()) {
1223 int number_of_bsm_parameters = bsm->GetMethodType()->GetNumberOfPTypes();
1224 if (number_of_bsm_parameters == 0) {
1225 ThrowBootstrapMethodError("Variable arity BSM does not have any arguments");
1226 return nullptr;
1227 }
1228 Handle<mirror::Class> collector_array_class =
1229 hs.NewHandle(bsm->GetMethodType()->GetPTypes()->Get(number_of_bsm_parameters - 1));
1230 if (!collector_array_class->IsArrayClass()) {
1231 ThrowBootstrapMethodError("Variable arity BSM does not have array as final argument");
1232 return nullptr;
1233 }
1234 // The call site may include no arguments to be collected. In this
1235 // case the number of arguments must be at least the number of BSM
1236 // parameters less the collector array.
1237 if (call_site_type->GetNumberOfPTypes() < number_of_bsm_parameters - 1) {
1238 ThrowWrongMethodTypeException(bsm->GetMethodType(), call_site_type.Get());
1239 return nullptr;
1240 }
1241 // Check all the arguments to be collected match the collector array component type.
1242 for (int i = number_of_bsm_parameters - 1; i < call_site_type->GetNumberOfPTypes(); ++i) {
1243 if (call_site_type->GetPTypes()->Get(i) != collector_array_class->GetComponentType()) {
1244 ThrowClassCastException(collector_array_class->GetComponentType(),
1245 call_site_type->GetPTypes()->Get(i));
1246 return nullptr;
1247 }
1248 }
1249 // Update the call site method type so it now includes the collector array.
1250 int32_t collector_arguments_start = number_of_bsm_parameters - 1;
1251 collector_arguments_length = call_site_type->GetNumberOfPTypes() - number_of_bsm_parameters + 1;
1252 call_site_type.Assign(
1253 mirror::MethodType::CollectTrailingArguments(self,
1254 call_site_type.Get(),
1255 collector_array_class.Get(),
1256 collector_arguments_start));
1257 if (call_site_type.IsNull()) {
1258 DCHECK(self->IsExceptionPending());
1259 return nullptr;
1260 }
1261 } else {
1262 collector_arguments_length = 0;
1263 }
1264
1265 if (call_site_type->GetNumberOfPTypes() != bsm->GetMethodType()->GetNumberOfPTypes()) {
1266 ThrowWrongMethodTypeException(bsm->GetMethodType(), call_site_type.Get());
1267 return nullptr;
1268 }
1269
1270 // BSM invocation has a different set of exceptions that
1271 // j.l.i.MethodHandle.invoke(). Scan arguments looking for CCE
1272 // "opportunities". Unfortunately we cannot just leave this to the
1273 // method handle invocation as this might generate a WMTE.
1274 for (int32_t i = 0; i < call_site_type->GetNumberOfPTypes(); ++i) {
1275 ObjPtr<mirror::Class> from = call_site_type->GetPTypes()->Get(i);
1276 ObjPtr<mirror::Class> to = bsm->GetMethodType()->GetPTypes()->Get(i);
1277 if (!IsParameterTypeConvertible(from, to)) {
1278 ThrowClassCastException(from, to);
1279 return nullptr;
1280 }
1281 }
1282 if (!IsReturnTypeConvertible(call_site_type->GetRType(), bsm->GetMethodType()->GetRType())) {
1283 ThrowClassCastException(bsm->GetMethodType()->GetRType(), call_site_type->GetRType());
1284 return nullptr;
1285 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001286
1287 // Set-up a shadow frame for invoking the bootstrap method handle.
1288 ShadowFrameAllocaUniquePtr bootstrap_frame =
Orion Hodsona5dca522018-02-27 12:42:11 +00001289 CREATE_SHADOW_FRAME(call_site_type->NumberOfVRegs(),
1290 nullptr,
1291 referrer,
1292 shadow_frame.GetDexPC());
Orion Hodsonc069a302017-01-18 09:23:12 +00001293 ScopedStackedShadowFramePusher pusher(
1294 self, bootstrap_frame.get(), StackedShadowFrameType::kShadowFrameUnderConstruction);
Orion Hodsona5dca522018-02-27 12:42:11 +00001295 ShadowFrameSetter setter(bootstrap_frame.get(), 0u);
Orion Hodsonc069a302017-01-18 09:23:12 +00001296
1297 // The first parameter is a MethodHandles lookup instance.
Orion Hodsona5dca522018-02-27 12:42:11 +00001298 Handle<mirror::Class> lookup_class =
1299 hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass());
1300 ObjPtr<mirror::MethodHandlesLookup> lookup =
1301 mirror::MethodHandlesLookup::Create(self, lookup_class);
1302 if (lookup.IsNull()) {
Orion Hodsonc069a302017-01-18 09:23:12 +00001303 DCHECK(self->IsExceptionPending());
1304 return nullptr;
1305 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001306 setter.SetReference(lookup);
Orion Hodsonc069a302017-01-18 09:23:12 +00001307
Orion Hodsona5dca522018-02-27 12:42:11 +00001308 // Pack the remaining arguments into the frame.
1309 int number_of_arguments = call_site_type->GetNumberOfPTypes();
1310 int argument_index;
1311 for (argument_index = 1; argument_index < number_of_arguments; ++argument_index) {
1312 if (argument_index == number_of_arguments - 1 &&
1313 call_site_type->GetPTypes()->Get(argument_index)->IsArrayClass()) {
1314 ObjPtr<mirror::Class> array_type = call_site_type->GetPTypes()->Get(argument_index);
1315 if (!PackCollectorArrayForBootstrapMethod(self,
1316 referrer,
1317 array_type,
1318 collector_arguments_length,
1319 &it,
1320 &setter)) {
1321 DCHECK(self->IsExceptionPending());
1322 return nullptr;
Orion Hodsonc069a302017-01-18 09:23:12 +00001323 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001324 } else if (!PackArgumentForBootstrapMethod(self, referrer, &it, &setter)) {
1325 DCHECK(self->IsExceptionPending());
1326 return nullptr;
Orion Hodsonc069a302017-01-18 09:23:12 +00001327 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001328 it.Next();
1329 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001330 DCHECK(!it.HasNext());
1331 DCHECK(setter.Done());
Orion Hodsonc069a302017-01-18 09:23:12 +00001332
1333 // Invoke the bootstrap method handle.
1334 JValue result;
Orion Hodsona5dca522018-02-27 12:42:11 +00001335 RangeInstructionOperands operands(0, bootstrap_frame->NumberOfVRegs());
1336 bool invoke_success = MethodHandleInvoke(self,
1337 *bootstrap_frame,
1338 bsm,
1339 call_site_type,
1340 &operands,
1341 &result);
Orion Hodsonc069a302017-01-18 09:23:12 +00001342 if (!invoke_success) {
1343 DCHECK(self->IsExceptionPending());
1344 return nullptr;
1345 }
1346
1347 Handle<mirror::Object> object(hs.NewHandle(result.GetL()));
Orion Hodsonc069a302017-01-18 09:23:12 +00001348 if (UNLIKELY(object.IsNull())) {
Orion Hodsonda1cdd02018-01-31 18:08:28 +00001349 // This will typically be for LambdaMetafactory which is not supported.
Orion Hodson76e6adb2018-02-23 13:15:55 +00001350 ThrowClassCastException("Bootstrap method returned null");
Orion Hodsonc069a302017-01-18 09:23:12 +00001351 return nullptr;
1352 }
1353
Orion Hodsona5dca522018-02-27 12:42:11 +00001354 // Check the result type is a subclass of j.l.i.CallSite.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001355 ObjPtr<mirror::Class> call_site_class = GetClassRoot<mirror::CallSite>(class_linker);
1356 if (UNLIKELY(!object->InstanceOf(call_site_class))) {
1357 ThrowClassCastException(object->GetClass(), call_site_class);
Orion Hodsonc069a302017-01-18 09:23:12 +00001358 return nullptr;
1359 }
1360
Orion Hodsona5dca522018-02-27 12:42:11 +00001361 // Check the call site target is not null as we're going to invoke it.
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001362 ObjPtr<mirror::CallSite> call_site =
1363 ObjPtr<mirror::CallSite>::DownCast(ObjPtr<mirror::Object>(result.GetL()));
1364 ObjPtr<mirror::MethodHandle> target = call_site->GetTarget();
1365 if (UNLIKELY(target == nullptr)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001366 ThrowClassCastException("Bootstrap method returned a CallSite with a null target");
Orion Hodsonc069a302017-01-18 09:23:12 +00001367 return nullptr;
1368 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001369 return call_site;
Orion Hodsonc069a302017-01-18 09:23:12 +00001370}
1371
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001372namespace {
1373
1374ObjPtr<mirror::CallSite> DoResolveCallSite(Thread* self,
1375 ShadowFrame& shadow_frame,
1376 uint32_t call_site_idx)
1377 REQUIRES_SHARED(Locks::mutator_lock_) {
1378 StackHandleScope<1> hs(self);
1379 Handle<mirror::DexCache> dex_cache(hs.NewHandle(shadow_frame.GetMethod()->GetDexCache()));
1380
1381 // Get the call site from the DexCache if present.
1382 ObjPtr<mirror::CallSite> call_site = dex_cache->GetResolvedCallSite(call_site_idx);
1383 if (LIKELY(call_site != nullptr)) {
1384 return call_site;
1385 }
1386
1387 // Invoke the bootstrap method to get a candidate call site.
1388 call_site = InvokeBootstrapMethod(self, shadow_frame, call_site_idx);
1389 if (UNLIKELY(call_site == nullptr)) {
1390 if (!self->GetException()->IsError()) {
1391 // Use a BootstrapMethodError if the exception is not an instance of java.lang.Error.
1392 ThrowWrappedBootstrapMethodError("Exception from call site #%u bootstrap method",
1393 call_site_idx);
1394 }
1395 return nullptr;
1396 }
1397
1398 // Attempt to place the candidate call site into the DexCache, return the winning call site.
1399 return dex_cache->SetResolvedCallSite(call_site_idx, call_site);
1400}
1401
1402} // namespace
1403
Orion Hodsonc069a302017-01-18 09:23:12 +00001404bool DoInvokeCustom(Thread* self,
1405 ShadowFrame& shadow_frame,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001406 uint32_t call_site_idx,
1407 const InstructionOperands* operands,
1408 JValue* result) {
Alex Light848574c2017-09-25 16:59:39 -07001409 // Make sure to check for async exceptions
1410 if (UNLIKELY(self->ObserveAsyncException())) {
1411 return false;
1412 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001413
Orion Hodsonc069a302017-01-18 09:23:12 +00001414 // invoke-custom is not supported in transactions. In transactions
1415 // there is a limited set of types supported. invoke-custom allows
1416 // running arbitrary code and instantiating arbitrary types.
1417 CHECK(!Runtime::Current()->IsActiveTransaction());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001418
1419 ObjPtr<mirror::CallSite> call_site = DoResolveCallSite(self, shadow_frame, call_site_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00001420 if (call_site.IsNull()) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001421 DCHECK(self->IsExceptionPending());
1422 return false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001423 }
1424
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001425 StackHandleScope<2> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +00001426 Handle<mirror::MethodHandle> target = hs.NewHandle(call_site->GetTarget());
1427 Handle<mirror::MethodType> target_method_type = hs.NewHandle(target->GetMethodType());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001428 DCHECK_EQ(operands->GetNumberOfOperands(), target_method_type->NumberOfVRegs())
1429 << " call_site_idx" << call_site_idx;
1430 return MethodHandleInvokeExact(self,
1431 shadow_frame,
1432 target,
1433 target_method_type,
1434 operands,
1435 result);
Orion Hodsonc069a302017-01-18 09:23:12 +00001436}
1437
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001438// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
1439static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
1440 size_t dest_reg, size_t src_reg)
1441 REQUIRES_SHARED(Locks::mutator_lock_) {
1442 // Uint required, so that sign extension does not make this wrong on 64b systems
1443 uint32_t src_value = shadow_frame.GetVReg(src_reg);
1444 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
1445
1446 // If both register locations contains the same value, the register probably holds a reference.
1447 // Note: As an optimization, non-moving collectors leave a stale reference value
1448 // in the references array even after the original vreg was overwritten to a non-reference.
Vladimir Marko78baed52018-10-11 10:44:58 +01001449 if (src_value == reinterpret_cast32<uint32_t>(o.Ptr())) {
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001450 new_shadow_frame->SetVRegReference(dest_reg, o);
1451 } else {
1452 new_shadow_frame->SetVReg(dest_reg, src_value);
1453 }
1454}
1455
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001456template <bool is_range>
1457inline void CopyRegisters(ShadowFrame& caller_frame,
1458 ShadowFrame* callee_frame,
1459 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
1460 const size_t first_src_reg,
1461 const size_t first_dest_reg,
1462 const size_t num_regs) {
1463 if (is_range) {
1464 const size_t dest_reg_bound = first_dest_reg + num_regs;
1465 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound;
1466 ++dest_reg, ++src_reg) {
1467 AssignRegister(callee_frame, caller_frame, dest_reg, src_reg);
1468 }
1469 } else {
1470 DCHECK_LE(num_regs, arraysize(arg));
1471
1472 for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) {
1473 AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]);
1474 }
1475 }
1476}
1477
Igor Murashkin6918bf12015-09-27 19:19:06 -07001478template <bool is_range,
Narayan Kamath370423d2016-10-03 16:51:22 +01001479 bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001480static inline bool DoCallCommon(ArtMethod* called_method,
1481 Thread* self,
1482 ShadowFrame& shadow_frame,
1483 JValue* result,
1484 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +01001485 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -07001486 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001487 bool string_init = false;
1488 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001489 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
1490 && called_method->IsConstructor())) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001491 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
Jeff Hao848f70a2014-01-15 13:49:50 -08001492 string_init = true;
1493 }
1494
Alex Lightdaf58c82016-03-16 23:00:49 +00001495 // Compute method information.
David Sehr0225f8e2018-01-31 08:52:24 +00001496 CodeItemDataAccessor accessor(called_method->DexInstructionData());
Igor Murashkin158f35c2015-06-10 15:55:30 -07001497 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001498 uint16_t num_regs;
Jeff Hao5ea84132017-05-05 16:59:29 -07001499 // Test whether to use the interpreter or compiler entrypoint, and save that result to pass to
1500 // PerformCall. A deoptimization could occur at any time, and we shouldn't change which
1501 // entrypoint to use once we start building the shadow frame.
Mathieu Chartier448bbcf2017-07-06 12:05:13 -07001502
1503 // For unstarted runtimes, always use the interpreter entrypoint. This fixes the case where we are
1504 // doing cross compilation. Note that GetEntryPointFromQuickCompiledCode doesn't use the image
1505 // pointer size here and this may case an overflow if it is called from the compiler. b/62402160
1506 const bool use_interpreter_entrypoint = !Runtime::Current()->IsStarted() ||
1507 ClassLinker::ShouldUseInterpreterEntrypoint(
1508 called_method,
1509 called_method->GetEntryPointFromQuickCompiledCode());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001510 if (LIKELY(accessor.HasCodeItem())) {
Jeff Hao5ea84132017-05-05 16:59:29 -07001511 // When transitioning to compiled code, space only needs to be reserved for the input registers.
1512 // The rest of the frame gets discarded. This also prevents accessing the called method's code
1513 // item, saving memory by keeping code items of compiled code untouched.
Mathieu Chartier448bbcf2017-07-06 12:05:13 -07001514 if (!use_interpreter_entrypoint) {
1515 DCHECK(!Runtime::Current()->IsAotCompiler()) << "Compiler should use interpreter entrypoint";
Jeff Hao5ea84132017-05-05 16:59:29 -07001516 num_regs = number_of_inputs;
1517 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001518 num_regs = accessor.RegistersSize();
1519 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, accessor.InsSize());
Jeff Hao5ea84132017-05-05 16:59:29 -07001520 }
Nicolas Geoffray01822292017-03-09 09:03:19 +00001521 } else {
1522 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1523 num_regs = number_of_inputs;
Jeff Haodf79ddb2017-02-27 14:47:06 -08001524 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001525
Igor Murashkin158f35c2015-06-10 15:55:30 -07001526 // Hack for String init:
1527 //
1528 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
1529 // invoke-x StringFactory(a, b, c, ...)
1530 // by effectively dropping the first virtual register from the invoke.
1531 //
1532 // (at this point the ArtMethod has already been replaced,
1533 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +00001534 //
1535 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
1536 // to handle the compiler optimization of replacing `this` with null without
1537 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001538 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -07001539 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001540 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 -07001541
Igor Murashkin158f35c2015-06-10 15:55:30 -07001542 // The new StringFactory call is static and has one fewer argument.
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001543 if (!accessor.HasCodeItem()) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001544 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1545 num_regs--;
1546 } // 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 -07001547 number_of_inputs--;
1548
1549 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -07001550 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001551 arg[i - 1] = arg[i];
1552 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07001553 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001554
1555 // Rewrite the non-var-arg case
1556 vregC++; // Skips the 0th vreg in the range ("this").
1557 }
1558
1559 // Parameter registers go at the end of the shadow frame.
1560 DCHECK_GE(num_regs, number_of_inputs);
1561 size_t first_dest_reg = num_regs - number_of_inputs;
1562 DCHECK_NE(first_dest_reg, (size_t)-1);
1563
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001564 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001565 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -07001566 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -07001567 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -07001568 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001569
Igor Murashkin158f35c2015-06-10 15:55:30 -07001570 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -07001571 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001572 // Slow path.
1573 // We might need to do class loading, which incurs a thread state change to kNative. So
1574 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07001575 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +02001576 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001577 self->EndAssertNoThreadSuspension(old_cause);
1578
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001579 // ArtMethod here is needed to check type information of the call site against the callee.
1580 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
1581 //
1582 // As a special case for proxy methods, which are not dex-backed,
1583 // we have to retrieve type information from the proxy's method
1584 // interface method instead (which is dex backed since proxies are never interfaces).
Andreas Gampe542451c2016-07-26 09:02:02 -07001585 ArtMethod* method =
1586 new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001587
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001588 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001589 // to get the exact type of each reference argument.
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001590 const DexFile::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001591 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001592 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001593
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001594 // Handle receiver apart since it's not part of the shorty.
1595 size_t dest_reg = first_dest_reg;
1596 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001597
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001598 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001599 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001600 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
1601 ++dest_reg;
1602 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -07001603 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001604 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001605
1606 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001607 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001608 // Skip the 0th 'shorty' type since it represents the return type.
1609 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001610 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
1611 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001612 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001613 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001614 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001615 if (do_assignability_check && o != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001616 const dex::TypeIndex type_idx = params->GetTypeItem(shorty_pos).type_idx_;
Vladimir Marko942fd312017-01-16 20:52:19 +00001617 ObjPtr<mirror::Class> arg_type = method->GetDexCache()->GetResolvedType(type_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001618 if (arg_type == nullptr) {
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001619 StackHandleScope<1> hs(self);
1620 // Preserve o since it is used below and GetClassFromTypeIndex may cause thread
1621 // suspension.
1622 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&o);
Vladimir Markob45528c2017-07-27 14:14:28 +01001623 arg_type = method->ResolveClassFromTypeIndex(type_idx);
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001624 if (arg_type == nullptr) {
1625 CHECK(self->IsExceptionPending());
1626 return false;
1627 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001628 }
1629 if (!o->VerifierInstanceOf(arg_type)) {
1630 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -07001631 std::string temp1, temp2;
Orion Hodsonfef06642016-11-25 16:07:11 +00001632 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001633 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -08001634 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -07001635 o->GetClass()->GetDescriptor(&temp1),
1636 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001637 return false;
1638 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001639 }
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001640 new_shadow_frame->SetVRegReference(dest_reg, o);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001641 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -07001642 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001643 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001644 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001645 uint64_t wide_value =
1646 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
1647 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001648 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001649 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001650 ++dest_reg;
1651 ++arg_offset;
1652 break;
1653 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001654 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001655 default:
1656 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
1657 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001658 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001659 }
1660 } else {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001661 if (is_range) {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001662 DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001663 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001664
1665 CopyRegisters<is_range>(shadow_frame,
1666 new_shadow_frame,
1667 arg,
1668 vregC,
1669 first_dest_reg,
1670 number_of_inputs);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001671 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001672 }
1673
Jeff Hao5ea84132017-05-05 16:59:29 -07001674 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001675 accessor,
Jeff Hao5ea84132017-05-05 16:59:29 -07001676 shadow_frame.GetMethod(),
1677 first_dest_reg,
1678 new_shadow_frame,
1679 result,
1680 use_interpreter_entrypoint);
Jeff Hao848f70a2014-01-15 13:49:50 -08001681
1682 if (string_init && !self->IsExceptionPending()) {
Mingyao Yangffedec52016-05-19 10:48:40 -07001683 SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001684 }
1685
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001686 return !self->IsExceptionPending();
1687}
1688
Igor Murashkin158f35c2015-06-10 15:55:30 -07001689template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001690bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
1691 const Instruction* inst, uint16_t inst_data, JValue* result) {
1692 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001693 const uint16_t number_of_inputs =
1694 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001695
1696 // TODO: find a cleaner way to separate non-range and range information without duplicating
1697 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -07001698 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001699 uint32_t vregC = 0;
1700 if (is_range) {
1701 vregC = inst->VRegC_3rc();
1702 } else {
1703 vregC = inst->VRegC_35c();
1704 inst->GetVarArgs(arg, inst_data);
1705 }
1706
1707 return DoCallCommon<is_range, do_assignability_check>(
1708 called_method, self, shadow_frame,
1709 result, number_of_inputs, arg, vregC);
1710}
1711
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001712template <bool is_range, bool do_access_check, bool transaction_active>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001713bool DoFilledNewArray(const Instruction* inst,
1714 const ShadowFrame& shadow_frame,
1715 Thread* self,
1716 JValue* result) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001717 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
1718 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
1719 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
1720 if (!is_range) {
1721 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
1722 CHECK_LE(length, 5);
1723 }
1724 if (UNLIKELY(length < 0)) {
1725 ThrowNegativeArraySizeException(length);
1726 return false;
1727 }
1728 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08001729 ObjPtr<mirror::Class> array_class = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
Mathieu Chartieref41db72016-10-25 15:08:01 -07001730 shadow_frame.GetMethod(),
1731 self,
1732 false,
1733 do_access_check);
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001734 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001735 DCHECK(self->IsExceptionPending());
1736 return false;
1737 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001738 CHECK(array_class->IsArrayClass());
Mathieu Chartieref41db72016-10-25 15:08:01 -07001739 ObjPtr<mirror::Class> component_class = array_class->GetComponentType();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001740 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
1741 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
1742 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001743 ThrowRuntimeException("Bad filled array request for type %s",
David Sehr709b0702016-10-13 09:12:37 -07001744 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001745 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001746 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -08001747 "Found type %s; filled-new-array not implemented for anything but 'int'",
David Sehr709b0702016-10-13 09:12:37 -07001748 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001749 }
1750 return false;
1751 }
Mathieu Chartieref41db72016-10-25 15:08:01 -07001752 ObjPtr<mirror::Object> new_array = mirror::Array::Alloc<true>(
1753 self,
1754 array_class,
1755 length,
1756 array_class->GetComponentSizeShift(),
1757 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001758 if (UNLIKELY(new_array == nullptr)) {
1759 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001760 return false;
1761 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001762 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
1763 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001764 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +01001765 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001766 } else {
Ian Rogers29a26482014-05-02 15:27:29 -07001767 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +01001768 }
Sebastien Hertzabff6432014-01-27 18:01:39 +01001769 for (int32_t i = 0; i < length; ++i) {
1770 size_t src_reg = is_range ? vregC + i : arg[i];
1771 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001772 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
1773 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +01001774 } else {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001775 new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<transaction_active>(
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001776 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001777 }
1778 }
1779
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001780 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001781 return true;
1782}
1783
Mathieu Chartieref41db72016-10-25 15:08:01 -07001784// TODO: Use ObjPtr here.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001785template<typename T>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001786static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array,
1787 int32_t count)
1788 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001789 Runtime* runtime = Runtime::Current();
1790 for (int32_t i = 0; i < count; ++i) {
1791 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
1792 }
1793}
1794
Mathieu Chartieref41db72016-10-25 15:08:01 -07001795void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001796 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001797 DCHECK(Runtime::Current()->IsActiveTransaction());
1798 DCHECK(array != nullptr);
1799 DCHECK_LE(count, array->GetLength());
1800 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
1801 switch (primitive_component_type) {
1802 case Primitive::kPrimBoolean:
1803 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
1804 break;
1805 case Primitive::kPrimByte:
1806 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
1807 break;
1808 case Primitive::kPrimChar:
1809 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
1810 break;
1811 case Primitive::kPrimShort:
1812 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
1813 break;
1814 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001815 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
1816 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001817 case Primitive::kPrimFloat:
1818 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
1819 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001820 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001821 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
1822 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001823 case Primitive::kPrimDouble:
1824 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
1825 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001826 default:
1827 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
1828 << " in fill-array-data";
1829 break;
1830 }
1831}
1832
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001833// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +02001834#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001835 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001836 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
1837 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +02001838 const Instruction* inst, uint16_t inst_data, \
1839 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001840EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
1841EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
1842EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
1843EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
1844#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001845
Orion Hodsonc069a302017-01-18 09:23:12 +00001846// Explicit DoInvokePolymorphic template function declarations.
1847#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range) \
1848 template REQUIRES_SHARED(Locks::mutator_lock_) \
1849 bool DoInvokePolymorphic<_is_range>( \
1850 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1851 uint16_t inst_data, JValue* result)
1852EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false);
1853EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true);
Narayan Kamath9823e782016-08-03 12:46:58 +01001854#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1855
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001856// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001857#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001858 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001859 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
1860 const ShadowFrame& shadow_frame, \
1861 Thread* self, JValue* result)
1862#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
1863 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
1864 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
1865 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
1866 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
1867EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
1868EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
1869#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001870#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
1871
1872} // namespace interpreter
1873} // namespace art