blob: 09d860140f279bfa8070f390be4b10ac1ef61366 [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
Tamas Berghammer3a98aae2016-02-08 20:21:54 +0000504SHARED_REQUIRES(Locks::mutator_lock_)
505static 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
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800640 // ArtMethod here is needed to check type information of the call site against the callee.
641 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
642 //
643 // As a special case for proxy methods, which are not dex-backed,
644 // we have to retrieve type information from the proxy's method
645 // interface method instead (which is dex backed since proxies are never interfaces).
646 ArtMethod* method = new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(sizeof(void*));
647
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700648 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200649 // to get the exact type of each reference argument.
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800650 const DexFile::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700651 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800652 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200653
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100654 // Handle receiver apart since it's not part of the shorty.
655 size_t dest_reg = first_dest_reg;
656 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700657
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800658 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700659 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100660 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
661 ++dest_reg;
662 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -0700663 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100664 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700665
666 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800667 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -0700668 // Skip the 0th 'shorty' type since it represents the return type.
669 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200670 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
671 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700672 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200673 case 'L': {
674 Object* o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700675 if (do_assignability_check && o != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100676 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Ian Rogersa0485602014-12-02 15:48:04 -0800677 Class* arg_type =
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800678 method->GetClassFromTypeIndex(
Vladimir Marko05792b92015-08-03 11:56:49 +0100679 params->GetTypeItem(shorty_pos).type_idx_, true /* resolve */, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700680 if (arg_type == nullptr) {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200681 CHECK(self->IsExceptionPending());
682 return false;
683 }
684 if (!o->VerifierInstanceOf(arg_type)) {
685 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700686 std::string temp1, temp2;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000687 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200688 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -0800689 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700690 o->GetClass()->GetDescriptor(&temp1),
691 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200692 return false;
693 }
Jeff Haoa3faaf42013-09-03 19:07:00 -0700694 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200695 new_shadow_frame->SetVRegReference(dest_reg, o);
696 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700697 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700698 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200699 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700700 uint64_t wide_value =
701 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
702 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200703 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700704 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200705 ++dest_reg;
706 ++arg_offset;
707 break;
708 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700709 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200710 default:
711 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
712 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200713 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200714 }
715 } else {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700716 size_t arg_index = 0;
717
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200718 // Fast path: no extra checks.
719 if (is_range) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700720 // TODO: Implement the range version of invoke-lambda
721 uint16_t first_src_reg = vregC;
722
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200723 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs;
724 ++dest_reg, ++src_reg) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800725 AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200726 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200727 } else {
Igor Murashkin6918bf12015-09-27 19:19:06 -0700728 DCHECK_LE(number_of_inputs, arraysize(arg));
Igor Murashkin158f35c2015-06-10 15:55:30 -0700729
730 for (; arg_index < number_of_inputs; ++arg_index) {
731 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, arg[arg_index]);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200732 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200733 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700734 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200735 }
736
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200737 // Do the call now.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200738 if (LIKELY(Runtime::Current()->IsStarted())) {
Tamas Berghammer3a98aae2016-02-08 20:21:54 +0000739 if (NeedsInterpreter(self, new_shadow_frame)) {
Tamas Berghammerc94a61f2016-02-05 18:09:08 +0000740 ArtInterpreterToInterpreterBridge(self, code_item, new_shadow_frame, result);
Tamas Berghammer3a98aae2016-02-08 20:21:54 +0000741 } else {
742 ArtInterpreterToCompiledCodeBridge(self, code_item, new_shadow_frame, result);
Nicolas Geoffray7070ccd2015-07-08 09:41:54 +0000743 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200744 } else {
Andreas Gampe799681b2015-05-15 19:24:12 -0700745 UnstartedRuntime::Invoke(self, code_item, new_shadow_frame, result, first_dest_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200746 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800747
748 if (string_init && !self->IsExceptionPending()) {
749 // Set the new string result of the StringFactory.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700750 shadow_frame.SetVRegReference(string_init_vreg_this, result->GetL());
Jeff Hao848f70a2014-01-15 13:49:50 -0800751 // Overwrite all potential copies of the original result of the new-instance of string with the
752 // new result of the StringFactory. Use the verifier to find this set of registers.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700753 ArtMethod* method = shadow_frame.GetMethod();
Jeff Hao848f70a2014-01-15 13:49:50 -0800754 MethodReference method_ref = method->ToMethodReference();
Jeff Hao4686c522015-09-18 14:44:32 -0700755 SafeMap<uint32_t, std::set<uint32_t>>* string_init_map_ptr = nullptr;
Jeff Hao848f70a2014-01-15 13:49:50 -0800756 MethodRefToStringInitRegMap& method_to_string_init_map = Runtime::Current()->GetStringInitMap();
Jeff Haoa2c38642015-09-17 17:29:01 -0700757 {
758 MutexLock mu(self, *Locks::interpreter_string_init_map_lock_);
Jeff Hao4686c522015-09-18 14:44:32 -0700759 auto it = method_to_string_init_map.find(method_ref);
760 if (it != method_to_string_init_map.end()) {
761 string_init_map_ptr = &it->second;
Jeff Haoa2c38642015-09-17 17:29:01 -0700762 }
Jeff Hao4686c522015-09-18 14:44:32 -0700763 }
764 if (string_init_map_ptr == nullptr) {
765 SafeMap<uint32_t, std::set<uint32_t>> string_init_map =
766 verifier::MethodVerifier::FindStringInitMap(method);
767 MutexLock mu(self, *Locks::interpreter_string_init_map_lock_);
Vladimir Marko78568352015-09-21 12:00:16 +0100768 auto it = method_to_string_init_map.lower_bound(method_ref);
769 if (it == method_to_string_init_map.end() ||
770 method_to_string_init_map.key_comp()(method_ref, it->first)) {
771 it = method_to_string_init_map.PutBefore(it, method_ref, std::move(string_init_map));
772 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800773 string_init_map_ptr = &it->second;
774 }
775 if (string_init_map_ptr->size() != 0) {
776 uint32_t dex_pc = shadow_frame.GetDexPC();
777 auto map_it = string_init_map_ptr->find(dex_pc);
778 if (map_it != string_init_map_ptr->end()) {
779 const std::set<uint32_t>& reg_set = map_it->second;
780 for (auto set_it = reg_set.begin(); set_it != reg_set.end(); ++set_it) {
781 shadow_frame.SetVRegReference(*set_it, result->GetL());
782 }
783 }
784 }
785 }
786
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200787 return !self->IsExceptionPending();
788}
789
Igor Murashkin158f35c2015-06-10 15:55:30 -0700790template<bool is_range, bool do_assignability_check>
791bool DoLambdaCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100792 const Instruction* inst, uint16_t inst_data ATTRIBUTE_UNUSED, JValue* result) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700793 const uint4_t num_additional_registers = inst->VRegB_25x();
794 // Argument word count.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700795 const uint16_t number_of_inputs = num_additional_registers + kLambdaVirtualRegisterWidth;
796 // The lambda closure register is always present and is not encoded in the count.
797 // Furthermore, the lambda closure register is always wide, so it counts as 2 inputs.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700798
799 // TODO: find a cleaner way to separate non-range and range information without duplicating
800 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700801 uint32_t arg[Instruction::kMaxVarArgRegs25x]; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700802 uint32_t vregC = 0; // only used in invoke-XXX-range.
803 if (is_range) {
804 vregC = inst->VRegC_3rc();
805 } else {
806 // 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 -0700807 inst->GetAllArgs25x(arg);
808 }
809
810 // TODO: if there's an assignability check, throw instead?
811 DCHECK(called_method->IsStatic());
812
813 return DoCallCommon<is_range, do_assignability_check>(
814 called_method, self, shadow_frame,
815 result, number_of_inputs, arg, vregC);
816}
817
818template<bool is_range, bool do_assignability_check>
819bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
820 const Instruction* inst, uint16_t inst_data, JValue* result) {
821 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100822 const uint16_t number_of_inputs =
823 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700824
825 // TODO: find a cleaner way to separate non-range and range information without duplicating
826 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700827 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700828 uint32_t vregC = 0;
829 if (is_range) {
830 vregC = inst->VRegC_3rc();
831 } else {
832 vregC = inst->VRegC_35c();
833 inst->GetVarArgs(arg, inst_data);
834 }
835
836 return DoCallCommon<is_range, do_assignability_check>(
837 called_method, self, shadow_frame,
838 result, number_of_inputs, arg, vregC);
839}
840
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100841template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200842bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
843 Thread* self, JValue* result) {
844 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
845 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
846 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
847 if (!is_range) {
848 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
849 CHECK_LE(length, 5);
850 }
851 if (UNLIKELY(length < 0)) {
852 ThrowNegativeArraySizeException(length);
853 return false;
854 }
855 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700856 Class* array_class = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(),
857 self, false, do_access_check);
858 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200859 DCHECK(self->IsExceptionPending());
860 return false;
861 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700862 CHECK(array_class->IsArrayClass());
863 Class* component_class = array_class->GetComponentType();
864 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
865 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
866 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200867 ThrowRuntimeException("Bad filled array request for type %s",
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700868 PrettyDescriptor(component_class).c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200869 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000870 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800871 "Found type %s; filled-new-array not implemented for anything but 'int'",
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700872 PrettyDescriptor(component_class).c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200873 }
874 return false;
875 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700876 Object* new_array = Array::Alloc<true>(self, array_class, length,
877 array_class->GetComponentSizeShift(),
878 Runtime::Current()->GetHeap()->GetCurrentAllocator());
879 if (UNLIKELY(new_array == nullptr)) {
880 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200881 return false;
882 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700883 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
884 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200885 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100886 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200887 } else {
Ian Rogers29a26482014-05-02 15:27:29 -0700888 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100889 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100890 for (int32_t i = 0; i < length; ++i) {
891 size_t src_reg = is_range ? vregC + i : arg[i];
892 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700893 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
894 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +0100895 } else {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700896 new_array->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(
897 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200898 }
899 }
900
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700901 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200902 return true;
903}
904
Mathieu Chartier90443472015-07-16 20:32:27 -0700905// TODO fix thread analysis: should be SHARED_REQUIRES(Locks::mutator_lock_).
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100906template<typename T>
907static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count)
908 NO_THREAD_SAFETY_ANALYSIS {
909 Runtime* runtime = Runtime::Current();
910 for (int32_t i = 0; i < count; ++i) {
911 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
912 }
913}
914
915void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
Mathieu Chartier90443472015-07-16 20:32:27 -0700916 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100917 DCHECK(Runtime::Current()->IsActiveTransaction());
918 DCHECK(array != nullptr);
919 DCHECK_LE(count, array->GetLength());
920 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
921 switch (primitive_component_type) {
922 case Primitive::kPrimBoolean:
923 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
924 break;
925 case Primitive::kPrimByte:
926 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
927 break;
928 case Primitive::kPrimChar:
929 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
930 break;
931 case Primitive::kPrimShort:
932 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
933 break;
934 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100935 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
936 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700937 case Primitive::kPrimFloat:
938 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
939 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100940 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100941 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
942 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700943 case Primitive::kPrimDouble:
944 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
945 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100946 default:
947 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
948 << " in fill-array-data";
949 break;
950 }
951}
952
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200953// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200954#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700955 template SHARED_REQUIRES(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100956 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
957 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +0200958 const Instruction* inst, uint16_t inst_data, \
959 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200960EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
961EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
962EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
963EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
964#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200965
Igor Murashkin158f35c2015-06-10 15:55:30 -0700966// Explicit DoLambdaCall template function declarations.
967#define EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700968 template SHARED_REQUIRES(Locks::mutator_lock_) \
Igor Murashkin158f35c2015-06-10 15:55:30 -0700969 bool DoLambdaCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
970 ShadowFrame& shadow_frame, \
971 const Instruction* inst, \
972 uint16_t inst_data, \
973 JValue* result)
974EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(false, false);
975EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(false, true);
976EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(true, false);
977EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(true, true);
978#undef EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL
979
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200980// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100981#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700982 template SHARED_REQUIRES(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100983 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
984 const ShadowFrame& shadow_frame, \
985 Thread* self, JValue* result)
986#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
987 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
988 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
989 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
990 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
991EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
992EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
993#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200994#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
995
996} // namespace interpreter
997} // namespace art