blob: b71236b51170eadd7ab43dcdb01ebdc01a4497f6 [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
Andreas Gampef0e128a2015-02-27 20:08:34 -080019#include <cmath>
20
Andreas Gampe542451c2016-07-26 09:02:02 -070021#include "base/enums.h"
Daniel Mihalyieb076692014-08-22 17:33:31 +020022#include "debugger.h"
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +000023#include "entrypoints/runtime_asm_entrypoints.h"
Tamas Berghammerdd5e5e92016-02-12 16:29:00 +000024#include "jit/jit.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010025#include "jvalue.h"
26#include "method_handles.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010027#include "method_handles-inl.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010028#include "mirror/array-inl.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010029#include "mirror/class.h"
30#include "mirror/method_handle_impl.h"
31#include "reflection.h"
32#include "reflection-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070033#include "stack.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070034#include "unstarted_runtime.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080035#include "verifier/method_verifier.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020036
37namespace art {
38namespace interpreter {
39
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000040void ThrowNullPointerExceptionFromInterpreter() {
41 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -070042}
43
44template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
45bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
46 uint16_t inst_data) {
47 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
48 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010049 ArtField* f =
50 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
51 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -070052 if (UNLIKELY(f == nullptr)) {
53 CHECK(self->IsExceptionPending());
54 return false;
55 }
Mathieu Chartier3398c782016-09-30 10:27:43 -070056 ObjPtr<Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -070057 if (is_static) {
58 obj = f->GetDeclaringClass();
59 } else {
60 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
61 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000062 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers54874942014-06-10 16:31:03 -070063 return false;
64 }
65 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +020066 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -070067 // Report this field access to instrumentation if needed.
68 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
69 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
Mathieu Chartier0715c0b2016-10-03 22:49:46 -070070 StackHandleScope<1> hs(self);
71 // Wrap in handle wrapper in case the listener does thread suspension.
72 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
Mathieu Chartier3398c782016-09-30 10:27:43 -070073 ObjPtr<Object> this_object;
74 if (!f->IsStatic()) {
75 this_object = obj;
76 }
77 instrumentation->FieldReadEvent(self,
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070078 this_object.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -070079 shadow_frame.GetMethod(),
80 shadow_frame.GetDexPC(),
81 f);
Ian Rogers54874942014-06-10 16:31:03 -070082 }
83 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
84 switch (field_type) {
85 case Primitive::kPrimBoolean:
86 shadow_frame.SetVReg(vregA, f->GetBoolean(obj));
87 break;
88 case Primitive::kPrimByte:
89 shadow_frame.SetVReg(vregA, f->GetByte(obj));
90 break;
91 case Primitive::kPrimChar:
92 shadow_frame.SetVReg(vregA, f->GetChar(obj));
93 break;
94 case Primitive::kPrimShort:
95 shadow_frame.SetVReg(vregA, f->GetShort(obj));
96 break;
97 case Primitive::kPrimInt:
98 shadow_frame.SetVReg(vregA, f->GetInt(obj));
99 break;
100 case Primitive::kPrimLong:
101 shadow_frame.SetVRegLong(vregA, f->GetLong(obj));
102 break;
103 case Primitive::kPrimNot:
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700104 shadow_frame.SetVRegReference(vregA, f->GetObject(obj).Ptr());
Ian Rogers54874942014-06-10 16:31:03 -0700105 break;
106 default:
107 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700108 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700109 }
110 return true;
111}
112
113// Explicitly instantiate all DoFieldGet functions.
114#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
115 template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \
116 ShadowFrame& shadow_frame, \
117 const Instruction* inst, \
118 uint16_t inst_data)
119
120#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
121 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \
122 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true);
123
124// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700125EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
126EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
127EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
128EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
129EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
130EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
131EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700132
133// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700134EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
135EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
136EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
137EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
138EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
139EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
140EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700141
142#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
143#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
144
145// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
146// Returns true on success, otherwise throws an exception and returns false.
147template<Primitive::Type field_type>
148bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
149 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
150 if (UNLIKELY(obj == nullptr)) {
151 // We lost the reference to the field index so we cannot get a more
152 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000153 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700154 return false;
155 }
156 MemberOffset field_offset(inst->VRegC_22c());
157 // Report this field access to instrumentation if needed. Since we only have the offset of
158 // the field from the base of the object, we need to look for it first.
159 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
160 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
161 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
162 field_offset.Uint32Value());
163 DCHECK(f != nullptr);
164 DCHECK(!f->IsStatic());
165 instrumentation->FieldReadEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
166 shadow_frame.GetDexPC(), f);
167 }
168 // Note: iget-x-quick instructions are only for non-volatile fields.
169 const uint32_t vregA = inst->VRegA_22c(inst_data);
170 switch (field_type) {
171 case Primitive::kPrimInt:
172 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
173 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800174 case Primitive::kPrimBoolean:
175 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
176 break;
177 case Primitive::kPrimByte:
178 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
179 break;
180 case Primitive::kPrimChar:
181 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
182 break;
183 case Primitive::kPrimShort:
184 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
185 break;
Ian Rogers54874942014-06-10 16:31:03 -0700186 case Primitive::kPrimLong:
187 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
188 break;
189 case Primitive::kPrimNot:
190 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
191 break;
192 default:
193 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700194 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700195 }
196 return true;
197}
198
199// Explicitly instantiate all DoIGetQuick functions.
200#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
201 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
202 uint16_t inst_data)
203
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800204EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
205EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
206EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
207EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
208EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
209EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
210EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700211#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
212
213template<Primitive::Type field_type>
214static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700215 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700216 JValue field_value;
217 switch (field_type) {
218 case Primitive::kPrimBoolean:
219 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
220 break;
221 case Primitive::kPrimByte:
222 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
223 break;
224 case Primitive::kPrimChar:
225 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
226 break;
227 case Primitive::kPrimShort:
228 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
229 break;
230 case Primitive::kPrimInt:
231 field_value.SetI(shadow_frame.GetVReg(vreg));
232 break;
233 case Primitive::kPrimLong:
234 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
235 break;
236 case Primitive::kPrimNot:
237 field_value.SetL(shadow_frame.GetVRegReference(vreg));
238 break;
239 default:
240 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700241 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700242 }
243 return field_value;
244}
245
246template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
247 bool transaction_active>
248bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
249 uint16_t inst_data) {
250 bool do_assignability_check = do_access_check;
251 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
252 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100253 ArtField* f =
254 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
255 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -0700256 if (UNLIKELY(f == nullptr)) {
257 CHECK(self->IsExceptionPending());
258 return false;
259 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700260 ObjPtr<Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -0700261 if (is_static) {
262 obj = f->GetDeclaringClass();
263 } else {
264 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
265 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000266 ThrowNullPointerExceptionForFieldAccess(f, false);
Ian Rogers54874942014-06-10 16:31:03 -0700267 return false;
268 }
269 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +0200270 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -0700271 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
272 // Report this field access to instrumentation if needed. Since we only have the offset of
273 // the field from the base of the object, we need to look for it first.
274 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
275 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
Mathieu Chartier0715c0b2016-10-03 22:49:46 -0700276 StackHandleScope<1> hs(self);
277 // Wrap in handle wrapper in case the listener does thread suspension.
278 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
Ian Rogers54874942014-06-10 16:31:03 -0700279 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700280 ObjPtr<Object> this_object = f->IsStatic() ? nullptr : obj;
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700281 instrumentation->FieldWriteEvent(self, this_object.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -0700282 shadow_frame.GetMethod(),
283 shadow_frame.GetDexPC(),
284 f,
285 field_value);
Ian Rogers54874942014-06-10 16:31:03 -0700286 }
287 switch (field_type) {
288 case Primitive::kPrimBoolean:
289 f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA));
290 break;
291 case Primitive::kPrimByte:
292 f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA));
293 break;
294 case Primitive::kPrimChar:
295 f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA));
296 break;
297 case Primitive::kPrimShort:
298 f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA));
299 break;
300 case Primitive::kPrimInt:
301 f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA));
302 break;
303 case Primitive::kPrimLong:
304 f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA));
305 break;
306 case Primitive::kPrimNot: {
307 Object* reg = shadow_frame.GetVRegReference(vregA);
308 if (do_assignability_check && reg != nullptr) {
309 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
310 // object in the destructor.
Mathieu Chartier3398c782016-09-30 10:27:43 -0700311 ObjPtr<Class> field_class;
Ian Rogers54874942014-06-10 16:31:03 -0700312 {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700313 StackHandleScope<2> hs(self);
Ian Rogers54874942014-06-10 16:31:03 -0700314 HandleWrapper<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
Mathieu Chartier3398c782016-09-30 10:27:43 -0700315 HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700316 field_class = f->GetType<true>();
Ian Rogers54874942014-06-10 16:31:03 -0700317 }
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700318 if (!reg->VerifierInstanceOf(field_class.Ptr())) {
Ian Rogers54874942014-06-10 16:31:03 -0700319 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700320 std::string temp1, temp2, temp3;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000321 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Ian Rogers54874942014-06-10 16:31:03 -0700322 "Put '%s' that is not instance of field '%s' in '%s'",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700323 reg->GetClass()->GetDescriptor(&temp1),
324 field_class->GetDescriptor(&temp2),
325 f->GetDeclaringClass()->GetDescriptor(&temp3));
Ian Rogers54874942014-06-10 16:31:03 -0700326 return false;
327 }
328 }
329 f->SetObj<transaction_active>(obj, reg);
330 break;
331 }
332 default:
333 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700334 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700335 }
336 return true;
337}
338
339// Explicitly instantiate all DoFieldPut functions.
340#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
341 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
342 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
343
344#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
345 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
346 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
347 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
348 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
349
350// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700351EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
352EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
353EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
354EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
355EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
356EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
357EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700358
359// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700360EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
361EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
362EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
363EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
364EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
365EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
366EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700367
368#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
369#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
370
371template<Primitive::Type field_type, bool transaction_active>
372bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
373 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
374 if (UNLIKELY(obj == nullptr)) {
375 // We lost the reference to the field index so we cannot get a more
376 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000377 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700378 return false;
379 }
380 MemberOffset field_offset(inst->VRegC_22c());
381 const uint32_t vregA = inst->VRegA_22c(inst_data);
382 // Report this field modification to instrumentation if needed. Since we only have the offset of
383 // the field from the base of the object, we need to look for it first.
384 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
385 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
386 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
387 field_offset.Uint32Value());
388 DCHECK(f != nullptr);
389 DCHECK(!f->IsStatic());
390 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
391 instrumentation->FieldWriteEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
392 shadow_frame.GetDexPC(), f, field_value);
393 }
394 // Note: iput-x-quick instructions are only for non-volatile fields.
395 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700396 case Primitive::kPrimBoolean:
397 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
398 break;
399 case Primitive::kPrimByte:
400 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
401 break;
402 case Primitive::kPrimChar:
403 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
404 break;
405 case Primitive::kPrimShort:
406 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
407 break;
Ian Rogers54874942014-06-10 16:31:03 -0700408 case Primitive::kPrimInt:
409 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
410 break;
411 case Primitive::kPrimLong:
412 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
413 break;
414 case Primitive::kPrimNot:
415 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
416 break;
417 default:
418 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700419 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700420 }
421 return true;
422}
423
424// Explicitly instantiate all DoIPutQuick functions.
425#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
426 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
427 const Instruction* inst, \
428 uint16_t inst_data)
429
430#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
431 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
432 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
433
Andreas Gampec8ccf682014-09-29 20:07:43 -0700434EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
435EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
436EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
437EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
438EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
439EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
440EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700441#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
442#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
443
Sebastien Hertz520633b2015-09-08 17:03:36 +0200444// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700445uint32_t FindNextInstructionFollowingException(
446 Thread* self, ShadowFrame& shadow_frame, uint32_t dex_pc,
447 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700448 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700449 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000450 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Sebastien Hertz520633b2015-09-08 17:03:36 +0200451 if (instrumentation != nullptr && instrumentation->HasExceptionCaughtListeners()
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000452 && self->IsExceptionThrownByCurrentMethod(exception.Get())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000453 instrumentation->ExceptionCaughtEvent(self, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200454 }
Ian Rogers54874942014-06-10 16:31:03 -0700455 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700456 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
457 hs.NewHandle(exception->GetClass()), dex_pc, &clear_exception);
Sebastien Hertz520633b2015-09-08 17:03:36 +0200458 if (found_dex_pc == DexFile::kDexNoIndex && instrumentation != nullptr) {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000459 // Exception is not caught by the current method. We will unwind to the
460 // caller. Notify any instrumentation listener.
Sebastien Hertz9f102032014-05-23 08:59:42 +0200461 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
Ian Rogers54874942014-06-10 16:31:03 -0700462 shadow_frame.GetMethod(), dex_pc);
463 } else {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000464 // Exception is caught in the current method. We will jump to the found_dex_pc.
Ian Rogers54874942014-06-10 16:31:03 -0700465 if (clear_exception) {
466 self->ClearException();
467 }
468 }
469 return found_dex_pc;
470}
471
Ian Rogerse94652f2014-12-02 11:13:19 -0800472void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
473 LOG(FATAL) << "Unexpected instruction: "
474 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
475 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700476}
477
Sebastien Hertz45b15972015-04-03 16:07:05 +0200478void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700479 va_list args;
480 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200481 AbortTransactionV(self, fmt, args);
482 va_end(args);
483}
484
485void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
486 CHECK(Runtime::Current()->IsActiveTransaction());
487 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100488 std::string abort_msg;
489 StringAppendV(&abort_msg, fmt, args);
490 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200491 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700492}
493
Igor Murashkin158f35c2015-06-10 15:55:30 -0700494// Separate declaration is required solely for the attributes.
Narayan Kamath9823e782016-08-03 12:46:58 +0100495template <bool is_range, bool do_assignability_check>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700496 REQUIRES_SHARED(Locks::mutator_lock_)
Igor Murashkin158f35c2015-06-10 15:55:30 -0700497static inline bool DoCallCommon(ArtMethod* called_method,
498 Thread* self,
499 ShadowFrame& shadow_frame,
500 JValue* result,
501 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +0100502 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -0700503 uint32_t vregC) ALWAYS_INLINE;
504
Narayan Kamath208f8572016-08-03 12:46:58 +0100505// Separate declaration is required solely for the attributes.
506template <bool is_range> REQUIRES_SHARED(Locks::mutator_lock_)
507static inline bool DoCallPolymorphic(ArtMethod* called_method,
508 Handle<mirror::MethodType> callsite_type,
509 Handle<mirror::MethodType> target_type,
510 Thread* self,
511 ShadowFrame& shadow_frame,
512 JValue* result,
513 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
514 uint32_t vregC) ALWAYS_INLINE;
515
Siva Chandra05d24152016-01-05 17:43:17 -0800516void ArtInterpreterToCompiledCodeBridge(Thread* self,
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100517 ArtMethod* caller,
Siva Chandra05d24152016-01-05 17:43:17 -0800518 const DexFile::CodeItem* code_item,
519 ShadowFrame* shadow_frame,
520 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700521 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700522 ArtMethod* method = shadow_frame->GetMethod();
523 // Ensure static methods are initialized.
524 if (method->IsStatic()) {
525 mirror::Class* declaringClass = method->GetDeclaringClass();
526 if (UNLIKELY(!declaringClass->IsInitialized())) {
527 self->PushShadowFrame(shadow_frame);
528 StackHandleScope<1> hs(self);
529 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
530 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
531 true))) {
532 self->PopShadowFrame();
533 DCHECK(self->IsExceptionPending());
534 return;
535 }
536 self->PopShadowFrame();
537 CHECK(h_class->IsInitializing());
538 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
539 method = shadow_frame->GetMethod();
540 }
541 }
542 uint16_t arg_offset = (code_item == nullptr)
543 ? 0
544 : code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100545 jit::Jit* jit = Runtime::Current()->GetJit();
546 if (jit != nullptr && caller != nullptr) {
547 jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
548 }
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700549 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
550 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
Andreas Gampe542451c2016-07-26 09:02:02 -0700551 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700552}
553
Mingyao Yangffedec52016-05-19 10:48:40 -0700554void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
555 uint16_t this_obj_vreg,
556 JValue result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700557 REQUIRES_SHARED(Locks::mutator_lock_) {
Mingyao Yangffedec52016-05-19 10:48:40 -0700558 Object* existing = shadow_frame->GetVRegReference(this_obj_vreg);
559 if (existing == nullptr) {
560 // If it's null, we come from compiled code that was deoptimized. Nothing to do,
561 // as the compiler verified there was no alias.
562 // Set the new string result of the StringFactory.
563 shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
564 return;
565 }
566 // Set the string init result into all aliases.
567 for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
568 if (shadow_frame->GetVRegReference(i) == existing) {
569 DCHECK_EQ(shadow_frame->GetVRegReference(i),
570 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
571 shadow_frame->SetVRegReference(i, result.GetL());
572 DCHECK_EQ(shadow_frame->GetVRegReference(i),
573 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
574 }
575 }
576}
577
Narayan Kamath9823e782016-08-03 12:46:58 +0100578template<bool is_range, bool do_access_check>
579 REQUIRES_SHARED(Locks::mutator_lock_)
580inline bool DoInvokePolymorphic(Thread* self, ShadowFrame& shadow_frame,
581 const Instruction* inst, uint16_t inst_data,
582 JValue* result) {
583 // Invoke-polymorphic instructions always take a receiver. i.e, they are never static.
584 const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc();
585
586 // The method_idx here is the name of the signature polymorphic method that
587 // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact)
588 // and not the method that we'll dispatch to in the end.
589 //
590 // TODO(narayan) We'll have to check in the verifier that this is in fact a
591 // signature polymorphic method so that we disallow calls via invoke-polymorphic
592 // to non sig-poly methods. This would also have the side effect of verifying
593 // that vRegC really is a reference type.
Narayan Kamath208f8572016-08-03 12:46:58 +0100594 StackHandleScope<6> hs(self);
595 Handle<mirror::MethodHandleImpl> method_handle(hs.NewHandle(
596 reinterpret_cast<mirror::MethodHandleImpl*>(shadow_frame.GetVRegReference(vRegC))));
597 if (UNLIKELY(method_handle.Get() == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100598 const int method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc();
599 // Note that the invoke type is kVirtual here because a call to a signature
600 // polymorphic method is shaped like a virtual call at the bytecode level.
601 ThrowNullPointerExceptionForMethodAccess(method_idx, InvokeType::kVirtual);
602
603 result->SetJ(0);
604 return false;
605 }
606
607 // The vRegH value gives the index of the proto_id associated with this
608 // signature polymorphic callsite.
609 const uint32_t callsite_proto_id = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
610
611 // Call through to the classlinker and ask it to resolve the static type associated
612 // with the callsite. This information is stored in the dex cache so it's
613 // guaranteed to be fast after the first resolution.
Narayan Kamath9823e782016-08-03 12:46:58 +0100614 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Narayan Kamath208f8572016-08-03 12:46:58 +0100615 Handle<mirror::Class> caller_class(hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass()));
616 Handle<mirror::MethodType> callsite_type(hs.NewHandle(class_linker->ResolveMethodType(
Narayan Kamath9823e782016-08-03 12:46:58 +0100617 caller_class->GetDexFile(), callsite_proto_id,
618 hs.NewHandle<mirror::DexCache>(caller_class->GetDexCache()),
Narayan Kamath208f8572016-08-03 12:46:58 +0100619 hs.NewHandle<mirror::ClassLoader>(caller_class->GetClassLoader()))));
Narayan Kamath9823e782016-08-03 12:46:58 +0100620
621 // This implies we couldn't resolve one or more types in this method handle.
Narayan Kamath208f8572016-08-03 12:46:58 +0100622 if (UNLIKELY(callsite_type.Get() == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100623 CHECK(self->IsExceptionPending());
624 result->SetJ(0);
625 return false;
626 }
627
Narayan Kamath9823e782016-08-03 12:46:58 +0100628 // Get the method we're actually invoking along with the kind of
629 // invoke that is desired. We don't need to perform access checks at this
630 // point because they would have been performed on our behalf at the point
631 // of creation of the method handle.
632 ArtMethod* called_method = method_handle->GetTargetMethod();
633 const MethodHandleKind handle_kind = method_handle->GetHandleKind();
Narayan Kamath208f8572016-08-03 12:46:58 +0100634 Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType()));
Narayan Kamath9823e782016-08-03 12:46:58 +0100635 CHECK(called_method != nullptr);
Narayan Kamath208f8572016-08-03 12:46:58 +0100636 CHECK(handle_type.Get() != nullptr);
Narayan Kamath9823e782016-08-03 12:46:58 +0100637
638 // We now have to massage the number of inputs to the target function.
639 // It's always one less than the number of inputs to the signature polymorphic
640 // invoke, the first input being a reference to the MethodHandle itself.
641 const uint16_t number_of_inputs =
642 ((is_range) ? inst->VRegA_4rcc(inst_data) : inst->VRegA_45cc(inst_data)) - 1;
643
644 uint32_t arg[Instruction::kMaxVarArgRegs] = {};
645 uint32_t receiver_vregC = 0;
646 if (is_range) {
647 receiver_vregC = (inst->VRegC_4rcc() + 1);
648 } else {
649 inst->GetVarArgs(arg, inst_data);
650 arg[0] = arg[1];
651 arg[1] = arg[2];
652 arg[2] = arg[3];
653 arg[3] = arg[4];
654 arg[4] = 0;
655 receiver_vregC = arg[0];
656 }
657
658 if (IsInvoke(handle_kind)) {
659 if (handle_kind == kInvokeVirtual || handle_kind == kInvokeInterface) {
660 mirror::Object* receiver = shadow_frame.GetVRegReference(receiver_vregC);
661 mirror::Class* declaring_class = called_method->GetDeclaringClass();
662 // Verify that _vRegC is an object reference and of the type expected by
663 // the receiver.
664 called_method = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(
665 called_method, kRuntimePointerSize);
666 if (!VerifyObjectIsClass(receiver, declaring_class)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100667 return false;
668 }
669 } else if (handle_kind == kInvokeDirect) {
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100670 if (called_method->IsConstructor()) {
671 // TODO(narayan) : We need to handle the case where the target method is a
672 // constructor here.
673 UNIMPLEMENTED(FATAL) << "Direct invokes for constructors are not implemented yet.";
674 return false;
675 }
676
677 // Nothing special to do in the case where we're not dealing with a
678 // constructor. It's a private method, and we've already access checked at
679 // the point of creating the handle.
680 } else if (handle_kind == kInvokeSuper) {
681 mirror::Class* declaring_class = called_method->GetDeclaringClass();
682
683 // Note that we're not dynamically dispatching on the type of the receiver
684 // here. We use the static type of the "receiver" object that we've
685 // recorded in the method handle's type, which will be the same as the
686 // special caller that was specified at the point of lookup.
687 mirror::Class* referrer_class = handle_type->GetPTypes()->Get(0);
688 if (!declaring_class->IsInterface()) {
689 mirror::Class* super_class = referrer_class->GetSuperClass();
690 uint16_t vtable_index = called_method->GetMethodIndex();
691 DCHECK(super_class != nullptr);
692 DCHECK(super_class->HasVTable());
693 // Note that super_class is a super of referrer_class and called_method
694 // will always be declared by super_class (or one of its super classes).
695 DCHECK_LT(vtable_index, super_class->GetVTableLength());
696 called_method = super_class->GetVTableEntry(vtable_index, kRuntimePointerSize);
697 } else {
698 called_method = referrer_class->FindVirtualMethodForInterfaceSuper(
699 called_method, kRuntimePointerSize);
700 }
701
702 CHECK(called_method != nullptr);
Narayan Kamath9823e782016-08-03 12:46:58 +0100703 }
704
705 // NOTE: handle_kind == kInvokeStatic needs no special treatment here. We
706 // can directly make the call. handle_kind == kInvokeSuper doesn't have any
707 // particular use and can probably be dropped.
Narayan Kamath208f8572016-08-03 12:46:58 +0100708
709 if (callsite_type->IsExactMatch(handle_type.Get())) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100710 return DoCallCommon<is_range, do_access_check>(
711 called_method, self, shadow_frame, result, number_of_inputs,
712 arg, receiver_vregC);
Narayan Kamath208f8572016-08-03 12:46:58 +0100713 } else {
714 return DoCallPolymorphic<is_range>(
715 called_method, callsite_type, handle_type, self, shadow_frame,
716 result, arg, receiver_vregC);
Narayan Kamath9823e782016-08-03 12:46:58 +0100717 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100718 } else {
719 // TODO(narayan): Implement field getters and setters.
Narayan Kamath9823e782016-08-03 12:46:58 +0100720 UNIMPLEMENTED(FATAL) << "Field references in method handles are not implemented yet.";
721 return false;
722 }
723}
724
Narayan Kamath208f8572016-08-03 12:46:58 +0100725// Calculate the number of ins for a proxy or native method, where we
726// can't just look at the code item.
727static inline size_t GetInsForProxyOrNativeMethod(ArtMethod* method)
728 REQUIRES_SHARED(Locks::mutator_lock_) {
729 DCHECK(method->IsNative() || method->IsProxyMethod());
730
731 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
732 size_t num_ins = 0;
733 // Separate accounting for the receiver, which isn't a part of the
734 // shorty.
735 if (!method->IsStatic()) {
736 ++num_ins;
737 }
738
739 uint32_t shorty_len = 0;
740 const char* shorty = method->GetShorty(&shorty_len);
741 for (size_t i = 1; i < shorty_len; ++i) {
742 const char c = shorty[i];
743 ++num_ins;
744 if (c == 'J' || c == 'D') {
745 ++num_ins;
746 }
747 }
748
749 return num_ins;
750}
751
752template <bool is_range>
753static inline bool DoCallPolymorphic(ArtMethod* called_method,
754 Handle<mirror::MethodType> callsite_type,
755 Handle<mirror::MethodType> target_type,
756 Thread* self,
757 ShadowFrame& shadow_frame,
758 JValue* result,
759 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
760 uint32_t vregC) {
761 // TODO(narayan): Wire in the String.init hacks.
762
763 // Compute method information.
764 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
765
766 // Number of registers for the callee's call frame. Note that for non-exact
767 // invokes, we always derive this information from the callee method. We
768 // cannot guarantee during verification that the number of registers encoded
769 // in the invoke is equal to the number of ins for the callee. This is because
770 // some transformations (such as boxing a long -> Long or wideining an
771 // int -> long will change that number.
772 uint16_t num_regs;
773 size_t first_dest_reg;
774 if (LIKELY(code_item != nullptr)) {
775 num_regs = code_item->registers_size_;
776 first_dest_reg = num_regs - code_item->ins_size_;
777 // Parameter registers go at the end of the shadow frame.
778 DCHECK_NE(first_dest_reg, (size_t)-1);
779 } else {
780 // No local regs for proxy and native methods.
781 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
782 num_regs = GetInsForProxyOrNativeMethod(called_method);
783 first_dest_reg = 0;
784 }
785
786 // Allocate shadow frame on the stack.
787 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
788 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
789 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
790
791 // Thread might be suspended during PerformArgumentConversions due to the
792 // allocations performed during boxing.
793 {
794 ScopedStackedShadowFramePusher pusher(
795 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
796 if (!PerformArgumentConversions<is_range>(self, callsite_type, target_type,
797 shadow_frame, vregC, first_dest_reg,
798 arg, new_shadow_frame, result)) {
799 DCHECK(self->IsExceptionPending());
800 result->SetL(0);
801 return false;
802 }
803 }
804
805 // Do the call now.
806 if (LIKELY(Runtime::Current()->IsStarted())) {
807 ArtMethod* target = new_shadow_frame->GetMethod();
808 if (ClassLinker::ShouldUseInterpreterEntrypoint(
809 target,
810 target->GetEntryPointFromQuickCompiledCode())) {
811 ArtInterpreterToInterpreterBridge(self, code_item, new_shadow_frame, result);
812 } else {
813 ArtInterpreterToCompiledCodeBridge(
814 self, shadow_frame.GetMethod(), code_item, new_shadow_frame, result);
815 }
816 } else {
817 UnstartedRuntime::Invoke(self, code_item, new_shadow_frame, result, first_dest_reg);
818 }
819
820 // TODO(narayan): Perform return value conversions.
821
822 return !self->IsExceptionPending();
823}
824
Igor Murashkin6918bf12015-09-27 19:19:06 -0700825template <bool is_range,
Narayan Kamath370423d2016-10-03 16:51:22 +0100826 bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -0700827static inline bool DoCallCommon(ArtMethod* called_method,
828 Thread* self,
829 ShadowFrame& shadow_frame,
830 JValue* result,
831 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +0100832 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -0700833 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800834 bool string_init = false;
835 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700836 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
837 && called_method->IsConstructor())) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100838 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
Jeff Hao848f70a2014-01-15 13:49:50 -0800839 string_init = true;
840 }
841
Alex Lightdaf58c82016-03-16 23:00:49 +0000842 // Compute method information.
843 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
Igor Murashkin158f35c2015-06-10 15:55:30 -0700844
845 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200846 uint16_t num_regs;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700847 if (LIKELY(code_item != nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200848 num_regs = code_item->registers_size_;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700849 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200850 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800851 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Igor Murashkin158f35c2015-06-10 15:55:30 -0700852 num_regs = number_of_inputs;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200853 }
854
Igor Murashkin158f35c2015-06-10 15:55:30 -0700855 // Hack for String init:
856 //
857 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
858 // invoke-x StringFactory(a, b, c, ...)
859 // by effectively dropping the first virtual register from the invoke.
860 //
861 // (at this point the ArtMethod has already been replaced,
862 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +0000863 //
864 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
865 // to handle the compiler optimization of replacing `this` with null without
866 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700867 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -0700868 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700869 DCHECK_GT(num_regs, 0u); // As the method is an instance method, there should be at least 1.
Igor Murashkina06b49b2015-06-25 15:18:12 -0700870
Igor Murashkin158f35c2015-06-10 15:55:30 -0700871 // The new StringFactory call is static and has one fewer argument.
Igor Murashkina06b49b2015-06-25 15:18:12 -0700872 if (code_item == nullptr) {
873 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
874 num_regs--;
875 } // else ... don't need to change num_regs since it comes up from the string_init's code item
Igor Murashkin158f35c2015-06-10 15:55:30 -0700876 number_of_inputs--;
877
878 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -0700879 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700880 arg[i - 1] = arg[i];
881 }
Igor Murashkin6918bf12015-09-27 19:19:06 -0700882 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700883
884 // Rewrite the non-var-arg case
885 vregC++; // Skips the 0th vreg in the range ("this").
886 }
887
888 // Parameter registers go at the end of the shadow frame.
889 DCHECK_GE(num_regs, number_of_inputs);
890 size_t first_dest_reg = num_regs - number_of_inputs;
891 DCHECK_NE(first_dest_reg, (size_t)-1);
892
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200893 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700894 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -0700895 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700896 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700897 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200898
Igor Murashkin158f35c2015-06-10 15:55:30 -0700899 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -0700900 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700901 // Slow path.
902 // We might need to do class loading, which incurs a thread state change to kNative. So
903 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700904 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +0200905 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700906 self->EndAssertNoThreadSuspension(old_cause);
907
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800908 // ArtMethod here is needed to check type information of the call site against the callee.
909 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
910 //
911 // As a special case for proxy methods, which are not dex-backed,
912 // we have to retrieve type information from the proxy's method
913 // interface method instead (which is dex backed since proxies are never interfaces).
Andreas Gampe542451c2016-07-26 09:02:02 -0700914 ArtMethod* method =
915 new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800916
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700917 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200918 // to get the exact type of each reference argument.
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800919 const DexFile::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700920 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800921 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200922
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100923 // Handle receiver apart since it's not part of the shorty.
924 size_t dest_reg = first_dest_reg;
925 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700926
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800927 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700928 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100929 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
930 ++dest_reg;
931 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -0700932 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100933 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700934
935 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800936 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -0700937 // Skip the 0th 'shorty' type since it represents the return type.
938 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200939 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
940 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700941 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200942 case 'L': {
943 Object* o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700944 if (do_assignability_check && o != nullptr) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700945 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Ian Rogersa0485602014-12-02 15:48:04 -0800946 Class* arg_type =
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800947 method->GetClassFromTypeIndex(
Vladimir Marko05792b92015-08-03 11:56:49 +0100948 params->GetTypeItem(shorty_pos).type_idx_, true /* resolve */, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700949 if (arg_type == nullptr) {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200950 CHECK(self->IsExceptionPending());
951 return false;
952 }
953 if (!o->VerifierInstanceOf(arg_type)) {
954 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700955 std::string temp1, temp2;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000956 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200957 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -0800958 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700959 o->GetClass()->GetDescriptor(&temp1),
960 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200961 return false;
962 }
Jeff Haoa3faaf42013-09-03 19:07:00 -0700963 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200964 new_shadow_frame->SetVRegReference(dest_reg, o);
965 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700966 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700967 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200968 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700969 uint64_t wide_value =
970 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
971 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200972 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700973 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200974 ++dest_reg;
975 ++arg_offset;
976 break;
977 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700978 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200979 default:
980 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
981 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200982 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200983 }
984 } else {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700985 size_t arg_index = 0;
986
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200987 // Fast path: no extra checks.
988 if (is_range) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700989 uint16_t first_src_reg = vregC;
990
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200991 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs;
992 ++dest_reg, ++src_reg) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800993 AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200994 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200995 } else {
Igor Murashkin6918bf12015-09-27 19:19:06 -0700996 DCHECK_LE(number_of_inputs, arraysize(arg));
Igor Murashkin158f35c2015-06-10 15:55:30 -0700997
998 for (; arg_index < number_of_inputs; ++arg_index) {
999 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, arg[arg_index]);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001000 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001001 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001002 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001003 }
1004
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001005 // Do the call now.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001006 if (LIKELY(Runtime::Current()->IsStarted())) {
Tamas Berghammerdd5e5e92016-02-12 16:29:00 +00001007 ArtMethod* target = new_shadow_frame->GetMethod();
1008 if (ClassLinker::ShouldUseInterpreterEntrypoint(
1009 target,
1010 target->GetEntryPointFromQuickCompiledCode())) {
Tamas Berghammerc94a61f2016-02-05 18:09:08 +00001011 ArtInterpreterToInterpreterBridge(self, code_item, new_shadow_frame, result);
Tamas Berghammer3a98aae2016-02-08 20:21:54 +00001012 } else {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +01001013 ArtInterpreterToCompiledCodeBridge(
1014 self, shadow_frame.GetMethod(), code_item, new_shadow_frame, result);
Nicolas Geoffray7070ccd2015-07-08 09:41:54 +00001015 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001016 } else {
Andreas Gampe799681b2015-05-15 19:24:12 -07001017 UnstartedRuntime::Invoke(self, code_item, new_shadow_frame, result, first_dest_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001018 }
Jeff Hao848f70a2014-01-15 13:49:50 -08001019
1020 if (string_init && !self->IsExceptionPending()) {
Mingyao Yangffedec52016-05-19 10:48:40 -07001021 SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001022 }
1023
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001024 return !self->IsExceptionPending();
1025}
1026
Igor Murashkin158f35c2015-06-10 15:55:30 -07001027template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001028bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
1029 const Instruction* inst, uint16_t inst_data, JValue* result) {
1030 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001031 const uint16_t number_of_inputs =
1032 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001033
1034 // TODO: find a cleaner way to separate non-range and range information without duplicating
1035 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -07001036 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001037 uint32_t vregC = 0;
1038 if (is_range) {
1039 vregC = inst->VRegC_3rc();
1040 } else {
1041 vregC = inst->VRegC_35c();
1042 inst->GetVarArgs(arg, inst_data);
1043 }
1044
1045 return DoCallCommon<is_range, do_assignability_check>(
1046 called_method, self, shadow_frame,
1047 result, number_of_inputs, arg, vregC);
1048}
1049
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001050template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001051bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
1052 Thread* self, JValue* result) {
1053 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
1054 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
1055 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
1056 if (!is_range) {
1057 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
1058 CHECK_LE(length, 5);
1059 }
1060 if (UNLIKELY(length < 0)) {
1061 ThrowNegativeArraySizeException(length);
1062 return false;
1063 }
1064 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001065 Class* array_class = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(),
1066 self, false, do_access_check);
1067 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001068 DCHECK(self->IsExceptionPending());
1069 return false;
1070 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001071 CHECK(array_class->IsArrayClass());
1072 Class* component_class = array_class->GetComponentType();
1073 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
1074 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
1075 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001076 ThrowRuntimeException("Bad filled array request for type %s",
David Sehr709b0702016-10-13 09:12:37 -07001077 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001078 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001079 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -08001080 "Found type %s; filled-new-array not implemented for anything but 'int'",
David Sehr709b0702016-10-13 09:12:37 -07001081 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001082 }
1083 return false;
1084 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001085 Object* new_array = Array::Alloc<true>(self, array_class, length,
1086 array_class->GetComponentSizeShift(),
1087 Runtime::Current()->GetHeap()->GetCurrentAllocator());
1088 if (UNLIKELY(new_array == nullptr)) {
1089 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001090 return false;
1091 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001092 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
1093 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001094 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +01001095 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001096 } else {
Ian Rogers29a26482014-05-02 15:27:29 -07001097 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +01001098 }
Sebastien Hertzabff6432014-01-27 18:01:39 +01001099 for (int32_t i = 0; i < length; ++i) {
1100 size_t src_reg = is_range ? vregC + i : arg[i];
1101 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001102 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
1103 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +01001104 } else {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001105 new_array->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(
1106 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001107 }
1108 }
1109
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001110 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001111 return true;
1112}
1113
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001114// TODO fix thread analysis: should be REQUIRES_SHARED(Locks::mutator_lock_).
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001115template<typename T>
1116static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count)
1117 NO_THREAD_SAFETY_ANALYSIS {
1118 Runtime* runtime = Runtime::Current();
1119 for (int32_t i = 0; i < count; ++i) {
1120 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
1121 }
1122}
1123
1124void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001125 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001126 DCHECK(Runtime::Current()->IsActiveTransaction());
1127 DCHECK(array != nullptr);
1128 DCHECK_LE(count, array->GetLength());
1129 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
1130 switch (primitive_component_type) {
1131 case Primitive::kPrimBoolean:
1132 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
1133 break;
1134 case Primitive::kPrimByte:
1135 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
1136 break;
1137 case Primitive::kPrimChar:
1138 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
1139 break;
1140 case Primitive::kPrimShort:
1141 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
1142 break;
1143 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001144 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
1145 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001146 case Primitive::kPrimFloat:
1147 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
1148 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001149 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001150 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
1151 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001152 case Primitive::kPrimDouble:
1153 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
1154 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001155 default:
1156 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
1157 << " in fill-array-data";
1158 break;
1159 }
1160}
1161
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001162// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +02001163#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001164 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001165 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
1166 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +02001167 const Instruction* inst, uint16_t inst_data, \
1168 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001169EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
1170EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
1171EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
1172EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
1173#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001174
Narayan Kamath9823e782016-08-03 12:46:58 +01001175// Explicit DoInvokePolymorphic template function declarations.
1176#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range, _do_assignability_check) \
1177 template REQUIRES_SHARED(Locks::mutator_lock_) \
1178 bool DoInvokePolymorphic<_is_range, _do_assignability_check>( \
1179 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1180 uint16_t inst_data, JValue* result)
1181
1182EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, false);
1183EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, true);
1184EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, false);
1185EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, true);
1186#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1187
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001188// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001189#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001190 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001191 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
1192 const ShadowFrame& shadow_frame, \
1193 Thread* self, JValue* result)
1194#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
1195 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
1196 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
1197 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
1198 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
1199EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
1200EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
1201#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001202#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
1203
1204} // namespace interpreter
1205} // namespace art