blob: 2de12dcec27a179dde35209a028ddc900a3a1411 [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
Igor Murashkin6918bf12015-09-27 19:19:06 -070030// All lambda closures have to be a consecutive pair of virtual registers.
31static constexpr size_t kLambdaVirtualRegisterWidth = 2;
32
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000033void ThrowNullPointerExceptionFromInterpreter() {
34 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -070035}
36
37template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
38bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
39 uint16_t inst_data) {
40 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
41 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
42 ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
Fred Shih37f05ef2014-07-16 18:38:08 -070043 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -070044 if (UNLIKELY(f == nullptr)) {
45 CHECK(self->IsExceptionPending());
46 return false;
47 }
48 Object* obj;
49 if (is_static) {
50 obj = f->GetDeclaringClass();
51 } else {
52 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
53 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000054 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers54874942014-06-10 16:31:03 -070055 return false;
56 }
57 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +020058 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -070059 // Report this field access to instrumentation if needed.
60 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
61 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
62 Object* this_object = f->IsStatic() ? nullptr : obj;
63 instrumentation->FieldReadEvent(self, this_object, shadow_frame.GetMethod(),
64 shadow_frame.GetDexPC(), f);
65 }
66 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
67 switch (field_type) {
68 case Primitive::kPrimBoolean:
69 shadow_frame.SetVReg(vregA, f->GetBoolean(obj));
70 break;
71 case Primitive::kPrimByte:
72 shadow_frame.SetVReg(vregA, f->GetByte(obj));
73 break;
74 case Primitive::kPrimChar:
75 shadow_frame.SetVReg(vregA, f->GetChar(obj));
76 break;
77 case Primitive::kPrimShort:
78 shadow_frame.SetVReg(vregA, f->GetShort(obj));
79 break;
80 case Primitive::kPrimInt:
81 shadow_frame.SetVReg(vregA, f->GetInt(obj));
82 break;
83 case Primitive::kPrimLong:
84 shadow_frame.SetVRegLong(vregA, f->GetLong(obj));
85 break;
86 case Primitive::kPrimNot:
87 shadow_frame.SetVRegReference(vregA, f->GetObject(obj));
88 break;
89 default:
90 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -070091 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -070092 }
93 return true;
94}
95
96// Explicitly instantiate all DoFieldGet functions.
97#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
98 template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \
99 ShadowFrame& shadow_frame, \
100 const Instruction* inst, \
101 uint16_t inst_data)
102
103#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
104 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \
105 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true);
106
107// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700108EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
109EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
110EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
111EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
112EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
113EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
114EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700115
116// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700117EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
118EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
119EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
120EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
121EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
122EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
123EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700124
125#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
126#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
127
128// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
129// Returns true on success, otherwise throws an exception and returns false.
130template<Primitive::Type field_type>
131bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
132 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
133 if (UNLIKELY(obj == nullptr)) {
134 // We lost the reference to the field index so we cannot get a more
135 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000136 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700137 return false;
138 }
139 MemberOffset field_offset(inst->VRegC_22c());
140 // Report this field access to instrumentation if needed. Since we only have the offset of
141 // the field from the base of the object, we need to look for it first.
142 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
143 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
144 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
145 field_offset.Uint32Value());
146 DCHECK(f != nullptr);
147 DCHECK(!f->IsStatic());
148 instrumentation->FieldReadEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
149 shadow_frame.GetDexPC(), f);
150 }
151 // Note: iget-x-quick instructions are only for non-volatile fields.
152 const uint32_t vregA = inst->VRegA_22c(inst_data);
153 switch (field_type) {
154 case Primitive::kPrimInt:
155 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
156 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800157 case Primitive::kPrimBoolean:
158 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
159 break;
160 case Primitive::kPrimByte:
161 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
162 break;
163 case Primitive::kPrimChar:
164 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
165 break;
166 case Primitive::kPrimShort:
167 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
168 break;
Ian Rogers54874942014-06-10 16:31:03 -0700169 case Primitive::kPrimLong:
170 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
171 break;
172 case Primitive::kPrimNot:
173 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
174 break;
175 default:
176 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700177 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700178 }
179 return true;
180}
181
182// Explicitly instantiate all DoIGetQuick functions.
183#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
184 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
185 uint16_t inst_data)
186
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800187EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
188EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
189EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
190EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
191EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
192EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
193EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700194#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
195
196template<Primitive::Type field_type>
197static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700198 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700199 JValue field_value;
200 switch (field_type) {
201 case Primitive::kPrimBoolean:
202 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
203 break;
204 case Primitive::kPrimByte:
205 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
206 break;
207 case Primitive::kPrimChar:
208 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
209 break;
210 case Primitive::kPrimShort:
211 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
212 break;
213 case Primitive::kPrimInt:
214 field_value.SetI(shadow_frame.GetVReg(vreg));
215 break;
216 case Primitive::kPrimLong:
217 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
218 break;
219 case Primitive::kPrimNot:
220 field_value.SetL(shadow_frame.GetVRegReference(vreg));
221 break;
222 default:
223 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700224 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700225 }
226 return field_value;
227}
228
229template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
230 bool transaction_active>
231bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
232 uint16_t inst_data) {
233 bool do_assignability_check = do_access_check;
234 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
235 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
236 ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
Fred Shih37f05ef2014-07-16 18:38:08 -0700237 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -0700238 if (UNLIKELY(f == nullptr)) {
239 CHECK(self->IsExceptionPending());
240 return false;
241 }
242 Object* obj;
243 if (is_static) {
244 obj = f->GetDeclaringClass();
245 } else {
246 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
247 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000248 ThrowNullPointerExceptionForFieldAccess(f, false);
Ian Rogers54874942014-06-10 16:31:03 -0700249 return false;
250 }
251 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +0200252 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -0700253 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
254 // Report this field access to instrumentation if needed. Since we only have the offset of
255 // the field from the base of the object, we need to look for it first.
256 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
257 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
258 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
259 Object* this_object = f->IsStatic() ? nullptr : obj;
260 instrumentation->FieldWriteEvent(self, this_object, shadow_frame.GetMethod(),
261 shadow_frame.GetDexPC(), f, field_value);
262 }
263 switch (field_type) {
264 case Primitive::kPrimBoolean:
265 f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA));
266 break;
267 case Primitive::kPrimByte:
268 f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA));
269 break;
270 case Primitive::kPrimChar:
271 f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA));
272 break;
273 case Primitive::kPrimShort:
274 f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA));
275 break;
276 case Primitive::kPrimInt:
277 f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA));
278 break;
279 case Primitive::kPrimLong:
280 f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA));
281 break;
282 case Primitive::kPrimNot: {
283 Object* reg = shadow_frame.GetVRegReference(vregA);
284 if (do_assignability_check && reg != nullptr) {
285 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
286 // object in the destructor.
287 Class* field_class;
288 {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700289 StackHandleScope<2> hs(self);
Ian Rogers54874942014-06-10 16:31:03 -0700290 HandleWrapper<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
291 HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700292 field_class = f->GetType<true>();
Ian Rogers54874942014-06-10 16:31:03 -0700293 }
294 if (!reg->VerifierInstanceOf(field_class)) {
295 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700296 std::string temp1, temp2, temp3;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000297 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Ian Rogers54874942014-06-10 16:31:03 -0700298 "Put '%s' that is not instance of field '%s' in '%s'",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700299 reg->GetClass()->GetDescriptor(&temp1),
300 field_class->GetDescriptor(&temp2),
301 f->GetDeclaringClass()->GetDescriptor(&temp3));
Ian Rogers54874942014-06-10 16:31:03 -0700302 return false;
303 }
304 }
305 f->SetObj<transaction_active>(obj, reg);
306 break;
307 }
308 default:
309 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700310 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700311 }
312 return true;
313}
314
315// Explicitly instantiate all DoFieldPut functions.
316#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
317 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
318 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
319
320#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
321 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
322 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
323 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
324 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
325
326// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700327EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
328EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
329EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
330EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
331EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
332EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
333EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700334
335// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700336EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
337EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
338EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
339EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
340EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
341EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
342EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700343
344#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
345#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
346
347template<Primitive::Type field_type, bool transaction_active>
348bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
349 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
350 if (UNLIKELY(obj == nullptr)) {
351 // We lost the reference to the field index so we cannot get a more
352 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000353 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700354 return false;
355 }
356 MemberOffset field_offset(inst->VRegC_22c());
357 const uint32_t vregA = inst->VRegA_22c(inst_data);
358 // Report this field modification to instrumentation if needed. Since we only have the offset of
359 // the field from the base of the object, we need to look for it first.
360 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
361 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
362 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
363 field_offset.Uint32Value());
364 DCHECK(f != nullptr);
365 DCHECK(!f->IsStatic());
366 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
367 instrumentation->FieldWriteEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
368 shadow_frame.GetDexPC(), f, field_value);
369 }
370 // Note: iput-x-quick instructions are only for non-volatile fields.
371 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700372 case Primitive::kPrimBoolean:
373 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
374 break;
375 case Primitive::kPrimByte:
376 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
377 break;
378 case Primitive::kPrimChar:
379 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
380 break;
381 case Primitive::kPrimShort:
382 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
383 break;
Ian Rogers54874942014-06-10 16:31:03 -0700384 case Primitive::kPrimInt:
385 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
386 break;
387 case Primitive::kPrimLong:
388 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
389 break;
390 case Primitive::kPrimNot:
391 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
392 break;
393 default:
394 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700395 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700396 }
397 return true;
398}
399
400// Explicitly instantiate all DoIPutQuick functions.
401#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
402 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
403 const Instruction* inst, \
404 uint16_t inst_data)
405
406#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
407 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
408 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
409
Andreas Gampec8ccf682014-09-29 20:07:43 -0700410EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
411EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
412EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
413EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
414EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
415EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
416EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700417#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
418#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
419
Sebastien Hertz520633b2015-09-08 17:03:36 +0200420// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700421uint32_t FindNextInstructionFollowingException(
422 Thread* self, ShadowFrame& shadow_frame, uint32_t dex_pc,
423 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700424 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700425 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000426 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Sebastien Hertz520633b2015-09-08 17:03:36 +0200427 if (instrumentation != nullptr && instrumentation->HasExceptionCaughtListeners()
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000428 && self->IsExceptionThrownByCurrentMethod(exception.Get())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000429 instrumentation->ExceptionCaughtEvent(self, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200430 }
Ian Rogers54874942014-06-10 16:31:03 -0700431 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700432 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
433 hs.NewHandle(exception->GetClass()), dex_pc, &clear_exception);
Sebastien Hertz520633b2015-09-08 17:03:36 +0200434 if (found_dex_pc == DexFile::kDexNoIndex && instrumentation != nullptr) {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000435 // Exception is not caught by the current method. We will unwind to the
436 // caller. Notify any instrumentation listener.
Sebastien Hertz9f102032014-05-23 08:59:42 +0200437 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
Ian Rogers54874942014-06-10 16:31:03 -0700438 shadow_frame.GetMethod(), dex_pc);
439 } else {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000440 // Exception is caught in the current method. We will jump to the found_dex_pc.
Ian Rogers54874942014-06-10 16:31:03 -0700441 if (clear_exception) {
442 self->ClearException();
443 }
444 }
445 return found_dex_pc;
446}
447
Ian Rogerse94652f2014-12-02 11:13:19 -0800448void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
449 LOG(FATAL) << "Unexpected instruction: "
450 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
451 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700452}
453
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200454// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800455static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
456 size_t dest_reg, size_t src_reg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700457 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700458 // Uint required, so that sign extension does not make this wrong on 64b systems
459 uint32_t src_value = shadow_frame.GetVReg(src_reg);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800460 mirror::Object* o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700461
462 // If both register locations contains the same value, the register probably holds a reference.
463 // Note: As an optimization, non-moving collectors leave a stale reference value
464 // in the references array even after the original vreg was overwritten to a non-reference.
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700465 if (src_value == reinterpret_cast<uintptr_t>(o)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800466 new_shadow_frame->SetVRegReference(dest_reg, o);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200467 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800468 new_shadow_frame->SetVReg(dest_reg, src_value);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200469 }
470}
471
Sebastien Hertz45b15972015-04-03 16:07:05 +0200472void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700473 va_list args;
474 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200475 AbortTransactionV(self, fmt, args);
476 va_end(args);
477}
478
479void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
480 CHECK(Runtime::Current()->IsActiveTransaction());
481 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100482 std::string abort_msg;
483 StringAppendV(&abort_msg, fmt, args);
484 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200485 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700486}
487
Igor Murashkin158f35c2015-06-10 15:55:30 -0700488// Separate declaration is required solely for the attributes.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700489template <bool is_range,
490 bool do_assignability_check,
491 size_t kVarArgMax>
492 SHARED_REQUIRES(Locks::mutator_lock_)
Igor Murashkin158f35c2015-06-10 15:55:30 -0700493static inline bool DoCallCommon(ArtMethod* called_method,
494 Thread* self,
495 ShadowFrame& shadow_frame,
496 JValue* result,
497 uint16_t number_of_inputs,
Igor Murashkin6918bf12015-09-27 19:19:06 -0700498 uint32_t (&arg)[kVarArgMax],
Igor Murashkin158f35c2015-06-10 15:55:30 -0700499 uint32_t vregC) ALWAYS_INLINE;
500
Mathieu Chartier90443472015-07-16 20:32:27 -0700501SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +0000502static inline bool NeedsInterpreter(Thread* self, ShadowFrame* new_shadow_frame) ALWAYS_INLINE;
503
504static inline bool NeedsInterpreter(Thread* self, ShadowFrame* new_shadow_frame) {
505 ArtMethod* target = new_shadow_frame->GetMethod();
506 if (UNLIKELY(target->IsNative() || target->IsProxyMethod())) {
507 return false;
508 }
509 Runtime* runtime = Runtime::Current();
510 ClassLinker* class_linker = runtime->GetClassLinker();
511 return runtime->GetInstrumentation()->IsForcedInterpretOnly() ||
512 // Doing this check avoids doing compiled/interpreter transitions.
513 class_linker->IsQuickToInterpreterBridge(target->GetEntryPointFromQuickCompiledCode()) ||
514 // Force the use of interpreter when it is required by the debugger.
515 Dbg::IsForcedInterpreterNeededForCalling(self, target);
516}
517
Igor Murashkin6918bf12015-09-27 19:19:06 -0700518template <bool is_range,
519 bool do_assignability_check,
520 size_t kVarArgMax>
Igor Murashkin158f35c2015-06-10 15:55:30 -0700521static inline bool DoCallCommon(ArtMethod* called_method,
522 Thread* self,
523 ShadowFrame& shadow_frame,
524 JValue* result,
525 uint16_t number_of_inputs,
Igor Murashkin6918bf12015-09-27 19:19:06 -0700526 uint32_t (&arg)[kVarArgMax],
Igor Murashkin158f35c2015-06-10 15:55:30 -0700527 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800528 bool string_init = false;
529 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700530 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
531 && called_method->IsConstructor())) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800532 ScopedObjectAccessUnchecked soa(self);
533 jmethodID mid = soa.EncodeMethod(called_method);
534 called_method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
535 string_init = true;
536 }
537
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200538 // Compute method information.
Ian Rogerse94652f2014-12-02 11:13:19 -0800539 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
Igor Murashkin158f35c2015-06-10 15:55:30 -0700540
541 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200542 uint16_t num_regs;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700543 if (LIKELY(code_item != nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200544 num_regs = code_item->registers_size_;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700545 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200546 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800547 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Igor Murashkin158f35c2015-06-10 15:55:30 -0700548 num_regs = number_of_inputs;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200549 }
550
Igor Murashkin158f35c2015-06-10 15:55:30 -0700551 // Hack for String init:
552 //
553 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
554 // invoke-x StringFactory(a, b, c, ...)
555 // by effectively dropping the first virtual register from the invoke.
556 //
557 // (at this point the ArtMethod has already been replaced,
558 // so we just need to fix-up the arguments)
559 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -0700560 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700561 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 -0700562
Igor Murashkin158f35c2015-06-10 15:55:30 -0700563 // The new StringFactory call is static and has one fewer argument.
Igor Murashkina06b49b2015-06-25 15:18:12 -0700564 if (code_item == nullptr) {
565 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
566 num_regs--;
567 } // 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 -0700568 number_of_inputs--;
569
570 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -0700571 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700572 arg[i - 1] = arg[i];
573 }
Igor Murashkin6918bf12015-09-27 19:19:06 -0700574 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700575
576 // Rewrite the non-var-arg case
577 vregC++; // Skips the 0th vreg in the range ("this").
578 }
579
580 // Parameter registers go at the end of the shadow frame.
581 DCHECK_GE(num_regs, number_of_inputs);
582 size_t first_dest_reg = num_regs - number_of_inputs;
583 DCHECK_NE(first_dest_reg, (size_t)-1);
584
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200585 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700586 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200587 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
Ian Rogerse94652f2014-12-02 11:13:19 -0800588 ShadowFrame* new_shadow_frame(ShadowFrame::Create(num_regs, &shadow_frame, called_method, 0,
589 memory));
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200590
Igor Murashkin158f35c2015-06-10 15:55:30 -0700591 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -0700592 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700593 // Slow path.
594 // We might need to do class loading, which incurs a thread state change to kNative. So
595 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700596 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +0200597 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700598 self->EndAssertNoThreadSuspension(old_cause);
599
600 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200601 // to get the exact type of each reference argument.
Ian Rogerse94652f2014-12-02 11:13:19 -0800602 const DexFile::TypeList* params = new_shadow_frame->GetMethod()->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700603 uint32_t shorty_len = 0;
Ian Rogerse94652f2014-12-02 11:13:19 -0800604 const char* shorty = new_shadow_frame->GetMethod()->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200605
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100606 // Handle receiver apart since it's not part of the shorty.
607 size_t dest_reg = first_dest_reg;
608 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700609
Ian Rogerse94652f2014-12-02 11:13:19 -0800610 if (!new_shadow_frame->GetMethod()->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700611 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100612 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
613 ++dest_reg;
614 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -0700615 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100616 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700617
618 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800619 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -0700620 // Skip the 0th 'shorty' type since it represents the return type.
621 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200622 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
623 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700624 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200625 case 'L': {
626 Object* o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700627 if (do_assignability_check && o != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100628 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Ian Rogersa0485602014-12-02 15:48:04 -0800629 Class* arg_type =
630 new_shadow_frame->GetMethod()->GetClassFromTypeIndex(
Vladimir Marko05792b92015-08-03 11:56:49 +0100631 params->GetTypeItem(shorty_pos).type_idx_, true /* resolve */, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700632 if (arg_type == nullptr) {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200633 CHECK(self->IsExceptionPending());
634 return false;
635 }
636 if (!o->VerifierInstanceOf(arg_type)) {
637 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700638 std::string temp1, temp2;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000639 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200640 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -0800641 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700642 o->GetClass()->GetDescriptor(&temp1),
643 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200644 return false;
645 }
Jeff Haoa3faaf42013-09-03 19:07:00 -0700646 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200647 new_shadow_frame->SetVRegReference(dest_reg, o);
648 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700649 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700650 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200651 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700652 uint64_t wide_value =
653 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
654 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200655 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700656 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200657 ++dest_reg;
658 ++arg_offset;
659 break;
660 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700661 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200662 default:
663 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
664 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200665 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200666 }
667 } else {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700668 size_t arg_index = 0;
669
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200670 // Fast path: no extra checks.
671 if (is_range) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700672 // TODO: Implement the range version of invoke-lambda
673 uint16_t first_src_reg = vregC;
674
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200675 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs;
676 ++dest_reg, ++src_reg) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800677 AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200678 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200679 } else {
Igor Murashkin6918bf12015-09-27 19:19:06 -0700680 DCHECK_LE(number_of_inputs, arraysize(arg));
Igor Murashkin158f35c2015-06-10 15:55:30 -0700681
682 for (; arg_index < number_of_inputs; ++arg_index) {
683 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, arg[arg_index]);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200684 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200685 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700686 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200687 }
688
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200689 // Do the call now.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200690 if (LIKELY(Runtime::Current()->IsStarted())) {
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +0000691 if (NeedsInterpreter(self, new_shadow_frame)) {
692 artInterpreterToInterpreterBridge(self, code_item, new_shadow_frame, result);
Nicolas Geoffray7070ccd2015-07-08 09:41:54 +0000693 } else {
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +0000694 artInterpreterToCompiledCodeBridge(self, code_item, new_shadow_frame, result);
Nicolas Geoffray7070ccd2015-07-08 09:41:54 +0000695 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200696 } else {
Andreas Gampe799681b2015-05-15 19:24:12 -0700697 UnstartedRuntime::Invoke(self, code_item, new_shadow_frame, result, first_dest_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200698 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800699
700 if (string_init && !self->IsExceptionPending()) {
701 // Set the new string result of the StringFactory.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700702 shadow_frame.SetVRegReference(string_init_vreg_this, result->GetL());
Jeff Hao848f70a2014-01-15 13:49:50 -0800703 // Overwrite all potential copies of the original result of the new-instance of string with the
704 // new result of the StringFactory. Use the verifier to find this set of registers.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700705 ArtMethod* method = shadow_frame.GetMethod();
Jeff Hao848f70a2014-01-15 13:49:50 -0800706 MethodReference method_ref = method->ToMethodReference();
Jeff Hao4686c522015-09-18 14:44:32 -0700707 SafeMap<uint32_t, std::set<uint32_t>>* string_init_map_ptr = nullptr;
Jeff Hao848f70a2014-01-15 13:49:50 -0800708 MethodRefToStringInitRegMap& method_to_string_init_map = Runtime::Current()->GetStringInitMap();
Jeff Haoa2c38642015-09-17 17:29:01 -0700709 {
710 MutexLock mu(self, *Locks::interpreter_string_init_map_lock_);
Jeff Hao4686c522015-09-18 14:44:32 -0700711 auto it = method_to_string_init_map.find(method_ref);
712 if (it != method_to_string_init_map.end()) {
713 string_init_map_ptr = &it->second;
Jeff Haoa2c38642015-09-17 17:29:01 -0700714 }
Jeff Hao4686c522015-09-18 14:44:32 -0700715 }
716 if (string_init_map_ptr == nullptr) {
717 SafeMap<uint32_t, std::set<uint32_t>> string_init_map =
718 verifier::MethodVerifier::FindStringInitMap(method);
719 MutexLock mu(self, *Locks::interpreter_string_init_map_lock_);
Vladimir Marko78568352015-09-21 12:00:16 +0100720 auto it = method_to_string_init_map.lower_bound(method_ref);
721 if (it == method_to_string_init_map.end() ||
722 method_to_string_init_map.key_comp()(method_ref, it->first)) {
723 it = method_to_string_init_map.PutBefore(it, method_ref, std::move(string_init_map));
724 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800725 string_init_map_ptr = &it->second;
726 }
727 if (string_init_map_ptr->size() != 0) {
728 uint32_t dex_pc = shadow_frame.GetDexPC();
729 auto map_it = string_init_map_ptr->find(dex_pc);
730 if (map_it != string_init_map_ptr->end()) {
731 const std::set<uint32_t>& reg_set = map_it->second;
732 for (auto set_it = reg_set.begin(); set_it != reg_set.end(); ++set_it) {
733 shadow_frame.SetVRegReference(*set_it, result->GetL());
734 }
735 }
736 }
737 }
738
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200739 return !self->IsExceptionPending();
740}
741
Igor Murashkin158f35c2015-06-10 15:55:30 -0700742template<bool is_range, bool do_assignability_check>
743bool DoLambdaCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
744 const Instruction* inst, uint16_t inst_data, JValue* result) {
745 const uint4_t num_additional_registers = inst->VRegB_25x();
746 // Argument word count.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700747 const uint16_t number_of_inputs = num_additional_registers + kLambdaVirtualRegisterWidth;
748 // The lambda closure register is always present and is not encoded in the count.
749 // Furthermore, the lambda closure register is always wide, so it counts as 2 inputs.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700750
751 // TODO: find a cleaner way to separate non-range and range information without duplicating
752 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700753 uint32_t arg[Instruction::kMaxVarArgRegs25x]; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700754 uint32_t vregC = 0; // only used in invoke-XXX-range.
755 if (is_range) {
756 vregC = inst->VRegC_3rc();
757 } else {
758 // TODO(iam): See if it's possible to remove inst_data dependency from 35x to avoid this path
759 UNUSED(inst_data);
760 inst->GetAllArgs25x(arg);
761 }
762
763 // TODO: if there's an assignability check, throw instead?
764 DCHECK(called_method->IsStatic());
765
766 return DoCallCommon<is_range, do_assignability_check>(
767 called_method, self, shadow_frame,
768 result, number_of_inputs, arg, vregC);
769}
770
771template<bool is_range, bool do_assignability_check>
772bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
773 const Instruction* inst, uint16_t inst_data, JValue* result) {
774 // Argument word count.
775 const uint16_t number_of_inputs = (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
776
777 // TODO: find a cleaner way to separate non-range and range information without duplicating
778 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700779 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700780 uint32_t vregC = 0;
781 if (is_range) {
782 vregC = inst->VRegC_3rc();
783 } else {
784 vregC = inst->VRegC_35c();
785 inst->GetVarArgs(arg, inst_data);
786 }
787
788 return DoCallCommon<is_range, do_assignability_check>(
789 called_method, self, shadow_frame,
790 result, number_of_inputs, arg, vregC);
791}
792
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100793template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200794bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
795 Thread* self, JValue* result) {
796 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
797 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
798 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
799 if (!is_range) {
800 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
801 CHECK_LE(length, 5);
802 }
803 if (UNLIKELY(length < 0)) {
804 ThrowNegativeArraySizeException(length);
805 return false;
806 }
807 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700808 Class* array_class = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(),
809 self, false, do_access_check);
810 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200811 DCHECK(self->IsExceptionPending());
812 return false;
813 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700814 CHECK(array_class->IsArrayClass());
815 Class* component_class = array_class->GetComponentType();
816 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
817 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
818 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200819 ThrowRuntimeException("Bad filled array request for type %s",
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700820 PrettyDescriptor(component_class).c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200821 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000822 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800823 "Found type %s; filled-new-array not implemented for anything but 'int'",
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700824 PrettyDescriptor(component_class).c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200825 }
826 return false;
827 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700828 Object* new_array = Array::Alloc<true>(self, array_class, length,
829 array_class->GetComponentSizeShift(),
830 Runtime::Current()->GetHeap()->GetCurrentAllocator());
831 if (UNLIKELY(new_array == nullptr)) {
832 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200833 return false;
834 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700835 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
836 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200837 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100838 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200839 } else {
Ian Rogers29a26482014-05-02 15:27:29 -0700840 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100841 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100842 for (int32_t i = 0; i < length; ++i) {
843 size_t src_reg = is_range ? vregC + i : arg[i];
844 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700845 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
846 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +0100847 } else {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700848 new_array->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(
849 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200850 }
851 }
852
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700853 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200854 return true;
855}
856
Mathieu Chartier90443472015-07-16 20:32:27 -0700857// TODO fix thread analysis: should be SHARED_REQUIRES(Locks::mutator_lock_).
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100858template<typename T>
859static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count)
860 NO_THREAD_SAFETY_ANALYSIS {
861 Runtime* runtime = Runtime::Current();
862 for (int32_t i = 0; i < count; ++i) {
863 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
864 }
865}
866
867void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
Mathieu Chartier90443472015-07-16 20:32:27 -0700868 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100869 DCHECK(Runtime::Current()->IsActiveTransaction());
870 DCHECK(array != nullptr);
871 DCHECK_LE(count, array->GetLength());
872 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
873 switch (primitive_component_type) {
874 case Primitive::kPrimBoolean:
875 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
876 break;
877 case Primitive::kPrimByte:
878 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
879 break;
880 case Primitive::kPrimChar:
881 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
882 break;
883 case Primitive::kPrimShort:
884 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
885 break;
886 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100887 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
888 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700889 case Primitive::kPrimFloat:
890 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
891 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100892 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100893 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
894 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700895 case Primitive::kPrimDouble:
896 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
897 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100898 default:
899 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
900 << " in fill-array-data";
901 break;
902 }
903}
904
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200905// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200906#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700907 template SHARED_REQUIRES(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100908 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
909 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +0200910 const Instruction* inst, uint16_t inst_data, \
911 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200912EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
913EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
914EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
915EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
916#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200917
Igor Murashkin158f35c2015-06-10 15:55:30 -0700918// Explicit DoLambdaCall template function declarations.
919#define EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700920 template SHARED_REQUIRES(Locks::mutator_lock_) \
Igor Murashkin158f35c2015-06-10 15:55:30 -0700921 bool DoLambdaCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
922 ShadowFrame& shadow_frame, \
923 const Instruction* inst, \
924 uint16_t inst_data, \
925 JValue* result)
926EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(false, false);
927EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(false, true);
928EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(true, false);
929EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(true, true);
930#undef EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL
931
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200932// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100933#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700934 template SHARED_REQUIRES(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100935 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
936 const ShadowFrame& shadow_frame, \
937 Thread* self, JValue* result)
938#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
939 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
940 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
941 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
942 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
943EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
944EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
945#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200946#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
947
948} // namespace interpreter
949} // namespace art