blob: d098ee239550125b8b979c210674e9ef1c670aac [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,
Orion Hodsonba28f9f2016-10-26 10:56:25 +010051 JValue* result)
52 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson3d617ac2016-10-19 14:00:46 +010053 field->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
54
55 // Report this field access to instrumentation if needed.
56 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
57 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
58 StackHandleScope<1> hs(self);
59 // Wrap in handle wrapper in case the listener does thread suspension.
60 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
61 ObjPtr<mirror::Object> this_object;
62 if (!field->IsStatic()) {
63 this_object = obj;
64 }
65 instrumentation->FieldReadEvent(self,
66 this_object.Ptr(),
67 shadow_frame.GetMethod(),
68 shadow_frame.GetDexPC(),
69 field);
70 }
71
72 switch (field_type) {
73 case Primitive::kPrimBoolean:
74 result->SetZ(field->GetBoolean(obj));
75 break;
76 case Primitive::kPrimByte:
77 result->SetB(field->GetByte(obj));
78 break;
79 case Primitive::kPrimChar:
80 result->SetC(field->GetChar(obj));
81 break;
82 case Primitive::kPrimShort:
83 result->SetS(field->GetShort(obj));
84 break;
85 case Primitive::kPrimInt:
86 result->SetI(field->GetInt(obj));
87 break;
88 case Primitive::kPrimLong:
89 result->SetJ(field->GetLong(obj));
90 break;
91 case Primitive::kPrimNot:
92 result->SetL(field->GetObject(obj));
93 break;
94 default:
95 LOG(FATAL) << "Unreachable: " << field_type;
96 UNREACHABLE();
97 }
98}
99
Ian Rogers54874942014-06-10 16:31:03 -0700100template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
101bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
102 uint16_t inst_data) {
103 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
104 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100105 ArtField* f =
106 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
107 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -0700108 if (UNLIKELY(f == nullptr)) {
109 CHECK(self->IsExceptionPending());
110 return false;
111 }
Mathieu Chartieref41db72016-10-25 15:08:01 -0700112 ObjPtr<mirror::Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -0700113 if (is_static) {
114 obj = f->GetDeclaringClass();
115 } else {
116 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
117 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000118 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers54874942014-06-10 16:31:03 -0700119 return false;
120 }
121 }
Orion Hodson3d617ac2016-10-19 14:00:46 +0100122
123 JValue result;
124 DoFieldGetCommon<field_type>(self, shadow_frame, obj, f, &result);
Ian Rogers54874942014-06-10 16:31:03 -0700125 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
126 switch (field_type) {
127 case Primitive::kPrimBoolean:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100128 shadow_frame.SetVReg(vregA, result.GetZ());
Ian Rogers54874942014-06-10 16:31:03 -0700129 break;
130 case Primitive::kPrimByte:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100131 shadow_frame.SetVReg(vregA, result.GetB());
Ian Rogers54874942014-06-10 16:31:03 -0700132 break;
133 case Primitive::kPrimChar:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100134 shadow_frame.SetVReg(vregA, result.GetC());
Ian Rogers54874942014-06-10 16:31:03 -0700135 break;
136 case Primitive::kPrimShort:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100137 shadow_frame.SetVReg(vregA, result.GetS());
Ian Rogers54874942014-06-10 16:31:03 -0700138 break;
139 case Primitive::kPrimInt:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100140 shadow_frame.SetVReg(vregA, result.GetI());
Ian Rogers54874942014-06-10 16:31:03 -0700141 break;
142 case Primitive::kPrimLong:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100143 shadow_frame.SetVRegLong(vregA, result.GetJ());
Ian Rogers54874942014-06-10 16:31:03 -0700144 break;
145 case Primitive::kPrimNot:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100146 shadow_frame.SetVRegReference(vregA, result.GetL());
Ian Rogers54874942014-06-10 16:31:03 -0700147 break;
148 default:
149 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700150 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700151 }
152 return true;
153}
154
155// Explicitly instantiate all DoFieldGet functions.
156#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
157 template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \
158 ShadowFrame& shadow_frame, \
159 const Instruction* inst, \
160 uint16_t inst_data)
161
162#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
163 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \
164 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true);
165
166// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700167EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
168EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
169EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
170EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
171EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
172EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
173EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700174
175// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700176EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
177EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
178EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
179EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
180EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
181EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
182EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700183
184#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
185#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
186
Orion Hodson3d617ac2016-10-19 14:00:46 +0100187// Helper for getters in invoke-polymorphic.
188inline static void DoFieldGetForInvokePolymorphic(Thread* self,
189 const ShadowFrame& shadow_frame,
190 ObjPtr<mirror::Object>& obj,
191 ArtField* field,
192 Primitive::Type field_type,
193 JValue* result)
194 REQUIRES_SHARED(Locks::mutator_lock_) {
195 switch (field_type) {
196 case Primitive::kPrimBoolean:
197 DoFieldGetCommon<Primitive::kPrimBoolean>(self, shadow_frame, obj, field, result);
198 break;
199 case Primitive::kPrimByte:
200 DoFieldGetCommon<Primitive::kPrimByte>(self, shadow_frame, obj, field, result);
201 break;
202 case Primitive::kPrimChar:
203 DoFieldGetCommon<Primitive::kPrimChar>(self, shadow_frame, obj, field, result);
204 break;
205 case Primitive::kPrimShort:
206 DoFieldGetCommon<Primitive::kPrimShort>(self, shadow_frame, obj, field, result);
207 break;
208 case Primitive::kPrimInt:
209 DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result);
210 break;
211 case Primitive::kPrimLong:
212 DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result);
213 break;
214 case Primitive::kPrimFloat:
215 DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result);
216 break;
217 case Primitive::kPrimDouble:
218 DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result);
219 break;
220 case Primitive::kPrimNot:
221 DoFieldGetCommon<Primitive::kPrimNot>(self, shadow_frame, obj, field, result);
222 break;
223 case Primitive::kPrimVoid:
224 LOG(FATAL) << "Unreachable: " << field_type;
225 UNREACHABLE();
226 }
227}
228
Ian Rogers54874942014-06-10 16:31:03 -0700229// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
230// Returns true on success, otherwise throws an exception and returns false.
231template<Primitive::Type field_type>
232bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700233 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700234 if (UNLIKELY(obj == nullptr)) {
235 // We lost the reference to the field index so we cannot get a more
236 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000237 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700238 return false;
239 }
240 MemberOffset field_offset(inst->VRegC_22c());
241 // Report this field access to instrumentation if needed. Since we only have the offset of
242 // the field from the base of the object, we need to look for it first.
243 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
244 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
245 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
246 field_offset.Uint32Value());
247 DCHECK(f != nullptr);
248 DCHECK(!f->IsStatic());
Mathieu Chartiera3147732016-10-26 22:57:02 -0700249 StackHandleScope<1> hs(Thread::Current());
250 // Save obj in case the instrumentation event has thread suspension.
251 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700252 instrumentation->FieldReadEvent(Thread::Current(),
253 obj.Ptr(),
254 shadow_frame.GetMethod(),
255 shadow_frame.GetDexPC(),
256 f);
Ian Rogers54874942014-06-10 16:31:03 -0700257 }
258 // Note: iget-x-quick instructions are only for non-volatile fields.
259 const uint32_t vregA = inst->VRegA_22c(inst_data);
260 switch (field_type) {
261 case Primitive::kPrimInt:
262 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
263 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800264 case Primitive::kPrimBoolean:
265 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
266 break;
267 case Primitive::kPrimByte:
268 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
269 break;
270 case Primitive::kPrimChar:
271 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
272 break;
273 case Primitive::kPrimShort:
274 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
275 break;
Ian Rogers54874942014-06-10 16:31:03 -0700276 case Primitive::kPrimLong:
277 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
278 break;
279 case Primitive::kPrimNot:
280 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
281 break;
282 default:
283 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700284 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700285 }
286 return true;
287}
288
289// Explicitly instantiate all DoIGetQuick functions.
290#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
291 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
292 uint16_t inst_data)
293
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800294EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
295EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
296EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
297EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
298EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
299EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
300EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700301#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
302
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100303static JValue GetFieldValue(const ShadowFrame& shadow_frame,
304 Primitive::Type field_type,
305 uint32_t vreg)
306 REQUIRES_SHARED(Locks::mutator_lock_) {
307 JValue field_value;
308 switch (field_type) {
309 case Primitive::kPrimBoolean:
310 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
311 break;
312 case Primitive::kPrimByte:
313 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
314 break;
315 case Primitive::kPrimChar:
316 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
317 break;
318 case Primitive::kPrimShort:
319 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
320 break;
321 case Primitive::kPrimInt:
322 case Primitive::kPrimFloat:
323 field_value.SetI(shadow_frame.GetVReg(vreg));
324 break;
325 case Primitive::kPrimLong:
326 case Primitive::kPrimDouble:
327 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
328 break;
329 case Primitive::kPrimNot:
330 field_value.SetL(shadow_frame.GetVRegReference(vreg));
331 break;
332 case Primitive::kPrimVoid:
333 LOG(FATAL) << "Unreachable: " << field_type;
334 UNREACHABLE();
335 }
336 return field_value;
337}
338
Ian Rogers54874942014-06-10 16:31:03 -0700339template<Primitive::Type field_type>
340static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700341 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700342 JValue field_value;
343 switch (field_type) {
344 case Primitive::kPrimBoolean:
345 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
346 break;
347 case Primitive::kPrimByte:
348 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
349 break;
350 case Primitive::kPrimChar:
351 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
352 break;
353 case Primitive::kPrimShort:
354 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
355 break;
356 case Primitive::kPrimInt:
357 field_value.SetI(shadow_frame.GetVReg(vreg));
358 break;
359 case Primitive::kPrimLong:
360 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
361 break;
362 case Primitive::kPrimNot:
363 field_value.SetL(shadow_frame.GetVRegReference(vreg));
364 break;
365 default:
366 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700367 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700368 }
369 return field_value;
370}
371
Orion Hodson3d617ac2016-10-19 14:00:46 +0100372template<Primitive::Type field_type, bool do_assignability_check, bool transaction_active>
373static inline bool DoFieldPutCommon(Thread* self,
374 const ShadowFrame& shadow_frame,
375 ObjPtr<mirror::Object>& obj,
376 ArtField* f,
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100377 const JValue& value)
378 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +0200379 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100380
Ian Rogers54874942014-06-10 16:31:03 -0700381 // Report this field access to instrumentation if needed. Since we only have the offset of
382 // the field from the base of the object, we need to look for it first.
383 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
384 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
Mathieu Chartier0715c0b2016-10-03 22:49:46 -0700385 StackHandleScope<1> hs(self);
386 // Wrap in handle wrapper in case the listener does thread suspension.
387 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
Mathieu Chartieref41db72016-10-25 15:08:01 -0700388 ObjPtr<mirror::Object> this_object = f->IsStatic() ? nullptr : obj;
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700389 instrumentation->FieldWriteEvent(self, this_object.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -0700390 shadow_frame.GetMethod(),
391 shadow_frame.GetDexPC(),
392 f,
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100393 value);
Ian Rogers54874942014-06-10 16:31:03 -0700394 }
Orion Hodson3d617ac2016-10-19 14:00:46 +0100395
Ian Rogers54874942014-06-10 16:31:03 -0700396 switch (field_type) {
397 case Primitive::kPrimBoolean:
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100398 f->SetBoolean<transaction_active>(obj, value.GetZ());
Ian Rogers54874942014-06-10 16:31:03 -0700399 break;
400 case Primitive::kPrimByte:
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100401 f->SetByte<transaction_active>(obj, value.GetB());
Ian Rogers54874942014-06-10 16:31:03 -0700402 break;
403 case Primitive::kPrimChar:
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100404 f->SetChar<transaction_active>(obj, value.GetC());
Ian Rogers54874942014-06-10 16:31:03 -0700405 break;
406 case Primitive::kPrimShort:
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100407 f->SetShort<transaction_active>(obj, value.GetS());
Ian Rogers54874942014-06-10 16:31:03 -0700408 break;
409 case Primitive::kPrimInt:
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100410 f->SetInt<transaction_active>(obj, value.GetI());
Ian Rogers54874942014-06-10 16:31:03 -0700411 break;
412 case Primitive::kPrimLong:
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100413 f->SetLong<transaction_active>(obj, value.GetJ());
Ian Rogers54874942014-06-10 16:31:03 -0700414 break;
415 case Primitive::kPrimNot: {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100416 ObjPtr<mirror::Object> reg = value.GetL();
Ian Rogers54874942014-06-10 16:31:03 -0700417 if (do_assignability_check && reg != nullptr) {
418 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
419 // object in the destructor.
Mathieu Chartieref41db72016-10-25 15:08:01 -0700420 ObjPtr<mirror::Class> field_class;
Ian Rogers54874942014-06-10 16:31:03 -0700421 {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700422 StackHandleScope<2> hs(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700423 HandleWrapperObjPtr<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
Mathieu Chartier3398c782016-09-30 10:27:43 -0700424 HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700425 field_class = f->GetType<true>();
Ian Rogers54874942014-06-10 16:31:03 -0700426 }
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700427 if (!reg->VerifierInstanceOf(field_class.Ptr())) {
Ian Rogers54874942014-06-10 16:31:03 -0700428 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700429 std::string temp1, temp2, temp3;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000430 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Ian Rogers54874942014-06-10 16:31:03 -0700431 "Put '%s' that is not instance of field '%s' in '%s'",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700432 reg->GetClass()->GetDescriptor(&temp1),
433 field_class->GetDescriptor(&temp2),
434 f->GetDeclaringClass()->GetDescriptor(&temp3));
Ian Rogers54874942014-06-10 16:31:03 -0700435 return false;
436 }
437 }
438 f->SetObj<transaction_active>(obj, reg);
439 break;
440 }
441 default:
442 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700443 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700444 }
445 return true;
446}
447
Orion Hodson3d617ac2016-10-19 14:00:46 +0100448template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
449 bool transaction_active>
450bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
451 uint16_t inst_data) {
452 const bool do_assignability_check = do_access_check;
453 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
454 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
455 ArtField* f =
456 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
457 Primitive::ComponentSize(field_type));
458 if (UNLIKELY(f == nullptr)) {
459 CHECK(self->IsExceptionPending());
460 return false;
461 }
462 ObjPtr<mirror::Object> obj;
463 if (is_static) {
464 obj = f->GetDeclaringClass();
465 } else {
466 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
467 if (UNLIKELY(obj == nullptr)) {
468 ThrowNullPointerExceptionForFieldAccess(f, false);
469 return false;
470 }
471 }
472
473 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100474 JValue value = GetFieldValue<field_type>(shadow_frame, vregA);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100475 return DoFieldPutCommon<field_type, do_assignability_check, transaction_active>(self,
476 shadow_frame,
477 obj,
478 f,
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100479 value);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100480}
481
Ian Rogers54874942014-06-10 16:31:03 -0700482// Explicitly instantiate all DoFieldPut functions.
483#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
484 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
485 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
486
487#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
488 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
489 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
490 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
491 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
492
493// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700494EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
495EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
496EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
497EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
498EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
499EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
500EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700501
502// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700503EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
504EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
505EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
506EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
507EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
508EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
509EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700510
511#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
512#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
513
Orion Hodson3d617ac2016-10-19 14:00:46 +0100514// Helper for setters in invoke-polymorphic.
515bool DoFieldPutForInvokePolymorphic(Thread* self,
516 ShadowFrame& shadow_frame,
517 ObjPtr<mirror::Object>& obj,
518 ArtField* field,
519 Primitive::Type field_type,
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100520 const JValue& value)
521 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson3d617ac2016-10-19 14:00:46 +0100522 static const bool kDoCheckAssignability = false;
523 static const bool kTransaction = false;
524 switch (field_type) {
525 case Primitive::kPrimBoolean:
526 return DoFieldPutCommon<Primitive::kPrimBoolean, kDoCheckAssignability, kTransaction>(
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100527 self, shadow_frame, obj, field, value);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100528 case Primitive::kPrimByte:
529 return DoFieldPutCommon<Primitive::kPrimByte, kDoCheckAssignability, kTransaction>(
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100530 self, shadow_frame, obj, field, value);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100531 case Primitive::kPrimChar:
532 return DoFieldPutCommon<Primitive::kPrimChar, kDoCheckAssignability, kTransaction>(
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100533 self, shadow_frame, obj, field, value);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100534 case Primitive::kPrimShort:
535 return DoFieldPutCommon<Primitive::kPrimShort, kDoCheckAssignability, kTransaction>(
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100536 self, shadow_frame, obj, field, value);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100537 case Primitive::kPrimInt:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100538 case Primitive::kPrimFloat:
539 return DoFieldPutCommon<Primitive::kPrimInt, kDoCheckAssignability, kTransaction>(
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100540 self, shadow_frame, obj, field, value);
541 case Primitive::kPrimLong:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100542 case Primitive::kPrimDouble:
543 return DoFieldPutCommon<Primitive::kPrimLong, kDoCheckAssignability, kTransaction>(
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100544 self, shadow_frame, obj, field, value);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100545 case Primitive::kPrimNot:
546 return DoFieldPutCommon<Primitive::kPrimNot, kDoCheckAssignability, kTransaction>(
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100547 self, shadow_frame, obj, field, value);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100548 case Primitive::kPrimVoid:
549 LOG(FATAL) << "Unreachable: " << field_type;
550 UNREACHABLE();
551 }
552}
553
Ian Rogers54874942014-06-10 16:31:03 -0700554template<Primitive::Type field_type, bool transaction_active>
555bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700556 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700557 if (UNLIKELY(obj == nullptr)) {
558 // We lost the reference to the field index so we cannot get a more
559 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000560 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700561 return false;
562 }
563 MemberOffset field_offset(inst->VRegC_22c());
564 const uint32_t vregA = inst->VRegA_22c(inst_data);
565 // Report this field modification to instrumentation if needed. Since we only have the offset of
566 // the field from the base of the object, we need to look for it first.
567 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
568 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
569 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
570 field_offset.Uint32Value());
571 DCHECK(f != nullptr);
572 DCHECK(!f->IsStatic());
573 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700574 StackHandleScope<1> hs(Thread::Current());
575 // Save obj in case the instrumentation event has thread suspension.
576 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700577 instrumentation->FieldWriteEvent(Thread::Current(),
578 obj.Ptr(),
579 shadow_frame.GetMethod(),
580 shadow_frame.GetDexPC(),
581 f,
582 field_value);
Ian Rogers54874942014-06-10 16:31:03 -0700583 }
584 // Note: iput-x-quick instructions are only for non-volatile fields.
585 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700586 case Primitive::kPrimBoolean:
587 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
588 break;
589 case Primitive::kPrimByte:
590 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
591 break;
592 case Primitive::kPrimChar:
593 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
594 break;
595 case Primitive::kPrimShort:
596 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
597 break;
Ian Rogers54874942014-06-10 16:31:03 -0700598 case Primitive::kPrimInt:
599 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
600 break;
601 case Primitive::kPrimLong:
602 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
603 break;
604 case Primitive::kPrimNot:
605 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
606 break;
607 default:
608 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700609 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700610 }
611 return true;
612}
613
614// Explicitly instantiate all DoIPutQuick functions.
615#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
616 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
617 const Instruction* inst, \
618 uint16_t inst_data)
619
620#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
621 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
622 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
623
Andreas Gampec8ccf682014-09-29 20:07:43 -0700624EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
625EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
626EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
627EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
628EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
629EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
630EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700631#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
632#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
633
Sebastien Hertz520633b2015-09-08 17:03:36 +0200634// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700635uint32_t FindNextInstructionFollowingException(
636 Thread* self, ShadowFrame& shadow_frame, uint32_t dex_pc,
637 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700638 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700639 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000640 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Sebastien Hertz520633b2015-09-08 17:03:36 +0200641 if (instrumentation != nullptr && instrumentation->HasExceptionCaughtListeners()
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000642 && self->IsExceptionThrownByCurrentMethod(exception.Get())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000643 instrumentation->ExceptionCaughtEvent(self, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200644 }
Ian Rogers54874942014-06-10 16:31:03 -0700645 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700646 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
647 hs.NewHandle(exception->GetClass()), dex_pc, &clear_exception);
Sebastien Hertz520633b2015-09-08 17:03:36 +0200648 if (found_dex_pc == DexFile::kDexNoIndex && instrumentation != nullptr) {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000649 // Exception is not caught by the current method. We will unwind to the
650 // caller. Notify any instrumentation listener.
Sebastien Hertz9f102032014-05-23 08:59:42 +0200651 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
Ian Rogers54874942014-06-10 16:31:03 -0700652 shadow_frame.GetMethod(), dex_pc);
653 } else {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000654 // Exception is caught in the current method. We will jump to the found_dex_pc.
Ian Rogers54874942014-06-10 16:31:03 -0700655 if (clear_exception) {
656 self->ClearException();
657 }
658 }
659 return found_dex_pc;
660}
661
Ian Rogerse94652f2014-12-02 11:13:19 -0800662void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
663 LOG(FATAL) << "Unexpected instruction: "
664 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
665 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700666}
667
Sebastien Hertz45b15972015-04-03 16:07:05 +0200668void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700669 va_list args;
670 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200671 AbortTransactionV(self, fmt, args);
672 va_end(args);
673}
674
675void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
676 CHECK(Runtime::Current()->IsActiveTransaction());
677 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100678 std::string abort_msg;
679 StringAppendV(&abort_msg, fmt, args);
680 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200681 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700682}
683
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100684// START DECLARATIONS :
685//
686// These additional declarations are required because clang complains
687// about ALWAYS_INLINE (-Werror, -Wgcc-compat) in definitions.
688//
689
Narayan Kamath9823e782016-08-03 12:46:58 +0100690template <bool is_range, bool do_assignability_check>
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000691static ALWAYS_INLINE bool DoCallCommon(ArtMethod* called_method,
692 Thread* self,
693 ShadowFrame& shadow_frame,
694 JValue* result,
695 uint16_t number_of_inputs,
696 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
697 uint32_t vregC) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100698
699template <bool is_range>
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000700static ALWAYS_INLINE bool DoCallPolymorphic(ArtMethod* called_method,
701 Handle<mirror::MethodType> callsite_type,
702 Handle<mirror::MethodType> target_type,
703 Thread* self,
704 ShadowFrame& shadow_frame,
705 JValue* result,
706 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
707 uint32_t vregC,
708 const MethodHandleKind handle_kind)
709 REQUIRES_SHARED(Locks::mutator_lock_);
710
711template <bool is_range>
712static ALWAYS_INLINE bool DoCallTransform(ArtMethod* called_method,
713 Handle<mirror::MethodType> callsite_type,
714 Handle<mirror::MethodType> callee_type,
715 Thread* self,
716 ShadowFrame& shadow_frame,
717 Handle<mirror::MethodHandleImpl> receiver,
718 JValue* result,
719 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
720 uint32_t vregC) REQUIRES_SHARED(Locks::mutator_lock_);
721
722ALWAYS_INLINE void PerformCall(Thread* self,
723 const DexFile::CodeItem* code_item,
724 ArtMethod* caller_method,
725 const size_t first_dest_reg,
726 ShadowFrame* callee_frame,
727 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_);
728
729template <bool is_range>
730ALWAYS_INLINE void CopyRegisters(ShadowFrame& caller_frame,
731 ShadowFrame* callee_frame,
732 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
733 const size_t first_src_reg,
734 const size_t first_dest_reg,
735 const size_t num_regs) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100736
737// END DECLARATIONS.
738
Siva Chandra05d24152016-01-05 17:43:17 -0800739void ArtInterpreterToCompiledCodeBridge(Thread* self,
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100740 ArtMethod* caller,
Siva Chandra05d24152016-01-05 17:43:17 -0800741 const DexFile::CodeItem* code_item,
742 ShadowFrame* shadow_frame,
743 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700744 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700745 ArtMethod* method = shadow_frame->GetMethod();
746 // Ensure static methods are initialized.
747 if (method->IsStatic()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700748 ObjPtr<mirror::Class> declaringClass = method->GetDeclaringClass();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700749 if (UNLIKELY(!declaringClass->IsInitialized())) {
750 self->PushShadowFrame(shadow_frame);
751 StackHandleScope<1> hs(self);
752 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
753 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
754 true))) {
755 self->PopShadowFrame();
756 DCHECK(self->IsExceptionPending());
757 return;
758 }
759 self->PopShadowFrame();
760 CHECK(h_class->IsInitializing());
761 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
762 method = shadow_frame->GetMethod();
763 }
764 }
765 uint16_t arg_offset = (code_item == nullptr)
766 ? 0
767 : code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100768 jit::Jit* jit = Runtime::Current()->GetJit();
769 if (jit != nullptr && caller != nullptr) {
770 jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
771 }
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700772 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
773 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
Andreas Gampe542451c2016-07-26 09:02:02 -0700774 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700775}
776
Mingyao Yangffedec52016-05-19 10:48:40 -0700777void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
778 uint16_t this_obj_vreg,
779 JValue result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700780 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700781 ObjPtr<mirror::Object> existing = shadow_frame->GetVRegReference(this_obj_vreg);
Mingyao Yangffedec52016-05-19 10:48:40 -0700782 if (existing == nullptr) {
783 // If it's null, we come from compiled code that was deoptimized. Nothing to do,
784 // as the compiler verified there was no alias.
785 // Set the new string result of the StringFactory.
786 shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
787 return;
788 }
789 // Set the string init result into all aliases.
790 for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
791 if (shadow_frame->GetVRegReference(i) == existing) {
792 DCHECK_EQ(shadow_frame->GetVRegReference(i),
793 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
794 shadow_frame->SetVRegReference(i, result.GetL());
795 DCHECK_EQ(shadow_frame->GetVRegReference(i),
796 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
797 }
798 }
799}
800
Orion Hodson3d617ac2016-10-19 14:00:46 +0100801inline static bool IsInvokeExact(const DexFile& dex_file, int invoke_method_idx) {
802 // This check uses string comparison as it needs less code and data
803 // to do than fetching the associated ArtMethod from the DexCache
804 // and checking against ArtMethods in the well known classes. The
805 // verifier needs to perform a more rigorous check.
806 const char* method_name = dex_file.GetMethodName(dex_file.GetMethodId(invoke_method_idx));
807 bool is_invoke_exact = (0 == strcmp(method_name, "invokeExact"));
808 DCHECK(is_invoke_exact || (0 == strcmp(method_name, "invoke")));
809 return is_invoke_exact;
810}
811
Orion Hodson0c14d8b2016-11-03 12:01:24 +0000812inline static ObjPtr<mirror::Class> GetAndInitializeDeclaringClass(Thread* self, ArtField* field)
813 REQUIRES_SHARED(Locks::mutator_lock_) {
814 // Method handle invocations on static fields should ensure class is
815 // initialized. This usually happens when an instance is constructed
816 // or class members referenced, but this is not guaranteed when
817 // looking up method handles.
818 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
819 if (UNLIKELY(!klass->IsInitialized())) {
820 StackHandleScope<1> hs(self);
821 HandleWrapperObjPtr<mirror::Class> h(hs.NewHandleWrapper(&klass));
822 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h, true, true)) {
823 DCHECK(self->IsExceptionPending());
824 return nullptr;
825 }
826 }
827 return klass;
828}
829
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000830// Returns true iff. the callsite type for a polymorphic invoke is transformer
831// like, i.e that it has a single input argument whose type is
832// dalvik.system.EmulatedStackFrame.
833static inline bool IsCallerTransformer(Handle<mirror::MethodType> callsite_type)
834 REQUIRES_SHARED(Locks::mutator_lock_) {
835 ObjPtr<mirror::ObjectArray<mirror::Class>> param_types(callsite_type->GetPTypes());
836 if (param_types->GetLength() == 1) {
837 ObjPtr<mirror::Class> param(param_types->GetWithoutChecks(0));
838 return param == WellKnownClasses::ToClass(WellKnownClasses::dalvik_system_EmulatedStackFrame);
839 }
840
841 return false;
842}
843
Narayan Kamath9823e782016-08-03 12:46:58 +0100844template<bool is_range, bool do_access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -0700845inline bool DoInvokePolymorphic(Thread* self,
846 ShadowFrame& shadow_frame,
847 const Instruction* inst,
848 uint16_t inst_data,
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100849 JValue* result)
850 REQUIRES_SHARED(Locks::mutator_lock_) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100851 // Invoke-polymorphic instructions always take a receiver. i.e, they are never static.
852 const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc();
Orion Hodson3d617ac2016-10-19 14:00:46 +0100853 const int invoke_method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc();
Narayan Kamath9823e782016-08-03 12:46:58 +0100854
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000855 // Initialize |result| to 0 as this is the default return value for
856 // polymorphic invocations of method handle types with void return
857 // and provides sane return result in error cases.
858 result->SetJ(0);
859
Orion Hodson3d617ac2016-10-19 14:00:46 +0100860 // Determine if this invocation is MethodHandle.invoke() or
861 // MethodHandle.invokeExact().
862 bool is_invoke_exact = IsInvokeExact(shadow_frame.GetMethod()->GetDeclaringClass()->GetDexFile(),
863 invoke_method_idx);
864
865 // The invoke_method_idx here is the name of the signature polymorphic method that
Narayan Kamath9823e782016-08-03 12:46:58 +0100866 // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact)
867 // and not the method that we'll dispatch to in the end.
868 //
869 // TODO(narayan) We'll have to check in the verifier that this is in fact a
870 // signature polymorphic method so that we disallow calls via invoke-polymorphic
871 // to non sig-poly methods. This would also have the side effect of verifying
872 // that vRegC really is a reference type.
Narayan Kamath208f8572016-08-03 12:46:58 +0100873 StackHandleScope<6> hs(self);
874 Handle<mirror::MethodHandleImpl> method_handle(hs.NewHandle(
Mathieu Chartieref41db72016-10-25 15:08:01 -0700875 ObjPtr<mirror::MethodHandleImpl>::DownCast(
876 MakeObjPtr(shadow_frame.GetVRegReference(vRegC)))));
Narayan Kamath208f8572016-08-03 12:46:58 +0100877 if (UNLIKELY(method_handle.Get() == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100878 // Note that the invoke type is kVirtual here because a call to a signature
879 // polymorphic method is shaped like a virtual call at the bytecode level.
Orion Hodson3d617ac2016-10-19 14:00:46 +0100880 ThrowNullPointerExceptionForMethodAccess(invoke_method_idx, InvokeType::kVirtual);
Narayan Kamath9823e782016-08-03 12:46:58 +0100881 return false;
882 }
883
884 // The vRegH value gives the index of the proto_id associated with this
885 // signature polymorphic callsite.
886 const uint32_t callsite_proto_id = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
887
888 // Call through to the classlinker and ask it to resolve the static type associated
889 // with the callsite. This information is stored in the dex cache so it's
890 // guaranteed to be fast after the first resolution.
Narayan Kamath9823e782016-08-03 12:46:58 +0100891 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Narayan Kamath208f8572016-08-03 12:46:58 +0100892 Handle<mirror::Class> caller_class(hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass()));
893 Handle<mirror::MethodType> callsite_type(hs.NewHandle(class_linker->ResolveMethodType(
Narayan Kamath9823e782016-08-03 12:46:58 +0100894 caller_class->GetDexFile(), callsite_proto_id,
895 hs.NewHandle<mirror::DexCache>(caller_class->GetDexCache()),
Narayan Kamath208f8572016-08-03 12:46:58 +0100896 hs.NewHandle<mirror::ClassLoader>(caller_class->GetClassLoader()))));
Narayan Kamath9823e782016-08-03 12:46:58 +0100897
898 // This implies we couldn't resolve one or more types in this method handle.
Narayan Kamath208f8572016-08-03 12:46:58 +0100899 if (UNLIKELY(callsite_type.Get() == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100900 CHECK(self->IsExceptionPending());
Narayan Kamath9823e782016-08-03 12:46:58 +0100901 return false;
902 }
903
Narayan Kamath9823e782016-08-03 12:46:58 +0100904 const MethodHandleKind handle_kind = method_handle->GetHandleKind();
Narayan Kamath208f8572016-08-03 12:46:58 +0100905 Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType()));
Narayan Kamath208f8572016-08-03 12:46:58 +0100906 CHECK(handle_type.Get() != nullptr);
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000907 {
Narayan Kamath0a8485e2016-11-02 18:47:11 +0000908 // We need to check the nominal type of the handle in addition to the
909 // real type. The "nominal" type is present when MethodHandle.asType is
910 // called any handle, and results in the declared type of the handle
911 // changing.
912 ObjPtr<mirror::MethodType> nominal_type(method_handle->GetNominalType());
913 ObjPtr<mirror::MethodType> check_type(nullptr);
914 if (LIKELY(nominal_type.Ptr() == nullptr)) {
915 check_type.Assign(handle_type.Get());
916 } else {
917 check_type.Assign(nominal_type.Ptr());
918 }
919
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000920 if (is_invoke_exact) {
921 if (UNLIKELY(!callsite_type->IsExactMatch(check_type.Ptr()))) {
922 ThrowWrongMethodTypeException(check_type.Ptr(), callsite_type.Get());
923 return false;
924 }
Orion Hodsonab52ce12016-11-11 11:11:31 +0000925 } else if (!IsInvokeTransform(handle_kind)) {
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000926 if (UNLIKELY(!IsCallerTransformer(callsite_type) &&
927 !callsite_type->IsConvertible(check_type.Ptr()))) {
928 ThrowWrongMethodTypeException(check_type.Ptr(), callsite_type.Get());
929 return false;
930 }
Narayan Kamath0a8485e2016-11-02 18:47:11 +0000931 }
Orion Hodson3d617ac2016-10-19 14:00:46 +0100932 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100933
Narayan Kamath9823e782016-08-03 12:46:58 +0100934 uint32_t arg[Instruction::kMaxVarArgRegs] = {};
Narayan Kamath000e1882016-10-24 17:14:25 +0100935 uint32_t first_src_reg = 0;
Narayan Kamath9823e782016-08-03 12:46:58 +0100936 if (is_range) {
Narayan Kamath000e1882016-10-24 17:14:25 +0100937 first_src_reg = (inst->VRegC_4rcc() + 1);
Narayan Kamath9823e782016-08-03 12:46:58 +0100938 } else {
939 inst->GetVarArgs(arg, inst_data);
940 arg[0] = arg[1];
941 arg[1] = arg[2];
942 arg[2] = arg[3];
943 arg[3] = arg[4];
944 arg[4] = 0;
Narayan Kamath000e1882016-10-24 17:14:25 +0100945 first_src_reg = arg[0];
Narayan Kamath9823e782016-08-03 12:46:58 +0100946 }
947
948 if (IsInvoke(handle_kind)) {
Orion Hodson3d617ac2016-10-19 14:00:46 +0100949 // Get the method we're actually invoking along with the kind of
950 // invoke that is desired. We don't need to perform access checks at this
951 // point because they would have been performed on our behalf at the point
952 // of creation of the method handle.
953 ArtMethod* called_method = method_handle->GetTargetMethod();
954 CHECK(called_method != nullptr);
955
Narayan Kamath9823e782016-08-03 12:46:58 +0100956 if (handle_kind == kInvokeVirtual || handle_kind == kInvokeInterface) {
Narayan Kamath000e1882016-10-24 17:14:25 +0100957 // TODO: Unfortunately, we have to postpone dynamic receiver based checks
958 // because the receiver might be cast or might come from an emulated stack
959 // frame, which means that it is unknown at this point. We perform these
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000960 // checks inside DoCallPolymorphic right before we do the actual invoke.
Narayan Kamath9823e782016-08-03 12:46:58 +0100961 } else if (handle_kind == kInvokeDirect) {
Orion Hodson0d781e62016-11-04 11:09:53 +0000962 // String constructors are a special case, they are replaced with StringFactory
963 // methods.
964 if (called_method->IsConstructor() && called_method->GetDeclaringClass()->IsStringClass()) {
965 DCHECK(handle_type->GetRType()->IsStringClass());
966 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100967 }
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100968 } else if (handle_kind == kInvokeSuper) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700969 ObjPtr<mirror::Class> declaring_class = called_method->GetDeclaringClass();
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100970
971 // Note that we're not dynamically dispatching on the type of the receiver
972 // here. We use the static type of the "receiver" object that we've
973 // recorded in the method handle's type, which will be the same as the
974 // special caller that was specified at the point of lookup.
Mathieu Chartieref41db72016-10-25 15:08:01 -0700975 ObjPtr<mirror::Class> referrer_class = handle_type->GetPTypes()->Get(0);
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100976 if (!declaring_class->IsInterface()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700977 ObjPtr<mirror::Class> super_class = referrer_class->GetSuperClass();
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100978 uint16_t vtable_index = called_method->GetMethodIndex();
979 DCHECK(super_class != nullptr);
980 DCHECK(super_class->HasVTable());
981 // Note that super_class is a super of referrer_class and called_method
982 // will always be declared by super_class (or one of its super classes).
983 DCHECK_LT(vtable_index, super_class->GetVTableLength());
984 called_method = super_class->GetVTableEntry(vtable_index, kRuntimePointerSize);
985 } else {
986 called_method = referrer_class->FindVirtualMethodForInterfaceSuper(
987 called_method, kRuntimePointerSize);
988 }
989
990 CHECK(called_method != nullptr);
Narayan Kamath9823e782016-08-03 12:46:58 +0100991 }
992
Orion Hodsonab52ce12016-11-11 11:11:31 +0000993 if (IsInvokeTransform(handle_kind)) {
994 // There are two cases here - method handles representing regular
995 // transforms and those representing call site transforms. Method
996 // handles for call site transforms adapt their MethodType to match
997 // the call site. For these, the |callee_type| is the same as the
998 // |callsite_type|. The VarargsCollector is such a tranform, its
999 // method type depends on the call site, ie. x(a) or x(a, b), or
1000 // x(a, b, c). The VarargsCollector invokes a variable arity method
1001 // with the arity arguments in an array.
1002 Handle<mirror::MethodType> callee_type =
1003 (handle_kind == kInvokeCallSiteTransform) ? callsite_type : handle_type;
1004 return DoCallTransform<is_range>(called_method,
1005 callsite_type,
1006 callee_type,
1007 self,
1008 shadow_frame,
1009 method_handle /* receiver */,
1010 result,
1011 arg,
1012 first_src_reg);
Narayan Kamath208f8572016-08-03 12:46:58 +01001013 } else {
Orion Hodsonab52ce12016-11-11 11:11:31 +00001014 return DoCallPolymorphic<is_range>(called_method,
1015 callsite_type,
1016 handle_type,
1017 self,
1018 shadow_frame,
1019 result,
1020 arg,
1021 first_src_reg,
1022 handle_kind);
Narayan Kamath9823e782016-08-03 12:46:58 +01001023 }
Narayan Kamath9823e782016-08-03 12:46:58 +01001024 } else {
Orion Hodson3d617ac2016-10-19 14:00:46 +01001025 DCHECK(!is_range);
1026 ArtField* field = method_handle->GetTargetField();
Mathieu Chartier6beced42016-11-15 15:51:31 -08001027 Primitive::Type field_type = field->GetTypeAsPrimitiveType();
Orion Hodson3d617ac2016-10-19 14:00:46 +01001028
Orion Hodson3d617ac2016-10-19 14:00:46 +01001029 switch (handle_kind) {
1030 case kInstanceGet: {
Narayan Kamath6fcc5e82016-10-31 11:50:54 +00001031 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(first_src_reg);
Orion Hodson3d617ac2016-10-19 14:00:46 +01001032 DoFieldGetForInvokePolymorphic(self, shadow_frame, obj, field, field_type, result);
Orion Hodsonba28f9f2016-10-26 10:56:25 +01001033 if (!ConvertReturnValue(callsite_type, handle_type, result)) {
1034 DCHECK(self->IsExceptionPending());
1035 return false;
1036 }
Orion Hodson3d617ac2016-10-19 14:00:46 +01001037 return true;
1038 }
Orion Hodson3d617ac2016-10-19 14:00:46 +01001039 case kStaticGet: {
Orion Hodson0c14d8b2016-11-03 12:01:24 +00001040 ObjPtr<mirror::Object> obj = GetAndInitializeDeclaringClass(self, field);
1041 if (obj == nullptr) {
1042 DCHECK(self->IsExceptionPending());
1043 return false;
1044 }
Orion Hodson3d617ac2016-10-19 14:00:46 +01001045 DoFieldGetForInvokePolymorphic(self, shadow_frame, obj, field, field_type, result);
Orion Hodsonba28f9f2016-10-26 10:56:25 +01001046 if (!ConvertReturnValue(callsite_type, handle_type, result)) {
Orion Hodson0c14d8b2016-11-03 12:01:24 +00001047 DCHECK(self->IsExceptionPending());
1048 return false;
1049 }
Orion Hodsonba28f9f2016-10-26 10:56:25 +01001050 return true;
1051 }
1052 case kInstancePut: {
1053 JValue value = GetFieldValue(shadow_frame, field_type, arg[1]);
1054 if (!ConvertArgumentValue(callsite_type, handle_type, 1, &value)) {
1055 DCHECK(self->IsExceptionPending());
1056 return false;
1057 }
1058 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(first_src_reg);
Orion Hodsonba28f9f2016-10-26 10:56:25 +01001059 return DoFieldPutForInvokePolymorphic(self, shadow_frame, obj, field, field_type, value);
1060 }
1061 case kStaticPut: {
1062 JValue value = GetFieldValue(shadow_frame, field_type, arg[0]);
1063 if (!ConvertArgumentValue(callsite_type, handle_type, 0, &value)) {
1064 DCHECK(self->IsExceptionPending());
1065 return false;
1066 }
1067 ObjPtr<mirror::Object> obj = field->GetDeclaringClass();
Orion Hodsonba28f9f2016-10-26 10:56:25 +01001068 return DoFieldPutForInvokePolymorphic(self, shadow_frame, obj, field, field_type, value);
Orion Hodson3d617ac2016-10-19 14:00:46 +01001069 }
1070 default:
1071 LOG(FATAL) << "Unreachable: " << handle_kind;
1072 UNREACHABLE();
1073 }
Narayan Kamath9823e782016-08-03 12:46:58 +01001074 }
1075}
1076
Narayan Kamath208f8572016-08-03 12:46:58 +01001077// Calculate the number of ins for a proxy or native method, where we
1078// can't just look at the code item.
1079static inline size_t GetInsForProxyOrNativeMethod(ArtMethod* method)
1080 REQUIRES_SHARED(Locks::mutator_lock_) {
1081 DCHECK(method->IsNative() || method->IsProxyMethod());
1082
1083 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
1084 size_t num_ins = 0;
1085 // Separate accounting for the receiver, which isn't a part of the
1086 // shorty.
1087 if (!method->IsStatic()) {
1088 ++num_ins;
1089 }
1090
1091 uint32_t shorty_len = 0;
1092 const char* shorty = method->GetShorty(&shorty_len);
1093 for (size_t i = 1; i < shorty_len; ++i) {
1094 const char c = shorty[i];
1095 ++num_ins;
1096 if (c == 'J' || c == 'D') {
1097 ++num_ins;
1098 }
1099 }
1100
1101 return num_ins;
1102}
1103
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001104inline void PerformCall(Thread* self,
1105 const DexFile::CodeItem* code_item,
1106 ArtMethod* caller_method,
1107 const size_t first_dest_reg,
1108 ShadowFrame* callee_frame,
1109 JValue* result) {
1110 if (LIKELY(Runtime::Current()->IsStarted())) {
1111 ArtMethod* target = callee_frame->GetMethod();
1112 if (ClassLinker::ShouldUseInterpreterEntrypoint(
1113 target,
1114 target->GetEntryPointFromQuickCompiledCode())) {
1115 ArtInterpreterToInterpreterBridge(self, code_item, callee_frame, result);
1116 } else {
1117 ArtInterpreterToCompiledCodeBridge(
1118 self, caller_method, code_item, callee_frame, result);
1119 }
1120 } else {
1121 UnstartedRuntime::Invoke(self, code_item, callee_frame, result, first_dest_reg);
1122 }
1123}
1124
1125template <bool is_range>
1126inline void CopyRegisters(ShadowFrame& caller_frame,
1127 ShadowFrame* callee_frame,
1128 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
1129 const size_t first_src_reg,
1130 const size_t first_dest_reg,
1131 const size_t num_regs) {
1132 if (is_range) {
1133 const size_t dest_reg_bound = first_dest_reg + num_regs;
1134 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound;
1135 ++dest_reg, ++src_reg) {
1136 AssignRegister(callee_frame, caller_frame, dest_reg, src_reg);
1137 }
1138 } else {
1139 DCHECK_LE(num_regs, arraysize(arg));
1140
1141 for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) {
1142 AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]);
1143 }
1144 }
1145}
1146
Narayan Kamath208f8572016-08-03 12:46:58 +01001147template <bool is_range>
1148static inline bool DoCallPolymorphic(ArtMethod* called_method,
1149 Handle<mirror::MethodType> callsite_type,
1150 Handle<mirror::MethodType> target_type,
1151 Thread* self,
1152 ShadowFrame& shadow_frame,
1153 JValue* result,
1154 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Narayan Kamath000e1882016-10-24 17:14:25 +01001155 uint32_t first_src_reg,
1156 const MethodHandleKind handle_kind) {
Narayan Kamath208f8572016-08-03 12:46:58 +01001157 // Compute method information.
1158 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
1159
1160 // Number of registers for the callee's call frame. Note that for non-exact
1161 // invokes, we always derive this information from the callee method. We
1162 // cannot guarantee during verification that the number of registers encoded
1163 // in the invoke is equal to the number of ins for the callee. This is because
1164 // some transformations (such as boxing a long -> Long or wideining an
1165 // int -> long will change that number.
1166 uint16_t num_regs;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001167 size_t num_input_regs;
Narayan Kamath208f8572016-08-03 12:46:58 +01001168 size_t first_dest_reg;
1169 if (LIKELY(code_item != nullptr)) {
1170 num_regs = code_item->registers_size_;
1171 first_dest_reg = num_regs - code_item->ins_size_;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001172 num_input_regs = code_item->ins_size_;
Narayan Kamath208f8572016-08-03 12:46:58 +01001173 // Parameter registers go at the end of the shadow frame.
1174 DCHECK_NE(first_dest_reg, (size_t)-1);
1175 } else {
1176 // No local regs for proxy and native methods.
1177 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001178 num_regs = num_input_regs = GetInsForProxyOrNativeMethod(called_method);
Narayan Kamath208f8572016-08-03 12:46:58 +01001179 first_dest_reg = 0;
1180 }
1181
1182 // Allocate shadow frame on the stack.
1183 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
1184 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
1185 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
1186
Narayan Kamath000e1882016-10-24 17:14:25 +01001187 // Whether this polymorphic invoke was issued by a transformer method.
1188 bool is_caller_transformer = false;
Narayan Kamath208f8572016-08-03 12:46:58 +01001189 // Thread might be suspended during PerformArgumentConversions due to the
1190 // allocations performed during boxing.
1191 {
1192 ScopedStackedShadowFramePusher pusher(
1193 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001194 if (callsite_type->IsExactMatch(target_type.Get())) {
1195 // This is an exact invoke, we can take the fast path of just copying all
1196 // registers without performing any argument conversions.
1197 CopyRegisters<is_range>(shadow_frame,
1198 new_shadow_frame,
1199 arg,
1200 first_src_reg,
1201 first_dest_reg,
1202 num_input_regs);
1203 } else {
1204 // This includes the case where we're entering this invoke-polymorphic
1205 // from a transformer method. In that case, the callsite_type will contain
1206 // a single argument of type dalvik.system.EmulatedStackFrame. In that
1207 // case, we'll have to unmarshal the EmulatedStackFrame into the
1208 // new_shadow_frame and perform argument conversions on it.
1209 if (IsCallerTransformer(callsite_type)) {
Narayan Kamath000e1882016-10-24 17:14:25 +01001210 is_caller_transformer = true;
1211 // The emulated stack frame is the first and only argument when we're coming
1212 // through from a transformer.
1213 ObjPtr<mirror::EmulatedStackFrame> emulated_stack_frame(
1214 reinterpret_cast<mirror::EmulatedStackFrame*>(
1215 shadow_frame.GetVRegReference(first_src_reg)));
1216 if (!emulated_stack_frame->WriteToShadowFrame(self,
1217 target_type,
1218 first_dest_reg,
1219 new_shadow_frame)) {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001220 DCHECK(self->IsExceptionPending());
1221 result->SetL(0);
1222 return false;
1223 }
1224 } else if (!ConvertAndCopyArgumentsFromCallerFrame<is_range>(self,
1225 callsite_type,
1226 target_type,
1227 shadow_frame,
1228 first_src_reg,
1229 first_dest_reg,
1230 arg,
1231 new_shadow_frame)) {
1232 DCHECK(self->IsExceptionPending());
1233 result->SetL(0);
1234 return false;
1235 }
Narayan Kamath208f8572016-08-03 12:46:58 +01001236 }
1237 }
1238
Narayan Kamath000e1882016-10-24 17:14:25 +01001239 // See TODO in DoInvokePolymorphic : We need to perform this dynamic, receiver
1240 // based dispatch right before we perform the actual call, because the
1241 // receiver isn't known very early.
1242 if (handle_kind == kInvokeVirtual || handle_kind == kInvokeInterface) {
1243 ObjPtr<mirror::Object> receiver(new_shadow_frame->GetVRegReference(first_dest_reg));
1244 ObjPtr<mirror::Class> declaring_class(called_method->GetDeclaringClass());
1245 // Verify that _vRegC is an object reference and of the type expected by
1246 // the receiver.
1247 if (!VerifyObjectIsClass(receiver, declaring_class)) {
1248 DCHECK(self->IsExceptionPending());
1249 return false;
1250 }
1251
1252 called_method = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(
1253 called_method, kRuntimePointerSize);
1254 }
1255
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001256 PerformCall(self, code_item, shadow_frame.GetMethod(), first_dest_reg, new_shadow_frame, result);
Orion Hodsonab52ce12016-11-11 11:11:31 +00001257 if (self->IsExceptionPending()) {
1258 return false;
1259 }
Narayan Kamath208f8572016-08-03 12:46:58 +01001260
Narayan Kamath000e1882016-10-24 17:14:25 +01001261 // If the caller of this signature polymorphic method was a transformer,
1262 // we need to copy the result back out to the emulated stack frame.
Orion Hodsonab52ce12016-11-11 11:11:31 +00001263 if (is_caller_transformer) {
1264 StackHandleScope<2> hs(self);
1265 Handle<mirror::EmulatedStackFrame> emulated_stack_frame(
1266 hs.NewHandle(reinterpret_cast<mirror::EmulatedStackFrame*>(
1267 shadow_frame.GetVRegReference(first_src_reg))));
1268 Handle<mirror::MethodType> emulated_stack_type(hs.NewHandle(emulated_stack_frame->GetType()));
1269 JValue local_result;
1270 local_result.SetJ(result->GetJ());
Narayan Kamath000e1882016-10-24 17:14:25 +01001271
Orion Hodsonab52ce12016-11-11 11:11:31 +00001272 if (ConvertReturnValue(emulated_stack_type, target_type, &local_result)) {
1273 emulated_stack_frame->SetReturnValue(self, local_result);
1274 return true;
1275 } else {
1276 DCHECK(self->IsExceptionPending());
1277 return false;
1278 }
1279 } else {
1280 return ConvertReturnValue(callsite_type, target_type, result);
Narayan Kamath000e1882016-10-24 17:14:25 +01001281 }
Narayan Kamath208f8572016-08-03 12:46:58 +01001282}
1283
Narayan Kamath000e1882016-10-24 17:14:25 +01001284template <bool is_range>
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001285static inline bool DoCallTransform(ArtMethod* called_method,
1286 Handle<mirror::MethodType> callsite_type,
Narayan Kamath000e1882016-10-24 17:14:25 +01001287 Handle<mirror::MethodType> callee_type,
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001288 Thread* self,
1289 ShadowFrame& shadow_frame,
1290 Handle<mirror::MethodHandleImpl> receiver,
Narayan Kamath000e1882016-10-24 17:14:25 +01001291 JValue* result,
1292 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
1293 uint32_t first_src_reg) {
1294 // This can be fixed to two, because the method we're calling here
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001295 // (MethodHandle.transformInternal) doesn't have any locals and the signature
1296 // is known :
1297 //
1298 // private MethodHandle.transformInternal(EmulatedStackFrame sf);
1299 //
1300 // This means we need only two vregs :
1301 // - One for the receiver object.
1302 // - One for the only method argument (an EmulatedStackFrame).
1303 static constexpr size_t kNumRegsForTransform = 2;
1304
1305 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
1306 DCHECK(code_item != nullptr);
1307 DCHECK_EQ(kNumRegsForTransform, code_item->registers_size_);
1308 DCHECK_EQ(kNumRegsForTransform, code_item->ins_size_);
1309
1310 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
1311 CREATE_SHADOW_FRAME(kNumRegsForTransform, &shadow_frame, called_method, /* dex pc */ 0);
1312 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
1313
Narayan Kamath000e1882016-10-24 17:14:25 +01001314 StackHandleScope<1> hs(self);
1315 MutableHandle<mirror::EmulatedStackFrame> sf(hs.NewHandle<mirror::EmulatedStackFrame>(nullptr));
1316 if (IsCallerTransformer(callsite_type)) {
1317 // If we're entering this transformer from another transformer, we can pass
1318 // through the handle directly to the callee, instead of having to
1319 // instantiate a new stack frame based on the shadow frame.
1320 sf.Assign(reinterpret_cast<mirror::EmulatedStackFrame*>(
1321 shadow_frame.GetVRegReference(first_src_reg)));
1322 } else {
1323 sf.Assign(mirror::EmulatedStackFrame::CreateFromShadowFrameAndArgs<is_range>(
1324 self,
1325 callsite_type,
1326 callee_type,
1327 shadow_frame,
1328 first_src_reg,
1329 arg));
1330
1331 // Something went wrong while creating the emulated stack frame, we should
1332 // throw the pending exception.
1333 if (sf.Get() == nullptr) {
1334 DCHECK(self->IsExceptionPending());
1335 return false;
1336 }
1337 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001338
1339 new_shadow_frame->SetVRegReference(0, receiver.Get());
Narayan Kamath000e1882016-10-24 17:14:25 +01001340 new_shadow_frame->SetVRegReference(1, sf.Get());
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001341
1342 PerformCall(self,
1343 code_item,
1344 shadow_frame.GetMethod(),
1345 0 /* first dest reg */,
1346 new_shadow_frame,
1347 result);
Orion Hodsonab52ce12016-11-11 11:11:31 +00001348 if (self->IsExceptionPending()) {
1349 return false;
1350 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001351
Narayan Kamath000e1882016-10-24 17:14:25 +01001352 // If the called transformer method we called has returned a value, then we
1353 // need to copy it back to |result|.
Orion Hodsonab52ce12016-11-11 11:11:31 +00001354 sf->GetReturnValue(self, result);
1355 return ConvertReturnValue(callsite_type, callee_type, result);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001356}
1357
Igor Murashkin6918bf12015-09-27 19:19:06 -07001358template <bool is_range,
Narayan Kamath370423d2016-10-03 16:51:22 +01001359 bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001360static inline bool DoCallCommon(ArtMethod* called_method,
1361 Thread* self,
1362 ShadowFrame& shadow_frame,
1363 JValue* result,
1364 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +01001365 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -07001366 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001367 bool string_init = false;
1368 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001369 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
1370 && called_method->IsConstructor())) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001371 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
Jeff Hao848f70a2014-01-15 13:49:50 -08001372 string_init = true;
1373 }
1374
Alex Lightdaf58c82016-03-16 23:00:49 +00001375 // Compute method information.
1376 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
Igor Murashkin158f35c2015-06-10 15:55:30 -07001377
1378 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001379 uint16_t num_regs;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001380 if (LIKELY(code_item != nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001381 num_regs = code_item->registers_size_;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001382 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001383 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -08001384 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Igor Murashkin158f35c2015-06-10 15:55:30 -07001385 num_regs = number_of_inputs;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001386 }
1387
Igor Murashkin158f35c2015-06-10 15:55:30 -07001388 // Hack for String init:
1389 //
1390 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
1391 // invoke-x StringFactory(a, b, c, ...)
1392 // by effectively dropping the first virtual register from the invoke.
1393 //
1394 // (at this point the ArtMethod has already been replaced,
1395 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +00001396 //
1397 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
1398 // to handle the compiler optimization of replacing `this` with null without
1399 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001400 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -07001401 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001402 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 -07001403
Igor Murashkin158f35c2015-06-10 15:55:30 -07001404 // The new StringFactory call is static and has one fewer argument.
Igor Murashkina06b49b2015-06-25 15:18:12 -07001405 if (code_item == nullptr) {
1406 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1407 num_regs--;
1408 } // 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 -07001409 number_of_inputs--;
1410
1411 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -07001412 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001413 arg[i - 1] = arg[i];
1414 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07001415 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001416
1417 // Rewrite the non-var-arg case
1418 vregC++; // Skips the 0th vreg in the range ("this").
1419 }
1420
1421 // Parameter registers go at the end of the shadow frame.
1422 DCHECK_GE(num_regs, number_of_inputs);
1423 size_t first_dest_reg = num_regs - number_of_inputs;
1424 DCHECK_NE(first_dest_reg, (size_t)-1);
1425
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001426 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001427 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -07001428 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -07001429 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -07001430 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001431
Igor Murashkin158f35c2015-06-10 15:55:30 -07001432 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -07001433 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001434 // Slow path.
1435 // We might need to do class loading, which incurs a thread state change to kNative. So
1436 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07001437 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +02001438 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001439 self->EndAssertNoThreadSuspension(old_cause);
1440
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001441 // ArtMethod here is needed to check type information of the call site against the callee.
1442 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
1443 //
1444 // As a special case for proxy methods, which are not dex-backed,
1445 // we have to retrieve type information from the proxy's method
1446 // interface method instead (which is dex backed since proxies are never interfaces).
Andreas Gampe542451c2016-07-26 09:02:02 -07001447 ArtMethod* method =
1448 new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001449
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001450 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001451 // to get the exact type of each reference argument.
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001452 const DexFile::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001453 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001454 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001455
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001456 // Handle receiver apart since it's not part of the shorty.
1457 size_t dest_reg = first_dest_reg;
1458 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001459
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001460 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001461 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001462 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
1463 ++dest_reg;
1464 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -07001465 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001466 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001467
1468 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001469 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001470 // Skip the 0th 'shorty' type since it represents the return type.
1471 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001472 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
1473 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001474 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001475 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001476 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001477 if (do_assignability_check && o != nullptr) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001478 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Andreas Gampea5b09a62016-11-17 15:21:22 -08001479 const dex::TypeIndex type_idx = params->GetTypeItem(shorty_pos).type_idx_;
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001480 ObjPtr<mirror::Class> arg_type = method->GetDexCacheResolvedType(type_idx,
1481 pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001482 if (arg_type == nullptr) {
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001483 StackHandleScope<1> hs(self);
1484 // Preserve o since it is used below and GetClassFromTypeIndex may cause thread
1485 // suspension.
1486 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&o);
1487 arg_type = method->GetClassFromTypeIndex(type_idx, true /* resolve */, pointer_size);
1488 if (arg_type == nullptr) {
1489 CHECK(self->IsExceptionPending());
1490 return false;
1491 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001492 }
1493 if (!o->VerifierInstanceOf(arg_type)) {
1494 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -07001495 std::string temp1, temp2;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001496 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001497 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -08001498 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -07001499 o->GetClass()->GetDescriptor(&temp1),
1500 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001501 return false;
1502 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001503 }
Mathieu Chartieref41db72016-10-25 15:08:01 -07001504 new_shadow_frame->SetVRegReference(dest_reg, o.Ptr());
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001505 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -07001506 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001507 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001508 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001509 uint64_t wide_value =
1510 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
1511 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001512 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001513 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001514 ++dest_reg;
1515 ++arg_offset;
1516 break;
1517 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001518 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001519 default:
1520 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
1521 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001522 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001523 }
1524 } else {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001525 if (is_range) {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001526 DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001527 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001528
1529 CopyRegisters<is_range>(shadow_frame,
1530 new_shadow_frame,
1531 arg,
1532 vregC,
1533 first_dest_reg,
1534 number_of_inputs);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001535 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001536 }
1537
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001538 PerformCall(self, code_item, shadow_frame.GetMethod(), first_dest_reg, new_shadow_frame, result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001539
1540 if (string_init && !self->IsExceptionPending()) {
Mingyao Yangffedec52016-05-19 10:48:40 -07001541 SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001542 }
1543
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001544 return !self->IsExceptionPending();
1545}
1546
Igor Murashkin158f35c2015-06-10 15:55:30 -07001547template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001548bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
1549 const Instruction* inst, uint16_t inst_data, JValue* result) {
1550 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001551 const uint16_t number_of_inputs =
1552 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001553
1554 // TODO: find a cleaner way to separate non-range and range information without duplicating
1555 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -07001556 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001557 uint32_t vregC = 0;
1558 if (is_range) {
1559 vregC = inst->VRegC_3rc();
1560 } else {
1561 vregC = inst->VRegC_35c();
1562 inst->GetVarArgs(arg, inst_data);
1563 }
1564
1565 return DoCallCommon<is_range, do_assignability_check>(
1566 called_method, self, shadow_frame,
1567 result, number_of_inputs, arg, vregC);
1568}
1569
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001570template <bool is_range, bool do_access_check, bool transaction_active>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001571bool DoFilledNewArray(const Instruction* inst,
1572 const ShadowFrame& shadow_frame,
1573 Thread* self,
1574 JValue* result) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001575 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
1576 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
1577 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
1578 if (!is_range) {
1579 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
1580 CHECK_LE(length, 5);
1581 }
1582 if (UNLIKELY(length < 0)) {
1583 ThrowNegativeArraySizeException(length);
1584 return false;
1585 }
1586 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08001587 ObjPtr<mirror::Class> array_class = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
Mathieu Chartieref41db72016-10-25 15:08:01 -07001588 shadow_frame.GetMethod(),
1589 self,
1590 false,
1591 do_access_check);
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001592 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001593 DCHECK(self->IsExceptionPending());
1594 return false;
1595 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001596 CHECK(array_class->IsArrayClass());
Mathieu Chartieref41db72016-10-25 15:08:01 -07001597 ObjPtr<mirror::Class> component_class = array_class->GetComponentType();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001598 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
1599 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
1600 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001601 ThrowRuntimeException("Bad filled array request for type %s",
David Sehr709b0702016-10-13 09:12:37 -07001602 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001603 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001604 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -08001605 "Found type %s; filled-new-array not implemented for anything but 'int'",
David Sehr709b0702016-10-13 09:12:37 -07001606 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001607 }
1608 return false;
1609 }
Mathieu Chartieref41db72016-10-25 15:08:01 -07001610 ObjPtr<mirror::Object> new_array = mirror::Array::Alloc<true>(
1611 self,
1612 array_class,
1613 length,
1614 array_class->GetComponentSizeShift(),
1615 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001616 if (UNLIKELY(new_array == nullptr)) {
1617 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001618 return false;
1619 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001620 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
1621 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001622 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +01001623 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001624 } else {
Ian Rogers29a26482014-05-02 15:27:29 -07001625 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +01001626 }
Sebastien Hertzabff6432014-01-27 18:01:39 +01001627 for (int32_t i = 0; i < length; ++i) {
1628 size_t src_reg = is_range ? vregC + i : arg[i];
1629 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001630 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
1631 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +01001632 } else {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001633 new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<transaction_active>(
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001634 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001635 }
1636 }
1637
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001638 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001639 return true;
1640}
1641
Mathieu Chartieref41db72016-10-25 15:08:01 -07001642// TODO: Use ObjPtr here.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001643template<typename T>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001644static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array,
1645 int32_t count)
1646 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001647 Runtime* runtime = Runtime::Current();
1648 for (int32_t i = 0; i < count; ++i) {
1649 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
1650 }
1651}
1652
Mathieu Chartieref41db72016-10-25 15:08:01 -07001653void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001654 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001655 DCHECK(Runtime::Current()->IsActiveTransaction());
1656 DCHECK(array != nullptr);
1657 DCHECK_LE(count, array->GetLength());
1658 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
1659 switch (primitive_component_type) {
1660 case Primitive::kPrimBoolean:
1661 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
1662 break;
1663 case Primitive::kPrimByte:
1664 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
1665 break;
1666 case Primitive::kPrimChar:
1667 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
1668 break;
1669 case Primitive::kPrimShort:
1670 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
1671 break;
1672 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001673 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
1674 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001675 case Primitive::kPrimFloat:
1676 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
1677 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001678 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001679 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
1680 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001681 case Primitive::kPrimDouble:
1682 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
1683 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001684 default:
1685 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
1686 << " in fill-array-data";
1687 break;
1688 }
1689}
1690
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001691// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +02001692#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001693 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001694 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
1695 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +02001696 const Instruction* inst, uint16_t inst_data, \
1697 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001698EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
1699EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
1700EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
1701EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
1702#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001703
Narayan Kamath9823e782016-08-03 12:46:58 +01001704// Explicit DoInvokePolymorphic template function declarations.
1705#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range, _do_assignability_check) \
1706 template REQUIRES_SHARED(Locks::mutator_lock_) \
1707 bool DoInvokePolymorphic<_is_range, _do_assignability_check>( \
1708 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1709 uint16_t inst_data, JValue* result)
1710
1711EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, false);
1712EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, true);
1713EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, false);
1714EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, true);
1715#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1716
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001717// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001718#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001719 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001720 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
1721 const ShadowFrame& shadow_frame, \
1722 Thread* self, JValue* result)
1723#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
1724 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
1725 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
1726 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
1727 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
1728EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
1729EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
1730#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001731#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
1732
1733} // namespace interpreter
1734} // namespace art