blob: 6c9cc7017be50a51fd7c42eeea27b23b9c4753b3 [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
Siva Chandra05d24152016-01-05 17:43:17 -0800521void ArtInterpreterToCompiledCodeBridge(Thread* self,
522 const DexFile::CodeItem* code_item,
523 ShadowFrame* shadow_frame,
524 JValue* result)
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700525 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)
David Brazdil65902e82016-01-15 09:35:13 +0000595 //
596 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
597 // to handle the compiler optimization of replacing `this` with null without
598 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700599 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -0700600 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700601 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 -0700602
Igor Murashkin158f35c2015-06-10 15:55:30 -0700603 // The new StringFactory call is static and has one fewer argument.
Igor Murashkina06b49b2015-06-25 15:18:12 -0700604 if (code_item == nullptr) {
605 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
606 num_regs--;
607 } // 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 -0700608 number_of_inputs--;
609
610 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -0700611 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700612 arg[i - 1] = arg[i];
613 }
Igor Murashkin6918bf12015-09-27 19:19:06 -0700614 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700615
616 // Rewrite the non-var-arg case
617 vregC++; // Skips the 0th vreg in the range ("this").
618 }
619
620 // Parameter registers go at the end of the shadow frame.
621 DCHECK_GE(num_regs, number_of_inputs);
622 size_t first_dest_reg = num_regs - number_of_inputs;
623 DCHECK_NE(first_dest_reg, (size_t)-1);
624
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200625 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700626 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -0700627 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700628 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700629 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200630
Igor Murashkin158f35c2015-06-10 15:55:30 -0700631 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -0700632 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700633 // Slow path.
634 // We might need to do class loading, which incurs a thread state change to kNative. So
635 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700636 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +0200637 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700638 self->EndAssertNoThreadSuspension(old_cause);
639
640 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200641 // to get the exact type of each reference argument.
Ian Rogerse94652f2014-12-02 11:13:19 -0800642 const DexFile::TypeList* params = new_shadow_frame->GetMethod()->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700643 uint32_t shorty_len = 0;
Ian Rogerse94652f2014-12-02 11:13:19 -0800644 const char* shorty = new_shadow_frame->GetMethod()->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200645
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100646 // Handle receiver apart since it's not part of the shorty.
647 size_t dest_reg = first_dest_reg;
648 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700649
Ian Rogerse94652f2014-12-02 11:13:19 -0800650 if (!new_shadow_frame->GetMethod()->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700651 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100652 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
653 ++dest_reg;
654 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -0700655 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100656 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700657
658 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800659 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -0700660 // Skip the 0th 'shorty' type since it represents the return type.
661 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200662 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
663 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700664 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200665 case 'L': {
666 Object* o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700667 if (do_assignability_check && o != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100668 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Ian Rogersa0485602014-12-02 15:48:04 -0800669 Class* arg_type =
670 new_shadow_frame->GetMethod()->GetClassFromTypeIndex(
Vladimir Marko05792b92015-08-03 11:56:49 +0100671 params->GetTypeItem(shorty_pos).type_idx_, true /* resolve */, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700672 if (arg_type == nullptr) {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200673 CHECK(self->IsExceptionPending());
674 return false;
675 }
676 if (!o->VerifierInstanceOf(arg_type)) {
677 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700678 std::string temp1, temp2;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000679 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200680 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -0800681 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700682 o->GetClass()->GetDescriptor(&temp1),
683 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200684 return false;
685 }
Jeff Haoa3faaf42013-09-03 19:07:00 -0700686 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200687 new_shadow_frame->SetVRegReference(dest_reg, o);
688 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700689 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700690 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200691 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700692 uint64_t wide_value =
693 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
694 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200695 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700696 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200697 ++dest_reg;
698 ++arg_offset;
699 break;
700 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700701 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200702 default:
703 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
704 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200705 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200706 }
707 } else {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700708 size_t arg_index = 0;
709
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200710 // Fast path: no extra checks.
711 if (is_range) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700712 // TODO: Implement the range version of invoke-lambda
713 uint16_t first_src_reg = vregC;
714
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200715 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs;
716 ++dest_reg, ++src_reg) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800717 AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200718 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200719 } else {
Igor Murashkin6918bf12015-09-27 19:19:06 -0700720 DCHECK_LE(number_of_inputs, arraysize(arg));
Igor Murashkin158f35c2015-06-10 15:55:30 -0700721
722 for (; arg_index < number_of_inputs; ++arg_index) {
723 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, arg[arg_index]);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200724 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200725 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700726 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200727 }
728
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200729 // Do the call now.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200730 if (LIKELY(Runtime::Current()->IsStarted())) {
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +0000731 if (NeedsInterpreter(self, new_shadow_frame)) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700732 ArtInterpreterToInterpreterBridge(self, code_item, new_shadow_frame, result);
Nicolas Geoffray7070ccd2015-07-08 09:41:54 +0000733 } else {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700734 ArtInterpreterToCompiledCodeBridge(self, code_item, new_shadow_frame, result);
Nicolas Geoffray7070ccd2015-07-08 09:41:54 +0000735 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200736 } else {
Andreas Gampe799681b2015-05-15 19:24:12 -0700737 UnstartedRuntime::Invoke(self, code_item, new_shadow_frame, result, first_dest_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200738 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800739
740 if (string_init && !self->IsExceptionPending()) {
741 // Set the new string result of the StringFactory.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700742 shadow_frame.SetVRegReference(string_init_vreg_this, result->GetL());
Jeff Hao848f70a2014-01-15 13:49:50 -0800743 // Overwrite all potential copies of the original result of the new-instance of string with the
744 // new result of the StringFactory. Use the verifier to find this set of registers.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700745 ArtMethod* method = shadow_frame.GetMethod();
Jeff Hao848f70a2014-01-15 13:49:50 -0800746 MethodReference method_ref = method->ToMethodReference();
Jeff Hao4686c522015-09-18 14:44:32 -0700747 SafeMap<uint32_t, std::set<uint32_t>>* string_init_map_ptr = nullptr;
Jeff Hao848f70a2014-01-15 13:49:50 -0800748 MethodRefToStringInitRegMap& method_to_string_init_map = Runtime::Current()->GetStringInitMap();
Jeff Haoa2c38642015-09-17 17:29:01 -0700749 {
750 MutexLock mu(self, *Locks::interpreter_string_init_map_lock_);
Jeff Hao4686c522015-09-18 14:44:32 -0700751 auto it = method_to_string_init_map.find(method_ref);
752 if (it != method_to_string_init_map.end()) {
753 string_init_map_ptr = &it->second;
Jeff Haoa2c38642015-09-17 17:29:01 -0700754 }
Jeff Hao4686c522015-09-18 14:44:32 -0700755 }
756 if (string_init_map_ptr == nullptr) {
757 SafeMap<uint32_t, std::set<uint32_t>> string_init_map =
758 verifier::MethodVerifier::FindStringInitMap(method);
759 MutexLock mu(self, *Locks::interpreter_string_init_map_lock_);
Vladimir Marko78568352015-09-21 12:00:16 +0100760 auto it = method_to_string_init_map.lower_bound(method_ref);
761 if (it == method_to_string_init_map.end() ||
762 method_to_string_init_map.key_comp()(method_ref, it->first)) {
763 it = method_to_string_init_map.PutBefore(it, method_ref, std::move(string_init_map));
764 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800765 string_init_map_ptr = &it->second;
766 }
767 if (string_init_map_ptr->size() != 0) {
768 uint32_t dex_pc = shadow_frame.GetDexPC();
769 auto map_it = string_init_map_ptr->find(dex_pc);
770 if (map_it != string_init_map_ptr->end()) {
771 const std::set<uint32_t>& reg_set = map_it->second;
772 for (auto set_it = reg_set.begin(); set_it != reg_set.end(); ++set_it) {
773 shadow_frame.SetVRegReference(*set_it, result->GetL());
774 }
775 }
776 }
777 }
778
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200779 return !self->IsExceptionPending();
780}
781
Igor Murashkin158f35c2015-06-10 15:55:30 -0700782template<bool is_range, bool do_assignability_check>
783bool DoLambdaCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100784 const Instruction* inst, uint16_t inst_data ATTRIBUTE_UNUSED, JValue* result) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700785 const uint4_t num_additional_registers = inst->VRegB_25x();
786 // Argument word count.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700787 const uint16_t number_of_inputs = num_additional_registers + kLambdaVirtualRegisterWidth;
788 // The lambda closure register is always present and is not encoded in the count.
789 // Furthermore, the lambda closure register is always wide, so it counts as 2 inputs.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700790
791 // TODO: find a cleaner way to separate non-range and range information without duplicating
792 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700793 uint32_t arg[Instruction::kMaxVarArgRegs25x]; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700794 uint32_t vregC = 0; // only used in invoke-XXX-range.
795 if (is_range) {
796 vregC = inst->VRegC_3rc();
797 } else {
798 // 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 -0700799 inst->GetAllArgs25x(arg);
800 }
801
802 // TODO: if there's an assignability check, throw instead?
803 DCHECK(called_method->IsStatic());
804
805 return DoCallCommon<is_range, do_assignability_check>(
806 called_method, self, shadow_frame,
807 result, number_of_inputs, arg, vregC);
808}
809
810template<bool is_range, bool do_assignability_check>
811bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
812 const Instruction* inst, uint16_t inst_data, JValue* result) {
813 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100814 const uint16_t number_of_inputs =
815 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700816
817 // TODO: find a cleaner way to separate non-range and range information without duplicating
818 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700819 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700820 uint32_t vregC = 0;
821 if (is_range) {
822 vregC = inst->VRegC_3rc();
823 } else {
824 vregC = inst->VRegC_35c();
825 inst->GetVarArgs(arg, inst_data);
826 }
827
828 return DoCallCommon<is_range, do_assignability_check>(
829 called_method, self, shadow_frame,
830 result, number_of_inputs, arg, vregC);
831}
832
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100833template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200834bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
835 Thread* self, JValue* result) {
836 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
837 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
838 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
839 if (!is_range) {
840 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
841 CHECK_LE(length, 5);
842 }
843 if (UNLIKELY(length < 0)) {
844 ThrowNegativeArraySizeException(length);
845 return false;
846 }
847 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700848 Class* array_class = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(),
849 self, false, do_access_check);
850 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200851 DCHECK(self->IsExceptionPending());
852 return false;
853 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700854 CHECK(array_class->IsArrayClass());
855 Class* component_class = array_class->GetComponentType();
856 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
857 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
858 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200859 ThrowRuntimeException("Bad filled array request for type %s",
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700860 PrettyDescriptor(component_class).c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200861 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000862 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800863 "Found type %s; filled-new-array not implemented for anything but 'int'",
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700864 PrettyDescriptor(component_class).c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200865 }
866 return false;
867 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700868 Object* new_array = Array::Alloc<true>(self, array_class, length,
869 array_class->GetComponentSizeShift(),
870 Runtime::Current()->GetHeap()->GetCurrentAllocator());
871 if (UNLIKELY(new_array == nullptr)) {
872 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200873 return false;
874 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700875 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
876 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200877 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100878 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200879 } else {
Ian Rogers29a26482014-05-02 15:27:29 -0700880 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100881 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100882 for (int32_t i = 0; i < length; ++i) {
883 size_t src_reg = is_range ? vregC + i : arg[i];
884 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700885 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
886 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +0100887 } else {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700888 new_array->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(
889 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200890 }
891 }
892
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700893 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200894 return true;
895}
896
Mathieu Chartier90443472015-07-16 20:32:27 -0700897// TODO fix thread analysis: should be SHARED_REQUIRES(Locks::mutator_lock_).
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100898template<typename T>
899static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count)
900 NO_THREAD_SAFETY_ANALYSIS {
901 Runtime* runtime = Runtime::Current();
902 for (int32_t i = 0; i < count; ++i) {
903 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
904 }
905}
906
907void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
Mathieu Chartier90443472015-07-16 20:32:27 -0700908 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100909 DCHECK(Runtime::Current()->IsActiveTransaction());
910 DCHECK(array != nullptr);
911 DCHECK_LE(count, array->GetLength());
912 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
913 switch (primitive_component_type) {
914 case Primitive::kPrimBoolean:
915 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
916 break;
917 case Primitive::kPrimByte:
918 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
919 break;
920 case Primitive::kPrimChar:
921 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
922 break;
923 case Primitive::kPrimShort:
924 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
925 break;
926 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100927 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
928 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700929 case Primitive::kPrimFloat:
930 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
931 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100932 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100933 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
934 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700935 case Primitive::kPrimDouble:
936 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
937 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100938 default:
939 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
940 << " in fill-array-data";
941 break;
942 }
943}
944
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200945// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200946#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700947 template SHARED_REQUIRES(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100948 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
949 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +0200950 const Instruction* inst, uint16_t inst_data, \
951 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200952EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
953EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
954EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
955EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
956#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200957
Igor Murashkin158f35c2015-06-10 15:55:30 -0700958// Explicit DoLambdaCall template function declarations.
959#define EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700960 template SHARED_REQUIRES(Locks::mutator_lock_) \
Igor Murashkin158f35c2015-06-10 15:55:30 -0700961 bool DoLambdaCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
962 ShadowFrame& shadow_frame, \
963 const Instruction* inst, \
964 uint16_t inst_data, \
965 JValue* result)
966EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(false, false);
967EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(false, true);
968EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(true, false);
969EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(true, true);
970#undef EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL
971
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200972// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100973#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700974 template SHARED_REQUIRES(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100975 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
976 const ShadowFrame& shadow_frame, \
977 Thread* self, JValue* result)
978#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
979 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
980 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
981 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
982 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
983EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
984EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
985#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200986#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
987
988} // namespace interpreter
989} // namespace art