blob: d33062da225b9db9c0b1be023ef4526775ec42e1 [file] [log] [blame]
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "interpreter_common.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070018
Andreas Gampef0e128a2015-02-27 20:08:34 -080019#include <cmath>
20
Vladimir Marko78baed52018-10-11 10:44:58 +010021#include "base/casts.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070022#include "base/enums.h"
Vladimir Markoc7aa87e2018-05-24 15:19:52 +010023#include "class_root.h"
Daniel Mihalyieb076692014-08-22 17:33:31 +020024#include "debugger.h"
David Sehr9e734c72018-01-04 17:56:19 -080025#include "dex/dex_file_types.h"
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +000026#include "entrypoints/runtime_asm_entrypoints.h"
Orion Hodson43f0cdb2017-10-10 14:47:32 +010027#include "intrinsics_enum.h"
Tamas Berghammerdd5e5e92016-02-12 16:29:00 +000028#include "jit/jit.h"
Andreas Gampec5b75642018-05-16 15:12:11 -070029#include "jvalue-inl.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010030#include "method_handles-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070031#include "method_handles.h"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070032#include "mirror/array-alloc-inl.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010033#include "mirror/array-inl.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010034#include "mirror/class.h"
Narayan Kamath000e1882016-10-24 17:14:25 +010035#include "mirror/emulated_stack_frame.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080036#include "mirror/method_handle_impl-inl.h"
Vladimir Marko5aead702019-03-27 11:00:36 +000037#include "mirror/method_type-inl.h"
Andreas Gampe52ecb652018-10-24 15:18:21 -070038#include "mirror/object_array-alloc-inl.h"
39#include "mirror/object_array-inl.h"
Orion Hodson928033d2018-02-07 05:30:54 +000040#include "mirror/var_handle.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010041#include "reflection-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070042#include "reflection.h"
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +010043#include "shadow_frame-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070044#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070045#include "thread-inl.h"
Chang Xingbd208d82017-07-12 14:53:17 -070046#include "transaction.h"
Orion Hodson537a4fe2018-05-15 13:57:58 +010047#include "var_handles.h"
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +010048#include "well_known_classes.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020049
50namespace art {
51namespace interpreter {
52
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000053void ThrowNullPointerExceptionFromInterpreter() {
54 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -070055}
56
David Srbecky960327b2018-10-25 10:11:59 +000057bool CheckStackOverflow(Thread* self, size_t frame_size)
58 REQUIRES_SHARED(Locks::mutator_lock_) {
59 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
60 uint8_t* stack_end = self->GetStackEndForInterpreter(implicit_check);
61 if (UNLIKELY(__builtin_frame_address(0) < stack_end + frame_size)) {
62 ThrowStackOverflowError(self);
63 return false;
64 }
65 return true;
66}
67
David Srbecky9581e612018-10-30 14:29:43 +000068bool UseFastInterpreterToInterpreterInvoke(ArtMethod* method) {
69 Runtime* runtime = Runtime::Current();
70 const void* quick_code = method->GetEntryPointFromQuickCompiledCode();
71 if (!runtime->GetClassLinker()->IsQuickToInterpreterBridge(quick_code)) {
72 return false;
73 }
74 if (!method->SkipAccessChecks() || method->IsNative() || method->IsProxyMethod()) {
75 return false;
76 }
77 if (method->IsIntrinsic()) {
78 return false;
79 }
80 if (method->GetDeclaringClass()->IsStringClass() && method->IsConstructor()) {
81 return false;
82 }
83 if (method->IsStatic() && !method->GetDeclaringClass()->IsInitialized()) {
84 return false;
85 }
86 ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
87 if ((profiling_info != nullptr) && (profiling_info->GetSavedEntryPoint() != nullptr)) {
88 return false;
89 }
90 return true;
91}
92
Chang Xingbd208d82017-07-12 14:53:17 -070093template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
94 bool transaction_active>
Ian Rogers54874942014-06-10 16:31:03 -070095bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
96 uint16_t inst_data) {
97 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
98 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010099 ArtField* f =
100 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
101 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -0700102 if (UNLIKELY(f == nullptr)) {
103 CHECK(self->IsExceptionPending());
104 return false;
105 }
Mathieu Chartieref41db72016-10-25 15:08:01 -0700106 ObjPtr<mirror::Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -0700107 if (is_static) {
108 obj = f->GetDeclaringClass();
Chang Xingbd208d82017-07-12 14:53:17 -0700109 if (transaction_active) {
110 if (Runtime::Current()->GetTransaction()->ReadConstraint(obj.Ptr(), f)) {
111 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Can't read static fields of "
112 + obj->PrettyTypeOf() + " since it does not belong to clinit's class.");
113 return false;
114 }
115 }
Ian Rogers54874942014-06-10 16:31:03 -0700116 } else {
117 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
118 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000119 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers54874942014-06-10 16:31:03 -0700120 return false;
121 }
122 }
Orion Hodson3d617ac2016-10-19 14:00:46 +0100123
124 JValue result;
Alex Light084fa372017-06-16 08:58:34 -0700125 if (UNLIKELY(!DoFieldGetCommon<field_type>(self, shadow_frame, obj, f, &result))) {
126 // Instrumentation threw an error!
127 CHECK(self->IsExceptionPending());
128 return false;
129 }
Ian Rogers54874942014-06-10 16:31:03 -0700130 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
131 switch (field_type) {
132 case Primitive::kPrimBoolean:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100133 shadow_frame.SetVReg(vregA, result.GetZ());
Ian Rogers54874942014-06-10 16:31:03 -0700134 break;
135 case Primitive::kPrimByte:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100136 shadow_frame.SetVReg(vregA, result.GetB());
Ian Rogers54874942014-06-10 16:31:03 -0700137 break;
138 case Primitive::kPrimChar:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100139 shadow_frame.SetVReg(vregA, result.GetC());
Ian Rogers54874942014-06-10 16:31:03 -0700140 break;
141 case Primitive::kPrimShort:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100142 shadow_frame.SetVReg(vregA, result.GetS());
Ian Rogers54874942014-06-10 16:31:03 -0700143 break;
144 case Primitive::kPrimInt:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100145 shadow_frame.SetVReg(vregA, result.GetI());
Ian Rogers54874942014-06-10 16:31:03 -0700146 break;
147 case Primitive::kPrimLong:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100148 shadow_frame.SetVRegLong(vregA, result.GetJ());
Ian Rogers54874942014-06-10 16:31:03 -0700149 break;
150 case Primitive::kPrimNot:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100151 shadow_frame.SetVRegReference(vregA, result.GetL());
Ian Rogers54874942014-06-10 16:31:03 -0700152 break;
153 default:
154 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700155 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700156 }
157 return true;
158}
159
160// Explicitly instantiate all DoFieldGet functions.
Chang Xingbd208d82017-07-12 14:53:17 -0700161#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
162 template bool DoFieldGet<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
Ian Rogers54874942014-06-10 16:31:03 -0700163 ShadowFrame& shadow_frame, \
164 const Instruction* inst, \
165 uint16_t inst_data)
166
167#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
Chang Xingbd208d82017-07-12 14:53:17 -0700168 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false, true); \
169 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false, false); \
170 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true, true); \
171 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true, false);
Ian Rogers54874942014-06-10 16:31:03 -0700172
173// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700174EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
175EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
176EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
177EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
178EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
179EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
180EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700181
182// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700183EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
184EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
185EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
186EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
187EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
188EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
189EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700190
191#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
192#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
193
194// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
195// Returns true on success, otherwise throws an exception and returns false.
196template<Primitive::Type field_type>
197bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700198 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700199 if (UNLIKELY(obj == nullptr)) {
200 // We lost the reference to the field index so we cannot get a more
201 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000202 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700203 return false;
204 }
205 MemberOffset field_offset(inst->VRegC_22c());
206 // Report this field access to instrumentation if needed. Since we only have the offset of
207 // the field from the base of the object, we need to look for it first.
208 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
209 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
210 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
211 field_offset.Uint32Value());
212 DCHECK(f != nullptr);
213 DCHECK(!f->IsStatic());
Alex Light084fa372017-06-16 08:58:34 -0700214 Thread* self = Thread::Current();
215 StackHandleScope<1> hs(self);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700216 // Save obj in case the instrumentation event has thread suspension.
217 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Alex Light084fa372017-06-16 08:58:34 -0700218 instrumentation->FieldReadEvent(self,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700219 obj.Ptr(),
220 shadow_frame.GetMethod(),
221 shadow_frame.GetDexPC(),
222 f);
Alex Light084fa372017-06-16 08:58:34 -0700223 if (UNLIKELY(self->IsExceptionPending())) {
224 return false;
225 }
Ian Rogers54874942014-06-10 16:31:03 -0700226 }
227 // Note: iget-x-quick instructions are only for non-volatile fields.
228 const uint32_t vregA = inst->VRegA_22c(inst_data);
229 switch (field_type) {
230 case Primitive::kPrimInt:
231 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
232 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800233 case Primitive::kPrimBoolean:
234 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
235 break;
236 case Primitive::kPrimByte:
237 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
238 break;
239 case Primitive::kPrimChar:
240 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
241 break;
242 case Primitive::kPrimShort:
243 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
244 break;
Ian Rogers54874942014-06-10 16:31:03 -0700245 case Primitive::kPrimLong:
246 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
247 break;
248 case Primitive::kPrimNot:
249 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
250 break;
251 default:
252 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700253 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700254 }
255 return true;
256}
257
258// Explicitly instantiate all DoIGetQuick functions.
259#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
260 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
261 uint16_t inst_data)
262
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800263EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
264EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
265EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
266EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
267EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
268EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
269EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700270#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
271
272template<Primitive::Type field_type>
273static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700274 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700275 JValue field_value;
276 switch (field_type) {
277 case Primitive::kPrimBoolean:
278 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
279 break;
280 case Primitive::kPrimByte:
281 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
282 break;
283 case Primitive::kPrimChar:
284 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
285 break;
286 case Primitive::kPrimShort:
287 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
288 break;
289 case Primitive::kPrimInt:
290 field_value.SetI(shadow_frame.GetVReg(vreg));
291 break;
292 case Primitive::kPrimLong:
293 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
294 break;
295 case Primitive::kPrimNot:
296 field_value.SetL(shadow_frame.GetVRegReference(vreg));
297 break;
298 default:
299 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700300 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700301 }
302 return field_value;
303}
304
Orion Hodson3d617ac2016-10-19 14:00:46 +0100305template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
306 bool transaction_active>
307bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
308 uint16_t inst_data) {
309 const bool do_assignability_check = do_access_check;
310 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
311 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
312 ArtField* f =
313 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
314 Primitive::ComponentSize(field_type));
315 if (UNLIKELY(f == nullptr)) {
316 CHECK(self->IsExceptionPending());
317 return false;
318 }
319 ObjPtr<mirror::Object> obj;
320 if (is_static) {
321 obj = f->GetDeclaringClass();
Chang Xingbd208d82017-07-12 14:53:17 -0700322 if (transaction_active) {
323 if (Runtime::Current()->GetTransaction()->WriteConstraint(obj.Ptr(), f)) {
324 Runtime::Current()->AbortTransactionAndThrowAbortError(
325 self, "Can't set fields of " + obj->PrettyTypeOf());
326 return false;
327 }
328 }
329
Orion Hodson3d617ac2016-10-19 14:00:46 +0100330 } else {
331 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
332 if (UNLIKELY(obj == nullptr)) {
333 ThrowNullPointerExceptionForFieldAccess(f, false);
334 return false;
335 }
336 }
337
338 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100339 JValue value = GetFieldValue<field_type>(shadow_frame, vregA);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100340 return DoFieldPutCommon<field_type, do_assignability_check, transaction_active>(self,
341 shadow_frame,
342 obj,
343 f,
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100344 value);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100345}
346
Ian Rogers54874942014-06-10 16:31:03 -0700347// Explicitly instantiate all DoFieldPut functions.
348#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
349 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
350 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
351
352#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
353 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
354 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
355 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
356 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
357
358// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700359EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
360EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
361EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
362EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
363EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
364EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
365EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700366
367// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700368EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
369EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
370EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
371EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
372EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
373EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
374EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700375
376#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
377#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
378
379template<Primitive::Type field_type, bool transaction_active>
380bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700381 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700382 if (UNLIKELY(obj == nullptr)) {
383 // We lost the reference to the field index so we cannot get a more
384 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000385 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700386 return false;
387 }
388 MemberOffset field_offset(inst->VRegC_22c());
389 const uint32_t vregA = inst->VRegA_22c(inst_data);
390 // Report this field modification to instrumentation if needed. Since we only have the offset of
391 // the field from the base of the object, we need to look for it first.
392 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
393 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
394 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
395 field_offset.Uint32Value());
396 DCHECK(f != nullptr);
397 DCHECK(!f->IsStatic());
398 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Alex Light084fa372017-06-16 08:58:34 -0700399 Thread* self = Thread::Current();
400 StackHandleScope<2> hs(self);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700401 // Save obj in case the instrumentation event has thread suspension.
402 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Alex Light084fa372017-06-16 08:58:34 -0700403 mirror::Object* fake_root = nullptr;
404 HandleWrapper<mirror::Object> ret(hs.NewHandleWrapper<mirror::Object>(
405 field_type == Primitive::kPrimNot ? field_value.GetGCRoot() : &fake_root));
406 instrumentation->FieldWriteEvent(self,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700407 obj.Ptr(),
408 shadow_frame.GetMethod(),
409 shadow_frame.GetDexPC(),
410 f,
411 field_value);
Alex Light084fa372017-06-16 08:58:34 -0700412 if (UNLIKELY(self->IsExceptionPending())) {
413 return false;
414 }
Alex Light0aa7a5a2018-10-10 15:58:14 +0000415 if (UNLIKELY(shadow_frame.GetForcePopFrame())) {
416 // Don't actually set the field. The next instruction will force us to pop.
417 DCHECK(Runtime::Current()->AreNonStandardExitsEnabled());
418 DCHECK(PrevFrameWillRetry(self, shadow_frame));
419 return true;
420 }
Ian Rogers54874942014-06-10 16:31:03 -0700421 }
422 // Note: iput-x-quick instructions are only for non-volatile fields.
423 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700424 case Primitive::kPrimBoolean:
425 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
426 break;
427 case Primitive::kPrimByte:
428 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
429 break;
430 case Primitive::kPrimChar:
431 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
432 break;
433 case Primitive::kPrimShort:
434 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
435 break;
Ian Rogers54874942014-06-10 16:31:03 -0700436 case Primitive::kPrimInt:
437 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
438 break;
439 case Primitive::kPrimLong:
440 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
441 break;
442 case Primitive::kPrimNot:
443 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
444 break;
445 default:
446 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700447 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700448 }
449 return true;
450}
451
452// Explicitly instantiate all DoIPutQuick functions.
453#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
454 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
455 const Instruction* inst, \
456 uint16_t inst_data)
457
458#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
459 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
460 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
461
Andreas Gampec8ccf682014-09-29 20:07:43 -0700462EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
463EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
464EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
465EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
466EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
467EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
468EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700469#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
470#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
471
Alex Light9fb1ab12017-09-05 09:32:49 -0700472// We execute any instrumentation events that are triggered by this exception and change the
473// shadow_frame's dex_pc to that of the exception handler if there is one in the current method.
474// Return true if we should continue executing in the current method and false if we need to go up
475// the stack to find an exception handler.
Sebastien Hertz520633b2015-09-08 17:03:36 +0200476// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Alex Light9fb1ab12017-09-05 09:32:49 -0700477// TODO We should have a better way to skip instrumentation reporting or possibly rethink that
478// behavior.
479bool MoveToExceptionHandler(Thread* self,
480 ShadowFrame& shadow_frame,
481 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700482 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700483 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000484 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Alex Light9fb1ab12017-09-05 09:32:49 -0700485 if (instrumentation != nullptr &&
486 instrumentation->HasExceptionThrownListeners() &&
487 self->IsExceptionThrownByCurrentMethod(exception.Get())) {
488 // 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 -0700489 instrumentation->ExceptionThrownEvent(self, exception.Get());
Alex Light0aa7a5a2018-10-10 15:58:14 +0000490 if (shadow_frame.GetForcePopFrame()) {
491 // We will check in the caller for GetForcePopFrame again. We need to bail out early to
492 // prevent an ExceptionHandledEvent from also being sent before popping.
493 return true;
494 }
Sebastien Hertz9f102032014-05-23 08:59:42 +0200495 }
Ian Rogers54874942014-06-10 16:31:03 -0700496 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700497 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
Alex Light9fb1ab12017-09-05 09:32:49 -0700498 hs.NewHandle(exception->GetClass()), shadow_frame.GetDexPC(), &clear_exception);
Andreas Gampee2abbc62017-09-15 11:59:26 -0700499 if (found_dex_pc == dex::kDexNoIndex) {
Alex Light9fb1ab12017-09-05 09:32:49 -0700500 if (instrumentation != nullptr) {
501 if (shadow_frame.NeedsNotifyPop()) {
502 instrumentation->WatchedFramePopped(self, shadow_frame);
503 }
504 // Exception is not caught by the current method. We will unwind to the
505 // caller. Notify any instrumentation listener.
506 instrumentation->MethodUnwindEvent(self,
507 shadow_frame.GetThisObject(),
508 shadow_frame.GetMethod(),
509 shadow_frame.GetDexPC());
Alex Lighte814f9d2017-07-31 16:14:39 -0700510 }
Alex Light9fb1ab12017-09-05 09:32:49 -0700511 return false;
Ian Rogers54874942014-06-10 16:31:03 -0700512 } else {
Alex Light9fb1ab12017-09-05 09:32:49 -0700513 shadow_frame.SetDexPC(found_dex_pc);
514 if (instrumentation != nullptr && instrumentation->HasExceptionHandledListeners()) {
515 self->ClearException();
516 instrumentation->ExceptionHandledEvent(self, exception.Get());
517 if (UNLIKELY(self->IsExceptionPending())) {
518 // Exception handled event threw an exception. Try to find the handler for this one.
519 return MoveToExceptionHandler(self, shadow_frame, instrumentation);
520 } else if (!clear_exception) {
521 self->SetException(exception.Get());
522 }
523 } else if (clear_exception) {
Ian Rogers54874942014-06-10 16:31:03 -0700524 self->ClearException();
525 }
Alex Light9fb1ab12017-09-05 09:32:49 -0700526 return true;
Ian Rogers54874942014-06-10 16:31:03 -0700527 }
Ian Rogers54874942014-06-10 16:31:03 -0700528}
529
Ian Rogerse94652f2014-12-02 11:13:19 -0800530void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
531 LOG(FATAL) << "Unexpected instruction: "
532 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
533 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700534}
535
Sebastien Hertz45b15972015-04-03 16:07:05 +0200536void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700537 va_list args;
538 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200539 AbortTransactionV(self, fmt, args);
540 va_end(args);
541}
542
543void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
544 CHECK(Runtime::Current()->IsActiveTransaction());
545 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100546 std::string abort_msg;
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800547 android::base::StringAppendV(&abort_msg, fmt, args);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100548 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200549 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700550}
551
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100552// START DECLARATIONS :
553//
554// These additional declarations are required because clang complains
555// about ALWAYS_INLINE (-Werror, -Wgcc-compat) in definitions.
556//
557
Narayan Kamath9823e782016-08-03 12:46:58 +0100558template <bool is_range, bool do_assignability_check>
Vladimir Markod16363a2017-02-01 14:09:13 +0000559static ALWAYS_INLINE bool DoCallCommon(ArtMethod* called_method,
560 Thread* self,
561 ShadowFrame& shadow_frame,
562 JValue* result,
563 uint16_t number_of_inputs,
564 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
565 uint32_t vregC) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100566
567template <bool is_range>
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000568ALWAYS_INLINE void CopyRegisters(ShadowFrame& caller_frame,
569 ShadowFrame* callee_frame,
570 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
571 const size_t first_src_reg,
572 const size_t first_dest_reg,
573 const size_t num_regs) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100574
575// END DECLARATIONS.
576
Siva Chandra05d24152016-01-05 17:43:17 -0800577void ArtInterpreterToCompiledCodeBridge(Thread* self,
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100578 ArtMethod* caller,
Siva Chandra05d24152016-01-05 17:43:17 -0800579 ShadowFrame* shadow_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -0700580 uint16_t arg_offset,
Siva Chandra05d24152016-01-05 17:43:17 -0800581 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700582 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700583 ArtMethod* method = shadow_frame->GetMethod();
584 // Ensure static methods are initialized.
585 if (method->IsStatic()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700586 ObjPtr<mirror::Class> declaringClass = method->GetDeclaringClass();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700587 if (UNLIKELY(!declaringClass->IsInitialized())) {
588 self->PushShadowFrame(shadow_frame);
589 StackHandleScope<1> hs(self);
590 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
Nicolas Geoffray01822292017-03-09 09:03:19 +0000591 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
592 true))) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700593 self->PopShadowFrame();
594 DCHECK(self->IsExceptionPending());
595 return;
596 }
597 self->PopShadowFrame();
598 CHECK(h_class->IsInitializing());
Nicolas Geoffray01822292017-03-09 09:03:19 +0000599 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
600 method = shadow_frame->GetMethod();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700601 }
602 }
Jeff Hao5ea84132017-05-05 16:59:29 -0700603 // Basic checks for the arg_offset. If there's no code item, the arg_offset must be 0. Otherwise,
604 // check that the arg_offset isn't greater than the number of registers. A stronger check is
605 // difficult since the frame may contain space for all the registers in the method, or only enough
606 // space for the arguments.
607 if (kIsDebugBuild) {
608 if (method->GetCodeItem() == nullptr) {
609 DCHECK_EQ(0u, arg_offset) << method->PrettyMethod();
610 } else {
611 DCHECK_LE(arg_offset, shadow_frame->NumberOfVRegs());
612 }
613 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100614 jit::Jit* jit = Runtime::Current()->GetJit();
615 if (jit != nullptr && caller != nullptr) {
616 jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
617 }
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700618 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
619 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
Andreas Gampe542451c2016-07-26 09:02:02 -0700620 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700621}
622
Mingyao Yangffedec52016-05-19 10:48:40 -0700623void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
624 uint16_t this_obj_vreg,
625 JValue result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700626 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700627 ObjPtr<mirror::Object> existing = shadow_frame->GetVRegReference(this_obj_vreg);
Mingyao Yangffedec52016-05-19 10:48:40 -0700628 if (existing == nullptr) {
629 // If it's null, we come from compiled code that was deoptimized. Nothing to do,
630 // as the compiler verified there was no alias.
631 // Set the new string result of the StringFactory.
632 shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
633 return;
634 }
635 // Set the string init result into all aliases.
636 for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
637 if (shadow_frame->GetVRegReference(i) == existing) {
638 DCHECK_EQ(shadow_frame->GetVRegReference(i),
Vladimir Marko78baed52018-10-11 10:44:58 +0100639 reinterpret_cast32<mirror::Object*>(shadow_frame->GetVReg(i)));
Mingyao Yangffedec52016-05-19 10:48:40 -0700640 shadow_frame->SetVRegReference(i, result.GetL());
641 DCHECK_EQ(shadow_frame->GetVRegReference(i),
Vladimir Marko78baed52018-10-11 10:44:58 +0100642 reinterpret_cast32<mirror::Object*>(shadow_frame->GetVReg(i)));
Mingyao Yangffedec52016-05-19 10:48:40 -0700643 }
644 }
645}
646
Orion Hodsonc069a302017-01-18 09:23:12 +0000647template<bool is_range>
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100648static bool DoMethodHandleInvokeCommon(Thread* self,
649 ShadowFrame& shadow_frame,
650 bool invoke_exact,
651 const Instruction* inst,
652 uint16_t inst_data,
653 JValue* result)
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100654 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Light848574c2017-09-25 16:59:39 -0700655 // Make sure to check for async exceptions
656 if (UNLIKELY(self->ObserveAsyncException())) {
657 return false;
658 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100659 // Invoke-polymorphic instructions always take a receiver. i.e, they are never static.
660 const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc();
Orion Hodson3d617ac2016-10-19 14:00:46 +0100661 const int invoke_method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc();
Narayan Kamath9823e782016-08-03 12:46:58 +0100662
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000663 // Initialize |result| to 0 as this is the default return value for
664 // polymorphic invocations of method handle types with void return
665 // and provides sane return result in error cases.
666 result->SetJ(0);
667
Orion Hodson3d617ac2016-10-19 14:00:46 +0100668 // The invoke_method_idx here is the name of the signature polymorphic method that
Narayan Kamath9823e782016-08-03 12:46:58 +0100669 // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact)
670 // and not the method that we'll dispatch to in the end.
Orion Hodsone7732be2017-10-11 14:35:20 +0100671 StackHandleScope<2> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +0000672 Handle<mirror::MethodHandle> method_handle(hs.NewHandle(
673 ObjPtr<mirror::MethodHandle>::DownCast(
Mathieu Chartieref41db72016-10-25 15:08:01 -0700674 MakeObjPtr(shadow_frame.GetVRegReference(vRegC)))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800675 if (UNLIKELY(method_handle == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100676 // Note that the invoke type is kVirtual here because a call to a signature
677 // polymorphic method is shaped like a virtual call at the bytecode level.
Orion Hodson3d617ac2016-10-19 14:00:46 +0100678 ThrowNullPointerExceptionForMethodAccess(invoke_method_idx, InvokeType::kVirtual);
Narayan Kamath9823e782016-08-03 12:46:58 +0100679 return false;
680 }
681
682 // The vRegH value gives the index of the proto_id associated with this
Orion Hodson811bd5f2016-12-07 11:35:37 +0000683 // signature polymorphic call site.
Orion Hodson06d10a72018-05-14 08:53:38 +0100684 const uint16_t vRegH = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
685 const dex::ProtoIndex callsite_proto_id(vRegH);
Narayan Kamath9823e782016-08-03 12:46:58 +0100686
687 // Call through to the classlinker and ask it to resolve the static type associated
688 // with the callsite. This information is stored in the dex cache so it's
689 // guaranteed to be fast after the first resolution.
Narayan Kamath9823e782016-08-03 12:46:58 +0100690 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Orion Hodsone7732be2017-10-11 14:35:20 +0100691 Handle<mirror::MethodType> callsite_type(hs.NewHandle(
692 class_linker->ResolveMethodType(self, callsite_proto_id, shadow_frame.GetMethod())));
Narayan Kamath9823e782016-08-03 12:46:58 +0100693
694 // This implies we couldn't resolve one or more types in this method handle.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800695 if (UNLIKELY(callsite_type == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100696 CHECK(self->IsExceptionPending());
Narayan Kamath9823e782016-08-03 12:46:58 +0100697 return false;
698 }
699
Orion Hodson811bd5f2016-12-07 11:35:37 +0000700 // There is a common dispatch method for method handles that takes
701 // arguments either from a range or an array of arguments depending
702 // on whether the DEX instruction is invoke-polymorphic/range or
703 // invoke-polymorphic. The array here is for the latter.
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100704 if (UNLIKELY(is_range)) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000705 // VRegC is the register holding the method handle. Arguments passed
706 // to the method handle's target do not include the method handle.
Orion Hodson960d4f72017-11-10 15:32:38 +0000707 RangeInstructionOperands operands(inst->VRegC_4rcc() + 1, inst->VRegA_4rcc() - 1);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100708 if (invoke_exact) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000709 return MethodHandleInvokeExact(self,
710 shadow_frame,
711 method_handle,
712 callsite_type,
713 &operands,
714 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100715 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000716 return MethodHandleInvoke(self,
717 shadow_frame,
718 method_handle,
719 callsite_type,
720 &operands,
721 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100722 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100723 } else {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000724 // Get the register arguments for the invoke.
Orion Hodson960d4f72017-11-10 15:32:38 +0000725 uint32_t args[Instruction::kMaxVarArgRegs] = {};
Orion Hodson811bd5f2016-12-07 11:35:37 +0000726 inst->GetVarArgs(args, inst_data);
727 // Drop the first register which is the method handle performing the invoke.
Alexey Frunze8631a462017-01-19 19:07:37 -0800728 memmove(args, args + 1, sizeof(args[0]) * (Instruction::kMaxVarArgRegs - 1));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000729 args[Instruction::kMaxVarArgRegs - 1] = 0;
Orion Hodson960d4f72017-11-10 15:32:38 +0000730 VarArgsInstructionOperands operands(args, inst->VRegA_45cc() - 1);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100731 if (invoke_exact) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000732 return MethodHandleInvokeExact(self,
733 shadow_frame,
734 method_handle,
735 callsite_type,
736 &operands,
737 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100738 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000739 return MethodHandleInvoke(self,
740 shadow_frame,
741 method_handle,
742 callsite_type,
743 &operands,
744 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100745 }
746 }
747}
748
749bool DoMethodHandleInvokeExact(Thread* self,
750 ShadowFrame& shadow_frame,
751 const Instruction* inst,
752 uint16_t inst_data,
753 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
754 if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) {
755 static const bool kIsRange = false;
756 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700757 self, shadow_frame, /* invoke_exact= */ true, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100758 } else {
759 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE);
760 static const bool kIsRange = true;
761 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700762 self, shadow_frame, /* invoke_exact= */ true, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100763 }
764}
765
766bool DoMethodHandleInvoke(Thread* self,
767 ShadowFrame& shadow_frame,
768 const Instruction* inst,
769 uint16_t inst_data,
770 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
771 if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) {
772 static const bool kIsRange = false;
773 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700774 self, shadow_frame, /* invoke_exact= */ false, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100775 } else {
776 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE);
777 static const bool kIsRange = true;
778 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700779 self, shadow_frame, /* invoke_exact= */ false, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100780 }
781}
782
Orion Hodson928033d2018-02-07 05:30:54 +0000783static bool DoVarHandleInvokeCommon(Thread* self,
784 ShadowFrame& shadow_frame,
785 const Instruction* inst,
786 uint16_t inst_data,
787 JValue* result,
788 mirror::VarHandle::AccessMode access_mode)
789 REQUIRES_SHARED(Locks::mutator_lock_) {
790 // Make sure to check for async exceptions
791 if (UNLIKELY(self->ObserveAsyncException())) {
792 return false;
793 }
794
Orion Hodson928033d2018-02-07 05:30:54 +0000795 StackHandleScope<2> hs(self);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100796 bool is_var_args = inst->HasVarArgs();
Orion Hodson06d10a72018-05-14 08:53:38 +0100797 const uint16_t vRegH = is_var_args ? inst->VRegH_45cc() : inst->VRegH_4rcc();
Orion Hodson928033d2018-02-07 05:30:54 +0000798 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
799 Handle<mirror::MethodType> callsite_type(hs.NewHandle(
Orion Hodson06d10a72018-05-14 08:53:38 +0100800 class_linker->ResolveMethodType(self, dex::ProtoIndex(vRegH), shadow_frame.GetMethod())));
Orion Hodson928033d2018-02-07 05:30:54 +0000801 // This implies we couldn't resolve one or more types in this VarHandle.
802 if (UNLIKELY(callsite_type == nullptr)) {
803 CHECK(self->IsExceptionPending());
804 return false;
805 }
806
Orion Hodson537a4fe2018-05-15 13:57:58 +0100807 const uint32_t vRegC = is_var_args ? inst->VRegC_45cc() : inst->VRegC_4rcc();
808 ObjPtr<mirror::Object> receiver(shadow_frame.GetVRegReference(vRegC));
Vladimir Marko179b7c62019-03-22 13:38:57 +0000809 Handle<mirror::VarHandle> var_handle(hs.NewHandle(ObjPtr<mirror::VarHandle>::DownCast(receiver)));
Orion Hodson928033d2018-02-07 05:30:54 +0000810 if (is_var_args) {
811 uint32_t args[Instruction::kMaxVarArgRegs];
812 inst->GetVarArgs(args, inst_data);
813 VarArgsInstructionOperands all_operands(args, inst->VRegA_45cc());
814 NoReceiverInstructionOperands operands(&all_operands);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100815 return VarHandleInvokeAccessor(self,
816 shadow_frame,
817 var_handle,
818 callsite_type,
819 access_mode,
820 &operands,
821 result);
Orion Hodson928033d2018-02-07 05:30:54 +0000822 } else {
823 RangeInstructionOperands all_operands(inst->VRegC_4rcc(), inst->VRegA_4rcc());
824 NoReceiverInstructionOperands operands(&all_operands);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100825 return VarHandleInvokeAccessor(self,
826 shadow_frame,
827 var_handle,
828 callsite_type,
829 access_mode,
830 &operands,
831 result);
Orion Hodson928033d2018-02-07 05:30:54 +0000832 }
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100833}
834
Orion Hodson928033d2018-02-07 05:30:54 +0000835#define DO_VAR_HANDLE_ACCESSOR(_access_mode) \
836bool DoVarHandle ## _access_mode(Thread* self, \
837 ShadowFrame& shadow_frame, \
838 const Instruction* inst, \
839 uint16_t inst_data, \
840 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { \
841 const auto access_mode = mirror::VarHandle::AccessMode::k ## _access_mode; \
842 return DoVarHandleInvokeCommon(self, shadow_frame, inst, inst_data, result, access_mode); \
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100843}
844
Orion Hodson928033d2018-02-07 05:30:54 +0000845DO_VAR_HANDLE_ACCESSOR(CompareAndExchange)
846DO_VAR_HANDLE_ACCESSOR(CompareAndExchangeAcquire)
847DO_VAR_HANDLE_ACCESSOR(CompareAndExchangeRelease)
848DO_VAR_HANDLE_ACCESSOR(CompareAndSet)
849DO_VAR_HANDLE_ACCESSOR(Get)
850DO_VAR_HANDLE_ACCESSOR(GetAcquire)
851DO_VAR_HANDLE_ACCESSOR(GetAndAdd)
852DO_VAR_HANDLE_ACCESSOR(GetAndAddAcquire)
853DO_VAR_HANDLE_ACCESSOR(GetAndAddRelease)
854DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAnd)
855DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAndAcquire)
856DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAndRelease)
857DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOr)
858DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOrAcquire)
859DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOrRelease)
860DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXor)
861DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXorAcquire)
862DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXorRelease)
863DO_VAR_HANDLE_ACCESSOR(GetAndSet)
864DO_VAR_HANDLE_ACCESSOR(GetAndSetAcquire)
865DO_VAR_HANDLE_ACCESSOR(GetAndSetRelease)
866DO_VAR_HANDLE_ACCESSOR(GetOpaque)
867DO_VAR_HANDLE_ACCESSOR(GetVolatile)
868DO_VAR_HANDLE_ACCESSOR(Set)
869DO_VAR_HANDLE_ACCESSOR(SetOpaque)
870DO_VAR_HANDLE_ACCESSOR(SetRelease)
871DO_VAR_HANDLE_ACCESSOR(SetVolatile)
872DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSet)
873DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetAcquire)
874DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetPlain)
875DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetRelease)
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100876
Orion Hodson928033d2018-02-07 05:30:54 +0000877#undef DO_VAR_HANDLE_ACCESSOR
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100878
879template<bool is_range>
880bool DoInvokePolymorphic(Thread* self,
881 ShadowFrame& shadow_frame,
882 const Instruction* inst,
883 uint16_t inst_data,
884 JValue* result) {
885 const int invoke_method_idx = inst->VRegB();
886 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
887 ArtMethod* invoke_method =
888 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
889 self, invoke_method_idx, shadow_frame.GetMethod(), kVirtual);
890
891 // Ensure intrinsic identifiers are initialized.
892 DCHECK(invoke_method->IsIntrinsic());
893
894 // Dispatch based on intrinsic identifier associated with method.
895 switch (static_cast<art::Intrinsics>(invoke_method->GetIntrinsic())) {
896#define CASE_SIGNATURE_POLYMORPHIC_INTRINSIC(Name, ...) \
897 case Intrinsics::k##Name: \
898 return Do ## Name(self, shadow_frame, inst, inst_data, result);
899#include "intrinsics_list.h"
900 SIGNATURE_POLYMORPHIC_INTRINSICS_LIST(CASE_SIGNATURE_POLYMORPHIC_INTRINSIC)
901#undef INTRINSICS_LIST
902#undef SIGNATURE_POLYMORPHIC_INTRINSICS_LIST
903#undef CASE_SIGNATURE_POLYMORPHIC_INTRINSIC
904 default:
905 LOG(FATAL) << "Unreachable: " << invoke_method->GetIntrinsic();
906 UNREACHABLE();
907 return false;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100908 }
909}
910
Orion Hodsona5dca522018-02-27 12:42:11 +0000911static JValue ConvertScalarBootstrapArgument(jvalue value) {
912 // value either contains a primitive scalar value if it corresponds
913 // to a primitive type, or it contains an integer value if it
914 // corresponds to an object instance reference id (e.g. a string id).
915 return JValue::FromPrimitive(value.j);
916}
917
918static ObjPtr<mirror::Class> GetClassForBootstrapArgument(EncodedArrayValueIterator::ValueType type)
919 REQUIRES_SHARED(Locks::mutator_lock_) {
920 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100921 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = class_linker->GetClassRoots();
Orion Hodsona5dca522018-02-27 12:42:11 +0000922 switch (type) {
923 case EncodedArrayValueIterator::ValueType::kBoolean:
924 case EncodedArrayValueIterator::ValueType::kByte:
925 case EncodedArrayValueIterator::ValueType::kChar:
926 case EncodedArrayValueIterator::ValueType::kShort:
927 // These types are disallowed by JVMS. Treat as integers. This
928 // will result in CCE's being raised if the BSM has one of these
929 // types.
930 case EncodedArrayValueIterator::ValueType::kInt:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100931 return GetClassRoot(ClassRoot::kPrimitiveInt, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000932 case EncodedArrayValueIterator::ValueType::kLong:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100933 return GetClassRoot(ClassRoot::kPrimitiveLong, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000934 case EncodedArrayValueIterator::ValueType::kFloat:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100935 return GetClassRoot(ClassRoot::kPrimitiveFloat, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000936 case EncodedArrayValueIterator::ValueType::kDouble:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100937 return GetClassRoot(ClassRoot::kPrimitiveDouble, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000938 case EncodedArrayValueIterator::ValueType::kMethodType:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100939 return GetClassRoot<mirror::MethodType>(class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000940 case EncodedArrayValueIterator::ValueType::kMethodHandle:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100941 return GetClassRoot<mirror::MethodHandle>(class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000942 case EncodedArrayValueIterator::ValueType::kString:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100943 return GetClassRoot<mirror::String>();
Orion Hodsona5dca522018-02-27 12:42:11 +0000944 case EncodedArrayValueIterator::ValueType::kType:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100945 return GetClassRoot<mirror::Class>();
Orion Hodsona5dca522018-02-27 12:42:11 +0000946 case EncodedArrayValueIterator::ValueType::kField:
947 case EncodedArrayValueIterator::ValueType::kMethod:
948 case EncodedArrayValueIterator::ValueType::kEnum:
949 case EncodedArrayValueIterator::ValueType::kArray:
950 case EncodedArrayValueIterator::ValueType::kAnnotation:
951 case EncodedArrayValueIterator::ValueType::kNull:
952 return nullptr;
953 }
954}
955
956static bool GetArgumentForBootstrapMethod(Thread* self,
957 ArtMethod* referrer,
958 EncodedArrayValueIterator::ValueType type,
959 const JValue* encoded_value,
960 JValue* decoded_value)
961 REQUIRES_SHARED(Locks::mutator_lock_) {
962 // The encoded_value contains either a scalar value (IJDF) or a
963 // scalar DEX file index to a reference type to be materialized.
964 switch (type) {
965 case EncodedArrayValueIterator::ValueType::kInt:
966 case EncodedArrayValueIterator::ValueType::kFloat:
967 decoded_value->SetI(encoded_value->GetI());
968 return true;
969 case EncodedArrayValueIterator::ValueType::kLong:
970 case EncodedArrayValueIterator::ValueType::kDouble:
971 decoded_value->SetJ(encoded_value->GetJ());
972 return true;
973 case EncodedArrayValueIterator::ValueType::kMethodType: {
974 StackHandleScope<2> hs(self);
975 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader()));
976 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
Orion Hodson06d10a72018-05-14 08:53:38 +0100977 dex::ProtoIndex proto_idx(encoded_value->GetC());
Orion Hodsona5dca522018-02-27 12:42:11 +0000978 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Orion Hodson06d10a72018-05-14 08:53:38 +0100979 ObjPtr<mirror::MethodType> o =
980 cl->ResolveMethodType(self, proto_idx, dex_cache, class_loader);
Orion Hodsona5dca522018-02-27 12:42:11 +0000981 if (UNLIKELY(o.IsNull())) {
982 DCHECK(self->IsExceptionPending());
983 return false;
984 }
985 decoded_value->SetL(o);
986 return true;
987 }
988 case EncodedArrayValueIterator::ValueType::kMethodHandle: {
989 uint32_t index = static_cast<uint32_t>(encoded_value->GetI());
990 ClassLinker* cl = Runtime::Current()->GetClassLinker();
991 ObjPtr<mirror::MethodHandle> o = cl->ResolveMethodHandle(self, index, referrer);
992 if (UNLIKELY(o.IsNull())) {
993 DCHECK(self->IsExceptionPending());
994 return false;
995 }
996 decoded_value->SetL(o);
997 return true;
998 }
999 case EncodedArrayValueIterator::ValueType::kString: {
Orion Hodsona5dca522018-02-27 12:42:11 +00001000 dex::StringIndex index(static_cast<uint32_t>(encoded_value->GetI()));
1001 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Vladimir Marko18090d12018-06-01 16:53:12 +01001002 ObjPtr<mirror::String> o = cl->ResolveString(index, referrer);
Orion Hodsona5dca522018-02-27 12:42:11 +00001003 if (UNLIKELY(o.IsNull())) {
1004 DCHECK(self->IsExceptionPending());
1005 return false;
1006 }
1007 decoded_value->SetL(o);
1008 return true;
1009 }
1010 case EncodedArrayValueIterator::ValueType::kType: {
Orion Hodsona5dca522018-02-27 12:42:11 +00001011 dex::TypeIndex index(static_cast<uint32_t>(encoded_value->GetI()));
1012 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Vladimir Marko09c5ca42018-05-31 15:15:31 +01001013 ObjPtr<mirror::Class> o = cl->ResolveType(index, referrer);
Orion Hodsona5dca522018-02-27 12:42:11 +00001014 if (UNLIKELY(o.IsNull())) {
1015 DCHECK(self->IsExceptionPending());
1016 return false;
1017 }
1018 decoded_value->SetL(o);
1019 return true;
1020 }
1021 case EncodedArrayValueIterator::ValueType::kBoolean:
1022 case EncodedArrayValueIterator::ValueType::kByte:
1023 case EncodedArrayValueIterator::ValueType::kChar:
1024 case EncodedArrayValueIterator::ValueType::kShort:
1025 case EncodedArrayValueIterator::ValueType::kField:
1026 case EncodedArrayValueIterator::ValueType::kMethod:
1027 case EncodedArrayValueIterator::ValueType::kEnum:
1028 case EncodedArrayValueIterator::ValueType::kArray:
1029 case EncodedArrayValueIterator::ValueType::kAnnotation:
1030 case EncodedArrayValueIterator::ValueType::kNull:
1031 // Unreachable - unsupported types that have been checked when
1032 // determining the effect call site type based on the bootstrap
1033 // argument types.
1034 UNREACHABLE();
1035 }
1036}
1037
1038static bool PackArgumentForBootstrapMethod(Thread* self,
1039 ArtMethod* referrer,
1040 CallSiteArrayValueIterator* it,
1041 ShadowFrameSetter* setter)
1042 REQUIRES_SHARED(Locks::mutator_lock_) {
1043 auto type = it->GetValueType();
1044 const JValue encoded_value = ConvertScalarBootstrapArgument(it->GetJavaValue());
1045 JValue decoded_value;
1046 if (!GetArgumentForBootstrapMethod(self, referrer, type, &encoded_value, &decoded_value)) {
1047 return false;
1048 }
1049 switch (it->GetValueType()) {
1050 case EncodedArrayValueIterator::ValueType::kInt:
1051 case EncodedArrayValueIterator::ValueType::kFloat:
1052 setter->Set(static_cast<uint32_t>(decoded_value.GetI()));
1053 return true;
1054 case EncodedArrayValueIterator::ValueType::kLong:
1055 case EncodedArrayValueIterator::ValueType::kDouble:
1056 setter->SetLong(decoded_value.GetJ());
1057 return true;
1058 case EncodedArrayValueIterator::ValueType::kMethodType:
1059 case EncodedArrayValueIterator::ValueType::kMethodHandle:
1060 case EncodedArrayValueIterator::ValueType::kString:
1061 case EncodedArrayValueIterator::ValueType::kType:
1062 setter->SetReference(decoded_value.GetL());
1063 return true;
1064 case EncodedArrayValueIterator::ValueType::kBoolean:
1065 case EncodedArrayValueIterator::ValueType::kByte:
1066 case EncodedArrayValueIterator::ValueType::kChar:
1067 case EncodedArrayValueIterator::ValueType::kShort:
1068 case EncodedArrayValueIterator::ValueType::kField:
1069 case EncodedArrayValueIterator::ValueType::kMethod:
1070 case EncodedArrayValueIterator::ValueType::kEnum:
1071 case EncodedArrayValueIterator::ValueType::kArray:
1072 case EncodedArrayValueIterator::ValueType::kAnnotation:
1073 case EncodedArrayValueIterator::ValueType::kNull:
1074 // Unreachable - unsupported types that have been checked when
1075 // determining the effect call site type based on the bootstrap
1076 // argument types.
1077 UNREACHABLE();
1078 }
1079}
1080
1081static bool PackCollectorArrayForBootstrapMethod(Thread* self,
1082 ArtMethod* referrer,
1083 ObjPtr<mirror::Class> array_type,
1084 int32_t array_length,
1085 CallSiteArrayValueIterator* it,
1086 ShadowFrameSetter* setter)
1087 REQUIRES_SHARED(Locks::mutator_lock_) {
1088 StackHandleScope<1> hs(self);
1089 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1090 JValue decoded_value;
1091
1092#define COLLECT_PRIMITIVE_ARRAY(Descriptor, Type) \
1093 Handle<mirror::Type ## Array> array = \
1094 hs.NewHandle(mirror::Type ## Array::Alloc(self, array_length)); \
1095 if (array.IsNull()) { \
1096 return false; \
1097 } \
1098 for (int32_t i = 0; it->HasNext(); it->Next(), ++i) { \
1099 auto type = it->GetValueType(); \
1100 DCHECK_EQ(type, EncodedArrayValueIterator::ValueType::k ## Type); \
1101 const JValue encoded_value = \
1102 ConvertScalarBootstrapArgument(it->GetJavaValue()); \
1103 GetArgumentForBootstrapMethod(self, \
1104 referrer, \
1105 type, \
1106 &encoded_value, \
1107 &decoded_value); \
1108 array->Set(i, decoded_value.Get ## Descriptor()); \
1109 } \
1110 setter->SetReference(array.Get()); \
1111 return true;
1112
1113#define COLLECT_REFERENCE_ARRAY(T, Type) \
Andreas Gampe584771b2018-10-18 13:22:23 -07001114 Handle<mirror::ObjectArray<T>> array = /* NOLINT */ \
Orion Hodsona5dca522018-02-27 12:42:11 +00001115 hs.NewHandle(mirror::ObjectArray<T>::Alloc(self, \
1116 array_type, \
1117 array_length)); \
1118 if (array.IsNull()) { \
1119 return false; \
1120 } \
1121 for (int32_t i = 0; it->HasNext(); it->Next(), ++i) { \
1122 auto type = it->GetValueType(); \
1123 DCHECK_EQ(type, EncodedArrayValueIterator::ValueType::k ## Type); \
1124 const JValue encoded_value = \
1125 ConvertScalarBootstrapArgument(it->GetJavaValue()); \
1126 if (!GetArgumentForBootstrapMethod(self, \
1127 referrer, \
1128 type, \
1129 &encoded_value, \
1130 &decoded_value)) { \
1131 return false; \
1132 } \
1133 ObjPtr<mirror::Object> o = decoded_value.GetL(); \
1134 if (Runtime::Current()->IsActiveTransaction()) { \
1135 array->Set<true>(i, ObjPtr<T>::DownCast(o)); \
1136 } else { \
1137 array->Set<false>(i, ObjPtr<T>::DownCast(o)); \
1138 } \
1139 } \
1140 setter->SetReference(array.Get()); \
1141 return true;
1142
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001143 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = class_linker->GetClassRoots();
1144 ObjPtr<mirror::Class> component_type = array_type->GetComponentType();
1145 if (component_type == GetClassRoot(ClassRoot::kPrimitiveInt, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001146 COLLECT_PRIMITIVE_ARRAY(I, Int);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001147 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveLong, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001148 COLLECT_PRIMITIVE_ARRAY(J, Long);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001149 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveFloat, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001150 COLLECT_PRIMITIVE_ARRAY(F, Float);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001151 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveDouble, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001152 COLLECT_PRIMITIVE_ARRAY(D, Double);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001153 } else if (component_type == GetClassRoot<mirror::MethodType>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001154 COLLECT_REFERENCE_ARRAY(mirror::MethodType, MethodType);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001155 } else if (component_type == GetClassRoot<mirror::MethodHandle>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001156 COLLECT_REFERENCE_ARRAY(mirror::MethodHandle, MethodHandle);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001157 } else if (component_type == GetClassRoot<mirror::String>(class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001158 COLLECT_REFERENCE_ARRAY(mirror::String, String);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001159 } else if (component_type == GetClassRoot<mirror::Class>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001160 COLLECT_REFERENCE_ARRAY(mirror::Class, Type);
1161 } else {
1162 UNREACHABLE();
1163 }
1164 #undef COLLECT_PRIMITIVE_ARRAY
1165 #undef COLLECT_REFERENCE_ARRAY
1166}
1167
1168static ObjPtr<mirror::MethodType> BuildCallSiteForBootstrapMethod(Thread* self,
1169 const DexFile* dex_file,
1170 uint32_t call_site_idx)
1171 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001172 const dex::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx);
Orion Hodsona5dca522018-02-27 12:42:11 +00001173 CallSiteArrayValueIterator it(*dex_file, csi);
1174 DCHECK_GE(it.Size(), 1u);
1175
1176 StackHandleScope<2> hs(self);
1177 // Create array for parameter types.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001178 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001179 ObjPtr<mirror::Class> class_array_type =
1180 GetClassRoot<mirror::ObjectArray<mirror::Class>>(class_linker);
Orion Hodsona5dca522018-02-27 12:42:11 +00001181 Handle<mirror::ObjectArray<mirror::Class>> ptypes = hs.NewHandle(
1182 mirror::ObjectArray<mirror::Class>::Alloc(self,
1183 class_array_type,
1184 static_cast<int>(it.Size())));
1185 if (ptypes.IsNull()) {
1186 DCHECK(self->IsExceptionPending());
1187 return nullptr;
1188 }
1189
1190 // Populate the first argument with an instance of j.l.i.MethodHandles.Lookup
1191 // that the runtime will construct.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001192 ptypes->Set(0, GetClassRoot<mirror::MethodHandlesLookup>(class_linker));
Orion Hodsona5dca522018-02-27 12:42:11 +00001193 it.Next();
1194
1195 // The remaining parameter types are derived from the types of
1196 // arguments present in the DEX file.
1197 int index = 1;
1198 while (it.HasNext()) {
1199 ObjPtr<mirror::Class> ptype = GetClassForBootstrapArgument(it.GetValueType());
1200 if (ptype.IsNull()) {
1201 ThrowClassCastException("Unsupported bootstrap argument type");
1202 return nullptr;
1203 }
1204 ptypes->Set(index, ptype);
1205 index++;
1206 it.Next();
1207 }
1208 DCHECK_EQ(static_cast<size_t>(index), it.Size());
1209
1210 // By definition, the return type is always a j.l.i.CallSite.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001211 Handle<mirror::Class> rtype = hs.NewHandle(GetClassRoot<mirror::CallSite>());
Orion Hodsona5dca522018-02-27 12:42:11 +00001212 return mirror::MethodType::Create(self, rtype, ptypes);
1213}
1214
Orion Hodsonc069a302017-01-18 09:23:12 +00001215static ObjPtr<mirror::CallSite> InvokeBootstrapMethod(Thread* self,
1216 ShadowFrame& shadow_frame,
1217 uint32_t call_site_idx)
1218 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001219 StackHandleScope<5> hs(self);
Orion Hodsona5dca522018-02-27 12:42:11 +00001220 // There are three mandatory arguments expected from the call site
1221 // value array in the DEX file: the bootstrap method handle, the
1222 // method name to pass to the bootstrap method, and the method type
1223 // to pass to the bootstrap method.
1224 static constexpr size_t kMandatoryArgumentsCount = 3;
Orion Hodsonc069a302017-01-18 09:23:12 +00001225 ArtMethod* referrer = shadow_frame.GetMethod();
1226 const DexFile* dex_file = referrer->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001227 const dex::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00001228 CallSiteArrayValueIterator it(*dex_file, csi);
Orion Hodsona5dca522018-02-27 12:42:11 +00001229 if (it.Size() < kMandatoryArgumentsCount) {
1230 ThrowBootstrapMethodError("Truncated bootstrap arguments (%zu < %zu)",
1231 it.Size(), kMandatoryArgumentsCount);
1232 return nullptr;
1233 }
1234
1235 if (it.GetValueType() != EncodedArrayValueIterator::ValueType::kMethodHandle) {
1236 ThrowBootstrapMethodError("First bootstrap argument is not a method handle");
1237 return nullptr;
1238 }
1239
1240 uint32_t bsm_index = static_cast<uint32_t>(it.GetJavaValue().i);
1241 it.Next();
1242
Orion Hodsonc069a302017-01-18 09:23:12 +00001243 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Orion Hodsona5dca522018-02-27 12:42:11 +00001244 Handle<mirror::MethodHandle> bsm =
1245 hs.NewHandle(class_linker->ResolveMethodHandle(self, bsm_index, referrer));
1246 if (bsm.IsNull()) {
Orion Hodsonc069a302017-01-18 09:23:12 +00001247 DCHECK(self->IsExceptionPending());
1248 return nullptr;
1249 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001250
Orion Hodsona5dca522018-02-27 12:42:11 +00001251 if (bsm->GetHandleKind() != mirror::MethodHandle::Kind::kInvokeStatic) {
1252 // JLS suggests also accepting constructors. This is currently
1253 // hard as constructor invocations happen via transformers in ART
1254 // today. The constructor would need to be a class derived from java.lang.invoke.CallSite.
1255 ThrowBootstrapMethodError("Unsupported bootstrap method invocation kind");
1256 return nullptr;
1257 }
1258
1259 // Construct the local call site type information based on the 3
1260 // mandatory arguments provided by the runtime and the static arguments
1261 // in the DEX file. We will use these arguments to build a shadow frame.
1262 MutableHandle<mirror::MethodType> call_site_type =
1263 hs.NewHandle(BuildCallSiteForBootstrapMethod(self, dex_file, call_site_idx));
1264 if (call_site_type.IsNull()) {
1265 DCHECK(self->IsExceptionPending());
1266 return nullptr;
1267 }
1268
1269 // Check if this BSM is targeting a variable arity method. If so,
1270 // we'll need to collect the trailing arguments into an array.
1271 Handle<mirror::Array> collector_arguments;
1272 int32_t collector_arguments_length;
1273 if (bsm->GetTargetMethod()->IsVarargs()) {
1274 int number_of_bsm_parameters = bsm->GetMethodType()->GetNumberOfPTypes();
1275 if (number_of_bsm_parameters == 0) {
1276 ThrowBootstrapMethodError("Variable arity BSM does not have any arguments");
1277 return nullptr;
1278 }
1279 Handle<mirror::Class> collector_array_class =
1280 hs.NewHandle(bsm->GetMethodType()->GetPTypes()->Get(number_of_bsm_parameters - 1));
1281 if (!collector_array_class->IsArrayClass()) {
1282 ThrowBootstrapMethodError("Variable arity BSM does not have array as final argument");
1283 return nullptr;
1284 }
1285 // The call site may include no arguments to be collected. In this
1286 // case the number of arguments must be at least the number of BSM
1287 // parameters less the collector array.
1288 if (call_site_type->GetNumberOfPTypes() < number_of_bsm_parameters - 1) {
1289 ThrowWrongMethodTypeException(bsm->GetMethodType(), call_site_type.Get());
1290 return nullptr;
1291 }
1292 // Check all the arguments to be collected match the collector array component type.
1293 for (int i = number_of_bsm_parameters - 1; i < call_site_type->GetNumberOfPTypes(); ++i) {
1294 if (call_site_type->GetPTypes()->Get(i) != collector_array_class->GetComponentType()) {
1295 ThrowClassCastException(collector_array_class->GetComponentType(),
1296 call_site_type->GetPTypes()->Get(i));
1297 return nullptr;
1298 }
1299 }
1300 // Update the call site method type so it now includes the collector array.
1301 int32_t collector_arguments_start = number_of_bsm_parameters - 1;
1302 collector_arguments_length = call_site_type->GetNumberOfPTypes() - number_of_bsm_parameters + 1;
1303 call_site_type.Assign(
1304 mirror::MethodType::CollectTrailingArguments(self,
1305 call_site_type.Get(),
1306 collector_array_class.Get(),
1307 collector_arguments_start));
1308 if (call_site_type.IsNull()) {
1309 DCHECK(self->IsExceptionPending());
1310 return nullptr;
1311 }
1312 } else {
1313 collector_arguments_length = 0;
1314 }
1315
1316 if (call_site_type->GetNumberOfPTypes() != bsm->GetMethodType()->GetNumberOfPTypes()) {
1317 ThrowWrongMethodTypeException(bsm->GetMethodType(), call_site_type.Get());
1318 return nullptr;
1319 }
1320
1321 // BSM invocation has a different set of exceptions that
1322 // j.l.i.MethodHandle.invoke(). Scan arguments looking for CCE
1323 // "opportunities". Unfortunately we cannot just leave this to the
1324 // method handle invocation as this might generate a WMTE.
1325 for (int32_t i = 0; i < call_site_type->GetNumberOfPTypes(); ++i) {
1326 ObjPtr<mirror::Class> from = call_site_type->GetPTypes()->Get(i);
1327 ObjPtr<mirror::Class> to = bsm->GetMethodType()->GetPTypes()->Get(i);
1328 if (!IsParameterTypeConvertible(from, to)) {
1329 ThrowClassCastException(from, to);
1330 return nullptr;
1331 }
1332 }
1333 if (!IsReturnTypeConvertible(call_site_type->GetRType(), bsm->GetMethodType()->GetRType())) {
1334 ThrowClassCastException(bsm->GetMethodType()->GetRType(), call_site_type->GetRType());
1335 return nullptr;
1336 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001337
1338 // Set-up a shadow frame for invoking the bootstrap method handle.
1339 ShadowFrameAllocaUniquePtr bootstrap_frame =
Orion Hodsona5dca522018-02-27 12:42:11 +00001340 CREATE_SHADOW_FRAME(call_site_type->NumberOfVRegs(),
1341 nullptr,
1342 referrer,
1343 shadow_frame.GetDexPC());
Orion Hodsonc069a302017-01-18 09:23:12 +00001344 ScopedStackedShadowFramePusher pusher(
1345 self, bootstrap_frame.get(), StackedShadowFrameType::kShadowFrameUnderConstruction);
Orion Hodsona5dca522018-02-27 12:42:11 +00001346 ShadowFrameSetter setter(bootstrap_frame.get(), 0u);
Orion Hodsonc069a302017-01-18 09:23:12 +00001347
1348 // The first parameter is a MethodHandles lookup instance.
Orion Hodsona5dca522018-02-27 12:42:11 +00001349 Handle<mirror::Class> lookup_class =
1350 hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass());
1351 ObjPtr<mirror::MethodHandlesLookup> lookup =
1352 mirror::MethodHandlesLookup::Create(self, lookup_class);
1353 if (lookup.IsNull()) {
Orion Hodsonc069a302017-01-18 09:23:12 +00001354 DCHECK(self->IsExceptionPending());
1355 return nullptr;
1356 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001357 setter.SetReference(lookup);
Orion Hodsonc069a302017-01-18 09:23:12 +00001358
Orion Hodsona5dca522018-02-27 12:42:11 +00001359 // Pack the remaining arguments into the frame.
1360 int number_of_arguments = call_site_type->GetNumberOfPTypes();
1361 int argument_index;
1362 for (argument_index = 1; argument_index < number_of_arguments; ++argument_index) {
1363 if (argument_index == number_of_arguments - 1 &&
1364 call_site_type->GetPTypes()->Get(argument_index)->IsArrayClass()) {
1365 ObjPtr<mirror::Class> array_type = call_site_type->GetPTypes()->Get(argument_index);
1366 if (!PackCollectorArrayForBootstrapMethod(self,
1367 referrer,
1368 array_type,
1369 collector_arguments_length,
1370 &it,
1371 &setter)) {
1372 DCHECK(self->IsExceptionPending());
1373 return nullptr;
Orion Hodsonc069a302017-01-18 09:23:12 +00001374 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001375 } else if (!PackArgumentForBootstrapMethod(self, referrer, &it, &setter)) {
1376 DCHECK(self->IsExceptionPending());
1377 return nullptr;
Orion Hodsonc069a302017-01-18 09:23:12 +00001378 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001379 it.Next();
1380 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001381 DCHECK(!it.HasNext());
1382 DCHECK(setter.Done());
Orion Hodsonc069a302017-01-18 09:23:12 +00001383
1384 // Invoke the bootstrap method handle.
1385 JValue result;
Orion Hodsona5dca522018-02-27 12:42:11 +00001386 RangeInstructionOperands operands(0, bootstrap_frame->NumberOfVRegs());
1387 bool invoke_success = MethodHandleInvoke(self,
1388 *bootstrap_frame,
1389 bsm,
1390 call_site_type,
1391 &operands,
1392 &result);
Orion Hodsonc069a302017-01-18 09:23:12 +00001393 if (!invoke_success) {
1394 DCHECK(self->IsExceptionPending());
1395 return nullptr;
1396 }
1397
1398 Handle<mirror::Object> object(hs.NewHandle(result.GetL()));
Orion Hodsonc069a302017-01-18 09:23:12 +00001399 if (UNLIKELY(object.IsNull())) {
Orion Hodsonda1cdd02018-01-31 18:08:28 +00001400 // This will typically be for LambdaMetafactory which is not supported.
Orion Hodson76e6adb2018-02-23 13:15:55 +00001401 ThrowClassCastException("Bootstrap method returned null");
Orion Hodsonc069a302017-01-18 09:23:12 +00001402 return nullptr;
1403 }
1404
Orion Hodsona5dca522018-02-27 12:42:11 +00001405 // Check the result type is a subclass of j.l.i.CallSite.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001406 ObjPtr<mirror::Class> call_site_class = GetClassRoot<mirror::CallSite>(class_linker);
1407 if (UNLIKELY(!object->InstanceOf(call_site_class))) {
1408 ThrowClassCastException(object->GetClass(), call_site_class);
Orion Hodsonc069a302017-01-18 09:23:12 +00001409 return nullptr;
1410 }
1411
Orion Hodsona5dca522018-02-27 12:42:11 +00001412 // Check the call site target is not null as we're going to invoke it.
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001413 ObjPtr<mirror::CallSite> call_site =
1414 ObjPtr<mirror::CallSite>::DownCast(ObjPtr<mirror::Object>(result.GetL()));
1415 ObjPtr<mirror::MethodHandle> target = call_site->GetTarget();
1416 if (UNLIKELY(target == nullptr)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001417 ThrowClassCastException("Bootstrap method returned a CallSite with a null target");
Orion Hodsonc069a302017-01-18 09:23:12 +00001418 return nullptr;
1419 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001420 return call_site;
Orion Hodsonc069a302017-01-18 09:23:12 +00001421}
1422
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001423namespace {
1424
1425ObjPtr<mirror::CallSite> DoResolveCallSite(Thread* self,
1426 ShadowFrame& shadow_frame,
1427 uint32_t call_site_idx)
1428 REQUIRES_SHARED(Locks::mutator_lock_) {
1429 StackHandleScope<1> hs(self);
1430 Handle<mirror::DexCache> dex_cache(hs.NewHandle(shadow_frame.GetMethod()->GetDexCache()));
1431
1432 // Get the call site from the DexCache if present.
1433 ObjPtr<mirror::CallSite> call_site = dex_cache->GetResolvedCallSite(call_site_idx);
1434 if (LIKELY(call_site != nullptr)) {
1435 return call_site;
1436 }
1437
1438 // Invoke the bootstrap method to get a candidate call site.
1439 call_site = InvokeBootstrapMethod(self, shadow_frame, call_site_idx);
1440 if (UNLIKELY(call_site == nullptr)) {
1441 if (!self->GetException()->IsError()) {
1442 // Use a BootstrapMethodError if the exception is not an instance of java.lang.Error.
1443 ThrowWrappedBootstrapMethodError("Exception from call site #%u bootstrap method",
1444 call_site_idx);
1445 }
1446 return nullptr;
1447 }
1448
1449 // Attempt to place the candidate call site into the DexCache, return the winning call site.
1450 return dex_cache->SetResolvedCallSite(call_site_idx, call_site);
1451}
1452
1453} // namespace
1454
Orion Hodsonc069a302017-01-18 09:23:12 +00001455bool DoInvokeCustom(Thread* self,
1456 ShadowFrame& shadow_frame,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001457 uint32_t call_site_idx,
1458 const InstructionOperands* operands,
1459 JValue* result) {
Alex Light848574c2017-09-25 16:59:39 -07001460 // Make sure to check for async exceptions
1461 if (UNLIKELY(self->ObserveAsyncException())) {
1462 return false;
1463 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001464
Orion Hodsonc069a302017-01-18 09:23:12 +00001465 // invoke-custom is not supported in transactions. In transactions
1466 // there is a limited set of types supported. invoke-custom allows
1467 // running arbitrary code and instantiating arbitrary types.
1468 CHECK(!Runtime::Current()->IsActiveTransaction());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001469
1470 ObjPtr<mirror::CallSite> call_site = DoResolveCallSite(self, shadow_frame, call_site_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00001471 if (call_site.IsNull()) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001472 DCHECK(self->IsExceptionPending());
1473 return false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001474 }
1475
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001476 StackHandleScope<2> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +00001477 Handle<mirror::MethodHandle> target = hs.NewHandle(call_site->GetTarget());
1478 Handle<mirror::MethodType> target_method_type = hs.NewHandle(target->GetMethodType());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001479 DCHECK_EQ(operands->GetNumberOfOperands(), target_method_type->NumberOfVRegs())
1480 << " call_site_idx" << call_site_idx;
1481 return MethodHandleInvokeExact(self,
1482 shadow_frame,
1483 target,
1484 target_method_type,
1485 operands,
1486 result);
Orion Hodsonc069a302017-01-18 09:23:12 +00001487}
1488
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001489// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
1490static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
1491 size_t dest_reg, size_t src_reg)
1492 REQUIRES_SHARED(Locks::mutator_lock_) {
1493 // Uint required, so that sign extension does not make this wrong on 64b systems
1494 uint32_t src_value = shadow_frame.GetVReg(src_reg);
1495 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
1496
1497 // If both register locations contains the same value, the register probably holds a reference.
1498 // Note: As an optimization, non-moving collectors leave a stale reference value
1499 // in the references array even after the original vreg was overwritten to a non-reference.
Vladimir Marko78baed52018-10-11 10:44:58 +01001500 if (src_value == reinterpret_cast32<uint32_t>(o.Ptr())) {
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001501 new_shadow_frame->SetVRegReference(dest_reg, o);
1502 } else {
1503 new_shadow_frame->SetVReg(dest_reg, src_value);
1504 }
1505}
1506
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001507template <bool is_range>
1508inline void CopyRegisters(ShadowFrame& caller_frame,
1509 ShadowFrame* callee_frame,
1510 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
1511 const size_t first_src_reg,
1512 const size_t first_dest_reg,
1513 const size_t num_regs) {
1514 if (is_range) {
1515 const size_t dest_reg_bound = first_dest_reg + num_regs;
1516 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound;
1517 ++dest_reg, ++src_reg) {
1518 AssignRegister(callee_frame, caller_frame, dest_reg, src_reg);
1519 }
1520 } else {
1521 DCHECK_LE(num_regs, arraysize(arg));
1522
1523 for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) {
1524 AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]);
1525 }
1526 }
1527}
1528
Igor Murashkin6918bf12015-09-27 19:19:06 -07001529template <bool is_range,
Narayan Kamath370423d2016-10-03 16:51:22 +01001530 bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001531static inline bool DoCallCommon(ArtMethod* called_method,
1532 Thread* self,
1533 ShadowFrame& shadow_frame,
1534 JValue* result,
1535 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +01001536 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -07001537 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001538 bool string_init = false;
1539 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001540 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
1541 && called_method->IsConstructor())) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001542 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
Jeff Hao848f70a2014-01-15 13:49:50 -08001543 string_init = true;
1544 }
1545
Alex Lightdaf58c82016-03-16 23:00:49 +00001546 // Compute method information.
David Sehr0225f8e2018-01-31 08:52:24 +00001547 CodeItemDataAccessor accessor(called_method->DexInstructionData());
Igor Murashkin158f35c2015-06-10 15:55:30 -07001548 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001549 uint16_t num_regs;
Jeff Hao5ea84132017-05-05 16:59:29 -07001550 // Test whether to use the interpreter or compiler entrypoint, and save that result to pass to
1551 // PerformCall. A deoptimization could occur at any time, and we shouldn't change which
1552 // entrypoint to use once we start building the shadow frame.
Mathieu Chartier448bbcf2017-07-06 12:05:13 -07001553
1554 // For unstarted runtimes, always use the interpreter entrypoint. This fixes the case where we are
1555 // doing cross compilation. Note that GetEntryPointFromQuickCompiledCode doesn't use the image
1556 // pointer size here and this may case an overflow if it is called from the compiler. b/62402160
1557 const bool use_interpreter_entrypoint = !Runtime::Current()->IsStarted() ||
1558 ClassLinker::ShouldUseInterpreterEntrypoint(
1559 called_method,
1560 called_method->GetEntryPointFromQuickCompiledCode());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001561 if (LIKELY(accessor.HasCodeItem())) {
Jeff Hao5ea84132017-05-05 16:59:29 -07001562 // When transitioning to compiled code, space only needs to be reserved for the input registers.
1563 // The rest of the frame gets discarded. This also prevents accessing the called method's code
1564 // item, saving memory by keeping code items of compiled code untouched.
Mathieu Chartier448bbcf2017-07-06 12:05:13 -07001565 if (!use_interpreter_entrypoint) {
1566 DCHECK(!Runtime::Current()->IsAotCompiler()) << "Compiler should use interpreter entrypoint";
Jeff Hao5ea84132017-05-05 16:59:29 -07001567 num_regs = number_of_inputs;
1568 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001569 num_regs = accessor.RegistersSize();
1570 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, accessor.InsSize());
Jeff Hao5ea84132017-05-05 16:59:29 -07001571 }
Nicolas Geoffray01822292017-03-09 09:03:19 +00001572 } else {
1573 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1574 num_regs = number_of_inputs;
Jeff Haodf79ddb2017-02-27 14:47:06 -08001575 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001576
Igor Murashkin158f35c2015-06-10 15:55:30 -07001577 // Hack for String init:
1578 //
1579 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
1580 // invoke-x StringFactory(a, b, c, ...)
1581 // by effectively dropping the first virtual register from the invoke.
1582 //
1583 // (at this point the ArtMethod has already been replaced,
1584 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +00001585 //
1586 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
1587 // to handle the compiler optimization of replacing `this` with null without
1588 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001589 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -07001590 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001591 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 -07001592
Igor Murashkin158f35c2015-06-10 15:55:30 -07001593 // The new StringFactory call is static and has one fewer argument.
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001594 if (!accessor.HasCodeItem()) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001595 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1596 num_regs--;
1597 } // 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 -07001598 number_of_inputs--;
1599
1600 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -07001601 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001602 arg[i - 1] = arg[i];
1603 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07001604 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001605
1606 // Rewrite the non-var-arg case
1607 vregC++; // Skips the 0th vreg in the range ("this").
1608 }
1609
1610 // Parameter registers go at the end of the shadow frame.
1611 DCHECK_GE(num_regs, number_of_inputs);
1612 size_t first_dest_reg = num_regs - number_of_inputs;
1613 DCHECK_NE(first_dest_reg, (size_t)-1);
1614
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001615 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001616 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -07001617 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -07001618 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -07001619 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001620
Igor Murashkin158f35c2015-06-10 15:55:30 -07001621 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -07001622 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001623 // Slow path.
1624 // We might need to do class loading, which incurs a thread state change to kNative. So
1625 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07001626 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +02001627 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001628 self->EndAssertNoThreadSuspension(old_cause);
1629
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001630 // ArtMethod here is needed to check type information of the call site against the callee.
1631 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
1632 //
1633 // As a special case for proxy methods, which are not dex-backed,
1634 // we have to retrieve type information from the proxy's method
1635 // interface method instead (which is dex backed since proxies are never interfaces).
Andreas Gampe542451c2016-07-26 09:02:02 -07001636 ArtMethod* method =
1637 new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001638
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001639 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001640 // to get the exact type of each reference argument.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001641 const dex::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001642 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001643 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001644
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001645 // Handle receiver apart since it's not part of the shorty.
1646 size_t dest_reg = first_dest_reg;
1647 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001648
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001649 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001650 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001651 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
1652 ++dest_reg;
1653 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -07001654 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001655 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001656
1657 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001658 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001659 // Skip the 0th 'shorty' type since it represents the return type.
1660 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001661 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
1662 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001663 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001664 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001665 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001666 if (do_assignability_check && o != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001667 const dex::TypeIndex type_idx = params->GetTypeItem(shorty_pos).type_idx_;
Vladimir Marko942fd312017-01-16 20:52:19 +00001668 ObjPtr<mirror::Class> arg_type = method->GetDexCache()->GetResolvedType(type_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001669 if (arg_type == nullptr) {
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001670 StackHandleScope<1> hs(self);
1671 // Preserve o since it is used below and GetClassFromTypeIndex may cause thread
1672 // suspension.
1673 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&o);
Vladimir Markob45528c2017-07-27 14:14:28 +01001674 arg_type = method->ResolveClassFromTypeIndex(type_idx);
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001675 if (arg_type == nullptr) {
1676 CHECK(self->IsExceptionPending());
1677 return false;
1678 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001679 }
1680 if (!o->VerifierInstanceOf(arg_type)) {
1681 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -07001682 std::string temp1, temp2;
Orion Hodsonfef06642016-11-25 16:07:11 +00001683 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001684 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -08001685 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -07001686 o->GetClass()->GetDescriptor(&temp1),
1687 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001688 return false;
1689 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001690 }
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001691 new_shadow_frame->SetVRegReference(dest_reg, o);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001692 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -07001693 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001694 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001695 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001696 uint64_t wide_value =
1697 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
1698 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001699 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001700 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001701 ++dest_reg;
1702 ++arg_offset;
1703 break;
1704 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001705 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001706 default:
1707 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
1708 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001709 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001710 }
1711 } else {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001712 if (is_range) {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001713 DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001714 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001715
1716 CopyRegisters<is_range>(shadow_frame,
1717 new_shadow_frame,
1718 arg,
1719 vregC,
1720 first_dest_reg,
1721 number_of_inputs);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001722 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001723 }
1724
Jeff Hao5ea84132017-05-05 16:59:29 -07001725 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001726 accessor,
Jeff Hao5ea84132017-05-05 16:59:29 -07001727 shadow_frame.GetMethod(),
1728 first_dest_reg,
1729 new_shadow_frame,
1730 result,
1731 use_interpreter_entrypoint);
Jeff Hao848f70a2014-01-15 13:49:50 -08001732
1733 if (string_init && !self->IsExceptionPending()) {
Mingyao Yangffedec52016-05-19 10:48:40 -07001734 SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001735 }
1736
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001737 return !self->IsExceptionPending();
1738}
1739
Igor Murashkin158f35c2015-06-10 15:55:30 -07001740template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001741bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
1742 const Instruction* inst, uint16_t inst_data, JValue* result) {
1743 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001744 const uint16_t number_of_inputs =
1745 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001746
1747 // TODO: find a cleaner way to separate non-range and range information without duplicating
1748 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -07001749 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001750 uint32_t vregC = 0;
1751 if (is_range) {
1752 vregC = inst->VRegC_3rc();
1753 } else {
1754 vregC = inst->VRegC_35c();
1755 inst->GetVarArgs(arg, inst_data);
1756 }
1757
1758 return DoCallCommon<is_range, do_assignability_check>(
1759 called_method, self, shadow_frame,
1760 result, number_of_inputs, arg, vregC);
1761}
1762
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001763template <bool is_range, bool do_access_check, bool transaction_active>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001764bool DoFilledNewArray(const Instruction* inst,
1765 const ShadowFrame& shadow_frame,
1766 Thread* self,
1767 JValue* result) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001768 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
1769 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
1770 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
1771 if (!is_range) {
1772 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
1773 CHECK_LE(length, 5);
1774 }
1775 if (UNLIKELY(length < 0)) {
1776 ThrowNegativeArraySizeException(length);
1777 return false;
1778 }
1779 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08001780 ObjPtr<mirror::Class> array_class = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
Mathieu Chartieref41db72016-10-25 15:08:01 -07001781 shadow_frame.GetMethod(),
1782 self,
1783 false,
1784 do_access_check);
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001785 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001786 DCHECK(self->IsExceptionPending());
1787 return false;
1788 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001789 CHECK(array_class->IsArrayClass());
Mathieu Chartieref41db72016-10-25 15:08:01 -07001790 ObjPtr<mirror::Class> component_class = array_class->GetComponentType();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001791 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
1792 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
1793 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001794 ThrowRuntimeException("Bad filled array request for type %s",
David Sehr709b0702016-10-13 09:12:37 -07001795 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001796 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001797 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -08001798 "Found type %s; filled-new-array not implemented for anything but 'int'",
David Sehr709b0702016-10-13 09:12:37 -07001799 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001800 }
1801 return false;
1802 }
Mathieu Chartieref41db72016-10-25 15:08:01 -07001803 ObjPtr<mirror::Object> new_array = mirror::Array::Alloc<true>(
1804 self,
1805 array_class,
1806 length,
1807 array_class->GetComponentSizeShift(),
1808 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001809 if (UNLIKELY(new_array == nullptr)) {
1810 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001811 return false;
1812 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001813 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
1814 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001815 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +01001816 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001817 } else {
Ian Rogers29a26482014-05-02 15:27:29 -07001818 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +01001819 }
Sebastien Hertzabff6432014-01-27 18:01:39 +01001820 for (int32_t i = 0; i < length; ++i) {
1821 size_t src_reg = is_range ? vregC + i : arg[i];
1822 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001823 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
1824 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +01001825 } else {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001826 new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<transaction_active>(
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001827 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001828 }
1829 }
1830
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001831 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001832 return true;
1833}
1834
Mathieu Chartieref41db72016-10-25 15:08:01 -07001835// TODO: Use ObjPtr here.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001836template<typename T>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001837static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array,
1838 int32_t count)
1839 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001840 Runtime* runtime = Runtime::Current();
1841 for (int32_t i = 0; i < count; ++i) {
1842 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
1843 }
1844}
1845
Mathieu Chartieref41db72016-10-25 15:08:01 -07001846void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001847 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001848 DCHECK(Runtime::Current()->IsActiveTransaction());
1849 DCHECK(array != nullptr);
1850 DCHECK_LE(count, array->GetLength());
1851 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
1852 switch (primitive_component_type) {
1853 case Primitive::kPrimBoolean:
1854 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
1855 break;
1856 case Primitive::kPrimByte:
1857 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
1858 break;
1859 case Primitive::kPrimChar:
1860 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
1861 break;
1862 case Primitive::kPrimShort:
1863 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
1864 break;
1865 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001866 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
1867 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001868 case Primitive::kPrimFloat:
1869 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
1870 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001871 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001872 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
1873 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001874 case Primitive::kPrimDouble:
1875 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
1876 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001877 default:
1878 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
1879 << " in fill-array-data";
Elliott Hughesc1896c92018-11-29 11:33:18 -08001880 UNREACHABLE();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001881 }
1882}
1883
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001884// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +02001885#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001886 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001887 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
1888 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +02001889 const Instruction* inst, uint16_t inst_data, \
1890 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001891EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
1892EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
1893EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
1894EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
1895#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001896
Orion Hodsonc069a302017-01-18 09:23:12 +00001897// Explicit DoInvokePolymorphic template function declarations.
1898#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range) \
1899 template REQUIRES_SHARED(Locks::mutator_lock_) \
1900 bool DoInvokePolymorphic<_is_range>( \
1901 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1902 uint16_t inst_data, JValue* result)
1903EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false);
1904EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true);
Narayan Kamath9823e782016-08-03 12:46:58 +01001905#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1906
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001907// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001908#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001909 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001910 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
1911 const ShadowFrame& shadow_frame, \
1912 Thread* self, JValue* result)
1913#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
1914 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
1915 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
1916 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
1917 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
1918EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
1919EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
1920#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001921#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
1922
1923} // namespace interpreter
1924} // namespace art