blob: 0028b21f94230a6b1761ef7cf21bb1f7174dcd64 [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"
Daniel Mihalyieb076692014-08-22 17:33:31 +020022#include "debugger.h"
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +000023#include "entrypoints/runtime_asm_entrypoints.h"
Tamas Berghammerdd5e5e92016-02-12 16:29:00 +000024#include "jit/jit.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010025#include "jvalue.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010026#include "method_handles-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070027#include "method_handles.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010028#include "mirror/array-inl.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010029#include "mirror/class.h"
Narayan Kamath000e1882016-10-24 17:14:25 +010030#include "mirror/emulated_stack_frame.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080031#include "mirror/method_handle_impl-inl.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010032#include "reflection-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070033#include "reflection.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070034#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070035#include "thread-inl.h"
Chang Xingbd208d82017-07-12 14:53:17 -070036#include "transaction.h"
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +010037#include "well_known_classes.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020038
39namespace art {
40namespace interpreter {
41
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000042void ThrowNullPointerExceptionFromInterpreter() {
43 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -070044}
45
Chang Xingbd208d82017-07-12 14:53:17 -070046template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
47 bool transaction_active>
Ian Rogers54874942014-06-10 16:31:03 -070048bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
49 uint16_t inst_data) {
50 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
51 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010052 ArtField* f =
53 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
54 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -070055 if (UNLIKELY(f == nullptr)) {
56 CHECK(self->IsExceptionPending());
57 return false;
58 }
Mathieu Chartieref41db72016-10-25 15:08:01 -070059 ObjPtr<mirror::Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -070060 if (is_static) {
61 obj = f->GetDeclaringClass();
Chang Xingbd208d82017-07-12 14:53:17 -070062 if (transaction_active) {
63 if (Runtime::Current()->GetTransaction()->ReadConstraint(obj.Ptr(), f)) {
64 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Can't read static fields of "
65 + obj->PrettyTypeOf() + " since it does not belong to clinit's class.");
66 return false;
67 }
68 }
Ian Rogers54874942014-06-10 16:31:03 -070069 } else {
70 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
71 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000072 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers54874942014-06-10 16:31:03 -070073 return false;
74 }
75 }
Orion Hodson3d617ac2016-10-19 14:00:46 +010076
77 JValue result;
Alex Light084fa372017-06-16 08:58:34 -070078 if (UNLIKELY(!DoFieldGetCommon<field_type>(self, shadow_frame, obj, f, &result))) {
79 // Instrumentation threw an error!
80 CHECK(self->IsExceptionPending());
81 return false;
82 }
Ian Rogers54874942014-06-10 16:31:03 -070083 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
84 switch (field_type) {
85 case Primitive::kPrimBoolean:
Orion Hodson3d617ac2016-10-19 14:00:46 +010086 shadow_frame.SetVReg(vregA, result.GetZ());
Ian Rogers54874942014-06-10 16:31:03 -070087 break;
88 case Primitive::kPrimByte:
Orion Hodson3d617ac2016-10-19 14:00:46 +010089 shadow_frame.SetVReg(vregA, result.GetB());
Ian Rogers54874942014-06-10 16:31:03 -070090 break;
91 case Primitive::kPrimChar:
Orion Hodson3d617ac2016-10-19 14:00:46 +010092 shadow_frame.SetVReg(vregA, result.GetC());
Ian Rogers54874942014-06-10 16:31:03 -070093 break;
94 case Primitive::kPrimShort:
Orion Hodson3d617ac2016-10-19 14:00:46 +010095 shadow_frame.SetVReg(vregA, result.GetS());
Ian Rogers54874942014-06-10 16:31:03 -070096 break;
97 case Primitive::kPrimInt:
Orion Hodson3d617ac2016-10-19 14:00:46 +010098 shadow_frame.SetVReg(vregA, result.GetI());
Ian Rogers54874942014-06-10 16:31:03 -070099 break;
100 case Primitive::kPrimLong:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100101 shadow_frame.SetVRegLong(vregA, result.GetJ());
Ian Rogers54874942014-06-10 16:31:03 -0700102 break;
103 case Primitive::kPrimNot:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100104 shadow_frame.SetVRegReference(vregA, result.GetL());
Ian Rogers54874942014-06-10 16:31:03 -0700105 break;
106 default:
107 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700108 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700109 }
110 return true;
111}
112
113// Explicitly instantiate all DoFieldGet functions.
Chang Xingbd208d82017-07-12 14:53:17 -0700114#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
115 template bool DoFieldGet<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
Ian Rogers54874942014-06-10 16:31:03 -0700116 ShadowFrame& shadow_frame, \
117 const Instruction* inst, \
118 uint16_t inst_data)
119
120#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
Chang Xingbd208d82017-07-12 14:53:17 -0700121 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false, true); \
122 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false, false); \
123 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true, true); \
124 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true, false);
Ian Rogers54874942014-06-10 16:31:03 -0700125
126// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700127EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
128EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
129EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
130EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
131EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
132EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
133EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700134
135// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700136EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
137EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
138EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
139EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
140EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
141EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
142EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700143
144#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
145#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
146
147// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
148// Returns true on success, otherwise throws an exception and returns false.
149template<Primitive::Type field_type>
150bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700151 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700152 if (UNLIKELY(obj == nullptr)) {
153 // We lost the reference to the field index so we cannot get a more
154 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000155 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700156 return false;
157 }
158 MemberOffset field_offset(inst->VRegC_22c());
159 // Report this field access to instrumentation if needed. Since we only have the offset of
160 // the field from the base of the object, we need to look for it first.
161 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
162 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
163 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
164 field_offset.Uint32Value());
165 DCHECK(f != nullptr);
166 DCHECK(!f->IsStatic());
Alex Light084fa372017-06-16 08:58:34 -0700167 Thread* self = Thread::Current();
168 StackHandleScope<1> hs(self);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700169 // Save obj in case the instrumentation event has thread suspension.
170 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Alex Light084fa372017-06-16 08:58:34 -0700171 instrumentation->FieldReadEvent(self,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700172 obj.Ptr(),
173 shadow_frame.GetMethod(),
174 shadow_frame.GetDexPC(),
175 f);
Alex Light084fa372017-06-16 08:58:34 -0700176 if (UNLIKELY(self->IsExceptionPending())) {
177 return false;
178 }
Ian Rogers54874942014-06-10 16:31:03 -0700179 }
180 // Note: iget-x-quick instructions are only for non-volatile fields.
181 const uint32_t vregA = inst->VRegA_22c(inst_data);
182 switch (field_type) {
183 case Primitive::kPrimInt:
184 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
185 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800186 case Primitive::kPrimBoolean:
187 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
188 break;
189 case Primitive::kPrimByte:
190 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
191 break;
192 case Primitive::kPrimChar:
193 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
194 break;
195 case Primitive::kPrimShort:
196 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
197 break;
Ian Rogers54874942014-06-10 16:31:03 -0700198 case Primitive::kPrimLong:
199 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
200 break;
201 case Primitive::kPrimNot:
202 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
203 break;
204 default:
205 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700206 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700207 }
208 return true;
209}
210
211// Explicitly instantiate all DoIGetQuick functions.
212#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
213 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
214 uint16_t inst_data)
215
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800216EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
217EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
218EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
219EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
220EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
221EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
222EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700223#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
224
225template<Primitive::Type field_type>
226static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700227 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700228 JValue field_value;
229 switch (field_type) {
230 case Primitive::kPrimBoolean:
231 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
232 break;
233 case Primitive::kPrimByte:
234 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
235 break;
236 case Primitive::kPrimChar:
237 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
238 break;
239 case Primitive::kPrimShort:
240 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
241 break;
242 case Primitive::kPrimInt:
243 field_value.SetI(shadow_frame.GetVReg(vreg));
244 break;
245 case Primitive::kPrimLong:
246 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
247 break;
248 case Primitive::kPrimNot:
249 field_value.SetL(shadow_frame.GetVRegReference(vreg));
250 break;
251 default:
252 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700253 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700254 }
255 return field_value;
256}
257
Orion Hodson3d617ac2016-10-19 14:00:46 +0100258template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
259 bool transaction_active>
260bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
261 uint16_t inst_data) {
262 const bool do_assignability_check = do_access_check;
263 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
264 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
265 ArtField* f =
266 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
267 Primitive::ComponentSize(field_type));
268 if (UNLIKELY(f == nullptr)) {
269 CHECK(self->IsExceptionPending());
270 return false;
271 }
272 ObjPtr<mirror::Object> obj;
273 if (is_static) {
274 obj = f->GetDeclaringClass();
Chang Xingbd208d82017-07-12 14:53:17 -0700275 if (transaction_active) {
276 if (Runtime::Current()->GetTransaction()->WriteConstraint(obj.Ptr(), f)) {
277 Runtime::Current()->AbortTransactionAndThrowAbortError(
278 self, "Can't set fields of " + obj->PrettyTypeOf());
279 return false;
280 }
281 }
282
Orion Hodson3d617ac2016-10-19 14:00:46 +0100283 } else {
284 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
285 if (UNLIKELY(obj == nullptr)) {
286 ThrowNullPointerExceptionForFieldAccess(f, false);
287 return false;
288 }
289 }
290
291 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100292 JValue value = GetFieldValue<field_type>(shadow_frame, vregA);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100293 return DoFieldPutCommon<field_type, do_assignability_check, transaction_active>(self,
294 shadow_frame,
295 obj,
296 f,
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100297 value);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100298}
299
Ian Rogers54874942014-06-10 16:31:03 -0700300// Explicitly instantiate all DoFieldPut functions.
301#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
302 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
303 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
304
305#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
306 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
307 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
308 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
309 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
310
311// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700312EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
313EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
314EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
315EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
316EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
317EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
318EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700319
320// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700321EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
322EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
323EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
324EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
325EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
326EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
327EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700328
329#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
330#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
331
332template<Primitive::Type field_type, bool transaction_active>
333bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700334 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700335 if (UNLIKELY(obj == nullptr)) {
336 // We lost the reference to the field index so we cannot get a more
337 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000338 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700339 return false;
340 }
341 MemberOffset field_offset(inst->VRegC_22c());
342 const uint32_t vregA = inst->VRegA_22c(inst_data);
343 // Report this field modification to instrumentation if needed. Since we only have the offset of
344 // the field from the base of the object, we need to look for it first.
345 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
346 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
347 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
348 field_offset.Uint32Value());
349 DCHECK(f != nullptr);
350 DCHECK(!f->IsStatic());
351 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Alex Light084fa372017-06-16 08:58:34 -0700352 Thread* self = Thread::Current();
353 StackHandleScope<2> hs(self);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700354 // Save obj in case the instrumentation event has thread suspension.
355 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Alex Light084fa372017-06-16 08:58:34 -0700356 mirror::Object* fake_root = nullptr;
357 HandleWrapper<mirror::Object> ret(hs.NewHandleWrapper<mirror::Object>(
358 field_type == Primitive::kPrimNot ? field_value.GetGCRoot() : &fake_root));
359 instrumentation->FieldWriteEvent(self,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700360 obj.Ptr(),
361 shadow_frame.GetMethod(),
362 shadow_frame.GetDexPC(),
363 f,
364 field_value);
Alex Light084fa372017-06-16 08:58:34 -0700365 if (UNLIKELY(self->IsExceptionPending())) {
366 return false;
367 }
Ian Rogers54874942014-06-10 16:31:03 -0700368 }
369 // Note: iput-x-quick instructions are only for non-volatile fields.
370 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700371 case Primitive::kPrimBoolean:
372 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
373 break;
374 case Primitive::kPrimByte:
375 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
376 break;
377 case Primitive::kPrimChar:
378 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
379 break;
380 case Primitive::kPrimShort:
381 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
382 break;
Ian Rogers54874942014-06-10 16:31:03 -0700383 case Primitive::kPrimInt:
384 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
385 break;
386 case Primitive::kPrimLong:
387 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
388 break;
389 case Primitive::kPrimNot:
390 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
391 break;
392 default:
393 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700394 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700395 }
396 return true;
397}
398
399// Explicitly instantiate all DoIPutQuick functions.
400#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
401 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
402 const Instruction* inst, \
403 uint16_t inst_data)
404
405#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
406 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
407 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
408
Andreas Gampec8ccf682014-09-29 20:07:43 -0700409EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
410EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
411EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
412EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
413EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
414EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
415EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700416#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
417#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
418
Alex Light9fb1ab12017-09-05 09:32:49 -0700419// We execute any instrumentation events that are triggered by this exception and change the
420// shadow_frame's dex_pc to that of the exception handler if there is one in the current method.
421// Return true if we should continue executing in the current method and false if we need to go up
422// the stack to find an exception handler.
Sebastien Hertz520633b2015-09-08 17:03:36 +0200423// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Alex Light9fb1ab12017-09-05 09:32:49 -0700424// TODO We should have a better way to skip instrumentation reporting or possibly rethink that
425// behavior.
426bool MoveToExceptionHandler(Thread* self,
427 ShadowFrame& shadow_frame,
428 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700429 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700430 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000431 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Alex Light9fb1ab12017-09-05 09:32:49 -0700432 if (instrumentation != nullptr &&
433 instrumentation->HasExceptionThrownListeners() &&
434 self->IsExceptionThrownByCurrentMethod(exception.Get())) {
435 // 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 -0700436 instrumentation->ExceptionThrownEvent(self, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200437 }
Ian Rogers54874942014-06-10 16:31:03 -0700438 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700439 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
Alex Light9fb1ab12017-09-05 09:32:49 -0700440 hs.NewHandle(exception->GetClass()), shadow_frame.GetDexPC(), &clear_exception);
441 if (found_dex_pc == DexFile::kDexNoIndex) {
442 if (instrumentation != nullptr) {
443 if (shadow_frame.NeedsNotifyPop()) {
444 instrumentation->WatchedFramePopped(self, shadow_frame);
445 }
446 // Exception is not caught by the current method. We will unwind to the
447 // caller. Notify any instrumentation listener.
448 instrumentation->MethodUnwindEvent(self,
449 shadow_frame.GetThisObject(),
450 shadow_frame.GetMethod(),
451 shadow_frame.GetDexPC());
Alex Lighte814f9d2017-07-31 16:14:39 -0700452 }
Alex Light9fb1ab12017-09-05 09:32:49 -0700453 return false;
Ian Rogers54874942014-06-10 16:31:03 -0700454 } else {
Alex Light9fb1ab12017-09-05 09:32:49 -0700455 shadow_frame.SetDexPC(found_dex_pc);
456 if (instrumentation != nullptr && instrumentation->HasExceptionHandledListeners()) {
457 self->ClearException();
458 instrumentation->ExceptionHandledEvent(self, exception.Get());
459 if (UNLIKELY(self->IsExceptionPending())) {
460 // Exception handled event threw an exception. Try to find the handler for this one.
461 return MoveToExceptionHandler(self, shadow_frame, instrumentation);
462 } else if (!clear_exception) {
463 self->SetException(exception.Get());
464 }
465 } else if (clear_exception) {
Ian Rogers54874942014-06-10 16:31:03 -0700466 self->ClearException();
467 }
Alex Light9fb1ab12017-09-05 09:32:49 -0700468 return true;
Ian Rogers54874942014-06-10 16:31:03 -0700469 }
Ian Rogers54874942014-06-10 16:31:03 -0700470}
471
Ian Rogerse94652f2014-12-02 11:13:19 -0800472void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
473 LOG(FATAL) << "Unexpected instruction: "
474 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
475 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700476}
477
Sebastien Hertz45b15972015-04-03 16:07:05 +0200478void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700479 va_list args;
480 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200481 AbortTransactionV(self, fmt, args);
482 va_end(args);
483}
484
485void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
486 CHECK(Runtime::Current()->IsActiveTransaction());
487 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100488 std::string abort_msg;
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800489 android::base::StringAppendV(&abort_msg, fmt, args);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100490 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200491 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700492}
493
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100494// START DECLARATIONS :
495//
496// These additional declarations are required because clang complains
497// about ALWAYS_INLINE (-Werror, -Wgcc-compat) in definitions.
498//
499
Narayan Kamath9823e782016-08-03 12:46:58 +0100500template <bool is_range, bool do_assignability_check>
Vladimir Markod16363a2017-02-01 14:09:13 +0000501static ALWAYS_INLINE bool DoCallCommon(ArtMethod* called_method,
502 Thread* self,
503 ShadowFrame& shadow_frame,
504 JValue* result,
505 uint16_t number_of_inputs,
506 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
507 uint32_t vregC) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100508
509template <bool is_range>
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000510ALWAYS_INLINE void CopyRegisters(ShadowFrame& caller_frame,
511 ShadowFrame* callee_frame,
512 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
513 const size_t first_src_reg,
514 const size_t first_dest_reg,
515 const size_t num_regs) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100516
517// END DECLARATIONS.
518
Siva Chandra05d24152016-01-05 17:43:17 -0800519void ArtInterpreterToCompiledCodeBridge(Thread* self,
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100520 ArtMethod* caller,
Siva Chandra05d24152016-01-05 17:43:17 -0800521 ShadowFrame* shadow_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -0700522 uint16_t arg_offset,
Siva Chandra05d24152016-01-05 17:43:17 -0800523 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700524 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700525 ArtMethod* method = shadow_frame->GetMethod();
526 // Ensure static methods are initialized.
527 if (method->IsStatic()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700528 ObjPtr<mirror::Class> declaringClass = method->GetDeclaringClass();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700529 if (UNLIKELY(!declaringClass->IsInitialized())) {
530 self->PushShadowFrame(shadow_frame);
531 StackHandleScope<1> hs(self);
532 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
Nicolas Geoffray01822292017-03-09 09:03:19 +0000533 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
534 true))) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700535 self->PopShadowFrame();
536 DCHECK(self->IsExceptionPending());
537 return;
538 }
539 self->PopShadowFrame();
540 CHECK(h_class->IsInitializing());
Nicolas Geoffray01822292017-03-09 09:03:19 +0000541 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
542 method = shadow_frame->GetMethod();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700543 }
544 }
Jeff Hao5ea84132017-05-05 16:59:29 -0700545 // Basic checks for the arg_offset. If there's no code item, the arg_offset must be 0. Otherwise,
546 // check that the arg_offset isn't greater than the number of registers. A stronger check is
547 // difficult since the frame may contain space for all the registers in the method, or only enough
548 // space for the arguments.
549 if (kIsDebugBuild) {
550 if (method->GetCodeItem() == nullptr) {
551 DCHECK_EQ(0u, arg_offset) << method->PrettyMethod();
552 } else {
553 DCHECK_LE(arg_offset, shadow_frame->NumberOfVRegs());
554 }
555 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100556 jit::Jit* jit = Runtime::Current()->GetJit();
557 if (jit != nullptr && caller != nullptr) {
558 jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
559 }
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700560 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
561 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
Andreas Gampe542451c2016-07-26 09:02:02 -0700562 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700563}
564
Mingyao Yangffedec52016-05-19 10:48:40 -0700565void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
566 uint16_t this_obj_vreg,
567 JValue result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700568 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700569 ObjPtr<mirror::Object> existing = shadow_frame->GetVRegReference(this_obj_vreg);
Mingyao Yangffedec52016-05-19 10:48:40 -0700570 if (existing == nullptr) {
571 // If it's null, we come from compiled code that was deoptimized. Nothing to do,
572 // as the compiler verified there was no alias.
573 // Set the new string result of the StringFactory.
574 shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
575 return;
576 }
577 // Set the string init result into all aliases.
578 for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
579 if (shadow_frame->GetVRegReference(i) == existing) {
580 DCHECK_EQ(shadow_frame->GetVRegReference(i),
581 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
582 shadow_frame->SetVRegReference(i, result.GetL());
583 DCHECK_EQ(shadow_frame->GetVRegReference(i),
584 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
585 }
586 }
587}
588
Orion Hodsonc069a302017-01-18 09:23:12 +0000589template<bool is_range>
Orion Hodson811bd5f2016-12-07 11:35:37 +0000590bool DoInvokePolymorphic(Thread* self,
591 ShadowFrame& shadow_frame,
592 const Instruction* inst,
593 uint16_t inst_data,
594 JValue* result)
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100595 REQUIRES_SHARED(Locks::mutator_lock_) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100596 // Invoke-polymorphic instructions always take a receiver. i.e, they are never static.
597 const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc();
Orion Hodson3d617ac2016-10-19 14:00:46 +0100598 const int invoke_method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc();
Narayan Kamath9823e782016-08-03 12:46:58 +0100599
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000600 // Initialize |result| to 0 as this is the default return value for
601 // polymorphic invocations of method handle types with void return
602 // and provides sane return result in error cases.
603 result->SetJ(0);
604
Orion Hodson3d617ac2016-10-19 14:00:46 +0100605 // The invoke_method_idx here is the name of the signature polymorphic method that
Narayan Kamath9823e782016-08-03 12:46:58 +0100606 // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact)
607 // and not the method that we'll dispatch to in the end.
Orion Hodson811bd5f2016-12-07 11:35:37 +0000608 StackHandleScope<5> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +0000609 Handle<mirror::MethodHandle> method_handle(hs.NewHandle(
610 ObjPtr<mirror::MethodHandle>::DownCast(
Mathieu Chartieref41db72016-10-25 15:08:01 -0700611 MakeObjPtr(shadow_frame.GetVRegReference(vRegC)))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800612 if (UNLIKELY(method_handle == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100613 // Note that the invoke type is kVirtual here because a call to a signature
614 // polymorphic method is shaped like a virtual call at the bytecode level.
Orion Hodson3d617ac2016-10-19 14:00:46 +0100615 ThrowNullPointerExceptionForMethodAccess(invoke_method_idx, InvokeType::kVirtual);
Narayan Kamath9823e782016-08-03 12:46:58 +0100616 return false;
617 }
618
619 // The vRegH value gives the index of the proto_id associated with this
Orion Hodson811bd5f2016-12-07 11:35:37 +0000620 // signature polymorphic call site.
Narayan Kamath9823e782016-08-03 12:46:58 +0100621 const uint32_t callsite_proto_id = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
622
623 // Call through to the classlinker and ask it to resolve the static type associated
624 // with the callsite. This information is stored in the dex cache so it's
625 // guaranteed to be fast after the first resolution.
Narayan Kamath9823e782016-08-03 12:46:58 +0100626 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Narayan Kamath208f8572016-08-03 12:46:58 +0100627 Handle<mirror::Class> caller_class(hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass()));
628 Handle<mirror::MethodType> callsite_type(hs.NewHandle(class_linker->ResolveMethodType(
Narayan Kamath9823e782016-08-03 12:46:58 +0100629 caller_class->GetDexFile(), callsite_proto_id,
630 hs.NewHandle<mirror::DexCache>(caller_class->GetDexCache()),
Narayan Kamath208f8572016-08-03 12:46:58 +0100631 hs.NewHandle<mirror::ClassLoader>(caller_class->GetClassLoader()))));
Narayan Kamath9823e782016-08-03 12:46:58 +0100632
633 // This implies we couldn't resolve one or more types in this method handle.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800634 if (UNLIKELY(callsite_type == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100635 CHECK(self->IsExceptionPending());
Narayan Kamath9823e782016-08-03 12:46:58 +0100636 return false;
637 }
638
Orion Hodson811bd5f2016-12-07 11:35:37 +0000639 ArtMethod* invoke_method =
Vladimir Markoba118822017-06-12 15:41:56 +0100640 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
641 self, invoke_method_idx, shadow_frame.GetMethod(), kVirtual);
Narayan Kamath0a8485e2016-11-02 18:47:11 +0000642
Orion Hodson811bd5f2016-12-07 11:35:37 +0000643 // There is a common dispatch method for method handles that takes
644 // arguments either from a range or an array of arguments depending
645 // on whether the DEX instruction is invoke-polymorphic/range or
646 // invoke-polymorphic. The array here is for the latter.
647 uint32_t args[Instruction::kMaxVarArgRegs] = {};
Narayan Kamath9823e782016-08-03 12:46:58 +0100648 if (is_range) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000649 // VRegC is the register holding the method handle. Arguments passed
650 // to the method handle's target do not include the method handle.
651 uint32_t first_arg = inst->VRegC_4rcc() + 1;
Orion Hodsonc069a302017-01-18 09:23:12 +0000652 return DoInvokePolymorphic<is_range>(self,
653 invoke_method,
654 shadow_frame,
655 method_handle,
656 callsite_type,
657 args /* unused */,
658 first_arg,
659 result);
Narayan Kamath9823e782016-08-03 12:46:58 +0100660 } else {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000661 // Get the register arguments for the invoke.
662 inst->GetVarArgs(args, inst_data);
663 // Drop the first register which is the method handle performing the invoke.
Alexey Frunze8631a462017-01-19 19:07:37 -0800664 memmove(args, args + 1, sizeof(args[0]) * (Instruction::kMaxVarArgRegs - 1));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000665 args[Instruction::kMaxVarArgRegs - 1] = 0;
Orion Hodsonc069a302017-01-18 09:23:12 +0000666 return DoInvokePolymorphic<is_range>(self,
667 invoke_method,
668 shadow_frame,
669 method_handle,
670 callsite_type,
671 args,
672 args[0],
673 result);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100674 }
675}
676
Orion Hodsonc069a302017-01-18 09:23:12 +0000677static ObjPtr<mirror::CallSite> InvokeBootstrapMethod(Thread* self,
678 ShadowFrame& shadow_frame,
679 uint32_t call_site_idx)
680 REQUIRES_SHARED(Locks::mutator_lock_) {
681 ArtMethod* referrer = shadow_frame.GetMethod();
682 const DexFile* dex_file = referrer->GetDexFile();
683 const DexFile::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx);
684
Orion Hodson631827d2017-04-10 14:53:47 +0100685 StackHandleScope<10> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +0000686 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader()));
687 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
688
689 CallSiteArrayValueIterator it(*dex_file, csi);
690 uint32_t method_handle_idx = static_cast<uint32_t>(it.GetJavaValue().i);
691 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
692 Handle<mirror::MethodHandle>
693 bootstrap(hs.NewHandle(class_linker->ResolveMethodHandle(method_handle_idx, referrer)));
694 if (bootstrap.IsNull()) {
695 DCHECK(self->IsExceptionPending());
696 return nullptr;
697 }
698 Handle<mirror::MethodType> bootstrap_method_type = hs.NewHandle(bootstrap->GetMethodType());
699 it.Next();
700
701 DCHECK_EQ(static_cast<size_t>(bootstrap->GetMethodType()->GetPTypes()->GetLength()), it.Size());
702 const size_t num_bootstrap_vregs = bootstrap->GetMethodType()->NumberOfVRegs();
703
704 // Set-up a shadow frame for invoking the bootstrap method handle.
705 ShadowFrameAllocaUniquePtr bootstrap_frame =
706 CREATE_SHADOW_FRAME(num_bootstrap_vregs, nullptr, referrer, shadow_frame.GetDexPC());
707 ScopedStackedShadowFramePusher pusher(
708 self, bootstrap_frame.get(), StackedShadowFrameType::kShadowFrameUnderConstruction);
709 size_t vreg = 0;
710
711 // The first parameter is a MethodHandles lookup instance.
712 {
713 Handle<mirror::Class> lookup_class(hs.NewHandle(bootstrap->GetTargetClass()));
714 ObjPtr<mirror::MethodHandlesLookup> lookup =
715 mirror::MethodHandlesLookup::Create(self, lookup_class);
716 if (lookup.IsNull()) {
717 DCHECK(self->IsExceptionPending());
718 return nullptr;
719 }
720 bootstrap_frame->SetVRegReference(vreg++, lookup.Ptr());
721 }
722
723 // The second parameter is the name to lookup.
724 {
725 dex::StringIndex name_idx(static_cast<uint32_t>(it.GetJavaValue().i));
726 ObjPtr<mirror::String> name = class_linker->ResolveString(*dex_file, name_idx, dex_cache);
727 if (name.IsNull()) {
728 DCHECK(self->IsExceptionPending());
729 return nullptr;
730 }
731 bootstrap_frame->SetVRegReference(vreg++, name.Ptr());
732 }
733 it.Next();
734
735 // The third parameter is the method type associated with the name.
736 uint32_t method_type_idx = static_cast<uint32_t>(it.GetJavaValue().i);
737 Handle<mirror::MethodType>
738 method_type(hs.NewHandle(class_linker->ResolveMethodType(*dex_file,
739 method_type_idx,
740 dex_cache,
741 class_loader)));
742 if (method_type.IsNull()) {
743 DCHECK(self->IsExceptionPending());
744 return nullptr;
745 }
746 bootstrap_frame->SetVRegReference(vreg++, method_type.Get());
747 it.Next();
748
749 // Append remaining arguments (if any).
750 while (it.HasNext()) {
751 const jvalue& jvalue = it.GetJavaValue();
752 switch (it.GetValueType()) {
753 case EncodedArrayValueIterator::ValueType::kBoolean:
754 case EncodedArrayValueIterator::ValueType::kByte:
755 case EncodedArrayValueIterator::ValueType::kChar:
756 case EncodedArrayValueIterator::ValueType::kShort:
757 case EncodedArrayValueIterator::ValueType::kInt:
758 bootstrap_frame->SetVReg(vreg, jvalue.i);
759 vreg += 1;
760 break;
761 case EncodedArrayValueIterator::ValueType::kLong:
762 bootstrap_frame->SetVRegLong(vreg, jvalue.j);
763 vreg += 2;
764 break;
765 case EncodedArrayValueIterator::ValueType::kFloat:
766 bootstrap_frame->SetVRegFloat(vreg, jvalue.f);
767 vreg += 1;
768 break;
769 case EncodedArrayValueIterator::ValueType::kDouble:
770 bootstrap_frame->SetVRegDouble(vreg, jvalue.d);
771 vreg += 2;
772 break;
773 case EncodedArrayValueIterator::ValueType::kMethodType: {
774 uint32_t idx = static_cast<uint32_t>(jvalue.i);
775 ObjPtr<mirror::MethodType> ref =
776 class_linker->ResolveMethodType(*dex_file, idx, dex_cache, class_loader);
777 if (ref.IsNull()) {
778 DCHECK(self->IsExceptionPending());
779 return nullptr;
780 }
781 bootstrap_frame->SetVRegReference(vreg, ref.Ptr());
782 vreg += 1;
783 break;
784 }
785 case EncodedArrayValueIterator::ValueType::kMethodHandle: {
786 uint32_t idx = static_cast<uint32_t>(jvalue.i);
787 ObjPtr<mirror::MethodHandle> ref =
788 class_linker->ResolveMethodHandle(idx, referrer);
789 if (ref.IsNull()) {
790 DCHECK(self->IsExceptionPending());
791 return nullptr;
792 }
793 bootstrap_frame->SetVRegReference(vreg, ref.Ptr());
794 vreg += 1;
795 break;
796 }
797 case EncodedArrayValueIterator::ValueType::kString: {
798 dex::StringIndex idx(static_cast<uint32_t>(jvalue.i));
799 ObjPtr<mirror::String> ref = class_linker->ResolveString(*dex_file, idx, dex_cache);
800 if (ref.IsNull()) {
801 DCHECK(self->IsExceptionPending());
802 return nullptr;
803 }
804 bootstrap_frame->SetVRegReference(vreg, ref.Ptr());
805 vreg += 1;
806 break;
807 }
808 case EncodedArrayValueIterator::ValueType::kType: {
809 dex::TypeIndex idx(static_cast<uint32_t>(jvalue.i));
810 ObjPtr<mirror::Class> ref =
811 class_linker->ResolveType(*dex_file, idx, dex_cache, class_loader);
812 if (ref.IsNull()) {
813 DCHECK(self->IsExceptionPending());
814 return nullptr;
815 }
816 bootstrap_frame->SetVRegReference(vreg, ref.Ptr());
817 vreg += 1;
818 break;
819 }
820 case EncodedArrayValueIterator::ValueType::kNull:
821 bootstrap_frame->SetVRegReference(vreg, nullptr);
822 vreg += 1;
823 break;
824 case EncodedArrayValueIterator::ValueType::kField:
825 case EncodedArrayValueIterator::ValueType::kMethod:
826 case EncodedArrayValueIterator::ValueType::kEnum:
827 case EncodedArrayValueIterator::ValueType::kArray:
828 case EncodedArrayValueIterator::ValueType::kAnnotation:
829 // Unreachable based on current EncodedArrayValueIterator::Next().
830 UNREACHABLE();
831 }
832
833 it.Next();
834 }
835
836 // Invoke the bootstrap method handle.
837 JValue result;
838
839 // This array of arguments is unused. DoInvokePolymorphic() operates on either a
840 // an argument array or a range, but always takes an array argument.
841 uint32_t args_unused[Instruction::kMaxVarArgRegs];
842 ArtMethod* invoke_exact =
843 jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_invokeExact);
844 bool invoke_success = DoInvokePolymorphic<true /* is_range */>(self,
845 invoke_exact,
846 *bootstrap_frame,
847 bootstrap,
848 bootstrap_method_type,
849 args_unused,
850 0,
851 &result);
852 if (!invoke_success) {
853 DCHECK(self->IsExceptionPending());
854 return nullptr;
855 }
856
857 Handle<mirror::Object> object(hs.NewHandle(result.GetL()));
858
859 // Check the result is not null.
860 if (UNLIKELY(object.IsNull())) {
861 ThrowNullPointerException("CallSite == null");
862 return nullptr;
863 }
864
865 // Check the result type is a subclass of CallSite.
866 if (UNLIKELY(!object->InstanceOf(mirror::CallSite::StaticClass()))) {
867 ThrowClassCastException(object->GetClass(), mirror::CallSite::StaticClass());
868 return nullptr;
869 }
870
871 Handle<mirror::CallSite> call_site =
872 hs.NewHandle(ObjPtr<mirror::CallSite>::DownCast(ObjPtr<mirror::Object>(result.GetL())));
873
874 // Check the call site target is not null as we're going to invoke it.
875 Handle<mirror::MethodHandle> target = hs.NewHandle(call_site->GetTarget());
876 if (UNLIKELY(target.IsNull())) {
877 ThrowNullPointerException("CallSite target == null");
878 return nullptr;
879 }
880
Orion Hodson631827d2017-04-10 14:53:47 +0100881 // Check the target method type matches the method type requested modulo the receiver
882 // needs to be compatible rather than exact.
883 Handle<mirror::MethodType> target_method_type = hs.NewHandle(target->GetMethodType());
884 if (UNLIKELY(!target_method_type->IsExactMatch(method_type.Get()) &&
885 !IsParameterTypeConvertible(target_method_type->GetPTypes()->GetWithoutChecks(0),
886 method_type->GetPTypes()->GetWithoutChecks(0)))) {
887 ThrowWrongMethodTypeException(target_method_type.Get(), method_type.Get());
Orion Hodsonc069a302017-01-18 09:23:12 +0000888 return nullptr;
889 }
890
891 return call_site.Get();
892}
893
894template<bool is_range>
895bool DoInvokeCustom(Thread* self,
896 ShadowFrame& shadow_frame,
897 const Instruction* inst,
898 uint16_t inst_data,
899 JValue* result)
900 REQUIRES_SHARED(Locks::mutator_lock_) {
901 // invoke-custom is not supported in transactions. In transactions
902 // there is a limited set of types supported. invoke-custom allows
903 // running arbitrary code and instantiating arbitrary types.
904 CHECK(!Runtime::Current()->IsActiveTransaction());
905 StackHandleScope<4> hs(self);
906 Handle<mirror::DexCache> dex_cache(hs.NewHandle(shadow_frame.GetMethod()->GetDexCache()));
907 const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
908 MutableHandle<mirror::CallSite>
909 call_site(hs.NewHandle(dex_cache->GetResolvedCallSite(call_site_idx)));
910 if (call_site.IsNull()) {
911 call_site.Assign(InvokeBootstrapMethod(self, shadow_frame, call_site_idx));
912 if (UNLIKELY(call_site.IsNull())) {
913 CHECK(self->IsExceptionPending());
914 ThrowWrappedBootstrapMethodError("Exception from call site #%u bootstrap method",
915 call_site_idx);
916 result->SetJ(0);
917 return false;
918 }
919 mirror::CallSite* winning_call_site =
920 dex_cache->SetResolvedCallSite(call_site_idx, call_site.Get());
921 call_site.Assign(winning_call_site);
922 }
923
924 // CallSite.java checks the re-assignment of the call site target
925 // when mutating call site targets. We only check the target is
926 // non-null and has the right type during bootstrap method execution.
927 Handle<mirror::MethodHandle> target = hs.NewHandle(call_site->GetTarget());
928 Handle<mirror::MethodType> target_method_type = hs.NewHandle(target->GetMethodType());
929 DCHECK_EQ(static_cast<size_t>(inst->VRegA()), target_method_type->NumberOfVRegs());
930
931 uint32_t args[Instruction::kMaxVarArgRegs];
932 if (is_range) {
933 args[0] = inst->VRegC_3rc();
934 } else {
935 inst->GetVarArgs(args, inst_data);
936 }
937
938 ArtMethod* invoke_exact =
939 jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_invokeExact);
940 return DoInvokePolymorphic<is_range>(self,
941 invoke_exact,
942 shadow_frame,
943 target,
944 target_method_type,
945 args,
946 args[0],
947 result);
948}
949
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100950template <bool is_range>
951inline void CopyRegisters(ShadowFrame& caller_frame,
952 ShadowFrame* callee_frame,
953 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
954 const size_t first_src_reg,
955 const size_t first_dest_reg,
956 const size_t num_regs) {
957 if (is_range) {
958 const size_t dest_reg_bound = first_dest_reg + num_regs;
959 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound;
960 ++dest_reg, ++src_reg) {
961 AssignRegister(callee_frame, caller_frame, dest_reg, src_reg);
962 }
963 } else {
964 DCHECK_LE(num_regs, arraysize(arg));
965
966 for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) {
967 AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]);
968 }
969 }
970}
971
Igor Murashkin6918bf12015-09-27 19:19:06 -0700972template <bool is_range,
Narayan Kamath370423d2016-10-03 16:51:22 +0100973 bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -0700974static inline bool DoCallCommon(ArtMethod* called_method,
975 Thread* self,
976 ShadowFrame& shadow_frame,
977 JValue* result,
978 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +0100979 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -0700980 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800981 bool string_init = false;
982 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700983 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
984 && called_method->IsConstructor())) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100985 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
Jeff Hao848f70a2014-01-15 13:49:50 -0800986 string_init = true;
987 }
988
Alex Lightdaf58c82016-03-16 23:00:49 +0000989 // Compute method information.
990 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
Igor Murashkin158f35c2015-06-10 15:55:30 -0700991 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200992 uint16_t num_regs;
Jeff Hao5ea84132017-05-05 16:59:29 -0700993 // Test whether to use the interpreter or compiler entrypoint, and save that result to pass to
994 // PerformCall. A deoptimization could occur at any time, and we shouldn't change which
995 // entrypoint to use once we start building the shadow frame.
Mathieu Chartier448bbcf2017-07-06 12:05:13 -0700996
997 // For unstarted runtimes, always use the interpreter entrypoint. This fixes the case where we are
998 // doing cross compilation. Note that GetEntryPointFromQuickCompiledCode doesn't use the image
999 // pointer size here and this may case an overflow if it is called from the compiler. b/62402160
1000 const bool use_interpreter_entrypoint = !Runtime::Current()->IsStarted() ||
1001 ClassLinker::ShouldUseInterpreterEntrypoint(
1002 called_method,
1003 called_method->GetEntryPointFromQuickCompiledCode());
Nicolas Geoffray01822292017-03-09 09:03:19 +00001004 if (LIKELY(code_item != nullptr)) {
Jeff Hao5ea84132017-05-05 16:59:29 -07001005 // When transitioning to compiled code, space only needs to be reserved for the input registers.
1006 // The rest of the frame gets discarded. This also prevents accessing the called method's code
1007 // item, saving memory by keeping code items of compiled code untouched.
Mathieu Chartier448bbcf2017-07-06 12:05:13 -07001008 if (!use_interpreter_entrypoint) {
1009 DCHECK(!Runtime::Current()->IsAotCompiler()) << "Compiler should use interpreter entrypoint";
Jeff Hao5ea84132017-05-05 16:59:29 -07001010 num_regs = number_of_inputs;
1011 } else {
1012 num_regs = code_item->registers_size_;
1013 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_);
1014 }
Nicolas Geoffray01822292017-03-09 09:03:19 +00001015 } else {
1016 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1017 num_regs = number_of_inputs;
Jeff Haodf79ddb2017-02-27 14:47:06 -08001018 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001019
Igor Murashkin158f35c2015-06-10 15:55:30 -07001020 // Hack for String init:
1021 //
1022 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
1023 // invoke-x StringFactory(a, b, c, ...)
1024 // by effectively dropping the first virtual register from the invoke.
1025 //
1026 // (at this point the ArtMethod has already been replaced,
1027 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +00001028 //
1029 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
1030 // to handle the compiler optimization of replacing `this` with null without
1031 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001032 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -07001033 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001034 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 -07001035
Igor Murashkin158f35c2015-06-10 15:55:30 -07001036 // The new StringFactory call is static and has one fewer argument.
Igor Murashkina06b49b2015-06-25 15:18:12 -07001037 if (code_item == nullptr) {
1038 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1039 num_regs--;
1040 } // 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 -07001041 number_of_inputs--;
1042
1043 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -07001044 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001045 arg[i - 1] = arg[i];
1046 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07001047 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001048
1049 // Rewrite the non-var-arg case
1050 vregC++; // Skips the 0th vreg in the range ("this").
1051 }
1052
1053 // Parameter registers go at the end of the shadow frame.
1054 DCHECK_GE(num_regs, number_of_inputs);
1055 size_t first_dest_reg = num_regs - number_of_inputs;
1056 DCHECK_NE(first_dest_reg, (size_t)-1);
1057
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001058 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001059 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -07001060 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -07001061 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -07001062 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001063
Igor Murashkin158f35c2015-06-10 15:55:30 -07001064 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -07001065 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001066 // Slow path.
1067 // We might need to do class loading, which incurs a thread state change to kNative. So
1068 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07001069 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +02001070 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001071 self->EndAssertNoThreadSuspension(old_cause);
1072
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001073 // ArtMethod here is needed to check type information of the call site against the callee.
1074 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
1075 //
1076 // As a special case for proxy methods, which are not dex-backed,
1077 // we have to retrieve type information from the proxy's method
1078 // interface method instead (which is dex backed since proxies are never interfaces).
Andreas Gampe542451c2016-07-26 09:02:02 -07001079 ArtMethod* method =
1080 new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001081
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001082 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001083 // to get the exact type of each reference argument.
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001084 const DexFile::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001085 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001086 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001087
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001088 // Handle receiver apart since it's not part of the shorty.
1089 size_t dest_reg = first_dest_reg;
1090 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001091
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001092 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001093 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001094 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
1095 ++dest_reg;
1096 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -07001097 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001098 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001099
1100 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001101 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001102 // Skip the 0th 'shorty' type since it represents the return type.
1103 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001104 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
1105 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001106 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001107 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001108 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001109 if (do_assignability_check && o != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001110 const dex::TypeIndex type_idx = params->GetTypeItem(shorty_pos).type_idx_;
Vladimir Marko942fd312017-01-16 20:52:19 +00001111 ObjPtr<mirror::Class> arg_type = method->GetDexCache()->GetResolvedType(type_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001112 if (arg_type == nullptr) {
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001113 StackHandleScope<1> hs(self);
1114 // Preserve o since it is used below and GetClassFromTypeIndex may cause thread
1115 // suspension.
1116 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&o);
Vladimir Markob45528c2017-07-27 14:14:28 +01001117 arg_type = method->ResolveClassFromTypeIndex(type_idx);
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001118 if (arg_type == nullptr) {
1119 CHECK(self->IsExceptionPending());
1120 return false;
1121 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001122 }
1123 if (!o->VerifierInstanceOf(arg_type)) {
1124 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -07001125 std::string temp1, temp2;
Orion Hodsonfef06642016-11-25 16:07:11 +00001126 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001127 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -08001128 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -07001129 o->GetClass()->GetDescriptor(&temp1),
1130 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001131 return false;
1132 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001133 }
Mathieu Chartieref41db72016-10-25 15:08:01 -07001134 new_shadow_frame->SetVRegReference(dest_reg, o.Ptr());
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001135 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -07001136 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001137 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001138 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001139 uint64_t wide_value =
1140 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
1141 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001142 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001143 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001144 ++dest_reg;
1145 ++arg_offset;
1146 break;
1147 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001148 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001149 default:
1150 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
1151 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001152 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001153 }
1154 } else {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001155 if (is_range) {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001156 DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001157 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001158
1159 CopyRegisters<is_range>(shadow_frame,
1160 new_shadow_frame,
1161 arg,
1162 vregC,
1163 first_dest_reg,
1164 number_of_inputs);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001165 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001166 }
1167
Jeff Hao5ea84132017-05-05 16:59:29 -07001168 PerformCall(self,
1169 code_item,
1170 shadow_frame.GetMethod(),
1171 first_dest_reg,
1172 new_shadow_frame,
1173 result,
1174 use_interpreter_entrypoint);
Jeff Hao848f70a2014-01-15 13:49:50 -08001175
1176 if (string_init && !self->IsExceptionPending()) {
Mingyao Yangffedec52016-05-19 10:48:40 -07001177 SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001178 }
1179
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001180 return !self->IsExceptionPending();
1181}
1182
Igor Murashkin158f35c2015-06-10 15:55:30 -07001183template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001184bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
1185 const Instruction* inst, uint16_t inst_data, JValue* result) {
1186 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001187 const uint16_t number_of_inputs =
1188 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001189
1190 // TODO: find a cleaner way to separate non-range and range information without duplicating
1191 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -07001192 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001193 uint32_t vregC = 0;
1194 if (is_range) {
1195 vregC = inst->VRegC_3rc();
1196 } else {
1197 vregC = inst->VRegC_35c();
1198 inst->GetVarArgs(arg, inst_data);
1199 }
1200
1201 return DoCallCommon<is_range, do_assignability_check>(
1202 called_method, self, shadow_frame,
1203 result, number_of_inputs, arg, vregC);
1204}
1205
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001206template <bool is_range, bool do_access_check, bool transaction_active>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001207bool DoFilledNewArray(const Instruction* inst,
1208 const ShadowFrame& shadow_frame,
1209 Thread* self,
1210 JValue* result) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001211 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
1212 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
1213 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
1214 if (!is_range) {
1215 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
1216 CHECK_LE(length, 5);
1217 }
1218 if (UNLIKELY(length < 0)) {
1219 ThrowNegativeArraySizeException(length);
1220 return false;
1221 }
1222 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08001223 ObjPtr<mirror::Class> array_class = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
Mathieu Chartieref41db72016-10-25 15:08:01 -07001224 shadow_frame.GetMethod(),
1225 self,
1226 false,
1227 do_access_check);
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001228 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001229 DCHECK(self->IsExceptionPending());
1230 return false;
1231 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001232 CHECK(array_class->IsArrayClass());
Mathieu Chartieref41db72016-10-25 15:08:01 -07001233 ObjPtr<mirror::Class> component_class = array_class->GetComponentType();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001234 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
1235 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
1236 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001237 ThrowRuntimeException("Bad filled array request for type %s",
David Sehr709b0702016-10-13 09:12:37 -07001238 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001239 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001240 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -08001241 "Found type %s; filled-new-array not implemented for anything but 'int'",
David Sehr709b0702016-10-13 09:12:37 -07001242 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001243 }
1244 return false;
1245 }
Mathieu Chartieref41db72016-10-25 15:08:01 -07001246 ObjPtr<mirror::Object> new_array = mirror::Array::Alloc<true>(
1247 self,
1248 array_class,
1249 length,
1250 array_class->GetComponentSizeShift(),
1251 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001252 if (UNLIKELY(new_array == nullptr)) {
1253 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001254 return false;
1255 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001256 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
1257 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001258 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +01001259 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001260 } else {
Ian Rogers29a26482014-05-02 15:27:29 -07001261 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +01001262 }
Sebastien Hertzabff6432014-01-27 18:01:39 +01001263 for (int32_t i = 0; i < length; ++i) {
1264 size_t src_reg = is_range ? vregC + i : arg[i];
1265 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001266 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
1267 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +01001268 } else {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001269 new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<transaction_active>(
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001270 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001271 }
1272 }
1273
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001274 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001275 return true;
1276}
1277
Mathieu Chartieref41db72016-10-25 15:08:01 -07001278// TODO: Use ObjPtr here.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001279template<typename T>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001280static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array,
1281 int32_t count)
1282 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001283 Runtime* runtime = Runtime::Current();
1284 for (int32_t i = 0; i < count; ++i) {
1285 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
1286 }
1287}
1288
Mathieu Chartieref41db72016-10-25 15:08:01 -07001289void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001290 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001291 DCHECK(Runtime::Current()->IsActiveTransaction());
1292 DCHECK(array != nullptr);
1293 DCHECK_LE(count, array->GetLength());
1294 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
1295 switch (primitive_component_type) {
1296 case Primitive::kPrimBoolean:
1297 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
1298 break;
1299 case Primitive::kPrimByte:
1300 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
1301 break;
1302 case Primitive::kPrimChar:
1303 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
1304 break;
1305 case Primitive::kPrimShort:
1306 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
1307 break;
1308 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001309 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
1310 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001311 case Primitive::kPrimFloat:
1312 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
1313 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001314 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001315 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
1316 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001317 case Primitive::kPrimDouble:
1318 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
1319 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001320 default:
1321 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
1322 << " in fill-array-data";
1323 break;
1324 }
1325}
1326
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001327// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +02001328#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001329 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001330 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
1331 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +02001332 const Instruction* inst, uint16_t inst_data, \
1333 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001334EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
1335EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
1336EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
1337EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
1338#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001339
Orion Hodsonc069a302017-01-18 09:23:12 +00001340// Explicit DoInvokeCustom template function declarations.
1341#define EXPLICIT_DO_INVOKE_CUSTOM_TEMPLATE_DECL(_is_range) \
1342 template REQUIRES_SHARED(Locks::mutator_lock_) \
1343 bool DoInvokeCustom<_is_range>( \
1344 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
Narayan Kamath9823e782016-08-03 12:46:58 +01001345 uint16_t inst_data, JValue* result)
Orion Hodsonc069a302017-01-18 09:23:12 +00001346EXPLICIT_DO_INVOKE_CUSTOM_TEMPLATE_DECL(false);
1347EXPLICIT_DO_INVOKE_CUSTOM_TEMPLATE_DECL(true);
1348#undef EXPLICIT_DO_INVOKE_CUSTOM_TEMPLATE_DECL
Narayan Kamath9823e782016-08-03 12:46:58 +01001349
Orion Hodsonc069a302017-01-18 09:23:12 +00001350// Explicit DoInvokePolymorphic template function declarations.
1351#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range) \
1352 template REQUIRES_SHARED(Locks::mutator_lock_) \
1353 bool DoInvokePolymorphic<_is_range>( \
1354 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1355 uint16_t inst_data, JValue* result)
1356EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false);
1357EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true);
Narayan Kamath9823e782016-08-03 12:46:58 +01001358#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1359
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001360// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001361#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001362 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001363 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
1364 const ShadowFrame& shadow_frame, \
1365 Thread* self, JValue* result)
1366#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
1367 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
1368 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
1369 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
1370 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
1371EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
1372EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
1373#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001374#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
1375
1376} // namespace interpreter
1377} // namespace art