blob: 041650f150f1fa0b2d4cb4bd85bd920760cb063b [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
Ian Rogerse94652f2014-12-02 11:13:19 -0800541static mirror::Class* GetClassFromTypeIdx(mirror::ArtMethod* method, uint16_t type_idx)
542 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
543 mirror::Class* type = method->GetDexCacheResolvedType(type_idx);
544 if (type == nullptr) {
545 type = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
546 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
547 }
548 return type;
549}
550
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200551template<bool is_range, bool do_assignability_check>
Ian Rogerse94652f2014-12-02 11:13:19 -0800552bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200553 const Instruction* inst, uint16_t inst_data, JValue* result) {
554 // Compute method information.
Ian Rogerse94652f2014-12-02 11:13:19 -0800555 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200556 const uint16_t num_ins = (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200557 uint16_t num_regs;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200558 if (LIKELY(code_item != NULL)) {
559 num_regs = code_item->registers_size_;
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200560 DCHECK_EQ(num_ins, code_item->ins_size_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200561 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800562 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200563 num_regs = num_ins;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200564 }
565
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200566 // Allocate shadow frame on the stack.
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700567 const char* old_cause = self->StartAssertNoThreadSuspension("DoCall");
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200568 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
Ian Rogerse94652f2014-12-02 11:13:19 -0800569 ShadowFrame* new_shadow_frame(ShadowFrame::Create(num_regs, &shadow_frame, called_method, 0,
570 memory));
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200571
572 // Initialize new shadow frame.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200573 const size_t first_dest_reg = num_regs - num_ins;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700574 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700575 // Slow path.
576 // We might need to do class loading, which incurs a thread state change to kNative. So
577 // register the shadow frame as under construction and allow suspension again.
578 self->SetShadowFrameUnderConstruction(new_shadow_frame);
579 self->EndAssertNoThreadSuspension(old_cause);
580
581 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200582 // to get the exact type of each reference argument.
Ian Rogerse94652f2014-12-02 11:13:19 -0800583 const DexFile::TypeList* params = new_shadow_frame->GetMethod()->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700584 uint32_t shorty_len = 0;
Ian Rogerse94652f2014-12-02 11:13:19 -0800585 const char* shorty = new_shadow_frame->GetMethod()->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200586
Ian Rogerse94652f2014-12-02 11:13:19 -0800587 // TODO: find a cleaner way to separate non-range and range information without duplicating
588 // code.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200589 uint32_t arg[5]; // only used in invoke-XXX.
590 uint32_t vregC; // only used in invoke-XXX-range.
591 if (is_range) {
592 vregC = inst->VRegC_3rc();
593 } else {
Ian Rogers29a26482014-05-02 15:27:29 -0700594 inst->GetVarArgs(arg, inst_data);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200595 }
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100596
597 // Handle receiver apart since it's not part of the shorty.
598 size_t dest_reg = first_dest_reg;
599 size_t arg_offset = 0;
Ian Rogerse94652f2014-12-02 11:13:19 -0800600 if (!new_shadow_frame->GetMethod()->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700601 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100602 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
603 ++dest_reg;
604 ++arg_offset;
605 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800606 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700607 DCHECK_LT(shorty_pos + 1, shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200608 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
609 switch (shorty[shorty_pos + 1]) {
610 case 'L': {
611 Object* o = shadow_frame.GetVRegReference(src_reg);
612 if (do_assignability_check && o != NULL) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800613 Class* arg_type = GetClassFromTypeIdx(new_shadow_frame->GetMethod(),
614 params->GetTypeItem(shorty_pos).type_idx_);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200615 if (arg_type == NULL) {
616 CHECK(self->IsExceptionPending());
617 return false;
618 }
619 if (!o->VerifierInstanceOf(arg_type)) {
620 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700621 std::string temp1, temp2;
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200622 self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(),
623 "Ljava/lang/VirtualMachineError;",
624 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -0800625 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700626 o->GetClass()->GetDescriptor(&temp1),
627 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200628 return false;
629 }
Jeff Haoa3faaf42013-09-03 19:07:00 -0700630 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200631 new_shadow_frame->SetVRegReference(dest_reg, o);
632 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700633 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200634 case 'J': case 'D': {
635 uint64_t wide_value = (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << 32) |
636 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
637 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
638 ++dest_reg;
639 ++arg_offset;
640 break;
641 }
642 default:
643 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
644 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200645 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200646 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700647 // We're done with the construction.
648 self->ClearShadowFrameUnderConstruction();
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200649 } else {
650 // Fast path: no extra checks.
651 if (is_range) {
652 const uint16_t first_src_reg = inst->VRegC_3rc();
653 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs;
654 ++dest_reg, ++src_reg) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800655 AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200656 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200657 } else {
658 DCHECK_LE(num_ins, 5U);
659 uint16_t regList = inst->Fetch16(2);
660 uint16_t count = num_ins;
661 if (count == 5) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800662 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + 4U,
663 (inst_data >> 8) & 0x0f);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200664 --count;
665 }
666 for (size_t arg_index = 0; arg_index < count; ++arg_index, regList >>= 4) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800667 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, regList & 0x0f);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200668 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200669 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700670 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200671 }
672
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200673 // Do the call now.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200674 if (LIKELY(Runtime::Current()->IsStarted())) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800675 if (kIsDebugBuild && new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter() == nullptr) {
676 LOG(FATAL) << "Attempt to invoke non-executable method: "
677 << PrettyMethod(new_shadow_frame->GetMethod());
678 UNREACHABLE();
Ian Rogers1d99e452014-01-02 17:36:41 -0800679 }
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800680 if (kIsDebugBuild && Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly() &&
Ian Rogerse94652f2014-12-02 11:13:19 -0800681 !new_shadow_frame->GetMethod()->IsNative() &&
682 !new_shadow_frame->GetMethod()->IsProxyMethod() &&
683 new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter()
684 == artInterpreterToCompiledCodeBridge) {
685 LOG(FATAL) << "Attempt to call compiled code when -Xint: "
686 << PrettyMethod(new_shadow_frame->GetMethod());
687 UNREACHABLE();
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800688 }
Ian Rogerse94652f2014-12-02 11:13:19 -0800689 (new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter())(self, code_item,
690 new_shadow_frame, result);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200691 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800692 UnstartedRuntimeInvoke(self, code_item, new_shadow_frame, result, first_dest_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200693 }
694 return !self->IsExceptionPending();
695}
696
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100697template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200698bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
699 Thread* self, JValue* result) {
700 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
701 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
702 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
703 if (!is_range) {
704 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
705 CHECK_LE(length, 5);
706 }
707 if (UNLIKELY(length < 0)) {
708 ThrowNegativeArraySizeException(length);
709 return false;
710 }
711 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
712 Class* arrayClass = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(),
713 self, false, do_access_check);
714 if (UNLIKELY(arrayClass == NULL)) {
715 DCHECK(self->IsExceptionPending());
716 return false;
717 }
718 CHECK(arrayClass->IsArrayClass());
719 Class* componentClass = arrayClass->GetComponentType();
720 if (UNLIKELY(componentClass->IsPrimitive() && !componentClass->IsPrimitiveInt())) {
721 if (componentClass->IsPrimitiveLong() || componentClass->IsPrimitiveDouble()) {
722 ThrowRuntimeException("Bad filled array request for type %s",
723 PrettyDescriptor(componentClass).c_str());
724 } else {
725 self->ThrowNewExceptionF(shadow_frame.GetCurrentLocationForThrow(),
726 "Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800727 "Found type %s; filled-new-array not implemented for anything but 'int'",
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200728 PrettyDescriptor(componentClass).c_str());
729 }
730 return false;
731 }
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700732 Object* newArray = Array::Alloc<true>(self, arrayClass, length,
733 arrayClass->GetComponentSizeShift(),
Ian Rogers6fac4472014-02-25 17:01:10 -0800734 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200735 if (UNLIKELY(newArray == NULL)) {
736 DCHECK(self->IsExceptionPending());
737 return false;
738 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100739 uint32_t arg[5]; // only used in filled-new-array.
740 uint32_t vregC; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200741 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100742 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200743 } else {
Ian Rogers29a26482014-05-02 15:27:29 -0700744 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100745 }
746 const bool is_primitive_int_component = componentClass->IsPrimitiveInt();
747 for (int32_t i = 0; i < length; ++i) {
748 size_t src_reg = is_range ? vregC + i : arg[i];
749 if (is_primitive_int_component) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100750 newArray->AsIntArray()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +0100751 } else {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100752 newArray->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200753 }
754 }
755
756 result->SetL(newArray);
757 return true;
758}
759
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100760// TODO fix thread analysis: should be SHARED_LOCKS_REQUIRED(Locks::mutator_lock_).
761template<typename T>
762static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count)
763 NO_THREAD_SAFETY_ANALYSIS {
764 Runtime* runtime = Runtime::Current();
765 for (int32_t i = 0; i < count; ++i) {
766 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
767 }
768}
769
770void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
771 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
772 DCHECK(Runtime::Current()->IsActiveTransaction());
773 DCHECK(array != nullptr);
774 DCHECK_LE(count, array->GetLength());
775 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
776 switch (primitive_component_type) {
777 case Primitive::kPrimBoolean:
778 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
779 break;
780 case Primitive::kPrimByte:
781 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
782 break;
783 case Primitive::kPrimChar:
784 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
785 break;
786 case Primitive::kPrimShort:
787 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
788 break;
789 case Primitive::kPrimInt:
790 case Primitive::kPrimFloat:
791 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
792 break;
793 case Primitive::kPrimLong:
794 case Primitive::kPrimDouble:
795 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
796 break;
797 default:
798 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
799 << " in fill-array-data";
800 break;
801 }
802}
803
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200804// Helper function to deal with class loading in an unstarted runtime.
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700805static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className,
806 Handle<mirror::ClassLoader> class_loader, JValue* result,
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200807 const std::string& method_name, bool initialize_class,
808 bool abort_if_not_found)
809 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
810 CHECK(className.Get() != nullptr);
811 std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str()));
812 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
813
814 Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader);
815 if (found == nullptr && abort_if_not_found) {
816 if (!self->IsExceptionPending()) {
817 AbortTransaction(self, "%s failed in un-started runtime for class: %s",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700818 method_name.c_str(), PrettyDescriptor(descriptor.c_str()).c_str());
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200819 }
820 return;
821 }
822 if (found != nullptr && initialize_class) {
823 StackHandleScope<1> hs(self);
824 Handle<mirror::Class> h_class(hs.NewHandle(found));
Ian Rogers7b078e82014-09-10 14:44:24 -0700825 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200826 CHECK(self->IsExceptionPending());
827 return;
828 }
829 }
830 result->SetL(found);
831}
832
Ian Rogerse94652f2014-12-02 11:13:19 -0800833static void UnstartedRuntimeInvoke(Thread* self, const DexFile::CodeItem* code_item,
834 ShadowFrame* shadow_frame,
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200835 JValue* result, size_t arg_offset) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200836 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
837 // problems in core libraries.
838 std::string name(PrettyMethod(shadow_frame->GetMethod()));
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200839 if (name == "java.lang.Class java.lang.Class.forName(java.lang.String)") {
840 // TODO: Support for the other variants that take more arguments should also be added.
841 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset)->AsString();
842 StackHandleScope<1> hs(self);
843 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
844 UnstartedRuntimeFindClass(self, h_class_name, NullHandle<mirror::ClassLoader>(), result, name,
845 true, true);
846 } else if (name == "java.lang.Class java.lang.VMClassLoader.loadClass(java.lang.String, boolean)") {
847 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset)->AsString();
848 StackHandleScope<1> hs(self);
849 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
850 UnstartedRuntimeFindClass(self, h_class_name, NullHandle<mirror::ClassLoader>(), result, name,
851 false, true);
852 } else if (name == "java.lang.Class java.lang.VMClassLoader.findLoadedClass(java.lang.ClassLoader, java.lang.String)") {
853 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
854 mirror::ClassLoader* class_loader =
855 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
856 StackHandleScope<2> hs(self);
857 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
858 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
859 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, name, false, false);
Ian Rogersc45b8b52014-05-03 01:39:59 -0700860 } else if (name == "java.lang.Class java.lang.Void.lookupType()") {
861 result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V'));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200862 } else if (name == "java.lang.Object java.lang.Class.newInstance()") {
863 Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
864 ArtMethod* c = klass->FindDeclaredDirectMethod("<init>", "()V");
865 CHECK(c != NULL);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700866 StackHandleScope<1> hs(self);
867 Handle<Object> obj(hs.NewHandle(klass->AllocObject(self)));
868 CHECK(obj.Get() != NULL);
869 EnterInterpreterFromInvoke(self, c, obj.Get(), NULL, NULL);
870 result->SetL(obj.Get());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200871 } else if (name == "java.lang.reflect.Field java.lang.Class.getDeclaredField(java.lang.String)") {
872 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
873 // going the reflective Dex way.
874 Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800875 String* name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200876 ArtField* found = NULL;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200877 ObjectArray<ArtField>* fields = klass->GetIFields();
878 for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) {
879 ArtField* f = fields->Get(i);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800880 if (name2->Equals(f->GetName())) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200881 found = f;
882 }
883 }
884 if (found == NULL) {
885 fields = klass->GetSFields();
886 for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) {
887 ArtField* f = fields->Get(i);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800888 if (name2->Equals(f->GetName())) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200889 found = f;
890 }
891 }
892 }
893 CHECK(found != NULL)
894 << "Failed to find field in Class.getDeclaredField in un-started runtime. name="
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800895 << name2->ToModifiedUtf8() << " class=" << PrettyDescriptor(klass);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200896 // TODO: getDeclaredField calls GetType once the field is found to ensure a
897 // NoClassDefFoundError is thrown if the field's type cannot be resolved.
898 Class* jlr_Field = self->DecodeJObject(WellKnownClasses::java_lang_reflect_Field)->AsClass();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700899 StackHandleScope<1> hs(self);
900 Handle<Object> field(hs.NewHandle(jlr_Field->AllocNonMovableObject(self)));
901 CHECK(field.Get() != NULL);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200902 ArtMethod* c = jlr_Field->FindDeclaredDirectMethod("<init>", "(Ljava/lang/reflect/ArtField;)V");
903 uint32_t args[1];
Ian Rogersef7d42f2014-01-06 12:55:46 -0800904 args[0] = StackReference<mirror::Object>::FromMirrorPtr(found).AsVRegValue();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700905 EnterInterpreterFromInvoke(self, c, field.Get(), args, NULL);
906 result->SetL(field.Get());
Ian Rogersc45b8b52014-05-03 01:39:59 -0700907 } else if (name == "int java.lang.Object.hashCode()") {
908 Object* obj = shadow_frame->GetVRegReference(arg_offset);
909 result->SetI(obj->IdentityHashCode());
910 } else if (name == "java.lang.String java.lang.reflect.ArtMethod.getMethodName(java.lang.reflect.ArtMethod)") {
Ian Rogers6b14d552014-10-28 21:50:58 -0700911 mirror::ArtMethod* method = shadow_frame->GetVRegReference(arg_offset)->AsArtMethod();
912 result->SetL(method->GetNameAsString(self));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200913 } else if (name == "void java.lang.System.arraycopy(java.lang.Object, int, java.lang.Object, int, int)" ||
914 name == "void java.lang.System.arraycopy(char[], int, char[], int, int)") {
915 // Special case array copying without initializing System.
916 Class* ctype = shadow_frame->GetVRegReference(arg_offset)->GetClass()->GetComponentType();
917 jint srcPos = shadow_frame->GetVReg(arg_offset + 1);
918 jint dstPos = shadow_frame->GetVReg(arg_offset + 3);
919 jint length = shadow_frame->GetVReg(arg_offset + 4);
920 if (!ctype->IsPrimitive()) {
921 ObjectArray<Object>* src = shadow_frame->GetVRegReference(arg_offset)->AsObjectArray<Object>();
922 ObjectArray<Object>* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<Object>();
923 for (jint i = 0; i < length; ++i) {
924 dst->Set(dstPos + i, src->Get(srcPos + i));
925 }
926 } else if (ctype->IsPrimitiveChar()) {
927 CharArray* src = shadow_frame->GetVRegReference(arg_offset)->AsCharArray();
928 CharArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray();
929 for (jint i = 0; i < length; ++i) {
930 dst->Set(dstPos + i, src->Get(srcPos + i));
931 }
932 } else if (ctype->IsPrimitiveInt()) {
933 IntArray* src = shadow_frame->GetVRegReference(arg_offset)->AsIntArray();
934 IntArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsIntArray();
935 for (jint i = 0; i < length; ++i) {
936 dst->Set(dstPos + i, src->Get(srcPos + i));
937 }
938 } else {
Ian Rogersc45b8b52014-05-03 01:39:59 -0700939 self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;",
940 "Unimplemented System.arraycopy for type '%s'",
941 PrettyDescriptor(ctype).c_str());
942 }
943 } else if (name == "java.lang.Object java.lang.ThreadLocal.get()") {
944 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
945 if (caller == "java.lang.String java.lang.IntegralToString.convertInt(java.lang.AbstractStringBuilder, int)") {
946 // Allocate non-threadlocal buffer.
947 result->SetL(mirror::CharArray::Alloc(self, 11));
948 } else {
949 self->ThrowNewException(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;",
950 "Unimplemented ThreadLocal.get");
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200951 }
952 } else {
953 // Not special, continue with regular interpreter execution.
Ian Rogerse94652f2014-12-02 11:13:19 -0800954 artInterpreterToInterpreterBridge(self, code_item, shadow_frame, result);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200955 }
956}
957
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200958// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200959#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
960 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100961 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
962 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +0200963 const Instruction* inst, uint16_t inst_data, \
964 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200965EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
966EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
967EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
968EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
969#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200970
971// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100972#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
973 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
974 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
975 const ShadowFrame& shadow_frame, \
976 Thread* self, JValue* result)
977#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
978 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
979 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
980 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
981 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
982EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
983EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
984#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200985#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
986
987} // namespace interpreter
988} // namespace art