blob: 42609fb0a745debb1ed6d918dbac0e714e559359 [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
Vladimir Marko78baed52018-10-11 10:44:58 +010021#include "base/casts.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070022#include "base/enums.h"
Vladimir Markoc7aa87e2018-05-24 15:19:52 +010023#include "class_root.h"
Daniel Mihalyieb076692014-08-22 17:33:31 +020024#include "debugger.h"
David Sehr9e734c72018-01-04 17:56:19 -080025#include "dex/dex_file_types.h"
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +000026#include "entrypoints/runtime_asm_entrypoints.h"
Orion Hodson43f0cdb2017-10-10 14:47:32 +010027#include "intrinsics_enum.h"
Tamas Berghammerdd5e5e92016-02-12 16:29:00 +000028#include "jit/jit.h"
Andreas Gampec5b75642018-05-16 15:12:11 -070029#include "jvalue-inl.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010030#include "method_handles-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070031#include "method_handles.h"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070032#include "mirror/array-alloc-inl.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010033#include "mirror/array-inl.h"
Vladimir Marko621c8802019-03-27 16:18:18 +000034#include "mirror/call_site-inl.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010035#include "mirror/class.h"
Narayan Kamath000e1882016-10-24 17:14:25 +010036#include "mirror/emulated_stack_frame.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080037#include "mirror/method_handle_impl-inl.h"
Vladimir Marko5aead702019-03-27 11:00:36 +000038#include "mirror/method_type-inl.h"
Andreas Gampe52ecb652018-10-24 15:18:21 -070039#include "mirror/object_array-alloc-inl.h"
40#include "mirror/object_array-inl.h"
Orion Hodson928033d2018-02-07 05:30:54 +000041#include "mirror/var_handle.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010042#include "reflection-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070043#include "reflection.h"
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +010044#include "shadow_frame-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070045#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070046#include "thread-inl.h"
Chang Xingbd208d82017-07-12 14:53:17 -070047#include "transaction.h"
Orion Hodson537a4fe2018-05-15 13:57:58 +010048#include "var_handles.h"
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +010049#include "well_known_classes.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020050
51namespace art {
52namespace interpreter {
53
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000054void ThrowNullPointerExceptionFromInterpreter() {
55 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -070056}
57
David Srbecky960327b2018-10-25 10:11:59 +000058bool CheckStackOverflow(Thread* self, size_t frame_size)
59 REQUIRES_SHARED(Locks::mutator_lock_) {
60 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
61 uint8_t* stack_end = self->GetStackEndForInterpreter(implicit_check);
62 if (UNLIKELY(__builtin_frame_address(0) < stack_end + frame_size)) {
63 ThrowStackOverflowError(self);
64 return false;
65 }
66 return true;
67}
68
David Srbecky9581e612018-10-30 14:29:43 +000069bool UseFastInterpreterToInterpreterInvoke(ArtMethod* method) {
70 Runtime* runtime = Runtime::Current();
71 const void* quick_code = method->GetEntryPointFromQuickCompiledCode();
72 if (!runtime->GetClassLinker()->IsQuickToInterpreterBridge(quick_code)) {
73 return false;
74 }
75 if (!method->SkipAccessChecks() || method->IsNative() || method->IsProxyMethod()) {
76 return false;
77 }
78 if (method->IsIntrinsic()) {
79 return false;
80 }
81 if (method->GetDeclaringClass()->IsStringClass() && method->IsConstructor()) {
82 return false;
83 }
84 if (method->IsStatic() && !method->GetDeclaringClass()->IsInitialized()) {
85 return false;
86 }
87 ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
88 if ((profiling_info != nullptr) && (profiling_info->GetSavedEntryPoint() != nullptr)) {
89 return false;
90 }
91 return true;
92}
93
Chang Xingbd208d82017-07-12 14:53:17 -070094template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
95 bool transaction_active>
Ian Rogers54874942014-06-10 16:31:03 -070096bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
97 uint16_t inst_data) {
98 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
99 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100100 ArtField* f =
101 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
102 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -0700103 if (UNLIKELY(f == nullptr)) {
104 CHECK(self->IsExceptionPending());
105 return false;
106 }
Mathieu Chartieref41db72016-10-25 15:08:01 -0700107 ObjPtr<mirror::Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -0700108 if (is_static) {
109 obj = f->GetDeclaringClass();
Chang Xingbd208d82017-07-12 14:53:17 -0700110 if (transaction_active) {
111 if (Runtime::Current()->GetTransaction()->ReadConstraint(obj.Ptr(), f)) {
112 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Can't read static fields of "
113 + obj->PrettyTypeOf() + " since it does not belong to clinit's class.");
114 return false;
115 }
116 }
Ian Rogers54874942014-06-10 16:31:03 -0700117 } else {
118 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
119 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000120 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers54874942014-06-10 16:31:03 -0700121 return false;
122 }
123 }
Orion Hodson3d617ac2016-10-19 14:00:46 +0100124
125 JValue result;
Alex Light084fa372017-06-16 08:58:34 -0700126 if (UNLIKELY(!DoFieldGetCommon<field_type>(self, shadow_frame, obj, f, &result))) {
127 // Instrumentation threw an error!
128 CHECK(self->IsExceptionPending());
129 return false;
130 }
Ian Rogers54874942014-06-10 16:31:03 -0700131 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
132 switch (field_type) {
133 case Primitive::kPrimBoolean:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100134 shadow_frame.SetVReg(vregA, result.GetZ());
Ian Rogers54874942014-06-10 16:31:03 -0700135 break;
136 case Primitive::kPrimByte:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100137 shadow_frame.SetVReg(vregA, result.GetB());
Ian Rogers54874942014-06-10 16:31:03 -0700138 break;
139 case Primitive::kPrimChar:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100140 shadow_frame.SetVReg(vregA, result.GetC());
Ian Rogers54874942014-06-10 16:31:03 -0700141 break;
142 case Primitive::kPrimShort:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100143 shadow_frame.SetVReg(vregA, result.GetS());
Ian Rogers54874942014-06-10 16:31:03 -0700144 break;
145 case Primitive::kPrimInt:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100146 shadow_frame.SetVReg(vregA, result.GetI());
Ian Rogers54874942014-06-10 16:31:03 -0700147 break;
148 case Primitive::kPrimLong:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100149 shadow_frame.SetVRegLong(vregA, result.GetJ());
Ian Rogers54874942014-06-10 16:31:03 -0700150 break;
151 case Primitive::kPrimNot:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100152 shadow_frame.SetVRegReference(vregA, result.GetL());
Ian Rogers54874942014-06-10 16:31:03 -0700153 break;
154 default:
155 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700156 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700157 }
158 return true;
159}
160
161// Explicitly instantiate all DoFieldGet functions.
Chang Xingbd208d82017-07-12 14:53:17 -0700162#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
163 template bool DoFieldGet<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
Ian Rogers54874942014-06-10 16:31:03 -0700164 ShadowFrame& shadow_frame, \
165 const Instruction* inst, \
166 uint16_t inst_data)
167
168#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
Chang Xingbd208d82017-07-12 14:53:17 -0700169 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false, true); \
170 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false, false); \
171 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true, true); \
172 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true, false);
Ian Rogers54874942014-06-10 16:31:03 -0700173
174// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700175EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
176EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
177EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
178EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
179EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
180EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
181EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700182
183// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700184EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
185EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
186EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
187EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
188EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
189EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
190EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700191
192#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
193#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
194
195// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
196// Returns true on success, otherwise throws an exception and returns false.
197template<Primitive::Type field_type>
198bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700199 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700200 if (UNLIKELY(obj == nullptr)) {
201 // We lost the reference to the field index so we cannot get a more
202 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000203 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700204 return false;
205 }
206 MemberOffset field_offset(inst->VRegC_22c());
207 // Report this field access to instrumentation if needed. Since we only have the offset of
208 // the field from the base of the object, we need to look for it first.
209 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
210 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
211 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
212 field_offset.Uint32Value());
213 DCHECK(f != nullptr);
214 DCHECK(!f->IsStatic());
Alex Light084fa372017-06-16 08:58:34 -0700215 Thread* self = Thread::Current();
216 StackHandleScope<1> hs(self);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700217 // Save obj in case the instrumentation event has thread suspension.
218 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Alex Light084fa372017-06-16 08:58:34 -0700219 instrumentation->FieldReadEvent(self,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700220 obj.Ptr(),
221 shadow_frame.GetMethod(),
222 shadow_frame.GetDexPC(),
223 f);
Alex Light084fa372017-06-16 08:58:34 -0700224 if (UNLIKELY(self->IsExceptionPending())) {
225 return false;
226 }
Ian Rogers54874942014-06-10 16:31:03 -0700227 }
228 // Note: iget-x-quick instructions are only for non-volatile fields.
229 const uint32_t vregA = inst->VRegA_22c(inst_data);
230 switch (field_type) {
231 case Primitive::kPrimInt:
232 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
233 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800234 case Primitive::kPrimBoolean:
235 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
236 break;
237 case Primitive::kPrimByte:
238 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
239 break;
240 case Primitive::kPrimChar:
241 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
242 break;
243 case Primitive::kPrimShort:
244 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
245 break;
Ian Rogers54874942014-06-10 16:31:03 -0700246 case Primitive::kPrimLong:
247 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
248 break;
249 case Primitive::kPrimNot:
250 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
251 break;
252 default:
253 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700254 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700255 }
256 return true;
257}
258
259// Explicitly instantiate all DoIGetQuick functions.
260#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
261 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
262 uint16_t inst_data)
263
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800264EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
265EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
266EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
267EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
268EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
269EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
270EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700271#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
272
273template<Primitive::Type field_type>
274static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700275 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700276 JValue field_value;
277 switch (field_type) {
278 case Primitive::kPrimBoolean:
279 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
280 break;
281 case Primitive::kPrimByte:
282 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
283 break;
284 case Primitive::kPrimChar:
285 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
286 break;
287 case Primitive::kPrimShort:
288 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
289 break;
290 case Primitive::kPrimInt:
291 field_value.SetI(shadow_frame.GetVReg(vreg));
292 break;
293 case Primitive::kPrimLong:
294 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
295 break;
296 case Primitive::kPrimNot:
297 field_value.SetL(shadow_frame.GetVRegReference(vreg));
298 break;
299 default:
300 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700301 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700302 }
303 return field_value;
304}
305
Orion Hodson3d617ac2016-10-19 14:00:46 +0100306template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
307 bool transaction_active>
308bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
309 uint16_t inst_data) {
310 const bool do_assignability_check = do_access_check;
311 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
312 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
313 ArtField* f =
314 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
315 Primitive::ComponentSize(field_type));
316 if (UNLIKELY(f == nullptr)) {
317 CHECK(self->IsExceptionPending());
318 return false;
319 }
320 ObjPtr<mirror::Object> obj;
321 if (is_static) {
322 obj = f->GetDeclaringClass();
Chang Xingbd208d82017-07-12 14:53:17 -0700323 if (transaction_active) {
324 if (Runtime::Current()->GetTransaction()->WriteConstraint(obj.Ptr(), f)) {
325 Runtime::Current()->AbortTransactionAndThrowAbortError(
326 self, "Can't set fields of " + obj->PrettyTypeOf());
327 return false;
328 }
329 }
330
Orion Hodson3d617ac2016-10-19 14:00:46 +0100331 } else {
332 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
333 if (UNLIKELY(obj == nullptr)) {
334 ThrowNullPointerExceptionForFieldAccess(f, false);
335 return false;
336 }
337 }
338
339 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100340 JValue value = GetFieldValue<field_type>(shadow_frame, vregA);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100341 return DoFieldPutCommon<field_type, do_assignability_check, transaction_active>(self,
342 shadow_frame,
343 obj,
344 f,
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100345 value);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100346}
347
Ian Rogers54874942014-06-10 16:31:03 -0700348// Explicitly instantiate all DoFieldPut functions.
349#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
350 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
351 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
352
353#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
354 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
355 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
356 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
357 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
358
359// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700360EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
361EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
362EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
363EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
364EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
365EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
366EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700367
368// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700369EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
370EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
371EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
372EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
373EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
374EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
375EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700376
377#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
378#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
379
380template<Primitive::Type field_type, bool transaction_active>
381bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700382 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700383 if (UNLIKELY(obj == nullptr)) {
384 // We lost the reference to the field index so we cannot get a more
385 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000386 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700387 return false;
388 }
389 MemberOffset field_offset(inst->VRegC_22c());
390 const uint32_t vregA = inst->VRegA_22c(inst_data);
391 // Report this field modification to instrumentation if needed. Since we only have the offset of
392 // the field from the base of the object, we need to look for it first.
393 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
394 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
395 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
396 field_offset.Uint32Value());
397 DCHECK(f != nullptr);
398 DCHECK(!f->IsStatic());
399 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Alex Light084fa372017-06-16 08:58:34 -0700400 Thread* self = Thread::Current();
401 StackHandleScope<2> hs(self);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700402 // Save obj in case the instrumentation event has thread suspension.
403 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Alex Light084fa372017-06-16 08:58:34 -0700404 mirror::Object* fake_root = nullptr;
405 HandleWrapper<mirror::Object> ret(hs.NewHandleWrapper<mirror::Object>(
406 field_type == Primitive::kPrimNot ? field_value.GetGCRoot() : &fake_root));
407 instrumentation->FieldWriteEvent(self,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700408 obj.Ptr(),
409 shadow_frame.GetMethod(),
410 shadow_frame.GetDexPC(),
411 f,
412 field_value);
Alex Light084fa372017-06-16 08:58:34 -0700413 if (UNLIKELY(self->IsExceptionPending())) {
414 return false;
415 }
Alex Light0aa7a5a2018-10-10 15:58:14 +0000416 if (UNLIKELY(shadow_frame.GetForcePopFrame())) {
417 // Don't actually set the field. The next instruction will force us to pop.
418 DCHECK(Runtime::Current()->AreNonStandardExitsEnabled());
419 DCHECK(PrevFrameWillRetry(self, shadow_frame));
420 return true;
421 }
Ian Rogers54874942014-06-10 16:31:03 -0700422 }
423 // Note: iput-x-quick instructions are only for non-volatile fields.
424 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700425 case Primitive::kPrimBoolean:
426 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
427 break;
428 case Primitive::kPrimByte:
429 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
430 break;
431 case Primitive::kPrimChar:
432 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
433 break;
434 case Primitive::kPrimShort:
435 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
436 break;
Ian Rogers54874942014-06-10 16:31:03 -0700437 case Primitive::kPrimInt:
438 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
439 break;
440 case Primitive::kPrimLong:
441 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
442 break;
443 case Primitive::kPrimNot:
444 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
445 break;
446 default:
447 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700448 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700449 }
450 return true;
451}
452
453// Explicitly instantiate all DoIPutQuick functions.
454#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
455 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
456 const Instruction* inst, \
457 uint16_t inst_data)
458
459#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
460 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
461 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
462
Andreas Gampec8ccf682014-09-29 20:07:43 -0700463EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
464EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
465EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
466EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
467EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
468EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
469EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700470#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
471#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
472
Alex Light9fb1ab12017-09-05 09:32:49 -0700473// We execute any instrumentation events that are triggered by this exception and change the
474// shadow_frame's dex_pc to that of the exception handler if there is one in the current method.
475// Return true if we should continue executing in the current method and false if we need to go up
476// the stack to find an exception handler.
Sebastien Hertz520633b2015-09-08 17:03:36 +0200477// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Alex Light9fb1ab12017-09-05 09:32:49 -0700478// TODO We should have a better way to skip instrumentation reporting or possibly rethink that
479// behavior.
480bool MoveToExceptionHandler(Thread* self,
481 ShadowFrame& shadow_frame,
482 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700483 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700484 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000485 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Alex Light9fb1ab12017-09-05 09:32:49 -0700486 if (instrumentation != nullptr &&
487 instrumentation->HasExceptionThrownListeners() &&
488 self->IsExceptionThrownByCurrentMethod(exception.Get())) {
489 // 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 -0700490 instrumentation->ExceptionThrownEvent(self, exception.Get());
Alex Light0aa7a5a2018-10-10 15:58:14 +0000491 if (shadow_frame.GetForcePopFrame()) {
492 // We will check in the caller for GetForcePopFrame again. We need to bail out early to
493 // prevent an ExceptionHandledEvent from also being sent before popping.
494 return true;
495 }
Sebastien Hertz9f102032014-05-23 08:59:42 +0200496 }
Ian Rogers54874942014-06-10 16:31:03 -0700497 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700498 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
Alex Light9fb1ab12017-09-05 09:32:49 -0700499 hs.NewHandle(exception->GetClass()), shadow_frame.GetDexPC(), &clear_exception);
Andreas Gampee2abbc62017-09-15 11:59:26 -0700500 if (found_dex_pc == dex::kDexNoIndex) {
Alex Light9fb1ab12017-09-05 09:32:49 -0700501 if (instrumentation != nullptr) {
502 if (shadow_frame.NeedsNotifyPop()) {
503 instrumentation->WatchedFramePopped(self, shadow_frame);
504 }
505 // Exception is not caught by the current method. We will unwind to the
506 // caller. Notify any instrumentation listener.
507 instrumentation->MethodUnwindEvent(self,
508 shadow_frame.GetThisObject(),
509 shadow_frame.GetMethod(),
510 shadow_frame.GetDexPC());
Alex Lighte814f9d2017-07-31 16:14:39 -0700511 }
Alex Light9fb1ab12017-09-05 09:32:49 -0700512 return false;
Ian Rogers54874942014-06-10 16:31:03 -0700513 } else {
Alex Light9fb1ab12017-09-05 09:32:49 -0700514 shadow_frame.SetDexPC(found_dex_pc);
515 if (instrumentation != nullptr && instrumentation->HasExceptionHandledListeners()) {
516 self->ClearException();
517 instrumentation->ExceptionHandledEvent(self, exception.Get());
518 if (UNLIKELY(self->IsExceptionPending())) {
519 // Exception handled event threw an exception. Try to find the handler for this one.
520 return MoveToExceptionHandler(self, shadow_frame, instrumentation);
521 } else if (!clear_exception) {
522 self->SetException(exception.Get());
523 }
524 } else if (clear_exception) {
Ian Rogers54874942014-06-10 16:31:03 -0700525 self->ClearException();
526 }
Alex Light9fb1ab12017-09-05 09:32:49 -0700527 return true;
Ian Rogers54874942014-06-10 16:31:03 -0700528 }
Ian Rogers54874942014-06-10 16:31:03 -0700529}
530
Ian Rogerse94652f2014-12-02 11:13:19 -0800531void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
532 LOG(FATAL) << "Unexpected instruction: "
533 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
534 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700535}
536
Sebastien Hertz45b15972015-04-03 16:07:05 +0200537void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700538 va_list args;
539 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200540 AbortTransactionV(self, fmt, args);
541 va_end(args);
542}
543
544void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
545 CHECK(Runtime::Current()->IsActiveTransaction());
546 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100547 std::string abort_msg;
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800548 android::base::StringAppendV(&abort_msg, fmt, args);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100549 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200550 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700551}
552
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100553// START DECLARATIONS :
554//
555// These additional declarations are required because clang complains
556// about ALWAYS_INLINE (-Werror, -Wgcc-compat) in definitions.
557//
558
Narayan Kamath9823e782016-08-03 12:46:58 +0100559template <bool is_range, bool do_assignability_check>
Vladimir Markod16363a2017-02-01 14:09:13 +0000560static ALWAYS_INLINE bool DoCallCommon(ArtMethod* called_method,
561 Thread* self,
562 ShadowFrame& shadow_frame,
563 JValue* result,
564 uint16_t number_of_inputs,
565 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
566 uint32_t vregC) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100567
568template <bool is_range>
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000569ALWAYS_INLINE void CopyRegisters(ShadowFrame& caller_frame,
570 ShadowFrame* callee_frame,
571 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
572 const size_t first_src_reg,
573 const size_t first_dest_reg,
574 const size_t num_regs) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100575
576// END DECLARATIONS.
577
Siva Chandra05d24152016-01-05 17:43:17 -0800578void ArtInterpreterToCompiledCodeBridge(Thread* self,
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100579 ArtMethod* caller,
Siva Chandra05d24152016-01-05 17:43:17 -0800580 ShadowFrame* shadow_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -0700581 uint16_t arg_offset,
Siva Chandra05d24152016-01-05 17:43:17 -0800582 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700583 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700584 ArtMethod* method = shadow_frame->GetMethod();
585 // Ensure static methods are initialized.
586 if (method->IsStatic()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700587 ObjPtr<mirror::Class> declaringClass = method->GetDeclaringClass();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700588 if (UNLIKELY(!declaringClass->IsInitialized())) {
589 self->PushShadowFrame(shadow_frame);
590 StackHandleScope<1> hs(self);
591 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
Nicolas Geoffray01822292017-03-09 09:03:19 +0000592 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
593 true))) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700594 self->PopShadowFrame();
595 DCHECK(self->IsExceptionPending());
596 return;
597 }
598 self->PopShadowFrame();
599 CHECK(h_class->IsInitializing());
Nicolas Geoffray01822292017-03-09 09:03:19 +0000600 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
601 method = shadow_frame->GetMethod();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700602 }
603 }
Jeff Hao5ea84132017-05-05 16:59:29 -0700604 // Basic checks for the arg_offset. If there's no code item, the arg_offset must be 0. Otherwise,
605 // check that the arg_offset isn't greater than the number of registers. A stronger check is
606 // difficult since the frame may contain space for all the registers in the method, or only enough
607 // space for the arguments.
608 if (kIsDebugBuild) {
609 if (method->GetCodeItem() == nullptr) {
610 DCHECK_EQ(0u, arg_offset) << method->PrettyMethod();
611 } else {
612 DCHECK_LE(arg_offset, shadow_frame->NumberOfVRegs());
613 }
614 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100615 jit::Jit* jit = Runtime::Current()->GetJit();
616 if (jit != nullptr && caller != nullptr) {
617 jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
618 }
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700619 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
620 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
Andreas Gampe542451c2016-07-26 09:02:02 -0700621 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700622}
623
Mingyao Yangffedec52016-05-19 10:48:40 -0700624void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
625 uint16_t this_obj_vreg,
626 JValue result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700627 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700628 ObjPtr<mirror::Object> existing = shadow_frame->GetVRegReference(this_obj_vreg);
Mingyao Yangffedec52016-05-19 10:48:40 -0700629 if (existing == nullptr) {
630 // If it's null, we come from compiled code that was deoptimized. Nothing to do,
631 // as the compiler verified there was no alias.
632 // Set the new string result of the StringFactory.
633 shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
634 return;
635 }
636 // Set the string init result into all aliases.
637 for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
638 if (shadow_frame->GetVRegReference(i) == existing) {
639 DCHECK_EQ(shadow_frame->GetVRegReference(i),
Vladimir Marko78baed52018-10-11 10:44:58 +0100640 reinterpret_cast32<mirror::Object*>(shadow_frame->GetVReg(i)));
Mingyao Yangffedec52016-05-19 10:48:40 -0700641 shadow_frame->SetVRegReference(i, result.GetL());
642 DCHECK_EQ(shadow_frame->GetVRegReference(i),
Vladimir Marko78baed52018-10-11 10:44:58 +0100643 reinterpret_cast32<mirror::Object*>(shadow_frame->GetVReg(i)));
Mingyao Yangffedec52016-05-19 10:48:40 -0700644 }
645 }
646}
647
Orion Hodsonc069a302017-01-18 09:23:12 +0000648template<bool is_range>
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100649static bool DoMethodHandleInvokeCommon(Thread* self,
650 ShadowFrame& shadow_frame,
651 bool invoke_exact,
652 const Instruction* inst,
653 uint16_t inst_data,
654 JValue* result)
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100655 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Light848574c2017-09-25 16:59:39 -0700656 // Make sure to check for async exceptions
657 if (UNLIKELY(self->ObserveAsyncException())) {
658 return false;
659 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100660 // Invoke-polymorphic instructions always take a receiver. i.e, they are never static.
661 const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc();
Orion Hodson3d617ac2016-10-19 14:00:46 +0100662 const int invoke_method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc();
Narayan Kamath9823e782016-08-03 12:46:58 +0100663
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000664 // Initialize |result| to 0 as this is the default return value for
665 // polymorphic invocations of method handle types with void return
666 // and provides sane return result in error cases.
667 result->SetJ(0);
668
Orion Hodson3d617ac2016-10-19 14:00:46 +0100669 // The invoke_method_idx here is the name of the signature polymorphic method that
Narayan Kamath9823e782016-08-03 12:46:58 +0100670 // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact)
671 // and not the method that we'll dispatch to in the end.
Orion Hodsone7732be2017-10-11 14:35:20 +0100672 StackHandleScope<2> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +0000673 Handle<mirror::MethodHandle> method_handle(hs.NewHandle(
674 ObjPtr<mirror::MethodHandle>::DownCast(
Mathieu Chartieref41db72016-10-25 15:08:01 -0700675 MakeObjPtr(shadow_frame.GetVRegReference(vRegC)))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800676 if (UNLIKELY(method_handle == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100677 // Note that the invoke type is kVirtual here because a call to a signature
678 // polymorphic method is shaped like a virtual call at the bytecode level.
Orion Hodson3d617ac2016-10-19 14:00:46 +0100679 ThrowNullPointerExceptionForMethodAccess(invoke_method_idx, InvokeType::kVirtual);
Narayan Kamath9823e782016-08-03 12:46:58 +0100680 return false;
681 }
682
683 // The vRegH value gives the index of the proto_id associated with this
Orion Hodson811bd5f2016-12-07 11:35:37 +0000684 // signature polymorphic call site.
Orion Hodson06d10a72018-05-14 08:53:38 +0100685 const uint16_t vRegH = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
686 const dex::ProtoIndex callsite_proto_id(vRegH);
Narayan Kamath9823e782016-08-03 12:46:58 +0100687
688 // Call through to the classlinker and ask it to resolve the static type associated
689 // with the callsite. This information is stored in the dex cache so it's
690 // guaranteed to be fast after the first resolution.
Narayan Kamath9823e782016-08-03 12:46:58 +0100691 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Orion Hodsone7732be2017-10-11 14:35:20 +0100692 Handle<mirror::MethodType> callsite_type(hs.NewHandle(
693 class_linker->ResolveMethodType(self, callsite_proto_id, shadow_frame.GetMethod())));
Narayan Kamath9823e782016-08-03 12:46:58 +0100694
695 // This implies we couldn't resolve one or more types in this method handle.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800696 if (UNLIKELY(callsite_type == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100697 CHECK(self->IsExceptionPending());
Narayan Kamath9823e782016-08-03 12:46:58 +0100698 return false;
699 }
700
Orion Hodson811bd5f2016-12-07 11:35:37 +0000701 // There is a common dispatch method for method handles that takes
702 // arguments either from a range or an array of arguments depending
703 // on whether the DEX instruction is invoke-polymorphic/range or
704 // invoke-polymorphic. The array here is for the latter.
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100705 if (UNLIKELY(is_range)) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000706 // VRegC is the register holding the method handle. Arguments passed
707 // to the method handle's target do not include the method handle.
Orion Hodson960d4f72017-11-10 15:32:38 +0000708 RangeInstructionOperands operands(inst->VRegC_4rcc() + 1, inst->VRegA_4rcc() - 1);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100709 if (invoke_exact) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000710 return MethodHandleInvokeExact(self,
711 shadow_frame,
712 method_handle,
713 callsite_type,
714 &operands,
715 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100716 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000717 return MethodHandleInvoke(self,
718 shadow_frame,
719 method_handle,
720 callsite_type,
721 &operands,
722 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100723 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100724 } else {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000725 // Get the register arguments for the invoke.
Orion Hodson960d4f72017-11-10 15:32:38 +0000726 uint32_t args[Instruction::kMaxVarArgRegs] = {};
Orion Hodson811bd5f2016-12-07 11:35:37 +0000727 inst->GetVarArgs(args, inst_data);
728 // Drop the first register which is the method handle performing the invoke.
Alexey Frunze8631a462017-01-19 19:07:37 -0800729 memmove(args, args + 1, sizeof(args[0]) * (Instruction::kMaxVarArgRegs - 1));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000730 args[Instruction::kMaxVarArgRegs - 1] = 0;
Orion Hodson960d4f72017-11-10 15:32:38 +0000731 VarArgsInstructionOperands operands(args, inst->VRegA_45cc() - 1);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100732 if (invoke_exact) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000733 return MethodHandleInvokeExact(self,
734 shadow_frame,
735 method_handle,
736 callsite_type,
737 &operands,
738 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100739 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000740 return MethodHandleInvoke(self,
741 shadow_frame,
742 method_handle,
743 callsite_type,
744 &operands,
745 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100746 }
747 }
748}
749
750bool DoMethodHandleInvokeExact(Thread* self,
751 ShadowFrame& shadow_frame,
752 const Instruction* inst,
753 uint16_t inst_data,
754 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
755 if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) {
756 static const bool kIsRange = false;
757 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700758 self, shadow_frame, /* invoke_exact= */ true, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100759 } else {
760 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE);
761 static const bool kIsRange = true;
762 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700763 self, shadow_frame, /* invoke_exact= */ true, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100764 }
765}
766
767bool DoMethodHandleInvoke(Thread* self,
768 ShadowFrame& shadow_frame,
769 const Instruction* inst,
770 uint16_t inst_data,
771 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
772 if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) {
773 static const bool kIsRange = false;
774 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700775 self, shadow_frame, /* invoke_exact= */ false, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100776 } else {
777 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE);
778 static const bool kIsRange = true;
779 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700780 self, shadow_frame, /* invoke_exact= */ false, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100781 }
782}
783
Orion Hodson928033d2018-02-07 05:30:54 +0000784static bool DoVarHandleInvokeCommon(Thread* self,
785 ShadowFrame& shadow_frame,
786 const Instruction* inst,
787 uint16_t inst_data,
788 JValue* result,
789 mirror::VarHandle::AccessMode access_mode)
790 REQUIRES_SHARED(Locks::mutator_lock_) {
791 // Make sure to check for async exceptions
792 if (UNLIKELY(self->ObserveAsyncException())) {
793 return false;
794 }
795
Orion Hodson928033d2018-02-07 05:30:54 +0000796 StackHandleScope<2> hs(self);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100797 bool is_var_args = inst->HasVarArgs();
Orion Hodson06d10a72018-05-14 08:53:38 +0100798 const uint16_t vRegH = is_var_args ? inst->VRegH_45cc() : inst->VRegH_4rcc();
Orion Hodson928033d2018-02-07 05:30:54 +0000799 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
800 Handle<mirror::MethodType> callsite_type(hs.NewHandle(
Orion Hodson06d10a72018-05-14 08:53:38 +0100801 class_linker->ResolveMethodType(self, dex::ProtoIndex(vRegH), shadow_frame.GetMethod())));
Orion Hodson928033d2018-02-07 05:30:54 +0000802 // This implies we couldn't resolve one or more types in this VarHandle.
803 if (UNLIKELY(callsite_type == nullptr)) {
804 CHECK(self->IsExceptionPending());
805 return false;
806 }
807
Orion Hodson537a4fe2018-05-15 13:57:58 +0100808 const uint32_t vRegC = is_var_args ? inst->VRegC_45cc() : inst->VRegC_4rcc();
809 ObjPtr<mirror::Object> receiver(shadow_frame.GetVRegReference(vRegC));
Vladimir Marko179b7c62019-03-22 13:38:57 +0000810 Handle<mirror::VarHandle> var_handle(hs.NewHandle(ObjPtr<mirror::VarHandle>::DownCast(receiver)));
Orion Hodson928033d2018-02-07 05:30:54 +0000811 if (is_var_args) {
812 uint32_t args[Instruction::kMaxVarArgRegs];
813 inst->GetVarArgs(args, inst_data);
814 VarArgsInstructionOperands all_operands(args, inst->VRegA_45cc());
815 NoReceiverInstructionOperands operands(&all_operands);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100816 return VarHandleInvokeAccessor(self,
817 shadow_frame,
818 var_handle,
819 callsite_type,
820 access_mode,
821 &operands,
822 result);
Orion Hodson928033d2018-02-07 05:30:54 +0000823 } else {
824 RangeInstructionOperands all_operands(inst->VRegC_4rcc(), inst->VRegA_4rcc());
825 NoReceiverInstructionOperands operands(&all_operands);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100826 return VarHandleInvokeAccessor(self,
827 shadow_frame,
828 var_handle,
829 callsite_type,
830 access_mode,
831 &operands,
832 result);
Orion Hodson928033d2018-02-07 05:30:54 +0000833 }
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100834}
835
Orion Hodson928033d2018-02-07 05:30:54 +0000836#define DO_VAR_HANDLE_ACCESSOR(_access_mode) \
837bool DoVarHandle ## _access_mode(Thread* self, \
838 ShadowFrame& shadow_frame, \
839 const Instruction* inst, \
840 uint16_t inst_data, \
841 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { \
842 const auto access_mode = mirror::VarHandle::AccessMode::k ## _access_mode; \
843 return DoVarHandleInvokeCommon(self, shadow_frame, inst, inst_data, result, access_mode); \
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100844}
845
Orion Hodson928033d2018-02-07 05:30:54 +0000846DO_VAR_HANDLE_ACCESSOR(CompareAndExchange)
847DO_VAR_HANDLE_ACCESSOR(CompareAndExchangeAcquire)
848DO_VAR_HANDLE_ACCESSOR(CompareAndExchangeRelease)
849DO_VAR_HANDLE_ACCESSOR(CompareAndSet)
850DO_VAR_HANDLE_ACCESSOR(Get)
851DO_VAR_HANDLE_ACCESSOR(GetAcquire)
852DO_VAR_HANDLE_ACCESSOR(GetAndAdd)
853DO_VAR_HANDLE_ACCESSOR(GetAndAddAcquire)
854DO_VAR_HANDLE_ACCESSOR(GetAndAddRelease)
855DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAnd)
856DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAndAcquire)
857DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAndRelease)
858DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOr)
859DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOrAcquire)
860DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOrRelease)
861DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXor)
862DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXorAcquire)
863DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXorRelease)
864DO_VAR_HANDLE_ACCESSOR(GetAndSet)
865DO_VAR_HANDLE_ACCESSOR(GetAndSetAcquire)
866DO_VAR_HANDLE_ACCESSOR(GetAndSetRelease)
867DO_VAR_HANDLE_ACCESSOR(GetOpaque)
868DO_VAR_HANDLE_ACCESSOR(GetVolatile)
869DO_VAR_HANDLE_ACCESSOR(Set)
870DO_VAR_HANDLE_ACCESSOR(SetOpaque)
871DO_VAR_HANDLE_ACCESSOR(SetRelease)
872DO_VAR_HANDLE_ACCESSOR(SetVolatile)
873DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSet)
874DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetAcquire)
875DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetPlain)
876DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetRelease)
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100877
Orion Hodson928033d2018-02-07 05:30:54 +0000878#undef DO_VAR_HANDLE_ACCESSOR
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100879
880template<bool is_range>
881bool DoInvokePolymorphic(Thread* self,
882 ShadowFrame& shadow_frame,
883 const Instruction* inst,
884 uint16_t inst_data,
885 JValue* result) {
886 const int invoke_method_idx = inst->VRegB();
887 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
888 ArtMethod* invoke_method =
889 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
890 self, invoke_method_idx, shadow_frame.GetMethod(), kVirtual);
891
892 // Ensure intrinsic identifiers are initialized.
893 DCHECK(invoke_method->IsIntrinsic());
894
895 // Dispatch based on intrinsic identifier associated with method.
896 switch (static_cast<art::Intrinsics>(invoke_method->GetIntrinsic())) {
897#define CASE_SIGNATURE_POLYMORPHIC_INTRINSIC(Name, ...) \
898 case Intrinsics::k##Name: \
899 return Do ## Name(self, shadow_frame, inst, inst_data, result);
900#include "intrinsics_list.h"
901 SIGNATURE_POLYMORPHIC_INTRINSICS_LIST(CASE_SIGNATURE_POLYMORPHIC_INTRINSIC)
902#undef INTRINSICS_LIST
903#undef SIGNATURE_POLYMORPHIC_INTRINSICS_LIST
904#undef CASE_SIGNATURE_POLYMORPHIC_INTRINSIC
905 default:
906 LOG(FATAL) << "Unreachable: " << invoke_method->GetIntrinsic();
907 UNREACHABLE();
908 return false;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100909 }
910}
911
Orion Hodsona5dca522018-02-27 12:42:11 +0000912static JValue ConvertScalarBootstrapArgument(jvalue value) {
913 // value either contains a primitive scalar value if it corresponds
914 // to a primitive type, or it contains an integer value if it
915 // corresponds to an object instance reference id (e.g. a string id).
916 return JValue::FromPrimitive(value.j);
917}
918
919static ObjPtr<mirror::Class> GetClassForBootstrapArgument(EncodedArrayValueIterator::ValueType type)
920 REQUIRES_SHARED(Locks::mutator_lock_) {
921 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100922 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = class_linker->GetClassRoots();
Orion Hodsona5dca522018-02-27 12:42:11 +0000923 switch (type) {
924 case EncodedArrayValueIterator::ValueType::kBoolean:
925 case EncodedArrayValueIterator::ValueType::kByte:
926 case EncodedArrayValueIterator::ValueType::kChar:
927 case EncodedArrayValueIterator::ValueType::kShort:
928 // These types are disallowed by JVMS. Treat as integers. This
929 // will result in CCE's being raised if the BSM has one of these
930 // types.
931 case EncodedArrayValueIterator::ValueType::kInt:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100932 return GetClassRoot(ClassRoot::kPrimitiveInt, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000933 case EncodedArrayValueIterator::ValueType::kLong:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100934 return GetClassRoot(ClassRoot::kPrimitiveLong, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000935 case EncodedArrayValueIterator::ValueType::kFloat:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100936 return GetClassRoot(ClassRoot::kPrimitiveFloat, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000937 case EncodedArrayValueIterator::ValueType::kDouble:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100938 return GetClassRoot(ClassRoot::kPrimitiveDouble, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000939 case EncodedArrayValueIterator::ValueType::kMethodType:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100940 return GetClassRoot<mirror::MethodType>(class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000941 case EncodedArrayValueIterator::ValueType::kMethodHandle:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100942 return GetClassRoot<mirror::MethodHandle>(class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000943 case EncodedArrayValueIterator::ValueType::kString:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100944 return GetClassRoot<mirror::String>();
Orion Hodsona5dca522018-02-27 12:42:11 +0000945 case EncodedArrayValueIterator::ValueType::kType:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100946 return GetClassRoot<mirror::Class>();
Orion Hodsona5dca522018-02-27 12:42:11 +0000947 case EncodedArrayValueIterator::ValueType::kField:
948 case EncodedArrayValueIterator::ValueType::kMethod:
949 case EncodedArrayValueIterator::ValueType::kEnum:
950 case EncodedArrayValueIterator::ValueType::kArray:
951 case EncodedArrayValueIterator::ValueType::kAnnotation:
952 case EncodedArrayValueIterator::ValueType::kNull:
953 return nullptr;
954 }
955}
956
957static bool GetArgumentForBootstrapMethod(Thread* self,
958 ArtMethod* referrer,
959 EncodedArrayValueIterator::ValueType type,
960 const JValue* encoded_value,
961 JValue* decoded_value)
962 REQUIRES_SHARED(Locks::mutator_lock_) {
963 // The encoded_value contains either a scalar value (IJDF) or a
964 // scalar DEX file index to a reference type to be materialized.
965 switch (type) {
966 case EncodedArrayValueIterator::ValueType::kInt:
967 case EncodedArrayValueIterator::ValueType::kFloat:
968 decoded_value->SetI(encoded_value->GetI());
969 return true;
970 case EncodedArrayValueIterator::ValueType::kLong:
971 case EncodedArrayValueIterator::ValueType::kDouble:
972 decoded_value->SetJ(encoded_value->GetJ());
973 return true;
974 case EncodedArrayValueIterator::ValueType::kMethodType: {
975 StackHandleScope<2> hs(self);
976 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader()));
977 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
Orion Hodson06d10a72018-05-14 08:53:38 +0100978 dex::ProtoIndex proto_idx(encoded_value->GetC());
Orion Hodsona5dca522018-02-27 12:42:11 +0000979 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Orion Hodson06d10a72018-05-14 08:53:38 +0100980 ObjPtr<mirror::MethodType> o =
981 cl->ResolveMethodType(self, proto_idx, dex_cache, class_loader);
Orion Hodsona5dca522018-02-27 12:42:11 +0000982 if (UNLIKELY(o.IsNull())) {
983 DCHECK(self->IsExceptionPending());
984 return false;
985 }
986 decoded_value->SetL(o);
987 return true;
988 }
989 case EncodedArrayValueIterator::ValueType::kMethodHandle: {
990 uint32_t index = static_cast<uint32_t>(encoded_value->GetI());
991 ClassLinker* cl = Runtime::Current()->GetClassLinker();
992 ObjPtr<mirror::MethodHandle> o = cl->ResolveMethodHandle(self, index, referrer);
993 if (UNLIKELY(o.IsNull())) {
994 DCHECK(self->IsExceptionPending());
995 return false;
996 }
997 decoded_value->SetL(o);
998 return true;
999 }
1000 case EncodedArrayValueIterator::ValueType::kString: {
Orion Hodsona5dca522018-02-27 12:42:11 +00001001 dex::StringIndex index(static_cast<uint32_t>(encoded_value->GetI()));
1002 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Vladimir Marko18090d12018-06-01 16:53:12 +01001003 ObjPtr<mirror::String> o = cl->ResolveString(index, referrer);
Orion Hodsona5dca522018-02-27 12:42:11 +00001004 if (UNLIKELY(o.IsNull())) {
1005 DCHECK(self->IsExceptionPending());
1006 return false;
1007 }
1008 decoded_value->SetL(o);
1009 return true;
1010 }
1011 case EncodedArrayValueIterator::ValueType::kType: {
Orion Hodsona5dca522018-02-27 12:42:11 +00001012 dex::TypeIndex index(static_cast<uint32_t>(encoded_value->GetI()));
1013 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Vladimir Marko09c5ca42018-05-31 15:15:31 +01001014 ObjPtr<mirror::Class> o = cl->ResolveType(index, referrer);
Orion Hodsona5dca522018-02-27 12:42:11 +00001015 if (UNLIKELY(o.IsNull())) {
1016 DCHECK(self->IsExceptionPending());
1017 return false;
1018 }
1019 decoded_value->SetL(o);
1020 return true;
1021 }
1022 case EncodedArrayValueIterator::ValueType::kBoolean:
1023 case EncodedArrayValueIterator::ValueType::kByte:
1024 case EncodedArrayValueIterator::ValueType::kChar:
1025 case EncodedArrayValueIterator::ValueType::kShort:
1026 case EncodedArrayValueIterator::ValueType::kField:
1027 case EncodedArrayValueIterator::ValueType::kMethod:
1028 case EncodedArrayValueIterator::ValueType::kEnum:
1029 case EncodedArrayValueIterator::ValueType::kArray:
1030 case EncodedArrayValueIterator::ValueType::kAnnotation:
1031 case EncodedArrayValueIterator::ValueType::kNull:
1032 // Unreachable - unsupported types that have been checked when
1033 // determining the effect call site type based on the bootstrap
1034 // argument types.
1035 UNREACHABLE();
1036 }
1037}
1038
1039static bool PackArgumentForBootstrapMethod(Thread* self,
1040 ArtMethod* referrer,
1041 CallSiteArrayValueIterator* it,
1042 ShadowFrameSetter* setter)
1043 REQUIRES_SHARED(Locks::mutator_lock_) {
1044 auto type = it->GetValueType();
1045 const JValue encoded_value = ConvertScalarBootstrapArgument(it->GetJavaValue());
1046 JValue decoded_value;
1047 if (!GetArgumentForBootstrapMethod(self, referrer, type, &encoded_value, &decoded_value)) {
1048 return false;
1049 }
1050 switch (it->GetValueType()) {
1051 case EncodedArrayValueIterator::ValueType::kInt:
1052 case EncodedArrayValueIterator::ValueType::kFloat:
1053 setter->Set(static_cast<uint32_t>(decoded_value.GetI()));
1054 return true;
1055 case EncodedArrayValueIterator::ValueType::kLong:
1056 case EncodedArrayValueIterator::ValueType::kDouble:
1057 setter->SetLong(decoded_value.GetJ());
1058 return true;
1059 case EncodedArrayValueIterator::ValueType::kMethodType:
1060 case EncodedArrayValueIterator::ValueType::kMethodHandle:
1061 case EncodedArrayValueIterator::ValueType::kString:
1062 case EncodedArrayValueIterator::ValueType::kType:
1063 setter->SetReference(decoded_value.GetL());
1064 return true;
1065 case EncodedArrayValueIterator::ValueType::kBoolean:
1066 case EncodedArrayValueIterator::ValueType::kByte:
1067 case EncodedArrayValueIterator::ValueType::kChar:
1068 case EncodedArrayValueIterator::ValueType::kShort:
1069 case EncodedArrayValueIterator::ValueType::kField:
1070 case EncodedArrayValueIterator::ValueType::kMethod:
1071 case EncodedArrayValueIterator::ValueType::kEnum:
1072 case EncodedArrayValueIterator::ValueType::kArray:
1073 case EncodedArrayValueIterator::ValueType::kAnnotation:
1074 case EncodedArrayValueIterator::ValueType::kNull:
1075 // Unreachable - unsupported types that have been checked when
1076 // determining the effect call site type based on the bootstrap
1077 // argument types.
1078 UNREACHABLE();
1079 }
1080}
1081
1082static bool PackCollectorArrayForBootstrapMethod(Thread* self,
1083 ArtMethod* referrer,
1084 ObjPtr<mirror::Class> array_type,
1085 int32_t array_length,
1086 CallSiteArrayValueIterator* it,
1087 ShadowFrameSetter* setter)
1088 REQUIRES_SHARED(Locks::mutator_lock_) {
1089 StackHandleScope<1> hs(self);
1090 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1091 JValue decoded_value;
1092
1093#define COLLECT_PRIMITIVE_ARRAY(Descriptor, Type) \
1094 Handle<mirror::Type ## Array> array = \
1095 hs.NewHandle(mirror::Type ## Array::Alloc(self, array_length)); \
1096 if (array.IsNull()) { \
1097 return false; \
1098 } \
1099 for (int32_t i = 0; it->HasNext(); it->Next(), ++i) { \
1100 auto type = it->GetValueType(); \
1101 DCHECK_EQ(type, EncodedArrayValueIterator::ValueType::k ## Type); \
1102 const JValue encoded_value = \
1103 ConvertScalarBootstrapArgument(it->GetJavaValue()); \
1104 GetArgumentForBootstrapMethod(self, \
1105 referrer, \
1106 type, \
1107 &encoded_value, \
1108 &decoded_value); \
1109 array->Set(i, decoded_value.Get ## Descriptor()); \
1110 } \
1111 setter->SetReference(array.Get()); \
1112 return true;
1113
1114#define COLLECT_REFERENCE_ARRAY(T, Type) \
Andreas Gampe584771b2018-10-18 13:22:23 -07001115 Handle<mirror::ObjectArray<T>> array = /* NOLINT */ \
Orion Hodsona5dca522018-02-27 12:42:11 +00001116 hs.NewHandle(mirror::ObjectArray<T>::Alloc(self, \
1117 array_type, \
1118 array_length)); \
1119 if (array.IsNull()) { \
1120 return false; \
1121 } \
1122 for (int32_t i = 0; it->HasNext(); it->Next(), ++i) { \
1123 auto type = it->GetValueType(); \
1124 DCHECK_EQ(type, EncodedArrayValueIterator::ValueType::k ## Type); \
1125 const JValue encoded_value = \
1126 ConvertScalarBootstrapArgument(it->GetJavaValue()); \
1127 if (!GetArgumentForBootstrapMethod(self, \
1128 referrer, \
1129 type, \
1130 &encoded_value, \
1131 &decoded_value)) { \
1132 return false; \
1133 } \
1134 ObjPtr<mirror::Object> o = decoded_value.GetL(); \
1135 if (Runtime::Current()->IsActiveTransaction()) { \
1136 array->Set<true>(i, ObjPtr<T>::DownCast(o)); \
1137 } else { \
1138 array->Set<false>(i, ObjPtr<T>::DownCast(o)); \
1139 } \
1140 } \
1141 setter->SetReference(array.Get()); \
1142 return true;
1143
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001144 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = class_linker->GetClassRoots();
1145 ObjPtr<mirror::Class> component_type = array_type->GetComponentType();
1146 if (component_type == GetClassRoot(ClassRoot::kPrimitiveInt, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001147 COLLECT_PRIMITIVE_ARRAY(I, Int);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001148 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveLong, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001149 COLLECT_PRIMITIVE_ARRAY(J, Long);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001150 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveFloat, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001151 COLLECT_PRIMITIVE_ARRAY(F, Float);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001152 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveDouble, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001153 COLLECT_PRIMITIVE_ARRAY(D, Double);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001154 } else if (component_type == GetClassRoot<mirror::MethodType>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001155 COLLECT_REFERENCE_ARRAY(mirror::MethodType, MethodType);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001156 } else if (component_type == GetClassRoot<mirror::MethodHandle>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001157 COLLECT_REFERENCE_ARRAY(mirror::MethodHandle, MethodHandle);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001158 } else if (component_type == GetClassRoot<mirror::String>(class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001159 COLLECT_REFERENCE_ARRAY(mirror::String, String);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001160 } else if (component_type == GetClassRoot<mirror::Class>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001161 COLLECT_REFERENCE_ARRAY(mirror::Class, Type);
1162 } else {
1163 UNREACHABLE();
1164 }
1165 #undef COLLECT_PRIMITIVE_ARRAY
1166 #undef COLLECT_REFERENCE_ARRAY
1167}
1168
1169static ObjPtr<mirror::MethodType> BuildCallSiteForBootstrapMethod(Thread* self,
1170 const DexFile* dex_file,
1171 uint32_t call_site_idx)
1172 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001173 const dex::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx);
Orion Hodsona5dca522018-02-27 12:42:11 +00001174 CallSiteArrayValueIterator it(*dex_file, csi);
1175 DCHECK_GE(it.Size(), 1u);
1176
1177 StackHandleScope<2> hs(self);
1178 // Create array for parameter types.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001179 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001180 ObjPtr<mirror::Class> class_array_type =
1181 GetClassRoot<mirror::ObjectArray<mirror::Class>>(class_linker);
Orion Hodsona5dca522018-02-27 12:42:11 +00001182 Handle<mirror::ObjectArray<mirror::Class>> ptypes = hs.NewHandle(
1183 mirror::ObjectArray<mirror::Class>::Alloc(self,
1184 class_array_type,
1185 static_cast<int>(it.Size())));
1186 if (ptypes.IsNull()) {
1187 DCHECK(self->IsExceptionPending());
1188 return nullptr;
1189 }
1190
1191 // Populate the first argument with an instance of j.l.i.MethodHandles.Lookup
1192 // that the runtime will construct.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001193 ptypes->Set(0, GetClassRoot<mirror::MethodHandlesLookup>(class_linker));
Orion Hodsona5dca522018-02-27 12:42:11 +00001194 it.Next();
1195
1196 // The remaining parameter types are derived from the types of
1197 // arguments present in the DEX file.
1198 int index = 1;
1199 while (it.HasNext()) {
1200 ObjPtr<mirror::Class> ptype = GetClassForBootstrapArgument(it.GetValueType());
1201 if (ptype.IsNull()) {
1202 ThrowClassCastException("Unsupported bootstrap argument type");
1203 return nullptr;
1204 }
1205 ptypes->Set(index, ptype);
1206 index++;
1207 it.Next();
1208 }
1209 DCHECK_EQ(static_cast<size_t>(index), it.Size());
1210
1211 // By definition, the return type is always a j.l.i.CallSite.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001212 Handle<mirror::Class> rtype = hs.NewHandle(GetClassRoot<mirror::CallSite>());
Orion Hodsona5dca522018-02-27 12:42:11 +00001213 return mirror::MethodType::Create(self, rtype, ptypes);
1214}
1215
Orion Hodsonc069a302017-01-18 09:23:12 +00001216static ObjPtr<mirror::CallSite> InvokeBootstrapMethod(Thread* self,
1217 ShadowFrame& shadow_frame,
1218 uint32_t call_site_idx)
1219 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001220 StackHandleScope<5> hs(self);
Orion Hodsona5dca522018-02-27 12:42:11 +00001221 // There are three mandatory arguments expected from the call site
1222 // value array in the DEX file: the bootstrap method handle, the
1223 // method name to pass to the bootstrap method, and the method type
1224 // to pass to the bootstrap method.
1225 static constexpr size_t kMandatoryArgumentsCount = 3;
Orion Hodsonc069a302017-01-18 09:23:12 +00001226 ArtMethod* referrer = shadow_frame.GetMethod();
1227 const DexFile* dex_file = referrer->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001228 const dex::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00001229 CallSiteArrayValueIterator it(*dex_file, csi);
Orion Hodsona5dca522018-02-27 12:42:11 +00001230 if (it.Size() < kMandatoryArgumentsCount) {
1231 ThrowBootstrapMethodError("Truncated bootstrap arguments (%zu < %zu)",
1232 it.Size(), kMandatoryArgumentsCount);
1233 return nullptr;
1234 }
1235
1236 if (it.GetValueType() != EncodedArrayValueIterator::ValueType::kMethodHandle) {
1237 ThrowBootstrapMethodError("First bootstrap argument is not a method handle");
1238 return nullptr;
1239 }
1240
1241 uint32_t bsm_index = static_cast<uint32_t>(it.GetJavaValue().i);
1242 it.Next();
1243
Orion Hodsonc069a302017-01-18 09:23:12 +00001244 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Orion Hodsona5dca522018-02-27 12:42:11 +00001245 Handle<mirror::MethodHandle> bsm =
1246 hs.NewHandle(class_linker->ResolveMethodHandle(self, bsm_index, referrer));
1247 if (bsm.IsNull()) {
Orion Hodsonc069a302017-01-18 09:23:12 +00001248 DCHECK(self->IsExceptionPending());
1249 return nullptr;
1250 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001251
Orion Hodsona5dca522018-02-27 12:42:11 +00001252 if (bsm->GetHandleKind() != mirror::MethodHandle::Kind::kInvokeStatic) {
1253 // JLS suggests also accepting constructors. This is currently
1254 // hard as constructor invocations happen via transformers in ART
1255 // today. The constructor would need to be a class derived from java.lang.invoke.CallSite.
1256 ThrowBootstrapMethodError("Unsupported bootstrap method invocation kind");
1257 return nullptr;
1258 }
1259
1260 // Construct the local call site type information based on the 3
1261 // mandatory arguments provided by the runtime and the static arguments
1262 // in the DEX file. We will use these arguments to build a shadow frame.
1263 MutableHandle<mirror::MethodType> call_site_type =
1264 hs.NewHandle(BuildCallSiteForBootstrapMethod(self, dex_file, call_site_idx));
1265 if (call_site_type.IsNull()) {
1266 DCHECK(self->IsExceptionPending());
1267 return nullptr;
1268 }
1269
1270 // Check if this BSM is targeting a variable arity method. If so,
1271 // we'll need to collect the trailing arguments into an array.
1272 Handle<mirror::Array> collector_arguments;
1273 int32_t collector_arguments_length;
1274 if (bsm->GetTargetMethod()->IsVarargs()) {
1275 int number_of_bsm_parameters = bsm->GetMethodType()->GetNumberOfPTypes();
1276 if (number_of_bsm_parameters == 0) {
1277 ThrowBootstrapMethodError("Variable arity BSM does not have any arguments");
1278 return nullptr;
1279 }
1280 Handle<mirror::Class> collector_array_class =
1281 hs.NewHandle(bsm->GetMethodType()->GetPTypes()->Get(number_of_bsm_parameters - 1));
1282 if (!collector_array_class->IsArrayClass()) {
1283 ThrowBootstrapMethodError("Variable arity BSM does not have array as final argument");
1284 return nullptr;
1285 }
1286 // The call site may include no arguments to be collected. In this
1287 // case the number of arguments must be at least the number of BSM
1288 // parameters less the collector array.
1289 if (call_site_type->GetNumberOfPTypes() < number_of_bsm_parameters - 1) {
1290 ThrowWrongMethodTypeException(bsm->GetMethodType(), call_site_type.Get());
1291 return nullptr;
1292 }
1293 // Check all the arguments to be collected match the collector array component type.
1294 for (int i = number_of_bsm_parameters - 1; i < call_site_type->GetNumberOfPTypes(); ++i) {
1295 if (call_site_type->GetPTypes()->Get(i) != collector_array_class->GetComponentType()) {
1296 ThrowClassCastException(collector_array_class->GetComponentType(),
1297 call_site_type->GetPTypes()->Get(i));
1298 return nullptr;
1299 }
1300 }
1301 // Update the call site method type so it now includes the collector array.
1302 int32_t collector_arguments_start = number_of_bsm_parameters - 1;
1303 collector_arguments_length = call_site_type->GetNumberOfPTypes() - number_of_bsm_parameters + 1;
1304 call_site_type.Assign(
1305 mirror::MethodType::CollectTrailingArguments(self,
1306 call_site_type.Get(),
1307 collector_array_class.Get(),
1308 collector_arguments_start));
1309 if (call_site_type.IsNull()) {
1310 DCHECK(self->IsExceptionPending());
1311 return nullptr;
1312 }
1313 } else {
1314 collector_arguments_length = 0;
1315 }
1316
1317 if (call_site_type->GetNumberOfPTypes() != bsm->GetMethodType()->GetNumberOfPTypes()) {
1318 ThrowWrongMethodTypeException(bsm->GetMethodType(), call_site_type.Get());
1319 return nullptr;
1320 }
1321
1322 // BSM invocation has a different set of exceptions that
1323 // j.l.i.MethodHandle.invoke(). Scan arguments looking for CCE
1324 // "opportunities". Unfortunately we cannot just leave this to the
1325 // method handle invocation as this might generate a WMTE.
1326 for (int32_t i = 0; i < call_site_type->GetNumberOfPTypes(); ++i) {
1327 ObjPtr<mirror::Class> from = call_site_type->GetPTypes()->Get(i);
1328 ObjPtr<mirror::Class> to = bsm->GetMethodType()->GetPTypes()->Get(i);
1329 if (!IsParameterTypeConvertible(from, to)) {
1330 ThrowClassCastException(from, to);
1331 return nullptr;
1332 }
1333 }
1334 if (!IsReturnTypeConvertible(call_site_type->GetRType(), bsm->GetMethodType()->GetRType())) {
1335 ThrowClassCastException(bsm->GetMethodType()->GetRType(), call_site_type->GetRType());
1336 return nullptr;
1337 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001338
1339 // Set-up a shadow frame for invoking the bootstrap method handle.
1340 ShadowFrameAllocaUniquePtr bootstrap_frame =
Orion Hodsona5dca522018-02-27 12:42:11 +00001341 CREATE_SHADOW_FRAME(call_site_type->NumberOfVRegs(),
1342 nullptr,
1343 referrer,
1344 shadow_frame.GetDexPC());
Orion Hodsonc069a302017-01-18 09:23:12 +00001345 ScopedStackedShadowFramePusher pusher(
1346 self, bootstrap_frame.get(), StackedShadowFrameType::kShadowFrameUnderConstruction);
Orion Hodsona5dca522018-02-27 12:42:11 +00001347 ShadowFrameSetter setter(bootstrap_frame.get(), 0u);
Orion Hodsonc069a302017-01-18 09:23:12 +00001348
1349 // The first parameter is a MethodHandles lookup instance.
Orion Hodsona5dca522018-02-27 12:42:11 +00001350 Handle<mirror::Class> lookup_class =
1351 hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass());
1352 ObjPtr<mirror::MethodHandlesLookup> lookup =
1353 mirror::MethodHandlesLookup::Create(self, lookup_class);
1354 if (lookup.IsNull()) {
Orion Hodsonc069a302017-01-18 09:23:12 +00001355 DCHECK(self->IsExceptionPending());
1356 return nullptr;
1357 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001358 setter.SetReference(lookup);
Orion Hodsonc069a302017-01-18 09:23:12 +00001359
Orion Hodsona5dca522018-02-27 12:42:11 +00001360 // Pack the remaining arguments into the frame.
1361 int number_of_arguments = call_site_type->GetNumberOfPTypes();
1362 int argument_index;
1363 for (argument_index = 1; argument_index < number_of_arguments; ++argument_index) {
1364 if (argument_index == number_of_arguments - 1 &&
1365 call_site_type->GetPTypes()->Get(argument_index)->IsArrayClass()) {
1366 ObjPtr<mirror::Class> array_type = call_site_type->GetPTypes()->Get(argument_index);
1367 if (!PackCollectorArrayForBootstrapMethod(self,
1368 referrer,
1369 array_type,
1370 collector_arguments_length,
1371 &it,
1372 &setter)) {
1373 DCHECK(self->IsExceptionPending());
1374 return nullptr;
Orion Hodsonc069a302017-01-18 09:23:12 +00001375 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001376 } else if (!PackArgumentForBootstrapMethod(self, referrer, &it, &setter)) {
1377 DCHECK(self->IsExceptionPending());
1378 return nullptr;
Orion Hodsonc069a302017-01-18 09:23:12 +00001379 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001380 it.Next();
1381 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001382 DCHECK(!it.HasNext());
1383 DCHECK(setter.Done());
Orion Hodsonc069a302017-01-18 09:23:12 +00001384
1385 // Invoke the bootstrap method handle.
1386 JValue result;
Orion Hodsona5dca522018-02-27 12:42:11 +00001387 RangeInstructionOperands operands(0, bootstrap_frame->NumberOfVRegs());
1388 bool invoke_success = MethodHandleInvoke(self,
1389 *bootstrap_frame,
1390 bsm,
1391 call_site_type,
1392 &operands,
1393 &result);
Orion Hodsonc069a302017-01-18 09:23:12 +00001394 if (!invoke_success) {
1395 DCHECK(self->IsExceptionPending());
1396 return nullptr;
1397 }
1398
1399 Handle<mirror::Object> object(hs.NewHandle(result.GetL()));
Orion Hodsonc069a302017-01-18 09:23:12 +00001400 if (UNLIKELY(object.IsNull())) {
Orion Hodsonda1cdd02018-01-31 18:08:28 +00001401 // This will typically be for LambdaMetafactory which is not supported.
Orion Hodson76e6adb2018-02-23 13:15:55 +00001402 ThrowClassCastException("Bootstrap method returned null");
Orion Hodsonc069a302017-01-18 09:23:12 +00001403 return nullptr;
1404 }
1405
Orion Hodsona5dca522018-02-27 12:42:11 +00001406 // Check the result type is a subclass of j.l.i.CallSite.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001407 ObjPtr<mirror::Class> call_site_class = GetClassRoot<mirror::CallSite>(class_linker);
1408 if (UNLIKELY(!object->InstanceOf(call_site_class))) {
1409 ThrowClassCastException(object->GetClass(), call_site_class);
Orion Hodsonc069a302017-01-18 09:23:12 +00001410 return nullptr;
1411 }
1412
Orion Hodsona5dca522018-02-27 12:42:11 +00001413 // Check the call site target is not null as we're going to invoke it.
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001414 ObjPtr<mirror::CallSite> call_site =
1415 ObjPtr<mirror::CallSite>::DownCast(ObjPtr<mirror::Object>(result.GetL()));
1416 ObjPtr<mirror::MethodHandle> target = call_site->GetTarget();
1417 if (UNLIKELY(target == nullptr)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001418 ThrowClassCastException("Bootstrap method returned a CallSite with a null target");
Orion Hodsonc069a302017-01-18 09:23:12 +00001419 return nullptr;
1420 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001421 return call_site;
Orion Hodsonc069a302017-01-18 09:23:12 +00001422}
1423
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001424namespace {
1425
1426ObjPtr<mirror::CallSite> DoResolveCallSite(Thread* self,
1427 ShadowFrame& shadow_frame,
1428 uint32_t call_site_idx)
1429 REQUIRES_SHARED(Locks::mutator_lock_) {
1430 StackHandleScope<1> hs(self);
1431 Handle<mirror::DexCache> dex_cache(hs.NewHandle(shadow_frame.GetMethod()->GetDexCache()));
1432
1433 // Get the call site from the DexCache if present.
1434 ObjPtr<mirror::CallSite> call_site = dex_cache->GetResolvedCallSite(call_site_idx);
1435 if (LIKELY(call_site != nullptr)) {
1436 return call_site;
1437 }
1438
1439 // Invoke the bootstrap method to get a candidate call site.
1440 call_site = InvokeBootstrapMethod(self, shadow_frame, call_site_idx);
1441 if (UNLIKELY(call_site == nullptr)) {
1442 if (!self->GetException()->IsError()) {
1443 // Use a BootstrapMethodError if the exception is not an instance of java.lang.Error.
1444 ThrowWrappedBootstrapMethodError("Exception from call site #%u bootstrap method",
1445 call_site_idx);
1446 }
1447 return nullptr;
1448 }
1449
1450 // Attempt to place the candidate call site into the DexCache, return the winning call site.
1451 return dex_cache->SetResolvedCallSite(call_site_idx, call_site);
1452}
1453
1454} // namespace
1455
Orion Hodsonc069a302017-01-18 09:23:12 +00001456bool DoInvokeCustom(Thread* self,
1457 ShadowFrame& shadow_frame,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001458 uint32_t call_site_idx,
1459 const InstructionOperands* operands,
1460 JValue* result) {
Alex Light848574c2017-09-25 16:59:39 -07001461 // Make sure to check for async exceptions
1462 if (UNLIKELY(self->ObserveAsyncException())) {
1463 return false;
1464 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001465
Orion Hodsonc069a302017-01-18 09:23:12 +00001466 // invoke-custom is not supported in transactions. In transactions
1467 // there is a limited set of types supported. invoke-custom allows
1468 // running arbitrary code and instantiating arbitrary types.
1469 CHECK(!Runtime::Current()->IsActiveTransaction());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001470
1471 ObjPtr<mirror::CallSite> call_site = DoResolveCallSite(self, shadow_frame, call_site_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00001472 if (call_site.IsNull()) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001473 DCHECK(self->IsExceptionPending());
1474 return false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001475 }
1476
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001477 StackHandleScope<2> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +00001478 Handle<mirror::MethodHandle> target = hs.NewHandle(call_site->GetTarget());
1479 Handle<mirror::MethodType> target_method_type = hs.NewHandle(target->GetMethodType());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001480 DCHECK_EQ(operands->GetNumberOfOperands(), target_method_type->NumberOfVRegs())
1481 << " call_site_idx" << call_site_idx;
1482 return MethodHandleInvokeExact(self,
1483 shadow_frame,
1484 target,
1485 target_method_type,
1486 operands,
1487 result);
Orion Hodsonc069a302017-01-18 09:23:12 +00001488}
1489
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001490// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
1491static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
1492 size_t dest_reg, size_t src_reg)
1493 REQUIRES_SHARED(Locks::mutator_lock_) {
1494 // Uint required, so that sign extension does not make this wrong on 64b systems
1495 uint32_t src_value = shadow_frame.GetVReg(src_reg);
1496 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
1497
1498 // If both register locations contains the same value, the register probably holds a reference.
1499 // Note: As an optimization, non-moving collectors leave a stale reference value
1500 // in the references array even after the original vreg was overwritten to a non-reference.
Vladimir Marko78baed52018-10-11 10:44:58 +01001501 if (src_value == reinterpret_cast32<uint32_t>(o.Ptr())) {
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001502 new_shadow_frame->SetVRegReference(dest_reg, o);
1503 } else {
1504 new_shadow_frame->SetVReg(dest_reg, src_value);
1505 }
1506}
1507
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001508template <bool is_range>
1509inline void CopyRegisters(ShadowFrame& caller_frame,
1510 ShadowFrame* callee_frame,
1511 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
1512 const size_t first_src_reg,
1513 const size_t first_dest_reg,
1514 const size_t num_regs) {
1515 if (is_range) {
1516 const size_t dest_reg_bound = first_dest_reg + num_regs;
1517 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound;
1518 ++dest_reg, ++src_reg) {
1519 AssignRegister(callee_frame, caller_frame, dest_reg, src_reg);
1520 }
1521 } else {
1522 DCHECK_LE(num_regs, arraysize(arg));
1523
1524 for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) {
1525 AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]);
1526 }
1527 }
1528}
1529
Igor Murashkin6918bf12015-09-27 19:19:06 -07001530template <bool is_range,
Narayan Kamath370423d2016-10-03 16:51:22 +01001531 bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001532static inline bool DoCallCommon(ArtMethod* called_method,
1533 Thread* self,
1534 ShadowFrame& shadow_frame,
1535 JValue* result,
1536 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +01001537 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -07001538 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001539 bool string_init = false;
1540 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001541 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
1542 && called_method->IsConstructor())) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001543 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
Jeff Hao848f70a2014-01-15 13:49:50 -08001544 string_init = true;
1545 }
1546
Alex Lightdaf58c82016-03-16 23:00:49 +00001547 // Compute method information.
David Sehr0225f8e2018-01-31 08:52:24 +00001548 CodeItemDataAccessor accessor(called_method->DexInstructionData());
Igor Murashkin158f35c2015-06-10 15:55:30 -07001549 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001550 uint16_t num_regs;
Jeff Hao5ea84132017-05-05 16:59:29 -07001551 // Test whether to use the interpreter or compiler entrypoint, and save that result to pass to
1552 // PerformCall. A deoptimization could occur at any time, and we shouldn't change which
1553 // entrypoint to use once we start building the shadow frame.
Mathieu Chartier448bbcf2017-07-06 12:05:13 -07001554
1555 // For unstarted runtimes, always use the interpreter entrypoint. This fixes the case where we are
1556 // doing cross compilation. Note that GetEntryPointFromQuickCompiledCode doesn't use the image
1557 // pointer size here and this may case an overflow if it is called from the compiler. b/62402160
1558 const bool use_interpreter_entrypoint = !Runtime::Current()->IsStarted() ||
1559 ClassLinker::ShouldUseInterpreterEntrypoint(
1560 called_method,
1561 called_method->GetEntryPointFromQuickCompiledCode());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001562 if (LIKELY(accessor.HasCodeItem())) {
Jeff Hao5ea84132017-05-05 16:59:29 -07001563 // When transitioning to compiled code, space only needs to be reserved for the input registers.
1564 // The rest of the frame gets discarded. This also prevents accessing the called method's code
1565 // item, saving memory by keeping code items of compiled code untouched.
Mathieu Chartier448bbcf2017-07-06 12:05:13 -07001566 if (!use_interpreter_entrypoint) {
1567 DCHECK(!Runtime::Current()->IsAotCompiler()) << "Compiler should use interpreter entrypoint";
Jeff Hao5ea84132017-05-05 16:59:29 -07001568 num_regs = number_of_inputs;
1569 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001570 num_regs = accessor.RegistersSize();
1571 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, accessor.InsSize());
Jeff Hao5ea84132017-05-05 16:59:29 -07001572 }
Nicolas Geoffray01822292017-03-09 09:03:19 +00001573 } else {
1574 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1575 num_regs = number_of_inputs;
Jeff Haodf79ddb2017-02-27 14:47:06 -08001576 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001577
Igor Murashkin158f35c2015-06-10 15:55:30 -07001578 // Hack for String init:
1579 //
1580 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
1581 // invoke-x StringFactory(a, b, c, ...)
1582 // by effectively dropping the first virtual register from the invoke.
1583 //
1584 // (at this point the ArtMethod has already been replaced,
1585 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +00001586 //
1587 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
1588 // to handle the compiler optimization of replacing `this` with null without
1589 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001590 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -07001591 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001592 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 -07001593
Igor Murashkin158f35c2015-06-10 15:55:30 -07001594 // The new StringFactory call is static and has one fewer argument.
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001595 if (!accessor.HasCodeItem()) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001596 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1597 num_regs--;
1598 } // 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 -07001599 number_of_inputs--;
1600
1601 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -07001602 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001603 arg[i - 1] = arg[i];
1604 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07001605 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001606
1607 // Rewrite the non-var-arg case
1608 vregC++; // Skips the 0th vreg in the range ("this").
1609 }
1610
1611 // Parameter registers go at the end of the shadow frame.
1612 DCHECK_GE(num_regs, number_of_inputs);
1613 size_t first_dest_reg = num_regs - number_of_inputs;
1614 DCHECK_NE(first_dest_reg, (size_t)-1);
1615
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001616 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001617 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -07001618 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -07001619 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -07001620 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001621
Igor Murashkin158f35c2015-06-10 15:55:30 -07001622 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -07001623 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001624 // Slow path.
1625 // We might need to do class loading, which incurs a thread state change to kNative. So
1626 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07001627 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +02001628 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001629 self->EndAssertNoThreadSuspension(old_cause);
1630
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001631 // ArtMethod here is needed to check type information of the call site against the callee.
1632 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
1633 //
1634 // As a special case for proxy methods, which are not dex-backed,
1635 // we have to retrieve type information from the proxy's method
1636 // interface method instead (which is dex backed since proxies are never interfaces).
Andreas Gampe542451c2016-07-26 09:02:02 -07001637 ArtMethod* method =
1638 new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001639
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001640 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001641 // to get the exact type of each reference argument.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001642 const dex::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001643 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001644 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001645
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001646 // Handle receiver apart since it's not part of the shorty.
1647 size_t dest_reg = first_dest_reg;
1648 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001649
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001650 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001651 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001652 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
1653 ++dest_reg;
1654 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -07001655 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001656 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001657
1658 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001659 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001660 // Skip the 0th 'shorty' type since it represents the return type.
1661 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001662 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
1663 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001664 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001665 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001666 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001667 if (do_assignability_check && o != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001668 const dex::TypeIndex type_idx = params->GetTypeItem(shorty_pos).type_idx_;
Vladimir Marko942fd312017-01-16 20:52:19 +00001669 ObjPtr<mirror::Class> arg_type = method->GetDexCache()->GetResolvedType(type_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001670 if (arg_type == nullptr) {
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001671 StackHandleScope<1> hs(self);
1672 // Preserve o since it is used below and GetClassFromTypeIndex may cause thread
1673 // suspension.
1674 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&o);
Vladimir Markob45528c2017-07-27 14:14:28 +01001675 arg_type = method->ResolveClassFromTypeIndex(type_idx);
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001676 if (arg_type == nullptr) {
1677 CHECK(self->IsExceptionPending());
1678 return false;
1679 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001680 }
1681 if (!o->VerifierInstanceOf(arg_type)) {
1682 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -07001683 std::string temp1, temp2;
Orion Hodsonfef06642016-11-25 16:07:11 +00001684 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001685 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -08001686 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -07001687 o->GetClass()->GetDescriptor(&temp1),
1688 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001689 return false;
1690 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001691 }
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001692 new_shadow_frame->SetVRegReference(dest_reg, o);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001693 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -07001694 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001695 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001696 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001697 uint64_t wide_value =
1698 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
1699 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001700 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001701 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001702 ++dest_reg;
1703 ++arg_offset;
1704 break;
1705 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001706 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001707 default:
1708 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
1709 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001710 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001711 }
1712 } else {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001713 if (is_range) {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001714 DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001715 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001716
1717 CopyRegisters<is_range>(shadow_frame,
1718 new_shadow_frame,
1719 arg,
1720 vregC,
1721 first_dest_reg,
1722 number_of_inputs);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001723 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001724 }
1725
Jeff Hao5ea84132017-05-05 16:59:29 -07001726 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001727 accessor,
Jeff Hao5ea84132017-05-05 16:59:29 -07001728 shadow_frame.GetMethod(),
1729 first_dest_reg,
1730 new_shadow_frame,
1731 result,
1732 use_interpreter_entrypoint);
Jeff Hao848f70a2014-01-15 13:49:50 -08001733
1734 if (string_init && !self->IsExceptionPending()) {
Mingyao Yangffedec52016-05-19 10:48:40 -07001735 SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001736 }
1737
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001738 return !self->IsExceptionPending();
1739}
1740
Igor Murashkin158f35c2015-06-10 15:55:30 -07001741template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001742bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
1743 const Instruction* inst, uint16_t inst_data, JValue* result) {
1744 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001745 const uint16_t number_of_inputs =
1746 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001747
1748 // TODO: find a cleaner way to separate non-range and range information without duplicating
1749 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -07001750 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001751 uint32_t vregC = 0;
1752 if (is_range) {
1753 vregC = inst->VRegC_3rc();
1754 } else {
1755 vregC = inst->VRegC_35c();
1756 inst->GetVarArgs(arg, inst_data);
1757 }
1758
1759 return DoCallCommon<is_range, do_assignability_check>(
1760 called_method, self, shadow_frame,
1761 result, number_of_inputs, arg, vregC);
1762}
1763
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001764template <bool is_range, bool do_access_check, bool transaction_active>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001765bool DoFilledNewArray(const Instruction* inst,
1766 const ShadowFrame& shadow_frame,
1767 Thread* self,
1768 JValue* result) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001769 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
1770 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
1771 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
1772 if (!is_range) {
1773 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
1774 CHECK_LE(length, 5);
1775 }
1776 if (UNLIKELY(length < 0)) {
1777 ThrowNegativeArraySizeException(length);
1778 return false;
1779 }
1780 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08001781 ObjPtr<mirror::Class> array_class = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
Mathieu Chartieref41db72016-10-25 15:08:01 -07001782 shadow_frame.GetMethod(),
1783 self,
1784 false,
1785 do_access_check);
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001786 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001787 DCHECK(self->IsExceptionPending());
1788 return false;
1789 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001790 CHECK(array_class->IsArrayClass());
Mathieu Chartieref41db72016-10-25 15:08:01 -07001791 ObjPtr<mirror::Class> component_class = array_class->GetComponentType();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001792 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
1793 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
1794 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001795 ThrowRuntimeException("Bad filled array request for type %s",
David Sehr709b0702016-10-13 09:12:37 -07001796 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001797 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001798 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -08001799 "Found type %s; filled-new-array not implemented for anything but 'int'",
David Sehr709b0702016-10-13 09:12:37 -07001800 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001801 }
1802 return false;
1803 }
Mathieu Chartieref41db72016-10-25 15:08:01 -07001804 ObjPtr<mirror::Object> new_array = mirror::Array::Alloc<true>(
1805 self,
1806 array_class,
1807 length,
1808 array_class->GetComponentSizeShift(),
1809 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001810 if (UNLIKELY(new_array == nullptr)) {
1811 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001812 return false;
1813 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001814 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
1815 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001816 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +01001817 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001818 } else {
Ian Rogers29a26482014-05-02 15:27:29 -07001819 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +01001820 }
Sebastien Hertzabff6432014-01-27 18:01:39 +01001821 for (int32_t i = 0; i < length; ++i) {
1822 size_t src_reg = is_range ? vregC + i : arg[i];
1823 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001824 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
1825 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +01001826 } else {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001827 new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<transaction_active>(
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001828 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001829 }
1830 }
1831
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001832 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001833 return true;
1834}
1835
Mathieu Chartieref41db72016-10-25 15:08:01 -07001836// TODO: Use ObjPtr here.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001837template<typename T>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001838static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array,
1839 int32_t count)
1840 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001841 Runtime* runtime = Runtime::Current();
1842 for (int32_t i = 0; i < count; ++i) {
1843 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
1844 }
1845}
1846
Mathieu Chartieref41db72016-10-25 15:08:01 -07001847void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001848 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001849 DCHECK(Runtime::Current()->IsActiveTransaction());
1850 DCHECK(array != nullptr);
1851 DCHECK_LE(count, array->GetLength());
1852 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
1853 switch (primitive_component_type) {
1854 case Primitive::kPrimBoolean:
1855 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
1856 break;
1857 case Primitive::kPrimByte:
1858 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
1859 break;
1860 case Primitive::kPrimChar:
1861 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
1862 break;
1863 case Primitive::kPrimShort:
1864 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
1865 break;
1866 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001867 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
1868 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001869 case Primitive::kPrimFloat:
1870 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
1871 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001872 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001873 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
1874 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001875 case Primitive::kPrimDouble:
1876 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
1877 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001878 default:
1879 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
1880 << " in fill-array-data";
Elliott Hughesc1896c92018-11-29 11:33:18 -08001881 UNREACHABLE();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001882 }
1883}
1884
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001885// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +02001886#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001887 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001888 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
1889 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +02001890 const Instruction* inst, uint16_t inst_data, \
1891 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001892EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
1893EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
1894EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
1895EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
1896#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001897
Orion Hodsonc069a302017-01-18 09:23:12 +00001898// Explicit DoInvokePolymorphic template function declarations.
1899#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range) \
1900 template REQUIRES_SHARED(Locks::mutator_lock_) \
1901 bool DoInvokePolymorphic<_is_range>( \
1902 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1903 uint16_t inst_data, JValue* result)
1904EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false);
1905EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true);
Narayan Kamath9823e782016-08-03 12:46:58 +01001906#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1907
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001908// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001909#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001910 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001911 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
1912 const ShadowFrame& shadow_frame, \
1913 Thread* self, JValue* result)
1914#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
1915 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
1916 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
1917 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
1918 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
1919EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
1920EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
1921#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001922#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
1923
1924} // namespace interpreter
1925} // namespace art