blob: af673793758418987b3aa3cd7e2c82f193450e89 [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
Mathieu Chartiere401d142015-04-22 13:56:20 -0700417uint32_t FindNextInstructionFollowingException(
418 Thread* self, ShadowFrame& shadow_frame, uint32_t dex_pc,
419 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700420 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700421 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000422 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000423 if (instrumentation->HasExceptionCaughtListeners()
424 && self->IsExceptionThrownByCurrentMethod(exception.Get())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000425 instrumentation->ExceptionCaughtEvent(self, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200426 }
Ian Rogers54874942014-06-10 16:31:03 -0700427 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700428 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
429 hs.NewHandle(exception->GetClass()), dex_pc, &clear_exception);
Ian Rogers54874942014-06-10 16:31:03 -0700430 if (found_dex_pc == DexFile::kDexNoIndex) {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000431 // Exception is not caught by the current method. We will unwind to the
432 // caller. Notify any instrumentation listener.
Sebastien Hertz9f102032014-05-23 08:59:42 +0200433 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
Ian Rogers54874942014-06-10 16:31:03 -0700434 shadow_frame.GetMethod(), dex_pc);
435 } else {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000436 // Exception is caught in the current method. We will jump to the found_dex_pc.
Ian Rogers54874942014-06-10 16:31:03 -0700437 if (clear_exception) {
438 self->ClearException();
439 }
440 }
441 return found_dex_pc;
442}
443
Ian Rogerse94652f2014-12-02 11:13:19 -0800444void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
445 LOG(FATAL) << "Unexpected instruction: "
446 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
447 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700448}
449
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200450// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800451static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
452 size_t dest_reg, size_t src_reg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700453 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700454 // Uint required, so that sign extension does not make this wrong on 64b systems
455 uint32_t src_value = shadow_frame.GetVReg(src_reg);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800456 mirror::Object* o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700457
458 // If both register locations contains the same value, the register probably holds a reference.
459 // Note: As an optimization, non-moving collectors leave a stale reference value
460 // in the references array even after the original vreg was overwritten to a non-reference.
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700461 if (src_value == reinterpret_cast<uintptr_t>(o)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800462 new_shadow_frame->SetVRegReference(dest_reg, o);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200463 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800464 new_shadow_frame->SetVReg(dest_reg, src_value);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200465 }
466}
467
Sebastien Hertz45b15972015-04-03 16:07:05 +0200468void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700469 va_list args;
470 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200471 AbortTransactionV(self, fmt, args);
472 va_end(args);
473}
474
475void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
476 CHECK(Runtime::Current()->IsActiveTransaction());
477 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100478 std::string abort_msg;
479 StringAppendV(&abort_msg, fmt, args);
480 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200481 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700482}
483
Igor Murashkin158f35c2015-06-10 15:55:30 -0700484// Separate declaration is required solely for the attributes.
Mathieu Chartier90443472015-07-16 20:32:27 -0700485template<bool is_range, bool do_assignability_check> SHARED_REQUIRES(Locks::mutator_lock_)
Igor Murashkin158f35c2015-06-10 15:55:30 -0700486static inline bool DoCallCommon(ArtMethod* called_method,
487 Thread* self,
488 ShadowFrame& shadow_frame,
489 JValue* result,
490 uint16_t number_of_inputs,
491 uint32_t arg[Instruction::kMaxVarArgRegs],
492 uint32_t vregC) ALWAYS_INLINE;
493
Mathieu Chartier90443472015-07-16 20:32:27 -0700494SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +0000495static inline bool NeedsInterpreter(Thread* self, ShadowFrame* new_shadow_frame) ALWAYS_INLINE;
496
497static inline bool NeedsInterpreter(Thread* self, ShadowFrame* new_shadow_frame) {
498 ArtMethod* target = new_shadow_frame->GetMethod();
499 if (UNLIKELY(target->IsNative() || target->IsProxyMethod())) {
500 return false;
501 }
502 Runtime* runtime = Runtime::Current();
503 ClassLinker* class_linker = runtime->GetClassLinker();
504 return runtime->GetInstrumentation()->IsForcedInterpretOnly() ||
505 // Doing this check avoids doing compiled/interpreter transitions.
506 class_linker->IsQuickToInterpreterBridge(target->GetEntryPointFromQuickCompiledCode()) ||
507 // Force the use of interpreter when it is required by the debugger.
508 Dbg::IsForcedInterpreterNeededForCalling(self, target);
509}
510
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200511template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -0700512static inline bool DoCallCommon(ArtMethod* called_method,
513 Thread* self,
514 ShadowFrame& shadow_frame,
515 JValue* result,
516 uint16_t number_of_inputs,
517 uint32_t arg[Instruction::kMaxVarArgRegs],
518 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800519 bool string_init = false;
520 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700521 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
522 && called_method->IsConstructor())) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800523 ScopedObjectAccessUnchecked soa(self);
524 jmethodID mid = soa.EncodeMethod(called_method);
525 called_method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
526 string_init = true;
527 }
528
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200529 // Compute method information.
Ian Rogerse94652f2014-12-02 11:13:19 -0800530 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
Igor Murashkin158f35c2015-06-10 15:55:30 -0700531
532 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200533 uint16_t num_regs;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700534 if (LIKELY(code_item != nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200535 num_regs = code_item->registers_size_;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700536 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200537 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800538 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Igor Murashkin158f35c2015-06-10 15:55:30 -0700539 num_regs = number_of_inputs;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200540 }
541
Igor Murashkin158f35c2015-06-10 15:55:30 -0700542 // Hack for String init:
543 //
544 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
545 // invoke-x StringFactory(a, b, c, ...)
546 // by effectively dropping the first virtual register from the invoke.
547 //
548 // (at this point the ArtMethod has already been replaced,
549 // so we just need to fix-up the arguments)
550 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -0700551 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700552 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 -0700553
Igor Murashkin158f35c2015-06-10 15:55:30 -0700554 // The new StringFactory call is static and has one fewer argument.
Igor Murashkina06b49b2015-06-25 15:18:12 -0700555 if (code_item == nullptr) {
556 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
557 num_regs--;
558 } // 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 -0700559 number_of_inputs--;
560
561 // Rewrite the var-args, dropping the 0th argument ("this")
562 for (uint32_t i = 1; i < Instruction::kMaxVarArgRegs; ++i) {
563 arg[i - 1] = arg[i];
564 }
565 arg[Instruction::kMaxVarArgRegs - 1] = 0;
566
567 // Rewrite the non-var-arg case
568 vregC++; // Skips the 0th vreg in the range ("this").
569 }
570
571 // Parameter registers go at the end of the shadow frame.
572 DCHECK_GE(num_regs, number_of_inputs);
573 size_t first_dest_reg = num_regs - number_of_inputs;
574 DCHECK_NE(first_dest_reg, (size_t)-1);
575
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200576 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700577 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200578 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
Ian Rogerse94652f2014-12-02 11:13:19 -0800579 ShadowFrame* new_shadow_frame(ShadowFrame::Create(num_regs, &shadow_frame, called_method, 0,
580 memory));
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200581
Igor Murashkin158f35c2015-06-10 15:55:30 -0700582 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -0700583 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700584 // Slow path.
585 // We might need to do class loading, which incurs a thread state change to kNative. So
586 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700587 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +0200588 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700589 self->EndAssertNoThreadSuspension(old_cause);
590
591 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200592 // to get the exact type of each reference argument.
Ian Rogerse94652f2014-12-02 11:13:19 -0800593 const DexFile::TypeList* params = new_shadow_frame->GetMethod()->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700594 uint32_t shorty_len = 0;
Ian Rogerse94652f2014-12-02 11:13:19 -0800595 const char* shorty = new_shadow_frame->GetMethod()->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200596
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100597 // Handle receiver apart since it's not part of the shorty.
598 size_t dest_reg = first_dest_reg;
599 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700600
Ian Rogerse94652f2014-12-02 11:13:19 -0800601 if (!new_shadow_frame->GetMethod()->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700602 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100603 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
604 ++dest_reg;
605 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -0700606 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100607 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700608
609 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800610 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -0700611 // Skip the 0th 'shorty' type since it represents the return type.
612 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200613 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
614 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700615 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200616 case 'L': {
617 Object* o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700618 if (do_assignability_check && o != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100619 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Ian Rogersa0485602014-12-02 15:48:04 -0800620 Class* arg_type =
621 new_shadow_frame->GetMethod()->GetClassFromTypeIndex(
Vladimir Marko05792b92015-08-03 11:56:49 +0100622 params->GetTypeItem(shorty_pos).type_idx_, true /* resolve */, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700623 if (arg_type == nullptr) {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200624 CHECK(self->IsExceptionPending());
625 return false;
626 }
627 if (!o->VerifierInstanceOf(arg_type)) {
628 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700629 std::string temp1, temp2;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000630 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200631 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -0800632 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700633 o->GetClass()->GetDescriptor(&temp1),
634 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200635 return false;
636 }
Jeff Haoa3faaf42013-09-03 19:07:00 -0700637 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200638 new_shadow_frame->SetVRegReference(dest_reg, o);
639 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700640 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700641 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200642 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700643 uint64_t wide_value =
644 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
645 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200646 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700647 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200648 ++dest_reg;
649 ++arg_offset;
650 break;
651 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700652 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200653 default:
654 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
655 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200656 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200657 }
658 } else {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700659 size_t arg_index = 0;
660
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200661 // Fast path: no extra checks.
662 if (is_range) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700663 // TODO: Implement the range version of invoke-lambda
664 uint16_t first_src_reg = vregC;
665
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200666 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs;
667 ++dest_reg, ++src_reg) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800668 AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200669 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200670 } else {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700671 DCHECK_LE(number_of_inputs, Instruction::kMaxVarArgRegs);
672
673 for (; arg_index < number_of_inputs; ++arg_index) {
674 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, arg[arg_index]);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200675 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200676 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700677 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200678 }
679
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200680 // Do the call now.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200681 if (LIKELY(Runtime::Current()->IsStarted())) {
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +0000682 if (NeedsInterpreter(self, new_shadow_frame)) {
683 artInterpreterToInterpreterBridge(self, code_item, new_shadow_frame, result);
Nicolas Geoffray7070ccd2015-07-08 09:41:54 +0000684 } else {
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +0000685 artInterpreterToCompiledCodeBridge(self, code_item, new_shadow_frame, result);
Nicolas Geoffray7070ccd2015-07-08 09:41:54 +0000686 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200687 } else {
Andreas Gampe799681b2015-05-15 19:24:12 -0700688 UnstartedRuntime::Invoke(self, code_item, new_shadow_frame, result, first_dest_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200689 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800690
691 if (string_init && !self->IsExceptionPending()) {
692 // Set the new string result of the StringFactory.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700693 shadow_frame.SetVRegReference(string_init_vreg_this, result->GetL());
Jeff Hao848f70a2014-01-15 13:49:50 -0800694 // Overwrite all potential copies of the original result of the new-instance of string with the
695 // new result of the StringFactory. Use the verifier to find this set of registers.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700696 ArtMethod* method = shadow_frame.GetMethod();
Jeff Hao848f70a2014-01-15 13:49:50 -0800697 MethodReference method_ref = method->ToMethodReference();
698 SafeMap<uint32_t, std::set<uint32_t>> string_init_map;
699 SafeMap<uint32_t, std::set<uint32_t>>* string_init_map_ptr;
700 MethodRefToStringInitRegMap& method_to_string_init_map = Runtime::Current()->GetStringInitMap();
701 auto it = method_to_string_init_map.find(method_ref);
702 if (it == method_to_string_init_map.end()) {
703 string_init_map = std::move(verifier::MethodVerifier::FindStringInitMap(method));
704 method_to_string_init_map.Overwrite(method_ref, string_init_map);
705 string_init_map_ptr = &string_init_map;
706 } else {
707 string_init_map_ptr = &it->second;
708 }
709 if (string_init_map_ptr->size() != 0) {
710 uint32_t dex_pc = shadow_frame.GetDexPC();
711 auto map_it = string_init_map_ptr->find(dex_pc);
712 if (map_it != string_init_map_ptr->end()) {
713 const std::set<uint32_t>& reg_set = map_it->second;
714 for (auto set_it = reg_set.begin(); set_it != reg_set.end(); ++set_it) {
715 shadow_frame.SetVRegReference(*set_it, result->GetL());
716 }
717 }
718 }
719 }
720
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200721 return !self->IsExceptionPending();
722}
723
Igor Murashkin158f35c2015-06-10 15:55:30 -0700724template<bool is_range, bool do_assignability_check>
725bool DoLambdaCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
726 const Instruction* inst, uint16_t inst_data, JValue* result) {
727 const uint4_t num_additional_registers = inst->VRegB_25x();
728 // Argument word count.
729 const uint16_t number_of_inputs = num_additional_registers + 1;
730 // The first input register is always present and is not encoded in the count.
731
732 // TODO: find a cleaner way to separate non-range and range information without duplicating
733 // code.
734 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in invoke-XXX.
735 uint32_t vregC = 0; // only used in invoke-XXX-range.
736 if (is_range) {
737 vregC = inst->VRegC_3rc();
738 } else {
739 // TODO(iam): See if it's possible to remove inst_data dependency from 35x to avoid this path
740 UNUSED(inst_data);
741 inst->GetAllArgs25x(arg);
742 }
743
744 // TODO: if there's an assignability check, throw instead?
745 DCHECK(called_method->IsStatic());
746
747 return DoCallCommon<is_range, do_assignability_check>(
748 called_method, self, shadow_frame,
749 result, number_of_inputs, arg, vregC);
750}
751
752template<bool is_range, bool do_assignability_check>
753bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
754 const Instruction* inst, uint16_t inst_data, JValue* result) {
755 // Argument word count.
756 const uint16_t number_of_inputs = (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
757
758 // TODO: find a cleaner way to separate non-range and range information without duplicating
759 // code.
760 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in invoke-XXX.
761 uint32_t vregC = 0;
762 if (is_range) {
763 vregC = inst->VRegC_3rc();
764 } else {
765 vregC = inst->VRegC_35c();
766 inst->GetVarArgs(arg, inst_data);
767 }
768
769 return DoCallCommon<is_range, do_assignability_check>(
770 called_method, self, shadow_frame,
771 result, number_of_inputs, arg, vregC);
772}
773
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100774template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200775bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
776 Thread* self, JValue* result) {
777 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
778 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
779 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
780 if (!is_range) {
781 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
782 CHECK_LE(length, 5);
783 }
784 if (UNLIKELY(length < 0)) {
785 ThrowNegativeArraySizeException(length);
786 return false;
787 }
788 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700789 Class* array_class = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(),
790 self, false, do_access_check);
791 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200792 DCHECK(self->IsExceptionPending());
793 return false;
794 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700795 CHECK(array_class->IsArrayClass());
796 Class* component_class = array_class->GetComponentType();
797 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
798 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
799 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200800 ThrowRuntimeException("Bad filled array request for type %s",
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700801 PrettyDescriptor(component_class).c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200802 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000803 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800804 "Found type %s; filled-new-array not implemented for anything but 'int'",
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700805 PrettyDescriptor(component_class).c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200806 }
807 return false;
808 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700809 Object* new_array = Array::Alloc<true>(self, array_class, length,
810 array_class->GetComponentSizeShift(),
811 Runtime::Current()->GetHeap()->GetCurrentAllocator());
812 if (UNLIKELY(new_array == nullptr)) {
813 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200814 return false;
815 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700816 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
817 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200818 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100819 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200820 } else {
Ian Rogers29a26482014-05-02 15:27:29 -0700821 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100822 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100823 for (int32_t i = 0; i < length; ++i) {
824 size_t src_reg = is_range ? vregC + i : arg[i];
825 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700826 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
827 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +0100828 } else {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700829 new_array->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(
830 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200831 }
832 }
833
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700834 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200835 return true;
836}
837
Mathieu Chartier90443472015-07-16 20:32:27 -0700838// TODO fix thread analysis: should be SHARED_REQUIRES(Locks::mutator_lock_).
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100839template<typename T>
840static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count)
841 NO_THREAD_SAFETY_ANALYSIS {
842 Runtime* runtime = Runtime::Current();
843 for (int32_t i = 0; i < count; ++i) {
844 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
845 }
846}
847
848void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
Mathieu Chartier90443472015-07-16 20:32:27 -0700849 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100850 DCHECK(Runtime::Current()->IsActiveTransaction());
851 DCHECK(array != nullptr);
852 DCHECK_LE(count, array->GetLength());
853 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
854 switch (primitive_component_type) {
855 case Primitive::kPrimBoolean:
856 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
857 break;
858 case Primitive::kPrimByte:
859 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
860 break;
861 case Primitive::kPrimChar:
862 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
863 break;
864 case Primitive::kPrimShort:
865 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
866 break;
867 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100868 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
869 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700870 case Primitive::kPrimFloat:
871 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
872 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100873 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100874 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
875 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700876 case Primitive::kPrimDouble:
877 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
878 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100879 default:
880 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
881 << " in fill-array-data";
882 break;
883 }
884}
885
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200886// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200887#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700888 template SHARED_REQUIRES(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100889 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
890 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +0200891 const Instruction* inst, uint16_t inst_data, \
892 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200893EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
894EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
895EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
896EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
897#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200898
Igor Murashkin158f35c2015-06-10 15:55:30 -0700899// Explicit DoLambdaCall template function declarations.
900#define EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700901 template SHARED_REQUIRES(Locks::mutator_lock_) \
Igor Murashkin158f35c2015-06-10 15:55:30 -0700902 bool DoLambdaCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
903 ShadowFrame& shadow_frame, \
904 const Instruction* inst, \
905 uint16_t inst_data, \
906 JValue* result)
907EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(false, false);
908EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(false, true);
909EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(true, false);
910EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(true, true);
911#undef EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL
912
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200913// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100914#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700915 template SHARED_REQUIRES(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100916 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
917 const ShadowFrame& shadow_frame, \
918 Thread* self, JValue* result)
919#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
920 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
921 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
922 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
923 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
924EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
925EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
926#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200927#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
928
929} // namespace interpreter
930} // namespace art