blob: 43a7085607b73f891775bb449e8fcc3d025fb55d [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
David Srbecky960327b2018-10-25 10:11:59 +000053bool CheckStackOverflow(Thread* self, size_t frame_size)
54 REQUIRES_SHARED(Locks::mutator_lock_) {
55 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
56 uint8_t* stack_end = self->GetStackEndForInterpreter(implicit_check);
57 if (UNLIKELY(__builtin_frame_address(0) < stack_end + frame_size)) {
58 ThrowStackOverflowError(self);
59 return false;
60 }
61 return true;
62}
63
Chang Xingbd208d82017-07-12 14:53:17 -070064template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
65 bool transaction_active>
Ian Rogers54874942014-06-10 16:31:03 -070066bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
67 uint16_t inst_data) {
68 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
69 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010070 ArtField* f =
71 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
72 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -070073 if (UNLIKELY(f == nullptr)) {
74 CHECK(self->IsExceptionPending());
75 return false;
76 }
Mathieu Chartieref41db72016-10-25 15:08:01 -070077 ObjPtr<mirror::Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -070078 if (is_static) {
79 obj = f->GetDeclaringClass();
Chang Xingbd208d82017-07-12 14:53:17 -070080 if (transaction_active) {
81 if (Runtime::Current()->GetTransaction()->ReadConstraint(obj.Ptr(), f)) {
82 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Can't read static fields of "
83 + obj->PrettyTypeOf() + " since it does not belong to clinit's class.");
84 return false;
85 }
86 }
Ian Rogers54874942014-06-10 16:31:03 -070087 } else {
88 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
89 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000090 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers54874942014-06-10 16:31:03 -070091 return false;
92 }
93 }
Orion Hodson3d617ac2016-10-19 14:00:46 +010094
95 JValue result;
Alex Light084fa372017-06-16 08:58:34 -070096 if (UNLIKELY(!DoFieldGetCommon<field_type>(self, shadow_frame, obj, f, &result))) {
97 // Instrumentation threw an error!
98 CHECK(self->IsExceptionPending());
99 return false;
100 }
Ian Rogers54874942014-06-10 16:31:03 -0700101 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
102 switch (field_type) {
103 case Primitive::kPrimBoolean:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100104 shadow_frame.SetVReg(vregA, result.GetZ());
Ian Rogers54874942014-06-10 16:31:03 -0700105 break;
106 case Primitive::kPrimByte:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100107 shadow_frame.SetVReg(vregA, result.GetB());
Ian Rogers54874942014-06-10 16:31:03 -0700108 break;
109 case Primitive::kPrimChar:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100110 shadow_frame.SetVReg(vregA, result.GetC());
Ian Rogers54874942014-06-10 16:31:03 -0700111 break;
112 case Primitive::kPrimShort:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100113 shadow_frame.SetVReg(vregA, result.GetS());
Ian Rogers54874942014-06-10 16:31:03 -0700114 break;
115 case Primitive::kPrimInt:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100116 shadow_frame.SetVReg(vregA, result.GetI());
Ian Rogers54874942014-06-10 16:31:03 -0700117 break;
118 case Primitive::kPrimLong:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100119 shadow_frame.SetVRegLong(vregA, result.GetJ());
Ian Rogers54874942014-06-10 16:31:03 -0700120 break;
121 case Primitive::kPrimNot:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100122 shadow_frame.SetVRegReference(vregA, result.GetL());
Ian Rogers54874942014-06-10 16:31:03 -0700123 break;
124 default:
125 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700126 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700127 }
128 return true;
129}
130
131// Explicitly instantiate all DoFieldGet functions.
Chang Xingbd208d82017-07-12 14:53:17 -0700132#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
133 template bool DoFieldGet<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
Ian Rogers54874942014-06-10 16:31:03 -0700134 ShadowFrame& shadow_frame, \
135 const Instruction* inst, \
136 uint16_t inst_data)
137
138#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
Chang Xingbd208d82017-07-12 14:53:17 -0700139 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false, true); \
140 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false, false); \
141 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true, true); \
142 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true, false);
Ian Rogers54874942014-06-10 16:31:03 -0700143
144// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700145EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
146EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
147EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
148EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
149EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
150EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
151EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700152
153// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700154EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
155EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
156EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
157EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
158EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
159EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
160EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700161
162#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
163#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
164
165// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
166// Returns true on success, otherwise throws an exception and returns false.
167template<Primitive::Type field_type>
168bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700169 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700170 if (UNLIKELY(obj == nullptr)) {
171 // We lost the reference to the field index so we cannot get a more
172 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000173 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700174 return false;
175 }
176 MemberOffset field_offset(inst->VRegC_22c());
177 // Report this field access to instrumentation if needed. Since we only have the offset of
178 // the field from the base of the object, we need to look for it first.
179 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
180 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
181 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
182 field_offset.Uint32Value());
183 DCHECK(f != nullptr);
184 DCHECK(!f->IsStatic());
Alex Light084fa372017-06-16 08:58:34 -0700185 Thread* self = Thread::Current();
186 StackHandleScope<1> hs(self);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700187 // Save obj in case the instrumentation event has thread suspension.
188 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Alex Light084fa372017-06-16 08:58:34 -0700189 instrumentation->FieldReadEvent(self,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700190 obj.Ptr(),
191 shadow_frame.GetMethod(),
192 shadow_frame.GetDexPC(),
193 f);
Alex Light084fa372017-06-16 08:58:34 -0700194 if (UNLIKELY(self->IsExceptionPending())) {
195 return false;
196 }
Ian Rogers54874942014-06-10 16:31:03 -0700197 }
198 // Note: iget-x-quick instructions are only for non-volatile fields.
199 const uint32_t vregA = inst->VRegA_22c(inst_data);
200 switch (field_type) {
201 case Primitive::kPrimInt:
202 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
203 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800204 case Primitive::kPrimBoolean:
205 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
206 break;
207 case Primitive::kPrimByte:
208 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
209 break;
210 case Primitive::kPrimChar:
211 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
212 break;
213 case Primitive::kPrimShort:
214 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
215 break;
Ian Rogers54874942014-06-10 16:31:03 -0700216 case Primitive::kPrimLong:
217 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
218 break;
219 case Primitive::kPrimNot:
220 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
221 break;
222 default:
223 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700224 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700225 }
226 return true;
227}
228
229// Explicitly instantiate all DoIGetQuick functions.
230#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
231 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
232 uint16_t inst_data)
233
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800234EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
235EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
236EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
237EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
238EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
239EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
240EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700241#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
242
243template<Primitive::Type field_type>
244static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700245 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700246 JValue field_value;
247 switch (field_type) {
248 case Primitive::kPrimBoolean:
249 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
250 break;
251 case Primitive::kPrimByte:
252 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
253 break;
254 case Primitive::kPrimChar:
255 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
256 break;
257 case Primitive::kPrimShort:
258 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
259 break;
260 case Primitive::kPrimInt:
261 field_value.SetI(shadow_frame.GetVReg(vreg));
262 break;
263 case Primitive::kPrimLong:
264 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
265 break;
266 case Primitive::kPrimNot:
267 field_value.SetL(shadow_frame.GetVRegReference(vreg));
268 break;
269 default:
270 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700271 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700272 }
273 return field_value;
274}
275
Orion Hodson3d617ac2016-10-19 14:00:46 +0100276template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
277 bool transaction_active>
278bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
279 uint16_t inst_data) {
280 const bool do_assignability_check = do_access_check;
281 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
282 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
283 ArtField* f =
284 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
285 Primitive::ComponentSize(field_type));
286 if (UNLIKELY(f == nullptr)) {
287 CHECK(self->IsExceptionPending());
288 return false;
289 }
290 ObjPtr<mirror::Object> obj;
291 if (is_static) {
292 obj = f->GetDeclaringClass();
Chang Xingbd208d82017-07-12 14:53:17 -0700293 if (transaction_active) {
294 if (Runtime::Current()->GetTransaction()->WriteConstraint(obj.Ptr(), f)) {
295 Runtime::Current()->AbortTransactionAndThrowAbortError(
296 self, "Can't set fields of " + obj->PrettyTypeOf());
297 return false;
298 }
299 }
300
Orion Hodson3d617ac2016-10-19 14:00:46 +0100301 } else {
302 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
303 if (UNLIKELY(obj == nullptr)) {
304 ThrowNullPointerExceptionForFieldAccess(f, false);
305 return false;
306 }
307 }
308
309 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100310 JValue value = GetFieldValue<field_type>(shadow_frame, vregA);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100311 return DoFieldPutCommon<field_type, do_assignability_check, transaction_active>(self,
312 shadow_frame,
313 obj,
314 f,
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100315 value);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100316}
317
Ian Rogers54874942014-06-10 16:31:03 -0700318// Explicitly instantiate all DoFieldPut functions.
319#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
320 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
321 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
322
323#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
324 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
325 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
326 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
327 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
328
329// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700330EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
331EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
332EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
333EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
334EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
335EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
336EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700337
338// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700339EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
340EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
341EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
342EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
343EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
344EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
345EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700346
347#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
348#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
349
350template<Primitive::Type field_type, bool transaction_active>
351bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700352 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700353 if (UNLIKELY(obj == nullptr)) {
354 // We lost the reference to the field index so we cannot get a more
355 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000356 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700357 return false;
358 }
359 MemberOffset field_offset(inst->VRegC_22c());
360 const uint32_t vregA = inst->VRegA_22c(inst_data);
361 // Report this field modification to instrumentation if needed. Since we only have the offset of
362 // the field from the base of the object, we need to look for it first.
363 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
364 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
365 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
366 field_offset.Uint32Value());
367 DCHECK(f != nullptr);
368 DCHECK(!f->IsStatic());
369 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Alex Light084fa372017-06-16 08:58:34 -0700370 Thread* self = Thread::Current();
371 StackHandleScope<2> hs(self);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700372 // Save obj in case the instrumentation event has thread suspension.
373 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Alex Light084fa372017-06-16 08:58:34 -0700374 mirror::Object* fake_root = nullptr;
375 HandleWrapper<mirror::Object> ret(hs.NewHandleWrapper<mirror::Object>(
376 field_type == Primitive::kPrimNot ? field_value.GetGCRoot() : &fake_root));
377 instrumentation->FieldWriteEvent(self,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700378 obj.Ptr(),
379 shadow_frame.GetMethod(),
380 shadow_frame.GetDexPC(),
381 f,
382 field_value);
Alex Light084fa372017-06-16 08:58:34 -0700383 if (UNLIKELY(self->IsExceptionPending())) {
384 return false;
385 }
Alex Light0aa7a5a2018-10-10 15:58:14 +0000386 if (UNLIKELY(shadow_frame.GetForcePopFrame())) {
387 // Don't actually set the field. The next instruction will force us to pop.
388 DCHECK(Runtime::Current()->AreNonStandardExitsEnabled());
389 DCHECK(PrevFrameWillRetry(self, shadow_frame));
390 return true;
391 }
Ian Rogers54874942014-06-10 16:31:03 -0700392 }
393 // Note: iput-x-quick instructions are only for non-volatile fields.
394 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700395 case Primitive::kPrimBoolean:
396 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
397 break;
398 case Primitive::kPrimByte:
399 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
400 break;
401 case Primitive::kPrimChar:
402 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
403 break;
404 case Primitive::kPrimShort:
405 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
406 break;
Ian Rogers54874942014-06-10 16:31:03 -0700407 case Primitive::kPrimInt:
408 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
409 break;
410 case Primitive::kPrimLong:
411 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
412 break;
413 case Primitive::kPrimNot:
414 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
415 break;
416 default:
417 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700418 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700419 }
420 return true;
421}
422
423// Explicitly instantiate all DoIPutQuick functions.
424#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
425 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
426 const Instruction* inst, \
427 uint16_t inst_data)
428
429#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
430 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
431 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
432
Andreas Gampec8ccf682014-09-29 20:07:43 -0700433EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
434EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
435EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
436EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
437EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
438EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
439EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700440#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
441#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
442
Alex Light9fb1ab12017-09-05 09:32:49 -0700443// We execute any instrumentation events that are triggered by this exception and change the
444// shadow_frame's dex_pc to that of the exception handler if there is one in the current method.
445// Return true if we should continue executing in the current method and false if we need to go up
446// the stack to find an exception handler.
Sebastien Hertz520633b2015-09-08 17:03:36 +0200447// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Alex Light9fb1ab12017-09-05 09:32:49 -0700448// TODO We should have a better way to skip instrumentation reporting or possibly rethink that
449// behavior.
450bool MoveToExceptionHandler(Thread* self,
451 ShadowFrame& shadow_frame,
452 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700453 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700454 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000455 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Alex Light9fb1ab12017-09-05 09:32:49 -0700456 if (instrumentation != nullptr &&
457 instrumentation->HasExceptionThrownListeners() &&
458 self->IsExceptionThrownByCurrentMethod(exception.Get())) {
459 // 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 -0700460 instrumentation->ExceptionThrownEvent(self, exception.Get());
Alex Light0aa7a5a2018-10-10 15:58:14 +0000461 if (shadow_frame.GetForcePopFrame()) {
462 // We will check in the caller for GetForcePopFrame again. We need to bail out early to
463 // prevent an ExceptionHandledEvent from also being sent before popping.
464 return true;
465 }
Sebastien Hertz9f102032014-05-23 08:59:42 +0200466 }
Ian Rogers54874942014-06-10 16:31:03 -0700467 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700468 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
Alex Light9fb1ab12017-09-05 09:32:49 -0700469 hs.NewHandle(exception->GetClass()), shadow_frame.GetDexPC(), &clear_exception);
Andreas Gampee2abbc62017-09-15 11:59:26 -0700470 if (found_dex_pc == dex::kDexNoIndex) {
Alex Light9fb1ab12017-09-05 09:32:49 -0700471 if (instrumentation != nullptr) {
472 if (shadow_frame.NeedsNotifyPop()) {
473 instrumentation->WatchedFramePopped(self, shadow_frame);
474 }
475 // Exception is not caught by the current method. We will unwind to the
476 // caller. Notify any instrumentation listener.
477 instrumentation->MethodUnwindEvent(self,
478 shadow_frame.GetThisObject(),
479 shadow_frame.GetMethod(),
480 shadow_frame.GetDexPC());
Alex Lighte814f9d2017-07-31 16:14:39 -0700481 }
Alex Light9fb1ab12017-09-05 09:32:49 -0700482 return false;
Ian Rogers54874942014-06-10 16:31:03 -0700483 } else {
Alex Light9fb1ab12017-09-05 09:32:49 -0700484 shadow_frame.SetDexPC(found_dex_pc);
485 if (instrumentation != nullptr && instrumentation->HasExceptionHandledListeners()) {
486 self->ClearException();
487 instrumentation->ExceptionHandledEvent(self, exception.Get());
488 if (UNLIKELY(self->IsExceptionPending())) {
489 // Exception handled event threw an exception. Try to find the handler for this one.
490 return MoveToExceptionHandler(self, shadow_frame, instrumentation);
491 } else if (!clear_exception) {
492 self->SetException(exception.Get());
493 }
494 } else if (clear_exception) {
Ian Rogers54874942014-06-10 16:31:03 -0700495 self->ClearException();
496 }
Alex Light9fb1ab12017-09-05 09:32:49 -0700497 return true;
Ian Rogers54874942014-06-10 16:31:03 -0700498 }
Ian Rogers54874942014-06-10 16:31:03 -0700499}
500
Ian Rogerse94652f2014-12-02 11:13:19 -0800501void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
502 LOG(FATAL) << "Unexpected instruction: "
503 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
504 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700505}
506
Sebastien Hertz45b15972015-04-03 16:07:05 +0200507void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700508 va_list args;
509 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200510 AbortTransactionV(self, fmt, args);
511 va_end(args);
512}
513
514void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
515 CHECK(Runtime::Current()->IsActiveTransaction());
516 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100517 std::string abort_msg;
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800518 android::base::StringAppendV(&abort_msg, fmt, args);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100519 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200520 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700521}
522
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100523// START DECLARATIONS :
524//
525// These additional declarations are required because clang complains
526// about ALWAYS_INLINE (-Werror, -Wgcc-compat) in definitions.
527//
528
Narayan Kamath9823e782016-08-03 12:46:58 +0100529template <bool is_range, bool do_assignability_check>
Vladimir Markod16363a2017-02-01 14:09:13 +0000530static ALWAYS_INLINE bool DoCallCommon(ArtMethod* called_method,
531 Thread* self,
532 ShadowFrame& shadow_frame,
533 JValue* result,
534 uint16_t number_of_inputs,
535 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
536 uint32_t vregC) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100537
538template <bool is_range>
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000539ALWAYS_INLINE void CopyRegisters(ShadowFrame& caller_frame,
540 ShadowFrame* callee_frame,
541 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
542 const size_t first_src_reg,
543 const size_t first_dest_reg,
544 const size_t num_regs) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100545
546// END DECLARATIONS.
547
Siva Chandra05d24152016-01-05 17:43:17 -0800548void ArtInterpreterToCompiledCodeBridge(Thread* self,
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100549 ArtMethod* caller,
Siva Chandra05d24152016-01-05 17:43:17 -0800550 ShadowFrame* shadow_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -0700551 uint16_t arg_offset,
Siva Chandra05d24152016-01-05 17:43:17 -0800552 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700553 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700554 ArtMethod* method = shadow_frame->GetMethod();
555 // Ensure static methods are initialized.
556 if (method->IsStatic()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700557 ObjPtr<mirror::Class> declaringClass = method->GetDeclaringClass();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700558 if (UNLIKELY(!declaringClass->IsInitialized())) {
559 self->PushShadowFrame(shadow_frame);
560 StackHandleScope<1> hs(self);
561 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
Nicolas Geoffray01822292017-03-09 09:03:19 +0000562 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
563 true))) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700564 self->PopShadowFrame();
565 DCHECK(self->IsExceptionPending());
566 return;
567 }
568 self->PopShadowFrame();
569 CHECK(h_class->IsInitializing());
Nicolas Geoffray01822292017-03-09 09:03:19 +0000570 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
571 method = shadow_frame->GetMethod();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700572 }
573 }
Jeff Hao5ea84132017-05-05 16:59:29 -0700574 // Basic checks for the arg_offset. If there's no code item, the arg_offset must be 0. Otherwise,
575 // check that the arg_offset isn't greater than the number of registers. A stronger check is
576 // difficult since the frame may contain space for all the registers in the method, or only enough
577 // space for the arguments.
578 if (kIsDebugBuild) {
579 if (method->GetCodeItem() == nullptr) {
580 DCHECK_EQ(0u, arg_offset) << method->PrettyMethod();
581 } else {
582 DCHECK_LE(arg_offset, shadow_frame->NumberOfVRegs());
583 }
584 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100585 jit::Jit* jit = Runtime::Current()->GetJit();
586 if (jit != nullptr && caller != nullptr) {
587 jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
588 }
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700589 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
590 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
Andreas Gampe542451c2016-07-26 09:02:02 -0700591 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700592}
593
Mingyao Yangffedec52016-05-19 10:48:40 -0700594void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
595 uint16_t this_obj_vreg,
596 JValue result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700597 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700598 ObjPtr<mirror::Object> existing = shadow_frame->GetVRegReference(this_obj_vreg);
Mingyao Yangffedec52016-05-19 10:48:40 -0700599 if (existing == nullptr) {
600 // If it's null, we come from compiled code that was deoptimized. Nothing to do,
601 // as the compiler verified there was no alias.
602 // Set the new string result of the StringFactory.
603 shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
604 return;
605 }
606 // Set the string init result into all aliases.
607 for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
608 if (shadow_frame->GetVRegReference(i) == existing) {
609 DCHECK_EQ(shadow_frame->GetVRegReference(i),
Vladimir Marko78baed52018-10-11 10:44:58 +0100610 reinterpret_cast32<mirror::Object*>(shadow_frame->GetVReg(i)));
Mingyao Yangffedec52016-05-19 10:48:40 -0700611 shadow_frame->SetVRegReference(i, result.GetL());
612 DCHECK_EQ(shadow_frame->GetVRegReference(i),
Vladimir Marko78baed52018-10-11 10:44:58 +0100613 reinterpret_cast32<mirror::Object*>(shadow_frame->GetVReg(i)));
Mingyao Yangffedec52016-05-19 10:48:40 -0700614 }
615 }
616}
617
Orion Hodsonc069a302017-01-18 09:23:12 +0000618template<bool is_range>
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100619static bool DoMethodHandleInvokeCommon(Thread* self,
620 ShadowFrame& shadow_frame,
621 bool invoke_exact,
622 const Instruction* inst,
623 uint16_t inst_data,
624 JValue* result)
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100625 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Light848574c2017-09-25 16:59:39 -0700626 // Make sure to check for async exceptions
627 if (UNLIKELY(self->ObserveAsyncException())) {
628 return false;
629 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100630 // Invoke-polymorphic instructions always take a receiver. i.e, they are never static.
631 const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc();
Orion Hodson3d617ac2016-10-19 14:00:46 +0100632 const int invoke_method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc();
Narayan Kamath9823e782016-08-03 12:46:58 +0100633
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000634 // Initialize |result| to 0 as this is the default return value for
635 // polymorphic invocations of method handle types with void return
636 // and provides sane return result in error cases.
637 result->SetJ(0);
638
Orion Hodson3d617ac2016-10-19 14:00:46 +0100639 // The invoke_method_idx here is the name of the signature polymorphic method that
Narayan Kamath9823e782016-08-03 12:46:58 +0100640 // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact)
641 // and not the method that we'll dispatch to in the end.
Orion Hodsone7732be2017-10-11 14:35:20 +0100642 StackHandleScope<2> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +0000643 Handle<mirror::MethodHandle> method_handle(hs.NewHandle(
644 ObjPtr<mirror::MethodHandle>::DownCast(
Mathieu Chartieref41db72016-10-25 15:08:01 -0700645 MakeObjPtr(shadow_frame.GetVRegReference(vRegC)))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800646 if (UNLIKELY(method_handle == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100647 // Note that the invoke type is kVirtual here because a call to a signature
648 // polymorphic method is shaped like a virtual call at the bytecode level.
Orion Hodson3d617ac2016-10-19 14:00:46 +0100649 ThrowNullPointerExceptionForMethodAccess(invoke_method_idx, InvokeType::kVirtual);
Narayan Kamath9823e782016-08-03 12:46:58 +0100650 return false;
651 }
652
653 // The vRegH value gives the index of the proto_id associated with this
Orion Hodson811bd5f2016-12-07 11:35:37 +0000654 // signature polymorphic call site.
Orion Hodson06d10a72018-05-14 08:53:38 +0100655 const uint16_t vRegH = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
656 const dex::ProtoIndex callsite_proto_id(vRegH);
Narayan Kamath9823e782016-08-03 12:46:58 +0100657
658 // Call through to the classlinker and ask it to resolve the static type associated
659 // with the callsite. This information is stored in the dex cache so it's
660 // guaranteed to be fast after the first resolution.
Narayan Kamath9823e782016-08-03 12:46:58 +0100661 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Orion Hodsone7732be2017-10-11 14:35:20 +0100662 Handle<mirror::MethodType> callsite_type(hs.NewHandle(
663 class_linker->ResolveMethodType(self, callsite_proto_id, shadow_frame.GetMethod())));
Narayan Kamath9823e782016-08-03 12:46:58 +0100664
665 // This implies we couldn't resolve one or more types in this method handle.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800666 if (UNLIKELY(callsite_type == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100667 CHECK(self->IsExceptionPending());
Narayan Kamath9823e782016-08-03 12:46:58 +0100668 return false;
669 }
670
Orion Hodson811bd5f2016-12-07 11:35:37 +0000671 // There is a common dispatch method for method handles that takes
672 // arguments either from a range or an array of arguments depending
673 // on whether the DEX instruction is invoke-polymorphic/range or
674 // invoke-polymorphic. The array here is for the latter.
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100675 if (UNLIKELY(is_range)) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000676 // VRegC is the register holding the method handle. Arguments passed
677 // to the method handle's target do not include the method handle.
Orion Hodson960d4f72017-11-10 15:32:38 +0000678 RangeInstructionOperands operands(inst->VRegC_4rcc() + 1, inst->VRegA_4rcc() - 1);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100679 if (invoke_exact) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000680 return MethodHandleInvokeExact(self,
681 shadow_frame,
682 method_handle,
683 callsite_type,
684 &operands,
685 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100686 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000687 return MethodHandleInvoke(self,
688 shadow_frame,
689 method_handle,
690 callsite_type,
691 &operands,
692 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100693 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100694 } else {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000695 // Get the register arguments for the invoke.
Orion Hodson960d4f72017-11-10 15:32:38 +0000696 uint32_t args[Instruction::kMaxVarArgRegs] = {};
Orion Hodson811bd5f2016-12-07 11:35:37 +0000697 inst->GetVarArgs(args, inst_data);
698 // Drop the first register which is the method handle performing the invoke.
Alexey Frunze8631a462017-01-19 19:07:37 -0800699 memmove(args, args + 1, sizeof(args[0]) * (Instruction::kMaxVarArgRegs - 1));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000700 args[Instruction::kMaxVarArgRegs - 1] = 0;
Orion Hodson960d4f72017-11-10 15:32:38 +0000701 VarArgsInstructionOperands operands(args, inst->VRegA_45cc() - 1);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100702 if (invoke_exact) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000703 return MethodHandleInvokeExact(self,
704 shadow_frame,
705 method_handle,
706 callsite_type,
707 &operands,
708 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100709 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000710 return MethodHandleInvoke(self,
711 shadow_frame,
712 method_handle,
713 callsite_type,
714 &operands,
715 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100716 }
717 }
718}
719
720bool DoMethodHandleInvokeExact(Thread* self,
721 ShadowFrame& shadow_frame,
722 const Instruction* inst,
723 uint16_t inst_data,
724 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
725 if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) {
726 static const bool kIsRange = false;
727 return DoMethodHandleInvokeCommon<kIsRange>(
728 self, shadow_frame, true /* is_exact */, inst, inst_data, result);
729 } else {
730 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE);
731 static const bool kIsRange = true;
732 return DoMethodHandleInvokeCommon<kIsRange>(
733 self, shadow_frame, true /* is_exact */, inst, inst_data, result);
734 }
735}
736
737bool DoMethodHandleInvoke(Thread* self,
738 ShadowFrame& shadow_frame,
739 const Instruction* inst,
740 uint16_t inst_data,
741 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
742 if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) {
743 static const bool kIsRange = false;
744 return DoMethodHandleInvokeCommon<kIsRange>(
745 self, shadow_frame, false /* is_exact */, inst, inst_data, result);
746 } else {
747 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE);
748 static const bool kIsRange = true;
749 return DoMethodHandleInvokeCommon<kIsRange>(
750 self, shadow_frame, false /* is_exact */, inst, inst_data, result);
751 }
752}
753
Orion Hodson928033d2018-02-07 05:30:54 +0000754static bool DoVarHandleInvokeCommon(Thread* self,
755 ShadowFrame& shadow_frame,
756 const Instruction* inst,
757 uint16_t inst_data,
758 JValue* result,
759 mirror::VarHandle::AccessMode access_mode)
760 REQUIRES_SHARED(Locks::mutator_lock_) {
761 // Make sure to check for async exceptions
762 if (UNLIKELY(self->ObserveAsyncException())) {
763 return false;
764 }
765
Orion Hodson928033d2018-02-07 05:30:54 +0000766 StackHandleScope<2> hs(self);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100767 bool is_var_args = inst->HasVarArgs();
Orion Hodson06d10a72018-05-14 08:53:38 +0100768 const uint16_t vRegH = is_var_args ? inst->VRegH_45cc() : inst->VRegH_4rcc();
Orion Hodson928033d2018-02-07 05:30:54 +0000769 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
770 Handle<mirror::MethodType> callsite_type(hs.NewHandle(
Orion Hodson06d10a72018-05-14 08:53:38 +0100771 class_linker->ResolveMethodType(self, dex::ProtoIndex(vRegH), shadow_frame.GetMethod())));
Orion Hodson928033d2018-02-07 05:30:54 +0000772 // This implies we couldn't resolve one or more types in this VarHandle.
773 if (UNLIKELY(callsite_type == nullptr)) {
774 CHECK(self->IsExceptionPending());
775 return false;
776 }
777
Orion Hodson537a4fe2018-05-15 13:57:58 +0100778 const uint32_t vRegC = is_var_args ? inst->VRegC_45cc() : inst->VRegC_4rcc();
779 ObjPtr<mirror::Object> receiver(shadow_frame.GetVRegReference(vRegC));
780 Handle<mirror::VarHandle> var_handle(hs.NewHandle(down_cast<mirror::VarHandle*>(receiver.Ptr())));
Orion Hodson928033d2018-02-07 05:30:54 +0000781 if (is_var_args) {
782 uint32_t args[Instruction::kMaxVarArgRegs];
783 inst->GetVarArgs(args, inst_data);
784 VarArgsInstructionOperands all_operands(args, inst->VRegA_45cc());
785 NoReceiverInstructionOperands operands(&all_operands);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100786 return VarHandleInvokeAccessor(self,
787 shadow_frame,
788 var_handle,
789 callsite_type,
790 access_mode,
791 &operands,
792 result);
Orion Hodson928033d2018-02-07 05:30:54 +0000793 } else {
794 RangeInstructionOperands all_operands(inst->VRegC_4rcc(), inst->VRegA_4rcc());
795 NoReceiverInstructionOperands operands(&all_operands);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100796 return VarHandleInvokeAccessor(self,
797 shadow_frame,
798 var_handle,
799 callsite_type,
800 access_mode,
801 &operands,
802 result);
Orion Hodson928033d2018-02-07 05:30:54 +0000803 }
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100804}
805
Orion Hodson928033d2018-02-07 05:30:54 +0000806#define DO_VAR_HANDLE_ACCESSOR(_access_mode) \
807bool DoVarHandle ## _access_mode(Thread* self, \
808 ShadowFrame& shadow_frame, \
809 const Instruction* inst, \
810 uint16_t inst_data, \
811 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { \
812 const auto access_mode = mirror::VarHandle::AccessMode::k ## _access_mode; \
813 return DoVarHandleInvokeCommon(self, shadow_frame, inst, inst_data, result, access_mode); \
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100814}
815
Orion Hodson928033d2018-02-07 05:30:54 +0000816DO_VAR_HANDLE_ACCESSOR(CompareAndExchange)
817DO_VAR_HANDLE_ACCESSOR(CompareAndExchangeAcquire)
818DO_VAR_HANDLE_ACCESSOR(CompareAndExchangeRelease)
819DO_VAR_HANDLE_ACCESSOR(CompareAndSet)
820DO_VAR_HANDLE_ACCESSOR(Get)
821DO_VAR_HANDLE_ACCESSOR(GetAcquire)
822DO_VAR_HANDLE_ACCESSOR(GetAndAdd)
823DO_VAR_HANDLE_ACCESSOR(GetAndAddAcquire)
824DO_VAR_HANDLE_ACCESSOR(GetAndAddRelease)
825DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAnd)
826DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAndAcquire)
827DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAndRelease)
828DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOr)
829DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOrAcquire)
830DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOrRelease)
831DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXor)
832DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXorAcquire)
833DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXorRelease)
834DO_VAR_HANDLE_ACCESSOR(GetAndSet)
835DO_VAR_HANDLE_ACCESSOR(GetAndSetAcquire)
836DO_VAR_HANDLE_ACCESSOR(GetAndSetRelease)
837DO_VAR_HANDLE_ACCESSOR(GetOpaque)
838DO_VAR_HANDLE_ACCESSOR(GetVolatile)
839DO_VAR_HANDLE_ACCESSOR(Set)
840DO_VAR_HANDLE_ACCESSOR(SetOpaque)
841DO_VAR_HANDLE_ACCESSOR(SetRelease)
842DO_VAR_HANDLE_ACCESSOR(SetVolatile)
843DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSet)
844DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetAcquire)
845DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetPlain)
846DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetRelease)
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100847
Orion Hodson928033d2018-02-07 05:30:54 +0000848#undef DO_VAR_HANDLE_ACCESSOR
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100849
850template<bool is_range>
851bool DoInvokePolymorphic(Thread* self,
852 ShadowFrame& shadow_frame,
853 const Instruction* inst,
854 uint16_t inst_data,
855 JValue* result) {
856 const int invoke_method_idx = inst->VRegB();
857 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
858 ArtMethod* invoke_method =
859 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
860 self, invoke_method_idx, shadow_frame.GetMethod(), kVirtual);
861
862 // Ensure intrinsic identifiers are initialized.
863 DCHECK(invoke_method->IsIntrinsic());
864
865 // Dispatch based on intrinsic identifier associated with method.
866 switch (static_cast<art::Intrinsics>(invoke_method->GetIntrinsic())) {
867#define CASE_SIGNATURE_POLYMORPHIC_INTRINSIC(Name, ...) \
868 case Intrinsics::k##Name: \
869 return Do ## Name(self, shadow_frame, inst, inst_data, result);
870#include "intrinsics_list.h"
871 SIGNATURE_POLYMORPHIC_INTRINSICS_LIST(CASE_SIGNATURE_POLYMORPHIC_INTRINSIC)
872#undef INTRINSICS_LIST
873#undef SIGNATURE_POLYMORPHIC_INTRINSICS_LIST
874#undef CASE_SIGNATURE_POLYMORPHIC_INTRINSIC
875 default:
876 LOG(FATAL) << "Unreachable: " << invoke_method->GetIntrinsic();
877 UNREACHABLE();
878 return false;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100879 }
880}
881
Orion Hodsona5dca522018-02-27 12:42:11 +0000882static JValue ConvertScalarBootstrapArgument(jvalue value) {
883 // value either contains a primitive scalar value if it corresponds
884 // to a primitive type, or it contains an integer value if it
885 // corresponds to an object instance reference id (e.g. a string id).
886 return JValue::FromPrimitive(value.j);
887}
888
889static ObjPtr<mirror::Class> GetClassForBootstrapArgument(EncodedArrayValueIterator::ValueType type)
890 REQUIRES_SHARED(Locks::mutator_lock_) {
891 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100892 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = class_linker->GetClassRoots();
Orion Hodsona5dca522018-02-27 12:42:11 +0000893 switch (type) {
894 case EncodedArrayValueIterator::ValueType::kBoolean:
895 case EncodedArrayValueIterator::ValueType::kByte:
896 case EncodedArrayValueIterator::ValueType::kChar:
897 case EncodedArrayValueIterator::ValueType::kShort:
898 // These types are disallowed by JVMS. Treat as integers. This
899 // will result in CCE's being raised if the BSM has one of these
900 // types.
901 case EncodedArrayValueIterator::ValueType::kInt:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100902 return GetClassRoot(ClassRoot::kPrimitiveInt, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000903 case EncodedArrayValueIterator::ValueType::kLong:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100904 return GetClassRoot(ClassRoot::kPrimitiveLong, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000905 case EncodedArrayValueIterator::ValueType::kFloat:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100906 return GetClassRoot(ClassRoot::kPrimitiveFloat, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000907 case EncodedArrayValueIterator::ValueType::kDouble:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100908 return GetClassRoot(ClassRoot::kPrimitiveDouble, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000909 case EncodedArrayValueIterator::ValueType::kMethodType:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100910 return GetClassRoot<mirror::MethodType>(class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000911 case EncodedArrayValueIterator::ValueType::kMethodHandle:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100912 return GetClassRoot<mirror::MethodHandle>(class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000913 case EncodedArrayValueIterator::ValueType::kString:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100914 return GetClassRoot<mirror::String>();
Orion Hodsona5dca522018-02-27 12:42:11 +0000915 case EncodedArrayValueIterator::ValueType::kType:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100916 return GetClassRoot<mirror::Class>();
Orion Hodsona5dca522018-02-27 12:42:11 +0000917 case EncodedArrayValueIterator::ValueType::kField:
918 case EncodedArrayValueIterator::ValueType::kMethod:
919 case EncodedArrayValueIterator::ValueType::kEnum:
920 case EncodedArrayValueIterator::ValueType::kArray:
921 case EncodedArrayValueIterator::ValueType::kAnnotation:
922 case EncodedArrayValueIterator::ValueType::kNull:
923 return nullptr;
924 }
925}
926
927static bool GetArgumentForBootstrapMethod(Thread* self,
928 ArtMethod* referrer,
929 EncodedArrayValueIterator::ValueType type,
930 const JValue* encoded_value,
931 JValue* decoded_value)
932 REQUIRES_SHARED(Locks::mutator_lock_) {
933 // The encoded_value contains either a scalar value (IJDF) or a
934 // scalar DEX file index to a reference type to be materialized.
935 switch (type) {
936 case EncodedArrayValueIterator::ValueType::kInt:
937 case EncodedArrayValueIterator::ValueType::kFloat:
938 decoded_value->SetI(encoded_value->GetI());
939 return true;
940 case EncodedArrayValueIterator::ValueType::kLong:
941 case EncodedArrayValueIterator::ValueType::kDouble:
942 decoded_value->SetJ(encoded_value->GetJ());
943 return true;
944 case EncodedArrayValueIterator::ValueType::kMethodType: {
945 StackHandleScope<2> hs(self);
946 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader()));
947 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
Orion Hodson06d10a72018-05-14 08:53:38 +0100948 dex::ProtoIndex proto_idx(encoded_value->GetC());
Orion Hodsona5dca522018-02-27 12:42:11 +0000949 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Orion Hodson06d10a72018-05-14 08:53:38 +0100950 ObjPtr<mirror::MethodType> o =
951 cl->ResolveMethodType(self, proto_idx, dex_cache, class_loader);
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::kMethodHandle: {
960 uint32_t index = static_cast<uint32_t>(encoded_value->GetI());
961 ClassLinker* cl = Runtime::Current()->GetClassLinker();
962 ObjPtr<mirror::MethodHandle> o = cl->ResolveMethodHandle(self, index, referrer);
963 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::kString: {
Orion Hodsona5dca522018-02-27 12:42:11 +0000971 dex::StringIndex index(static_cast<uint32_t>(encoded_value->GetI()));
972 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Vladimir Marko18090d12018-06-01 16:53:12 +0100973 ObjPtr<mirror::String> o = cl->ResolveString(index, referrer);
Orion Hodsona5dca522018-02-27 12:42:11 +0000974 if (UNLIKELY(o.IsNull())) {
975 DCHECK(self->IsExceptionPending());
976 return false;
977 }
978 decoded_value->SetL(o);
979 return true;
980 }
981 case EncodedArrayValueIterator::ValueType::kType: {
Orion Hodsona5dca522018-02-27 12:42:11 +0000982 dex::TypeIndex index(static_cast<uint32_t>(encoded_value->GetI()));
983 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100984 ObjPtr<mirror::Class> o = cl->ResolveType(index, referrer);
Orion Hodsona5dca522018-02-27 12:42:11 +0000985 if (UNLIKELY(o.IsNull())) {
986 DCHECK(self->IsExceptionPending());
987 return false;
988 }
989 decoded_value->SetL(o);
990 return true;
991 }
992 case EncodedArrayValueIterator::ValueType::kBoolean:
993 case EncodedArrayValueIterator::ValueType::kByte:
994 case EncodedArrayValueIterator::ValueType::kChar:
995 case EncodedArrayValueIterator::ValueType::kShort:
996 case EncodedArrayValueIterator::ValueType::kField:
997 case EncodedArrayValueIterator::ValueType::kMethod:
998 case EncodedArrayValueIterator::ValueType::kEnum:
999 case EncodedArrayValueIterator::ValueType::kArray:
1000 case EncodedArrayValueIterator::ValueType::kAnnotation:
1001 case EncodedArrayValueIterator::ValueType::kNull:
1002 // Unreachable - unsupported types that have been checked when
1003 // determining the effect call site type based on the bootstrap
1004 // argument types.
1005 UNREACHABLE();
1006 }
1007}
1008
1009static bool PackArgumentForBootstrapMethod(Thread* self,
1010 ArtMethod* referrer,
1011 CallSiteArrayValueIterator* it,
1012 ShadowFrameSetter* setter)
1013 REQUIRES_SHARED(Locks::mutator_lock_) {
1014 auto type = it->GetValueType();
1015 const JValue encoded_value = ConvertScalarBootstrapArgument(it->GetJavaValue());
1016 JValue decoded_value;
1017 if (!GetArgumentForBootstrapMethod(self, referrer, type, &encoded_value, &decoded_value)) {
1018 return false;
1019 }
1020 switch (it->GetValueType()) {
1021 case EncodedArrayValueIterator::ValueType::kInt:
1022 case EncodedArrayValueIterator::ValueType::kFloat:
1023 setter->Set(static_cast<uint32_t>(decoded_value.GetI()));
1024 return true;
1025 case EncodedArrayValueIterator::ValueType::kLong:
1026 case EncodedArrayValueIterator::ValueType::kDouble:
1027 setter->SetLong(decoded_value.GetJ());
1028 return true;
1029 case EncodedArrayValueIterator::ValueType::kMethodType:
1030 case EncodedArrayValueIterator::ValueType::kMethodHandle:
1031 case EncodedArrayValueIterator::ValueType::kString:
1032 case EncodedArrayValueIterator::ValueType::kType:
1033 setter->SetReference(decoded_value.GetL());
1034 return true;
1035 case EncodedArrayValueIterator::ValueType::kBoolean:
1036 case EncodedArrayValueIterator::ValueType::kByte:
1037 case EncodedArrayValueIterator::ValueType::kChar:
1038 case EncodedArrayValueIterator::ValueType::kShort:
1039 case EncodedArrayValueIterator::ValueType::kField:
1040 case EncodedArrayValueIterator::ValueType::kMethod:
1041 case EncodedArrayValueIterator::ValueType::kEnum:
1042 case EncodedArrayValueIterator::ValueType::kArray:
1043 case EncodedArrayValueIterator::ValueType::kAnnotation:
1044 case EncodedArrayValueIterator::ValueType::kNull:
1045 // Unreachable - unsupported types that have been checked when
1046 // determining the effect call site type based on the bootstrap
1047 // argument types.
1048 UNREACHABLE();
1049 }
1050}
1051
1052static bool PackCollectorArrayForBootstrapMethod(Thread* self,
1053 ArtMethod* referrer,
1054 ObjPtr<mirror::Class> array_type,
1055 int32_t array_length,
1056 CallSiteArrayValueIterator* it,
1057 ShadowFrameSetter* setter)
1058 REQUIRES_SHARED(Locks::mutator_lock_) {
1059 StackHandleScope<1> hs(self);
1060 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1061 JValue decoded_value;
1062
1063#define COLLECT_PRIMITIVE_ARRAY(Descriptor, Type) \
1064 Handle<mirror::Type ## Array> array = \
1065 hs.NewHandle(mirror::Type ## Array::Alloc(self, array_length)); \
1066 if (array.IsNull()) { \
1067 return false; \
1068 } \
1069 for (int32_t i = 0; it->HasNext(); it->Next(), ++i) { \
1070 auto type = it->GetValueType(); \
1071 DCHECK_EQ(type, EncodedArrayValueIterator::ValueType::k ## Type); \
1072 const JValue encoded_value = \
1073 ConvertScalarBootstrapArgument(it->GetJavaValue()); \
1074 GetArgumentForBootstrapMethod(self, \
1075 referrer, \
1076 type, \
1077 &encoded_value, \
1078 &decoded_value); \
1079 array->Set(i, decoded_value.Get ## Descriptor()); \
1080 } \
1081 setter->SetReference(array.Get()); \
1082 return true;
1083
1084#define COLLECT_REFERENCE_ARRAY(T, Type) \
1085 Handle<mirror::ObjectArray<T>> array = \
1086 hs.NewHandle(mirror::ObjectArray<T>::Alloc(self, \
1087 array_type, \
1088 array_length)); \
1089 if (array.IsNull()) { \
1090 return false; \
1091 } \
1092 for (int32_t i = 0; it->HasNext(); it->Next(), ++i) { \
1093 auto type = it->GetValueType(); \
1094 DCHECK_EQ(type, EncodedArrayValueIterator::ValueType::k ## Type); \
1095 const JValue encoded_value = \
1096 ConvertScalarBootstrapArgument(it->GetJavaValue()); \
1097 if (!GetArgumentForBootstrapMethod(self, \
1098 referrer, \
1099 type, \
1100 &encoded_value, \
1101 &decoded_value)) { \
1102 return false; \
1103 } \
1104 ObjPtr<mirror::Object> o = decoded_value.GetL(); \
1105 if (Runtime::Current()->IsActiveTransaction()) { \
1106 array->Set<true>(i, ObjPtr<T>::DownCast(o)); \
1107 } else { \
1108 array->Set<false>(i, ObjPtr<T>::DownCast(o)); \
1109 } \
1110 } \
1111 setter->SetReference(array.Get()); \
1112 return true;
1113
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001114 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = class_linker->GetClassRoots();
1115 ObjPtr<mirror::Class> component_type = array_type->GetComponentType();
1116 if (component_type == GetClassRoot(ClassRoot::kPrimitiveInt, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001117 COLLECT_PRIMITIVE_ARRAY(I, Int);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001118 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveLong, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001119 COLLECT_PRIMITIVE_ARRAY(J, Long);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001120 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveFloat, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001121 COLLECT_PRIMITIVE_ARRAY(F, Float);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001122 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveDouble, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001123 COLLECT_PRIMITIVE_ARRAY(D, Double);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001124 } else if (component_type == GetClassRoot<mirror::MethodType>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001125 COLLECT_REFERENCE_ARRAY(mirror::MethodType, MethodType);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001126 } else if (component_type == GetClassRoot<mirror::MethodHandle>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001127 COLLECT_REFERENCE_ARRAY(mirror::MethodHandle, MethodHandle);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001128 } else if (component_type == GetClassRoot<mirror::String>(class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001129 COLLECT_REFERENCE_ARRAY(mirror::String, String);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001130 } else if (component_type == GetClassRoot<mirror::Class>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001131 COLLECT_REFERENCE_ARRAY(mirror::Class, Type);
1132 } else {
1133 UNREACHABLE();
1134 }
1135 #undef COLLECT_PRIMITIVE_ARRAY
1136 #undef COLLECT_REFERENCE_ARRAY
1137}
1138
1139static ObjPtr<mirror::MethodType> BuildCallSiteForBootstrapMethod(Thread* self,
1140 const DexFile* dex_file,
1141 uint32_t call_site_idx)
1142 REQUIRES_SHARED(Locks::mutator_lock_) {
1143 const DexFile::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx);
1144 CallSiteArrayValueIterator it(*dex_file, csi);
1145 DCHECK_GE(it.Size(), 1u);
1146
1147 StackHandleScope<2> hs(self);
1148 // Create array for parameter types.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001149 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001150 ObjPtr<mirror::Class> class_array_type =
1151 GetClassRoot<mirror::ObjectArray<mirror::Class>>(class_linker);
Orion Hodsona5dca522018-02-27 12:42:11 +00001152 Handle<mirror::ObjectArray<mirror::Class>> ptypes = hs.NewHandle(
1153 mirror::ObjectArray<mirror::Class>::Alloc(self,
1154 class_array_type,
1155 static_cast<int>(it.Size())));
1156 if (ptypes.IsNull()) {
1157 DCHECK(self->IsExceptionPending());
1158 return nullptr;
1159 }
1160
1161 // Populate the first argument with an instance of j.l.i.MethodHandles.Lookup
1162 // that the runtime will construct.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001163 ptypes->Set(0, GetClassRoot<mirror::MethodHandlesLookup>(class_linker));
Orion Hodsona5dca522018-02-27 12:42:11 +00001164 it.Next();
1165
1166 // The remaining parameter types are derived from the types of
1167 // arguments present in the DEX file.
1168 int index = 1;
1169 while (it.HasNext()) {
1170 ObjPtr<mirror::Class> ptype = GetClassForBootstrapArgument(it.GetValueType());
1171 if (ptype.IsNull()) {
1172 ThrowClassCastException("Unsupported bootstrap argument type");
1173 return nullptr;
1174 }
1175 ptypes->Set(index, ptype);
1176 index++;
1177 it.Next();
1178 }
1179 DCHECK_EQ(static_cast<size_t>(index), it.Size());
1180
1181 // By definition, the return type is always a j.l.i.CallSite.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001182 Handle<mirror::Class> rtype = hs.NewHandle(GetClassRoot<mirror::CallSite>());
Orion Hodsona5dca522018-02-27 12:42:11 +00001183 return mirror::MethodType::Create(self, rtype, ptypes);
1184}
1185
Orion Hodsonc069a302017-01-18 09:23:12 +00001186static ObjPtr<mirror::CallSite> InvokeBootstrapMethod(Thread* self,
1187 ShadowFrame& shadow_frame,
1188 uint32_t call_site_idx)
1189 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001190 StackHandleScope<5> hs(self);
Orion Hodsona5dca522018-02-27 12:42:11 +00001191 // There are three mandatory arguments expected from the call site
1192 // value array in the DEX file: the bootstrap method handle, the
1193 // method name to pass to the bootstrap method, and the method type
1194 // to pass to the bootstrap method.
1195 static constexpr size_t kMandatoryArgumentsCount = 3;
Orion Hodsonc069a302017-01-18 09:23:12 +00001196 ArtMethod* referrer = shadow_frame.GetMethod();
1197 const DexFile* dex_file = referrer->GetDexFile();
1198 const DexFile::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00001199 CallSiteArrayValueIterator it(*dex_file, csi);
Orion Hodsona5dca522018-02-27 12:42:11 +00001200 if (it.Size() < kMandatoryArgumentsCount) {
1201 ThrowBootstrapMethodError("Truncated bootstrap arguments (%zu < %zu)",
1202 it.Size(), kMandatoryArgumentsCount);
1203 return nullptr;
1204 }
1205
1206 if (it.GetValueType() != EncodedArrayValueIterator::ValueType::kMethodHandle) {
1207 ThrowBootstrapMethodError("First bootstrap argument is not a method handle");
1208 return nullptr;
1209 }
1210
1211 uint32_t bsm_index = static_cast<uint32_t>(it.GetJavaValue().i);
1212 it.Next();
1213
Orion Hodsonc069a302017-01-18 09:23:12 +00001214 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Orion Hodsona5dca522018-02-27 12:42:11 +00001215 Handle<mirror::MethodHandle> bsm =
1216 hs.NewHandle(class_linker->ResolveMethodHandle(self, bsm_index, referrer));
1217 if (bsm.IsNull()) {
Orion Hodsonc069a302017-01-18 09:23:12 +00001218 DCHECK(self->IsExceptionPending());
1219 return nullptr;
1220 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001221
Orion Hodsona5dca522018-02-27 12:42:11 +00001222 if (bsm->GetHandleKind() != mirror::MethodHandle::Kind::kInvokeStatic) {
1223 // JLS suggests also accepting constructors. This is currently
1224 // hard as constructor invocations happen via transformers in ART
1225 // today. The constructor would need to be a class derived from java.lang.invoke.CallSite.
1226 ThrowBootstrapMethodError("Unsupported bootstrap method invocation kind");
1227 return nullptr;
1228 }
1229
1230 // Construct the local call site type information based on the 3
1231 // mandatory arguments provided by the runtime and the static arguments
1232 // in the DEX file. We will use these arguments to build a shadow frame.
1233 MutableHandle<mirror::MethodType> call_site_type =
1234 hs.NewHandle(BuildCallSiteForBootstrapMethod(self, dex_file, call_site_idx));
1235 if (call_site_type.IsNull()) {
1236 DCHECK(self->IsExceptionPending());
1237 return nullptr;
1238 }
1239
1240 // Check if this BSM is targeting a variable arity method. If so,
1241 // we'll need to collect the trailing arguments into an array.
1242 Handle<mirror::Array> collector_arguments;
1243 int32_t collector_arguments_length;
1244 if (bsm->GetTargetMethod()->IsVarargs()) {
1245 int number_of_bsm_parameters = bsm->GetMethodType()->GetNumberOfPTypes();
1246 if (number_of_bsm_parameters == 0) {
1247 ThrowBootstrapMethodError("Variable arity BSM does not have any arguments");
1248 return nullptr;
1249 }
1250 Handle<mirror::Class> collector_array_class =
1251 hs.NewHandle(bsm->GetMethodType()->GetPTypes()->Get(number_of_bsm_parameters - 1));
1252 if (!collector_array_class->IsArrayClass()) {
1253 ThrowBootstrapMethodError("Variable arity BSM does not have array as final argument");
1254 return nullptr;
1255 }
1256 // The call site may include no arguments to be collected. In this
1257 // case the number of arguments must be at least the number of BSM
1258 // parameters less the collector array.
1259 if (call_site_type->GetNumberOfPTypes() < number_of_bsm_parameters - 1) {
1260 ThrowWrongMethodTypeException(bsm->GetMethodType(), call_site_type.Get());
1261 return nullptr;
1262 }
1263 // Check all the arguments to be collected match the collector array component type.
1264 for (int i = number_of_bsm_parameters - 1; i < call_site_type->GetNumberOfPTypes(); ++i) {
1265 if (call_site_type->GetPTypes()->Get(i) != collector_array_class->GetComponentType()) {
1266 ThrowClassCastException(collector_array_class->GetComponentType(),
1267 call_site_type->GetPTypes()->Get(i));
1268 return nullptr;
1269 }
1270 }
1271 // Update the call site method type so it now includes the collector array.
1272 int32_t collector_arguments_start = number_of_bsm_parameters - 1;
1273 collector_arguments_length = call_site_type->GetNumberOfPTypes() - number_of_bsm_parameters + 1;
1274 call_site_type.Assign(
1275 mirror::MethodType::CollectTrailingArguments(self,
1276 call_site_type.Get(),
1277 collector_array_class.Get(),
1278 collector_arguments_start));
1279 if (call_site_type.IsNull()) {
1280 DCHECK(self->IsExceptionPending());
1281 return nullptr;
1282 }
1283 } else {
1284 collector_arguments_length = 0;
1285 }
1286
1287 if (call_site_type->GetNumberOfPTypes() != bsm->GetMethodType()->GetNumberOfPTypes()) {
1288 ThrowWrongMethodTypeException(bsm->GetMethodType(), call_site_type.Get());
1289 return nullptr;
1290 }
1291
1292 // BSM invocation has a different set of exceptions that
1293 // j.l.i.MethodHandle.invoke(). Scan arguments looking for CCE
1294 // "opportunities". Unfortunately we cannot just leave this to the
1295 // method handle invocation as this might generate a WMTE.
1296 for (int32_t i = 0; i < call_site_type->GetNumberOfPTypes(); ++i) {
1297 ObjPtr<mirror::Class> from = call_site_type->GetPTypes()->Get(i);
1298 ObjPtr<mirror::Class> to = bsm->GetMethodType()->GetPTypes()->Get(i);
1299 if (!IsParameterTypeConvertible(from, to)) {
1300 ThrowClassCastException(from, to);
1301 return nullptr;
1302 }
1303 }
1304 if (!IsReturnTypeConvertible(call_site_type->GetRType(), bsm->GetMethodType()->GetRType())) {
1305 ThrowClassCastException(bsm->GetMethodType()->GetRType(), call_site_type->GetRType());
1306 return nullptr;
1307 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001308
1309 // Set-up a shadow frame for invoking the bootstrap method handle.
1310 ShadowFrameAllocaUniquePtr bootstrap_frame =
Orion Hodsona5dca522018-02-27 12:42:11 +00001311 CREATE_SHADOW_FRAME(call_site_type->NumberOfVRegs(),
1312 nullptr,
1313 referrer,
1314 shadow_frame.GetDexPC());
Orion Hodsonc069a302017-01-18 09:23:12 +00001315 ScopedStackedShadowFramePusher pusher(
1316 self, bootstrap_frame.get(), StackedShadowFrameType::kShadowFrameUnderConstruction);
Orion Hodsona5dca522018-02-27 12:42:11 +00001317 ShadowFrameSetter setter(bootstrap_frame.get(), 0u);
Orion Hodsonc069a302017-01-18 09:23:12 +00001318
1319 // The first parameter is a MethodHandles lookup instance.
Orion Hodsona5dca522018-02-27 12:42:11 +00001320 Handle<mirror::Class> lookup_class =
1321 hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass());
1322 ObjPtr<mirror::MethodHandlesLookup> lookup =
1323 mirror::MethodHandlesLookup::Create(self, lookup_class);
1324 if (lookup.IsNull()) {
Orion Hodsonc069a302017-01-18 09:23:12 +00001325 DCHECK(self->IsExceptionPending());
1326 return nullptr;
1327 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001328 setter.SetReference(lookup);
Orion Hodsonc069a302017-01-18 09:23:12 +00001329
Orion Hodsona5dca522018-02-27 12:42:11 +00001330 // Pack the remaining arguments into the frame.
1331 int number_of_arguments = call_site_type->GetNumberOfPTypes();
1332 int argument_index;
1333 for (argument_index = 1; argument_index < number_of_arguments; ++argument_index) {
1334 if (argument_index == number_of_arguments - 1 &&
1335 call_site_type->GetPTypes()->Get(argument_index)->IsArrayClass()) {
1336 ObjPtr<mirror::Class> array_type = call_site_type->GetPTypes()->Get(argument_index);
1337 if (!PackCollectorArrayForBootstrapMethod(self,
1338 referrer,
1339 array_type,
1340 collector_arguments_length,
1341 &it,
1342 &setter)) {
1343 DCHECK(self->IsExceptionPending());
1344 return nullptr;
Orion Hodsonc069a302017-01-18 09:23:12 +00001345 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001346 } else if (!PackArgumentForBootstrapMethod(self, referrer, &it, &setter)) {
1347 DCHECK(self->IsExceptionPending());
1348 return nullptr;
Orion Hodsonc069a302017-01-18 09:23:12 +00001349 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001350 it.Next();
1351 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001352 DCHECK(!it.HasNext());
1353 DCHECK(setter.Done());
Orion Hodsonc069a302017-01-18 09:23:12 +00001354
1355 // Invoke the bootstrap method handle.
1356 JValue result;
Orion Hodsona5dca522018-02-27 12:42:11 +00001357 RangeInstructionOperands operands(0, bootstrap_frame->NumberOfVRegs());
1358 bool invoke_success = MethodHandleInvoke(self,
1359 *bootstrap_frame,
1360 bsm,
1361 call_site_type,
1362 &operands,
1363 &result);
Orion Hodsonc069a302017-01-18 09:23:12 +00001364 if (!invoke_success) {
1365 DCHECK(self->IsExceptionPending());
1366 return nullptr;
1367 }
1368
1369 Handle<mirror::Object> object(hs.NewHandle(result.GetL()));
Orion Hodsonc069a302017-01-18 09:23:12 +00001370 if (UNLIKELY(object.IsNull())) {
Orion Hodsonda1cdd02018-01-31 18:08:28 +00001371 // This will typically be for LambdaMetafactory which is not supported.
Orion Hodson76e6adb2018-02-23 13:15:55 +00001372 ThrowClassCastException("Bootstrap method returned null");
Orion Hodsonc069a302017-01-18 09:23:12 +00001373 return nullptr;
1374 }
1375
Orion Hodsona5dca522018-02-27 12:42:11 +00001376 // Check the result type is a subclass of j.l.i.CallSite.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001377 ObjPtr<mirror::Class> call_site_class = GetClassRoot<mirror::CallSite>(class_linker);
1378 if (UNLIKELY(!object->InstanceOf(call_site_class))) {
1379 ThrowClassCastException(object->GetClass(), call_site_class);
Orion Hodsonc069a302017-01-18 09:23:12 +00001380 return nullptr;
1381 }
1382
Orion Hodsona5dca522018-02-27 12:42:11 +00001383 // Check the call site target is not null as we're going to invoke it.
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001384 ObjPtr<mirror::CallSite> call_site =
1385 ObjPtr<mirror::CallSite>::DownCast(ObjPtr<mirror::Object>(result.GetL()));
1386 ObjPtr<mirror::MethodHandle> target = call_site->GetTarget();
1387 if (UNLIKELY(target == nullptr)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001388 ThrowClassCastException("Bootstrap method returned a CallSite with a null target");
Orion Hodsonc069a302017-01-18 09:23:12 +00001389 return nullptr;
1390 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001391 return call_site;
Orion Hodsonc069a302017-01-18 09:23:12 +00001392}
1393
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001394namespace {
1395
1396ObjPtr<mirror::CallSite> DoResolveCallSite(Thread* self,
1397 ShadowFrame& shadow_frame,
1398 uint32_t call_site_idx)
1399 REQUIRES_SHARED(Locks::mutator_lock_) {
1400 StackHandleScope<1> hs(self);
1401 Handle<mirror::DexCache> dex_cache(hs.NewHandle(shadow_frame.GetMethod()->GetDexCache()));
1402
1403 // Get the call site from the DexCache if present.
1404 ObjPtr<mirror::CallSite> call_site = dex_cache->GetResolvedCallSite(call_site_idx);
1405 if (LIKELY(call_site != nullptr)) {
1406 return call_site;
1407 }
1408
1409 // Invoke the bootstrap method to get a candidate call site.
1410 call_site = InvokeBootstrapMethod(self, shadow_frame, call_site_idx);
1411 if (UNLIKELY(call_site == nullptr)) {
1412 if (!self->GetException()->IsError()) {
1413 // Use a BootstrapMethodError if the exception is not an instance of java.lang.Error.
1414 ThrowWrappedBootstrapMethodError("Exception from call site #%u bootstrap method",
1415 call_site_idx);
1416 }
1417 return nullptr;
1418 }
1419
1420 // Attempt to place the candidate call site into the DexCache, return the winning call site.
1421 return dex_cache->SetResolvedCallSite(call_site_idx, call_site);
1422}
1423
1424} // namespace
1425
Orion Hodsonc069a302017-01-18 09:23:12 +00001426bool DoInvokeCustom(Thread* self,
1427 ShadowFrame& shadow_frame,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001428 uint32_t call_site_idx,
1429 const InstructionOperands* operands,
1430 JValue* result) {
Alex Light848574c2017-09-25 16:59:39 -07001431 // Make sure to check for async exceptions
1432 if (UNLIKELY(self->ObserveAsyncException())) {
1433 return false;
1434 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001435
Orion Hodsonc069a302017-01-18 09:23:12 +00001436 // invoke-custom is not supported in transactions. In transactions
1437 // there is a limited set of types supported. invoke-custom allows
1438 // running arbitrary code and instantiating arbitrary types.
1439 CHECK(!Runtime::Current()->IsActiveTransaction());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001440
1441 ObjPtr<mirror::CallSite> call_site = DoResolveCallSite(self, shadow_frame, call_site_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00001442 if (call_site.IsNull()) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001443 DCHECK(self->IsExceptionPending());
1444 return false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001445 }
1446
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001447 StackHandleScope<2> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +00001448 Handle<mirror::MethodHandle> target = hs.NewHandle(call_site->GetTarget());
1449 Handle<mirror::MethodType> target_method_type = hs.NewHandle(target->GetMethodType());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001450 DCHECK_EQ(operands->GetNumberOfOperands(), target_method_type->NumberOfVRegs())
1451 << " call_site_idx" << call_site_idx;
1452 return MethodHandleInvokeExact(self,
1453 shadow_frame,
1454 target,
1455 target_method_type,
1456 operands,
1457 result);
Orion Hodsonc069a302017-01-18 09:23:12 +00001458}
1459
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001460// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
1461static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
1462 size_t dest_reg, size_t src_reg)
1463 REQUIRES_SHARED(Locks::mutator_lock_) {
1464 // Uint required, so that sign extension does not make this wrong on 64b systems
1465 uint32_t src_value = shadow_frame.GetVReg(src_reg);
1466 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
1467
1468 // If both register locations contains the same value, the register probably holds a reference.
1469 // Note: As an optimization, non-moving collectors leave a stale reference value
1470 // in the references array even after the original vreg was overwritten to a non-reference.
Vladimir Marko78baed52018-10-11 10:44:58 +01001471 if (src_value == reinterpret_cast32<uint32_t>(o.Ptr())) {
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001472 new_shadow_frame->SetVRegReference(dest_reg, o);
1473 } else {
1474 new_shadow_frame->SetVReg(dest_reg, src_value);
1475 }
1476}
1477
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001478template <bool is_range>
1479inline void CopyRegisters(ShadowFrame& caller_frame,
1480 ShadowFrame* callee_frame,
1481 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
1482 const size_t first_src_reg,
1483 const size_t first_dest_reg,
1484 const size_t num_regs) {
1485 if (is_range) {
1486 const size_t dest_reg_bound = first_dest_reg + num_regs;
1487 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound;
1488 ++dest_reg, ++src_reg) {
1489 AssignRegister(callee_frame, caller_frame, dest_reg, src_reg);
1490 }
1491 } else {
1492 DCHECK_LE(num_regs, arraysize(arg));
1493
1494 for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) {
1495 AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]);
1496 }
1497 }
1498}
1499
Igor Murashkin6918bf12015-09-27 19:19:06 -07001500template <bool is_range,
Narayan Kamath370423d2016-10-03 16:51:22 +01001501 bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001502static inline bool DoCallCommon(ArtMethod* called_method,
1503 Thread* self,
1504 ShadowFrame& shadow_frame,
1505 JValue* result,
1506 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +01001507 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -07001508 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001509 bool string_init = false;
1510 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001511 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
1512 && called_method->IsConstructor())) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001513 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
Jeff Hao848f70a2014-01-15 13:49:50 -08001514 string_init = true;
1515 }
1516
Alex Lightdaf58c82016-03-16 23:00:49 +00001517 // Compute method information.
David Sehr0225f8e2018-01-31 08:52:24 +00001518 CodeItemDataAccessor accessor(called_method->DexInstructionData());
Igor Murashkin158f35c2015-06-10 15:55:30 -07001519 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001520 uint16_t num_regs;
Jeff Hao5ea84132017-05-05 16:59:29 -07001521 // Test whether to use the interpreter or compiler entrypoint, and save that result to pass to
1522 // PerformCall. A deoptimization could occur at any time, and we shouldn't change which
1523 // entrypoint to use once we start building the shadow frame.
Mathieu Chartier448bbcf2017-07-06 12:05:13 -07001524
1525 // For unstarted runtimes, always use the interpreter entrypoint. This fixes the case where we are
1526 // doing cross compilation. Note that GetEntryPointFromQuickCompiledCode doesn't use the image
1527 // pointer size here and this may case an overflow if it is called from the compiler. b/62402160
1528 const bool use_interpreter_entrypoint = !Runtime::Current()->IsStarted() ||
1529 ClassLinker::ShouldUseInterpreterEntrypoint(
1530 called_method,
1531 called_method->GetEntryPointFromQuickCompiledCode());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001532 if (LIKELY(accessor.HasCodeItem())) {
Jeff Hao5ea84132017-05-05 16:59:29 -07001533 // When transitioning to compiled code, space only needs to be reserved for the input registers.
1534 // The rest of the frame gets discarded. This also prevents accessing the called method's code
1535 // item, saving memory by keeping code items of compiled code untouched.
Mathieu Chartier448bbcf2017-07-06 12:05:13 -07001536 if (!use_interpreter_entrypoint) {
1537 DCHECK(!Runtime::Current()->IsAotCompiler()) << "Compiler should use interpreter entrypoint";
Jeff Hao5ea84132017-05-05 16:59:29 -07001538 num_regs = number_of_inputs;
1539 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001540 num_regs = accessor.RegistersSize();
1541 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, accessor.InsSize());
Jeff Hao5ea84132017-05-05 16:59:29 -07001542 }
Nicolas Geoffray01822292017-03-09 09:03:19 +00001543 } else {
1544 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1545 num_regs = number_of_inputs;
Jeff Haodf79ddb2017-02-27 14:47:06 -08001546 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001547
Igor Murashkin158f35c2015-06-10 15:55:30 -07001548 // Hack for String init:
1549 //
1550 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
1551 // invoke-x StringFactory(a, b, c, ...)
1552 // by effectively dropping the first virtual register from the invoke.
1553 //
1554 // (at this point the ArtMethod has already been replaced,
1555 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +00001556 //
1557 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
1558 // to handle the compiler optimization of replacing `this` with null without
1559 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001560 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -07001561 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001562 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 -07001563
Igor Murashkin158f35c2015-06-10 15:55:30 -07001564 // The new StringFactory call is static and has one fewer argument.
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001565 if (!accessor.HasCodeItem()) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001566 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1567 num_regs--;
1568 } // 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 -07001569 number_of_inputs--;
1570
1571 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -07001572 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001573 arg[i - 1] = arg[i];
1574 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07001575 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001576
1577 // Rewrite the non-var-arg case
1578 vregC++; // Skips the 0th vreg in the range ("this").
1579 }
1580
1581 // Parameter registers go at the end of the shadow frame.
1582 DCHECK_GE(num_regs, number_of_inputs);
1583 size_t first_dest_reg = num_regs - number_of_inputs;
1584 DCHECK_NE(first_dest_reg, (size_t)-1);
1585
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001586 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001587 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -07001588 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -07001589 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -07001590 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001591
Igor Murashkin158f35c2015-06-10 15:55:30 -07001592 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -07001593 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001594 // Slow path.
1595 // We might need to do class loading, which incurs a thread state change to kNative. So
1596 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07001597 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +02001598 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001599 self->EndAssertNoThreadSuspension(old_cause);
1600
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001601 // ArtMethod here is needed to check type information of the call site against the callee.
1602 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
1603 //
1604 // As a special case for proxy methods, which are not dex-backed,
1605 // we have to retrieve type information from the proxy's method
1606 // interface method instead (which is dex backed since proxies are never interfaces).
Andreas Gampe542451c2016-07-26 09:02:02 -07001607 ArtMethod* method =
1608 new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001609
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001610 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001611 // to get the exact type of each reference argument.
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001612 const DexFile::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001613 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001614 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001615
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001616 // Handle receiver apart since it's not part of the shorty.
1617 size_t dest_reg = first_dest_reg;
1618 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001619
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001620 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001621 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001622 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
1623 ++dest_reg;
1624 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -07001625 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001626 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001627
1628 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001629 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001630 // Skip the 0th 'shorty' type since it represents the return type.
1631 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001632 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
1633 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001634 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001635 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001636 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001637 if (do_assignability_check && o != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001638 const dex::TypeIndex type_idx = params->GetTypeItem(shorty_pos).type_idx_;
Vladimir Marko942fd312017-01-16 20:52:19 +00001639 ObjPtr<mirror::Class> arg_type = method->GetDexCache()->GetResolvedType(type_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001640 if (arg_type == nullptr) {
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001641 StackHandleScope<1> hs(self);
1642 // Preserve o since it is used below and GetClassFromTypeIndex may cause thread
1643 // suspension.
1644 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&o);
Vladimir Markob45528c2017-07-27 14:14:28 +01001645 arg_type = method->ResolveClassFromTypeIndex(type_idx);
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001646 if (arg_type == nullptr) {
1647 CHECK(self->IsExceptionPending());
1648 return false;
1649 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001650 }
1651 if (!o->VerifierInstanceOf(arg_type)) {
1652 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -07001653 std::string temp1, temp2;
Orion Hodsonfef06642016-11-25 16:07:11 +00001654 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001655 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -08001656 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -07001657 o->GetClass()->GetDescriptor(&temp1),
1658 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001659 return false;
1660 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001661 }
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001662 new_shadow_frame->SetVRegReference(dest_reg, o);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001663 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -07001664 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001665 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001666 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001667 uint64_t wide_value =
1668 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
1669 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001670 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001671 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001672 ++dest_reg;
1673 ++arg_offset;
1674 break;
1675 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001676 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001677 default:
1678 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
1679 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001680 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001681 }
1682 } else {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001683 if (is_range) {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001684 DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001685 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001686
1687 CopyRegisters<is_range>(shadow_frame,
1688 new_shadow_frame,
1689 arg,
1690 vregC,
1691 first_dest_reg,
1692 number_of_inputs);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001693 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001694 }
1695
Jeff Hao5ea84132017-05-05 16:59:29 -07001696 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001697 accessor,
Jeff Hao5ea84132017-05-05 16:59:29 -07001698 shadow_frame.GetMethod(),
1699 first_dest_reg,
1700 new_shadow_frame,
1701 result,
1702 use_interpreter_entrypoint);
Jeff Hao848f70a2014-01-15 13:49:50 -08001703
1704 if (string_init && !self->IsExceptionPending()) {
Mingyao Yangffedec52016-05-19 10:48:40 -07001705 SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001706 }
1707
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001708 return !self->IsExceptionPending();
1709}
1710
Igor Murashkin158f35c2015-06-10 15:55:30 -07001711template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001712bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
1713 const Instruction* inst, uint16_t inst_data, JValue* result) {
1714 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001715 const uint16_t number_of_inputs =
1716 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001717
1718 // TODO: find a cleaner way to separate non-range and range information without duplicating
1719 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -07001720 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001721 uint32_t vregC = 0;
1722 if (is_range) {
1723 vregC = inst->VRegC_3rc();
1724 } else {
1725 vregC = inst->VRegC_35c();
1726 inst->GetVarArgs(arg, inst_data);
1727 }
1728
1729 return DoCallCommon<is_range, do_assignability_check>(
1730 called_method, self, shadow_frame,
1731 result, number_of_inputs, arg, vregC);
1732}
1733
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001734template <bool is_range, bool do_access_check, bool transaction_active>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001735bool DoFilledNewArray(const Instruction* inst,
1736 const ShadowFrame& shadow_frame,
1737 Thread* self,
1738 JValue* result) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001739 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
1740 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
1741 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
1742 if (!is_range) {
1743 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
1744 CHECK_LE(length, 5);
1745 }
1746 if (UNLIKELY(length < 0)) {
1747 ThrowNegativeArraySizeException(length);
1748 return false;
1749 }
1750 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08001751 ObjPtr<mirror::Class> array_class = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
Mathieu Chartieref41db72016-10-25 15:08:01 -07001752 shadow_frame.GetMethod(),
1753 self,
1754 false,
1755 do_access_check);
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001756 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001757 DCHECK(self->IsExceptionPending());
1758 return false;
1759 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001760 CHECK(array_class->IsArrayClass());
Mathieu Chartieref41db72016-10-25 15:08:01 -07001761 ObjPtr<mirror::Class> component_class = array_class->GetComponentType();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001762 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
1763 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
1764 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001765 ThrowRuntimeException("Bad filled array request for type %s",
David Sehr709b0702016-10-13 09:12:37 -07001766 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001767 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001768 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -08001769 "Found type %s; filled-new-array not implemented for anything but 'int'",
David Sehr709b0702016-10-13 09:12:37 -07001770 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001771 }
1772 return false;
1773 }
Mathieu Chartieref41db72016-10-25 15:08:01 -07001774 ObjPtr<mirror::Object> new_array = mirror::Array::Alloc<true>(
1775 self,
1776 array_class,
1777 length,
1778 array_class->GetComponentSizeShift(),
1779 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001780 if (UNLIKELY(new_array == nullptr)) {
1781 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001782 return false;
1783 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001784 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
1785 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001786 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +01001787 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001788 } else {
Ian Rogers29a26482014-05-02 15:27:29 -07001789 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +01001790 }
Sebastien Hertzabff6432014-01-27 18:01:39 +01001791 for (int32_t i = 0; i < length; ++i) {
1792 size_t src_reg = is_range ? vregC + i : arg[i];
1793 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001794 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
1795 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +01001796 } else {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001797 new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<transaction_active>(
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001798 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001799 }
1800 }
1801
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001802 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001803 return true;
1804}
1805
Mathieu Chartieref41db72016-10-25 15:08:01 -07001806// TODO: Use ObjPtr here.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001807template<typename T>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001808static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array,
1809 int32_t count)
1810 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001811 Runtime* runtime = Runtime::Current();
1812 for (int32_t i = 0; i < count; ++i) {
1813 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
1814 }
1815}
1816
Mathieu Chartieref41db72016-10-25 15:08:01 -07001817void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001818 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001819 DCHECK(Runtime::Current()->IsActiveTransaction());
1820 DCHECK(array != nullptr);
1821 DCHECK_LE(count, array->GetLength());
1822 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
1823 switch (primitive_component_type) {
1824 case Primitive::kPrimBoolean:
1825 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
1826 break;
1827 case Primitive::kPrimByte:
1828 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
1829 break;
1830 case Primitive::kPrimChar:
1831 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
1832 break;
1833 case Primitive::kPrimShort:
1834 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
1835 break;
1836 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001837 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
1838 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001839 case Primitive::kPrimFloat:
1840 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
1841 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001842 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001843 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
1844 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001845 case Primitive::kPrimDouble:
1846 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
1847 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001848 default:
1849 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
1850 << " in fill-array-data";
1851 break;
1852 }
1853}
1854
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001855// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +02001856#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001857 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001858 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
1859 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +02001860 const Instruction* inst, uint16_t inst_data, \
1861 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001862EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
1863EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
1864EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
1865EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
1866#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001867
Orion Hodsonc069a302017-01-18 09:23:12 +00001868// Explicit DoInvokePolymorphic template function declarations.
1869#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range) \
1870 template REQUIRES_SHARED(Locks::mutator_lock_) \
1871 bool DoInvokePolymorphic<_is_range>( \
1872 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1873 uint16_t inst_data, JValue* result)
1874EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false);
1875EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true);
Narayan Kamath9823e782016-08-03 12:46:58 +01001876#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1877
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001878// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001879#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001880 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001881 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
1882 const ShadowFrame& shadow_frame, \
1883 Thread* self, JValue* result)
1884#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
1885 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
1886 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
1887 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
1888 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
1889EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
1890EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
1891#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001892#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
1893
1894} // namespace interpreter
1895} // namespace art