blob: 88f30a89009dc39da64f8d0163dcc26cac249434 [file] [log] [blame]
Orion Hodsonba28f9f2016-10-26 10:56:25 +01001/*
2 * Copyright (C) 2016 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
Orion Hodsonba28f9f2016-10-26 10:56:25 +010017#include "method_handles-inl.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080018
19#include "android-base/stringprintf.h"
20
Orion Hodson811bd5f2016-12-07 11:35:37 +000021#include "common_dex_operations.h"
Orion Hodsonba28f9f2016-10-26 10:56:25 +010022#include "jvalue-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070023#include "jvalue.h"
Orion Hodson811bd5f2016-12-07 11:35:37 +000024#include "mirror/emulated_stack_frame.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080025#include "mirror/method_handle_impl-inl.h"
Orion Hodson811bd5f2016-12-07 11:35:37 +000026#include "mirror/method_type.h"
Orion Hodsonba28f9f2016-10-26 10:56:25 +010027#include "reflection-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070028#include "reflection.h"
Orion Hodson1a06f9f2016-11-09 08:32:42 +000029#include "well_known_classes.h"
Orion Hodsonba28f9f2016-10-26 10:56:25 +010030
31namespace art {
32
Andreas Gampe46ee31b2016-12-14 10:11:49 -080033using android::base::StringPrintf;
34
Orion Hodsonba28f9f2016-10-26 10:56:25 +010035namespace {
36
Orion Hodson1a06f9f2016-11-09 08:32:42 +000037#define PRIMITIVES_LIST(V) \
38 V(Primitive::kPrimBoolean, Boolean, Boolean, Z) \
39 V(Primitive::kPrimByte, Byte, Byte, B) \
40 V(Primitive::kPrimChar, Char, Character, C) \
41 V(Primitive::kPrimShort, Short, Short, S) \
42 V(Primitive::kPrimInt, Int, Integer, I) \
43 V(Primitive::kPrimLong, Long, Long, J) \
44 V(Primitive::kPrimFloat, Float, Float, F) \
45 V(Primitive::kPrimDouble, Double, Double, D)
Orion Hodsonba28f9f2016-10-26 10:56:25 +010046
47// Assigns |type| to the primitive type associated with |klass|. Returns
48// true iff. |klass| was a boxed type (Integer, Long etc.), false otherwise.
49bool GetUnboxedPrimitiveType(ObjPtr<mirror::Class> klass, Primitive::Type* type)
50 REQUIRES_SHARED(Locks::mutator_lock_) {
51 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Orion Hodsona1be7132017-03-21 10:04:12 +000052 std::string storage;
53 const char* descriptor = klass->GetDescriptor(&storage);
54 static const char kJavaLangPrefix[] = "Ljava/lang/";
55 static const size_t kJavaLangPrefixSize = sizeof(kJavaLangPrefix) - 1;
56 if (strncmp(descriptor, kJavaLangPrefix, kJavaLangPrefixSize) != 0) {
57 return false;
58 }
59
60 descriptor += kJavaLangPrefixSize;
61#define LOOKUP_PRIMITIVE(primitive, _, java_name, ___) \
62 if (strcmp(descriptor, #java_name ";") == 0) { \
63 *type = primitive; \
64 return true; \
Orion Hodsonba28f9f2016-10-26 10:56:25 +010065 }
Orion Hodsonba28f9f2016-10-26 10:56:25 +010066
Orion Hodson1a06f9f2016-11-09 08:32:42 +000067 PRIMITIVES_LIST(LOOKUP_PRIMITIVE);
68#undef LOOKUP_PRIMITIVE
Orion Hodsonba28f9f2016-10-26 10:56:25 +010069 return false;
70}
71
Orion Hodson1a06f9f2016-11-09 08:32:42 +000072ObjPtr<mirror::Class> GetBoxedPrimitiveClass(Primitive::Type type)
Orion Hodsonba28f9f2016-10-26 10:56:25 +010073 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson1a06f9f2016-11-09 08:32:42 +000074 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
75 jmethodID m = nullptr;
76 switch (type) {
77#define CASE_PRIMITIVE(primitive, _, java_name, __) \
78 case primitive: \
79 m = WellKnownClasses::java_lang_ ## java_name ## _valueOf; \
80 break;
81 PRIMITIVES_LIST(CASE_PRIMITIVE);
82#undef CASE_PRIMITIVE
83 case Primitive::Type::kPrimNot:
84 case Primitive::Type::kPrimVoid:
85 return nullptr;
Orion Hodsonba28f9f2016-10-26 10:56:25 +010086 }
Orion Hodson1a06f9f2016-11-09 08:32:42 +000087 return jni::DecodeArtMethod(m)->GetDeclaringClass();
88}
Orion Hodsonba28f9f2016-10-26 10:56:25 +010089
Orion Hodson1a06f9f2016-11-09 08:32:42 +000090bool GetUnboxedTypeAndValue(ObjPtr<mirror::Object> o, Primitive::Type* type, JValue* value)
91 REQUIRES_SHARED(Locks::mutator_lock_) {
92 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Orion Hodsonba28f9f2016-10-26 10:56:25 +010093 ObjPtr<mirror::Class> klass = o->GetClass();
Orion Hodsonba28f9f2016-10-26 10:56:25 +010094 ArtField* primitive_field = &klass->GetIFieldsPtr()->At(0);
Orion Hodson1a06f9f2016-11-09 08:32:42 +000095#define CASE_PRIMITIVE(primitive, abbrev, _, shorthand) \
96 if (klass == GetBoxedPrimitiveClass(primitive)) { \
97 *type = primitive; \
98 value->Set ## shorthand(primitive_field->Get ## abbrev(o)); \
99 return true; \
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100100 }
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000101 PRIMITIVES_LIST(CASE_PRIMITIVE)
102#undef CASE_PRIMITIVE
103 return false;
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100104}
105
106inline bool IsReferenceType(Primitive::Type type) {
107 return type == Primitive::kPrimNot;
108}
109
110inline bool IsPrimitiveType(Primitive::Type type) {
111 return !IsReferenceType(type);
112}
113
114} // namespace
115
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000116bool IsParameterTypeConvertible(ObjPtr<mirror::Class> from, ObjPtr<mirror::Class> to)
117 REQUIRES_SHARED(Locks::mutator_lock_) {
118 // This function returns true if there's any conceivable conversion
119 // between |from| and |to|. It's expected this method will be used
120 // to determine if a WrongMethodTypeException should be raised. The
121 // decision logic follows the documentation for MethodType.asType().
122 if (from == to) {
123 return true;
124 }
125
126 Primitive::Type from_primitive = from->GetPrimitiveType();
127 Primitive::Type to_primitive = to->GetPrimitiveType();
128 DCHECK(from_primitive != Primitive::Type::kPrimVoid);
129 DCHECK(to_primitive != Primitive::Type::kPrimVoid);
130
131 // If |to| and |from| are references.
132 if (IsReferenceType(from_primitive) && IsReferenceType(to_primitive)) {
133 // Assignability is determined during parameter conversion when
134 // invoking the associated method handle.
135 return true;
136 }
137
138 // If |to| and |from| are primitives and a widening conversion exists.
139 if (Primitive::IsWidenable(from_primitive, to_primitive)) {
140 return true;
141 }
142
143 // If |to| is a reference and |from| is a primitive, then boxing conversion.
144 if (IsReferenceType(to_primitive) && IsPrimitiveType(from_primitive)) {
145 return to->IsAssignableFrom(GetBoxedPrimitiveClass(from_primitive));
146 }
147
148 // If |from| is a reference and |to| is a primitive, then unboxing conversion.
149 if (IsPrimitiveType(to_primitive) && IsReferenceType(from_primitive)) {
150 if (from->DescriptorEquals("Ljava/lang/Object;")) {
151 // Object might be converted into a primitive during unboxing.
152 return true;
Orion Hodsona1be7132017-03-21 10:04:12 +0000153 }
154
155 if (Primitive::IsNumericType(to_primitive) && from->DescriptorEquals("Ljava/lang/Number;")) {
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000156 // Number might be unboxed into any of the number primitive types.
157 return true;
158 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000159
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000160 Primitive::Type unboxed_type;
161 if (GetUnboxedPrimitiveType(from, &unboxed_type)) {
Orion Hodsonf1412b42016-11-11 12:03:29 +0000162 if (unboxed_type == to_primitive) {
163 // Straightforward unboxing conversion such as Boolean => boolean.
164 return true;
Orion Hodsonf1412b42016-11-11 12:03:29 +0000165 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000166
167 // Check if widening operations for numeric primitives would work,
168 // such as Byte => byte => long.
169 return Primitive::IsWidenable(unboxed_type, to_primitive);
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000170 }
171 }
172
173 return false;
174}
175
176bool IsReturnTypeConvertible(ObjPtr<mirror::Class> from, ObjPtr<mirror::Class> to)
177 REQUIRES_SHARED(Locks::mutator_lock_) {
178 if (to->GetPrimitiveType() == Primitive::Type::kPrimVoid) {
179 // Result will be ignored.
180 return true;
181 } else if (from->GetPrimitiveType() == Primitive::Type::kPrimVoid) {
182 // Returned value will be 0 / null.
183 return true;
184 } else {
185 // Otherwise apply usual parameter conversion rules.
186 return IsParameterTypeConvertible(from, to);
187 }
188}
189
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100190bool ConvertJValueCommon(
191 Handle<mirror::MethodType> callsite_type,
192 Handle<mirror::MethodType> callee_type,
193 ObjPtr<mirror::Class> from,
194 ObjPtr<mirror::Class> to,
195 JValue* value) {
196 // The reader maybe concerned about the safety of the heap object
197 // that may be in |value|. There is only one case where allocation
198 // is obviously needed and that's for boxing. However, in the case
199 // of boxing |value| contains a non-reference type.
200
201 const Primitive::Type from_type = from->GetPrimitiveType();
202 const Primitive::Type to_type = to->GetPrimitiveType();
203
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000204 // Put incoming value into |src_value| and set return value to 0.
205 // Errors and conversions from void require the return value to be 0.
206 const JValue src_value(*value);
207 value->SetJ(0);
208
209 // Conversion from void set result to zero.
210 if (from_type == Primitive::kPrimVoid) {
211 return true;
212 }
213
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100214 // This method must be called only when the types don't match.
215 DCHECK(from != to);
216
217 if (IsPrimitiveType(from_type) && IsPrimitiveType(to_type)) {
218 // The source and target types are both primitives.
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000219 if (UNLIKELY(!ConvertPrimitiveValueNoThrow(from_type, to_type, src_value, value))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100220 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100221 return false;
222 }
223 return true;
224 } else if (IsReferenceType(from_type) && IsReferenceType(to_type)) {
225 // They're both reference types. If "from" is null, we can pass it
226 // through unchanged. If not, we must generate a cast exception if
227 // |to| is not assignable from the dynamic type of |ref|.
228 //
229 // Playing it safe with StackHandleScope here, not expecting any allocation
230 // in mirror::Class::IsAssignable().
231 StackHandleScope<2> hs(Thread::Current());
232 Handle<mirror::Class> h_to(hs.NewHandle(to));
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000233 Handle<mirror::Object> h_obj(hs.NewHandle(src_value.GetL()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800234 if (h_obj != nullptr && !to->IsAssignableFrom(h_obj->GetClass())) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100235 ThrowClassCastException(h_to.Get(), h_obj->GetClass());
236 return false;
237 }
238 value->SetL(h_obj.Get());
239 return true;
240 } else if (IsReferenceType(to_type)) {
241 DCHECK(IsPrimitiveType(from_type));
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100242 // The source type is a primitive and the target type is a reference, so we must box.
243 // The target type maybe a super class of the boxed source type, for example,
244 // if the source type is int, it's boxed type is java.lang.Integer, and the target
245 // type could be java.lang.Number.
246 Primitive::Type type;
247 if (!GetUnboxedPrimitiveType(to, &type)) {
248 ObjPtr<mirror::Class> boxed_from_class = GetBoxedPrimitiveClass(from_type);
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000249 if (boxed_from_class->IsSubClass(to)) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100250 type = from_type;
251 } else {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100252 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
253 return false;
254 }
255 }
256
257 if (UNLIKELY(from_type != type)) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100258 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
259 return false;
260 }
261
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000262 if (!ConvertPrimitiveValueNoThrow(from_type, type, src_value, value)) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100263 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
264 return false;
265 }
266
267 // Then perform the actual boxing, and then set the reference.
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000268 ObjPtr<mirror::Object> boxed = BoxPrimitive(type, src_value);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100269 value->SetL(boxed.Ptr());
270 return true;
271 } else {
272 // The source type is a reference and the target type is a primitive, so we must unbox.
273 DCHECK(IsReferenceType(from_type));
274 DCHECK(IsPrimitiveType(to_type));
275
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000276 ObjPtr<mirror::Object> from_obj(src_value.GetL());
277 if (UNLIKELY(from_obj == nullptr)) {
278 ThrowNullPointerException(
279 StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
280 from->PrettyDescriptor().c_str()).c_str());
281 return false;
282 }
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100283
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000284 Primitive::Type unboxed_type;
285 JValue unboxed_value;
286 if (UNLIKELY(!GetUnboxedTypeAndValue(from_obj, &unboxed_type, &unboxed_value))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100287 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
288 return false;
289 }
290
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000291 if (UNLIKELY(!ConvertPrimitiveValueNoThrow(unboxed_type, to_type, unboxed_value, value))) {
292 ThrowClassCastException(from, to);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100293 return false;
294 }
295
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000296 return true;
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100297 }
298}
299
Orion Hodson811bd5f2016-12-07 11:35:37 +0000300namespace {
301
Orion Hodson811bd5f2016-12-07 11:35:37 +0000302inline void CopyArgumentsFromCallerFrame(const ShadowFrame& caller_frame,
303 ShadowFrame* callee_frame,
Orion Hodson960d4f72017-11-10 15:32:38 +0000304 const InstructionOperands* const operands,
305 const size_t first_dst_reg)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000306 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000307 for (size_t i = 0; i < operands->GetNumberOfOperands(); ++i) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000308 size_t dst_reg = first_dst_reg + i;
Orion Hodson960d4f72017-11-10 15:32:38 +0000309 size_t src_reg = operands->GetOperand(i);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000310 // Uint required, so that sign extension does not make this wrong on 64-bit systems
311 uint32_t src_value = caller_frame.GetVReg(src_reg);
312 ObjPtr<mirror::Object> o = caller_frame.GetVRegReference<kVerifyNone>(src_reg);
313 // If both register locations contains the same value, the register probably holds a reference.
314 // Note: As an optimization, non-moving collectors leave a stale reference value
315 // in the references array even after the original vreg was overwritten to a non-reference.
316 if (src_value == reinterpret_cast<uintptr_t>(o.Ptr())) {
317 callee_frame->SetVRegReference(dst_reg, o.Ptr());
318 } else {
319 callee_frame->SetVReg(dst_reg, src_value);
320 }
321 }
322}
323
Orion Hodson811bd5f2016-12-07 11:35:37 +0000324inline bool ConvertAndCopyArgumentsFromCallerFrame(
325 Thread* self,
326 Handle<mirror::MethodType> callsite_type,
327 Handle<mirror::MethodType> callee_type,
328 const ShadowFrame& caller_frame,
Orion Hodson960d4f72017-11-10 15:32:38 +0000329 uint32_t first_dest_reg,
330 const InstructionOperands* const operands,
Orion Hodson811bd5f2016-12-07 11:35:37 +0000331 ShadowFrame* callee_frame)
332 REQUIRES_SHARED(Locks::mutator_lock_) {
333 ObjPtr<mirror::ObjectArray<mirror::Class>> from_types(callsite_type->GetPTypes());
334 ObjPtr<mirror::ObjectArray<mirror::Class>> to_types(callee_type->GetPTypes());
335
336 const int32_t num_method_params = from_types->GetLength();
337 if (to_types->GetLength() != num_method_params) {
338 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
339 return false;
340 }
341
Orion Hodson960d4f72017-11-10 15:32:38 +0000342 ShadowFrameGetter getter(operands, caller_frame);
343 ShadowFrameSetter setter(callee_frame, first_dest_reg);
344 return PerformConversions<ShadowFrameGetter, ShadowFrameSetter>(self,
345 callsite_type,
346 callee_type,
347 &getter,
348 &setter,
349 num_method_params);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000350}
351
Orion Hodson811bd5f2016-12-07 11:35:37 +0000352inline bool IsInvoke(const mirror::MethodHandle::Kind handle_kind) {
353 return handle_kind <= mirror::MethodHandle::Kind::kLastInvokeKind;
354}
355
356inline bool IsInvokeTransform(const mirror::MethodHandle::Kind handle_kind) {
357 return (handle_kind == mirror::MethodHandle::Kind::kInvokeTransform
358 || handle_kind == mirror::MethodHandle::Kind::kInvokeCallSiteTransform);
359}
360
361inline bool IsFieldAccess(mirror::MethodHandle::Kind handle_kind) {
362 return (handle_kind >= mirror::MethodHandle::Kind::kFirstAccessorKind
363 && handle_kind <= mirror::MethodHandle::Kind::kLastAccessorKind);
364}
365
366// Calculate the number of ins for a proxy or native method, where we
367// can't just look at the code item.
368static inline size_t GetInsForProxyOrNativeMethod(ArtMethod* method)
369 REQUIRES_SHARED(Locks::mutator_lock_) {
370 DCHECK(method->IsNative() || method->IsProxyMethod());
Orion Hodson811bd5f2016-12-07 11:35:37 +0000371 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Orion Hodsona1be7132017-03-21 10:04:12 +0000372 uint32_t shorty_length = 0;
373 const char* shorty = method->GetShorty(&shorty_length);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000374
Orion Hodsona1be7132017-03-21 10:04:12 +0000375 // Static methods do not include the receiver. The receiver isn't included
376 // in the shorty_length though the return value is.
377 size_t num_ins = method->IsStatic() ? shorty_length - 1 : shorty_length;
378 for (const char* c = shorty + 1; *c != '\0'; ++c) {
379 if (*c == 'J' || *c == 'D') {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000380 ++num_ins;
381 }
382 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000383 return num_ins;
384}
385
386// Returns true iff. the callsite type for a polymorphic invoke is transformer
387// like, i.e that it has a single input argument whose type is
388// dalvik.system.EmulatedStackFrame.
389static inline bool IsCallerTransformer(Handle<mirror::MethodType> callsite_type)
390 REQUIRES_SHARED(Locks::mutator_lock_) {
391 ObjPtr<mirror::ObjectArray<mirror::Class>> param_types(callsite_type->GetPTypes());
392 if (param_types->GetLength() == 1) {
393 ObjPtr<mirror::Class> param(param_types->GetWithoutChecks(0));
Orion Hodsona1be7132017-03-21 10:04:12 +0000394 // NB Comparing descriptor here as it appears faster in cycle simulation than using:
395 // param == WellKnownClasses::ToClass(WellKnownClasses::dalvik_system_EmulatedStackFrame)
396 // Costs are 98 vs 173 cycles per invocation.
397 return param->DescriptorEquals("Ldalvik/system/EmulatedStackFrame;");
Orion Hodson811bd5f2016-12-07 11:35:37 +0000398 }
399
400 return false;
401}
402
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100403static inline bool MethodHandleInvokeMethod(ArtMethod* called_method,
404 Handle<mirror::MethodType> callsite_type,
405 Handle<mirror::MethodType> target_type,
406 Thread* self,
407 ShadowFrame& shadow_frame,
Orion Hodson960d4f72017-11-10 15:32:38 +0000408 const InstructionOperands* const operands,
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100409 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000410 // Compute method information.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800411 CodeItemDataAccessor accessor(called_method);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000412
413 // Number of registers for the callee's call frame. Note that for non-exact
414 // invokes, we always derive this information from the callee method. We
415 // cannot guarantee during verification that the number of registers encoded
416 // in the invoke is equal to the number of ins for the callee. This is because
417 // some transformations (such as boxing a long -> Long or wideining an
418 // int -> long will change that number.
419 uint16_t num_regs;
420 size_t num_input_regs;
421 size_t first_dest_reg;
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800422 if (LIKELY(accessor.HasCodeItem())) {
423 num_regs = accessor.RegistersSize();
424 first_dest_reg = num_regs - accessor.InsSize();
425 num_input_regs = accessor.InsSize();
Orion Hodson811bd5f2016-12-07 11:35:37 +0000426 // Parameter registers go at the end of the shadow frame.
427 DCHECK_NE(first_dest_reg, (size_t)-1);
428 } else {
429 // No local regs for proxy and native methods.
430 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
431 num_regs = num_input_regs = GetInsForProxyOrNativeMethod(called_method);
432 first_dest_reg = 0;
433 }
434
435 // Allocate shadow frame on the stack.
436 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
437 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
438 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
439
440 // Whether this polymorphic invoke was issued by a transformer method.
441 bool is_caller_transformer = false;
442 // Thread might be suspended during PerformArgumentConversions due to the
443 // allocations performed during boxing.
444 {
445 ScopedStackedShadowFramePusher pusher(
446 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
447 if (callsite_type->IsExactMatch(target_type.Get())) {
448 // This is an exact invoke, we can take the fast path of just copying all
449 // registers without performing any argument conversions.
Orion Hodson960d4f72017-11-10 15:32:38 +0000450 CopyArgumentsFromCallerFrame(shadow_frame,
451 new_shadow_frame,
452 operands,
453 first_dest_reg);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000454 } else {
455 // This includes the case where we're entering this invoke-polymorphic
456 // from a transformer method. In that case, the callsite_type will contain
457 // a single argument of type dalvik.system.EmulatedStackFrame. In that
458 // case, we'll have to unmarshal the EmulatedStackFrame into the
459 // new_shadow_frame and perform argument conversions on it.
460 if (IsCallerTransformer(callsite_type)) {
461 is_caller_transformer = true;
462 // The emulated stack frame is the first and only argument when we're coming
463 // through from a transformer.
Orion Hodson960d4f72017-11-10 15:32:38 +0000464 size_t first_arg_register = operands->GetOperand(0);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000465 ObjPtr<mirror::EmulatedStackFrame> emulated_stack_frame(
466 reinterpret_cast<mirror::EmulatedStackFrame*>(
467 shadow_frame.GetVRegReference(first_arg_register)));
468 if (!emulated_stack_frame->WriteToShadowFrame(self,
469 target_type,
470 first_dest_reg,
471 new_shadow_frame)) {
472 DCHECK(self->IsExceptionPending());
473 result->SetL(0);
474 return false;
475 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000476 } else {
477 if (!callsite_type->IsConvertible(target_type.Get())) {
478 ThrowWrongMethodTypeException(target_type.Get(), callsite_type.Get());
479 return false;
480 }
Orion Hodson960d4f72017-11-10 15:32:38 +0000481 if (!ConvertAndCopyArgumentsFromCallerFrame(self,
482 callsite_type,
483 target_type,
484 shadow_frame,
485 first_dest_reg,
486 operands,
487 new_shadow_frame)) {
Orion Hodsona1be7132017-03-21 10:04:12 +0000488 DCHECK(self->IsExceptionPending());
489 result->SetL(0);
490 return false;
491 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000492 }
493 }
494 }
495
Jeff Hao5ea84132017-05-05 16:59:29 -0700496 bool use_interpreter_entrypoint = ClassLinker::ShouldUseInterpreterEntrypoint(
497 called_method, called_method->GetEntryPointFromQuickCompiledCode());
498 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800499 accessor,
Jeff Hao5ea84132017-05-05 16:59:29 -0700500 shadow_frame.GetMethod(),
501 first_dest_reg,
502 new_shadow_frame,
503 result,
504 use_interpreter_entrypoint);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000505 if (self->IsExceptionPending()) {
506 return false;
507 }
508
509 // If the caller of this signature polymorphic method was a transformer,
510 // we need to copy the result back out to the emulated stack frame.
511 if (is_caller_transformer) {
512 StackHandleScope<2> hs(self);
Orion Hodson960d4f72017-11-10 15:32:38 +0000513 size_t first_callee_register = operands->GetOperand(0);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000514 Handle<mirror::EmulatedStackFrame> emulated_stack_frame(
515 hs.NewHandle(reinterpret_cast<mirror::EmulatedStackFrame*>(
516 shadow_frame.GetVRegReference(first_callee_register))));
517 Handle<mirror::MethodType> emulated_stack_type(hs.NewHandle(emulated_stack_frame->GetType()));
518 JValue local_result;
519 local_result.SetJ(result->GetJ());
520
521 if (ConvertReturnValue(emulated_stack_type, target_type, &local_result)) {
522 emulated_stack_frame->SetReturnValue(self, local_result);
523 return true;
Orion Hodson811bd5f2016-12-07 11:35:37 +0000524 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000525
526 DCHECK(self->IsExceptionPending());
527 return false;
Orion Hodson811bd5f2016-12-07 11:35:37 +0000528 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000529
530 return ConvertReturnValue(callsite_type, target_type, result);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000531}
532
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100533static inline bool MethodHandleInvokeTransform(ArtMethod* called_method,
534 Handle<mirror::MethodType> callsite_type,
535 Handle<mirror::MethodType> callee_type,
536 Thread* self,
537 ShadowFrame& shadow_frame,
538 Handle<mirror::MethodHandle> receiver,
Orion Hodson960d4f72017-11-10 15:32:38 +0000539 const InstructionOperands* const operands,
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100540 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000541 REQUIRES_SHARED(Locks::mutator_lock_) {
542 // This can be fixed to two, because the method we're calling here
543 // (MethodHandle.transformInternal) doesn't have any locals and the signature
544 // is known :
545 //
546 // private MethodHandle.transformInternal(EmulatedStackFrame sf);
547 //
548 // This means we need only two vregs :
549 // - One for the receiver object.
550 // - One for the only method argument (an EmulatedStackFrame).
551 static constexpr size_t kNumRegsForTransform = 2;
552
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800553 CodeItemDataAccessor accessor(called_method);
554 DCHECK_EQ(kNumRegsForTransform, accessor.RegistersSize());
555 DCHECK_EQ(kNumRegsForTransform, accessor.InsSize());
Orion Hodson811bd5f2016-12-07 11:35:37 +0000556
557 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
558 CREATE_SHADOW_FRAME(kNumRegsForTransform, &shadow_frame, called_method, /* dex pc */ 0);
559 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
560
561 StackHandleScope<1> hs(self);
562 MutableHandle<mirror::EmulatedStackFrame> sf(hs.NewHandle<mirror::EmulatedStackFrame>(nullptr));
563 if (IsCallerTransformer(callsite_type)) {
564 // If we're entering this transformer from another transformer, we can pass
565 // through the handle directly to the callee, instead of having to
566 // instantiate a new stack frame based on the shadow frame.
Orion Hodson960d4f72017-11-10 15:32:38 +0000567 size_t first_callee_register = operands->GetOperand(0);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000568 sf.Assign(reinterpret_cast<mirror::EmulatedStackFrame*>(
569 shadow_frame.GetVRegReference(first_callee_register)));
570 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000571 sf.Assign(mirror::EmulatedStackFrame::CreateFromShadowFrameAndArgs(self,
572 callsite_type,
573 callee_type,
574 shadow_frame,
575 operands));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000576
577 // Something went wrong while creating the emulated stack frame, we should
578 // throw the pending exception.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800579 if (sf == nullptr) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000580 DCHECK(self->IsExceptionPending());
581 return false;
582 }
583 }
584
585 new_shadow_frame->SetVRegReference(0, receiver.Get());
586 new_shadow_frame->SetVRegReference(1, sf.Get());
587
Jeff Hao5ea84132017-05-05 16:59:29 -0700588 bool use_interpreter_entrypoint = ClassLinker::ShouldUseInterpreterEntrypoint(
589 called_method, called_method->GetEntryPointFromQuickCompiledCode());
Orion Hodson811bd5f2016-12-07 11:35:37 +0000590 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800591 accessor,
Orion Hodson811bd5f2016-12-07 11:35:37 +0000592 shadow_frame.GetMethod(),
593 0 /* first destination register */,
594 new_shadow_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -0700595 result,
596 use_interpreter_entrypoint);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000597 if (self->IsExceptionPending()) {
598 return false;
599 }
600
601 // If the called transformer method we called has returned a value, then we
602 // need to copy it back to |result|.
603 sf->GetReturnValue(self, result);
604 return ConvertReturnValue(callsite_type, callee_type, result);
605}
606
607inline static ObjPtr<mirror::Class> GetAndInitializeDeclaringClass(Thread* self, ArtField* field)
608 REQUIRES_SHARED(Locks::mutator_lock_) {
609 // Method handle invocations on static fields should ensure class is
610 // initialized. This usually happens when an instance is constructed
611 // or class members referenced, but this is not guaranteed when
612 // looking up method handles.
613 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
614 if (UNLIKELY(!klass->IsInitialized())) {
615 StackHandleScope<1> hs(self);
616 HandleWrapperObjPtr<mirror::Class> h(hs.NewHandleWrapper(&klass));
617 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h, true, true)) {
618 DCHECK(self->IsExceptionPending());
619 return nullptr;
620 }
621 }
622 return klass;
623}
624
Orion Hodsona1be7132017-03-21 10:04:12 +0000625ArtMethod* RefineTargetMethod(Thread* self,
626 ShadowFrame& shadow_frame,
627 const mirror::MethodHandle::Kind& handle_kind,
628 Handle<mirror::MethodType> handle_type,
629 Handle<mirror::MethodType> callsite_type,
630 const uint32_t receiver_reg,
631 ArtMethod* target_method)
632 REQUIRES_SHARED(Locks::mutator_lock_) {
633 if (handle_kind == mirror::MethodHandle::Kind::kInvokeVirtual ||
634 handle_kind == mirror::MethodHandle::Kind::kInvokeInterface) {
635 // For virtual and interface methods ensure target_method points to
636 // the actual method to invoke.
637 ObjPtr<mirror::Object> receiver(shadow_frame.GetVRegReference(receiver_reg));
638 if (IsCallerTransformer(callsite_type)) {
639 // The current receiver is an emulated stack frame, the method's
640 // receiver needs to be fetched from there as the emulated frame
641 // will be unpacked into a new frame.
642 receiver = ObjPtr<mirror::EmulatedStackFrame>::DownCast(receiver)->GetReceiver();
643 }
644
645 ObjPtr<mirror::Class> declaring_class(target_method->GetDeclaringClass());
646 if (receiver == nullptr || receiver->GetClass() != declaring_class) {
647 // Verify that _vRegC is an object reference and of the type expected by
648 // the receiver.
649 if (!VerifyObjectIsClass(receiver, declaring_class)) {
650 DCHECK(self->IsExceptionPending());
651 return nullptr;
652 }
653 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(
654 target_method, kRuntimePointerSize);
655 }
656 } else if (handle_kind == mirror::MethodHandle::Kind::kInvokeDirect) {
657 // String constructors are a special case, they are replaced with
658 // StringFactory methods.
659 if (target_method->IsConstructor() && target_method->GetDeclaringClass()->IsStringClass()) {
660 DCHECK(handle_type->GetRType()->IsStringClass());
661 return WellKnownClasses::StringInitToStringFactory(target_method);
662 }
663 } else if (handle_kind == mirror::MethodHandle::Kind::kInvokeSuper) {
664 ObjPtr<mirror::Class> declaring_class = target_method->GetDeclaringClass();
665
666 // Note that we're not dynamically dispatching on the type of the receiver
667 // here. We use the static type of the "receiver" object that we've
668 // recorded in the method handle's type, which will be the same as the
669 // special caller that was specified at the point of lookup.
670 ObjPtr<mirror::Class> referrer_class = handle_type->GetPTypes()->Get(0);
671 if (!declaring_class->IsInterface()) {
672 ObjPtr<mirror::Class> super_class = referrer_class->GetSuperClass();
673 uint16_t vtable_index = target_method->GetMethodIndex();
674 DCHECK(super_class != nullptr);
675 DCHECK(super_class->HasVTable());
676 // Note that super_class is a super of referrer_class and target_method
677 // will always be declared by super_class (or one of its super classes).
678 DCHECK_LT(vtable_index, super_class->GetVTableLength());
679 return super_class->GetVTableEntry(vtable_index, kRuntimePointerSize);
680 } else {
681 return referrer_class->FindVirtualMethodForInterfaceSuper(target_method, kRuntimePointerSize);
682 }
683 }
684 return target_method;
685}
686
Orion Hodsona1be7132017-03-21 10:04:12 +0000687bool DoInvokePolymorphicMethod(Thread* self,
688 ShadowFrame& shadow_frame,
689 Handle<mirror::MethodHandle> method_handle,
690 Handle<mirror::MethodType> callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +0000691 const InstructionOperands* const operands,
Orion Hodsona1be7132017-03-21 10:04:12 +0000692 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000693 REQUIRES_SHARED(Locks::mutator_lock_) {
694 StackHandleScope<1> hs(self);
695 Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType()));
696 const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind();
Orion Hodsona1be7132017-03-21 10:04:12 +0000697 DCHECK(IsInvoke(handle_kind));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000698
Orion Hodsona1be7132017-03-21 10:04:12 +0000699 // Get the method we're actually invoking along with the kind of
700 // invoke that is desired. We don't need to perform access checks at this
701 // point because they would have been performed on our behalf at the point
702 // of creation of the method handle.
703 ArtMethod* target_method = method_handle->GetTargetMethod();
Orion Hodson960d4f72017-11-10 15:32:38 +0000704 uint32_t receiver_reg = (operands->GetNumberOfOperands() > 0) ? operands->GetOperand(0) : 0u;
Orion Hodsona1be7132017-03-21 10:04:12 +0000705 ArtMethod* called_method = RefineTargetMethod(self,
706 shadow_frame,
707 handle_kind,
708 handle_type,
709 callsite_type,
710 receiver_reg,
711 target_method);
712 if (called_method == nullptr) {
713 DCHECK(self->IsExceptionPending());
714 return false;
715 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000716
Orion Hodsona1be7132017-03-21 10:04:12 +0000717 if (IsInvokeTransform(handle_kind)) {
718 // There are two cases here - method handles representing regular
719 // transforms and those representing call site transforms. Method
720 // handles for call site transforms adapt their MethodType to match
721 // the call site. For these, the |callee_type| is the same as the
722 // |callsite_type|. The VarargsCollector is such a tranform, its
723 // method type depends on the call site, ie. x(a) or x(a, b), or
724 // x(a, b, c). The VarargsCollector invokes a variable arity method
725 // with the arity arguments in an array.
726 Handle<mirror::MethodType> callee_type =
727 (handle_kind == mirror::MethodHandle::Kind::kInvokeCallSiteTransform) ? callsite_type
728 : handle_type;
Orion Hodson960d4f72017-11-10 15:32:38 +0000729 return MethodHandleInvokeTransform(called_method,
730 callsite_type,
731 callee_type,
732 self,
733 shadow_frame,
734 method_handle /* receiver */,
735 operands,
736 result);
Orion Hodsona1be7132017-03-21 10:04:12 +0000737 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000738 return MethodHandleInvokeMethod(called_method,
739 callsite_type,
740 handle_type,
741 self,
742 shadow_frame,
743 operands,
744 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000745 }
746}
747
748// Helper for getters in invoke-polymorphic.
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100749inline static void MethodHandleFieldGet(Thread* self,
750 const ShadowFrame& shadow_frame,
751 ObjPtr<mirror::Object>& obj,
752 ArtField* field,
753 Primitive::Type field_type,
754 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000755 REQUIRES_SHARED(Locks::mutator_lock_) {
756 switch (field_type) {
757 case Primitive::kPrimBoolean:
758 DoFieldGetCommon<Primitive::kPrimBoolean>(self, shadow_frame, obj, field, result);
759 break;
760 case Primitive::kPrimByte:
761 DoFieldGetCommon<Primitive::kPrimByte>(self, shadow_frame, obj, field, result);
762 break;
763 case Primitive::kPrimChar:
764 DoFieldGetCommon<Primitive::kPrimChar>(self, shadow_frame, obj, field, result);
765 break;
766 case Primitive::kPrimShort:
767 DoFieldGetCommon<Primitive::kPrimShort>(self, shadow_frame, obj, field, result);
768 break;
769 case Primitive::kPrimInt:
770 DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result);
771 break;
772 case Primitive::kPrimLong:
773 DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result);
774 break;
775 case Primitive::kPrimFloat:
776 DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result);
777 break;
778 case Primitive::kPrimDouble:
779 DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result);
780 break;
781 case Primitive::kPrimNot:
782 DoFieldGetCommon<Primitive::kPrimNot>(self, shadow_frame, obj, field, result);
783 break;
784 case Primitive::kPrimVoid:
785 LOG(FATAL) << "Unreachable: " << field_type;
786 UNREACHABLE();
787 }
788}
789
790// Helper for setters in invoke-polymorphic.
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100791inline bool MethodHandleFieldPut(Thread* self,
792 ShadowFrame& shadow_frame,
793 ObjPtr<mirror::Object>& obj,
794 ArtField* field,
795 Primitive::Type field_type,
796 JValue& value)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000797 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodsonc069a302017-01-18 09:23:12 +0000798 DCHECK(!Runtime::Current()->IsActiveTransaction());
799 static const bool kTransaction = false; // Not in a transaction.
800 static const bool kAssignabilityCheck = false; // No access check.
Orion Hodson811bd5f2016-12-07 11:35:37 +0000801 switch (field_type) {
802 case Primitive::kPrimBoolean:
Orion Hodsonc069a302017-01-18 09:23:12 +0000803 return
804 DoFieldPutCommon<Primitive::kPrimBoolean, kAssignabilityCheck, kTransaction>(
805 self, shadow_frame, obj, field, value);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000806 case Primitive::kPrimByte:
Orion Hodsonc069a302017-01-18 09:23:12 +0000807 return DoFieldPutCommon<Primitive::kPrimByte, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000808 self, shadow_frame, obj, field, value);
809 case Primitive::kPrimChar:
Orion Hodsonc069a302017-01-18 09:23:12 +0000810 return DoFieldPutCommon<Primitive::kPrimChar, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000811 self, shadow_frame, obj, field, value);
812 case Primitive::kPrimShort:
Orion Hodsonc069a302017-01-18 09:23:12 +0000813 return DoFieldPutCommon<Primitive::kPrimShort, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000814 self, shadow_frame, obj, field, value);
815 case Primitive::kPrimInt:
816 case Primitive::kPrimFloat:
Orion Hodsonc069a302017-01-18 09:23:12 +0000817 return DoFieldPutCommon<Primitive::kPrimInt, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000818 self, shadow_frame, obj, field, value);
819 case Primitive::kPrimLong:
820 case Primitive::kPrimDouble:
Orion Hodsonc069a302017-01-18 09:23:12 +0000821 return DoFieldPutCommon<Primitive::kPrimLong, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000822 self, shadow_frame, obj, field, value);
823 case Primitive::kPrimNot:
Orion Hodsonc069a302017-01-18 09:23:12 +0000824 return DoFieldPutCommon<Primitive::kPrimNot, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000825 self, shadow_frame, obj, field, value);
826 case Primitive::kPrimVoid:
827 LOG(FATAL) << "Unreachable: " << field_type;
828 UNREACHABLE();
829 }
830}
831
832static JValue GetValueFromShadowFrame(const ShadowFrame& shadow_frame,
833 Primitive::Type field_type,
834 uint32_t vreg)
835 REQUIRES_SHARED(Locks::mutator_lock_) {
836 JValue field_value;
837 switch (field_type) {
838 case Primitive::kPrimBoolean:
839 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
840 break;
841 case Primitive::kPrimByte:
842 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
843 break;
844 case Primitive::kPrimChar:
845 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
846 break;
847 case Primitive::kPrimShort:
848 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
849 break;
850 case Primitive::kPrimInt:
851 case Primitive::kPrimFloat:
852 field_value.SetI(shadow_frame.GetVReg(vreg));
853 break;
854 case Primitive::kPrimLong:
855 case Primitive::kPrimDouble:
856 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
857 break;
858 case Primitive::kPrimNot:
859 field_value.SetL(shadow_frame.GetVRegReference(vreg));
860 break;
861 case Primitive::kPrimVoid:
862 LOG(FATAL) << "Unreachable: " << field_type;
863 UNREACHABLE();
864 }
865 return field_value;
866}
867
Orion Hodson960d4f72017-11-10 15:32:38 +0000868template <bool do_conversions>
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100869bool MethodHandleFieldAccess(Thread* self,
870 ShadowFrame& shadow_frame,
871 Handle<mirror::MethodHandle> method_handle,
872 Handle<mirror::MethodType> callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +0000873 const InstructionOperands* const operands,
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100874 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000875 StackHandleScope<1> hs(self);
876 Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType()));
877 const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind();
878 ArtField* field = method_handle->GetTargetField();
879 Primitive::Type field_type = field->GetTypeAsPrimitiveType();
Orion Hodson811bd5f2016-12-07 11:35:37 +0000880 switch (handle_kind) {
881 case mirror::MethodHandle::kInstanceGet: {
Orion Hodson960d4f72017-11-10 15:32:38 +0000882 size_t obj_reg = operands->GetOperand(0);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000883 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(obj_reg);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100884 MethodHandleFieldGet(self, shadow_frame, obj, field, field_type, result);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000885 if (do_conversions && !ConvertReturnValue(callsite_type, handle_type, result)) {
886 DCHECK(self->IsExceptionPending());
887 return false;
888 }
889 return true;
890 }
891 case mirror::MethodHandle::kStaticGet: {
892 ObjPtr<mirror::Object> obj = GetAndInitializeDeclaringClass(self, field);
893 if (obj == nullptr) {
894 DCHECK(self->IsExceptionPending());
895 return false;
896 }
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100897 MethodHandleFieldGet(self, shadow_frame, obj, field, field_type, result);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000898 if (do_conversions && !ConvertReturnValue(callsite_type, handle_type, result)) {
899 DCHECK(self->IsExceptionPending());
900 return false;
901 }
902 return true;
903 }
904 case mirror::MethodHandle::kInstancePut: {
Orion Hodson960d4f72017-11-10 15:32:38 +0000905 size_t obj_reg = operands->GetOperand(0);
906 size_t value_reg = operands->GetOperand(1);
Mathieu Chartier71b17082017-04-17 20:12:29 -0700907 const size_t kPTypeIndex = 1;
908 // Use ptypes instead of field type since we may be unboxing a reference for a primitive
909 // field. The field type is incorrect for this case.
910 JValue value = GetValueFromShadowFrame(
911 shadow_frame,
912 callsite_type->GetPTypes()->Get(kPTypeIndex)->GetPrimitiveType(),
913 value_reg);
914 if (do_conversions && !ConvertArgumentValue(callsite_type,
915 handle_type,
916 kPTypeIndex,
917 &value)) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000918 DCHECK(self->IsExceptionPending());
919 return false;
920 }
921 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(obj_reg);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100922 return MethodHandleFieldPut(self, shadow_frame, obj, field, field_type, value);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000923 }
924 case mirror::MethodHandle::kStaticPut: {
925 ObjPtr<mirror::Object> obj = GetAndInitializeDeclaringClass(self, field);
926 if (obj == nullptr) {
927 DCHECK(self->IsExceptionPending());
928 return false;
929 }
Orion Hodson960d4f72017-11-10 15:32:38 +0000930 size_t value_reg = operands->GetOperand(0);
Mathieu Chartier71b17082017-04-17 20:12:29 -0700931 const size_t kPTypeIndex = 0;
932 // Use ptypes instead of field type since we may be unboxing a reference for a primitive
933 // field. The field type is incorrect for this case.
934 JValue value = GetValueFromShadowFrame(
935 shadow_frame,
936 callsite_type->GetPTypes()->Get(kPTypeIndex)->GetPrimitiveType(),
937 value_reg);
938 if (do_conversions && !ConvertArgumentValue(callsite_type,
939 handle_type,
940 kPTypeIndex,
941 &value)) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000942 DCHECK(self->IsExceptionPending());
943 return false;
944 }
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100945 return MethodHandleFieldPut(self, shadow_frame, obj, field, field_type, value);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000946 }
947 default:
948 LOG(FATAL) << "Unreachable: " << handle_kind;
949 UNREACHABLE();
950 }
951}
952
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100953static inline bool MethodHandleInvokeInternal(Thread* self,
954 ShadowFrame& shadow_frame,
955 Handle<mirror::MethodHandle> method_handle,
956 Handle<mirror::MethodType> callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +0000957 const InstructionOperands* const operands,
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100958 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000959 REQUIRES_SHARED(Locks::mutator_lock_) {
960 const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind();
Orion Hodsona1be7132017-03-21 10:04:12 +0000961 if (IsFieldAccess(handle_kind)) {
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100962 ObjPtr<mirror::MethodType> handle_type(method_handle->GetMethodType());
Orion Hodsona1be7132017-03-21 10:04:12 +0000963 DCHECK(!callsite_type->IsExactMatch(handle_type.Ptr()));
964 if (!callsite_type->IsConvertible(handle_type.Ptr())) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000965 ThrowWrongMethodTypeException(handle_type.Ptr(), callsite_type.Get());
966 return false;
967 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000968 const bool do_convert = true;
Orion Hodson960d4f72017-11-10 15:32:38 +0000969 return MethodHandleFieldAccess<do_convert>(
Orion Hodsona1be7132017-03-21 10:04:12 +0000970 self,
971 shadow_frame,
972 method_handle,
973 callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +0000974 operands,
Orion Hodsona1be7132017-03-21 10:04:12 +0000975 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000976 }
Orion Hodson960d4f72017-11-10 15:32:38 +0000977 return DoInvokePolymorphicMethod(self,
978 shadow_frame,
979 method_handle,
980 callsite_type,
981 operands,
982 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000983}
984
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100985static inline bool MethodHandleInvokeExactInternal(
986 Thread* self,
987 ShadowFrame& shadow_frame,
988 Handle<mirror::MethodHandle> method_handle,
989 Handle<mirror::MethodType> callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +0000990 const InstructionOperands* const operands,
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100991 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000992 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodsona1be7132017-03-21 10:04:12 +0000993 StackHandleScope<1> hs(self);
Orion Hodsona1be7132017-03-21 10:04:12 +0000994 Handle<mirror::MethodType> method_handle_type(hs.NewHandle(method_handle->GetMethodType()));
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100995 if (!callsite_type->IsExactMatch(method_handle_type.Get())) {
996 ThrowWrongMethodTypeException(method_handle_type.Get(), callsite_type.Get());
997 return false;
998 }
999
1000 const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind();
Orion Hodson811bd5f2016-12-07 11:35:37 +00001001 if (IsFieldAccess(handle_kind)) {
1002 const bool do_convert = false;
Orion Hodson960d4f72017-11-10 15:32:38 +00001003 return MethodHandleFieldAccess<do_convert>(self,
1004 shadow_frame,
1005 method_handle,
1006 callsite_type,
1007 operands,
1008 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +00001009 }
1010
Orion Hodsona1be7132017-03-21 10:04:12 +00001011 // Slow-path check.
1012 if (IsInvokeTransform(handle_kind) || IsCallerTransformer(callsite_type)) {
Orion Hodson960d4f72017-11-10 15:32:38 +00001013 return DoInvokePolymorphicMethod(self,
1014 shadow_frame,
1015 method_handle,
1016 callsite_type,
1017 operands,
1018 result);
Orion Hodsona1be7132017-03-21 10:04:12 +00001019 }
1020
1021 // On the fast-path. This is equivalent to DoCallPolymoprhic without the conversion paths.
1022 ArtMethod* target_method = method_handle->GetTargetMethod();
Orion Hodson960d4f72017-11-10 15:32:38 +00001023 uint32_t receiver_reg = (operands->GetNumberOfOperands() > 0) ? operands->GetOperand(0) : 0u;
Orion Hodsona1be7132017-03-21 10:04:12 +00001024 ArtMethod* called_method = RefineTargetMethod(self,
Orion Hodson811bd5f2016-12-07 11:35:37 +00001025 shadow_frame,
Orion Hodsona1be7132017-03-21 10:04:12 +00001026 handle_kind,
1027 method_handle_type,
Orion Hodson811bd5f2016-12-07 11:35:37 +00001028 callsite_type,
Orion Hodsona1be7132017-03-21 10:04:12 +00001029 receiver_reg,
1030 target_method);
1031 if (called_method == nullptr) {
1032 DCHECK(self->IsExceptionPending());
1033 return false;
1034 }
1035
1036 // Compute method information.
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001037 CodeItemDataAccessor accessor(called_method);
Orion Hodsona1be7132017-03-21 10:04:12 +00001038 uint16_t num_regs;
1039 size_t num_input_regs;
1040 size_t first_dest_reg;
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001041 if (LIKELY(accessor.HasCodeItem())) {
1042 num_regs = accessor.RegistersSize();
1043 first_dest_reg = num_regs - accessor.InsSize();
1044 num_input_regs = accessor.InsSize();
Orion Hodsona1be7132017-03-21 10:04:12 +00001045 // Parameter registers go at the end of the shadow frame.
1046 DCHECK_NE(first_dest_reg, (size_t)-1);
1047 } else {
1048 // No local regs for proxy and native methods.
1049 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1050 num_regs = num_input_regs = GetInsForProxyOrNativeMethod(called_method);
1051 first_dest_reg = 0;
1052 }
1053
1054 // Allocate shadow frame on the stack.
1055 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
1056 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
1057 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
1058 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Orion Hodson960d4f72017-11-10 15:32:38 +00001059 CopyArgumentsFromCallerFrame(shadow_frame,
1060 new_shadow_frame,
1061 operands,
1062 first_dest_reg);
Orion Hodsona1be7132017-03-21 10:04:12 +00001063 self->EndAssertNoThreadSuspension(old_cause);
1064
Jeff Hao5ea84132017-05-05 16:59:29 -07001065 bool use_interpreter_entrypoint = ClassLinker::ShouldUseInterpreterEntrypoint(
1066 called_method, called_method->GetEntryPointFromQuickCompiledCode());
1067 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001068 accessor,
Jeff Hao5ea84132017-05-05 16:59:29 -07001069 shadow_frame.GetMethod(),
1070 first_dest_reg,
1071 new_shadow_frame,
1072 result,
1073 use_interpreter_entrypoint);
Orion Hodsona1be7132017-03-21 10:04:12 +00001074 if (self->IsExceptionPending()) {
1075 return false;
1076 }
1077 return true;
Orion Hodson811bd5f2016-12-07 11:35:37 +00001078}
1079
1080} // namespace
1081
Orion Hodson960d4f72017-11-10 15:32:38 +00001082bool MethodHandleInvoke(Thread* self,
1083 ShadowFrame& shadow_frame,
1084 Handle<mirror::MethodHandle> method_handle,
1085 Handle<mirror::MethodType> callsite_type,
1086 const InstructionOperands* const operands,
1087 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +00001088 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001089 if (UNLIKELY(callsite_type->IsExactMatch(method_handle->GetMethodType()))) {
1090 // A non-exact invoke that can be invoked exactly.
Orion Hodson960d4f72017-11-10 15:32:38 +00001091 return MethodHandleInvokeExactInternal(self,
1092 shadow_frame,
1093 method_handle,
1094 callsite_type,
1095 operands,
1096 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +00001097 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +00001098 return MethodHandleInvokeInternal(self,
1099 shadow_frame,
1100 method_handle,
1101 callsite_type,
1102 operands,
1103 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +00001104 }
1105}
1106
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001107bool MethodHandleInvokeExact(Thread* self,
Orion Hodson960d4f72017-11-10 15:32:38 +00001108 ShadowFrame& shadow_frame,
1109 Handle<mirror::MethodHandle> method_handle,
1110 Handle<mirror::MethodType> callsite_type,
1111 const InstructionOperands* const operands,
1112 JValue* result)
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001113 REQUIRES_SHARED(Locks::mutator_lock_) {
1114 // We need to check the nominal type of the handle in addition to the
1115 // real type. The "nominal" type is present when MethodHandle.asType is
1116 // called any handle, and results in the declared type of the handle
1117 // changing.
1118 ObjPtr<mirror::MethodType> nominal_type(method_handle->GetNominalType());
1119 if (UNLIKELY(nominal_type != nullptr)) {
1120 if (UNLIKELY(!callsite_type->IsExactMatch(nominal_type.Ptr()))) {
1121 ThrowWrongMethodTypeException(nominal_type.Ptr(), callsite_type.Get());
1122 return false;
1123 }
1124 if (LIKELY(!nominal_type->IsExactMatch(method_handle->GetMethodType()))) {
1125 // Different nominal type means we have to treat as non-exact.
Orion Hodson960d4f72017-11-10 15:32:38 +00001126 return MethodHandleInvokeInternal(self,
1127 shadow_frame,
1128 method_handle,
1129 callsite_type,
1130 operands,
1131 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001132 }
1133 }
Orion Hodson960d4f72017-11-10 15:32:38 +00001134 return MethodHandleInvokeExactInternal(self,
1135 shadow_frame,
1136 method_handle,
1137 callsite_type,
1138 operands,
1139 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001140}
1141
Orion Hodsonba28f9f2016-10-26 10:56:25 +01001142} // namespace art