blob: e607d4d8bb9f33e4dce6abe4cd18d0ec5d1ebb3c [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"
Narayan Kamath9823e782016-08-03 12:46:58 +010031#include "mirror/method_handle_impl.h"
32#include "reflection.h"
33#include "reflection-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070034#include "stack.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070035#include "unstarted_runtime.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080036#include "verifier/method_verifier.h"
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +010037#include "well_known_classes.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020038
39namespace art {
40namespace interpreter {
41
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000042void ThrowNullPointerExceptionFromInterpreter() {
43 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -070044}
45
Orion Hodson3d617ac2016-10-19 14:00:46 +010046template<Primitive::Type field_type>
47static ALWAYS_INLINE void DoFieldGetCommon(Thread* self,
48 const ShadowFrame& shadow_frame,
49 ObjPtr<mirror::Object>& obj,
50 ArtField* field,
51 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
52 field->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
53
54 // Report this field access to instrumentation if needed.
55 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
56 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
57 StackHandleScope<1> hs(self);
58 // Wrap in handle wrapper in case the listener does thread suspension.
59 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
60 ObjPtr<mirror::Object> this_object;
61 if (!field->IsStatic()) {
62 this_object = obj;
63 }
64 instrumentation->FieldReadEvent(self,
65 this_object.Ptr(),
66 shadow_frame.GetMethod(),
67 shadow_frame.GetDexPC(),
68 field);
69 }
70
71 switch (field_type) {
72 case Primitive::kPrimBoolean:
73 result->SetZ(field->GetBoolean(obj));
74 break;
75 case Primitive::kPrimByte:
76 result->SetB(field->GetByte(obj));
77 break;
78 case Primitive::kPrimChar:
79 result->SetC(field->GetChar(obj));
80 break;
81 case Primitive::kPrimShort:
82 result->SetS(field->GetShort(obj));
83 break;
84 case Primitive::kPrimInt:
85 result->SetI(field->GetInt(obj));
86 break;
87 case Primitive::kPrimLong:
88 result->SetJ(field->GetLong(obj));
89 break;
90 case Primitive::kPrimNot:
91 result->SetL(field->GetObject(obj));
92 break;
93 default:
94 LOG(FATAL) << "Unreachable: " << field_type;
95 UNREACHABLE();
96 }
97}
98
Ian Rogers54874942014-06-10 16:31:03 -070099template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
100bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
101 uint16_t inst_data) {
102 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
103 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100104 ArtField* f =
105 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
106 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -0700107 if (UNLIKELY(f == nullptr)) {
108 CHECK(self->IsExceptionPending());
109 return false;
110 }
Mathieu Chartieref41db72016-10-25 15:08:01 -0700111 ObjPtr<mirror::Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -0700112 if (is_static) {
113 obj = f->GetDeclaringClass();
114 } else {
115 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
116 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000117 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers54874942014-06-10 16:31:03 -0700118 return false;
119 }
120 }
Orion Hodson3d617ac2016-10-19 14:00:46 +0100121
122 JValue result;
123 DoFieldGetCommon<field_type>(self, shadow_frame, obj, f, &result);
Ian Rogers54874942014-06-10 16:31:03 -0700124 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
125 switch (field_type) {
126 case Primitive::kPrimBoolean:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100127 shadow_frame.SetVReg(vregA, result.GetZ());
Ian Rogers54874942014-06-10 16:31:03 -0700128 break;
129 case Primitive::kPrimByte:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100130 shadow_frame.SetVReg(vregA, result.GetB());
Ian Rogers54874942014-06-10 16:31:03 -0700131 break;
132 case Primitive::kPrimChar:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100133 shadow_frame.SetVReg(vregA, result.GetC());
Ian Rogers54874942014-06-10 16:31:03 -0700134 break;
135 case Primitive::kPrimShort:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100136 shadow_frame.SetVReg(vregA, result.GetS());
Ian Rogers54874942014-06-10 16:31:03 -0700137 break;
138 case Primitive::kPrimInt:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100139 shadow_frame.SetVReg(vregA, result.GetI());
Ian Rogers54874942014-06-10 16:31:03 -0700140 break;
141 case Primitive::kPrimLong:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100142 shadow_frame.SetVRegLong(vregA, result.GetJ());
Ian Rogers54874942014-06-10 16:31:03 -0700143 break;
144 case Primitive::kPrimNot:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100145 shadow_frame.SetVRegReference(vregA, result.GetL());
Ian Rogers54874942014-06-10 16:31:03 -0700146 break;
147 default:
148 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700149 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700150 }
151 return true;
152}
153
154// Explicitly instantiate all DoFieldGet functions.
155#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
156 template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \
157 ShadowFrame& shadow_frame, \
158 const Instruction* inst, \
159 uint16_t inst_data)
160
161#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
162 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \
163 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true);
164
165// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700166EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
167EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
168EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
169EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
170EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
171EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
172EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700173
174// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700175EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
176EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
177EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
178EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
179EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
180EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
181EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700182
183#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
184#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
185
Orion Hodson3d617ac2016-10-19 14:00:46 +0100186// Helper for getters in invoke-polymorphic.
187inline static void DoFieldGetForInvokePolymorphic(Thread* self,
188 const ShadowFrame& shadow_frame,
189 ObjPtr<mirror::Object>& obj,
190 ArtField* field,
191 Primitive::Type field_type,
192 JValue* result)
193 REQUIRES_SHARED(Locks::mutator_lock_) {
194 switch (field_type) {
195 case Primitive::kPrimBoolean:
196 DoFieldGetCommon<Primitive::kPrimBoolean>(self, shadow_frame, obj, field, result);
197 break;
198 case Primitive::kPrimByte:
199 DoFieldGetCommon<Primitive::kPrimByte>(self, shadow_frame, obj, field, result);
200 break;
201 case Primitive::kPrimChar:
202 DoFieldGetCommon<Primitive::kPrimChar>(self, shadow_frame, obj, field, result);
203 break;
204 case Primitive::kPrimShort:
205 DoFieldGetCommon<Primitive::kPrimShort>(self, shadow_frame, obj, field, result);
206 break;
207 case Primitive::kPrimInt:
208 DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result);
209 break;
210 case Primitive::kPrimLong:
211 DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result);
212 break;
213 case Primitive::kPrimFloat:
214 DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result);
215 break;
216 case Primitive::kPrimDouble:
217 DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result);
218 break;
219 case Primitive::kPrimNot:
220 DoFieldGetCommon<Primitive::kPrimNot>(self, shadow_frame, obj, field, result);
221 break;
222 case Primitive::kPrimVoid:
223 LOG(FATAL) << "Unreachable: " << field_type;
224 UNREACHABLE();
225 }
226}
227
Ian Rogers54874942014-06-10 16:31:03 -0700228// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
229// Returns true on success, otherwise throws an exception and returns false.
230template<Primitive::Type field_type>
231bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700232 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700233 if (UNLIKELY(obj == nullptr)) {
234 // We lost the reference to the field index so we cannot get a more
235 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000236 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700237 return false;
238 }
239 MemberOffset field_offset(inst->VRegC_22c());
240 // Report this field access to instrumentation if needed. Since we only have the offset of
241 // the field from the base of the object, we need to look for it first.
242 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
243 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
244 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
245 field_offset.Uint32Value());
246 DCHECK(f != nullptr);
247 DCHECK(!f->IsStatic());
Mathieu Chartiera3147732016-10-26 22:57:02 -0700248 StackHandleScope<1> hs(Thread::Current());
249 // Save obj in case the instrumentation event has thread suspension.
250 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700251 instrumentation->FieldReadEvent(Thread::Current(),
252 obj.Ptr(),
253 shadow_frame.GetMethod(),
254 shadow_frame.GetDexPC(),
255 f);
Ian Rogers54874942014-06-10 16:31:03 -0700256 }
257 // Note: iget-x-quick instructions are only for non-volatile fields.
258 const uint32_t vregA = inst->VRegA_22c(inst_data);
259 switch (field_type) {
260 case Primitive::kPrimInt:
261 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
262 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800263 case Primitive::kPrimBoolean:
264 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
265 break;
266 case Primitive::kPrimByte:
267 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
268 break;
269 case Primitive::kPrimChar:
270 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
271 break;
272 case Primitive::kPrimShort:
273 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
274 break;
Ian Rogers54874942014-06-10 16:31:03 -0700275 case Primitive::kPrimLong:
276 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
277 break;
278 case Primitive::kPrimNot:
279 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
280 break;
281 default:
282 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700283 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700284 }
285 return true;
286}
287
288// Explicitly instantiate all DoIGetQuick functions.
289#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
290 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
291 uint16_t inst_data)
292
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800293EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
294EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
295EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
296EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
297EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
298EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
299EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700300#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
301
302template<Primitive::Type field_type>
303static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700304 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700305 JValue field_value;
306 switch (field_type) {
307 case Primitive::kPrimBoolean:
308 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
309 break;
310 case Primitive::kPrimByte:
311 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
312 break;
313 case Primitive::kPrimChar:
314 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
315 break;
316 case Primitive::kPrimShort:
317 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
318 break;
319 case Primitive::kPrimInt:
320 field_value.SetI(shadow_frame.GetVReg(vreg));
321 break;
322 case Primitive::kPrimLong:
323 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
324 break;
325 case Primitive::kPrimNot:
326 field_value.SetL(shadow_frame.GetVRegReference(vreg));
327 break;
328 default:
329 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700330 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700331 }
332 return field_value;
333}
334
Orion Hodson3d617ac2016-10-19 14:00:46 +0100335template<Primitive::Type field_type, bool do_assignability_check, bool transaction_active>
336static inline bool DoFieldPutCommon(Thread* self,
337 const ShadowFrame& shadow_frame,
338 ObjPtr<mirror::Object>& obj,
339 ArtField* f,
340 size_t vregA) REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +0200341 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100342
Ian Rogers54874942014-06-10 16:31:03 -0700343 // Report this field access to instrumentation if needed. Since we only have the offset of
344 // the field from the base of the object, we need to look for it first.
345 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
346 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
Mathieu Chartier0715c0b2016-10-03 22:49:46 -0700347 StackHandleScope<1> hs(self);
348 // Wrap in handle wrapper in case the listener does thread suspension.
349 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
Ian Rogers54874942014-06-10 16:31:03 -0700350 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700351 ObjPtr<mirror::Object> this_object = f->IsStatic() ? nullptr : obj;
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700352 instrumentation->FieldWriteEvent(self, this_object.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -0700353 shadow_frame.GetMethod(),
354 shadow_frame.GetDexPC(),
355 f,
356 field_value);
Ian Rogers54874942014-06-10 16:31:03 -0700357 }
Orion Hodson3d617ac2016-10-19 14:00:46 +0100358
Ian Rogers54874942014-06-10 16:31:03 -0700359 switch (field_type) {
360 case Primitive::kPrimBoolean:
361 f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA));
362 break;
363 case Primitive::kPrimByte:
364 f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA));
365 break;
366 case Primitive::kPrimChar:
367 f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA));
368 break;
369 case Primitive::kPrimShort:
370 f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA));
371 break;
372 case Primitive::kPrimInt:
373 f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA));
374 break;
375 case Primitive::kPrimLong:
376 f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA));
377 break;
378 case Primitive::kPrimNot: {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700379 ObjPtr<mirror::Object> reg = shadow_frame.GetVRegReference(vregA);
Ian Rogers54874942014-06-10 16:31:03 -0700380 if (do_assignability_check && reg != nullptr) {
381 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
382 // object in the destructor.
Mathieu Chartieref41db72016-10-25 15:08:01 -0700383 ObjPtr<mirror::Class> field_class;
Ian Rogers54874942014-06-10 16:31:03 -0700384 {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700385 StackHandleScope<2> hs(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700386 HandleWrapperObjPtr<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
Mathieu Chartier3398c782016-09-30 10:27:43 -0700387 HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700388 field_class = f->GetType<true>();
Ian Rogers54874942014-06-10 16:31:03 -0700389 }
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700390 if (!reg->VerifierInstanceOf(field_class.Ptr())) {
Ian Rogers54874942014-06-10 16:31:03 -0700391 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700392 std::string temp1, temp2, temp3;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000393 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Ian Rogers54874942014-06-10 16:31:03 -0700394 "Put '%s' that is not instance of field '%s' in '%s'",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700395 reg->GetClass()->GetDescriptor(&temp1),
396 field_class->GetDescriptor(&temp2),
397 f->GetDeclaringClass()->GetDescriptor(&temp3));
Ian Rogers54874942014-06-10 16:31:03 -0700398 return false;
399 }
400 }
401 f->SetObj<transaction_active>(obj, reg);
402 break;
403 }
404 default:
405 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700406 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700407 }
408 return true;
409}
410
Orion Hodson3d617ac2016-10-19 14:00:46 +0100411template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
412 bool transaction_active>
413bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
414 uint16_t inst_data) {
415 const bool do_assignability_check = do_access_check;
416 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
417 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
418 ArtField* f =
419 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
420 Primitive::ComponentSize(field_type));
421 if (UNLIKELY(f == nullptr)) {
422 CHECK(self->IsExceptionPending());
423 return false;
424 }
425 ObjPtr<mirror::Object> obj;
426 if (is_static) {
427 obj = f->GetDeclaringClass();
428 } else {
429 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
430 if (UNLIKELY(obj == nullptr)) {
431 ThrowNullPointerExceptionForFieldAccess(f, false);
432 return false;
433 }
434 }
435
436 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
437 return DoFieldPutCommon<field_type, do_assignability_check, transaction_active>(self,
438 shadow_frame,
439 obj,
440 f,
441 vregA);
442}
443
Ian Rogers54874942014-06-10 16:31:03 -0700444// Explicitly instantiate all DoFieldPut functions.
445#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
446 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
447 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
448
449#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
450 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
451 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
452 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
453 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
454
455// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700456EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
457EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
458EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
459EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
460EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
461EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
462EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700463
464// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700465EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
466EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
467EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
468EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
469EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
470EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
471EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700472
473#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
474#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
475
Orion Hodson3d617ac2016-10-19 14:00:46 +0100476// Helper for setters in invoke-polymorphic.
477bool DoFieldPutForInvokePolymorphic(Thread* self,
478 ShadowFrame& shadow_frame,
479 ObjPtr<mirror::Object>& obj,
480 ArtField* field,
481 Primitive::Type field_type,
482 size_t vregA) REQUIRES_SHARED(Locks::mutator_lock_) {
483 static const bool kDoCheckAssignability = false;
484 static const bool kTransaction = false;
485 switch (field_type) {
486 case Primitive::kPrimBoolean:
487 return DoFieldPutCommon<Primitive::kPrimBoolean, kDoCheckAssignability, kTransaction>(
488 self, shadow_frame, obj, field, vregA);
489 case Primitive::kPrimByte:
490 return DoFieldPutCommon<Primitive::kPrimByte, kDoCheckAssignability, kTransaction>(
491 self, shadow_frame, obj, field, vregA);
492 case Primitive::kPrimChar:
493 return DoFieldPutCommon<Primitive::kPrimChar, kDoCheckAssignability, kTransaction>(
494 self, shadow_frame, obj, field, vregA);
495 case Primitive::kPrimShort:
496 return DoFieldPutCommon<Primitive::kPrimShort, kDoCheckAssignability, kTransaction>(
497 self, shadow_frame, obj, field, vregA);
498 case Primitive::kPrimInt:
499 return DoFieldPutCommon<Primitive::kPrimInt, kDoCheckAssignability, kTransaction>(
500 self, shadow_frame, obj, field, vregA);
501 case Primitive::kPrimLong:
502 return DoFieldPutCommon<Primitive::kPrimLong, kDoCheckAssignability, kTransaction>(
503 self, shadow_frame, obj, field, vregA);
504 case Primitive::kPrimFloat:
505 return DoFieldPutCommon<Primitive::kPrimInt, kDoCheckAssignability, kTransaction>(
506 self, shadow_frame, obj, field, vregA);
507 case Primitive::kPrimDouble:
508 return DoFieldPutCommon<Primitive::kPrimLong, kDoCheckAssignability, kTransaction>(
509 self, shadow_frame, obj, field, vregA);
510 case Primitive::kPrimNot:
511 return DoFieldPutCommon<Primitive::kPrimNot, kDoCheckAssignability, kTransaction>(
512 self, shadow_frame, obj, field, vregA);
513 case Primitive::kPrimVoid:
514 LOG(FATAL) << "Unreachable: " << field_type;
515 UNREACHABLE();
516 }
517}
518
Ian Rogers54874942014-06-10 16:31:03 -0700519template<Primitive::Type field_type, bool transaction_active>
520bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700521 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700522 if (UNLIKELY(obj == nullptr)) {
523 // We lost the reference to the field index so we cannot get a more
524 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000525 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700526 return false;
527 }
528 MemberOffset field_offset(inst->VRegC_22c());
529 const uint32_t vregA = inst->VRegA_22c(inst_data);
530 // Report this field modification to instrumentation if needed. Since we only have the offset of
531 // the field from the base of the object, we need to look for it first.
532 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
533 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
534 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
535 field_offset.Uint32Value());
536 DCHECK(f != nullptr);
537 DCHECK(!f->IsStatic());
538 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700539 StackHandleScope<1> hs(Thread::Current());
540 // Save obj in case the instrumentation event has thread suspension.
541 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700542 instrumentation->FieldWriteEvent(Thread::Current(),
543 obj.Ptr(),
544 shadow_frame.GetMethod(),
545 shadow_frame.GetDexPC(),
546 f,
547 field_value);
Ian Rogers54874942014-06-10 16:31:03 -0700548 }
549 // Note: iput-x-quick instructions are only for non-volatile fields.
550 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700551 case Primitive::kPrimBoolean:
552 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
553 break;
554 case Primitive::kPrimByte:
555 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
556 break;
557 case Primitive::kPrimChar:
558 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
559 break;
560 case Primitive::kPrimShort:
561 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
562 break;
Ian Rogers54874942014-06-10 16:31:03 -0700563 case Primitive::kPrimInt:
564 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
565 break;
566 case Primitive::kPrimLong:
567 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
568 break;
569 case Primitive::kPrimNot:
570 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
571 break;
572 default:
573 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700574 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700575 }
576 return true;
577}
578
579// Explicitly instantiate all DoIPutQuick functions.
580#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
581 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
582 const Instruction* inst, \
583 uint16_t inst_data)
584
585#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
586 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
587 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
588
Andreas Gampec8ccf682014-09-29 20:07:43 -0700589EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
590EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
591EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
592EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
593EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
594EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
595EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700596#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
597#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
598
Sebastien Hertz520633b2015-09-08 17:03:36 +0200599// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700600uint32_t FindNextInstructionFollowingException(
601 Thread* self, ShadowFrame& shadow_frame, uint32_t dex_pc,
602 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700603 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700604 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000605 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Sebastien Hertz520633b2015-09-08 17:03:36 +0200606 if (instrumentation != nullptr && instrumentation->HasExceptionCaughtListeners()
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000607 && self->IsExceptionThrownByCurrentMethod(exception.Get())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000608 instrumentation->ExceptionCaughtEvent(self, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200609 }
Ian Rogers54874942014-06-10 16:31:03 -0700610 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700611 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
612 hs.NewHandle(exception->GetClass()), dex_pc, &clear_exception);
Sebastien Hertz520633b2015-09-08 17:03:36 +0200613 if (found_dex_pc == DexFile::kDexNoIndex && instrumentation != nullptr) {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000614 // Exception is not caught by the current method. We will unwind to the
615 // caller. Notify any instrumentation listener.
Sebastien Hertz9f102032014-05-23 08:59:42 +0200616 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
Ian Rogers54874942014-06-10 16:31:03 -0700617 shadow_frame.GetMethod(), dex_pc);
618 } else {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000619 // Exception is caught in the current method. We will jump to the found_dex_pc.
Ian Rogers54874942014-06-10 16:31:03 -0700620 if (clear_exception) {
621 self->ClearException();
622 }
623 }
624 return found_dex_pc;
625}
626
Ian Rogerse94652f2014-12-02 11:13:19 -0800627void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
628 LOG(FATAL) << "Unexpected instruction: "
629 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
630 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700631}
632
Sebastien Hertz45b15972015-04-03 16:07:05 +0200633void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700634 va_list args;
635 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200636 AbortTransactionV(self, fmt, args);
637 va_end(args);
638}
639
640void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
641 CHECK(Runtime::Current()->IsActiveTransaction());
642 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100643 std::string abort_msg;
644 StringAppendV(&abort_msg, fmt, args);
645 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200646 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700647}
648
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100649// START DECLARATIONS :
650//
651// These additional declarations are required because clang complains
652// about ALWAYS_INLINE (-Werror, -Wgcc-compat) in definitions.
653//
654
Narayan Kamath9823e782016-08-03 12:46:58 +0100655template <bool is_range, bool do_assignability_check>
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000656static ALWAYS_INLINE bool DoCallCommon(ArtMethod* called_method,
657 Thread* self,
658 ShadowFrame& shadow_frame,
659 JValue* result,
660 uint16_t number_of_inputs,
661 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
662 uint32_t vregC) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100663
664template <bool is_range>
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000665static ALWAYS_INLINE bool DoCallPolymorphic(ArtMethod* called_method,
666 Handle<mirror::MethodType> callsite_type,
667 Handle<mirror::MethodType> target_type,
668 Thread* self,
669 ShadowFrame& shadow_frame,
670 JValue* result,
671 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
672 uint32_t vregC,
673 const MethodHandleKind handle_kind)
674 REQUIRES_SHARED(Locks::mutator_lock_);
675
676template <bool is_range>
677static ALWAYS_INLINE bool DoCallTransform(ArtMethod* called_method,
678 Handle<mirror::MethodType> callsite_type,
679 Handle<mirror::MethodType> callee_type,
680 Thread* self,
681 ShadowFrame& shadow_frame,
682 Handle<mirror::MethodHandleImpl> receiver,
683 JValue* result,
684 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
685 uint32_t vregC) REQUIRES_SHARED(Locks::mutator_lock_);
686
687ALWAYS_INLINE void PerformCall(Thread* self,
688 const DexFile::CodeItem* code_item,
689 ArtMethod* caller_method,
690 const size_t first_dest_reg,
691 ShadowFrame* callee_frame,
692 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_);
693
694template <bool is_range>
695ALWAYS_INLINE void CopyRegisters(ShadowFrame& caller_frame,
696 ShadowFrame* callee_frame,
697 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
698 const size_t first_src_reg,
699 const size_t first_dest_reg,
700 const size_t num_regs) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100701
702// END DECLARATIONS.
703
Siva Chandra05d24152016-01-05 17:43:17 -0800704void ArtInterpreterToCompiledCodeBridge(Thread* self,
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100705 ArtMethod* caller,
Siva Chandra05d24152016-01-05 17:43:17 -0800706 const DexFile::CodeItem* code_item,
707 ShadowFrame* shadow_frame,
708 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700709 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700710 ArtMethod* method = shadow_frame->GetMethod();
711 // Ensure static methods are initialized.
712 if (method->IsStatic()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700713 ObjPtr<mirror::Class> declaringClass = method->GetDeclaringClass();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700714 if (UNLIKELY(!declaringClass->IsInitialized())) {
715 self->PushShadowFrame(shadow_frame);
716 StackHandleScope<1> hs(self);
717 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
718 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
719 true))) {
720 self->PopShadowFrame();
721 DCHECK(self->IsExceptionPending());
722 return;
723 }
724 self->PopShadowFrame();
725 CHECK(h_class->IsInitializing());
726 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
727 method = shadow_frame->GetMethod();
728 }
729 }
730 uint16_t arg_offset = (code_item == nullptr)
731 ? 0
732 : code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100733 jit::Jit* jit = Runtime::Current()->GetJit();
734 if (jit != nullptr && caller != nullptr) {
735 jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
736 }
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700737 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
738 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
Andreas Gampe542451c2016-07-26 09:02:02 -0700739 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700740}
741
Mingyao Yangffedec52016-05-19 10:48:40 -0700742void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
743 uint16_t this_obj_vreg,
744 JValue result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700745 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700746 ObjPtr<mirror::Object> existing = shadow_frame->GetVRegReference(this_obj_vreg);
Mingyao Yangffedec52016-05-19 10:48:40 -0700747 if (existing == nullptr) {
748 // If it's null, we come from compiled code that was deoptimized. Nothing to do,
749 // as the compiler verified there was no alias.
750 // Set the new string result of the StringFactory.
751 shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
752 return;
753 }
754 // Set the string init result into all aliases.
755 for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
756 if (shadow_frame->GetVRegReference(i) == existing) {
757 DCHECK_EQ(shadow_frame->GetVRegReference(i),
758 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
759 shadow_frame->SetVRegReference(i, result.GetL());
760 DCHECK_EQ(shadow_frame->GetVRegReference(i),
761 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
762 }
763 }
764}
765
Orion Hodson3d617ac2016-10-19 14:00:46 +0100766inline static bool IsInvokeExact(const DexFile& dex_file, int invoke_method_idx) {
767 // This check uses string comparison as it needs less code and data
768 // to do than fetching the associated ArtMethod from the DexCache
769 // and checking against ArtMethods in the well known classes. The
770 // verifier needs to perform a more rigorous check.
771 const char* method_name = dex_file.GetMethodName(dex_file.GetMethodId(invoke_method_idx));
772 bool is_invoke_exact = (0 == strcmp(method_name, "invokeExact"));
773 DCHECK(is_invoke_exact || (0 == strcmp(method_name, "invoke")));
774 return is_invoke_exact;
775}
776
Narayan Kamath9823e782016-08-03 12:46:58 +0100777template<bool is_range, bool do_access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -0700778inline bool DoInvokePolymorphic(Thread* self,
779 ShadowFrame& shadow_frame,
780 const Instruction* inst,
781 uint16_t inst_data,
782 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100783 // Invoke-polymorphic instructions always take a receiver. i.e, they are never static.
784 const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc();
Orion Hodson3d617ac2016-10-19 14:00:46 +0100785 const int invoke_method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc();
Narayan Kamath9823e782016-08-03 12:46:58 +0100786
Orion Hodson3d617ac2016-10-19 14:00:46 +0100787 // Determine if this invocation is MethodHandle.invoke() or
788 // MethodHandle.invokeExact().
789 bool is_invoke_exact = IsInvokeExact(shadow_frame.GetMethod()->GetDeclaringClass()->GetDexFile(),
790 invoke_method_idx);
791
792 // The invoke_method_idx here is the name of the signature polymorphic method that
Narayan Kamath9823e782016-08-03 12:46:58 +0100793 // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact)
794 // and not the method that we'll dispatch to in the end.
795 //
796 // TODO(narayan) We'll have to check in the verifier that this is in fact a
797 // signature polymorphic method so that we disallow calls via invoke-polymorphic
798 // to non sig-poly methods. This would also have the side effect of verifying
799 // that vRegC really is a reference type.
Narayan Kamath208f8572016-08-03 12:46:58 +0100800 StackHandleScope<6> hs(self);
801 Handle<mirror::MethodHandleImpl> method_handle(hs.NewHandle(
Mathieu Chartieref41db72016-10-25 15:08:01 -0700802 ObjPtr<mirror::MethodHandleImpl>::DownCast(
803 MakeObjPtr(shadow_frame.GetVRegReference(vRegC)))));
Narayan Kamath208f8572016-08-03 12:46:58 +0100804 if (UNLIKELY(method_handle.Get() == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100805 // Note that the invoke type is kVirtual here because a call to a signature
806 // polymorphic method is shaped like a virtual call at the bytecode level.
Orion Hodson3d617ac2016-10-19 14:00:46 +0100807 ThrowNullPointerExceptionForMethodAccess(invoke_method_idx, InvokeType::kVirtual);
Narayan Kamath9823e782016-08-03 12:46:58 +0100808 result->SetJ(0);
809 return false;
810 }
811
812 // The vRegH value gives the index of the proto_id associated with this
813 // signature polymorphic callsite.
814 const uint32_t callsite_proto_id = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
815
816 // Call through to the classlinker and ask it to resolve the static type associated
817 // with the callsite. This information is stored in the dex cache so it's
818 // guaranteed to be fast after the first resolution.
Narayan Kamath9823e782016-08-03 12:46:58 +0100819 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Narayan Kamath208f8572016-08-03 12:46:58 +0100820 Handle<mirror::Class> caller_class(hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass()));
821 Handle<mirror::MethodType> callsite_type(hs.NewHandle(class_linker->ResolveMethodType(
Narayan Kamath9823e782016-08-03 12:46:58 +0100822 caller_class->GetDexFile(), callsite_proto_id,
823 hs.NewHandle<mirror::DexCache>(caller_class->GetDexCache()),
Narayan Kamath208f8572016-08-03 12:46:58 +0100824 hs.NewHandle<mirror::ClassLoader>(caller_class->GetClassLoader()))));
Narayan Kamath9823e782016-08-03 12:46:58 +0100825
826 // This implies we couldn't resolve one or more types in this method handle.
Narayan Kamath208f8572016-08-03 12:46:58 +0100827 if (UNLIKELY(callsite_type.Get() == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100828 CHECK(self->IsExceptionPending());
829 result->SetJ(0);
830 return false;
831 }
832
Narayan Kamath9823e782016-08-03 12:46:58 +0100833 const MethodHandleKind handle_kind = method_handle->GetHandleKind();
Narayan Kamath208f8572016-08-03 12:46:58 +0100834 Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType()));
Narayan Kamath208f8572016-08-03 12:46:58 +0100835 CHECK(handle_type.Get() != nullptr);
Narayan Kamath0a8485e2016-11-02 18:47:11 +0000836 if (is_invoke_exact) {
837 // We need to check the nominal type of the handle in addition to the
838 // real type. The "nominal" type is present when MethodHandle.asType is
839 // called any handle, and results in the declared type of the handle
840 // changing.
841 ObjPtr<mirror::MethodType> nominal_type(method_handle->GetNominalType());
842 ObjPtr<mirror::MethodType> check_type(nullptr);
843 if (LIKELY(nominal_type.Ptr() == nullptr)) {
844 check_type.Assign(handle_type.Get());
845 } else {
846 check_type.Assign(nominal_type.Ptr());
847 }
848
849 if (UNLIKELY(!callsite_type->IsExactMatch(check_type.Ptr()))) {
850 ThrowWrongMethodTypeException(check_type.Ptr(), callsite_type.Get());
851 return false;
852 }
Orion Hodson3d617ac2016-10-19 14:00:46 +0100853 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100854
Narayan Kamath9823e782016-08-03 12:46:58 +0100855 uint32_t arg[Instruction::kMaxVarArgRegs] = {};
Narayan Kamath000e1882016-10-24 17:14:25 +0100856 uint32_t first_src_reg = 0;
Narayan Kamath9823e782016-08-03 12:46:58 +0100857 if (is_range) {
Narayan Kamath000e1882016-10-24 17:14:25 +0100858 first_src_reg = (inst->VRegC_4rcc() + 1);
Narayan Kamath9823e782016-08-03 12:46:58 +0100859 } else {
860 inst->GetVarArgs(arg, inst_data);
861 arg[0] = arg[1];
862 arg[1] = arg[2];
863 arg[2] = arg[3];
864 arg[3] = arg[4];
865 arg[4] = 0;
Narayan Kamath000e1882016-10-24 17:14:25 +0100866 first_src_reg = arg[0];
Narayan Kamath9823e782016-08-03 12:46:58 +0100867 }
868
869 if (IsInvoke(handle_kind)) {
Orion Hodson3d617ac2016-10-19 14:00:46 +0100870 // Get the method we're actually invoking along with the kind of
871 // invoke that is desired. We don't need to perform access checks at this
872 // point because they would have been performed on our behalf at the point
873 // of creation of the method handle.
874 ArtMethod* called_method = method_handle->GetTargetMethod();
875 CHECK(called_method != nullptr);
876
Narayan Kamath9823e782016-08-03 12:46:58 +0100877 if (handle_kind == kInvokeVirtual || handle_kind == kInvokeInterface) {
Narayan Kamath000e1882016-10-24 17:14:25 +0100878 // TODO: Unfortunately, we have to postpone dynamic receiver based checks
879 // because the receiver might be cast or might come from an emulated stack
880 // frame, which means that it is unknown at this point. We perform these
881 // checks inside DoCallPolymorphic right before we do the actualy invoke.
Narayan Kamath9823e782016-08-03 12:46:58 +0100882 } else if (handle_kind == kInvokeDirect) {
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100883 if (called_method->IsConstructor()) {
884 // TODO(narayan) : We need to handle the case where the target method is a
885 // constructor here.
886 UNIMPLEMENTED(FATAL) << "Direct invokes for constructors are not implemented yet.";
887 return false;
888 }
889
890 // Nothing special to do in the case where we're not dealing with a
891 // constructor. It's a private method, and we've already access checked at
892 // the point of creating the handle.
893 } else if (handle_kind == kInvokeSuper) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700894 ObjPtr<mirror::Class> declaring_class = called_method->GetDeclaringClass();
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100895
896 // Note that we're not dynamically dispatching on the type of the receiver
897 // here. We use the static type of the "receiver" object that we've
898 // recorded in the method handle's type, which will be the same as the
899 // special caller that was specified at the point of lookup.
Mathieu Chartieref41db72016-10-25 15:08:01 -0700900 ObjPtr<mirror::Class> referrer_class = handle_type->GetPTypes()->Get(0);
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100901 if (!declaring_class->IsInterface()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700902 ObjPtr<mirror::Class> super_class = referrer_class->GetSuperClass();
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100903 uint16_t vtable_index = called_method->GetMethodIndex();
904 DCHECK(super_class != nullptr);
905 DCHECK(super_class->HasVTable());
906 // Note that super_class is a super of referrer_class and called_method
907 // will always be declared by super_class (or one of its super classes).
908 DCHECK_LT(vtable_index, super_class->GetVTableLength());
909 called_method = super_class->GetVTableEntry(vtable_index, kRuntimePointerSize);
910 } else {
911 called_method = referrer_class->FindVirtualMethodForInterfaceSuper(
912 called_method, kRuntimePointerSize);
913 }
914
915 CHECK(called_method != nullptr);
Narayan Kamath9823e782016-08-03 12:46:58 +0100916 }
917
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100918 if (handle_kind == kInvokeTransform) {
Narayan Kamath000e1882016-10-24 17:14:25 +0100919 return DoCallTransform<is_range>(called_method,
920 callsite_type,
921 handle_type,
922 self,
923 shadow_frame,
924 method_handle /* receiver */,
925 result,
926 arg,
927 first_src_reg);
Narayan Kamath208f8572016-08-03 12:46:58 +0100928 } else {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100929 return DoCallPolymorphic<is_range>(called_method,
930 callsite_type,
931 handle_type,
932 self,
933 shadow_frame,
934 result,
935 arg,
Narayan Kamath000e1882016-10-24 17:14:25 +0100936 first_src_reg,
937 handle_kind);
Narayan Kamath9823e782016-08-03 12:46:58 +0100938 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100939 } else {
Orion Hodson3d617ac2016-10-19 14:00:46 +0100940 DCHECK(!is_range);
941 ArtField* field = method_handle->GetTargetField();
942 Primitive::Type field_type = field->GetTypeAsPrimitiveType();;
943
944 if (!is_invoke_exact) {
945 // TODO(oth): conversion plumbing for invoke().
946 UNIMPLEMENTED(FATAL);
947 }
948
949 switch (handle_kind) {
950 case kInstanceGet: {
Narayan Kamath6fcc5e82016-10-31 11:50:54 +0000951 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(first_src_reg);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100952 DoFieldGetForInvokePolymorphic(self, shadow_frame, obj, field, field_type, result);
953 return true;
954 }
955 case kInstancePut: {
Narayan Kamath6fcc5e82016-10-31 11:50:54 +0000956 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(first_src_reg);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100957 return DoFieldPutForInvokePolymorphic(self, shadow_frame, obj, field, field_type, arg[1]);
958 }
959 case kStaticGet: {
960 ObjPtr<mirror::Object> obj = field->GetDeclaringClass();
961 DoFieldGetForInvokePolymorphic(self, shadow_frame, obj, field, field_type, result);
962 return true;
963 }
964 case kStaticPut: {
965 ObjPtr<mirror::Object> obj = field->GetDeclaringClass();
966 return DoFieldPutForInvokePolymorphic(self, shadow_frame, obj, field, field_type, arg[0]);
967 }
968 default:
969 LOG(FATAL) << "Unreachable: " << handle_kind;
970 UNREACHABLE();
971 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100972 }
973}
974
Narayan Kamath208f8572016-08-03 12:46:58 +0100975// Calculate the number of ins for a proxy or native method, where we
976// can't just look at the code item.
977static inline size_t GetInsForProxyOrNativeMethod(ArtMethod* method)
978 REQUIRES_SHARED(Locks::mutator_lock_) {
979 DCHECK(method->IsNative() || method->IsProxyMethod());
980
981 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
982 size_t num_ins = 0;
983 // Separate accounting for the receiver, which isn't a part of the
984 // shorty.
985 if (!method->IsStatic()) {
986 ++num_ins;
987 }
988
989 uint32_t shorty_len = 0;
990 const char* shorty = method->GetShorty(&shorty_len);
991 for (size_t i = 1; i < shorty_len; ++i) {
992 const char c = shorty[i];
993 ++num_ins;
994 if (c == 'J' || c == 'D') {
995 ++num_ins;
996 }
997 }
998
999 return num_ins;
1000}
1001
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001002
1003inline void PerformCall(Thread* self,
1004 const DexFile::CodeItem* code_item,
1005 ArtMethod* caller_method,
1006 const size_t first_dest_reg,
1007 ShadowFrame* callee_frame,
1008 JValue* result) {
1009 if (LIKELY(Runtime::Current()->IsStarted())) {
1010 ArtMethod* target = callee_frame->GetMethod();
1011 if (ClassLinker::ShouldUseInterpreterEntrypoint(
1012 target,
1013 target->GetEntryPointFromQuickCompiledCode())) {
1014 ArtInterpreterToInterpreterBridge(self, code_item, callee_frame, result);
1015 } else {
1016 ArtInterpreterToCompiledCodeBridge(
1017 self, caller_method, code_item, callee_frame, result);
1018 }
1019 } else {
1020 UnstartedRuntime::Invoke(self, code_item, callee_frame, result, first_dest_reg);
1021 }
1022}
1023
1024template <bool is_range>
1025inline void CopyRegisters(ShadowFrame& caller_frame,
1026 ShadowFrame* callee_frame,
1027 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
1028 const size_t first_src_reg,
1029 const size_t first_dest_reg,
1030 const size_t num_regs) {
1031 if (is_range) {
1032 const size_t dest_reg_bound = first_dest_reg + num_regs;
1033 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound;
1034 ++dest_reg, ++src_reg) {
1035 AssignRegister(callee_frame, caller_frame, dest_reg, src_reg);
1036 }
1037 } else {
1038 DCHECK_LE(num_regs, arraysize(arg));
1039
1040 for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) {
1041 AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]);
1042 }
1043 }
1044}
1045
1046// Returns true iff. the callsite type for a polymorphic invoke is transformer
1047// like, i.e that it has a single input argument whose type is
1048// dalvik.system.EmulatedStackFrame.
1049static inline bool IsCallerTransformer(Handle<mirror::MethodType> callsite_type)
1050 REQUIRES_SHARED(Locks::mutator_lock_) {
1051 ObjPtr<mirror::ObjectArray<mirror::Class>> param_types(callsite_type->GetPTypes());
1052 if (param_types->GetLength() == 1) {
1053 ObjPtr<mirror::Class> param(param_types->GetWithoutChecks(0));
1054 return param == WellKnownClasses::ToClass(WellKnownClasses::dalvik_system_EmulatedStackFrame);
1055 }
1056
1057 return false;
1058}
1059
Narayan Kamath208f8572016-08-03 12:46:58 +01001060template <bool is_range>
1061static inline bool DoCallPolymorphic(ArtMethod* called_method,
1062 Handle<mirror::MethodType> callsite_type,
1063 Handle<mirror::MethodType> target_type,
1064 Thread* self,
1065 ShadowFrame& shadow_frame,
1066 JValue* result,
1067 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Narayan Kamath000e1882016-10-24 17:14:25 +01001068 uint32_t first_src_reg,
1069 const MethodHandleKind handle_kind) {
Narayan Kamath208f8572016-08-03 12:46:58 +01001070 // TODO(narayan): Wire in the String.init hacks.
1071
1072 // Compute method information.
1073 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
1074
1075 // Number of registers for the callee's call frame. Note that for non-exact
1076 // invokes, we always derive this information from the callee method. We
1077 // cannot guarantee during verification that the number of registers encoded
1078 // in the invoke is equal to the number of ins for the callee. This is because
1079 // some transformations (such as boxing a long -> Long or wideining an
1080 // int -> long will change that number.
1081 uint16_t num_regs;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001082 size_t num_input_regs;
Narayan Kamath208f8572016-08-03 12:46:58 +01001083 size_t first_dest_reg;
1084 if (LIKELY(code_item != nullptr)) {
1085 num_regs = code_item->registers_size_;
1086 first_dest_reg = num_regs - code_item->ins_size_;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001087 num_input_regs = code_item->ins_size_;
Narayan Kamath208f8572016-08-03 12:46:58 +01001088 // Parameter registers go at the end of the shadow frame.
1089 DCHECK_NE(first_dest_reg, (size_t)-1);
1090 } else {
1091 // No local regs for proxy and native methods.
1092 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001093 num_regs = num_input_regs = GetInsForProxyOrNativeMethod(called_method);
Narayan Kamath208f8572016-08-03 12:46:58 +01001094 first_dest_reg = 0;
1095 }
1096
1097 // Allocate shadow frame on the stack.
1098 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
1099 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
1100 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
1101
Narayan Kamath000e1882016-10-24 17:14:25 +01001102 // Whether this polymorphic invoke was issued by a transformer method.
1103 bool is_caller_transformer = false;
Narayan Kamath208f8572016-08-03 12:46:58 +01001104 // Thread might be suspended during PerformArgumentConversions due to the
1105 // allocations performed during boxing.
1106 {
1107 ScopedStackedShadowFramePusher pusher(
1108 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001109 if (callsite_type->IsExactMatch(target_type.Get())) {
1110 // This is an exact invoke, we can take the fast path of just copying all
1111 // registers without performing any argument conversions.
1112 CopyRegisters<is_range>(shadow_frame,
1113 new_shadow_frame,
1114 arg,
1115 first_src_reg,
1116 first_dest_reg,
1117 num_input_regs);
1118 } else {
1119 // This includes the case where we're entering this invoke-polymorphic
1120 // from a transformer method. In that case, the callsite_type will contain
1121 // a single argument of type dalvik.system.EmulatedStackFrame. In that
1122 // case, we'll have to unmarshal the EmulatedStackFrame into the
1123 // new_shadow_frame and perform argument conversions on it.
1124 if (IsCallerTransformer(callsite_type)) {
Narayan Kamath000e1882016-10-24 17:14:25 +01001125 is_caller_transformer = true;
1126 // The emulated stack frame is the first and only argument when we're coming
1127 // through from a transformer.
1128 ObjPtr<mirror::EmulatedStackFrame> emulated_stack_frame(
1129 reinterpret_cast<mirror::EmulatedStackFrame*>(
1130 shadow_frame.GetVRegReference(first_src_reg)));
1131 if (!emulated_stack_frame->WriteToShadowFrame(self,
1132 target_type,
1133 first_dest_reg,
1134 new_shadow_frame)) {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001135 DCHECK(self->IsExceptionPending());
1136 result->SetL(0);
1137 return false;
1138 }
1139 } else if (!ConvertAndCopyArgumentsFromCallerFrame<is_range>(self,
1140 callsite_type,
1141 target_type,
1142 shadow_frame,
1143 first_src_reg,
1144 first_dest_reg,
1145 arg,
1146 new_shadow_frame)) {
1147 DCHECK(self->IsExceptionPending());
1148 result->SetL(0);
1149 return false;
1150 }
Narayan Kamath208f8572016-08-03 12:46:58 +01001151 }
1152 }
1153
Narayan Kamath000e1882016-10-24 17:14:25 +01001154 // See TODO in DoInvokePolymorphic : We need to perform this dynamic, receiver
1155 // based dispatch right before we perform the actual call, because the
1156 // receiver isn't known very early.
1157 if (handle_kind == kInvokeVirtual || handle_kind == kInvokeInterface) {
1158 ObjPtr<mirror::Object> receiver(new_shadow_frame->GetVRegReference(first_dest_reg));
1159 ObjPtr<mirror::Class> declaring_class(called_method->GetDeclaringClass());
1160 // Verify that _vRegC is an object reference and of the type expected by
1161 // the receiver.
1162 if (!VerifyObjectIsClass(receiver, declaring_class)) {
1163 DCHECK(self->IsExceptionPending());
1164 return false;
1165 }
1166
1167 called_method = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(
1168 called_method, kRuntimePointerSize);
1169 }
1170
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001171 PerformCall(self, code_item, shadow_frame.GetMethod(), first_dest_reg, new_shadow_frame, result);
Narayan Kamath208f8572016-08-03 12:46:58 +01001172
1173 // TODO(narayan): Perform return value conversions.
1174
Narayan Kamath000e1882016-10-24 17:14:25 +01001175 // If the caller of this signature polymorphic method was a transformer,
1176 // we need to copy the result back out to the emulated stack frame.
1177 if (is_caller_transformer && !self->IsExceptionPending()) {
1178 ObjPtr<mirror::EmulatedStackFrame> emulated_stack_frame(
1179 reinterpret_cast<mirror::EmulatedStackFrame*>(
1180 shadow_frame.GetVRegReference(first_src_reg)));
1181
1182 emulated_stack_frame->SetReturnValue(self, *result);
1183 }
1184
Narayan Kamath208f8572016-08-03 12:46:58 +01001185 return !self->IsExceptionPending();
1186}
1187
Narayan Kamath000e1882016-10-24 17:14:25 +01001188template <bool is_range>
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001189static inline bool DoCallTransform(ArtMethod* called_method,
1190 Handle<mirror::MethodType> callsite_type,
Narayan Kamath000e1882016-10-24 17:14:25 +01001191 Handle<mirror::MethodType> callee_type,
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001192 Thread* self,
1193 ShadowFrame& shadow_frame,
1194 Handle<mirror::MethodHandleImpl> receiver,
Narayan Kamath000e1882016-10-24 17:14:25 +01001195 JValue* result,
1196 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
1197 uint32_t first_src_reg) {
1198 // This can be fixed to two, because the method we're calling here
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001199 // (MethodHandle.transformInternal) doesn't have any locals and the signature
1200 // is known :
1201 //
1202 // private MethodHandle.transformInternal(EmulatedStackFrame sf);
1203 //
1204 // This means we need only two vregs :
1205 // - One for the receiver object.
1206 // - One for the only method argument (an EmulatedStackFrame).
1207 static constexpr size_t kNumRegsForTransform = 2;
1208
1209 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
1210 DCHECK(code_item != nullptr);
1211 DCHECK_EQ(kNumRegsForTransform, code_item->registers_size_);
1212 DCHECK_EQ(kNumRegsForTransform, code_item->ins_size_);
1213
1214 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
1215 CREATE_SHADOW_FRAME(kNumRegsForTransform, &shadow_frame, called_method, /* dex pc */ 0);
1216 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
1217
Narayan Kamath000e1882016-10-24 17:14:25 +01001218 StackHandleScope<1> hs(self);
1219 MutableHandle<mirror::EmulatedStackFrame> sf(hs.NewHandle<mirror::EmulatedStackFrame>(nullptr));
1220 if (IsCallerTransformer(callsite_type)) {
1221 // If we're entering this transformer from another transformer, we can pass
1222 // through the handle directly to the callee, instead of having to
1223 // instantiate a new stack frame based on the shadow frame.
1224 sf.Assign(reinterpret_cast<mirror::EmulatedStackFrame*>(
1225 shadow_frame.GetVRegReference(first_src_reg)));
1226 } else {
1227 sf.Assign(mirror::EmulatedStackFrame::CreateFromShadowFrameAndArgs<is_range>(
1228 self,
1229 callsite_type,
1230 callee_type,
1231 shadow_frame,
1232 first_src_reg,
1233 arg));
1234
1235 // Something went wrong while creating the emulated stack frame, we should
1236 // throw the pending exception.
1237 if (sf.Get() == nullptr) {
1238 DCHECK(self->IsExceptionPending());
1239 return false;
1240 }
1241 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001242
1243 new_shadow_frame->SetVRegReference(0, receiver.Get());
Narayan Kamath000e1882016-10-24 17:14:25 +01001244 new_shadow_frame->SetVRegReference(1, sf.Get());
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001245
1246 PerformCall(self,
1247 code_item,
1248 shadow_frame.GetMethod(),
1249 0 /* first dest reg */,
1250 new_shadow_frame,
1251 result);
1252
Narayan Kamath000e1882016-10-24 17:14:25 +01001253 // If the called transformer method we called has returned a value, then we
1254 // need to copy it back to |result|.
1255 if (!self->IsExceptionPending()) {
1256 sf->GetReturnValue(self, result);
1257 }
1258
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001259 return !self->IsExceptionPending();
1260}
1261
Igor Murashkin6918bf12015-09-27 19:19:06 -07001262template <bool is_range,
Narayan Kamath370423d2016-10-03 16:51:22 +01001263 bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001264static inline bool DoCallCommon(ArtMethod* called_method,
1265 Thread* self,
1266 ShadowFrame& shadow_frame,
1267 JValue* result,
1268 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +01001269 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -07001270 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001271 bool string_init = false;
1272 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001273 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
1274 && called_method->IsConstructor())) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001275 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
Jeff Hao848f70a2014-01-15 13:49:50 -08001276 string_init = true;
1277 }
1278
Alex Lightdaf58c82016-03-16 23:00:49 +00001279 // Compute method information.
1280 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
Igor Murashkin158f35c2015-06-10 15:55:30 -07001281
1282 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001283 uint16_t num_regs;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001284 if (LIKELY(code_item != nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001285 num_regs = code_item->registers_size_;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001286 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001287 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -08001288 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Igor Murashkin158f35c2015-06-10 15:55:30 -07001289 num_regs = number_of_inputs;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001290 }
1291
Igor Murashkin158f35c2015-06-10 15:55:30 -07001292 // Hack for String init:
1293 //
1294 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
1295 // invoke-x StringFactory(a, b, c, ...)
1296 // by effectively dropping the first virtual register from the invoke.
1297 //
1298 // (at this point the ArtMethod has already been replaced,
1299 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +00001300 //
1301 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
1302 // to handle the compiler optimization of replacing `this` with null without
1303 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001304 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -07001305 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001306 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 -07001307
Igor Murashkin158f35c2015-06-10 15:55:30 -07001308 // The new StringFactory call is static and has one fewer argument.
Igor Murashkina06b49b2015-06-25 15:18:12 -07001309 if (code_item == nullptr) {
1310 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1311 num_regs--;
1312 } // 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 -07001313 number_of_inputs--;
1314
1315 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -07001316 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001317 arg[i - 1] = arg[i];
1318 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07001319 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001320
1321 // Rewrite the non-var-arg case
1322 vregC++; // Skips the 0th vreg in the range ("this").
1323 }
1324
1325 // Parameter registers go at the end of the shadow frame.
1326 DCHECK_GE(num_regs, number_of_inputs);
1327 size_t first_dest_reg = num_regs - number_of_inputs;
1328 DCHECK_NE(first_dest_reg, (size_t)-1);
1329
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001330 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001331 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -07001332 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -07001333 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -07001334 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001335
Igor Murashkin158f35c2015-06-10 15:55:30 -07001336 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -07001337 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001338 // Slow path.
1339 // We might need to do class loading, which incurs a thread state change to kNative. So
1340 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07001341 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +02001342 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001343 self->EndAssertNoThreadSuspension(old_cause);
1344
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001345 // ArtMethod here is needed to check type information of the call site against the callee.
1346 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
1347 //
1348 // As a special case for proxy methods, which are not dex-backed,
1349 // we have to retrieve type information from the proxy's method
1350 // interface method instead (which is dex backed since proxies are never interfaces).
Andreas Gampe542451c2016-07-26 09:02:02 -07001351 ArtMethod* method =
1352 new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001353
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001354 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001355 // to get the exact type of each reference argument.
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001356 const DexFile::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001357 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001358 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001359
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001360 // Handle receiver apart since it's not part of the shorty.
1361 size_t dest_reg = first_dest_reg;
1362 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001363
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001364 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001365 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001366 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
1367 ++dest_reg;
1368 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -07001369 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001370 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001371
1372 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001373 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001374 // Skip the 0th 'shorty' type since it represents the return type.
1375 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001376 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
1377 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001378 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001379 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001380 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001381 if (do_assignability_check && o != nullptr) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001382 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001383 const uint32_t type_idx = params->GetTypeItem(shorty_pos).type_idx_;
1384 ObjPtr<mirror::Class> arg_type = method->GetDexCacheResolvedType(type_idx,
1385 pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001386 if (arg_type == nullptr) {
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001387 StackHandleScope<1> hs(self);
1388 // Preserve o since it is used below and GetClassFromTypeIndex may cause thread
1389 // suspension.
1390 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&o);
1391 arg_type = method->GetClassFromTypeIndex(type_idx, true /* resolve */, pointer_size);
1392 if (arg_type == nullptr) {
1393 CHECK(self->IsExceptionPending());
1394 return false;
1395 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001396 }
1397 if (!o->VerifierInstanceOf(arg_type)) {
1398 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -07001399 std::string temp1, temp2;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001400 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001401 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -08001402 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -07001403 o->GetClass()->GetDescriptor(&temp1),
1404 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001405 return false;
1406 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001407 }
Mathieu Chartieref41db72016-10-25 15:08:01 -07001408 new_shadow_frame->SetVRegReference(dest_reg, o.Ptr());
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001409 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -07001410 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001411 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001412 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001413 uint64_t wide_value =
1414 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
1415 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001416 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001417 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001418 ++dest_reg;
1419 ++arg_offset;
1420 break;
1421 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001422 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001423 default:
1424 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
1425 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001426 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001427 }
1428 } else {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001429 if (is_range) {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001430 DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001431 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001432
1433 CopyRegisters<is_range>(shadow_frame,
1434 new_shadow_frame,
1435 arg,
1436 vregC,
1437 first_dest_reg,
1438 number_of_inputs);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001439 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001440 }
1441
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001442 PerformCall(self, code_item, shadow_frame.GetMethod(), first_dest_reg, new_shadow_frame, result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001443
1444 if (string_init && !self->IsExceptionPending()) {
Mingyao Yangffedec52016-05-19 10:48:40 -07001445 SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001446 }
1447
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001448 return !self->IsExceptionPending();
1449}
1450
Igor Murashkin158f35c2015-06-10 15:55:30 -07001451template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001452bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
1453 const Instruction* inst, uint16_t inst_data, JValue* result) {
1454 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001455 const uint16_t number_of_inputs =
1456 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001457
1458 // TODO: find a cleaner way to separate non-range and range information without duplicating
1459 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -07001460 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001461 uint32_t vregC = 0;
1462 if (is_range) {
1463 vregC = inst->VRegC_3rc();
1464 } else {
1465 vregC = inst->VRegC_35c();
1466 inst->GetVarArgs(arg, inst_data);
1467 }
1468
1469 return DoCallCommon<is_range, do_assignability_check>(
1470 called_method, self, shadow_frame,
1471 result, number_of_inputs, arg, vregC);
1472}
1473
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001474template <bool is_range, bool do_access_check, bool transaction_active>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001475bool DoFilledNewArray(const Instruction* inst,
1476 const ShadowFrame& shadow_frame,
1477 Thread* self,
1478 JValue* result) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001479 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
1480 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
1481 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
1482 if (!is_range) {
1483 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
1484 CHECK_LE(length, 5);
1485 }
1486 if (UNLIKELY(length < 0)) {
1487 ThrowNegativeArraySizeException(length);
1488 return false;
1489 }
1490 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartieref41db72016-10-25 15:08:01 -07001491 ObjPtr<mirror::Class> array_class = ResolveVerifyAndClinit(type_idx,
1492 shadow_frame.GetMethod(),
1493 self,
1494 false,
1495 do_access_check);
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001496 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001497 DCHECK(self->IsExceptionPending());
1498 return false;
1499 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001500 CHECK(array_class->IsArrayClass());
Mathieu Chartieref41db72016-10-25 15:08:01 -07001501 ObjPtr<mirror::Class> component_class = array_class->GetComponentType();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001502 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
1503 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
1504 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001505 ThrowRuntimeException("Bad filled array request for type %s",
David Sehr709b0702016-10-13 09:12:37 -07001506 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001507 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001508 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -08001509 "Found type %s; filled-new-array not implemented for anything but 'int'",
David Sehr709b0702016-10-13 09:12:37 -07001510 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001511 }
1512 return false;
1513 }
Mathieu Chartieref41db72016-10-25 15:08:01 -07001514 ObjPtr<mirror::Object> new_array = mirror::Array::Alloc<true>(
1515 self,
1516 array_class,
1517 length,
1518 array_class->GetComponentSizeShift(),
1519 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001520 if (UNLIKELY(new_array == nullptr)) {
1521 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001522 return false;
1523 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001524 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
1525 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001526 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +01001527 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001528 } else {
Ian Rogers29a26482014-05-02 15:27:29 -07001529 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +01001530 }
Sebastien Hertzabff6432014-01-27 18:01:39 +01001531 for (int32_t i = 0; i < length; ++i) {
1532 size_t src_reg = is_range ? vregC + i : arg[i];
1533 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001534 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
1535 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +01001536 } else {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001537 new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<transaction_active>(
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001538 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001539 }
1540 }
1541
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001542 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001543 return true;
1544}
1545
Mathieu Chartieref41db72016-10-25 15:08:01 -07001546// TODO: Use ObjPtr here.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001547template<typename T>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001548static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array,
1549 int32_t count)
1550 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001551 Runtime* runtime = Runtime::Current();
1552 for (int32_t i = 0; i < count; ++i) {
1553 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
1554 }
1555}
1556
Mathieu Chartieref41db72016-10-25 15:08:01 -07001557void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001558 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001559 DCHECK(Runtime::Current()->IsActiveTransaction());
1560 DCHECK(array != nullptr);
1561 DCHECK_LE(count, array->GetLength());
1562 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
1563 switch (primitive_component_type) {
1564 case Primitive::kPrimBoolean:
1565 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
1566 break;
1567 case Primitive::kPrimByte:
1568 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
1569 break;
1570 case Primitive::kPrimChar:
1571 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
1572 break;
1573 case Primitive::kPrimShort:
1574 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
1575 break;
1576 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001577 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
1578 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001579 case Primitive::kPrimFloat:
1580 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
1581 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001582 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001583 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
1584 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001585 case Primitive::kPrimDouble:
1586 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
1587 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001588 default:
1589 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
1590 << " in fill-array-data";
1591 break;
1592 }
1593}
1594
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001595// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +02001596#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001597 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001598 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
1599 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +02001600 const Instruction* inst, uint16_t inst_data, \
1601 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001602EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
1603EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
1604EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
1605EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
1606#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001607
Narayan Kamath9823e782016-08-03 12:46:58 +01001608// Explicit DoInvokePolymorphic template function declarations.
1609#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range, _do_assignability_check) \
1610 template REQUIRES_SHARED(Locks::mutator_lock_) \
1611 bool DoInvokePolymorphic<_is_range, _do_assignability_check>( \
1612 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1613 uint16_t inst_data, JValue* result)
1614
1615EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, false);
1616EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, true);
1617EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, false);
1618EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, true);
1619#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1620
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001621// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001622#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001623 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001624 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
1625 const ShadowFrame& shadow_frame, \
1626 Thread* self, JValue* result)
1627#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
1628 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
1629 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
1630 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
1631 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
1632EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
1633EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
1634#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001635#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
1636
1637} // namespace interpreter
1638} // namespace art