blob: 5a03601bce6d0465ac04781961f52ac8d0ab1e76 [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"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010018#include "mirror/array-inl.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020019
20namespace art {
21namespace interpreter {
22
Ian Rogers54874942014-06-10 16:31:03 -070023void ThrowNullPointerExceptionFromInterpreter(const ShadowFrame& shadow_frame) {
24 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow());
25}
26
27template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
28bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
29 uint16_t inst_data) {
30 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
31 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
32 ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
33 Primitive::FieldSize(field_type));
34 if (UNLIKELY(f == nullptr)) {
35 CHECK(self->IsExceptionPending());
36 return false;
37 }
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +020038 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -070039 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 }
49 // Report this field access to instrumentation if needed.
50 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
51 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
52 Object* this_object = f->IsStatic() ? nullptr : obj;
53 instrumentation->FieldReadEvent(self, this_object, shadow_frame.GetMethod(),
54 shadow_frame.GetDexPC(), f);
55 }
56 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
57 switch (field_type) {
58 case Primitive::kPrimBoolean:
59 shadow_frame.SetVReg(vregA, f->GetBoolean(obj));
60 break;
61 case Primitive::kPrimByte:
62 shadow_frame.SetVReg(vregA, f->GetByte(obj));
63 break;
64 case Primitive::kPrimChar:
65 shadow_frame.SetVReg(vregA, f->GetChar(obj));
66 break;
67 case Primitive::kPrimShort:
68 shadow_frame.SetVReg(vregA, f->GetShort(obj));
69 break;
70 case Primitive::kPrimInt:
71 shadow_frame.SetVReg(vregA, f->GetInt(obj));
72 break;
73 case Primitive::kPrimLong:
74 shadow_frame.SetVRegLong(vregA, f->GetLong(obj));
75 break;
76 case Primitive::kPrimNot:
77 shadow_frame.SetVRegReference(vregA, f->GetObject(obj));
78 break;
79 default:
80 LOG(FATAL) << "Unreachable: " << field_type;
81 }
82 return true;
83}
84
85// Explicitly instantiate all DoFieldGet functions.
86#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
87 template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \
88 ShadowFrame& shadow_frame, \
89 const Instruction* inst, \
90 uint16_t inst_data)
91
92#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
93 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \
94 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true);
95
96// iget-XXX
97EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean);
98EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte);
99EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar);
100EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort);
101EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt);
102EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong);
103EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot);
104
105// sget-XXX
106EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean);
107EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte);
108EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar);
109EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort);
110EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt);
111EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong);
112EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot);
113
114#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
115#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
116
117// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
118// Returns true on success, otherwise throws an exception and returns false.
119template<Primitive::Type field_type>
120bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
121 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
122 if (UNLIKELY(obj == nullptr)) {
123 // We lost the reference to the field index so we cannot get a more
124 // precised exception message.
125 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow());
126 return false;
127 }
128 MemberOffset field_offset(inst->VRegC_22c());
129 // Report this field access to instrumentation if needed. Since we only have the offset of
130 // the field from the base of the object, we need to look for it first.
131 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
132 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
133 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
134 field_offset.Uint32Value());
135 DCHECK(f != nullptr);
136 DCHECK(!f->IsStatic());
137 instrumentation->FieldReadEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
138 shadow_frame.GetDexPC(), f);
139 }
140 // Note: iget-x-quick instructions are only for non-volatile fields.
141 const uint32_t vregA = inst->VRegA_22c(inst_data);
142 switch (field_type) {
143 case Primitive::kPrimInt:
144 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
145 break;
146 case Primitive::kPrimLong:
147 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
148 break;
149 case Primitive::kPrimNot:
150 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
151 break;
152 default:
153 LOG(FATAL) << "Unreachable: " << field_type;
154 }
155 return true;
156}
157
158// Explicitly instantiate all DoIGetQuick functions.
159#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
160 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
161 uint16_t inst_data)
162
163EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
164EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
165EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
166#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
167
168template<Primitive::Type field_type>
169static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
170 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
171 JValue field_value;
172 switch (field_type) {
173 case Primitive::kPrimBoolean:
174 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
175 break;
176 case Primitive::kPrimByte:
177 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
178 break;
179 case Primitive::kPrimChar:
180 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
181 break;
182 case Primitive::kPrimShort:
183 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
184 break;
185 case Primitive::kPrimInt:
186 field_value.SetI(shadow_frame.GetVReg(vreg));
187 break;
188 case Primitive::kPrimLong:
189 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
190 break;
191 case Primitive::kPrimNot:
192 field_value.SetL(shadow_frame.GetVRegReference(vreg));
193 break;
194 default:
195 LOG(FATAL) << "Unreachable: " << field_type;
196 break;
197 }
198 return field_value;
199}
200
201template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
202 bool transaction_active>
203bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
204 uint16_t inst_data) {
205 bool do_assignability_check = do_access_check;
206 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
207 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
208 ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
209 Primitive::FieldSize(field_type));
210 if (UNLIKELY(f == nullptr)) {
211 CHECK(self->IsExceptionPending());
212 return false;
213 }
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200214 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -0700215 Object* obj;
216 if (is_static) {
217 obj = f->GetDeclaringClass();
218 } else {
219 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
220 if (UNLIKELY(obj == nullptr)) {
221 ThrowNullPointerExceptionForFieldAccess(shadow_frame.GetCurrentLocationForThrow(),
222 f, false);
223 return false;
224 }
225 }
226 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
227 // Report this field access to instrumentation if needed. Since we only have the offset of
228 // the field from the base of the object, we need to look for it first.
229 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
230 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
231 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
232 Object* this_object = f->IsStatic() ? nullptr : obj;
233 instrumentation->FieldWriteEvent(self, this_object, shadow_frame.GetMethod(),
234 shadow_frame.GetDexPC(), f, field_value);
235 }
236 switch (field_type) {
237 case Primitive::kPrimBoolean:
238 f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA));
239 break;
240 case Primitive::kPrimByte:
241 f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA));
242 break;
243 case Primitive::kPrimChar:
244 f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA));
245 break;
246 case Primitive::kPrimShort:
247 f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA));
248 break;
249 case Primitive::kPrimInt:
250 f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA));
251 break;
252 case Primitive::kPrimLong:
253 f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA));
254 break;
255 case Primitive::kPrimNot: {
256 Object* reg = shadow_frame.GetVRegReference(vregA);
257 if (do_assignability_check && reg != nullptr) {
258 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
259 // object in the destructor.
260 Class* field_class;
261 {
262 StackHandleScope<3> hs(self);
263 HandleWrapper<mirror::ArtField> h_f(hs.NewHandleWrapper(&f));
264 HandleWrapper<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
265 HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
266 FieldHelper fh(h_f);
267 field_class = fh.GetType();
268 }
269 if (!reg->VerifierInstanceOf(field_class)) {
270 // This should never happen.
271 self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(),
272 "Ljava/lang/VirtualMachineError;",
273 "Put '%s' that is not instance of field '%s' in '%s'",
274 reg->GetClass()->GetDescriptor().c_str(),
275 field_class->GetDescriptor().c_str(),
276 f->GetDeclaringClass()->GetDescriptor().c_str());
277 return false;
278 }
279 }
280 f->SetObj<transaction_active>(obj, reg);
281 break;
282 }
283 default:
284 LOG(FATAL) << "Unreachable: " << field_type;
285 }
286 return true;
287}
288
289// Explicitly instantiate all DoFieldPut functions.
290#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
291 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
292 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
293
294#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
295 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
296 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
297 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
298 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
299
300// iput-XXX
301EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean);
302EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte);
303EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar);
304EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort);
305EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt);
306EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong);
307EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot);
308
309// sput-XXX
310EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean);
311EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte);
312EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar);
313EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort);
314EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt);
315EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong);
316EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot);
317
318#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
319#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
320
321template<Primitive::Type field_type, bool transaction_active>
322bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
323 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
324 if (UNLIKELY(obj == nullptr)) {
325 // We lost the reference to the field index so we cannot get a more
326 // precised exception message.
327 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow());
328 return false;
329 }
330 MemberOffset field_offset(inst->VRegC_22c());
331 const uint32_t vregA = inst->VRegA_22c(inst_data);
332 // Report this field modification to instrumentation if needed. Since we only have the offset of
333 // the field from the base of the object, we need to look for it first.
334 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
335 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
336 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
337 field_offset.Uint32Value());
338 DCHECK(f != nullptr);
339 DCHECK(!f->IsStatic());
340 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
341 instrumentation->FieldWriteEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
342 shadow_frame.GetDexPC(), f, field_value);
343 }
344 // Note: iput-x-quick instructions are only for non-volatile fields.
345 switch (field_type) {
346 case Primitive::kPrimInt:
347 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
348 break;
349 case Primitive::kPrimLong:
350 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
351 break;
352 case Primitive::kPrimNot:
353 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
354 break;
355 default:
356 LOG(FATAL) << "Unreachable: " << field_type;
357 }
358 return true;
359}
360
361// Explicitly instantiate all DoIPutQuick functions.
362#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
363 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
364 const Instruction* inst, \
365 uint16_t inst_data)
366
367#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
368 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
369 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
370
371EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
372EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
373EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
374#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
375#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
376
Sebastien Hertz9f102032014-05-23 08:59:42 +0200377/**
378 * Finds the location where this exception will be caught. We search until we reach either the top
379 * frame or a native frame, in which cases this exception is considered uncaught.
380 */
381class CatchLocationFinder : public StackVisitor {
382 public:
383 explicit CatchLocationFinder(Thread* self, Handle<mirror::Throwable>* exception)
384 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
385 : StackVisitor(self, nullptr), self_(self), handle_scope_(self), exception_(exception),
386 catch_method_(handle_scope_.NewHandle<mirror::ArtMethod>(nullptr)),
387 catch_dex_pc_(DexFile::kDexNoIndex), clear_exception_(false) {
388 }
389
390 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
391 mirror::ArtMethod* method = GetMethod();
392 if (method == nullptr) {
393 return true;
394 }
395 if (method->IsRuntimeMethod()) {
396 // Ignore callee save method.
397 DCHECK(method->IsCalleeSaveMethod());
398 return true;
399 }
400 if (method->IsNative()) {
401 return false; // End stack walk.
402 }
403 DCHECK(!method->IsNative());
404 uint32_t dex_pc = GetDexPc();
405 if (dex_pc != DexFile::kDexNoIndex) {
406 uint32_t found_dex_pc;
407 {
408 StackHandleScope<3> hs(self_);
409 Handle<mirror::Class> exception_class(hs.NewHandle((*exception_)->GetClass()));
410 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
411 found_dex_pc = mirror::ArtMethod::FindCatchBlock(h_method, exception_class, dex_pc,
412 &clear_exception_);
413 }
414 if (found_dex_pc != DexFile::kDexNoIndex) {
415 catch_method_.Assign(method);
416 catch_dex_pc_ = found_dex_pc;
417 return false; // End stack walk.
418 }
419 }
420 return true; // Continue stack walk.
421 }
422
423 ArtMethod* GetCatchMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
424 return catch_method_.Get();
425 }
426
427 uint32_t GetCatchDexPc() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
428 return catch_dex_pc_;
429 }
430
431 bool NeedClearException() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
432 return clear_exception_;
433 }
434
435 private:
436 Thread* const self_;
437 StackHandleScope<1> handle_scope_;
438 Handle<mirror::Throwable>* exception_;
439 Handle<mirror::ArtMethod> catch_method_;
440 uint32_t catch_dex_pc_;
441 bool clear_exception_;
442
443
444 DISALLOW_COPY_AND_ASSIGN(CatchLocationFinder);
445};
446
Ian Rogers54874942014-06-10 16:31:03 -0700447uint32_t FindNextInstructionFollowingException(Thread* self,
448 ShadowFrame& shadow_frame,
449 uint32_t dex_pc,
Ian Rogers54874942014-06-10 16:31:03 -0700450 const instrumentation::Instrumentation* instrumentation) {
451 self->VerifyStack();
452 ThrowLocation throw_location;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200453 StackHandleScope<3> hs(self);
454 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException(&throw_location)));
455 if (!self->IsExceptionReportedToInstrumentation() && instrumentation->HasExceptionCaughtListeners()) {
456 CatchLocationFinder clf(self, &exception);
457 clf.WalkStack(false);
458 instrumentation->ExceptionCaughtEvent(self, throw_location, clf.GetCatchMethod(),
459 clf.GetCatchDexPc(), exception.Get());
460 self->SetExceptionReportedToInstrumentation(true);
461 }
Ian Rogers54874942014-06-10 16:31:03 -0700462 bool clear_exception = false;
463 uint32_t found_dex_pc;
464 {
Ian Rogers54874942014-06-10 16:31:03 -0700465 Handle<mirror::Class> exception_class(hs.NewHandle(exception->GetClass()));
466 Handle<mirror::ArtMethod> h_method(hs.NewHandle(shadow_frame.GetMethod()));
Ian Rogers54874942014-06-10 16:31:03 -0700467 found_dex_pc = mirror::ArtMethod::FindCatchBlock(h_method, exception_class, dex_pc,
468 &clear_exception);
469 }
470 if (found_dex_pc == DexFile::kDexNoIndex) {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200471 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
Ian Rogers54874942014-06-10 16:31:03 -0700472 shadow_frame.GetMethod(), dex_pc);
473 } else {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200474 if (self->IsExceptionReportedToInstrumentation()) {
475 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
476 shadow_frame.GetMethod(), dex_pc);
477 }
Ian Rogers54874942014-06-10 16:31:03 -0700478 if (clear_exception) {
479 self->ClearException();
480 }
481 }
482 return found_dex_pc;
483}
484
485void UnexpectedOpcode(const Instruction* inst, MethodHelper& mh) {
486 LOG(FATAL) << "Unexpected instruction: " << inst->DumpString(mh.GetMethod()->GetDexFile());
487 exit(0); // Unreachable, keep GCC happy.
488}
489
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200490static void UnstartedRuntimeInvoke(Thread* self, MethodHelper& mh,
491 const DexFile::CodeItem* code_item, ShadowFrame* shadow_frame,
492 JValue* result, size_t arg_offset)
493 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200494
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200495// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800496static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
497 size_t dest_reg, size_t src_reg)
498 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200499 // If both register locations contains the same value, the register probably holds a reference.
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700500 // Uint required, so that sign extension does not make this wrong on 64b systems
501 uint32_t src_value = shadow_frame.GetVReg(src_reg);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800502 mirror::Object* o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700503 if (src_value == reinterpret_cast<uintptr_t>(o)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800504 new_shadow_frame->SetVRegReference(dest_reg, o);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200505 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800506 new_shadow_frame->SetVReg(dest_reg, src_value);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200507 }
508}
509
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700510void AbortTransaction(Thread* self, const char* fmt, ...) {
511 CHECK(Runtime::Current()->IsActiveTransaction());
512 // Throw an exception so we can abort the transaction and undo every change.
513 va_list args;
514 va_start(args, fmt);
515 self->ThrowNewExceptionV(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;", fmt,
516 args);
517 va_end(args);
518}
519
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200520template<bool is_range, bool do_assignability_check>
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100521bool DoCall(ArtMethod* method, Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200522 const Instruction* inst, uint16_t inst_data, JValue* result) {
523 // Compute method information.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700524 const DexFile::CodeItem* code_item = method->GetCodeItem();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200525 const uint16_t num_ins = (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200526 uint16_t num_regs;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200527 if (LIKELY(code_item != NULL)) {
528 num_regs = code_item->registers_size_;
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200529 DCHECK_EQ(num_ins, code_item->ins_size_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200530 } else {
531 DCHECK(method->IsNative() || method->IsProxyMethod());
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200532 num_regs = num_ins;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200533 }
534
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200535 // Allocate shadow frame on the stack.
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700536 const char* old_cause = self->StartAssertNoThreadSuspension("DoCall");
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200537 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
538 ShadowFrame* new_shadow_frame(ShadowFrame::Create(num_regs, &shadow_frame, method, 0, memory));
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200539
540 // Initialize new shadow frame.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200541 const size_t first_dest_reg = num_regs - num_ins;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700542 StackHandleScope<1> hs(self);
543 MethodHelper mh(hs.NewHandle(method));
Jeff Haoa3faaf42013-09-03 19:07:00 -0700544 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700545 // Slow path.
546 // We might need to do class loading, which incurs a thread state change to kNative. So
547 // register the shadow frame as under construction and allow suspension again.
548 self->SetShadowFrameUnderConstruction(new_shadow_frame);
549 self->EndAssertNoThreadSuspension(old_cause);
550
551 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200552 // to get the exact type of each reference argument.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700553 const DexFile::TypeList* params = method->GetParameterTypeList();
554 uint32_t shorty_len = 0;
555 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200556
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200557 // TODO: find a cleaner way to separate non-range and range information without duplicating code.
558 uint32_t arg[5]; // only used in invoke-XXX.
559 uint32_t vregC; // only used in invoke-XXX-range.
560 if (is_range) {
561 vregC = inst->VRegC_3rc();
562 } else {
Ian Rogers29a26482014-05-02 15:27:29 -0700563 inst->GetVarArgs(arg, inst_data);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200564 }
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100565
566 // Handle receiver apart since it's not part of the shorty.
567 size_t dest_reg = first_dest_reg;
568 size_t arg_offset = 0;
569 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700570 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100571 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
572 ++dest_reg;
573 ++arg_offset;
574 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800575 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700576 DCHECK_LT(shorty_pos + 1, shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200577 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
578 switch (shorty[shorty_pos + 1]) {
579 case 'L': {
580 Object* o = shadow_frame.GetVRegReference(src_reg);
581 if (do_assignability_check && o != NULL) {
582 Class* arg_type = mh.GetClassFromTypeIdx(params->GetTypeItem(shorty_pos).type_idx_);
583 if (arg_type == NULL) {
584 CHECK(self->IsExceptionPending());
585 return false;
586 }
587 if (!o->VerifierInstanceOf(arg_type)) {
588 // This should never happen.
589 self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(),
590 "Ljava/lang/VirtualMachineError;",
591 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700592 method->GetName(), shorty_pos,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700593 o->GetClass()->GetDescriptor().c_str(),
594 arg_type->GetDescriptor().c_str());
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200595 return false;
596 }
Jeff Haoa3faaf42013-09-03 19:07:00 -0700597 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200598 new_shadow_frame->SetVRegReference(dest_reg, o);
599 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700600 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200601 case 'J': case 'D': {
602 uint64_t wide_value = (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << 32) |
603 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
604 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
605 ++dest_reg;
606 ++arg_offset;
607 break;
608 }
609 default:
610 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
611 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200612 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200613 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700614 // We're done with the construction.
615 self->ClearShadowFrameUnderConstruction();
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200616 } else {
617 // Fast path: no extra checks.
618 if (is_range) {
619 const uint16_t first_src_reg = inst->VRegC_3rc();
620 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs;
621 ++dest_reg, ++src_reg) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800622 AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200623 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200624 } else {
625 DCHECK_LE(num_ins, 5U);
626 uint16_t regList = inst->Fetch16(2);
627 uint16_t count = num_ins;
628 if (count == 5) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800629 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + 4U, (inst_data >> 8) & 0x0f);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200630 --count;
631 }
632 for (size_t arg_index = 0; arg_index < count; ++arg_index, regList >>= 4) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800633 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, regList & 0x0f);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200634 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200635 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700636 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200637 }
638
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200639 // Do the call now.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200640 if (LIKELY(Runtime::Current()->IsStarted())) {
Ian Rogers1d99e452014-01-02 17:36:41 -0800641 if (kIsDebugBuild && method->GetEntryPointFromInterpreter() == nullptr) {
642 LOG(FATAL) << "Attempt to invoke non-executable method: " << PrettyMethod(method);
643 }
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800644 if (kIsDebugBuild && Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly() &&
645 !method->IsNative() && !method->IsProxyMethod() &&
646 method->GetEntryPointFromInterpreter() == artInterpreterToCompiledCodeBridge) {
647 LOG(FATAL) << "Attempt to call compiled code when -Xint: " << PrettyMethod(method);
648 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200649 (method->GetEntryPointFromInterpreter())(self, mh, code_item, new_shadow_frame, result);
650 } else {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200651 UnstartedRuntimeInvoke(self, mh, code_item, new_shadow_frame, result, first_dest_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200652 }
653 return !self->IsExceptionPending();
654}
655
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100656template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200657bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
658 Thread* self, JValue* result) {
659 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
660 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
661 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
662 if (!is_range) {
663 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
664 CHECK_LE(length, 5);
665 }
666 if (UNLIKELY(length < 0)) {
667 ThrowNegativeArraySizeException(length);
668 return false;
669 }
670 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
671 Class* arrayClass = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(),
672 self, false, do_access_check);
673 if (UNLIKELY(arrayClass == NULL)) {
674 DCHECK(self->IsExceptionPending());
675 return false;
676 }
677 CHECK(arrayClass->IsArrayClass());
678 Class* componentClass = arrayClass->GetComponentType();
679 if (UNLIKELY(componentClass->IsPrimitive() && !componentClass->IsPrimitiveInt())) {
680 if (componentClass->IsPrimitiveLong() || componentClass->IsPrimitiveDouble()) {
681 ThrowRuntimeException("Bad filled array request for type %s",
682 PrettyDescriptor(componentClass).c_str());
683 } else {
684 self->ThrowNewExceptionF(shadow_frame.GetCurrentLocationForThrow(),
685 "Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800686 "Found type %s; filled-new-array not implemented for anything but 'int'",
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200687 PrettyDescriptor(componentClass).c_str());
688 }
689 return false;
690 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800691 Object* newArray = Array::Alloc<true>(self, arrayClass, length, arrayClass->GetComponentSize(),
692 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200693 if (UNLIKELY(newArray == NULL)) {
694 DCHECK(self->IsExceptionPending());
695 return false;
696 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100697 uint32_t arg[5]; // only used in filled-new-array.
698 uint32_t vregC; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200699 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100700 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200701 } else {
Ian Rogers29a26482014-05-02 15:27:29 -0700702 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100703 }
704 const bool is_primitive_int_component = componentClass->IsPrimitiveInt();
705 for (int32_t i = 0; i < length; ++i) {
706 size_t src_reg = is_range ? vregC + i : arg[i];
707 if (is_primitive_int_component) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100708 newArray->AsIntArray()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +0100709 } else {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100710 newArray->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200711 }
712 }
713
714 result->SetL(newArray);
715 return true;
716}
717
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100718// TODO fix thread analysis: should be SHARED_LOCKS_REQUIRED(Locks::mutator_lock_).
719template<typename T>
720static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count)
721 NO_THREAD_SAFETY_ANALYSIS {
722 Runtime* runtime = Runtime::Current();
723 for (int32_t i = 0; i < count; ++i) {
724 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
725 }
726}
727
728void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
729 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
730 DCHECK(Runtime::Current()->IsActiveTransaction());
731 DCHECK(array != nullptr);
732 DCHECK_LE(count, array->GetLength());
733 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
734 switch (primitive_component_type) {
735 case Primitive::kPrimBoolean:
736 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
737 break;
738 case Primitive::kPrimByte:
739 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
740 break;
741 case Primitive::kPrimChar:
742 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
743 break;
744 case Primitive::kPrimShort:
745 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
746 break;
747 case Primitive::kPrimInt:
748 case Primitive::kPrimFloat:
749 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
750 break;
751 case Primitive::kPrimLong:
752 case Primitive::kPrimDouble:
753 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
754 break;
755 default:
756 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
757 << " in fill-array-data";
758 break;
759 }
760}
761
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200762// Helper function to deal with class loading in an unstarted runtime.
763static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className,
764 Handle<mirror::ClassLoader> class_loader, JValue* result,
765 const std::string& method_name, bool initialize_class,
766 bool abort_if_not_found)
767 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
768 CHECK(className.Get() != nullptr);
769 std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str()));
770 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
771
772 Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader);
773 if (found == nullptr && abort_if_not_found) {
774 if (!self->IsExceptionPending()) {
775 AbortTransaction(self, "%s failed in un-started runtime for class: %s",
776 method_name.c_str(), PrettyDescriptor(descriptor).c_str());
777 }
778 return;
779 }
780 if (found != nullptr && initialize_class) {
781 StackHandleScope<1> hs(self);
782 Handle<mirror::Class> h_class(hs.NewHandle(found));
783 if (!class_linker->EnsureInitialized(h_class, true, true)) {
784 CHECK(self->IsExceptionPending());
785 return;
786 }
787 }
788 result->SetL(found);
789}
790
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200791static void UnstartedRuntimeInvoke(Thread* self, MethodHelper& mh,
792 const DexFile::CodeItem* code_item, ShadowFrame* shadow_frame,
793 JValue* result, size_t arg_offset) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200794 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
795 // problems in core libraries.
796 std::string name(PrettyMethod(shadow_frame->GetMethod()));
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200797 if (name == "java.lang.Class java.lang.Class.forName(java.lang.String)") {
798 // TODO: Support for the other variants that take more arguments should also be added.
799 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset)->AsString();
800 StackHandleScope<1> hs(self);
801 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
802 UnstartedRuntimeFindClass(self, h_class_name, NullHandle<mirror::ClassLoader>(), result, name,
803 true, true);
804 } else if (name == "java.lang.Class java.lang.VMClassLoader.loadClass(java.lang.String, boolean)") {
805 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset)->AsString();
806 StackHandleScope<1> hs(self);
807 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
808 UnstartedRuntimeFindClass(self, h_class_name, NullHandle<mirror::ClassLoader>(), result, name,
809 false, true);
810 } else if (name == "java.lang.Class java.lang.VMClassLoader.findLoadedClass(java.lang.ClassLoader, java.lang.String)") {
811 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
812 mirror::ClassLoader* class_loader =
813 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
814 StackHandleScope<2> hs(self);
815 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
816 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
817 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, name, false, false);
Ian Rogersc45b8b52014-05-03 01:39:59 -0700818 } else if (name == "java.lang.Class java.lang.Void.lookupType()") {
819 result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V'));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200820 } else if (name == "java.lang.Object java.lang.Class.newInstance()") {
821 Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
822 ArtMethod* c = klass->FindDeclaredDirectMethod("<init>", "()V");
823 CHECK(c != NULL);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700824 StackHandleScope<1> hs(self);
825 Handle<Object> obj(hs.NewHandle(klass->AllocObject(self)));
826 CHECK(obj.Get() != NULL);
827 EnterInterpreterFromInvoke(self, c, obj.Get(), NULL, NULL);
828 result->SetL(obj.Get());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200829 } else if (name == "java.lang.reflect.Field java.lang.Class.getDeclaredField(java.lang.String)") {
830 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
831 // going the reflective Dex way.
832 Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
833 String* name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
834 ArtField* found = NULL;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200835 ObjectArray<ArtField>* fields = klass->GetIFields();
836 for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) {
837 ArtField* f = fields->Get(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700838 if (name->Equals(f->GetName())) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200839 found = f;
840 }
841 }
842 if (found == NULL) {
843 fields = klass->GetSFields();
844 for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) {
845 ArtField* f = fields->Get(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700846 if (name->Equals(f->GetName())) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200847 found = f;
848 }
849 }
850 }
851 CHECK(found != NULL)
852 << "Failed to find field in Class.getDeclaredField in un-started runtime. name="
853 << name->ToModifiedUtf8() << " class=" << PrettyDescriptor(klass);
854 // TODO: getDeclaredField calls GetType once the field is found to ensure a
855 // NoClassDefFoundError is thrown if the field's type cannot be resolved.
856 Class* jlr_Field = self->DecodeJObject(WellKnownClasses::java_lang_reflect_Field)->AsClass();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700857 StackHandleScope<1> hs(self);
858 Handle<Object> field(hs.NewHandle(jlr_Field->AllocNonMovableObject(self)));
859 CHECK(field.Get() != NULL);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200860 ArtMethod* c = jlr_Field->FindDeclaredDirectMethod("<init>", "(Ljava/lang/reflect/ArtField;)V");
861 uint32_t args[1];
Ian Rogersef7d42f2014-01-06 12:55:46 -0800862 args[0] = StackReference<mirror::Object>::FromMirrorPtr(found).AsVRegValue();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700863 EnterInterpreterFromInvoke(self, c, field.Get(), args, NULL);
864 result->SetL(field.Get());
Ian Rogersc45b8b52014-05-03 01:39:59 -0700865 } else if (name == "int java.lang.Object.hashCode()") {
866 Object* obj = shadow_frame->GetVRegReference(arg_offset);
867 result->SetI(obj->IdentityHashCode());
868 } else if (name == "java.lang.String java.lang.reflect.ArtMethod.getMethodName(java.lang.reflect.ArtMethod)") {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700869 StackHandleScope<1> hs(self);
870 MethodHelper mh(hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsArtMethod()));
871 result->SetL(mh.GetNameAsString(self));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200872 } else if (name == "void java.lang.System.arraycopy(java.lang.Object, int, java.lang.Object, int, int)" ||
873 name == "void java.lang.System.arraycopy(char[], int, char[], int, int)") {
874 // Special case array copying without initializing System.
875 Class* ctype = shadow_frame->GetVRegReference(arg_offset)->GetClass()->GetComponentType();
876 jint srcPos = shadow_frame->GetVReg(arg_offset + 1);
877 jint dstPos = shadow_frame->GetVReg(arg_offset + 3);
878 jint length = shadow_frame->GetVReg(arg_offset + 4);
879 if (!ctype->IsPrimitive()) {
880 ObjectArray<Object>* src = shadow_frame->GetVRegReference(arg_offset)->AsObjectArray<Object>();
881 ObjectArray<Object>* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<Object>();
882 for (jint i = 0; i < length; ++i) {
883 dst->Set(dstPos + i, src->Get(srcPos + i));
884 }
885 } else if (ctype->IsPrimitiveChar()) {
886 CharArray* src = shadow_frame->GetVRegReference(arg_offset)->AsCharArray();
887 CharArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray();
888 for (jint i = 0; i < length; ++i) {
889 dst->Set(dstPos + i, src->Get(srcPos + i));
890 }
891 } else if (ctype->IsPrimitiveInt()) {
892 IntArray* src = shadow_frame->GetVRegReference(arg_offset)->AsIntArray();
893 IntArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsIntArray();
894 for (jint i = 0; i < length; ++i) {
895 dst->Set(dstPos + i, src->Get(srcPos + i));
896 }
897 } else {
Ian Rogersc45b8b52014-05-03 01:39:59 -0700898 self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;",
899 "Unimplemented System.arraycopy for type '%s'",
900 PrettyDescriptor(ctype).c_str());
901 }
902 } else if (name == "java.lang.Object java.lang.ThreadLocal.get()") {
903 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
904 if (caller == "java.lang.String java.lang.IntegralToString.convertInt(java.lang.AbstractStringBuilder, int)") {
905 // Allocate non-threadlocal buffer.
906 result->SetL(mirror::CharArray::Alloc(self, 11));
907 } else {
908 self->ThrowNewException(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;",
909 "Unimplemented ThreadLocal.get");
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200910 }
911 } else {
912 // Not special, continue with regular interpreter execution.
913 artInterpreterToInterpreterBridge(self, mh, code_item, shadow_frame, result);
914 }
915}
916
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200917// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200918#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
919 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100920 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
921 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +0200922 const Instruction* inst, uint16_t inst_data, \
923 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200924EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
925EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
926EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
927EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
928#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200929
930// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100931#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
932 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
933 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
934 const ShadowFrame& shadow_frame, \
935 Thread* self, JValue* result)
936#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
937 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
938 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
939 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
940 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
941EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
942EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
943#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200944#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
945
946} // namespace interpreter
947} // namespace art