blob: 0e3c2de7747c105e04d3c7a03efad131e2b2d7ad [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
19#include "field_helper.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010020#include "mirror/array-inl.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020021
22namespace art {
23namespace interpreter {
24
Ian Rogers54874942014-06-10 16:31:03 -070025void ThrowNullPointerExceptionFromInterpreter(const ShadowFrame& shadow_frame) {
26 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow());
27}
28
29template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
30bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
31 uint16_t inst_data) {
32 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
33 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
34 ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
Fred Shih37f05ef2014-07-16 18:38:08 -070035 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -070036 if (UNLIKELY(f == nullptr)) {
37 CHECK(self->IsExceptionPending());
38 return false;
39 }
40 Object* obj;
41 if (is_static) {
42 obj = f->GetDeclaringClass();
43 } else {
44 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
45 if (UNLIKELY(obj == nullptr)) {
46 ThrowNullPointerExceptionForFieldAccess(shadow_frame.GetCurrentLocationForThrow(), f, true);
47 return false;
48 }
49 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +020050 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -070051 // Report this field access to instrumentation if needed.
52 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
53 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
54 Object* this_object = f->IsStatic() ? nullptr : obj;
55 instrumentation->FieldReadEvent(self, this_object, shadow_frame.GetMethod(),
56 shadow_frame.GetDexPC(), f);
57 }
58 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
59 switch (field_type) {
60 case Primitive::kPrimBoolean:
61 shadow_frame.SetVReg(vregA, f->GetBoolean(obj));
62 break;
63 case Primitive::kPrimByte:
64 shadow_frame.SetVReg(vregA, f->GetByte(obj));
65 break;
66 case Primitive::kPrimChar:
67 shadow_frame.SetVReg(vregA, f->GetChar(obj));
68 break;
69 case Primitive::kPrimShort:
70 shadow_frame.SetVReg(vregA, f->GetShort(obj));
71 break;
72 case Primitive::kPrimInt:
73 shadow_frame.SetVReg(vregA, f->GetInt(obj));
74 break;
75 case Primitive::kPrimLong:
76 shadow_frame.SetVRegLong(vregA, f->GetLong(obj));
77 break;
78 case Primitive::kPrimNot:
79 shadow_frame.SetVRegReference(vregA, f->GetObject(obj));
80 break;
81 default:
82 LOG(FATAL) << "Unreachable: " << field_type;
83 }
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
99EXPLICIT_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);
106
107// sget-XXX
108EXPLICIT_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);
115
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;
156 }
157 return true;
158}
159
160// Explicitly instantiate all DoIGetQuick functions.
161#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
162 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
163 uint16_t inst_data)
164
165EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
166EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
167EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
168#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
169
170template<Primitive::Type field_type>
171static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
172 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
173 JValue field_value;
174 switch (field_type) {
175 case Primitive::kPrimBoolean:
176 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
177 break;
178 case Primitive::kPrimByte:
179 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
180 break;
181 case Primitive::kPrimChar:
182 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
183 break;
184 case Primitive::kPrimShort:
185 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
186 break;
187 case Primitive::kPrimInt:
188 field_value.SetI(shadow_frame.GetVReg(vreg));
189 break;
190 case Primitive::kPrimLong:
191 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
192 break;
193 case Primitive::kPrimNot:
194 field_value.SetL(shadow_frame.GetVRegReference(vreg));
195 break;
196 default:
197 LOG(FATAL) << "Unreachable: " << field_type;
198 break;
199 }
200 return field_value;
201}
202
203template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
204 bool transaction_active>
205bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
206 uint16_t inst_data) {
207 bool do_assignability_check = do_access_check;
208 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
209 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
210 ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
Fred Shih37f05ef2014-07-16 18:38:08 -0700211 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -0700212 if (UNLIKELY(f == nullptr)) {
213 CHECK(self->IsExceptionPending());
214 return false;
215 }
216 Object* obj;
217 if (is_static) {
218 obj = f->GetDeclaringClass();
219 } else {
220 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
221 if (UNLIKELY(obj == nullptr)) {
222 ThrowNullPointerExceptionForFieldAccess(shadow_frame.GetCurrentLocationForThrow(),
223 f, false);
224 return false;
225 }
226 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +0200227 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -0700228 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
229 // Report this field access to instrumentation if needed. Since we only have the offset of
230 // the field from the base of the object, we need to look for it first.
231 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
232 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
233 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
234 Object* this_object = f->IsStatic() ? nullptr : obj;
235 instrumentation->FieldWriteEvent(self, this_object, shadow_frame.GetMethod(),
236 shadow_frame.GetDexPC(), f, field_value);
237 }
238 switch (field_type) {
239 case Primitive::kPrimBoolean:
240 f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA));
241 break;
242 case Primitive::kPrimByte:
243 f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA));
244 break;
245 case Primitive::kPrimChar:
246 f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA));
247 break;
248 case Primitive::kPrimShort:
249 f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA));
250 break;
251 case Primitive::kPrimInt:
252 f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA));
253 break;
254 case Primitive::kPrimLong:
255 f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA));
256 break;
257 case Primitive::kPrimNot: {
258 Object* reg = shadow_frame.GetVRegReference(vregA);
259 if (do_assignability_check && reg != nullptr) {
260 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
261 // object in the destructor.
262 Class* field_class;
263 {
264 StackHandleScope<3> hs(self);
265 HandleWrapper<mirror::ArtField> h_f(hs.NewHandleWrapper(&f));
266 HandleWrapper<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
267 HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
268 FieldHelper fh(h_f);
269 field_class = fh.GetType();
270 }
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;
288 }
289 return true;
290}
291
292// Explicitly instantiate all DoFieldPut functions.
293#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
294 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
295 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
296
297#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
298 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
299 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
300 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
301 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
302
303// iput-XXX
304EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean);
305EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte);
306EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar);
307EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort);
308EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt);
309EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong);
310EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot);
311
312// sput-XXX
313EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean);
314EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte);
315EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar);
316EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort);
317EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt);
318EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong);
319EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot);
320
321#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
322#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
323
324template<Primitive::Type field_type, bool transaction_active>
325bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
326 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
327 if (UNLIKELY(obj == nullptr)) {
328 // We lost the reference to the field index so we cannot get a more
329 // precised exception message.
330 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow());
331 return false;
332 }
333 MemberOffset field_offset(inst->VRegC_22c());
334 const uint32_t vregA = inst->VRegA_22c(inst_data);
335 // Report this field modification to instrumentation if needed. Since we only have the offset of
336 // the field from the base of the object, we need to look for it first.
337 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
338 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
339 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
340 field_offset.Uint32Value());
341 DCHECK(f != nullptr);
342 DCHECK(!f->IsStatic());
343 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
344 instrumentation->FieldWriteEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
345 shadow_frame.GetDexPC(), f, field_value);
346 }
347 // Note: iput-x-quick instructions are only for non-volatile fields.
348 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700349 case Primitive::kPrimBoolean:
350 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
351 break;
352 case Primitive::kPrimByte:
353 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
354 break;
355 case Primitive::kPrimChar:
356 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
357 break;
358 case Primitive::kPrimShort:
359 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
360 break;
Ian Rogers54874942014-06-10 16:31:03 -0700361 case Primitive::kPrimInt:
362 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
363 break;
364 case Primitive::kPrimLong:
365 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
366 break;
367 case Primitive::kPrimNot:
368 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
369 break;
370 default:
371 LOG(FATAL) << "Unreachable: " << field_type;
372 }
373 return true;
374}
375
376// Explicitly instantiate all DoIPutQuick functions.
377#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
378 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
379 const Instruction* inst, \
380 uint16_t inst_data)
381
382#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
383 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
384 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
385
Fred Shih37f05ef2014-07-16 18:38:08 -0700386EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt); // iput-quick.
387EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean); // iput-boolean-quick.
388EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte); // iput-byte-quick.
389EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar); // iput-char-quick.
390EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort); // iput-short-quick.
391EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong); // iput-wide-quick.
392EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot); // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700393#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
394#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
395
Sebastien Hertz9f102032014-05-23 08:59:42 +0200396/**
397 * Finds the location where this exception will be caught. We search until we reach either the top
398 * frame or a native frame, in which cases this exception is considered uncaught.
399 */
400class CatchLocationFinder : public StackVisitor {
401 public:
402 explicit CatchLocationFinder(Thread* self, Handle<mirror::Throwable>* exception)
403 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
404 : StackVisitor(self, nullptr), self_(self), handle_scope_(self), exception_(exception),
405 catch_method_(handle_scope_.NewHandle<mirror::ArtMethod>(nullptr)),
406 catch_dex_pc_(DexFile::kDexNoIndex), clear_exception_(false) {
407 }
408
409 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
410 mirror::ArtMethod* method = GetMethod();
411 if (method == nullptr) {
412 return true;
413 }
414 if (method->IsRuntimeMethod()) {
415 // Ignore callee save method.
416 DCHECK(method->IsCalleeSaveMethod());
417 return true;
418 }
419 if (method->IsNative()) {
420 return false; // End stack walk.
421 }
422 DCHECK(!method->IsNative());
423 uint32_t dex_pc = GetDexPc();
424 if (dex_pc != DexFile::kDexNoIndex) {
425 uint32_t found_dex_pc;
426 {
427 StackHandleScope<3> hs(self_);
428 Handle<mirror::Class> exception_class(hs.NewHandle((*exception_)->GetClass()));
429 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
430 found_dex_pc = mirror::ArtMethod::FindCatchBlock(h_method, exception_class, dex_pc,
431 &clear_exception_);
432 }
433 if (found_dex_pc != DexFile::kDexNoIndex) {
434 catch_method_.Assign(method);
435 catch_dex_pc_ = found_dex_pc;
436 return false; // End stack walk.
437 }
438 }
439 return true; // Continue stack walk.
440 }
441
442 ArtMethod* GetCatchMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
443 return catch_method_.Get();
444 }
445
446 uint32_t GetCatchDexPc() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
447 return catch_dex_pc_;
448 }
449
450 bool NeedClearException() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
451 return clear_exception_;
452 }
453
454 private:
455 Thread* const self_;
456 StackHandleScope<1> handle_scope_;
457 Handle<mirror::Throwable>* exception_;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700458 MutableHandle<mirror::ArtMethod> catch_method_;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200459 uint32_t catch_dex_pc_;
460 bool clear_exception_;
461
462
463 DISALLOW_COPY_AND_ASSIGN(CatchLocationFinder);
464};
465
Ian Rogers54874942014-06-10 16:31:03 -0700466uint32_t FindNextInstructionFollowingException(Thread* self,
467 ShadowFrame& shadow_frame,
468 uint32_t dex_pc,
Ian Rogers54874942014-06-10 16:31:03 -0700469 const instrumentation::Instrumentation* instrumentation) {
470 self->VerifyStack();
471 ThrowLocation throw_location;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200472 StackHandleScope<3> hs(self);
473 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException(&throw_location)));
474 if (!self->IsExceptionReportedToInstrumentation() && instrumentation->HasExceptionCaughtListeners()) {
475 CatchLocationFinder clf(self, &exception);
476 clf.WalkStack(false);
477 instrumentation->ExceptionCaughtEvent(self, throw_location, clf.GetCatchMethod(),
478 clf.GetCatchDexPc(), exception.Get());
479 self->SetExceptionReportedToInstrumentation(true);
480 }
Ian Rogers54874942014-06-10 16:31:03 -0700481 bool clear_exception = false;
482 uint32_t found_dex_pc;
483 {
Ian Rogers54874942014-06-10 16:31:03 -0700484 Handle<mirror::Class> exception_class(hs.NewHandle(exception->GetClass()));
485 Handle<mirror::ArtMethod> h_method(hs.NewHandle(shadow_frame.GetMethod()));
Ian Rogers54874942014-06-10 16:31:03 -0700486 found_dex_pc = mirror::ArtMethod::FindCatchBlock(h_method, exception_class, dex_pc,
487 &clear_exception);
488 }
489 if (found_dex_pc == DexFile::kDexNoIndex) {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200490 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
Ian Rogers54874942014-06-10 16:31:03 -0700491 shadow_frame.GetMethod(), dex_pc);
492 } else {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200493 if (self->IsExceptionReportedToInstrumentation()) {
494 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
495 shadow_frame.GetMethod(), dex_pc);
496 }
Ian Rogers54874942014-06-10 16:31:03 -0700497 if (clear_exception) {
498 self->ClearException();
499 }
500 }
501 return found_dex_pc;
502}
503
504void UnexpectedOpcode(const Instruction* inst, MethodHelper& mh) {
505 LOG(FATAL) << "Unexpected instruction: " << inst->DumpString(mh.GetMethod()->GetDexFile());
506 exit(0); // Unreachable, keep GCC happy.
507}
508
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200509static void UnstartedRuntimeInvoke(Thread* self, MethodHelper& mh,
510 const DexFile::CodeItem* code_item, ShadowFrame* shadow_frame,
511 JValue* result, size_t arg_offset)
512 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200513
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200514// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800515static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
516 size_t dest_reg, size_t src_reg)
517 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200518 // If both register locations contains the same value, the register probably holds a reference.
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700519 // Uint required, so that sign extension does not make this wrong on 64b systems
520 uint32_t src_value = shadow_frame.GetVReg(src_reg);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800521 mirror::Object* o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700522 if (src_value == reinterpret_cast<uintptr_t>(o)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800523 new_shadow_frame->SetVRegReference(dest_reg, o);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200524 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800525 new_shadow_frame->SetVReg(dest_reg, src_value);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200526 }
527}
528
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700529void AbortTransaction(Thread* self, const char* fmt, ...) {
530 CHECK(Runtime::Current()->IsActiveTransaction());
531 // Throw an exception so we can abort the transaction and undo every change.
532 va_list args;
533 va_start(args, fmt);
534 self->ThrowNewExceptionV(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;", fmt,
535 args);
536 va_end(args);
537}
538
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200539template<bool is_range, bool do_assignability_check>
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100540bool DoCall(ArtMethod* method, Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200541 const Instruction* inst, uint16_t inst_data, JValue* result) {
542 // Compute method information.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700543 const DexFile::CodeItem* code_item = method->GetCodeItem();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200544 const uint16_t num_ins = (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200545 uint16_t num_regs;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200546 if (LIKELY(code_item != NULL)) {
547 num_regs = code_item->registers_size_;
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200548 DCHECK_EQ(num_ins, code_item->ins_size_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200549 } else {
550 DCHECK(method->IsNative() || method->IsProxyMethod());
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200551 num_regs = num_ins;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200552 }
553
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200554 // Allocate shadow frame on the stack.
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700555 const char* old_cause = self->StartAssertNoThreadSuspension("DoCall");
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200556 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
557 ShadowFrame* new_shadow_frame(ShadowFrame::Create(num_regs, &shadow_frame, method, 0, memory));
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200558
559 // Initialize new shadow frame.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200560 const size_t first_dest_reg = num_regs - num_ins;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700561 StackHandleScope<1> hs(self);
562 MethodHelper mh(hs.NewHandle(method));
Jeff Haoa3faaf42013-09-03 19:07:00 -0700563 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700564 // Slow path.
565 // We might need to do class loading, which incurs a thread state change to kNative. So
566 // register the shadow frame as under construction and allow suspension again.
567 self->SetShadowFrameUnderConstruction(new_shadow_frame);
568 self->EndAssertNoThreadSuspension(old_cause);
569
570 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200571 // to get the exact type of each reference argument.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700572 const DexFile::TypeList* params = method->GetParameterTypeList();
573 uint32_t shorty_len = 0;
574 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200575
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200576 // TODO: find a cleaner way to separate non-range and range information without duplicating code.
577 uint32_t arg[5]; // only used in invoke-XXX.
578 uint32_t vregC; // only used in invoke-XXX-range.
579 if (is_range) {
580 vregC = inst->VRegC_3rc();
581 } else {
Ian Rogers29a26482014-05-02 15:27:29 -0700582 inst->GetVarArgs(arg, inst_data);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200583 }
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100584
585 // Handle receiver apart since it's not part of the shorty.
586 size_t dest_reg = first_dest_reg;
587 size_t arg_offset = 0;
588 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700589 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100590 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
591 ++dest_reg;
592 ++arg_offset;
593 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800594 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700595 DCHECK_LT(shorty_pos + 1, shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200596 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
597 switch (shorty[shorty_pos + 1]) {
598 case 'L': {
599 Object* o = shadow_frame.GetVRegReference(src_reg);
600 if (do_assignability_check && o != NULL) {
601 Class* arg_type = mh.GetClassFromTypeIdx(params->GetTypeItem(shorty_pos).type_idx_);
602 if (arg_type == NULL) {
603 CHECK(self->IsExceptionPending());
604 return false;
605 }
606 if (!o->VerifierInstanceOf(arg_type)) {
607 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700608 std::string temp1, temp2;
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200609 self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(),
610 "Ljava/lang/VirtualMachineError;",
611 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700612 method->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700613 o->GetClass()->GetDescriptor(&temp1),
614 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200615 return false;
616 }
Jeff Haoa3faaf42013-09-03 19:07:00 -0700617 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200618 new_shadow_frame->SetVRegReference(dest_reg, o);
619 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700620 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200621 case 'J': case 'D': {
622 uint64_t wide_value = (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << 32) |
623 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
624 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
625 ++dest_reg;
626 ++arg_offset;
627 break;
628 }
629 default:
630 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
631 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200632 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200633 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700634 // We're done with the construction.
635 self->ClearShadowFrameUnderConstruction();
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200636 } else {
637 // Fast path: no extra checks.
638 if (is_range) {
639 const uint16_t first_src_reg = inst->VRegC_3rc();
640 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs;
641 ++dest_reg, ++src_reg) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800642 AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200643 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200644 } else {
645 DCHECK_LE(num_ins, 5U);
646 uint16_t regList = inst->Fetch16(2);
647 uint16_t count = num_ins;
648 if (count == 5) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800649 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + 4U, (inst_data >> 8) & 0x0f);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200650 --count;
651 }
652 for (size_t arg_index = 0; arg_index < count; ++arg_index, regList >>= 4) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800653 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, regList & 0x0f);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200654 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200655 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700656 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200657 }
658
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200659 // Do the call now.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200660 if (LIKELY(Runtime::Current()->IsStarted())) {
Ian Rogers1d99e452014-01-02 17:36:41 -0800661 if (kIsDebugBuild && method->GetEntryPointFromInterpreter() == nullptr) {
662 LOG(FATAL) << "Attempt to invoke non-executable method: " << PrettyMethod(method);
663 }
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800664 if (kIsDebugBuild && Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly() &&
665 !method->IsNative() && !method->IsProxyMethod() &&
666 method->GetEntryPointFromInterpreter() == artInterpreterToCompiledCodeBridge) {
667 LOG(FATAL) << "Attempt to call compiled code when -Xint: " << PrettyMethod(method);
668 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200669 (method->GetEntryPointFromInterpreter())(self, mh, code_item, new_shadow_frame, result);
670 } else {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200671 UnstartedRuntimeInvoke(self, mh, code_item, new_shadow_frame, result, first_dest_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200672 }
673 return !self->IsExceptionPending();
674}
675
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100676template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200677bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
678 Thread* self, JValue* result) {
679 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
680 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
681 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
682 if (!is_range) {
683 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
684 CHECK_LE(length, 5);
685 }
686 if (UNLIKELY(length < 0)) {
687 ThrowNegativeArraySizeException(length);
688 return false;
689 }
690 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
691 Class* arrayClass = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(),
692 self, false, do_access_check);
693 if (UNLIKELY(arrayClass == NULL)) {
694 DCHECK(self->IsExceptionPending());
695 return false;
696 }
697 CHECK(arrayClass->IsArrayClass());
698 Class* componentClass = arrayClass->GetComponentType();
699 if (UNLIKELY(componentClass->IsPrimitive() && !componentClass->IsPrimitiveInt())) {
700 if (componentClass->IsPrimitiveLong() || componentClass->IsPrimitiveDouble()) {
701 ThrowRuntimeException("Bad filled array request for type %s",
702 PrettyDescriptor(componentClass).c_str());
703 } else {
704 self->ThrowNewExceptionF(shadow_frame.GetCurrentLocationForThrow(),
705 "Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800706 "Found type %s; filled-new-array not implemented for anything but 'int'",
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200707 PrettyDescriptor(componentClass).c_str());
708 }
709 return false;
710 }
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700711 Object* newArray = Array::Alloc<true>(self, arrayClass, length,
712 arrayClass->GetComponentSizeShift(),
Ian Rogers6fac4472014-02-25 17:01:10 -0800713 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200714 if (UNLIKELY(newArray == NULL)) {
715 DCHECK(self->IsExceptionPending());
716 return false;
717 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100718 uint32_t arg[5]; // only used in filled-new-array.
719 uint32_t vregC; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200720 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100721 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200722 } else {
Ian Rogers29a26482014-05-02 15:27:29 -0700723 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100724 }
725 const bool is_primitive_int_component = componentClass->IsPrimitiveInt();
726 for (int32_t i = 0; i < length; ++i) {
727 size_t src_reg = is_range ? vregC + i : arg[i];
728 if (is_primitive_int_component) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100729 newArray->AsIntArray()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +0100730 } else {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100731 newArray->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200732 }
733 }
734
735 result->SetL(newArray);
736 return true;
737}
738
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100739// TODO fix thread analysis: should be SHARED_LOCKS_REQUIRED(Locks::mutator_lock_).
740template<typename T>
741static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count)
742 NO_THREAD_SAFETY_ANALYSIS {
743 Runtime* runtime = Runtime::Current();
744 for (int32_t i = 0; i < count; ++i) {
745 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
746 }
747}
748
749void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
750 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
751 DCHECK(Runtime::Current()->IsActiveTransaction());
752 DCHECK(array != nullptr);
753 DCHECK_LE(count, array->GetLength());
754 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
755 switch (primitive_component_type) {
756 case Primitive::kPrimBoolean:
757 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
758 break;
759 case Primitive::kPrimByte:
760 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
761 break;
762 case Primitive::kPrimChar:
763 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
764 break;
765 case Primitive::kPrimShort:
766 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
767 break;
768 case Primitive::kPrimInt:
769 case Primitive::kPrimFloat:
770 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
771 break;
772 case Primitive::kPrimLong:
773 case Primitive::kPrimDouble:
774 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
775 break;
776 default:
777 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
778 << " in fill-array-data";
779 break;
780 }
781}
782
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200783// Helper function to deal with class loading in an unstarted runtime.
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700784static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className,
785 Handle<mirror::ClassLoader> class_loader, JValue* result,
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200786 const std::string& method_name, bool initialize_class,
787 bool abort_if_not_found)
788 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
789 CHECK(className.Get() != nullptr);
790 std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str()));
791 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
792
793 Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader);
794 if (found == nullptr && abort_if_not_found) {
795 if (!self->IsExceptionPending()) {
796 AbortTransaction(self, "%s failed in un-started runtime for class: %s",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700797 method_name.c_str(), PrettyDescriptor(descriptor.c_str()).c_str());
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200798 }
799 return;
800 }
801 if (found != nullptr && initialize_class) {
802 StackHandleScope<1> hs(self);
803 Handle<mirror::Class> h_class(hs.NewHandle(found));
Ian Rogers7b078e82014-09-10 14:44:24 -0700804 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200805 CHECK(self->IsExceptionPending());
806 return;
807 }
808 }
809 result->SetL(found);
810}
811
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200812static void UnstartedRuntimeInvoke(Thread* self, MethodHelper& mh,
813 const DexFile::CodeItem* code_item, ShadowFrame* shadow_frame,
814 JValue* result, size_t arg_offset) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200815 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
816 // problems in core libraries.
817 std::string name(PrettyMethod(shadow_frame->GetMethod()));
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200818 if (name == "java.lang.Class java.lang.Class.forName(java.lang.String)") {
819 // TODO: Support for the other variants that take more arguments should also be added.
820 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset)->AsString();
821 StackHandleScope<1> hs(self);
822 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
823 UnstartedRuntimeFindClass(self, h_class_name, NullHandle<mirror::ClassLoader>(), result, name,
824 true, true);
825 } else if (name == "java.lang.Class java.lang.VMClassLoader.loadClass(java.lang.String, boolean)") {
826 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset)->AsString();
827 StackHandleScope<1> hs(self);
828 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
829 UnstartedRuntimeFindClass(self, h_class_name, NullHandle<mirror::ClassLoader>(), result, name,
830 false, true);
831 } else if (name == "java.lang.Class java.lang.VMClassLoader.findLoadedClass(java.lang.ClassLoader, java.lang.String)") {
832 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
833 mirror::ClassLoader* class_loader =
834 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
835 StackHandleScope<2> hs(self);
836 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
837 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
838 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, name, false, false);
Ian Rogersc45b8b52014-05-03 01:39:59 -0700839 } else if (name == "java.lang.Class java.lang.Void.lookupType()") {
840 result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V'));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200841 } else if (name == "java.lang.Object java.lang.Class.newInstance()") {
842 Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
843 ArtMethod* c = klass->FindDeclaredDirectMethod("<init>", "()V");
844 CHECK(c != NULL);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700845 StackHandleScope<1> hs(self);
846 Handle<Object> obj(hs.NewHandle(klass->AllocObject(self)));
847 CHECK(obj.Get() != NULL);
848 EnterInterpreterFromInvoke(self, c, obj.Get(), NULL, NULL);
849 result->SetL(obj.Get());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200850 } else if (name == "java.lang.reflect.Field java.lang.Class.getDeclaredField(java.lang.String)") {
851 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
852 // going the reflective Dex way.
853 Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
854 String* name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
855 ArtField* found = NULL;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200856 ObjectArray<ArtField>* fields = klass->GetIFields();
857 for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) {
858 ArtField* f = fields->Get(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700859 if (name->Equals(f->GetName())) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200860 found = f;
861 }
862 }
863 if (found == NULL) {
864 fields = klass->GetSFields();
865 for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) {
866 ArtField* f = fields->Get(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700867 if (name->Equals(f->GetName())) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200868 found = f;
869 }
870 }
871 }
872 CHECK(found != NULL)
873 << "Failed to find field in Class.getDeclaredField in un-started runtime. name="
874 << name->ToModifiedUtf8() << " class=" << PrettyDescriptor(klass);
875 // TODO: getDeclaredField calls GetType once the field is found to ensure a
876 // NoClassDefFoundError is thrown if the field's type cannot be resolved.
877 Class* jlr_Field = self->DecodeJObject(WellKnownClasses::java_lang_reflect_Field)->AsClass();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700878 StackHandleScope<1> hs(self);
879 Handle<Object> field(hs.NewHandle(jlr_Field->AllocNonMovableObject(self)));
880 CHECK(field.Get() != NULL);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200881 ArtMethod* c = jlr_Field->FindDeclaredDirectMethod("<init>", "(Ljava/lang/reflect/ArtField;)V");
882 uint32_t args[1];
Ian Rogersef7d42f2014-01-06 12:55:46 -0800883 args[0] = StackReference<mirror::Object>::FromMirrorPtr(found).AsVRegValue();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700884 EnterInterpreterFromInvoke(self, c, field.Get(), args, NULL);
885 result->SetL(field.Get());
Ian Rogersc45b8b52014-05-03 01:39:59 -0700886 } else if (name == "int java.lang.Object.hashCode()") {
887 Object* obj = shadow_frame->GetVRegReference(arg_offset);
888 result->SetI(obj->IdentityHashCode());
889 } else if (name == "java.lang.String java.lang.reflect.ArtMethod.getMethodName(java.lang.reflect.ArtMethod)") {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700890 StackHandleScope<1> hs(self);
891 MethodHelper mh(hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsArtMethod()));
892 result->SetL(mh.GetNameAsString(self));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200893 } else if (name == "void java.lang.System.arraycopy(java.lang.Object, int, java.lang.Object, int, int)" ||
894 name == "void java.lang.System.arraycopy(char[], int, char[], int, int)") {
895 // Special case array copying without initializing System.
896 Class* ctype = shadow_frame->GetVRegReference(arg_offset)->GetClass()->GetComponentType();
897 jint srcPos = shadow_frame->GetVReg(arg_offset + 1);
898 jint dstPos = shadow_frame->GetVReg(arg_offset + 3);
899 jint length = shadow_frame->GetVReg(arg_offset + 4);
900 if (!ctype->IsPrimitive()) {
901 ObjectArray<Object>* src = shadow_frame->GetVRegReference(arg_offset)->AsObjectArray<Object>();
902 ObjectArray<Object>* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<Object>();
903 for (jint i = 0; i < length; ++i) {
904 dst->Set(dstPos + i, src->Get(srcPos + i));
905 }
906 } else if (ctype->IsPrimitiveChar()) {
907 CharArray* src = shadow_frame->GetVRegReference(arg_offset)->AsCharArray();
908 CharArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray();
909 for (jint i = 0; i < length; ++i) {
910 dst->Set(dstPos + i, src->Get(srcPos + i));
911 }
912 } else if (ctype->IsPrimitiveInt()) {
913 IntArray* src = shadow_frame->GetVRegReference(arg_offset)->AsIntArray();
914 IntArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsIntArray();
915 for (jint i = 0; i < length; ++i) {
916 dst->Set(dstPos + i, src->Get(srcPos + i));
917 }
918 } else {
Ian Rogersc45b8b52014-05-03 01:39:59 -0700919 self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;",
920 "Unimplemented System.arraycopy for type '%s'",
921 PrettyDescriptor(ctype).c_str());
922 }
923 } else if (name == "java.lang.Object java.lang.ThreadLocal.get()") {
924 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
925 if (caller == "java.lang.String java.lang.IntegralToString.convertInt(java.lang.AbstractStringBuilder, int)") {
926 // Allocate non-threadlocal buffer.
927 result->SetL(mirror::CharArray::Alloc(self, 11));
928 } else {
929 self->ThrowNewException(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;",
930 "Unimplemented ThreadLocal.get");
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200931 }
932 } else {
933 // Not special, continue with regular interpreter execution.
934 artInterpreterToInterpreterBridge(self, mh, code_item, shadow_frame, result);
935 }
936}
937
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200938// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200939#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
940 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100941 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
942 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +0200943 const Instruction* inst, uint16_t inst_data, \
944 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200945EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
946EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
947EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
948EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
949#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200950
951// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100952#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
953 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
954 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
955 const ShadowFrame& shadow_frame, \
956 Thread* self, JValue* result)
957#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
958 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
959 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
960 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
961 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
962EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
963EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
964#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200965#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
966
967} // namespace interpreter
968} // namespace art