blob: 1ed3d550b997a99e620613b9adc60854cae0285c [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 Chartieref41db72016-10-25 15:08:01 -070057 ObjPtr<mirror::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 Chartieref41db72016-10-25 15:08:01 -070074 ObjPtr<mirror::Object> this_object;
Mathieu Chartier3398c782016-09-30 10:27:43 -070075 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) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700150 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700151 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());
Mathieu Chartiera3147732016-10-26 22:57:02 -0700166 StackHandleScope<1> hs(Thread::Current());
167 // Save obj in case the instrumentation event has thread suspension.
168 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700169 instrumentation->FieldReadEvent(Thread::Current(),
170 obj.Ptr(),
171 shadow_frame.GetMethod(),
172 shadow_frame.GetDexPC(),
173 f);
Ian Rogers54874942014-06-10 16:31:03 -0700174 }
175 // Note: iget-x-quick instructions are only for non-volatile fields.
176 const uint32_t vregA = inst->VRegA_22c(inst_data);
177 switch (field_type) {
178 case Primitive::kPrimInt:
179 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
180 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800181 case Primitive::kPrimBoolean:
182 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
183 break;
184 case Primitive::kPrimByte:
185 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
186 break;
187 case Primitive::kPrimChar:
188 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
189 break;
190 case Primitive::kPrimShort:
191 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
192 break;
Ian Rogers54874942014-06-10 16:31:03 -0700193 case Primitive::kPrimLong:
194 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
195 break;
196 case Primitive::kPrimNot:
197 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
198 break;
199 default:
200 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700201 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700202 }
203 return true;
204}
205
206// Explicitly instantiate all DoIGetQuick functions.
207#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
208 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
209 uint16_t inst_data)
210
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800211EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
212EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
213EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
214EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
215EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
216EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
217EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700218#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
219
220template<Primitive::Type field_type>
221static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700222 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700223 JValue field_value;
224 switch (field_type) {
225 case Primitive::kPrimBoolean:
226 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
227 break;
228 case Primitive::kPrimByte:
229 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
230 break;
231 case Primitive::kPrimChar:
232 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
233 break;
234 case Primitive::kPrimShort:
235 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
236 break;
237 case Primitive::kPrimInt:
238 field_value.SetI(shadow_frame.GetVReg(vreg));
239 break;
240 case Primitive::kPrimLong:
241 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
242 break;
243 case Primitive::kPrimNot:
244 field_value.SetL(shadow_frame.GetVRegReference(vreg));
245 break;
246 default:
247 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700248 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700249 }
250 return field_value;
251}
252
253template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
254 bool transaction_active>
255bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
256 uint16_t inst_data) {
257 bool do_assignability_check = do_access_check;
258 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
259 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100260 ArtField* f =
261 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
262 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -0700263 if (UNLIKELY(f == nullptr)) {
264 CHECK(self->IsExceptionPending());
265 return false;
266 }
Mathieu Chartieref41db72016-10-25 15:08:01 -0700267 ObjPtr<mirror::Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -0700268 if (is_static) {
269 obj = f->GetDeclaringClass();
270 } else {
271 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
272 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000273 ThrowNullPointerExceptionForFieldAccess(f, false);
Ian Rogers54874942014-06-10 16:31:03 -0700274 return false;
275 }
276 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +0200277 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -0700278 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
279 // Report this field access to instrumentation if needed. Since we only have the offset of
280 // the field from the base of the object, we need to look for it first.
281 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
282 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
Mathieu Chartier0715c0b2016-10-03 22:49:46 -0700283 StackHandleScope<1> hs(self);
284 // Wrap in handle wrapper in case the listener does thread suspension.
285 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
Ian Rogers54874942014-06-10 16:31:03 -0700286 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700287 ObjPtr<mirror::Object> this_object = f->IsStatic() ? nullptr : obj;
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700288 instrumentation->FieldWriteEvent(self, this_object.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -0700289 shadow_frame.GetMethod(),
290 shadow_frame.GetDexPC(),
291 f,
292 field_value);
Ian Rogers54874942014-06-10 16:31:03 -0700293 }
294 switch (field_type) {
295 case Primitive::kPrimBoolean:
296 f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA));
297 break;
298 case Primitive::kPrimByte:
299 f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA));
300 break;
301 case Primitive::kPrimChar:
302 f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA));
303 break;
304 case Primitive::kPrimShort:
305 f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA));
306 break;
307 case Primitive::kPrimInt:
308 f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA));
309 break;
310 case Primitive::kPrimLong:
311 f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA));
312 break;
313 case Primitive::kPrimNot: {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700314 ObjPtr<mirror::Object> reg = shadow_frame.GetVRegReference(vregA);
Ian Rogers54874942014-06-10 16:31:03 -0700315 if (do_assignability_check && reg != nullptr) {
316 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
317 // object in the destructor.
Mathieu Chartieref41db72016-10-25 15:08:01 -0700318 ObjPtr<mirror::Class> field_class;
Ian Rogers54874942014-06-10 16:31:03 -0700319 {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700320 StackHandleScope<2> hs(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700321 HandleWrapperObjPtr<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
Mathieu Chartier3398c782016-09-30 10:27:43 -0700322 HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700323 field_class = f->GetType<true>();
Ian Rogers54874942014-06-10 16:31:03 -0700324 }
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700325 if (!reg->VerifierInstanceOf(field_class.Ptr())) {
Ian Rogers54874942014-06-10 16:31:03 -0700326 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700327 std::string temp1, temp2, temp3;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000328 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Ian Rogers54874942014-06-10 16:31:03 -0700329 "Put '%s' that is not instance of field '%s' in '%s'",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700330 reg->GetClass()->GetDescriptor(&temp1),
331 field_class->GetDescriptor(&temp2),
332 f->GetDeclaringClass()->GetDescriptor(&temp3));
Ian Rogers54874942014-06-10 16:31:03 -0700333 return false;
334 }
335 }
336 f->SetObj<transaction_active>(obj, reg);
337 break;
338 }
339 default:
340 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700341 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700342 }
343 return true;
344}
345
346// Explicitly instantiate all DoFieldPut functions.
347#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
348 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
349 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
350
351#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
352 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
353 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
354 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
355 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
356
357// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700358EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
359EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
360EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
361EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
362EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
363EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
364EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700365
366// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700367EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
368EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
369EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
370EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
371EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
372EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
373EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700374
375#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
376#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
377
378template<Primitive::Type field_type, bool transaction_active>
379bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700380 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700381 if (UNLIKELY(obj == nullptr)) {
382 // We lost the reference to the field index so we cannot get a more
383 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000384 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700385 return false;
386 }
387 MemberOffset field_offset(inst->VRegC_22c());
388 const uint32_t vregA = inst->VRegA_22c(inst_data);
389 // Report this field modification to instrumentation if needed. Since we only have the offset of
390 // the field from the base of the object, we need to look for it first.
391 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
392 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
393 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
394 field_offset.Uint32Value());
395 DCHECK(f != nullptr);
396 DCHECK(!f->IsStatic());
397 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700398 StackHandleScope<1> hs(Thread::Current());
399 // Save obj in case the instrumentation event has thread suspension.
400 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700401 instrumentation->FieldWriteEvent(Thread::Current(),
402 obj.Ptr(),
403 shadow_frame.GetMethod(),
404 shadow_frame.GetDexPC(),
405 f,
406 field_value);
Ian Rogers54874942014-06-10 16:31:03 -0700407 }
408 // Note: iput-x-quick instructions are only for non-volatile fields.
409 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700410 case Primitive::kPrimBoolean:
411 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
412 break;
413 case Primitive::kPrimByte:
414 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
415 break;
416 case Primitive::kPrimChar:
417 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
418 break;
419 case Primitive::kPrimShort:
420 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
421 break;
Ian Rogers54874942014-06-10 16:31:03 -0700422 case Primitive::kPrimInt:
423 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
424 break;
425 case Primitive::kPrimLong:
426 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
427 break;
428 case Primitive::kPrimNot:
429 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
430 break;
431 default:
432 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700433 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700434 }
435 return true;
436}
437
438// Explicitly instantiate all DoIPutQuick functions.
439#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
440 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
441 const Instruction* inst, \
442 uint16_t inst_data)
443
444#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
445 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
446 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
447
Andreas Gampec8ccf682014-09-29 20:07:43 -0700448EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
449EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
450EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
451EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
452EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
453EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
454EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700455#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
456#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
457
Sebastien Hertz520633b2015-09-08 17:03:36 +0200458// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700459uint32_t FindNextInstructionFollowingException(
460 Thread* self, ShadowFrame& shadow_frame, uint32_t dex_pc,
461 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700462 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700463 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000464 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Sebastien Hertz520633b2015-09-08 17:03:36 +0200465 if (instrumentation != nullptr && instrumentation->HasExceptionCaughtListeners()
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000466 && self->IsExceptionThrownByCurrentMethod(exception.Get())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000467 instrumentation->ExceptionCaughtEvent(self, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200468 }
Ian Rogers54874942014-06-10 16:31:03 -0700469 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700470 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
471 hs.NewHandle(exception->GetClass()), dex_pc, &clear_exception);
Sebastien Hertz520633b2015-09-08 17:03:36 +0200472 if (found_dex_pc == DexFile::kDexNoIndex && instrumentation != nullptr) {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000473 // Exception is not caught by the current method. We will unwind to the
474 // caller. Notify any instrumentation listener.
Sebastien Hertz9f102032014-05-23 08:59:42 +0200475 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
Ian Rogers54874942014-06-10 16:31:03 -0700476 shadow_frame.GetMethod(), dex_pc);
477 } else {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000478 // Exception is caught in the current method. We will jump to the found_dex_pc.
Ian Rogers54874942014-06-10 16:31:03 -0700479 if (clear_exception) {
480 self->ClearException();
481 }
482 }
483 return found_dex_pc;
484}
485
Ian Rogerse94652f2014-12-02 11:13:19 -0800486void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
487 LOG(FATAL) << "Unexpected instruction: "
488 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
489 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700490}
491
Sebastien Hertz45b15972015-04-03 16:07:05 +0200492void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700493 va_list args;
494 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200495 AbortTransactionV(self, fmt, args);
496 va_end(args);
497}
498
499void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
500 CHECK(Runtime::Current()->IsActiveTransaction());
501 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100502 std::string abort_msg;
503 StringAppendV(&abort_msg, fmt, args);
504 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200505 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700506}
507
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100508// START DECLARATIONS :
509//
510// These additional declarations are required because clang complains
511// about ALWAYS_INLINE (-Werror, -Wgcc-compat) in definitions.
512//
513
Narayan Kamath9823e782016-08-03 12:46:58 +0100514template <bool is_range, bool do_assignability_check>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700515 REQUIRES_SHARED(Locks::mutator_lock_)
Igor Murashkin158f35c2015-06-10 15:55:30 -0700516static inline bool DoCallCommon(ArtMethod* called_method,
517 Thread* self,
518 ShadowFrame& shadow_frame,
519 JValue* result,
520 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +0100521 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -0700522 uint32_t vregC) ALWAYS_INLINE;
523
Narayan Kamath208f8572016-08-03 12:46:58 +0100524template <bool is_range> REQUIRES_SHARED(Locks::mutator_lock_)
525static inline bool DoCallPolymorphic(ArtMethod* called_method,
526 Handle<mirror::MethodType> callsite_type,
527 Handle<mirror::MethodType> target_type,
528 Thread* self,
529 ShadowFrame& shadow_frame,
530 JValue* result,
531 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
532 uint32_t vregC) ALWAYS_INLINE;
533
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100534REQUIRES_SHARED(Locks::mutator_lock_)
535static inline bool DoCallTransform(ArtMethod* called_method,
536 Handle<mirror::MethodType> callsite_type,
537 Thread* self,
538 ShadowFrame& shadow_frame,
539 Handle<mirror::MethodHandleImpl> receiver,
540 JValue* result) ALWAYS_INLINE;
541
542REQUIRES_SHARED(Locks::mutator_lock_)
543inline void PerformCall(Thread* self,
544 const DexFile::CodeItem* code_item,
545 ArtMethod* caller_method,
546 const size_t first_dest_reg,
547 ShadowFrame* callee_frame,
548 JValue* result) ALWAYS_INLINE;
549
550template <bool is_range>
551REQUIRES_SHARED(Locks::mutator_lock_)
552inline void CopyRegisters(ShadowFrame& caller_frame,
553 ShadowFrame* callee_frame,
554 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
555 const size_t first_src_reg,
556 const size_t first_dest_reg,
557 const size_t num_regs) ALWAYS_INLINE;
558
559// END DECLARATIONS.
560
Siva Chandra05d24152016-01-05 17:43:17 -0800561void ArtInterpreterToCompiledCodeBridge(Thread* self,
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100562 ArtMethod* caller,
Siva Chandra05d24152016-01-05 17:43:17 -0800563 const DexFile::CodeItem* code_item,
564 ShadowFrame* shadow_frame,
565 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700566 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700567 ArtMethod* method = shadow_frame->GetMethod();
568 // Ensure static methods are initialized.
569 if (method->IsStatic()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700570 ObjPtr<mirror::Class> declaringClass = method->GetDeclaringClass();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700571 if (UNLIKELY(!declaringClass->IsInitialized())) {
572 self->PushShadowFrame(shadow_frame);
573 StackHandleScope<1> hs(self);
574 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
575 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
576 true))) {
577 self->PopShadowFrame();
578 DCHECK(self->IsExceptionPending());
579 return;
580 }
581 self->PopShadowFrame();
582 CHECK(h_class->IsInitializing());
583 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
584 method = shadow_frame->GetMethod();
585 }
586 }
587 uint16_t arg_offset = (code_item == nullptr)
588 ? 0
589 : code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100590 jit::Jit* jit = Runtime::Current()->GetJit();
591 if (jit != nullptr && caller != nullptr) {
592 jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
593 }
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700594 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
595 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
Andreas Gampe542451c2016-07-26 09:02:02 -0700596 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700597}
598
Mingyao Yangffedec52016-05-19 10:48:40 -0700599void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
600 uint16_t this_obj_vreg,
601 JValue result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700602 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700603 ObjPtr<mirror::Object> existing = shadow_frame->GetVRegReference(this_obj_vreg);
Mingyao Yangffedec52016-05-19 10:48:40 -0700604 if (existing == nullptr) {
605 // If it's null, we come from compiled code that was deoptimized. Nothing to do,
606 // as the compiler verified there was no alias.
607 // Set the new string result of the StringFactory.
608 shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
609 return;
610 }
611 // Set the string init result into all aliases.
612 for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
613 if (shadow_frame->GetVRegReference(i) == existing) {
614 DCHECK_EQ(shadow_frame->GetVRegReference(i),
615 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
616 shadow_frame->SetVRegReference(i, result.GetL());
617 DCHECK_EQ(shadow_frame->GetVRegReference(i),
618 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
619 }
620 }
621}
622
Narayan Kamath9823e782016-08-03 12:46:58 +0100623template<bool is_range, bool do_access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -0700624inline bool DoInvokePolymorphic(Thread* self,
625 ShadowFrame& shadow_frame,
626 const Instruction* inst,
627 uint16_t inst_data,
628 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100629 // Invoke-polymorphic instructions always take a receiver. i.e, they are never static.
630 const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc();
631
632 // The method_idx here is the name of the signature polymorphic method that
633 // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact)
634 // and not the method that we'll dispatch to in the end.
635 //
636 // TODO(narayan) We'll have to check in the verifier that this is in fact a
637 // signature polymorphic method so that we disallow calls via invoke-polymorphic
638 // to non sig-poly methods. This would also have the side effect of verifying
639 // that vRegC really is a reference type.
Narayan Kamath208f8572016-08-03 12:46:58 +0100640 StackHandleScope<6> hs(self);
641 Handle<mirror::MethodHandleImpl> method_handle(hs.NewHandle(
Mathieu Chartieref41db72016-10-25 15:08:01 -0700642 ObjPtr<mirror::MethodHandleImpl>::DownCast(
643 MakeObjPtr(shadow_frame.GetVRegReference(vRegC)))));
Narayan Kamath208f8572016-08-03 12:46:58 +0100644 if (UNLIKELY(method_handle.Get() == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100645 const int method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc();
646 // Note that the invoke type is kVirtual here because a call to a signature
647 // polymorphic method is shaped like a virtual call at the bytecode level.
648 ThrowNullPointerExceptionForMethodAccess(method_idx, InvokeType::kVirtual);
649
650 result->SetJ(0);
651 return false;
652 }
653
654 // The vRegH value gives the index of the proto_id associated with this
655 // signature polymorphic callsite.
656 const uint32_t callsite_proto_id = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
657
658 // Call through to the classlinker and ask it to resolve the static type associated
659 // with the callsite. This information is stored in the dex cache so it's
660 // guaranteed to be fast after the first resolution.
Narayan Kamath9823e782016-08-03 12:46:58 +0100661 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Narayan Kamath208f8572016-08-03 12:46:58 +0100662 Handle<mirror::Class> caller_class(hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass()));
663 Handle<mirror::MethodType> callsite_type(hs.NewHandle(class_linker->ResolveMethodType(
Narayan Kamath9823e782016-08-03 12:46:58 +0100664 caller_class->GetDexFile(), callsite_proto_id,
665 hs.NewHandle<mirror::DexCache>(caller_class->GetDexCache()),
Narayan Kamath208f8572016-08-03 12:46:58 +0100666 hs.NewHandle<mirror::ClassLoader>(caller_class->GetClassLoader()))));
Narayan Kamath9823e782016-08-03 12:46:58 +0100667
668 // This implies we couldn't resolve one or more types in this method handle.
Narayan Kamath208f8572016-08-03 12:46:58 +0100669 if (UNLIKELY(callsite_type.Get() == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100670 CHECK(self->IsExceptionPending());
671 result->SetJ(0);
672 return false;
673 }
674
Narayan Kamath9823e782016-08-03 12:46:58 +0100675 // Get the method we're actually invoking along with the kind of
676 // invoke that is desired. We don't need to perform access checks at this
677 // point because they would have been performed on our behalf at the point
678 // of creation of the method handle.
679 ArtMethod* called_method = method_handle->GetTargetMethod();
680 const MethodHandleKind handle_kind = method_handle->GetHandleKind();
Narayan Kamath208f8572016-08-03 12:46:58 +0100681 Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType()));
Narayan Kamath9823e782016-08-03 12:46:58 +0100682 CHECK(called_method != nullptr);
Narayan Kamath208f8572016-08-03 12:46:58 +0100683 CHECK(handle_type.Get() != nullptr);
Narayan Kamath9823e782016-08-03 12:46:58 +0100684
Narayan Kamath9823e782016-08-03 12:46:58 +0100685 uint32_t arg[Instruction::kMaxVarArgRegs] = {};
686 uint32_t receiver_vregC = 0;
687 if (is_range) {
688 receiver_vregC = (inst->VRegC_4rcc() + 1);
689 } else {
690 inst->GetVarArgs(arg, inst_data);
691 arg[0] = arg[1];
692 arg[1] = arg[2];
693 arg[2] = arg[3];
694 arg[3] = arg[4];
695 arg[4] = 0;
696 receiver_vregC = arg[0];
697 }
698
699 if (IsInvoke(handle_kind)) {
700 if (handle_kind == kInvokeVirtual || handle_kind == kInvokeInterface) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700701 ObjPtr<mirror::Object> receiver = shadow_frame.GetVRegReference(receiver_vregC);
702 ObjPtr<mirror::Class> declaring_class = called_method->GetDeclaringClass();
Narayan Kamath9823e782016-08-03 12:46:58 +0100703 // Verify that _vRegC is an object reference and of the type expected by
704 // the receiver.
705 called_method = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(
706 called_method, kRuntimePointerSize);
707 if (!VerifyObjectIsClass(receiver, declaring_class)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100708 return false;
709 }
710 } else if (handle_kind == kInvokeDirect) {
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100711 if (called_method->IsConstructor()) {
712 // TODO(narayan) : We need to handle the case where the target method is a
713 // constructor here.
714 UNIMPLEMENTED(FATAL) << "Direct invokes for constructors are not implemented yet.";
715 return false;
716 }
717
718 // Nothing special to do in the case where we're not dealing with a
719 // constructor. It's a private method, and we've already access checked at
720 // the point of creating the handle.
721 } else if (handle_kind == kInvokeSuper) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700722 ObjPtr<mirror::Class> declaring_class = called_method->GetDeclaringClass();
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100723
724 // Note that we're not dynamically dispatching on the type of the receiver
725 // here. We use the static type of the "receiver" object that we've
726 // recorded in the method handle's type, which will be the same as the
727 // special caller that was specified at the point of lookup.
Mathieu Chartieref41db72016-10-25 15:08:01 -0700728 ObjPtr<mirror::Class> referrer_class = handle_type->GetPTypes()->Get(0);
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100729 if (!declaring_class->IsInterface()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700730 ObjPtr<mirror::Class> super_class = referrer_class->GetSuperClass();
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100731 uint16_t vtable_index = called_method->GetMethodIndex();
732 DCHECK(super_class != nullptr);
733 DCHECK(super_class->HasVTable());
734 // Note that super_class is a super of referrer_class and called_method
735 // will always be declared by super_class (or one of its super classes).
736 DCHECK_LT(vtable_index, super_class->GetVTableLength());
737 called_method = super_class->GetVTableEntry(vtable_index, kRuntimePointerSize);
738 } else {
739 called_method = referrer_class->FindVirtualMethodForInterfaceSuper(
740 called_method, kRuntimePointerSize);
741 }
742
743 CHECK(called_method != nullptr);
Narayan Kamath9823e782016-08-03 12:46:58 +0100744 }
745
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100746 if (handle_kind == kInvokeTransform) {
747 return DoCallTransform(called_method,
748 callsite_type,
749 self,
750 shadow_frame,
751 method_handle /* receiver */,
752 result);
Narayan Kamath208f8572016-08-03 12:46:58 +0100753 } else {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100754 return DoCallPolymorphic<is_range>(called_method,
755 callsite_type,
756 handle_type,
757 self,
758 shadow_frame,
759 result,
760 arg,
761 receiver_vregC);
Narayan Kamath9823e782016-08-03 12:46:58 +0100762 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100763 } else {
764 // TODO(narayan): Implement field getters and setters.
Narayan Kamath9823e782016-08-03 12:46:58 +0100765 UNIMPLEMENTED(FATAL) << "Field references in method handles are not implemented yet.";
766 return false;
767 }
768}
769
Narayan Kamath208f8572016-08-03 12:46:58 +0100770// Calculate the number of ins for a proxy or native method, where we
771// can't just look at the code item.
772static inline size_t GetInsForProxyOrNativeMethod(ArtMethod* method)
773 REQUIRES_SHARED(Locks::mutator_lock_) {
774 DCHECK(method->IsNative() || method->IsProxyMethod());
775
776 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
777 size_t num_ins = 0;
778 // Separate accounting for the receiver, which isn't a part of the
779 // shorty.
780 if (!method->IsStatic()) {
781 ++num_ins;
782 }
783
784 uint32_t shorty_len = 0;
785 const char* shorty = method->GetShorty(&shorty_len);
786 for (size_t i = 1; i < shorty_len; ++i) {
787 const char c = shorty[i];
788 ++num_ins;
789 if (c == 'J' || c == 'D') {
790 ++num_ins;
791 }
792 }
793
794 return num_ins;
795}
796
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100797
798inline void PerformCall(Thread* self,
799 const DexFile::CodeItem* code_item,
800 ArtMethod* caller_method,
801 const size_t first_dest_reg,
802 ShadowFrame* callee_frame,
803 JValue* result) {
804 if (LIKELY(Runtime::Current()->IsStarted())) {
805 ArtMethod* target = callee_frame->GetMethod();
806 if (ClassLinker::ShouldUseInterpreterEntrypoint(
807 target,
808 target->GetEntryPointFromQuickCompiledCode())) {
809 ArtInterpreterToInterpreterBridge(self, code_item, callee_frame, result);
810 } else {
811 ArtInterpreterToCompiledCodeBridge(
812 self, caller_method, code_item, callee_frame, result);
813 }
814 } else {
815 UnstartedRuntime::Invoke(self, code_item, callee_frame, result, first_dest_reg);
816 }
817}
818
819template <bool is_range>
820inline void CopyRegisters(ShadowFrame& caller_frame,
821 ShadowFrame* callee_frame,
822 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
823 const size_t first_src_reg,
824 const size_t first_dest_reg,
825 const size_t num_regs) {
826 if (is_range) {
827 const size_t dest_reg_bound = first_dest_reg + num_regs;
828 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound;
829 ++dest_reg, ++src_reg) {
830 AssignRegister(callee_frame, caller_frame, dest_reg, src_reg);
831 }
832 } else {
833 DCHECK_LE(num_regs, arraysize(arg));
834
835 for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) {
836 AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]);
837 }
838 }
839}
840
841// Returns true iff. the callsite type for a polymorphic invoke is transformer
842// like, i.e that it has a single input argument whose type is
843// dalvik.system.EmulatedStackFrame.
844static inline bool IsCallerTransformer(Handle<mirror::MethodType> callsite_type)
845 REQUIRES_SHARED(Locks::mutator_lock_) {
846 ObjPtr<mirror::ObjectArray<mirror::Class>> param_types(callsite_type->GetPTypes());
847 if (param_types->GetLength() == 1) {
848 ObjPtr<mirror::Class> param(param_types->GetWithoutChecks(0));
849 return param == WellKnownClasses::ToClass(WellKnownClasses::dalvik_system_EmulatedStackFrame);
850 }
851
852 return false;
853}
854
Narayan Kamath208f8572016-08-03 12:46:58 +0100855template <bool is_range>
856static inline bool DoCallPolymorphic(ArtMethod* called_method,
857 Handle<mirror::MethodType> callsite_type,
858 Handle<mirror::MethodType> target_type,
859 Thread* self,
860 ShadowFrame& shadow_frame,
861 JValue* result,
862 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100863 uint32_t first_src_reg) {
Narayan Kamath208f8572016-08-03 12:46:58 +0100864 // TODO(narayan): Wire in the String.init hacks.
865
866 // Compute method information.
867 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
868
869 // Number of registers for the callee's call frame. Note that for non-exact
870 // invokes, we always derive this information from the callee method. We
871 // cannot guarantee during verification that the number of registers encoded
872 // in the invoke is equal to the number of ins for the callee. This is because
873 // some transformations (such as boxing a long -> Long or wideining an
874 // int -> long will change that number.
875 uint16_t num_regs;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100876 size_t num_input_regs;
Narayan Kamath208f8572016-08-03 12:46:58 +0100877 size_t first_dest_reg;
878 if (LIKELY(code_item != nullptr)) {
879 num_regs = code_item->registers_size_;
880 first_dest_reg = num_regs - code_item->ins_size_;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100881 num_input_regs = code_item->ins_size_;
Narayan Kamath208f8572016-08-03 12:46:58 +0100882 // Parameter registers go at the end of the shadow frame.
883 DCHECK_NE(first_dest_reg, (size_t)-1);
884 } else {
885 // No local regs for proxy and native methods.
886 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100887 num_regs = num_input_regs = GetInsForProxyOrNativeMethod(called_method);
Narayan Kamath208f8572016-08-03 12:46:58 +0100888 first_dest_reg = 0;
889 }
890
891 // Allocate shadow frame on the stack.
892 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
893 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
894 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
895
896 // Thread might be suspended during PerformArgumentConversions due to the
897 // allocations performed during boxing.
898 {
899 ScopedStackedShadowFramePusher pusher(
900 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100901 if (callsite_type->IsExactMatch(target_type.Get())) {
902 // This is an exact invoke, we can take the fast path of just copying all
903 // registers without performing any argument conversions.
904 CopyRegisters<is_range>(shadow_frame,
905 new_shadow_frame,
906 arg,
907 first_src_reg,
908 first_dest_reg,
909 num_input_regs);
910 } else {
911 // This includes the case where we're entering this invoke-polymorphic
912 // from a transformer method. In that case, the callsite_type will contain
913 // a single argument of type dalvik.system.EmulatedStackFrame. In that
914 // case, we'll have to unmarshal the EmulatedStackFrame into the
915 // new_shadow_frame and perform argument conversions on it.
916 if (IsCallerTransformer(callsite_type)) {
917 // The emulated stack frame will be the first ahnd only argument
918 // when we're coming through from a transformer.
919 //
920 // TODO(narayan): This should be a mirror::EmulatedStackFrame after that
921 // type is introduced.
922 ObjPtr<mirror::Object> emulated_stack_frame(
923 shadow_frame.GetVRegReference(first_src_reg));
924 if (!ConvertAndCopyArgumentsFromEmulatedStackFrame<is_range>(self,
925 emulated_stack_frame,
926 target_type,
927 first_dest_reg,
928 new_shadow_frame)) {
929 DCHECK(self->IsExceptionPending());
930 result->SetL(0);
931 return false;
932 }
933 } else if (!ConvertAndCopyArgumentsFromCallerFrame<is_range>(self,
934 callsite_type,
935 target_type,
936 shadow_frame,
937 first_src_reg,
938 first_dest_reg,
939 arg,
940 new_shadow_frame)) {
941 DCHECK(self->IsExceptionPending());
942 result->SetL(0);
943 return false;
944 }
Narayan Kamath208f8572016-08-03 12:46:58 +0100945 }
946 }
947
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100948 PerformCall(self, code_item, shadow_frame.GetMethod(), first_dest_reg, new_shadow_frame, result);
Narayan Kamath208f8572016-08-03 12:46:58 +0100949
950 // TODO(narayan): Perform return value conversions.
951
952 return !self->IsExceptionPending();
953}
954
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100955static inline bool DoCallTransform(ArtMethod* called_method,
956 Handle<mirror::MethodType> callsite_type,
957 Thread* self,
958 ShadowFrame& shadow_frame,
959 Handle<mirror::MethodHandleImpl> receiver,
960 JValue* result) {
961 // This can be fixed, because the method we're calling here
962 // (MethodHandle.transformInternal) doesn't have any locals and the signature
963 // is known :
964 //
965 // private MethodHandle.transformInternal(EmulatedStackFrame sf);
966 //
967 // This means we need only two vregs :
968 // - One for the receiver object.
969 // - One for the only method argument (an EmulatedStackFrame).
970 static constexpr size_t kNumRegsForTransform = 2;
971
972 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
973 DCHECK(code_item != nullptr);
974 DCHECK_EQ(kNumRegsForTransform, code_item->registers_size_);
975 DCHECK_EQ(kNumRegsForTransform, code_item->ins_size_);
976
977 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
978 CREATE_SHADOW_FRAME(kNumRegsForTransform, &shadow_frame, called_method, /* dex pc */ 0);
979 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
980
981 // TODO(narayan): Perform argument conversions first (if this is an inexact invoke), and
982 // then construct an argument list object that's passed through to the
983 // method. Note that the ArgumentList reference is currently a nullptr.
984 //
985 // NOTE(narayan): If the caller is a transformer method (i.e, there is only
986 // one argument and its type is EmulatedStackFrame), we can directly pass that
987 // through without having to do any additional work.
988 UNUSED(callsite_type);
989
990 new_shadow_frame->SetVRegReference(0, receiver.Get());
991 // TODO(narayan): This is the EmulatedStackFrame, currently nullptr.
992 new_shadow_frame->SetVRegReference(1, nullptr);
993
994 PerformCall(self,
995 code_item,
996 shadow_frame.GetMethod(),
997 0 /* first dest reg */,
998 new_shadow_frame,
999 result);
1000
1001 return !self->IsExceptionPending();
1002}
1003
Igor Murashkin6918bf12015-09-27 19:19:06 -07001004template <bool is_range,
Narayan Kamath370423d2016-10-03 16:51:22 +01001005 bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001006static inline bool DoCallCommon(ArtMethod* called_method,
1007 Thread* self,
1008 ShadowFrame& shadow_frame,
1009 JValue* result,
1010 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +01001011 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -07001012 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001013 bool string_init = false;
1014 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001015 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
1016 && called_method->IsConstructor())) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001017 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
Jeff Hao848f70a2014-01-15 13:49:50 -08001018 string_init = true;
1019 }
1020
Alex Lightdaf58c82016-03-16 23:00:49 +00001021 // Compute method information.
1022 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
Igor Murashkin158f35c2015-06-10 15:55:30 -07001023
1024 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001025 uint16_t num_regs;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001026 if (LIKELY(code_item != nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001027 num_regs = code_item->registers_size_;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001028 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001029 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -08001030 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Igor Murashkin158f35c2015-06-10 15:55:30 -07001031 num_regs = number_of_inputs;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001032 }
1033
Igor Murashkin158f35c2015-06-10 15:55:30 -07001034 // Hack for String init:
1035 //
1036 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
1037 // invoke-x StringFactory(a, b, c, ...)
1038 // by effectively dropping the first virtual register from the invoke.
1039 //
1040 // (at this point the ArtMethod has already been replaced,
1041 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +00001042 //
1043 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
1044 // to handle the compiler optimization of replacing `this` with null without
1045 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001046 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -07001047 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001048 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 -07001049
Igor Murashkin158f35c2015-06-10 15:55:30 -07001050 // The new StringFactory call is static and has one fewer argument.
Igor Murashkina06b49b2015-06-25 15:18:12 -07001051 if (code_item == nullptr) {
1052 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1053 num_regs--;
1054 } // 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 -07001055 number_of_inputs--;
1056
1057 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -07001058 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001059 arg[i - 1] = arg[i];
1060 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07001061 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001062
1063 // Rewrite the non-var-arg case
1064 vregC++; // Skips the 0th vreg in the range ("this").
1065 }
1066
1067 // Parameter registers go at the end of the shadow frame.
1068 DCHECK_GE(num_regs, number_of_inputs);
1069 size_t first_dest_reg = num_regs - number_of_inputs;
1070 DCHECK_NE(first_dest_reg, (size_t)-1);
1071
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001072 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001073 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -07001074 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -07001075 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -07001076 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001077
Igor Murashkin158f35c2015-06-10 15:55:30 -07001078 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -07001079 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001080 // Slow path.
1081 // We might need to do class loading, which incurs a thread state change to kNative. So
1082 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07001083 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +02001084 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001085 self->EndAssertNoThreadSuspension(old_cause);
1086
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001087 // ArtMethod here is needed to check type information of the call site against the callee.
1088 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
1089 //
1090 // As a special case for proxy methods, which are not dex-backed,
1091 // we have to retrieve type information from the proxy's method
1092 // interface method instead (which is dex backed since proxies are never interfaces).
Andreas Gampe542451c2016-07-26 09:02:02 -07001093 ArtMethod* method =
1094 new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001095
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001096 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001097 // to get the exact type of each reference argument.
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001098 const DexFile::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001099 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001100 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001101
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001102 // Handle receiver apart since it's not part of the shorty.
1103 size_t dest_reg = first_dest_reg;
1104 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001105
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001106 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001107 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001108 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
1109 ++dest_reg;
1110 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -07001111 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001112 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001113
1114 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001115 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001116 // Skip the 0th 'shorty' type since it represents the return type.
1117 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001118 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
1119 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001120 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001121 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001122 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001123 if (do_assignability_check && o != nullptr) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001124 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001125 const uint32_t type_idx = params->GetTypeItem(shorty_pos).type_idx_;
1126 ObjPtr<mirror::Class> arg_type = method->GetDexCacheResolvedType(type_idx,
1127 pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001128 if (arg_type == nullptr) {
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001129 StackHandleScope<1> hs(self);
1130 // Preserve o since it is used below and GetClassFromTypeIndex may cause thread
1131 // suspension.
1132 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&o);
1133 arg_type = method->GetClassFromTypeIndex(type_idx, true /* resolve */, pointer_size);
1134 if (arg_type == nullptr) {
1135 CHECK(self->IsExceptionPending());
1136 return false;
1137 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001138 }
1139 if (!o->VerifierInstanceOf(arg_type)) {
1140 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -07001141 std::string temp1, temp2;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001142 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001143 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -08001144 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -07001145 o->GetClass()->GetDescriptor(&temp1),
1146 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001147 return false;
1148 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001149 }
Mathieu Chartieref41db72016-10-25 15:08:01 -07001150 new_shadow_frame->SetVRegReference(dest_reg, o.Ptr());
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001151 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -07001152 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001153 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001154 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001155 uint64_t wide_value =
1156 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
1157 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001158 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001159 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001160 ++dest_reg;
1161 ++arg_offset;
1162 break;
1163 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001164 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001165 default:
1166 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
1167 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001168 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001169 }
1170 } else {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001171 if (is_range) {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001172 DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001173 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001174
1175 CopyRegisters<is_range>(shadow_frame,
1176 new_shadow_frame,
1177 arg,
1178 vregC,
1179 first_dest_reg,
1180 number_of_inputs);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001181 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001182 }
1183
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001184 PerformCall(self, code_item, shadow_frame.GetMethod(), first_dest_reg, new_shadow_frame, result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001185
1186 if (string_init && !self->IsExceptionPending()) {
Mingyao Yangffedec52016-05-19 10:48:40 -07001187 SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001188 }
1189
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001190 return !self->IsExceptionPending();
1191}
1192
Igor Murashkin158f35c2015-06-10 15:55:30 -07001193template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001194bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
1195 const Instruction* inst, uint16_t inst_data, JValue* result) {
1196 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001197 const uint16_t number_of_inputs =
1198 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001199
1200 // TODO: find a cleaner way to separate non-range and range information without duplicating
1201 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -07001202 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001203 uint32_t vregC = 0;
1204 if (is_range) {
1205 vregC = inst->VRegC_3rc();
1206 } else {
1207 vregC = inst->VRegC_35c();
1208 inst->GetVarArgs(arg, inst_data);
1209 }
1210
1211 return DoCallCommon<is_range, do_assignability_check>(
1212 called_method, self, shadow_frame,
1213 result, number_of_inputs, arg, vregC);
1214}
1215
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001216template <bool is_range, bool do_access_check, bool transaction_active>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001217bool DoFilledNewArray(const Instruction* inst,
1218 const ShadowFrame& shadow_frame,
1219 Thread* self,
1220 JValue* result) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001221 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
1222 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
1223 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
1224 if (!is_range) {
1225 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
1226 CHECK_LE(length, 5);
1227 }
1228 if (UNLIKELY(length < 0)) {
1229 ThrowNegativeArraySizeException(length);
1230 return false;
1231 }
1232 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartieref41db72016-10-25 15:08:01 -07001233 ObjPtr<mirror::Class> array_class = ResolveVerifyAndClinit(type_idx,
1234 shadow_frame.GetMethod(),
1235 self,
1236 false,
1237 do_access_check);
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001238 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001239 DCHECK(self->IsExceptionPending());
1240 return false;
1241 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001242 CHECK(array_class->IsArrayClass());
Mathieu Chartieref41db72016-10-25 15:08:01 -07001243 ObjPtr<mirror::Class> component_class = array_class->GetComponentType();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001244 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
1245 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
1246 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001247 ThrowRuntimeException("Bad filled array request for type %s",
David Sehr709b0702016-10-13 09:12:37 -07001248 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001249 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001250 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -08001251 "Found type %s; filled-new-array not implemented for anything but 'int'",
David Sehr709b0702016-10-13 09:12:37 -07001252 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001253 }
1254 return false;
1255 }
Mathieu Chartieref41db72016-10-25 15:08:01 -07001256 ObjPtr<mirror::Object> new_array = mirror::Array::Alloc<true>(
1257 self,
1258 array_class,
1259 length,
1260 array_class->GetComponentSizeShift(),
1261 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001262 if (UNLIKELY(new_array == nullptr)) {
1263 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001264 return false;
1265 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001266 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
1267 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001268 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +01001269 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001270 } else {
Ian Rogers29a26482014-05-02 15:27:29 -07001271 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +01001272 }
Sebastien Hertzabff6432014-01-27 18:01:39 +01001273 for (int32_t i = 0; i < length; ++i) {
1274 size_t src_reg = is_range ? vregC + i : arg[i];
1275 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001276 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
1277 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +01001278 } else {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001279 new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<transaction_active>(
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001280 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001281 }
1282 }
1283
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001284 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001285 return true;
1286}
1287
Mathieu Chartieref41db72016-10-25 15:08:01 -07001288// TODO: Use ObjPtr here.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001289template<typename T>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001290static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array,
1291 int32_t count)
1292 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001293 Runtime* runtime = Runtime::Current();
1294 for (int32_t i = 0; i < count; ++i) {
1295 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
1296 }
1297}
1298
Mathieu Chartieref41db72016-10-25 15:08:01 -07001299void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001300 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001301 DCHECK(Runtime::Current()->IsActiveTransaction());
1302 DCHECK(array != nullptr);
1303 DCHECK_LE(count, array->GetLength());
1304 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
1305 switch (primitive_component_type) {
1306 case Primitive::kPrimBoolean:
1307 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
1308 break;
1309 case Primitive::kPrimByte:
1310 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
1311 break;
1312 case Primitive::kPrimChar:
1313 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
1314 break;
1315 case Primitive::kPrimShort:
1316 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
1317 break;
1318 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001319 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
1320 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001321 case Primitive::kPrimFloat:
1322 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
1323 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001324 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001325 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
1326 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001327 case Primitive::kPrimDouble:
1328 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
1329 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001330 default:
1331 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
1332 << " in fill-array-data";
1333 break;
1334 }
1335}
1336
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001337// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +02001338#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001339 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001340 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
1341 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +02001342 const Instruction* inst, uint16_t inst_data, \
1343 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001344EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
1345EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
1346EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
1347EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
1348#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001349
Narayan Kamath9823e782016-08-03 12:46:58 +01001350// Explicit DoInvokePolymorphic template function declarations.
1351#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range, _do_assignability_check) \
1352 template REQUIRES_SHARED(Locks::mutator_lock_) \
1353 bool DoInvokePolymorphic<_is_range, _do_assignability_check>( \
1354 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1355 uint16_t inst_data, JValue* result)
1356
1357EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, false);
1358EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, true);
1359EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, false);
1360EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, true);
1361#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1362
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001363// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001364#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001365 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001366 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
1367 const ShadowFrame& shadow_frame, \
1368 Thread* self, JValue* result)
1369#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
1370 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
1371 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
1372 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
1373 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
1374EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
1375EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
1376#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001377#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
1378
1379} // namespace interpreter
1380} // namespace art