blob: fe6154792c96c57817be987f9f615a9e9bd675a9 [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"
Andreas Gampe52ecb652018-10-24 15:18:21 -070037#include "mirror/object_array-alloc-inl.h"
38#include "mirror/object_array-inl.h"
Orion Hodson928033d2018-02-07 05:30:54 +000039#include "mirror/var_handle.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010040#include "reflection-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070041#include "reflection.h"
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +010042#include "shadow_frame-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070043#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070044#include "thread-inl.h"
Chang Xingbd208d82017-07-12 14:53:17 -070045#include "transaction.h"
Orion Hodson537a4fe2018-05-15 13:57:58 +010046#include "var_handles.h"
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +010047#include "well_known_classes.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020048
49namespace art {
50namespace interpreter {
51
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000052void ThrowNullPointerExceptionFromInterpreter() {
53 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -070054}
55
David Srbecky960327b2018-10-25 10:11:59 +000056bool CheckStackOverflow(Thread* self, size_t frame_size)
57 REQUIRES_SHARED(Locks::mutator_lock_) {
58 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
59 uint8_t* stack_end = self->GetStackEndForInterpreter(implicit_check);
60 if (UNLIKELY(__builtin_frame_address(0) < stack_end + frame_size)) {
61 ThrowStackOverflowError(self);
62 return false;
63 }
64 return true;
65}
66
David Srbecky9581e612018-10-30 14:29:43 +000067bool UseFastInterpreterToInterpreterInvoke(ArtMethod* method) {
68 Runtime* runtime = Runtime::Current();
69 const void* quick_code = method->GetEntryPointFromQuickCompiledCode();
70 if (!runtime->GetClassLinker()->IsQuickToInterpreterBridge(quick_code)) {
71 return false;
72 }
73 if (!method->SkipAccessChecks() || method->IsNative() || method->IsProxyMethod()) {
74 return false;
75 }
76 if (method->IsIntrinsic()) {
77 return false;
78 }
79 if (method->GetDeclaringClass()->IsStringClass() && method->IsConstructor()) {
80 return false;
81 }
82 if (method->IsStatic() && !method->GetDeclaringClass()->IsInitialized()) {
83 return false;
84 }
85 ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
86 if ((profiling_info != nullptr) && (profiling_info->GetSavedEntryPoint() != nullptr)) {
87 return false;
88 }
89 return true;
90}
91
Chang Xingbd208d82017-07-12 14:53:17 -070092template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
93 bool transaction_active>
Ian Rogers54874942014-06-10 16:31:03 -070094bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
95 uint16_t inst_data) {
96 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
97 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010098 ArtField* f =
99 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
100 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -0700101 if (UNLIKELY(f == nullptr)) {
102 CHECK(self->IsExceptionPending());
103 return false;
104 }
Mathieu Chartieref41db72016-10-25 15:08:01 -0700105 ObjPtr<mirror::Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -0700106 if (is_static) {
107 obj = f->GetDeclaringClass();
Chang Xingbd208d82017-07-12 14:53:17 -0700108 if (transaction_active) {
109 if (Runtime::Current()->GetTransaction()->ReadConstraint(obj.Ptr(), f)) {
110 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Can't read static fields of "
111 + obj->PrettyTypeOf() + " since it does not belong to clinit's class.");
112 return false;
113 }
114 }
Ian Rogers54874942014-06-10 16:31:03 -0700115 } else {
116 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
117 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000118 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers54874942014-06-10 16:31:03 -0700119 return false;
120 }
121 }
Orion Hodson3d617ac2016-10-19 14:00:46 +0100122
123 JValue result;
Alex Light084fa372017-06-16 08:58:34 -0700124 if (UNLIKELY(!DoFieldGetCommon<field_type>(self, shadow_frame, obj, f, &result))) {
125 // Instrumentation threw an error!
126 CHECK(self->IsExceptionPending());
127 return false;
128 }
Ian Rogers54874942014-06-10 16:31:03 -0700129 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
130 switch (field_type) {
131 case Primitive::kPrimBoolean:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100132 shadow_frame.SetVReg(vregA, result.GetZ());
Ian Rogers54874942014-06-10 16:31:03 -0700133 break;
134 case Primitive::kPrimByte:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100135 shadow_frame.SetVReg(vregA, result.GetB());
Ian Rogers54874942014-06-10 16:31:03 -0700136 break;
137 case Primitive::kPrimChar:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100138 shadow_frame.SetVReg(vregA, result.GetC());
Ian Rogers54874942014-06-10 16:31:03 -0700139 break;
140 case Primitive::kPrimShort:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100141 shadow_frame.SetVReg(vregA, result.GetS());
Ian Rogers54874942014-06-10 16:31:03 -0700142 break;
143 case Primitive::kPrimInt:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100144 shadow_frame.SetVReg(vregA, result.GetI());
Ian Rogers54874942014-06-10 16:31:03 -0700145 break;
146 case Primitive::kPrimLong:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100147 shadow_frame.SetVRegLong(vregA, result.GetJ());
Ian Rogers54874942014-06-10 16:31:03 -0700148 break;
149 case Primitive::kPrimNot:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100150 shadow_frame.SetVRegReference(vregA, result.GetL());
Ian Rogers54874942014-06-10 16:31:03 -0700151 break;
152 default:
153 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700154 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700155 }
156 return true;
157}
158
159// Explicitly instantiate all DoFieldGet functions.
Chang Xingbd208d82017-07-12 14:53:17 -0700160#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
161 template bool DoFieldGet<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
Ian Rogers54874942014-06-10 16:31:03 -0700162 ShadowFrame& shadow_frame, \
163 const Instruction* inst, \
164 uint16_t inst_data)
165
166#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
Chang Xingbd208d82017-07-12 14:53:17 -0700167 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false, true); \
168 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false, false); \
169 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true, true); \
170 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true, false);
Ian Rogers54874942014-06-10 16:31:03 -0700171
172// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700173EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
174EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
175EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
176EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
177EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
178EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
179EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700180
181// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700182EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
183EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
184EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
185EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
186EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
187EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
188EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700189
190#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
191#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
192
193// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
194// Returns true on success, otherwise throws an exception and returns false.
195template<Primitive::Type field_type>
196bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700197 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700198 if (UNLIKELY(obj == nullptr)) {
199 // We lost the reference to the field index so we cannot get a more
200 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000201 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700202 return false;
203 }
204 MemberOffset field_offset(inst->VRegC_22c());
205 // Report this field access to instrumentation if needed. Since we only have the offset of
206 // the field from the base of the object, we need to look for it first.
207 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
208 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
209 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
210 field_offset.Uint32Value());
211 DCHECK(f != nullptr);
212 DCHECK(!f->IsStatic());
Alex Light084fa372017-06-16 08:58:34 -0700213 Thread* self = Thread::Current();
214 StackHandleScope<1> hs(self);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700215 // Save obj in case the instrumentation event has thread suspension.
216 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Alex Light084fa372017-06-16 08:58:34 -0700217 instrumentation->FieldReadEvent(self,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700218 obj.Ptr(),
219 shadow_frame.GetMethod(),
220 shadow_frame.GetDexPC(),
221 f);
Alex Light084fa372017-06-16 08:58:34 -0700222 if (UNLIKELY(self->IsExceptionPending())) {
223 return false;
224 }
Ian Rogers54874942014-06-10 16:31:03 -0700225 }
226 // Note: iget-x-quick instructions are only for non-volatile fields.
227 const uint32_t vregA = inst->VRegA_22c(inst_data);
228 switch (field_type) {
229 case Primitive::kPrimInt:
230 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
231 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800232 case Primitive::kPrimBoolean:
233 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
234 break;
235 case Primitive::kPrimByte:
236 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
237 break;
238 case Primitive::kPrimChar:
239 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
240 break;
241 case Primitive::kPrimShort:
242 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
243 break;
Ian Rogers54874942014-06-10 16:31:03 -0700244 case Primitive::kPrimLong:
245 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
246 break;
247 case Primitive::kPrimNot:
248 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
249 break;
250 default:
251 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700252 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700253 }
254 return true;
255}
256
257// Explicitly instantiate all DoIGetQuick functions.
258#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
259 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
260 uint16_t inst_data)
261
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800262EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
263EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
264EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
265EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
266EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
267EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
268EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700269#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
270
271template<Primitive::Type field_type>
272static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700273 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700274 JValue field_value;
275 switch (field_type) {
276 case Primitive::kPrimBoolean:
277 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
278 break;
279 case Primitive::kPrimByte:
280 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
281 break;
282 case Primitive::kPrimChar:
283 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
284 break;
285 case Primitive::kPrimShort:
286 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
287 break;
288 case Primitive::kPrimInt:
289 field_value.SetI(shadow_frame.GetVReg(vreg));
290 break;
291 case Primitive::kPrimLong:
292 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
293 break;
294 case Primitive::kPrimNot:
295 field_value.SetL(shadow_frame.GetVRegReference(vreg));
296 break;
297 default:
298 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700299 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700300 }
301 return field_value;
302}
303
Orion Hodson3d617ac2016-10-19 14:00:46 +0100304template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
305 bool transaction_active>
306bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
307 uint16_t inst_data) {
308 const bool do_assignability_check = do_access_check;
309 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
310 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
311 ArtField* f =
312 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
313 Primitive::ComponentSize(field_type));
314 if (UNLIKELY(f == nullptr)) {
315 CHECK(self->IsExceptionPending());
316 return false;
317 }
318 ObjPtr<mirror::Object> obj;
319 if (is_static) {
320 obj = f->GetDeclaringClass();
Chang Xingbd208d82017-07-12 14:53:17 -0700321 if (transaction_active) {
322 if (Runtime::Current()->GetTransaction()->WriteConstraint(obj.Ptr(), f)) {
323 Runtime::Current()->AbortTransactionAndThrowAbortError(
324 self, "Can't set fields of " + obj->PrettyTypeOf());
325 return false;
326 }
327 }
328
Orion Hodson3d617ac2016-10-19 14:00:46 +0100329 } else {
330 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
331 if (UNLIKELY(obj == nullptr)) {
332 ThrowNullPointerExceptionForFieldAccess(f, false);
333 return false;
334 }
335 }
336
337 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100338 JValue value = GetFieldValue<field_type>(shadow_frame, vregA);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100339 return DoFieldPutCommon<field_type, do_assignability_check, transaction_active>(self,
340 shadow_frame,
341 obj,
342 f,
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100343 value);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100344}
345
Ian Rogers54874942014-06-10 16:31:03 -0700346// Explicitly instantiate all DoFieldPut functions.
347#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
348 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
349 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
350
351#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
352 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
353 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
354 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
355 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
356
357// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700358EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
359EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
360EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
361EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
362EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
363EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
364EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700365
366// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700367EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
368EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
369EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
370EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
371EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
372EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
373EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700374
375#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
376#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
377
378template<Primitive::Type field_type, bool transaction_active>
379bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700380 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700381 if (UNLIKELY(obj == nullptr)) {
382 // We lost the reference to the field index so we cannot get a more
383 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000384 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700385 return false;
386 }
387 MemberOffset field_offset(inst->VRegC_22c());
388 const uint32_t vregA = inst->VRegA_22c(inst_data);
389 // Report this field modification to instrumentation if needed. Since we only have the offset of
390 // the field from the base of the object, we need to look for it first.
391 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
392 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
393 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
394 field_offset.Uint32Value());
395 DCHECK(f != nullptr);
396 DCHECK(!f->IsStatic());
397 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Alex Light084fa372017-06-16 08:58:34 -0700398 Thread* self = Thread::Current();
399 StackHandleScope<2> hs(self);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700400 // Save obj in case the instrumentation event has thread suspension.
401 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Alex Light084fa372017-06-16 08:58:34 -0700402 mirror::Object* fake_root = nullptr;
403 HandleWrapper<mirror::Object> ret(hs.NewHandleWrapper<mirror::Object>(
404 field_type == Primitive::kPrimNot ? field_value.GetGCRoot() : &fake_root));
405 instrumentation->FieldWriteEvent(self,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700406 obj.Ptr(),
407 shadow_frame.GetMethod(),
408 shadow_frame.GetDexPC(),
409 f,
410 field_value);
Alex Light084fa372017-06-16 08:58:34 -0700411 if (UNLIKELY(self->IsExceptionPending())) {
412 return false;
413 }
Alex Light0aa7a5a2018-10-10 15:58:14 +0000414 if (UNLIKELY(shadow_frame.GetForcePopFrame())) {
415 // Don't actually set the field. The next instruction will force us to pop.
416 DCHECK(Runtime::Current()->AreNonStandardExitsEnabled());
417 DCHECK(PrevFrameWillRetry(self, shadow_frame));
418 return true;
419 }
Ian Rogers54874942014-06-10 16:31:03 -0700420 }
421 // Note: iput-x-quick instructions are only for non-volatile fields.
422 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700423 case Primitive::kPrimBoolean:
424 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
425 break;
426 case Primitive::kPrimByte:
427 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
428 break;
429 case Primitive::kPrimChar:
430 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
431 break;
432 case Primitive::kPrimShort:
433 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
434 break;
Ian Rogers54874942014-06-10 16:31:03 -0700435 case Primitive::kPrimInt:
436 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
437 break;
438 case Primitive::kPrimLong:
439 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
440 break;
441 case Primitive::kPrimNot:
442 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
443 break;
444 default:
445 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700446 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700447 }
448 return true;
449}
450
451// Explicitly instantiate all DoIPutQuick functions.
452#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
453 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
454 const Instruction* inst, \
455 uint16_t inst_data)
456
457#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
458 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
459 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
460
Andreas Gampec8ccf682014-09-29 20:07:43 -0700461EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
462EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
463EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
464EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
465EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
466EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
467EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700468#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
469#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
470
Alex Light9fb1ab12017-09-05 09:32:49 -0700471// We execute any instrumentation events that are triggered by this exception and change the
472// shadow_frame's dex_pc to that of the exception handler if there is one in the current method.
473// Return true if we should continue executing in the current method and false if we need to go up
474// the stack to find an exception handler.
Sebastien Hertz520633b2015-09-08 17:03:36 +0200475// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Alex Light9fb1ab12017-09-05 09:32:49 -0700476// TODO We should have a better way to skip instrumentation reporting or possibly rethink that
477// behavior.
478bool MoveToExceptionHandler(Thread* self,
479 ShadowFrame& shadow_frame,
480 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700481 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700482 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000483 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Alex Light9fb1ab12017-09-05 09:32:49 -0700484 if (instrumentation != nullptr &&
485 instrumentation->HasExceptionThrownListeners() &&
486 self->IsExceptionThrownByCurrentMethod(exception.Get())) {
487 // 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 -0700488 instrumentation->ExceptionThrownEvent(self, exception.Get());
Alex Light0aa7a5a2018-10-10 15:58:14 +0000489 if (shadow_frame.GetForcePopFrame()) {
490 // We will check in the caller for GetForcePopFrame again. We need to bail out early to
491 // prevent an ExceptionHandledEvent from also being sent before popping.
492 return true;
493 }
Sebastien Hertz9f102032014-05-23 08:59:42 +0200494 }
Ian Rogers54874942014-06-10 16:31:03 -0700495 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700496 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
Alex Light9fb1ab12017-09-05 09:32:49 -0700497 hs.NewHandle(exception->GetClass()), shadow_frame.GetDexPC(), &clear_exception);
Andreas Gampee2abbc62017-09-15 11:59:26 -0700498 if (found_dex_pc == dex::kDexNoIndex) {
Alex Light9fb1ab12017-09-05 09:32:49 -0700499 if (instrumentation != nullptr) {
500 if (shadow_frame.NeedsNotifyPop()) {
501 instrumentation->WatchedFramePopped(self, shadow_frame);
502 }
503 // Exception is not caught by the current method. We will unwind to the
504 // caller. Notify any instrumentation listener.
505 instrumentation->MethodUnwindEvent(self,
506 shadow_frame.GetThisObject(),
507 shadow_frame.GetMethod(),
508 shadow_frame.GetDexPC());
Alex Lighte814f9d2017-07-31 16:14:39 -0700509 }
Alex Light9fb1ab12017-09-05 09:32:49 -0700510 return false;
Ian Rogers54874942014-06-10 16:31:03 -0700511 } else {
Alex Light9fb1ab12017-09-05 09:32:49 -0700512 shadow_frame.SetDexPC(found_dex_pc);
513 if (instrumentation != nullptr && instrumentation->HasExceptionHandledListeners()) {
514 self->ClearException();
515 instrumentation->ExceptionHandledEvent(self, exception.Get());
516 if (UNLIKELY(self->IsExceptionPending())) {
517 // Exception handled event threw an exception. Try to find the handler for this one.
518 return MoveToExceptionHandler(self, shadow_frame, instrumentation);
519 } else if (!clear_exception) {
520 self->SetException(exception.Get());
521 }
522 } else if (clear_exception) {
Ian Rogers54874942014-06-10 16:31:03 -0700523 self->ClearException();
524 }
Alex Light9fb1ab12017-09-05 09:32:49 -0700525 return true;
Ian Rogers54874942014-06-10 16:31:03 -0700526 }
Ian Rogers54874942014-06-10 16:31:03 -0700527}
528
Ian Rogerse94652f2014-12-02 11:13:19 -0800529void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
530 LOG(FATAL) << "Unexpected instruction: "
531 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
532 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700533}
534
Sebastien Hertz45b15972015-04-03 16:07:05 +0200535void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700536 va_list args;
537 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200538 AbortTransactionV(self, fmt, args);
539 va_end(args);
540}
541
542void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
543 CHECK(Runtime::Current()->IsActiveTransaction());
544 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100545 std::string abort_msg;
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800546 android::base::StringAppendV(&abort_msg, fmt, args);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100547 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200548 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700549}
550
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100551// START DECLARATIONS :
552//
553// These additional declarations are required because clang complains
554// about ALWAYS_INLINE (-Werror, -Wgcc-compat) in definitions.
555//
556
Narayan Kamath9823e782016-08-03 12:46:58 +0100557template <bool is_range, bool do_assignability_check>
Vladimir Markod16363a2017-02-01 14:09:13 +0000558static ALWAYS_INLINE bool DoCallCommon(ArtMethod* called_method,
559 Thread* self,
560 ShadowFrame& shadow_frame,
561 JValue* result,
562 uint16_t number_of_inputs,
563 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
564 uint32_t vregC) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100565
566template <bool is_range>
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000567ALWAYS_INLINE void CopyRegisters(ShadowFrame& caller_frame,
568 ShadowFrame* callee_frame,
569 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
570 const size_t first_src_reg,
571 const size_t first_dest_reg,
572 const size_t num_regs) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100573
574// END DECLARATIONS.
575
Siva Chandra05d24152016-01-05 17:43:17 -0800576void ArtInterpreterToCompiledCodeBridge(Thread* self,
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100577 ArtMethod* caller,
Siva Chandra05d24152016-01-05 17:43:17 -0800578 ShadowFrame* shadow_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -0700579 uint16_t arg_offset,
Siva Chandra05d24152016-01-05 17:43:17 -0800580 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700581 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700582 ArtMethod* method = shadow_frame->GetMethod();
583 // Ensure static methods are initialized.
584 if (method->IsStatic()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700585 ObjPtr<mirror::Class> declaringClass = method->GetDeclaringClass();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700586 if (UNLIKELY(!declaringClass->IsInitialized())) {
587 self->PushShadowFrame(shadow_frame);
588 StackHandleScope<1> hs(self);
589 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
Nicolas Geoffray01822292017-03-09 09:03:19 +0000590 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
591 true))) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700592 self->PopShadowFrame();
593 DCHECK(self->IsExceptionPending());
594 return;
595 }
596 self->PopShadowFrame();
597 CHECK(h_class->IsInitializing());
Nicolas Geoffray01822292017-03-09 09:03:19 +0000598 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
599 method = shadow_frame->GetMethod();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700600 }
601 }
Jeff Hao5ea84132017-05-05 16:59:29 -0700602 // Basic checks for the arg_offset. If there's no code item, the arg_offset must be 0. Otherwise,
603 // check that the arg_offset isn't greater than the number of registers. A stronger check is
604 // difficult since the frame may contain space for all the registers in the method, or only enough
605 // space for the arguments.
606 if (kIsDebugBuild) {
607 if (method->GetCodeItem() == nullptr) {
608 DCHECK_EQ(0u, arg_offset) << method->PrettyMethod();
609 } else {
610 DCHECK_LE(arg_offset, shadow_frame->NumberOfVRegs());
611 }
612 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100613 jit::Jit* jit = Runtime::Current()->GetJit();
614 if (jit != nullptr && caller != nullptr) {
615 jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
616 }
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700617 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
618 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
Andreas Gampe542451c2016-07-26 09:02:02 -0700619 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700620}
621
Mingyao Yangffedec52016-05-19 10:48:40 -0700622void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
623 uint16_t this_obj_vreg,
624 JValue result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700625 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700626 ObjPtr<mirror::Object> existing = shadow_frame->GetVRegReference(this_obj_vreg);
Mingyao Yangffedec52016-05-19 10:48:40 -0700627 if (existing == nullptr) {
628 // If it's null, we come from compiled code that was deoptimized. Nothing to do,
629 // as the compiler verified there was no alias.
630 // Set the new string result of the StringFactory.
631 shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
632 return;
633 }
634 // Set the string init result into all aliases.
635 for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
636 if (shadow_frame->GetVRegReference(i) == existing) {
637 DCHECK_EQ(shadow_frame->GetVRegReference(i),
Vladimir Marko78baed52018-10-11 10:44:58 +0100638 reinterpret_cast32<mirror::Object*>(shadow_frame->GetVReg(i)));
Mingyao Yangffedec52016-05-19 10:48:40 -0700639 shadow_frame->SetVRegReference(i, result.GetL());
640 DCHECK_EQ(shadow_frame->GetVRegReference(i),
Vladimir Marko78baed52018-10-11 10:44:58 +0100641 reinterpret_cast32<mirror::Object*>(shadow_frame->GetVReg(i)));
Mingyao Yangffedec52016-05-19 10:48:40 -0700642 }
643 }
644}
645
Orion Hodsonc069a302017-01-18 09:23:12 +0000646template<bool is_range>
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100647static bool DoMethodHandleInvokeCommon(Thread* self,
648 ShadowFrame& shadow_frame,
649 bool invoke_exact,
650 const Instruction* inst,
651 uint16_t inst_data,
652 JValue* result)
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100653 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Light848574c2017-09-25 16:59:39 -0700654 // Make sure to check for async exceptions
655 if (UNLIKELY(self->ObserveAsyncException())) {
656 return false;
657 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100658 // Invoke-polymorphic instructions always take a receiver. i.e, they are never static.
659 const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc();
Orion Hodson3d617ac2016-10-19 14:00:46 +0100660 const int invoke_method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc();
Narayan Kamath9823e782016-08-03 12:46:58 +0100661
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000662 // Initialize |result| to 0 as this is the default return value for
663 // polymorphic invocations of method handle types with void return
664 // and provides sane return result in error cases.
665 result->SetJ(0);
666
Orion Hodson3d617ac2016-10-19 14:00:46 +0100667 // The invoke_method_idx here is the name of the signature polymorphic method that
Narayan Kamath9823e782016-08-03 12:46:58 +0100668 // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact)
669 // and not the method that we'll dispatch to in the end.
Orion Hodsone7732be2017-10-11 14:35:20 +0100670 StackHandleScope<2> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +0000671 Handle<mirror::MethodHandle> method_handle(hs.NewHandle(
672 ObjPtr<mirror::MethodHandle>::DownCast(
Mathieu Chartieref41db72016-10-25 15:08:01 -0700673 MakeObjPtr(shadow_frame.GetVRegReference(vRegC)))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800674 if (UNLIKELY(method_handle == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100675 // Note that the invoke type is kVirtual here because a call to a signature
676 // polymorphic method is shaped like a virtual call at the bytecode level.
Orion Hodson3d617ac2016-10-19 14:00:46 +0100677 ThrowNullPointerExceptionForMethodAccess(invoke_method_idx, InvokeType::kVirtual);
Narayan Kamath9823e782016-08-03 12:46:58 +0100678 return false;
679 }
680
681 // The vRegH value gives the index of the proto_id associated with this
Orion Hodson811bd5f2016-12-07 11:35:37 +0000682 // signature polymorphic call site.
Orion Hodson06d10a72018-05-14 08:53:38 +0100683 const uint16_t vRegH = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
684 const dex::ProtoIndex callsite_proto_id(vRegH);
Narayan Kamath9823e782016-08-03 12:46:58 +0100685
686 // Call through to the classlinker and ask it to resolve the static type associated
687 // with the callsite. This information is stored in the dex cache so it's
688 // guaranteed to be fast after the first resolution.
Narayan Kamath9823e782016-08-03 12:46:58 +0100689 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Orion Hodsone7732be2017-10-11 14:35:20 +0100690 Handle<mirror::MethodType> callsite_type(hs.NewHandle(
691 class_linker->ResolveMethodType(self, callsite_proto_id, shadow_frame.GetMethod())));
Narayan Kamath9823e782016-08-03 12:46:58 +0100692
693 // This implies we couldn't resolve one or more types in this method handle.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800694 if (UNLIKELY(callsite_type == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100695 CHECK(self->IsExceptionPending());
Narayan Kamath9823e782016-08-03 12:46:58 +0100696 return false;
697 }
698
Orion Hodson811bd5f2016-12-07 11:35:37 +0000699 // There is a common dispatch method for method handles that takes
700 // arguments either from a range or an array of arguments depending
701 // on whether the DEX instruction is invoke-polymorphic/range or
702 // invoke-polymorphic. The array here is for the latter.
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100703 if (UNLIKELY(is_range)) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000704 // VRegC is the register holding the method handle. Arguments passed
705 // to the method handle's target do not include the method handle.
Orion Hodson960d4f72017-11-10 15:32:38 +0000706 RangeInstructionOperands operands(inst->VRegC_4rcc() + 1, inst->VRegA_4rcc() - 1);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100707 if (invoke_exact) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000708 return MethodHandleInvokeExact(self,
709 shadow_frame,
710 method_handle,
711 callsite_type,
712 &operands,
713 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100714 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000715 return MethodHandleInvoke(self,
716 shadow_frame,
717 method_handle,
718 callsite_type,
719 &operands,
720 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100721 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100722 } else {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000723 // Get the register arguments for the invoke.
Orion Hodson960d4f72017-11-10 15:32:38 +0000724 uint32_t args[Instruction::kMaxVarArgRegs] = {};
Orion Hodson811bd5f2016-12-07 11:35:37 +0000725 inst->GetVarArgs(args, inst_data);
726 // Drop the first register which is the method handle performing the invoke.
Alexey Frunze8631a462017-01-19 19:07:37 -0800727 memmove(args, args + 1, sizeof(args[0]) * (Instruction::kMaxVarArgRegs - 1));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000728 args[Instruction::kMaxVarArgRegs - 1] = 0;
Orion Hodson960d4f72017-11-10 15:32:38 +0000729 VarArgsInstructionOperands operands(args, inst->VRegA_45cc() - 1);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100730 if (invoke_exact) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000731 return MethodHandleInvokeExact(self,
732 shadow_frame,
733 method_handle,
734 callsite_type,
735 &operands,
736 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100737 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000738 return MethodHandleInvoke(self,
739 shadow_frame,
740 method_handle,
741 callsite_type,
742 &operands,
743 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100744 }
745 }
746}
747
748bool DoMethodHandleInvokeExact(Thread* self,
749 ShadowFrame& shadow_frame,
750 const Instruction* inst,
751 uint16_t inst_data,
752 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
753 if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) {
754 static const bool kIsRange = false;
755 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700756 self, shadow_frame, /* invoke_exact= */ true, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100757 } else {
758 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE);
759 static const bool kIsRange = true;
760 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700761 self, shadow_frame, /* invoke_exact= */ true, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100762 }
763}
764
765bool DoMethodHandleInvoke(Thread* self,
766 ShadowFrame& shadow_frame,
767 const Instruction* inst,
768 uint16_t inst_data,
769 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
770 if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) {
771 static const bool kIsRange = false;
772 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700773 self, shadow_frame, /* invoke_exact= */ false, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100774 } else {
775 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE);
776 static const bool kIsRange = true;
777 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700778 self, shadow_frame, /* invoke_exact= */ false, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100779 }
780}
781
Orion Hodson928033d2018-02-07 05:30:54 +0000782static bool DoVarHandleInvokeCommon(Thread* self,
783 ShadowFrame& shadow_frame,
784 const Instruction* inst,
785 uint16_t inst_data,
786 JValue* result,
787 mirror::VarHandle::AccessMode access_mode)
788 REQUIRES_SHARED(Locks::mutator_lock_) {
789 // Make sure to check for async exceptions
790 if (UNLIKELY(self->ObserveAsyncException())) {
791 return false;
792 }
793
Orion Hodson928033d2018-02-07 05:30:54 +0000794 StackHandleScope<2> hs(self);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100795 bool is_var_args = inst->HasVarArgs();
Orion Hodson06d10a72018-05-14 08:53:38 +0100796 const uint16_t vRegH = is_var_args ? inst->VRegH_45cc() : inst->VRegH_4rcc();
Orion Hodson928033d2018-02-07 05:30:54 +0000797 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
798 Handle<mirror::MethodType> callsite_type(hs.NewHandle(
Orion Hodson06d10a72018-05-14 08:53:38 +0100799 class_linker->ResolveMethodType(self, dex::ProtoIndex(vRegH), shadow_frame.GetMethod())));
Orion Hodson928033d2018-02-07 05:30:54 +0000800 // This implies we couldn't resolve one or more types in this VarHandle.
801 if (UNLIKELY(callsite_type == nullptr)) {
802 CHECK(self->IsExceptionPending());
803 return false;
804 }
805
Orion Hodson537a4fe2018-05-15 13:57:58 +0100806 const uint32_t vRegC = is_var_args ? inst->VRegC_45cc() : inst->VRegC_4rcc();
807 ObjPtr<mirror::Object> receiver(shadow_frame.GetVRegReference(vRegC));
808 Handle<mirror::VarHandle> var_handle(hs.NewHandle(down_cast<mirror::VarHandle*>(receiver.Ptr())));
Orion Hodson928033d2018-02-07 05:30:54 +0000809 if (is_var_args) {
810 uint32_t args[Instruction::kMaxVarArgRegs];
811 inst->GetVarArgs(args, inst_data);
812 VarArgsInstructionOperands all_operands(args, inst->VRegA_45cc());
813 NoReceiverInstructionOperands operands(&all_operands);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100814 return VarHandleInvokeAccessor(self,
815 shadow_frame,
816 var_handle,
817 callsite_type,
818 access_mode,
819 &operands,
820 result);
Orion Hodson928033d2018-02-07 05:30:54 +0000821 } else {
822 RangeInstructionOperands all_operands(inst->VRegC_4rcc(), inst->VRegA_4rcc());
823 NoReceiverInstructionOperands operands(&all_operands);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100824 return VarHandleInvokeAccessor(self,
825 shadow_frame,
826 var_handle,
827 callsite_type,
828 access_mode,
829 &operands,
830 result);
Orion Hodson928033d2018-02-07 05:30:54 +0000831 }
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100832}
833
Orion Hodson928033d2018-02-07 05:30:54 +0000834#define DO_VAR_HANDLE_ACCESSOR(_access_mode) \
835bool DoVarHandle ## _access_mode(Thread* self, \
836 ShadowFrame& shadow_frame, \
837 const Instruction* inst, \
838 uint16_t inst_data, \
839 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { \
840 const auto access_mode = mirror::VarHandle::AccessMode::k ## _access_mode; \
841 return DoVarHandleInvokeCommon(self, shadow_frame, inst, inst_data, result, access_mode); \
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100842}
843
Orion Hodson928033d2018-02-07 05:30:54 +0000844DO_VAR_HANDLE_ACCESSOR(CompareAndExchange)
845DO_VAR_HANDLE_ACCESSOR(CompareAndExchangeAcquire)
846DO_VAR_HANDLE_ACCESSOR(CompareAndExchangeRelease)
847DO_VAR_HANDLE_ACCESSOR(CompareAndSet)
848DO_VAR_HANDLE_ACCESSOR(Get)
849DO_VAR_HANDLE_ACCESSOR(GetAcquire)
850DO_VAR_HANDLE_ACCESSOR(GetAndAdd)
851DO_VAR_HANDLE_ACCESSOR(GetAndAddAcquire)
852DO_VAR_HANDLE_ACCESSOR(GetAndAddRelease)
853DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAnd)
854DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAndAcquire)
855DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAndRelease)
856DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOr)
857DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOrAcquire)
858DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOrRelease)
859DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXor)
860DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXorAcquire)
861DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXorRelease)
862DO_VAR_HANDLE_ACCESSOR(GetAndSet)
863DO_VAR_HANDLE_ACCESSOR(GetAndSetAcquire)
864DO_VAR_HANDLE_ACCESSOR(GetAndSetRelease)
865DO_VAR_HANDLE_ACCESSOR(GetOpaque)
866DO_VAR_HANDLE_ACCESSOR(GetVolatile)
867DO_VAR_HANDLE_ACCESSOR(Set)
868DO_VAR_HANDLE_ACCESSOR(SetOpaque)
869DO_VAR_HANDLE_ACCESSOR(SetRelease)
870DO_VAR_HANDLE_ACCESSOR(SetVolatile)
871DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSet)
872DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetAcquire)
873DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetPlain)
874DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetRelease)
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100875
Orion Hodson928033d2018-02-07 05:30:54 +0000876#undef DO_VAR_HANDLE_ACCESSOR
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100877
878template<bool is_range>
879bool DoInvokePolymorphic(Thread* self,
880 ShadowFrame& shadow_frame,
881 const Instruction* inst,
882 uint16_t inst_data,
883 JValue* result) {
884 const int invoke_method_idx = inst->VRegB();
885 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
886 ArtMethod* invoke_method =
887 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
888 self, invoke_method_idx, shadow_frame.GetMethod(), kVirtual);
889
890 // Ensure intrinsic identifiers are initialized.
891 DCHECK(invoke_method->IsIntrinsic());
892
893 // Dispatch based on intrinsic identifier associated with method.
894 switch (static_cast<art::Intrinsics>(invoke_method->GetIntrinsic())) {
895#define CASE_SIGNATURE_POLYMORPHIC_INTRINSIC(Name, ...) \
896 case Intrinsics::k##Name: \
897 return Do ## Name(self, shadow_frame, inst, inst_data, result);
898#include "intrinsics_list.h"
899 SIGNATURE_POLYMORPHIC_INTRINSICS_LIST(CASE_SIGNATURE_POLYMORPHIC_INTRINSIC)
900#undef INTRINSICS_LIST
901#undef SIGNATURE_POLYMORPHIC_INTRINSICS_LIST
902#undef CASE_SIGNATURE_POLYMORPHIC_INTRINSIC
903 default:
904 LOG(FATAL) << "Unreachable: " << invoke_method->GetIntrinsic();
905 UNREACHABLE();
906 return false;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100907 }
908}
909
Orion Hodsona5dca522018-02-27 12:42:11 +0000910static JValue ConvertScalarBootstrapArgument(jvalue value) {
911 // value either contains a primitive scalar value if it corresponds
912 // to a primitive type, or it contains an integer value if it
913 // corresponds to an object instance reference id (e.g. a string id).
914 return JValue::FromPrimitive(value.j);
915}
916
917static ObjPtr<mirror::Class> GetClassForBootstrapArgument(EncodedArrayValueIterator::ValueType type)
918 REQUIRES_SHARED(Locks::mutator_lock_) {
919 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100920 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = class_linker->GetClassRoots();
Orion Hodsona5dca522018-02-27 12:42:11 +0000921 switch (type) {
922 case EncodedArrayValueIterator::ValueType::kBoolean:
923 case EncodedArrayValueIterator::ValueType::kByte:
924 case EncodedArrayValueIterator::ValueType::kChar:
925 case EncodedArrayValueIterator::ValueType::kShort:
926 // These types are disallowed by JVMS. Treat as integers. This
927 // will result in CCE's being raised if the BSM has one of these
928 // types.
929 case EncodedArrayValueIterator::ValueType::kInt:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100930 return GetClassRoot(ClassRoot::kPrimitiveInt, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000931 case EncodedArrayValueIterator::ValueType::kLong:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100932 return GetClassRoot(ClassRoot::kPrimitiveLong, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000933 case EncodedArrayValueIterator::ValueType::kFloat:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100934 return GetClassRoot(ClassRoot::kPrimitiveFloat, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000935 case EncodedArrayValueIterator::ValueType::kDouble:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100936 return GetClassRoot(ClassRoot::kPrimitiveDouble, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000937 case EncodedArrayValueIterator::ValueType::kMethodType:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100938 return GetClassRoot<mirror::MethodType>(class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000939 case EncodedArrayValueIterator::ValueType::kMethodHandle:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100940 return GetClassRoot<mirror::MethodHandle>(class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000941 case EncodedArrayValueIterator::ValueType::kString:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100942 return GetClassRoot<mirror::String>();
Orion Hodsona5dca522018-02-27 12:42:11 +0000943 case EncodedArrayValueIterator::ValueType::kType:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100944 return GetClassRoot<mirror::Class>();
Orion Hodsona5dca522018-02-27 12:42:11 +0000945 case EncodedArrayValueIterator::ValueType::kField:
946 case EncodedArrayValueIterator::ValueType::kMethod:
947 case EncodedArrayValueIterator::ValueType::kEnum:
948 case EncodedArrayValueIterator::ValueType::kArray:
949 case EncodedArrayValueIterator::ValueType::kAnnotation:
950 case EncodedArrayValueIterator::ValueType::kNull:
951 return nullptr;
952 }
953}
954
955static bool GetArgumentForBootstrapMethod(Thread* self,
956 ArtMethod* referrer,
957 EncodedArrayValueIterator::ValueType type,
958 const JValue* encoded_value,
959 JValue* decoded_value)
960 REQUIRES_SHARED(Locks::mutator_lock_) {
961 // The encoded_value contains either a scalar value (IJDF) or a
962 // scalar DEX file index to a reference type to be materialized.
963 switch (type) {
964 case EncodedArrayValueIterator::ValueType::kInt:
965 case EncodedArrayValueIterator::ValueType::kFloat:
966 decoded_value->SetI(encoded_value->GetI());
967 return true;
968 case EncodedArrayValueIterator::ValueType::kLong:
969 case EncodedArrayValueIterator::ValueType::kDouble:
970 decoded_value->SetJ(encoded_value->GetJ());
971 return true;
972 case EncodedArrayValueIterator::ValueType::kMethodType: {
973 StackHandleScope<2> hs(self);
974 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader()));
975 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
Orion Hodson06d10a72018-05-14 08:53:38 +0100976 dex::ProtoIndex proto_idx(encoded_value->GetC());
Orion Hodsona5dca522018-02-27 12:42:11 +0000977 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Orion Hodson06d10a72018-05-14 08:53:38 +0100978 ObjPtr<mirror::MethodType> o =
979 cl->ResolveMethodType(self, proto_idx, dex_cache, class_loader);
Orion Hodsona5dca522018-02-27 12:42:11 +0000980 if (UNLIKELY(o.IsNull())) {
981 DCHECK(self->IsExceptionPending());
982 return false;
983 }
984 decoded_value->SetL(o);
985 return true;
986 }
987 case EncodedArrayValueIterator::ValueType::kMethodHandle: {
988 uint32_t index = static_cast<uint32_t>(encoded_value->GetI());
989 ClassLinker* cl = Runtime::Current()->GetClassLinker();
990 ObjPtr<mirror::MethodHandle> o = cl->ResolveMethodHandle(self, index, referrer);
991 if (UNLIKELY(o.IsNull())) {
992 DCHECK(self->IsExceptionPending());
993 return false;
994 }
995 decoded_value->SetL(o);
996 return true;
997 }
998 case EncodedArrayValueIterator::ValueType::kString: {
Orion Hodsona5dca522018-02-27 12:42:11 +0000999 dex::StringIndex index(static_cast<uint32_t>(encoded_value->GetI()));
1000 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Vladimir Marko18090d12018-06-01 16:53:12 +01001001 ObjPtr<mirror::String> o = cl->ResolveString(index, referrer);
Orion Hodsona5dca522018-02-27 12:42:11 +00001002 if (UNLIKELY(o.IsNull())) {
1003 DCHECK(self->IsExceptionPending());
1004 return false;
1005 }
1006 decoded_value->SetL(o);
1007 return true;
1008 }
1009 case EncodedArrayValueIterator::ValueType::kType: {
Orion Hodsona5dca522018-02-27 12:42:11 +00001010 dex::TypeIndex index(static_cast<uint32_t>(encoded_value->GetI()));
1011 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Vladimir Marko09c5ca42018-05-31 15:15:31 +01001012 ObjPtr<mirror::Class> o = cl->ResolveType(index, referrer);
Orion Hodsona5dca522018-02-27 12:42:11 +00001013 if (UNLIKELY(o.IsNull())) {
1014 DCHECK(self->IsExceptionPending());
1015 return false;
1016 }
1017 decoded_value->SetL(o);
1018 return true;
1019 }
1020 case EncodedArrayValueIterator::ValueType::kBoolean:
1021 case EncodedArrayValueIterator::ValueType::kByte:
1022 case EncodedArrayValueIterator::ValueType::kChar:
1023 case EncodedArrayValueIterator::ValueType::kShort:
1024 case EncodedArrayValueIterator::ValueType::kField:
1025 case EncodedArrayValueIterator::ValueType::kMethod:
1026 case EncodedArrayValueIterator::ValueType::kEnum:
1027 case EncodedArrayValueIterator::ValueType::kArray:
1028 case EncodedArrayValueIterator::ValueType::kAnnotation:
1029 case EncodedArrayValueIterator::ValueType::kNull:
1030 // Unreachable - unsupported types that have been checked when
1031 // determining the effect call site type based on the bootstrap
1032 // argument types.
1033 UNREACHABLE();
1034 }
1035}
1036
1037static bool PackArgumentForBootstrapMethod(Thread* self,
1038 ArtMethod* referrer,
1039 CallSiteArrayValueIterator* it,
1040 ShadowFrameSetter* setter)
1041 REQUIRES_SHARED(Locks::mutator_lock_) {
1042 auto type = it->GetValueType();
1043 const JValue encoded_value = ConvertScalarBootstrapArgument(it->GetJavaValue());
1044 JValue decoded_value;
1045 if (!GetArgumentForBootstrapMethod(self, referrer, type, &encoded_value, &decoded_value)) {
1046 return false;
1047 }
1048 switch (it->GetValueType()) {
1049 case EncodedArrayValueIterator::ValueType::kInt:
1050 case EncodedArrayValueIterator::ValueType::kFloat:
1051 setter->Set(static_cast<uint32_t>(decoded_value.GetI()));
1052 return true;
1053 case EncodedArrayValueIterator::ValueType::kLong:
1054 case EncodedArrayValueIterator::ValueType::kDouble:
1055 setter->SetLong(decoded_value.GetJ());
1056 return true;
1057 case EncodedArrayValueIterator::ValueType::kMethodType:
1058 case EncodedArrayValueIterator::ValueType::kMethodHandle:
1059 case EncodedArrayValueIterator::ValueType::kString:
1060 case EncodedArrayValueIterator::ValueType::kType:
1061 setter->SetReference(decoded_value.GetL());
1062 return true;
1063 case EncodedArrayValueIterator::ValueType::kBoolean:
1064 case EncodedArrayValueIterator::ValueType::kByte:
1065 case EncodedArrayValueIterator::ValueType::kChar:
1066 case EncodedArrayValueIterator::ValueType::kShort:
1067 case EncodedArrayValueIterator::ValueType::kField:
1068 case EncodedArrayValueIterator::ValueType::kMethod:
1069 case EncodedArrayValueIterator::ValueType::kEnum:
1070 case EncodedArrayValueIterator::ValueType::kArray:
1071 case EncodedArrayValueIterator::ValueType::kAnnotation:
1072 case EncodedArrayValueIterator::ValueType::kNull:
1073 // Unreachable - unsupported types that have been checked when
1074 // determining the effect call site type based on the bootstrap
1075 // argument types.
1076 UNREACHABLE();
1077 }
1078}
1079
1080static bool PackCollectorArrayForBootstrapMethod(Thread* self,
1081 ArtMethod* referrer,
1082 ObjPtr<mirror::Class> array_type,
1083 int32_t array_length,
1084 CallSiteArrayValueIterator* it,
1085 ShadowFrameSetter* setter)
1086 REQUIRES_SHARED(Locks::mutator_lock_) {
1087 StackHandleScope<1> hs(self);
1088 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1089 JValue decoded_value;
1090
1091#define COLLECT_PRIMITIVE_ARRAY(Descriptor, Type) \
1092 Handle<mirror::Type ## Array> array = \
1093 hs.NewHandle(mirror::Type ## Array::Alloc(self, array_length)); \
1094 if (array.IsNull()) { \
1095 return false; \
1096 } \
1097 for (int32_t i = 0; it->HasNext(); it->Next(), ++i) { \
1098 auto type = it->GetValueType(); \
1099 DCHECK_EQ(type, EncodedArrayValueIterator::ValueType::k ## Type); \
1100 const JValue encoded_value = \
1101 ConvertScalarBootstrapArgument(it->GetJavaValue()); \
1102 GetArgumentForBootstrapMethod(self, \
1103 referrer, \
1104 type, \
1105 &encoded_value, \
1106 &decoded_value); \
1107 array->Set(i, decoded_value.Get ## Descriptor()); \
1108 } \
1109 setter->SetReference(array.Get()); \
1110 return true;
1111
1112#define COLLECT_REFERENCE_ARRAY(T, Type) \
Andreas Gampe584771b2018-10-18 13:22:23 -07001113 Handle<mirror::ObjectArray<T>> array = /* NOLINT */ \
Orion Hodsona5dca522018-02-27 12:42:11 +00001114 hs.NewHandle(mirror::ObjectArray<T>::Alloc(self, \
1115 array_type, \
1116 array_length)); \
1117 if (array.IsNull()) { \
1118 return false; \
1119 } \
1120 for (int32_t i = 0; it->HasNext(); it->Next(), ++i) { \
1121 auto type = it->GetValueType(); \
1122 DCHECK_EQ(type, EncodedArrayValueIterator::ValueType::k ## Type); \
1123 const JValue encoded_value = \
1124 ConvertScalarBootstrapArgument(it->GetJavaValue()); \
1125 if (!GetArgumentForBootstrapMethod(self, \
1126 referrer, \
1127 type, \
1128 &encoded_value, \
1129 &decoded_value)) { \
1130 return false; \
1131 } \
1132 ObjPtr<mirror::Object> o = decoded_value.GetL(); \
1133 if (Runtime::Current()->IsActiveTransaction()) { \
1134 array->Set<true>(i, ObjPtr<T>::DownCast(o)); \
1135 } else { \
1136 array->Set<false>(i, ObjPtr<T>::DownCast(o)); \
1137 } \
1138 } \
1139 setter->SetReference(array.Get()); \
1140 return true;
1141
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001142 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = class_linker->GetClassRoots();
1143 ObjPtr<mirror::Class> component_type = array_type->GetComponentType();
1144 if (component_type == GetClassRoot(ClassRoot::kPrimitiveInt, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001145 COLLECT_PRIMITIVE_ARRAY(I, Int);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001146 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveLong, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001147 COLLECT_PRIMITIVE_ARRAY(J, Long);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001148 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveFloat, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001149 COLLECT_PRIMITIVE_ARRAY(F, Float);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001150 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveDouble, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001151 COLLECT_PRIMITIVE_ARRAY(D, Double);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001152 } else if (component_type == GetClassRoot<mirror::MethodType>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001153 COLLECT_REFERENCE_ARRAY(mirror::MethodType, MethodType);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001154 } else if (component_type == GetClassRoot<mirror::MethodHandle>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001155 COLLECT_REFERENCE_ARRAY(mirror::MethodHandle, MethodHandle);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001156 } else if (component_type == GetClassRoot<mirror::String>(class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001157 COLLECT_REFERENCE_ARRAY(mirror::String, String);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001158 } else if (component_type == GetClassRoot<mirror::Class>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001159 COLLECT_REFERENCE_ARRAY(mirror::Class, Type);
1160 } else {
1161 UNREACHABLE();
1162 }
1163 #undef COLLECT_PRIMITIVE_ARRAY
1164 #undef COLLECT_REFERENCE_ARRAY
1165}
1166
1167static ObjPtr<mirror::MethodType> BuildCallSiteForBootstrapMethod(Thread* self,
1168 const DexFile* dex_file,
1169 uint32_t call_site_idx)
1170 REQUIRES_SHARED(Locks::mutator_lock_) {
1171 const DexFile::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx);
1172 CallSiteArrayValueIterator it(*dex_file, csi);
1173 DCHECK_GE(it.Size(), 1u);
1174
1175 StackHandleScope<2> hs(self);
1176 // Create array for parameter types.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001177 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001178 ObjPtr<mirror::Class> class_array_type =
1179 GetClassRoot<mirror::ObjectArray<mirror::Class>>(class_linker);
Orion Hodsona5dca522018-02-27 12:42:11 +00001180 Handle<mirror::ObjectArray<mirror::Class>> ptypes = hs.NewHandle(
1181 mirror::ObjectArray<mirror::Class>::Alloc(self,
1182 class_array_type,
1183 static_cast<int>(it.Size())));
1184 if (ptypes.IsNull()) {
1185 DCHECK(self->IsExceptionPending());
1186 return nullptr;
1187 }
1188
1189 // Populate the first argument with an instance of j.l.i.MethodHandles.Lookup
1190 // that the runtime will construct.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001191 ptypes->Set(0, GetClassRoot<mirror::MethodHandlesLookup>(class_linker));
Orion Hodsona5dca522018-02-27 12:42:11 +00001192 it.Next();
1193
1194 // The remaining parameter types are derived from the types of
1195 // arguments present in the DEX file.
1196 int index = 1;
1197 while (it.HasNext()) {
1198 ObjPtr<mirror::Class> ptype = GetClassForBootstrapArgument(it.GetValueType());
1199 if (ptype.IsNull()) {
1200 ThrowClassCastException("Unsupported bootstrap argument type");
1201 return nullptr;
1202 }
1203 ptypes->Set(index, ptype);
1204 index++;
1205 it.Next();
1206 }
1207 DCHECK_EQ(static_cast<size_t>(index), it.Size());
1208
1209 // By definition, the return type is always a j.l.i.CallSite.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001210 Handle<mirror::Class> rtype = hs.NewHandle(GetClassRoot<mirror::CallSite>());
Orion Hodsona5dca522018-02-27 12:42:11 +00001211 return mirror::MethodType::Create(self, rtype, ptypes);
1212}
1213
Orion Hodsonc069a302017-01-18 09:23:12 +00001214static ObjPtr<mirror::CallSite> InvokeBootstrapMethod(Thread* self,
1215 ShadowFrame& shadow_frame,
1216 uint32_t call_site_idx)
1217 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001218 StackHandleScope<5> hs(self);
Orion Hodsona5dca522018-02-27 12:42:11 +00001219 // There are three mandatory arguments expected from the call site
1220 // value array in the DEX file: the bootstrap method handle, the
1221 // method name to pass to the bootstrap method, and the method type
1222 // to pass to the bootstrap method.
1223 static constexpr size_t kMandatoryArgumentsCount = 3;
Orion Hodsonc069a302017-01-18 09:23:12 +00001224 ArtMethod* referrer = shadow_frame.GetMethod();
1225 const DexFile* dex_file = referrer->GetDexFile();
1226 const DexFile::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00001227 CallSiteArrayValueIterator it(*dex_file, csi);
Orion Hodsona5dca522018-02-27 12:42:11 +00001228 if (it.Size() < kMandatoryArgumentsCount) {
1229 ThrowBootstrapMethodError("Truncated bootstrap arguments (%zu < %zu)",
1230 it.Size(), kMandatoryArgumentsCount);
1231 return nullptr;
1232 }
1233
1234 if (it.GetValueType() != EncodedArrayValueIterator::ValueType::kMethodHandle) {
1235 ThrowBootstrapMethodError("First bootstrap argument is not a method handle");
1236 return nullptr;
1237 }
1238
1239 uint32_t bsm_index = static_cast<uint32_t>(it.GetJavaValue().i);
1240 it.Next();
1241
Orion Hodsonc069a302017-01-18 09:23:12 +00001242 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Orion Hodsona5dca522018-02-27 12:42:11 +00001243 Handle<mirror::MethodHandle> bsm =
1244 hs.NewHandle(class_linker->ResolveMethodHandle(self, bsm_index, referrer));
1245 if (bsm.IsNull()) {
Orion Hodsonc069a302017-01-18 09:23:12 +00001246 DCHECK(self->IsExceptionPending());
1247 return nullptr;
1248 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001249
Orion Hodsona5dca522018-02-27 12:42:11 +00001250 if (bsm->GetHandleKind() != mirror::MethodHandle::Kind::kInvokeStatic) {
1251 // JLS suggests also accepting constructors. This is currently
1252 // hard as constructor invocations happen via transformers in ART
1253 // today. The constructor would need to be a class derived from java.lang.invoke.CallSite.
1254 ThrowBootstrapMethodError("Unsupported bootstrap method invocation kind");
1255 return nullptr;
1256 }
1257
1258 // Construct the local call site type information based on the 3
1259 // mandatory arguments provided by the runtime and the static arguments
1260 // in the DEX file. We will use these arguments to build a shadow frame.
1261 MutableHandle<mirror::MethodType> call_site_type =
1262 hs.NewHandle(BuildCallSiteForBootstrapMethod(self, dex_file, call_site_idx));
1263 if (call_site_type.IsNull()) {
1264 DCHECK(self->IsExceptionPending());
1265 return nullptr;
1266 }
1267
1268 // Check if this BSM is targeting a variable arity method. If so,
1269 // we'll need to collect the trailing arguments into an array.
1270 Handle<mirror::Array> collector_arguments;
1271 int32_t collector_arguments_length;
1272 if (bsm->GetTargetMethod()->IsVarargs()) {
1273 int number_of_bsm_parameters = bsm->GetMethodType()->GetNumberOfPTypes();
1274 if (number_of_bsm_parameters == 0) {
1275 ThrowBootstrapMethodError("Variable arity BSM does not have any arguments");
1276 return nullptr;
1277 }
1278 Handle<mirror::Class> collector_array_class =
1279 hs.NewHandle(bsm->GetMethodType()->GetPTypes()->Get(number_of_bsm_parameters - 1));
1280 if (!collector_array_class->IsArrayClass()) {
1281 ThrowBootstrapMethodError("Variable arity BSM does not have array as final argument");
1282 return nullptr;
1283 }
1284 // The call site may include no arguments to be collected. In this
1285 // case the number of arguments must be at least the number of BSM
1286 // parameters less the collector array.
1287 if (call_site_type->GetNumberOfPTypes() < number_of_bsm_parameters - 1) {
1288 ThrowWrongMethodTypeException(bsm->GetMethodType(), call_site_type.Get());
1289 return nullptr;
1290 }
1291 // Check all the arguments to be collected match the collector array component type.
1292 for (int i = number_of_bsm_parameters - 1; i < call_site_type->GetNumberOfPTypes(); ++i) {
1293 if (call_site_type->GetPTypes()->Get(i) != collector_array_class->GetComponentType()) {
1294 ThrowClassCastException(collector_array_class->GetComponentType(),
1295 call_site_type->GetPTypes()->Get(i));
1296 return nullptr;
1297 }
1298 }
1299 // Update the call site method type so it now includes the collector array.
1300 int32_t collector_arguments_start = number_of_bsm_parameters - 1;
1301 collector_arguments_length = call_site_type->GetNumberOfPTypes() - number_of_bsm_parameters + 1;
1302 call_site_type.Assign(
1303 mirror::MethodType::CollectTrailingArguments(self,
1304 call_site_type.Get(),
1305 collector_array_class.Get(),
1306 collector_arguments_start));
1307 if (call_site_type.IsNull()) {
1308 DCHECK(self->IsExceptionPending());
1309 return nullptr;
1310 }
1311 } else {
1312 collector_arguments_length = 0;
1313 }
1314
1315 if (call_site_type->GetNumberOfPTypes() != bsm->GetMethodType()->GetNumberOfPTypes()) {
1316 ThrowWrongMethodTypeException(bsm->GetMethodType(), call_site_type.Get());
1317 return nullptr;
1318 }
1319
1320 // BSM invocation has a different set of exceptions that
1321 // j.l.i.MethodHandle.invoke(). Scan arguments looking for CCE
1322 // "opportunities". Unfortunately we cannot just leave this to the
1323 // method handle invocation as this might generate a WMTE.
1324 for (int32_t i = 0; i < call_site_type->GetNumberOfPTypes(); ++i) {
1325 ObjPtr<mirror::Class> from = call_site_type->GetPTypes()->Get(i);
1326 ObjPtr<mirror::Class> to = bsm->GetMethodType()->GetPTypes()->Get(i);
1327 if (!IsParameterTypeConvertible(from, to)) {
1328 ThrowClassCastException(from, to);
1329 return nullptr;
1330 }
1331 }
1332 if (!IsReturnTypeConvertible(call_site_type->GetRType(), bsm->GetMethodType()->GetRType())) {
1333 ThrowClassCastException(bsm->GetMethodType()->GetRType(), call_site_type->GetRType());
1334 return nullptr;
1335 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001336
1337 // Set-up a shadow frame for invoking the bootstrap method handle.
1338 ShadowFrameAllocaUniquePtr bootstrap_frame =
Orion Hodsona5dca522018-02-27 12:42:11 +00001339 CREATE_SHADOW_FRAME(call_site_type->NumberOfVRegs(),
1340 nullptr,
1341 referrer,
1342 shadow_frame.GetDexPC());
Orion Hodsonc069a302017-01-18 09:23:12 +00001343 ScopedStackedShadowFramePusher pusher(
1344 self, bootstrap_frame.get(), StackedShadowFrameType::kShadowFrameUnderConstruction);
Orion Hodsona5dca522018-02-27 12:42:11 +00001345 ShadowFrameSetter setter(bootstrap_frame.get(), 0u);
Orion Hodsonc069a302017-01-18 09:23:12 +00001346
1347 // The first parameter is a MethodHandles lookup instance.
Orion Hodsona5dca522018-02-27 12:42:11 +00001348 Handle<mirror::Class> lookup_class =
1349 hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass());
1350 ObjPtr<mirror::MethodHandlesLookup> lookup =
1351 mirror::MethodHandlesLookup::Create(self, lookup_class);
1352 if (lookup.IsNull()) {
Orion Hodsonc069a302017-01-18 09:23:12 +00001353 DCHECK(self->IsExceptionPending());
1354 return nullptr;
1355 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001356 setter.SetReference(lookup);
Orion Hodsonc069a302017-01-18 09:23:12 +00001357
Orion Hodsona5dca522018-02-27 12:42:11 +00001358 // Pack the remaining arguments into the frame.
1359 int number_of_arguments = call_site_type->GetNumberOfPTypes();
1360 int argument_index;
1361 for (argument_index = 1; argument_index < number_of_arguments; ++argument_index) {
1362 if (argument_index == number_of_arguments - 1 &&
1363 call_site_type->GetPTypes()->Get(argument_index)->IsArrayClass()) {
1364 ObjPtr<mirror::Class> array_type = call_site_type->GetPTypes()->Get(argument_index);
1365 if (!PackCollectorArrayForBootstrapMethod(self,
1366 referrer,
1367 array_type,
1368 collector_arguments_length,
1369 &it,
1370 &setter)) {
1371 DCHECK(self->IsExceptionPending());
1372 return nullptr;
Orion Hodsonc069a302017-01-18 09:23:12 +00001373 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001374 } else if (!PackArgumentForBootstrapMethod(self, referrer, &it, &setter)) {
1375 DCHECK(self->IsExceptionPending());
1376 return nullptr;
Orion Hodsonc069a302017-01-18 09:23:12 +00001377 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001378 it.Next();
1379 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001380 DCHECK(!it.HasNext());
1381 DCHECK(setter.Done());
Orion Hodsonc069a302017-01-18 09:23:12 +00001382
1383 // Invoke the bootstrap method handle.
1384 JValue result;
Orion Hodsona5dca522018-02-27 12:42:11 +00001385 RangeInstructionOperands operands(0, bootstrap_frame->NumberOfVRegs());
1386 bool invoke_success = MethodHandleInvoke(self,
1387 *bootstrap_frame,
1388 bsm,
1389 call_site_type,
1390 &operands,
1391 &result);
Orion Hodsonc069a302017-01-18 09:23:12 +00001392 if (!invoke_success) {
1393 DCHECK(self->IsExceptionPending());
1394 return nullptr;
1395 }
1396
1397 Handle<mirror::Object> object(hs.NewHandle(result.GetL()));
Orion Hodsonc069a302017-01-18 09:23:12 +00001398 if (UNLIKELY(object.IsNull())) {
Orion Hodsonda1cdd02018-01-31 18:08:28 +00001399 // This will typically be for LambdaMetafactory which is not supported.
Orion Hodson76e6adb2018-02-23 13:15:55 +00001400 ThrowClassCastException("Bootstrap method returned null");
Orion Hodsonc069a302017-01-18 09:23:12 +00001401 return nullptr;
1402 }
1403
Orion Hodsona5dca522018-02-27 12:42:11 +00001404 // Check the result type is a subclass of j.l.i.CallSite.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001405 ObjPtr<mirror::Class> call_site_class = GetClassRoot<mirror::CallSite>(class_linker);
1406 if (UNLIKELY(!object->InstanceOf(call_site_class))) {
1407 ThrowClassCastException(object->GetClass(), call_site_class);
Orion Hodsonc069a302017-01-18 09:23:12 +00001408 return nullptr;
1409 }
1410
Orion Hodsona5dca522018-02-27 12:42:11 +00001411 // Check the call site target is not null as we're going to invoke it.
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001412 ObjPtr<mirror::CallSite> call_site =
1413 ObjPtr<mirror::CallSite>::DownCast(ObjPtr<mirror::Object>(result.GetL()));
1414 ObjPtr<mirror::MethodHandle> target = call_site->GetTarget();
1415 if (UNLIKELY(target == nullptr)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001416 ThrowClassCastException("Bootstrap method returned a CallSite with a null target");
Orion Hodsonc069a302017-01-18 09:23:12 +00001417 return nullptr;
1418 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001419 return call_site;
Orion Hodsonc069a302017-01-18 09:23:12 +00001420}
1421
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001422namespace {
1423
1424ObjPtr<mirror::CallSite> DoResolveCallSite(Thread* self,
1425 ShadowFrame& shadow_frame,
1426 uint32_t call_site_idx)
1427 REQUIRES_SHARED(Locks::mutator_lock_) {
1428 StackHandleScope<1> hs(self);
1429 Handle<mirror::DexCache> dex_cache(hs.NewHandle(shadow_frame.GetMethod()->GetDexCache()));
1430
1431 // Get the call site from the DexCache if present.
1432 ObjPtr<mirror::CallSite> call_site = dex_cache->GetResolvedCallSite(call_site_idx);
1433 if (LIKELY(call_site != nullptr)) {
1434 return call_site;
1435 }
1436
1437 // Invoke the bootstrap method to get a candidate call site.
1438 call_site = InvokeBootstrapMethod(self, shadow_frame, call_site_idx);
1439 if (UNLIKELY(call_site == nullptr)) {
1440 if (!self->GetException()->IsError()) {
1441 // Use a BootstrapMethodError if the exception is not an instance of java.lang.Error.
1442 ThrowWrappedBootstrapMethodError("Exception from call site #%u bootstrap method",
1443 call_site_idx);
1444 }
1445 return nullptr;
1446 }
1447
1448 // Attempt to place the candidate call site into the DexCache, return the winning call site.
1449 return dex_cache->SetResolvedCallSite(call_site_idx, call_site);
1450}
1451
1452} // namespace
1453
Orion Hodsonc069a302017-01-18 09:23:12 +00001454bool DoInvokeCustom(Thread* self,
1455 ShadowFrame& shadow_frame,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001456 uint32_t call_site_idx,
1457 const InstructionOperands* operands,
1458 JValue* result) {
Alex Light848574c2017-09-25 16:59:39 -07001459 // Make sure to check for async exceptions
1460 if (UNLIKELY(self->ObserveAsyncException())) {
1461 return false;
1462 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001463
Orion Hodsonc069a302017-01-18 09:23:12 +00001464 // invoke-custom is not supported in transactions. In transactions
1465 // there is a limited set of types supported. invoke-custom allows
1466 // running arbitrary code and instantiating arbitrary types.
1467 CHECK(!Runtime::Current()->IsActiveTransaction());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001468
1469 ObjPtr<mirror::CallSite> call_site = DoResolveCallSite(self, shadow_frame, call_site_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00001470 if (call_site.IsNull()) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001471 DCHECK(self->IsExceptionPending());
1472 return false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001473 }
1474
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001475 StackHandleScope<2> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +00001476 Handle<mirror::MethodHandle> target = hs.NewHandle(call_site->GetTarget());
1477 Handle<mirror::MethodType> target_method_type = hs.NewHandle(target->GetMethodType());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001478 DCHECK_EQ(operands->GetNumberOfOperands(), target_method_type->NumberOfVRegs())
1479 << " call_site_idx" << call_site_idx;
1480 return MethodHandleInvokeExact(self,
1481 shadow_frame,
1482 target,
1483 target_method_type,
1484 operands,
1485 result);
Orion Hodsonc069a302017-01-18 09:23:12 +00001486}
1487
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001488// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
1489static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
1490 size_t dest_reg, size_t src_reg)
1491 REQUIRES_SHARED(Locks::mutator_lock_) {
1492 // Uint required, so that sign extension does not make this wrong on 64b systems
1493 uint32_t src_value = shadow_frame.GetVReg(src_reg);
1494 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
1495
1496 // If both register locations contains the same value, the register probably holds a reference.
1497 // Note: As an optimization, non-moving collectors leave a stale reference value
1498 // in the references array even after the original vreg was overwritten to a non-reference.
Vladimir Marko78baed52018-10-11 10:44:58 +01001499 if (src_value == reinterpret_cast32<uint32_t>(o.Ptr())) {
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001500 new_shadow_frame->SetVRegReference(dest_reg, o);
1501 } else {
1502 new_shadow_frame->SetVReg(dest_reg, src_value);
1503 }
1504}
1505
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001506template <bool is_range>
1507inline void CopyRegisters(ShadowFrame& caller_frame,
1508 ShadowFrame* callee_frame,
1509 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
1510 const size_t first_src_reg,
1511 const size_t first_dest_reg,
1512 const size_t num_regs) {
1513 if (is_range) {
1514 const size_t dest_reg_bound = first_dest_reg + num_regs;
1515 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound;
1516 ++dest_reg, ++src_reg) {
1517 AssignRegister(callee_frame, caller_frame, dest_reg, src_reg);
1518 }
1519 } else {
1520 DCHECK_LE(num_regs, arraysize(arg));
1521
1522 for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) {
1523 AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]);
1524 }
1525 }
1526}
1527
Igor Murashkin6918bf12015-09-27 19:19:06 -07001528template <bool is_range,
Narayan Kamath370423d2016-10-03 16:51:22 +01001529 bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001530static inline bool DoCallCommon(ArtMethod* called_method,
1531 Thread* self,
1532 ShadowFrame& shadow_frame,
1533 JValue* result,
1534 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +01001535 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -07001536 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001537 bool string_init = false;
1538 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001539 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
1540 && called_method->IsConstructor())) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001541 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
Jeff Hao848f70a2014-01-15 13:49:50 -08001542 string_init = true;
1543 }
1544
Alex Lightdaf58c82016-03-16 23:00:49 +00001545 // Compute method information.
David Sehr0225f8e2018-01-31 08:52:24 +00001546 CodeItemDataAccessor accessor(called_method->DexInstructionData());
Igor Murashkin158f35c2015-06-10 15:55:30 -07001547 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001548 uint16_t num_regs;
Jeff Hao5ea84132017-05-05 16:59:29 -07001549 // Test whether to use the interpreter or compiler entrypoint, and save that result to pass to
1550 // PerformCall. A deoptimization could occur at any time, and we shouldn't change which
1551 // entrypoint to use once we start building the shadow frame.
Mathieu Chartier448bbcf2017-07-06 12:05:13 -07001552
1553 // For unstarted runtimes, always use the interpreter entrypoint. This fixes the case where we are
1554 // doing cross compilation. Note that GetEntryPointFromQuickCompiledCode doesn't use the image
1555 // pointer size here and this may case an overflow if it is called from the compiler. b/62402160
1556 const bool use_interpreter_entrypoint = !Runtime::Current()->IsStarted() ||
1557 ClassLinker::ShouldUseInterpreterEntrypoint(
1558 called_method,
1559 called_method->GetEntryPointFromQuickCompiledCode());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001560 if (LIKELY(accessor.HasCodeItem())) {
Jeff Hao5ea84132017-05-05 16:59:29 -07001561 // When transitioning to compiled code, space only needs to be reserved for the input registers.
1562 // The rest of the frame gets discarded. This also prevents accessing the called method's code
1563 // item, saving memory by keeping code items of compiled code untouched.
Mathieu Chartier448bbcf2017-07-06 12:05:13 -07001564 if (!use_interpreter_entrypoint) {
1565 DCHECK(!Runtime::Current()->IsAotCompiler()) << "Compiler should use interpreter entrypoint";
Jeff Hao5ea84132017-05-05 16:59:29 -07001566 num_regs = number_of_inputs;
1567 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001568 num_regs = accessor.RegistersSize();
1569 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, accessor.InsSize());
Jeff Hao5ea84132017-05-05 16:59:29 -07001570 }
Nicolas Geoffray01822292017-03-09 09:03:19 +00001571 } else {
1572 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1573 num_regs = number_of_inputs;
Jeff Haodf79ddb2017-02-27 14:47:06 -08001574 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001575
Igor Murashkin158f35c2015-06-10 15:55:30 -07001576 // Hack for String init:
1577 //
1578 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
1579 // invoke-x StringFactory(a, b, c, ...)
1580 // by effectively dropping the first virtual register from the invoke.
1581 //
1582 // (at this point the ArtMethod has already been replaced,
1583 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +00001584 //
1585 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
1586 // to handle the compiler optimization of replacing `this` with null without
1587 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001588 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -07001589 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001590 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 -07001591
Igor Murashkin158f35c2015-06-10 15:55:30 -07001592 // The new StringFactory call is static and has one fewer argument.
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001593 if (!accessor.HasCodeItem()) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001594 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1595 num_regs--;
1596 } // 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 -07001597 number_of_inputs--;
1598
1599 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -07001600 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001601 arg[i - 1] = arg[i];
1602 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07001603 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001604
1605 // Rewrite the non-var-arg case
1606 vregC++; // Skips the 0th vreg in the range ("this").
1607 }
1608
1609 // Parameter registers go at the end of the shadow frame.
1610 DCHECK_GE(num_regs, number_of_inputs);
1611 size_t first_dest_reg = num_regs - number_of_inputs;
1612 DCHECK_NE(first_dest_reg, (size_t)-1);
1613
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001614 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001615 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -07001616 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -07001617 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -07001618 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001619
Igor Murashkin158f35c2015-06-10 15:55:30 -07001620 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -07001621 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001622 // Slow path.
1623 // We might need to do class loading, which incurs a thread state change to kNative. So
1624 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07001625 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +02001626 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001627 self->EndAssertNoThreadSuspension(old_cause);
1628
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001629 // ArtMethod here is needed to check type information of the call site against the callee.
1630 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
1631 //
1632 // As a special case for proxy methods, which are not dex-backed,
1633 // we have to retrieve type information from the proxy's method
1634 // interface method instead (which is dex backed since proxies are never interfaces).
Andreas Gampe542451c2016-07-26 09:02:02 -07001635 ArtMethod* method =
1636 new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001637
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001638 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001639 // to get the exact type of each reference argument.
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001640 const DexFile::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001641 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001642 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001643
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001644 // Handle receiver apart since it's not part of the shorty.
1645 size_t dest_reg = first_dest_reg;
1646 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001647
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001648 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001649 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001650 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
1651 ++dest_reg;
1652 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -07001653 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001654 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001655
1656 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001657 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001658 // Skip the 0th 'shorty' type since it represents the return type.
1659 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001660 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
1661 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001662 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001663 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001664 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001665 if (do_assignability_check && o != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001666 const dex::TypeIndex type_idx = params->GetTypeItem(shorty_pos).type_idx_;
Vladimir Marko942fd312017-01-16 20:52:19 +00001667 ObjPtr<mirror::Class> arg_type = method->GetDexCache()->GetResolvedType(type_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001668 if (arg_type == nullptr) {
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001669 StackHandleScope<1> hs(self);
1670 // Preserve o since it is used below and GetClassFromTypeIndex may cause thread
1671 // suspension.
1672 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&o);
Vladimir Markob45528c2017-07-27 14:14:28 +01001673 arg_type = method->ResolveClassFromTypeIndex(type_idx);
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001674 if (arg_type == nullptr) {
1675 CHECK(self->IsExceptionPending());
1676 return false;
1677 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001678 }
1679 if (!o->VerifierInstanceOf(arg_type)) {
1680 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -07001681 std::string temp1, temp2;
Orion Hodsonfef06642016-11-25 16:07:11 +00001682 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001683 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -08001684 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -07001685 o->GetClass()->GetDescriptor(&temp1),
1686 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001687 return false;
1688 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001689 }
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001690 new_shadow_frame->SetVRegReference(dest_reg, o);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001691 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -07001692 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001693 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001694 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001695 uint64_t wide_value =
1696 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
1697 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001698 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001699 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001700 ++dest_reg;
1701 ++arg_offset;
1702 break;
1703 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001704 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001705 default:
1706 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
1707 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001708 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001709 }
1710 } else {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001711 if (is_range) {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001712 DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001713 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001714
1715 CopyRegisters<is_range>(shadow_frame,
1716 new_shadow_frame,
1717 arg,
1718 vregC,
1719 first_dest_reg,
1720 number_of_inputs);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001721 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001722 }
1723
Jeff Hao5ea84132017-05-05 16:59:29 -07001724 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001725 accessor,
Jeff Hao5ea84132017-05-05 16:59:29 -07001726 shadow_frame.GetMethod(),
1727 first_dest_reg,
1728 new_shadow_frame,
1729 result,
1730 use_interpreter_entrypoint);
Jeff Hao848f70a2014-01-15 13:49:50 -08001731
1732 if (string_init && !self->IsExceptionPending()) {
Mingyao Yangffedec52016-05-19 10:48:40 -07001733 SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001734 }
1735
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001736 return !self->IsExceptionPending();
1737}
1738
Igor Murashkin158f35c2015-06-10 15:55:30 -07001739template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001740bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
1741 const Instruction* inst, uint16_t inst_data, JValue* result) {
1742 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001743 const uint16_t number_of_inputs =
1744 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001745
1746 // TODO: find a cleaner way to separate non-range and range information without duplicating
1747 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -07001748 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001749 uint32_t vregC = 0;
1750 if (is_range) {
1751 vregC = inst->VRegC_3rc();
1752 } else {
1753 vregC = inst->VRegC_35c();
1754 inst->GetVarArgs(arg, inst_data);
1755 }
1756
1757 return DoCallCommon<is_range, do_assignability_check>(
1758 called_method, self, shadow_frame,
1759 result, number_of_inputs, arg, vregC);
1760}
1761
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001762template <bool is_range, bool do_access_check, bool transaction_active>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001763bool DoFilledNewArray(const Instruction* inst,
1764 const ShadowFrame& shadow_frame,
1765 Thread* self,
1766 JValue* result) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001767 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
1768 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
1769 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
1770 if (!is_range) {
1771 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
1772 CHECK_LE(length, 5);
1773 }
1774 if (UNLIKELY(length < 0)) {
1775 ThrowNegativeArraySizeException(length);
1776 return false;
1777 }
1778 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08001779 ObjPtr<mirror::Class> array_class = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
Mathieu Chartieref41db72016-10-25 15:08:01 -07001780 shadow_frame.GetMethod(),
1781 self,
1782 false,
1783 do_access_check);
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001784 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001785 DCHECK(self->IsExceptionPending());
1786 return false;
1787 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001788 CHECK(array_class->IsArrayClass());
Mathieu Chartieref41db72016-10-25 15:08:01 -07001789 ObjPtr<mirror::Class> component_class = array_class->GetComponentType();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001790 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
1791 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
1792 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001793 ThrowRuntimeException("Bad filled array request for type %s",
David Sehr709b0702016-10-13 09:12:37 -07001794 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001795 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001796 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -08001797 "Found type %s; filled-new-array not implemented for anything but 'int'",
David Sehr709b0702016-10-13 09:12:37 -07001798 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001799 }
1800 return false;
1801 }
Mathieu Chartieref41db72016-10-25 15:08:01 -07001802 ObjPtr<mirror::Object> new_array = mirror::Array::Alloc<true>(
1803 self,
1804 array_class,
1805 length,
1806 array_class->GetComponentSizeShift(),
1807 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001808 if (UNLIKELY(new_array == nullptr)) {
1809 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001810 return false;
1811 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001812 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
1813 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001814 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +01001815 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001816 } else {
Ian Rogers29a26482014-05-02 15:27:29 -07001817 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +01001818 }
Sebastien Hertzabff6432014-01-27 18:01:39 +01001819 for (int32_t i = 0; i < length; ++i) {
1820 size_t src_reg = is_range ? vregC + i : arg[i];
1821 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001822 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
1823 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +01001824 } else {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001825 new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<transaction_active>(
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001826 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001827 }
1828 }
1829
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001830 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001831 return true;
1832}
1833
Mathieu Chartieref41db72016-10-25 15:08:01 -07001834// TODO: Use ObjPtr here.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001835template<typename T>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001836static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array,
1837 int32_t count)
1838 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001839 Runtime* runtime = Runtime::Current();
1840 for (int32_t i = 0; i < count; ++i) {
1841 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
1842 }
1843}
1844
Mathieu Chartieref41db72016-10-25 15:08:01 -07001845void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001846 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001847 DCHECK(Runtime::Current()->IsActiveTransaction());
1848 DCHECK(array != nullptr);
1849 DCHECK_LE(count, array->GetLength());
1850 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
1851 switch (primitive_component_type) {
1852 case Primitive::kPrimBoolean:
1853 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
1854 break;
1855 case Primitive::kPrimByte:
1856 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
1857 break;
1858 case Primitive::kPrimChar:
1859 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
1860 break;
1861 case Primitive::kPrimShort:
1862 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
1863 break;
1864 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001865 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
1866 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001867 case Primitive::kPrimFloat:
1868 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
1869 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001870 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001871 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
1872 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001873 case Primitive::kPrimDouble:
1874 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
1875 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001876 default:
1877 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
1878 << " in fill-array-data";
1879 break;
1880 }
1881}
1882
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001883// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +02001884#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001885 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001886 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
1887 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +02001888 const Instruction* inst, uint16_t inst_data, \
1889 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001890EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
1891EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
1892EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
1893EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
1894#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001895
Orion Hodsonc069a302017-01-18 09:23:12 +00001896// Explicit DoInvokePolymorphic template function declarations.
1897#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range) \
1898 template REQUIRES_SHARED(Locks::mutator_lock_) \
1899 bool DoInvokePolymorphic<_is_range>( \
1900 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1901 uint16_t inst_data, JValue* result)
1902EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false);
1903EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true);
Narayan Kamath9823e782016-08-03 12:46:58 +01001904#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1905
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001906// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001907#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001908 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001909 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
1910 const ShadowFrame& shadow_frame, \
1911 Thread* self, JValue* result)
1912#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
1913 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
1914 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
1915 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
1916 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
1917EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
1918EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
1919#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001920#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
1921
1922} // namespace interpreter
1923} // namespace art