blob: 5c77b96a5f71317ca1332736ae7faa9a4595033a [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;
Ian Rogers2c4257b2014-10-24 14:20:06 -070083 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -070084 }
85 return true;
86}
87
88// Explicitly instantiate all DoFieldGet functions.
89#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
90 template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \
91 ShadowFrame& shadow_frame, \
92 const Instruction* inst, \
93 uint16_t inst_data)
94
95#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
96 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \
97 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true);
98
99// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700100EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
101EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
102EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
103EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
104EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
105EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
106EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700107
108// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700109EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
110EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
111EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
112EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
113EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
114EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
115EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700116
117#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
118#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
119
120// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
121// Returns true on success, otherwise throws an exception and returns false.
122template<Primitive::Type field_type>
123bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
124 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
125 if (UNLIKELY(obj == nullptr)) {
126 // We lost the reference to the field index so we cannot get a more
127 // precised exception message.
128 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow());
129 return false;
130 }
131 MemberOffset field_offset(inst->VRegC_22c());
132 // Report this field access to instrumentation if needed. Since we only have the offset of
133 // the field from the base of the object, we need to look for it first.
134 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
135 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
136 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
137 field_offset.Uint32Value());
138 DCHECK(f != nullptr);
139 DCHECK(!f->IsStatic());
140 instrumentation->FieldReadEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
141 shadow_frame.GetDexPC(), f);
142 }
143 // Note: iget-x-quick instructions are only for non-volatile fields.
144 const uint32_t vregA = inst->VRegA_22c(inst_data);
145 switch (field_type) {
146 case Primitive::kPrimInt:
147 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
148 break;
149 case Primitive::kPrimLong:
150 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
151 break;
152 case Primitive::kPrimNot:
153 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
154 break;
155 default:
156 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700157 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700158 }
159 return true;
160}
161
162// Explicitly instantiate all DoIGetQuick functions.
163#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
164 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
165 uint16_t inst_data)
166
167EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
168EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
169EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
170#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
171
172template<Primitive::Type field_type>
173static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
174 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
175 JValue field_value;
176 switch (field_type) {
177 case Primitive::kPrimBoolean:
178 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
179 break;
180 case Primitive::kPrimByte:
181 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
182 break;
183 case Primitive::kPrimChar:
184 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
185 break;
186 case Primitive::kPrimShort:
187 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
188 break;
189 case Primitive::kPrimInt:
190 field_value.SetI(shadow_frame.GetVReg(vreg));
191 break;
192 case Primitive::kPrimLong:
193 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
194 break;
195 case Primitive::kPrimNot:
196 field_value.SetL(shadow_frame.GetVRegReference(vreg));
197 break;
198 default:
199 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700200 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700201 }
202 return field_value;
203}
204
205template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
206 bool transaction_active>
207bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
208 uint16_t inst_data) {
209 bool do_assignability_check = do_access_check;
210 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
211 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
212 ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
Fred Shih37f05ef2014-07-16 18:38:08 -0700213 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -0700214 if (UNLIKELY(f == nullptr)) {
215 CHECK(self->IsExceptionPending());
216 return false;
217 }
218 Object* obj;
219 if (is_static) {
220 obj = f->GetDeclaringClass();
221 } else {
222 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
223 if (UNLIKELY(obj == nullptr)) {
224 ThrowNullPointerExceptionForFieldAccess(shadow_frame.GetCurrentLocationForThrow(),
225 f, false);
226 return false;
227 }
228 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +0200229 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -0700230 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
231 // Report this field access to instrumentation if needed. Since we only have the offset of
232 // the field from the base of the object, we need to look for it first.
233 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
234 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
235 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
236 Object* this_object = f->IsStatic() ? nullptr : obj;
237 instrumentation->FieldWriteEvent(self, this_object, shadow_frame.GetMethod(),
238 shadow_frame.GetDexPC(), f, field_value);
239 }
240 switch (field_type) {
241 case Primitive::kPrimBoolean:
242 f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA));
243 break;
244 case Primitive::kPrimByte:
245 f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA));
246 break;
247 case Primitive::kPrimChar:
248 f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA));
249 break;
250 case Primitive::kPrimShort:
251 f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA));
252 break;
253 case Primitive::kPrimInt:
254 f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA));
255 break;
256 case Primitive::kPrimLong:
257 f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA));
258 break;
259 case Primitive::kPrimNot: {
260 Object* reg = shadow_frame.GetVRegReference(vregA);
261 if (do_assignability_check && reg != nullptr) {
262 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
263 // object in the destructor.
264 Class* field_class;
265 {
266 StackHandleScope<3> hs(self);
267 HandleWrapper<mirror::ArtField> h_f(hs.NewHandleWrapper(&f));
268 HandleWrapper<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
269 HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
270 FieldHelper fh(h_f);
271 field_class = fh.GetType();
272 }
273 if (!reg->VerifierInstanceOf(field_class)) {
274 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700275 std::string temp1, temp2, temp3;
Ian Rogers54874942014-06-10 16:31:03 -0700276 self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(),
277 "Ljava/lang/VirtualMachineError;",
278 "Put '%s' that is not instance of field '%s' in '%s'",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700279 reg->GetClass()->GetDescriptor(&temp1),
280 field_class->GetDescriptor(&temp2),
281 f->GetDeclaringClass()->GetDescriptor(&temp3));
Ian Rogers54874942014-06-10 16:31:03 -0700282 return false;
283 }
284 }
285 f->SetObj<transaction_active>(obj, reg);
286 break;
287 }
288 default:
289 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700290 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700291 }
292 return true;
293}
294
295// Explicitly instantiate all DoFieldPut functions.
296#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
297 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
298 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
299
300#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
301 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
302 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
303 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
304 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
305
306// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700307EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
308EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
309EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
310EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
311EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
312EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
313EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700314
315// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700316EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
317EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
318EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
319EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
320EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
321EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
322EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700323
324#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
325#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
326
327template<Primitive::Type field_type, bool transaction_active>
328bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
329 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
330 if (UNLIKELY(obj == nullptr)) {
331 // We lost the reference to the field index so we cannot get a more
332 // precised exception message.
333 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow());
334 return false;
335 }
336 MemberOffset field_offset(inst->VRegC_22c());
337 const uint32_t vregA = inst->VRegA_22c(inst_data);
338 // Report this field modification to instrumentation if needed. Since we only have the offset of
339 // the field from the base of the object, we need to look for it first.
340 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
341 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
342 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
343 field_offset.Uint32Value());
344 DCHECK(f != nullptr);
345 DCHECK(!f->IsStatic());
346 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
347 instrumentation->FieldWriteEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
348 shadow_frame.GetDexPC(), f, field_value);
349 }
350 // Note: iput-x-quick instructions are only for non-volatile fields.
351 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700352 case Primitive::kPrimBoolean:
353 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
354 break;
355 case Primitive::kPrimByte:
356 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
357 break;
358 case Primitive::kPrimChar:
359 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
360 break;
361 case Primitive::kPrimShort:
362 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
363 break;
Ian Rogers54874942014-06-10 16:31:03 -0700364 case Primitive::kPrimInt:
365 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
366 break;
367 case Primitive::kPrimLong:
368 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
369 break;
370 case Primitive::kPrimNot:
371 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
372 break;
373 default:
374 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700375 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700376 }
377 return true;
378}
379
380// Explicitly instantiate all DoIPutQuick functions.
381#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
382 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
383 const Instruction* inst, \
384 uint16_t inst_data)
385
386#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
387 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
388 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
389
Andreas Gampec8ccf682014-09-29 20:07:43 -0700390EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
391EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
392EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
393EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
394EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
395EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
396EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700397#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
398#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
399
Sebastien Hertz9f102032014-05-23 08:59:42 +0200400/**
401 * Finds the location where this exception will be caught. We search until we reach either the top
402 * frame or a native frame, in which cases this exception is considered uncaught.
403 */
404class CatchLocationFinder : public StackVisitor {
405 public:
406 explicit CatchLocationFinder(Thread* self, Handle<mirror::Throwable>* exception)
407 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
408 : StackVisitor(self, nullptr), self_(self), handle_scope_(self), exception_(exception),
409 catch_method_(handle_scope_.NewHandle<mirror::ArtMethod>(nullptr)),
410 catch_dex_pc_(DexFile::kDexNoIndex), clear_exception_(false) {
411 }
412
413 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
414 mirror::ArtMethod* method = GetMethod();
415 if (method == nullptr) {
416 return true;
417 }
418 if (method->IsRuntimeMethod()) {
419 // Ignore callee save method.
420 DCHECK(method->IsCalleeSaveMethod());
421 return true;
422 }
423 if (method->IsNative()) {
424 return false; // End stack walk.
425 }
426 DCHECK(!method->IsNative());
427 uint32_t dex_pc = GetDexPc();
428 if (dex_pc != DexFile::kDexNoIndex) {
429 uint32_t found_dex_pc;
430 {
431 StackHandleScope<3> hs(self_);
432 Handle<mirror::Class> exception_class(hs.NewHandle((*exception_)->GetClass()));
433 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
434 found_dex_pc = mirror::ArtMethod::FindCatchBlock(h_method, exception_class, dex_pc,
435 &clear_exception_);
436 }
437 if (found_dex_pc != DexFile::kDexNoIndex) {
438 catch_method_.Assign(method);
439 catch_dex_pc_ = found_dex_pc;
440 return false; // End stack walk.
441 }
442 }
443 return true; // Continue stack walk.
444 }
445
446 ArtMethod* GetCatchMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
447 return catch_method_.Get();
448 }
449
450 uint32_t GetCatchDexPc() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
451 return catch_dex_pc_;
452 }
453
454 bool NeedClearException() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
455 return clear_exception_;
456 }
457
458 private:
459 Thread* const self_;
460 StackHandleScope<1> handle_scope_;
461 Handle<mirror::Throwable>* exception_;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700462 MutableHandle<mirror::ArtMethod> catch_method_;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200463 uint32_t catch_dex_pc_;
464 bool clear_exception_;
465
466
467 DISALLOW_COPY_AND_ASSIGN(CatchLocationFinder);
468};
469
Ian Rogers54874942014-06-10 16:31:03 -0700470uint32_t FindNextInstructionFollowingException(Thread* self,
471 ShadowFrame& shadow_frame,
472 uint32_t dex_pc,
Ian Rogers54874942014-06-10 16:31:03 -0700473 const instrumentation::Instrumentation* instrumentation) {
474 self->VerifyStack();
475 ThrowLocation throw_location;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200476 StackHandleScope<3> hs(self);
477 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException(&throw_location)));
478 if (!self->IsExceptionReportedToInstrumentation() && instrumentation->HasExceptionCaughtListeners()) {
479 CatchLocationFinder clf(self, &exception);
480 clf.WalkStack(false);
481 instrumentation->ExceptionCaughtEvent(self, throw_location, clf.GetCatchMethod(),
482 clf.GetCatchDexPc(), exception.Get());
483 self->SetExceptionReportedToInstrumentation(true);
484 }
Ian Rogers54874942014-06-10 16:31:03 -0700485 bool clear_exception = false;
486 uint32_t found_dex_pc;
487 {
Ian Rogers54874942014-06-10 16:31:03 -0700488 Handle<mirror::Class> exception_class(hs.NewHandle(exception->GetClass()));
489 Handle<mirror::ArtMethod> h_method(hs.NewHandle(shadow_frame.GetMethod()));
Ian Rogers54874942014-06-10 16:31:03 -0700490 found_dex_pc = mirror::ArtMethod::FindCatchBlock(h_method, exception_class, dex_pc,
491 &clear_exception);
492 }
493 if (found_dex_pc == DexFile::kDexNoIndex) {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200494 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
Ian Rogers54874942014-06-10 16:31:03 -0700495 shadow_frame.GetMethod(), dex_pc);
496 } else {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200497 if (self->IsExceptionReportedToInstrumentation()) {
498 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
499 shadow_frame.GetMethod(), dex_pc);
500 }
Ian Rogers54874942014-06-10 16:31:03 -0700501 if (clear_exception) {
502 self->ClearException();
503 }
504 }
505 return found_dex_pc;
506}
507
508void UnexpectedOpcode(const Instruction* inst, MethodHelper& mh) {
509 LOG(FATAL) << "Unexpected instruction: " << inst->DumpString(mh.GetMethod()->GetDexFile());
510 exit(0); // Unreachable, keep GCC happy.
511}
512
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700513static void UnstartedRuntimeInvoke(Thread* self, MethodHelper* mh,
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200514 const DexFile::CodeItem* code_item, ShadowFrame* shadow_frame,
515 JValue* result, size_t arg_offset)
516 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200517
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200518// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800519static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
520 size_t dest_reg, size_t src_reg)
521 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200522 // If both register locations contains the same value, the register probably holds a reference.
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700523 // Uint required, so that sign extension does not make this wrong on 64b systems
524 uint32_t src_value = shadow_frame.GetVReg(src_reg);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800525 mirror::Object* o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700526 if (src_value == reinterpret_cast<uintptr_t>(o)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800527 new_shadow_frame->SetVRegReference(dest_reg, o);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200528 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800529 new_shadow_frame->SetVReg(dest_reg, src_value);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200530 }
531}
532
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700533void AbortTransaction(Thread* self, const char* fmt, ...) {
534 CHECK(Runtime::Current()->IsActiveTransaction());
535 // Throw an exception so we can abort the transaction and undo every change.
536 va_list args;
537 va_start(args, fmt);
538 self->ThrowNewExceptionV(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;", fmt,
539 args);
540 va_end(args);
541}
542
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200543template<bool is_range, bool do_assignability_check>
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100544bool DoCall(ArtMethod* method, Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200545 const Instruction* inst, uint16_t inst_data, JValue* result) {
546 // Compute method information.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700547 const DexFile::CodeItem* code_item = method->GetCodeItem();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200548 const uint16_t num_ins = (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200549 uint16_t num_regs;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200550 if (LIKELY(code_item != NULL)) {
551 num_regs = code_item->registers_size_;
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200552 DCHECK_EQ(num_ins, code_item->ins_size_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200553 } else {
554 DCHECK(method->IsNative() || method->IsProxyMethod());
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200555 num_regs = num_ins;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200556 }
557
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200558 // Allocate shadow frame on the stack.
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700559 const char* old_cause = self->StartAssertNoThreadSuspension("DoCall");
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200560 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
561 ShadowFrame* new_shadow_frame(ShadowFrame::Create(num_regs, &shadow_frame, method, 0, memory));
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200562
563 // Initialize new shadow frame.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200564 const size_t first_dest_reg = num_regs - num_ins;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700565 StackHandleScope<1> hs(self);
566 MethodHelper mh(hs.NewHandle(method));
Jeff Haoa3faaf42013-09-03 19:07:00 -0700567 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700568 // Slow path.
569 // We might need to do class loading, which incurs a thread state change to kNative. So
570 // register the shadow frame as under construction and allow suspension again.
571 self->SetShadowFrameUnderConstruction(new_shadow_frame);
572 self->EndAssertNoThreadSuspension(old_cause);
573
574 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200575 // to get the exact type of each reference argument.
Hiroshi Yamauchi5ad97da2014-10-06 10:46:14 -0700576 const DexFile::TypeList* params = mh.Get()->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700577 uint32_t shorty_len = 0;
Hiroshi Yamauchi5ad97da2014-10-06 10:46:14 -0700578 const char* shorty = mh.Get()->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200579
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200580 // TODO: find a cleaner way to separate non-range and range information without duplicating code.
581 uint32_t arg[5]; // only used in invoke-XXX.
582 uint32_t vregC; // only used in invoke-XXX-range.
583 if (is_range) {
584 vregC = inst->VRegC_3rc();
585 } else {
Ian Rogers29a26482014-05-02 15:27:29 -0700586 inst->GetVarArgs(arg, inst_data);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200587 }
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100588
589 // Handle receiver apart since it's not part of the shorty.
590 size_t dest_reg = first_dest_reg;
591 size_t arg_offset = 0;
Hiroshi Yamauchi5ad97da2014-10-06 10:46:14 -0700592 if (!mh.Get()->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700593 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100594 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
595 ++dest_reg;
596 ++arg_offset;
597 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800598 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700599 DCHECK_LT(shorty_pos + 1, shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200600 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
601 switch (shorty[shorty_pos + 1]) {
602 case 'L': {
603 Object* o = shadow_frame.GetVRegReference(src_reg);
604 if (do_assignability_check && o != NULL) {
605 Class* arg_type = mh.GetClassFromTypeIdx(params->GetTypeItem(shorty_pos).type_idx_);
606 if (arg_type == NULL) {
607 CHECK(self->IsExceptionPending());
608 return false;
609 }
610 if (!o->VerifierInstanceOf(arg_type)) {
611 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700612 std::string temp1, temp2;
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200613 self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(),
614 "Ljava/lang/VirtualMachineError;",
615 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Hiroshi Yamauchi5ad97da2014-10-06 10:46:14 -0700616 mh.Get()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700617 o->GetClass()->GetDescriptor(&temp1),
618 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200619 return false;
620 }
Jeff Haoa3faaf42013-09-03 19:07:00 -0700621 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200622 new_shadow_frame->SetVRegReference(dest_reg, o);
623 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700624 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200625 case 'J': case 'D': {
626 uint64_t wide_value = (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << 32) |
627 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
628 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
629 ++dest_reg;
630 ++arg_offset;
631 break;
632 }
633 default:
634 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
635 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200636 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200637 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700638 // We're done with the construction.
639 self->ClearShadowFrameUnderConstruction();
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200640 } else {
641 // Fast path: no extra checks.
642 if (is_range) {
643 const uint16_t first_src_reg = inst->VRegC_3rc();
644 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs;
645 ++dest_reg, ++src_reg) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800646 AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200647 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200648 } else {
649 DCHECK_LE(num_ins, 5U);
650 uint16_t regList = inst->Fetch16(2);
651 uint16_t count = num_ins;
652 if (count == 5) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800653 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + 4U, (inst_data >> 8) & 0x0f);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200654 --count;
655 }
656 for (size_t arg_index = 0; arg_index < count; ++arg_index, regList >>= 4) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800657 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, regList & 0x0f);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200658 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200659 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700660 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200661 }
662
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200663 // Do the call now.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200664 if (LIKELY(Runtime::Current()->IsStarted())) {
Hiroshi Yamauchi5ad97da2014-10-06 10:46:14 -0700665 if (kIsDebugBuild && mh.Get()->GetEntryPointFromInterpreter() == nullptr) {
666 LOG(FATAL) << "Attempt to invoke non-executable method: " << PrettyMethod(mh.Get());
Ian Rogers1d99e452014-01-02 17:36:41 -0800667 }
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800668 if (kIsDebugBuild && Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly() &&
Hiroshi Yamauchi5ad97da2014-10-06 10:46:14 -0700669 !mh.Get()->IsNative() && !mh.Get()->IsProxyMethod() &&
670 mh.Get()->GetEntryPointFromInterpreter() == artInterpreterToCompiledCodeBridge) {
671 LOG(FATAL) << "Attempt to call compiled code when -Xint: " << PrettyMethod(mh.Get());
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800672 }
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700673 (mh.Get()->GetEntryPointFromInterpreter())(self, &mh, code_item, new_shadow_frame, result);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200674 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700675 UnstartedRuntimeInvoke(self, &mh, code_item, new_shadow_frame, result, first_dest_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200676 }
677 return !self->IsExceptionPending();
678}
679
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100680template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200681bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
682 Thread* self, JValue* result) {
683 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
684 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
685 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
686 if (!is_range) {
687 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
688 CHECK_LE(length, 5);
689 }
690 if (UNLIKELY(length < 0)) {
691 ThrowNegativeArraySizeException(length);
692 return false;
693 }
694 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
695 Class* arrayClass = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(),
696 self, false, do_access_check);
697 if (UNLIKELY(arrayClass == NULL)) {
698 DCHECK(self->IsExceptionPending());
699 return false;
700 }
701 CHECK(arrayClass->IsArrayClass());
702 Class* componentClass = arrayClass->GetComponentType();
703 if (UNLIKELY(componentClass->IsPrimitive() && !componentClass->IsPrimitiveInt())) {
704 if (componentClass->IsPrimitiveLong() || componentClass->IsPrimitiveDouble()) {
705 ThrowRuntimeException("Bad filled array request for type %s",
706 PrettyDescriptor(componentClass).c_str());
707 } else {
708 self->ThrowNewExceptionF(shadow_frame.GetCurrentLocationForThrow(),
709 "Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800710 "Found type %s; filled-new-array not implemented for anything but 'int'",
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200711 PrettyDescriptor(componentClass).c_str());
712 }
713 return false;
714 }
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700715 Object* newArray = Array::Alloc<true>(self, arrayClass, length,
716 arrayClass->GetComponentSizeShift(),
Ian Rogers6fac4472014-02-25 17:01:10 -0800717 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200718 if (UNLIKELY(newArray == NULL)) {
719 DCHECK(self->IsExceptionPending());
720 return false;
721 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100722 uint32_t arg[5]; // only used in filled-new-array.
723 uint32_t vregC; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200724 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100725 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200726 } else {
Ian Rogers29a26482014-05-02 15:27:29 -0700727 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100728 }
729 const bool is_primitive_int_component = componentClass->IsPrimitiveInt();
730 for (int32_t i = 0; i < length; ++i) {
731 size_t src_reg = is_range ? vregC + i : arg[i];
732 if (is_primitive_int_component) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100733 newArray->AsIntArray()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +0100734 } else {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100735 newArray->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200736 }
737 }
738
739 result->SetL(newArray);
740 return true;
741}
742
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100743// TODO fix thread analysis: should be SHARED_LOCKS_REQUIRED(Locks::mutator_lock_).
744template<typename T>
745static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count)
746 NO_THREAD_SAFETY_ANALYSIS {
747 Runtime* runtime = Runtime::Current();
748 for (int32_t i = 0; i < count; ++i) {
749 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
750 }
751}
752
753void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
754 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
755 DCHECK(Runtime::Current()->IsActiveTransaction());
756 DCHECK(array != nullptr);
757 DCHECK_LE(count, array->GetLength());
758 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
759 switch (primitive_component_type) {
760 case Primitive::kPrimBoolean:
761 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
762 break;
763 case Primitive::kPrimByte:
764 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
765 break;
766 case Primitive::kPrimChar:
767 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
768 break;
769 case Primitive::kPrimShort:
770 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
771 break;
772 case Primitive::kPrimInt:
773 case Primitive::kPrimFloat:
774 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
775 break;
776 case Primitive::kPrimLong:
777 case Primitive::kPrimDouble:
778 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
779 break;
780 default:
781 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
782 << " in fill-array-data";
783 break;
784 }
785}
786
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200787// Helper function to deal with class loading in an unstarted runtime.
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700788static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className,
789 Handle<mirror::ClassLoader> class_loader, JValue* result,
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200790 const std::string& method_name, bool initialize_class,
791 bool abort_if_not_found)
792 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
793 CHECK(className.Get() != nullptr);
794 std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str()));
795 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
796
797 Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader);
798 if (found == nullptr && abort_if_not_found) {
799 if (!self->IsExceptionPending()) {
800 AbortTransaction(self, "%s failed in un-started runtime for class: %s",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700801 method_name.c_str(), PrettyDescriptor(descriptor.c_str()).c_str());
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200802 }
803 return;
804 }
805 if (found != nullptr && initialize_class) {
806 StackHandleScope<1> hs(self);
807 Handle<mirror::Class> h_class(hs.NewHandle(found));
Ian Rogers7b078e82014-09-10 14:44:24 -0700808 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200809 CHECK(self->IsExceptionPending());
810 return;
811 }
812 }
813 result->SetL(found);
814}
815
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700816static void UnstartedRuntimeInvoke(Thread* self, MethodHelper* mh,
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200817 const DexFile::CodeItem* code_item, ShadowFrame* shadow_frame,
818 JValue* result, size_t arg_offset) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200819 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
820 // problems in core libraries.
821 std::string name(PrettyMethod(shadow_frame->GetMethod()));
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200822 if (name == "java.lang.Class java.lang.Class.forName(java.lang.String)") {
823 // TODO: Support for the other variants that take more arguments should also be added.
824 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset)->AsString();
825 StackHandleScope<1> hs(self);
826 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
827 UnstartedRuntimeFindClass(self, h_class_name, NullHandle<mirror::ClassLoader>(), result, name,
828 true, true);
829 } else if (name == "java.lang.Class java.lang.VMClassLoader.loadClass(java.lang.String, boolean)") {
830 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset)->AsString();
831 StackHandleScope<1> hs(self);
832 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
833 UnstartedRuntimeFindClass(self, h_class_name, NullHandle<mirror::ClassLoader>(), result, name,
834 false, true);
835 } else if (name == "java.lang.Class java.lang.VMClassLoader.findLoadedClass(java.lang.ClassLoader, java.lang.String)") {
836 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
837 mirror::ClassLoader* class_loader =
838 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
839 StackHandleScope<2> hs(self);
840 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
841 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
842 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, name, false, false);
Ian Rogersc45b8b52014-05-03 01:39:59 -0700843 } else if (name == "java.lang.Class java.lang.Void.lookupType()") {
844 result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V'));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200845 } else if (name == "java.lang.Object java.lang.Class.newInstance()") {
846 Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
847 ArtMethod* c = klass->FindDeclaredDirectMethod("<init>", "()V");
848 CHECK(c != NULL);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700849 StackHandleScope<1> hs(self);
850 Handle<Object> obj(hs.NewHandle(klass->AllocObject(self)));
851 CHECK(obj.Get() != NULL);
852 EnterInterpreterFromInvoke(self, c, obj.Get(), NULL, NULL);
853 result->SetL(obj.Get());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200854 } else if (name == "java.lang.reflect.Field java.lang.Class.getDeclaredField(java.lang.String)") {
855 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
856 // going the reflective Dex way.
857 Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
858 String* name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
859 ArtField* found = NULL;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200860 ObjectArray<ArtField>* fields = klass->GetIFields();
861 for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) {
862 ArtField* f = fields->Get(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700863 if (name->Equals(f->GetName())) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200864 found = f;
865 }
866 }
867 if (found == NULL) {
868 fields = klass->GetSFields();
869 for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) {
870 ArtField* f = fields->Get(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700871 if (name->Equals(f->GetName())) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200872 found = f;
873 }
874 }
875 }
876 CHECK(found != NULL)
877 << "Failed to find field in Class.getDeclaredField in un-started runtime. name="
878 << name->ToModifiedUtf8() << " class=" << PrettyDescriptor(klass);
879 // TODO: getDeclaredField calls GetType once the field is found to ensure a
880 // NoClassDefFoundError is thrown if the field's type cannot be resolved.
881 Class* jlr_Field = self->DecodeJObject(WellKnownClasses::java_lang_reflect_Field)->AsClass();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700882 StackHandleScope<1> hs(self);
883 Handle<Object> field(hs.NewHandle(jlr_Field->AllocNonMovableObject(self)));
884 CHECK(field.Get() != NULL);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200885 ArtMethod* c = jlr_Field->FindDeclaredDirectMethod("<init>", "(Ljava/lang/reflect/ArtField;)V");
886 uint32_t args[1];
Ian Rogersef7d42f2014-01-06 12:55:46 -0800887 args[0] = StackReference<mirror::Object>::FromMirrorPtr(found).AsVRegValue();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700888 EnterInterpreterFromInvoke(self, c, field.Get(), args, NULL);
889 result->SetL(field.Get());
Ian Rogersc45b8b52014-05-03 01:39:59 -0700890 } else if (name == "int java.lang.Object.hashCode()") {
891 Object* obj = shadow_frame->GetVRegReference(arg_offset);
892 result->SetI(obj->IdentityHashCode());
893 } else if (name == "java.lang.String java.lang.reflect.ArtMethod.getMethodName(java.lang.reflect.ArtMethod)") {
Ian Rogers6b14d552014-10-28 21:50:58 -0700894 mirror::ArtMethod* method = shadow_frame->GetVRegReference(arg_offset)->AsArtMethod();
895 result->SetL(method->GetNameAsString(self));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200896 } else if (name == "void java.lang.System.arraycopy(java.lang.Object, int, java.lang.Object, int, int)" ||
897 name == "void java.lang.System.arraycopy(char[], int, char[], int, int)") {
898 // Special case array copying without initializing System.
899 Class* ctype = shadow_frame->GetVRegReference(arg_offset)->GetClass()->GetComponentType();
900 jint srcPos = shadow_frame->GetVReg(arg_offset + 1);
901 jint dstPos = shadow_frame->GetVReg(arg_offset + 3);
902 jint length = shadow_frame->GetVReg(arg_offset + 4);
903 if (!ctype->IsPrimitive()) {
904 ObjectArray<Object>* src = shadow_frame->GetVRegReference(arg_offset)->AsObjectArray<Object>();
905 ObjectArray<Object>* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<Object>();
906 for (jint i = 0; i < length; ++i) {
907 dst->Set(dstPos + i, src->Get(srcPos + i));
908 }
909 } else if (ctype->IsPrimitiveChar()) {
910 CharArray* src = shadow_frame->GetVRegReference(arg_offset)->AsCharArray();
911 CharArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray();
912 for (jint i = 0; i < length; ++i) {
913 dst->Set(dstPos + i, src->Get(srcPos + i));
914 }
915 } else if (ctype->IsPrimitiveInt()) {
916 IntArray* src = shadow_frame->GetVRegReference(arg_offset)->AsIntArray();
917 IntArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsIntArray();
918 for (jint i = 0; i < length; ++i) {
919 dst->Set(dstPos + i, src->Get(srcPos + i));
920 }
921 } else {
Ian Rogersc45b8b52014-05-03 01:39:59 -0700922 self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;",
923 "Unimplemented System.arraycopy for type '%s'",
924 PrettyDescriptor(ctype).c_str());
925 }
926 } else if (name == "java.lang.Object java.lang.ThreadLocal.get()") {
927 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
928 if (caller == "java.lang.String java.lang.IntegralToString.convertInt(java.lang.AbstractStringBuilder, int)") {
929 // Allocate non-threadlocal buffer.
930 result->SetL(mirror::CharArray::Alloc(self, 11));
931 } else {
932 self->ThrowNewException(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;",
933 "Unimplemented ThreadLocal.get");
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200934 }
935 } else {
936 // Not special, continue with regular interpreter execution.
937 artInterpreterToInterpreterBridge(self, mh, code_item, shadow_frame, result);
938 }
939}
940
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200941// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200942#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
943 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100944 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
945 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +0200946 const Instruction* inst, uint16_t inst_data, \
947 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200948EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
949EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
950EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
951EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
952#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200953
954// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100955#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
956 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
957 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
958 const ShadowFrame& shadow_frame, \
959 Thread* self, JValue* result)
960#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
961 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
962 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
963 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
964 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
965EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
966EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
967#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200968#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
969
970} // namespace interpreter
971} // namespace art