blob: 084cb4218f2722ce7573f2ec1bdf2a8a6d0824e7 [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
Andreas Gampe542451c2016-07-26 09:02:02 -070021#include "base/enums.h"
Daniel Mihalyieb076692014-08-22 17:33:31 +020022#include "debugger.h"
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +000023#include "entrypoints/runtime_asm_entrypoints.h"
Tamas Berghammerdd5e5e92016-02-12 16:29:00 +000024#include "jit/jit.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010025#include "jvalue.h"
26#include "method_handles.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010027#include "method_handles-inl.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010028#include "mirror/array-inl.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010029#include "mirror/class.h"
Narayan Kamath000e1882016-10-24 17:14:25 +010030#include "mirror/emulated_stack_frame.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080031#include "mirror/method_handle_impl-inl.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010032#include "reflection.h"
33#include "reflection-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070034#include "stack.h"
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +010035#include "well_known_classes.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020036
37namespace art {
38namespace interpreter {
39
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000040void ThrowNullPointerExceptionFromInterpreter() {
41 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -070042}
43
44template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
45bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
46 uint16_t inst_data) {
47 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
48 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010049 ArtField* f =
50 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
51 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -070052 if (UNLIKELY(f == nullptr)) {
53 CHECK(self->IsExceptionPending());
54 return false;
55 }
Mathieu Chartieref41db72016-10-25 15:08:01 -070056 ObjPtr<mirror::Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -070057 if (is_static) {
58 obj = f->GetDeclaringClass();
59 } else {
60 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
61 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000062 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers54874942014-06-10 16:31:03 -070063 return false;
64 }
65 }
Orion Hodson3d617ac2016-10-19 14:00:46 +010066
67 JValue result;
68 DoFieldGetCommon<field_type>(self, shadow_frame, obj, f, &result);
Ian Rogers54874942014-06-10 16:31:03 -070069 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
70 switch (field_type) {
71 case Primitive::kPrimBoolean:
Orion Hodson3d617ac2016-10-19 14:00:46 +010072 shadow_frame.SetVReg(vregA, result.GetZ());
Ian Rogers54874942014-06-10 16:31:03 -070073 break;
74 case Primitive::kPrimByte:
Orion Hodson3d617ac2016-10-19 14:00:46 +010075 shadow_frame.SetVReg(vregA, result.GetB());
Ian Rogers54874942014-06-10 16:31:03 -070076 break;
77 case Primitive::kPrimChar:
Orion Hodson3d617ac2016-10-19 14:00:46 +010078 shadow_frame.SetVReg(vregA, result.GetC());
Ian Rogers54874942014-06-10 16:31:03 -070079 break;
80 case Primitive::kPrimShort:
Orion Hodson3d617ac2016-10-19 14:00:46 +010081 shadow_frame.SetVReg(vregA, result.GetS());
Ian Rogers54874942014-06-10 16:31:03 -070082 break;
83 case Primitive::kPrimInt:
Orion Hodson3d617ac2016-10-19 14:00:46 +010084 shadow_frame.SetVReg(vregA, result.GetI());
Ian Rogers54874942014-06-10 16:31:03 -070085 break;
86 case Primitive::kPrimLong:
Orion Hodson3d617ac2016-10-19 14:00:46 +010087 shadow_frame.SetVRegLong(vregA, result.GetJ());
Ian Rogers54874942014-06-10 16:31:03 -070088 break;
89 case Primitive::kPrimNot:
Orion Hodson3d617ac2016-10-19 14:00:46 +010090 shadow_frame.SetVRegReference(vregA, result.GetL());
Ian Rogers54874942014-06-10 16:31:03 -070091 break;
92 default:
93 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -070094 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -070095 }
96 return true;
97}
98
99// Explicitly instantiate all DoFieldGet functions.
100#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
101 template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \
102 ShadowFrame& shadow_frame, \
103 const Instruction* inst, \
104 uint16_t inst_data)
105
106#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
107 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \
108 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true);
109
110// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700111EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
112EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
113EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
114EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
115EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
116EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
117EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700118
119// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700120EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
121EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
122EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
123EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
124EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
125EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
126EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700127
128#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
129#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
130
131// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
132// Returns true on success, otherwise throws an exception and returns false.
133template<Primitive::Type field_type>
134bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700135 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700136 if (UNLIKELY(obj == nullptr)) {
137 // We lost the reference to the field index so we cannot get a more
138 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000139 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700140 return false;
141 }
142 MemberOffset field_offset(inst->VRegC_22c());
143 // Report this field access to instrumentation if needed. Since we only have the offset of
144 // the field from the base of the object, we need to look for it first.
145 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
146 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
147 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
148 field_offset.Uint32Value());
149 DCHECK(f != nullptr);
150 DCHECK(!f->IsStatic());
Mathieu Chartiera3147732016-10-26 22:57:02 -0700151 StackHandleScope<1> hs(Thread::Current());
152 // Save obj in case the instrumentation event has thread suspension.
153 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700154 instrumentation->FieldReadEvent(Thread::Current(),
155 obj.Ptr(),
156 shadow_frame.GetMethod(),
157 shadow_frame.GetDexPC(),
158 f);
Ian Rogers54874942014-06-10 16:31:03 -0700159 }
160 // Note: iget-x-quick instructions are only for non-volatile fields.
161 const uint32_t vregA = inst->VRegA_22c(inst_data);
162 switch (field_type) {
163 case Primitive::kPrimInt:
164 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
165 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800166 case Primitive::kPrimBoolean:
167 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
168 break;
169 case Primitive::kPrimByte:
170 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
171 break;
172 case Primitive::kPrimChar:
173 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
174 break;
175 case Primitive::kPrimShort:
176 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
177 break;
Ian Rogers54874942014-06-10 16:31:03 -0700178 case Primitive::kPrimLong:
179 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
180 break;
181 case Primitive::kPrimNot:
182 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
183 break;
184 default:
185 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700186 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700187 }
188 return true;
189}
190
191// Explicitly instantiate all DoIGetQuick functions.
192#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
193 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
194 uint16_t inst_data)
195
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800196EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
197EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
198EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
199EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
200EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
201EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
202EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700203#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
204
205template<Primitive::Type field_type>
206static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700207 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700208 JValue field_value;
209 switch (field_type) {
210 case Primitive::kPrimBoolean:
211 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
212 break;
213 case Primitive::kPrimByte:
214 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
215 break;
216 case Primitive::kPrimChar:
217 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
218 break;
219 case Primitive::kPrimShort:
220 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
221 break;
222 case Primitive::kPrimInt:
223 field_value.SetI(shadow_frame.GetVReg(vreg));
224 break;
225 case Primitive::kPrimLong:
226 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
227 break;
228 case Primitive::kPrimNot:
229 field_value.SetL(shadow_frame.GetVRegReference(vreg));
230 break;
231 default:
232 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700233 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700234 }
235 return field_value;
236}
237
Orion Hodson3d617ac2016-10-19 14:00:46 +0100238template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
239 bool transaction_active>
240bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
241 uint16_t inst_data) {
242 const bool do_assignability_check = do_access_check;
243 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
244 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
245 ArtField* f =
246 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
247 Primitive::ComponentSize(field_type));
248 if (UNLIKELY(f == nullptr)) {
249 CHECK(self->IsExceptionPending());
250 return false;
251 }
252 ObjPtr<mirror::Object> obj;
253 if (is_static) {
254 obj = f->GetDeclaringClass();
255 } else {
256 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
257 if (UNLIKELY(obj == nullptr)) {
258 ThrowNullPointerExceptionForFieldAccess(f, false);
259 return false;
260 }
261 }
262
263 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100264 JValue value = GetFieldValue<field_type>(shadow_frame, vregA);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100265 return DoFieldPutCommon<field_type, do_assignability_check, transaction_active>(self,
266 shadow_frame,
267 obj,
268 f,
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100269 value);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100270}
271
Ian Rogers54874942014-06-10 16:31:03 -0700272// Explicitly instantiate all DoFieldPut functions.
273#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
274 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
275 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
276
277#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
278 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
279 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
280 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
281 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
282
283// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700284EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
285EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
286EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
287EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
288EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
289EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
290EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700291
292// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700293EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
294EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
295EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
296EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
297EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
298EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
299EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700300
301#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
302#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
303
304template<Primitive::Type field_type, bool transaction_active>
305bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700306 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700307 if (UNLIKELY(obj == nullptr)) {
308 // We lost the reference to the field index so we cannot get a more
309 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000310 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700311 return false;
312 }
313 MemberOffset field_offset(inst->VRegC_22c());
314 const uint32_t vregA = inst->VRegA_22c(inst_data);
315 // Report this field modification to instrumentation if needed. Since we only have the offset of
316 // the field from the base of the object, we need to look for it first.
317 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
318 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
319 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
320 field_offset.Uint32Value());
321 DCHECK(f != nullptr);
322 DCHECK(!f->IsStatic());
323 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700324 StackHandleScope<1> hs(Thread::Current());
325 // Save obj in case the instrumentation event has thread suspension.
326 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700327 instrumentation->FieldWriteEvent(Thread::Current(),
328 obj.Ptr(),
329 shadow_frame.GetMethod(),
330 shadow_frame.GetDexPC(),
331 f,
332 field_value);
Ian Rogers54874942014-06-10 16:31:03 -0700333 }
334 // Note: iput-x-quick instructions are only for non-volatile fields.
335 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700336 case Primitive::kPrimBoolean:
337 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
338 break;
339 case Primitive::kPrimByte:
340 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
341 break;
342 case Primitive::kPrimChar:
343 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
344 break;
345 case Primitive::kPrimShort:
346 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
347 break;
Ian Rogers54874942014-06-10 16:31:03 -0700348 case Primitive::kPrimInt:
349 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
350 break;
351 case Primitive::kPrimLong:
352 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
353 break;
354 case Primitive::kPrimNot:
355 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
356 break;
357 default:
358 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700359 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700360 }
361 return true;
362}
363
364// Explicitly instantiate all DoIPutQuick functions.
365#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
366 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
367 const Instruction* inst, \
368 uint16_t inst_data)
369
370#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
371 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
372 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
373
Andreas Gampec8ccf682014-09-29 20:07:43 -0700374EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
375EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
376EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
377EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
378EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
379EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
380EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700381#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
382#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
383
Sebastien Hertz520633b2015-09-08 17:03:36 +0200384// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700385uint32_t FindNextInstructionFollowingException(
386 Thread* self, ShadowFrame& shadow_frame, uint32_t dex_pc,
387 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700388 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700389 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000390 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Sebastien Hertz520633b2015-09-08 17:03:36 +0200391 if (instrumentation != nullptr && instrumentation->HasExceptionCaughtListeners()
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000392 && self->IsExceptionThrownByCurrentMethod(exception.Get())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000393 instrumentation->ExceptionCaughtEvent(self, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200394 }
Ian Rogers54874942014-06-10 16:31:03 -0700395 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700396 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
397 hs.NewHandle(exception->GetClass()), dex_pc, &clear_exception);
Sebastien Hertz520633b2015-09-08 17:03:36 +0200398 if (found_dex_pc == DexFile::kDexNoIndex && instrumentation != nullptr) {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000399 // Exception is not caught by the current method. We will unwind to the
400 // caller. Notify any instrumentation listener.
Sebastien Hertz9f102032014-05-23 08:59:42 +0200401 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
Ian Rogers54874942014-06-10 16:31:03 -0700402 shadow_frame.GetMethod(), dex_pc);
403 } else {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000404 // Exception is caught in the current method. We will jump to the found_dex_pc.
Ian Rogers54874942014-06-10 16:31:03 -0700405 if (clear_exception) {
406 self->ClearException();
407 }
408 }
409 return found_dex_pc;
410}
411
Ian Rogerse94652f2014-12-02 11:13:19 -0800412void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
413 LOG(FATAL) << "Unexpected instruction: "
414 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
415 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700416}
417
Sebastien Hertz45b15972015-04-03 16:07:05 +0200418void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700419 va_list args;
420 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200421 AbortTransactionV(self, fmt, args);
422 va_end(args);
423}
424
425void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
426 CHECK(Runtime::Current()->IsActiveTransaction());
427 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100428 std::string abort_msg;
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800429 android::base::StringAppendV(&abort_msg, fmt, args);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100430 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200431 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700432}
433
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100434// START DECLARATIONS :
435//
436// These additional declarations are required because clang complains
437// about ALWAYS_INLINE (-Werror, -Wgcc-compat) in definitions.
438//
439
Narayan Kamath9823e782016-08-03 12:46:58 +0100440template <bool is_range, bool do_assignability_check>
Vladimir Markod16363a2017-02-01 14:09:13 +0000441static ALWAYS_INLINE bool DoCallCommon(ArtMethod* called_method,
442 Thread* self,
443 ShadowFrame& shadow_frame,
444 JValue* result,
445 uint16_t number_of_inputs,
446 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
447 uint32_t vregC) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100448
449template <bool is_range>
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000450ALWAYS_INLINE void CopyRegisters(ShadowFrame& caller_frame,
451 ShadowFrame* callee_frame,
452 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
453 const size_t first_src_reg,
454 const size_t first_dest_reg,
455 const size_t num_regs) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100456
457// END DECLARATIONS.
458
Siva Chandra05d24152016-01-05 17:43:17 -0800459void ArtInterpreterToCompiledCodeBridge(Thread* self,
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100460 ArtMethod* caller,
Siva Chandra05d24152016-01-05 17:43:17 -0800461 ShadowFrame* shadow_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -0700462 uint16_t arg_offset,
Siva Chandra05d24152016-01-05 17:43:17 -0800463 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700464 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700465 ArtMethod* method = shadow_frame->GetMethod();
466 // Ensure static methods are initialized.
467 if (method->IsStatic()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700468 ObjPtr<mirror::Class> declaringClass = method->GetDeclaringClass();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700469 if (UNLIKELY(!declaringClass->IsInitialized())) {
470 self->PushShadowFrame(shadow_frame);
471 StackHandleScope<1> hs(self);
472 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
Nicolas Geoffray01822292017-03-09 09:03:19 +0000473 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
474 true))) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700475 self->PopShadowFrame();
476 DCHECK(self->IsExceptionPending());
477 return;
478 }
479 self->PopShadowFrame();
480 CHECK(h_class->IsInitializing());
Nicolas Geoffray01822292017-03-09 09:03:19 +0000481 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
482 method = shadow_frame->GetMethod();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700483 }
484 }
Jeff Hao5ea84132017-05-05 16:59:29 -0700485 // Basic checks for the arg_offset. If there's no code item, the arg_offset must be 0. Otherwise,
486 // check that the arg_offset isn't greater than the number of registers. A stronger check is
487 // difficult since the frame may contain space for all the registers in the method, or only enough
488 // space for the arguments.
489 if (kIsDebugBuild) {
490 if (method->GetCodeItem() == nullptr) {
491 DCHECK_EQ(0u, arg_offset) << method->PrettyMethod();
492 } else {
493 DCHECK_LE(arg_offset, shadow_frame->NumberOfVRegs());
494 }
495 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100496 jit::Jit* jit = Runtime::Current()->GetJit();
497 if (jit != nullptr && caller != nullptr) {
498 jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
499 }
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700500 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
501 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
Andreas Gampe542451c2016-07-26 09:02:02 -0700502 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700503}
504
Mingyao Yangffedec52016-05-19 10:48:40 -0700505void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
506 uint16_t this_obj_vreg,
507 JValue result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700508 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700509 ObjPtr<mirror::Object> existing = shadow_frame->GetVRegReference(this_obj_vreg);
Mingyao Yangffedec52016-05-19 10:48:40 -0700510 if (existing == nullptr) {
511 // If it's null, we come from compiled code that was deoptimized. Nothing to do,
512 // as the compiler verified there was no alias.
513 // Set the new string result of the StringFactory.
514 shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
515 return;
516 }
517 // Set the string init result into all aliases.
518 for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
519 if (shadow_frame->GetVRegReference(i) == existing) {
520 DCHECK_EQ(shadow_frame->GetVRegReference(i),
521 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
522 shadow_frame->SetVRegReference(i, result.GetL());
523 DCHECK_EQ(shadow_frame->GetVRegReference(i),
524 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
525 }
526 }
527}
528
Orion Hodsonc069a302017-01-18 09:23:12 +0000529template<bool is_range>
Orion Hodson811bd5f2016-12-07 11:35:37 +0000530bool DoInvokePolymorphic(Thread* self,
531 ShadowFrame& shadow_frame,
532 const Instruction* inst,
533 uint16_t inst_data,
534 JValue* result)
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100535 REQUIRES_SHARED(Locks::mutator_lock_) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100536 // Invoke-polymorphic instructions always take a receiver. i.e, they are never static.
537 const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc();
Orion Hodson3d617ac2016-10-19 14:00:46 +0100538 const int invoke_method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc();
Narayan Kamath9823e782016-08-03 12:46:58 +0100539
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000540 // Initialize |result| to 0 as this is the default return value for
541 // polymorphic invocations of method handle types with void return
542 // and provides sane return result in error cases.
543 result->SetJ(0);
544
Orion Hodson3d617ac2016-10-19 14:00:46 +0100545 // The invoke_method_idx here is the name of the signature polymorphic method that
Narayan Kamath9823e782016-08-03 12:46:58 +0100546 // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact)
547 // and not the method that we'll dispatch to in the end.
Orion Hodson811bd5f2016-12-07 11:35:37 +0000548 StackHandleScope<5> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +0000549 Handle<mirror::MethodHandle> method_handle(hs.NewHandle(
550 ObjPtr<mirror::MethodHandle>::DownCast(
Mathieu Chartieref41db72016-10-25 15:08:01 -0700551 MakeObjPtr(shadow_frame.GetVRegReference(vRegC)))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800552 if (UNLIKELY(method_handle == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100553 // Note that the invoke type is kVirtual here because a call to a signature
554 // polymorphic method is shaped like a virtual call at the bytecode level.
Orion Hodson3d617ac2016-10-19 14:00:46 +0100555 ThrowNullPointerExceptionForMethodAccess(invoke_method_idx, InvokeType::kVirtual);
Narayan Kamath9823e782016-08-03 12:46:58 +0100556 return false;
557 }
558
559 // The vRegH value gives the index of the proto_id associated with this
Orion Hodson811bd5f2016-12-07 11:35:37 +0000560 // signature polymorphic call site.
Narayan Kamath9823e782016-08-03 12:46:58 +0100561 const uint32_t callsite_proto_id = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
562
563 // Call through to the classlinker and ask it to resolve the static type associated
564 // with the callsite. This information is stored in the dex cache so it's
565 // guaranteed to be fast after the first resolution.
Narayan Kamath9823e782016-08-03 12:46:58 +0100566 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Narayan Kamath208f8572016-08-03 12:46:58 +0100567 Handle<mirror::Class> caller_class(hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass()));
568 Handle<mirror::MethodType> callsite_type(hs.NewHandle(class_linker->ResolveMethodType(
Narayan Kamath9823e782016-08-03 12:46:58 +0100569 caller_class->GetDexFile(), callsite_proto_id,
570 hs.NewHandle<mirror::DexCache>(caller_class->GetDexCache()),
Narayan Kamath208f8572016-08-03 12:46:58 +0100571 hs.NewHandle<mirror::ClassLoader>(caller_class->GetClassLoader()))));
Narayan Kamath9823e782016-08-03 12:46:58 +0100572
573 // This implies we couldn't resolve one or more types in this method handle.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800574 if (UNLIKELY(callsite_type == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100575 CHECK(self->IsExceptionPending());
Narayan Kamath9823e782016-08-03 12:46:58 +0100576 return false;
577 }
578
Orion Hodson811bd5f2016-12-07 11:35:37 +0000579 ArtMethod* invoke_method =
580 class_linker->ResolveMethod<ClassLinker::kForceICCECheck>(self,
581 invoke_method_idx,
582 shadow_frame.GetMethod(),
583 kVirtual);
Narayan Kamath0a8485e2016-11-02 18:47:11 +0000584
Orion Hodson811bd5f2016-12-07 11:35:37 +0000585 // There is a common dispatch method for method handles that takes
586 // arguments either from a range or an array of arguments depending
587 // on whether the DEX instruction is invoke-polymorphic/range or
588 // invoke-polymorphic. The array here is for the latter.
589 uint32_t args[Instruction::kMaxVarArgRegs] = {};
Narayan Kamath9823e782016-08-03 12:46:58 +0100590 if (is_range) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000591 // VRegC is the register holding the method handle. Arguments passed
592 // to the method handle's target do not include the method handle.
593 uint32_t first_arg = inst->VRegC_4rcc() + 1;
Orion Hodsonc069a302017-01-18 09:23:12 +0000594 return DoInvokePolymorphic<is_range>(self,
595 invoke_method,
596 shadow_frame,
597 method_handle,
598 callsite_type,
599 args /* unused */,
600 first_arg,
601 result);
Narayan Kamath9823e782016-08-03 12:46:58 +0100602 } else {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000603 // Get the register arguments for the invoke.
604 inst->GetVarArgs(args, inst_data);
605 // Drop the first register which is the method handle performing the invoke.
Alexey Frunze8631a462017-01-19 19:07:37 -0800606 memmove(args, args + 1, sizeof(args[0]) * (Instruction::kMaxVarArgRegs - 1));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000607 args[Instruction::kMaxVarArgRegs - 1] = 0;
Orion Hodsonc069a302017-01-18 09:23:12 +0000608 return DoInvokePolymorphic<is_range>(self,
609 invoke_method,
610 shadow_frame,
611 method_handle,
612 callsite_type,
613 args,
614 args[0],
615 result);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100616 }
617}
618
Orion Hodsonc069a302017-01-18 09:23:12 +0000619static ObjPtr<mirror::CallSite> InvokeBootstrapMethod(Thread* self,
620 ShadowFrame& shadow_frame,
621 uint32_t call_site_idx)
622 REQUIRES_SHARED(Locks::mutator_lock_) {
623 ArtMethod* referrer = shadow_frame.GetMethod();
624 const DexFile* dex_file = referrer->GetDexFile();
625 const DexFile::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx);
626
627 StackHandleScope<9> hs(self);
628 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader()));
629 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
630
631 CallSiteArrayValueIterator it(*dex_file, csi);
632 uint32_t method_handle_idx = static_cast<uint32_t>(it.GetJavaValue().i);
633 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
634 Handle<mirror::MethodHandle>
635 bootstrap(hs.NewHandle(class_linker->ResolveMethodHandle(method_handle_idx, referrer)));
636 if (bootstrap.IsNull()) {
637 DCHECK(self->IsExceptionPending());
638 return nullptr;
639 }
640 Handle<mirror::MethodType> bootstrap_method_type = hs.NewHandle(bootstrap->GetMethodType());
641 it.Next();
642
643 DCHECK_EQ(static_cast<size_t>(bootstrap->GetMethodType()->GetPTypes()->GetLength()), it.Size());
644 const size_t num_bootstrap_vregs = bootstrap->GetMethodType()->NumberOfVRegs();
645
646 // Set-up a shadow frame for invoking the bootstrap method handle.
647 ShadowFrameAllocaUniquePtr bootstrap_frame =
648 CREATE_SHADOW_FRAME(num_bootstrap_vregs, nullptr, referrer, shadow_frame.GetDexPC());
649 ScopedStackedShadowFramePusher pusher(
650 self, bootstrap_frame.get(), StackedShadowFrameType::kShadowFrameUnderConstruction);
651 size_t vreg = 0;
652
653 // The first parameter is a MethodHandles lookup instance.
654 {
655 Handle<mirror::Class> lookup_class(hs.NewHandle(bootstrap->GetTargetClass()));
656 ObjPtr<mirror::MethodHandlesLookup> lookup =
657 mirror::MethodHandlesLookup::Create(self, lookup_class);
658 if (lookup.IsNull()) {
659 DCHECK(self->IsExceptionPending());
660 return nullptr;
661 }
662 bootstrap_frame->SetVRegReference(vreg++, lookup.Ptr());
663 }
664
665 // The second parameter is the name to lookup.
666 {
667 dex::StringIndex name_idx(static_cast<uint32_t>(it.GetJavaValue().i));
668 ObjPtr<mirror::String> name = class_linker->ResolveString(*dex_file, name_idx, dex_cache);
669 if (name.IsNull()) {
670 DCHECK(self->IsExceptionPending());
671 return nullptr;
672 }
673 bootstrap_frame->SetVRegReference(vreg++, name.Ptr());
674 }
675 it.Next();
676
677 // The third parameter is the method type associated with the name.
678 uint32_t method_type_idx = static_cast<uint32_t>(it.GetJavaValue().i);
679 Handle<mirror::MethodType>
680 method_type(hs.NewHandle(class_linker->ResolveMethodType(*dex_file,
681 method_type_idx,
682 dex_cache,
683 class_loader)));
684 if (method_type.IsNull()) {
685 DCHECK(self->IsExceptionPending());
686 return nullptr;
687 }
688 bootstrap_frame->SetVRegReference(vreg++, method_type.Get());
689 it.Next();
690
691 // Append remaining arguments (if any).
692 while (it.HasNext()) {
693 const jvalue& jvalue = it.GetJavaValue();
694 switch (it.GetValueType()) {
695 case EncodedArrayValueIterator::ValueType::kBoolean:
696 case EncodedArrayValueIterator::ValueType::kByte:
697 case EncodedArrayValueIterator::ValueType::kChar:
698 case EncodedArrayValueIterator::ValueType::kShort:
699 case EncodedArrayValueIterator::ValueType::kInt:
700 bootstrap_frame->SetVReg(vreg, jvalue.i);
701 vreg += 1;
702 break;
703 case EncodedArrayValueIterator::ValueType::kLong:
704 bootstrap_frame->SetVRegLong(vreg, jvalue.j);
705 vreg += 2;
706 break;
707 case EncodedArrayValueIterator::ValueType::kFloat:
708 bootstrap_frame->SetVRegFloat(vreg, jvalue.f);
709 vreg += 1;
710 break;
711 case EncodedArrayValueIterator::ValueType::kDouble:
712 bootstrap_frame->SetVRegDouble(vreg, jvalue.d);
713 vreg += 2;
714 break;
715 case EncodedArrayValueIterator::ValueType::kMethodType: {
716 uint32_t idx = static_cast<uint32_t>(jvalue.i);
717 ObjPtr<mirror::MethodType> ref =
718 class_linker->ResolveMethodType(*dex_file, idx, dex_cache, class_loader);
719 if (ref.IsNull()) {
720 DCHECK(self->IsExceptionPending());
721 return nullptr;
722 }
723 bootstrap_frame->SetVRegReference(vreg, ref.Ptr());
724 vreg += 1;
725 break;
726 }
727 case EncodedArrayValueIterator::ValueType::kMethodHandle: {
728 uint32_t idx = static_cast<uint32_t>(jvalue.i);
729 ObjPtr<mirror::MethodHandle> ref =
730 class_linker->ResolveMethodHandle(idx, referrer);
731 if (ref.IsNull()) {
732 DCHECK(self->IsExceptionPending());
733 return nullptr;
734 }
735 bootstrap_frame->SetVRegReference(vreg, ref.Ptr());
736 vreg += 1;
737 break;
738 }
739 case EncodedArrayValueIterator::ValueType::kString: {
740 dex::StringIndex idx(static_cast<uint32_t>(jvalue.i));
741 ObjPtr<mirror::String> ref = class_linker->ResolveString(*dex_file, idx, dex_cache);
742 if (ref.IsNull()) {
743 DCHECK(self->IsExceptionPending());
744 return nullptr;
745 }
746 bootstrap_frame->SetVRegReference(vreg, ref.Ptr());
747 vreg += 1;
748 break;
749 }
750 case EncodedArrayValueIterator::ValueType::kType: {
751 dex::TypeIndex idx(static_cast<uint32_t>(jvalue.i));
752 ObjPtr<mirror::Class> ref =
753 class_linker->ResolveType(*dex_file, idx, dex_cache, class_loader);
754 if (ref.IsNull()) {
755 DCHECK(self->IsExceptionPending());
756 return nullptr;
757 }
758 bootstrap_frame->SetVRegReference(vreg, ref.Ptr());
759 vreg += 1;
760 break;
761 }
762 case EncodedArrayValueIterator::ValueType::kNull:
763 bootstrap_frame->SetVRegReference(vreg, nullptr);
764 vreg += 1;
765 break;
766 case EncodedArrayValueIterator::ValueType::kField:
767 case EncodedArrayValueIterator::ValueType::kMethod:
768 case EncodedArrayValueIterator::ValueType::kEnum:
769 case EncodedArrayValueIterator::ValueType::kArray:
770 case EncodedArrayValueIterator::ValueType::kAnnotation:
771 // Unreachable based on current EncodedArrayValueIterator::Next().
772 UNREACHABLE();
773 }
774
775 it.Next();
776 }
777
778 // Invoke the bootstrap method handle.
779 JValue result;
780
781 // This array of arguments is unused. DoInvokePolymorphic() operates on either a
782 // an argument array or a range, but always takes an array argument.
783 uint32_t args_unused[Instruction::kMaxVarArgRegs];
784 ArtMethod* invoke_exact =
785 jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_invokeExact);
786 bool invoke_success = DoInvokePolymorphic<true /* is_range */>(self,
787 invoke_exact,
788 *bootstrap_frame,
789 bootstrap,
790 bootstrap_method_type,
791 args_unused,
792 0,
793 &result);
794 if (!invoke_success) {
795 DCHECK(self->IsExceptionPending());
796 return nullptr;
797 }
798
799 Handle<mirror::Object> object(hs.NewHandle(result.GetL()));
800
801 // Check the result is not null.
802 if (UNLIKELY(object.IsNull())) {
803 ThrowNullPointerException("CallSite == null");
804 return nullptr;
805 }
806
807 // Check the result type is a subclass of CallSite.
808 if (UNLIKELY(!object->InstanceOf(mirror::CallSite::StaticClass()))) {
809 ThrowClassCastException(object->GetClass(), mirror::CallSite::StaticClass());
810 return nullptr;
811 }
812
813 Handle<mirror::CallSite> call_site =
814 hs.NewHandle(ObjPtr<mirror::CallSite>::DownCast(ObjPtr<mirror::Object>(result.GetL())));
815
816 // Check the call site target is not null as we're going to invoke it.
817 Handle<mirror::MethodHandle> target = hs.NewHandle(call_site->GetTarget());
818 if (UNLIKELY(target.IsNull())) {
819 ThrowNullPointerException("CallSite target == null");
820 return nullptr;
821 }
822
823 // Check the target method type matches the method type requested.
824 if (UNLIKELY(!target->GetMethodType()->IsExactMatch(method_type.Get()))) {
825 ThrowWrongMethodTypeException(target->GetMethodType(), method_type.Get());
826 return nullptr;
827 }
828
829 return call_site.Get();
830}
831
832template<bool is_range>
833bool DoInvokeCustom(Thread* self,
834 ShadowFrame& shadow_frame,
835 const Instruction* inst,
836 uint16_t inst_data,
837 JValue* result)
838 REQUIRES_SHARED(Locks::mutator_lock_) {
839 // invoke-custom is not supported in transactions. In transactions
840 // there is a limited set of types supported. invoke-custom allows
841 // running arbitrary code and instantiating arbitrary types.
842 CHECK(!Runtime::Current()->IsActiveTransaction());
843 StackHandleScope<4> hs(self);
844 Handle<mirror::DexCache> dex_cache(hs.NewHandle(shadow_frame.GetMethod()->GetDexCache()));
845 const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
846 MutableHandle<mirror::CallSite>
847 call_site(hs.NewHandle(dex_cache->GetResolvedCallSite(call_site_idx)));
848 if (call_site.IsNull()) {
849 call_site.Assign(InvokeBootstrapMethod(self, shadow_frame, call_site_idx));
850 if (UNLIKELY(call_site.IsNull())) {
851 CHECK(self->IsExceptionPending());
852 ThrowWrappedBootstrapMethodError("Exception from call site #%u bootstrap method",
853 call_site_idx);
854 result->SetJ(0);
855 return false;
856 }
857 mirror::CallSite* winning_call_site =
858 dex_cache->SetResolvedCallSite(call_site_idx, call_site.Get());
859 call_site.Assign(winning_call_site);
860 }
861
862 // CallSite.java checks the re-assignment of the call site target
863 // when mutating call site targets. We only check the target is
864 // non-null and has the right type during bootstrap method execution.
865 Handle<mirror::MethodHandle> target = hs.NewHandle(call_site->GetTarget());
866 Handle<mirror::MethodType> target_method_type = hs.NewHandle(target->GetMethodType());
867 DCHECK_EQ(static_cast<size_t>(inst->VRegA()), target_method_type->NumberOfVRegs());
868
869 uint32_t args[Instruction::kMaxVarArgRegs];
870 if (is_range) {
871 args[0] = inst->VRegC_3rc();
872 } else {
873 inst->GetVarArgs(args, inst_data);
874 }
875
876 ArtMethod* invoke_exact =
877 jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_invokeExact);
878 return DoInvokePolymorphic<is_range>(self,
879 invoke_exact,
880 shadow_frame,
881 target,
882 target_method_type,
883 args,
884 args[0],
885 result);
886}
887
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100888template <bool is_range>
889inline void CopyRegisters(ShadowFrame& caller_frame,
890 ShadowFrame* callee_frame,
891 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
892 const size_t first_src_reg,
893 const size_t first_dest_reg,
894 const size_t num_regs) {
895 if (is_range) {
896 const size_t dest_reg_bound = first_dest_reg + num_regs;
897 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound;
898 ++dest_reg, ++src_reg) {
899 AssignRegister(callee_frame, caller_frame, dest_reg, src_reg);
900 }
901 } else {
902 DCHECK_LE(num_regs, arraysize(arg));
903
904 for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) {
905 AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]);
906 }
907 }
908}
909
Igor Murashkin6918bf12015-09-27 19:19:06 -0700910template <bool is_range,
Narayan Kamath370423d2016-10-03 16:51:22 +0100911 bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -0700912static inline bool DoCallCommon(ArtMethod* called_method,
913 Thread* self,
914 ShadowFrame& shadow_frame,
915 JValue* result,
916 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +0100917 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -0700918 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800919 bool string_init = false;
920 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700921 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
922 && called_method->IsConstructor())) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100923 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
Jeff Hao848f70a2014-01-15 13:49:50 -0800924 string_init = true;
925 }
926
Alex Lightdaf58c82016-03-16 23:00:49 +0000927 // Compute method information.
928 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
Igor Murashkin158f35c2015-06-10 15:55:30 -0700929 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200930 uint16_t num_regs;
Jeff Hao5ea84132017-05-05 16:59:29 -0700931 // Test whether to use the interpreter or compiler entrypoint, and save that result to pass to
932 // PerformCall. A deoptimization could occur at any time, and we shouldn't change which
933 // entrypoint to use once we start building the shadow frame.
934 bool use_interpreter_entrypoint = ClassLinker::ShouldUseInterpreterEntrypoint(
935 called_method, called_method->GetEntryPointFromQuickCompiledCode());
Nicolas Geoffray01822292017-03-09 09:03:19 +0000936 if (LIKELY(code_item != nullptr)) {
Jeff Hao5ea84132017-05-05 16:59:29 -0700937 // When transitioning to compiled code, space only needs to be reserved for the input registers.
938 // The rest of the frame gets discarded. This also prevents accessing the called method's code
939 // item, saving memory by keeping code items of compiled code untouched.
940 if (Runtime::Current()->IsStarted() && !use_interpreter_entrypoint) {
941 num_regs = number_of_inputs;
942 } else {
943 num_regs = code_item->registers_size_;
944 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_);
945 }
Nicolas Geoffray01822292017-03-09 09:03:19 +0000946 } else {
947 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
948 num_regs = number_of_inputs;
Jeff Haodf79ddb2017-02-27 14:47:06 -0800949 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200950
Igor Murashkin158f35c2015-06-10 15:55:30 -0700951 // Hack for String init:
952 //
953 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
954 // invoke-x StringFactory(a, b, c, ...)
955 // by effectively dropping the first virtual register from the invoke.
956 //
957 // (at this point the ArtMethod has already been replaced,
958 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +0000959 //
960 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
961 // to handle the compiler optimization of replacing `this` with null without
962 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700963 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -0700964 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700965 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 -0700966
Igor Murashkin158f35c2015-06-10 15:55:30 -0700967 // The new StringFactory call is static and has one fewer argument.
Igor Murashkina06b49b2015-06-25 15:18:12 -0700968 if (code_item == nullptr) {
969 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
970 num_regs--;
971 } // 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 -0700972 number_of_inputs--;
973
974 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -0700975 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700976 arg[i - 1] = arg[i];
977 }
Igor Murashkin6918bf12015-09-27 19:19:06 -0700978 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700979
980 // Rewrite the non-var-arg case
981 vregC++; // Skips the 0th vreg in the range ("this").
982 }
983
984 // Parameter registers go at the end of the shadow frame.
985 DCHECK_GE(num_regs, number_of_inputs);
986 size_t first_dest_reg = num_regs - number_of_inputs;
987 DCHECK_NE(first_dest_reg, (size_t)-1);
988
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200989 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700990 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -0700991 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700992 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700993 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200994
Igor Murashkin158f35c2015-06-10 15:55:30 -0700995 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -0700996 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700997 // Slow path.
998 // We might need to do class loading, which incurs a thread state change to kNative. So
999 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07001000 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +02001001 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001002 self->EndAssertNoThreadSuspension(old_cause);
1003
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001004 // ArtMethod here is needed to check type information of the call site against the callee.
1005 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
1006 //
1007 // As a special case for proxy methods, which are not dex-backed,
1008 // we have to retrieve type information from the proxy's method
1009 // interface method instead (which is dex backed since proxies are never interfaces).
Andreas Gampe542451c2016-07-26 09:02:02 -07001010 ArtMethod* method =
1011 new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001012
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001013 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001014 // to get the exact type of each reference argument.
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001015 const DexFile::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001016 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001017 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001018
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001019 // Handle receiver apart since it's not part of the shorty.
1020 size_t dest_reg = first_dest_reg;
1021 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001022
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001023 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001024 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001025 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
1026 ++dest_reg;
1027 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -07001028 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001029 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001030
1031 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001032 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001033 // Skip the 0th 'shorty' type since it represents the return type.
1034 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001035 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
1036 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001037 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001038 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001039 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001040 if (do_assignability_check && o != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001041 const dex::TypeIndex type_idx = params->GetTypeItem(shorty_pos).type_idx_;
Vladimir Marko942fd312017-01-16 20:52:19 +00001042 ObjPtr<mirror::Class> arg_type = method->GetDexCache()->GetResolvedType(type_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001043 if (arg_type == nullptr) {
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001044 StackHandleScope<1> hs(self);
1045 // Preserve o since it is used below and GetClassFromTypeIndex may cause thread
1046 // suspension.
1047 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&o);
Vladimir Marko942fd312017-01-16 20:52:19 +00001048 arg_type = method->GetClassFromTypeIndex(type_idx, true /* resolve */);
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001049 if (arg_type == nullptr) {
1050 CHECK(self->IsExceptionPending());
1051 return false;
1052 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001053 }
1054 if (!o->VerifierInstanceOf(arg_type)) {
1055 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -07001056 std::string temp1, temp2;
Orion Hodsonfef06642016-11-25 16:07:11 +00001057 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001058 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -08001059 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -07001060 o->GetClass()->GetDescriptor(&temp1),
1061 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001062 return false;
1063 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001064 }
Mathieu Chartieref41db72016-10-25 15:08:01 -07001065 new_shadow_frame->SetVRegReference(dest_reg, o.Ptr());
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001066 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -07001067 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001068 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001069 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001070 uint64_t wide_value =
1071 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
1072 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001073 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001074 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001075 ++dest_reg;
1076 ++arg_offset;
1077 break;
1078 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001079 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001080 default:
1081 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
1082 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001083 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001084 }
1085 } else {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001086 if (is_range) {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001087 DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001088 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001089
1090 CopyRegisters<is_range>(shadow_frame,
1091 new_shadow_frame,
1092 arg,
1093 vregC,
1094 first_dest_reg,
1095 number_of_inputs);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001096 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001097 }
1098
Jeff Hao5ea84132017-05-05 16:59:29 -07001099 PerformCall(self,
1100 code_item,
1101 shadow_frame.GetMethod(),
1102 first_dest_reg,
1103 new_shadow_frame,
1104 result,
1105 use_interpreter_entrypoint);
Jeff Hao848f70a2014-01-15 13:49:50 -08001106
1107 if (string_init && !self->IsExceptionPending()) {
Mingyao Yangffedec52016-05-19 10:48:40 -07001108 SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001109 }
1110
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001111 return !self->IsExceptionPending();
1112}
1113
Igor Murashkin158f35c2015-06-10 15:55:30 -07001114template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001115bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
1116 const Instruction* inst, uint16_t inst_data, JValue* result) {
1117 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001118 const uint16_t number_of_inputs =
1119 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001120
1121 // TODO: find a cleaner way to separate non-range and range information without duplicating
1122 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -07001123 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001124 uint32_t vregC = 0;
1125 if (is_range) {
1126 vregC = inst->VRegC_3rc();
1127 } else {
1128 vregC = inst->VRegC_35c();
1129 inst->GetVarArgs(arg, inst_data);
1130 }
1131
1132 return DoCallCommon<is_range, do_assignability_check>(
1133 called_method, self, shadow_frame,
1134 result, number_of_inputs, arg, vregC);
1135}
1136
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001137template <bool is_range, bool do_access_check, bool transaction_active>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001138bool DoFilledNewArray(const Instruction* inst,
1139 const ShadowFrame& shadow_frame,
1140 Thread* self,
1141 JValue* result) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001142 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
1143 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
1144 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
1145 if (!is_range) {
1146 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
1147 CHECK_LE(length, 5);
1148 }
1149 if (UNLIKELY(length < 0)) {
1150 ThrowNegativeArraySizeException(length);
1151 return false;
1152 }
1153 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08001154 ObjPtr<mirror::Class> array_class = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
Mathieu Chartieref41db72016-10-25 15:08:01 -07001155 shadow_frame.GetMethod(),
1156 self,
1157 false,
1158 do_access_check);
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001159 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001160 DCHECK(self->IsExceptionPending());
1161 return false;
1162 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001163 CHECK(array_class->IsArrayClass());
Mathieu Chartieref41db72016-10-25 15:08:01 -07001164 ObjPtr<mirror::Class> component_class = array_class->GetComponentType();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001165 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
1166 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
1167 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001168 ThrowRuntimeException("Bad filled array request for type %s",
David Sehr709b0702016-10-13 09:12:37 -07001169 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001170 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001171 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -08001172 "Found type %s; filled-new-array not implemented for anything but 'int'",
David Sehr709b0702016-10-13 09:12:37 -07001173 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001174 }
1175 return false;
1176 }
Mathieu Chartieref41db72016-10-25 15:08:01 -07001177 ObjPtr<mirror::Object> new_array = mirror::Array::Alloc<true>(
1178 self,
1179 array_class,
1180 length,
1181 array_class->GetComponentSizeShift(),
1182 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001183 if (UNLIKELY(new_array == nullptr)) {
1184 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001185 return false;
1186 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001187 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
1188 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001189 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +01001190 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001191 } else {
Ian Rogers29a26482014-05-02 15:27:29 -07001192 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +01001193 }
Sebastien Hertzabff6432014-01-27 18:01:39 +01001194 for (int32_t i = 0; i < length; ++i) {
1195 size_t src_reg = is_range ? vregC + i : arg[i];
1196 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001197 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
1198 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +01001199 } else {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001200 new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<transaction_active>(
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001201 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001202 }
1203 }
1204
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001205 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001206 return true;
1207}
1208
Mathieu Chartieref41db72016-10-25 15:08:01 -07001209// TODO: Use ObjPtr here.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001210template<typename T>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001211static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array,
1212 int32_t count)
1213 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001214 Runtime* runtime = Runtime::Current();
1215 for (int32_t i = 0; i < count; ++i) {
1216 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
1217 }
1218}
1219
Mathieu Chartieref41db72016-10-25 15:08:01 -07001220void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001221 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001222 DCHECK(Runtime::Current()->IsActiveTransaction());
1223 DCHECK(array != nullptr);
1224 DCHECK_LE(count, array->GetLength());
1225 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
1226 switch (primitive_component_type) {
1227 case Primitive::kPrimBoolean:
1228 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
1229 break;
1230 case Primitive::kPrimByte:
1231 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
1232 break;
1233 case Primitive::kPrimChar:
1234 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
1235 break;
1236 case Primitive::kPrimShort:
1237 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
1238 break;
1239 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001240 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
1241 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001242 case Primitive::kPrimFloat:
1243 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
1244 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001245 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001246 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
1247 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001248 case Primitive::kPrimDouble:
1249 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
1250 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001251 default:
1252 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
1253 << " in fill-array-data";
1254 break;
1255 }
1256}
1257
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001258// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +02001259#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001260 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001261 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
1262 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +02001263 const Instruction* inst, uint16_t inst_data, \
1264 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001265EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
1266EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
1267EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
1268EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
1269#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001270
Orion Hodsonc069a302017-01-18 09:23:12 +00001271// Explicit DoInvokeCustom template function declarations.
1272#define EXPLICIT_DO_INVOKE_CUSTOM_TEMPLATE_DECL(_is_range) \
1273 template REQUIRES_SHARED(Locks::mutator_lock_) \
1274 bool DoInvokeCustom<_is_range>( \
1275 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
Narayan Kamath9823e782016-08-03 12:46:58 +01001276 uint16_t inst_data, JValue* result)
Orion Hodsonc069a302017-01-18 09:23:12 +00001277EXPLICIT_DO_INVOKE_CUSTOM_TEMPLATE_DECL(false);
1278EXPLICIT_DO_INVOKE_CUSTOM_TEMPLATE_DECL(true);
1279#undef EXPLICIT_DO_INVOKE_CUSTOM_TEMPLATE_DECL
Narayan Kamath9823e782016-08-03 12:46:58 +01001280
Orion Hodsonc069a302017-01-18 09:23:12 +00001281// Explicit DoInvokePolymorphic template function declarations.
1282#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range) \
1283 template REQUIRES_SHARED(Locks::mutator_lock_) \
1284 bool DoInvokePolymorphic<_is_range>( \
1285 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1286 uint16_t inst_data, JValue* result)
1287EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false);
1288EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true);
Narayan Kamath9823e782016-08-03 12:46:58 +01001289#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1290
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001291// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001292#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001293 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001294 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
1295 const ShadowFrame& shadow_frame, \
1296 Thread* self, JValue* result)
1297#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
1298 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
1299 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
1300 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
1301 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
1302EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
1303EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
1304#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001305#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
1306
1307} // namespace interpreter
1308} // namespace art