blob: 90e89cf3db5e68374f043477b1376f4d119e0633 [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
Andreas Gampe542451c2016-07-26 09:02:02 -070021#include "base/enums.h"
Vladimir Markoc7aa87e2018-05-24 15:19:52 +010022#include "class_root.h"
Daniel Mihalyieb076692014-08-22 17:33:31 +020023#include "debugger.h"
David Sehr9e734c72018-01-04 17:56:19 -080024#include "dex/dex_file_types.h"
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +000025#include "entrypoints/runtime_asm_entrypoints.h"
Orion Hodson43f0cdb2017-10-10 14:47:32 +010026#include "intrinsics_enum.h"
Tamas Berghammerdd5e5e92016-02-12 16:29:00 +000027#include "jit/jit.h"
Andreas Gampec5b75642018-05-16 15:12:11 -070028#include "jvalue-inl.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010029#include "method_handles-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070030#include "method_handles.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010031#include "mirror/array-inl.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010032#include "mirror/class.h"
Narayan Kamath000e1882016-10-24 17:14:25 +010033#include "mirror/emulated_stack_frame.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080034#include "mirror/method_handle_impl-inl.h"
Orion Hodson928033d2018-02-07 05:30:54 +000035#include "mirror/var_handle.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010036#include "reflection-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070037#include "reflection.h"
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +010038#include "shadow_frame-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070039#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070040#include "thread-inl.h"
Chang Xingbd208d82017-07-12 14:53:17 -070041#include "transaction.h"
Orion Hodson537a4fe2018-05-15 13:57:58 +010042#include "var_handles.h"
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +010043#include "well_known_classes.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020044
45namespace art {
46namespace interpreter {
47
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000048void ThrowNullPointerExceptionFromInterpreter() {
49 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -070050}
51
Chang Xingbd208d82017-07-12 14:53:17 -070052template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
53 bool transaction_active>
Ian Rogers54874942014-06-10 16:31:03 -070054bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
55 uint16_t inst_data) {
56 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
57 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010058 ArtField* f =
59 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
60 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -070061 if (UNLIKELY(f == nullptr)) {
62 CHECK(self->IsExceptionPending());
63 return false;
64 }
Mathieu Chartieref41db72016-10-25 15:08:01 -070065 ObjPtr<mirror::Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -070066 if (is_static) {
67 obj = f->GetDeclaringClass();
Chang Xingbd208d82017-07-12 14:53:17 -070068 if (transaction_active) {
69 if (Runtime::Current()->GetTransaction()->ReadConstraint(obj.Ptr(), f)) {
70 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Can't read static fields of "
71 + obj->PrettyTypeOf() + " since it does not belong to clinit's class.");
72 return false;
73 }
74 }
Ian Rogers54874942014-06-10 16:31:03 -070075 } else {
76 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
77 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000078 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers54874942014-06-10 16:31:03 -070079 return false;
80 }
81 }
Orion Hodson3d617ac2016-10-19 14:00:46 +010082
83 JValue result;
Alex Light084fa372017-06-16 08:58:34 -070084 if (UNLIKELY(!DoFieldGetCommon<field_type>(self, shadow_frame, obj, f, &result))) {
85 // Instrumentation threw an error!
86 CHECK(self->IsExceptionPending());
87 return false;
88 }
Ian Rogers54874942014-06-10 16:31:03 -070089 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
90 switch (field_type) {
91 case Primitive::kPrimBoolean:
Orion Hodson3d617ac2016-10-19 14:00:46 +010092 shadow_frame.SetVReg(vregA, result.GetZ());
Ian Rogers54874942014-06-10 16:31:03 -070093 break;
94 case Primitive::kPrimByte:
Orion Hodson3d617ac2016-10-19 14:00:46 +010095 shadow_frame.SetVReg(vregA, result.GetB());
Ian Rogers54874942014-06-10 16:31:03 -070096 break;
97 case Primitive::kPrimChar:
Orion Hodson3d617ac2016-10-19 14:00:46 +010098 shadow_frame.SetVReg(vregA, result.GetC());
Ian Rogers54874942014-06-10 16:31:03 -070099 break;
100 case Primitive::kPrimShort:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100101 shadow_frame.SetVReg(vregA, result.GetS());
Ian Rogers54874942014-06-10 16:31:03 -0700102 break;
103 case Primitive::kPrimInt:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100104 shadow_frame.SetVReg(vregA, result.GetI());
Ian Rogers54874942014-06-10 16:31:03 -0700105 break;
106 case Primitive::kPrimLong:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100107 shadow_frame.SetVRegLong(vregA, result.GetJ());
Ian Rogers54874942014-06-10 16:31:03 -0700108 break;
109 case Primitive::kPrimNot:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100110 shadow_frame.SetVRegReference(vregA, result.GetL());
Ian Rogers54874942014-06-10 16:31:03 -0700111 break;
112 default:
113 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700114 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700115 }
116 return true;
117}
118
119// Explicitly instantiate all DoFieldGet functions.
Chang Xingbd208d82017-07-12 14:53:17 -0700120#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
121 template bool DoFieldGet<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
Ian Rogers54874942014-06-10 16:31:03 -0700122 ShadowFrame& shadow_frame, \
123 const Instruction* inst, \
124 uint16_t inst_data)
125
126#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
Chang Xingbd208d82017-07-12 14:53:17 -0700127 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false, true); \
128 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false, false); \
129 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true, true); \
130 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true, false);
Ian Rogers54874942014-06-10 16:31:03 -0700131
132// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700133EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
134EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
135EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
136EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
137EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
138EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
139EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700140
141// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700142EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
143EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
144EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
145EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
146EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
147EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
148EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700149
150#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
151#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
152
153// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
154// Returns true on success, otherwise throws an exception and returns false.
155template<Primitive::Type field_type>
156bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700157 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700158 if (UNLIKELY(obj == nullptr)) {
159 // We lost the reference to the field index so we cannot get a more
160 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000161 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700162 return false;
163 }
164 MemberOffset field_offset(inst->VRegC_22c());
165 // Report this field access to instrumentation if needed. Since we only have the offset of
166 // the field from the base of the object, we need to look for it first.
167 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
168 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
169 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
170 field_offset.Uint32Value());
171 DCHECK(f != nullptr);
172 DCHECK(!f->IsStatic());
Alex Light084fa372017-06-16 08:58:34 -0700173 Thread* self = Thread::Current();
174 StackHandleScope<1> hs(self);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700175 // Save obj in case the instrumentation event has thread suspension.
176 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Alex Light084fa372017-06-16 08:58:34 -0700177 instrumentation->FieldReadEvent(self,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700178 obj.Ptr(),
179 shadow_frame.GetMethod(),
180 shadow_frame.GetDexPC(),
181 f);
Alex Light084fa372017-06-16 08:58:34 -0700182 if (UNLIKELY(self->IsExceptionPending())) {
183 return false;
184 }
Ian Rogers54874942014-06-10 16:31:03 -0700185 }
186 // Note: iget-x-quick instructions are only for non-volatile fields.
187 const uint32_t vregA = inst->VRegA_22c(inst_data);
188 switch (field_type) {
189 case Primitive::kPrimInt:
190 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
191 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800192 case Primitive::kPrimBoolean:
193 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
194 break;
195 case Primitive::kPrimByte:
196 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
197 break;
198 case Primitive::kPrimChar:
199 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
200 break;
201 case Primitive::kPrimShort:
202 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
203 break;
Ian Rogers54874942014-06-10 16:31:03 -0700204 case Primitive::kPrimLong:
205 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
206 break;
207 case Primitive::kPrimNot:
208 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
209 break;
210 default:
211 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700212 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700213 }
214 return true;
215}
216
217// Explicitly instantiate all DoIGetQuick functions.
218#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
219 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
220 uint16_t inst_data)
221
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800222EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
223EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
224EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
225EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
226EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
227EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
228EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700229#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
230
231template<Primitive::Type field_type>
232static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700233 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700234 JValue field_value;
235 switch (field_type) {
236 case Primitive::kPrimBoolean:
237 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
238 break;
239 case Primitive::kPrimByte:
240 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
241 break;
242 case Primitive::kPrimChar:
243 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
244 break;
245 case Primitive::kPrimShort:
246 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
247 break;
248 case Primitive::kPrimInt:
249 field_value.SetI(shadow_frame.GetVReg(vreg));
250 break;
251 case Primitive::kPrimLong:
252 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
253 break;
254 case Primitive::kPrimNot:
255 field_value.SetL(shadow_frame.GetVRegReference(vreg));
256 break;
257 default:
258 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700259 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700260 }
261 return field_value;
262}
263
Orion Hodson3d617ac2016-10-19 14:00:46 +0100264template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
265 bool transaction_active>
266bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
267 uint16_t inst_data) {
268 const bool do_assignability_check = do_access_check;
269 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
270 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
271 ArtField* f =
272 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
273 Primitive::ComponentSize(field_type));
274 if (UNLIKELY(f == nullptr)) {
275 CHECK(self->IsExceptionPending());
276 return false;
277 }
278 ObjPtr<mirror::Object> obj;
279 if (is_static) {
280 obj = f->GetDeclaringClass();
Chang Xingbd208d82017-07-12 14:53:17 -0700281 if (transaction_active) {
282 if (Runtime::Current()->GetTransaction()->WriteConstraint(obj.Ptr(), f)) {
283 Runtime::Current()->AbortTransactionAndThrowAbortError(
284 self, "Can't set fields of " + obj->PrettyTypeOf());
285 return false;
286 }
287 }
288
Orion Hodson3d617ac2016-10-19 14:00:46 +0100289 } else {
290 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
291 if (UNLIKELY(obj == nullptr)) {
292 ThrowNullPointerExceptionForFieldAccess(f, false);
293 return false;
294 }
295 }
296
297 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100298 JValue value = GetFieldValue<field_type>(shadow_frame, vregA);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100299 return DoFieldPutCommon<field_type, do_assignability_check, transaction_active>(self,
300 shadow_frame,
301 obj,
302 f,
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100303 value);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100304}
305
Ian Rogers54874942014-06-10 16:31:03 -0700306// Explicitly instantiate all DoFieldPut functions.
307#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
308 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
309 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
310
311#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
312 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
313 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
314 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
315 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
316
317// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700318EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
319EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
320EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
321EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
322EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
323EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
324EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700325
326// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700327EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
328EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
329EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
330EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
331EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
332EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
333EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700334
335#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
336#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
337
338template<Primitive::Type field_type, bool transaction_active>
339bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700340 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700341 if (UNLIKELY(obj == nullptr)) {
342 // We lost the reference to the field index so we cannot get a more
343 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000344 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700345 return false;
346 }
347 MemberOffset field_offset(inst->VRegC_22c());
348 const uint32_t vregA = inst->VRegA_22c(inst_data);
349 // Report this field modification to instrumentation if needed. Since we only have the offset of
350 // the field from the base of the object, we need to look for it first.
351 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
352 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
353 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
354 field_offset.Uint32Value());
355 DCHECK(f != nullptr);
356 DCHECK(!f->IsStatic());
357 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Alex Light084fa372017-06-16 08:58:34 -0700358 Thread* self = Thread::Current();
359 StackHandleScope<2> hs(self);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700360 // Save obj in case the instrumentation event has thread suspension.
361 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Alex Light084fa372017-06-16 08:58:34 -0700362 mirror::Object* fake_root = nullptr;
363 HandleWrapper<mirror::Object> ret(hs.NewHandleWrapper<mirror::Object>(
364 field_type == Primitive::kPrimNot ? field_value.GetGCRoot() : &fake_root));
365 instrumentation->FieldWriteEvent(self,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700366 obj.Ptr(),
367 shadow_frame.GetMethod(),
368 shadow_frame.GetDexPC(),
369 f,
370 field_value);
Alex Light084fa372017-06-16 08:58:34 -0700371 if (UNLIKELY(self->IsExceptionPending())) {
372 return false;
373 }
Ian Rogers54874942014-06-10 16:31:03 -0700374 }
375 // Note: iput-x-quick instructions are only for non-volatile fields.
376 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700377 case Primitive::kPrimBoolean:
378 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
379 break;
380 case Primitive::kPrimByte:
381 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
382 break;
383 case Primitive::kPrimChar:
384 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
385 break;
386 case Primitive::kPrimShort:
387 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
388 break;
Ian Rogers54874942014-06-10 16:31:03 -0700389 case Primitive::kPrimInt:
390 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
391 break;
392 case Primitive::kPrimLong:
393 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
394 break;
395 case Primitive::kPrimNot:
396 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
397 break;
398 default:
399 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700400 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700401 }
402 return true;
403}
404
405// Explicitly instantiate all DoIPutQuick functions.
406#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
407 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
408 const Instruction* inst, \
409 uint16_t inst_data)
410
411#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
412 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
413 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
414
Andreas Gampec8ccf682014-09-29 20:07:43 -0700415EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
416EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
417EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
418EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
419EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
420EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
421EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700422#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
423#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
424
Alex Light9fb1ab12017-09-05 09:32:49 -0700425// We execute any instrumentation events that are triggered by this exception and change the
426// shadow_frame's dex_pc to that of the exception handler if there is one in the current method.
427// Return true if we should continue executing in the current method and false if we need to go up
428// the stack to find an exception handler.
Sebastien Hertz520633b2015-09-08 17:03:36 +0200429// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Alex Light9fb1ab12017-09-05 09:32:49 -0700430// TODO We should have a better way to skip instrumentation reporting or possibly rethink that
431// behavior.
432bool MoveToExceptionHandler(Thread* self,
433 ShadowFrame& shadow_frame,
434 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700435 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700436 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000437 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Alex Light9fb1ab12017-09-05 09:32:49 -0700438 if (instrumentation != nullptr &&
439 instrumentation->HasExceptionThrownListeners() &&
440 self->IsExceptionThrownByCurrentMethod(exception.Get())) {
441 // 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 -0700442 instrumentation->ExceptionThrownEvent(self, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200443 }
Ian Rogers54874942014-06-10 16:31:03 -0700444 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700445 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
Alex Light9fb1ab12017-09-05 09:32:49 -0700446 hs.NewHandle(exception->GetClass()), shadow_frame.GetDexPC(), &clear_exception);
Andreas Gampee2abbc62017-09-15 11:59:26 -0700447 if (found_dex_pc == dex::kDexNoIndex) {
Alex Light9fb1ab12017-09-05 09:32:49 -0700448 if (instrumentation != nullptr) {
449 if (shadow_frame.NeedsNotifyPop()) {
450 instrumentation->WatchedFramePopped(self, shadow_frame);
451 }
452 // Exception is not caught by the current method. We will unwind to the
453 // caller. Notify any instrumentation listener.
454 instrumentation->MethodUnwindEvent(self,
455 shadow_frame.GetThisObject(),
456 shadow_frame.GetMethod(),
457 shadow_frame.GetDexPC());
Alex Lighte814f9d2017-07-31 16:14:39 -0700458 }
Alex Light9fb1ab12017-09-05 09:32:49 -0700459 return false;
Ian Rogers54874942014-06-10 16:31:03 -0700460 } else {
Alex Light9fb1ab12017-09-05 09:32:49 -0700461 shadow_frame.SetDexPC(found_dex_pc);
462 if (instrumentation != nullptr && instrumentation->HasExceptionHandledListeners()) {
463 self->ClearException();
464 instrumentation->ExceptionHandledEvent(self, exception.Get());
465 if (UNLIKELY(self->IsExceptionPending())) {
466 // Exception handled event threw an exception. Try to find the handler for this one.
467 return MoveToExceptionHandler(self, shadow_frame, instrumentation);
468 } else if (!clear_exception) {
469 self->SetException(exception.Get());
470 }
471 } else if (clear_exception) {
Ian Rogers54874942014-06-10 16:31:03 -0700472 self->ClearException();
473 }
Alex Light9fb1ab12017-09-05 09:32:49 -0700474 return true;
Ian Rogers54874942014-06-10 16:31:03 -0700475 }
Ian Rogers54874942014-06-10 16:31:03 -0700476}
477
Ian Rogerse94652f2014-12-02 11:13:19 -0800478void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
479 LOG(FATAL) << "Unexpected instruction: "
480 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
481 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700482}
483
Sebastien Hertz45b15972015-04-03 16:07:05 +0200484void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700485 va_list args;
486 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200487 AbortTransactionV(self, fmt, args);
488 va_end(args);
489}
490
491void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
492 CHECK(Runtime::Current()->IsActiveTransaction());
493 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100494 std::string abort_msg;
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800495 android::base::StringAppendV(&abort_msg, fmt, args);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100496 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200497 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700498}
499
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100500// START DECLARATIONS :
501//
502// These additional declarations are required because clang complains
503// about ALWAYS_INLINE (-Werror, -Wgcc-compat) in definitions.
504//
505
Narayan Kamath9823e782016-08-03 12:46:58 +0100506template <bool is_range, bool do_assignability_check>
Vladimir Markod16363a2017-02-01 14:09:13 +0000507static ALWAYS_INLINE bool DoCallCommon(ArtMethod* called_method,
508 Thread* self,
509 ShadowFrame& shadow_frame,
510 JValue* result,
511 uint16_t number_of_inputs,
512 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
513 uint32_t vregC) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100514
515template <bool is_range>
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000516ALWAYS_INLINE void CopyRegisters(ShadowFrame& caller_frame,
517 ShadowFrame* callee_frame,
518 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
519 const size_t first_src_reg,
520 const size_t first_dest_reg,
521 const size_t num_regs) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100522
523// END DECLARATIONS.
524
Siva Chandra05d24152016-01-05 17:43:17 -0800525void ArtInterpreterToCompiledCodeBridge(Thread* self,
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100526 ArtMethod* caller,
Siva Chandra05d24152016-01-05 17:43:17 -0800527 ShadowFrame* shadow_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -0700528 uint16_t arg_offset,
Siva Chandra05d24152016-01-05 17:43:17 -0800529 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700530 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700531 ArtMethod* method = shadow_frame->GetMethod();
532 // Ensure static methods are initialized.
533 if (method->IsStatic()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700534 ObjPtr<mirror::Class> declaringClass = method->GetDeclaringClass();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700535 if (UNLIKELY(!declaringClass->IsInitialized())) {
536 self->PushShadowFrame(shadow_frame);
537 StackHandleScope<1> hs(self);
538 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
Nicolas Geoffray01822292017-03-09 09:03:19 +0000539 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
540 true))) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700541 self->PopShadowFrame();
542 DCHECK(self->IsExceptionPending());
543 return;
544 }
545 self->PopShadowFrame();
546 CHECK(h_class->IsInitializing());
Nicolas Geoffray01822292017-03-09 09:03:19 +0000547 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
548 method = shadow_frame->GetMethod();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700549 }
550 }
Jeff Hao5ea84132017-05-05 16:59:29 -0700551 // Basic checks for the arg_offset. If there's no code item, the arg_offset must be 0. Otherwise,
552 // check that the arg_offset isn't greater than the number of registers. A stronger check is
553 // difficult since the frame may contain space for all the registers in the method, or only enough
554 // space for the arguments.
555 if (kIsDebugBuild) {
556 if (method->GetCodeItem() == nullptr) {
557 DCHECK_EQ(0u, arg_offset) << method->PrettyMethod();
558 } else {
559 DCHECK_LE(arg_offset, shadow_frame->NumberOfVRegs());
560 }
561 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100562 jit::Jit* jit = Runtime::Current()->GetJit();
563 if (jit != nullptr && caller != nullptr) {
564 jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
565 }
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700566 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
567 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
Andreas Gampe542451c2016-07-26 09:02:02 -0700568 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700569}
570
Mingyao Yangffedec52016-05-19 10:48:40 -0700571void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
572 uint16_t this_obj_vreg,
573 JValue result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700574 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700575 ObjPtr<mirror::Object> existing = shadow_frame->GetVRegReference(this_obj_vreg);
Mingyao Yangffedec52016-05-19 10:48:40 -0700576 if (existing == nullptr) {
577 // If it's null, we come from compiled code that was deoptimized. Nothing to do,
578 // as the compiler verified there was no alias.
579 // Set the new string result of the StringFactory.
580 shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
581 return;
582 }
583 // Set the string init result into all aliases.
584 for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
585 if (shadow_frame->GetVRegReference(i) == existing) {
586 DCHECK_EQ(shadow_frame->GetVRegReference(i),
587 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
588 shadow_frame->SetVRegReference(i, result.GetL());
589 DCHECK_EQ(shadow_frame->GetVRegReference(i),
590 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
591 }
592 }
593}
594
Orion Hodsonc069a302017-01-18 09:23:12 +0000595template<bool is_range>
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100596static bool DoMethodHandleInvokeCommon(Thread* self,
597 ShadowFrame& shadow_frame,
598 bool invoke_exact,
599 const Instruction* inst,
600 uint16_t inst_data,
601 JValue* result)
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100602 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Light848574c2017-09-25 16:59:39 -0700603 // Make sure to check for async exceptions
604 if (UNLIKELY(self->ObserveAsyncException())) {
605 return false;
606 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100607 // Invoke-polymorphic instructions always take a receiver. i.e, they are never static.
608 const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc();
Orion Hodson3d617ac2016-10-19 14:00:46 +0100609 const int invoke_method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc();
Narayan Kamath9823e782016-08-03 12:46:58 +0100610
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000611 // Initialize |result| to 0 as this is the default return value for
612 // polymorphic invocations of method handle types with void return
613 // and provides sane return result in error cases.
614 result->SetJ(0);
615
Orion Hodson3d617ac2016-10-19 14:00:46 +0100616 // The invoke_method_idx here is the name of the signature polymorphic method that
Narayan Kamath9823e782016-08-03 12:46:58 +0100617 // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact)
618 // and not the method that we'll dispatch to in the end.
Orion Hodsone7732be2017-10-11 14:35:20 +0100619 StackHandleScope<2> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +0000620 Handle<mirror::MethodHandle> method_handle(hs.NewHandle(
621 ObjPtr<mirror::MethodHandle>::DownCast(
Mathieu Chartieref41db72016-10-25 15:08:01 -0700622 MakeObjPtr(shadow_frame.GetVRegReference(vRegC)))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800623 if (UNLIKELY(method_handle == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100624 // Note that the invoke type is kVirtual here because a call to a signature
625 // polymorphic method is shaped like a virtual call at the bytecode level.
Orion Hodson3d617ac2016-10-19 14:00:46 +0100626 ThrowNullPointerExceptionForMethodAccess(invoke_method_idx, InvokeType::kVirtual);
Narayan Kamath9823e782016-08-03 12:46:58 +0100627 return false;
628 }
629
630 // The vRegH value gives the index of the proto_id associated with this
Orion Hodson811bd5f2016-12-07 11:35:37 +0000631 // signature polymorphic call site.
Orion Hodson06d10a72018-05-14 08:53:38 +0100632 const uint16_t vRegH = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
633 const dex::ProtoIndex callsite_proto_id(vRegH);
Narayan Kamath9823e782016-08-03 12:46:58 +0100634
635 // Call through to the classlinker and ask it to resolve the static type associated
636 // with the callsite. This information is stored in the dex cache so it's
637 // guaranteed to be fast after the first resolution.
Narayan Kamath9823e782016-08-03 12:46:58 +0100638 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Orion Hodsone7732be2017-10-11 14:35:20 +0100639 Handle<mirror::MethodType> callsite_type(hs.NewHandle(
640 class_linker->ResolveMethodType(self, callsite_proto_id, shadow_frame.GetMethod())));
Narayan Kamath9823e782016-08-03 12:46:58 +0100641
642 // This implies we couldn't resolve one or more types in this method handle.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800643 if (UNLIKELY(callsite_type == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100644 CHECK(self->IsExceptionPending());
Narayan Kamath9823e782016-08-03 12:46:58 +0100645 return false;
646 }
647
Orion Hodson811bd5f2016-12-07 11:35:37 +0000648 // There is a common dispatch method for method handles that takes
649 // arguments either from a range or an array of arguments depending
650 // on whether the DEX instruction is invoke-polymorphic/range or
651 // invoke-polymorphic. The array here is for the latter.
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100652 if (UNLIKELY(is_range)) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000653 // VRegC is the register holding the method handle. Arguments passed
654 // to the method handle's target do not include the method handle.
Orion Hodson960d4f72017-11-10 15:32:38 +0000655 RangeInstructionOperands operands(inst->VRegC_4rcc() + 1, inst->VRegA_4rcc() - 1);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100656 if (invoke_exact) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000657 return MethodHandleInvokeExact(self,
658 shadow_frame,
659 method_handle,
660 callsite_type,
661 &operands,
662 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100663 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000664 return MethodHandleInvoke(self,
665 shadow_frame,
666 method_handle,
667 callsite_type,
668 &operands,
669 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100670 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100671 } else {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000672 // Get the register arguments for the invoke.
Orion Hodson960d4f72017-11-10 15:32:38 +0000673 uint32_t args[Instruction::kMaxVarArgRegs] = {};
Orion Hodson811bd5f2016-12-07 11:35:37 +0000674 inst->GetVarArgs(args, inst_data);
675 // Drop the first register which is the method handle performing the invoke.
Alexey Frunze8631a462017-01-19 19:07:37 -0800676 memmove(args, args + 1, sizeof(args[0]) * (Instruction::kMaxVarArgRegs - 1));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000677 args[Instruction::kMaxVarArgRegs - 1] = 0;
Orion Hodson960d4f72017-11-10 15:32:38 +0000678 VarArgsInstructionOperands operands(args, inst->VRegA_45cc() - 1);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100679 if (invoke_exact) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000680 return MethodHandleInvokeExact(self,
681 shadow_frame,
682 method_handle,
683 callsite_type,
684 &operands,
685 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100686 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000687 return MethodHandleInvoke(self,
688 shadow_frame,
689 method_handle,
690 callsite_type,
691 &operands,
692 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100693 }
694 }
695}
696
697bool DoMethodHandleInvokeExact(Thread* self,
698 ShadowFrame& shadow_frame,
699 const Instruction* inst,
700 uint16_t inst_data,
701 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
702 if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) {
703 static const bool kIsRange = false;
704 return DoMethodHandleInvokeCommon<kIsRange>(
705 self, shadow_frame, true /* is_exact */, inst, inst_data, result);
706 } else {
707 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE);
708 static const bool kIsRange = true;
709 return DoMethodHandleInvokeCommon<kIsRange>(
710 self, shadow_frame, true /* is_exact */, inst, inst_data, result);
711 }
712}
713
714bool DoMethodHandleInvoke(Thread* self,
715 ShadowFrame& shadow_frame,
716 const Instruction* inst,
717 uint16_t inst_data,
718 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
719 if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) {
720 static const bool kIsRange = false;
721 return DoMethodHandleInvokeCommon<kIsRange>(
722 self, shadow_frame, false /* is_exact */, inst, inst_data, result);
723 } else {
724 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE);
725 static const bool kIsRange = true;
726 return DoMethodHandleInvokeCommon<kIsRange>(
727 self, shadow_frame, false /* is_exact */, inst, inst_data, result);
728 }
729}
730
Orion Hodson928033d2018-02-07 05:30:54 +0000731static bool DoVarHandleInvokeCommon(Thread* self,
732 ShadowFrame& shadow_frame,
733 const Instruction* inst,
734 uint16_t inst_data,
735 JValue* result,
736 mirror::VarHandle::AccessMode access_mode)
737 REQUIRES_SHARED(Locks::mutator_lock_) {
738 // Make sure to check for async exceptions
739 if (UNLIKELY(self->ObserveAsyncException())) {
740 return false;
741 }
742
Orion Hodson928033d2018-02-07 05:30:54 +0000743 StackHandleScope<2> hs(self);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100744 bool is_var_args = inst->HasVarArgs();
Orion Hodson06d10a72018-05-14 08:53:38 +0100745 const uint16_t vRegH = is_var_args ? inst->VRegH_45cc() : inst->VRegH_4rcc();
Orion Hodson928033d2018-02-07 05:30:54 +0000746 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
747 Handle<mirror::MethodType> callsite_type(hs.NewHandle(
Orion Hodson06d10a72018-05-14 08:53:38 +0100748 class_linker->ResolveMethodType(self, dex::ProtoIndex(vRegH), shadow_frame.GetMethod())));
Orion Hodson928033d2018-02-07 05:30:54 +0000749 // This implies we couldn't resolve one or more types in this VarHandle.
750 if (UNLIKELY(callsite_type == nullptr)) {
751 CHECK(self->IsExceptionPending());
752 return false;
753 }
754
Orion Hodson537a4fe2018-05-15 13:57:58 +0100755 const uint32_t vRegC = is_var_args ? inst->VRegC_45cc() : inst->VRegC_4rcc();
756 ObjPtr<mirror::Object> receiver(shadow_frame.GetVRegReference(vRegC));
757 Handle<mirror::VarHandle> var_handle(hs.NewHandle(down_cast<mirror::VarHandle*>(receiver.Ptr())));
Orion Hodson928033d2018-02-07 05:30:54 +0000758 if (is_var_args) {
759 uint32_t args[Instruction::kMaxVarArgRegs];
760 inst->GetVarArgs(args, inst_data);
761 VarArgsInstructionOperands all_operands(args, inst->VRegA_45cc());
762 NoReceiverInstructionOperands operands(&all_operands);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100763 return VarHandleInvokeAccessor(self,
764 shadow_frame,
765 var_handle,
766 callsite_type,
767 access_mode,
768 &operands,
769 result);
Orion Hodson928033d2018-02-07 05:30:54 +0000770 } else {
771 RangeInstructionOperands all_operands(inst->VRegC_4rcc(), inst->VRegA_4rcc());
772 NoReceiverInstructionOperands operands(&all_operands);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100773 return VarHandleInvokeAccessor(self,
774 shadow_frame,
775 var_handle,
776 callsite_type,
777 access_mode,
778 &operands,
779 result);
Orion Hodson928033d2018-02-07 05:30:54 +0000780 }
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100781}
782
Orion Hodson928033d2018-02-07 05:30:54 +0000783#define DO_VAR_HANDLE_ACCESSOR(_access_mode) \
784bool DoVarHandle ## _access_mode(Thread* self, \
785 ShadowFrame& shadow_frame, \
786 const Instruction* inst, \
787 uint16_t inst_data, \
788 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { \
789 const auto access_mode = mirror::VarHandle::AccessMode::k ## _access_mode; \
790 return DoVarHandleInvokeCommon(self, shadow_frame, inst, inst_data, result, access_mode); \
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100791}
792
Orion Hodson928033d2018-02-07 05:30:54 +0000793DO_VAR_HANDLE_ACCESSOR(CompareAndExchange)
794DO_VAR_HANDLE_ACCESSOR(CompareAndExchangeAcquire)
795DO_VAR_HANDLE_ACCESSOR(CompareAndExchangeRelease)
796DO_VAR_HANDLE_ACCESSOR(CompareAndSet)
797DO_VAR_HANDLE_ACCESSOR(Get)
798DO_VAR_HANDLE_ACCESSOR(GetAcquire)
799DO_VAR_HANDLE_ACCESSOR(GetAndAdd)
800DO_VAR_HANDLE_ACCESSOR(GetAndAddAcquire)
801DO_VAR_HANDLE_ACCESSOR(GetAndAddRelease)
802DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAnd)
803DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAndAcquire)
804DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAndRelease)
805DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOr)
806DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOrAcquire)
807DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOrRelease)
808DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXor)
809DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXorAcquire)
810DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXorRelease)
811DO_VAR_HANDLE_ACCESSOR(GetAndSet)
812DO_VAR_HANDLE_ACCESSOR(GetAndSetAcquire)
813DO_VAR_HANDLE_ACCESSOR(GetAndSetRelease)
814DO_VAR_HANDLE_ACCESSOR(GetOpaque)
815DO_VAR_HANDLE_ACCESSOR(GetVolatile)
816DO_VAR_HANDLE_ACCESSOR(Set)
817DO_VAR_HANDLE_ACCESSOR(SetOpaque)
818DO_VAR_HANDLE_ACCESSOR(SetRelease)
819DO_VAR_HANDLE_ACCESSOR(SetVolatile)
820DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSet)
821DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetAcquire)
822DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetPlain)
823DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetRelease)
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100824
Orion Hodson928033d2018-02-07 05:30:54 +0000825#undef DO_VAR_HANDLE_ACCESSOR
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100826
827template<bool is_range>
828bool DoInvokePolymorphic(Thread* self,
829 ShadowFrame& shadow_frame,
830 const Instruction* inst,
831 uint16_t inst_data,
832 JValue* result) {
833 const int invoke_method_idx = inst->VRegB();
834 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
835 ArtMethod* invoke_method =
836 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
837 self, invoke_method_idx, shadow_frame.GetMethod(), kVirtual);
838
839 // Ensure intrinsic identifiers are initialized.
840 DCHECK(invoke_method->IsIntrinsic());
841
842 // Dispatch based on intrinsic identifier associated with method.
843 switch (static_cast<art::Intrinsics>(invoke_method->GetIntrinsic())) {
844#define CASE_SIGNATURE_POLYMORPHIC_INTRINSIC(Name, ...) \
845 case Intrinsics::k##Name: \
846 return Do ## Name(self, shadow_frame, inst, inst_data, result);
847#include "intrinsics_list.h"
848 SIGNATURE_POLYMORPHIC_INTRINSICS_LIST(CASE_SIGNATURE_POLYMORPHIC_INTRINSIC)
849#undef INTRINSICS_LIST
850#undef SIGNATURE_POLYMORPHIC_INTRINSICS_LIST
851#undef CASE_SIGNATURE_POLYMORPHIC_INTRINSIC
852 default:
853 LOG(FATAL) << "Unreachable: " << invoke_method->GetIntrinsic();
854 UNREACHABLE();
855 return false;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100856 }
857}
858
Orion Hodsona5dca522018-02-27 12:42:11 +0000859static JValue ConvertScalarBootstrapArgument(jvalue value) {
860 // value either contains a primitive scalar value if it corresponds
861 // to a primitive type, or it contains an integer value if it
862 // corresponds to an object instance reference id (e.g. a string id).
863 return JValue::FromPrimitive(value.j);
864}
865
866static ObjPtr<mirror::Class> GetClassForBootstrapArgument(EncodedArrayValueIterator::ValueType type)
867 REQUIRES_SHARED(Locks::mutator_lock_) {
868 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100869 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = class_linker->GetClassRoots();
Orion Hodsona5dca522018-02-27 12:42:11 +0000870 switch (type) {
871 case EncodedArrayValueIterator::ValueType::kBoolean:
872 case EncodedArrayValueIterator::ValueType::kByte:
873 case EncodedArrayValueIterator::ValueType::kChar:
874 case EncodedArrayValueIterator::ValueType::kShort:
875 // These types are disallowed by JVMS. Treat as integers. This
876 // will result in CCE's being raised if the BSM has one of these
877 // types.
878 case EncodedArrayValueIterator::ValueType::kInt:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100879 return GetClassRoot(ClassRoot::kPrimitiveInt, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000880 case EncodedArrayValueIterator::ValueType::kLong:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100881 return GetClassRoot(ClassRoot::kPrimitiveLong, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000882 case EncodedArrayValueIterator::ValueType::kFloat:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100883 return GetClassRoot(ClassRoot::kPrimitiveFloat, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000884 case EncodedArrayValueIterator::ValueType::kDouble:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100885 return GetClassRoot(ClassRoot::kPrimitiveDouble, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000886 case EncodedArrayValueIterator::ValueType::kMethodType:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100887 return GetClassRoot<mirror::MethodType>(class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000888 case EncodedArrayValueIterator::ValueType::kMethodHandle:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100889 return GetClassRoot<mirror::MethodHandle>(class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000890 case EncodedArrayValueIterator::ValueType::kString:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100891 return GetClassRoot<mirror::String>();
Orion Hodsona5dca522018-02-27 12:42:11 +0000892 case EncodedArrayValueIterator::ValueType::kType:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100893 return GetClassRoot<mirror::Class>();
Orion Hodsona5dca522018-02-27 12:42:11 +0000894 case EncodedArrayValueIterator::ValueType::kField:
895 case EncodedArrayValueIterator::ValueType::kMethod:
896 case EncodedArrayValueIterator::ValueType::kEnum:
897 case EncodedArrayValueIterator::ValueType::kArray:
898 case EncodedArrayValueIterator::ValueType::kAnnotation:
899 case EncodedArrayValueIterator::ValueType::kNull:
900 return nullptr;
901 }
902}
903
904static bool GetArgumentForBootstrapMethod(Thread* self,
905 ArtMethod* referrer,
906 EncodedArrayValueIterator::ValueType type,
907 const JValue* encoded_value,
908 JValue* decoded_value)
909 REQUIRES_SHARED(Locks::mutator_lock_) {
910 // The encoded_value contains either a scalar value (IJDF) or a
911 // scalar DEX file index to a reference type to be materialized.
912 switch (type) {
913 case EncodedArrayValueIterator::ValueType::kInt:
914 case EncodedArrayValueIterator::ValueType::kFloat:
915 decoded_value->SetI(encoded_value->GetI());
916 return true;
917 case EncodedArrayValueIterator::ValueType::kLong:
918 case EncodedArrayValueIterator::ValueType::kDouble:
919 decoded_value->SetJ(encoded_value->GetJ());
920 return true;
921 case EncodedArrayValueIterator::ValueType::kMethodType: {
922 StackHandleScope<2> hs(self);
923 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader()));
924 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
Orion Hodson06d10a72018-05-14 08:53:38 +0100925 dex::ProtoIndex proto_idx(encoded_value->GetC());
Orion Hodsona5dca522018-02-27 12:42:11 +0000926 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Orion Hodson06d10a72018-05-14 08:53:38 +0100927 ObjPtr<mirror::MethodType> o =
928 cl->ResolveMethodType(self, proto_idx, dex_cache, class_loader);
Orion Hodsona5dca522018-02-27 12:42:11 +0000929 if (UNLIKELY(o.IsNull())) {
930 DCHECK(self->IsExceptionPending());
931 return false;
932 }
933 decoded_value->SetL(o);
934 return true;
935 }
936 case EncodedArrayValueIterator::ValueType::kMethodHandle: {
937 uint32_t index = static_cast<uint32_t>(encoded_value->GetI());
938 ClassLinker* cl = Runtime::Current()->GetClassLinker();
939 ObjPtr<mirror::MethodHandle> o = cl->ResolveMethodHandle(self, index, referrer);
940 if (UNLIKELY(o.IsNull())) {
941 DCHECK(self->IsExceptionPending());
942 return false;
943 }
944 decoded_value->SetL(o);
945 return true;
946 }
947 case EncodedArrayValueIterator::ValueType::kString: {
948 StackHandleScope<1> hs(self);
949 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
950 dex::StringIndex index(static_cast<uint32_t>(encoded_value->GetI()));
951 ClassLinker* cl = Runtime::Current()->GetClassLinker();
952 ObjPtr<mirror::String> o = cl->ResolveString(index, dex_cache);
953 if (UNLIKELY(o.IsNull())) {
954 DCHECK(self->IsExceptionPending());
955 return false;
956 }
957 decoded_value->SetL(o);
958 return true;
959 }
960 case EncodedArrayValueIterator::ValueType::kType: {
Orion Hodsona5dca522018-02-27 12:42:11 +0000961 dex::TypeIndex index(static_cast<uint32_t>(encoded_value->GetI()));
962 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100963 ObjPtr<mirror::Class> o = cl->ResolveType(index, referrer);
Orion Hodsona5dca522018-02-27 12:42:11 +0000964 if (UNLIKELY(o.IsNull())) {
965 DCHECK(self->IsExceptionPending());
966 return false;
967 }
968 decoded_value->SetL(o);
969 return true;
970 }
971 case EncodedArrayValueIterator::ValueType::kBoolean:
972 case EncodedArrayValueIterator::ValueType::kByte:
973 case EncodedArrayValueIterator::ValueType::kChar:
974 case EncodedArrayValueIterator::ValueType::kShort:
975 case EncodedArrayValueIterator::ValueType::kField:
976 case EncodedArrayValueIterator::ValueType::kMethod:
977 case EncodedArrayValueIterator::ValueType::kEnum:
978 case EncodedArrayValueIterator::ValueType::kArray:
979 case EncodedArrayValueIterator::ValueType::kAnnotation:
980 case EncodedArrayValueIterator::ValueType::kNull:
981 // Unreachable - unsupported types that have been checked when
982 // determining the effect call site type based on the bootstrap
983 // argument types.
984 UNREACHABLE();
985 }
986}
987
988static bool PackArgumentForBootstrapMethod(Thread* self,
989 ArtMethod* referrer,
990 CallSiteArrayValueIterator* it,
991 ShadowFrameSetter* setter)
992 REQUIRES_SHARED(Locks::mutator_lock_) {
993 auto type = it->GetValueType();
994 const JValue encoded_value = ConvertScalarBootstrapArgument(it->GetJavaValue());
995 JValue decoded_value;
996 if (!GetArgumentForBootstrapMethod(self, referrer, type, &encoded_value, &decoded_value)) {
997 return false;
998 }
999 switch (it->GetValueType()) {
1000 case EncodedArrayValueIterator::ValueType::kInt:
1001 case EncodedArrayValueIterator::ValueType::kFloat:
1002 setter->Set(static_cast<uint32_t>(decoded_value.GetI()));
1003 return true;
1004 case EncodedArrayValueIterator::ValueType::kLong:
1005 case EncodedArrayValueIterator::ValueType::kDouble:
1006 setter->SetLong(decoded_value.GetJ());
1007 return true;
1008 case EncodedArrayValueIterator::ValueType::kMethodType:
1009 case EncodedArrayValueIterator::ValueType::kMethodHandle:
1010 case EncodedArrayValueIterator::ValueType::kString:
1011 case EncodedArrayValueIterator::ValueType::kType:
1012 setter->SetReference(decoded_value.GetL());
1013 return true;
1014 case EncodedArrayValueIterator::ValueType::kBoolean:
1015 case EncodedArrayValueIterator::ValueType::kByte:
1016 case EncodedArrayValueIterator::ValueType::kChar:
1017 case EncodedArrayValueIterator::ValueType::kShort:
1018 case EncodedArrayValueIterator::ValueType::kField:
1019 case EncodedArrayValueIterator::ValueType::kMethod:
1020 case EncodedArrayValueIterator::ValueType::kEnum:
1021 case EncodedArrayValueIterator::ValueType::kArray:
1022 case EncodedArrayValueIterator::ValueType::kAnnotation:
1023 case EncodedArrayValueIterator::ValueType::kNull:
1024 // Unreachable - unsupported types that have been checked when
1025 // determining the effect call site type based on the bootstrap
1026 // argument types.
1027 UNREACHABLE();
1028 }
1029}
1030
1031static bool PackCollectorArrayForBootstrapMethod(Thread* self,
1032 ArtMethod* referrer,
1033 ObjPtr<mirror::Class> array_type,
1034 int32_t array_length,
1035 CallSiteArrayValueIterator* it,
1036 ShadowFrameSetter* setter)
1037 REQUIRES_SHARED(Locks::mutator_lock_) {
1038 StackHandleScope<1> hs(self);
1039 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1040 JValue decoded_value;
1041
1042#define COLLECT_PRIMITIVE_ARRAY(Descriptor, Type) \
1043 Handle<mirror::Type ## Array> array = \
1044 hs.NewHandle(mirror::Type ## Array::Alloc(self, array_length)); \
1045 if (array.IsNull()) { \
1046 return false; \
1047 } \
1048 for (int32_t i = 0; it->HasNext(); it->Next(), ++i) { \
1049 auto type = it->GetValueType(); \
1050 DCHECK_EQ(type, EncodedArrayValueIterator::ValueType::k ## Type); \
1051 const JValue encoded_value = \
1052 ConvertScalarBootstrapArgument(it->GetJavaValue()); \
1053 GetArgumentForBootstrapMethod(self, \
1054 referrer, \
1055 type, \
1056 &encoded_value, \
1057 &decoded_value); \
1058 array->Set(i, decoded_value.Get ## Descriptor()); \
1059 } \
1060 setter->SetReference(array.Get()); \
1061 return true;
1062
1063#define COLLECT_REFERENCE_ARRAY(T, Type) \
1064 Handle<mirror::ObjectArray<T>> array = \
1065 hs.NewHandle(mirror::ObjectArray<T>::Alloc(self, \
1066 array_type, \
1067 array_length)); \
1068 if (array.IsNull()) { \
1069 return false; \
1070 } \
1071 for (int32_t i = 0; it->HasNext(); it->Next(), ++i) { \
1072 auto type = it->GetValueType(); \
1073 DCHECK_EQ(type, EncodedArrayValueIterator::ValueType::k ## Type); \
1074 const JValue encoded_value = \
1075 ConvertScalarBootstrapArgument(it->GetJavaValue()); \
1076 if (!GetArgumentForBootstrapMethod(self, \
1077 referrer, \
1078 type, \
1079 &encoded_value, \
1080 &decoded_value)) { \
1081 return false; \
1082 } \
1083 ObjPtr<mirror::Object> o = decoded_value.GetL(); \
1084 if (Runtime::Current()->IsActiveTransaction()) { \
1085 array->Set<true>(i, ObjPtr<T>::DownCast(o)); \
1086 } else { \
1087 array->Set<false>(i, ObjPtr<T>::DownCast(o)); \
1088 } \
1089 } \
1090 setter->SetReference(array.Get()); \
1091 return true;
1092
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001093 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = class_linker->GetClassRoots();
1094 ObjPtr<mirror::Class> component_type = array_type->GetComponentType();
1095 if (component_type == GetClassRoot(ClassRoot::kPrimitiveInt, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001096 COLLECT_PRIMITIVE_ARRAY(I, Int);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001097 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveLong, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001098 COLLECT_PRIMITIVE_ARRAY(J, Long);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001099 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveFloat, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001100 COLLECT_PRIMITIVE_ARRAY(F, Float);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001101 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveDouble, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001102 COLLECT_PRIMITIVE_ARRAY(D, Double);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001103 } else if (component_type == GetClassRoot<mirror::MethodType>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001104 COLLECT_REFERENCE_ARRAY(mirror::MethodType, MethodType);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001105 } else if (component_type == GetClassRoot<mirror::MethodHandle>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001106 COLLECT_REFERENCE_ARRAY(mirror::MethodHandle, MethodHandle);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001107 } else if (component_type == GetClassRoot<mirror::String>(class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001108 COLLECT_REFERENCE_ARRAY(mirror::String, String);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001109 } else if (component_type == GetClassRoot<mirror::Class>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001110 COLLECT_REFERENCE_ARRAY(mirror::Class, Type);
1111 } else {
1112 UNREACHABLE();
1113 }
1114 #undef COLLECT_PRIMITIVE_ARRAY
1115 #undef COLLECT_REFERENCE_ARRAY
1116}
1117
1118static ObjPtr<mirror::MethodType> BuildCallSiteForBootstrapMethod(Thread* self,
1119 const DexFile* dex_file,
1120 uint32_t call_site_idx)
1121 REQUIRES_SHARED(Locks::mutator_lock_) {
1122 const DexFile::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx);
1123 CallSiteArrayValueIterator it(*dex_file, csi);
1124 DCHECK_GE(it.Size(), 1u);
1125
1126 StackHandleScope<2> hs(self);
1127 // Create array for parameter types.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001128 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001129 ObjPtr<mirror::Class> class_array_type =
1130 GetClassRoot<mirror::ObjectArray<mirror::Class>>(class_linker);
Orion Hodsona5dca522018-02-27 12:42:11 +00001131 Handle<mirror::ObjectArray<mirror::Class>> ptypes = hs.NewHandle(
1132 mirror::ObjectArray<mirror::Class>::Alloc(self,
1133 class_array_type,
1134 static_cast<int>(it.Size())));
1135 if (ptypes.IsNull()) {
1136 DCHECK(self->IsExceptionPending());
1137 return nullptr;
1138 }
1139
1140 // Populate the first argument with an instance of j.l.i.MethodHandles.Lookup
1141 // that the runtime will construct.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001142 ptypes->Set(0, GetClassRoot<mirror::MethodHandlesLookup>(class_linker));
Orion Hodsona5dca522018-02-27 12:42:11 +00001143 it.Next();
1144
1145 // The remaining parameter types are derived from the types of
1146 // arguments present in the DEX file.
1147 int index = 1;
1148 while (it.HasNext()) {
1149 ObjPtr<mirror::Class> ptype = GetClassForBootstrapArgument(it.GetValueType());
1150 if (ptype.IsNull()) {
1151 ThrowClassCastException("Unsupported bootstrap argument type");
1152 return nullptr;
1153 }
1154 ptypes->Set(index, ptype);
1155 index++;
1156 it.Next();
1157 }
1158 DCHECK_EQ(static_cast<size_t>(index), it.Size());
1159
1160 // By definition, the return type is always a j.l.i.CallSite.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001161 Handle<mirror::Class> rtype = hs.NewHandle(GetClassRoot<mirror::CallSite>());
Orion Hodsona5dca522018-02-27 12:42:11 +00001162 return mirror::MethodType::Create(self, rtype, ptypes);
1163}
1164
Orion Hodsonc069a302017-01-18 09:23:12 +00001165static ObjPtr<mirror::CallSite> InvokeBootstrapMethod(Thread* self,
1166 ShadowFrame& shadow_frame,
1167 uint32_t call_site_idx)
1168 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001169 StackHandleScope<7> hs(self);
1170 // There are three mandatory arguments expected from the call site
1171 // value array in the DEX file: the bootstrap method handle, the
1172 // method name to pass to the bootstrap method, and the method type
1173 // to pass to the bootstrap method.
1174 static constexpr size_t kMandatoryArgumentsCount = 3;
Orion Hodsonc069a302017-01-18 09:23:12 +00001175 ArtMethod* referrer = shadow_frame.GetMethod();
1176 const DexFile* dex_file = referrer->GetDexFile();
1177 const DexFile::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00001178 CallSiteArrayValueIterator it(*dex_file, csi);
Orion Hodsona5dca522018-02-27 12:42:11 +00001179 if (it.Size() < kMandatoryArgumentsCount) {
1180 ThrowBootstrapMethodError("Truncated bootstrap arguments (%zu < %zu)",
1181 it.Size(), kMandatoryArgumentsCount);
1182 return nullptr;
1183 }
1184
1185 if (it.GetValueType() != EncodedArrayValueIterator::ValueType::kMethodHandle) {
1186 ThrowBootstrapMethodError("First bootstrap argument is not a method handle");
1187 return nullptr;
1188 }
1189
1190 uint32_t bsm_index = static_cast<uint32_t>(it.GetJavaValue().i);
1191 it.Next();
1192
Orion Hodsonc069a302017-01-18 09:23:12 +00001193 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Orion Hodsona5dca522018-02-27 12:42:11 +00001194 Handle<mirror::MethodHandle> bsm =
1195 hs.NewHandle(class_linker->ResolveMethodHandle(self, bsm_index, referrer));
1196 if (bsm.IsNull()) {
Orion Hodsonc069a302017-01-18 09:23:12 +00001197 DCHECK(self->IsExceptionPending());
1198 return nullptr;
1199 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001200
Orion Hodsona5dca522018-02-27 12:42:11 +00001201 if (bsm->GetHandleKind() != mirror::MethodHandle::Kind::kInvokeStatic) {
1202 // JLS suggests also accepting constructors. This is currently
1203 // hard as constructor invocations happen via transformers in ART
1204 // today. The constructor would need to be a class derived from java.lang.invoke.CallSite.
1205 ThrowBootstrapMethodError("Unsupported bootstrap method invocation kind");
1206 return nullptr;
1207 }
1208
1209 // Construct the local call site type information based on the 3
1210 // mandatory arguments provided by the runtime and the static arguments
1211 // in the DEX file. We will use these arguments to build a shadow frame.
1212 MutableHandle<mirror::MethodType> call_site_type =
1213 hs.NewHandle(BuildCallSiteForBootstrapMethod(self, dex_file, call_site_idx));
1214 if (call_site_type.IsNull()) {
1215 DCHECK(self->IsExceptionPending());
1216 return nullptr;
1217 }
1218
1219 // Check if this BSM is targeting a variable arity method. If so,
1220 // we'll need to collect the trailing arguments into an array.
1221 Handle<mirror::Array> collector_arguments;
1222 int32_t collector_arguments_length;
1223 if (bsm->GetTargetMethod()->IsVarargs()) {
1224 int number_of_bsm_parameters = bsm->GetMethodType()->GetNumberOfPTypes();
1225 if (number_of_bsm_parameters == 0) {
1226 ThrowBootstrapMethodError("Variable arity BSM does not have any arguments");
1227 return nullptr;
1228 }
1229 Handle<mirror::Class> collector_array_class =
1230 hs.NewHandle(bsm->GetMethodType()->GetPTypes()->Get(number_of_bsm_parameters - 1));
1231 if (!collector_array_class->IsArrayClass()) {
1232 ThrowBootstrapMethodError("Variable arity BSM does not have array as final argument");
1233 return nullptr;
1234 }
1235 // The call site may include no arguments to be collected. In this
1236 // case the number of arguments must be at least the number of BSM
1237 // parameters less the collector array.
1238 if (call_site_type->GetNumberOfPTypes() < number_of_bsm_parameters - 1) {
1239 ThrowWrongMethodTypeException(bsm->GetMethodType(), call_site_type.Get());
1240 return nullptr;
1241 }
1242 // Check all the arguments to be collected match the collector array component type.
1243 for (int i = number_of_bsm_parameters - 1; i < call_site_type->GetNumberOfPTypes(); ++i) {
1244 if (call_site_type->GetPTypes()->Get(i) != collector_array_class->GetComponentType()) {
1245 ThrowClassCastException(collector_array_class->GetComponentType(),
1246 call_site_type->GetPTypes()->Get(i));
1247 return nullptr;
1248 }
1249 }
1250 // Update the call site method type so it now includes the collector array.
1251 int32_t collector_arguments_start = number_of_bsm_parameters - 1;
1252 collector_arguments_length = call_site_type->GetNumberOfPTypes() - number_of_bsm_parameters + 1;
1253 call_site_type.Assign(
1254 mirror::MethodType::CollectTrailingArguments(self,
1255 call_site_type.Get(),
1256 collector_array_class.Get(),
1257 collector_arguments_start));
1258 if (call_site_type.IsNull()) {
1259 DCHECK(self->IsExceptionPending());
1260 return nullptr;
1261 }
1262 } else {
1263 collector_arguments_length = 0;
1264 }
1265
1266 if (call_site_type->GetNumberOfPTypes() != bsm->GetMethodType()->GetNumberOfPTypes()) {
1267 ThrowWrongMethodTypeException(bsm->GetMethodType(), call_site_type.Get());
1268 return nullptr;
1269 }
1270
1271 // BSM invocation has a different set of exceptions that
1272 // j.l.i.MethodHandle.invoke(). Scan arguments looking for CCE
1273 // "opportunities". Unfortunately we cannot just leave this to the
1274 // method handle invocation as this might generate a WMTE.
1275 for (int32_t i = 0; i < call_site_type->GetNumberOfPTypes(); ++i) {
1276 ObjPtr<mirror::Class> from = call_site_type->GetPTypes()->Get(i);
1277 ObjPtr<mirror::Class> to = bsm->GetMethodType()->GetPTypes()->Get(i);
1278 if (!IsParameterTypeConvertible(from, to)) {
1279 ThrowClassCastException(from, to);
1280 return nullptr;
1281 }
1282 }
1283 if (!IsReturnTypeConvertible(call_site_type->GetRType(), bsm->GetMethodType()->GetRType())) {
1284 ThrowClassCastException(bsm->GetMethodType()->GetRType(), call_site_type->GetRType());
1285 return nullptr;
1286 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001287
1288 // Set-up a shadow frame for invoking the bootstrap method handle.
1289 ShadowFrameAllocaUniquePtr bootstrap_frame =
Orion Hodsona5dca522018-02-27 12:42:11 +00001290 CREATE_SHADOW_FRAME(call_site_type->NumberOfVRegs(),
1291 nullptr,
1292 referrer,
1293 shadow_frame.GetDexPC());
Orion Hodsonc069a302017-01-18 09:23:12 +00001294 ScopedStackedShadowFramePusher pusher(
1295 self, bootstrap_frame.get(), StackedShadowFrameType::kShadowFrameUnderConstruction);
Orion Hodsona5dca522018-02-27 12:42:11 +00001296 ShadowFrameSetter setter(bootstrap_frame.get(), 0u);
Orion Hodsonc069a302017-01-18 09:23:12 +00001297
1298 // The first parameter is a MethodHandles lookup instance.
Orion Hodsona5dca522018-02-27 12:42:11 +00001299 Handle<mirror::Class> lookup_class =
1300 hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass());
1301 ObjPtr<mirror::MethodHandlesLookup> lookup =
1302 mirror::MethodHandlesLookup::Create(self, lookup_class);
1303 if (lookup.IsNull()) {
Orion Hodsonc069a302017-01-18 09:23:12 +00001304 DCHECK(self->IsExceptionPending());
1305 return nullptr;
1306 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001307 setter.SetReference(lookup);
Orion Hodsonc069a302017-01-18 09:23:12 +00001308
Orion Hodsona5dca522018-02-27 12:42:11 +00001309 // Pack the remaining arguments into the frame.
1310 int number_of_arguments = call_site_type->GetNumberOfPTypes();
1311 int argument_index;
1312 for (argument_index = 1; argument_index < number_of_arguments; ++argument_index) {
1313 if (argument_index == number_of_arguments - 1 &&
1314 call_site_type->GetPTypes()->Get(argument_index)->IsArrayClass()) {
1315 ObjPtr<mirror::Class> array_type = call_site_type->GetPTypes()->Get(argument_index);
1316 if (!PackCollectorArrayForBootstrapMethod(self,
1317 referrer,
1318 array_type,
1319 collector_arguments_length,
1320 &it,
1321 &setter)) {
1322 DCHECK(self->IsExceptionPending());
1323 return nullptr;
Orion Hodsonc069a302017-01-18 09:23:12 +00001324 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001325 } else if (!PackArgumentForBootstrapMethod(self, referrer, &it, &setter)) {
1326 DCHECK(self->IsExceptionPending());
1327 return nullptr;
Orion Hodsonc069a302017-01-18 09:23:12 +00001328 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001329 it.Next();
1330 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001331 DCHECK(!it.HasNext());
1332 DCHECK(setter.Done());
Orion Hodsonc069a302017-01-18 09:23:12 +00001333
1334 // Invoke the bootstrap method handle.
1335 JValue result;
Orion Hodsona5dca522018-02-27 12:42:11 +00001336 RangeInstructionOperands operands(0, bootstrap_frame->NumberOfVRegs());
1337 bool invoke_success = MethodHandleInvoke(self,
1338 *bootstrap_frame,
1339 bsm,
1340 call_site_type,
1341 &operands,
1342 &result);
Orion Hodsonc069a302017-01-18 09:23:12 +00001343 if (!invoke_success) {
1344 DCHECK(self->IsExceptionPending());
1345 return nullptr;
1346 }
1347
1348 Handle<mirror::Object> object(hs.NewHandle(result.GetL()));
Orion Hodsonc069a302017-01-18 09:23:12 +00001349 if (UNLIKELY(object.IsNull())) {
Orion Hodsonda1cdd02018-01-31 18:08:28 +00001350 // This will typically be for LambdaMetafactory which is not supported.
Orion Hodson76e6adb2018-02-23 13:15:55 +00001351 ThrowClassCastException("Bootstrap method returned null");
Orion Hodsonc069a302017-01-18 09:23:12 +00001352 return nullptr;
1353 }
1354
Orion Hodsona5dca522018-02-27 12:42:11 +00001355 // Check the result type is a subclass of j.l.i.CallSite.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001356 ObjPtr<mirror::Class> call_site_class = GetClassRoot<mirror::CallSite>(class_linker);
1357 if (UNLIKELY(!object->InstanceOf(call_site_class))) {
1358 ThrowClassCastException(object->GetClass(), call_site_class);
Orion Hodsonc069a302017-01-18 09:23:12 +00001359 return nullptr;
1360 }
1361
Orion Hodsona5dca522018-02-27 12:42:11 +00001362 // Check the call site target is not null as we're going to invoke it.
Orion Hodsonc069a302017-01-18 09:23:12 +00001363 Handle<mirror::CallSite> call_site =
1364 hs.NewHandle(ObjPtr<mirror::CallSite>::DownCast(ObjPtr<mirror::Object>(result.GetL())));
Orion Hodsonc069a302017-01-18 09:23:12 +00001365 Handle<mirror::MethodHandle> target = hs.NewHandle(call_site->GetTarget());
1366 if (UNLIKELY(target.IsNull())) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001367 ThrowClassCastException("Bootstrap method returned a CallSite with a null target");
Orion Hodsonc069a302017-01-18 09:23:12 +00001368 return nullptr;
1369 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001370 return call_site.Get();
1371}
1372
1373template<bool is_range>
1374bool DoInvokeCustom(Thread* self,
1375 ShadowFrame& shadow_frame,
1376 const Instruction* inst,
1377 uint16_t inst_data,
1378 JValue* result)
1379 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Light848574c2017-09-25 16:59:39 -07001380 // Make sure to check for async exceptions
1381 if (UNLIKELY(self->ObserveAsyncException())) {
1382 return false;
1383 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001384 // invoke-custom is not supported in transactions. In transactions
1385 // there is a limited set of types supported. invoke-custom allows
1386 // running arbitrary code and instantiating arbitrary types.
1387 CHECK(!Runtime::Current()->IsActiveTransaction());
1388 StackHandleScope<4> hs(self);
1389 Handle<mirror::DexCache> dex_cache(hs.NewHandle(shadow_frame.GetMethod()->GetDexCache()));
1390 const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
1391 MutableHandle<mirror::CallSite>
1392 call_site(hs.NewHandle(dex_cache->GetResolvedCallSite(call_site_idx)));
1393 if (call_site.IsNull()) {
1394 call_site.Assign(InvokeBootstrapMethod(self, shadow_frame, call_site_idx));
1395 if (UNLIKELY(call_site.IsNull())) {
1396 CHECK(self->IsExceptionPending());
Orion Hodsona5dca522018-02-27 12:42:11 +00001397 if (!self->GetException()->IsError()) {
1398 // Use a BootstrapMethodError if the exception is not an instance of java.lang.Error.
1399 ThrowWrappedBootstrapMethodError("Exception from call site #%u bootstrap method",
1400 call_site_idx);
1401 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001402 result->SetJ(0);
1403 return false;
1404 }
1405 mirror::CallSite* winning_call_site =
1406 dex_cache->SetResolvedCallSite(call_site_idx, call_site.Get());
1407 call_site.Assign(winning_call_site);
1408 }
1409
Orion Hodsonc069a302017-01-18 09:23:12 +00001410 Handle<mirror::MethodHandle> target = hs.NewHandle(call_site->GetTarget());
1411 Handle<mirror::MethodType> target_method_type = hs.NewHandle(target->GetMethodType());
1412 DCHECK_EQ(static_cast<size_t>(inst->VRegA()), target_method_type->NumberOfVRegs());
Orion Hodsonc069a302017-01-18 09:23:12 +00001413 if (is_range) {
Orion Hodson960d4f72017-11-10 15:32:38 +00001414 RangeInstructionOperands operands(inst->VRegC_3rc(), inst->VRegA_3rc());
1415 return MethodHandleInvokeExact(self,
1416 shadow_frame,
1417 target,
1418 target_method_type,
1419 &operands,
1420 result);
Orion Hodsonc069a302017-01-18 09:23:12 +00001421 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +00001422 uint32_t args[Instruction::kMaxVarArgRegs];
Orion Hodsonc069a302017-01-18 09:23:12 +00001423 inst->GetVarArgs(args, inst_data);
Orion Hodson960d4f72017-11-10 15:32:38 +00001424 VarArgsInstructionOperands operands(args, inst->VRegA_35c());
1425 return MethodHandleInvokeExact(self,
1426 shadow_frame,
1427 target,
1428 target_method_type,
1429 &operands,
1430 result);
Orion Hodsonc069a302017-01-18 09:23:12 +00001431 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001432}
1433
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001434// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
1435static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
1436 size_t dest_reg, size_t src_reg)
1437 REQUIRES_SHARED(Locks::mutator_lock_) {
1438 // Uint required, so that sign extension does not make this wrong on 64b systems
1439 uint32_t src_value = shadow_frame.GetVReg(src_reg);
1440 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
1441
1442 // If both register locations contains the same value, the register probably holds a reference.
1443 // Note: As an optimization, non-moving collectors leave a stale reference value
1444 // in the references array even after the original vreg was overwritten to a non-reference.
1445 if (src_value == reinterpret_cast<uintptr_t>(o.Ptr())) {
1446 new_shadow_frame->SetVRegReference(dest_reg, o);
1447 } else {
1448 new_shadow_frame->SetVReg(dest_reg, src_value);
1449 }
1450}
1451
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001452template <bool is_range>
1453inline void CopyRegisters(ShadowFrame& caller_frame,
1454 ShadowFrame* callee_frame,
1455 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
1456 const size_t first_src_reg,
1457 const size_t first_dest_reg,
1458 const size_t num_regs) {
1459 if (is_range) {
1460 const size_t dest_reg_bound = first_dest_reg + num_regs;
1461 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound;
1462 ++dest_reg, ++src_reg) {
1463 AssignRegister(callee_frame, caller_frame, dest_reg, src_reg);
1464 }
1465 } else {
1466 DCHECK_LE(num_regs, arraysize(arg));
1467
1468 for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) {
1469 AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]);
1470 }
1471 }
1472}
1473
Igor Murashkin6918bf12015-09-27 19:19:06 -07001474template <bool is_range,
Narayan Kamath370423d2016-10-03 16:51:22 +01001475 bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001476static inline bool DoCallCommon(ArtMethod* called_method,
1477 Thread* self,
1478 ShadowFrame& shadow_frame,
1479 JValue* result,
1480 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +01001481 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -07001482 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001483 bool string_init = false;
1484 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001485 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
1486 && called_method->IsConstructor())) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001487 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
Jeff Hao848f70a2014-01-15 13:49:50 -08001488 string_init = true;
1489 }
1490
Alex Lightdaf58c82016-03-16 23:00:49 +00001491 // Compute method information.
David Sehr0225f8e2018-01-31 08:52:24 +00001492 CodeItemDataAccessor accessor(called_method->DexInstructionData());
Igor Murashkin158f35c2015-06-10 15:55:30 -07001493 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001494 uint16_t num_regs;
Jeff Hao5ea84132017-05-05 16:59:29 -07001495 // Test whether to use the interpreter or compiler entrypoint, and save that result to pass to
1496 // PerformCall. A deoptimization could occur at any time, and we shouldn't change which
1497 // entrypoint to use once we start building the shadow frame.
Mathieu Chartier448bbcf2017-07-06 12:05:13 -07001498
1499 // For unstarted runtimes, always use the interpreter entrypoint. This fixes the case where we are
1500 // doing cross compilation. Note that GetEntryPointFromQuickCompiledCode doesn't use the image
1501 // pointer size here and this may case an overflow if it is called from the compiler. b/62402160
1502 const bool use_interpreter_entrypoint = !Runtime::Current()->IsStarted() ||
1503 ClassLinker::ShouldUseInterpreterEntrypoint(
1504 called_method,
1505 called_method->GetEntryPointFromQuickCompiledCode());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001506 if (LIKELY(accessor.HasCodeItem())) {
Jeff Hao5ea84132017-05-05 16:59:29 -07001507 // When transitioning to compiled code, space only needs to be reserved for the input registers.
1508 // The rest of the frame gets discarded. This also prevents accessing the called method's code
1509 // item, saving memory by keeping code items of compiled code untouched.
Mathieu Chartier448bbcf2017-07-06 12:05:13 -07001510 if (!use_interpreter_entrypoint) {
1511 DCHECK(!Runtime::Current()->IsAotCompiler()) << "Compiler should use interpreter entrypoint";
Jeff Hao5ea84132017-05-05 16:59:29 -07001512 num_regs = number_of_inputs;
1513 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001514 num_regs = accessor.RegistersSize();
1515 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, accessor.InsSize());
Jeff Hao5ea84132017-05-05 16:59:29 -07001516 }
Nicolas Geoffray01822292017-03-09 09:03:19 +00001517 } else {
1518 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1519 num_regs = number_of_inputs;
Jeff Haodf79ddb2017-02-27 14:47:06 -08001520 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001521
Igor Murashkin158f35c2015-06-10 15:55:30 -07001522 // Hack for String init:
1523 //
1524 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
1525 // invoke-x StringFactory(a, b, c, ...)
1526 // by effectively dropping the first virtual register from the invoke.
1527 //
1528 // (at this point the ArtMethod has already been replaced,
1529 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +00001530 //
1531 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
1532 // to handle the compiler optimization of replacing `this` with null without
1533 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001534 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -07001535 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001536 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 -07001537
Igor Murashkin158f35c2015-06-10 15:55:30 -07001538 // The new StringFactory call is static and has one fewer argument.
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001539 if (!accessor.HasCodeItem()) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001540 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1541 num_regs--;
1542 } // 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 -07001543 number_of_inputs--;
1544
1545 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -07001546 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001547 arg[i - 1] = arg[i];
1548 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07001549 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001550
1551 // Rewrite the non-var-arg case
1552 vregC++; // Skips the 0th vreg in the range ("this").
1553 }
1554
1555 // Parameter registers go at the end of the shadow frame.
1556 DCHECK_GE(num_regs, number_of_inputs);
1557 size_t first_dest_reg = num_regs - number_of_inputs;
1558 DCHECK_NE(first_dest_reg, (size_t)-1);
1559
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001560 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001561 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -07001562 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -07001563 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -07001564 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001565
Igor Murashkin158f35c2015-06-10 15:55:30 -07001566 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -07001567 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001568 // Slow path.
1569 // We might need to do class loading, which incurs a thread state change to kNative. So
1570 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07001571 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +02001572 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001573 self->EndAssertNoThreadSuspension(old_cause);
1574
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001575 // ArtMethod here is needed to check type information of the call site against the callee.
1576 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
1577 //
1578 // As a special case for proxy methods, which are not dex-backed,
1579 // we have to retrieve type information from the proxy's method
1580 // interface method instead (which is dex backed since proxies are never interfaces).
Andreas Gampe542451c2016-07-26 09:02:02 -07001581 ArtMethod* method =
1582 new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001583
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001584 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001585 // to get the exact type of each reference argument.
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001586 const DexFile::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001587 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001588 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001589
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001590 // Handle receiver apart since it's not part of the shorty.
1591 size_t dest_reg = first_dest_reg;
1592 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001593
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001594 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001595 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001596 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
1597 ++dest_reg;
1598 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -07001599 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001600 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001601
1602 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001603 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001604 // Skip the 0th 'shorty' type since it represents the return type.
1605 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001606 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
1607 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001608 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001609 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001610 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001611 if (do_assignability_check && o != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001612 const dex::TypeIndex type_idx = params->GetTypeItem(shorty_pos).type_idx_;
Vladimir Marko942fd312017-01-16 20:52:19 +00001613 ObjPtr<mirror::Class> arg_type = method->GetDexCache()->GetResolvedType(type_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001614 if (arg_type == nullptr) {
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001615 StackHandleScope<1> hs(self);
1616 // Preserve o since it is used below and GetClassFromTypeIndex may cause thread
1617 // suspension.
1618 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&o);
Vladimir Markob45528c2017-07-27 14:14:28 +01001619 arg_type = method->ResolveClassFromTypeIndex(type_idx);
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001620 if (arg_type == nullptr) {
1621 CHECK(self->IsExceptionPending());
1622 return false;
1623 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001624 }
1625 if (!o->VerifierInstanceOf(arg_type)) {
1626 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -07001627 std::string temp1, temp2;
Orion Hodsonfef06642016-11-25 16:07:11 +00001628 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001629 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -08001630 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -07001631 o->GetClass()->GetDescriptor(&temp1),
1632 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001633 return false;
1634 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001635 }
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001636 new_shadow_frame->SetVRegReference(dest_reg, o);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001637 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -07001638 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001639 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001640 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001641 uint64_t wide_value =
1642 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
1643 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001644 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001645 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001646 ++dest_reg;
1647 ++arg_offset;
1648 break;
1649 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001650 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001651 default:
1652 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
1653 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001654 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001655 }
1656 } else {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001657 if (is_range) {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001658 DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001659 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001660
1661 CopyRegisters<is_range>(shadow_frame,
1662 new_shadow_frame,
1663 arg,
1664 vregC,
1665 first_dest_reg,
1666 number_of_inputs);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001667 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001668 }
1669
Jeff Hao5ea84132017-05-05 16:59:29 -07001670 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001671 accessor,
Jeff Hao5ea84132017-05-05 16:59:29 -07001672 shadow_frame.GetMethod(),
1673 first_dest_reg,
1674 new_shadow_frame,
1675 result,
1676 use_interpreter_entrypoint);
Jeff Hao848f70a2014-01-15 13:49:50 -08001677
1678 if (string_init && !self->IsExceptionPending()) {
Mingyao Yangffedec52016-05-19 10:48:40 -07001679 SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001680 }
1681
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001682 return !self->IsExceptionPending();
1683}
1684
Igor Murashkin158f35c2015-06-10 15:55:30 -07001685template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001686bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
1687 const Instruction* inst, uint16_t inst_data, JValue* result) {
1688 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001689 const uint16_t number_of_inputs =
1690 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001691
1692 // TODO: find a cleaner way to separate non-range and range information without duplicating
1693 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -07001694 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001695 uint32_t vregC = 0;
1696 if (is_range) {
1697 vregC = inst->VRegC_3rc();
1698 } else {
1699 vregC = inst->VRegC_35c();
1700 inst->GetVarArgs(arg, inst_data);
1701 }
1702
1703 return DoCallCommon<is_range, do_assignability_check>(
1704 called_method, self, shadow_frame,
1705 result, number_of_inputs, arg, vregC);
1706}
1707
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001708template <bool is_range, bool do_access_check, bool transaction_active>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001709bool DoFilledNewArray(const Instruction* inst,
1710 const ShadowFrame& shadow_frame,
1711 Thread* self,
1712 JValue* result) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001713 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
1714 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
1715 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
1716 if (!is_range) {
1717 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
1718 CHECK_LE(length, 5);
1719 }
1720 if (UNLIKELY(length < 0)) {
1721 ThrowNegativeArraySizeException(length);
1722 return false;
1723 }
1724 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08001725 ObjPtr<mirror::Class> array_class = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
Mathieu Chartieref41db72016-10-25 15:08:01 -07001726 shadow_frame.GetMethod(),
1727 self,
1728 false,
1729 do_access_check);
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001730 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001731 DCHECK(self->IsExceptionPending());
1732 return false;
1733 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001734 CHECK(array_class->IsArrayClass());
Mathieu Chartieref41db72016-10-25 15:08:01 -07001735 ObjPtr<mirror::Class> component_class = array_class->GetComponentType();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001736 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
1737 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
1738 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001739 ThrowRuntimeException("Bad filled array request for type %s",
David Sehr709b0702016-10-13 09:12:37 -07001740 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001741 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001742 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -08001743 "Found type %s; filled-new-array not implemented for anything but 'int'",
David Sehr709b0702016-10-13 09:12:37 -07001744 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001745 }
1746 return false;
1747 }
Mathieu Chartieref41db72016-10-25 15:08:01 -07001748 ObjPtr<mirror::Object> new_array = mirror::Array::Alloc<true>(
1749 self,
1750 array_class,
1751 length,
1752 array_class->GetComponentSizeShift(),
1753 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001754 if (UNLIKELY(new_array == nullptr)) {
1755 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001756 return false;
1757 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001758 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
1759 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001760 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +01001761 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001762 } else {
Ian Rogers29a26482014-05-02 15:27:29 -07001763 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +01001764 }
Sebastien Hertzabff6432014-01-27 18:01:39 +01001765 for (int32_t i = 0; i < length; ++i) {
1766 size_t src_reg = is_range ? vregC + i : arg[i];
1767 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001768 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
1769 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +01001770 } else {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001771 new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<transaction_active>(
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001772 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001773 }
1774 }
1775
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001776 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001777 return true;
1778}
1779
Mathieu Chartieref41db72016-10-25 15:08:01 -07001780// TODO: Use ObjPtr here.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001781template<typename T>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001782static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array,
1783 int32_t count)
1784 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001785 Runtime* runtime = Runtime::Current();
1786 for (int32_t i = 0; i < count; ++i) {
1787 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
1788 }
1789}
1790
Mathieu Chartieref41db72016-10-25 15:08:01 -07001791void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001792 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001793 DCHECK(Runtime::Current()->IsActiveTransaction());
1794 DCHECK(array != nullptr);
1795 DCHECK_LE(count, array->GetLength());
1796 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
1797 switch (primitive_component_type) {
1798 case Primitive::kPrimBoolean:
1799 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
1800 break;
1801 case Primitive::kPrimByte:
1802 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
1803 break;
1804 case Primitive::kPrimChar:
1805 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
1806 break;
1807 case Primitive::kPrimShort:
1808 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
1809 break;
1810 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001811 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
1812 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001813 case Primitive::kPrimFloat:
1814 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
1815 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001816 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001817 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
1818 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001819 case Primitive::kPrimDouble:
1820 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
1821 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001822 default:
1823 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
1824 << " in fill-array-data";
1825 break;
1826 }
1827}
1828
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001829// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +02001830#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001831 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001832 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
1833 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +02001834 const Instruction* inst, uint16_t inst_data, \
1835 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001836EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
1837EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
1838EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
1839EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
1840#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001841
Orion Hodsonc069a302017-01-18 09:23:12 +00001842// Explicit DoInvokePolymorphic template function declarations.
1843#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range) \
1844 template REQUIRES_SHARED(Locks::mutator_lock_) \
1845 bool DoInvokePolymorphic<_is_range>( \
1846 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1847 uint16_t inst_data, JValue* result)
1848EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false);
1849EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true);
Narayan Kamath9823e782016-08-03 12:46:58 +01001850#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1851
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001852// Explicit DoInvokeCustom template function declarations.
1853#define EXPLICIT_DO_INVOKE_CUSTOM_TEMPLATE_DECL(_is_range) \
1854 template REQUIRES_SHARED(Locks::mutator_lock_) \
1855 bool DoInvokeCustom<_is_range>( \
1856 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1857 uint16_t inst_data, JValue* result)
1858EXPLICIT_DO_INVOKE_CUSTOM_TEMPLATE_DECL(false);
1859EXPLICIT_DO_INVOKE_CUSTOM_TEMPLATE_DECL(true);
1860#undef EXPLICIT_DO_INVOKE_CUSTOM_TEMPLATE_DECL
1861
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001862// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001863#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001864 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001865 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
1866 const ShadowFrame& shadow_frame, \
1867 Thread* self, JValue* result)
1868#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
1869 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
1870 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
1871 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
1872 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
1873EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
1874EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
1875#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001876#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
1877
1878} // namespace interpreter
1879} // namespace art