blob: a0d712ecbbdec4cdfec6eb297d6154b5deb5de06 [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"
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +010036#include "well_known_classes.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020037
38namespace art {
39namespace interpreter {
40
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000041void ThrowNullPointerExceptionFromInterpreter() {
42 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -070043}
44
45template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
46bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
47 uint16_t inst_data) {
48 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
49 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010050 ArtField* f =
51 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
52 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -070053 if (UNLIKELY(f == nullptr)) {
54 CHECK(self->IsExceptionPending());
55 return false;
56 }
Mathieu Chartier3398c782016-09-30 10:27:43 -070057 ObjPtr<Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -070058 if (is_static) {
59 obj = f->GetDeclaringClass();
60 } else {
61 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
62 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000063 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers54874942014-06-10 16:31:03 -070064 return false;
65 }
66 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +020067 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -070068 // Report this field access to instrumentation if needed.
69 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
70 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
Mathieu Chartier0715c0b2016-10-03 22:49:46 -070071 StackHandleScope<1> hs(self);
72 // Wrap in handle wrapper in case the listener does thread suspension.
73 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
Mathieu Chartier3398c782016-09-30 10:27:43 -070074 ObjPtr<Object> this_object;
75 if (!f->IsStatic()) {
76 this_object = obj;
77 }
78 instrumentation->FieldReadEvent(self,
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070079 this_object.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -070080 shadow_frame.GetMethod(),
81 shadow_frame.GetDexPC(),
82 f);
Ian Rogers54874942014-06-10 16:31:03 -070083 }
84 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
85 switch (field_type) {
86 case Primitive::kPrimBoolean:
87 shadow_frame.SetVReg(vregA, f->GetBoolean(obj));
88 break;
89 case Primitive::kPrimByte:
90 shadow_frame.SetVReg(vregA, f->GetByte(obj));
91 break;
92 case Primitive::kPrimChar:
93 shadow_frame.SetVReg(vregA, f->GetChar(obj));
94 break;
95 case Primitive::kPrimShort:
96 shadow_frame.SetVReg(vregA, f->GetShort(obj));
97 break;
98 case Primitive::kPrimInt:
99 shadow_frame.SetVReg(vregA, f->GetInt(obj));
100 break;
101 case Primitive::kPrimLong:
102 shadow_frame.SetVRegLong(vregA, f->GetLong(obj));
103 break;
104 case Primitive::kPrimNot:
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700105 shadow_frame.SetVRegReference(vregA, f->GetObject(obj).Ptr());
Ian Rogers54874942014-06-10 16:31:03 -0700106 break;
107 default:
108 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700109 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700110 }
111 return true;
112}
113
114// Explicitly instantiate all DoFieldGet functions.
115#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
116 template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \
117 ShadowFrame& shadow_frame, \
118 const Instruction* inst, \
119 uint16_t inst_data)
120
121#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
122 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \
123 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true);
124
125// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700126EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
127EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
128EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
129EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
130EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
131EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
132EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700133
134// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700135EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
136EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
137EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
138EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
139EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
140EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
141EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700142
143#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
144#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
145
146// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
147// Returns true on success, otherwise throws an exception and returns false.
148template<Primitive::Type field_type>
149bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
150 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
151 if (UNLIKELY(obj == nullptr)) {
152 // We lost the reference to the field index so we cannot get a more
153 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000154 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700155 return false;
156 }
157 MemberOffset field_offset(inst->VRegC_22c());
158 // Report this field access to instrumentation if needed. Since we only have the offset of
159 // the field from the base of the object, we need to look for it first.
160 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
161 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
162 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
163 field_offset.Uint32Value());
164 DCHECK(f != nullptr);
165 DCHECK(!f->IsStatic());
166 instrumentation->FieldReadEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
167 shadow_frame.GetDexPC(), f);
168 }
169 // Note: iget-x-quick instructions are only for non-volatile fields.
170 const uint32_t vregA = inst->VRegA_22c(inst_data);
171 switch (field_type) {
172 case Primitive::kPrimInt:
173 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
174 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800175 case Primitive::kPrimBoolean:
176 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
177 break;
178 case Primitive::kPrimByte:
179 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
180 break;
181 case Primitive::kPrimChar:
182 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
183 break;
184 case Primitive::kPrimShort:
185 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
186 break;
Ian Rogers54874942014-06-10 16:31:03 -0700187 case Primitive::kPrimLong:
188 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
189 break;
190 case Primitive::kPrimNot:
191 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
192 break;
193 default:
194 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700195 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700196 }
197 return true;
198}
199
200// Explicitly instantiate all DoIGetQuick functions.
201#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
202 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
203 uint16_t inst_data)
204
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800205EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
206EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
207EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
208EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
209EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
210EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
211EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700212#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
213
214template<Primitive::Type field_type>
215static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700216 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700217 JValue field_value;
218 switch (field_type) {
219 case Primitive::kPrimBoolean:
220 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
221 break;
222 case Primitive::kPrimByte:
223 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
224 break;
225 case Primitive::kPrimChar:
226 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
227 break;
228 case Primitive::kPrimShort:
229 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
230 break;
231 case Primitive::kPrimInt:
232 field_value.SetI(shadow_frame.GetVReg(vreg));
233 break;
234 case Primitive::kPrimLong:
235 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
236 break;
237 case Primitive::kPrimNot:
238 field_value.SetL(shadow_frame.GetVRegReference(vreg));
239 break;
240 default:
241 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700242 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700243 }
244 return field_value;
245}
246
247template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
248 bool transaction_active>
249bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
250 uint16_t inst_data) {
251 bool do_assignability_check = do_access_check;
252 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
253 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100254 ArtField* f =
255 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
256 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -0700257 if (UNLIKELY(f == nullptr)) {
258 CHECK(self->IsExceptionPending());
259 return false;
260 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700261 ObjPtr<Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -0700262 if (is_static) {
263 obj = f->GetDeclaringClass();
264 } else {
265 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
266 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000267 ThrowNullPointerExceptionForFieldAccess(f, false);
Ian Rogers54874942014-06-10 16:31:03 -0700268 return false;
269 }
270 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +0200271 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -0700272 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
273 // Report this field access to instrumentation if needed. Since we only have the offset of
274 // the field from the base of the object, we need to look for it first.
275 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
276 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
Mathieu Chartier0715c0b2016-10-03 22:49:46 -0700277 StackHandleScope<1> hs(self);
278 // Wrap in handle wrapper in case the listener does thread suspension.
279 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
Ian Rogers54874942014-06-10 16:31:03 -0700280 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700281 ObjPtr<Object> this_object = f->IsStatic() ? nullptr : obj;
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700282 instrumentation->FieldWriteEvent(self, this_object.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -0700283 shadow_frame.GetMethod(),
284 shadow_frame.GetDexPC(),
285 f,
286 field_value);
Ian Rogers54874942014-06-10 16:31:03 -0700287 }
288 switch (field_type) {
289 case Primitive::kPrimBoolean:
290 f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA));
291 break;
292 case Primitive::kPrimByte:
293 f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA));
294 break;
295 case Primitive::kPrimChar:
296 f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA));
297 break;
298 case Primitive::kPrimShort:
299 f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA));
300 break;
301 case Primitive::kPrimInt:
302 f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA));
303 break;
304 case Primitive::kPrimLong:
305 f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA));
306 break;
307 case Primitive::kPrimNot: {
308 Object* reg = shadow_frame.GetVRegReference(vregA);
309 if (do_assignability_check && reg != nullptr) {
310 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
311 // object in the destructor.
Mathieu Chartier3398c782016-09-30 10:27:43 -0700312 ObjPtr<Class> field_class;
Ian Rogers54874942014-06-10 16:31:03 -0700313 {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700314 StackHandleScope<2> hs(self);
Ian Rogers54874942014-06-10 16:31:03 -0700315 HandleWrapper<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
Mathieu Chartier3398c782016-09-30 10:27:43 -0700316 HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700317 field_class = f->GetType<true>();
Ian Rogers54874942014-06-10 16:31:03 -0700318 }
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700319 if (!reg->VerifierInstanceOf(field_class.Ptr())) {
Ian Rogers54874942014-06-10 16:31:03 -0700320 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700321 std::string temp1, temp2, temp3;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000322 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Ian Rogers54874942014-06-10 16:31:03 -0700323 "Put '%s' that is not instance of field '%s' in '%s'",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700324 reg->GetClass()->GetDescriptor(&temp1),
325 field_class->GetDescriptor(&temp2),
326 f->GetDeclaringClass()->GetDescriptor(&temp3));
Ian Rogers54874942014-06-10 16:31:03 -0700327 return false;
328 }
329 }
330 f->SetObj<transaction_active>(obj, reg);
331 break;
332 }
333 default:
334 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700335 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700336 }
337 return true;
338}
339
340// Explicitly instantiate all DoFieldPut functions.
341#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
342 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
343 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
344
345#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
346 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
347 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
348 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
349 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
350
351// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700352EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
353EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
354EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
355EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
356EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
357EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
358EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700359
360// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700361EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
362EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
363EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
364EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
365EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
366EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
367EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700368
369#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
370#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
371
372template<Primitive::Type field_type, bool transaction_active>
373bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
374 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
375 if (UNLIKELY(obj == nullptr)) {
376 // We lost the reference to the field index so we cannot get a more
377 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000378 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700379 return false;
380 }
381 MemberOffset field_offset(inst->VRegC_22c());
382 const uint32_t vregA = inst->VRegA_22c(inst_data);
383 // Report this field modification to instrumentation if needed. Since we only have the offset of
384 // the field from the base of the object, we need to look for it first.
385 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
386 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
387 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
388 field_offset.Uint32Value());
389 DCHECK(f != nullptr);
390 DCHECK(!f->IsStatic());
391 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
392 instrumentation->FieldWriteEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
393 shadow_frame.GetDexPC(), f, field_value);
394 }
395 // Note: iput-x-quick instructions are only for non-volatile fields.
396 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700397 case Primitive::kPrimBoolean:
398 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
399 break;
400 case Primitive::kPrimByte:
401 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
402 break;
403 case Primitive::kPrimChar:
404 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
405 break;
406 case Primitive::kPrimShort:
407 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
408 break;
Ian Rogers54874942014-06-10 16:31:03 -0700409 case Primitive::kPrimInt:
410 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
411 break;
412 case Primitive::kPrimLong:
413 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
414 break;
415 case Primitive::kPrimNot:
416 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
417 break;
418 default:
419 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700420 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700421 }
422 return true;
423}
424
425// Explicitly instantiate all DoIPutQuick functions.
426#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
427 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
428 const Instruction* inst, \
429 uint16_t inst_data)
430
431#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
432 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
433 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
434
Andreas Gampec8ccf682014-09-29 20:07:43 -0700435EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
436EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
437EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
438EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
439EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
440EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
441EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700442#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
443#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
444
Sebastien Hertz520633b2015-09-08 17:03:36 +0200445// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700446uint32_t FindNextInstructionFollowingException(
447 Thread* self, ShadowFrame& shadow_frame, uint32_t dex_pc,
448 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700449 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700450 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000451 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Sebastien Hertz520633b2015-09-08 17:03:36 +0200452 if (instrumentation != nullptr && instrumentation->HasExceptionCaughtListeners()
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000453 && self->IsExceptionThrownByCurrentMethod(exception.Get())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000454 instrumentation->ExceptionCaughtEvent(self, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200455 }
Ian Rogers54874942014-06-10 16:31:03 -0700456 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700457 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
458 hs.NewHandle(exception->GetClass()), dex_pc, &clear_exception);
Sebastien Hertz520633b2015-09-08 17:03:36 +0200459 if (found_dex_pc == DexFile::kDexNoIndex && instrumentation != nullptr) {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000460 // Exception is not caught by the current method. We will unwind to the
461 // caller. Notify any instrumentation listener.
Sebastien Hertz9f102032014-05-23 08:59:42 +0200462 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
Ian Rogers54874942014-06-10 16:31:03 -0700463 shadow_frame.GetMethod(), dex_pc);
464 } else {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000465 // Exception is caught in the current method. We will jump to the found_dex_pc.
Ian Rogers54874942014-06-10 16:31:03 -0700466 if (clear_exception) {
467 self->ClearException();
468 }
469 }
470 return found_dex_pc;
471}
472
Ian Rogerse94652f2014-12-02 11:13:19 -0800473void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
474 LOG(FATAL) << "Unexpected instruction: "
475 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
476 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700477}
478
Sebastien Hertz45b15972015-04-03 16:07:05 +0200479void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700480 va_list args;
481 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200482 AbortTransactionV(self, fmt, args);
483 va_end(args);
484}
485
486void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
487 CHECK(Runtime::Current()->IsActiveTransaction());
488 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100489 std::string abort_msg;
490 StringAppendV(&abort_msg, fmt, args);
491 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200492 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700493}
494
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100495// START DECLARATIONS :
496//
497// These additional declarations are required because clang complains
498// about ALWAYS_INLINE (-Werror, -Wgcc-compat) in definitions.
499//
500
Narayan Kamath9823e782016-08-03 12:46:58 +0100501template <bool is_range, bool do_assignability_check>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700502 REQUIRES_SHARED(Locks::mutator_lock_)
Igor Murashkin158f35c2015-06-10 15:55:30 -0700503static inline bool DoCallCommon(ArtMethod* called_method,
504 Thread* self,
505 ShadowFrame& shadow_frame,
506 JValue* result,
507 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +0100508 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -0700509 uint32_t vregC) ALWAYS_INLINE;
510
Narayan Kamath208f8572016-08-03 12:46:58 +0100511template <bool is_range> REQUIRES_SHARED(Locks::mutator_lock_)
512static inline bool DoCallPolymorphic(ArtMethod* called_method,
513 Handle<mirror::MethodType> callsite_type,
514 Handle<mirror::MethodType> target_type,
515 Thread* self,
516 ShadowFrame& shadow_frame,
517 JValue* result,
518 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
519 uint32_t vregC) ALWAYS_INLINE;
520
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100521REQUIRES_SHARED(Locks::mutator_lock_)
522static inline bool DoCallTransform(ArtMethod* called_method,
523 Handle<mirror::MethodType> callsite_type,
524 Thread* self,
525 ShadowFrame& shadow_frame,
526 Handle<mirror::MethodHandleImpl> receiver,
527 JValue* result) ALWAYS_INLINE;
528
529REQUIRES_SHARED(Locks::mutator_lock_)
530inline void PerformCall(Thread* self,
531 const DexFile::CodeItem* code_item,
532 ArtMethod* caller_method,
533 const size_t first_dest_reg,
534 ShadowFrame* callee_frame,
535 JValue* result) ALWAYS_INLINE;
536
537template <bool is_range>
538REQUIRES_SHARED(Locks::mutator_lock_)
539inline void CopyRegisters(ShadowFrame& caller_frame,
540 ShadowFrame* callee_frame,
541 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
542 const size_t first_src_reg,
543 const size_t first_dest_reg,
544 const size_t num_regs) ALWAYS_INLINE;
545
546// END DECLARATIONS.
547
Siva Chandra05d24152016-01-05 17:43:17 -0800548void ArtInterpreterToCompiledCodeBridge(Thread* self,
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100549 ArtMethod* caller,
Siva Chandra05d24152016-01-05 17:43:17 -0800550 const DexFile::CodeItem* code_item,
551 ShadowFrame* shadow_frame,
552 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700553 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700554 ArtMethod* method = shadow_frame->GetMethod();
555 // Ensure static methods are initialized.
556 if (method->IsStatic()) {
557 mirror::Class* declaringClass = method->GetDeclaringClass();
558 if (UNLIKELY(!declaringClass->IsInitialized())) {
559 self->PushShadowFrame(shadow_frame);
560 StackHandleScope<1> hs(self);
561 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
562 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
563 true))) {
564 self->PopShadowFrame();
565 DCHECK(self->IsExceptionPending());
566 return;
567 }
568 self->PopShadowFrame();
569 CHECK(h_class->IsInitializing());
570 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
571 method = shadow_frame->GetMethod();
572 }
573 }
574 uint16_t arg_offset = (code_item == nullptr)
575 ? 0
576 : code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100577 jit::Jit* jit = Runtime::Current()->GetJit();
578 if (jit != nullptr && caller != nullptr) {
579 jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
580 }
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700581 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
582 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
Andreas Gampe542451c2016-07-26 09:02:02 -0700583 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700584}
585
Mingyao Yangffedec52016-05-19 10:48:40 -0700586void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
587 uint16_t this_obj_vreg,
588 JValue result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700589 REQUIRES_SHARED(Locks::mutator_lock_) {
Mingyao Yangffedec52016-05-19 10:48:40 -0700590 Object* existing = shadow_frame->GetVRegReference(this_obj_vreg);
591 if (existing == nullptr) {
592 // If it's null, we come from compiled code that was deoptimized. Nothing to do,
593 // as the compiler verified there was no alias.
594 // Set the new string result of the StringFactory.
595 shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
596 return;
597 }
598 // Set the string init result into all aliases.
599 for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
600 if (shadow_frame->GetVRegReference(i) == existing) {
601 DCHECK_EQ(shadow_frame->GetVRegReference(i),
602 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
603 shadow_frame->SetVRegReference(i, result.GetL());
604 DCHECK_EQ(shadow_frame->GetVRegReference(i),
605 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
606 }
607 }
608}
609
Narayan Kamath9823e782016-08-03 12:46:58 +0100610template<bool is_range, bool do_access_check>
611 REQUIRES_SHARED(Locks::mutator_lock_)
612inline bool DoInvokePolymorphic(Thread* self, ShadowFrame& shadow_frame,
613 const Instruction* inst, uint16_t inst_data,
614 JValue* result) {
615 // Invoke-polymorphic instructions always take a receiver. i.e, they are never static.
616 const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc();
617
618 // The method_idx here is the name of the signature polymorphic method that
619 // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact)
620 // and not the method that we'll dispatch to in the end.
621 //
622 // TODO(narayan) We'll have to check in the verifier that this is in fact a
623 // signature polymorphic method so that we disallow calls via invoke-polymorphic
624 // to non sig-poly methods. This would also have the side effect of verifying
625 // that vRegC really is a reference type.
Narayan Kamath208f8572016-08-03 12:46:58 +0100626 StackHandleScope<6> hs(self);
627 Handle<mirror::MethodHandleImpl> method_handle(hs.NewHandle(
628 reinterpret_cast<mirror::MethodHandleImpl*>(shadow_frame.GetVRegReference(vRegC))));
629 if (UNLIKELY(method_handle.Get() == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100630 const int method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc();
631 // Note that the invoke type is kVirtual here because a call to a signature
632 // polymorphic method is shaped like a virtual call at the bytecode level.
633 ThrowNullPointerExceptionForMethodAccess(method_idx, InvokeType::kVirtual);
634
635 result->SetJ(0);
636 return false;
637 }
638
639 // The vRegH value gives the index of the proto_id associated with this
640 // signature polymorphic callsite.
641 const uint32_t callsite_proto_id = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
642
643 // Call through to the classlinker and ask it to resolve the static type associated
644 // with the callsite. This information is stored in the dex cache so it's
645 // guaranteed to be fast after the first resolution.
Narayan Kamath9823e782016-08-03 12:46:58 +0100646 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Narayan Kamath208f8572016-08-03 12:46:58 +0100647 Handle<mirror::Class> caller_class(hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass()));
648 Handle<mirror::MethodType> callsite_type(hs.NewHandle(class_linker->ResolveMethodType(
Narayan Kamath9823e782016-08-03 12:46:58 +0100649 caller_class->GetDexFile(), callsite_proto_id,
650 hs.NewHandle<mirror::DexCache>(caller_class->GetDexCache()),
Narayan Kamath208f8572016-08-03 12:46:58 +0100651 hs.NewHandle<mirror::ClassLoader>(caller_class->GetClassLoader()))));
Narayan Kamath9823e782016-08-03 12:46:58 +0100652
653 // This implies we couldn't resolve one or more types in this method handle.
Narayan Kamath208f8572016-08-03 12:46:58 +0100654 if (UNLIKELY(callsite_type.Get() == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100655 CHECK(self->IsExceptionPending());
656 result->SetJ(0);
657 return false;
658 }
659
Narayan Kamath9823e782016-08-03 12:46:58 +0100660 // Get the method we're actually invoking along with the kind of
661 // invoke that is desired. We don't need to perform access checks at this
662 // point because they would have been performed on our behalf at the point
663 // of creation of the method handle.
664 ArtMethod* called_method = method_handle->GetTargetMethod();
665 const MethodHandleKind handle_kind = method_handle->GetHandleKind();
Narayan Kamath208f8572016-08-03 12:46:58 +0100666 Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType()));
Narayan Kamath9823e782016-08-03 12:46:58 +0100667 CHECK(called_method != nullptr);
Narayan Kamath208f8572016-08-03 12:46:58 +0100668 CHECK(handle_type.Get() != nullptr);
Narayan Kamath9823e782016-08-03 12:46:58 +0100669
Narayan Kamath9823e782016-08-03 12:46:58 +0100670 uint32_t arg[Instruction::kMaxVarArgRegs] = {};
671 uint32_t receiver_vregC = 0;
672 if (is_range) {
673 receiver_vregC = (inst->VRegC_4rcc() + 1);
674 } else {
675 inst->GetVarArgs(arg, inst_data);
676 arg[0] = arg[1];
677 arg[1] = arg[2];
678 arg[2] = arg[3];
679 arg[3] = arg[4];
680 arg[4] = 0;
681 receiver_vregC = arg[0];
682 }
683
684 if (IsInvoke(handle_kind)) {
685 if (handle_kind == kInvokeVirtual || handle_kind == kInvokeInterface) {
686 mirror::Object* receiver = shadow_frame.GetVRegReference(receiver_vregC);
687 mirror::Class* declaring_class = called_method->GetDeclaringClass();
688 // Verify that _vRegC is an object reference and of the type expected by
689 // the receiver.
690 called_method = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(
691 called_method, kRuntimePointerSize);
692 if (!VerifyObjectIsClass(receiver, declaring_class)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100693 return false;
694 }
695 } else if (handle_kind == kInvokeDirect) {
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100696 if (called_method->IsConstructor()) {
697 // TODO(narayan) : We need to handle the case where the target method is a
698 // constructor here.
699 UNIMPLEMENTED(FATAL) << "Direct invokes for constructors are not implemented yet.";
700 return false;
701 }
702
703 // Nothing special to do in the case where we're not dealing with a
704 // constructor. It's a private method, and we've already access checked at
705 // the point of creating the handle.
706 } else if (handle_kind == kInvokeSuper) {
707 mirror::Class* declaring_class = called_method->GetDeclaringClass();
708
709 // Note that we're not dynamically dispatching on the type of the receiver
710 // here. We use the static type of the "receiver" object that we've
711 // recorded in the method handle's type, which will be the same as the
712 // special caller that was specified at the point of lookup.
713 mirror::Class* referrer_class = handle_type->GetPTypes()->Get(0);
714 if (!declaring_class->IsInterface()) {
715 mirror::Class* super_class = referrer_class->GetSuperClass();
716 uint16_t vtable_index = called_method->GetMethodIndex();
717 DCHECK(super_class != nullptr);
718 DCHECK(super_class->HasVTable());
719 // Note that super_class is a super of referrer_class and called_method
720 // will always be declared by super_class (or one of its super classes).
721 DCHECK_LT(vtable_index, super_class->GetVTableLength());
722 called_method = super_class->GetVTableEntry(vtable_index, kRuntimePointerSize);
723 } else {
724 called_method = referrer_class->FindVirtualMethodForInterfaceSuper(
725 called_method, kRuntimePointerSize);
726 }
727
728 CHECK(called_method != nullptr);
Narayan Kamath9823e782016-08-03 12:46:58 +0100729 }
730
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100731 if (handle_kind == kInvokeTransform) {
732 return DoCallTransform(called_method,
733 callsite_type,
734 self,
735 shadow_frame,
736 method_handle /* receiver */,
737 result);
Narayan Kamath208f8572016-08-03 12:46:58 +0100738 } else {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100739 return DoCallPolymorphic<is_range>(called_method,
740 callsite_type,
741 handle_type,
742 self,
743 shadow_frame,
744 result,
745 arg,
746 receiver_vregC);
Narayan Kamath9823e782016-08-03 12:46:58 +0100747 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100748 } else {
749 // TODO(narayan): Implement field getters and setters.
Narayan Kamath9823e782016-08-03 12:46:58 +0100750 UNIMPLEMENTED(FATAL) << "Field references in method handles are not implemented yet.";
751 return false;
752 }
753}
754
Narayan Kamath208f8572016-08-03 12:46:58 +0100755// Calculate the number of ins for a proxy or native method, where we
756// can't just look at the code item.
757static inline size_t GetInsForProxyOrNativeMethod(ArtMethod* method)
758 REQUIRES_SHARED(Locks::mutator_lock_) {
759 DCHECK(method->IsNative() || method->IsProxyMethod());
760
761 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
762 size_t num_ins = 0;
763 // Separate accounting for the receiver, which isn't a part of the
764 // shorty.
765 if (!method->IsStatic()) {
766 ++num_ins;
767 }
768
769 uint32_t shorty_len = 0;
770 const char* shorty = method->GetShorty(&shorty_len);
771 for (size_t i = 1; i < shorty_len; ++i) {
772 const char c = shorty[i];
773 ++num_ins;
774 if (c == 'J' || c == 'D') {
775 ++num_ins;
776 }
777 }
778
779 return num_ins;
780}
781
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100782
783inline void PerformCall(Thread* self,
784 const DexFile::CodeItem* code_item,
785 ArtMethod* caller_method,
786 const size_t first_dest_reg,
787 ShadowFrame* callee_frame,
788 JValue* result) {
789 if (LIKELY(Runtime::Current()->IsStarted())) {
790 ArtMethod* target = callee_frame->GetMethod();
791 if (ClassLinker::ShouldUseInterpreterEntrypoint(
792 target,
793 target->GetEntryPointFromQuickCompiledCode())) {
794 ArtInterpreterToInterpreterBridge(self, code_item, callee_frame, result);
795 } else {
796 ArtInterpreterToCompiledCodeBridge(
797 self, caller_method, code_item, callee_frame, result);
798 }
799 } else {
800 UnstartedRuntime::Invoke(self, code_item, callee_frame, result, first_dest_reg);
801 }
802}
803
804template <bool is_range>
805inline void CopyRegisters(ShadowFrame& caller_frame,
806 ShadowFrame* callee_frame,
807 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
808 const size_t first_src_reg,
809 const size_t first_dest_reg,
810 const size_t num_regs) {
811 if (is_range) {
812 const size_t dest_reg_bound = first_dest_reg + num_regs;
813 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound;
814 ++dest_reg, ++src_reg) {
815 AssignRegister(callee_frame, caller_frame, dest_reg, src_reg);
816 }
817 } else {
818 DCHECK_LE(num_regs, arraysize(arg));
819
820 for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) {
821 AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]);
822 }
823 }
824}
825
826// Returns true iff. the callsite type for a polymorphic invoke is transformer
827// like, i.e that it has a single input argument whose type is
828// dalvik.system.EmulatedStackFrame.
829static inline bool IsCallerTransformer(Handle<mirror::MethodType> callsite_type)
830 REQUIRES_SHARED(Locks::mutator_lock_) {
831 ObjPtr<mirror::ObjectArray<mirror::Class>> param_types(callsite_type->GetPTypes());
832 if (param_types->GetLength() == 1) {
833 ObjPtr<mirror::Class> param(param_types->GetWithoutChecks(0));
834 return param == WellKnownClasses::ToClass(WellKnownClasses::dalvik_system_EmulatedStackFrame);
835 }
836
837 return false;
838}
839
Narayan Kamath208f8572016-08-03 12:46:58 +0100840template <bool is_range>
841static inline bool DoCallPolymorphic(ArtMethod* called_method,
842 Handle<mirror::MethodType> callsite_type,
843 Handle<mirror::MethodType> target_type,
844 Thread* self,
845 ShadowFrame& shadow_frame,
846 JValue* result,
847 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100848 uint32_t first_src_reg) {
Narayan Kamath208f8572016-08-03 12:46:58 +0100849 // TODO(narayan): Wire in the String.init hacks.
850
851 // Compute method information.
852 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
853
854 // Number of registers for the callee's call frame. Note that for non-exact
855 // invokes, we always derive this information from the callee method. We
856 // cannot guarantee during verification that the number of registers encoded
857 // in the invoke is equal to the number of ins for the callee. This is because
858 // some transformations (such as boxing a long -> Long or wideining an
859 // int -> long will change that number.
860 uint16_t num_regs;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100861 size_t num_input_regs;
Narayan Kamath208f8572016-08-03 12:46:58 +0100862 size_t first_dest_reg;
863 if (LIKELY(code_item != nullptr)) {
864 num_regs = code_item->registers_size_;
865 first_dest_reg = num_regs - code_item->ins_size_;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100866 num_input_regs = code_item->ins_size_;
Narayan Kamath208f8572016-08-03 12:46:58 +0100867 // Parameter registers go at the end of the shadow frame.
868 DCHECK_NE(first_dest_reg, (size_t)-1);
869 } else {
870 // No local regs for proxy and native methods.
871 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100872 num_regs = num_input_regs = GetInsForProxyOrNativeMethod(called_method);
Narayan Kamath208f8572016-08-03 12:46:58 +0100873 first_dest_reg = 0;
874 }
875
876 // Allocate shadow frame on the stack.
877 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
878 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
879 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
880
881 // Thread might be suspended during PerformArgumentConversions due to the
882 // allocations performed during boxing.
883 {
884 ScopedStackedShadowFramePusher pusher(
885 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100886 if (callsite_type->IsExactMatch(target_type.Get())) {
887 // This is an exact invoke, we can take the fast path of just copying all
888 // registers without performing any argument conversions.
889 CopyRegisters<is_range>(shadow_frame,
890 new_shadow_frame,
891 arg,
892 first_src_reg,
893 first_dest_reg,
894 num_input_regs);
895 } else {
896 // This includes the case where we're entering this invoke-polymorphic
897 // from a transformer method. In that case, the callsite_type will contain
898 // a single argument of type dalvik.system.EmulatedStackFrame. In that
899 // case, we'll have to unmarshal the EmulatedStackFrame into the
900 // new_shadow_frame and perform argument conversions on it.
901 if (IsCallerTransformer(callsite_type)) {
902 // The emulated stack frame will be the first ahnd only argument
903 // when we're coming through from a transformer.
904 //
905 // TODO(narayan): This should be a mirror::EmulatedStackFrame after that
906 // type is introduced.
907 ObjPtr<mirror::Object> emulated_stack_frame(
908 shadow_frame.GetVRegReference(first_src_reg));
909 if (!ConvertAndCopyArgumentsFromEmulatedStackFrame<is_range>(self,
910 emulated_stack_frame,
911 target_type,
912 first_dest_reg,
913 new_shadow_frame)) {
914 DCHECK(self->IsExceptionPending());
915 result->SetL(0);
916 return false;
917 }
918 } else if (!ConvertAndCopyArgumentsFromCallerFrame<is_range>(self,
919 callsite_type,
920 target_type,
921 shadow_frame,
922 first_src_reg,
923 first_dest_reg,
924 arg,
925 new_shadow_frame)) {
926 DCHECK(self->IsExceptionPending());
927 result->SetL(0);
928 return false;
929 }
Narayan Kamath208f8572016-08-03 12:46:58 +0100930 }
931 }
932
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100933 PerformCall(self, code_item, shadow_frame.GetMethod(), first_dest_reg, new_shadow_frame, result);
Narayan Kamath208f8572016-08-03 12:46:58 +0100934
935 // TODO(narayan): Perform return value conversions.
936
937 return !self->IsExceptionPending();
938}
939
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100940static inline bool DoCallTransform(ArtMethod* called_method,
941 Handle<mirror::MethodType> callsite_type,
942 Thread* self,
943 ShadowFrame& shadow_frame,
944 Handle<mirror::MethodHandleImpl> receiver,
945 JValue* result) {
946 // This can be fixed, because the method we're calling here
947 // (MethodHandle.transformInternal) doesn't have any locals and the signature
948 // is known :
949 //
950 // private MethodHandle.transformInternal(EmulatedStackFrame sf);
951 //
952 // This means we need only two vregs :
953 // - One for the receiver object.
954 // - One for the only method argument (an EmulatedStackFrame).
955 static constexpr size_t kNumRegsForTransform = 2;
956
957 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
958 DCHECK(code_item != nullptr);
959 DCHECK_EQ(kNumRegsForTransform, code_item->registers_size_);
960 DCHECK_EQ(kNumRegsForTransform, code_item->ins_size_);
961
962 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
963 CREATE_SHADOW_FRAME(kNumRegsForTransform, &shadow_frame, called_method, /* dex pc */ 0);
964 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
965
966 // TODO(narayan): Perform argument conversions first (if this is an inexact invoke), and
967 // then construct an argument list object that's passed through to the
968 // method. Note that the ArgumentList reference is currently a nullptr.
969 //
970 // NOTE(narayan): If the caller is a transformer method (i.e, there is only
971 // one argument and its type is EmulatedStackFrame), we can directly pass that
972 // through without having to do any additional work.
973 UNUSED(callsite_type);
974
975 new_shadow_frame->SetVRegReference(0, receiver.Get());
976 // TODO(narayan): This is the EmulatedStackFrame, currently nullptr.
977 new_shadow_frame->SetVRegReference(1, nullptr);
978
979 PerformCall(self,
980 code_item,
981 shadow_frame.GetMethod(),
982 0 /* first dest reg */,
983 new_shadow_frame,
984 result);
985
986 return !self->IsExceptionPending();
987}
988
Igor Murashkin6918bf12015-09-27 19:19:06 -0700989template <bool is_range,
Narayan Kamath370423d2016-10-03 16:51:22 +0100990 bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -0700991static inline bool DoCallCommon(ArtMethod* called_method,
992 Thread* self,
993 ShadowFrame& shadow_frame,
994 JValue* result,
995 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +0100996 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -0700997 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800998 bool string_init = false;
999 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001000 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
1001 && called_method->IsConstructor())) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001002 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
Jeff Hao848f70a2014-01-15 13:49:50 -08001003 string_init = true;
1004 }
1005
Alex Lightdaf58c82016-03-16 23:00:49 +00001006 // Compute method information.
1007 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
Igor Murashkin158f35c2015-06-10 15:55:30 -07001008
1009 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001010 uint16_t num_regs;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001011 if (LIKELY(code_item != nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001012 num_regs = code_item->registers_size_;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001013 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001014 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -08001015 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Igor Murashkin158f35c2015-06-10 15:55:30 -07001016 num_regs = number_of_inputs;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001017 }
1018
Igor Murashkin158f35c2015-06-10 15:55:30 -07001019 // Hack for String init:
1020 //
1021 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
1022 // invoke-x StringFactory(a, b, c, ...)
1023 // by effectively dropping the first virtual register from the invoke.
1024 //
1025 // (at this point the ArtMethod has already been replaced,
1026 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +00001027 //
1028 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
1029 // to handle the compiler optimization of replacing `this` with null without
1030 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001031 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -07001032 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001033 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 -07001034
Igor Murashkin158f35c2015-06-10 15:55:30 -07001035 // The new StringFactory call is static and has one fewer argument.
Igor Murashkina06b49b2015-06-25 15:18:12 -07001036 if (code_item == nullptr) {
1037 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1038 num_regs--;
1039 } // 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 -07001040 number_of_inputs--;
1041
1042 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -07001043 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001044 arg[i - 1] = arg[i];
1045 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07001046 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001047
1048 // Rewrite the non-var-arg case
1049 vregC++; // Skips the 0th vreg in the range ("this").
1050 }
1051
1052 // Parameter registers go at the end of the shadow frame.
1053 DCHECK_GE(num_regs, number_of_inputs);
1054 size_t first_dest_reg = num_regs - number_of_inputs;
1055 DCHECK_NE(first_dest_reg, (size_t)-1);
1056
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001057 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001058 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -07001059 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -07001060 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -07001061 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001062
Igor Murashkin158f35c2015-06-10 15:55:30 -07001063 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -07001064 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001065 // Slow path.
1066 // We might need to do class loading, which incurs a thread state change to kNative. So
1067 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07001068 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +02001069 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001070 self->EndAssertNoThreadSuspension(old_cause);
1071
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001072 // ArtMethod here is needed to check type information of the call site against the callee.
1073 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
1074 //
1075 // As a special case for proxy methods, which are not dex-backed,
1076 // we have to retrieve type information from the proxy's method
1077 // interface method instead (which is dex backed since proxies are never interfaces).
Andreas Gampe542451c2016-07-26 09:02:02 -07001078 ArtMethod* method =
1079 new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001080
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001081 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001082 // to get the exact type of each reference argument.
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001083 const DexFile::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001084 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001085 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001086
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001087 // Handle receiver apart since it's not part of the shorty.
1088 size_t dest_reg = first_dest_reg;
1089 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001090
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001091 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001092 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001093 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
1094 ++dest_reg;
1095 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -07001096 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001097 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001098
1099 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001100 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001101 // Skip the 0th 'shorty' type since it represents the return type.
1102 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001103 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
1104 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001105 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001106 case 'L': {
1107 Object* o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001108 if (do_assignability_check && o != nullptr) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001109 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Ian Rogersa0485602014-12-02 15:48:04 -08001110 Class* arg_type =
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001111 method->GetClassFromTypeIndex(
Vladimir Marko05792b92015-08-03 11:56:49 +01001112 params->GetTypeItem(shorty_pos).type_idx_, true /* resolve */, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001113 if (arg_type == nullptr) {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001114 CHECK(self->IsExceptionPending());
1115 return false;
1116 }
1117 if (!o->VerifierInstanceOf(arg_type)) {
1118 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -07001119 std::string temp1, temp2;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001120 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001121 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -08001122 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -07001123 o->GetClass()->GetDescriptor(&temp1),
1124 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001125 return false;
1126 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001127 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001128 new_shadow_frame->SetVRegReference(dest_reg, o);
1129 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -07001130 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001131 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001132 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001133 uint64_t wide_value =
1134 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
1135 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001136 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001137 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001138 ++dest_reg;
1139 ++arg_offset;
1140 break;
1141 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001142 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001143 default:
1144 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
1145 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001146 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001147 }
1148 } else {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001149 if (is_range) {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001150 DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001151 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001152
1153 CopyRegisters<is_range>(shadow_frame,
1154 new_shadow_frame,
1155 arg,
1156 vregC,
1157 first_dest_reg,
1158 number_of_inputs);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001159 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001160 }
1161
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001162 PerformCall(self, code_item, shadow_frame.GetMethod(), first_dest_reg, new_shadow_frame, result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001163
1164 if (string_init && !self->IsExceptionPending()) {
Mingyao Yangffedec52016-05-19 10:48:40 -07001165 SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001166 }
1167
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001168 return !self->IsExceptionPending();
1169}
1170
Igor Murashkin158f35c2015-06-10 15:55:30 -07001171template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001172bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
1173 const Instruction* inst, uint16_t inst_data, JValue* result) {
1174 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001175 const uint16_t number_of_inputs =
1176 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001177
1178 // TODO: find a cleaner way to separate non-range and range information without duplicating
1179 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -07001180 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001181 uint32_t vregC = 0;
1182 if (is_range) {
1183 vregC = inst->VRegC_3rc();
1184 } else {
1185 vregC = inst->VRegC_35c();
1186 inst->GetVarArgs(arg, inst_data);
1187 }
1188
1189 return DoCallCommon<is_range, do_assignability_check>(
1190 called_method, self, shadow_frame,
1191 result, number_of_inputs, arg, vregC);
1192}
1193
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001194template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001195bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
1196 Thread* self, JValue* result) {
1197 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
1198 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
1199 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
1200 if (!is_range) {
1201 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
1202 CHECK_LE(length, 5);
1203 }
1204 if (UNLIKELY(length < 0)) {
1205 ThrowNegativeArraySizeException(length);
1206 return false;
1207 }
1208 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001209 Class* array_class = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(),
1210 self, false, do_access_check);
1211 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001212 DCHECK(self->IsExceptionPending());
1213 return false;
1214 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001215 CHECK(array_class->IsArrayClass());
1216 Class* component_class = array_class->GetComponentType();
1217 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
1218 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
1219 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001220 ThrowRuntimeException("Bad filled array request for type %s",
David Sehr709b0702016-10-13 09:12:37 -07001221 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001222 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001223 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -08001224 "Found type %s; filled-new-array not implemented for anything but 'int'",
David Sehr709b0702016-10-13 09:12:37 -07001225 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001226 }
1227 return false;
1228 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001229 Object* new_array = Array::Alloc<true>(self, array_class, length,
1230 array_class->GetComponentSizeShift(),
1231 Runtime::Current()->GetHeap()->GetCurrentAllocator());
1232 if (UNLIKELY(new_array == nullptr)) {
1233 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001234 return false;
1235 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001236 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
1237 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001238 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +01001239 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001240 } else {
Ian Rogers29a26482014-05-02 15:27:29 -07001241 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +01001242 }
Sebastien Hertzabff6432014-01-27 18:01:39 +01001243 for (int32_t i = 0; i < length; ++i) {
1244 size_t src_reg = is_range ? vregC + i : arg[i];
1245 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001246 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
1247 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +01001248 } else {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001249 new_array->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(
1250 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001251 }
1252 }
1253
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001254 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001255 return true;
1256}
1257
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001258// TODO fix thread analysis: should be REQUIRES_SHARED(Locks::mutator_lock_).
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001259template<typename T>
1260static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count)
1261 NO_THREAD_SAFETY_ANALYSIS {
1262 Runtime* runtime = Runtime::Current();
1263 for (int32_t i = 0; i < count; ++i) {
1264 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
1265 }
1266}
1267
1268void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001269 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001270 DCHECK(Runtime::Current()->IsActiveTransaction());
1271 DCHECK(array != nullptr);
1272 DCHECK_LE(count, array->GetLength());
1273 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
1274 switch (primitive_component_type) {
1275 case Primitive::kPrimBoolean:
1276 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
1277 break;
1278 case Primitive::kPrimByte:
1279 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
1280 break;
1281 case Primitive::kPrimChar:
1282 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
1283 break;
1284 case Primitive::kPrimShort:
1285 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
1286 break;
1287 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001288 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
1289 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001290 case Primitive::kPrimFloat:
1291 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
1292 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001293 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001294 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
1295 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001296 case Primitive::kPrimDouble:
1297 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
1298 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001299 default:
1300 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
1301 << " in fill-array-data";
1302 break;
1303 }
1304}
1305
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001306// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +02001307#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001308 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001309 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
1310 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +02001311 const Instruction* inst, uint16_t inst_data, \
1312 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001313EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
1314EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
1315EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
1316EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
1317#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001318
Narayan Kamath9823e782016-08-03 12:46:58 +01001319// Explicit DoInvokePolymorphic template function declarations.
1320#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range, _do_assignability_check) \
1321 template REQUIRES_SHARED(Locks::mutator_lock_) \
1322 bool DoInvokePolymorphic<_is_range, _do_assignability_check>( \
1323 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1324 uint16_t inst_data, JValue* result)
1325
1326EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, false);
1327EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, true);
1328EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, false);
1329EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, true);
1330#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1331
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001332// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001333#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001334 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001335 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
1336 const ShadowFrame& shadow_frame, \
1337 Thread* self, JValue* result)
1338#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
1339 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
1340 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
1341 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
1342 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
1343EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
1344EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
1345#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001346#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
1347
1348} // namespace interpreter
1349} // namespace art