blob: 18fb0d8518aadb93130f5f8c3bae51e8133f6e3d [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 Gampeb3025922015-09-01 14:45:00 -070024#include "stack.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070025#include "unstarted_runtime.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080026#include "verifier/method_verifier.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020027
28namespace art {
29namespace interpreter {
30
Igor Murashkin6918bf12015-09-27 19:19:06 -070031// All lambda closures have to be a consecutive pair of virtual registers.
32static constexpr size_t kLambdaVirtualRegisterWidth = 2;
33
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000034void ThrowNullPointerExceptionFromInterpreter() {
35 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -070036}
37
38template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
39bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
40 uint16_t inst_data) {
41 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
42 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010043 ArtField* f =
44 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
45 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -070046 if (UNLIKELY(f == nullptr)) {
47 CHECK(self->IsExceptionPending());
48 return false;
49 }
50 Object* obj;
51 if (is_static) {
52 obj = f->GetDeclaringClass();
53 } else {
54 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
55 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000056 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers54874942014-06-10 16:31:03 -070057 return false;
58 }
59 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +020060 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -070061 // Report this field access to instrumentation if needed.
62 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
63 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
64 Object* this_object = f->IsStatic() ? nullptr : obj;
65 instrumentation->FieldReadEvent(self, this_object, shadow_frame.GetMethod(),
66 shadow_frame.GetDexPC(), f);
67 }
68 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
69 switch (field_type) {
70 case Primitive::kPrimBoolean:
71 shadow_frame.SetVReg(vregA, f->GetBoolean(obj));
72 break;
73 case Primitive::kPrimByte:
74 shadow_frame.SetVReg(vregA, f->GetByte(obj));
75 break;
76 case Primitive::kPrimChar:
77 shadow_frame.SetVReg(vregA, f->GetChar(obj));
78 break;
79 case Primitive::kPrimShort:
80 shadow_frame.SetVReg(vregA, f->GetShort(obj));
81 break;
82 case Primitive::kPrimInt:
83 shadow_frame.SetVReg(vregA, f->GetInt(obj));
84 break;
85 case Primitive::kPrimLong:
86 shadow_frame.SetVRegLong(vregA, f->GetLong(obj));
87 break;
88 case Primitive::kPrimNot:
89 shadow_frame.SetVRegReference(vregA, f->GetObject(obj));
90 break;
91 default:
92 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -070093 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -070094 }
95 return true;
96}
97
98// Explicitly instantiate all DoFieldGet functions.
99#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
100 template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \
101 ShadowFrame& shadow_frame, \
102 const Instruction* inst, \
103 uint16_t inst_data)
104
105#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
106 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \
107 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true);
108
109// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700110EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
111EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
112EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
113EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
114EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
115EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
116EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700117
118// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700119EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
120EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
121EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
122EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
123EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
124EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
125EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700126
127#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
128#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
129
130// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
131// Returns true on success, otherwise throws an exception and returns false.
132template<Primitive::Type field_type>
133bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
134 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
135 if (UNLIKELY(obj == nullptr)) {
136 // We lost the reference to the field index so we cannot get a more
137 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000138 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700139 return false;
140 }
141 MemberOffset field_offset(inst->VRegC_22c());
142 // Report this field access to instrumentation if needed. Since we only have the offset of
143 // the field from the base of the object, we need to look for it first.
144 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
145 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
146 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
147 field_offset.Uint32Value());
148 DCHECK(f != nullptr);
149 DCHECK(!f->IsStatic());
150 instrumentation->FieldReadEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
151 shadow_frame.GetDexPC(), f);
152 }
153 // Note: iget-x-quick instructions are only for non-volatile fields.
154 const uint32_t vregA = inst->VRegA_22c(inst_data);
155 switch (field_type) {
156 case Primitive::kPrimInt:
157 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
158 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800159 case Primitive::kPrimBoolean:
160 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
161 break;
162 case Primitive::kPrimByte:
163 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
164 break;
165 case Primitive::kPrimChar:
166 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
167 break;
168 case Primitive::kPrimShort:
169 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
170 break;
Ian Rogers54874942014-06-10 16:31:03 -0700171 case Primitive::kPrimLong:
172 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
173 break;
174 case Primitive::kPrimNot:
175 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
176 break;
177 default:
178 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700179 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700180 }
181 return true;
182}
183
184// Explicitly instantiate all DoIGetQuick functions.
185#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
186 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
187 uint16_t inst_data)
188
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800189EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
190EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
191EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
192EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
193EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
194EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
195EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700196#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
197
198template<Primitive::Type field_type>
199static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700200 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700201 JValue field_value;
202 switch (field_type) {
203 case Primitive::kPrimBoolean:
204 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
205 break;
206 case Primitive::kPrimByte:
207 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
208 break;
209 case Primitive::kPrimChar:
210 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
211 break;
212 case Primitive::kPrimShort:
213 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
214 break;
215 case Primitive::kPrimInt:
216 field_value.SetI(shadow_frame.GetVReg(vreg));
217 break;
218 case Primitive::kPrimLong:
219 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
220 break;
221 case Primitive::kPrimNot:
222 field_value.SetL(shadow_frame.GetVRegReference(vreg));
223 break;
224 default:
225 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700226 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700227 }
228 return field_value;
229}
230
231template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
232 bool transaction_active>
233bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
234 uint16_t inst_data) {
235 bool do_assignability_check = do_access_check;
236 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
237 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100238 ArtField* f =
239 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
240 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -0700241 if (UNLIKELY(f == nullptr)) {
242 CHECK(self->IsExceptionPending());
243 return false;
244 }
245 Object* obj;
246 if (is_static) {
247 obj = f->GetDeclaringClass();
248 } else {
249 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
250 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000251 ThrowNullPointerExceptionForFieldAccess(f, false);
Ian Rogers54874942014-06-10 16:31:03 -0700252 return false;
253 }
254 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +0200255 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -0700256 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
257 // Report this field access to instrumentation if needed. Since we only have the offset of
258 // the field from the base of the object, we need to look for it first.
259 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
260 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
261 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
262 Object* this_object = f->IsStatic() ? nullptr : obj;
263 instrumentation->FieldWriteEvent(self, this_object, shadow_frame.GetMethod(),
264 shadow_frame.GetDexPC(), f, field_value);
265 }
266 switch (field_type) {
267 case Primitive::kPrimBoolean:
268 f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA));
269 break;
270 case Primitive::kPrimByte:
271 f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA));
272 break;
273 case Primitive::kPrimChar:
274 f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA));
275 break;
276 case Primitive::kPrimShort:
277 f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA));
278 break;
279 case Primitive::kPrimInt:
280 f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA));
281 break;
282 case Primitive::kPrimLong:
283 f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA));
284 break;
285 case Primitive::kPrimNot: {
286 Object* reg = shadow_frame.GetVRegReference(vregA);
287 if (do_assignability_check && reg != nullptr) {
288 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
289 // object in the destructor.
290 Class* field_class;
291 {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700292 StackHandleScope<2> hs(self);
Ian Rogers54874942014-06-10 16:31:03 -0700293 HandleWrapper<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
294 HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700295 field_class = f->GetType<true>();
Ian Rogers54874942014-06-10 16:31:03 -0700296 }
297 if (!reg->VerifierInstanceOf(field_class)) {
298 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700299 std::string temp1, temp2, temp3;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000300 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Ian Rogers54874942014-06-10 16:31:03 -0700301 "Put '%s' that is not instance of field '%s' in '%s'",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700302 reg->GetClass()->GetDescriptor(&temp1),
303 field_class->GetDescriptor(&temp2),
304 f->GetDeclaringClass()->GetDescriptor(&temp3));
Ian Rogers54874942014-06-10 16:31:03 -0700305 return false;
306 }
307 }
308 f->SetObj<transaction_active>(obj, reg);
309 break;
310 }
311 default:
312 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700313 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700314 }
315 return true;
316}
317
318// Explicitly instantiate all DoFieldPut functions.
319#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
320 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
321 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
322
323#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
324 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
325 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
326 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
327 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
328
329// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700330EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
331EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
332EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
333EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
334EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
335EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
336EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700337
338// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700339EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
340EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
341EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
342EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
343EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
344EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
345EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700346
347#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
348#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
349
350template<Primitive::Type field_type, bool transaction_active>
351bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
352 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
353 if (UNLIKELY(obj == nullptr)) {
354 // We lost the reference to the field index so we cannot get a more
355 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000356 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700357 return false;
358 }
359 MemberOffset field_offset(inst->VRegC_22c());
360 const uint32_t vregA = inst->VRegA_22c(inst_data);
361 // Report this field modification to instrumentation if needed. Since we only have the offset of
362 // the field from the base of the object, we need to look for it first.
363 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
364 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
365 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
366 field_offset.Uint32Value());
367 DCHECK(f != nullptr);
368 DCHECK(!f->IsStatic());
369 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
370 instrumentation->FieldWriteEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
371 shadow_frame.GetDexPC(), f, field_value);
372 }
373 // Note: iput-x-quick instructions are only for non-volatile fields.
374 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700375 case Primitive::kPrimBoolean:
376 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
377 break;
378 case Primitive::kPrimByte:
379 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
380 break;
381 case Primitive::kPrimChar:
382 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
383 break;
384 case Primitive::kPrimShort:
385 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
386 break;
Ian Rogers54874942014-06-10 16:31:03 -0700387 case Primitive::kPrimInt:
388 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
389 break;
390 case Primitive::kPrimLong:
391 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
392 break;
393 case Primitive::kPrimNot:
394 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
395 break;
396 default:
397 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700398 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700399 }
400 return true;
401}
402
403// Explicitly instantiate all DoIPutQuick functions.
404#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
405 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
406 const Instruction* inst, \
407 uint16_t inst_data)
408
409#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
410 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
411 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
412
Andreas Gampec8ccf682014-09-29 20:07:43 -0700413EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
414EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
415EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
416EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
417EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
418EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
419EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700420#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
421#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
422
Sebastien Hertz520633b2015-09-08 17:03:36 +0200423// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700424uint32_t FindNextInstructionFollowingException(
425 Thread* self, ShadowFrame& shadow_frame, uint32_t dex_pc,
426 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700427 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700428 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000429 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Sebastien Hertz520633b2015-09-08 17:03:36 +0200430 if (instrumentation != nullptr && instrumentation->HasExceptionCaughtListeners()
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000431 && self->IsExceptionThrownByCurrentMethod(exception.Get())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000432 instrumentation->ExceptionCaughtEvent(self, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200433 }
Ian Rogers54874942014-06-10 16:31:03 -0700434 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700435 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
436 hs.NewHandle(exception->GetClass()), dex_pc, &clear_exception);
Sebastien Hertz520633b2015-09-08 17:03:36 +0200437 if (found_dex_pc == DexFile::kDexNoIndex && instrumentation != nullptr) {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000438 // Exception is not caught by the current method. We will unwind to the
439 // caller. Notify any instrumentation listener.
Sebastien Hertz9f102032014-05-23 08:59:42 +0200440 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
Ian Rogers54874942014-06-10 16:31:03 -0700441 shadow_frame.GetMethod(), dex_pc);
442 } else {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000443 // Exception is caught in the current method. We will jump to the found_dex_pc.
Ian Rogers54874942014-06-10 16:31:03 -0700444 if (clear_exception) {
445 self->ClearException();
446 }
447 }
448 return found_dex_pc;
449}
450
Ian Rogerse94652f2014-12-02 11:13:19 -0800451void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
452 LOG(FATAL) << "Unexpected instruction: "
453 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
454 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700455}
456
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200457// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800458static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
459 size_t dest_reg, size_t src_reg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700460 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700461 // Uint required, so that sign extension does not make this wrong on 64b systems
462 uint32_t src_value = shadow_frame.GetVReg(src_reg);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800463 mirror::Object* o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700464
465 // If both register locations contains the same value, the register probably holds a reference.
466 // Note: As an optimization, non-moving collectors leave a stale reference value
467 // in the references array even after the original vreg was overwritten to a non-reference.
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700468 if (src_value == reinterpret_cast<uintptr_t>(o)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800469 new_shadow_frame->SetVRegReference(dest_reg, o);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200470 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800471 new_shadow_frame->SetVReg(dest_reg, src_value);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200472 }
473}
474
Sebastien Hertz45b15972015-04-03 16:07:05 +0200475void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700476 va_list args;
477 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200478 AbortTransactionV(self, fmt, args);
479 va_end(args);
480}
481
482void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
483 CHECK(Runtime::Current()->IsActiveTransaction());
484 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100485 std::string abort_msg;
486 StringAppendV(&abort_msg, fmt, args);
487 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200488 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700489}
490
Igor Murashkin158f35c2015-06-10 15:55:30 -0700491// Separate declaration is required solely for the attributes.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700492template <bool is_range,
493 bool do_assignability_check,
494 size_t kVarArgMax>
495 SHARED_REQUIRES(Locks::mutator_lock_)
Igor Murashkin158f35c2015-06-10 15:55:30 -0700496static inline bool DoCallCommon(ArtMethod* called_method,
497 Thread* self,
498 ShadowFrame& shadow_frame,
499 JValue* result,
500 uint16_t number_of_inputs,
Igor Murashkin6918bf12015-09-27 19:19:06 -0700501 uint32_t (&arg)[kVarArgMax],
Igor Murashkin158f35c2015-06-10 15:55:30 -0700502 uint32_t vregC) ALWAYS_INLINE;
503
Mathieu Chartier90443472015-07-16 20:32:27 -0700504SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +0000505static inline bool NeedsInterpreter(Thread* self, ShadowFrame* new_shadow_frame) ALWAYS_INLINE;
506
507static inline bool NeedsInterpreter(Thread* self, ShadowFrame* new_shadow_frame) {
508 ArtMethod* target = new_shadow_frame->GetMethod();
509 if (UNLIKELY(target->IsNative() || target->IsProxyMethod())) {
510 return false;
511 }
512 Runtime* runtime = Runtime::Current();
513 ClassLinker* class_linker = runtime->GetClassLinker();
514 return runtime->GetInstrumentation()->IsForcedInterpretOnly() ||
515 // Doing this check avoids doing compiled/interpreter transitions.
516 class_linker->IsQuickToInterpreterBridge(target->GetEntryPointFromQuickCompiledCode()) ||
517 // Force the use of interpreter when it is required by the debugger.
518 Dbg::IsForcedInterpreterNeededForCalling(self, target);
519}
520
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700521static void ArtInterpreterToCompiledCodeBridge(Thread* self,
522 const DexFile::CodeItem* code_item,
523 ShadowFrame* shadow_frame,
524 JValue* result)
525 SHARED_REQUIRES(Locks::mutator_lock_) {
526 ArtMethod* method = shadow_frame->GetMethod();
527 // Ensure static methods are initialized.
528 if (method->IsStatic()) {
529 mirror::Class* declaringClass = method->GetDeclaringClass();
530 if (UNLIKELY(!declaringClass->IsInitialized())) {
531 self->PushShadowFrame(shadow_frame);
532 StackHandleScope<1> hs(self);
533 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
534 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
535 true))) {
536 self->PopShadowFrame();
537 DCHECK(self->IsExceptionPending());
538 return;
539 }
540 self->PopShadowFrame();
541 CHECK(h_class->IsInitializing());
542 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
543 method = shadow_frame->GetMethod();
544 }
545 }
546 uint16_t arg_offset = (code_item == nullptr)
547 ? 0
548 : code_item->registers_size_ - code_item->ins_size_;
549 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
550 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
551 result, method->GetInterfaceMethodIfProxy(sizeof(void*))->GetShorty());
552}
553
Igor Murashkin6918bf12015-09-27 19:19:06 -0700554template <bool is_range,
555 bool do_assignability_check,
556 size_t kVarArgMax>
Igor Murashkin158f35c2015-06-10 15:55:30 -0700557static inline bool DoCallCommon(ArtMethod* called_method,
558 Thread* self,
559 ShadowFrame& shadow_frame,
560 JValue* result,
561 uint16_t number_of_inputs,
Igor Murashkin6918bf12015-09-27 19:19:06 -0700562 uint32_t (&arg)[kVarArgMax],
Igor Murashkin158f35c2015-06-10 15:55:30 -0700563 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800564 bool string_init = false;
565 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700566 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
567 && called_method->IsConstructor())) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800568 ScopedObjectAccessUnchecked soa(self);
569 jmethodID mid = soa.EncodeMethod(called_method);
570 called_method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
571 string_init = true;
572 }
573
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200574 // Compute method information.
Ian Rogerse94652f2014-12-02 11:13:19 -0800575 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
Igor Murashkin158f35c2015-06-10 15:55:30 -0700576
577 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200578 uint16_t num_regs;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700579 if (LIKELY(code_item != nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200580 num_regs = code_item->registers_size_;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700581 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200582 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800583 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Igor Murashkin158f35c2015-06-10 15:55:30 -0700584 num_regs = number_of_inputs;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200585 }
586
Igor Murashkin158f35c2015-06-10 15:55:30 -0700587 // Hack for String init:
588 //
589 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
590 // invoke-x StringFactory(a, b, c, ...)
591 // by effectively dropping the first virtual register from the invoke.
592 //
593 // (at this point the ArtMethod has already been replaced,
594 // so we just need to fix-up the arguments)
595 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -0700596 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700597 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 -0700598
Igor Murashkin158f35c2015-06-10 15:55:30 -0700599 // The new StringFactory call is static and has one fewer argument.
Igor Murashkina06b49b2015-06-25 15:18:12 -0700600 if (code_item == nullptr) {
601 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
602 num_regs--;
603 } // 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 -0700604 number_of_inputs--;
605
606 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -0700607 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700608 arg[i - 1] = arg[i];
609 }
Igor Murashkin6918bf12015-09-27 19:19:06 -0700610 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700611
612 // Rewrite the non-var-arg case
613 vregC++; // Skips the 0th vreg in the range ("this").
614 }
615
616 // Parameter registers go at the end of the shadow frame.
617 DCHECK_GE(num_regs, number_of_inputs);
618 size_t first_dest_reg = num_regs - number_of_inputs;
619 DCHECK_NE(first_dest_reg, (size_t)-1);
620
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200621 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700622 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -0700623 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700624 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700625 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200626
Igor Murashkin158f35c2015-06-10 15:55:30 -0700627 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -0700628 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700629 // Slow path.
630 // We might need to do class loading, which incurs a thread state change to kNative. So
631 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700632 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +0200633 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700634 self->EndAssertNoThreadSuspension(old_cause);
635
636 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200637 // to get the exact type of each reference argument.
Ian Rogerse94652f2014-12-02 11:13:19 -0800638 const DexFile::TypeList* params = new_shadow_frame->GetMethod()->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700639 uint32_t shorty_len = 0;
Ian Rogerse94652f2014-12-02 11:13:19 -0800640 const char* shorty = new_shadow_frame->GetMethod()->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200641
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100642 // Handle receiver apart since it's not part of the shorty.
643 size_t dest_reg = first_dest_reg;
644 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700645
Ian Rogerse94652f2014-12-02 11:13:19 -0800646 if (!new_shadow_frame->GetMethod()->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700647 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100648 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
649 ++dest_reg;
650 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -0700651 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100652 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700653
654 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800655 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -0700656 // Skip the 0th 'shorty' type since it represents the return type.
657 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200658 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
659 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700660 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200661 case 'L': {
662 Object* o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700663 if (do_assignability_check && o != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100664 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Ian Rogersa0485602014-12-02 15:48:04 -0800665 Class* arg_type =
666 new_shadow_frame->GetMethod()->GetClassFromTypeIndex(
Vladimir Marko05792b92015-08-03 11:56:49 +0100667 params->GetTypeItem(shorty_pos).type_idx_, true /* resolve */, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700668 if (arg_type == nullptr) {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200669 CHECK(self->IsExceptionPending());
670 return false;
671 }
672 if (!o->VerifierInstanceOf(arg_type)) {
673 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700674 std::string temp1, temp2;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000675 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200676 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -0800677 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700678 o->GetClass()->GetDescriptor(&temp1),
679 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200680 return false;
681 }
Jeff Haoa3faaf42013-09-03 19:07:00 -0700682 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200683 new_shadow_frame->SetVRegReference(dest_reg, o);
684 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700685 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700686 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200687 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700688 uint64_t wide_value =
689 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
690 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200691 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700692 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200693 ++dest_reg;
694 ++arg_offset;
695 break;
696 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700697 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200698 default:
699 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
700 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200701 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200702 }
703 } else {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700704 size_t arg_index = 0;
705
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200706 // Fast path: no extra checks.
707 if (is_range) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700708 // TODO: Implement the range version of invoke-lambda
709 uint16_t first_src_reg = vregC;
710
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200711 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs;
712 ++dest_reg, ++src_reg) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800713 AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200714 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200715 } else {
Igor Murashkin6918bf12015-09-27 19:19:06 -0700716 DCHECK_LE(number_of_inputs, arraysize(arg));
Igor Murashkin158f35c2015-06-10 15:55:30 -0700717
718 for (; arg_index < number_of_inputs; ++arg_index) {
719 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, arg[arg_index]);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200720 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200721 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700722 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200723 }
724
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200725 // Do the call now.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200726 if (LIKELY(Runtime::Current()->IsStarted())) {
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +0000727 if (NeedsInterpreter(self, new_shadow_frame)) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700728 ArtInterpreterToInterpreterBridge(self, code_item, new_shadow_frame, result);
Nicolas Geoffray7070ccd2015-07-08 09:41:54 +0000729 } else {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700730 ArtInterpreterToCompiledCodeBridge(self, code_item, new_shadow_frame, result);
Nicolas Geoffray7070ccd2015-07-08 09:41:54 +0000731 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200732 } else {
Andreas Gampe799681b2015-05-15 19:24:12 -0700733 UnstartedRuntime::Invoke(self, code_item, new_shadow_frame, result, first_dest_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200734 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800735
736 if (string_init && !self->IsExceptionPending()) {
737 // Set the new string result of the StringFactory.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700738 shadow_frame.SetVRegReference(string_init_vreg_this, result->GetL());
Jeff Hao848f70a2014-01-15 13:49:50 -0800739 // Overwrite all potential copies of the original result of the new-instance of string with the
740 // new result of the StringFactory. Use the verifier to find this set of registers.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700741 ArtMethod* method = shadow_frame.GetMethod();
Jeff Hao848f70a2014-01-15 13:49:50 -0800742 MethodReference method_ref = method->ToMethodReference();
Jeff Hao4686c522015-09-18 14:44:32 -0700743 SafeMap<uint32_t, std::set<uint32_t>>* string_init_map_ptr = nullptr;
Jeff Hao848f70a2014-01-15 13:49:50 -0800744 MethodRefToStringInitRegMap& method_to_string_init_map = Runtime::Current()->GetStringInitMap();
Jeff Haoa2c38642015-09-17 17:29:01 -0700745 {
746 MutexLock mu(self, *Locks::interpreter_string_init_map_lock_);
Jeff Hao4686c522015-09-18 14:44:32 -0700747 auto it = method_to_string_init_map.find(method_ref);
748 if (it != method_to_string_init_map.end()) {
749 string_init_map_ptr = &it->second;
Jeff Haoa2c38642015-09-17 17:29:01 -0700750 }
Jeff Hao4686c522015-09-18 14:44:32 -0700751 }
752 if (string_init_map_ptr == nullptr) {
753 SafeMap<uint32_t, std::set<uint32_t>> string_init_map =
754 verifier::MethodVerifier::FindStringInitMap(method);
755 MutexLock mu(self, *Locks::interpreter_string_init_map_lock_);
Vladimir Marko78568352015-09-21 12:00:16 +0100756 auto it = method_to_string_init_map.lower_bound(method_ref);
757 if (it == method_to_string_init_map.end() ||
758 method_to_string_init_map.key_comp()(method_ref, it->first)) {
759 it = method_to_string_init_map.PutBefore(it, method_ref, std::move(string_init_map));
760 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800761 string_init_map_ptr = &it->second;
762 }
763 if (string_init_map_ptr->size() != 0) {
764 uint32_t dex_pc = shadow_frame.GetDexPC();
765 auto map_it = string_init_map_ptr->find(dex_pc);
766 if (map_it != string_init_map_ptr->end()) {
767 const std::set<uint32_t>& reg_set = map_it->second;
768 for (auto set_it = reg_set.begin(); set_it != reg_set.end(); ++set_it) {
769 shadow_frame.SetVRegReference(*set_it, result->GetL());
770 }
771 }
772 }
773 }
774
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200775 return !self->IsExceptionPending();
776}
777
Igor Murashkin158f35c2015-06-10 15:55:30 -0700778template<bool is_range, bool do_assignability_check>
779bool DoLambdaCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100780 const Instruction* inst, uint16_t inst_data ATTRIBUTE_UNUSED, JValue* result) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700781 const uint4_t num_additional_registers = inst->VRegB_25x();
782 // Argument word count.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700783 const uint16_t number_of_inputs = num_additional_registers + kLambdaVirtualRegisterWidth;
784 // The lambda closure register is always present and is not encoded in the count.
785 // Furthermore, the lambda closure register is always wide, so it counts as 2 inputs.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700786
787 // TODO: find a cleaner way to separate non-range and range information without duplicating
788 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700789 uint32_t arg[Instruction::kMaxVarArgRegs25x]; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700790 uint32_t vregC = 0; // only used in invoke-XXX-range.
791 if (is_range) {
792 vregC = inst->VRegC_3rc();
793 } else {
794 // TODO(iam): See if it's possible to remove inst_data dependency from 35x to avoid this path
Igor Murashkin158f35c2015-06-10 15:55:30 -0700795 inst->GetAllArgs25x(arg);
796 }
797
798 // TODO: if there's an assignability check, throw instead?
799 DCHECK(called_method->IsStatic());
800
801 return DoCallCommon<is_range, do_assignability_check>(
802 called_method, self, shadow_frame,
803 result, number_of_inputs, arg, vregC);
804}
805
806template<bool is_range, bool do_assignability_check>
807bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
808 const Instruction* inst, uint16_t inst_data, JValue* result) {
809 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100810 const uint16_t number_of_inputs =
811 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700812
813 // TODO: find a cleaner way to separate non-range and range information without duplicating
814 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700815 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700816 uint32_t vregC = 0;
817 if (is_range) {
818 vregC = inst->VRegC_3rc();
819 } else {
820 vregC = inst->VRegC_35c();
821 inst->GetVarArgs(arg, inst_data);
822 }
823
824 return DoCallCommon<is_range, do_assignability_check>(
825 called_method, self, shadow_frame,
826 result, number_of_inputs, arg, vregC);
827}
828
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100829template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200830bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
831 Thread* self, JValue* result) {
832 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
833 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
834 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
835 if (!is_range) {
836 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
837 CHECK_LE(length, 5);
838 }
839 if (UNLIKELY(length < 0)) {
840 ThrowNegativeArraySizeException(length);
841 return false;
842 }
843 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700844 Class* array_class = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(),
845 self, false, do_access_check);
846 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200847 DCHECK(self->IsExceptionPending());
848 return false;
849 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700850 CHECK(array_class->IsArrayClass());
851 Class* component_class = array_class->GetComponentType();
852 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
853 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
854 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200855 ThrowRuntimeException("Bad filled array request for type %s",
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700856 PrettyDescriptor(component_class).c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200857 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000858 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800859 "Found type %s; filled-new-array not implemented for anything but 'int'",
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700860 PrettyDescriptor(component_class).c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200861 }
862 return false;
863 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700864 Object* new_array = Array::Alloc<true>(self, array_class, length,
865 array_class->GetComponentSizeShift(),
866 Runtime::Current()->GetHeap()->GetCurrentAllocator());
867 if (UNLIKELY(new_array == nullptr)) {
868 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200869 return false;
870 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700871 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
872 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200873 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100874 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200875 } else {
Ian Rogers29a26482014-05-02 15:27:29 -0700876 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100877 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100878 for (int32_t i = 0; i < length; ++i) {
879 size_t src_reg = is_range ? vregC + i : arg[i];
880 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700881 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
882 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +0100883 } else {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700884 new_array->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(
885 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200886 }
887 }
888
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700889 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200890 return true;
891}
892
Mathieu Chartier90443472015-07-16 20:32:27 -0700893// TODO fix thread analysis: should be SHARED_REQUIRES(Locks::mutator_lock_).
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100894template<typename T>
895static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count)
896 NO_THREAD_SAFETY_ANALYSIS {
897 Runtime* runtime = Runtime::Current();
898 for (int32_t i = 0; i < count; ++i) {
899 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
900 }
901}
902
903void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
Mathieu Chartier90443472015-07-16 20:32:27 -0700904 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100905 DCHECK(Runtime::Current()->IsActiveTransaction());
906 DCHECK(array != nullptr);
907 DCHECK_LE(count, array->GetLength());
908 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
909 switch (primitive_component_type) {
910 case Primitive::kPrimBoolean:
911 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
912 break;
913 case Primitive::kPrimByte:
914 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
915 break;
916 case Primitive::kPrimChar:
917 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
918 break;
919 case Primitive::kPrimShort:
920 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
921 break;
922 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100923 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
924 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700925 case Primitive::kPrimFloat:
926 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
927 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100928 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100929 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
930 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700931 case Primitive::kPrimDouble:
932 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
933 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100934 default:
935 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
936 << " in fill-array-data";
937 break;
938 }
939}
940
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200941// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200942#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700943 template SHARED_REQUIRES(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100944 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
945 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +0200946 const Instruction* inst, uint16_t inst_data, \
947 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200948EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
949EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
950EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
951EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
952#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200953
Igor Murashkin158f35c2015-06-10 15:55:30 -0700954// Explicit DoLambdaCall template function declarations.
955#define EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700956 template SHARED_REQUIRES(Locks::mutator_lock_) \
Igor Murashkin158f35c2015-06-10 15:55:30 -0700957 bool DoLambdaCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
958 ShadowFrame& shadow_frame, \
959 const Instruction* inst, \
960 uint16_t inst_data, \
961 JValue* result)
962EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(false, false);
963EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(false, true);
964EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(true, false);
965EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(true, true);
966#undef EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL
967
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200968// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100969#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700970 template SHARED_REQUIRES(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100971 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
972 const ShadowFrame& shadow_frame, \
973 Thread* self, JValue* result)
974#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
975 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
976 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
977 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
978 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
979EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
980EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
981#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200982#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
983
984} // namespace interpreter
985} // namespace art