blob: 475f93803d78f0bb219a6a1c8cbb879900fb1a26 [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"
David Sehr9e734c72018-01-04 17:56:19 -080023#include "dex/dex_file_types.h"
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +000024#include "entrypoints/runtime_asm_entrypoints.h"
Orion Hodson43f0cdb2017-10-10 14:47:32 +010025#include "intrinsics_enum.h"
Tamas Berghammerdd5e5e92016-02-12 16:29:00 +000026#include "jit/jit.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010027#include "jvalue.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010028#include "method_handles-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070029#include "method_handles.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010030#include "mirror/array-inl.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010031#include "mirror/class.h"
Narayan Kamath000e1882016-10-24 17:14:25 +010032#include "mirror/emulated_stack_frame.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080033#include "mirror/method_handle_impl-inl.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010034#include "reflection-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "reflection.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070036#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070037#include "thread-inl.h"
Chang Xingbd208d82017-07-12 14:53:17 -070038#include "transaction.h"
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +010039#include "well_known_classes.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020040
41namespace art {
42namespace interpreter {
43
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000044void ThrowNullPointerExceptionFromInterpreter() {
45 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -070046}
47
Chang Xingbd208d82017-07-12 14:53:17 -070048template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
49 bool transaction_active>
Ian Rogers54874942014-06-10 16:31:03 -070050bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
51 uint16_t inst_data) {
52 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
53 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010054 ArtField* f =
55 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
56 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -070057 if (UNLIKELY(f == nullptr)) {
58 CHECK(self->IsExceptionPending());
59 return false;
60 }
Mathieu Chartieref41db72016-10-25 15:08:01 -070061 ObjPtr<mirror::Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -070062 if (is_static) {
63 obj = f->GetDeclaringClass();
Chang Xingbd208d82017-07-12 14:53:17 -070064 if (transaction_active) {
65 if (Runtime::Current()->GetTransaction()->ReadConstraint(obj.Ptr(), f)) {
66 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Can't read static fields of "
67 + obj->PrettyTypeOf() + " since it does not belong to clinit's class.");
68 return false;
69 }
70 }
Ian Rogers54874942014-06-10 16:31:03 -070071 } else {
72 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
73 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000074 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers54874942014-06-10 16:31:03 -070075 return false;
76 }
77 }
Orion Hodson3d617ac2016-10-19 14:00:46 +010078
79 JValue result;
Alex Light084fa372017-06-16 08:58:34 -070080 if (UNLIKELY(!DoFieldGetCommon<field_type>(self, shadow_frame, obj, f, &result))) {
81 // Instrumentation threw an error!
82 CHECK(self->IsExceptionPending());
83 return false;
84 }
Ian Rogers54874942014-06-10 16:31:03 -070085 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
86 switch (field_type) {
87 case Primitive::kPrimBoolean:
Orion Hodson3d617ac2016-10-19 14:00:46 +010088 shadow_frame.SetVReg(vregA, result.GetZ());
Ian Rogers54874942014-06-10 16:31:03 -070089 break;
90 case Primitive::kPrimByte:
Orion Hodson3d617ac2016-10-19 14:00:46 +010091 shadow_frame.SetVReg(vregA, result.GetB());
Ian Rogers54874942014-06-10 16:31:03 -070092 break;
93 case Primitive::kPrimChar:
Orion Hodson3d617ac2016-10-19 14:00:46 +010094 shadow_frame.SetVReg(vregA, result.GetC());
Ian Rogers54874942014-06-10 16:31:03 -070095 break;
96 case Primitive::kPrimShort:
Orion Hodson3d617ac2016-10-19 14:00:46 +010097 shadow_frame.SetVReg(vregA, result.GetS());
Ian Rogers54874942014-06-10 16:31:03 -070098 break;
99 case Primitive::kPrimInt:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100100 shadow_frame.SetVReg(vregA, result.GetI());
Ian Rogers54874942014-06-10 16:31:03 -0700101 break;
102 case Primitive::kPrimLong:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100103 shadow_frame.SetVRegLong(vregA, result.GetJ());
Ian Rogers54874942014-06-10 16:31:03 -0700104 break;
105 case Primitive::kPrimNot:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100106 shadow_frame.SetVRegReference(vregA, result.GetL());
Ian Rogers54874942014-06-10 16:31:03 -0700107 break;
108 default:
109 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700110 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700111 }
112 return true;
113}
114
115// Explicitly instantiate all DoFieldGet functions.
Chang Xingbd208d82017-07-12 14:53:17 -0700116#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
117 template bool DoFieldGet<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
Ian Rogers54874942014-06-10 16:31:03 -0700118 ShadowFrame& shadow_frame, \
119 const Instruction* inst, \
120 uint16_t inst_data)
121
122#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
Chang Xingbd208d82017-07-12 14:53:17 -0700123 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false, true); \
124 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false, false); \
125 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true, true); \
126 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true, false);
Ian Rogers54874942014-06-10 16:31:03 -0700127
128// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700129EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
130EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
131EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
132EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
133EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
134EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
135EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700136
137// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700138EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
139EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
140EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
141EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
142EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
143EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
144EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700145
146#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
147#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
148
149// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
150// Returns true on success, otherwise throws an exception and returns false.
151template<Primitive::Type field_type>
152bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700153 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700154 if (UNLIKELY(obj == nullptr)) {
155 // We lost the reference to the field index so we cannot get a more
156 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000157 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700158 return false;
159 }
160 MemberOffset field_offset(inst->VRegC_22c());
161 // Report this field access to instrumentation if needed. Since we only have the offset of
162 // the field from the base of the object, we need to look for it first.
163 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
164 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
165 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
166 field_offset.Uint32Value());
167 DCHECK(f != nullptr);
168 DCHECK(!f->IsStatic());
Alex Light084fa372017-06-16 08:58:34 -0700169 Thread* self = Thread::Current();
170 StackHandleScope<1> hs(self);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700171 // Save obj in case the instrumentation event has thread suspension.
172 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Alex Light084fa372017-06-16 08:58:34 -0700173 instrumentation->FieldReadEvent(self,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700174 obj.Ptr(),
175 shadow_frame.GetMethod(),
176 shadow_frame.GetDexPC(),
177 f);
Alex Light084fa372017-06-16 08:58:34 -0700178 if (UNLIKELY(self->IsExceptionPending())) {
179 return false;
180 }
Ian Rogers54874942014-06-10 16:31:03 -0700181 }
182 // Note: iget-x-quick instructions are only for non-volatile fields.
183 const uint32_t vregA = inst->VRegA_22c(inst_data);
184 switch (field_type) {
185 case Primitive::kPrimInt:
186 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
187 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800188 case Primitive::kPrimBoolean:
189 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
190 break;
191 case Primitive::kPrimByte:
192 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
193 break;
194 case Primitive::kPrimChar:
195 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
196 break;
197 case Primitive::kPrimShort:
198 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
199 break;
Ian Rogers54874942014-06-10 16:31:03 -0700200 case Primitive::kPrimLong:
201 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
202 break;
203 case Primitive::kPrimNot:
204 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
205 break;
206 default:
207 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700208 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700209 }
210 return true;
211}
212
213// Explicitly instantiate all DoIGetQuick functions.
214#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
215 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
216 uint16_t inst_data)
217
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800218EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
219EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
220EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
221EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
222EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
223EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
224EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700225#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
226
227template<Primitive::Type field_type>
228static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700229 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700230 JValue field_value;
231 switch (field_type) {
232 case Primitive::kPrimBoolean:
233 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
234 break;
235 case Primitive::kPrimByte:
236 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
237 break;
238 case Primitive::kPrimChar:
239 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
240 break;
241 case Primitive::kPrimShort:
242 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
243 break;
244 case Primitive::kPrimInt:
245 field_value.SetI(shadow_frame.GetVReg(vreg));
246 break;
247 case Primitive::kPrimLong:
248 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
249 break;
250 case Primitive::kPrimNot:
251 field_value.SetL(shadow_frame.GetVRegReference(vreg));
252 break;
253 default:
254 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700255 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700256 }
257 return field_value;
258}
259
Orion Hodson3d617ac2016-10-19 14:00:46 +0100260template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
261 bool transaction_active>
262bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
263 uint16_t inst_data) {
264 const bool do_assignability_check = do_access_check;
265 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
266 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
267 ArtField* f =
268 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
269 Primitive::ComponentSize(field_type));
270 if (UNLIKELY(f == nullptr)) {
271 CHECK(self->IsExceptionPending());
272 return false;
273 }
274 ObjPtr<mirror::Object> obj;
275 if (is_static) {
276 obj = f->GetDeclaringClass();
Chang Xingbd208d82017-07-12 14:53:17 -0700277 if (transaction_active) {
278 if (Runtime::Current()->GetTransaction()->WriteConstraint(obj.Ptr(), f)) {
279 Runtime::Current()->AbortTransactionAndThrowAbortError(
280 self, "Can't set fields of " + obj->PrettyTypeOf());
281 return false;
282 }
283 }
284
Orion Hodson3d617ac2016-10-19 14:00:46 +0100285 } else {
286 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
287 if (UNLIKELY(obj == nullptr)) {
288 ThrowNullPointerExceptionForFieldAccess(f, false);
289 return false;
290 }
291 }
292
293 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100294 JValue value = GetFieldValue<field_type>(shadow_frame, vregA);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100295 return DoFieldPutCommon<field_type, do_assignability_check, transaction_active>(self,
296 shadow_frame,
297 obj,
298 f,
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100299 value);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100300}
301
Ian Rogers54874942014-06-10 16:31:03 -0700302// Explicitly instantiate all DoFieldPut functions.
303#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
304 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
305 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
306
307#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
308 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
309 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
310 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
311 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
312
313// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700314EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
315EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
316EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
317EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
318EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
319EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
320EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700321
322// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700323EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
324EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
325EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
326EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
327EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
328EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
329EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700330
331#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
332#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
333
334template<Primitive::Type field_type, bool transaction_active>
335bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700336 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700337 if (UNLIKELY(obj == nullptr)) {
338 // We lost the reference to the field index so we cannot get a more
339 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000340 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700341 return false;
342 }
343 MemberOffset field_offset(inst->VRegC_22c());
344 const uint32_t vregA = inst->VRegA_22c(inst_data);
345 // Report this field modification to instrumentation if needed. Since we only have the offset of
346 // the field from the base of the object, we need to look for it first.
347 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
348 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
349 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
350 field_offset.Uint32Value());
351 DCHECK(f != nullptr);
352 DCHECK(!f->IsStatic());
353 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Alex Light084fa372017-06-16 08:58:34 -0700354 Thread* self = Thread::Current();
355 StackHandleScope<2> hs(self);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700356 // Save obj in case the instrumentation event has thread suspension.
357 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Alex Light084fa372017-06-16 08:58:34 -0700358 mirror::Object* fake_root = nullptr;
359 HandleWrapper<mirror::Object> ret(hs.NewHandleWrapper<mirror::Object>(
360 field_type == Primitive::kPrimNot ? field_value.GetGCRoot() : &fake_root));
361 instrumentation->FieldWriteEvent(self,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700362 obj.Ptr(),
363 shadow_frame.GetMethod(),
364 shadow_frame.GetDexPC(),
365 f,
366 field_value);
Alex Light084fa372017-06-16 08:58:34 -0700367 if (UNLIKELY(self->IsExceptionPending())) {
368 return false;
369 }
Ian Rogers54874942014-06-10 16:31:03 -0700370 }
371 // Note: iput-x-quick instructions are only for non-volatile fields.
372 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700373 case Primitive::kPrimBoolean:
374 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
375 break;
376 case Primitive::kPrimByte:
377 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
378 break;
379 case Primitive::kPrimChar:
380 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
381 break;
382 case Primitive::kPrimShort:
383 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
384 break;
Ian Rogers54874942014-06-10 16:31:03 -0700385 case Primitive::kPrimInt:
386 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
387 break;
388 case Primitive::kPrimLong:
389 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
390 break;
391 case Primitive::kPrimNot:
392 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
393 break;
394 default:
395 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700396 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700397 }
398 return true;
399}
400
401// Explicitly instantiate all DoIPutQuick functions.
402#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
403 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
404 const Instruction* inst, \
405 uint16_t inst_data)
406
407#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
408 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
409 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
410
Andreas Gampec8ccf682014-09-29 20:07:43 -0700411EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
412EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
413EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
414EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
415EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
416EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
417EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700418#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
419#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
420
Alex Light9fb1ab12017-09-05 09:32:49 -0700421// We execute any instrumentation events that are triggered by this exception and change the
422// shadow_frame's dex_pc to that of the exception handler if there is one in the current method.
423// Return true if we should continue executing in the current method and false if we need to go up
424// the stack to find an exception handler.
Sebastien Hertz520633b2015-09-08 17:03:36 +0200425// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Alex Light9fb1ab12017-09-05 09:32:49 -0700426// TODO We should have a better way to skip instrumentation reporting or possibly rethink that
427// behavior.
428bool MoveToExceptionHandler(Thread* self,
429 ShadowFrame& shadow_frame,
430 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700431 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700432 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000433 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Alex Light9fb1ab12017-09-05 09:32:49 -0700434 if (instrumentation != nullptr &&
435 instrumentation->HasExceptionThrownListeners() &&
436 self->IsExceptionThrownByCurrentMethod(exception.Get())) {
437 // See b/65049545 for why we don't need to check to see if the exception has changed.
Alex Light6e1607e2017-08-23 10:06:18 -0700438 instrumentation->ExceptionThrownEvent(self, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200439 }
Ian Rogers54874942014-06-10 16:31:03 -0700440 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700441 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
Alex Light9fb1ab12017-09-05 09:32:49 -0700442 hs.NewHandle(exception->GetClass()), shadow_frame.GetDexPC(), &clear_exception);
Andreas Gampee2abbc62017-09-15 11:59:26 -0700443 if (found_dex_pc == dex::kDexNoIndex) {
Alex Light9fb1ab12017-09-05 09:32:49 -0700444 if (instrumentation != nullptr) {
445 if (shadow_frame.NeedsNotifyPop()) {
446 instrumentation->WatchedFramePopped(self, shadow_frame);
447 }
448 // Exception is not caught by the current method. We will unwind to the
449 // caller. Notify any instrumentation listener.
450 instrumentation->MethodUnwindEvent(self,
451 shadow_frame.GetThisObject(),
452 shadow_frame.GetMethod(),
453 shadow_frame.GetDexPC());
Alex Lighte814f9d2017-07-31 16:14:39 -0700454 }
Alex Light9fb1ab12017-09-05 09:32:49 -0700455 return false;
Ian Rogers54874942014-06-10 16:31:03 -0700456 } else {
Alex Light9fb1ab12017-09-05 09:32:49 -0700457 shadow_frame.SetDexPC(found_dex_pc);
458 if (instrumentation != nullptr && instrumentation->HasExceptionHandledListeners()) {
459 self->ClearException();
460 instrumentation->ExceptionHandledEvent(self, exception.Get());
461 if (UNLIKELY(self->IsExceptionPending())) {
462 // Exception handled event threw an exception. Try to find the handler for this one.
463 return MoveToExceptionHandler(self, shadow_frame, instrumentation);
464 } else if (!clear_exception) {
465 self->SetException(exception.Get());
466 }
467 } else if (clear_exception) {
Ian Rogers54874942014-06-10 16:31:03 -0700468 self->ClearException();
469 }
Alex Light9fb1ab12017-09-05 09:32:49 -0700470 return true;
Ian Rogers54874942014-06-10 16:31:03 -0700471 }
Ian Rogers54874942014-06-10 16:31:03 -0700472}
473
Ian Rogerse94652f2014-12-02 11:13:19 -0800474void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
475 LOG(FATAL) << "Unexpected instruction: "
476 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
477 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700478}
479
Sebastien Hertz45b15972015-04-03 16:07:05 +0200480void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700481 va_list args;
482 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200483 AbortTransactionV(self, fmt, args);
484 va_end(args);
485}
486
487void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
488 CHECK(Runtime::Current()->IsActiveTransaction());
489 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100490 std::string abort_msg;
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800491 android::base::StringAppendV(&abort_msg, fmt, args);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100492 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200493 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700494}
495
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100496// START DECLARATIONS :
497//
498// These additional declarations are required because clang complains
499// about ALWAYS_INLINE (-Werror, -Wgcc-compat) in definitions.
500//
501
Narayan Kamath9823e782016-08-03 12:46:58 +0100502template <bool is_range, bool do_assignability_check>
Vladimir Markod16363a2017-02-01 14:09:13 +0000503static ALWAYS_INLINE bool DoCallCommon(ArtMethod* called_method,
504 Thread* self,
505 ShadowFrame& shadow_frame,
506 JValue* result,
507 uint16_t number_of_inputs,
508 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
509 uint32_t vregC) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100510
511template <bool is_range>
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000512ALWAYS_INLINE void CopyRegisters(ShadowFrame& caller_frame,
513 ShadowFrame* callee_frame,
514 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
515 const size_t first_src_reg,
516 const size_t first_dest_reg,
517 const size_t num_regs) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100518
519// END DECLARATIONS.
520
Siva Chandra05d24152016-01-05 17:43:17 -0800521void ArtInterpreterToCompiledCodeBridge(Thread* self,
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100522 ArtMethod* caller,
Siva Chandra05d24152016-01-05 17:43:17 -0800523 ShadowFrame* shadow_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -0700524 uint16_t arg_offset,
Siva Chandra05d24152016-01-05 17:43:17 -0800525 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700526 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700527 ArtMethod* method = shadow_frame->GetMethod();
528 // Ensure static methods are initialized.
529 if (method->IsStatic()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700530 ObjPtr<mirror::Class> declaringClass = method->GetDeclaringClass();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700531 if (UNLIKELY(!declaringClass->IsInitialized())) {
532 self->PushShadowFrame(shadow_frame);
533 StackHandleScope<1> hs(self);
534 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
Nicolas Geoffray01822292017-03-09 09:03:19 +0000535 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
536 true))) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700537 self->PopShadowFrame();
538 DCHECK(self->IsExceptionPending());
539 return;
540 }
541 self->PopShadowFrame();
542 CHECK(h_class->IsInitializing());
Nicolas Geoffray01822292017-03-09 09:03:19 +0000543 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
544 method = shadow_frame->GetMethod();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700545 }
546 }
Jeff Hao5ea84132017-05-05 16:59:29 -0700547 // Basic checks for the arg_offset. If there's no code item, the arg_offset must be 0. Otherwise,
548 // check that the arg_offset isn't greater than the number of registers. A stronger check is
549 // difficult since the frame may contain space for all the registers in the method, or only enough
550 // space for the arguments.
551 if (kIsDebugBuild) {
552 if (method->GetCodeItem() == nullptr) {
553 DCHECK_EQ(0u, arg_offset) << method->PrettyMethod();
554 } else {
555 DCHECK_LE(arg_offset, shadow_frame->NumberOfVRegs());
556 }
557 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100558 jit::Jit* jit = Runtime::Current()->GetJit();
559 if (jit != nullptr && caller != nullptr) {
560 jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
561 }
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700562 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
563 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
Andreas Gampe542451c2016-07-26 09:02:02 -0700564 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700565}
566
Mingyao Yangffedec52016-05-19 10:48:40 -0700567void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
568 uint16_t this_obj_vreg,
569 JValue result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700570 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700571 ObjPtr<mirror::Object> existing = shadow_frame->GetVRegReference(this_obj_vreg);
Mingyao Yangffedec52016-05-19 10:48:40 -0700572 if (existing == nullptr) {
573 // If it's null, we come from compiled code that was deoptimized. Nothing to do,
574 // as the compiler verified there was no alias.
575 // Set the new string result of the StringFactory.
576 shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
577 return;
578 }
579 // Set the string init result into all aliases.
580 for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
581 if (shadow_frame->GetVRegReference(i) == existing) {
582 DCHECK_EQ(shadow_frame->GetVRegReference(i),
583 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
584 shadow_frame->SetVRegReference(i, result.GetL());
585 DCHECK_EQ(shadow_frame->GetVRegReference(i),
586 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
587 }
588 }
589}
590
Orion Hodsonc069a302017-01-18 09:23:12 +0000591template<bool is_range>
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100592static bool DoMethodHandleInvokeCommon(Thread* self,
593 ShadowFrame& shadow_frame,
594 bool invoke_exact,
595 const Instruction* inst,
596 uint16_t inst_data,
597 JValue* result)
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100598 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Light848574c2017-09-25 16:59:39 -0700599 // Make sure to check for async exceptions
600 if (UNLIKELY(self->ObserveAsyncException())) {
601 return false;
602 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100603 // Invoke-polymorphic instructions always take a receiver. i.e, they are never static.
604 const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc();
Orion Hodson3d617ac2016-10-19 14:00:46 +0100605 const int invoke_method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc();
Narayan Kamath9823e782016-08-03 12:46:58 +0100606
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000607 // Initialize |result| to 0 as this is the default return value for
608 // polymorphic invocations of method handle types with void return
609 // and provides sane return result in error cases.
610 result->SetJ(0);
611
Orion Hodson3d617ac2016-10-19 14:00:46 +0100612 // The invoke_method_idx here is the name of the signature polymorphic method that
Narayan Kamath9823e782016-08-03 12:46:58 +0100613 // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact)
614 // and not the method that we'll dispatch to in the end.
Orion Hodsone7732be2017-10-11 14:35:20 +0100615 StackHandleScope<2> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +0000616 Handle<mirror::MethodHandle> method_handle(hs.NewHandle(
617 ObjPtr<mirror::MethodHandle>::DownCast(
Mathieu Chartieref41db72016-10-25 15:08:01 -0700618 MakeObjPtr(shadow_frame.GetVRegReference(vRegC)))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800619 if (UNLIKELY(method_handle == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100620 // Note that the invoke type is kVirtual here because a call to a signature
621 // polymorphic method is shaped like a virtual call at the bytecode level.
Orion Hodson3d617ac2016-10-19 14:00:46 +0100622 ThrowNullPointerExceptionForMethodAccess(invoke_method_idx, InvokeType::kVirtual);
Narayan Kamath9823e782016-08-03 12:46:58 +0100623 return false;
624 }
625
626 // The vRegH value gives the index of the proto_id associated with this
Orion Hodson811bd5f2016-12-07 11:35:37 +0000627 // signature polymorphic call site.
Narayan Kamath9823e782016-08-03 12:46:58 +0100628 const uint32_t callsite_proto_id = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
629
630 // Call through to the classlinker and ask it to resolve the static type associated
631 // with the callsite. This information is stored in the dex cache so it's
632 // guaranteed to be fast after the first resolution.
Narayan Kamath9823e782016-08-03 12:46:58 +0100633 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Orion Hodsone7732be2017-10-11 14:35:20 +0100634 Handle<mirror::MethodType> callsite_type(hs.NewHandle(
635 class_linker->ResolveMethodType(self, callsite_proto_id, shadow_frame.GetMethod())));
Narayan Kamath9823e782016-08-03 12:46:58 +0100636
637 // This implies we couldn't resolve one or more types in this method handle.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800638 if (UNLIKELY(callsite_type == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100639 CHECK(self->IsExceptionPending());
Narayan Kamath9823e782016-08-03 12:46:58 +0100640 return false;
641 }
642
Orion Hodson811bd5f2016-12-07 11:35:37 +0000643 // There is a common dispatch method for method handles that takes
644 // arguments either from a range or an array of arguments depending
645 // on whether the DEX instruction is invoke-polymorphic/range or
646 // invoke-polymorphic. The array here is for the latter.
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100647 if (UNLIKELY(is_range)) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000648 // VRegC is the register holding the method handle. Arguments passed
649 // to the method handle's target do not include the method handle.
Orion Hodson960d4f72017-11-10 15:32:38 +0000650 RangeInstructionOperands operands(inst->VRegC_4rcc() + 1, inst->VRegA_4rcc() - 1);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100651 if (invoke_exact) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000652 return MethodHandleInvokeExact(self,
653 shadow_frame,
654 method_handle,
655 callsite_type,
656 &operands,
657 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100658 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000659 return MethodHandleInvoke(self,
660 shadow_frame,
661 method_handle,
662 callsite_type,
663 &operands,
664 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100665 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100666 } else {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000667 // Get the register arguments for the invoke.
Orion Hodson960d4f72017-11-10 15:32:38 +0000668 uint32_t args[Instruction::kMaxVarArgRegs] = {};
Orion Hodson811bd5f2016-12-07 11:35:37 +0000669 inst->GetVarArgs(args, inst_data);
670 // Drop the first register which is the method handle performing the invoke.
Alexey Frunze8631a462017-01-19 19:07:37 -0800671 memmove(args, args + 1, sizeof(args[0]) * (Instruction::kMaxVarArgRegs - 1));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000672 args[Instruction::kMaxVarArgRegs - 1] = 0;
Orion Hodson960d4f72017-11-10 15:32:38 +0000673 VarArgsInstructionOperands operands(args, inst->VRegA_45cc() - 1);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100674 if (invoke_exact) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000675 return MethodHandleInvokeExact(self,
676 shadow_frame,
677 method_handle,
678 callsite_type,
679 &operands,
680 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100681 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000682 return MethodHandleInvoke(self,
683 shadow_frame,
684 method_handle,
685 callsite_type,
686 &operands,
687 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100688 }
689 }
690}
691
692bool DoMethodHandleInvokeExact(Thread* self,
693 ShadowFrame& shadow_frame,
694 const Instruction* inst,
695 uint16_t inst_data,
696 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
697 if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) {
698 static const bool kIsRange = false;
699 return DoMethodHandleInvokeCommon<kIsRange>(
700 self, shadow_frame, true /* is_exact */, inst, inst_data, result);
701 } else {
702 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE);
703 static const bool kIsRange = true;
704 return DoMethodHandleInvokeCommon<kIsRange>(
705 self, shadow_frame, true /* is_exact */, inst, inst_data, result);
706 }
707}
708
709bool DoMethodHandleInvoke(Thread* self,
710 ShadowFrame& shadow_frame,
711 const Instruction* inst,
712 uint16_t inst_data,
713 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
714 if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) {
715 static const bool kIsRange = false;
716 return DoMethodHandleInvokeCommon<kIsRange>(
717 self, shadow_frame, false /* is_exact */, inst, inst_data, result);
718 } else {
719 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE);
720 static const bool kIsRange = true;
721 return DoMethodHandleInvokeCommon<kIsRange>(
722 self, shadow_frame, false /* is_exact */, inst, inst_data, result);
723 }
724}
725
726static bool UnimplementedSignaturePolymorphicMethod(Thread* self ATTRIBUTE_UNUSED,
727 ShadowFrame& shadow_frame ATTRIBUTE_UNUSED,
728 const Instruction* inst ATTRIBUTE_UNUSED,
729 uint16_t inst_data ATTRIBUTE_UNUSED,
730 JValue* result ATTRIBUTE_UNUSED)
731 REQUIRES_SHARED(Locks::mutator_lock_) {
732 UNIMPLEMENTED(FATAL) << "TODO(oth): b/65872996";
733 return false;
734}
735
736bool DoVarHandleCompareAndExchange(Thread* self,
737 ShadowFrame& shadow_frame,
738 const Instruction* inst,
739 uint16_t inst_data,
740 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
741 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
742}
743
744bool DoVarHandleCompareAndExchangeAcquire(Thread* self,
745 ShadowFrame& shadow_frame,
746 const Instruction* inst,
747 uint16_t inst_data,
748 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
749 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
750}
751
752bool DoVarHandleCompareAndExchangeRelease(Thread* self,
753 ShadowFrame& shadow_frame,
754 const Instruction* inst,
755 uint16_t inst_data,
756 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
757 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
758}
759
760bool DoVarHandleCompareAndSet(Thread* self,
761 ShadowFrame& shadow_frame,
762 const Instruction* inst,
763 uint16_t inst_data,
764 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
765 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
766}
767
768bool DoVarHandleGet(Thread* self,
769 ShadowFrame& shadow_frame,
770 const Instruction* inst,
771 uint16_t inst_data,
772 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
773 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
774}
775
776bool DoVarHandleGetAcquire(Thread* self,
777 ShadowFrame& shadow_frame,
778 const Instruction* inst,
779 uint16_t inst_data,
780 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
781 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
782}
783
784bool DoVarHandleGetAndAdd(Thread* self,
785 ShadowFrame& shadow_frame,
786 const Instruction* inst,
787 uint16_t inst_data,
788 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
789 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
790}
791
792bool DoVarHandleGetAndAddAcquire(Thread* self,
793 ShadowFrame& shadow_frame,
794 const Instruction* inst,
795 uint16_t inst_data,
796 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
797 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
798}
799
800bool DoVarHandleGetAndAddRelease(Thread* self,
801 ShadowFrame& shadow_frame,
802 const Instruction* inst,
803 uint16_t inst_data,
804 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
805 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
806}
807
808bool DoVarHandleGetAndBitwiseAnd(Thread* self,
809 ShadowFrame& shadow_frame,
810 const Instruction* inst,
811 uint16_t inst_data,
812 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
813 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
814}
815
816bool DoVarHandleGetAndBitwiseAndAcquire(Thread* self,
817 ShadowFrame& shadow_frame,
818 const Instruction* inst,
819 uint16_t inst_data,
820 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
821 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
822}
823
824bool DoVarHandleGetAndBitwiseAndRelease(Thread* self,
825 ShadowFrame& shadow_frame,
826 const Instruction* inst,
827 uint16_t inst_data,
828 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
829 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
830}
831
832bool DoVarHandleGetAndBitwiseOr(Thread* self,
833 ShadowFrame& shadow_frame,
834 const Instruction* inst,
835 uint16_t inst_data,
836 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
837 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
838}
839
840bool DoVarHandleGetAndBitwiseOrAcquire(Thread* self,
841 ShadowFrame& shadow_frame,
842 const Instruction* inst,
843 uint16_t inst_data,
844 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
845 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
846}
847
848bool DoVarHandleGetAndBitwiseOrRelease(Thread* self,
849 ShadowFrame& shadow_frame,
850 const Instruction* inst,
851 uint16_t inst_data,
852 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
853 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
854}
855
856bool DoVarHandleGetAndBitwiseXor(Thread* self,
857 ShadowFrame& shadow_frame,
858 const Instruction* inst,
859 uint16_t inst_data,
860 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
861 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
862}
863
864bool DoVarHandleGetAndBitwiseXorAcquire(Thread* self,
865 ShadowFrame& shadow_frame,
866 const Instruction* inst,
867 uint16_t inst_data,
868 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
869 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
870}
871
872bool DoVarHandleGetAndBitwiseXorRelease(Thread* self,
873 ShadowFrame& shadow_frame,
874 const Instruction* inst,
875 uint16_t inst_data,
876 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
877 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
878}
879
880bool DoVarHandleGetAndSet(Thread* self,
881 ShadowFrame& shadow_frame,
882 const Instruction* inst,
883 uint16_t inst_data,
884 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
885 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
886}
887
888bool DoVarHandleGetAndSetAcquire(Thread* self,
889 ShadowFrame& shadow_frame,
890 const Instruction* inst,
891 uint16_t inst_data,
892 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
893 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
894}
895
896bool DoVarHandleGetAndSetRelease(Thread* self,
897 ShadowFrame& shadow_frame,
898 const Instruction* inst,
899 uint16_t inst_data,
900 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
901 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
902}
903
904bool DoVarHandleGetOpaque(Thread* self,
905 ShadowFrame& shadow_frame,
906 const Instruction* inst,
907 uint16_t inst_data,
908 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
909 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
910}
911
912bool DoVarHandleGetVolatile(Thread* self,
913 ShadowFrame& shadow_frame,
914 const Instruction* inst,
915 uint16_t inst_data,
916 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
917 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
918}
919
920bool DoVarHandleSet(Thread* self,
921 ShadowFrame& shadow_frame,
922 const Instruction* inst,
923 uint16_t inst_data,
924 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
925 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
926}
927
928bool DoVarHandleSetOpaque(Thread* self,
929 ShadowFrame& shadow_frame,
930 const Instruction* inst,
931 uint16_t inst_data,
932 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
933 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
934}
935
936bool DoVarHandleSetRelease(Thread* self,
937 ShadowFrame& shadow_frame,
938 const Instruction* inst,
939 uint16_t inst_data,
940 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
941 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
942}
943
944bool DoVarHandleSetVolatile(Thread* self,
945 ShadowFrame& shadow_frame,
946 const Instruction* inst,
947 uint16_t inst_data,
948 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
949 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
950}
951
952bool DoVarHandleWeakCompareAndSet(Thread* self,
953 ShadowFrame& shadow_frame,
954 const Instruction* inst,
955 uint16_t inst_data,
956 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
957 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
958}
959
960bool DoVarHandleWeakCompareAndSetAcquire(Thread* self,
961 ShadowFrame& shadow_frame,
962 const Instruction* inst,
963 uint16_t inst_data,
964 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
965 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
966}
967
968bool DoVarHandleWeakCompareAndSetPlain(Thread* self,
969 ShadowFrame& shadow_frame,
970 const Instruction* inst,
971 uint16_t inst_data,
972 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
973 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
974}
975
976bool DoVarHandleWeakCompareAndSetRelease(Thread* self,
977 ShadowFrame& shadow_frame,
978 const Instruction* inst,
979 uint16_t inst_data,
980 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
981 return UnimplementedSignaturePolymorphicMethod(self, shadow_frame, inst, inst_data, result);
982}
983
984template<bool is_range>
985bool DoInvokePolymorphic(Thread* self,
986 ShadowFrame& shadow_frame,
987 const Instruction* inst,
988 uint16_t inst_data,
989 JValue* result) {
990 const int invoke_method_idx = inst->VRegB();
991 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
992 ArtMethod* invoke_method =
993 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
994 self, invoke_method_idx, shadow_frame.GetMethod(), kVirtual);
995
996 // Ensure intrinsic identifiers are initialized.
997 DCHECK(invoke_method->IsIntrinsic());
998
999 // Dispatch based on intrinsic identifier associated with method.
1000 switch (static_cast<art::Intrinsics>(invoke_method->GetIntrinsic())) {
1001#define CASE_SIGNATURE_POLYMORPHIC_INTRINSIC(Name, ...) \
1002 case Intrinsics::k##Name: \
1003 return Do ## Name(self, shadow_frame, inst, inst_data, result);
1004#include "intrinsics_list.h"
1005 SIGNATURE_POLYMORPHIC_INTRINSICS_LIST(CASE_SIGNATURE_POLYMORPHIC_INTRINSIC)
1006#undef INTRINSICS_LIST
1007#undef SIGNATURE_POLYMORPHIC_INTRINSICS_LIST
1008#undef CASE_SIGNATURE_POLYMORPHIC_INTRINSIC
1009 default:
1010 LOG(FATAL) << "Unreachable: " << invoke_method->GetIntrinsic();
1011 UNREACHABLE();
1012 return false;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001013 }
1014}
1015
Orion Hodsonc069a302017-01-18 09:23:12 +00001016static ObjPtr<mirror::CallSite> InvokeBootstrapMethod(Thread* self,
1017 ShadowFrame& shadow_frame,
1018 uint32_t call_site_idx)
1019 REQUIRES_SHARED(Locks::mutator_lock_) {
1020 ArtMethod* referrer = shadow_frame.GetMethod();
1021 const DexFile* dex_file = referrer->GetDexFile();
1022 const DexFile::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx);
1023
Orion Hodson631827d2017-04-10 14:53:47 +01001024 StackHandleScope<10> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +00001025 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader()));
1026 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
1027
1028 CallSiteArrayValueIterator it(*dex_file, csi);
1029 uint32_t method_handle_idx = static_cast<uint32_t>(it.GetJavaValue().i);
1030 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1031 Handle<mirror::MethodHandle>
Orion Hodsone7732be2017-10-11 14:35:20 +01001032 bootstrap(hs.NewHandle(class_linker->ResolveMethodHandle(self, method_handle_idx, referrer)));
Orion Hodsonc069a302017-01-18 09:23:12 +00001033 if (bootstrap.IsNull()) {
1034 DCHECK(self->IsExceptionPending());
1035 return nullptr;
1036 }
1037 Handle<mirror::MethodType> bootstrap_method_type = hs.NewHandle(bootstrap->GetMethodType());
1038 it.Next();
1039
1040 DCHECK_EQ(static_cast<size_t>(bootstrap->GetMethodType()->GetPTypes()->GetLength()), it.Size());
1041 const size_t num_bootstrap_vregs = bootstrap->GetMethodType()->NumberOfVRegs();
1042
1043 // Set-up a shadow frame for invoking the bootstrap method handle.
1044 ShadowFrameAllocaUniquePtr bootstrap_frame =
1045 CREATE_SHADOW_FRAME(num_bootstrap_vregs, nullptr, referrer, shadow_frame.GetDexPC());
1046 ScopedStackedShadowFramePusher pusher(
1047 self, bootstrap_frame.get(), StackedShadowFrameType::kShadowFrameUnderConstruction);
1048 size_t vreg = 0;
1049
1050 // The first parameter is a MethodHandles lookup instance.
1051 {
1052 Handle<mirror::Class> lookup_class(hs.NewHandle(bootstrap->GetTargetClass()));
1053 ObjPtr<mirror::MethodHandlesLookup> lookup =
1054 mirror::MethodHandlesLookup::Create(self, lookup_class);
1055 if (lookup.IsNull()) {
1056 DCHECK(self->IsExceptionPending());
1057 return nullptr;
1058 }
1059 bootstrap_frame->SetVRegReference(vreg++, lookup.Ptr());
1060 }
1061
1062 // The second parameter is the name to lookup.
1063 {
1064 dex::StringIndex name_idx(static_cast<uint32_t>(it.GetJavaValue().i));
Vladimir Markoa64b52d2017-12-08 16:27:49 +00001065 ObjPtr<mirror::String> name = class_linker->ResolveString(name_idx, dex_cache);
Orion Hodsonc069a302017-01-18 09:23:12 +00001066 if (name.IsNull()) {
1067 DCHECK(self->IsExceptionPending());
1068 return nullptr;
1069 }
1070 bootstrap_frame->SetVRegReference(vreg++, name.Ptr());
1071 }
1072 it.Next();
1073
1074 // The third parameter is the method type associated with the name.
1075 uint32_t method_type_idx = static_cast<uint32_t>(it.GetJavaValue().i);
Vladimir Markoaf940202017-12-08 15:01:18 +00001076 Handle<mirror::MethodType> method_type(hs.NewHandle(
1077 class_linker->ResolveMethodType(self, method_type_idx, dex_cache, class_loader)));
Orion Hodsonc069a302017-01-18 09:23:12 +00001078 if (method_type.IsNull()) {
1079 DCHECK(self->IsExceptionPending());
1080 return nullptr;
1081 }
1082 bootstrap_frame->SetVRegReference(vreg++, method_type.Get());
1083 it.Next();
1084
1085 // Append remaining arguments (if any).
1086 while (it.HasNext()) {
1087 const jvalue& jvalue = it.GetJavaValue();
1088 switch (it.GetValueType()) {
1089 case EncodedArrayValueIterator::ValueType::kBoolean:
1090 case EncodedArrayValueIterator::ValueType::kByte:
1091 case EncodedArrayValueIterator::ValueType::kChar:
1092 case EncodedArrayValueIterator::ValueType::kShort:
1093 case EncodedArrayValueIterator::ValueType::kInt:
1094 bootstrap_frame->SetVReg(vreg, jvalue.i);
1095 vreg += 1;
1096 break;
1097 case EncodedArrayValueIterator::ValueType::kLong:
1098 bootstrap_frame->SetVRegLong(vreg, jvalue.j);
1099 vreg += 2;
1100 break;
1101 case EncodedArrayValueIterator::ValueType::kFloat:
1102 bootstrap_frame->SetVRegFloat(vreg, jvalue.f);
1103 vreg += 1;
1104 break;
1105 case EncodedArrayValueIterator::ValueType::kDouble:
1106 bootstrap_frame->SetVRegDouble(vreg, jvalue.d);
1107 vreg += 2;
1108 break;
1109 case EncodedArrayValueIterator::ValueType::kMethodType: {
1110 uint32_t idx = static_cast<uint32_t>(jvalue.i);
1111 ObjPtr<mirror::MethodType> ref =
Vladimir Markoaf940202017-12-08 15:01:18 +00001112 class_linker->ResolveMethodType(self, idx, dex_cache, class_loader);
Orion Hodsonc069a302017-01-18 09:23:12 +00001113 if (ref.IsNull()) {
1114 DCHECK(self->IsExceptionPending());
1115 return nullptr;
1116 }
1117 bootstrap_frame->SetVRegReference(vreg, ref.Ptr());
1118 vreg += 1;
1119 break;
1120 }
1121 case EncodedArrayValueIterator::ValueType::kMethodHandle: {
1122 uint32_t idx = static_cast<uint32_t>(jvalue.i);
1123 ObjPtr<mirror::MethodHandle> ref =
Orion Hodsone7732be2017-10-11 14:35:20 +01001124 class_linker->ResolveMethodHandle(self, idx, referrer);
Orion Hodsonc069a302017-01-18 09:23:12 +00001125 if (ref.IsNull()) {
1126 DCHECK(self->IsExceptionPending());
1127 return nullptr;
1128 }
1129 bootstrap_frame->SetVRegReference(vreg, ref.Ptr());
1130 vreg += 1;
1131 break;
1132 }
1133 case EncodedArrayValueIterator::ValueType::kString: {
1134 dex::StringIndex idx(static_cast<uint32_t>(jvalue.i));
Vladimir Markoa64b52d2017-12-08 16:27:49 +00001135 ObjPtr<mirror::String> ref = class_linker->ResolveString(idx, dex_cache);
Orion Hodsonc069a302017-01-18 09:23:12 +00001136 if (ref.IsNull()) {
1137 DCHECK(self->IsExceptionPending());
1138 return nullptr;
1139 }
1140 bootstrap_frame->SetVRegReference(vreg, ref.Ptr());
1141 vreg += 1;
1142 break;
1143 }
1144 case EncodedArrayValueIterator::ValueType::kType: {
1145 dex::TypeIndex idx(static_cast<uint32_t>(jvalue.i));
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001146 ObjPtr<mirror::Class> ref = class_linker->ResolveType(idx, dex_cache, class_loader);
Orion Hodsonc069a302017-01-18 09:23:12 +00001147 if (ref.IsNull()) {
1148 DCHECK(self->IsExceptionPending());
1149 return nullptr;
1150 }
1151 bootstrap_frame->SetVRegReference(vreg, ref.Ptr());
1152 vreg += 1;
1153 break;
1154 }
1155 case EncodedArrayValueIterator::ValueType::kNull:
1156 bootstrap_frame->SetVRegReference(vreg, nullptr);
1157 vreg += 1;
1158 break;
1159 case EncodedArrayValueIterator::ValueType::kField:
1160 case EncodedArrayValueIterator::ValueType::kMethod:
1161 case EncodedArrayValueIterator::ValueType::kEnum:
1162 case EncodedArrayValueIterator::ValueType::kArray:
1163 case EncodedArrayValueIterator::ValueType::kAnnotation:
1164 // Unreachable based on current EncodedArrayValueIterator::Next().
1165 UNREACHABLE();
1166 }
1167
1168 it.Next();
1169 }
1170
1171 // Invoke the bootstrap method handle.
1172 JValue result;
Orion Hodson960d4f72017-11-10 15:32:38 +00001173 RangeInstructionOperands operands(0, vreg);
1174 bool invoke_success = MethodHandleInvokeExact(self,
1175 *bootstrap_frame,
1176 bootstrap,
1177 bootstrap_method_type,
1178 &operands,
1179 &result);
Orion Hodsonc069a302017-01-18 09:23:12 +00001180 if (!invoke_success) {
1181 DCHECK(self->IsExceptionPending());
1182 return nullptr;
1183 }
1184
1185 Handle<mirror::Object> object(hs.NewHandle(result.GetL()));
1186
1187 // Check the result is not null.
1188 if (UNLIKELY(object.IsNull())) {
1189 ThrowNullPointerException("CallSite == null");
1190 return nullptr;
1191 }
1192
1193 // Check the result type is a subclass of CallSite.
1194 if (UNLIKELY(!object->InstanceOf(mirror::CallSite::StaticClass()))) {
1195 ThrowClassCastException(object->GetClass(), mirror::CallSite::StaticClass());
1196 return nullptr;
1197 }
1198
1199 Handle<mirror::CallSite> call_site =
1200 hs.NewHandle(ObjPtr<mirror::CallSite>::DownCast(ObjPtr<mirror::Object>(result.GetL())));
1201
1202 // Check the call site target is not null as we're going to invoke it.
1203 Handle<mirror::MethodHandle> target = hs.NewHandle(call_site->GetTarget());
1204 if (UNLIKELY(target.IsNull())) {
1205 ThrowNullPointerException("CallSite target == null");
1206 return nullptr;
1207 }
1208
Orion Hodson631827d2017-04-10 14:53:47 +01001209 // Check the target method type matches the method type requested modulo the receiver
1210 // needs to be compatible rather than exact.
1211 Handle<mirror::MethodType> target_method_type = hs.NewHandle(target->GetMethodType());
1212 if (UNLIKELY(!target_method_type->IsExactMatch(method_type.Get()) &&
1213 !IsParameterTypeConvertible(target_method_type->GetPTypes()->GetWithoutChecks(0),
1214 method_type->GetPTypes()->GetWithoutChecks(0)))) {
1215 ThrowWrongMethodTypeException(target_method_type.Get(), method_type.Get());
Orion Hodsonc069a302017-01-18 09:23:12 +00001216 return nullptr;
1217 }
1218
1219 return call_site.Get();
1220}
1221
1222template<bool is_range>
1223bool DoInvokeCustom(Thread* self,
1224 ShadowFrame& shadow_frame,
1225 const Instruction* inst,
1226 uint16_t inst_data,
1227 JValue* result)
1228 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Light848574c2017-09-25 16:59:39 -07001229 // Make sure to check for async exceptions
1230 if (UNLIKELY(self->ObserveAsyncException())) {
1231 return false;
1232 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001233 // invoke-custom is not supported in transactions. In transactions
1234 // there is a limited set of types supported. invoke-custom allows
1235 // running arbitrary code and instantiating arbitrary types.
1236 CHECK(!Runtime::Current()->IsActiveTransaction());
1237 StackHandleScope<4> hs(self);
1238 Handle<mirror::DexCache> dex_cache(hs.NewHandle(shadow_frame.GetMethod()->GetDexCache()));
1239 const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
1240 MutableHandle<mirror::CallSite>
1241 call_site(hs.NewHandle(dex_cache->GetResolvedCallSite(call_site_idx)));
1242 if (call_site.IsNull()) {
1243 call_site.Assign(InvokeBootstrapMethod(self, shadow_frame, call_site_idx));
1244 if (UNLIKELY(call_site.IsNull())) {
1245 CHECK(self->IsExceptionPending());
1246 ThrowWrappedBootstrapMethodError("Exception from call site #%u bootstrap method",
1247 call_site_idx);
1248 result->SetJ(0);
1249 return false;
1250 }
1251 mirror::CallSite* winning_call_site =
1252 dex_cache->SetResolvedCallSite(call_site_idx, call_site.Get());
1253 call_site.Assign(winning_call_site);
1254 }
1255
1256 // CallSite.java checks the re-assignment of the call site target
1257 // when mutating call site targets. We only check the target is
1258 // non-null and has the right type during bootstrap method execution.
1259 Handle<mirror::MethodHandle> target = hs.NewHandle(call_site->GetTarget());
1260 Handle<mirror::MethodType> target_method_type = hs.NewHandle(target->GetMethodType());
1261 DCHECK_EQ(static_cast<size_t>(inst->VRegA()), target_method_type->NumberOfVRegs());
Orion Hodsonc069a302017-01-18 09:23:12 +00001262 if (is_range) {
Orion Hodson960d4f72017-11-10 15:32:38 +00001263 RangeInstructionOperands operands(inst->VRegC_3rc(), inst->VRegA_3rc());
1264 return MethodHandleInvokeExact(self,
1265 shadow_frame,
1266 target,
1267 target_method_type,
1268 &operands,
1269 result);
Orion Hodsonc069a302017-01-18 09:23:12 +00001270 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +00001271 uint32_t args[Instruction::kMaxVarArgRegs];
Orion Hodsonc069a302017-01-18 09:23:12 +00001272 inst->GetVarArgs(args, inst_data);
Orion Hodson960d4f72017-11-10 15:32:38 +00001273 VarArgsInstructionOperands operands(args, inst->VRegA_35c());
1274 return MethodHandleInvokeExact(self,
1275 shadow_frame,
1276 target,
1277 target_method_type,
1278 &operands,
1279 result);
Orion Hodsonc069a302017-01-18 09:23:12 +00001280 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001281}
1282
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001283template <bool is_range>
1284inline void CopyRegisters(ShadowFrame& caller_frame,
1285 ShadowFrame* callee_frame,
1286 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
1287 const size_t first_src_reg,
1288 const size_t first_dest_reg,
1289 const size_t num_regs) {
1290 if (is_range) {
1291 const size_t dest_reg_bound = first_dest_reg + num_regs;
1292 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound;
1293 ++dest_reg, ++src_reg) {
1294 AssignRegister(callee_frame, caller_frame, dest_reg, src_reg);
1295 }
1296 } else {
1297 DCHECK_LE(num_regs, arraysize(arg));
1298
1299 for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) {
1300 AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]);
1301 }
1302 }
1303}
1304
Igor Murashkin6918bf12015-09-27 19:19:06 -07001305template <bool is_range,
Narayan Kamath370423d2016-10-03 16:51:22 +01001306 bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001307static inline bool DoCallCommon(ArtMethod* called_method,
1308 Thread* self,
1309 ShadowFrame& shadow_frame,
1310 JValue* result,
1311 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +01001312 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -07001313 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001314 bool string_init = false;
1315 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001316 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
1317 && called_method->IsConstructor())) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001318 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
Jeff Hao848f70a2014-01-15 13:49:50 -08001319 string_init = true;
1320 }
1321
Alex Lightdaf58c82016-03-16 23:00:49 +00001322 // Compute method information.
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001323 CodeItemDataAccessor accessor(called_method);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001324 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001325 uint16_t num_regs;
Jeff Hao5ea84132017-05-05 16:59:29 -07001326 // Test whether to use the interpreter or compiler entrypoint, and save that result to pass to
1327 // PerformCall. A deoptimization could occur at any time, and we shouldn't change which
1328 // entrypoint to use once we start building the shadow frame.
Mathieu Chartier448bbcf2017-07-06 12:05:13 -07001329
1330 // For unstarted runtimes, always use the interpreter entrypoint. This fixes the case where we are
1331 // doing cross compilation. Note that GetEntryPointFromQuickCompiledCode doesn't use the image
1332 // pointer size here and this may case an overflow if it is called from the compiler. b/62402160
1333 const bool use_interpreter_entrypoint = !Runtime::Current()->IsStarted() ||
1334 ClassLinker::ShouldUseInterpreterEntrypoint(
1335 called_method,
1336 called_method->GetEntryPointFromQuickCompiledCode());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001337 if (LIKELY(accessor.HasCodeItem())) {
Jeff Hao5ea84132017-05-05 16:59:29 -07001338 // When transitioning to compiled code, space only needs to be reserved for the input registers.
1339 // The rest of the frame gets discarded. This also prevents accessing the called method's code
1340 // item, saving memory by keeping code items of compiled code untouched.
Mathieu Chartier448bbcf2017-07-06 12:05:13 -07001341 if (!use_interpreter_entrypoint) {
1342 DCHECK(!Runtime::Current()->IsAotCompiler()) << "Compiler should use interpreter entrypoint";
Jeff Hao5ea84132017-05-05 16:59:29 -07001343 num_regs = number_of_inputs;
1344 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001345 num_regs = accessor.RegistersSize();
1346 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, accessor.InsSize());
Jeff Hao5ea84132017-05-05 16:59:29 -07001347 }
Nicolas Geoffray01822292017-03-09 09:03:19 +00001348 } else {
1349 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1350 num_regs = number_of_inputs;
Jeff Haodf79ddb2017-02-27 14:47:06 -08001351 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001352
Igor Murashkin158f35c2015-06-10 15:55:30 -07001353 // Hack for String init:
1354 //
1355 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
1356 // invoke-x StringFactory(a, b, c, ...)
1357 // by effectively dropping the first virtual register from the invoke.
1358 //
1359 // (at this point the ArtMethod has already been replaced,
1360 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +00001361 //
1362 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
1363 // to handle the compiler optimization of replacing `this` with null without
1364 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001365 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -07001366 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001367 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 -07001368
Igor Murashkin158f35c2015-06-10 15:55:30 -07001369 // The new StringFactory call is static and has one fewer argument.
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001370 if (!accessor.HasCodeItem()) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001371 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1372 num_regs--;
1373 } // 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 -07001374 number_of_inputs--;
1375
1376 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -07001377 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001378 arg[i - 1] = arg[i];
1379 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07001380 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001381
1382 // Rewrite the non-var-arg case
1383 vregC++; // Skips the 0th vreg in the range ("this").
1384 }
1385
1386 // Parameter registers go at the end of the shadow frame.
1387 DCHECK_GE(num_regs, number_of_inputs);
1388 size_t first_dest_reg = num_regs - number_of_inputs;
1389 DCHECK_NE(first_dest_reg, (size_t)-1);
1390
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001391 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001392 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -07001393 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -07001394 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -07001395 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001396
Igor Murashkin158f35c2015-06-10 15:55:30 -07001397 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -07001398 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001399 // Slow path.
1400 // We might need to do class loading, which incurs a thread state change to kNative. So
1401 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07001402 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +02001403 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001404 self->EndAssertNoThreadSuspension(old_cause);
1405
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001406 // ArtMethod here is needed to check type information of the call site against the callee.
1407 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
1408 //
1409 // As a special case for proxy methods, which are not dex-backed,
1410 // we have to retrieve type information from the proxy's method
1411 // interface method instead (which is dex backed since proxies are never interfaces).
Andreas Gampe542451c2016-07-26 09:02:02 -07001412 ArtMethod* method =
1413 new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001414
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001415 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001416 // to get the exact type of each reference argument.
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001417 const DexFile::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001418 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001419 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001420
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001421 // Handle receiver apart since it's not part of the shorty.
1422 size_t dest_reg = first_dest_reg;
1423 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001424
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001425 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001426 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001427 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
1428 ++dest_reg;
1429 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -07001430 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001431 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001432
1433 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001434 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001435 // Skip the 0th 'shorty' type since it represents the return type.
1436 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001437 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
1438 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001439 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001440 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001441 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001442 if (do_assignability_check && o != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001443 const dex::TypeIndex type_idx = params->GetTypeItem(shorty_pos).type_idx_;
Vladimir Marko942fd312017-01-16 20:52:19 +00001444 ObjPtr<mirror::Class> arg_type = method->GetDexCache()->GetResolvedType(type_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001445 if (arg_type == nullptr) {
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001446 StackHandleScope<1> hs(self);
1447 // Preserve o since it is used below and GetClassFromTypeIndex may cause thread
1448 // suspension.
1449 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&o);
Vladimir Markob45528c2017-07-27 14:14:28 +01001450 arg_type = method->ResolveClassFromTypeIndex(type_idx);
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001451 if (arg_type == nullptr) {
1452 CHECK(self->IsExceptionPending());
1453 return false;
1454 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001455 }
1456 if (!o->VerifierInstanceOf(arg_type)) {
1457 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -07001458 std::string temp1, temp2;
Orion Hodsonfef06642016-11-25 16:07:11 +00001459 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001460 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -08001461 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -07001462 o->GetClass()->GetDescriptor(&temp1),
1463 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001464 return false;
1465 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001466 }
Mathieu Chartieref41db72016-10-25 15:08:01 -07001467 new_shadow_frame->SetVRegReference(dest_reg, o.Ptr());
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001468 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -07001469 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001470 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001471 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001472 uint64_t wide_value =
1473 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
1474 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001475 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001476 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001477 ++dest_reg;
1478 ++arg_offset;
1479 break;
1480 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001481 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001482 default:
1483 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
1484 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001485 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001486 }
1487 } else {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001488 if (is_range) {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001489 DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001490 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001491
1492 CopyRegisters<is_range>(shadow_frame,
1493 new_shadow_frame,
1494 arg,
1495 vregC,
1496 first_dest_reg,
1497 number_of_inputs);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001498 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001499 }
1500
Jeff Hao5ea84132017-05-05 16:59:29 -07001501 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001502 accessor,
Jeff Hao5ea84132017-05-05 16:59:29 -07001503 shadow_frame.GetMethod(),
1504 first_dest_reg,
1505 new_shadow_frame,
1506 result,
1507 use_interpreter_entrypoint);
Jeff Hao848f70a2014-01-15 13:49:50 -08001508
1509 if (string_init && !self->IsExceptionPending()) {
Mingyao Yangffedec52016-05-19 10:48:40 -07001510 SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001511 }
1512
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001513 return !self->IsExceptionPending();
1514}
1515
Igor Murashkin158f35c2015-06-10 15:55:30 -07001516template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001517bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
1518 const Instruction* inst, uint16_t inst_data, JValue* result) {
1519 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001520 const uint16_t number_of_inputs =
1521 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001522
1523 // TODO: find a cleaner way to separate non-range and range information without duplicating
1524 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -07001525 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001526 uint32_t vregC = 0;
1527 if (is_range) {
1528 vregC = inst->VRegC_3rc();
1529 } else {
1530 vregC = inst->VRegC_35c();
1531 inst->GetVarArgs(arg, inst_data);
1532 }
1533
1534 return DoCallCommon<is_range, do_assignability_check>(
1535 called_method, self, shadow_frame,
1536 result, number_of_inputs, arg, vregC);
1537}
1538
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001539template <bool is_range, bool do_access_check, bool transaction_active>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001540bool DoFilledNewArray(const Instruction* inst,
1541 const ShadowFrame& shadow_frame,
1542 Thread* self,
1543 JValue* result) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001544 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
1545 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
1546 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
1547 if (!is_range) {
1548 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
1549 CHECK_LE(length, 5);
1550 }
1551 if (UNLIKELY(length < 0)) {
1552 ThrowNegativeArraySizeException(length);
1553 return false;
1554 }
1555 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08001556 ObjPtr<mirror::Class> array_class = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
Mathieu Chartieref41db72016-10-25 15:08:01 -07001557 shadow_frame.GetMethod(),
1558 self,
1559 false,
1560 do_access_check);
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001561 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001562 DCHECK(self->IsExceptionPending());
1563 return false;
1564 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001565 CHECK(array_class->IsArrayClass());
Mathieu Chartieref41db72016-10-25 15:08:01 -07001566 ObjPtr<mirror::Class> component_class = array_class->GetComponentType();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001567 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
1568 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
1569 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001570 ThrowRuntimeException("Bad filled array request for type %s",
David Sehr709b0702016-10-13 09:12:37 -07001571 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001572 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001573 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -08001574 "Found type %s; filled-new-array not implemented for anything but 'int'",
David Sehr709b0702016-10-13 09:12:37 -07001575 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001576 }
1577 return false;
1578 }
Mathieu Chartieref41db72016-10-25 15:08:01 -07001579 ObjPtr<mirror::Object> new_array = mirror::Array::Alloc<true>(
1580 self,
1581 array_class,
1582 length,
1583 array_class->GetComponentSizeShift(),
1584 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001585 if (UNLIKELY(new_array == nullptr)) {
1586 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001587 return false;
1588 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001589 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
1590 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001591 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +01001592 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001593 } else {
Ian Rogers29a26482014-05-02 15:27:29 -07001594 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +01001595 }
Sebastien Hertzabff6432014-01-27 18:01:39 +01001596 for (int32_t i = 0; i < length; ++i) {
1597 size_t src_reg = is_range ? vregC + i : arg[i];
1598 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001599 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
1600 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +01001601 } else {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001602 new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<transaction_active>(
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001603 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001604 }
1605 }
1606
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001607 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001608 return true;
1609}
1610
Mathieu Chartieref41db72016-10-25 15:08:01 -07001611// TODO: Use ObjPtr here.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001612template<typename T>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001613static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array,
1614 int32_t count)
1615 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001616 Runtime* runtime = Runtime::Current();
1617 for (int32_t i = 0; i < count; ++i) {
1618 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
1619 }
1620}
1621
Mathieu Chartieref41db72016-10-25 15:08:01 -07001622void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001623 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001624 DCHECK(Runtime::Current()->IsActiveTransaction());
1625 DCHECK(array != nullptr);
1626 DCHECK_LE(count, array->GetLength());
1627 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
1628 switch (primitive_component_type) {
1629 case Primitive::kPrimBoolean:
1630 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
1631 break;
1632 case Primitive::kPrimByte:
1633 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
1634 break;
1635 case Primitive::kPrimChar:
1636 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
1637 break;
1638 case Primitive::kPrimShort:
1639 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
1640 break;
1641 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001642 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
1643 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001644 case Primitive::kPrimFloat:
1645 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
1646 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001647 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001648 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
1649 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001650 case Primitive::kPrimDouble:
1651 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
1652 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001653 default:
1654 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
1655 << " in fill-array-data";
1656 break;
1657 }
1658}
1659
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001660// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +02001661#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001662 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001663 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
1664 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +02001665 const Instruction* inst, uint16_t inst_data, \
1666 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001667EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
1668EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
1669EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
1670EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
1671#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001672
Orion Hodsonc069a302017-01-18 09:23:12 +00001673// Explicit DoInvokePolymorphic template function declarations.
1674#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range) \
1675 template REQUIRES_SHARED(Locks::mutator_lock_) \
1676 bool DoInvokePolymorphic<_is_range>( \
1677 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1678 uint16_t inst_data, JValue* result)
1679EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false);
1680EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true);
Narayan Kamath9823e782016-08-03 12:46:58 +01001681#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1682
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001683// Explicit DoInvokeCustom template function declarations.
1684#define EXPLICIT_DO_INVOKE_CUSTOM_TEMPLATE_DECL(_is_range) \
1685 template REQUIRES_SHARED(Locks::mutator_lock_) \
1686 bool DoInvokeCustom<_is_range>( \
1687 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1688 uint16_t inst_data, JValue* result)
1689EXPLICIT_DO_INVOKE_CUSTOM_TEMPLATE_DECL(false);
1690EXPLICIT_DO_INVOKE_CUSTOM_TEMPLATE_DECL(true);
1691#undef EXPLICIT_DO_INVOKE_CUSTOM_TEMPLATE_DECL
1692
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001693// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001694#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001695 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001696 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
1697 const ShadowFrame& shadow_frame, \
1698 Thread* self, JValue* result)
1699#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
1700 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
1701 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
1702 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
1703 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
1704EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
1705EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
1706#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001707#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
1708
1709} // namespace interpreter
1710} // namespace art