blob: fa103b15d106905062c0c0f90783e1cb636fa1ae [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
Daniel Mihalyieb076692014-08-22 17:33:31 +020021#include "debugger.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010022#include "mirror/array-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070023#include "unstarted_runtime.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080024#include "verifier/method_verifier.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020025
26namespace art {
27namespace interpreter {
28
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000029void ThrowNullPointerExceptionFromInterpreter() {
30 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -070031}
32
33template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
34bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
35 uint16_t inst_data) {
36 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
37 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
38 ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
Fred Shih37f05ef2014-07-16 18:38:08 -070039 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -070040 if (UNLIKELY(f == nullptr)) {
41 CHECK(self->IsExceptionPending());
42 return false;
43 }
44 Object* obj;
45 if (is_static) {
46 obj = f->GetDeclaringClass();
47 } else {
48 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
49 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000050 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers54874942014-06-10 16:31:03 -070051 return false;
52 }
53 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +020054 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -070055 // Report this field access to instrumentation if needed.
56 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
57 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
58 Object* this_object = f->IsStatic() ? nullptr : obj;
59 instrumentation->FieldReadEvent(self, this_object, shadow_frame.GetMethod(),
60 shadow_frame.GetDexPC(), f);
61 }
62 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
63 switch (field_type) {
64 case Primitive::kPrimBoolean:
65 shadow_frame.SetVReg(vregA, f->GetBoolean(obj));
66 break;
67 case Primitive::kPrimByte:
68 shadow_frame.SetVReg(vregA, f->GetByte(obj));
69 break;
70 case Primitive::kPrimChar:
71 shadow_frame.SetVReg(vregA, f->GetChar(obj));
72 break;
73 case Primitive::kPrimShort:
74 shadow_frame.SetVReg(vregA, f->GetShort(obj));
75 break;
76 case Primitive::kPrimInt:
77 shadow_frame.SetVReg(vregA, f->GetInt(obj));
78 break;
79 case Primitive::kPrimLong:
80 shadow_frame.SetVRegLong(vregA, f->GetLong(obj));
81 break;
82 case Primitive::kPrimNot:
83 shadow_frame.SetVRegReference(vregA, f->GetObject(obj));
84 break;
85 default:
86 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -070087 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -070088 }
89 return true;
90}
91
92// Explicitly instantiate all DoFieldGet functions.
93#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
94 template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \
95 ShadowFrame& shadow_frame, \
96 const Instruction* inst, \
97 uint16_t inst_data)
98
99#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
100 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \
101 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true);
102
103// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700104EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
105EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
106EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
107EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
108EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
109EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
110EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700111
112// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700113EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
114EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
115EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
116EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
117EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
118EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
119EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700120
121#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
122#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
123
124// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
125// Returns true on success, otherwise throws an exception and returns false.
126template<Primitive::Type field_type>
127bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
128 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
129 if (UNLIKELY(obj == nullptr)) {
130 // We lost the reference to the field index so we cannot get a more
131 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000132 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700133 return false;
134 }
135 MemberOffset field_offset(inst->VRegC_22c());
136 // Report this field access to instrumentation if needed. Since we only have the offset of
137 // the field from the base of the object, we need to look for it first.
138 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
139 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
140 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
141 field_offset.Uint32Value());
142 DCHECK(f != nullptr);
143 DCHECK(!f->IsStatic());
144 instrumentation->FieldReadEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
145 shadow_frame.GetDexPC(), f);
146 }
147 // Note: iget-x-quick instructions are only for non-volatile fields.
148 const uint32_t vregA = inst->VRegA_22c(inst_data);
149 switch (field_type) {
150 case Primitive::kPrimInt:
151 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
152 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800153 case Primitive::kPrimBoolean:
154 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
155 break;
156 case Primitive::kPrimByte:
157 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
158 break;
159 case Primitive::kPrimChar:
160 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
161 break;
162 case Primitive::kPrimShort:
163 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
164 break;
Ian Rogers54874942014-06-10 16:31:03 -0700165 case Primitive::kPrimLong:
166 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
167 break;
168 case Primitive::kPrimNot:
169 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
170 break;
171 default:
172 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700173 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700174 }
175 return true;
176}
177
178// Explicitly instantiate all DoIGetQuick functions.
179#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
180 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
181 uint16_t inst_data)
182
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800183EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
184EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
185EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
186EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
187EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
188EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
189EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700190#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
191
192template<Primitive::Type field_type>
193static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
194 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
195 JValue field_value;
196 switch (field_type) {
197 case Primitive::kPrimBoolean:
198 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
199 break;
200 case Primitive::kPrimByte:
201 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
202 break;
203 case Primitive::kPrimChar:
204 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
205 break;
206 case Primitive::kPrimShort:
207 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
208 break;
209 case Primitive::kPrimInt:
210 field_value.SetI(shadow_frame.GetVReg(vreg));
211 break;
212 case Primitive::kPrimLong:
213 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
214 break;
215 case Primitive::kPrimNot:
216 field_value.SetL(shadow_frame.GetVRegReference(vreg));
217 break;
218 default:
219 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700220 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700221 }
222 return field_value;
223}
224
225template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
226 bool transaction_active>
227bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
228 uint16_t inst_data) {
229 bool do_assignability_check = do_access_check;
230 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
231 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
232 ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
Fred Shih37f05ef2014-07-16 18:38:08 -0700233 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -0700234 if (UNLIKELY(f == nullptr)) {
235 CHECK(self->IsExceptionPending());
236 return false;
237 }
238 Object* obj;
239 if (is_static) {
240 obj = f->GetDeclaringClass();
241 } else {
242 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
243 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000244 ThrowNullPointerExceptionForFieldAccess(f, false);
Ian Rogers54874942014-06-10 16:31:03 -0700245 return false;
246 }
247 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +0200248 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -0700249 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
250 // Report this field access to instrumentation if needed. Since we only have the offset of
251 // the field from the base of the object, we need to look for it first.
252 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
253 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
254 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
255 Object* this_object = f->IsStatic() ? nullptr : obj;
256 instrumentation->FieldWriteEvent(self, this_object, shadow_frame.GetMethod(),
257 shadow_frame.GetDexPC(), f, field_value);
258 }
259 switch (field_type) {
260 case Primitive::kPrimBoolean:
261 f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA));
262 break;
263 case Primitive::kPrimByte:
264 f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA));
265 break;
266 case Primitive::kPrimChar:
267 f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA));
268 break;
269 case Primitive::kPrimShort:
270 f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA));
271 break;
272 case Primitive::kPrimInt:
273 f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA));
274 break;
275 case Primitive::kPrimLong:
276 f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA));
277 break;
278 case Primitive::kPrimNot: {
279 Object* reg = shadow_frame.GetVRegReference(vregA);
280 if (do_assignability_check && reg != nullptr) {
281 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
282 // object in the destructor.
283 Class* field_class;
284 {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700285 StackHandleScope<2> hs(self);
Ian Rogers54874942014-06-10 16:31:03 -0700286 HandleWrapper<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
287 HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700288 field_class = f->GetType<true>();
Ian Rogers54874942014-06-10 16:31:03 -0700289 }
290 if (!reg->VerifierInstanceOf(field_class)) {
291 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700292 std::string temp1, temp2, temp3;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000293 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Ian Rogers54874942014-06-10 16:31:03 -0700294 "Put '%s' that is not instance of field '%s' in '%s'",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700295 reg->GetClass()->GetDescriptor(&temp1),
296 field_class->GetDescriptor(&temp2),
297 f->GetDeclaringClass()->GetDescriptor(&temp3));
Ian Rogers54874942014-06-10 16:31:03 -0700298 return false;
299 }
300 }
301 f->SetObj<transaction_active>(obj, reg);
302 break;
303 }
304 default:
305 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700306 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700307 }
308 return true;
309}
310
311// Explicitly instantiate all DoFieldPut functions.
312#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
313 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
314 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
315
316#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
317 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
318 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
319 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
320 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
321
322// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700323EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
324EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
325EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
326EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
327EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
328EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
329EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700330
331// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700332EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
333EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
334EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
335EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
336EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
337EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
338EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700339
340#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
341#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
342
343template<Primitive::Type field_type, bool transaction_active>
344bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
345 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
346 if (UNLIKELY(obj == nullptr)) {
347 // We lost the reference to the field index so we cannot get a more
348 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000349 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700350 return false;
351 }
352 MemberOffset field_offset(inst->VRegC_22c());
353 const uint32_t vregA = inst->VRegA_22c(inst_data);
354 // Report this field modification to instrumentation if needed. Since we only have the offset of
355 // the field from the base of the object, we need to look for it first.
356 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
357 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
358 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
359 field_offset.Uint32Value());
360 DCHECK(f != nullptr);
361 DCHECK(!f->IsStatic());
362 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
363 instrumentation->FieldWriteEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
364 shadow_frame.GetDexPC(), f, field_value);
365 }
366 // Note: iput-x-quick instructions are only for non-volatile fields.
367 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700368 case Primitive::kPrimBoolean:
369 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
370 break;
371 case Primitive::kPrimByte:
372 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
373 break;
374 case Primitive::kPrimChar:
375 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
376 break;
377 case Primitive::kPrimShort:
378 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
379 break;
Ian Rogers54874942014-06-10 16:31:03 -0700380 case Primitive::kPrimInt:
381 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
382 break;
383 case Primitive::kPrimLong:
384 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
385 break;
386 case Primitive::kPrimNot:
387 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
388 break;
389 default:
390 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700391 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700392 }
393 return true;
394}
395
396// Explicitly instantiate all DoIPutQuick functions.
397#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
398 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
399 const Instruction* inst, \
400 uint16_t inst_data)
401
402#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
403 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
404 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
405
Andreas Gampec8ccf682014-09-29 20:07:43 -0700406EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
407EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
408EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
409EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
410EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
411EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
412EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700413#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
414#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
415
Mathieu Chartiere401d142015-04-22 13:56:20 -0700416uint32_t FindNextInstructionFollowingException(
417 Thread* self, ShadowFrame& shadow_frame, uint32_t dex_pc,
418 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700419 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700420 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000421 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000422 if (instrumentation->HasExceptionCaughtListeners()
423 && self->IsExceptionThrownByCurrentMethod(exception.Get())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000424 instrumentation->ExceptionCaughtEvent(self, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200425 }
Ian Rogers54874942014-06-10 16:31:03 -0700426 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700427 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
428 hs.NewHandle(exception->GetClass()), dex_pc, &clear_exception);
Ian Rogers54874942014-06-10 16:31:03 -0700429 if (found_dex_pc == DexFile::kDexNoIndex) {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000430 // Exception is not caught by the current method. We will unwind to the
431 // caller. Notify any instrumentation listener.
Sebastien Hertz9f102032014-05-23 08:59:42 +0200432 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
Ian Rogers54874942014-06-10 16:31:03 -0700433 shadow_frame.GetMethod(), dex_pc);
434 } else {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000435 // Exception is caught in the current method. We will jump to the found_dex_pc.
Ian Rogers54874942014-06-10 16:31:03 -0700436 if (clear_exception) {
437 self->ClearException();
438 }
439 }
440 return found_dex_pc;
441}
442
Ian Rogerse94652f2014-12-02 11:13:19 -0800443void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
444 LOG(FATAL) << "Unexpected instruction: "
445 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
446 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700447}
448
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200449// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800450static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
451 size_t dest_reg, size_t src_reg)
452 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700453 // Uint required, so that sign extension does not make this wrong on 64b systems
454 uint32_t src_value = shadow_frame.GetVReg(src_reg);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800455 mirror::Object* o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700456
457 // If both register locations contains the same value, the register probably holds a reference.
458 // Note: As an optimization, non-moving collectors leave a stale reference value
459 // in the references array even after the original vreg was overwritten to a non-reference.
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700460 if (src_value == reinterpret_cast<uintptr_t>(o)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800461 new_shadow_frame->SetVRegReference(dest_reg, o);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200462 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800463 new_shadow_frame->SetVReg(dest_reg, src_value);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200464 }
465}
466
Sebastien Hertz45b15972015-04-03 16:07:05 +0200467void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700468 va_list args;
469 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200470 AbortTransactionV(self, fmt, args);
471 va_end(args);
472}
473
474void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
475 CHECK(Runtime::Current()->IsActiveTransaction());
476 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100477 std::string abort_msg;
478 StringAppendV(&abort_msg, fmt, args);
479 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200480 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700481}
482
Igor Murashkin158f35c2015-06-10 15:55:30 -0700483// Separate declaration is required solely for the attributes.
484template<bool is_range, bool do_assignability_check> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
485static inline bool DoCallCommon(ArtMethod* called_method,
486 Thread* self,
487 ShadowFrame& shadow_frame,
488 JValue* result,
489 uint16_t number_of_inputs,
490 uint32_t arg[Instruction::kMaxVarArgRegs],
491 uint32_t vregC) ALWAYS_INLINE;
492
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200493template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -0700494static inline bool DoCallCommon(ArtMethod* called_method,
495 Thread* self,
496 ShadowFrame& shadow_frame,
497 JValue* result,
498 uint16_t number_of_inputs,
499 uint32_t arg[Instruction::kMaxVarArgRegs],
500 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800501 bool string_init = false;
502 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700503 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
504 && called_method->IsConstructor())) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800505 ScopedObjectAccessUnchecked soa(self);
506 jmethodID mid = soa.EncodeMethod(called_method);
507 called_method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
508 string_init = true;
509 }
510
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200511 // Compute method information.
Ian Rogerse94652f2014-12-02 11:13:19 -0800512 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
Igor Murashkin158f35c2015-06-10 15:55:30 -0700513
514 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200515 uint16_t num_regs;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700516 if (LIKELY(code_item != nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200517 num_regs = code_item->registers_size_;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700518 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200519 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800520 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Igor Murashkin158f35c2015-06-10 15:55:30 -0700521 num_regs = number_of_inputs;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200522 }
523
Igor Murashkin158f35c2015-06-10 15:55:30 -0700524 // Hack for String init:
525 //
526 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
527 // invoke-x StringFactory(a, b, c, ...)
528 // by effectively dropping the first virtual register from the invoke.
529 //
530 // (at this point the ArtMethod has already been replaced,
531 // so we just need to fix-up the arguments)
532 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -0700533 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700534 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 -0700535
Igor Murashkin158f35c2015-06-10 15:55:30 -0700536 // The new StringFactory call is static and has one fewer argument.
Igor Murashkina06b49b2015-06-25 15:18:12 -0700537 if (code_item == nullptr) {
538 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
539 num_regs--;
540 } // 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 -0700541 number_of_inputs--;
542
543 // Rewrite the var-args, dropping the 0th argument ("this")
544 for (uint32_t i = 1; i < Instruction::kMaxVarArgRegs; ++i) {
545 arg[i - 1] = arg[i];
546 }
547 arg[Instruction::kMaxVarArgRegs - 1] = 0;
548
549 // Rewrite the non-var-arg case
550 vregC++; // Skips the 0th vreg in the range ("this").
551 }
552
553 // Parameter registers go at the end of the shadow frame.
554 DCHECK_GE(num_regs, number_of_inputs);
555 size_t first_dest_reg = num_regs - number_of_inputs;
556 DCHECK_NE(first_dest_reg, (size_t)-1);
557
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200558 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700559 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200560 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
Ian Rogerse94652f2014-12-02 11:13:19 -0800561 ShadowFrame* new_shadow_frame(ShadowFrame::Create(num_regs, &shadow_frame, called_method, 0,
562 memory));
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200563
Igor Murashkin158f35c2015-06-10 15:55:30 -0700564 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -0700565 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700566 // Slow path.
567 // We might need to do class loading, which incurs a thread state change to kNative. So
568 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700569 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +0200570 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700571 self->EndAssertNoThreadSuspension(old_cause);
572
573 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200574 // to get the exact type of each reference argument.
Ian Rogerse94652f2014-12-02 11:13:19 -0800575 const DexFile::TypeList* params = new_shadow_frame->GetMethod()->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700576 uint32_t shorty_len = 0;
Ian Rogerse94652f2014-12-02 11:13:19 -0800577 const char* shorty = new_shadow_frame->GetMethod()->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200578
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100579 // Handle receiver apart since it's not part of the shorty.
580 size_t dest_reg = first_dest_reg;
581 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700582
Ian Rogerse94652f2014-12-02 11:13:19 -0800583 if (!new_shadow_frame->GetMethod()->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700584 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100585 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
586 ++dest_reg;
587 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -0700588 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100589 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700590
591 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800592 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -0700593 // Skip the 0th 'shorty' type since it represents the return type.
594 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200595 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
596 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700597 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200598 case 'L': {
599 Object* o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700600 if (do_assignability_check && o != nullptr) {
Ian Rogersa0485602014-12-02 15:48:04 -0800601 Class* arg_type =
602 new_shadow_frame->GetMethod()->GetClassFromTypeIndex(
603 params->GetTypeItem(shorty_pos).type_idx_, true);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700604 if (arg_type == nullptr) {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200605 CHECK(self->IsExceptionPending());
606 return false;
607 }
608 if (!o->VerifierInstanceOf(arg_type)) {
609 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700610 std::string temp1, temp2;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000611 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200612 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -0800613 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700614 o->GetClass()->GetDescriptor(&temp1),
615 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200616 return false;
617 }
Jeff Haoa3faaf42013-09-03 19:07:00 -0700618 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200619 new_shadow_frame->SetVRegReference(dest_reg, o);
620 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700621 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700622 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200623 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700624 uint64_t wide_value =
625 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
626 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200627 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700628 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200629 ++dest_reg;
630 ++arg_offset;
631 break;
632 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700633 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200634 default:
635 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
636 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200637 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200638 }
639 } else {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700640 size_t arg_index = 0;
641
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200642 // Fast path: no extra checks.
643 if (is_range) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700644 // TODO: Implement the range version of invoke-lambda
645 uint16_t first_src_reg = vregC;
646
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200647 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs;
648 ++dest_reg, ++src_reg) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800649 AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200650 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200651 } else {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700652 DCHECK_LE(number_of_inputs, Instruction::kMaxVarArgRegs);
653
654 for (; arg_index < number_of_inputs; ++arg_index) {
655 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, arg[arg_index]);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200656 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200657 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700658 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200659 }
660
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200661 // Do the call now.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200662 if (LIKELY(Runtime::Current()->IsStarted())) {
Nicolas Geoffray7070ccd2015-07-08 09:41:54 +0000663 if (kIsDebugBuild && new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter() == nullptr) {
664 LOG(FATAL) << "Attempt to invoke non-executable method: "
665 << PrettyMethod(new_shadow_frame->GetMethod());
666 UNREACHABLE();
Daniel Mihalyieb076692014-08-22 17:33:31 +0200667 }
Nicolas Geoffray7070ccd2015-07-08 09:41:54 +0000668 if (kIsDebugBuild && Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly() &&
669 !new_shadow_frame->GetMethod()->IsNative() &&
670 !new_shadow_frame->GetMethod()->IsProxyMethod() &&
671 new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter()
672 == artInterpreterToCompiledCodeBridge) {
673 LOG(FATAL) << "Attempt to call compiled code when -Xint: "
674 << PrettyMethod(new_shadow_frame->GetMethod());
675 UNREACHABLE();
676 }
677 // Force the use of interpreter when it is required by the debugger.
678 EntryPointFromInterpreter* entry;
679 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForCalling(self, new_shadow_frame->GetMethod()))) {
680 entry = &art::artInterpreterToInterpreterBridge;
681 } else {
682 entry = new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter();
683 }
684 entry(self, code_item, new_shadow_frame, result);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200685 } else {
Andreas Gampe799681b2015-05-15 19:24:12 -0700686 UnstartedRuntime::Invoke(self, code_item, new_shadow_frame, result, first_dest_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200687 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800688
689 if (string_init && !self->IsExceptionPending()) {
690 // Set the new string result of the StringFactory.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700691 shadow_frame.SetVRegReference(string_init_vreg_this, result->GetL());
Jeff Hao848f70a2014-01-15 13:49:50 -0800692 // Overwrite all potential copies of the original result of the new-instance of string with the
693 // new result of the StringFactory. Use the verifier to find this set of registers.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700694 ArtMethod* method = shadow_frame.GetMethod();
Jeff Hao848f70a2014-01-15 13:49:50 -0800695 MethodReference method_ref = method->ToMethodReference();
696 SafeMap<uint32_t, std::set<uint32_t>> string_init_map;
697 SafeMap<uint32_t, std::set<uint32_t>>* string_init_map_ptr;
698 MethodRefToStringInitRegMap& method_to_string_init_map = Runtime::Current()->GetStringInitMap();
699 auto it = method_to_string_init_map.find(method_ref);
700 if (it == method_to_string_init_map.end()) {
701 string_init_map = std::move(verifier::MethodVerifier::FindStringInitMap(method));
702 method_to_string_init_map.Overwrite(method_ref, string_init_map);
703 string_init_map_ptr = &string_init_map;
704 } else {
705 string_init_map_ptr = &it->second;
706 }
707 if (string_init_map_ptr->size() != 0) {
708 uint32_t dex_pc = shadow_frame.GetDexPC();
709 auto map_it = string_init_map_ptr->find(dex_pc);
710 if (map_it != string_init_map_ptr->end()) {
711 const std::set<uint32_t>& reg_set = map_it->second;
712 for (auto set_it = reg_set.begin(); set_it != reg_set.end(); ++set_it) {
713 shadow_frame.SetVRegReference(*set_it, result->GetL());
714 }
715 }
716 }
717 }
718
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200719 return !self->IsExceptionPending();
720}
721
Igor Murashkin158f35c2015-06-10 15:55:30 -0700722template<bool is_range, bool do_assignability_check>
723bool DoLambdaCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
724 const Instruction* inst, uint16_t inst_data, JValue* result) {
725 const uint4_t num_additional_registers = inst->VRegB_25x();
726 // Argument word count.
727 const uint16_t number_of_inputs = num_additional_registers + 1;
728 // The first input register is always present and is not encoded in the count.
729
730 // TODO: find a cleaner way to separate non-range and range information without duplicating
731 // code.
732 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in invoke-XXX.
733 uint32_t vregC = 0; // only used in invoke-XXX-range.
734 if (is_range) {
735 vregC = inst->VRegC_3rc();
736 } else {
737 // TODO(iam): See if it's possible to remove inst_data dependency from 35x to avoid this path
738 UNUSED(inst_data);
739 inst->GetAllArgs25x(arg);
740 }
741
742 // TODO: if there's an assignability check, throw instead?
743 DCHECK(called_method->IsStatic());
744
745 return DoCallCommon<is_range, do_assignability_check>(
746 called_method, self, shadow_frame,
747 result, number_of_inputs, arg, vregC);
748}
749
750template<bool is_range, bool do_assignability_check>
751bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
752 const Instruction* inst, uint16_t inst_data, JValue* result) {
753 // Argument word count.
754 const uint16_t number_of_inputs = (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
755
756 // TODO: find a cleaner way to separate non-range and range information without duplicating
757 // code.
758 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in invoke-XXX.
759 uint32_t vregC = 0;
760 if (is_range) {
761 vregC = inst->VRegC_3rc();
762 } else {
763 vregC = inst->VRegC_35c();
764 inst->GetVarArgs(arg, inst_data);
765 }
766
767 return DoCallCommon<is_range, do_assignability_check>(
768 called_method, self, shadow_frame,
769 result, number_of_inputs, arg, vregC);
770}
771
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100772template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200773bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
774 Thread* self, JValue* result) {
775 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
776 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
777 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
778 if (!is_range) {
779 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
780 CHECK_LE(length, 5);
781 }
782 if (UNLIKELY(length < 0)) {
783 ThrowNegativeArraySizeException(length);
784 return false;
785 }
786 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700787 Class* array_class = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(),
788 self, false, do_access_check);
789 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200790 DCHECK(self->IsExceptionPending());
791 return false;
792 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700793 CHECK(array_class->IsArrayClass());
794 Class* component_class = array_class->GetComponentType();
795 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
796 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
797 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200798 ThrowRuntimeException("Bad filled array request for type %s",
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700799 PrettyDescriptor(component_class).c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200800 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000801 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800802 "Found type %s; filled-new-array not implemented for anything but 'int'",
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700803 PrettyDescriptor(component_class).c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200804 }
805 return false;
806 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700807 Object* new_array = Array::Alloc<true>(self, array_class, length,
808 array_class->GetComponentSizeShift(),
809 Runtime::Current()->GetHeap()->GetCurrentAllocator());
810 if (UNLIKELY(new_array == nullptr)) {
811 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200812 return false;
813 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700814 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
815 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200816 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100817 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200818 } else {
Ian Rogers29a26482014-05-02 15:27:29 -0700819 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100820 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100821 for (int32_t i = 0; i < length; ++i) {
822 size_t src_reg = is_range ? vregC + i : arg[i];
823 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700824 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
825 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +0100826 } else {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700827 new_array->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(
828 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200829 }
830 }
831
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700832 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200833 return true;
834}
835
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100836// TODO fix thread analysis: should be SHARED_LOCKS_REQUIRED(Locks::mutator_lock_).
837template<typename T>
838static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count)
839 NO_THREAD_SAFETY_ANALYSIS {
840 Runtime* runtime = Runtime::Current();
841 for (int32_t i = 0; i < count; ++i) {
842 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
843 }
844}
845
846void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
847 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
848 DCHECK(Runtime::Current()->IsActiveTransaction());
849 DCHECK(array != nullptr);
850 DCHECK_LE(count, array->GetLength());
851 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
852 switch (primitive_component_type) {
853 case Primitive::kPrimBoolean:
854 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
855 break;
856 case Primitive::kPrimByte:
857 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
858 break;
859 case Primitive::kPrimChar:
860 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
861 break;
862 case Primitive::kPrimShort:
863 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
864 break;
865 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100866 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
867 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700868 case Primitive::kPrimFloat:
869 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
870 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100871 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100872 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
873 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700874 case Primitive::kPrimDouble:
875 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
876 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100877 default:
878 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
879 << " in fill-array-data";
880 break;
881 }
882}
883
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200884// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200885#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
886 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100887 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
888 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +0200889 const Instruction* inst, uint16_t inst_data, \
890 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200891EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
892EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
893EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
894EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
895#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200896
Igor Murashkin158f35c2015-06-10 15:55:30 -0700897// Explicit DoLambdaCall template function declarations.
898#define EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
899 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
900 bool DoLambdaCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
901 ShadowFrame& shadow_frame, \
902 const Instruction* inst, \
903 uint16_t inst_data, \
904 JValue* result)
905EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(false, false);
906EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(false, true);
907EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(true, false);
908EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(true, true);
909#undef EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL
910
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200911// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100912#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
913 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
914 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
915 const ShadowFrame& shadow_frame, \
916 Thread* self, JValue* result)
917#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
918 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
919 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
920 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
921 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
922EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
923EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
924#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200925#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
926
927} // namespace interpreter
928} // namespace art