blob: a29558e3d8f79f026e2c0244a41bd2b2bb8e0423 [file] [log] [blame]
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "interpreter_common.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070018
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010019#include "mirror/array-inl.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020020
21namespace art {
22namespace interpreter {
23
Ian Rogers54874942014-06-10 16:31:03 -070024void ThrowNullPointerExceptionFromInterpreter(const ShadowFrame& shadow_frame) {
25 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow());
26}
27
28template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
29bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
30 uint16_t inst_data) {
31 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
32 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
33 ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
Fred Shih37f05ef2014-07-16 18:38:08 -070034 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -070035 if (UNLIKELY(f == nullptr)) {
36 CHECK(self->IsExceptionPending());
37 return false;
38 }
39 Object* obj;
40 if (is_static) {
41 obj = f->GetDeclaringClass();
42 } else {
43 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
44 if (UNLIKELY(obj == nullptr)) {
45 ThrowNullPointerExceptionForFieldAccess(shadow_frame.GetCurrentLocationForThrow(), f, true);
46 return false;
47 }
48 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +020049 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -070050 // Report this field access to instrumentation if needed.
51 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
52 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
53 Object* this_object = f->IsStatic() ? nullptr : obj;
54 instrumentation->FieldReadEvent(self, this_object, shadow_frame.GetMethod(),
55 shadow_frame.GetDexPC(), f);
56 }
57 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
58 switch (field_type) {
59 case Primitive::kPrimBoolean:
60 shadow_frame.SetVReg(vregA, f->GetBoolean(obj));
61 break;
62 case Primitive::kPrimByte:
63 shadow_frame.SetVReg(vregA, f->GetByte(obj));
64 break;
65 case Primitive::kPrimChar:
66 shadow_frame.SetVReg(vregA, f->GetChar(obj));
67 break;
68 case Primitive::kPrimShort:
69 shadow_frame.SetVReg(vregA, f->GetShort(obj));
70 break;
71 case Primitive::kPrimInt:
72 shadow_frame.SetVReg(vregA, f->GetInt(obj));
73 break;
74 case Primitive::kPrimLong:
75 shadow_frame.SetVRegLong(vregA, f->GetLong(obj));
76 break;
77 case Primitive::kPrimNot:
78 shadow_frame.SetVRegReference(vregA, f->GetObject(obj));
79 break;
80 default:
81 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -070082 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -070083 }
84 return true;
85}
86
87// Explicitly instantiate all DoFieldGet functions.
88#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
89 template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \
90 ShadowFrame& shadow_frame, \
91 const Instruction* inst, \
92 uint16_t inst_data)
93
94#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
95 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \
96 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true);
97
98// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -070099EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
100EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
101EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
102EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
103EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
104EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
105EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700106
107// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700108EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
109EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
110EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
111EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
112EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
113EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
114EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700115
116#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
117#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
118
119// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
120// Returns true on success, otherwise throws an exception and returns false.
121template<Primitive::Type field_type>
122bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
123 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
124 if (UNLIKELY(obj == nullptr)) {
125 // We lost the reference to the field index so we cannot get a more
126 // precised exception message.
127 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow());
128 return false;
129 }
130 MemberOffset field_offset(inst->VRegC_22c());
131 // Report this field access to instrumentation if needed. Since we only have the offset of
132 // the field from the base of the object, we need to look for it first.
133 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
134 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
135 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
136 field_offset.Uint32Value());
137 DCHECK(f != nullptr);
138 DCHECK(!f->IsStatic());
139 instrumentation->FieldReadEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
140 shadow_frame.GetDexPC(), f);
141 }
142 // Note: iget-x-quick instructions are only for non-volatile fields.
143 const uint32_t vregA = inst->VRegA_22c(inst_data);
144 switch (field_type) {
145 case Primitive::kPrimInt:
146 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
147 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800148 case Primitive::kPrimBoolean:
149 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
150 break;
151 case Primitive::kPrimByte:
152 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
153 break;
154 case Primitive::kPrimChar:
155 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
156 break;
157 case Primitive::kPrimShort:
158 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
159 break;
Ian Rogers54874942014-06-10 16:31:03 -0700160 case Primitive::kPrimLong:
161 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
162 break;
163 case Primitive::kPrimNot:
164 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
165 break;
166 default:
167 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700168 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700169 }
170 return true;
171}
172
173// Explicitly instantiate all DoIGetQuick functions.
174#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
175 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
176 uint16_t inst_data)
177
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800178EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
179EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
180EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
181EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
182EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
183EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
184EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700185#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
186
187template<Primitive::Type field_type>
188static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
189 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
190 JValue field_value;
191 switch (field_type) {
192 case Primitive::kPrimBoolean:
193 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
194 break;
195 case Primitive::kPrimByte:
196 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
197 break;
198 case Primitive::kPrimChar:
199 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
200 break;
201 case Primitive::kPrimShort:
202 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
203 break;
204 case Primitive::kPrimInt:
205 field_value.SetI(shadow_frame.GetVReg(vreg));
206 break;
207 case Primitive::kPrimLong:
208 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
209 break;
210 case Primitive::kPrimNot:
211 field_value.SetL(shadow_frame.GetVRegReference(vreg));
212 break;
213 default:
214 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700215 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700216 }
217 return field_value;
218}
219
220template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
221 bool transaction_active>
222bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
223 uint16_t inst_data) {
224 bool do_assignability_check = do_access_check;
225 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
226 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
227 ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
Fred Shih37f05ef2014-07-16 18:38:08 -0700228 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -0700229 if (UNLIKELY(f == nullptr)) {
230 CHECK(self->IsExceptionPending());
231 return false;
232 }
233 Object* obj;
234 if (is_static) {
235 obj = f->GetDeclaringClass();
236 } else {
237 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
238 if (UNLIKELY(obj == nullptr)) {
239 ThrowNullPointerExceptionForFieldAccess(shadow_frame.GetCurrentLocationForThrow(),
240 f, false);
241 return false;
242 }
243 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +0200244 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -0700245 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
246 // Report this field access to instrumentation if needed. Since we only have the offset of
247 // the field from the base of the object, we need to look for it first.
248 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
249 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
250 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
251 Object* this_object = f->IsStatic() ? nullptr : obj;
252 instrumentation->FieldWriteEvent(self, this_object, shadow_frame.GetMethod(),
253 shadow_frame.GetDexPC(), f, field_value);
254 }
255 switch (field_type) {
256 case Primitive::kPrimBoolean:
257 f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA));
258 break;
259 case Primitive::kPrimByte:
260 f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA));
261 break;
262 case Primitive::kPrimChar:
263 f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA));
264 break;
265 case Primitive::kPrimShort:
266 f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA));
267 break;
268 case Primitive::kPrimInt:
269 f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA));
270 break;
271 case Primitive::kPrimLong:
272 f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA));
273 break;
274 case Primitive::kPrimNot: {
275 Object* reg = shadow_frame.GetVRegReference(vregA);
276 if (do_assignability_check && reg != nullptr) {
277 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
278 // object in the destructor.
279 Class* field_class;
280 {
281 StackHandleScope<3> hs(self);
282 HandleWrapper<mirror::ArtField> h_f(hs.NewHandleWrapper(&f));
283 HandleWrapper<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
284 HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
Ian Rogers08f1f502014-12-02 15:04:37 -0800285 field_class = h_f->GetType(true);
Ian Rogers54874942014-06-10 16:31:03 -0700286 }
287 if (!reg->VerifierInstanceOf(field_class)) {
288 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700289 std::string temp1, temp2, temp3;
Ian Rogers54874942014-06-10 16:31:03 -0700290 self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(),
291 "Ljava/lang/VirtualMachineError;",
292 "Put '%s' that is not instance of field '%s' in '%s'",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700293 reg->GetClass()->GetDescriptor(&temp1),
294 field_class->GetDescriptor(&temp2),
295 f->GetDeclaringClass()->GetDescriptor(&temp3));
Ian Rogers54874942014-06-10 16:31:03 -0700296 return false;
297 }
298 }
299 f->SetObj<transaction_active>(obj, reg);
300 break;
301 }
302 default:
303 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700304 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700305 }
306 return true;
307}
308
309// Explicitly instantiate all DoFieldPut functions.
310#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
311 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
312 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
313
314#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
315 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
316 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
317 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
318 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
319
320// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700321EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
322EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
323EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
324EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
325EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
326EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
327EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700328
329// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700330EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
331EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
332EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
333EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
334EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
335EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
336EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700337
338#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
339#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
340
341template<Primitive::Type field_type, bool transaction_active>
342bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
343 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
344 if (UNLIKELY(obj == nullptr)) {
345 // We lost the reference to the field index so we cannot get a more
346 // precised exception message.
347 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow());
348 return false;
349 }
350 MemberOffset field_offset(inst->VRegC_22c());
351 const uint32_t vregA = inst->VRegA_22c(inst_data);
352 // Report this field modification to instrumentation if needed. Since we only have the offset of
353 // the field from the base of the object, we need to look for it first.
354 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
355 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
356 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
357 field_offset.Uint32Value());
358 DCHECK(f != nullptr);
359 DCHECK(!f->IsStatic());
360 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
361 instrumentation->FieldWriteEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
362 shadow_frame.GetDexPC(), f, field_value);
363 }
364 // Note: iput-x-quick instructions are only for non-volatile fields.
365 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700366 case Primitive::kPrimBoolean:
367 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
368 break;
369 case Primitive::kPrimByte:
370 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
371 break;
372 case Primitive::kPrimChar:
373 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
374 break;
375 case Primitive::kPrimShort:
376 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
377 break;
Ian Rogers54874942014-06-10 16:31:03 -0700378 case Primitive::kPrimInt:
379 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
380 break;
381 case Primitive::kPrimLong:
382 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
383 break;
384 case Primitive::kPrimNot:
385 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
386 break;
387 default:
388 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700389 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700390 }
391 return true;
392}
393
394// Explicitly instantiate all DoIPutQuick functions.
395#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
396 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
397 const Instruction* inst, \
398 uint16_t inst_data)
399
400#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
401 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
402 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
403
Andreas Gampec8ccf682014-09-29 20:07:43 -0700404EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
405EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
406EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
407EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
408EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
409EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
410EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700411#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
412#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
413
Sebastien Hertz9f102032014-05-23 08:59:42 +0200414/**
415 * Finds the location where this exception will be caught. We search until we reach either the top
416 * frame or a native frame, in which cases this exception is considered uncaught.
417 */
418class CatchLocationFinder : public StackVisitor {
419 public:
420 explicit CatchLocationFinder(Thread* self, Handle<mirror::Throwable>* exception)
421 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
422 : StackVisitor(self, nullptr), self_(self), handle_scope_(self), exception_(exception),
423 catch_method_(handle_scope_.NewHandle<mirror::ArtMethod>(nullptr)),
424 catch_dex_pc_(DexFile::kDexNoIndex), clear_exception_(false) {
425 }
426
427 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
428 mirror::ArtMethod* method = GetMethod();
429 if (method == nullptr) {
430 return true;
431 }
432 if (method->IsRuntimeMethod()) {
433 // Ignore callee save method.
434 DCHECK(method->IsCalleeSaveMethod());
435 return true;
436 }
437 if (method->IsNative()) {
438 return false; // End stack walk.
439 }
440 DCHECK(!method->IsNative());
441 uint32_t dex_pc = GetDexPc();
442 if (dex_pc != DexFile::kDexNoIndex) {
443 uint32_t found_dex_pc;
444 {
445 StackHandleScope<3> hs(self_);
446 Handle<mirror::Class> exception_class(hs.NewHandle((*exception_)->GetClass()));
447 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
448 found_dex_pc = mirror::ArtMethod::FindCatchBlock(h_method, exception_class, dex_pc,
449 &clear_exception_);
450 }
451 if (found_dex_pc != DexFile::kDexNoIndex) {
452 catch_method_.Assign(method);
453 catch_dex_pc_ = found_dex_pc;
454 return false; // End stack walk.
455 }
456 }
457 return true; // Continue stack walk.
458 }
459
460 ArtMethod* GetCatchMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
461 return catch_method_.Get();
462 }
463
464 uint32_t GetCatchDexPc() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
465 return catch_dex_pc_;
466 }
467
468 bool NeedClearException() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
469 return clear_exception_;
470 }
471
472 private:
473 Thread* const self_;
474 StackHandleScope<1> handle_scope_;
475 Handle<mirror::Throwable>* exception_;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700476 MutableHandle<mirror::ArtMethod> catch_method_;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200477 uint32_t catch_dex_pc_;
478 bool clear_exception_;
479
480
481 DISALLOW_COPY_AND_ASSIGN(CatchLocationFinder);
482};
483
Ian Rogers54874942014-06-10 16:31:03 -0700484uint32_t FindNextInstructionFollowingException(Thread* self,
485 ShadowFrame& shadow_frame,
486 uint32_t dex_pc,
Ian Rogers54874942014-06-10 16:31:03 -0700487 const instrumentation::Instrumentation* instrumentation) {
488 self->VerifyStack();
489 ThrowLocation throw_location;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200490 StackHandleScope<3> hs(self);
491 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException(&throw_location)));
492 if (!self->IsExceptionReportedToInstrumentation() && instrumentation->HasExceptionCaughtListeners()) {
493 CatchLocationFinder clf(self, &exception);
494 clf.WalkStack(false);
495 instrumentation->ExceptionCaughtEvent(self, throw_location, clf.GetCatchMethod(),
496 clf.GetCatchDexPc(), exception.Get());
497 self->SetExceptionReportedToInstrumentation(true);
498 }
Ian Rogers54874942014-06-10 16:31:03 -0700499 bool clear_exception = false;
500 uint32_t found_dex_pc;
501 {
Ian Rogers54874942014-06-10 16:31:03 -0700502 Handle<mirror::Class> exception_class(hs.NewHandle(exception->GetClass()));
503 Handle<mirror::ArtMethod> h_method(hs.NewHandle(shadow_frame.GetMethod()));
Ian Rogers54874942014-06-10 16:31:03 -0700504 found_dex_pc = mirror::ArtMethod::FindCatchBlock(h_method, exception_class, dex_pc,
505 &clear_exception);
506 }
507 if (found_dex_pc == DexFile::kDexNoIndex) {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200508 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
Ian Rogers54874942014-06-10 16:31:03 -0700509 shadow_frame.GetMethod(), dex_pc);
510 } else {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200511 if (self->IsExceptionReportedToInstrumentation()) {
512 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
513 shadow_frame.GetMethod(), dex_pc);
514 }
Ian Rogers54874942014-06-10 16:31:03 -0700515 if (clear_exception) {
516 self->ClearException();
517 }
518 }
519 return found_dex_pc;
520}
521
Ian Rogerse94652f2014-12-02 11:13:19 -0800522void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
523 LOG(FATAL) << "Unexpected instruction: "
524 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
525 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700526}
527
Ian Rogerse94652f2014-12-02 11:13:19 -0800528static void UnstartedRuntimeInvoke(Thread* self, const DexFile::CodeItem* code_item,
529 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200530 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200531
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200532// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800533static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
534 size_t dest_reg, size_t src_reg)
535 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200536 // If both register locations contains the same value, the register probably holds a reference.
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700537 // Uint required, so that sign extension does not make this wrong on 64b systems
538 uint32_t src_value = shadow_frame.GetVReg(src_reg);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800539 mirror::Object* o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700540 if (src_value == reinterpret_cast<uintptr_t>(o)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800541 new_shadow_frame->SetVRegReference(dest_reg, o);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200542 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800543 new_shadow_frame->SetVReg(dest_reg, src_value);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200544 }
545}
546
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700547void AbortTransaction(Thread* self, const char* fmt, ...) {
548 CHECK(Runtime::Current()->IsActiveTransaction());
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100549 // Constructs abort message.
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700550 va_list args;
551 va_start(args, fmt);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100552 std::string abort_msg;
553 StringAppendV(&abort_msg, fmt, args);
554 // Throws an exception so we can abort the transaction and rollback every change.
555 Runtime::Current()->AbortTransactionAndThrowInternalError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700556 va_end(args);
557}
558
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200559template<bool is_range, bool do_assignability_check>
Ian Rogerse94652f2014-12-02 11:13:19 -0800560bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200561 const Instruction* inst, uint16_t inst_data, JValue* result) {
562 // Compute method information.
Ian Rogerse94652f2014-12-02 11:13:19 -0800563 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200564 const uint16_t num_ins = (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200565 uint16_t num_regs;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200566 if (LIKELY(code_item != NULL)) {
567 num_regs = code_item->registers_size_;
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200568 DCHECK_EQ(num_ins, code_item->ins_size_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200569 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800570 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200571 num_regs = num_ins;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200572 }
573
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200574 // Allocate shadow frame on the stack.
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700575 const char* old_cause = self->StartAssertNoThreadSuspension("DoCall");
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200576 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
Ian Rogerse94652f2014-12-02 11:13:19 -0800577 ShadowFrame* new_shadow_frame(ShadowFrame::Create(num_regs, &shadow_frame, called_method, 0,
578 memory));
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200579
580 // Initialize new shadow frame.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200581 const size_t first_dest_reg = num_regs - num_ins;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700582 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700583 // Slow path.
584 // We might need to do class loading, which incurs a thread state change to kNative. So
585 // register the shadow frame as under construction and allow suspension again.
586 self->SetShadowFrameUnderConstruction(new_shadow_frame);
587 self->EndAssertNoThreadSuspension(old_cause);
588
589 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200590 // to get the exact type of each reference argument.
Ian Rogerse94652f2014-12-02 11:13:19 -0800591 const DexFile::TypeList* params = new_shadow_frame->GetMethod()->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700592 uint32_t shorty_len = 0;
Ian Rogerse94652f2014-12-02 11:13:19 -0800593 const char* shorty = new_shadow_frame->GetMethod()->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200594
Ian Rogerse94652f2014-12-02 11:13:19 -0800595 // TODO: find a cleaner way to separate non-range and range information without duplicating
596 // code.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200597 uint32_t arg[5]; // only used in invoke-XXX.
598 uint32_t vregC; // only used in invoke-XXX-range.
599 if (is_range) {
600 vregC = inst->VRegC_3rc();
601 } else {
Ian Rogers29a26482014-05-02 15:27:29 -0700602 inst->GetVarArgs(arg, inst_data);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200603 }
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100604
605 // Handle receiver apart since it's not part of the shorty.
606 size_t dest_reg = first_dest_reg;
607 size_t arg_offset = 0;
Ian Rogerse94652f2014-12-02 11:13:19 -0800608 if (!new_shadow_frame->GetMethod()->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700609 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100610 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
611 ++dest_reg;
612 ++arg_offset;
613 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800614 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700615 DCHECK_LT(shorty_pos + 1, shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200616 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
617 switch (shorty[shorty_pos + 1]) {
618 case 'L': {
619 Object* o = shadow_frame.GetVRegReference(src_reg);
620 if (do_assignability_check && o != NULL) {
Ian Rogersa0485602014-12-02 15:48:04 -0800621 Class* arg_type =
622 new_shadow_frame->GetMethod()->GetClassFromTypeIndex(
623 params->GetTypeItem(shorty_pos).type_idx_, true);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200624 if (arg_type == NULL) {
625 CHECK(self->IsExceptionPending());
626 return false;
627 }
628 if (!o->VerifierInstanceOf(arg_type)) {
629 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700630 std::string temp1, temp2;
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200631 self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(),
632 "Ljava/lang/VirtualMachineError;",
633 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -0800634 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700635 o->GetClass()->GetDescriptor(&temp1),
636 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200637 return false;
638 }
Jeff Haoa3faaf42013-09-03 19:07:00 -0700639 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200640 new_shadow_frame->SetVRegReference(dest_reg, o);
641 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700642 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200643 case 'J': case 'D': {
644 uint64_t wide_value = (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << 32) |
645 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
646 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
647 ++dest_reg;
648 ++arg_offset;
649 break;
650 }
651 default:
652 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
653 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200654 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200655 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700656 // We're done with the construction.
657 self->ClearShadowFrameUnderConstruction();
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200658 } else {
659 // Fast path: no extra checks.
660 if (is_range) {
661 const uint16_t first_src_reg = inst->VRegC_3rc();
662 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs;
663 ++dest_reg, ++src_reg) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800664 AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200665 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200666 } else {
667 DCHECK_LE(num_ins, 5U);
668 uint16_t regList = inst->Fetch16(2);
669 uint16_t count = num_ins;
670 if (count == 5) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800671 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + 4U,
672 (inst_data >> 8) & 0x0f);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200673 --count;
674 }
675 for (size_t arg_index = 0; arg_index < count; ++arg_index, regList >>= 4) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800676 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, regList & 0x0f);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200677 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200678 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700679 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200680 }
681
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200682 // Do the call now.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200683 if (LIKELY(Runtime::Current()->IsStarted())) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800684 if (kIsDebugBuild && new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter() == nullptr) {
685 LOG(FATAL) << "Attempt to invoke non-executable method: "
686 << PrettyMethod(new_shadow_frame->GetMethod());
687 UNREACHABLE();
Ian Rogers1d99e452014-01-02 17:36:41 -0800688 }
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800689 if (kIsDebugBuild && Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly() &&
Ian Rogerse94652f2014-12-02 11:13:19 -0800690 !new_shadow_frame->GetMethod()->IsNative() &&
691 !new_shadow_frame->GetMethod()->IsProxyMethod() &&
692 new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter()
693 == artInterpreterToCompiledCodeBridge) {
694 LOG(FATAL) << "Attempt to call compiled code when -Xint: "
695 << PrettyMethod(new_shadow_frame->GetMethod());
696 UNREACHABLE();
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800697 }
Ian Rogerse94652f2014-12-02 11:13:19 -0800698 (new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter())(self, code_item,
699 new_shadow_frame, result);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200700 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800701 UnstartedRuntimeInvoke(self, code_item, new_shadow_frame, result, first_dest_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200702 }
703 return !self->IsExceptionPending();
704}
705
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100706template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200707bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
708 Thread* self, JValue* result) {
709 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
710 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
711 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
712 if (!is_range) {
713 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
714 CHECK_LE(length, 5);
715 }
716 if (UNLIKELY(length < 0)) {
717 ThrowNegativeArraySizeException(length);
718 return false;
719 }
720 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
721 Class* arrayClass = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(),
722 self, false, do_access_check);
723 if (UNLIKELY(arrayClass == NULL)) {
724 DCHECK(self->IsExceptionPending());
725 return false;
726 }
727 CHECK(arrayClass->IsArrayClass());
728 Class* componentClass = arrayClass->GetComponentType();
729 if (UNLIKELY(componentClass->IsPrimitive() && !componentClass->IsPrimitiveInt())) {
730 if (componentClass->IsPrimitiveLong() || componentClass->IsPrimitiveDouble()) {
731 ThrowRuntimeException("Bad filled array request for type %s",
732 PrettyDescriptor(componentClass).c_str());
733 } else {
734 self->ThrowNewExceptionF(shadow_frame.GetCurrentLocationForThrow(),
735 "Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800736 "Found type %s; filled-new-array not implemented for anything but 'int'",
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200737 PrettyDescriptor(componentClass).c_str());
738 }
739 return false;
740 }
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700741 Object* newArray = Array::Alloc<true>(self, arrayClass, length,
742 arrayClass->GetComponentSizeShift(),
Ian Rogers6fac4472014-02-25 17:01:10 -0800743 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200744 if (UNLIKELY(newArray == NULL)) {
745 DCHECK(self->IsExceptionPending());
746 return false;
747 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100748 uint32_t arg[5]; // only used in filled-new-array.
749 uint32_t vregC; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200750 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100751 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200752 } else {
Ian Rogers29a26482014-05-02 15:27:29 -0700753 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100754 }
755 const bool is_primitive_int_component = componentClass->IsPrimitiveInt();
756 for (int32_t i = 0; i < length; ++i) {
757 size_t src_reg = is_range ? vregC + i : arg[i];
758 if (is_primitive_int_component) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100759 newArray->AsIntArray()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +0100760 } else {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100761 newArray->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200762 }
763 }
764
765 result->SetL(newArray);
766 return true;
767}
768
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100769// TODO fix thread analysis: should be SHARED_LOCKS_REQUIRED(Locks::mutator_lock_).
770template<typename T>
771static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count)
772 NO_THREAD_SAFETY_ANALYSIS {
773 Runtime* runtime = Runtime::Current();
774 for (int32_t i = 0; i < count; ++i) {
775 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
776 }
777}
778
779void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
780 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
781 DCHECK(Runtime::Current()->IsActiveTransaction());
782 DCHECK(array != nullptr);
783 DCHECK_LE(count, array->GetLength());
784 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
785 switch (primitive_component_type) {
786 case Primitive::kPrimBoolean:
787 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
788 break;
789 case Primitive::kPrimByte:
790 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
791 break;
792 case Primitive::kPrimChar:
793 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
794 break;
795 case Primitive::kPrimShort:
796 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
797 break;
798 case Primitive::kPrimInt:
799 case Primitive::kPrimFloat:
800 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
801 break;
802 case Primitive::kPrimLong:
803 case Primitive::kPrimDouble:
804 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
805 break;
806 default:
807 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
808 << " in fill-array-data";
809 break;
810 }
811}
812
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200813// Helper function to deal with class loading in an unstarted runtime.
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700814static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className,
815 Handle<mirror::ClassLoader> class_loader, JValue* result,
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200816 const std::string& method_name, bool initialize_class,
817 bool abort_if_not_found)
818 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
819 CHECK(className.Get() != nullptr);
820 std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str()));
821 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
822
823 Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader);
824 if (found == nullptr && abort_if_not_found) {
825 if (!self->IsExceptionPending()) {
826 AbortTransaction(self, "%s failed in un-started runtime for class: %s",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700827 method_name.c_str(), PrettyDescriptor(descriptor.c_str()).c_str());
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200828 }
829 return;
830 }
831 if (found != nullptr && initialize_class) {
832 StackHandleScope<1> hs(self);
833 Handle<mirror::Class> h_class(hs.NewHandle(found));
Ian Rogers7b078e82014-09-10 14:44:24 -0700834 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200835 CHECK(self->IsExceptionPending());
836 return;
837 }
838 }
839 result->SetL(found);
840}
841
Ian Rogerse94652f2014-12-02 11:13:19 -0800842static void UnstartedRuntimeInvoke(Thread* self, const DexFile::CodeItem* code_item,
843 ShadowFrame* shadow_frame,
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200844 JValue* result, size_t arg_offset) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200845 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
846 // problems in core libraries.
847 std::string name(PrettyMethod(shadow_frame->GetMethod()));
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200848 if (name == "java.lang.Class java.lang.Class.forName(java.lang.String)") {
849 // TODO: Support for the other variants that take more arguments should also be added.
850 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset)->AsString();
851 StackHandleScope<1> hs(self);
852 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
853 UnstartedRuntimeFindClass(self, h_class_name, NullHandle<mirror::ClassLoader>(), result, name,
854 true, true);
855 } else if (name == "java.lang.Class java.lang.VMClassLoader.loadClass(java.lang.String, boolean)") {
856 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset)->AsString();
857 StackHandleScope<1> hs(self);
858 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
859 UnstartedRuntimeFindClass(self, h_class_name, NullHandle<mirror::ClassLoader>(), result, name,
860 false, true);
861 } else if (name == "java.lang.Class java.lang.VMClassLoader.findLoadedClass(java.lang.ClassLoader, java.lang.String)") {
862 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
863 mirror::ClassLoader* class_loader =
864 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
865 StackHandleScope<2> hs(self);
866 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
867 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
868 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, name, false, false);
Ian Rogersc45b8b52014-05-03 01:39:59 -0700869 } else if (name == "java.lang.Class java.lang.Void.lookupType()") {
870 result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V'));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200871 } else if (name == "java.lang.Object java.lang.Class.newInstance()") {
872 Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
873 ArtMethod* c = klass->FindDeclaredDirectMethod("<init>", "()V");
874 CHECK(c != NULL);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700875 StackHandleScope<1> hs(self);
876 Handle<Object> obj(hs.NewHandle(klass->AllocObject(self)));
877 CHECK(obj.Get() != NULL);
878 EnterInterpreterFromInvoke(self, c, obj.Get(), NULL, NULL);
879 result->SetL(obj.Get());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200880 } else if (name == "java.lang.reflect.Field java.lang.Class.getDeclaredField(java.lang.String)") {
881 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
882 // going the reflective Dex way.
883 Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800884 String* name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200885 ArtField* found = NULL;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200886 ObjectArray<ArtField>* fields = klass->GetIFields();
887 for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) {
888 ArtField* f = fields->Get(i);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800889 if (name2->Equals(f->GetName())) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200890 found = f;
891 }
892 }
893 if (found == NULL) {
894 fields = klass->GetSFields();
895 for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) {
896 ArtField* f = fields->Get(i);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800897 if (name2->Equals(f->GetName())) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200898 found = f;
899 }
900 }
901 }
902 CHECK(found != NULL)
903 << "Failed to find field in Class.getDeclaredField in un-started runtime. name="
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800904 << name2->ToModifiedUtf8() << " class=" << PrettyDescriptor(klass);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200905 // TODO: getDeclaredField calls GetType once the field is found to ensure a
906 // NoClassDefFoundError is thrown if the field's type cannot be resolved.
907 Class* jlr_Field = self->DecodeJObject(WellKnownClasses::java_lang_reflect_Field)->AsClass();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700908 StackHandleScope<1> hs(self);
909 Handle<Object> field(hs.NewHandle(jlr_Field->AllocNonMovableObject(self)));
910 CHECK(field.Get() != NULL);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200911 ArtMethod* c = jlr_Field->FindDeclaredDirectMethod("<init>", "(Ljava/lang/reflect/ArtField;)V");
912 uint32_t args[1];
Ian Rogersef7d42f2014-01-06 12:55:46 -0800913 args[0] = StackReference<mirror::Object>::FromMirrorPtr(found).AsVRegValue();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700914 EnterInterpreterFromInvoke(self, c, field.Get(), args, NULL);
915 result->SetL(field.Get());
Ian Rogersc45b8b52014-05-03 01:39:59 -0700916 } else if (name == "int java.lang.Object.hashCode()") {
917 Object* obj = shadow_frame->GetVRegReference(arg_offset);
918 result->SetI(obj->IdentityHashCode());
919 } else if (name == "java.lang.String java.lang.reflect.ArtMethod.getMethodName(java.lang.reflect.ArtMethod)") {
Ian Rogers6b14d552014-10-28 21:50:58 -0700920 mirror::ArtMethod* method = shadow_frame->GetVRegReference(arg_offset)->AsArtMethod();
921 result->SetL(method->GetNameAsString(self));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200922 } else if (name == "void java.lang.System.arraycopy(java.lang.Object, int, java.lang.Object, int, int)" ||
923 name == "void java.lang.System.arraycopy(char[], int, char[], int, int)") {
924 // Special case array copying without initializing System.
925 Class* ctype = shadow_frame->GetVRegReference(arg_offset)->GetClass()->GetComponentType();
926 jint srcPos = shadow_frame->GetVReg(arg_offset + 1);
927 jint dstPos = shadow_frame->GetVReg(arg_offset + 3);
928 jint length = shadow_frame->GetVReg(arg_offset + 4);
929 if (!ctype->IsPrimitive()) {
930 ObjectArray<Object>* src = shadow_frame->GetVRegReference(arg_offset)->AsObjectArray<Object>();
931 ObjectArray<Object>* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<Object>();
932 for (jint i = 0; i < length; ++i) {
933 dst->Set(dstPos + i, src->Get(srcPos + i));
934 }
935 } else if (ctype->IsPrimitiveChar()) {
936 CharArray* src = shadow_frame->GetVRegReference(arg_offset)->AsCharArray();
937 CharArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray();
938 for (jint i = 0; i < length; ++i) {
939 dst->Set(dstPos + i, src->Get(srcPos + i));
940 }
941 } else if (ctype->IsPrimitiveInt()) {
942 IntArray* src = shadow_frame->GetVRegReference(arg_offset)->AsIntArray();
943 IntArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsIntArray();
944 for (jint i = 0; i < length; ++i) {
945 dst->Set(dstPos + i, src->Get(srcPos + i));
946 }
947 } else {
Ian Rogersc45b8b52014-05-03 01:39:59 -0700948 self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;",
949 "Unimplemented System.arraycopy for type '%s'",
950 PrettyDescriptor(ctype).c_str());
951 }
952 } else if (name == "java.lang.Object java.lang.ThreadLocal.get()") {
953 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
954 if (caller == "java.lang.String java.lang.IntegralToString.convertInt(java.lang.AbstractStringBuilder, int)") {
955 // Allocate non-threadlocal buffer.
956 result->SetL(mirror::CharArray::Alloc(self, 11));
957 } else {
958 self->ThrowNewException(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;",
959 "Unimplemented ThreadLocal.get");
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200960 }
961 } else {
962 // Not special, continue with regular interpreter execution.
Ian Rogerse94652f2014-12-02 11:13:19 -0800963 artInterpreterToInterpreterBridge(self, code_item, shadow_frame, result);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200964 }
965}
966
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200967// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200968#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
969 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100970 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
971 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +0200972 const Instruction* inst, uint16_t inst_data, \
973 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200974EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
975EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
976EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
977EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
978#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200979
980// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100981#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
982 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
983 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
984 const ShadowFrame& shadow_frame, \
985 Thread* self, JValue* result)
986#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
987 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
988 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
989 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
990 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
991EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
992EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
993#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200994#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
995
996} // namespace interpreter
997} // namespace art