blob: 68d56f519845633021d8d9b7667ec7d5876c2a17 [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"
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +000022#include "entrypoints/runtime_asm_entrypoints.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010023#include "mirror/array-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070024#include "unstarted_runtime.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080025#include "verifier/method_verifier.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020026
27namespace art {
28namespace interpreter {
29
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000030void ThrowNullPointerExceptionFromInterpreter() {
31 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -070032}
33
34template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
35bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
36 uint16_t inst_data) {
37 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
38 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
39 ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
Fred Shih37f05ef2014-07-16 18:38:08 -070040 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -070041 if (UNLIKELY(f == nullptr)) {
42 CHECK(self->IsExceptionPending());
43 return false;
44 }
45 Object* obj;
46 if (is_static) {
47 obj = f->GetDeclaringClass();
48 } else {
49 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
50 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000051 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers54874942014-06-10 16:31:03 -070052 return false;
53 }
54 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +020055 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -070056 // Report this field access to instrumentation if needed.
57 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
58 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
59 Object* this_object = f->IsStatic() ? nullptr : obj;
60 instrumentation->FieldReadEvent(self, this_object, shadow_frame.GetMethod(),
61 shadow_frame.GetDexPC(), f);
62 }
63 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
64 switch (field_type) {
65 case Primitive::kPrimBoolean:
66 shadow_frame.SetVReg(vregA, f->GetBoolean(obj));
67 break;
68 case Primitive::kPrimByte:
69 shadow_frame.SetVReg(vregA, f->GetByte(obj));
70 break;
71 case Primitive::kPrimChar:
72 shadow_frame.SetVReg(vregA, f->GetChar(obj));
73 break;
74 case Primitive::kPrimShort:
75 shadow_frame.SetVReg(vregA, f->GetShort(obj));
76 break;
77 case Primitive::kPrimInt:
78 shadow_frame.SetVReg(vregA, f->GetInt(obj));
79 break;
80 case Primitive::kPrimLong:
81 shadow_frame.SetVRegLong(vregA, f->GetLong(obj));
82 break;
83 case Primitive::kPrimNot:
84 shadow_frame.SetVRegReference(vregA, f->GetObject(obj));
85 break;
86 default:
87 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -070088 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -070089 }
90 return true;
91}
92
93// Explicitly instantiate all DoFieldGet functions.
94#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
95 template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \
96 ShadowFrame& shadow_frame, \
97 const Instruction* inst, \
98 uint16_t inst_data)
99
100#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
101 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \
102 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true);
103
104// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700105EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
106EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
107EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
108EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
109EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
110EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
111EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700112
113// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700114EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
115EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
116EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
117EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
118EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
119EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
120EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700121
122#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
123#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
124
125// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
126// Returns true on success, otherwise throws an exception and returns false.
127template<Primitive::Type field_type>
128bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
129 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
130 if (UNLIKELY(obj == nullptr)) {
131 // We lost the reference to the field index so we cannot get a more
132 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000133 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700134 return false;
135 }
136 MemberOffset field_offset(inst->VRegC_22c());
137 // Report this field access to instrumentation if needed. Since we only have the offset of
138 // the field from the base of the object, we need to look for it first.
139 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
140 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
141 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
142 field_offset.Uint32Value());
143 DCHECK(f != nullptr);
144 DCHECK(!f->IsStatic());
145 instrumentation->FieldReadEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
146 shadow_frame.GetDexPC(), f);
147 }
148 // Note: iget-x-quick instructions are only for non-volatile fields.
149 const uint32_t vregA = inst->VRegA_22c(inst_data);
150 switch (field_type) {
151 case Primitive::kPrimInt:
152 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
153 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800154 case Primitive::kPrimBoolean:
155 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
156 break;
157 case Primitive::kPrimByte:
158 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
159 break;
160 case Primitive::kPrimChar:
161 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
162 break;
163 case Primitive::kPrimShort:
164 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
165 break;
Ian Rogers54874942014-06-10 16:31:03 -0700166 case Primitive::kPrimLong:
167 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
168 break;
169 case Primitive::kPrimNot:
170 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
171 break;
172 default:
173 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700174 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700175 }
176 return true;
177}
178
179// Explicitly instantiate all DoIGetQuick functions.
180#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
181 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
182 uint16_t inst_data)
183
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800184EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
185EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
186EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
187EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
188EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
189EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
190EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700191#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
192
193template<Primitive::Type field_type>
194static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700195 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700196 JValue field_value;
197 switch (field_type) {
198 case Primitive::kPrimBoolean:
199 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
200 break;
201 case Primitive::kPrimByte:
202 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
203 break;
204 case Primitive::kPrimChar:
205 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
206 break;
207 case Primitive::kPrimShort:
208 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
209 break;
210 case Primitive::kPrimInt:
211 field_value.SetI(shadow_frame.GetVReg(vreg));
212 break;
213 case Primitive::kPrimLong:
214 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
215 break;
216 case Primitive::kPrimNot:
217 field_value.SetL(shadow_frame.GetVRegReference(vreg));
218 break;
219 default:
220 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700221 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700222 }
223 return field_value;
224}
225
226template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
227 bool transaction_active>
228bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
229 uint16_t inst_data) {
230 bool do_assignability_check = do_access_check;
231 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
232 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
233 ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
Fred Shih37f05ef2014-07-16 18:38:08 -0700234 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -0700235 if (UNLIKELY(f == nullptr)) {
236 CHECK(self->IsExceptionPending());
237 return false;
238 }
239 Object* obj;
240 if (is_static) {
241 obj = f->GetDeclaringClass();
242 } else {
243 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
244 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000245 ThrowNullPointerExceptionForFieldAccess(f, false);
Ian Rogers54874942014-06-10 16:31:03 -0700246 return false;
247 }
248 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +0200249 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -0700250 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
251 // Report this field access to instrumentation if needed. Since we only have the offset of
252 // the field from the base of the object, we need to look for it first.
253 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
254 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
255 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
256 Object* this_object = f->IsStatic() ? nullptr : obj;
257 instrumentation->FieldWriteEvent(self, this_object, shadow_frame.GetMethod(),
258 shadow_frame.GetDexPC(), f, field_value);
259 }
260 switch (field_type) {
261 case Primitive::kPrimBoolean:
262 f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA));
263 break;
264 case Primitive::kPrimByte:
265 f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA));
266 break;
267 case Primitive::kPrimChar:
268 f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA));
269 break;
270 case Primitive::kPrimShort:
271 f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA));
272 break;
273 case Primitive::kPrimInt:
274 f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA));
275 break;
276 case Primitive::kPrimLong:
277 f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA));
278 break;
279 case Primitive::kPrimNot: {
280 Object* reg = shadow_frame.GetVRegReference(vregA);
281 if (do_assignability_check && reg != nullptr) {
282 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
283 // object in the destructor.
284 Class* field_class;
285 {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700286 StackHandleScope<2> hs(self);
Ian Rogers54874942014-06-10 16:31:03 -0700287 HandleWrapper<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
288 HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700289 field_class = f->GetType<true>();
Ian Rogers54874942014-06-10 16:31:03 -0700290 }
291 if (!reg->VerifierInstanceOf(field_class)) {
292 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700293 std::string temp1, temp2, temp3;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000294 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Ian Rogers54874942014-06-10 16:31:03 -0700295 "Put '%s' that is not instance of field '%s' in '%s'",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700296 reg->GetClass()->GetDescriptor(&temp1),
297 field_class->GetDescriptor(&temp2),
298 f->GetDeclaringClass()->GetDescriptor(&temp3));
Ian Rogers54874942014-06-10 16:31:03 -0700299 return false;
300 }
301 }
302 f->SetObj<transaction_active>(obj, reg);
303 break;
304 }
305 default:
306 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700307 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700308 }
309 return true;
310}
311
312// Explicitly instantiate all DoFieldPut functions.
313#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
314 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
315 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
316
317#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
318 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
319 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
320 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
321 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
322
323// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700324EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
325EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
326EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
327EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
328EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
329EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
330EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700331
332// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700333EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
334EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
335EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
336EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
337EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
338EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
339EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700340
341#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
342#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
343
344template<Primitive::Type field_type, bool transaction_active>
345bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
346 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
347 if (UNLIKELY(obj == nullptr)) {
348 // We lost the reference to the field index so we cannot get a more
349 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000350 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700351 return false;
352 }
353 MemberOffset field_offset(inst->VRegC_22c());
354 const uint32_t vregA = inst->VRegA_22c(inst_data);
355 // Report this field modification to instrumentation if needed. Since we only have the offset of
356 // the field from the base of the object, we need to look for it first.
357 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
358 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
359 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
360 field_offset.Uint32Value());
361 DCHECK(f != nullptr);
362 DCHECK(!f->IsStatic());
363 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
364 instrumentation->FieldWriteEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
365 shadow_frame.GetDexPC(), f, field_value);
366 }
367 // Note: iput-x-quick instructions are only for non-volatile fields.
368 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700369 case Primitive::kPrimBoolean:
370 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
371 break;
372 case Primitive::kPrimByte:
373 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
374 break;
375 case Primitive::kPrimChar:
376 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
377 break;
378 case Primitive::kPrimShort:
379 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
380 break;
Ian Rogers54874942014-06-10 16:31:03 -0700381 case Primitive::kPrimInt:
382 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
383 break;
384 case Primitive::kPrimLong:
385 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
386 break;
387 case Primitive::kPrimNot:
388 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
389 break;
390 default:
391 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700392 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700393 }
394 return true;
395}
396
397// Explicitly instantiate all DoIPutQuick functions.
398#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
399 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
400 const Instruction* inst, \
401 uint16_t inst_data)
402
403#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
404 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
405 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
406
Andreas Gampec8ccf682014-09-29 20:07:43 -0700407EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
408EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
409EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
410EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
411EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
412EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
413EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700414#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
415#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
416
Sebastien Hertz520633b2015-09-08 17:03:36 +0200417// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700418uint32_t FindNextInstructionFollowingException(
419 Thread* self, ShadowFrame& shadow_frame, uint32_t dex_pc,
420 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700421 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700422 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000423 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Sebastien Hertz520633b2015-09-08 17:03:36 +0200424 if (instrumentation != nullptr && instrumentation->HasExceptionCaughtListeners()
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000425 && self->IsExceptionThrownByCurrentMethod(exception.Get())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000426 instrumentation->ExceptionCaughtEvent(self, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200427 }
Ian Rogers54874942014-06-10 16:31:03 -0700428 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700429 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
430 hs.NewHandle(exception->GetClass()), dex_pc, &clear_exception);
Sebastien Hertz520633b2015-09-08 17:03:36 +0200431 if (found_dex_pc == DexFile::kDexNoIndex && instrumentation != nullptr) {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000432 // Exception is not caught by the current method. We will unwind to the
433 // caller. Notify any instrumentation listener.
Sebastien Hertz9f102032014-05-23 08:59:42 +0200434 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
Ian Rogers54874942014-06-10 16:31:03 -0700435 shadow_frame.GetMethod(), dex_pc);
436 } else {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000437 // Exception is caught in the current method. We will jump to the found_dex_pc.
Ian Rogers54874942014-06-10 16:31:03 -0700438 if (clear_exception) {
439 self->ClearException();
440 }
441 }
442 return found_dex_pc;
443}
444
Ian Rogerse94652f2014-12-02 11:13:19 -0800445void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
446 LOG(FATAL) << "Unexpected instruction: "
447 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
448 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700449}
450
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200451// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800452static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
453 size_t dest_reg, size_t src_reg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700454 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700455 // Uint required, so that sign extension does not make this wrong on 64b systems
456 uint32_t src_value = shadow_frame.GetVReg(src_reg);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800457 mirror::Object* o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700458
459 // If both register locations contains the same value, the register probably holds a reference.
460 // Note: As an optimization, non-moving collectors leave a stale reference value
461 // in the references array even after the original vreg was overwritten to a non-reference.
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700462 if (src_value == reinterpret_cast<uintptr_t>(o)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800463 new_shadow_frame->SetVRegReference(dest_reg, o);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200464 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800465 new_shadow_frame->SetVReg(dest_reg, src_value);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200466 }
467}
468
Sebastien Hertz45b15972015-04-03 16:07:05 +0200469void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700470 va_list args;
471 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200472 AbortTransactionV(self, fmt, args);
473 va_end(args);
474}
475
476void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
477 CHECK(Runtime::Current()->IsActiveTransaction());
478 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100479 std::string abort_msg;
480 StringAppendV(&abort_msg, fmt, args);
481 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200482 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700483}
484
Igor Murashkin158f35c2015-06-10 15:55:30 -0700485// Separate declaration is required solely for the attributes.
Mathieu Chartier90443472015-07-16 20:32:27 -0700486template<bool is_range, bool do_assignability_check> SHARED_REQUIRES(Locks::mutator_lock_)
Igor Murashkin158f35c2015-06-10 15:55:30 -0700487static inline bool DoCallCommon(ArtMethod* called_method,
488 Thread* self,
489 ShadowFrame& shadow_frame,
490 JValue* result,
491 uint16_t number_of_inputs,
492 uint32_t arg[Instruction::kMaxVarArgRegs],
493 uint32_t vregC) ALWAYS_INLINE;
494
Mathieu Chartier90443472015-07-16 20:32:27 -0700495SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +0000496static inline bool NeedsInterpreter(Thread* self, ShadowFrame* new_shadow_frame) ALWAYS_INLINE;
497
498static inline bool NeedsInterpreter(Thread* self, ShadowFrame* new_shadow_frame) {
499 ArtMethod* target = new_shadow_frame->GetMethod();
500 if (UNLIKELY(target->IsNative() || target->IsProxyMethod())) {
501 return false;
502 }
503 Runtime* runtime = Runtime::Current();
504 ClassLinker* class_linker = runtime->GetClassLinker();
505 return runtime->GetInstrumentation()->IsForcedInterpretOnly() ||
506 // Doing this check avoids doing compiled/interpreter transitions.
507 class_linker->IsQuickToInterpreterBridge(target->GetEntryPointFromQuickCompiledCode()) ||
508 // Force the use of interpreter when it is required by the debugger.
509 Dbg::IsForcedInterpreterNeededForCalling(self, target);
510}
511
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200512template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -0700513static inline bool DoCallCommon(ArtMethod* called_method,
514 Thread* self,
515 ShadowFrame& shadow_frame,
516 JValue* result,
517 uint16_t number_of_inputs,
518 uint32_t arg[Instruction::kMaxVarArgRegs],
519 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800520 bool string_init = false;
521 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700522 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
523 && called_method->IsConstructor())) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800524 ScopedObjectAccessUnchecked soa(self);
525 jmethodID mid = soa.EncodeMethod(called_method);
526 called_method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
527 string_init = true;
528 }
529
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200530 // Compute method information.
Ian Rogerse94652f2014-12-02 11:13:19 -0800531 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
Igor Murashkin158f35c2015-06-10 15:55:30 -0700532
533 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200534 uint16_t num_regs;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700535 if (LIKELY(code_item != nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200536 num_regs = code_item->registers_size_;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700537 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200538 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800539 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Igor Murashkin158f35c2015-06-10 15:55:30 -0700540 num_regs = number_of_inputs;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200541 }
542
Igor Murashkin158f35c2015-06-10 15:55:30 -0700543 // Hack for String init:
544 //
545 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
546 // invoke-x StringFactory(a, b, c, ...)
547 // by effectively dropping the first virtual register from the invoke.
548 //
549 // (at this point the ArtMethod has already been replaced,
550 // so we just need to fix-up the arguments)
551 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -0700552 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700553 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 -0700554
Igor Murashkin158f35c2015-06-10 15:55:30 -0700555 // The new StringFactory call is static and has one fewer argument.
Igor Murashkina06b49b2015-06-25 15:18:12 -0700556 if (code_item == nullptr) {
557 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
558 num_regs--;
559 } // 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 -0700560 number_of_inputs--;
561
562 // Rewrite the var-args, dropping the 0th argument ("this")
563 for (uint32_t i = 1; i < Instruction::kMaxVarArgRegs; ++i) {
564 arg[i - 1] = arg[i];
565 }
566 arg[Instruction::kMaxVarArgRegs - 1] = 0;
567
568 // Rewrite the non-var-arg case
569 vregC++; // Skips the 0th vreg in the range ("this").
570 }
571
572 // Parameter registers go at the end of the shadow frame.
573 DCHECK_GE(num_regs, number_of_inputs);
574 size_t first_dest_reg = num_regs - number_of_inputs;
575 DCHECK_NE(first_dest_reg, (size_t)-1);
576
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200577 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700578 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200579 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
Ian Rogerse94652f2014-12-02 11:13:19 -0800580 ShadowFrame* new_shadow_frame(ShadowFrame::Create(num_regs, &shadow_frame, called_method, 0,
581 memory));
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200582
Igor Murashkin158f35c2015-06-10 15:55:30 -0700583 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -0700584 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700585 // Slow path.
586 // We might need to do class loading, which incurs a thread state change to kNative. So
587 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700588 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +0200589 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700590 self->EndAssertNoThreadSuspension(old_cause);
591
592 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200593 // to get the exact type of each reference argument.
Ian Rogerse94652f2014-12-02 11:13:19 -0800594 const DexFile::TypeList* params = new_shadow_frame->GetMethod()->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700595 uint32_t shorty_len = 0;
Ian Rogerse94652f2014-12-02 11:13:19 -0800596 const char* shorty = new_shadow_frame->GetMethod()->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200597
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100598 // Handle receiver apart since it's not part of the shorty.
599 size_t dest_reg = first_dest_reg;
600 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700601
Ian Rogerse94652f2014-12-02 11:13:19 -0800602 if (!new_shadow_frame->GetMethod()->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700603 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100604 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
605 ++dest_reg;
606 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -0700607 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100608 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700609
610 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800611 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -0700612 // Skip the 0th 'shorty' type since it represents the return type.
613 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200614 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
615 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700616 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200617 case 'L': {
618 Object* o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700619 if (do_assignability_check && o != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100620 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Ian Rogersa0485602014-12-02 15:48:04 -0800621 Class* arg_type =
622 new_shadow_frame->GetMethod()->GetClassFromTypeIndex(
Vladimir Marko05792b92015-08-03 11:56:49 +0100623 params->GetTypeItem(shorty_pos).type_idx_, true /* resolve */, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700624 if (arg_type == nullptr) {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200625 CHECK(self->IsExceptionPending());
626 return false;
627 }
628 if (!o->VerifierInstanceOf(arg_type)) {
629 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700630 std::string temp1, temp2;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000631 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200632 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -0800633 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700634 o->GetClass()->GetDescriptor(&temp1),
635 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200636 return false;
637 }
Jeff Haoa3faaf42013-09-03 19:07:00 -0700638 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200639 new_shadow_frame->SetVRegReference(dest_reg, o);
640 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700641 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700642 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200643 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700644 uint64_t wide_value =
645 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
646 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200647 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700648 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200649 ++dest_reg;
650 ++arg_offset;
651 break;
652 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700653 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200654 default:
655 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
656 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200657 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200658 }
659 } else {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700660 size_t arg_index = 0;
661
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200662 // Fast path: no extra checks.
663 if (is_range) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700664 // TODO: Implement the range version of invoke-lambda
665 uint16_t first_src_reg = vregC;
666
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200667 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs;
668 ++dest_reg, ++src_reg) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800669 AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200670 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200671 } else {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700672 DCHECK_LE(number_of_inputs, Instruction::kMaxVarArgRegs);
673
674 for (; arg_index < number_of_inputs; ++arg_index) {
675 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, arg[arg_index]);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200676 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200677 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700678 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200679 }
680
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200681 // Do the call now.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200682 if (LIKELY(Runtime::Current()->IsStarted())) {
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +0000683 if (NeedsInterpreter(self, new_shadow_frame)) {
684 artInterpreterToInterpreterBridge(self, code_item, new_shadow_frame, result);
Nicolas Geoffray7070ccd2015-07-08 09:41:54 +0000685 } else {
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +0000686 artInterpreterToCompiledCodeBridge(self, code_item, new_shadow_frame, result);
Nicolas Geoffray7070ccd2015-07-08 09:41:54 +0000687 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200688 } else {
Andreas Gampe799681b2015-05-15 19:24:12 -0700689 UnstartedRuntime::Invoke(self, code_item, new_shadow_frame, result, first_dest_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200690 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800691
692 if (string_init && !self->IsExceptionPending()) {
693 // Set the new string result of the StringFactory.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700694 shadow_frame.SetVRegReference(string_init_vreg_this, result->GetL());
Jeff Hao848f70a2014-01-15 13:49:50 -0800695 // Overwrite all potential copies of the original result of the new-instance of string with the
696 // new result of the StringFactory. Use the verifier to find this set of registers.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700697 ArtMethod* method = shadow_frame.GetMethod();
Jeff Hao848f70a2014-01-15 13:49:50 -0800698 MethodReference method_ref = method->ToMethodReference();
Jeff Hao4686c522015-09-18 14:44:32 -0700699 SafeMap<uint32_t, std::set<uint32_t>>* string_init_map_ptr = nullptr;
Jeff Hao848f70a2014-01-15 13:49:50 -0800700 MethodRefToStringInitRegMap& method_to_string_init_map = Runtime::Current()->GetStringInitMap();
Jeff Haoa2c38642015-09-17 17:29:01 -0700701 {
702 MutexLock mu(self, *Locks::interpreter_string_init_map_lock_);
Jeff Hao4686c522015-09-18 14:44:32 -0700703 auto it = method_to_string_init_map.find(method_ref);
704 if (it != method_to_string_init_map.end()) {
705 string_init_map_ptr = &it->second;
Jeff Haoa2c38642015-09-17 17:29:01 -0700706 }
Jeff Hao4686c522015-09-18 14:44:32 -0700707 }
708 if (string_init_map_ptr == nullptr) {
709 SafeMap<uint32_t, std::set<uint32_t>> string_init_map =
710 verifier::MethodVerifier::FindStringInitMap(method);
711 MutexLock mu(self, *Locks::interpreter_string_init_map_lock_);
712 auto it = method_to_string_init_map.Overwrite(method_ref, string_init_map);
Jeff Hao848f70a2014-01-15 13:49:50 -0800713 string_init_map_ptr = &it->second;
714 }
715 if (string_init_map_ptr->size() != 0) {
716 uint32_t dex_pc = shadow_frame.GetDexPC();
717 auto map_it = string_init_map_ptr->find(dex_pc);
718 if (map_it != string_init_map_ptr->end()) {
719 const std::set<uint32_t>& reg_set = map_it->second;
720 for (auto set_it = reg_set.begin(); set_it != reg_set.end(); ++set_it) {
721 shadow_frame.SetVRegReference(*set_it, result->GetL());
722 }
723 }
724 }
725 }
726
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200727 return !self->IsExceptionPending();
728}
729
Igor Murashkin158f35c2015-06-10 15:55:30 -0700730template<bool is_range, bool do_assignability_check>
731bool DoLambdaCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
732 const Instruction* inst, uint16_t inst_data, JValue* result) {
733 const uint4_t num_additional_registers = inst->VRegB_25x();
734 // Argument word count.
735 const uint16_t number_of_inputs = num_additional_registers + 1;
736 // The first input register is always present and is not encoded in the count.
737
738 // TODO: find a cleaner way to separate non-range and range information without duplicating
739 // code.
740 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in invoke-XXX.
741 uint32_t vregC = 0; // only used in invoke-XXX-range.
742 if (is_range) {
743 vregC = inst->VRegC_3rc();
744 } else {
745 // TODO(iam): See if it's possible to remove inst_data dependency from 35x to avoid this path
746 UNUSED(inst_data);
747 inst->GetAllArgs25x(arg);
748 }
749
750 // TODO: if there's an assignability check, throw instead?
751 DCHECK(called_method->IsStatic());
752
753 return DoCallCommon<is_range, do_assignability_check>(
754 called_method, self, shadow_frame,
755 result, number_of_inputs, arg, vregC);
756}
757
758template<bool is_range, bool do_assignability_check>
759bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
760 const Instruction* inst, uint16_t inst_data, JValue* result) {
761 // Argument word count.
762 const uint16_t number_of_inputs = (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
763
764 // TODO: find a cleaner way to separate non-range and range information without duplicating
765 // code.
766 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in invoke-XXX.
767 uint32_t vregC = 0;
768 if (is_range) {
769 vregC = inst->VRegC_3rc();
770 } else {
771 vregC = inst->VRegC_35c();
772 inst->GetVarArgs(arg, inst_data);
773 }
774
775 return DoCallCommon<is_range, do_assignability_check>(
776 called_method, self, shadow_frame,
777 result, number_of_inputs, arg, vregC);
778}
779
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100780template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200781bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
782 Thread* self, JValue* result) {
783 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
784 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
785 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
786 if (!is_range) {
787 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
788 CHECK_LE(length, 5);
789 }
790 if (UNLIKELY(length < 0)) {
791 ThrowNegativeArraySizeException(length);
792 return false;
793 }
794 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700795 Class* array_class = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(),
796 self, false, do_access_check);
797 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200798 DCHECK(self->IsExceptionPending());
799 return false;
800 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700801 CHECK(array_class->IsArrayClass());
802 Class* component_class = array_class->GetComponentType();
803 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
804 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
805 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200806 ThrowRuntimeException("Bad filled array request for type %s",
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700807 PrettyDescriptor(component_class).c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200808 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000809 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800810 "Found type %s; filled-new-array not implemented for anything but 'int'",
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700811 PrettyDescriptor(component_class).c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200812 }
813 return false;
814 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700815 Object* new_array = Array::Alloc<true>(self, array_class, length,
816 array_class->GetComponentSizeShift(),
817 Runtime::Current()->GetHeap()->GetCurrentAllocator());
818 if (UNLIKELY(new_array == nullptr)) {
819 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200820 return false;
821 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700822 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
823 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200824 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100825 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200826 } else {
Ian Rogers29a26482014-05-02 15:27:29 -0700827 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100828 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100829 for (int32_t i = 0; i < length; ++i) {
830 size_t src_reg = is_range ? vregC + i : arg[i];
831 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700832 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
833 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +0100834 } else {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700835 new_array->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(
836 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200837 }
838 }
839
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700840 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200841 return true;
842}
843
Mathieu Chartier90443472015-07-16 20:32:27 -0700844// TODO fix thread analysis: should be SHARED_REQUIRES(Locks::mutator_lock_).
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100845template<typename T>
846static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count)
847 NO_THREAD_SAFETY_ANALYSIS {
848 Runtime* runtime = Runtime::Current();
849 for (int32_t i = 0; i < count; ++i) {
850 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
851 }
852}
853
854void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
Mathieu Chartier90443472015-07-16 20:32:27 -0700855 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100856 DCHECK(Runtime::Current()->IsActiveTransaction());
857 DCHECK(array != nullptr);
858 DCHECK_LE(count, array->GetLength());
859 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
860 switch (primitive_component_type) {
861 case Primitive::kPrimBoolean:
862 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
863 break;
864 case Primitive::kPrimByte:
865 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
866 break;
867 case Primitive::kPrimChar:
868 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
869 break;
870 case Primitive::kPrimShort:
871 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
872 break;
873 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100874 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
875 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700876 case Primitive::kPrimFloat:
877 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
878 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100879 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100880 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
881 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700882 case Primitive::kPrimDouble:
883 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
884 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100885 default:
886 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
887 << " in fill-array-data";
888 break;
889 }
890}
891
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200892// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200893#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700894 template SHARED_REQUIRES(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100895 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
896 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +0200897 const Instruction* inst, uint16_t inst_data, \
898 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200899EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
900EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
901EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
902EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
903#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200904
Igor Murashkin158f35c2015-06-10 15:55:30 -0700905// Explicit DoLambdaCall template function declarations.
906#define EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700907 template SHARED_REQUIRES(Locks::mutator_lock_) \
Igor Murashkin158f35c2015-06-10 15:55:30 -0700908 bool DoLambdaCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
909 ShadowFrame& shadow_frame, \
910 const Instruction* inst, \
911 uint16_t inst_data, \
912 JValue* result)
913EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(false, false);
914EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(false, true);
915EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(true, false);
916EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(true, true);
917#undef EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL
918
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200919// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100920#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700921 template SHARED_REQUIRES(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100922 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
923 const ShadowFrame& shadow_frame, \
924 Thread* self, JValue* result)
925#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
926 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
927 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
928 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
929 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
930EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
931EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
932#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200933#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
934
935} // namespace interpreter
936} // namespace art