blob: 3c7db853950e82c60835852039f546faf706625e [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
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010019#include "mirror/array-inl.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020020
21namespace art {
22namespace interpreter {
23
Ian Rogers54874942014-06-10 16:31:03 -070024void ThrowNullPointerExceptionFromInterpreter(const ShadowFrame& shadow_frame) {
25 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow());
26}
27
28template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
29bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
30 uint16_t inst_data) {
31 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
32 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
33 ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
Fred Shih37f05ef2014-07-16 18:38:08 -070034 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -070035 if (UNLIKELY(f == nullptr)) {
36 CHECK(self->IsExceptionPending());
37 return false;
38 }
39 Object* obj;
40 if (is_static) {
41 obj = f->GetDeclaringClass();
42 } else {
43 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
44 if (UNLIKELY(obj == nullptr)) {
45 ThrowNullPointerExceptionForFieldAccess(shadow_frame.GetCurrentLocationForThrow(), f, true);
46 return false;
47 }
48 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +020049 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -070050 // Report this field access to instrumentation if needed.
51 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
52 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
53 Object* this_object = f->IsStatic() ? nullptr : obj;
54 instrumentation->FieldReadEvent(self, this_object, shadow_frame.GetMethod(),
55 shadow_frame.GetDexPC(), f);
56 }
57 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
58 switch (field_type) {
59 case Primitive::kPrimBoolean:
60 shadow_frame.SetVReg(vregA, f->GetBoolean(obj));
61 break;
62 case Primitive::kPrimByte:
63 shadow_frame.SetVReg(vregA, f->GetByte(obj));
64 break;
65 case Primitive::kPrimChar:
66 shadow_frame.SetVReg(vregA, f->GetChar(obj));
67 break;
68 case Primitive::kPrimShort:
69 shadow_frame.SetVReg(vregA, f->GetShort(obj));
70 break;
71 case Primitive::kPrimInt:
72 shadow_frame.SetVReg(vregA, f->GetInt(obj));
73 break;
74 case Primitive::kPrimLong:
75 shadow_frame.SetVRegLong(vregA, f->GetLong(obj));
76 break;
77 case Primitive::kPrimNot:
78 shadow_frame.SetVRegReference(vregA, f->GetObject(obj));
79 break;
80 default:
81 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -070082 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -070083 }
84 return true;
85}
86
87// Explicitly instantiate all DoFieldGet functions.
88#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
89 template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \
90 ShadowFrame& shadow_frame, \
91 const Instruction* inst, \
92 uint16_t inst_data)
93
94#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
95 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \
96 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true);
97
98// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -070099EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
100EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
101EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
102EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
103EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
104EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
105EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700106
107// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700108EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
109EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
110EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
111EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
112EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
113EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
114EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700115
116#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
117#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
118
119// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
120// Returns true on success, otherwise throws an exception and returns false.
121template<Primitive::Type field_type>
122bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
123 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
124 if (UNLIKELY(obj == nullptr)) {
125 // We lost the reference to the field index so we cannot get a more
126 // precised exception message.
127 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow());
128 return false;
129 }
130 MemberOffset field_offset(inst->VRegC_22c());
131 // Report this field access to instrumentation if needed. Since we only have the offset of
132 // the field from the base of the object, we need to look for it first.
133 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
134 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
135 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
136 field_offset.Uint32Value());
137 DCHECK(f != nullptr);
138 DCHECK(!f->IsStatic());
139 instrumentation->FieldReadEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
140 shadow_frame.GetDexPC(), f);
141 }
142 // Note: iget-x-quick instructions are only for non-volatile fields.
143 const uint32_t vregA = inst->VRegA_22c(inst_data);
144 switch (field_type) {
145 case Primitive::kPrimInt:
146 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
147 break;
148 case Primitive::kPrimLong:
149 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
150 break;
151 case Primitive::kPrimNot:
152 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
153 break;
154 default:
155 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700156 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700157 }
158 return true;
159}
160
161// Explicitly instantiate all DoIGetQuick functions.
162#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
163 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
164 uint16_t inst_data)
165
166EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
167EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
168EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
169#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
170
171template<Primitive::Type field_type>
172static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
173 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
174 JValue field_value;
175 switch (field_type) {
176 case Primitive::kPrimBoolean:
177 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
178 break;
179 case Primitive::kPrimByte:
180 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
181 break;
182 case Primitive::kPrimChar:
183 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
184 break;
185 case Primitive::kPrimShort:
186 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
187 break;
188 case Primitive::kPrimInt:
189 field_value.SetI(shadow_frame.GetVReg(vreg));
190 break;
191 case Primitive::kPrimLong:
192 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
193 break;
194 case Primitive::kPrimNot:
195 field_value.SetL(shadow_frame.GetVRegReference(vreg));
196 break;
197 default:
198 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700199 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700200 }
201 return field_value;
202}
203
204template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
205 bool transaction_active>
206bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
207 uint16_t inst_data) {
208 bool do_assignability_check = do_access_check;
209 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
210 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
211 ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
Fred Shih37f05ef2014-07-16 18:38:08 -0700212 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -0700213 if (UNLIKELY(f == nullptr)) {
214 CHECK(self->IsExceptionPending());
215 return false;
216 }
217 Object* obj;
218 if (is_static) {
219 obj = f->GetDeclaringClass();
220 } else {
221 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
222 if (UNLIKELY(obj == nullptr)) {
223 ThrowNullPointerExceptionForFieldAccess(shadow_frame.GetCurrentLocationForThrow(),
224 f, false);
225 return false;
226 }
227 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +0200228 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -0700229 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
230 // Report this field access to instrumentation if needed. Since we only have the offset of
231 // the field from the base of the object, we need to look for it first.
232 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
233 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
234 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
235 Object* this_object = f->IsStatic() ? nullptr : obj;
236 instrumentation->FieldWriteEvent(self, this_object, shadow_frame.GetMethod(),
237 shadow_frame.GetDexPC(), f, field_value);
238 }
239 switch (field_type) {
240 case Primitive::kPrimBoolean:
241 f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA));
242 break;
243 case Primitive::kPrimByte:
244 f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA));
245 break;
246 case Primitive::kPrimChar:
247 f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA));
248 break;
249 case Primitive::kPrimShort:
250 f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA));
251 break;
252 case Primitive::kPrimInt:
253 f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA));
254 break;
255 case Primitive::kPrimLong:
256 f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA));
257 break;
258 case Primitive::kPrimNot: {
259 Object* reg = shadow_frame.GetVRegReference(vregA);
260 if (do_assignability_check && reg != nullptr) {
261 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
262 // object in the destructor.
263 Class* field_class;
264 {
265 StackHandleScope<3> hs(self);
266 HandleWrapper<mirror::ArtField> h_f(hs.NewHandleWrapper(&f));
267 HandleWrapper<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
268 HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
Ian Rogers08f1f502014-12-02 15:04:37 -0800269 field_class = h_f->GetType(true);
Ian Rogers54874942014-06-10 16:31:03 -0700270 }
271 if (!reg->VerifierInstanceOf(field_class)) {
272 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700273 std::string temp1, temp2, temp3;
Ian Rogers54874942014-06-10 16:31:03 -0700274 self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(),
275 "Ljava/lang/VirtualMachineError;",
276 "Put '%s' that is not instance of field '%s' in '%s'",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700277 reg->GetClass()->GetDescriptor(&temp1),
278 field_class->GetDescriptor(&temp2),
279 f->GetDeclaringClass()->GetDescriptor(&temp3));
Ian Rogers54874942014-06-10 16:31:03 -0700280 return false;
281 }
282 }
283 f->SetObj<transaction_active>(obj, reg);
284 break;
285 }
286 default:
287 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700288 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700289 }
290 return true;
291}
292
293// Explicitly instantiate all DoFieldPut functions.
294#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
295 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
296 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
297
298#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
299 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
300 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
301 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
302 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
303
304// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700305EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
306EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
307EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
308EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
309EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
310EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
311EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700312
313// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700314EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
315EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
316EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
317EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
318EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
319EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
320EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700321
322#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
323#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
324
325template<Primitive::Type field_type, bool transaction_active>
326bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
327 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
328 if (UNLIKELY(obj == nullptr)) {
329 // We lost the reference to the field index so we cannot get a more
330 // precised exception message.
331 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow());
332 return false;
333 }
334 MemberOffset field_offset(inst->VRegC_22c());
335 const uint32_t vregA = inst->VRegA_22c(inst_data);
336 // Report this field modification to instrumentation if needed. Since we only have the offset of
337 // the field from the base of the object, we need to look for it first.
338 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
339 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
340 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
341 field_offset.Uint32Value());
342 DCHECK(f != nullptr);
343 DCHECK(!f->IsStatic());
344 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
345 instrumentation->FieldWriteEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
346 shadow_frame.GetDexPC(), f, field_value);
347 }
348 // Note: iput-x-quick instructions are only for non-volatile fields.
349 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700350 case Primitive::kPrimBoolean:
351 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
352 break;
353 case Primitive::kPrimByte:
354 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
355 break;
356 case Primitive::kPrimChar:
357 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
358 break;
359 case Primitive::kPrimShort:
360 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
361 break;
Ian Rogers54874942014-06-10 16:31:03 -0700362 case Primitive::kPrimInt:
363 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
364 break;
365 case Primitive::kPrimLong:
366 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
367 break;
368 case Primitive::kPrimNot:
369 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
370 break;
371 default:
372 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700373 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700374 }
375 return true;
376}
377
378// Explicitly instantiate all DoIPutQuick functions.
379#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
380 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
381 const Instruction* inst, \
382 uint16_t inst_data)
383
384#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
385 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
386 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
387
Andreas Gampec8ccf682014-09-29 20:07:43 -0700388EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
389EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
390EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
391EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
392EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
393EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
394EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700395#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
396#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
397
Sebastien Hertz9f102032014-05-23 08:59:42 +0200398/**
399 * Finds the location where this exception will be caught. We search until we reach either the top
400 * frame or a native frame, in which cases this exception is considered uncaught.
401 */
402class CatchLocationFinder : public StackVisitor {
403 public:
404 explicit CatchLocationFinder(Thread* self, Handle<mirror::Throwable>* exception)
405 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
406 : StackVisitor(self, nullptr), self_(self), handle_scope_(self), exception_(exception),
407 catch_method_(handle_scope_.NewHandle<mirror::ArtMethod>(nullptr)),
408 catch_dex_pc_(DexFile::kDexNoIndex), clear_exception_(false) {
409 }
410
411 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
412 mirror::ArtMethod* method = GetMethod();
413 if (method == nullptr) {
414 return true;
415 }
416 if (method->IsRuntimeMethod()) {
417 // Ignore callee save method.
418 DCHECK(method->IsCalleeSaveMethod());
419 return true;
420 }
421 if (method->IsNative()) {
422 return false; // End stack walk.
423 }
424 DCHECK(!method->IsNative());
425 uint32_t dex_pc = GetDexPc();
426 if (dex_pc != DexFile::kDexNoIndex) {
427 uint32_t found_dex_pc;
428 {
429 StackHandleScope<3> hs(self_);
430 Handle<mirror::Class> exception_class(hs.NewHandle((*exception_)->GetClass()));
431 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
432 found_dex_pc = mirror::ArtMethod::FindCatchBlock(h_method, exception_class, dex_pc,
433 &clear_exception_);
434 }
435 if (found_dex_pc != DexFile::kDexNoIndex) {
436 catch_method_.Assign(method);
437 catch_dex_pc_ = found_dex_pc;
438 return false; // End stack walk.
439 }
440 }
441 return true; // Continue stack walk.
442 }
443
444 ArtMethod* GetCatchMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
445 return catch_method_.Get();
446 }
447
448 uint32_t GetCatchDexPc() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
449 return catch_dex_pc_;
450 }
451
452 bool NeedClearException() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
453 return clear_exception_;
454 }
455
456 private:
457 Thread* const self_;
458 StackHandleScope<1> handle_scope_;
459 Handle<mirror::Throwable>* exception_;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700460 MutableHandle<mirror::ArtMethod> catch_method_;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200461 uint32_t catch_dex_pc_;
462 bool clear_exception_;
463
464
465 DISALLOW_COPY_AND_ASSIGN(CatchLocationFinder);
466};
467
Ian Rogers54874942014-06-10 16:31:03 -0700468uint32_t FindNextInstructionFollowingException(Thread* self,
469 ShadowFrame& shadow_frame,
470 uint32_t dex_pc,
Ian Rogers54874942014-06-10 16:31:03 -0700471 const instrumentation::Instrumentation* instrumentation) {
472 self->VerifyStack();
473 ThrowLocation throw_location;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200474 StackHandleScope<3> hs(self);
475 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException(&throw_location)));
476 if (!self->IsExceptionReportedToInstrumentation() && instrumentation->HasExceptionCaughtListeners()) {
477 CatchLocationFinder clf(self, &exception);
478 clf.WalkStack(false);
479 instrumentation->ExceptionCaughtEvent(self, throw_location, clf.GetCatchMethod(),
480 clf.GetCatchDexPc(), exception.Get());
481 self->SetExceptionReportedToInstrumentation(true);
482 }
Ian Rogers54874942014-06-10 16:31:03 -0700483 bool clear_exception = false;
484 uint32_t found_dex_pc;
485 {
Ian Rogers54874942014-06-10 16:31:03 -0700486 Handle<mirror::Class> exception_class(hs.NewHandle(exception->GetClass()));
487 Handle<mirror::ArtMethod> h_method(hs.NewHandle(shadow_frame.GetMethod()));
Ian Rogers54874942014-06-10 16:31:03 -0700488 found_dex_pc = mirror::ArtMethod::FindCatchBlock(h_method, exception_class, dex_pc,
489 &clear_exception);
490 }
491 if (found_dex_pc == DexFile::kDexNoIndex) {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200492 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
Ian Rogers54874942014-06-10 16:31:03 -0700493 shadow_frame.GetMethod(), dex_pc);
494 } else {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200495 if (self->IsExceptionReportedToInstrumentation()) {
496 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
497 shadow_frame.GetMethod(), dex_pc);
498 }
Ian Rogers54874942014-06-10 16:31:03 -0700499 if (clear_exception) {
500 self->ClearException();
501 }
502 }
503 return found_dex_pc;
504}
505
Ian Rogerse94652f2014-12-02 11:13:19 -0800506void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
507 LOG(FATAL) << "Unexpected instruction: "
508 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
509 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700510}
511
Ian Rogerse94652f2014-12-02 11:13:19 -0800512static void UnstartedRuntimeInvoke(Thread* self, const DexFile::CodeItem* code_item,
513 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200514 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200515
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200516// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800517static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
518 size_t dest_reg, size_t src_reg)
519 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200520 // If both register locations contains the same value, the register probably holds a reference.
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700521 // Uint required, so that sign extension does not make this wrong on 64b systems
522 uint32_t src_value = shadow_frame.GetVReg(src_reg);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800523 mirror::Object* o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700524 if (src_value == reinterpret_cast<uintptr_t>(o)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800525 new_shadow_frame->SetVRegReference(dest_reg, o);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200526 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800527 new_shadow_frame->SetVReg(dest_reg, src_value);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200528 }
529}
530
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700531void AbortTransaction(Thread* self, const char* fmt, ...) {
532 CHECK(Runtime::Current()->IsActiveTransaction());
533 // Throw an exception so we can abort the transaction and undo every change.
534 va_list args;
535 va_start(args, fmt);
536 self->ThrowNewExceptionV(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;", fmt,
537 args);
538 va_end(args);
539}
540
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200541template<bool is_range, bool do_assignability_check>
Ian Rogerse94652f2014-12-02 11:13:19 -0800542bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200543 const Instruction* inst, uint16_t inst_data, JValue* result) {
544 // Compute method information.
Ian Rogerse94652f2014-12-02 11:13:19 -0800545 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200546 const uint16_t num_ins = (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200547 uint16_t num_regs;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200548 if (LIKELY(code_item != NULL)) {
549 num_regs = code_item->registers_size_;
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200550 DCHECK_EQ(num_ins, code_item->ins_size_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200551 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800552 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200553 num_regs = num_ins;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200554 }
555
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200556 // Allocate shadow frame on the stack.
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700557 const char* old_cause = self->StartAssertNoThreadSuspension("DoCall");
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200558 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
Ian Rogerse94652f2014-12-02 11:13:19 -0800559 ShadowFrame* new_shadow_frame(ShadowFrame::Create(num_regs, &shadow_frame, called_method, 0,
560 memory));
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200561
562 // Initialize new shadow frame.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200563 const size_t first_dest_reg = num_regs - num_ins;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700564 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700565 // Slow path.
566 // We might need to do class loading, which incurs a thread state change to kNative. So
567 // register the shadow frame as under construction and allow suspension again.
568 self->SetShadowFrameUnderConstruction(new_shadow_frame);
569 self->EndAssertNoThreadSuspension(old_cause);
570
571 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200572 // to get the exact type of each reference argument.
Ian Rogerse94652f2014-12-02 11:13:19 -0800573 const DexFile::TypeList* params = new_shadow_frame->GetMethod()->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700574 uint32_t shorty_len = 0;
Ian Rogerse94652f2014-12-02 11:13:19 -0800575 const char* shorty = new_shadow_frame->GetMethod()->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200576
Ian Rogerse94652f2014-12-02 11:13:19 -0800577 // TODO: find a cleaner way to separate non-range and range information without duplicating
578 // code.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200579 uint32_t arg[5]; // only used in invoke-XXX.
580 uint32_t vregC; // only used in invoke-XXX-range.
581 if (is_range) {
582 vregC = inst->VRegC_3rc();
583 } else {
Ian Rogers29a26482014-05-02 15:27:29 -0700584 inst->GetVarArgs(arg, inst_data);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200585 }
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100586
587 // Handle receiver apart since it's not part of the shorty.
588 size_t dest_reg = first_dest_reg;
589 size_t arg_offset = 0;
Ian Rogerse94652f2014-12-02 11:13:19 -0800590 if (!new_shadow_frame->GetMethod()->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700591 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100592 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
593 ++dest_reg;
594 ++arg_offset;
595 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800596 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700597 DCHECK_LT(shorty_pos + 1, shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200598 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
599 switch (shorty[shorty_pos + 1]) {
600 case 'L': {
601 Object* o = shadow_frame.GetVRegReference(src_reg);
602 if (do_assignability_check && o != NULL) {
Ian Rogersa0485602014-12-02 15:48:04 -0800603 Class* arg_type =
604 new_shadow_frame->GetMethod()->GetClassFromTypeIndex(
605 params->GetTypeItem(shorty_pos).type_idx_, true);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200606 if (arg_type == NULL) {
607 CHECK(self->IsExceptionPending());
608 return false;
609 }
610 if (!o->VerifierInstanceOf(arg_type)) {
611 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700612 std::string temp1, temp2;
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200613 self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(),
614 "Ljava/lang/VirtualMachineError;",
615 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -0800616 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700617 o->GetClass()->GetDescriptor(&temp1),
618 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200619 return false;
620 }
Jeff Haoa3faaf42013-09-03 19:07:00 -0700621 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200622 new_shadow_frame->SetVRegReference(dest_reg, o);
623 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700624 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200625 case 'J': case 'D': {
626 uint64_t wide_value = (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << 32) |
627 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
628 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
629 ++dest_reg;
630 ++arg_offset;
631 break;
632 }
633 default:
634 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
635 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200636 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200637 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700638 // We're done with the construction.
639 self->ClearShadowFrameUnderConstruction();
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200640 } else {
641 // Fast path: no extra checks.
642 if (is_range) {
643 const uint16_t first_src_reg = inst->VRegC_3rc();
644 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs;
645 ++dest_reg, ++src_reg) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800646 AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200647 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200648 } else {
649 DCHECK_LE(num_ins, 5U);
650 uint16_t regList = inst->Fetch16(2);
651 uint16_t count = num_ins;
652 if (count == 5) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800653 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + 4U,
654 (inst_data >> 8) & 0x0f);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200655 --count;
656 }
657 for (size_t arg_index = 0; arg_index < count; ++arg_index, regList >>= 4) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800658 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, regList & 0x0f);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200659 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200660 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700661 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200662 }
663
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200664 // Do the call now.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200665 if (LIKELY(Runtime::Current()->IsStarted())) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800666 if (kIsDebugBuild && new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter() == nullptr) {
667 LOG(FATAL) << "Attempt to invoke non-executable method: "
668 << PrettyMethod(new_shadow_frame->GetMethod());
669 UNREACHABLE();
Ian Rogers1d99e452014-01-02 17:36:41 -0800670 }
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800671 if (kIsDebugBuild && Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly() &&
Ian Rogerse94652f2014-12-02 11:13:19 -0800672 !new_shadow_frame->GetMethod()->IsNative() &&
673 !new_shadow_frame->GetMethod()->IsProxyMethod() &&
674 new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter()
675 == artInterpreterToCompiledCodeBridge) {
676 LOG(FATAL) << "Attempt to call compiled code when -Xint: "
677 << PrettyMethod(new_shadow_frame->GetMethod());
678 UNREACHABLE();
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800679 }
Ian Rogerse94652f2014-12-02 11:13:19 -0800680 (new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter())(self, code_item,
681 new_shadow_frame, result);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200682 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800683 UnstartedRuntimeInvoke(self, code_item, new_shadow_frame, result, first_dest_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200684 }
685 return !self->IsExceptionPending();
686}
687
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100688template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200689bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
690 Thread* self, JValue* result) {
691 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
692 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
693 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
694 if (!is_range) {
695 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
696 CHECK_LE(length, 5);
697 }
698 if (UNLIKELY(length < 0)) {
699 ThrowNegativeArraySizeException(length);
700 return false;
701 }
702 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
703 Class* arrayClass = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(),
704 self, false, do_access_check);
705 if (UNLIKELY(arrayClass == NULL)) {
706 DCHECK(self->IsExceptionPending());
707 return false;
708 }
709 CHECK(arrayClass->IsArrayClass());
710 Class* componentClass = arrayClass->GetComponentType();
711 if (UNLIKELY(componentClass->IsPrimitive() && !componentClass->IsPrimitiveInt())) {
712 if (componentClass->IsPrimitiveLong() || componentClass->IsPrimitiveDouble()) {
713 ThrowRuntimeException("Bad filled array request for type %s",
714 PrettyDescriptor(componentClass).c_str());
715 } else {
716 self->ThrowNewExceptionF(shadow_frame.GetCurrentLocationForThrow(),
717 "Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800718 "Found type %s; filled-new-array not implemented for anything but 'int'",
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200719 PrettyDescriptor(componentClass).c_str());
720 }
721 return false;
722 }
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700723 Object* newArray = Array::Alloc<true>(self, arrayClass, length,
724 arrayClass->GetComponentSizeShift(),
Ian Rogers6fac4472014-02-25 17:01:10 -0800725 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200726 if (UNLIKELY(newArray == NULL)) {
727 DCHECK(self->IsExceptionPending());
728 return false;
729 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100730 uint32_t arg[5]; // only used in filled-new-array.
731 uint32_t vregC; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200732 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100733 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200734 } else {
Ian Rogers29a26482014-05-02 15:27:29 -0700735 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100736 }
737 const bool is_primitive_int_component = componentClass->IsPrimitiveInt();
738 for (int32_t i = 0; i < length; ++i) {
739 size_t src_reg = is_range ? vregC + i : arg[i];
740 if (is_primitive_int_component) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100741 newArray->AsIntArray()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +0100742 } else {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100743 newArray->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200744 }
745 }
746
747 result->SetL(newArray);
748 return true;
749}
750
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100751// TODO fix thread analysis: should be SHARED_LOCKS_REQUIRED(Locks::mutator_lock_).
752template<typename T>
753static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count)
754 NO_THREAD_SAFETY_ANALYSIS {
755 Runtime* runtime = Runtime::Current();
756 for (int32_t i = 0; i < count; ++i) {
757 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
758 }
759}
760
761void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
762 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
763 DCHECK(Runtime::Current()->IsActiveTransaction());
764 DCHECK(array != nullptr);
765 DCHECK_LE(count, array->GetLength());
766 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
767 switch (primitive_component_type) {
768 case Primitive::kPrimBoolean:
769 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
770 break;
771 case Primitive::kPrimByte:
772 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
773 break;
774 case Primitive::kPrimChar:
775 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
776 break;
777 case Primitive::kPrimShort:
778 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
779 break;
780 case Primitive::kPrimInt:
781 case Primitive::kPrimFloat:
782 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
783 break;
784 case Primitive::kPrimLong:
785 case Primitive::kPrimDouble:
786 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
787 break;
788 default:
789 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
790 << " in fill-array-data";
791 break;
792 }
793}
794
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200795// Helper function to deal with class loading in an unstarted runtime.
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700796static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className,
797 Handle<mirror::ClassLoader> class_loader, JValue* result,
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200798 const std::string& method_name, bool initialize_class,
799 bool abort_if_not_found)
800 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
801 CHECK(className.Get() != nullptr);
802 std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str()));
803 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
804
805 Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader);
806 if (found == nullptr && abort_if_not_found) {
807 if (!self->IsExceptionPending()) {
808 AbortTransaction(self, "%s failed in un-started runtime for class: %s",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700809 method_name.c_str(), PrettyDescriptor(descriptor.c_str()).c_str());
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200810 }
811 return;
812 }
813 if (found != nullptr && initialize_class) {
814 StackHandleScope<1> hs(self);
815 Handle<mirror::Class> h_class(hs.NewHandle(found));
Ian Rogers7b078e82014-09-10 14:44:24 -0700816 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200817 CHECK(self->IsExceptionPending());
818 return;
819 }
820 }
821 result->SetL(found);
822}
823
Ian Rogerse94652f2014-12-02 11:13:19 -0800824static void UnstartedRuntimeInvoke(Thread* self, const DexFile::CodeItem* code_item,
825 ShadowFrame* shadow_frame,
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200826 JValue* result, size_t arg_offset) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200827 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
828 // problems in core libraries.
829 std::string name(PrettyMethod(shadow_frame->GetMethod()));
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200830 if (name == "java.lang.Class java.lang.Class.forName(java.lang.String)") {
831 // TODO: Support for the other variants that take more arguments should also be added.
832 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset)->AsString();
833 StackHandleScope<1> hs(self);
834 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
835 UnstartedRuntimeFindClass(self, h_class_name, NullHandle<mirror::ClassLoader>(), result, name,
836 true, true);
837 } else if (name == "java.lang.Class java.lang.VMClassLoader.loadClass(java.lang.String, boolean)") {
838 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset)->AsString();
839 StackHandleScope<1> hs(self);
840 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
841 UnstartedRuntimeFindClass(self, h_class_name, NullHandle<mirror::ClassLoader>(), result, name,
842 false, true);
843 } else if (name == "java.lang.Class java.lang.VMClassLoader.findLoadedClass(java.lang.ClassLoader, java.lang.String)") {
844 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
845 mirror::ClassLoader* class_loader =
846 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
847 StackHandleScope<2> hs(self);
848 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
849 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
850 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, name, false, false);
Ian Rogersc45b8b52014-05-03 01:39:59 -0700851 } else if (name == "java.lang.Class java.lang.Void.lookupType()") {
852 result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V'));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200853 } else if (name == "java.lang.Object java.lang.Class.newInstance()") {
854 Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
855 ArtMethod* c = klass->FindDeclaredDirectMethod("<init>", "()V");
856 CHECK(c != NULL);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700857 StackHandleScope<1> hs(self);
858 Handle<Object> obj(hs.NewHandle(klass->AllocObject(self)));
859 CHECK(obj.Get() != NULL);
860 EnterInterpreterFromInvoke(self, c, obj.Get(), NULL, NULL);
861 result->SetL(obj.Get());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200862 } else if (name == "java.lang.reflect.Field java.lang.Class.getDeclaredField(java.lang.String)") {
863 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
864 // going the reflective Dex way.
865 Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800866 String* name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200867 ArtField* found = NULL;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200868 ObjectArray<ArtField>* fields = klass->GetIFields();
869 for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) {
870 ArtField* f = fields->Get(i);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800871 if (name2->Equals(f->GetName())) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200872 found = f;
873 }
874 }
875 if (found == NULL) {
876 fields = klass->GetSFields();
877 for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) {
878 ArtField* f = fields->Get(i);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800879 if (name2->Equals(f->GetName())) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200880 found = f;
881 }
882 }
883 }
884 CHECK(found != NULL)
885 << "Failed to find field in Class.getDeclaredField in un-started runtime. name="
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800886 << name2->ToModifiedUtf8() << " class=" << PrettyDescriptor(klass);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200887 // TODO: getDeclaredField calls GetType once the field is found to ensure a
888 // NoClassDefFoundError is thrown if the field's type cannot be resolved.
889 Class* jlr_Field = self->DecodeJObject(WellKnownClasses::java_lang_reflect_Field)->AsClass();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700890 StackHandleScope<1> hs(self);
891 Handle<Object> field(hs.NewHandle(jlr_Field->AllocNonMovableObject(self)));
892 CHECK(field.Get() != NULL);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200893 ArtMethod* c = jlr_Field->FindDeclaredDirectMethod("<init>", "(Ljava/lang/reflect/ArtField;)V");
894 uint32_t args[1];
Ian Rogersef7d42f2014-01-06 12:55:46 -0800895 args[0] = StackReference<mirror::Object>::FromMirrorPtr(found).AsVRegValue();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700896 EnterInterpreterFromInvoke(self, c, field.Get(), args, NULL);
897 result->SetL(field.Get());
Ian Rogersc45b8b52014-05-03 01:39:59 -0700898 } else if (name == "int java.lang.Object.hashCode()") {
899 Object* obj = shadow_frame->GetVRegReference(arg_offset);
900 result->SetI(obj->IdentityHashCode());
901 } else if (name == "java.lang.String java.lang.reflect.ArtMethod.getMethodName(java.lang.reflect.ArtMethod)") {
Ian Rogers6b14d552014-10-28 21:50:58 -0700902 mirror::ArtMethod* method = shadow_frame->GetVRegReference(arg_offset)->AsArtMethod();
903 result->SetL(method->GetNameAsString(self));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200904 } else if (name == "void java.lang.System.arraycopy(java.lang.Object, int, java.lang.Object, int, int)" ||
905 name == "void java.lang.System.arraycopy(char[], int, char[], int, int)") {
906 // Special case array copying without initializing System.
907 Class* ctype = shadow_frame->GetVRegReference(arg_offset)->GetClass()->GetComponentType();
908 jint srcPos = shadow_frame->GetVReg(arg_offset + 1);
909 jint dstPos = shadow_frame->GetVReg(arg_offset + 3);
910 jint length = shadow_frame->GetVReg(arg_offset + 4);
911 if (!ctype->IsPrimitive()) {
912 ObjectArray<Object>* src = shadow_frame->GetVRegReference(arg_offset)->AsObjectArray<Object>();
913 ObjectArray<Object>* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<Object>();
914 for (jint i = 0; i < length; ++i) {
915 dst->Set(dstPos + i, src->Get(srcPos + i));
916 }
917 } else if (ctype->IsPrimitiveChar()) {
918 CharArray* src = shadow_frame->GetVRegReference(arg_offset)->AsCharArray();
919 CharArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray();
920 for (jint i = 0; i < length; ++i) {
921 dst->Set(dstPos + i, src->Get(srcPos + i));
922 }
923 } else if (ctype->IsPrimitiveInt()) {
924 IntArray* src = shadow_frame->GetVRegReference(arg_offset)->AsIntArray();
925 IntArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsIntArray();
926 for (jint i = 0; i < length; ++i) {
927 dst->Set(dstPos + i, src->Get(srcPos + i));
928 }
929 } else {
Ian Rogersc45b8b52014-05-03 01:39:59 -0700930 self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;",
931 "Unimplemented System.arraycopy for type '%s'",
932 PrettyDescriptor(ctype).c_str());
933 }
934 } else if (name == "java.lang.Object java.lang.ThreadLocal.get()") {
935 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
936 if (caller == "java.lang.String java.lang.IntegralToString.convertInt(java.lang.AbstractStringBuilder, int)") {
937 // Allocate non-threadlocal buffer.
938 result->SetL(mirror::CharArray::Alloc(self, 11));
939 } else {
940 self->ThrowNewException(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;",
941 "Unimplemented ThreadLocal.get");
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200942 }
943 } else {
944 // Not special, continue with regular interpreter execution.
Ian Rogerse94652f2014-12-02 11:13:19 -0800945 artInterpreterToInterpreterBridge(self, code_item, shadow_frame, result);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200946 }
947}
948
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200949// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200950#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
951 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100952 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
953 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +0200954 const Instruction* inst, uint16_t inst_data, \
955 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200956EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
957EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
958EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
959EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
960#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200961
962// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100963#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
964 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
965 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
966 const ShadowFrame& shadow_frame, \
967 Thread* self, JValue* result)
968#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
969 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
970 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
971 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
972 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
973EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
974EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
975#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200976#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
977
978} // namespace interpreter
979} // namespace art