blob: f212cda2a4cda63156b7c661869d3d06439467cd [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 Chartieref41db72016-10-25 15:08:01 -0700166 instrumentation->FieldReadEvent(Thread::Current(),
167 obj.Ptr(),
168 shadow_frame.GetMethod(),
169 shadow_frame.GetDexPC(),
170 f);
Ian Rogers54874942014-06-10 16:31:03 -0700171 }
172 // Note: iget-x-quick instructions are only for non-volatile fields.
173 const uint32_t vregA = inst->VRegA_22c(inst_data);
174 switch (field_type) {
175 case Primitive::kPrimInt:
176 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
177 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800178 case Primitive::kPrimBoolean:
179 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
180 break;
181 case Primitive::kPrimByte:
182 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
183 break;
184 case Primitive::kPrimChar:
185 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
186 break;
187 case Primitive::kPrimShort:
188 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
189 break;
Ian Rogers54874942014-06-10 16:31:03 -0700190 case Primitive::kPrimLong:
191 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
192 break;
193 case Primitive::kPrimNot:
194 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
195 break;
196 default:
197 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700198 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700199 }
200 return true;
201}
202
203// Explicitly instantiate all DoIGetQuick functions.
204#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
205 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
206 uint16_t inst_data)
207
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800208EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
209EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
210EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
211EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
212EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
213EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
214EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700215#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
216
217template<Primitive::Type field_type>
218static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700219 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700220 JValue field_value;
221 switch (field_type) {
222 case Primitive::kPrimBoolean:
223 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
224 break;
225 case Primitive::kPrimByte:
226 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
227 break;
228 case Primitive::kPrimChar:
229 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
230 break;
231 case Primitive::kPrimShort:
232 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
233 break;
234 case Primitive::kPrimInt:
235 field_value.SetI(shadow_frame.GetVReg(vreg));
236 break;
237 case Primitive::kPrimLong:
238 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
239 break;
240 case Primitive::kPrimNot:
241 field_value.SetL(shadow_frame.GetVRegReference(vreg));
242 break;
243 default:
244 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700245 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700246 }
247 return field_value;
248}
249
250template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
251 bool transaction_active>
252bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
253 uint16_t inst_data) {
254 bool do_assignability_check = do_access_check;
255 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
256 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100257 ArtField* f =
258 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
259 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -0700260 if (UNLIKELY(f == nullptr)) {
261 CHECK(self->IsExceptionPending());
262 return false;
263 }
Mathieu Chartieref41db72016-10-25 15:08:01 -0700264 ObjPtr<mirror::Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -0700265 if (is_static) {
266 obj = f->GetDeclaringClass();
267 } else {
268 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
269 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000270 ThrowNullPointerExceptionForFieldAccess(f, false);
Ian Rogers54874942014-06-10 16:31:03 -0700271 return false;
272 }
273 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +0200274 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -0700275 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
276 // Report this field access to instrumentation if needed. Since we only have the offset of
277 // the field from the base of the object, we need to look for it first.
278 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
279 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
Mathieu Chartier0715c0b2016-10-03 22:49:46 -0700280 StackHandleScope<1> hs(self);
281 // Wrap in handle wrapper in case the listener does thread suspension.
282 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
Ian Rogers54874942014-06-10 16:31:03 -0700283 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700284 ObjPtr<mirror::Object> this_object = f->IsStatic() ? nullptr : obj;
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700285 instrumentation->FieldWriteEvent(self, this_object.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -0700286 shadow_frame.GetMethod(),
287 shadow_frame.GetDexPC(),
288 f,
289 field_value);
Ian Rogers54874942014-06-10 16:31:03 -0700290 }
291 switch (field_type) {
292 case Primitive::kPrimBoolean:
293 f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA));
294 break;
295 case Primitive::kPrimByte:
296 f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA));
297 break;
298 case Primitive::kPrimChar:
299 f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA));
300 break;
301 case Primitive::kPrimShort:
302 f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA));
303 break;
304 case Primitive::kPrimInt:
305 f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA));
306 break;
307 case Primitive::kPrimLong:
308 f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA));
309 break;
310 case Primitive::kPrimNot: {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700311 ObjPtr<mirror::Object> reg = shadow_frame.GetVRegReference(vregA);
Ian Rogers54874942014-06-10 16:31:03 -0700312 if (do_assignability_check && reg != nullptr) {
313 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
314 // object in the destructor.
Mathieu Chartieref41db72016-10-25 15:08:01 -0700315 ObjPtr<mirror::Class> field_class;
Ian Rogers54874942014-06-10 16:31:03 -0700316 {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700317 StackHandleScope<2> hs(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700318 HandleWrapperObjPtr<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
Mathieu Chartier3398c782016-09-30 10:27:43 -0700319 HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700320 field_class = f->GetType<true>();
Ian Rogers54874942014-06-10 16:31:03 -0700321 }
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700322 if (!reg->VerifierInstanceOf(field_class.Ptr())) {
Ian Rogers54874942014-06-10 16:31:03 -0700323 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700324 std::string temp1, temp2, temp3;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000325 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Ian Rogers54874942014-06-10 16:31:03 -0700326 "Put '%s' that is not instance of field '%s' in '%s'",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700327 reg->GetClass()->GetDescriptor(&temp1),
328 field_class->GetDescriptor(&temp2),
329 f->GetDeclaringClass()->GetDescriptor(&temp3));
Ian Rogers54874942014-06-10 16:31:03 -0700330 return false;
331 }
332 }
333 f->SetObj<transaction_active>(obj, reg);
334 break;
335 }
336 default:
337 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700338 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700339 }
340 return true;
341}
342
343// Explicitly instantiate all DoFieldPut functions.
344#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
345 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
346 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
347
348#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
349 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
350 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
351 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
352 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
353
354// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700355EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
356EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
357EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
358EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
359EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
360EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
361EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700362
363// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700364EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
365EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
366EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
367EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
368EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
369EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
370EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700371
372#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
373#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
374
375template<Primitive::Type field_type, bool transaction_active>
376bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700377 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700378 if (UNLIKELY(obj == nullptr)) {
379 // We lost the reference to the field index so we cannot get a more
380 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000381 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700382 return false;
383 }
384 MemberOffset field_offset(inst->VRegC_22c());
385 const uint32_t vregA = inst->VRegA_22c(inst_data);
386 // Report this field modification to instrumentation if needed. Since we only have the offset of
387 // the field from the base of the object, we need to look for it first.
388 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
389 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
390 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
391 field_offset.Uint32Value());
392 DCHECK(f != nullptr);
393 DCHECK(!f->IsStatic());
394 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700395 instrumentation->FieldWriteEvent(Thread::Current(),
396 obj.Ptr(),
397 shadow_frame.GetMethod(),
398 shadow_frame.GetDexPC(),
399 f,
400 field_value);
Ian Rogers54874942014-06-10 16:31:03 -0700401 }
402 // Note: iput-x-quick instructions are only for non-volatile fields.
403 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700404 case Primitive::kPrimBoolean:
405 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
406 break;
407 case Primitive::kPrimByte:
408 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
409 break;
410 case Primitive::kPrimChar:
411 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
412 break;
413 case Primitive::kPrimShort:
414 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
415 break;
Ian Rogers54874942014-06-10 16:31:03 -0700416 case Primitive::kPrimInt:
417 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
418 break;
419 case Primitive::kPrimLong:
420 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
421 break;
422 case Primitive::kPrimNot:
423 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
424 break;
425 default:
426 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700427 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700428 }
429 return true;
430}
431
432// Explicitly instantiate all DoIPutQuick functions.
433#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
434 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
435 const Instruction* inst, \
436 uint16_t inst_data)
437
438#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
439 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
440 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
441
Andreas Gampec8ccf682014-09-29 20:07:43 -0700442EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
443EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
444EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
445EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
446EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
447EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
448EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700449#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
450#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
451
Sebastien Hertz520633b2015-09-08 17:03:36 +0200452// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700453uint32_t FindNextInstructionFollowingException(
454 Thread* self, ShadowFrame& shadow_frame, uint32_t dex_pc,
455 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700456 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700457 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000458 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Sebastien Hertz520633b2015-09-08 17:03:36 +0200459 if (instrumentation != nullptr && instrumentation->HasExceptionCaughtListeners()
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000460 && self->IsExceptionThrownByCurrentMethod(exception.Get())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000461 instrumentation->ExceptionCaughtEvent(self, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200462 }
Ian Rogers54874942014-06-10 16:31:03 -0700463 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700464 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
465 hs.NewHandle(exception->GetClass()), dex_pc, &clear_exception);
Sebastien Hertz520633b2015-09-08 17:03:36 +0200466 if (found_dex_pc == DexFile::kDexNoIndex && instrumentation != nullptr) {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000467 // Exception is not caught by the current method. We will unwind to the
468 // caller. Notify any instrumentation listener.
Sebastien Hertz9f102032014-05-23 08:59:42 +0200469 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
Ian Rogers54874942014-06-10 16:31:03 -0700470 shadow_frame.GetMethod(), dex_pc);
471 } else {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000472 // Exception is caught in the current method. We will jump to the found_dex_pc.
Ian Rogers54874942014-06-10 16:31:03 -0700473 if (clear_exception) {
474 self->ClearException();
475 }
476 }
477 return found_dex_pc;
478}
479
Ian Rogerse94652f2014-12-02 11:13:19 -0800480void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
481 LOG(FATAL) << "Unexpected instruction: "
482 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
483 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700484}
485
Sebastien Hertz45b15972015-04-03 16:07:05 +0200486void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700487 va_list args;
488 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200489 AbortTransactionV(self, fmt, args);
490 va_end(args);
491}
492
493void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
494 CHECK(Runtime::Current()->IsActiveTransaction());
495 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100496 std::string abort_msg;
497 StringAppendV(&abort_msg, fmt, args);
498 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200499 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700500}
501
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100502// START DECLARATIONS :
503//
504// These additional declarations are required because clang complains
505// about ALWAYS_INLINE (-Werror, -Wgcc-compat) in definitions.
506//
507
Narayan Kamath9823e782016-08-03 12:46:58 +0100508template <bool is_range, bool do_assignability_check>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700509 REQUIRES_SHARED(Locks::mutator_lock_)
Igor Murashkin158f35c2015-06-10 15:55:30 -0700510static inline bool DoCallCommon(ArtMethod* called_method,
511 Thread* self,
512 ShadowFrame& shadow_frame,
513 JValue* result,
514 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +0100515 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -0700516 uint32_t vregC) ALWAYS_INLINE;
517
Narayan Kamath208f8572016-08-03 12:46:58 +0100518template <bool is_range> REQUIRES_SHARED(Locks::mutator_lock_)
519static inline bool DoCallPolymorphic(ArtMethod* called_method,
520 Handle<mirror::MethodType> callsite_type,
521 Handle<mirror::MethodType> target_type,
522 Thread* self,
523 ShadowFrame& shadow_frame,
524 JValue* result,
525 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
526 uint32_t vregC) ALWAYS_INLINE;
527
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100528REQUIRES_SHARED(Locks::mutator_lock_)
529static inline bool DoCallTransform(ArtMethod* called_method,
530 Handle<mirror::MethodType> callsite_type,
531 Thread* self,
532 ShadowFrame& shadow_frame,
533 Handle<mirror::MethodHandleImpl> receiver,
534 JValue* result) ALWAYS_INLINE;
535
536REQUIRES_SHARED(Locks::mutator_lock_)
537inline void PerformCall(Thread* self,
538 const DexFile::CodeItem* code_item,
539 ArtMethod* caller_method,
540 const size_t first_dest_reg,
541 ShadowFrame* callee_frame,
542 JValue* result) ALWAYS_INLINE;
543
544template <bool is_range>
545REQUIRES_SHARED(Locks::mutator_lock_)
546inline void CopyRegisters(ShadowFrame& caller_frame,
547 ShadowFrame* callee_frame,
548 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
549 const size_t first_src_reg,
550 const size_t first_dest_reg,
551 const size_t num_regs) ALWAYS_INLINE;
552
553// END DECLARATIONS.
554
Siva Chandra05d24152016-01-05 17:43:17 -0800555void ArtInterpreterToCompiledCodeBridge(Thread* self,
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100556 ArtMethod* caller,
Siva Chandra05d24152016-01-05 17:43:17 -0800557 const DexFile::CodeItem* code_item,
558 ShadowFrame* shadow_frame,
559 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700560 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700561 ArtMethod* method = shadow_frame->GetMethod();
562 // Ensure static methods are initialized.
563 if (method->IsStatic()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700564 ObjPtr<mirror::Class> declaringClass = method->GetDeclaringClass();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700565 if (UNLIKELY(!declaringClass->IsInitialized())) {
566 self->PushShadowFrame(shadow_frame);
567 StackHandleScope<1> hs(self);
568 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
569 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
570 true))) {
571 self->PopShadowFrame();
572 DCHECK(self->IsExceptionPending());
573 return;
574 }
575 self->PopShadowFrame();
576 CHECK(h_class->IsInitializing());
577 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
578 method = shadow_frame->GetMethod();
579 }
580 }
581 uint16_t arg_offset = (code_item == nullptr)
582 ? 0
583 : code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100584 jit::Jit* jit = Runtime::Current()->GetJit();
585 if (jit != nullptr && caller != nullptr) {
586 jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
587 }
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700588 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
589 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
Andreas Gampe542451c2016-07-26 09:02:02 -0700590 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700591}
592
Mingyao Yangffedec52016-05-19 10:48:40 -0700593void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
594 uint16_t this_obj_vreg,
595 JValue result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700596 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700597 ObjPtr<mirror::Object> existing = shadow_frame->GetVRegReference(this_obj_vreg);
Mingyao Yangffedec52016-05-19 10:48:40 -0700598 if (existing == nullptr) {
599 // If it's null, we come from compiled code that was deoptimized. Nothing to do,
600 // as the compiler verified there was no alias.
601 // Set the new string result of the StringFactory.
602 shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
603 return;
604 }
605 // Set the string init result into all aliases.
606 for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
607 if (shadow_frame->GetVRegReference(i) == existing) {
608 DCHECK_EQ(shadow_frame->GetVRegReference(i),
609 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
610 shadow_frame->SetVRegReference(i, result.GetL());
611 DCHECK_EQ(shadow_frame->GetVRegReference(i),
612 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
613 }
614 }
615}
616
Narayan Kamath9823e782016-08-03 12:46:58 +0100617template<bool is_range, bool do_access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -0700618inline bool DoInvokePolymorphic(Thread* self,
619 ShadowFrame& shadow_frame,
620 const Instruction* inst,
621 uint16_t inst_data,
622 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100623 // Invoke-polymorphic instructions always take a receiver. i.e, they are never static.
624 const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc();
625
626 // The method_idx here is the name of the signature polymorphic method that
627 // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact)
628 // and not the method that we'll dispatch to in the end.
629 //
630 // TODO(narayan) We'll have to check in the verifier that this is in fact a
631 // signature polymorphic method so that we disallow calls via invoke-polymorphic
632 // to non sig-poly methods. This would also have the side effect of verifying
633 // that vRegC really is a reference type.
Narayan Kamath208f8572016-08-03 12:46:58 +0100634 StackHandleScope<6> hs(self);
635 Handle<mirror::MethodHandleImpl> method_handle(hs.NewHandle(
Mathieu Chartieref41db72016-10-25 15:08:01 -0700636 ObjPtr<mirror::MethodHandleImpl>::DownCast(
637 MakeObjPtr(shadow_frame.GetVRegReference(vRegC)))));
Narayan Kamath208f8572016-08-03 12:46:58 +0100638 if (UNLIKELY(method_handle.Get() == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100639 const int method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc();
640 // Note that the invoke type is kVirtual here because a call to a signature
641 // polymorphic method is shaped like a virtual call at the bytecode level.
642 ThrowNullPointerExceptionForMethodAccess(method_idx, InvokeType::kVirtual);
643
644 result->SetJ(0);
645 return false;
646 }
647
648 // The vRegH value gives the index of the proto_id associated with this
649 // signature polymorphic callsite.
650 const uint32_t callsite_proto_id = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
651
652 // Call through to the classlinker and ask it to resolve the static type associated
653 // with the callsite. This information is stored in the dex cache so it's
654 // guaranteed to be fast after the first resolution.
Narayan Kamath9823e782016-08-03 12:46:58 +0100655 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Narayan Kamath208f8572016-08-03 12:46:58 +0100656 Handle<mirror::Class> caller_class(hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass()));
657 Handle<mirror::MethodType> callsite_type(hs.NewHandle(class_linker->ResolveMethodType(
Narayan Kamath9823e782016-08-03 12:46:58 +0100658 caller_class->GetDexFile(), callsite_proto_id,
659 hs.NewHandle<mirror::DexCache>(caller_class->GetDexCache()),
Narayan Kamath208f8572016-08-03 12:46:58 +0100660 hs.NewHandle<mirror::ClassLoader>(caller_class->GetClassLoader()))));
Narayan Kamath9823e782016-08-03 12:46:58 +0100661
662 // This implies we couldn't resolve one or more types in this method handle.
Narayan Kamath208f8572016-08-03 12:46:58 +0100663 if (UNLIKELY(callsite_type.Get() == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100664 CHECK(self->IsExceptionPending());
665 result->SetJ(0);
666 return false;
667 }
668
Narayan Kamath9823e782016-08-03 12:46:58 +0100669 // Get the method we're actually invoking along with the kind of
670 // invoke that is desired. We don't need to perform access checks at this
671 // point because they would have been performed on our behalf at the point
672 // of creation of the method handle.
673 ArtMethod* called_method = method_handle->GetTargetMethod();
674 const MethodHandleKind handle_kind = method_handle->GetHandleKind();
Narayan Kamath208f8572016-08-03 12:46:58 +0100675 Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType()));
Narayan Kamath9823e782016-08-03 12:46:58 +0100676 CHECK(called_method != nullptr);
Narayan Kamath208f8572016-08-03 12:46:58 +0100677 CHECK(handle_type.Get() != nullptr);
Narayan Kamath9823e782016-08-03 12:46:58 +0100678
Narayan Kamath9823e782016-08-03 12:46:58 +0100679 uint32_t arg[Instruction::kMaxVarArgRegs] = {};
680 uint32_t receiver_vregC = 0;
681 if (is_range) {
682 receiver_vregC = (inst->VRegC_4rcc() + 1);
683 } else {
684 inst->GetVarArgs(arg, inst_data);
685 arg[0] = arg[1];
686 arg[1] = arg[2];
687 arg[2] = arg[3];
688 arg[3] = arg[4];
689 arg[4] = 0;
690 receiver_vregC = arg[0];
691 }
692
693 if (IsInvoke(handle_kind)) {
694 if (handle_kind == kInvokeVirtual || handle_kind == kInvokeInterface) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700695 ObjPtr<mirror::Object> receiver = shadow_frame.GetVRegReference(receiver_vregC);
696 ObjPtr<mirror::Class> declaring_class = called_method->GetDeclaringClass();
Narayan Kamath9823e782016-08-03 12:46:58 +0100697 // Verify that _vRegC is an object reference and of the type expected by
698 // the receiver.
699 called_method = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(
700 called_method, kRuntimePointerSize);
701 if (!VerifyObjectIsClass(receiver, declaring_class)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100702 return false;
703 }
704 } else if (handle_kind == kInvokeDirect) {
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100705 if (called_method->IsConstructor()) {
706 // TODO(narayan) : We need to handle the case where the target method is a
707 // constructor here.
708 UNIMPLEMENTED(FATAL) << "Direct invokes for constructors are not implemented yet.";
709 return false;
710 }
711
712 // Nothing special to do in the case where we're not dealing with a
713 // constructor. It's a private method, and we've already access checked at
714 // the point of creating the handle.
715 } else if (handle_kind == kInvokeSuper) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700716 ObjPtr<mirror::Class> declaring_class = called_method->GetDeclaringClass();
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100717
718 // Note that we're not dynamically dispatching on the type of the receiver
719 // here. We use the static type of the "receiver" object that we've
720 // recorded in the method handle's type, which will be the same as the
721 // special caller that was specified at the point of lookup.
Mathieu Chartieref41db72016-10-25 15:08:01 -0700722 ObjPtr<mirror::Class> referrer_class = handle_type->GetPTypes()->Get(0);
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100723 if (!declaring_class->IsInterface()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700724 ObjPtr<mirror::Class> super_class = referrer_class->GetSuperClass();
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100725 uint16_t vtable_index = called_method->GetMethodIndex();
726 DCHECK(super_class != nullptr);
727 DCHECK(super_class->HasVTable());
728 // Note that super_class is a super of referrer_class and called_method
729 // will always be declared by super_class (or one of its super classes).
730 DCHECK_LT(vtable_index, super_class->GetVTableLength());
731 called_method = super_class->GetVTableEntry(vtable_index, kRuntimePointerSize);
732 } else {
733 called_method = referrer_class->FindVirtualMethodForInterfaceSuper(
734 called_method, kRuntimePointerSize);
735 }
736
737 CHECK(called_method != nullptr);
Narayan Kamath9823e782016-08-03 12:46:58 +0100738 }
739
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100740 if (handle_kind == kInvokeTransform) {
741 return DoCallTransform(called_method,
742 callsite_type,
743 self,
744 shadow_frame,
745 method_handle /* receiver */,
746 result);
Narayan Kamath208f8572016-08-03 12:46:58 +0100747 } else {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100748 return DoCallPolymorphic<is_range>(called_method,
749 callsite_type,
750 handle_type,
751 self,
752 shadow_frame,
753 result,
754 arg,
755 receiver_vregC);
Narayan Kamath9823e782016-08-03 12:46:58 +0100756 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100757 } else {
758 // TODO(narayan): Implement field getters and setters.
Narayan Kamath9823e782016-08-03 12:46:58 +0100759 UNIMPLEMENTED(FATAL) << "Field references in method handles are not implemented yet.";
760 return false;
761 }
762}
763
Narayan Kamath208f8572016-08-03 12:46:58 +0100764// Calculate the number of ins for a proxy or native method, where we
765// can't just look at the code item.
766static inline size_t GetInsForProxyOrNativeMethod(ArtMethod* method)
767 REQUIRES_SHARED(Locks::mutator_lock_) {
768 DCHECK(method->IsNative() || method->IsProxyMethod());
769
770 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
771 size_t num_ins = 0;
772 // Separate accounting for the receiver, which isn't a part of the
773 // shorty.
774 if (!method->IsStatic()) {
775 ++num_ins;
776 }
777
778 uint32_t shorty_len = 0;
779 const char* shorty = method->GetShorty(&shorty_len);
780 for (size_t i = 1; i < shorty_len; ++i) {
781 const char c = shorty[i];
782 ++num_ins;
783 if (c == 'J' || c == 'D') {
784 ++num_ins;
785 }
786 }
787
788 return num_ins;
789}
790
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100791
792inline void PerformCall(Thread* self,
793 const DexFile::CodeItem* code_item,
794 ArtMethod* caller_method,
795 const size_t first_dest_reg,
796 ShadowFrame* callee_frame,
797 JValue* result) {
798 if (LIKELY(Runtime::Current()->IsStarted())) {
799 ArtMethod* target = callee_frame->GetMethod();
800 if (ClassLinker::ShouldUseInterpreterEntrypoint(
801 target,
802 target->GetEntryPointFromQuickCompiledCode())) {
803 ArtInterpreterToInterpreterBridge(self, code_item, callee_frame, result);
804 } else {
805 ArtInterpreterToCompiledCodeBridge(
806 self, caller_method, code_item, callee_frame, result);
807 }
808 } else {
809 UnstartedRuntime::Invoke(self, code_item, callee_frame, result, first_dest_reg);
810 }
811}
812
813template <bool is_range>
814inline void CopyRegisters(ShadowFrame& caller_frame,
815 ShadowFrame* callee_frame,
816 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
817 const size_t first_src_reg,
818 const size_t first_dest_reg,
819 const size_t num_regs) {
820 if (is_range) {
821 const size_t dest_reg_bound = first_dest_reg + num_regs;
822 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound;
823 ++dest_reg, ++src_reg) {
824 AssignRegister(callee_frame, caller_frame, dest_reg, src_reg);
825 }
826 } else {
827 DCHECK_LE(num_regs, arraysize(arg));
828
829 for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) {
830 AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]);
831 }
832 }
833}
834
835// Returns true iff. the callsite type for a polymorphic invoke is transformer
836// like, i.e that it has a single input argument whose type is
837// dalvik.system.EmulatedStackFrame.
838static inline bool IsCallerTransformer(Handle<mirror::MethodType> callsite_type)
839 REQUIRES_SHARED(Locks::mutator_lock_) {
840 ObjPtr<mirror::ObjectArray<mirror::Class>> param_types(callsite_type->GetPTypes());
841 if (param_types->GetLength() == 1) {
842 ObjPtr<mirror::Class> param(param_types->GetWithoutChecks(0));
843 return param == WellKnownClasses::ToClass(WellKnownClasses::dalvik_system_EmulatedStackFrame);
844 }
845
846 return false;
847}
848
Narayan Kamath208f8572016-08-03 12:46:58 +0100849template <bool is_range>
850static inline bool DoCallPolymorphic(ArtMethod* called_method,
851 Handle<mirror::MethodType> callsite_type,
852 Handle<mirror::MethodType> target_type,
853 Thread* self,
854 ShadowFrame& shadow_frame,
855 JValue* result,
856 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100857 uint32_t first_src_reg) {
Narayan Kamath208f8572016-08-03 12:46:58 +0100858 // TODO(narayan): Wire in the String.init hacks.
859
860 // Compute method information.
861 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
862
863 // Number of registers for the callee's call frame. Note that for non-exact
864 // invokes, we always derive this information from the callee method. We
865 // cannot guarantee during verification that the number of registers encoded
866 // in the invoke is equal to the number of ins for the callee. This is because
867 // some transformations (such as boxing a long -> Long or wideining an
868 // int -> long will change that number.
869 uint16_t num_regs;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100870 size_t num_input_regs;
Narayan Kamath208f8572016-08-03 12:46:58 +0100871 size_t first_dest_reg;
872 if (LIKELY(code_item != nullptr)) {
873 num_regs = code_item->registers_size_;
874 first_dest_reg = num_regs - code_item->ins_size_;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100875 num_input_regs = code_item->ins_size_;
Narayan Kamath208f8572016-08-03 12:46:58 +0100876 // Parameter registers go at the end of the shadow frame.
877 DCHECK_NE(first_dest_reg, (size_t)-1);
878 } else {
879 // No local regs for proxy and native methods.
880 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100881 num_regs = num_input_regs = GetInsForProxyOrNativeMethod(called_method);
Narayan Kamath208f8572016-08-03 12:46:58 +0100882 first_dest_reg = 0;
883 }
884
885 // Allocate shadow frame on the stack.
886 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
887 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
888 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
889
890 // Thread might be suspended during PerformArgumentConversions due to the
891 // allocations performed during boxing.
892 {
893 ScopedStackedShadowFramePusher pusher(
894 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100895 if (callsite_type->IsExactMatch(target_type.Get())) {
896 // This is an exact invoke, we can take the fast path of just copying all
897 // registers without performing any argument conversions.
898 CopyRegisters<is_range>(shadow_frame,
899 new_shadow_frame,
900 arg,
901 first_src_reg,
902 first_dest_reg,
903 num_input_regs);
904 } else {
905 // This includes the case where we're entering this invoke-polymorphic
906 // from a transformer method. In that case, the callsite_type will contain
907 // a single argument of type dalvik.system.EmulatedStackFrame. In that
908 // case, we'll have to unmarshal the EmulatedStackFrame into the
909 // new_shadow_frame and perform argument conversions on it.
910 if (IsCallerTransformer(callsite_type)) {
911 // The emulated stack frame will be the first ahnd only argument
912 // when we're coming through from a transformer.
913 //
914 // TODO(narayan): This should be a mirror::EmulatedStackFrame after that
915 // type is introduced.
916 ObjPtr<mirror::Object> emulated_stack_frame(
917 shadow_frame.GetVRegReference(first_src_reg));
918 if (!ConvertAndCopyArgumentsFromEmulatedStackFrame<is_range>(self,
919 emulated_stack_frame,
920 target_type,
921 first_dest_reg,
922 new_shadow_frame)) {
923 DCHECK(self->IsExceptionPending());
924 result->SetL(0);
925 return false;
926 }
927 } else if (!ConvertAndCopyArgumentsFromCallerFrame<is_range>(self,
928 callsite_type,
929 target_type,
930 shadow_frame,
931 first_src_reg,
932 first_dest_reg,
933 arg,
934 new_shadow_frame)) {
935 DCHECK(self->IsExceptionPending());
936 result->SetL(0);
937 return false;
938 }
Narayan Kamath208f8572016-08-03 12:46:58 +0100939 }
940 }
941
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100942 PerformCall(self, code_item, shadow_frame.GetMethod(), first_dest_reg, new_shadow_frame, result);
Narayan Kamath208f8572016-08-03 12:46:58 +0100943
944 // TODO(narayan): Perform return value conversions.
945
946 return !self->IsExceptionPending();
947}
948
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100949static inline bool DoCallTransform(ArtMethod* called_method,
950 Handle<mirror::MethodType> callsite_type,
951 Thread* self,
952 ShadowFrame& shadow_frame,
953 Handle<mirror::MethodHandleImpl> receiver,
954 JValue* result) {
955 // This can be fixed, because the method we're calling here
956 // (MethodHandle.transformInternal) doesn't have any locals and the signature
957 // is known :
958 //
959 // private MethodHandle.transformInternal(EmulatedStackFrame sf);
960 //
961 // This means we need only two vregs :
962 // - One for the receiver object.
963 // - One for the only method argument (an EmulatedStackFrame).
964 static constexpr size_t kNumRegsForTransform = 2;
965
966 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
967 DCHECK(code_item != nullptr);
968 DCHECK_EQ(kNumRegsForTransform, code_item->registers_size_);
969 DCHECK_EQ(kNumRegsForTransform, code_item->ins_size_);
970
971 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
972 CREATE_SHADOW_FRAME(kNumRegsForTransform, &shadow_frame, called_method, /* dex pc */ 0);
973 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
974
975 // TODO(narayan): Perform argument conversions first (if this is an inexact invoke), and
976 // then construct an argument list object that's passed through to the
977 // method. Note that the ArgumentList reference is currently a nullptr.
978 //
979 // NOTE(narayan): If the caller is a transformer method (i.e, there is only
980 // one argument and its type is EmulatedStackFrame), we can directly pass that
981 // through without having to do any additional work.
982 UNUSED(callsite_type);
983
984 new_shadow_frame->SetVRegReference(0, receiver.Get());
985 // TODO(narayan): This is the EmulatedStackFrame, currently nullptr.
986 new_shadow_frame->SetVRegReference(1, nullptr);
987
988 PerformCall(self,
989 code_item,
990 shadow_frame.GetMethod(),
991 0 /* first dest reg */,
992 new_shadow_frame,
993 result);
994
995 return !self->IsExceptionPending();
996}
997
Igor Murashkin6918bf12015-09-27 19:19:06 -0700998template <bool is_range,
Narayan Kamath370423d2016-10-03 16:51:22 +0100999 bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001000static inline bool DoCallCommon(ArtMethod* called_method,
1001 Thread* self,
1002 ShadowFrame& shadow_frame,
1003 JValue* result,
1004 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +01001005 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -07001006 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001007 bool string_init = false;
1008 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001009 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
1010 && called_method->IsConstructor())) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001011 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
Jeff Hao848f70a2014-01-15 13:49:50 -08001012 string_init = true;
1013 }
1014
Alex Lightdaf58c82016-03-16 23:00:49 +00001015 // Compute method information.
1016 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
Igor Murashkin158f35c2015-06-10 15:55:30 -07001017
1018 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001019 uint16_t num_regs;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001020 if (LIKELY(code_item != nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001021 num_regs = code_item->registers_size_;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001022 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001023 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -08001024 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Igor Murashkin158f35c2015-06-10 15:55:30 -07001025 num_regs = number_of_inputs;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001026 }
1027
Igor Murashkin158f35c2015-06-10 15:55:30 -07001028 // Hack for String init:
1029 //
1030 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
1031 // invoke-x StringFactory(a, b, c, ...)
1032 // by effectively dropping the first virtual register from the invoke.
1033 //
1034 // (at this point the ArtMethod has already been replaced,
1035 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +00001036 //
1037 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
1038 // to handle the compiler optimization of replacing `this` with null without
1039 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001040 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -07001041 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001042 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 -07001043
Igor Murashkin158f35c2015-06-10 15:55:30 -07001044 // The new StringFactory call is static and has one fewer argument.
Igor Murashkina06b49b2015-06-25 15:18:12 -07001045 if (code_item == nullptr) {
1046 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1047 num_regs--;
1048 } // 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 -07001049 number_of_inputs--;
1050
1051 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -07001052 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001053 arg[i - 1] = arg[i];
1054 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07001055 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001056
1057 // Rewrite the non-var-arg case
1058 vregC++; // Skips the 0th vreg in the range ("this").
1059 }
1060
1061 // Parameter registers go at the end of the shadow frame.
1062 DCHECK_GE(num_regs, number_of_inputs);
1063 size_t first_dest_reg = num_regs - number_of_inputs;
1064 DCHECK_NE(first_dest_reg, (size_t)-1);
1065
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001066 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001067 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -07001068 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -07001069 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -07001070 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001071
Igor Murashkin158f35c2015-06-10 15:55:30 -07001072 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -07001073 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001074 // Slow path.
1075 // We might need to do class loading, which incurs a thread state change to kNative. So
1076 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07001077 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +02001078 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001079 self->EndAssertNoThreadSuspension(old_cause);
1080
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001081 // ArtMethod here is needed to check type information of the call site against the callee.
1082 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
1083 //
1084 // As a special case for proxy methods, which are not dex-backed,
1085 // we have to retrieve type information from the proxy's method
1086 // interface method instead (which is dex backed since proxies are never interfaces).
Andreas Gampe542451c2016-07-26 09:02:02 -07001087 ArtMethod* method =
1088 new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001089
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001090 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001091 // to get the exact type of each reference argument.
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001092 const DexFile::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001093 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001094 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001095
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001096 // Handle receiver apart since it's not part of the shorty.
1097 size_t dest_reg = first_dest_reg;
1098 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001099
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001100 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001101 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001102 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
1103 ++dest_reg;
1104 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -07001105 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001106 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001107
1108 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001109 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001110 // Skip the 0th 'shorty' type since it represents the return type.
1111 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001112 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
1113 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001114 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001115 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001116 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001117 if (do_assignability_check && o != nullptr) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001118 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001119 const uint32_t type_idx = params->GetTypeItem(shorty_pos).type_idx_;
1120 ObjPtr<mirror::Class> arg_type = method->GetDexCacheResolvedType(type_idx,
1121 pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001122 if (arg_type == nullptr) {
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001123 StackHandleScope<1> hs(self);
1124 // Preserve o since it is used below and GetClassFromTypeIndex may cause thread
1125 // suspension.
1126 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&o);
1127 arg_type = method->GetClassFromTypeIndex(type_idx, true /* resolve */, pointer_size);
1128 if (arg_type == nullptr) {
1129 CHECK(self->IsExceptionPending());
1130 return false;
1131 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001132 }
1133 if (!o->VerifierInstanceOf(arg_type)) {
1134 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -07001135 std::string temp1, temp2;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001136 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001137 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -08001138 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -07001139 o->GetClass()->GetDescriptor(&temp1),
1140 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001141 return false;
1142 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001143 }
Mathieu Chartieref41db72016-10-25 15:08:01 -07001144 new_shadow_frame->SetVRegReference(dest_reg, o.Ptr());
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001145 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -07001146 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001147 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001148 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001149 uint64_t wide_value =
1150 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
1151 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001152 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001153 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001154 ++dest_reg;
1155 ++arg_offset;
1156 break;
1157 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001158 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001159 default:
1160 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
1161 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001162 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001163 }
1164 } else {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001165 if (is_range) {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001166 DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001167 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001168
1169 CopyRegisters<is_range>(shadow_frame,
1170 new_shadow_frame,
1171 arg,
1172 vregC,
1173 first_dest_reg,
1174 number_of_inputs);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001175 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001176 }
1177
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001178 PerformCall(self, code_item, shadow_frame.GetMethod(), first_dest_reg, new_shadow_frame, result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001179
1180 if (string_init && !self->IsExceptionPending()) {
Mingyao Yangffedec52016-05-19 10:48:40 -07001181 SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001182 }
1183
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001184 return !self->IsExceptionPending();
1185}
1186
Igor Murashkin158f35c2015-06-10 15:55:30 -07001187template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001188bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
1189 const Instruction* inst, uint16_t inst_data, JValue* result) {
1190 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001191 const uint16_t number_of_inputs =
1192 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001193
1194 // TODO: find a cleaner way to separate non-range and range information without duplicating
1195 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -07001196 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001197 uint32_t vregC = 0;
1198 if (is_range) {
1199 vregC = inst->VRegC_3rc();
1200 } else {
1201 vregC = inst->VRegC_35c();
1202 inst->GetVarArgs(arg, inst_data);
1203 }
1204
1205 return DoCallCommon<is_range, do_assignability_check>(
1206 called_method, self, shadow_frame,
1207 result, number_of_inputs, arg, vregC);
1208}
1209
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001210template <bool is_range, bool do_access_check, bool transaction_active>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001211bool DoFilledNewArray(const Instruction* inst,
1212 const ShadowFrame& shadow_frame,
1213 Thread* self,
1214 JValue* result) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001215 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
1216 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
1217 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
1218 if (!is_range) {
1219 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
1220 CHECK_LE(length, 5);
1221 }
1222 if (UNLIKELY(length < 0)) {
1223 ThrowNegativeArraySizeException(length);
1224 return false;
1225 }
1226 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartieref41db72016-10-25 15:08:01 -07001227 ObjPtr<mirror::Class> array_class = ResolveVerifyAndClinit(type_idx,
1228 shadow_frame.GetMethod(),
1229 self,
1230 false,
1231 do_access_check);
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001232 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001233 DCHECK(self->IsExceptionPending());
1234 return false;
1235 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001236 CHECK(array_class->IsArrayClass());
Mathieu Chartieref41db72016-10-25 15:08:01 -07001237 ObjPtr<mirror::Class> component_class = array_class->GetComponentType();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001238 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
1239 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
1240 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001241 ThrowRuntimeException("Bad filled array request for type %s",
David Sehr709b0702016-10-13 09:12:37 -07001242 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001243 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001244 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -08001245 "Found type %s; filled-new-array not implemented for anything but 'int'",
David Sehr709b0702016-10-13 09:12:37 -07001246 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001247 }
1248 return false;
1249 }
Mathieu Chartieref41db72016-10-25 15:08:01 -07001250 ObjPtr<mirror::Object> new_array = mirror::Array::Alloc<true>(
1251 self,
1252 array_class,
1253 length,
1254 array_class->GetComponentSizeShift(),
1255 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001256 if (UNLIKELY(new_array == nullptr)) {
1257 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001258 return false;
1259 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001260 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
1261 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001262 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +01001263 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001264 } else {
Ian Rogers29a26482014-05-02 15:27:29 -07001265 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +01001266 }
Sebastien Hertzabff6432014-01-27 18:01:39 +01001267 for (int32_t i = 0; i < length; ++i) {
1268 size_t src_reg = is_range ? vregC + i : arg[i];
1269 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001270 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
1271 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +01001272 } else {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001273 new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<transaction_active>(
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001274 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001275 }
1276 }
1277
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001278 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001279 return true;
1280}
1281
Mathieu Chartieref41db72016-10-25 15:08:01 -07001282// TODO: Use ObjPtr here.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001283template<typename T>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001284static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array,
1285 int32_t count)
1286 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001287 Runtime* runtime = Runtime::Current();
1288 for (int32_t i = 0; i < count; ++i) {
1289 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
1290 }
1291}
1292
Mathieu Chartieref41db72016-10-25 15:08:01 -07001293void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001294 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001295 DCHECK(Runtime::Current()->IsActiveTransaction());
1296 DCHECK(array != nullptr);
1297 DCHECK_LE(count, array->GetLength());
1298 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
1299 switch (primitive_component_type) {
1300 case Primitive::kPrimBoolean:
1301 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
1302 break;
1303 case Primitive::kPrimByte:
1304 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
1305 break;
1306 case Primitive::kPrimChar:
1307 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
1308 break;
1309 case Primitive::kPrimShort:
1310 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
1311 break;
1312 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001313 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
1314 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001315 case Primitive::kPrimFloat:
1316 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
1317 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001318 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001319 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
1320 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001321 case Primitive::kPrimDouble:
1322 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
1323 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001324 default:
1325 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
1326 << " in fill-array-data";
1327 break;
1328 }
1329}
1330
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001331// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +02001332#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001333 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001334 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
1335 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +02001336 const Instruction* inst, uint16_t inst_data, \
1337 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001338EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
1339EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
1340EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
1341EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
1342#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001343
Narayan Kamath9823e782016-08-03 12:46:58 +01001344// Explicit DoInvokePolymorphic template function declarations.
1345#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range, _do_assignability_check) \
1346 template REQUIRES_SHARED(Locks::mutator_lock_) \
1347 bool DoInvokePolymorphic<_is_range, _do_assignability_check>( \
1348 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1349 uint16_t inst_data, JValue* result)
1350
1351EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, false);
1352EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, true);
1353EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, false);
1354EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, true);
1355#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1356
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001357// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001358#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001359 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001360 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
1361 const ShadowFrame& shadow_frame, \
1362 Thread* self, JValue* result)
1363#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
1364 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
1365 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
1366 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
1367 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
1368EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
1369EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
1370#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001371#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
1372
1373} // namespace interpreter
1374} // namespace art