blob: 9953743f04592bc1c07f038f8af193c045d63d14 [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 }
85 if (method->IsStatic() && !method->GetDeclaringClass()->IsInitialized()) {
86 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) {
112 if (Runtime::Current()->GetTransaction()->ReadConstraint(obj.Ptr(), f)) {
113 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();
Chang Xingbd208d82017-07-12 14:53:17 -0700324 if (transaction_active) {
325 if (Runtime::Current()->GetTransaction()->WriteConstraint(obj.Ptr(), f)) {
326 Runtime::Current()->AbortTransactionAndThrowAbortError(
327 self, "Can't set fields of " + obj->PrettyTypeOf());
328 return false;
329 }
330 }
331
Orion Hodson3d617ac2016-10-19 14:00:46 +0100332 } else {
333 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
334 if (UNLIKELY(obj == nullptr)) {
335 ThrowNullPointerExceptionForFieldAccess(f, false);
336 return false;
337 }
338 }
339
340 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100341 JValue value = GetFieldValue<field_type>(shadow_frame, vregA);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100342 return DoFieldPutCommon<field_type, do_assignability_check, transaction_active>(self,
343 shadow_frame,
344 obj,
345 f,
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100346 value);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100347}
348
Ian Rogers54874942014-06-10 16:31:03 -0700349// Explicitly instantiate all DoFieldPut functions.
350#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
351 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
352 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
353
354#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
355 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
356 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
357 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
358 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
359
360// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700361EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
362EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
363EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
364EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
365EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
366EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
367EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700368
369// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700370EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
371EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
372EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
373EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
374EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
375EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
376EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700377
378#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
379#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
380
381template<Primitive::Type field_type, bool transaction_active>
382bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700383 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700384 if (UNLIKELY(obj == nullptr)) {
385 // We lost the reference to the field index so we cannot get a more
386 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000387 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700388 return false;
389 }
390 MemberOffset field_offset(inst->VRegC_22c());
391 const uint32_t vregA = inst->VRegA_22c(inst_data);
392 // Report this field modification to instrumentation if needed. Since we only have the offset of
393 // the field from the base of the object, we need to look for it first.
394 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
395 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
396 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
397 field_offset.Uint32Value());
398 DCHECK(f != nullptr);
399 DCHECK(!f->IsStatic());
400 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Alex Light084fa372017-06-16 08:58:34 -0700401 Thread* self = Thread::Current();
402 StackHandleScope<2> hs(self);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700403 // Save obj in case the instrumentation event has thread suspension.
404 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Alex Light084fa372017-06-16 08:58:34 -0700405 mirror::Object* fake_root = nullptr;
406 HandleWrapper<mirror::Object> ret(hs.NewHandleWrapper<mirror::Object>(
407 field_type == Primitive::kPrimNot ? field_value.GetGCRoot() : &fake_root));
408 instrumentation->FieldWriteEvent(self,
Vladimir Marko19711d42019-04-12 14:05:34 +0100409 obj,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700410 shadow_frame.GetMethod(),
411 shadow_frame.GetDexPC(),
412 f,
413 field_value);
Alex Light084fa372017-06-16 08:58:34 -0700414 if (UNLIKELY(self->IsExceptionPending())) {
415 return false;
416 }
Alex Light0aa7a5a2018-10-10 15:58:14 +0000417 if (UNLIKELY(shadow_frame.GetForcePopFrame())) {
418 // Don't actually set the field. The next instruction will force us to pop.
419 DCHECK(Runtime::Current()->AreNonStandardExitsEnabled());
Alex Light0aa7a5a2018-10-10 15:58:14 +0000420 return true;
421 }
Ian Rogers54874942014-06-10 16:31:03 -0700422 }
423 // Note: iput-x-quick instructions are only for non-volatile fields.
424 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700425 case Primitive::kPrimBoolean:
426 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
427 break;
428 case Primitive::kPrimByte:
429 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
430 break;
431 case Primitive::kPrimChar:
432 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
433 break;
434 case Primitive::kPrimShort:
435 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
436 break;
Ian Rogers54874942014-06-10 16:31:03 -0700437 case Primitive::kPrimInt:
438 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
439 break;
440 case Primitive::kPrimLong:
441 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
442 break;
443 case Primitive::kPrimNot:
444 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
445 break;
446 default:
447 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700448 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700449 }
450 return true;
451}
452
453// Explicitly instantiate all DoIPutQuick functions.
454#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
455 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
456 const Instruction* inst, \
457 uint16_t inst_data)
458
459#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
460 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
461 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
462
Andreas Gampec8ccf682014-09-29 20:07:43 -0700463EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
464EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
465EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
466EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
467EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
468EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
469EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700470#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
471#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
472
Alex Lightb7c640d2019-03-20 15:52:13 -0700473template <typename T>
474bool SendMethodExitEvents(Thread* self,
475 const instrumentation::Instrumentation* instrumentation,
476 ShadowFrame& frame,
477 ObjPtr<mirror::Object> thiz,
478 ArtMethod* method,
479 uint32_t dex_pc,
480 T& result) {
481 bool had_event = false;
482 // We can get additional ForcePopFrame requests during handling of these events. We should
483 // respect these and send additional instrumentation events.
484 StackHandleScope<1> hs(self);
485 Handle<mirror::Object> h_thiz(hs.NewHandle(thiz));
486 do {
487 frame.SetForcePopFrame(false);
488 if (UNLIKELY(instrumentation->HasMethodExitListeners() && !frame.GetSkipMethodExitEvents())) {
489 had_event = true;
490 instrumentation->MethodExitEvent(
491 self, h_thiz.Get(), method, dex_pc, instrumentation::OptionalFrame{ frame }, result);
492 }
493 // We don't send method-exit if it's a pop-frame. We still send frame_popped though.
494 if (UNLIKELY(frame.NeedsNotifyPop() && instrumentation->HasWatchedFramePopListeners())) {
495 had_event = true;
496 instrumentation->WatchedFramePopped(self, frame);
497 }
498 } while (UNLIKELY(frame.GetForcePopFrame()));
499 if (UNLIKELY(had_event)) {
500 return !self->IsExceptionPending();
501 } else {
502 return true;
503 }
504}
505
506template
507bool SendMethodExitEvents(Thread* self,
508 const instrumentation::Instrumentation* instrumentation,
509 ShadowFrame& frame,
510 ObjPtr<mirror::Object> thiz,
511 ArtMethod* method,
512 uint32_t dex_pc,
513 MutableHandle<mirror::Object>& result);
514
515template
516bool SendMethodExitEvents(Thread* self,
517 const instrumentation::Instrumentation* instrumentation,
518 ShadowFrame& frame,
519 ObjPtr<mirror::Object> thiz,
520 ArtMethod* method,
521 uint32_t dex_pc,
522 JValue& result);
523
Alex Light9fb1ab12017-09-05 09:32:49 -0700524// We execute any instrumentation events that are triggered by this exception and change the
525// shadow_frame's dex_pc to that of the exception handler if there is one in the current method.
526// Return true if we should continue executing in the current method and false if we need to go up
527// the stack to find an exception handler.
Sebastien Hertz520633b2015-09-08 17:03:36 +0200528// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Alex Light9fb1ab12017-09-05 09:32:49 -0700529// TODO We should have a better way to skip instrumentation reporting or possibly rethink that
530// behavior.
531bool MoveToExceptionHandler(Thread* self,
532 ShadowFrame& shadow_frame,
533 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700534 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700535 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000536 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Alex Light9fb1ab12017-09-05 09:32:49 -0700537 if (instrumentation != nullptr &&
538 instrumentation->HasExceptionThrownListeners() &&
539 self->IsExceptionThrownByCurrentMethod(exception.Get())) {
540 // 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 -0700541 instrumentation->ExceptionThrownEvent(self, exception.Get());
Alex Light0aa7a5a2018-10-10 15:58:14 +0000542 if (shadow_frame.GetForcePopFrame()) {
543 // We will check in the caller for GetForcePopFrame again. We need to bail out early to
544 // prevent an ExceptionHandledEvent from also being sent before popping.
545 return true;
546 }
Sebastien Hertz9f102032014-05-23 08:59:42 +0200547 }
Ian Rogers54874942014-06-10 16:31:03 -0700548 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700549 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
Alex Light9fb1ab12017-09-05 09:32:49 -0700550 hs.NewHandle(exception->GetClass()), shadow_frame.GetDexPC(), &clear_exception);
Andreas Gampee2abbc62017-09-15 11:59:26 -0700551 if (found_dex_pc == dex::kDexNoIndex) {
Alex Light9fb1ab12017-09-05 09:32:49 -0700552 if (instrumentation != nullptr) {
553 if (shadow_frame.NeedsNotifyPop()) {
554 instrumentation->WatchedFramePopped(self, shadow_frame);
Alex Lightb7c640d2019-03-20 15:52:13 -0700555 if (shadow_frame.GetForcePopFrame()) {
556 // We will check in the caller for GetForcePopFrame again. We need to bail out early to
557 // prevent an ExceptionHandledEvent from also being sent before popping and to ensure we
558 // handle other types of non-standard-exits.
559 return true;
560 }
Alex Light9fb1ab12017-09-05 09:32:49 -0700561 }
562 // Exception is not caught by the current method. We will unwind to the
563 // caller. Notify any instrumentation listener.
564 instrumentation->MethodUnwindEvent(self,
565 shadow_frame.GetThisObject(),
566 shadow_frame.GetMethod(),
567 shadow_frame.GetDexPC());
Alex Lighte814f9d2017-07-31 16:14:39 -0700568 }
Alex Lightb7c640d2019-03-20 15:52:13 -0700569 return shadow_frame.GetForcePopFrame();
Ian Rogers54874942014-06-10 16:31:03 -0700570 } else {
Alex Light9fb1ab12017-09-05 09:32:49 -0700571 shadow_frame.SetDexPC(found_dex_pc);
572 if (instrumentation != nullptr && instrumentation->HasExceptionHandledListeners()) {
573 self->ClearException();
574 instrumentation->ExceptionHandledEvent(self, exception.Get());
575 if (UNLIKELY(self->IsExceptionPending())) {
576 // Exception handled event threw an exception. Try to find the handler for this one.
577 return MoveToExceptionHandler(self, shadow_frame, instrumentation);
578 } else if (!clear_exception) {
579 self->SetException(exception.Get());
580 }
581 } else if (clear_exception) {
Ian Rogers54874942014-06-10 16:31:03 -0700582 self->ClearException();
583 }
Alex Light9fb1ab12017-09-05 09:32:49 -0700584 return true;
Ian Rogers54874942014-06-10 16:31:03 -0700585 }
Ian Rogers54874942014-06-10 16:31:03 -0700586}
587
Ian Rogerse94652f2014-12-02 11:13:19 -0800588void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
589 LOG(FATAL) << "Unexpected instruction: "
590 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
591 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700592}
593
Sebastien Hertz45b15972015-04-03 16:07:05 +0200594void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700595 va_list args;
596 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200597 AbortTransactionV(self, fmt, args);
598 va_end(args);
599}
600
601void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
602 CHECK(Runtime::Current()->IsActiveTransaction());
603 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100604 std::string abort_msg;
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800605 android::base::StringAppendV(&abort_msg, fmt, args);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100606 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200607 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700608}
609
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100610// START DECLARATIONS :
611//
612// These additional declarations are required because clang complains
613// about ALWAYS_INLINE (-Werror, -Wgcc-compat) in definitions.
614//
615
Narayan Kamath9823e782016-08-03 12:46:58 +0100616template <bool is_range, bool do_assignability_check>
Vladimir Markod16363a2017-02-01 14:09:13 +0000617static ALWAYS_INLINE bool DoCallCommon(ArtMethod* called_method,
618 Thread* self,
619 ShadowFrame& shadow_frame,
620 JValue* result,
621 uint16_t number_of_inputs,
622 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
623 uint32_t vregC) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100624
625template <bool is_range>
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000626ALWAYS_INLINE void CopyRegisters(ShadowFrame& caller_frame,
627 ShadowFrame* callee_frame,
628 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
629 const size_t first_src_reg,
630 const size_t first_dest_reg,
631 const size_t num_regs) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100632
633// END DECLARATIONS.
634
Siva Chandra05d24152016-01-05 17:43:17 -0800635void ArtInterpreterToCompiledCodeBridge(Thread* self,
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100636 ArtMethod* caller,
Siva Chandra05d24152016-01-05 17:43:17 -0800637 ShadowFrame* shadow_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -0700638 uint16_t arg_offset,
Siva Chandra05d24152016-01-05 17:43:17 -0800639 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700640 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700641 ArtMethod* method = shadow_frame->GetMethod();
642 // Ensure static methods are initialized.
643 if (method->IsStatic()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700644 ObjPtr<mirror::Class> declaringClass = method->GetDeclaringClass();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700645 if (UNLIKELY(!declaringClass->IsInitialized())) {
646 self->PushShadowFrame(shadow_frame);
647 StackHandleScope<1> hs(self);
648 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
Nicolas Geoffray01822292017-03-09 09:03:19 +0000649 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
650 true))) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700651 self->PopShadowFrame();
652 DCHECK(self->IsExceptionPending());
653 return;
654 }
655 self->PopShadowFrame();
656 CHECK(h_class->IsInitializing());
Nicolas Geoffray01822292017-03-09 09:03:19 +0000657 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
658 method = shadow_frame->GetMethod();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700659 }
660 }
Jeff Hao5ea84132017-05-05 16:59:29 -0700661 // Basic checks for the arg_offset. If there's no code item, the arg_offset must be 0. Otherwise,
662 // check that the arg_offset isn't greater than the number of registers. A stronger check is
663 // difficult since the frame may contain space for all the registers in the method, or only enough
664 // space for the arguments.
665 if (kIsDebugBuild) {
666 if (method->GetCodeItem() == nullptr) {
667 DCHECK_EQ(0u, arg_offset) << method->PrettyMethod();
668 } else {
669 DCHECK_LE(arg_offset, shadow_frame->NumberOfVRegs());
670 }
671 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100672 jit::Jit* jit = Runtime::Current()->GetJit();
673 if (jit != nullptr && caller != nullptr) {
674 jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
675 }
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700676 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
677 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
Andreas Gampe542451c2016-07-26 09:02:02 -0700678 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700679}
680
Mingyao Yangffedec52016-05-19 10:48:40 -0700681void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
682 uint16_t this_obj_vreg,
683 JValue result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700684 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700685 ObjPtr<mirror::Object> existing = shadow_frame->GetVRegReference(this_obj_vreg);
Mingyao Yangffedec52016-05-19 10:48:40 -0700686 if (existing == nullptr) {
687 // If it's null, we come from compiled code that was deoptimized. Nothing to do,
688 // as the compiler verified there was no alias.
689 // Set the new string result of the StringFactory.
690 shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
691 return;
692 }
693 // Set the string init result into all aliases.
694 for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
695 if (shadow_frame->GetVRegReference(i) == existing) {
696 DCHECK_EQ(shadow_frame->GetVRegReference(i),
Vladimir Marko78baed52018-10-11 10:44:58 +0100697 reinterpret_cast32<mirror::Object*>(shadow_frame->GetVReg(i)));
Mingyao Yangffedec52016-05-19 10:48:40 -0700698 shadow_frame->SetVRegReference(i, result.GetL());
699 DCHECK_EQ(shadow_frame->GetVRegReference(i),
Vladimir Marko78baed52018-10-11 10:44:58 +0100700 reinterpret_cast32<mirror::Object*>(shadow_frame->GetVReg(i)));
Mingyao Yangffedec52016-05-19 10:48:40 -0700701 }
702 }
703}
704
Orion Hodsonc069a302017-01-18 09:23:12 +0000705template<bool is_range>
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100706static bool DoMethodHandleInvokeCommon(Thread* self,
707 ShadowFrame& shadow_frame,
708 bool invoke_exact,
709 const Instruction* inst,
710 uint16_t inst_data,
711 JValue* result)
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100712 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Light848574c2017-09-25 16:59:39 -0700713 // Make sure to check for async exceptions
714 if (UNLIKELY(self->ObserveAsyncException())) {
715 return false;
716 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100717 // Invoke-polymorphic instructions always take a receiver. i.e, they are never static.
718 const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc();
Orion Hodson3d617ac2016-10-19 14:00:46 +0100719 const int invoke_method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc();
Narayan Kamath9823e782016-08-03 12:46:58 +0100720
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000721 // Initialize |result| to 0 as this is the default return value for
722 // polymorphic invocations of method handle types with void return
723 // and provides sane return result in error cases.
724 result->SetJ(0);
725
Orion Hodson3d617ac2016-10-19 14:00:46 +0100726 // The invoke_method_idx here is the name of the signature polymorphic method that
Narayan Kamath9823e782016-08-03 12:46:58 +0100727 // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact)
728 // and not the method that we'll dispatch to in the end.
Orion Hodsone7732be2017-10-11 14:35:20 +0100729 StackHandleScope<2> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +0000730 Handle<mirror::MethodHandle> method_handle(hs.NewHandle(
Vladimir Markod7e9bbf2019-03-28 13:18:57 +0000731 ObjPtr<mirror::MethodHandle>::DownCast(shadow_frame.GetVRegReference(vRegC))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800732 if (UNLIKELY(method_handle == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100733 // Note that the invoke type is kVirtual here because a call to a signature
734 // polymorphic method is shaped like a virtual call at the bytecode level.
Orion Hodson3d617ac2016-10-19 14:00:46 +0100735 ThrowNullPointerExceptionForMethodAccess(invoke_method_idx, InvokeType::kVirtual);
Narayan Kamath9823e782016-08-03 12:46:58 +0100736 return false;
737 }
738
739 // The vRegH value gives the index of the proto_id associated with this
Orion Hodson811bd5f2016-12-07 11:35:37 +0000740 // signature polymorphic call site.
Orion Hodson06d10a72018-05-14 08:53:38 +0100741 const uint16_t vRegH = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
742 const dex::ProtoIndex callsite_proto_id(vRegH);
Narayan Kamath9823e782016-08-03 12:46:58 +0100743
744 // Call through to the classlinker and ask it to resolve the static type associated
745 // with the callsite. This information is stored in the dex cache so it's
746 // guaranteed to be fast after the first resolution.
Narayan Kamath9823e782016-08-03 12:46:58 +0100747 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Orion Hodsone7732be2017-10-11 14:35:20 +0100748 Handle<mirror::MethodType> callsite_type(hs.NewHandle(
749 class_linker->ResolveMethodType(self, callsite_proto_id, shadow_frame.GetMethod())));
Narayan Kamath9823e782016-08-03 12:46:58 +0100750
751 // This implies we couldn't resolve one or more types in this method handle.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800752 if (UNLIKELY(callsite_type == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100753 CHECK(self->IsExceptionPending());
Narayan Kamath9823e782016-08-03 12:46:58 +0100754 return false;
755 }
756
Orion Hodson811bd5f2016-12-07 11:35:37 +0000757 // There is a common dispatch method for method handles that takes
758 // arguments either from a range or an array of arguments depending
759 // on whether the DEX instruction is invoke-polymorphic/range or
760 // invoke-polymorphic. The array here is for the latter.
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100761 if (UNLIKELY(is_range)) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000762 // VRegC is the register holding the method handle. Arguments passed
763 // to the method handle's target do not include the method handle.
Orion Hodson960d4f72017-11-10 15:32:38 +0000764 RangeInstructionOperands operands(inst->VRegC_4rcc() + 1, inst->VRegA_4rcc() - 1);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100765 if (invoke_exact) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000766 return MethodHandleInvokeExact(self,
767 shadow_frame,
768 method_handle,
769 callsite_type,
770 &operands,
771 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100772 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000773 return MethodHandleInvoke(self,
774 shadow_frame,
775 method_handle,
776 callsite_type,
777 &operands,
778 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100779 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100780 } else {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000781 // Get the register arguments for the invoke.
Orion Hodson960d4f72017-11-10 15:32:38 +0000782 uint32_t args[Instruction::kMaxVarArgRegs] = {};
Orion Hodson811bd5f2016-12-07 11:35:37 +0000783 inst->GetVarArgs(args, inst_data);
784 // Drop the first register which is the method handle performing the invoke.
Alexey Frunze8631a462017-01-19 19:07:37 -0800785 memmove(args, args + 1, sizeof(args[0]) * (Instruction::kMaxVarArgRegs - 1));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000786 args[Instruction::kMaxVarArgRegs - 1] = 0;
Orion Hodson960d4f72017-11-10 15:32:38 +0000787 VarArgsInstructionOperands operands(args, inst->VRegA_45cc() - 1);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100788 if (invoke_exact) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000789 return MethodHandleInvokeExact(self,
790 shadow_frame,
791 method_handle,
792 callsite_type,
793 &operands,
794 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100795 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000796 return MethodHandleInvoke(self,
797 shadow_frame,
798 method_handle,
799 callsite_type,
800 &operands,
801 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100802 }
803 }
804}
805
806bool DoMethodHandleInvokeExact(Thread* self,
807 ShadowFrame& shadow_frame,
808 const Instruction* inst,
809 uint16_t inst_data,
810 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
811 if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) {
812 static const bool kIsRange = false;
813 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700814 self, shadow_frame, /* invoke_exact= */ true, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100815 } else {
816 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE);
817 static const bool kIsRange = true;
818 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700819 self, shadow_frame, /* invoke_exact= */ true, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100820 }
821}
822
823bool DoMethodHandleInvoke(Thread* self,
824 ShadowFrame& shadow_frame,
825 const Instruction* inst,
826 uint16_t inst_data,
827 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
828 if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) {
829 static const bool kIsRange = false;
830 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700831 self, shadow_frame, /* invoke_exact= */ false, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100832 } else {
833 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE);
834 static const bool kIsRange = true;
835 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700836 self, shadow_frame, /* invoke_exact= */ false, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100837 }
838}
839
Orion Hodson928033d2018-02-07 05:30:54 +0000840static bool DoVarHandleInvokeCommon(Thread* self,
841 ShadowFrame& shadow_frame,
842 const Instruction* inst,
843 uint16_t inst_data,
844 JValue* result,
845 mirror::VarHandle::AccessMode access_mode)
846 REQUIRES_SHARED(Locks::mutator_lock_) {
847 // Make sure to check for async exceptions
848 if (UNLIKELY(self->ObserveAsyncException())) {
849 return false;
850 }
851
Orion Hodson928033d2018-02-07 05:30:54 +0000852 StackHandleScope<2> hs(self);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100853 bool is_var_args = inst->HasVarArgs();
Orion Hodson06d10a72018-05-14 08:53:38 +0100854 const uint16_t vRegH = is_var_args ? inst->VRegH_45cc() : inst->VRegH_4rcc();
Orion Hodson928033d2018-02-07 05:30:54 +0000855 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
856 Handle<mirror::MethodType> callsite_type(hs.NewHandle(
Orion Hodson06d10a72018-05-14 08:53:38 +0100857 class_linker->ResolveMethodType(self, dex::ProtoIndex(vRegH), shadow_frame.GetMethod())));
Orion Hodson928033d2018-02-07 05:30:54 +0000858 // This implies we couldn't resolve one or more types in this VarHandle.
859 if (UNLIKELY(callsite_type == nullptr)) {
860 CHECK(self->IsExceptionPending());
861 return false;
862 }
863
Orion Hodson537a4fe2018-05-15 13:57:58 +0100864 const uint32_t vRegC = is_var_args ? inst->VRegC_45cc() : inst->VRegC_4rcc();
865 ObjPtr<mirror::Object> receiver(shadow_frame.GetVRegReference(vRegC));
Vladimir Marko179b7c62019-03-22 13:38:57 +0000866 Handle<mirror::VarHandle> var_handle(hs.NewHandle(ObjPtr<mirror::VarHandle>::DownCast(receiver)));
Orion Hodson928033d2018-02-07 05:30:54 +0000867 if (is_var_args) {
868 uint32_t args[Instruction::kMaxVarArgRegs];
869 inst->GetVarArgs(args, inst_data);
870 VarArgsInstructionOperands all_operands(args, inst->VRegA_45cc());
871 NoReceiverInstructionOperands operands(&all_operands);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100872 return VarHandleInvokeAccessor(self,
873 shadow_frame,
874 var_handle,
875 callsite_type,
876 access_mode,
877 &operands,
878 result);
Orion Hodson928033d2018-02-07 05:30:54 +0000879 } else {
880 RangeInstructionOperands all_operands(inst->VRegC_4rcc(), inst->VRegA_4rcc());
881 NoReceiverInstructionOperands operands(&all_operands);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100882 return VarHandleInvokeAccessor(self,
883 shadow_frame,
884 var_handle,
885 callsite_type,
886 access_mode,
887 &operands,
888 result);
Orion Hodson928033d2018-02-07 05:30:54 +0000889 }
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100890}
891
Orion Hodson928033d2018-02-07 05:30:54 +0000892#define DO_VAR_HANDLE_ACCESSOR(_access_mode) \
893bool DoVarHandle ## _access_mode(Thread* self, \
894 ShadowFrame& shadow_frame, \
895 const Instruction* inst, \
896 uint16_t inst_data, \
897 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { \
898 const auto access_mode = mirror::VarHandle::AccessMode::k ## _access_mode; \
899 return DoVarHandleInvokeCommon(self, shadow_frame, inst, inst_data, result, access_mode); \
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100900}
901
Orion Hodson928033d2018-02-07 05:30:54 +0000902DO_VAR_HANDLE_ACCESSOR(CompareAndExchange)
903DO_VAR_HANDLE_ACCESSOR(CompareAndExchangeAcquire)
904DO_VAR_HANDLE_ACCESSOR(CompareAndExchangeRelease)
905DO_VAR_HANDLE_ACCESSOR(CompareAndSet)
906DO_VAR_HANDLE_ACCESSOR(Get)
907DO_VAR_HANDLE_ACCESSOR(GetAcquire)
908DO_VAR_HANDLE_ACCESSOR(GetAndAdd)
909DO_VAR_HANDLE_ACCESSOR(GetAndAddAcquire)
910DO_VAR_HANDLE_ACCESSOR(GetAndAddRelease)
911DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAnd)
912DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAndAcquire)
913DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAndRelease)
914DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOr)
915DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOrAcquire)
916DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOrRelease)
917DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXor)
918DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXorAcquire)
919DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXorRelease)
920DO_VAR_HANDLE_ACCESSOR(GetAndSet)
921DO_VAR_HANDLE_ACCESSOR(GetAndSetAcquire)
922DO_VAR_HANDLE_ACCESSOR(GetAndSetRelease)
923DO_VAR_HANDLE_ACCESSOR(GetOpaque)
924DO_VAR_HANDLE_ACCESSOR(GetVolatile)
925DO_VAR_HANDLE_ACCESSOR(Set)
926DO_VAR_HANDLE_ACCESSOR(SetOpaque)
927DO_VAR_HANDLE_ACCESSOR(SetRelease)
928DO_VAR_HANDLE_ACCESSOR(SetVolatile)
929DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSet)
930DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetAcquire)
931DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetPlain)
932DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetRelease)
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100933
Orion Hodson928033d2018-02-07 05:30:54 +0000934#undef DO_VAR_HANDLE_ACCESSOR
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100935
936template<bool is_range>
937bool DoInvokePolymorphic(Thread* self,
938 ShadowFrame& shadow_frame,
939 const Instruction* inst,
940 uint16_t inst_data,
941 JValue* result) {
942 const int invoke_method_idx = inst->VRegB();
943 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
944 ArtMethod* invoke_method =
945 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
946 self, invoke_method_idx, shadow_frame.GetMethod(), kVirtual);
947
948 // Ensure intrinsic identifiers are initialized.
949 DCHECK(invoke_method->IsIntrinsic());
950
951 // Dispatch based on intrinsic identifier associated with method.
952 switch (static_cast<art::Intrinsics>(invoke_method->GetIntrinsic())) {
953#define CASE_SIGNATURE_POLYMORPHIC_INTRINSIC(Name, ...) \
954 case Intrinsics::k##Name: \
955 return Do ## Name(self, shadow_frame, inst, inst_data, result);
956#include "intrinsics_list.h"
957 SIGNATURE_POLYMORPHIC_INTRINSICS_LIST(CASE_SIGNATURE_POLYMORPHIC_INTRINSIC)
958#undef INTRINSICS_LIST
959#undef SIGNATURE_POLYMORPHIC_INTRINSICS_LIST
960#undef CASE_SIGNATURE_POLYMORPHIC_INTRINSIC
961 default:
962 LOG(FATAL) << "Unreachable: " << invoke_method->GetIntrinsic();
963 UNREACHABLE();
964 return false;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100965 }
966}
967
Orion Hodsona5dca522018-02-27 12:42:11 +0000968static JValue ConvertScalarBootstrapArgument(jvalue value) {
969 // value either contains a primitive scalar value if it corresponds
970 // to a primitive type, or it contains an integer value if it
971 // corresponds to an object instance reference id (e.g. a string id).
972 return JValue::FromPrimitive(value.j);
973}
974
975static ObjPtr<mirror::Class> GetClassForBootstrapArgument(EncodedArrayValueIterator::ValueType type)
976 REQUIRES_SHARED(Locks::mutator_lock_) {
977 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100978 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = class_linker->GetClassRoots();
Orion Hodsona5dca522018-02-27 12:42:11 +0000979 switch (type) {
980 case EncodedArrayValueIterator::ValueType::kBoolean:
981 case EncodedArrayValueIterator::ValueType::kByte:
982 case EncodedArrayValueIterator::ValueType::kChar:
983 case EncodedArrayValueIterator::ValueType::kShort:
984 // These types are disallowed by JVMS. Treat as integers. This
985 // will result in CCE's being raised if the BSM has one of these
986 // types.
987 case EncodedArrayValueIterator::ValueType::kInt:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100988 return GetClassRoot(ClassRoot::kPrimitiveInt, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000989 case EncodedArrayValueIterator::ValueType::kLong:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100990 return GetClassRoot(ClassRoot::kPrimitiveLong, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000991 case EncodedArrayValueIterator::ValueType::kFloat:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100992 return GetClassRoot(ClassRoot::kPrimitiveFloat, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000993 case EncodedArrayValueIterator::ValueType::kDouble:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100994 return GetClassRoot(ClassRoot::kPrimitiveDouble, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000995 case EncodedArrayValueIterator::ValueType::kMethodType:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100996 return GetClassRoot<mirror::MethodType>(class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000997 case EncodedArrayValueIterator::ValueType::kMethodHandle:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100998 return GetClassRoot<mirror::MethodHandle>(class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000999 case EncodedArrayValueIterator::ValueType::kString:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001000 return GetClassRoot<mirror::String>();
Orion Hodsona5dca522018-02-27 12:42:11 +00001001 case EncodedArrayValueIterator::ValueType::kType:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001002 return GetClassRoot<mirror::Class>();
Orion Hodsona5dca522018-02-27 12:42:11 +00001003 case EncodedArrayValueIterator::ValueType::kField:
1004 case EncodedArrayValueIterator::ValueType::kMethod:
1005 case EncodedArrayValueIterator::ValueType::kEnum:
1006 case EncodedArrayValueIterator::ValueType::kArray:
1007 case EncodedArrayValueIterator::ValueType::kAnnotation:
1008 case EncodedArrayValueIterator::ValueType::kNull:
1009 return nullptr;
1010 }
1011}
1012
1013static bool GetArgumentForBootstrapMethod(Thread* self,
1014 ArtMethod* referrer,
1015 EncodedArrayValueIterator::ValueType type,
1016 const JValue* encoded_value,
1017 JValue* decoded_value)
1018 REQUIRES_SHARED(Locks::mutator_lock_) {
1019 // The encoded_value contains either a scalar value (IJDF) or a
1020 // scalar DEX file index to a reference type to be materialized.
1021 switch (type) {
1022 case EncodedArrayValueIterator::ValueType::kInt:
1023 case EncodedArrayValueIterator::ValueType::kFloat:
1024 decoded_value->SetI(encoded_value->GetI());
1025 return true;
1026 case EncodedArrayValueIterator::ValueType::kLong:
1027 case EncodedArrayValueIterator::ValueType::kDouble:
1028 decoded_value->SetJ(encoded_value->GetJ());
1029 return true;
1030 case EncodedArrayValueIterator::ValueType::kMethodType: {
1031 StackHandleScope<2> hs(self);
1032 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader()));
1033 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
Orion Hodson06d10a72018-05-14 08:53:38 +01001034 dex::ProtoIndex proto_idx(encoded_value->GetC());
Orion Hodsona5dca522018-02-27 12:42:11 +00001035 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Orion Hodson06d10a72018-05-14 08:53:38 +01001036 ObjPtr<mirror::MethodType> o =
1037 cl->ResolveMethodType(self, proto_idx, dex_cache, class_loader);
Orion Hodsona5dca522018-02-27 12:42:11 +00001038 if (UNLIKELY(o.IsNull())) {
1039 DCHECK(self->IsExceptionPending());
1040 return false;
1041 }
1042 decoded_value->SetL(o);
1043 return true;
1044 }
1045 case EncodedArrayValueIterator::ValueType::kMethodHandle: {
1046 uint32_t index = static_cast<uint32_t>(encoded_value->GetI());
1047 ClassLinker* cl = Runtime::Current()->GetClassLinker();
1048 ObjPtr<mirror::MethodHandle> o = cl->ResolveMethodHandle(self, index, referrer);
1049 if (UNLIKELY(o.IsNull())) {
1050 DCHECK(self->IsExceptionPending());
1051 return false;
1052 }
1053 decoded_value->SetL(o);
1054 return true;
1055 }
1056 case EncodedArrayValueIterator::ValueType::kString: {
Orion Hodsona5dca522018-02-27 12:42:11 +00001057 dex::StringIndex index(static_cast<uint32_t>(encoded_value->GetI()));
1058 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Vladimir Marko18090d12018-06-01 16:53:12 +01001059 ObjPtr<mirror::String> o = cl->ResolveString(index, referrer);
Orion Hodsona5dca522018-02-27 12:42:11 +00001060 if (UNLIKELY(o.IsNull())) {
1061 DCHECK(self->IsExceptionPending());
1062 return false;
1063 }
1064 decoded_value->SetL(o);
1065 return true;
1066 }
1067 case EncodedArrayValueIterator::ValueType::kType: {
Orion Hodsona5dca522018-02-27 12:42:11 +00001068 dex::TypeIndex index(static_cast<uint32_t>(encoded_value->GetI()));
1069 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Vladimir Marko09c5ca42018-05-31 15:15:31 +01001070 ObjPtr<mirror::Class> o = cl->ResolveType(index, referrer);
Orion Hodsona5dca522018-02-27 12:42:11 +00001071 if (UNLIKELY(o.IsNull())) {
1072 DCHECK(self->IsExceptionPending());
1073 return false;
1074 }
1075 decoded_value->SetL(o);
1076 return true;
1077 }
1078 case EncodedArrayValueIterator::ValueType::kBoolean:
1079 case EncodedArrayValueIterator::ValueType::kByte:
1080 case EncodedArrayValueIterator::ValueType::kChar:
1081 case EncodedArrayValueIterator::ValueType::kShort:
1082 case EncodedArrayValueIterator::ValueType::kField:
1083 case EncodedArrayValueIterator::ValueType::kMethod:
1084 case EncodedArrayValueIterator::ValueType::kEnum:
1085 case EncodedArrayValueIterator::ValueType::kArray:
1086 case EncodedArrayValueIterator::ValueType::kAnnotation:
1087 case EncodedArrayValueIterator::ValueType::kNull:
1088 // Unreachable - unsupported types that have been checked when
1089 // determining the effect call site type based on the bootstrap
1090 // argument types.
1091 UNREACHABLE();
1092 }
1093}
1094
1095static bool PackArgumentForBootstrapMethod(Thread* self,
1096 ArtMethod* referrer,
1097 CallSiteArrayValueIterator* it,
1098 ShadowFrameSetter* setter)
1099 REQUIRES_SHARED(Locks::mutator_lock_) {
1100 auto type = it->GetValueType();
1101 const JValue encoded_value = ConvertScalarBootstrapArgument(it->GetJavaValue());
1102 JValue decoded_value;
1103 if (!GetArgumentForBootstrapMethod(self, referrer, type, &encoded_value, &decoded_value)) {
1104 return false;
1105 }
1106 switch (it->GetValueType()) {
1107 case EncodedArrayValueIterator::ValueType::kInt:
1108 case EncodedArrayValueIterator::ValueType::kFloat:
1109 setter->Set(static_cast<uint32_t>(decoded_value.GetI()));
1110 return true;
1111 case EncodedArrayValueIterator::ValueType::kLong:
1112 case EncodedArrayValueIterator::ValueType::kDouble:
1113 setter->SetLong(decoded_value.GetJ());
1114 return true;
1115 case EncodedArrayValueIterator::ValueType::kMethodType:
1116 case EncodedArrayValueIterator::ValueType::kMethodHandle:
1117 case EncodedArrayValueIterator::ValueType::kString:
1118 case EncodedArrayValueIterator::ValueType::kType:
1119 setter->SetReference(decoded_value.GetL());
1120 return true;
1121 case EncodedArrayValueIterator::ValueType::kBoolean:
1122 case EncodedArrayValueIterator::ValueType::kByte:
1123 case EncodedArrayValueIterator::ValueType::kChar:
1124 case EncodedArrayValueIterator::ValueType::kShort:
1125 case EncodedArrayValueIterator::ValueType::kField:
1126 case EncodedArrayValueIterator::ValueType::kMethod:
1127 case EncodedArrayValueIterator::ValueType::kEnum:
1128 case EncodedArrayValueIterator::ValueType::kArray:
1129 case EncodedArrayValueIterator::ValueType::kAnnotation:
1130 case EncodedArrayValueIterator::ValueType::kNull:
1131 // Unreachable - unsupported types that have been checked when
1132 // determining the effect call site type based on the bootstrap
1133 // argument types.
1134 UNREACHABLE();
1135 }
1136}
1137
1138static bool PackCollectorArrayForBootstrapMethod(Thread* self,
1139 ArtMethod* referrer,
1140 ObjPtr<mirror::Class> array_type,
1141 int32_t array_length,
1142 CallSiteArrayValueIterator* it,
1143 ShadowFrameSetter* setter)
1144 REQUIRES_SHARED(Locks::mutator_lock_) {
1145 StackHandleScope<1> hs(self);
1146 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1147 JValue decoded_value;
1148
1149#define COLLECT_PRIMITIVE_ARRAY(Descriptor, Type) \
1150 Handle<mirror::Type ## Array> array = \
1151 hs.NewHandle(mirror::Type ## Array::Alloc(self, array_length)); \
1152 if (array.IsNull()) { \
1153 return false; \
1154 } \
1155 for (int32_t i = 0; it->HasNext(); it->Next(), ++i) { \
1156 auto type = it->GetValueType(); \
1157 DCHECK_EQ(type, EncodedArrayValueIterator::ValueType::k ## Type); \
1158 const JValue encoded_value = \
1159 ConvertScalarBootstrapArgument(it->GetJavaValue()); \
1160 GetArgumentForBootstrapMethod(self, \
1161 referrer, \
1162 type, \
1163 &encoded_value, \
1164 &decoded_value); \
1165 array->Set(i, decoded_value.Get ## Descriptor()); \
1166 } \
1167 setter->SetReference(array.Get()); \
1168 return true;
1169
1170#define COLLECT_REFERENCE_ARRAY(T, Type) \
Andreas Gampe584771b2018-10-18 13:22:23 -07001171 Handle<mirror::ObjectArray<T>> array = /* NOLINT */ \
Orion Hodsona5dca522018-02-27 12:42:11 +00001172 hs.NewHandle(mirror::ObjectArray<T>::Alloc(self, \
1173 array_type, \
1174 array_length)); \
1175 if (array.IsNull()) { \
1176 return false; \
1177 } \
1178 for (int32_t i = 0; it->HasNext(); it->Next(), ++i) { \
1179 auto type = it->GetValueType(); \
1180 DCHECK_EQ(type, EncodedArrayValueIterator::ValueType::k ## Type); \
1181 const JValue encoded_value = \
1182 ConvertScalarBootstrapArgument(it->GetJavaValue()); \
1183 if (!GetArgumentForBootstrapMethod(self, \
1184 referrer, \
1185 type, \
1186 &encoded_value, \
1187 &decoded_value)) { \
1188 return false; \
1189 } \
1190 ObjPtr<mirror::Object> o = decoded_value.GetL(); \
1191 if (Runtime::Current()->IsActiveTransaction()) { \
1192 array->Set<true>(i, ObjPtr<T>::DownCast(o)); \
1193 } else { \
1194 array->Set<false>(i, ObjPtr<T>::DownCast(o)); \
1195 } \
1196 } \
1197 setter->SetReference(array.Get()); \
1198 return true;
1199
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001200 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = class_linker->GetClassRoots();
1201 ObjPtr<mirror::Class> component_type = array_type->GetComponentType();
1202 if (component_type == GetClassRoot(ClassRoot::kPrimitiveInt, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001203 COLLECT_PRIMITIVE_ARRAY(I, Int);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001204 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveLong, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001205 COLLECT_PRIMITIVE_ARRAY(J, Long);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001206 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveFloat, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001207 COLLECT_PRIMITIVE_ARRAY(F, Float);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001208 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveDouble, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001209 COLLECT_PRIMITIVE_ARRAY(D, Double);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001210 } else if (component_type == GetClassRoot<mirror::MethodType>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001211 COLLECT_REFERENCE_ARRAY(mirror::MethodType, MethodType);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001212 } else if (component_type == GetClassRoot<mirror::MethodHandle>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001213 COLLECT_REFERENCE_ARRAY(mirror::MethodHandle, MethodHandle);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001214 } else if (component_type == GetClassRoot<mirror::String>(class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001215 COLLECT_REFERENCE_ARRAY(mirror::String, String);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001216 } else if (component_type == GetClassRoot<mirror::Class>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001217 COLLECT_REFERENCE_ARRAY(mirror::Class, Type);
1218 } else {
1219 UNREACHABLE();
1220 }
1221 #undef COLLECT_PRIMITIVE_ARRAY
1222 #undef COLLECT_REFERENCE_ARRAY
1223}
1224
1225static ObjPtr<mirror::MethodType> BuildCallSiteForBootstrapMethod(Thread* self,
1226 const DexFile* dex_file,
1227 uint32_t call_site_idx)
1228 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001229 const dex::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx);
Orion Hodsona5dca522018-02-27 12:42:11 +00001230 CallSiteArrayValueIterator it(*dex_file, csi);
1231 DCHECK_GE(it.Size(), 1u);
1232
1233 StackHandleScope<2> hs(self);
1234 // Create array for parameter types.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001235 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001236 ObjPtr<mirror::Class> class_array_type =
1237 GetClassRoot<mirror::ObjectArray<mirror::Class>>(class_linker);
Orion Hodsona5dca522018-02-27 12:42:11 +00001238 Handle<mirror::ObjectArray<mirror::Class>> ptypes = hs.NewHandle(
1239 mirror::ObjectArray<mirror::Class>::Alloc(self,
1240 class_array_type,
1241 static_cast<int>(it.Size())));
1242 if (ptypes.IsNull()) {
1243 DCHECK(self->IsExceptionPending());
1244 return nullptr;
1245 }
1246
1247 // Populate the first argument with an instance of j.l.i.MethodHandles.Lookup
1248 // that the runtime will construct.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001249 ptypes->Set(0, GetClassRoot<mirror::MethodHandlesLookup>(class_linker));
Orion Hodsona5dca522018-02-27 12:42:11 +00001250 it.Next();
1251
1252 // The remaining parameter types are derived from the types of
1253 // arguments present in the DEX file.
1254 int index = 1;
1255 while (it.HasNext()) {
1256 ObjPtr<mirror::Class> ptype = GetClassForBootstrapArgument(it.GetValueType());
1257 if (ptype.IsNull()) {
1258 ThrowClassCastException("Unsupported bootstrap argument type");
1259 return nullptr;
1260 }
1261 ptypes->Set(index, ptype);
1262 index++;
1263 it.Next();
1264 }
1265 DCHECK_EQ(static_cast<size_t>(index), it.Size());
1266
1267 // By definition, the return type is always a j.l.i.CallSite.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001268 Handle<mirror::Class> rtype = hs.NewHandle(GetClassRoot<mirror::CallSite>());
Orion Hodsona5dca522018-02-27 12:42:11 +00001269 return mirror::MethodType::Create(self, rtype, ptypes);
1270}
1271
Orion Hodsonc069a302017-01-18 09:23:12 +00001272static ObjPtr<mirror::CallSite> InvokeBootstrapMethod(Thread* self,
1273 ShadowFrame& shadow_frame,
1274 uint32_t call_site_idx)
1275 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001276 StackHandleScope<5> hs(self);
Orion Hodsona5dca522018-02-27 12:42:11 +00001277 // There are three mandatory arguments expected from the call site
1278 // value array in the DEX file: the bootstrap method handle, the
1279 // method name to pass to the bootstrap method, and the method type
1280 // to pass to the bootstrap method.
1281 static constexpr size_t kMandatoryArgumentsCount = 3;
Orion Hodsonc069a302017-01-18 09:23:12 +00001282 ArtMethod* referrer = shadow_frame.GetMethod();
1283 const DexFile* dex_file = referrer->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001284 const dex::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00001285 CallSiteArrayValueIterator it(*dex_file, csi);
Orion Hodsona5dca522018-02-27 12:42:11 +00001286 if (it.Size() < kMandatoryArgumentsCount) {
1287 ThrowBootstrapMethodError("Truncated bootstrap arguments (%zu < %zu)",
1288 it.Size(), kMandatoryArgumentsCount);
1289 return nullptr;
1290 }
1291
1292 if (it.GetValueType() != EncodedArrayValueIterator::ValueType::kMethodHandle) {
1293 ThrowBootstrapMethodError("First bootstrap argument is not a method handle");
1294 return nullptr;
1295 }
1296
1297 uint32_t bsm_index = static_cast<uint32_t>(it.GetJavaValue().i);
1298 it.Next();
1299
Orion Hodsonc069a302017-01-18 09:23:12 +00001300 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Orion Hodsona5dca522018-02-27 12:42:11 +00001301 Handle<mirror::MethodHandle> bsm =
1302 hs.NewHandle(class_linker->ResolveMethodHandle(self, bsm_index, referrer));
1303 if (bsm.IsNull()) {
Orion Hodsonc069a302017-01-18 09:23:12 +00001304 DCHECK(self->IsExceptionPending());
1305 return nullptr;
1306 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001307
Orion Hodsona5dca522018-02-27 12:42:11 +00001308 if (bsm->GetHandleKind() != mirror::MethodHandle::Kind::kInvokeStatic) {
1309 // JLS suggests also accepting constructors. This is currently
1310 // hard as constructor invocations happen via transformers in ART
1311 // today. The constructor would need to be a class derived from java.lang.invoke.CallSite.
1312 ThrowBootstrapMethodError("Unsupported bootstrap method invocation kind");
1313 return nullptr;
1314 }
1315
1316 // Construct the local call site type information based on the 3
1317 // mandatory arguments provided by the runtime and the static arguments
1318 // in the DEX file. We will use these arguments to build a shadow frame.
1319 MutableHandle<mirror::MethodType> call_site_type =
1320 hs.NewHandle(BuildCallSiteForBootstrapMethod(self, dex_file, call_site_idx));
1321 if (call_site_type.IsNull()) {
1322 DCHECK(self->IsExceptionPending());
1323 return nullptr;
1324 }
1325
1326 // Check if this BSM is targeting a variable arity method. If so,
1327 // we'll need to collect the trailing arguments into an array.
1328 Handle<mirror::Array> collector_arguments;
1329 int32_t collector_arguments_length;
1330 if (bsm->GetTargetMethod()->IsVarargs()) {
1331 int number_of_bsm_parameters = bsm->GetMethodType()->GetNumberOfPTypes();
1332 if (number_of_bsm_parameters == 0) {
1333 ThrowBootstrapMethodError("Variable arity BSM does not have any arguments");
1334 return nullptr;
1335 }
1336 Handle<mirror::Class> collector_array_class =
1337 hs.NewHandle(bsm->GetMethodType()->GetPTypes()->Get(number_of_bsm_parameters - 1));
1338 if (!collector_array_class->IsArrayClass()) {
1339 ThrowBootstrapMethodError("Variable arity BSM does not have array as final argument");
1340 return nullptr;
1341 }
1342 // The call site may include no arguments to be collected. In this
1343 // case the number of arguments must be at least the number of BSM
1344 // parameters less the collector array.
1345 if (call_site_type->GetNumberOfPTypes() < number_of_bsm_parameters - 1) {
1346 ThrowWrongMethodTypeException(bsm->GetMethodType(), call_site_type.Get());
1347 return nullptr;
1348 }
1349 // Check all the arguments to be collected match the collector array component type.
1350 for (int i = number_of_bsm_parameters - 1; i < call_site_type->GetNumberOfPTypes(); ++i) {
1351 if (call_site_type->GetPTypes()->Get(i) != collector_array_class->GetComponentType()) {
1352 ThrowClassCastException(collector_array_class->GetComponentType(),
1353 call_site_type->GetPTypes()->Get(i));
1354 return nullptr;
1355 }
1356 }
1357 // Update the call site method type so it now includes the collector array.
1358 int32_t collector_arguments_start = number_of_bsm_parameters - 1;
1359 collector_arguments_length = call_site_type->GetNumberOfPTypes() - number_of_bsm_parameters + 1;
1360 call_site_type.Assign(
1361 mirror::MethodType::CollectTrailingArguments(self,
1362 call_site_type.Get(),
1363 collector_array_class.Get(),
1364 collector_arguments_start));
1365 if (call_site_type.IsNull()) {
1366 DCHECK(self->IsExceptionPending());
1367 return nullptr;
1368 }
1369 } else {
1370 collector_arguments_length = 0;
1371 }
1372
1373 if (call_site_type->GetNumberOfPTypes() != bsm->GetMethodType()->GetNumberOfPTypes()) {
1374 ThrowWrongMethodTypeException(bsm->GetMethodType(), call_site_type.Get());
1375 return nullptr;
1376 }
1377
1378 // BSM invocation has a different set of exceptions that
1379 // j.l.i.MethodHandle.invoke(). Scan arguments looking for CCE
1380 // "opportunities". Unfortunately we cannot just leave this to the
1381 // method handle invocation as this might generate a WMTE.
1382 for (int32_t i = 0; i < call_site_type->GetNumberOfPTypes(); ++i) {
1383 ObjPtr<mirror::Class> from = call_site_type->GetPTypes()->Get(i);
1384 ObjPtr<mirror::Class> to = bsm->GetMethodType()->GetPTypes()->Get(i);
1385 if (!IsParameterTypeConvertible(from, to)) {
1386 ThrowClassCastException(from, to);
1387 return nullptr;
1388 }
1389 }
1390 if (!IsReturnTypeConvertible(call_site_type->GetRType(), bsm->GetMethodType()->GetRType())) {
1391 ThrowClassCastException(bsm->GetMethodType()->GetRType(), call_site_type->GetRType());
1392 return nullptr;
1393 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001394
1395 // Set-up a shadow frame for invoking the bootstrap method handle.
1396 ShadowFrameAllocaUniquePtr bootstrap_frame =
Orion Hodsona5dca522018-02-27 12:42:11 +00001397 CREATE_SHADOW_FRAME(call_site_type->NumberOfVRegs(),
1398 nullptr,
1399 referrer,
1400 shadow_frame.GetDexPC());
Orion Hodsonc069a302017-01-18 09:23:12 +00001401 ScopedStackedShadowFramePusher pusher(
1402 self, bootstrap_frame.get(), StackedShadowFrameType::kShadowFrameUnderConstruction);
Orion Hodsona5dca522018-02-27 12:42:11 +00001403 ShadowFrameSetter setter(bootstrap_frame.get(), 0u);
Orion Hodsonc069a302017-01-18 09:23:12 +00001404
1405 // The first parameter is a MethodHandles lookup instance.
Orion Hodsona5dca522018-02-27 12:42:11 +00001406 Handle<mirror::Class> lookup_class =
1407 hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass());
1408 ObjPtr<mirror::MethodHandlesLookup> lookup =
1409 mirror::MethodHandlesLookup::Create(self, lookup_class);
1410 if (lookup.IsNull()) {
Orion Hodsonc069a302017-01-18 09:23:12 +00001411 DCHECK(self->IsExceptionPending());
1412 return nullptr;
1413 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001414 setter.SetReference(lookup);
Orion Hodsonc069a302017-01-18 09:23:12 +00001415
Orion Hodsona5dca522018-02-27 12:42:11 +00001416 // Pack the remaining arguments into the frame.
1417 int number_of_arguments = call_site_type->GetNumberOfPTypes();
1418 int argument_index;
1419 for (argument_index = 1; argument_index < number_of_arguments; ++argument_index) {
1420 if (argument_index == number_of_arguments - 1 &&
1421 call_site_type->GetPTypes()->Get(argument_index)->IsArrayClass()) {
1422 ObjPtr<mirror::Class> array_type = call_site_type->GetPTypes()->Get(argument_index);
1423 if (!PackCollectorArrayForBootstrapMethod(self,
1424 referrer,
1425 array_type,
1426 collector_arguments_length,
1427 &it,
1428 &setter)) {
1429 DCHECK(self->IsExceptionPending());
1430 return nullptr;
Orion Hodsonc069a302017-01-18 09:23:12 +00001431 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001432 } else if (!PackArgumentForBootstrapMethod(self, referrer, &it, &setter)) {
1433 DCHECK(self->IsExceptionPending());
1434 return nullptr;
Orion Hodsonc069a302017-01-18 09:23:12 +00001435 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001436 it.Next();
1437 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001438 DCHECK(!it.HasNext());
1439 DCHECK(setter.Done());
Orion Hodsonc069a302017-01-18 09:23:12 +00001440
1441 // Invoke the bootstrap method handle.
1442 JValue result;
Orion Hodsona5dca522018-02-27 12:42:11 +00001443 RangeInstructionOperands operands(0, bootstrap_frame->NumberOfVRegs());
1444 bool invoke_success = MethodHandleInvoke(self,
1445 *bootstrap_frame,
1446 bsm,
1447 call_site_type,
1448 &operands,
1449 &result);
Orion Hodsonc069a302017-01-18 09:23:12 +00001450 if (!invoke_success) {
1451 DCHECK(self->IsExceptionPending());
1452 return nullptr;
1453 }
1454
1455 Handle<mirror::Object> object(hs.NewHandle(result.GetL()));
Orion Hodsonc069a302017-01-18 09:23:12 +00001456 if (UNLIKELY(object.IsNull())) {
Orion Hodsonda1cdd02018-01-31 18:08:28 +00001457 // This will typically be for LambdaMetafactory which is not supported.
Orion Hodson76e6adb2018-02-23 13:15:55 +00001458 ThrowClassCastException("Bootstrap method returned null");
Orion Hodsonc069a302017-01-18 09:23:12 +00001459 return nullptr;
1460 }
1461
Orion Hodsona5dca522018-02-27 12:42:11 +00001462 // Check the result type is a subclass of j.l.i.CallSite.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001463 ObjPtr<mirror::Class> call_site_class = GetClassRoot<mirror::CallSite>(class_linker);
1464 if (UNLIKELY(!object->InstanceOf(call_site_class))) {
1465 ThrowClassCastException(object->GetClass(), call_site_class);
Orion Hodsonc069a302017-01-18 09:23:12 +00001466 return nullptr;
1467 }
1468
Orion Hodsona5dca522018-02-27 12:42:11 +00001469 // Check the call site target is not null as we're going to invoke it.
Vladimir Markod7e9bbf2019-03-28 13:18:57 +00001470 ObjPtr<mirror::CallSite> call_site = ObjPtr<mirror::CallSite>::DownCast(result.GetL());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001471 ObjPtr<mirror::MethodHandle> target = call_site->GetTarget();
1472 if (UNLIKELY(target == nullptr)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001473 ThrowClassCastException("Bootstrap method returned a CallSite with a null target");
Orion Hodsonc069a302017-01-18 09:23:12 +00001474 return nullptr;
1475 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001476 return call_site;
Orion Hodsonc069a302017-01-18 09:23:12 +00001477}
1478
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001479namespace {
1480
1481ObjPtr<mirror::CallSite> DoResolveCallSite(Thread* self,
1482 ShadowFrame& shadow_frame,
1483 uint32_t call_site_idx)
1484 REQUIRES_SHARED(Locks::mutator_lock_) {
1485 StackHandleScope<1> hs(self);
1486 Handle<mirror::DexCache> dex_cache(hs.NewHandle(shadow_frame.GetMethod()->GetDexCache()));
1487
1488 // Get the call site from the DexCache if present.
1489 ObjPtr<mirror::CallSite> call_site = dex_cache->GetResolvedCallSite(call_site_idx);
1490 if (LIKELY(call_site != nullptr)) {
1491 return call_site;
1492 }
1493
1494 // Invoke the bootstrap method to get a candidate call site.
1495 call_site = InvokeBootstrapMethod(self, shadow_frame, call_site_idx);
1496 if (UNLIKELY(call_site == nullptr)) {
1497 if (!self->GetException()->IsError()) {
1498 // Use a BootstrapMethodError if the exception is not an instance of java.lang.Error.
1499 ThrowWrappedBootstrapMethodError("Exception from call site #%u bootstrap method",
1500 call_site_idx);
1501 }
1502 return nullptr;
1503 }
1504
1505 // Attempt to place the candidate call site into the DexCache, return the winning call site.
1506 return dex_cache->SetResolvedCallSite(call_site_idx, call_site);
1507}
1508
1509} // namespace
1510
Orion Hodsonc069a302017-01-18 09:23:12 +00001511bool DoInvokeCustom(Thread* self,
1512 ShadowFrame& shadow_frame,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001513 uint32_t call_site_idx,
1514 const InstructionOperands* operands,
1515 JValue* result) {
Alex Light848574c2017-09-25 16:59:39 -07001516 // Make sure to check for async exceptions
1517 if (UNLIKELY(self->ObserveAsyncException())) {
1518 return false;
1519 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001520
Orion Hodsonc069a302017-01-18 09:23:12 +00001521 // invoke-custom is not supported in transactions. In transactions
1522 // there is a limited set of types supported. invoke-custom allows
1523 // running arbitrary code and instantiating arbitrary types.
1524 CHECK(!Runtime::Current()->IsActiveTransaction());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001525
1526 ObjPtr<mirror::CallSite> call_site = DoResolveCallSite(self, shadow_frame, call_site_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00001527 if (call_site.IsNull()) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001528 DCHECK(self->IsExceptionPending());
1529 return false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001530 }
1531
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001532 StackHandleScope<2> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +00001533 Handle<mirror::MethodHandle> target = hs.NewHandle(call_site->GetTarget());
1534 Handle<mirror::MethodType> target_method_type = hs.NewHandle(target->GetMethodType());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001535 DCHECK_EQ(operands->GetNumberOfOperands(), target_method_type->NumberOfVRegs())
1536 << " call_site_idx" << call_site_idx;
1537 return MethodHandleInvokeExact(self,
1538 shadow_frame,
1539 target,
1540 target_method_type,
1541 operands,
1542 result);
Orion Hodsonc069a302017-01-18 09:23:12 +00001543}
1544
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001545// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
1546static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
1547 size_t dest_reg, size_t src_reg)
1548 REQUIRES_SHARED(Locks::mutator_lock_) {
1549 // Uint required, so that sign extension does not make this wrong on 64b systems
1550 uint32_t src_value = shadow_frame.GetVReg(src_reg);
1551 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
1552
1553 // If both register locations contains the same value, the register probably holds a reference.
1554 // Note: As an optimization, non-moving collectors leave a stale reference value
1555 // in the references array even after the original vreg was overwritten to a non-reference.
Vladimir Marko78baed52018-10-11 10:44:58 +01001556 if (src_value == reinterpret_cast32<uint32_t>(o.Ptr())) {
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001557 new_shadow_frame->SetVRegReference(dest_reg, o);
1558 } else {
1559 new_shadow_frame->SetVReg(dest_reg, src_value);
1560 }
1561}
1562
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001563template <bool is_range>
1564inline void CopyRegisters(ShadowFrame& caller_frame,
1565 ShadowFrame* callee_frame,
1566 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
1567 const size_t first_src_reg,
1568 const size_t first_dest_reg,
1569 const size_t num_regs) {
1570 if (is_range) {
1571 const size_t dest_reg_bound = first_dest_reg + num_regs;
1572 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound;
1573 ++dest_reg, ++src_reg) {
1574 AssignRegister(callee_frame, caller_frame, dest_reg, src_reg);
1575 }
1576 } else {
1577 DCHECK_LE(num_regs, arraysize(arg));
1578
1579 for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) {
1580 AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]);
1581 }
1582 }
1583}
1584
Igor Murashkin6918bf12015-09-27 19:19:06 -07001585template <bool is_range,
Narayan Kamath370423d2016-10-03 16:51:22 +01001586 bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001587static inline bool DoCallCommon(ArtMethod* called_method,
1588 Thread* self,
1589 ShadowFrame& shadow_frame,
1590 JValue* result,
1591 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +01001592 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -07001593 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001594 bool string_init = false;
1595 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001596 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
1597 && called_method->IsConstructor())) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001598 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
Jeff Hao848f70a2014-01-15 13:49:50 -08001599 string_init = true;
1600 }
1601
Alex Lightdaf58c82016-03-16 23:00:49 +00001602 // Compute method information.
David Sehr0225f8e2018-01-31 08:52:24 +00001603 CodeItemDataAccessor accessor(called_method->DexInstructionData());
Igor Murashkin158f35c2015-06-10 15:55:30 -07001604 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001605 uint16_t num_regs;
Jeff Hao5ea84132017-05-05 16:59:29 -07001606 // Test whether to use the interpreter or compiler entrypoint, and save that result to pass to
1607 // PerformCall. A deoptimization could occur at any time, and we shouldn't change which
1608 // entrypoint to use once we start building the shadow frame.
Mathieu Chartier448bbcf2017-07-06 12:05:13 -07001609
1610 // For unstarted runtimes, always use the interpreter entrypoint. This fixes the case where we are
1611 // doing cross compilation. Note that GetEntryPointFromQuickCompiledCode doesn't use the image
1612 // pointer size here and this may case an overflow if it is called from the compiler. b/62402160
1613 const bool use_interpreter_entrypoint = !Runtime::Current()->IsStarted() ||
1614 ClassLinker::ShouldUseInterpreterEntrypoint(
1615 called_method,
1616 called_method->GetEntryPointFromQuickCompiledCode());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001617 if (LIKELY(accessor.HasCodeItem())) {
Jeff Hao5ea84132017-05-05 16:59:29 -07001618 // When transitioning to compiled code, space only needs to be reserved for the input registers.
1619 // The rest of the frame gets discarded. This also prevents accessing the called method's code
1620 // item, saving memory by keeping code items of compiled code untouched.
Mathieu Chartier448bbcf2017-07-06 12:05:13 -07001621 if (!use_interpreter_entrypoint) {
1622 DCHECK(!Runtime::Current()->IsAotCompiler()) << "Compiler should use interpreter entrypoint";
Jeff Hao5ea84132017-05-05 16:59:29 -07001623 num_regs = number_of_inputs;
1624 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001625 num_regs = accessor.RegistersSize();
1626 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, accessor.InsSize());
Jeff Hao5ea84132017-05-05 16:59:29 -07001627 }
Nicolas Geoffray01822292017-03-09 09:03:19 +00001628 } else {
1629 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1630 num_regs = number_of_inputs;
Jeff Haodf79ddb2017-02-27 14:47:06 -08001631 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001632
Igor Murashkin158f35c2015-06-10 15:55:30 -07001633 // Hack for String init:
1634 //
1635 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
1636 // invoke-x StringFactory(a, b, c, ...)
1637 // by effectively dropping the first virtual register from the invoke.
1638 //
1639 // (at this point the ArtMethod has already been replaced,
1640 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +00001641 //
1642 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
1643 // to handle the compiler optimization of replacing `this` with null without
1644 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001645 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -07001646 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001647 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 -07001648
Igor Murashkin158f35c2015-06-10 15:55:30 -07001649 // The new StringFactory call is static and has one fewer argument.
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001650 if (!accessor.HasCodeItem()) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001651 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1652 num_regs--;
1653 } // 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 -07001654 number_of_inputs--;
1655
1656 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -07001657 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001658 arg[i - 1] = arg[i];
1659 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07001660 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001661
1662 // Rewrite the non-var-arg case
1663 vregC++; // Skips the 0th vreg in the range ("this").
1664 }
1665
1666 // Parameter registers go at the end of the shadow frame.
1667 DCHECK_GE(num_regs, number_of_inputs);
1668 size_t first_dest_reg = num_regs - number_of_inputs;
1669 DCHECK_NE(first_dest_reg, (size_t)-1);
1670
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001671 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001672 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -07001673 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -07001674 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -07001675 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001676
Igor Murashkin158f35c2015-06-10 15:55:30 -07001677 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -07001678 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001679 // Slow path.
1680 // We might need to do class loading, which incurs a thread state change to kNative. So
1681 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07001682 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +02001683 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001684 self->EndAssertNoThreadSuspension(old_cause);
1685
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001686 // ArtMethod here is needed to check type information of the call site against the callee.
1687 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
1688 //
1689 // As a special case for proxy methods, which are not dex-backed,
1690 // we have to retrieve type information from the proxy's method
1691 // interface method instead (which is dex backed since proxies are never interfaces).
Andreas Gampe542451c2016-07-26 09:02:02 -07001692 ArtMethod* method =
1693 new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001694
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001695 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001696 // to get the exact type of each reference argument.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001697 const dex::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001698 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001699 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001700
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001701 // Handle receiver apart since it's not part of the shorty.
1702 size_t dest_reg = first_dest_reg;
1703 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001704
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001705 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001706 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001707 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
1708 ++dest_reg;
1709 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -07001710 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001711 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001712
1713 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001714 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001715 // Skip the 0th 'shorty' type since it represents the return type.
1716 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001717 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
1718 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001719 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001720 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001721 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001722 if (do_assignability_check && o != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001723 const dex::TypeIndex type_idx = params->GetTypeItem(shorty_pos).type_idx_;
Vladimir Marko942fd312017-01-16 20:52:19 +00001724 ObjPtr<mirror::Class> arg_type = method->GetDexCache()->GetResolvedType(type_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001725 if (arg_type == nullptr) {
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001726 StackHandleScope<1> hs(self);
1727 // Preserve o since it is used below and GetClassFromTypeIndex may cause thread
1728 // suspension.
1729 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&o);
Vladimir Markob45528c2017-07-27 14:14:28 +01001730 arg_type = method->ResolveClassFromTypeIndex(type_idx);
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001731 if (arg_type == nullptr) {
1732 CHECK(self->IsExceptionPending());
1733 return false;
1734 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001735 }
1736 if (!o->VerifierInstanceOf(arg_type)) {
1737 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -07001738 std::string temp1, temp2;
Orion Hodsonfef06642016-11-25 16:07:11 +00001739 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001740 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -08001741 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -07001742 o->GetClass()->GetDescriptor(&temp1),
1743 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001744 return false;
1745 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001746 }
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001747 new_shadow_frame->SetVRegReference(dest_reg, o);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001748 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -07001749 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001750 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001751 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001752 uint64_t wide_value =
1753 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
1754 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001755 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001756 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001757 ++dest_reg;
1758 ++arg_offset;
1759 break;
1760 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001761 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001762 default:
1763 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
1764 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001765 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001766 }
1767 } else {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001768 if (is_range) {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001769 DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001770 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001771
1772 CopyRegisters<is_range>(shadow_frame,
1773 new_shadow_frame,
1774 arg,
1775 vregC,
1776 first_dest_reg,
1777 number_of_inputs);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001778 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001779 }
1780
Jeff Hao5ea84132017-05-05 16:59:29 -07001781 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001782 accessor,
Jeff Hao5ea84132017-05-05 16:59:29 -07001783 shadow_frame.GetMethod(),
1784 first_dest_reg,
1785 new_shadow_frame,
1786 result,
1787 use_interpreter_entrypoint);
Jeff Hao848f70a2014-01-15 13:49:50 -08001788
1789 if (string_init && !self->IsExceptionPending()) {
Mingyao Yangffedec52016-05-19 10:48:40 -07001790 SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001791 }
1792
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001793 return !self->IsExceptionPending();
1794}
1795
Igor Murashkin158f35c2015-06-10 15:55:30 -07001796template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001797bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
1798 const Instruction* inst, uint16_t inst_data, JValue* result) {
1799 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001800 const uint16_t number_of_inputs =
1801 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001802
1803 // TODO: find a cleaner way to separate non-range and range information without duplicating
1804 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -07001805 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001806 uint32_t vregC = 0;
1807 if (is_range) {
1808 vregC = inst->VRegC_3rc();
1809 } else {
1810 vregC = inst->VRegC_35c();
1811 inst->GetVarArgs(arg, inst_data);
1812 }
1813
1814 return DoCallCommon<is_range, do_assignability_check>(
1815 called_method, self, shadow_frame,
1816 result, number_of_inputs, arg, vregC);
1817}
1818
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001819template <bool is_range, bool do_access_check, bool transaction_active>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001820bool DoFilledNewArray(const Instruction* inst,
1821 const ShadowFrame& shadow_frame,
1822 Thread* self,
1823 JValue* result) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001824 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
1825 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
1826 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
1827 if (!is_range) {
1828 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
1829 CHECK_LE(length, 5);
1830 }
1831 if (UNLIKELY(length < 0)) {
1832 ThrowNegativeArraySizeException(length);
1833 return false;
1834 }
1835 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08001836 ObjPtr<mirror::Class> array_class = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
Mathieu Chartieref41db72016-10-25 15:08:01 -07001837 shadow_frame.GetMethod(),
1838 self,
1839 false,
1840 do_access_check);
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001841 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001842 DCHECK(self->IsExceptionPending());
1843 return false;
1844 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001845 CHECK(array_class->IsArrayClass());
Mathieu Chartieref41db72016-10-25 15:08:01 -07001846 ObjPtr<mirror::Class> component_class = array_class->GetComponentType();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001847 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
1848 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
1849 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001850 ThrowRuntimeException("Bad filled array request for type %s",
David Sehr709b0702016-10-13 09:12:37 -07001851 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001852 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001853 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -08001854 "Found type %s; filled-new-array not implemented for anything but 'int'",
David Sehr709b0702016-10-13 09:12:37 -07001855 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001856 }
1857 return false;
1858 }
Vladimir Marko9b81ac32019-05-16 16:47:08 +01001859 ObjPtr<mirror::Object> new_array = mirror::Array::Alloc(
Mathieu Chartieref41db72016-10-25 15:08:01 -07001860 self,
1861 array_class,
1862 length,
1863 array_class->GetComponentSizeShift(),
1864 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001865 if (UNLIKELY(new_array == nullptr)) {
1866 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001867 return false;
1868 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001869 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
1870 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001871 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +01001872 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001873 } else {
Ian Rogers29a26482014-05-02 15:27:29 -07001874 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +01001875 }
Sebastien Hertzabff6432014-01-27 18:01:39 +01001876 for (int32_t i = 0; i < length; ++i) {
1877 size_t src_reg = is_range ? vregC + i : arg[i];
1878 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001879 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
1880 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +01001881 } else {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001882 new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<transaction_active>(
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001883 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001884 }
1885 }
1886
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001887 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001888 return true;
1889}
1890
Mathieu Chartieref41db72016-10-25 15:08:01 -07001891// TODO: Use ObjPtr here.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001892template<typename T>
Vladimir Marko4617d582019-03-28 13:48:31 +00001893static void RecordArrayElementsInTransactionImpl(ObjPtr<mirror::PrimitiveArray<T>> array,
Mathieu Chartieref41db72016-10-25 15:08:01 -07001894 int32_t count)
1895 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001896 Runtime* runtime = Runtime::Current();
1897 for (int32_t i = 0; i < count; ++i) {
Vladimir Marko4617d582019-03-28 13:48:31 +00001898 runtime->RecordWriteArray(array.Ptr(), i, array->GetWithoutChecks(i));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001899 }
1900}
1901
Mathieu Chartieref41db72016-10-25 15:08:01 -07001902void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001903 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001904 DCHECK(Runtime::Current()->IsActiveTransaction());
1905 DCHECK(array != nullptr);
1906 DCHECK_LE(count, array->GetLength());
1907 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
1908 switch (primitive_component_type) {
1909 case Primitive::kPrimBoolean:
1910 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
1911 break;
1912 case Primitive::kPrimByte:
1913 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
1914 break;
1915 case Primitive::kPrimChar:
1916 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
1917 break;
1918 case Primitive::kPrimShort:
1919 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
1920 break;
1921 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001922 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
1923 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001924 case Primitive::kPrimFloat:
1925 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
1926 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001927 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001928 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
1929 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001930 case Primitive::kPrimDouble:
1931 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
1932 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001933 default:
1934 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
1935 << " in fill-array-data";
Elliott Hughesc1896c92018-11-29 11:33:18 -08001936 UNREACHABLE();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001937 }
1938}
1939
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001940// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +02001941#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001942 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001943 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
1944 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +02001945 const Instruction* inst, uint16_t inst_data, \
1946 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001947EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
1948EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
1949EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
1950EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
1951#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001952
Orion Hodsonc069a302017-01-18 09:23:12 +00001953// Explicit DoInvokePolymorphic template function declarations.
1954#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range) \
1955 template REQUIRES_SHARED(Locks::mutator_lock_) \
1956 bool DoInvokePolymorphic<_is_range>( \
1957 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1958 uint16_t inst_data, JValue* result)
1959EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false);
1960EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true);
Narayan Kamath9823e782016-08-03 12:46:58 +01001961#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1962
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001963// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001964#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001965 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001966 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
1967 const ShadowFrame& shadow_frame, \
1968 Thread* self, JValue* result)
1969#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
1970 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
1971 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
1972 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
1973 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
1974EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
1975EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
1976#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001977#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
1978
1979} // namespace interpreter
1980} // namespace art