blob: 981cc1b9986985ef774c7a9d7ab1ee6867725b10 [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
Orion Hodson0c14d8b2016-11-03 12:01:24 +0000777inline static ObjPtr<mirror::Class> GetAndInitializeDeclaringClass(Thread* self, ArtField* field)
778 REQUIRES_SHARED(Locks::mutator_lock_) {
779 // Method handle invocations on static fields should ensure class is
780 // initialized. This usually happens when an instance is constructed
781 // or class members referenced, but this is not guaranteed when
782 // looking up method handles.
783 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
784 if (UNLIKELY(!klass->IsInitialized())) {
785 StackHandleScope<1> hs(self);
786 HandleWrapperObjPtr<mirror::Class> h(hs.NewHandleWrapper(&klass));
787 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h, true, true)) {
788 DCHECK(self->IsExceptionPending());
789 return nullptr;
790 }
791 }
792 return klass;
793}
794
Narayan Kamath9823e782016-08-03 12:46:58 +0100795template<bool is_range, bool do_access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -0700796inline bool DoInvokePolymorphic(Thread* self,
797 ShadowFrame& shadow_frame,
798 const Instruction* inst,
799 uint16_t inst_data,
800 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100801 // Invoke-polymorphic instructions always take a receiver. i.e, they are never static.
802 const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc();
Orion Hodson3d617ac2016-10-19 14:00:46 +0100803 const int invoke_method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc();
Narayan Kamath9823e782016-08-03 12:46:58 +0100804
Orion Hodson3d617ac2016-10-19 14:00:46 +0100805 // Determine if this invocation is MethodHandle.invoke() or
806 // MethodHandle.invokeExact().
807 bool is_invoke_exact = IsInvokeExact(shadow_frame.GetMethod()->GetDeclaringClass()->GetDexFile(),
808 invoke_method_idx);
809
810 // The invoke_method_idx here is the name of the signature polymorphic method that
Narayan Kamath9823e782016-08-03 12:46:58 +0100811 // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact)
812 // and not the method that we'll dispatch to in the end.
813 //
814 // TODO(narayan) We'll have to check in the verifier that this is in fact a
815 // signature polymorphic method so that we disallow calls via invoke-polymorphic
816 // to non sig-poly methods. This would also have the side effect of verifying
817 // that vRegC really is a reference type.
Narayan Kamath208f8572016-08-03 12:46:58 +0100818 StackHandleScope<6> hs(self);
819 Handle<mirror::MethodHandleImpl> method_handle(hs.NewHandle(
Mathieu Chartieref41db72016-10-25 15:08:01 -0700820 ObjPtr<mirror::MethodHandleImpl>::DownCast(
821 MakeObjPtr(shadow_frame.GetVRegReference(vRegC)))));
Narayan Kamath208f8572016-08-03 12:46:58 +0100822 if (UNLIKELY(method_handle.Get() == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100823 // Note that the invoke type is kVirtual here because a call to a signature
824 // polymorphic method is shaped like a virtual call at the bytecode level.
Orion Hodson3d617ac2016-10-19 14:00:46 +0100825 ThrowNullPointerExceptionForMethodAccess(invoke_method_idx, InvokeType::kVirtual);
Narayan Kamath9823e782016-08-03 12:46:58 +0100826 result->SetJ(0);
827 return false;
828 }
829
830 // The vRegH value gives the index of the proto_id associated with this
831 // signature polymorphic callsite.
832 const uint32_t callsite_proto_id = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
833
834 // Call through to the classlinker and ask it to resolve the static type associated
835 // with the callsite. This information is stored in the dex cache so it's
836 // guaranteed to be fast after the first resolution.
Narayan Kamath9823e782016-08-03 12:46:58 +0100837 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Narayan Kamath208f8572016-08-03 12:46:58 +0100838 Handle<mirror::Class> caller_class(hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass()));
839 Handle<mirror::MethodType> callsite_type(hs.NewHandle(class_linker->ResolveMethodType(
Narayan Kamath9823e782016-08-03 12:46:58 +0100840 caller_class->GetDexFile(), callsite_proto_id,
841 hs.NewHandle<mirror::DexCache>(caller_class->GetDexCache()),
Narayan Kamath208f8572016-08-03 12:46:58 +0100842 hs.NewHandle<mirror::ClassLoader>(caller_class->GetClassLoader()))));
Narayan Kamath9823e782016-08-03 12:46:58 +0100843
844 // This implies we couldn't resolve one or more types in this method handle.
Narayan Kamath208f8572016-08-03 12:46:58 +0100845 if (UNLIKELY(callsite_type.Get() == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100846 CHECK(self->IsExceptionPending());
847 result->SetJ(0);
848 return false;
849 }
850
Narayan Kamath9823e782016-08-03 12:46:58 +0100851 const MethodHandleKind handle_kind = method_handle->GetHandleKind();
Narayan Kamath208f8572016-08-03 12:46:58 +0100852 Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType()));
Narayan Kamath208f8572016-08-03 12:46:58 +0100853 CHECK(handle_type.Get() != nullptr);
Narayan Kamath0a8485e2016-11-02 18:47:11 +0000854 if (is_invoke_exact) {
855 // We need to check the nominal type of the handle in addition to the
856 // real type. The "nominal" type is present when MethodHandle.asType is
857 // called any handle, and results in the declared type of the handle
858 // changing.
859 ObjPtr<mirror::MethodType> nominal_type(method_handle->GetNominalType());
860 ObjPtr<mirror::MethodType> check_type(nullptr);
861 if (LIKELY(nominal_type.Ptr() == nullptr)) {
862 check_type.Assign(handle_type.Get());
863 } else {
864 check_type.Assign(nominal_type.Ptr());
865 }
866
867 if (UNLIKELY(!callsite_type->IsExactMatch(check_type.Ptr()))) {
868 ThrowWrongMethodTypeException(check_type.Ptr(), callsite_type.Get());
869 return false;
870 }
Orion Hodson3d617ac2016-10-19 14:00:46 +0100871 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100872
Narayan Kamath9823e782016-08-03 12:46:58 +0100873 uint32_t arg[Instruction::kMaxVarArgRegs] = {};
Narayan Kamath000e1882016-10-24 17:14:25 +0100874 uint32_t first_src_reg = 0;
Narayan Kamath9823e782016-08-03 12:46:58 +0100875 if (is_range) {
Narayan Kamath000e1882016-10-24 17:14:25 +0100876 first_src_reg = (inst->VRegC_4rcc() + 1);
Narayan Kamath9823e782016-08-03 12:46:58 +0100877 } else {
878 inst->GetVarArgs(arg, inst_data);
879 arg[0] = arg[1];
880 arg[1] = arg[2];
881 arg[2] = arg[3];
882 arg[3] = arg[4];
883 arg[4] = 0;
Narayan Kamath000e1882016-10-24 17:14:25 +0100884 first_src_reg = arg[0];
Narayan Kamath9823e782016-08-03 12:46:58 +0100885 }
886
887 if (IsInvoke(handle_kind)) {
Orion Hodson3d617ac2016-10-19 14:00:46 +0100888 // Get the method we're actually invoking along with the kind of
889 // invoke that is desired. We don't need to perform access checks at this
890 // point because they would have been performed on our behalf at the point
891 // of creation of the method handle.
892 ArtMethod* called_method = method_handle->GetTargetMethod();
893 CHECK(called_method != nullptr);
894
Narayan Kamath9823e782016-08-03 12:46:58 +0100895 if (handle_kind == kInvokeVirtual || handle_kind == kInvokeInterface) {
Narayan Kamath000e1882016-10-24 17:14:25 +0100896 // TODO: Unfortunately, we have to postpone dynamic receiver based checks
897 // because the receiver might be cast or might come from an emulated stack
898 // frame, which means that it is unknown at this point. We perform these
899 // checks inside DoCallPolymorphic right before we do the actualy invoke.
Narayan Kamath9823e782016-08-03 12:46:58 +0100900 } else if (handle_kind == kInvokeDirect) {
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100901 if (called_method->IsConstructor()) {
902 // TODO(narayan) : We need to handle the case where the target method is a
903 // constructor here.
904 UNIMPLEMENTED(FATAL) << "Direct invokes for constructors are not implemented yet.";
905 return false;
906 }
907
908 // Nothing special to do in the case where we're not dealing with a
909 // constructor. It's a private method, and we've already access checked at
910 // the point of creating the handle.
911 } else if (handle_kind == kInvokeSuper) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700912 ObjPtr<mirror::Class> declaring_class = called_method->GetDeclaringClass();
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100913
914 // Note that we're not dynamically dispatching on the type of the receiver
915 // here. We use the static type of the "receiver" object that we've
916 // recorded in the method handle's type, which will be the same as the
917 // special caller that was specified at the point of lookup.
Mathieu Chartieref41db72016-10-25 15:08:01 -0700918 ObjPtr<mirror::Class> referrer_class = handle_type->GetPTypes()->Get(0);
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100919 if (!declaring_class->IsInterface()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700920 ObjPtr<mirror::Class> super_class = referrer_class->GetSuperClass();
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100921 uint16_t vtable_index = called_method->GetMethodIndex();
922 DCHECK(super_class != nullptr);
923 DCHECK(super_class->HasVTable());
924 // Note that super_class is a super of referrer_class and called_method
925 // will always be declared by super_class (or one of its super classes).
926 DCHECK_LT(vtable_index, super_class->GetVTableLength());
927 called_method = super_class->GetVTableEntry(vtable_index, kRuntimePointerSize);
928 } else {
929 called_method = referrer_class->FindVirtualMethodForInterfaceSuper(
930 called_method, kRuntimePointerSize);
931 }
932
933 CHECK(called_method != nullptr);
Narayan Kamath9823e782016-08-03 12:46:58 +0100934 }
935
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100936 if (handle_kind == kInvokeTransform) {
Narayan Kamath000e1882016-10-24 17:14:25 +0100937 return DoCallTransform<is_range>(called_method,
938 callsite_type,
939 handle_type,
940 self,
941 shadow_frame,
942 method_handle /* receiver */,
943 result,
944 arg,
945 first_src_reg);
Narayan Kamath208f8572016-08-03 12:46:58 +0100946 } else {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100947 return DoCallPolymorphic<is_range>(called_method,
948 callsite_type,
949 handle_type,
950 self,
951 shadow_frame,
952 result,
953 arg,
Narayan Kamath000e1882016-10-24 17:14:25 +0100954 first_src_reg,
955 handle_kind);
Narayan Kamath9823e782016-08-03 12:46:58 +0100956 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100957 } else {
Orion Hodson3d617ac2016-10-19 14:00:46 +0100958 DCHECK(!is_range);
959 ArtField* field = method_handle->GetTargetField();
960 Primitive::Type field_type = field->GetTypeAsPrimitiveType();;
961
962 if (!is_invoke_exact) {
963 // TODO(oth): conversion plumbing for invoke().
964 UNIMPLEMENTED(FATAL);
965 }
966
967 switch (handle_kind) {
968 case kInstanceGet: {
Narayan Kamath6fcc5e82016-10-31 11:50:54 +0000969 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(first_src_reg);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100970 DoFieldGetForInvokePolymorphic(self, shadow_frame, obj, field, field_type, result);
971 return true;
972 }
973 case kInstancePut: {
Narayan Kamath6fcc5e82016-10-31 11:50:54 +0000974 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(first_src_reg);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100975 return DoFieldPutForInvokePolymorphic(self, shadow_frame, obj, field, field_type, arg[1]);
976 }
977 case kStaticGet: {
Orion Hodson0c14d8b2016-11-03 12:01:24 +0000978 ObjPtr<mirror::Object> obj = GetAndInitializeDeclaringClass(self, field);
979 if (obj == nullptr) {
980 DCHECK(self->IsExceptionPending());
981 return false;
982 }
Orion Hodson3d617ac2016-10-19 14:00:46 +0100983 DoFieldGetForInvokePolymorphic(self, shadow_frame, obj, field, field_type, result);
984 return true;
985 }
986 case kStaticPut: {
Orion Hodson0c14d8b2016-11-03 12:01:24 +0000987 ObjPtr<mirror::Object> obj = GetAndInitializeDeclaringClass(self, field);
988 if (obj == nullptr) {
989 DCHECK(self->IsExceptionPending());
990 return false;
991 }
Orion Hodson3d617ac2016-10-19 14:00:46 +0100992 return DoFieldPutForInvokePolymorphic(self, shadow_frame, obj, field, field_type, arg[0]);
993 }
994 default:
995 LOG(FATAL) << "Unreachable: " << handle_kind;
996 UNREACHABLE();
997 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100998 }
999}
1000
Narayan Kamath208f8572016-08-03 12:46:58 +01001001// Calculate the number of ins for a proxy or native method, where we
1002// can't just look at the code item.
1003static inline size_t GetInsForProxyOrNativeMethod(ArtMethod* method)
1004 REQUIRES_SHARED(Locks::mutator_lock_) {
1005 DCHECK(method->IsNative() || method->IsProxyMethod());
1006
1007 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
1008 size_t num_ins = 0;
1009 // Separate accounting for the receiver, which isn't a part of the
1010 // shorty.
1011 if (!method->IsStatic()) {
1012 ++num_ins;
1013 }
1014
1015 uint32_t shorty_len = 0;
1016 const char* shorty = method->GetShorty(&shorty_len);
1017 for (size_t i = 1; i < shorty_len; ++i) {
1018 const char c = shorty[i];
1019 ++num_ins;
1020 if (c == 'J' || c == 'D') {
1021 ++num_ins;
1022 }
1023 }
1024
1025 return num_ins;
1026}
1027
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001028
1029inline void PerformCall(Thread* self,
1030 const DexFile::CodeItem* code_item,
1031 ArtMethod* caller_method,
1032 const size_t first_dest_reg,
1033 ShadowFrame* callee_frame,
1034 JValue* result) {
1035 if (LIKELY(Runtime::Current()->IsStarted())) {
1036 ArtMethod* target = callee_frame->GetMethod();
1037 if (ClassLinker::ShouldUseInterpreterEntrypoint(
1038 target,
1039 target->GetEntryPointFromQuickCompiledCode())) {
1040 ArtInterpreterToInterpreterBridge(self, code_item, callee_frame, result);
1041 } else {
1042 ArtInterpreterToCompiledCodeBridge(
1043 self, caller_method, code_item, callee_frame, result);
1044 }
1045 } else {
1046 UnstartedRuntime::Invoke(self, code_item, callee_frame, result, first_dest_reg);
1047 }
1048}
1049
1050template <bool is_range>
1051inline void CopyRegisters(ShadowFrame& caller_frame,
1052 ShadowFrame* callee_frame,
1053 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
1054 const size_t first_src_reg,
1055 const size_t first_dest_reg,
1056 const size_t num_regs) {
1057 if (is_range) {
1058 const size_t dest_reg_bound = first_dest_reg + num_regs;
1059 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound;
1060 ++dest_reg, ++src_reg) {
1061 AssignRegister(callee_frame, caller_frame, dest_reg, src_reg);
1062 }
1063 } else {
1064 DCHECK_LE(num_regs, arraysize(arg));
1065
1066 for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) {
1067 AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]);
1068 }
1069 }
1070}
1071
1072// Returns true iff. the callsite type for a polymorphic invoke is transformer
1073// like, i.e that it has a single input argument whose type is
1074// dalvik.system.EmulatedStackFrame.
1075static inline bool IsCallerTransformer(Handle<mirror::MethodType> callsite_type)
1076 REQUIRES_SHARED(Locks::mutator_lock_) {
1077 ObjPtr<mirror::ObjectArray<mirror::Class>> param_types(callsite_type->GetPTypes());
1078 if (param_types->GetLength() == 1) {
1079 ObjPtr<mirror::Class> param(param_types->GetWithoutChecks(0));
1080 return param == WellKnownClasses::ToClass(WellKnownClasses::dalvik_system_EmulatedStackFrame);
1081 }
1082
1083 return false;
1084}
1085
Narayan Kamath208f8572016-08-03 12:46:58 +01001086template <bool is_range>
1087static inline bool DoCallPolymorphic(ArtMethod* called_method,
1088 Handle<mirror::MethodType> callsite_type,
1089 Handle<mirror::MethodType> target_type,
1090 Thread* self,
1091 ShadowFrame& shadow_frame,
1092 JValue* result,
1093 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Narayan Kamath000e1882016-10-24 17:14:25 +01001094 uint32_t first_src_reg,
1095 const MethodHandleKind handle_kind) {
Narayan Kamath208f8572016-08-03 12:46:58 +01001096 // TODO(narayan): Wire in the String.init hacks.
1097
1098 // Compute method information.
1099 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
1100
1101 // Number of registers for the callee's call frame. Note that for non-exact
1102 // invokes, we always derive this information from the callee method. We
1103 // cannot guarantee during verification that the number of registers encoded
1104 // in the invoke is equal to the number of ins for the callee. This is because
1105 // some transformations (such as boxing a long -> Long or wideining an
1106 // int -> long will change that number.
1107 uint16_t num_regs;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001108 size_t num_input_regs;
Narayan Kamath208f8572016-08-03 12:46:58 +01001109 size_t first_dest_reg;
1110 if (LIKELY(code_item != nullptr)) {
1111 num_regs = code_item->registers_size_;
1112 first_dest_reg = num_regs - code_item->ins_size_;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001113 num_input_regs = code_item->ins_size_;
Narayan Kamath208f8572016-08-03 12:46:58 +01001114 // Parameter registers go at the end of the shadow frame.
1115 DCHECK_NE(first_dest_reg, (size_t)-1);
1116 } else {
1117 // No local regs for proxy and native methods.
1118 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001119 num_regs = num_input_regs = GetInsForProxyOrNativeMethod(called_method);
Narayan Kamath208f8572016-08-03 12:46:58 +01001120 first_dest_reg = 0;
1121 }
1122
1123 // Allocate shadow frame on the stack.
1124 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
1125 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
1126 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
1127
Narayan Kamath000e1882016-10-24 17:14:25 +01001128 // Whether this polymorphic invoke was issued by a transformer method.
1129 bool is_caller_transformer = false;
Narayan Kamath208f8572016-08-03 12:46:58 +01001130 // Thread might be suspended during PerformArgumentConversions due to the
1131 // allocations performed during boxing.
1132 {
1133 ScopedStackedShadowFramePusher pusher(
1134 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001135 if (callsite_type->IsExactMatch(target_type.Get())) {
1136 // This is an exact invoke, we can take the fast path of just copying all
1137 // registers without performing any argument conversions.
1138 CopyRegisters<is_range>(shadow_frame,
1139 new_shadow_frame,
1140 arg,
1141 first_src_reg,
1142 first_dest_reg,
1143 num_input_regs);
1144 } else {
1145 // This includes the case where we're entering this invoke-polymorphic
1146 // from a transformer method. In that case, the callsite_type will contain
1147 // a single argument of type dalvik.system.EmulatedStackFrame. In that
1148 // case, we'll have to unmarshal the EmulatedStackFrame into the
1149 // new_shadow_frame and perform argument conversions on it.
1150 if (IsCallerTransformer(callsite_type)) {
Narayan Kamath000e1882016-10-24 17:14:25 +01001151 is_caller_transformer = true;
1152 // The emulated stack frame is the first and only argument when we're coming
1153 // through from a transformer.
1154 ObjPtr<mirror::EmulatedStackFrame> emulated_stack_frame(
1155 reinterpret_cast<mirror::EmulatedStackFrame*>(
1156 shadow_frame.GetVRegReference(first_src_reg)));
1157 if (!emulated_stack_frame->WriteToShadowFrame(self,
1158 target_type,
1159 first_dest_reg,
1160 new_shadow_frame)) {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001161 DCHECK(self->IsExceptionPending());
1162 result->SetL(0);
1163 return false;
1164 }
1165 } else if (!ConvertAndCopyArgumentsFromCallerFrame<is_range>(self,
1166 callsite_type,
1167 target_type,
1168 shadow_frame,
1169 first_src_reg,
1170 first_dest_reg,
1171 arg,
1172 new_shadow_frame)) {
1173 DCHECK(self->IsExceptionPending());
1174 result->SetL(0);
1175 return false;
1176 }
Narayan Kamath208f8572016-08-03 12:46:58 +01001177 }
1178 }
1179
Narayan Kamath000e1882016-10-24 17:14:25 +01001180 // See TODO in DoInvokePolymorphic : We need to perform this dynamic, receiver
1181 // based dispatch right before we perform the actual call, because the
1182 // receiver isn't known very early.
1183 if (handle_kind == kInvokeVirtual || handle_kind == kInvokeInterface) {
1184 ObjPtr<mirror::Object> receiver(new_shadow_frame->GetVRegReference(first_dest_reg));
1185 ObjPtr<mirror::Class> declaring_class(called_method->GetDeclaringClass());
1186 // Verify that _vRegC is an object reference and of the type expected by
1187 // the receiver.
1188 if (!VerifyObjectIsClass(receiver, declaring_class)) {
1189 DCHECK(self->IsExceptionPending());
1190 return false;
1191 }
1192
1193 called_method = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(
1194 called_method, kRuntimePointerSize);
1195 }
1196
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001197 PerformCall(self, code_item, shadow_frame.GetMethod(), first_dest_reg, new_shadow_frame, result);
Narayan Kamath208f8572016-08-03 12:46:58 +01001198
1199 // TODO(narayan): Perform return value conversions.
1200
Narayan Kamath000e1882016-10-24 17:14:25 +01001201 // If the caller of this signature polymorphic method was a transformer,
1202 // we need to copy the result back out to the emulated stack frame.
1203 if (is_caller_transformer && !self->IsExceptionPending()) {
1204 ObjPtr<mirror::EmulatedStackFrame> emulated_stack_frame(
1205 reinterpret_cast<mirror::EmulatedStackFrame*>(
1206 shadow_frame.GetVRegReference(first_src_reg)));
1207
1208 emulated_stack_frame->SetReturnValue(self, *result);
1209 }
1210
Narayan Kamath208f8572016-08-03 12:46:58 +01001211 return !self->IsExceptionPending();
1212}
1213
Narayan Kamath000e1882016-10-24 17:14:25 +01001214template <bool is_range>
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001215static inline bool DoCallTransform(ArtMethod* called_method,
1216 Handle<mirror::MethodType> callsite_type,
Narayan Kamath000e1882016-10-24 17:14:25 +01001217 Handle<mirror::MethodType> callee_type,
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001218 Thread* self,
1219 ShadowFrame& shadow_frame,
1220 Handle<mirror::MethodHandleImpl> receiver,
Narayan Kamath000e1882016-10-24 17:14:25 +01001221 JValue* result,
1222 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
1223 uint32_t first_src_reg) {
1224 // This can be fixed to two, because the method we're calling here
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001225 // (MethodHandle.transformInternal) doesn't have any locals and the signature
1226 // is known :
1227 //
1228 // private MethodHandle.transformInternal(EmulatedStackFrame sf);
1229 //
1230 // This means we need only two vregs :
1231 // - One for the receiver object.
1232 // - One for the only method argument (an EmulatedStackFrame).
1233 static constexpr size_t kNumRegsForTransform = 2;
1234
1235 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
1236 DCHECK(code_item != nullptr);
1237 DCHECK_EQ(kNumRegsForTransform, code_item->registers_size_);
1238 DCHECK_EQ(kNumRegsForTransform, code_item->ins_size_);
1239
1240 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
1241 CREATE_SHADOW_FRAME(kNumRegsForTransform, &shadow_frame, called_method, /* dex pc */ 0);
1242 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
1243
Narayan Kamath000e1882016-10-24 17:14:25 +01001244 StackHandleScope<1> hs(self);
1245 MutableHandle<mirror::EmulatedStackFrame> sf(hs.NewHandle<mirror::EmulatedStackFrame>(nullptr));
1246 if (IsCallerTransformer(callsite_type)) {
1247 // If we're entering this transformer from another transformer, we can pass
1248 // through the handle directly to the callee, instead of having to
1249 // instantiate a new stack frame based on the shadow frame.
1250 sf.Assign(reinterpret_cast<mirror::EmulatedStackFrame*>(
1251 shadow_frame.GetVRegReference(first_src_reg)));
1252 } else {
1253 sf.Assign(mirror::EmulatedStackFrame::CreateFromShadowFrameAndArgs<is_range>(
1254 self,
1255 callsite_type,
1256 callee_type,
1257 shadow_frame,
1258 first_src_reg,
1259 arg));
1260
1261 // Something went wrong while creating the emulated stack frame, we should
1262 // throw the pending exception.
1263 if (sf.Get() == nullptr) {
1264 DCHECK(self->IsExceptionPending());
1265 return false;
1266 }
1267 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001268
1269 new_shadow_frame->SetVRegReference(0, receiver.Get());
Narayan Kamath000e1882016-10-24 17:14:25 +01001270 new_shadow_frame->SetVRegReference(1, sf.Get());
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001271
1272 PerformCall(self,
1273 code_item,
1274 shadow_frame.GetMethod(),
1275 0 /* first dest reg */,
1276 new_shadow_frame,
1277 result);
1278
Narayan Kamath000e1882016-10-24 17:14:25 +01001279 // If the called transformer method we called has returned a value, then we
1280 // need to copy it back to |result|.
1281 if (!self->IsExceptionPending()) {
1282 sf->GetReturnValue(self, result);
1283 }
1284
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001285 return !self->IsExceptionPending();
1286}
1287
Igor Murashkin6918bf12015-09-27 19:19:06 -07001288template <bool is_range,
Narayan Kamath370423d2016-10-03 16:51:22 +01001289 bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001290static inline bool DoCallCommon(ArtMethod* called_method,
1291 Thread* self,
1292 ShadowFrame& shadow_frame,
1293 JValue* result,
1294 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +01001295 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -07001296 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001297 bool string_init = false;
1298 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001299 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
1300 && called_method->IsConstructor())) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001301 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
Jeff Hao848f70a2014-01-15 13:49:50 -08001302 string_init = true;
1303 }
1304
Alex Lightdaf58c82016-03-16 23:00:49 +00001305 // Compute method information.
1306 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
Igor Murashkin158f35c2015-06-10 15:55:30 -07001307
1308 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001309 uint16_t num_regs;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001310 if (LIKELY(code_item != nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001311 num_regs = code_item->registers_size_;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001312 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001313 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -08001314 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Igor Murashkin158f35c2015-06-10 15:55:30 -07001315 num_regs = number_of_inputs;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001316 }
1317
Igor Murashkin158f35c2015-06-10 15:55:30 -07001318 // Hack for String init:
1319 //
1320 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
1321 // invoke-x StringFactory(a, b, c, ...)
1322 // by effectively dropping the first virtual register from the invoke.
1323 //
1324 // (at this point the ArtMethod has already been replaced,
1325 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +00001326 //
1327 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
1328 // to handle the compiler optimization of replacing `this` with null without
1329 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001330 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -07001331 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001332 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 -07001333
Igor Murashkin158f35c2015-06-10 15:55:30 -07001334 // The new StringFactory call is static and has one fewer argument.
Igor Murashkina06b49b2015-06-25 15:18:12 -07001335 if (code_item == nullptr) {
1336 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1337 num_regs--;
1338 } // 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 -07001339 number_of_inputs--;
1340
1341 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -07001342 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001343 arg[i - 1] = arg[i];
1344 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07001345 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001346
1347 // Rewrite the non-var-arg case
1348 vregC++; // Skips the 0th vreg in the range ("this").
1349 }
1350
1351 // Parameter registers go at the end of the shadow frame.
1352 DCHECK_GE(num_regs, number_of_inputs);
1353 size_t first_dest_reg = num_regs - number_of_inputs;
1354 DCHECK_NE(first_dest_reg, (size_t)-1);
1355
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001356 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001357 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -07001358 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -07001359 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -07001360 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001361
Igor Murashkin158f35c2015-06-10 15:55:30 -07001362 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -07001363 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001364 // Slow path.
1365 // We might need to do class loading, which incurs a thread state change to kNative. So
1366 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07001367 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +02001368 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001369 self->EndAssertNoThreadSuspension(old_cause);
1370
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001371 // ArtMethod here is needed to check type information of the call site against the callee.
1372 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
1373 //
1374 // As a special case for proxy methods, which are not dex-backed,
1375 // we have to retrieve type information from the proxy's method
1376 // interface method instead (which is dex backed since proxies are never interfaces).
Andreas Gampe542451c2016-07-26 09:02:02 -07001377 ArtMethod* method =
1378 new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001379
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001380 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001381 // to get the exact type of each reference argument.
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001382 const DexFile::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001383 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001384 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001385
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001386 // Handle receiver apart since it's not part of the shorty.
1387 size_t dest_reg = first_dest_reg;
1388 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001389
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001390 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001391 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001392 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
1393 ++dest_reg;
1394 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -07001395 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001396 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001397
1398 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001399 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001400 // Skip the 0th 'shorty' type since it represents the return type.
1401 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001402 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
1403 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001404 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001405 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001406 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001407 if (do_assignability_check && o != nullptr) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001408 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001409 const uint32_t type_idx = params->GetTypeItem(shorty_pos).type_idx_;
1410 ObjPtr<mirror::Class> arg_type = method->GetDexCacheResolvedType(type_idx,
1411 pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001412 if (arg_type == nullptr) {
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001413 StackHandleScope<1> hs(self);
1414 // Preserve o since it is used below and GetClassFromTypeIndex may cause thread
1415 // suspension.
1416 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&o);
1417 arg_type = method->GetClassFromTypeIndex(type_idx, true /* resolve */, pointer_size);
1418 if (arg_type == nullptr) {
1419 CHECK(self->IsExceptionPending());
1420 return false;
1421 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001422 }
1423 if (!o->VerifierInstanceOf(arg_type)) {
1424 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -07001425 std::string temp1, temp2;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001426 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001427 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -08001428 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -07001429 o->GetClass()->GetDescriptor(&temp1),
1430 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001431 return false;
1432 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001433 }
Mathieu Chartieref41db72016-10-25 15:08:01 -07001434 new_shadow_frame->SetVRegReference(dest_reg, o.Ptr());
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001435 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -07001436 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001437 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001438 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001439 uint64_t wide_value =
1440 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
1441 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001442 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001443 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001444 ++dest_reg;
1445 ++arg_offset;
1446 break;
1447 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001448 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001449 default:
1450 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
1451 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001452 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001453 }
1454 } else {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001455 if (is_range) {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001456 DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001457 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001458
1459 CopyRegisters<is_range>(shadow_frame,
1460 new_shadow_frame,
1461 arg,
1462 vregC,
1463 first_dest_reg,
1464 number_of_inputs);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001465 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001466 }
1467
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001468 PerformCall(self, code_item, shadow_frame.GetMethod(), first_dest_reg, new_shadow_frame, result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001469
1470 if (string_init && !self->IsExceptionPending()) {
Mingyao Yangffedec52016-05-19 10:48:40 -07001471 SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001472 }
1473
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001474 return !self->IsExceptionPending();
1475}
1476
Igor Murashkin158f35c2015-06-10 15:55:30 -07001477template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001478bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
1479 const Instruction* inst, uint16_t inst_data, JValue* result) {
1480 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001481 const uint16_t number_of_inputs =
1482 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001483
1484 // TODO: find a cleaner way to separate non-range and range information without duplicating
1485 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -07001486 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001487 uint32_t vregC = 0;
1488 if (is_range) {
1489 vregC = inst->VRegC_3rc();
1490 } else {
1491 vregC = inst->VRegC_35c();
1492 inst->GetVarArgs(arg, inst_data);
1493 }
1494
1495 return DoCallCommon<is_range, do_assignability_check>(
1496 called_method, self, shadow_frame,
1497 result, number_of_inputs, arg, vregC);
1498}
1499
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001500template <bool is_range, bool do_access_check, bool transaction_active>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001501bool DoFilledNewArray(const Instruction* inst,
1502 const ShadowFrame& shadow_frame,
1503 Thread* self,
1504 JValue* result) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001505 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
1506 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
1507 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
1508 if (!is_range) {
1509 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
1510 CHECK_LE(length, 5);
1511 }
1512 if (UNLIKELY(length < 0)) {
1513 ThrowNegativeArraySizeException(length);
1514 return false;
1515 }
1516 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartieref41db72016-10-25 15:08:01 -07001517 ObjPtr<mirror::Class> array_class = ResolveVerifyAndClinit(type_idx,
1518 shadow_frame.GetMethod(),
1519 self,
1520 false,
1521 do_access_check);
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001522 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001523 DCHECK(self->IsExceptionPending());
1524 return false;
1525 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001526 CHECK(array_class->IsArrayClass());
Mathieu Chartieref41db72016-10-25 15:08:01 -07001527 ObjPtr<mirror::Class> component_class = array_class->GetComponentType();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001528 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
1529 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
1530 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001531 ThrowRuntimeException("Bad filled array request for type %s",
David Sehr709b0702016-10-13 09:12:37 -07001532 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001533 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001534 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -08001535 "Found type %s; filled-new-array not implemented for anything but 'int'",
David Sehr709b0702016-10-13 09:12:37 -07001536 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001537 }
1538 return false;
1539 }
Mathieu Chartieref41db72016-10-25 15:08:01 -07001540 ObjPtr<mirror::Object> new_array = mirror::Array::Alloc<true>(
1541 self,
1542 array_class,
1543 length,
1544 array_class->GetComponentSizeShift(),
1545 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001546 if (UNLIKELY(new_array == nullptr)) {
1547 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001548 return false;
1549 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001550 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
1551 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001552 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +01001553 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001554 } else {
Ian Rogers29a26482014-05-02 15:27:29 -07001555 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +01001556 }
Sebastien Hertzabff6432014-01-27 18:01:39 +01001557 for (int32_t i = 0; i < length; ++i) {
1558 size_t src_reg = is_range ? vregC + i : arg[i];
1559 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001560 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
1561 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +01001562 } else {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001563 new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<transaction_active>(
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001564 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001565 }
1566 }
1567
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001568 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001569 return true;
1570}
1571
Mathieu Chartieref41db72016-10-25 15:08:01 -07001572// TODO: Use ObjPtr here.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001573template<typename T>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001574static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array,
1575 int32_t count)
1576 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001577 Runtime* runtime = Runtime::Current();
1578 for (int32_t i = 0; i < count; ++i) {
1579 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
1580 }
1581}
1582
Mathieu Chartieref41db72016-10-25 15:08:01 -07001583void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001584 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001585 DCHECK(Runtime::Current()->IsActiveTransaction());
1586 DCHECK(array != nullptr);
1587 DCHECK_LE(count, array->GetLength());
1588 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
1589 switch (primitive_component_type) {
1590 case Primitive::kPrimBoolean:
1591 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
1592 break;
1593 case Primitive::kPrimByte:
1594 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
1595 break;
1596 case Primitive::kPrimChar:
1597 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
1598 break;
1599 case Primitive::kPrimShort:
1600 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
1601 break;
1602 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001603 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
1604 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001605 case Primitive::kPrimFloat:
1606 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
1607 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001608 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001609 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
1610 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001611 case Primitive::kPrimDouble:
1612 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
1613 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001614 default:
1615 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
1616 << " in fill-array-data";
1617 break;
1618 }
1619}
1620
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001621// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +02001622#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001623 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001624 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
1625 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +02001626 const Instruction* inst, uint16_t inst_data, \
1627 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001628EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
1629EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
1630EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
1631EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
1632#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001633
Narayan Kamath9823e782016-08-03 12:46:58 +01001634// Explicit DoInvokePolymorphic template function declarations.
1635#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range, _do_assignability_check) \
1636 template REQUIRES_SHARED(Locks::mutator_lock_) \
1637 bool DoInvokePolymorphic<_is_range, _do_assignability_check>( \
1638 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1639 uint16_t inst_data, JValue* result)
1640
1641EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, false);
1642EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, true);
1643EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, false);
1644EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, true);
1645#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1646
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001647// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001648#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001649 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001650 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
1651 const ShadowFrame& shadow_frame, \
1652 Thread* self, JValue* result)
1653#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
1654 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
1655 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
1656 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
1657 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
1658EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
1659EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
1660#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001661#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
1662
1663} // namespace interpreter
1664} // namespace art