blob: 4e80a6104fb5952a05e22ade82704f936eac084d [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"
Alex Lightb7c640d2019-03-20 15:52:13 -070027#include "handle.h"
Orion Hodson43f0cdb2017-10-10 14:47:32 +010028#include "intrinsics_enum.h"
Tamas Berghammerdd5e5e92016-02-12 16:29:00 +000029#include "jit/jit.h"
Andreas Gampec5b75642018-05-16 15:12:11 -070030#include "jvalue-inl.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010031#include "method_handles-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070032#include "method_handles.h"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070033#include "mirror/array-alloc-inl.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010034#include "mirror/array-inl.h"
Vladimir Marko621c8802019-03-27 16:18:18 +000035#include "mirror/call_site-inl.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010036#include "mirror/class.h"
Narayan Kamath000e1882016-10-24 17:14:25 +010037#include "mirror/emulated_stack_frame.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080038#include "mirror/method_handle_impl-inl.h"
Vladimir Marko5aead702019-03-27 11:00:36 +000039#include "mirror/method_type-inl.h"
Andreas Gampe52ecb652018-10-24 15:18:21 -070040#include "mirror/object_array-alloc-inl.h"
41#include "mirror/object_array-inl.h"
Orion Hodson928033d2018-02-07 05:30:54 +000042#include "mirror/var_handle.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010043#include "reflection-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070044#include "reflection.h"
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +010045#include "shadow_frame-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070046#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070047#include "thread-inl.h"
Chang Xingbd208d82017-07-12 14:53:17 -070048#include "transaction.h"
Orion Hodson537a4fe2018-05-15 13:57:58 +010049#include "var_handles.h"
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +010050#include "well_known_classes.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020051
52namespace art {
53namespace interpreter {
54
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000055void ThrowNullPointerExceptionFromInterpreter() {
56 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -070057}
58
David Srbecky960327b2018-10-25 10:11:59 +000059bool CheckStackOverflow(Thread* self, size_t frame_size)
60 REQUIRES_SHARED(Locks::mutator_lock_) {
61 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
62 uint8_t* stack_end = self->GetStackEndForInterpreter(implicit_check);
63 if (UNLIKELY(__builtin_frame_address(0) < stack_end + frame_size)) {
64 ThrowStackOverflowError(self);
65 return false;
66 }
67 return true;
68}
69
David Srbecky9581e612018-10-30 14:29:43 +000070bool UseFastInterpreterToInterpreterInvoke(ArtMethod* method) {
71 Runtime* runtime = Runtime::Current();
72 const void* quick_code = method->GetEntryPointFromQuickCompiledCode();
73 if (!runtime->GetClassLinker()->IsQuickToInterpreterBridge(quick_code)) {
74 return false;
75 }
76 if (!method->SkipAccessChecks() || method->IsNative() || method->IsProxyMethod()) {
77 return false;
78 }
79 if (method->IsIntrinsic()) {
80 return false;
81 }
82 if (method->GetDeclaringClass()->IsStringClass() && method->IsConstructor()) {
83 return false;
84 }
Vladimir Markobaa81b52019-08-02 10:14:04 +010085 if (method->IsStatic() && !method->GetDeclaringClass()->IsVisiblyInitialized()) {
David Srbecky9581e612018-10-30 14:29:43 +000086 return false;
87 }
88 ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
89 if ((profiling_info != nullptr) && (profiling_info->GetSavedEntryPoint() != nullptr)) {
90 return false;
91 }
92 return true;
93}
94
Chang Xingbd208d82017-07-12 14:53:17 -070095template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
96 bool transaction_active>
Ian Rogers54874942014-06-10 16:31:03 -070097bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
98 uint16_t inst_data) {
99 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
100 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100101 ArtField* f =
102 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
103 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -0700104 if (UNLIKELY(f == nullptr)) {
105 CHECK(self->IsExceptionPending());
106 return false;
107 }
Mathieu Chartieref41db72016-10-25 15:08:01 -0700108 ObjPtr<mirror::Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -0700109 if (is_static) {
110 obj = f->GetDeclaringClass();
Chang Xingbd208d82017-07-12 14:53:17 -0700111 if (transaction_active) {
Vladimir Marko672c0802019-07-26 13:03:13 +0100112 if (Runtime::Current()->GetTransaction()->ReadConstraint(self, obj, f)) {
Chang Xingbd208d82017-07-12 14:53:17 -0700113 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Can't read static fields of "
114 + obj->PrettyTypeOf() + " since it does not belong to clinit's class.");
115 return false;
116 }
117 }
Ian Rogers54874942014-06-10 16:31:03 -0700118 } else {
119 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
120 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000121 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers54874942014-06-10 16:31:03 -0700122 return false;
123 }
124 }
Orion Hodson3d617ac2016-10-19 14:00:46 +0100125
126 JValue result;
Alex Light084fa372017-06-16 08:58:34 -0700127 if (UNLIKELY(!DoFieldGetCommon<field_type>(self, shadow_frame, obj, f, &result))) {
128 // Instrumentation threw an error!
129 CHECK(self->IsExceptionPending());
130 return false;
131 }
Ian Rogers54874942014-06-10 16:31:03 -0700132 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
133 switch (field_type) {
134 case Primitive::kPrimBoolean:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100135 shadow_frame.SetVReg(vregA, result.GetZ());
Ian Rogers54874942014-06-10 16:31:03 -0700136 break;
137 case Primitive::kPrimByte:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100138 shadow_frame.SetVReg(vregA, result.GetB());
Ian Rogers54874942014-06-10 16:31:03 -0700139 break;
140 case Primitive::kPrimChar:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100141 shadow_frame.SetVReg(vregA, result.GetC());
Ian Rogers54874942014-06-10 16:31:03 -0700142 break;
143 case Primitive::kPrimShort:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100144 shadow_frame.SetVReg(vregA, result.GetS());
Ian Rogers54874942014-06-10 16:31:03 -0700145 break;
146 case Primitive::kPrimInt:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100147 shadow_frame.SetVReg(vregA, result.GetI());
Ian Rogers54874942014-06-10 16:31:03 -0700148 break;
149 case Primitive::kPrimLong:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100150 shadow_frame.SetVRegLong(vregA, result.GetJ());
Ian Rogers54874942014-06-10 16:31:03 -0700151 break;
152 case Primitive::kPrimNot:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100153 shadow_frame.SetVRegReference(vregA, result.GetL());
Ian Rogers54874942014-06-10 16:31:03 -0700154 break;
155 default:
156 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700157 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700158 }
159 return true;
160}
161
162// Explicitly instantiate all DoFieldGet functions.
Chang Xingbd208d82017-07-12 14:53:17 -0700163#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
164 template bool DoFieldGet<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
Ian Rogers54874942014-06-10 16:31:03 -0700165 ShadowFrame& shadow_frame, \
166 const Instruction* inst, \
167 uint16_t inst_data)
168
169#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
Chang Xingbd208d82017-07-12 14:53:17 -0700170 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false, true); \
171 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false, false); \
172 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true, true); \
173 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true, false);
Ian Rogers54874942014-06-10 16:31:03 -0700174
175// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700176EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
177EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
178EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
179EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
180EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
181EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
182EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700183
184// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700185EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
186EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
187EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
188EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
189EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
190EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
191EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700192
193#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
194#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
195
196// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
197// Returns true on success, otherwise throws an exception and returns false.
198template<Primitive::Type field_type>
199bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700200 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700201 if (UNLIKELY(obj == nullptr)) {
202 // We lost the reference to the field index so we cannot get a more
203 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000204 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700205 return false;
206 }
207 MemberOffset field_offset(inst->VRegC_22c());
208 // Report this field access to instrumentation if needed. Since we only have the offset of
209 // the field from the base of the object, we need to look for it first.
210 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
211 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
212 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
213 field_offset.Uint32Value());
214 DCHECK(f != nullptr);
215 DCHECK(!f->IsStatic());
Alex Light084fa372017-06-16 08:58:34 -0700216 Thread* self = Thread::Current();
217 StackHandleScope<1> hs(self);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700218 // Save obj in case the instrumentation event has thread suspension.
219 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Alex Light084fa372017-06-16 08:58:34 -0700220 instrumentation->FieldReadEvent(self,
Vladimir Marko19711d42019-04-12 14:05:34 +0100221 obj,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700222 shadow_frame.GetMethod(),
223 shadow_frame.GetDexPC(),
224 f);
Alex Light084fa372017-06-16 08:58:34 -0700225 if (UNLIKELY(self->IsExceptionPending())) {
226 return false;
227 }
Ian Rogers54874942014-06-10 16:31:03 -0700228 }
229 // Note: iget-x-quick instructions are only for non-volatile fields.
230 const uint32_t vregA = inst->VRegA_22c(inst_data);
231 switch (field_type) {
232 case Primitive::kPrimInt:
233 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
234 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800235 case Primitive::kPrimBoolean:
236 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
237 break;
238 case Primitive::kPrimByte:
239 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
240 break;
241 case Primitive::kPrimChar:
242 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
243 break;
244 case Primitive::kPrimShort:
245 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
246 break;
Ian Rogers54874942014-06-10 16:31:03 -0700247 case Primitive::kPrimLong:
248 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
249 break;
250 case Primitive::kPrimNot:
251 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
252 break;
253 default:
254 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700255 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700256 }
257 return true;
258}
259
260// Explicitly instantiate all DoIGetQuick functions.
261#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
262 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
263 uint16_t inst_data)
264
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800265EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
266EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
267EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
268EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
269EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
270EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
271EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700272#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
273
274template<Primitive::Type field_type>
275static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700276 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700277 JValue field_value;
278 switch (field_type) {
279 case Primitive::kPrimBoolean:
280 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
281 break;
282 case Primitive::kPrimByte:
283 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
284 break;
285 case Primitive::kPrimChar:
286 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
287 break;
288 case Primitive::kPrimShort:
289 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
290 break;
291 case Primitive::kPrimInt:
292 field_value.SetI(shadow_frame.GetVReg(vreg));
293 break;
294 case Primitive::kPrimLong:
295 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
296 break;
297 case Primitive::kPrimNot:
298 field_value.SetL(shadow_frame.GetVRegReference(vreg));
299 break;
300 default:
301 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700302 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700303 }
304 return field_value;
305}
306
Orion Hodson3d617ac2016-10-19 14:00:46 +0100307template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
308 bool transaction_active>
309bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
310 uint16_t inst_data) {
311 const bool do_assignability_check = do_access_check;
312 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
313 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
314 ArtField* f =
315 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
316 Primitive::ComponentSize(field_type));
317 if (UNLIKELY(f == nullptr)) {
318 CHECK(self->IsExceptionPending());
319 return false;
320 }
321 ObjPtr<mirror::Object> obj;
322 if (is_static) {
323 obj = f->GetDeclaringClass();
324 } else {
325 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
326 if (UNLIKELY(obj == nullptr)) {
327 ThrowNullPointerExceptionForFieldAccess(f, false);
328 return false;
329 }
330 }
Vladimir Marko672c0802019-07-26 13:03:13 +0100331 if (transaction_active) {
332 Runtime* runtime = Runtime::Current();
333 if (runtime->GetTransaction()->WriteConstraint(self, obj, f)) {
334 if (is_static) {
335 runtime->AbortTransactionAndThrowAbortError(
336 self, "Can't set fields of " + obj->PrettyTypeOf());
337 } else {
338 // This can happen only when compiling a boot image extension.
339 DCHECK(!runtime->GetTransaction()->IsStrict());
340 DCHECK(runtime->GetHeap()->ObjectIsInBootImageSpace(obj));
341 runtime->AbortTransactionAndThrowAbortError(
342 self, "Can't set fields of boot image objects");
343 }
344 return false;
345 }
346 }
Orion Hodson3d617ac2016-10-19 14:00:46 +0100347
348 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100349 JValue value = GetFieldValue<field_type>(shadow_frame, vregA);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100350 return DoFieldPutCommon<field_type, do_assignability_check, transaction_active>(self,
351 shadow_frame,
352 obj,
353 f,
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100354 value);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100355}
356
Ian Rogers54874942014-06-10 16:31:03 -0700357// Explicitly instantiate all DoFieldPut functions.
358#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
359 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
360 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
361
362#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
363 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
364 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
365 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
366 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
367
368// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700369EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
370EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
371EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
372EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
373EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
374EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
375EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700376
377// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700378EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
379EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
380EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
381EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
382EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
383EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
384EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700385
386#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
387#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
388
389template<Primitive::Type field_type, bool transaction_active>
390bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700391 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700392 if (UNLIKELY(obj == nullptr)) {
393 // We lost the reference to the field index so we cannot get a more
394 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000395 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700396 return false;
397 }
398 MemberOffset field_offset(inst->VRegC_22c());
399 const uint32_t vregA = inst->VRegA_22c(inst_data);
400 // Report this field modification to instrumentation if needed. Since we only have the offset of
401 // the field from the base of the object, we need to look for it first.
402 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
403 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
404 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
405 field_offset.Uint32Value());
406 DCHECK(f != nullptr);
407 DCHECK(!f->IsStatic());
408 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Alex Light084fa372017-06-16 08:58:34 -0700409 Thread* self = Thread::Current();
410 StackHandleScope<2> hs(self);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700411 // Save obj in case the instrumentation event has thread suspension.
412 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Alex Light084fa372017-06-16 08:58:34 -0700413 mirror::Object* fake_root = nullptr;
414 HandleWrapper<mirror::Object> ret(hs.NewHandleWrapper<mirror::Object>(
415 field_type == Primitive::kPrimNot ? field_value.GetGCRoot() : &fake_root));
416 instrumentation->FieldWriteEvent(self,
Vladimir Marko19711d42019-04-12 14:05:34 +0100417 obj,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700418 shadow_frame.GetMethod(),
419 shadow_frame.GetDexPC(),
420 f,
421 field_value);
Alex Light084fa372017-06-16 08:58:34 -0700422 if (UNLIKELY(self->IsExceptionPending())) {
423 return false;
424 }
Alex Light0aa7a5a2018-10-10 15:58:14 +0000425 if (UNLIKELY(shadow_frame.GetForcePopFrame())) {
426 // Don't actually set the field. The next instruction will force us to pop.
427 DCHECK(Runtime::Current()->AreNonStandardExitsEnabled());
Alex Light0aa7a5a2018-10-10 15:58:14 +0000428 return true;
429 }
Ian Rogers54874942014-06-10 16:31:03 -0700430 }
431 // Note: iput-x-quick instructions are only for non-volatile fields.
432 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700433 case Primitive::kPrimBoolean:
434 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
435 break;
436 case Primitive::kPrimByte:
437 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
438 break;
439 case Primitive::kPrimChar:
440 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
441 break;
442 case Primitive::kPrimShort:
443 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
444 break;
Ian Rogers54874942014-06-10 16:31:03 -0700445 case Primitive::kPrimInt:
446 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
447 break;
448 case Primitive::kPrimLong:
449 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
450 break;
451 case Primitive::kPrimNot:
452 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
453 break;
454 default:
455 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700456 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700457 }
458 return true;
459}
460
461// Explicitly instantiate all DoIPutQuick functions.
462#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
463 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
464 const Instruction* inst, \
465 uint16_t inst_data)
466
467#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
468 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
469 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
470
Andreas Gampec8ccf682014-09-29 20:07:43 -0700471EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
472EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
473EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
474EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
475EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
476EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
477EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700478#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
479#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
480
Alex Lightb7c640d2019-03-20 15:52:13 -0700481template <typename T>
482bool SendMethodExitEvents(Thread* self,
483 const instrumentation::Instrumentation* instrumentation,
484 ShadowFrame& frame,
485 ObjPtr<mirror::Object> thiz,
486 ArtMethod* method,
487 uint32_t dex_pc,
488 T& result) {
489 bool had_event = false;
490 // We can get additional ForcePopFrame requests during handling of these events. We should
491 // respect these and send additional instrumentation events.
492 StackHandleScope<1> hs(self);
493 Handle<mirror::Object> h_thiz(hs.NewHandle(thiz));
494 do {
495 frame.SetForcePopFrame(false);
496 if (UNLIKELY(instrumentation->HasMethodExitListeners() && !frame.GetSkipMethodExitEvents())) {
497 had_event = true;
498 instrumentation->MethodExitEvent(
499 self, h_thiz.Get(), method, dex_pc, instrumentation::OptionalFrame{ frame }, result);
500 }
501 // We don't send method-exit if it's a pop-frame. We still send frame_popped though.
502 if (UNLIKELY(frame.NeedsNotifyPop() && instrumentation->HasWatchedFramePopListeners())) {
503 had_event = true;
504 instrumentation->WatchedFramePopped(self, frame);
505 }
506 } while (UNLIKELY(frame.GetForcePopFrame()));
507 if (UNLIKELY(had_event)) {
508 return !self->IsExceptionPending();
509 } else {
510 return true;
511 }
512}
513
514template
515bool SendMethodExitEvents(Thread* self,
516 const instrumentation::Instrumentation* instrumentation,
517 ShadowFrame& frame,
518 ObjPtr<mirror::Object> thiz,
519 ArtMethod* method,
520 uint32_t dex_pc,
521 MutableHandle<mirror::Object>& result);
522
523template
524bool SendMethodExitEvents(Thread* self,
525 const instrumentation::Instrumentation* instrumentation,
526 ShadowFrame& frame,
527 ObjPtr<mirror::Object> thiz,
528 ArtMethod* method,
529 uint32_t dex_pc,
530 JValue& result);
531
Alex Light9fb1ab12017-09-05 09:32:49 -0700532// We execute any instrumentation events that are triggered by this exception and change the
533// shadow_frame's dex_pc to that of the exception handler if there is one in the current method.
534// Return true if we should continue executing in the current method and false if we need to go up
535// the stack to find an exception handler.
Sebastien Hertz520633b2015-09-08 17:03:36 +0200536// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Alex Light9fb1ab12017-09-05 09:32:49 -0700537// TODO We should have a better way to skip instrumentation reporting or possibly rethink that
538// behavior.
539bool MoveToExceptionHandler(Thread* self,
540 ShadowFrame& shadow_frame,
541 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700542 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700543 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000544 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Alex Light9fb1ab12017-09-05 09:32:49 -0700545 if (instrumentation != nullptr &&
546 instrumentation->HasExceptionThrownListeners() &&
547 self->IsExceptionThrownByCurrentMethod(exception.Get())) {
548 // 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 -0700549 instrumentation->ExceptionThrownEvent(self, exception.Get());
Alex Light0aa7a5a2018-10-10 15:58:14 +0000550 if (shadow_frame.GetForcePopFrame()) {
551 // We will check in the caller for GetForcePopFrame again. We need to bail out early to
552 // prevent an ExceptionHandledEvent from also being sent before popping.
553 return true;
554 }
Sebastien Hertz9f102032014-05-23 08:59:42 +0200555 }
Ian Rogers54874942014-06-10 16:31:03 -0700556 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700557 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
Alex Light9fb1ab12017-09-05 09:32:49 -0700558 hs.NewHandle(exception->GetClass()), shadow_frame.GetDexPC(), &clear_exception);
Andreas Gampee2abbc62017-09-15 11:59:26 -0700559 if (found_dex_pc == dex::kDexNoIndex) {
Alex Light9fb1ab12017-09-05 09:32:49 -0700560 if (instrumentation != nullptr) {
561 if (shadow_frame.NeedsNotifyPop()) {
562 instrumentation->WatchedFramePopped(self, shadow_frame);
Alex Lightb7c640d2019-03-20 15:52:13 -0700563 if (shadow_frame.GetForcePopFrame()) {
564 // We will check in the caller for GetForcePopFrame again. We need to bail out early to
565 // prevent an ExceptionHandledEvent from also being sent before popping and to ensure we
566 // handle other types of non-standard-exits.
567 return true;
568 }
Alex Light9fb1ab12017-09-05 09:32:49 -0700569 }
570 // Exception is not caught by the current method. We will unwind to the
571 // caller. Notify any instrumentation listener.
572 instrumentation->MethodUnwindEvent(self,
573 shadow_frame.GetThisObject(),
574 shadow_frame.GetMethod(),
575 shadow_frame.GetDexPC());
Alex Lighte814f9d2017-07-31 16:14:39 -0700576 }
Alex Lightb7c640d2019-03-20 15:52:13 -0700577 return shadow_frame.GetForcePopFrame();
Ian Rogers54874942014-06-10 16:31:03 -0700578 } else {
Alex Light9fb1ab12017-09-05 09:32:49 -0700579 shadow_frame.SetDexPC(found_dex_pc);
580 if (instrumentation != nullptr && instrumentation->HasExceptionHandledListeners()) {
581 self->ClearException();
582 instrumentation->ExceptionHandledEvent(self, exception.Get());
583 if (UNLIKELY(self->IsExceptionPending())) {
584 // Exception handled event threw an exception. Try to find the handler for this one.
585 return MoveToExceptionHandler(self, shadow_frame, instrumentation);
586 } else if (!clear_exception) {
587 self->SetException(exception.Get());
588 }
589 } else if (clear_exception) {
Ian Rogers54874942014-06-10 16:31:03 -0700590 self->ClearException();
591 }
Alex Light9fb1ab12017-09-05 09:32:49 -0700592 return true;
Ian Rogers54874942014-06-10 16:31:03 -0700593 }
Ian Rogers54874942014-06-10 16:31:03 -0700594}
595
Ian Rogerse94652f2014-12-02 11:13:19 -0800596void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
597 LOG(FATAL) << "Unexpected instruction: "
598 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
599 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700600}
601
Sebastien Hertz45b15972015-04-03 16:07:05 +0200602void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700603 va_list args;
604 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200605 AbortTransactionV(self, fmt, args);
606 va_end(args);
607}
608
609void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
610 CHECK(Runtime::Current()->IsActiveTransaction());
611 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100612 std::string abort_msg;
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800613 android::base::StringAppendV(&abort_msg, fmt, args);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100614 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200615 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700616}
617
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100618// START DECLARATIONS :
619//
620// These additional declarations are required because clang complains
621// about ALWAYS_INLINE (-Werror, -Wgcc-compat) in definitions.
622//
623
Narayan Kamath9823e782016-08-03 12:46:58 +0100624template <bool is_range, bool do_assignability_check>
Vladimir Markod16363a2017-02-01 14:09:13 +0000625static ALWAYS_INLINE bool DoCallCommon(ArtMethod* called_method,
626 Thread* self,
627 ShadowFrame& shadow_frame,
628 JValue* result,
629 uint16_t number_of_inputs,
630 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
631 uint32_t vregC) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100632
633template <bool is_range>
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000634ALWAYS_INLINE void CopyRegisters(ShadowFrame& caller_frame,
635 ShadowFrame* callee_frame,
636 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
637 const size_t first_src_reg,
638 const size_t first_dest_reg,
639 const size_t num_regs) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100640
641// END DECLARATIONS.
642
Siva Chandra05d24152016-01-05 17:43:17 -0800643void ArtInterpreterToCompiledCodeBridge(Thread* self,
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100644 ArtMethod* caller,
Siva Chandra05d24152016-01-05 17:43:17 -0800645 ShadowFrame* shadow_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -0700646 uint16_t arg_offset,
Siva Chandra05d24152016-01-05 17:43:17 -0800647 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700648 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700649 ArtMethod* method = shadow_frame->GetMethod();
650 // Ensure static methods are initialized.
651 if (method->IsStatic()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700652 ObjPtr<mirror::Class> declaringClass = method->GetDeclaringClass();
Vladimir Markobaa81b52019-08-02 10:14:04 +0100653 if (UNLIKELY(!declaringClass->IsVisiblyInitialized())) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700654 self->PushShadowFrame(shadow_frame);
655 StackHandleScope<1> hs(self);
656 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
Vladimir Markobaa81b52019-08-02 10:14:04 +0100657 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(
658 self, h_class, /*can_init_fields=*/ true, /*can_init_parents=*/ true))) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700659 self->PopShadowFrame();
660 DCHECK(self->IsExceptionPending());
661 return;
662 }
663 self->PopShadowFrame();
Vladimir Markobaa81b52019-08-02 10:14:04 +0100664 DCHECK(h_class->IsInitializing());
Nicolas Geoffray01822292017-03-09 09:03:19 +0000665 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
666 method = shadow_frame->GetMethod();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700667 }
668 }
Jeff Hao5ea84132017-05-05 16:59:29 -0700669 // Basic checks for the arg_offset. If there's no code item, the arg_offset must be 0. Otherwise,
670 // check that the arg_offset isn't greater than the number of registers. A stronger check is
671 // difficult since the frame may contain space for all the registers in the method, or only enough
672 // space for the arguments.
673 if (kIsDebugBuild) {
674 if (method->GetCodeItem() == nullptr) {
675 DCHECK_EQ(0u, arg_offset) << method->PrettyMethod();
676 } else {
677 DCHECK_LE(arg_offset, shadow_frame->NumberOfVRegs());
678 }
679 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100680 jit::Jit* jit = Runtime::Current()->GetJit();
681 if (jit != nullptr && caller != nullptr) {
682 jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
683 }
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700684 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
685 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
Andreas Gampe542451c2016-07-26 09:02:02 -0700686 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700687}
688
Mingyao Yangffedec52016-05-19 10:48:40 -0700689void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
690 uint16_t this_obj_vreg,
691 JValue result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700692 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700693 ObjPtr<mirror::Object> existing = shadow_frame->GetVRegReference(this_obj_vreg);
Mingyao Yangffedec52016-05-19 10:48:40 -0700694 if (existing == nullptr) {
695 // If it's null, we come from compiled code that was deoptimized. Nothing to do,
696 // as the compiler verified there was no alias.
697 // Set the new string result of the StringFactory.
698 shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
699 return;
700 }
701 // Set the string init result into all aliases.
702 for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
703 if (shadow_frame->GetVRegReference(i) == existing) {
704 DCHECK_EQ(shadow_frame->GetVRegReference(i),
Vladimir Marko78baed52018-10-11 10:44:58 +0100705 reinterpret_cast32<mirror::Object*>(shadow_frame->GetVReg(i)));
Mingyao Yangffedec52016-05-19 10:48:40 -0700706 shadow_frame->SetVRegReference(i, result.GetL());
707 DCHECK_EQ(shadow_frame->GetVRegReference(i),
Vladimir Marko78baed52018-10-11 10:44:58 +0100708 reinterpret_cast32<mirror::Object*>(shadow_frame->GetVReg(i)));
Mingyao Yangffedec52016-05-19 10:48:40 -0700709 }
710 }
711}
712
Orion Hodsonc069a302017-01-18 09:23:12 +0000713template<bool is_range>
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100714static bool DoMethodHandleInvokeCommon(Thread* self,
715 ShadowFrame& shadow_frame,
716 bool invoke_exact,
717 const Instruction* inst,
718 uint16_t inst_data,
719 JValue* result)
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100720 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Light848574c2017-09-25 16:59:39 -0700721 // Make sure to check for async exceptions
722 if (UNLIKELY(self->ObserveAsyncException())) {
723 return false;
724 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100725 // Invoke-polymorphic instructions always take a receiver. i.e, they are never static.
726 const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc();
Orion Hodson3d617ac2016-10-19 14:00:46 +0100727 const int invoke_method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc();
Narayan Kamath9823e782016-08-03 12:46:58 +0100728
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000729 // Initialize |result| to 0 as this is the default return value for
730 // polymorphic invocations of method handle types with void return
731 // and provides sane return result in error cases.
732 result->SetJ(0);
733
Orion Hodson3d617ac2016-10-19 14:00:46 +0100734 // The invoke_method_idx here is the name of the signature polymorphic method that
Narayan Kamath9823e782016-08-03 12:46:58 +0100735 // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact)
736 // and not the method that we'll dispatch to in the end.
Orion Hodsone7732be2017-10-11 14:35:20 +0100737 StackHandleScope<2> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +0000738 Handle<mirror::MethodHandle> method_handle(hs.NewHandle(
Vladimir Markod7e9bbf2019-03-28 13:18:57 +0000739 ObjPtr<mirror::MethodHandle>::DownCast(shadow_frame.GetVRegReference(vRegC))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800740 if (UNLIKELY(method_handle == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100741 // Note that the invoke type is kVirtual here because a call to a signature
742 // polymorphic method is shaped like a virtual call at the bytecode level.
Orion Hodson3d617ac2016-10-19 14:00:46 +0100743 ThrowNullPointerExceptionForMethodAccess(invoke_method_idx, InvokeType::kVirtual);
Narayan Kamath9823e782016-08-03 12:46:58 +0100744 return false;
745 }
746
747 // The vRegH value gives the index of the proto_id associated with this
Orion Hodson811bd5f2016-12-07 11:35:37 +0000748 // signature polymorphic call site.
Orion Hodson06d10a72018-05-14 08:53:38 +0100749 const uint16_t vRegH = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
750 const dex::ProtoIndex callsite_proto_id(vRegH);
Narayan Kamath9823e782016-08-03 12:46:58 +0100751
752 // Call through to the classlinker and ask it to resolve the static type associated
753 // with the callsite. This information is stored in the dex cache so it's
754 // guaranteed to be fast after the first resolution.
Narayan Kamath9823e782016-08-03 12:46:58 +0100755 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Orion Hodsone7732be2017-10-11 14:35:20 +0100756 Handle<mirror::MethodType> callsite_type(hs.NewHandle(
757 class_linker->ResolveMethodType(self, callsite_proto_id, shadow_frame.GetMethod())));
Narayan Kamath9823e782016-08-03 12:46:58 +0100758
759 // This implies we couldn't resolve one or more types in this method handle.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800760 if (UNLIKELY(callsite_type == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100761 CHECK(self->IsExceptionPending());
Narayan Kamath9823e782016-08-03 12:46:58 +0100762 return false;
763 }
764
Orion Hodson811bd5f2016-12-07 11:35:37 +0000765 // There is a common dispatch method for method handles that takes
766 // arguments either from a range or an array of arguments depending
767 // on whether the DEX instruction is invoke-polymorphic/range or
768 // invoke-polymorphic. The array here is for the latter.
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100769 if (UNLIKELY(is_range)) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000770 // VRegC is the register holding the method handle. Arguments passed
771 // to the method handle's target do not include the method handle.
Orion Hodson960d4f72017-11-10 15:32:38 +0000772 RangeInstructionOperands operands(inst->VRegC_4rcc() + 1, inst->VRegA_4rcc() - 1);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100773 if (invoke_exact) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000774 return MethodHandleInvokeExact(self,
775 shadow_frame,
776 method_handle,
777 callsite_type,
778 &operands,
779 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100780 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000781 return MethodHandleInvoke(self,
782 shadow_frame,
783 method_handle,
784 callsite_type,
785 &operands,
786 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100787 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100788 } else {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000789 // Get the register arguments for the invoke.
Orion Hodson960d4f72017-11-10 15:32:38 +0000790 uint32_t args[Instruction::kMaxVarArgRegs] = {};
Orion Hodson811bd5f2016-12-07 11:35:37 +0000791 inst->GetVarArgs(args, inst_data);
792 // Drop the first register which is the method handle performing the invoke.
Alexey Frunze8631a462017-01-19 19:07:37 -0800793 memmove(args, args + 1, sizeof(args[0]) * (Instruction::kMaxVarArgRegs - 1));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000794 args[Instruction::kMaxVarArgRegs - 1] = 0;
Orion Hodson960d4f72017-11-10 15:32:38 +0000795 VarArgsInstructionOperands operands(args, inst->VRegA_45cc() - 1);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100796 if (invoke_exact) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000797 return MethodHandleInvokeExact(self,
798 shadow_frame,
799 method_handle,
800 callsite_type,
801 &operands,
802 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100803 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000804 return MethodHandleInvoke(self,
805 shadow_frame,
806 method_handle,
807 callsite_type,
808 &operands,
809 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100810 }
811 }
812}
813
814bool DoMethodHandleInvokeExact(Thread* self,
815 ShadowFrame& shadow_frame,
816 const Instruction* inst,
817 uint16_t inst_data,
818 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
819 if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) {
820 static const bool kIsRange = false;
821 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700822 self, shadow_frame, /* invoke_exact= */ true, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100823 } else {
824 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE);
825 static const bool kIsRange = true;
826 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700827 self, shadow_frame, /* invoke_exact= */ true, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100828 }
829}
830
831bool DoMethodHandleInvoke(Thread* self,
832 ShadowFrame& shadow_frame,
833 const Instruction* inst,
834 uint16_t inst_data,
835 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
836 if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) {
837 static const bool kIsRange = false;
838 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700839 self, shadow_frame, /* invoke_exact= */ false, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100840 } else {
841 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE);
842 static const bool kIsRange = true;
843 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700844 self, shadow_frame, /* invoke_exact= */ false, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100845 }
846}
847
Orion Hodson928033d2018-02-07 05:30:54 +0000848static bool DoVarHandleInvokeCommon(Thread* self,
849 ShadowFrame& shadow_frame,
850 const Instruction* inst,
851 uint16_t inst_data,
852 JValue* result,
853 mirror::VarHandle::AccessMode access_mode)
854 REQUIRES_SHARED(Locks::mutator_lock_) {
855 // Make sure to check for async exceptions
856 if (UNLIKELY(self->ObserveAsyncException())) {
857 return false;
858 }
859
Orion Hodson928033d2018-02-07 05:30:54 +0000860 StackHandleScope<2> hs(self);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100861 bool is_var_args = inst->HasVarArgs();
Orion Hodson06d10a72018-05-14 08:53:38 +0100862 const uint16_t vRegH = is_var_args ? inst->VRegH_45cc() : inst->VRegH_4rcc();
Orion Hodson928033d2018-02-07 05:30:54 +0000863 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
864 Handle<mirror::MethodType> callsite_type(hs.NewHandle(
Orion Hodson06d10a72018-05-14 08:53:38 +0100865 class_linker->ResolveMethodType(self, dex::ProtoIndex(vRegH), shadow_frame.GetMethod())));
Orion Hodson928033d2018-02-07 05:30:54 +0000866 // This implies we couldn't resolve one or more types in this VarHandle.
867 if (UNLIKELY(callsite_type == nullptr)) {
868 CHECK(self->IsExceptionPending());
869 return false;
870 }
871
Orion Hodson537a4fe2018-05-15 13:57:58 +0100872 const uint32_t vRegC = is_var_args ? inst->VRegC_45cc() : inst->VRegC_4rcc();
873 ObjPtr<mirror::Object> receiver(shadow_frame.GetVRegReference(vRegC));
Vladimir Marko179b7c62019-03-22 13:38:57 +0000874 Handle<mirror::VarHandle> var_handle(hs.NewHandle(ObjPtr<mirror::VarHandle>::DownCast(receiver)));
Orion Hodson928033d2018-02-07 05:30:54 +0000875 if (is_var_args) {
876 uint32_t args[Instruction::kMaxVarArgRegs];
877 inst->GetVarArgs(args, inst_data);
878 VarArgsInstructionOperands all_operands(args, inst->VRegA_45cc());
879 NoReceiverInstructionOperands operands(&all_operands);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100880 return VarHandleInvokeAccessor(self,
881 shadow_frame,
882 var_handle,
883 callsite_type,
884 access_mode,
885 &operands,
886 result);
Orion Hodson928033d2018-02-07 05:30:54 +0000887 } else {
888 RangeInstructionOperands all_operands(inst->VRegC_4rcc(), inst->VRegA_4rcc());
889 NoReceiverInstructionOperands operands(&all_operands);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100890 return VarHandleInvokeAccessor(self,
891 shadow_frame,
892 var_handle,
893 callsite_type,
894 access_mode,
895 &operands,
896 result);
Orion Hodson928033d2018-02-07 05:30:54 +0000897 }
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100898}
899
Orion Hodson928033d2018-02-07 05:30:54 +0000900#define DO_VAR_HANDLE_ACCESSOR(_access_mode) \
901bool DoVarHandle ## _access_mode(Thread* self, \
902 ShadowFrame& shadow_frame, \
903 const Instruction* inst, \
904 uint16_t inst_data, \
905 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { \
906 const auto access_mode = mirror::VarHandle::AccessMode::k ## _access_mode; \
907 return DoVarHandleInvokeCommon(self, shadow_frame, inst, inst_data, result, access_mode); \
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100908}
909
Orion Hodson928033d2018-02-07 05:30:54 +0000910DO_VAR_HANDLE_ACCESSOR(CompareAndExchange)
911DO_VAR_HANDLE_ACCESSOR(CompareAndExchangeAcquire)
912DO_VAR_HANDLE_ACCESSOR(CompareAndExchangeRelease)
913DO_VAR_HANDLE_ACCESSOR(CompareAndSet)
914DO_VAR_HANDLE_ACCESSOR(Get)
915DO_VAR_HANDLE_ACCESSOR(GetAcquire)
916DO_VAR_HANDLE_ACCESSOR(GetAndAdd)
917DO_VAR_HANDLE_ACCESSOR(GetAndAddAcquire)
918DO_VAR_HANDLE_ACCESSOR(GetAndAddRelease)
919DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAnd)
920DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAndAcquire)
921DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAndRelease)
922DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOr)
923DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOrAcquire)
924DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOrRelease)
925DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXor)
926DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXorAcquire)
927DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXorRelease)
928DO_VAR_HANDLE_ACCESSOR(GetAndSet)
929DO_VAR_HANDLE_ACCESSOR(GetAndSetAcquire)
930DO_VAR_HANDLE_ACCESSOR(GetAndSetRelease)
931DO_VAR_HANDLE_ACCESSOR(GetOpaque)
932DO_VAR_HANDLE_ACCESSOR(GetVolatile)
933DO_VAR_HANDLE_ACCESSOR(Set)
934DO_VAR_HANDLE_ACCESSOR(SetOpaque)
935DO_VAR_HANDLE_ACCESSOR(SetRelease)
936DO_VAR_HANDLE_ACCESSOR(SetVolatile)
937DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSet)
938DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetAcquire)
939DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetPlain)
940DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetRelease)
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100941
Orion Hodson928033d2018-02-07 05:30:54 +0000942#undef DO_VAR_HANDLE_ACCESSOR
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100943
944template<bool is_range>
945bool DoInvokePolymorphic(Thread* self,
946 ShadowFrame& shadow_frame,
947 const Instruction* inst,
948 uint16_t inst_data,
949 JValue* result) {
950 const int invoke_method_idx = inst->VRegB();
951 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
952 ArtMethod* invoke_method =
953 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
954 self, invoke_method_idx, shadow_frame.GetMethod(), kVirtual);
955
956 // Ensure intrinsic identifiers are initialized.
957 DCHECK(invoke_method->IsIntrinsic());
958
959 // Dispatch based on intrinsic identifier associated with method.
960 switch (static_cast<art::Intrinsics>(invoke_method->GetIntrinsic())) {
961#define CASE_SIGNATURE_POLYMORPHIC_INTRINSIC(Name, ...) \
962 case Intrinsics::k##Name: \
963 return Do ## Name(self, shadow_frame, inst, inst_data, result);
964#include "intrinsics_list.h"
965 SIGNATURE_POLYMORPHIC_INTRINSICS_LIST(CASE_SIGNATURE_POLYMORPHIC_INTRINSIC)
966#undef INTRINSICS_LIST
967#undef SIGNATURE_POLYMORPHIC_INTRINSICS_LIST
968#undef CASE_SIGNATURE_POLYMORPHIC_INTRINSIC
969 default:
970 LOG(FATAL) << "Unreachable: " << invoke_method->GetIntrinsic();
971 UNREACHABLE();
972 return false;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100973 }
974}
975
Orion Hodsona5dca522018-02-27 12:42:11 +0000976static JValue ConvertScalarBootstrapArgument(jvalue value) {
977 // value either contains a primitive scalar value if it corresponds
978 // to a primitive type, or it contains an integer value if it
979 // corresponds to an object instance reference id (e.g. a string id).
980 return JValue::FromPrimitive(value.j);
981}
982
983static ObjPtr<mirror::Class> GetClassForBootstrapArgument(EncodedArrayValueIterator::ValueType type)
984 REQUIRES_SHARED(Locks::mutator_lock_) {
985 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100986 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = class_linker->GetClassRoots();
Orion Hodsona5dca522018-02-27 12:42:11 +0000987 switch (type) {
988 case EncodedArrayValueIterator::ValueType::kBoolean:
989 case EncodedArrayValueIterator::ValueType::kByte:
990 case EncodedArrayValueIterator::ValueType::kChar:
991 case EncodedArrayValueIterator::ValueType::kShort:
992 // These types are disallowed by JVMS. Treat as integers. This
993 // will result in CCE's being raised if the BSM has one of these
994 // types.
995 case EncodedArrayValueIterator::ValueType::kInt:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100996 return GetClassRoot(ClassRoot::kPrimitiveInt, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000997 case EncodedArrayValueIterator::ValueType::kLong:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100998 return GetClassRoot(ClassRoot::kPrimitiveLong, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000999 case EncodedArrayValueIterator::ValueType::kFloat:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001000 return GetClassRoot(ClassRoot::kPrimitiveFloat, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +00001001 case EncodedArrayValueIterator::ValueType::kDouble:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001002 return GetClassRoot(ClassRoot::kPrimitiveDouble, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +00001003 case EncodedArrayValueIterator::ValueType::kMethodType:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001004 return GetClassRoot<mirror::MethodType>(class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +00001005 case EncodedArrayValueIterator::ValueType::kMethodHandle:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001006 return GetClassRoot<mirror::MethodHandle>(class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +00001007 case EncodedArrayValueIterator::ValueType::kString:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001008 return GetClassRoot<mirror::String>();
Orion Hodsona5dca522018-02-27 12:42:11 +00001009 case EncodedArrayValueIterator::ValueType::kType:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001010 return GetClassRoot<mirror::Class>();
Orion Hodsona5dca522018-02-27 12:42:11 +00001011 case EncodedArrayValueIterator::ValueType::kField:
1012 case EncodedArrayValueIterator::ValueType::kMethod:
1013 case EncodedArrayValueIterator::ValueType::kEnum:
1014 case EncodedArrayValueIterator::ValueType::kArray:
1015 case EncodedArrayValueIterator::ValueType::kAnnotation:
1016 case EncodedArrayValueIterator::ValueType::kNull:
1017 return nullptr;
1018 }
1019}
1020
1021static bool GetArgumentForBootstrapMethod(Thread* self,
1022 ArtMethod* referrer,
1023 EncodedArrayValueIterator::ValueType type,
1024 const JValue* encoded_value,
1025 JValue* decoded_value)
1026 REQUIRES_SHARED(Locks::mutator_lock_) {
1027 // The encoded_value contains either a scalar value (IJDF) or a
1028 // scalar DEX file index to a reference type to be materialized.
1029 switch (type) {
1030 case EncodedArrayValueIterator::ValueType::kInt:
1031 case EncodedArrayValueIterator::ValueType::kFloat:
1032 decoded_value->SetI(encoded_value->GetI());
1033 return true;
1034 case EncodedArrayValueIterator::ValueType::kLong:
1035 case EncodedArrayValueIterator::ValueType::kDouble:
1036 decoded_value->SetJ(encoded_value->GetJ());
1037 return true;
1038 case EncodedArrayValueIterator::ValueType::kMethodType: {
1039 StackHandleScope<2> hs(self);
1040 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader()));
1041 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
Orion Hodson06d10a72018-05-14 08:53:38 +01001042 dex::ProtoIndex proto_idx(encoded_value->GetC());
Orion Hodsona5dca522018-02-27 12:42:11 +00001043 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Orion Hodson06d10a72018-05-14 08:53:38 +01001044 ObjPtr<mirror::MethodType> o =
1045 cl->ResolveMethodType(self, proto_idx, dex_cache, class_loader);
Orion Hodsona5dca522018-02-27 12:42:11 +00001046 if (UNLIKELY(o.IsNull())) {
1047 DCHECK(self->IsExceptionPending());
1048 return false;
1049 }
1050 decoded_value->SetL(o);
1051 return true;
1052 }
1053 case EncodedArrayValueIterator::ValueType::kMethodHandle: {
1054 uint32_t index = static_cast<uint32_t>(encoded_value->GetI());
1055 ClassLinker* cl = Runtime::Current()->GetClassLinker();
1056 ObjPtr<mirror::MethodHandle> o = cl->ResolveMethodHandle(self, index, referrer);
1057 if (UNLIKELY(o.IsNull())) {
1058 DCHECK(self->IsExceptionPending());
1059 return false;
1060 }
1061 decoded_value->SetL(o);
1062 return true;
1063 }
1064 case EncodedArrayValueIterator::ValueType::kString: {
Orion Hodsona5dca522018-02-27 12:42:11 +00001065 dex::StringIndex index(static_cast<uint32_t>(encoded_value->GetI()));
1066 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Vladimir Marko18090d12018-06-01 16:53:12 +01001067 ObjPtr<mirror::String> o = cl->ResolveString(index, referrer);
Orion Hodsona5dca522018-02-27 12:42:11 +00001068 if (UNLIKELY(o.IsNull())) {
1069 DCHECK(self->IsExceptionPending());
1070 return false;
1071 }
1072 decoded_value->SetL(o);
1073 return true;
1074 }
1075 case EncodedArrayValueIterator::ValueType::kType: {
Orion Hodsona5dca522018-02-27 12:42:11 +00001076 dex::TypeIndex index(static_cast<uint32_t>(encoded_value->GetI()));
1077 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Vladimir Marko09c5ca42018-05-31 15:15:31 +01001078 ObjPtr<mirror::Class> o = cl->ResolveType(index, referrer);
Orion Hodsona5dca522018-02-27 12:42:11 +00001079 if (UNLIKELY(o.IsNull())) {
1080 DCHECK(self->IsExceptionPending());
1081 return false;
1082 }
1083 decoded_value->SetL(o);
1084 return true;
1085 }
1086 case EncodedArrayValueIterator::ValueType::kBoolean:
1087 case EncodedArrayValueIterator::ValueType::kByte:
1088 case EncodedArrayValueIterator::ValueType::kChar:
1089 case EncodedArrayValueIterator::ValueType::kShort:
1090 case EncodedArrayValueIterator::ValueType::kField:
1091 case EncodedArrayValueIterator::ValueType::kMethod:
1092 case EncodedArrayValueIterator::ValueType::kEnum:
1093 case EncodedArrayValueIterator::ValueType::kArray:
1094 case EncodedArrayValueIterator::ValueType::kAnnotation:
1095 case EncodedArrayValueIterator::ValueType::kNull:
1096 // Unreachable - unsupported types that have been checked when
1097 // determining the effect call site type based on the bootstrap
1098 // argument types.
1099 UNREACHABLE();
1100 }
1101}
1102
1103static bool PackArgumentForBootstrapMethod(Thread* self,
1104 ArtMethod* referrer,
1105 CallSiteArrayValueIterator* it,
1106 ShadowFrameSetter* setter)
1107 REQUIRES_SHARED(Locks::mutator_lock_) {
1108 auto type = it->GetValueType();
1109 const JValue encoded_value = ConvertScalarBootstrapArgument(it->GetJavaValue());
1110 JValue decoded_value;
1111 if (!GetArgumentForBootstrapMethod(self, referrer, type, &encoded_value, &decoded_value)) {
1112 return false;
1113 }
1114 switch (it->GetValueType()) {
1115 case EncodedArrayValueIterator::ValueType::kInt:
1116 case EncodedArrayValueIterator::ValueType::kFloat:
1117 setter->Set(static_cast<uint32_t>(decoded_value.GetI()));
1118 return true;
1119 case EncodedArrayValueIterator::ValueType::kLong:
1120 case EncodedArrayValueIterator::ValueType::kDouble:
1121 setter->SetLong(decoded_value.GetJ());
1122 return true;
1123 case EncodedArrayValueIterator::ValueType::kMethodType:
1124 case EncodedArrayValueIterator::ValueType::kMethodHandle:
1125 case EncodedArrayValueIterator::ValueType::kString:
1126 case EncodedArrayValueIterator::ValueType::kType:
1127 setter->SetReference(decoded_value.GetL());
1128 return true;
1129 case EncodedArrayValueIterator::ValueType::kBoolean:
1130 case EncodedArrayValueIterator::ValueType::kByte:
1131 case EncodedArrayValueIterator::ValueType::kChar:
1132 case EncodedArrayValueIterator::ValueType::kShort:
1133 case EncodedArrayValueIterator::ValueType::kField:
1134 case EncodedArrayValueIterator::ValueType::kMethod:
1135 case EncodedArrayValueIterator::ValueType::kEnum:
1136 case EncodedArrayValueIterator::ValueType::kArray:
1137 case EncodedArrayValueIterator::ValueType::kAnnotation:
1138 case EncodedArrayValueIterator::ValueType::kNull:
1139 // Unreachable - unsupported types that have been checked when
1140 // determining the effect call site type based on the bootstrap
1141 // argument types.
1142 UNREACHABLE();
1143 }
1144}
1145
1146static bool PackCollectorArrayForBootstrapMethod(Thread* self,
1147 ArtMethod* referrer,
1148 ObjPtr<mirror::Class> array_type,
1149 int32_t array_length,
1150 CallSiteArrayValueIterator* it,
1151 ShadowFrameSetter* setter)
1152 REQUIRES_SHARED(Locks::mutator_lock_) {
1153 StackHandleScope<1> hs(self);
1154 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1155 JValue decoded_value;
1156
1157#define COLLECT_PRIMITIVE_ARRAY(Descriptor, Type) \
1158 Handle<mirror::Type ## Array> array = \
1159 hs.NewHandle(mirror::Type ## Array::Alloc(self, array_length)); \
1160 if (array.IsNull()) { \
1161 return false; \
1162 } \
1163 for (int32_t i = 0; it->HasNext(); it->Next(), ++i) { \
1164 auto type = it->GetValueType(); \
1165 DCHECK_EQ(type, EncodedArrayValueIterator::ValueType::k ## Type); \
1166 const JValue encoded_value = \
1167 ConvertScalarBootstrapArgument(it->GetJavaValue()); \
1168 GetArgumentForBootstrapMethod(self, \
1169 referrer, \
1170 type, \
1171 &encoded_value, \
1172 &decoded_value); \
1173 array->Set(i, decoded_value.Get ## Descriptor()); \
1174 } \
1175 setter->SetReference(array.Get()); \
1176 return true;
1177
1178#define COLLECT_REFERENCE_ARRAY(T, Type) \
Andreas Gampe584771b2018-10-18 13:22:23 -07001179 Handle<mirror::ObjectArray<T>> array = /* NOLINT */ \
Orion Hodsona5dca522018-02-27 12:42:11 +00001180 hs.NewHandle(mirror::ObjectArray<T>::Alloc(self, \
1181 array_type, \
1182 array_length)); \
1183 if (array.IsNull()) { \
1184 return false; \
1185 } \
1186 for (int32_t i = 0; it->HasNext(); it->Next(), ++i) { \
1187 auto type = it->GetValueType(); \
1188 DCHECK_EQ(type, EncodedArrayValueIterator::ValueType::k ## Type); \
1189 const JValue encoded_value = \
1190 ConvertScalarBootstrapArgument(it->GetJavaValue()); \
1191 if (!GetArgumentForBootstrapMethod(self, \
1192 referrer, \
1193 type, \
1194 &encoded_value, \
1195 &decoded_value)) { \
1196 return false; \
1197 } \
1198 ObjPtr<mirror::Object> o = decoded_value.GetL(); \
1199 if (Runtime::Current()->IsActiveTransaction()) { \
1200 array->Set<true>(i, ObjPtr<T>::DownCast(o)); \
1201 } else { \
1202 array->Set<false>(i, ObjPtr<T>::DownCast(o)); \
1203 } \
1204 } \
1205 setter->SetReference(array.Get()); \
1206 return true;
1207
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001208 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = class_linker->GetClassRoots();
1209 ObjPtr<mirror::Class> component_type = array_type->GetComponentType();
1210 if (component_type == GetClassRoot(ClassRoot::kPrimitiveInt, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001211 COLLECT_PRIMITIVE_ARRAY(I, Int);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001212 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveLong, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001213 COLLECT_PRIMITIVE_ARRAY(J, Long);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001214 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveFloat, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001215 COLLECT_PRIMITIVE_ARRAY(F, Float);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001216 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveDouble, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001217 COLLECT_PRIMITIVE_ARRAY(D, Double);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001218 } else if (component_type == GetClassRoot<mirror::MethodType>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001219 COLLECT_REFERENCE_ARRAY(mirror::MethodType, MethodType);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001220 } else if (component_type == GetClassRoot<mirror::MethodHandle>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001221 COLLECT_REFERENCE_ARRAY(mirror::MethodHandle, MethodHandle);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001222 } else if (component_type == GetClassRoot<mirror::String>(class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001223 COLLECT_REFERENCE_ARRAY(mirror::String, String);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001224 } else if (component_type == GetClassRoot<mirror::Class>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001225 COLLECT_REFERENCE_ARRAY(mirror::Class, Type);
1226 } else {
1227 UNREACHABLE();
1228 }
1229 #undef COLLECT_PRIMITIVE_ARRAY
1230 #undef COLLECT_REFERENCE_ARRAY
1231}
1232
1233static ObjPtr<mirror::MethodType> BuildCallSiteForBootstrapMethod(Thread* self,
1234 const DexFile* dex_file,
1235 uint32_t call_site_idx)
1236 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001237 const dex::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx);
Orion Hodsona5dca522018-02-27 12:42:11 +00001238 CallSiteArrayValueIterator it(*dex_file, csi);
1239 DCHECK_GE(it.Size(), 1u);
1240
1241 StackHandleScope<2> hs(self);
1242 // Create array for parameter types.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001243 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001244 ObjPtr<mirror::Class> class_array_type =
1245 GetClassRoot<mirror::ObjectArray<mirror::Class>>(class_linker);
Orion Hodsona5dca522018-02-27 12:42:11 +00001246 Handle<mirror::ObjectArray<mirror::Class>> ptypes = hs.NewHandle(
1247 mirror::ObjectArray<mirror::Class>::Alloc(self,
1248 class_array_type,
1249 static_cast<int>(it.Size())));
1250 if (ptypes.IsNull()) {
1251 DCHECK(self->IsExceptionPending());
1252 return nullptr;
1253 }
1254
1255 // Populate the first argument with an instance of j.l.i.MethodHandles.Lookup
1256 // that the runtime will construct.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001257 ptypes->Set(0, GetClassRoot<mirror::MethodHandlesLookup>(class_linker));
Orion Hodsona5dca522018-02-27 12:42:11 +00001258 it.Next();
1259
1260 // The remaining parameter types are derived from the types of
1261 // arguments present in the DEX file.
1262 int index = 1;
1263 while (it.HasNext()) {
1264 ObjPtr<mirror::Class> ptype = GetClassForBootstrapArgument(it.GetValueType());
1265 if (ptype.IsNull()) {
1266 ThrowClassCastException("Unsupported bootstrap argument type");
1267 return nullptr;
1268 }
1269 ptypes->Set(index, ptype);
1270 index++;
1271 it.Next();
1272 }
1273 DCHECK_EQ(static_cast<size_t>(index), it.Size());
1274
1275 // By definition, the return type is always a j.l.i.CallSite.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001276 Handle<mirror::Class> rtype = hs.NewHandle(GetClassRoot<mirror::CallSite>());
Orion Hodsona5dca522018-02-27 12:42:11 +00001277 return mirror::MethodType::Create(self, rtype, ptypes);
1278}
1279
Orion Hodsonc069a302017-01-18 09:23:12 +00001280static ObjPtr<mirror::CallSite> InvokeBootstrapMethod(Thread* self,
1281 ShadowFrame& shadow_frame,
1282 uint32_t call_site_idx)
1283 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001284 StackHandleScope<5> hs(self);
Orion Hodsona5dca522018-02-27 12:42:11 +00001285 // There are three mandatory arguments expected from the call site
1286 // value array in the DEX file: the bootstrap method handle, the
1287 // method name to pass to the bootstrap method, and the method type
1288 // to pass to the bootstrap method.
1289 static constexpr size_t kMandatoryArgumentsCount = 3;
Orion Hodsonc069a302017-01-18 09:23:12 +00001290 ArtMethod* referrer = shadow_frame.GetMethod();
1291 const DexFile* dex_file = referrer->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001292 const dex::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00001293 CallSiteArrayValueIterator it(*dex_file, csi);
Orion Hodsona5dca522018-02-27 12:42:11 +00001294 if (it.Size() < kMandatoryArgumentsCount) {
1295 ThrowBootstrapMethodError("Truncated bootstrap arguments (%zu < %zu)",
1296 it.Size(), kMandatoryArgumentsCount);
1297 return nullptr;
1298 }
1299
1300 if (it.GetValueType() != EncodedArrayValueIterator::ValueType::kMethodHandle) {
1301 ThrowBootstrapMethodError("First bootstrap argument is not a method handle");
1302 return nullptr;
1303 }
1304
1305 uint32_t bsm_index = static_cast<uint32_t>(it.GetJavaValue().i);
1306 it.Next();
1307
Orion Hodsonc069a302017-01-18 09:23:12 +00001308 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Orion Hodsona5dca522018-02-27 12:42:11 +00001309 Handle<mirror::MethodHandle> bsm =
1310 hs.NewHandle(class_linker->ResolveMethodHandle(self, bsm_index, referrer));
1311 if (bsm.IsNull()) {
Orion Hodsonc069a302017-01-18 09:23:12 +00001312 DCHECK(self->IsExceptionPending());
1313 return nullptr;
1314 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001315
Orion Hodsona5dca522018-02-27 12:42:11 +00001316 if (bsm->GetHandleKind() != mirror::MethodHandle::Kind::kInvokeStatic) {
1317 // JLS suggests also accepting constructors. This is currently
1318 // hard as constructor invocations happen via transformers in ART
1319 // today. The constructor would need to be a class derived from java.lang.invoke.CallSite.
1320 ThrowBootstrapMethodError("Unsupported bootstrap method invocation kind");
1321 return nullptr;
1322 }
1323
1324 // Construct the local call site type information based on the 3
1325 // mandatory arguments provided by the runtime and the static arguments
1326 // in the DEX file. We will use these arguments to build a shadow frame.
1327 MutableHandle<mirror::MethodType> call_site_type =
1328 hs.NewHandle(BuildCallSiteForBootstrapMethod(self, dex_file, call_site_idx));
1329 if (call_site_type.IsNull()) {
1330 DCHECK(self->IsExceptionPending());
1331 return nullptr;
1332 }
1333
1334 // Check if this BSM is targeting a variable arity method. If so,
1335 // we'll need to collect the trailing arguments into an array.
1336 Handle<mirror::Array> collector_arguments;
1337 int32_t collector_arguments_length;
1338 if (bsm->GetTargetMethod()->IsVarargs()) {
1339 int number_of_bsm_parameters = bsm->GetMethodType()->GetNumberOfPTypes();
1340 if (number_of_bsm_parameters == 0) {
1341 ThrowBootstrapMethodError("Variable arity BSM does not have any arguments");
1342 return nullptr;
1343 }
1344 Handle<mirror::Class> collector_array_class =
1345 hs.NewHandle(bsm->GetMethodType()->GetPTypes()->Get(number_of_bsm_parameters - 1));
1346 if (!collector_array_class->IsArrayClass()) {
1347 ThrowBootstrapMethodError("Variable arity BSM does not have array as final argument");
1348 return nullptr;
1349 }
1350 // The call site may include no arguments to be collected. In this
1351 // case the number of arguments must be at least the number of BSM
1352 // parameters less the collector array.
1353 if (call_site_type->GetNumberOfPTypes() < number_of_bsm_parameters - 1) {
1354 ThrowWrongMethodTypeException(bsm->GetMethodType(), call_site_type.Get());
1355 return nullptr;
1356 }
1357 // Check all the arguments to be collected match the collector array component type.
1358 for (int i = number_of_bsm_parameters - 1; i < call_site_type->GetNumberOfPTypes(); ++i) {
1359 if (call_site_type->GetPTypes()->Get(i) != collector_array_class->GetComponentType()) {
1360 ThrowClassCastException(collector_array_class->GetComponentType(),
1361 call_site_type->GetPTypes()->Get(i));
1362 return nullptr;
1363 }
1364 }
1365 // Update the call site method type so it now includes the collector array.
1366 int32_t collector_arguments_start = number_of_bsm_parameters - 1;
1367 collector_arguments_length = call_site_type->GetNumberOfPTypes() - number_of_bsm_parameters + 1;
1368 call_site_type.Assign(
1369 mirror::MethodType::CollectTrailingArguments(self,
1370 call_site_type.Get(),
1371 collector_array_class.Get(),
1372 collector_arguments_start));
1373 if (call_site_type.IsNull()) {
1374 DCHECK(self->IsExceptionPending());
1375 return nullptr;
1376 }
1377 } else {
1378 collector_arguments_length = 0;
1379 }
1380
1381 if (call_site_type->GetNumberOfPTypes() != bsm->GetMethodType()->GetNumberOfPTypes()) {
1382 ThrowWrongMethodTypeException(bsm->GetMethodType(), call_site_type.Get());
1383 return nullptr;
1384 }
1385
1386 // BSM invocation has a different set of exceptions that
1387 // j.l.i.MethodHandle.invoke(). Scan arguments looking for CCE
1388 // "opportunities". Unfortunately we cannot just leave this to the
1389 // method handle invocation as this might generate a WMTE.
1390 for (int32_t i = 0; i < call_site_type->GetNumberOfPTypes(); ++i) {
1391 ObjPtr<mirror::Class> from = call_site_type->GetPTypes()->Get(i);
1392 ObjPtr<mirror::Class> to = bsm->GetMethodType()->GetPTypes()->Get(i);
1393 if (!IsParameterTypeConvertible(from, to)) {
1394 ThrowClassCastException(from, to);
1395 return nullptr;
1396 }
1397 }
1398 if (!IsReturnTypeConvertible(call_site_type->GetRType(), bsm->GetMethodType()->GetRType())) {
1399 ThrowClassCastException(bsm->GetMethodType()->GetRType(), call_site_type->GetRType());
1400 return nullptr;
1401 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001402
1403 // Set-up a shadow frame for invoking the bootstrap method handle.
1404 ShadowFrameAllocaUniquePtr bootstrap_frame =
Orion Hodsona5dca522018-02-27 12:42:11 +00001405 CREATE_SHADOW_FRAME(call_site_type->NumberOfVRegs(),
1406 nullptr,
1407 referrer,
1408 shadow_frame.GetDexPC());
Orion Hodsonc069a302017-01-18 09:23:12 +00001409 ScopedStackedShadowFramePusher pusher(
1410 self, bootstrap_frame.get(), StackedShadowFrameType::kShadowFrameUnderConstruction);
Orion Hodsona5dca522018-02-27 12:42:11 +00001411 ShadowFrameSetter setter(bootstrap_frame.get(), 0u);
Orion Hodsonc069a302017-01-18 09:23:12 +00001412
1413 // The first parameter is a MethodHandles lookup instance.
Orion Hodsona5dca522018-02-27 12:42:11 +00001414 Handle<mirror::Class> lookup_class =
1415 hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass());
1416 ObjPtr<mirror::MethodHandlesLookup> lookup =
1417 mirror::MethodHandlesLookup::Create(self, lookup_class);
1418 if (lookup.IsNull()) {
Orion Hodsonc069a302017-01-18 09:23:12 +00001419 DCHECK(self->IsExceptionPending());
1420 return nullptr;
1421 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001422 setter.SetReference(lookup);
Orion Hodsonc069a302017-01-18 09:23:12 +00001423
Orion Hodsona5dca522018-02-27 12:42:11 +00001424 // Pack the remaining arguments into the frame.
1425 int number_of_arguments = call_site_type->GetNumberOfPTypes();
1426 int argument_index;
1427 for (argument_index = 1; argument_index < number_of_arguments; ++argument_index) {
1428 if (argument_index == number_of_arguments - 1 &&
1429 call_site_type->GetPTypes()->Get(argument_index)->IsArrayClass()) {
1430 ObjPtr<mirror::Class> array_type = call_site_type->GetPTypes()->Get(argument_index);
1431 if (!PackCollectorArrayForBootstrapMethod(self,
1432 referrer,
1433 array_type,
1434 collector_arguments_length,
1435 &it,
1436 &setter)) {
1437 DCHECK(self->IsExceptionPending());
1438 return nullptr;
Orion Hodsonc069a302017-01-18 09:23:12 +00001439 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001440 } else if (!PackArgumentForBootstrapMethod(self, referrer, &it, &setter)) {
1441 DCHECK(self->IsExceptionPending());
1442 return nullptr;
Orion Hodsonc069a302017-01-18 09:23:12 +00001443 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001444 it.Next();
1445 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001446 DCHECK(!it.HasNext());
1447 DCHECK(setter.Done());
Orion Hodsonc069a302017-01-18 09:23:12 +00001448
1449 // Invoke the bootstrap method handle.
1450 JValue result;
Orion Hodsona5dca522018-02-27 12:42:11 +00001451 RangeInstructionOperands operands(0, bootstrap_frame->NumberOfVRegs());
1452 bool invoke_success = MethodHandleInvoke(self,
1453 *bootstrap_frame,
1454 bsm,
1455 call_site_type,
1456 &operands,
1457 &result);
Orion Hodsonc069a302017-01-18 09:23:12 +00001458 if (!invoke_success) {
1459 DCHECK(self->IsExceptionPending());
1460 return nullptr;
1461 }
1462
1463 Handle<mirror::Object> object(hs.NewHandle(result.GetL()));
Orion Hodsonc069a302017-01-18 09:23:12 +00001464 if (UNLIKELY(object.IsNull())) {
Orion Hodsonda1cdd02018-01-31 18:08:28 +00001465 // This will typically be for LambdaMetafactory which is not supported.
Orion Hodson76e6adb2018-02-23 13:15:55 +00001466 ThrowClassCastException("Bootstrap method returned null");
Orion Hodsonc069a302017-01-18 09:23:12 +00001467 return nullptr;
1468 }
1469
Orion Hodsona5dca522018-02-27 12:42:11 +00001470 // Check the result type is a subclass of j.l.i.CallSite.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001471 ObjPtr<mirror::Class> call_site_class = GetClassRoot<mirror::CallSite>(class_linker);
1472 if (UNLIKELY(!object->InstanceOf(call_site_class))) {
1473 ThrowClassCastException(object->GetClass(), call_site_class);
Orion Hodsonc069a302017-01-18 09:23:12 +00001474 return nullptr;
1475 }
1476
Orion Hodsona5dca522018-02-27 12:42:11 +00001477 // Check the call site target is not null as we're going to invoke it.
Vladimir Markod7e9bbf2019-03-28 13:18:57 +00001478 ObjPtr<mirror::CallSite> call_site = ObjPtr<mirror::CallSite>::DownCast(result.GetL());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001479 ObjPtr<mirror::MethodHandle> target = call_site->GetTarget();
1480 if (UNLIKELY(target == nullptr)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001481 ThrowClassCastException("Bootstrap method returned a CallSite with a null target");
Orion Hodsonc069a302017-01-18 09:23:12 +00001482 return nullptr;
1483 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001484 return call_site;
Orion Hodsonc069a302017-01-18 09:23:12 +00001485}
1486
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001487namespace {
1488
1489ObjPtr<mirror::CallSite> DoResolveCallSite(Thread* self,
1490 ShadowFrame& shadow_frame,
1491 uint32_t call_site_idx)
1492 REQUIRES_SHARED(Locks::mutator_lock_) {
1493 StackHandleScope<1> hs(self);
1494 Handle<mirror::DexCache> dex_cache(hs.NewHandle(shadow_frame.GetMethod()->GetDexCache()));
1495
1496 // Get the call site from the DexCache if present.
1497 ObjPtr<mirror::CallSite> call_site = dex_cache->GetResolvedCallSite(call_site_idx);
1498 if (LIKELY(call_site != nullptr)) {
1499 return call_site;
1500 }
1501
1502 // Invoke the bootstrap method to get a candidate call site.
1503 call_site = InvokeBootstrapMethod(self, shadow_frame, call_site_idx);
1504 if (UNLIKELY(call_site == nullptr)) {
1505 if (!self->GetException()->IsError()) {
1506 // Use a BootstrapMethodError if the exception is not an instance of java.lang.Error.
1507 ThrowWrappedBootstrapMethodError("Exception from call site #%u bootstrap method",
1508 call_site_idx);
1509 }
1510 return nullptr;
1511 }
1512
1513 // Attempt to place the candidate call site into the DexCache, return the winning call site.
1514 return dex_cache->SetResolvedCallSite(call_site_idx, call_site);
1515}
1516
1517} // namespace
1518
Orion Hodsonc069a302017-01-18 09:23:12 +00001519bool DoInvokeCustom(Thread* self,
1520 ShadowFrame& shadow_frame,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001521 uint32_t call_site_idx,
1522 const InstructionOperands* operands,
1523 JValue* result) {
Alex Light848574c2017-09-25 16:59:39 -07001524 // Make sure to check for async exceptions
1525 if (UNLIKELY(self->ObserveAsyncException())) {
1526 return false;
1527 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001528
Orion Hodsonc069a302017-01-18 09:23:12 +00001529 // invoke-custom is not supported in transactions. In transactions
1530 // there is a limited set of types supported. invoke-custom allows
1531 // running arbitrary code and instantiating arbitrary types.
1532 CHECK(!Runtime::Current()->IsActiveTransaction());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001533
1534 ObjPtr<mirror::CallSite> call_site = DoResolveCallSite(self, shadow_frame, call_site_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00001535 if (call_site.IsNull()) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001536 DCHECK(self->IsExceptionPending());
1537 return false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001538 }
1539
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001540 StackHandleScope<2> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +00001541 Handle<mirror::MethodHandle> target = hs.NewHandle(call_site->GetTarget());
1542 Handle<mirror::MethodType> target_method_type = hs.NewHandle(target->GetMethodType());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001543 DCHECK_EQ(operands->GetNumberOfOperands(), target_method_type->NumberOfVRegs())
1544 << " call_site_idx" << call_site_idx;
1545 return MethodHandleInvokeExact(self,
1546 shadow_frame,
1547 target,
1548 target_method_type,
1549 operands,
1550 result);
Orion Hodsonc069a302017-01-18 09:23:12 +00001551}
1552
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001553// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
1554static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
1555 size_t dest_reg, size_t src_reg)
1556 REQUIRES_SHARED(Locks::mutator_lock_) {
1557 // Uint required, so that sign extension does not make this wrong on 64b systems
1558 uint32_t src_value = shadow_frame.GetVReg(src_reg);
1559 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
1560
1561 // If both register locations contains the same value, the register probably holds a reference.
1562 // Note: As an optimization, non-moving collectors leave a stale reference value
1563 // in the references array even after the original vreg was overwritten to a non-reference.
Vladimir Marko78baed52018-10-11 10:44:58 +01001564 if (src_value == reinterpret_cast32<uint32_t>(o.Ptr())) {
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001565 new_shadow_frame->SetVRegReference(dest_reg, o);
1566 } else {
1567 new_shadow_frame->SetVReg(dest_reg, src_value);
1568 }
1569}
1570
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001571template <bool is_range>
1572inline void CopyRegisters(ShadowFrame& caller_frame,
1573 ShadowFrame* callee_frame,
1574 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
1575 const size_t first_src_reg,
1576 const size_t first_dest_reg,
1577 const size_t num_regs) {
1578 if (is_range) {
1579 const size_t dest_reg_bound = first_dest_reg + num_regs;
1580 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound;
1581 ++dest_reg, ++src_reg) {
1582 AssignRegister(callee_frame, caller_frame, dest_reg, src_reg);
1583 }
1584 } else {
1585 DCHECK_LE(num_regs, arraysize(arg));
1586
1587 for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) {
1588 AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]);
1589 }
1590 }
1591}
1592
Igor Murashkin6918bf12015-09-27 19:19:06 -07001593template <bool is_range,
Narayan Kamath370423d2016-10-03 16:51:22 +01001594 bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001595static inline bool DoCallCommon(ArtMethod* called_method,
1596 Thread* self,
1597 ShadowFrame& shadow_frame,
1598 JValue* result,
1599 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +01001600 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -07001601 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001602 bool string_init = false;
1603 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001604 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
1605 && called_method->IsConstructor())) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001606 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
Jeff Hao848f70a2014-01-15 13:49:50 -08001607 string_init = true;
1608 }
1609
Alex Lightdaf58c82016-03-16 23:00:49 +00001610 // Compute method information.
David Sehr0225f8e2018-01-31 08:52:24 +00001611 CodeItemDataAccessor accessor(called_method->DexInstructionData());
Igor Murashkin158f35c2015-06-10 15:55:30 -07001612 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001613 uint16_t num_regs;
Jeff Hao5ea84132017-05-05 16:59:29 -07001614 // Test whether to use the interpreter or compiler entrypoint, and save that result to pass to
1615 // PerformCall. A deoptimization could occur at any time, and we shouldn't change which
1616 // entrypoint to use once we start building the shadow frame.
Mathieu Chartier448bbcf2017-07-06 12:05:13 -07001617
1618 // For unstarted runtimes, always use the interpreter entrypoint. This fixes the case where we are
1619 // doing cross compilation. Note that GetEntryPointFromQuickCompiledCode doesn't use the image
1620 // pointer size here and this may case an overflow if it is called from the compiler. b/62402160
1621 const bool use_interpreter_entrypoint = !Runtime::Current()->IsStarted() ||
1622 ClassLinker::ShouldUseInterpreterEntrypoint(
1623 called_method,
1624 called_method->GetEntryPointFromQuickCompiledCode());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001625 if (LIKELY(accessor.HasCodeItem())) {
Jeff Hao5ea84132017-05-05 16:59:29 -07001626 // When transitioning to compiled code, space only needs to be reserved for the input registers.
1627 // The rest of the frame gets discarded. This also prevents accessing the called method's code
1628 // item, saving memory by keeping code items of compiled code untouched.
Mathieu Chartier448bbcf2017-07-06 12:05:13 -07001629 if (!use_interpreter_entrypoint) {
1630 DCHECK(!Runtime::Current()->IsAotCompiler()) << "Compiler should use interpreter entrypoint";
Jeff Hao5ea84132017-05-05 16:59:29 -07001631 num_regs = number_of_inputs;
1632 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001633 num_regs = accessor.RegistersSize();
1634 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, accessor.InsSize());
Jeff Hao5ea84132017-05-05 16:59:29 -07001635 }
Nicolas Geoffray01822292017-03-09 09:03:19 +00001636 } else {
1637 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1638 num_regs = number_of_inputs;
Jeff Haodf79ddb2017-02-27 14:47:06 -08001639 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001640
Igor Murashkin158f35c2015-06-10 15:55:30 -07001641 // Hack for String init:
1642 //
1643 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
1644 // invoke-x StringFactory(a, b, c, ...)
1645 // by effectively dropping the first virtual register from the invoke.
1646 //
1647 // (at this point the ArtMethod has already been replaced,
1648 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +00001649 //
1650 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
1651 // to handle the compiler optimization of replacing `this` with null without
1652 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001653 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -07001654 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001655 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 -07001656
Igor Murashkin158f35c2015-06-10 15:55:30 -07001657 // The new StringFactory call is static and has one fewer argument.
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001658 if (!accessor.HasCodeItem()) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001659 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1660 num_regs--;
1661 } // 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 -07001662 number_of_inputs--;
1663
1664 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -07001665 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001666 arg[i - 1] = arg[i];
1667 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07001668 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001669
1670 // Rewrite the non-var-arg case
1671 vregC++; // Skips the 0th vreg in the range ("this").
1672 }
1673
1674 // Parameter registers go at the end of the shadow frame.
1675 DCHECK_GE(num_regs, number_of_inputs);
1676 size_t first_dest_reg = num_regs - number_of_inputs;
1677 DCHECK_NE(first_dest_reg, (size_t)-1);
1678
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001679 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001680 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -07001681 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -07001682 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -07001683 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001684
Igor Murashkin158f35c2015-06-10 15:55:30 -07001685 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -07001686 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001687 // Slow path.
1688 // We might need to do class loading, which incurs a thread state change to kNative. So
1689 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07001690 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +02001691 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001692 self->EndAssertNoThreadSuspension(old_cause);
1693
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001694 // ArtMethod here is needed to check type information of the call site against the callee.
1695 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
1696 //
1697 // As a special case for proxy methods, which are not dex-backed,
1698 // we have to retrieve type information from the proxy's method
1699 // interface method instead (which is dex backed since proxies are never interfaces).
Andreas Gampe542451c2016-07-26 09:02:02 -07001700 ArtMethod* method =
1701 new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001702
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001703 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001704 // to get the exact type of each reference argument.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001705 const dex::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001706 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001707 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001708
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001709 // Handle receiver apart since it's not part of the shorty.
1710 size_t dest_reg = first_dest_reg;
1711 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001712
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001713 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001714 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001715 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
1716 ++dest_reg;
1717 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -07001718 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001719 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001720
1721 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001722 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001723 // Skip the 0th 'shorty' type since it represents the return type.
1724 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001725 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
1726 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001727 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001728 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001729 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001730 if (do_assignability_check && o != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001731 const dex::TypeIndex type_idx = params->GetTypeItem(shorty_pos).type_idx_;
Vladimir Marko942fd312017-01-16 20:52:19 +00001732 ObjPtr<mirror::Class> arg_type = method->GetDexCache()->GetResolvedType(type_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001733 if (arg_type == nullptr) {
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001734 StackHandleScope<1> hs(self);
1735 // Preserve o since it is used below and GetClassFromTypeIndex may cause thread
1736 // suspension.
1737 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&o);
Vladimir Markob45528c2017-07-27 14:14:28 +01001738 arg_type = method->ResolveClassFromTypeIndex(type_idx);
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001739 if (arg_type == nullptr) {
1740 CHECK(self->IsExceptionPending());
1741 return false;
1742 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001743 }
1744 if (!o->VerifierInstanceOf(arg_type)) {
1745 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -07001746 std::string temp1, temp2;
Orion Hodsonfef06642016-11-25 16:07:11 +00001747 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001748 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -08001749 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -07001750 o->GetClass()->GetDescriptor(&temp1),
1751 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001752 return false;
1753 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001754 }
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001755 new_shadow_frame->SetVRegReference(dest_reg, o);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001756 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -07001757 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001758 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001759 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001760 uint64_t wide_value =
1761 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
1762 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001763 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001764 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001765 ++dest_reg;
1766 ++arg_offset;
1767 break;
1768 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001769 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001770 default:
1771 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
1772 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001773 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001774 }
1775 } else {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001776 if (is_range) {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001777 DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001778 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001779
1780 CopyRegisters<is_range>(shadow_frame,
1781 new_shadow_frame,
1782 arg,
1783 vregC,
1784 first_dest_reg,
1785 number_of_inputs);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001786 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001787 }
1788
Jeff Hao5ea84132017-05-05 16:59:29 -07001789 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001790 accessor,
Jeff Hao5ea84132017-05-05 16:59:29 -07001791 shadow_frame.GetMethod(),
1792 first_dest_reg,
1793 new_shadow_frame,
1794 result,
1795 use_interpreter_entrypoint);
Jeff Hao848f70a2014-01-15 13:49:50 -08001796
1797 if (string_init && !self->IsExceptionPending()) {
Mingyao Yangffedec52016-05-19 10:48:40 -07001798 SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001799 }
1800
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001801 return !self->IsExceptionPending();
1802}
1803
Igor Murashkin158f35c2015-06-10 15:55:30 -07001804template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001805bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
1806 const Instruction* inst, uint16_t inst_data, JValue* result) {
1807 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001808 const uint16_t number_of_inputs =
1809 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001810
1811 // TODO: find a cleaner way to separate non-range and range information without duplicating
1812 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -07001813 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001814 uint32_t vregC = 0;
1815 if (is_range) {
1816 vregC = inst->VRegC_3rc();
1817 } else {
1818 vregC = inst->VRegC_35c();
1819 inst->GetVarArgs(arg, inst_data);
1820 }
1821
1822 return DoCallCommon<is_range, do_assignability_check>(
1823 called_method, self, shadow_frame,
1824 result, number_of_inputs, arg, vregC);
1825}
1826
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001827template <bool is_range, bool do_access_check, bool transaction_active>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001828bool DoFilledNewArray(const Instruction* inst,
1829 const ShadowFrame& shadow_frame,
1830 Thread* self,
1831 JValue* result) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001832 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
1833 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
1834 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
1835 if (!is_range) {
1836 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
1837 CHECK_LE(length, 5);
1838 }
1839 if (UNLIKELY(length < 0)) {
1840 ThrowNegativeArraySizeException(length);
1841 return false;
1842 }
1843 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08001844 ObjPtr<mirror::Class> array_class = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
Mathieu Chartieref41db72016-10-25 15:08:01 -07001845 shadow_frame.GetMethod(),
1846 self,
1847 false,
1848 do_access_check);
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001849 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001850 DCHECK(self->IsExceptionPending());
1851 return false;
1852 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001853 CHECK(array_class->IsArrayClass());
Mathieu Chartieref41db72016-10-25 15:08:01 -07001854 ObjPtr<mirror::Class> component_class = array_class->GetComponentType();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001855 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
1856 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
1857 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001858 ThrowRuntimeException("Bad filled array request for type %s",
David Sehr709b0702016-10-13 09:12:37 -07001859 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001860 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001861 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -08001862 "Found type %s; filled-new-array not implemented for anything but 'int'",
David Sehr709b0702016-10-13 09:12:37 -07001863 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001864 }
1865 return false;
1866 }
Vladimir Marko9b81ac32019-05-16 16:47:08 +01001867 ObjPtr<mirror::Object> new_array = mirror::Array::Alloc(
Mathieu Chartieref41db72016-10-25 15:08:01 -07001868 self,
1869 array_class,
1870 length,
1871 array_class->GetComponentSizeShift(),
1872 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001873 if (UNLIKELY(new_array == nullptr)) {
1874 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001875 return false;
1876 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001877 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
1878 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001879 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +01001880 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001881 } else {
Ian Rogers29a26482014-05-02 15:27:29 -07001882 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +01001883 }
Sebastien Hertzabff6432014-01-27 18:01:39 +01001884 for (int32_t i = 0; i < length; ++i) {
1885 size_t src_reg = is_range ? vregC + i : arg[i];
1886 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001887 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
1888 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +01001889 } else {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001890 new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<transaction_active>(
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001891 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001892 }
1893 }
1894
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001895 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001896 return true;
1897}
1898
Mathieu Chartieref41db72016-10-25 15:08:01 -07001899// TODO: Use ObjPtr here.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001900template<typename T>
Vladimir Marko4617d582019-03-28 13:48:31 +00001901static void RecordArrayElementsInTransactionImpl(ObjPtr<mirror::PrimitiveArray<T>> array,
Mathieu Chartieref41db72016-10-25 15:08:01 -07001902 int32_t count)
1903 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001904 Runtime* runtime = Runtime::Current();
1905 for (int32_t i = 0; i < count; ++i) {
Vladimir Marko4617d582019-03-28 13:48:31 +00001906 runtime->RecordWriteArray(array.Ptr(), i, array->GetWithoutChecks(i));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001907 }
1908}
1909
Mathieu Chartieref41db72016-10-25 15:08:01 -07001910void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001911 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001912 DCHECK(Runtime::Current()->IsActiveTransaction());
1913 DCHECK(array != nullptr);
1914 DCHECK_LE(count, array->GetLength());
1915 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
1916 switch (primitive_component_type) {
1917 case Primitive::kPrimBoolean:
1918 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
1919 break;
1920 case Primitive::kPrimByte:
1921 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
1922 break;
1923 case Primitive::kPrimChar:
1924 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
1925 break;
1926 case Primitive::kPrimShort:
1927 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
1928 break;
1929 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001930 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
1931 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001932 case Primitive::kPrimFloat:
1933 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
1934 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001935 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001936 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
1937 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001938 case Primitive::kPrimDouble:
1939 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
1940 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001941 default:
1942 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
1943 << " in fill-array-data";
Elliott Hughesc1896c92018-11-29 11:33:18 -08001944 UNREACHABLE();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001945 }
1946}
1947
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001948// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +02001949#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001950 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001951 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
1952 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +02001953 const Instruction* inst, uint16_t inst_data, \
1954 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001955EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
1956EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
1957EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
1958EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
1959#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001960
Orion Hodsonc069a302017-01-18 09:23:12 +00001961// Explicit DoInvokePolymorphic template function declarations.
1962#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range) \
1963 template REQUIRES_SHARED(Locks::mutator_lock_) \
1964 bool DoInvokePolymorphic<_is_range>( \
1965 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1966 uint16_t inst_data, JValue* result)
1967EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false);
1968EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true);
Narayan Kamath9823e782016-08-03 12:46:58 +01001969#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1970
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001971// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001972#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001973 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001974 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
1975 const ShadowFrame& shadow_frame, \
1976 Thread* self, JValue* result)
1977#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
1978 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
1979 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
1980 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
1981 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
1982EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
1983EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
1984#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001985#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
1986
1987} // namespace interpreter
1988} // namespace art