blob: 78e75616f17a997f4f05bbe73c2be0e61d3a638e [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
Vladimir Marko5868ada2020-05-12 11:50:34 +010021#include "class_root-inl.h"
Orion Hodson811bd5f2016-12-07 11:35:37 +000022#include "common_dex_operations.h"
Alex Light49df7152019-10-03 11:22:35 -070023#include "common_throws.h"
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +010024#include "interpreter/shadow_frame-inl.h"
Orion Hodsonba28f9f2016-10-26 10:56:25 +010025#include "jvalue-inl.h"
Andreas Gampe88dbad32018-06-26 19:54:12 -070026#include "mirror/class-inl.h"
Vladimir Marko423bebb2019-03-26 15:17:21 +000027#include "mirror/emulated_stack_frame-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080028#include "mirror/method_handle_impl-inl.h"
Vladimir Marko5aead702019-03-27 11:00:36 +000029#include "mirror/method_type-inl.h"
Orion Hodsonb8b93872018-01-30 07:51:10 +000030#include "mirror/var_handle.h"
Orion Hodsonba28f9f2016-10-26 10:56:25 +010031#include "reflection-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070032#include "reflection.h"
Orion Hodson1a06f9f2016-11-09 08:32:42 +000033#include "well_known_classes.h"
Orion Hodsonba28f9f2016-10-26 10:56:25 +010034
35namespace art {
36
Andreas Gampe46ee31b2016-12-14 10:11:49 -080037using android::base::StringPrintf;
38
Orion Hodsonba28f9f2016-10-26 10:56:25 +010039namespace {
40
Orion Hodson1a06f9f2016-11-09 08:32:42 +000041#define PRIMITIVES_LIST(V) \
42 V(Primitive::kPrimBoolean, Boolean, Boolean, Z) \
43 V(Primitive::kPrimByte, Byte, Byte, B) \
44 V(Primitive::kPrimChar, Char, Character, C) \
45 V(Primitive::kPrimShort, Short, Short, S) \
46 V(Primitive::kPrimInt, Int, Integer, I) \
47 V(Primitive::kPrimLong, Long, Long, J) \
48 V(Primitive::kPrimFloat, Float, Float, F) \
49 V(Primitive::kPrimDouble, Double, Double, D)
Orion Hodsonba28f9f2016-10-26 10:56:25 +010050
51// Assigns |type| to the primitive type associated with |klass|. Returns
52// true iff. |klass| was a boxed type (Integer, Long etc.), false otherwise.
53bool GetUnboxedPrimitiveType(ObjPtr<mirror::Class> klass, Primitive::Type* type)
54 REQUIRES_SHARED(Locks::mutator_lock_) {
55 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Orion Hodsona1be7132017-03-21 10:04:12 +000056 std::string storage;
57 const char* descriptor = klass->GetDescriptor(&storage);
58 static const char kJavaLangPrefix[] = "Ljava/lang/";
59 static const size_t kJavaLangPrefixSize = sizeof(kJavaLangPrefix) - 1;
60 if (strncmp(descriptor, kJavaLangPrefix, kJavaLangPrefixSize) != 0) {
61 return false;
62 }
63
64 descriptor += kJavaLangPrefixSize;
65#define LOOKUP_PRIMITIVE(primitive, _, java_name, ___) \
66 if (strcmp(descriptor, #java_name ";") == 0) { \
67 *type = primitive; \
68 return true; \
Orion Hodsonba28f9f2016-10-26 10:56:25 +010069 }
Orion Hodsonba28f9f2016-10-26 10:56:25 +010070
Orion Hodson1a06f9f2016-11-09 08:32:42 +000071 PRIMITIVES_LIST(LOOKUP_PRIMITIVE);
72#undef LOOKUP_PRIMITIVE
Orion Hodsonba28f9f2016-10-26 10:56:25 +010073 return false;
74}
75
Orion Hodson1a06f9f2016-11-09 08:32:42 +000076ObjPtr<mirror::Class> GetBoxedPrimitiveClass(Primitive::Type type)
Orion Hodsonba28f9f2016-10-26 10:56:25 +010077 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson1a06f9f2016-11-09 08:32:42 +000078 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
79 jmethodID m = nullptr;
80 switch (type) {
81#define CASE_PRIMITIVE(primitive, _, java_name, __) \
82 case primitive: \
83 m = WellKnownClasses::java_lang_ ## java_name ## _valueOf; \
84 break;
85 PRIMITIVES_LIST(CASE_PRIMITIVE);
86#undef CASE_PRIMITIVE
87 case Primitive::Type::kPrimNot:
88 case Primitive::Type::kPrimVoid:
89 return nullptr;
Orion Hodsonba28f9f2016-10-26 10:56:25 +010090 }
Orion Hodson1a06f9f2016-11-09 08:32:42 +000091 return jni::DecodeArtMethod(m)->GetDeclaringClass();
92}
Orion Hodsonba28f9f2016-10-26 10:56:25 +010093
Orion Hodson1a06f9f2016-11-09 08:32:42 +000094bool GetUnboxedTypeAndValue(ObjPtr<mirror::Object> o, Primitive::Type* type, JValue* value)
95 REQUIRES_SHARED(Locks::mutator_lock_) {
96 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Orion Hodsonba28f9f2016-10-26 10:56:25 +010097 ObjPtr<mirror::Class> klass = o->GetClass();
Orion Hodsonba28f9f2016-10-26 10:56:25 +010098 ArtField* primitive_field = &klass->GetIFieldsPtr()->At(0);
Orion Hodson1a06f9f2016-11-09 08:32:42 +000099#define CASE_PRIMITIVE(primitive, abbrev, _, shorthand) \
100 if (klass == GetBoxedPrimitiveClass(primitive)) { \
101 *type = primitive; \
102 value->Set ## shorthand(primitive_field->Get ## abbrev(o)); \
103 return true; \
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100104 }
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000105 PRIMITIVES_LIST(CASE_PRIMITIVE)
106#undef CASE_PRIMITIVE
107 return false;
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100108}
109
110inline bool IsReferenceType(Primitive::Type type) {
111 return type == Primitive::kPrimNot;
112}
113
114inline bool IsPrimitiveType(Primitive::Type type) {
115 return !IsReferenceType(type);
116}
117
118} // namespace
119
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000120bool IsParameterTypeConvertible(ObjPtr<mirror::Class> from, ObjPtr<mirror::Class> to)
121 REQUIRES_SHARED(Locks::mutator_lock_) {
122 // This function returns true if there's any conceivable conversion
123 // between |from| and |to|. It's expected this method will be used
124 // to determine if a WrongMethodTypeException should be raised. The
125 // decision logic follows the documentation for MethodType.asType().
126 if (from == to) {
127 return true;
128 }
129
130 Primitive::Type from_primitive = from->GetPrimitiveType();
131 Primitive::Type to_primitive = to->GetPrimitiveType();
132 DCHECK(from_primitive != Primitive::Type::kPrimVoid);
133 DCHECK(to_primitive != Primitive::Type::kPrimVoid);
134
135 // If |to| and |from| are references.
136 if (IsReferenceType(from_primitive) && IsReferenceType(to_primitive)) {
137 // Assignability is determined during parameter conversion when
138 // invoking the associated method handle.
139 return true;
140 }
141
142 // If |to| and |from| are primitives and a widening conversion exists.
143 if (Primitive::IsWidenable(from_primitive, to_primitive)) {
144 return true;
145 }
146
147 // If |to| is a reference and |from| is a primitive, then boxing conversion.
148 if (IsReferenceType(to_primitive) && IsPrimitiveType(from_primitive)) {
149 return to->IsAssignableFrom(GetBoxedPrimitiveClass(from_primitive));
150 }
151
152 // If |from| is a reference and |to| is a primitive, then unboxing conversion.
153 if (IsPrimitiveType(to_primitive) && IsReferenceType(from_primitive)) {
154 if (from->DescriptorEquals("Ljava/lang/Object;")) {
155 // Object might be converted into a primitive during unboxing.
156 return true;
Orion Hodsona1be7132017-03-21 10:04:12 +0000157 }
158
159 if (Primitive::IsNumericType(to_primitive) && from->DescriptorEquals("Ljava/lang/Number;")) {
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000160 // Number might be unboxed into any of the number primitive types.
161 return true;
162 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000163
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000164 Primitive::Type unboxed_type;
165 if (GetUnboxedPrimitiveType(from, &unboxed_type)) {
Orion Hodsonf1412b42016-11-11 12:03:29 +0000166 if (unboxed_type == to_primitive) {
167 // Straightforward unboxing conversion such as Boolean => boolean.
168 return true;
Orion Hodsonf1412b42016-11-11 12:03:29 +0000169 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000170
171 // Check if widening operations for numeric primitives would work,
172 // such as Byte => byte => long.
173 return Primitive::IsWidenable(unboxed_type, to_primitive);
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000174 }
175 }
176
177 return false;
178}
179
180bool IsReturnTypeConvertible(ObjPtr<mirror::Class> from, ObjPtr<mirror::Class> to)
181 REQUIRES_SHARED(Locks::mutator_lock_) {
182 if (to->GetPrimitiveType() == Primitive::Type::kPrimVoid) {
183 // Result will be ignored.
184 return true;
185 } else if (from->GetPrimitiveType() == Primitive::Type::kPrimVoid) {
186 // Returned value will be 0 / null.
187 return true;
188 } else {
189 // Otherwise apply usual parameter conversion rules.
190 return IsParameterTypeConvertible(from, to);
191 }
192}
193
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100194bool ConvertJValueCommon(
195 Handle<mirror::MethodType> callsite_type,
196 Handle<mirror::MethodType> callee_type,
197 ObjPtr<mirror::Class> from,
198 ObjPtr<mirror::Class> to,
199 JValue* value) {
200 // The reader maybe concerned about the safety of the heap object
201 // that may be in |value|. There is only one case where allocation
202 // is obviously needed and that's for boxing. However, in the case
203 // of boxing |value| contains a non-reference type.
204
205 const Primitive::Type from_type = from->GetPrimitiveType();
206 const Primitive::Type to_type = to->GetPrimitiveType();
207
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000208 // Put incoming value into |src_value| and set return value to 0.
209 // Errors and conversions from void require the return value to be 0.
210 const JValue src_value(*value);
211 value->SetJ(0);
212
213 // Conversion from void set result to zero.
214 if (from_type == Primitive::kPrimVoid) {
215 return true;
216 }
217
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100218 // This method must be called only when the types don't match.
219 DCHECK(from != to);
220
221 if (IsPrimitiveType(from_type) && IsPrimitiveType(to_type)) {
222 // The source and target types are both primitives.
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000223 if (UNLIKELY(!ConvertPrimitiveValueNoThrow(from_type, to_type, src_value, value))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100224 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100225 return false;
226 }
227 return true;
228 } else if (IsReferenceType(from_type) && IsReferenceType(to_type)) {
229 // They're both reference types. If "from" is null, we can pass it
230 // through unchanged. If not, we must generate a cast exception if
231 // |to| is not assignable from the dynamic type of |ref|.
232 //
233 // Playing it safe with StackHandleScope here, not expecting any allocation
234 // in mirror::Class::IsAssignable().
235 StackHandleScope<2> hs(Thread::Current());
236 Handle<mirror::Class> h_to(hs.NewHandle(to));
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000237 Handle<mirror::Object> h_obj(hs.NewHandle(src_value.GetL()));
Orion Hodsonc1d3bac2018-01-26 14:38:55 +0000238 if (UNLIKELY(!h_obj.IsNull() && !to->IsAssignableFrom(h_obj->GetClass()))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100239 ThrowClassCastException(h_to.Get(), h_obj->GetClass());
240 return false;
241 }
242 value->SetL(h_obj.Get());
243 return true;
244 } else if (IsReferenceType(to_type)) {
245 DCHECK(IsPrimitiveType(from_type));
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100246 // The source type is a primitive and the target type is a reference, so we must box.
247 // The target type maybe a super class of the boxed source type, for example,
248 // if the source type is int, it's boxed type is java.lang.Integer, and the target
249 // type could be java.lang.Number.
250 Primitive::Type type;
251 if (!GetUnboxedPrimitiveType(to, &type)) {
252 ObjPtr<mirror::Class> boxed_from_class = GetBoxedPrimitiveClass(from_type);
Orion Hodsonc1d3bac2018-01-26 14:38:55 +0000253 if (LIKELY(boxed_from_class->IsSubClass(to))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100254 type = from_type;
255 } else {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100256 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
257 return false;
258 }
259 }
260
261 if (UNLIKELY(from_type != type)) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100262 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
263 return false;
264 }
265
Orion Hodsonc1d3bac2018-01-26 14:38:55 +0000266 if (UNLIKELY(!ConvertPrimitiveValueNoThrow(from_type, type, src_value, value))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100267 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
268 return false;
269 }
270
271 // Then perform the actual boxing, and then set the reference.
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000272 ObjPtr<mirror::Object> boxed = BoxPrimitive(type, src_value);
Vladimir Markobcf17522018-06-01 13:14:32 +0100273 value->SetL(boxed);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100274 return true;
275 } else {
276 // The source type is a reference and the target type is a primitive, so we must unbox.
277 DCHECK(IsReferenceType(from_type));
278 DCHECK(IsPrimitiveType(to_type));
279
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000280 ObjPtr<mirror::Object> from_obj(src_value.GetL());
Orion Hodsonc1d3bac2018-01-26 14:38:55 +0000281 if (UNLIKELY(from_obj.IsNull())) {
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000282 ThrowNullPointerException(
283 StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
284 from->PrettyDescriptor().c_str()).c_str());
285 return false;
286 }
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100287
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000288 Primitive::Type unboxed_type;
289 JValue unboxed_value;
290 if (UNLIKELY(!GetUnboxedTypeAndValue(from_obj, &unboxed_type, &unboxed_value))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100291 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
292 return false;
293 }
294
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000295 if (UNLIKELY(!ConvertPrimitiveValueNoThrow(unboxed_type, to_type, unboxed_value, value))) {
Orion Hodsonc1d3bac2018-01-26 14:38:55 +0000296 if (from->IsAssignableFrom(GetBoxedPrimitiveClass(to_type))) {
297 // CallSite may be Number, but the Number object is
298 // incompatible, e.g. Number (Integer) for a short.
299 ThrowClassCastException(from, to);
300 } else {
301 // CallSite is incompatible, e.g. Integer for a short.
302 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
303 }
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100304 return false;
305 }
306
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000307 return true;
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100308 }
309}
310
Orion Hodson811bd5f2016-12-07 11:35:37 +0000311namespace {
312
Orion Hodson811bd5f2016-12-07 11:35:37 +0000313inline void CopyArgumentsFromCallerFrame(const ShadowFrame& caller_frame,
314 ShadowFrame* callee_frame,
Orion Hodson960d4f72017-11-10 15:32:38 +0000315 const InstructionOperands* const operands,
316 const size_t first_dst_reg)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000317 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000318 for (size_t i = 0; i < operands->GetNumberOfOperands(); ++i) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000319 size_t dst_reg = first_dst_reg + i;
Orion Hodson960d4f72017-11-10 15:32:38 +0000320 size_t src_reg = operands->GetOperand(i);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000321 // Uint required, so that sign extension does not make this wrong on 64-bit systems
322 uint32_t src_value = caller_frame.GetVReg(src_reg);
323 ObjPtr<mirror::Object> o = caller_frame.GetVRegReference<kVerifyNone>(src_reg);
324 // If both register locations contains the same value, the register probably holds a reference.
325 // Note: As an optimization, non-moving collectors leave a stale reference value
326 // in the references array even after the original vreg was overwritten to a non-reference.
327 if (src_value == reinterpret_cast<uintptr_t>(o.Ptr())) {
Vladimir Markobcf17522018-06-01 13:14:32 +0100328 callee_frame->SetVRegReference(dst_reg, o);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000329 } else {
330 callee_frame->SetVReg(dst_reg, src_value);
331 }
332 }
333}
334
Orion Hodson811bd5f2016-12-07 11:35:37 +0000335inline bool ConvertAndCopyArgumentsFromCallerFrame(
336 Thread* self,
337 Handle<mirror::MethodType> callsite_type,
338 Handle<mirror::MethodType> callee_type,
339 const ShadowFrame& caller_frame,
Orion Hodson960d4f72017-11-10 15:32:38 +0000340 uint32_t first_dest_reg,
341 const InstructionOperands* const operands,
Orion Hodson811bd5f2016-12-07 11:35:37 +0000342 ShadowFrame* callee_frame)
343 REQUIRES_SHARED(Locks::mutator_lock_) {
344 ObjPtr<mirror::ObjectArray<mirror::Class>> from_types(callsite_type->GetPTypes());
345 ObjPtr<mirror::ObjectArray<mirror::Class>> to_types(callee_type->GetPTypes());
346
347 const int32_t num_method_params = from_types->GetLength();
348 if (to_types->GetLength() != num_method_params) {
349 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
350 return false;
351 }
352
Orion Hodson928033d2018-02-07 05:30:54 +0000353 ShadowFrameGetter getter(caller_frame, operands);
Orion Hodson960d4f72017-11-10 15:32:38 +0000354 ShadowFrameSetter setter(callee_frame, first_dest_reg);
355 return PerformConversions<ShadowFrameGetter, ShadowFrameSetter>(self,
356 callsite_type,
357 callee_type,
358 &getter,
359 &setter,
360 num_method_params);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000361}
362
Orion Hodson811bd5f2016-12-07 11:35:37 +0000363inline bool IsInvoke(const mirror::MethodHandle::Kind handle_kind) {
364 return handle_kind <= mirror::MethodHandle::Kind::kLastInvokeKind;
365}
366
367inline bool IsInvokeTransform(const mirror::MethodHandle::Kind handle_kind) {
368 return (handle_kind == mirror::MethodHandle::Kind::kInvokeTransform
369 || handle_kind == mirror::MethodHandle::Kind::kInvokeCallSiteTransform);
370}
371
Orion Hodsonb8b93872018-01-30 07:51:10 +0000372inline bool IsInvokeVarHandle(const mirror::MethodHandle::Kind handle_kind) {
373 return (handle_kind == mirror::MethodHandle::Kind::kInvokeVarHandle ||
374 handle_kind == mirror::MethodHandle::Kind::kInvokeVarHandleExact);
375}
376
Orion Hodson811bd5f2016-12-07 11:35:37 +0000377inline bool IsFieldAccess(mirror::MethodHandle::Kind handle_kind) {
378 return (handle_kind >= mirror::MethodHandle::Kind::kFirstAccessorKind
379 && handle_kind <= mirror::MethodHandle::Kind::kLastAccessorKind);
380}
381
382// Calculate the number of ins for a proxy or native method, where we
383// can't just look at the code item.
384static inline size_t GetInsForProxyOrNativeMethod(ArtMethod* method)
385 REQUIRES_SHARED(Locks::mutator_lock_) {
386 DCHECK(method->IsNative() || method->IsProxyMethod());
Orion Hodson811bd5f2016-12-07 11:35:37 +0000387 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Orion Hodsona1be7132017-03-21 10:04:12 +0000388 uint32_t shorty_length = 0;
389 const char* shorty = method->GetShorty(&shorty_length);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000390
Orion Hodsona1be7132017-03-21 10:04:12 +0000391 // Static methods do not include the receiver. The receiver isn't included
392 // in the shorty_length though the return value is.
393 size_t num_ins = method->IsStatic() ? shorty_length - 1 : shorty_length;
394 for (const char* c = shorty + 1; *c != '\0'; ++c) {
395 if (*c == 'J' || *c == 'D') {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000396 ++num_ins;
397 }
398 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000399 return num_ins;
400}
401
402// Returns true iff. the callsite type for a polymorphic invoke is transformer
403// like, i.e that it has a single input argument whose type is
404// dalvik.system.EmulatedStackFrame.
405static inline bool IsCallerTransformer(Handle<mirror::MethodType> callsite_type)
406 REQUIRES_SHARED(Locks::mutator_lock_) {
407 ObjPtr<mirror::ObjectArray<mirror::Class>> param_types(callsite_type->GetPTypes());
408 if (param_types->GetLength() == 1) {
409 ObjPtr<mirror::Class> param(param_types->GetWithoutChecks(0));
Orion Hodsona1be7132017-03-21 10:04:12 +0000410 // NB Comparing descriptor here as it appears faster in cycle simulation than using:
411 // param == WellKnownClasses::ToClass(WellKnownClasses::dalvik_system_EmulatedStackFrame)
412 // Costs are 98 vs 173 cycles per invocation.
413 return param->DescriptorEquals("Ldalvik/system/EmulatedStackFrame;");
Orion Hodson811bd5f2016-12-07 11:35:37 +0000414 }
415
416 return false;
417}
418
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100419static inline bool MethodHandleInvokeMethod(ArtMethod* called_method,
420 Handle<mirror::MethodType> callsite_type,
421 Handle<mirror::MethodType> target_type,
Orion Hodson2764f612019-11-11 14:36:20 +0000422 Handle<mirror::MethodType> nominal_type,
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100423 Thread* self,
424 ShadowFrame& shadow_frame,
Orion Hodson960d4f72017-11-10 15:32:38 +0000425 const InstructionOperands* const operands,
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100426 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000427 // Compute method information.
David Sehr0225f8e2018-01-31 08:52:24 +0000428 CodeItemDataAccessor accessor(called_method->DexInstructionData());
Orion Hodson811bd5f2016-12-07 11:35:37 +0000429
430 // Number of registers for the callee's call frame. Note that for non-exact
431 // invokes, we always derive this information from the callee method. We
432 // cannot guarantee during verification that the number of registers encoded
433 // in the invoke is equal to the number of ins for the callee. This is because
434 // some transformations (such as boxing a long -> Long or wideining an
435 // int -> long will change that number.
436 uint16_t num_regs;
437 size_t num_input_regs;
438 size_t first_dest_reg;
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800439 if (LIKELY(accessor.HasCodeItem())) {
440 num_regs = accessor.RegistersSize();
441 first_dest_reg = num_regs - accessor.InsSize();
442 num_input_regs = accessor.InsSize();
Orion Hodson811bd5f2016-12-07 11:35:37 +0000443 // Parameter registers go at the end of the shadow frame.
444 DCHECK_NE(first_dest_reg, (size_t)-1);
445 } else {
446 // No local regs for proxy and native methods.
447 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
448 num_regs = num_input_regs = GetInsForProxyOrNativeMethod(called_method);
449 first_dest_reg = 0;
450 }
451
452 // Allocate shadow frame on the stack.
453 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
454 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
455 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
456
457 // Whether this polymorphic invoke was issued by a transformer method.
458 bool is_caller_transformer = false;
459 // Thread might be suspended during PerformArgumentConversions due to the
460 // allocations performed during boxing.
461 {
462 ScopedStackedShadowFramePusher pusher(
463 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
464 if (callsite_type->IsExactMatch(target_type.Get())) {
465 // This is an exact invoke, we can take the fast path of just copying all
466 // registers without performing any argument conversions.
Orion Hodson960d4f72017-11-10 15:32:38 +0000467 CopyArgumentsFromCallerFrame(shadow_frame,
468 new_shadow_frame,
469 operands,
470 first_dest_reg);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000471 } else {
472 // This includes the case where we're entering this invoke-polymorphic
473 // from a transformer method. In that case, the callsite_type will contain
474 // a single argument of type dalvik.system.EmulatedStackFrame. In that
475 // case, we'll have to unmarshal the EmulatedStackFrame into the
476 // new_shadow_frame and perform argument conversions on it.
477 if (IsCallerTransformer(callsite_type)) {
478 is_caller_transformer = true;
479 // The emulated stack frame is the first and only argument when we're coming
480 // through from a transformer.
Orion Hodson960d4f72017-11-10 15:32:38 +0000481 size_t first_arg_register = operands->GetOperand(0);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000482 ObjPtr<mirror::EmulatedStackFrame> emulated_stack_frame(
Vladimir Markod7e9bbf2019-03-28 13:18:57 +0000483 ObjPtr<mirror::EmulatedStackFrame>::DownCast(
484 shadow_frame.GetVRegReference(first_arg_register)));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000485 if (!emulated_stack_frame->WriteToShadowFrame(self,
486 target_type,
487 first_dest_reg,
488 new_shadow_frame)) {
489 DCHECK(self->IsExceptionPending());
Yi Kong4b22b342018-08-02 14:43:21 -0700490 result->SetL(nullptr);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000491 return false;
492 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000493 } else {
494 if (!callsite_type->IsConvertible(target_type.Get())) {
495 ThrowWrongMethodTypeException(target_type.Get(), callsite_type.Get());
496 return false;
497 }
Orion Hodson960d4f72017-11-10 15:32:38 +0000498 if (!ConvertAndCopyArgumentsFromCallerFrame(self,
499 callsite_type,
500 target_type,
501 shadow_frame,
502 first_dest_reg,
503 operands,
504 new_shadow_frame)) {
Orion Hodsona1be7132017-03-21 10:04:12 +0000505 DCHECK(self->IsExceptionPending());
Yi Kong4b22b342018-08-02 14:43:21 -0700506 result->SetL(nullptr);
Orion Hodsona1be7132017-03-21 10:04:12 +0000507 return false;
508 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000509 }
510 }
511 }
512
Jeff Hao5ea84132017-05-05 16:59:29 -0700513 bool use_interpreter_entrypoint = ClassLinker::ShouldUseInterpreterEntrypoint(
514 called_method, called_method->GetEntryPointFromQuickCompiledCode());
515 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800516 accessor,
Jeff Hao5ea84132017-05-05 16:59:29 -0700517 shadow_frame.GetMethod(),
518 first_dest_reg,
519 new_shadow_frame,
520 result,
521 use_interpreter_entrypoint);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000522 if (self->IsExceptionPending()) {
523 return false;
524 }
525
526 // If the caller of this signature polymorphic method was a transformer,
527 // we need to copy the result back out to the emulated stack frame.
528 if (is_caller_transformer) {
529 StackHandleScope<2> hs(self);
Orion Hodson960d4f72017-11-10 15:32:38 +0000530 size_t first_callee_register = operands->GetOperand(0);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000531 Handle<mirror::EmulatedStackFrame> emulated_stack_frame(
Vladimir Markod7e9bbf2019-03-28 13:18:57 +0000532 hs.NewHandle(ObjPtr<mirror::EmulatedStackFrame>::DownCast(
533 shadow_frame.GetVRegReference(first_callee_register))));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000534 Handle<mirror::MethodType> emulated_stack_type(hs.NewHandle(emulated_stack_frame->GetType()));
535 JValue local_result;
536 local_result.SetJ(result->GetJ());
537
538 if (ConvertReturnValue(emulated_stack_type, target_type, &local_result)) {
539 emulated_stack_frame->SetReturnValue(self, local_result);
540 return true;
Orion Hodson811bd5f2016-12-07 11:35:37 +0000541 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000542
543 DCHECK(self->IsExceptionPending());
544 return false;
Orion Hodson811bd5f2016-12-07 11:35:37 +0000545 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000546
Orion Hodson2764f612019-11-11 14:36:20 +0000547 if (nominal_type != nullptr) {
548 return ConvertReturnValue(nominal_type, target_type, result) &&
549 ConvertReturnValue(callsite_type, nominal_type, result);
550 }
551
Orion Hodsona1be7132017-03-21 10:04:12 +0000552 return ConvertReturnValue(callsite_type, target_type, result);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000553}
554
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100555static inline bool MethodHandleInvokeTransform(ArtMethod* called_method,
556 Handle<mirror::MethodType> callsite_type,
557 Handle<mirror::MethodType> callee_type,
558 Thread* self,
559 ShadowFrame& shadow_frame,
560 Handle<mirror::MethodHandle> receiver,
Orion Hodson960d4f72017-11-10 15:32:38 +0000561 const InstructionOperands* const operands,
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100562 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000563 REQUIRES_SHARED(Locks::mutator_lock_) {
564 // This can be fixed to two, because the method we're calling here
565 // (MethodHandle.transformInternal) doesn't have any locals and the signature
566 // is known :
567 //
568 // private MethodHandle.transformInternal(EmulatedStackFrame sf);
569 //
570 // This means we need only two vregs :
571 // - One for the receiver object.
572 // - One for the only method argument (an EmulatedStackFrame).
573 static constexpr size_t kNumRegsForTransform = 2;
574
David Sehr0225f8e2018-01-31 08:52:24 +0000575 CodeItemDataAccessor accessor(called_method->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800576 DCHECK_EQ(kNumRegsForTransform, accessor.RegistersSize());
577 DCHECK_EQ(kNumRegsForTransform, accessor.InsSize());
Orion Hodson811bd5f2016-12-07 11:35:37 +0000578
579 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
580 CREATE_SHADOW_FRAME(kNumRegsForTransform, &shadow_frame, called_method, /* dex pc */ 0);
581 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
582
583 StackHandleScope<1> hs(self);
584 MutableHandle<mirror::EmulatedStackFrame> sf(hs.NewHandle<mirror::EmulatedStackFrame>(nullptr));
585 if (IsCallerTransformer(callsite_type)) {
586 // If we're entering this transformer from another transformer, we can pass
587 // through the handle directly to the callee, instead of having to
588 // instantiate a new stack frame based on the shadow frame.
Orion Hodson960d4f72017-11-10 15:32:38 +0000589 size_t first_callee_register = operands->GetOperand(0);
Vladimir Markod7e9bbf2019-03-28 13:18:57 +0000590 sf.Assign(ObjPtr<mirror::EmulatedStackFrame>::DownCast(
591 shadow_frame.GetVRegReference(first_callee_register)));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000592 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000593 sf.Assign(mirror::EmulatedStackFrame::CreateFromShadowFrameAndArgs(self,
594 callsite_type,
595 callee_type,
596 shadow_frame,
597 operands));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000598
599 // Something went wrong while creating the emulated stack frame, we should
600 // throw the pending exception.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800601 if (sf == nullptr) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000602 DCHECK(self->IsExceptionPending());
603 return false;
604 }
605 }
606
607 new_shadow_frame->SetVRegReference(0, receiver.Get());
608 new_shadow_frame->SetVRegReference(1, sf.Get());
609
Jeff Hao5ea84132017-05-05 16:59:29 -0700610 bool use_interpreter_entrypoint = ClassLinker::ShouldUseInterpreterEntrypoint(
611 called_method, called_method->GetEntryPointFromQuickCompiledCode());
Orion Hodson811bd5f2016-12-07 11:35:37 +0000612 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800613 accessor,
Orion Hodson811bd5f2016-12-07 11:35:37 +0000614 shadow_frame.GetMethod(),
615 0 /* first destination register */,
616 new_shadow_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -0700617 result,
618 use_interpreter_entrypoint);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000619 if (self->IsExceptionPending()) {
620 return false;
621 }
622
623 // If the called transformer method we called has returned a value, then we
624 // need to copy it back to |result|.
625 sf->GetReturnValue(self, result);
626 return ConvertReturnValue(callsite_type, callee_type, result);
627}
628
629inline static ObjPtr<mirror::Class> GetAndInitializeDeclaringClass(Thread* self, ArtField* field)
630 REQUIRES_SHARED(Locks::mutator_lock_) {
631 // Method handle invocations on static fields should ensure class is
632 // initialized. This usually happens when an instance is constructed
633 // or class members referenced, but this is not guaranteed when
634 // looking up method handles.
635 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
636 if (UNLIKELY(!klass->IsInitialized())) {
637 StackHandleScope<1> hs(self);
638 HandleWrapperObjPtr<mirror::Class> h(hs.NewHandleWrapper(&klass));
639 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h, true, true)) {
640 DCHECK(self->IsExceptionPending());
641 return nullptr;
642 }
643 }
644 return klass;
645}
646
Orion Hodsona1be7132017-03-21 10:04:12 +0000647ArtMethod* RefineTargetMethod(Thread* self,
648 ShadowFrame& shadow_frame,
649 const mirror::MethodHandle::Kind& handle_kind,
650 Handle<mirror::MethodType> handle_type,
651 Handle<mirror::MethodType> callsite_type,
652 const uint32_t receiver_reg,
653 ArtMethod* target_method)
654 REQUIRES_SHARED(Locks::mutator_lock_) {
655 if (handle_kind == mirror::MethodHandle::Kind::kInvokeVirtual ||
656 handle_kind == mirror::MethodHandle::Kind::kInvokeInterface) {
657 // For virtual and interface methods ensure target_method points to
658 // the actual method to invoke.
659 ObjPtr<mirror::Object> receiver(shadow_frame.GetVRegReference(receiver_reg));
660 if (IsCallerTransformer(callsite_type)) {
661 // The current receiver is an emulated stack frame, the method's
662 // receiver needs to be fetched from there as the emulated frame
663 // will be unpacked into a new frame.
664 receiver = ObjPtr<mirror::EmulatedStackFrame>::DownCast(receiver)->GetReceiver();
665 }
666
667 ObjPtr<mirror::Class> declaring_class(target_method->GetDeclaringClass());
668 if (receiver == nullptr || receiver->GetClass() != declaring_class) {
669 // Verify that _vRegC is an object reference and of the type expected by
670 // the receiver.
671 if (!VerifyObjectIsClass(receiver, declaring_class)) {
672 DCHECK(self->IsExceptionPending());
673 return nullptr;
674 }
675 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(
676 target_method, kRuntimePointerSize);
677 }
678 } else if (handle_kind == mirror::MethodHandle::Kind::kInvokeDirect) {
679 // String constructors are a special case, they are replaced with
680 // StringFactory methods.
681 if (target_method->IsConstructor() && target_method->GetDeclaringClass()->IsStringClass()) {
682 DCHECK(handle_type->GetRType()->IsStringClass());
683 return WellKnownClasses::StringInitToStringFactory(target_method);
684 }
685 } else if (handle_kind == mirror::MethodHandle::Kind::kInvokeSuper) {
Orion Hodsona1be7132017-03-21 10:04:12 +0000686 // Note that we're not dynamically dispatching on the type of the receiver
687 // here. We use the static type of the "receiver" object that we've
688 // recorded in the method handle's type, which will be the same as the
689 // special caller that was specified at the point of lookup.
690 ObjPtr<mirror::Class> referrer_class = handle_type->GetPTypes()->Get(0);
Orion Hodson0f595742018-04-10 14:49:28 +0100691 ObjPtr<mirror::Class> declaring_class = target_method->GetDeclaringClass();
692 if (referrer_class == declaring_class) {
693 return target_method;
694 }
Alex Light49df7152019-10-03 11:22:35 -0700695 if (declaring_class->IsInterface()) {
696 if (target_method->IsAbstract()) {
697 std::string msg =
698 "Method " + target_method->PrettyMethod() + " is abstract interface method!";
699 ThrowIllegalAccessException(msg.c_str());
700 return nullptr;
701 }
702 } else {
Orion Hodsona1be7132017-03-21 10:04:12 +0000703 ObjPtr<mirror::Class> super_class = referrer_class->GetSuperClass();
704 uint16_t vtable_index = target_method->GetMethodIndex();
705 DCHECK(super_class != nullptr);
706 DCHECK(super_class->HasVTable());
707 // Note that super_class is a super of referrer_class and target_method
708 // will always be declared by super_class (or one of its super classes).
709 DCHECK_LT(vtable_index, super_class->GetVTableLength());
710 return super_class->GetVTableEntry(vtable_index, kRuntimePointerSize);
Orion Hodsona1be7132017-03-21 10:04:12 +0000711 }
712 }
713 return target_method;
714}
715
Orion Hodsona1be7132017-03-21 10:04:12 +0000716bool DoInvokePolymorphicMethod(Thread* self,
717 ShadowFrame& shadow_frame,
718 Handle<mirror::MethodHandle> method_handle,
719 Handle<mirror::MethodType> callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +0000720 const InstructionOperands* const operands,
Orion Hodsona1be7132017-03-21 10:04:12 +0000721 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000722 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson2764f612019-11-11 14:36:20 +0000723 StackHandleScope<2> hs(self);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000724 Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType()));
Orion Hodson2764f612019-11-11 14:36:20 +0000725 Handle<mirror::MethodType> nominal_handle_type(hs.NewHandle(method_handle->GetNominalType()));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000726 const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind();
Orion Hodsona1be7132017-03-21 10:04:12 +0000727 DCHECK(IsInvoke(handle_kind));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000728
Orion Hodsona1be7132017-03-21 10:04:12 +0000729 // Get the method we're actually invoking along with the kind of
730 // invoke that is desired. We don't need to perform access checks at this
731 // point because they would have been performed on our behalf at the point
732 // of creation of the method handle.
733 ArtMethod* target_method = method_handle->GetTargetMethod();
Orion Hodson960d4f72017-11-10 15:32:38 +0000734 uint32_t receiver_reg = (operands->GetNumberOfOperands() > 0) ? operands->GetOperand(0) : 0u;
Orion Hodsona1be7132017-03-21 10:04:12 +0000735 ArtMethod* called_method = RefineTargetMethod(self,
736 shadow_frame,
737 handle_kind,
738 handle_type,
739 callsite_type,
740 receiver_reg,
741 target_method);
742 if (called_method == nullptr) {
743 DCHECK(self->IsExceptionPending());
744 return false;
745 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000746
Orion Hodsona1be7132017-03-21 10:04:12 +0000747 if (IsInvokeTransform(handle_kind)) {
748 // There are two cases here - method handles representing regular
749 // transforms and those representing call site transforms. Method
750 // handles for call site transforms adapt their MethodType to match
751 // the call site. For these, the |callee_type| is the same as the
752 // |callsite_type|. The VarargsCollector is such a tranform, its
753 // method type depends on the call site, ie. x(a) or x(a, b), or
754 // x(a, b, c). The VarargsCollector invokes a variable arity method
755 // with the arity arguments in an array.
756 Handle<mirror::MethodType> callee_type =
757 (handle_kind == mirror::MethodHandle::Kind::kInvokeCallSiteTransform) ? callsite_type
758 : handle_type;
Orion Hodson960d4f72017-11-10 15:32:38 +0000759 return MethodHandleInvokeTransform(called_method,
760 callsite_type,
761 callee_type,
762 self,
763 shadow_frame,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700764 /* receiver= */ method_handle,
Orion Hodson960d4f72017-11-10 15:32:38 +0000765 operands,
766 result);
Orion Hodsona1be7132017-03-21 10:04:12 +0000767 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000768 return MethodHandleInvokeMethod(called_method,
769 callsite_type,
770 handle_type,
Orion Hodson2764f612019-11-11 14:36:20 +0000771 nominal_handle_type,
Orion Hodson960d4f72017-11-10 15:32:38 +0000772 self,
773 shadow_frame,
774 operands,
775 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000776 }
777}
778
779// Helper for getters in invoke-polymorphic.
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100780inline static void MethodHandleFieldGet(Thread* self,
781 const ShadowFrame& shadow_frame,
782 ObjPtr<mirror::Object>& obj,
783 ArtField* field,
784 Primitive::Type field_type,
785 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000786 REQUIRES_SHARED(Locks::mutator_lock_) {
787 switch (field_type) {
788 case Primitive::kPrimBoolean:
789 DoFieldGetCommon<Primitive::kPrimBoolean>(self, shadow_frame, obj, field, result);
790 break;
791 case Primitive::kPrimByte:
792 DoFieldGetCommon<Primitive::kPrimByte>(self, shadow_frame, obj, field, result);
793 break;
794 case Primitive::kPrimChar:
795 DoFieldGetCommon<Primitive::kPrimChar>(self, shadow_frame, obj, field, result);
796 break;
797 case Primitive::kPrimShort:
798 DoFieldGetCommon<Primitive::kPrimShort>(self, shadow_frame, obj, field, result);
799 break;
800 case Primitive::kPrimInt:
801 DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result);
802 break;
803 case Primitive::kPrimLong:
804 DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result);
805 break;
806 case Primitive::kPrimFloat:
807 DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result);
808 break;
809 case Primitive::kPrimDouble:
810 DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result);
811 break;
812 case Primitive::kPrimNot:
813 DoFieldGetCommon<Primitive::kPrimNot>(self, shadow_frame, obj, field, result);
814 break;
815 case Primitive::kPrimVoid:
816 LOG(FATAL) << "Unreachable: " << field_type;
817 UNREACHABLE();
818 }
819}
820
821// Helper for setters in invoke-polymorphic.
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100822inline bool MethodHandleFieldPut(Thread* self,
823 ShadowFrame& shadow_frame,
824 ObjPtr<mirror::Object>& obj,
825 ArtField* field,
826 Primitive::Type field_type,
827 JValue& value)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000828 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodsonc069a302017-01-18 09:23:12 +0000829 DCHECK(!Runtime::Current()->IsActiveTransaction());
830 static const bool kTransaction = false; // Not in a transaction.
831 static const bool kAssignabilityCheck = false; // No access check.
Orion Hodson811bd5f2016-12-07 11:35:37 +0000832 switch (field_type) {
833 case Primitive::kPrimBoolean:
Orion Hodsonc069a302017-01-18 09:23:12 +0000834 return
835 DoFieldPutCommon<Primitive::kPrimBoolean, kAssignabilityCheck, kTransaction>(
836 self, shadow_frame, obj, field, value);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000837 case Primitive::kPrimByte:
Orion Hodsonc069a302017-01-18 09:23:12 +0000838 return DoFieldPutCommon<Primitive::kPrimByte, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000839 self, shadow_frame, obj, field, value);
840 case Primitive::kPrimChar:
Orion Hodsonc069a302017-01-18 09:23:12 +0000841 return DoFieldPutCommon<Primitive::kPrimChar, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000842 self, shadow_frame, obj, field, value);
843 case Primitive::kPrimShort:
Orion Hodsonc069a302017-01-18 09:23:12 +0000844 return DoFieldPutCommon<Primitive::kPrimShort, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000845 self, shadow_frame, obj, field, value);
846 case Primitive::kPrimInt:
847 case Primitive::kPrimFloat:
Orion Hodsonc069a302017-01-18 09:23:12 +0000848 return DoFieldPutCommon<Primitive::kPrimInt, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000849 self, shadow_frame, obj, field, value);
850 case Primitive::kPrimLong:
851 case Primitive::kPrimDouble:
Orion Hodsonc069a302017-01-18 09:23:12 +0000852 return DoFieldPutCommon<Primitive::kPrimLong, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000853 self, shadow_frame, obj, field, value);
854 case Primitive::kPrimNot:
Orion Hodsonc069a302017-01-18 09:23:12 +0000855 return DoFieldPutCommon<Primitive::kPrimNot, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000856 self, shadow_frame, obj, field, value);
857 case Primitive::kPrimVoid:
858 LOG(FATAL) << "Unreachable: " << field_type;
859 UNREACHABLE();
860 }
861}
862
863static JValue GetValueFromShadowFrame(const ShadowFrame& shadow_frame,
864 Primitive::Type field_type,
865 uint32_t vreg)
866 REQUIRES_SHARED(Locks::mutator_lock_) {
867 JValue field_value;
868 switch (field_type) {
869 case Primitive::kPrimBoolean:
870 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
871 break;
872 case Primitive::kPrimByte:
873 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
874 break;
875 case Primitive::kPrimChar:
876 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
877 break;
878 case Primitive::kPrimShort:
879 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
880 break;
881 case Primitive::kPrimInt:
882 case Primitive::kPrimFloat:
883 field_value.SetI(shadow_frame.GetVReg(vreg));
884 break;
885 case Primitive::kPrimLong:
886 case Primitive::kPrimDouble:
887 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
888 break;
889 case Primitive::kPrimNot:
890 field_value.SetL(shadow_frame.GetVRegReference(vreg));
891 break;
892 case Primitive::kPrimVoid:
893 LOG(FATAL) << "Unreachable: " << field_type;
894 UNREACHABLE();
895 }
896 return field_value;
897}
898
Orion Hodson960d4f72017-11-10 15:32:38 +0000899template <bool do_conversions>
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100900bool MethodHandleFieldAccess(Thread* self,
901 ShadowFrame& shadow_frame,
902 Handle<mirror::MethodHandle> method_handle,
903 Handle<mirror::MethodType> callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +0000904 const InstructionOperands* const operands,
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100905 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000906 StackHandleScope<1> hs(self);
907 Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType()));
908 const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind();
909 ArtField* field = method_handle->GetTargetField();
910 Primitive::Type field_type = field->GetTypeAsPrimitiveType();
Orion Hodson811bd5f2016-12-07 11:35:37 +0000911 switch (handle_kind) {
912 case mirror::MethodHandle::kInstanceGet: {
Orion Hodson960d4f72017-11-10 15:32:38 +0000913 size_t obj_reg = operands->GetOperand(0);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000914 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(obj_reg);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100915 MethodHandleFieldGet(self, shadow_frame, obj, field, field_type, result);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000916 if (do_conversions && !ConvertReturnValue(callsite_type, handle_type, result)) {
917 DCHECK(self->IsExceptionPending());
918 return false;
919 }
920 return true;
921 }
922 case mirror::MethodHandle::kStaticGet: {
923 ObjPtr<mirror::Object> obj = GetAndInitializeDeclaringClass(self, field);
924 if (obj == nullptr) {
925 DCHECK(self->IsExceptionPending());
926 return false;
927 }
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100928 MethodHandleFieldGet(self, shadow_frame, obj, field, field_type, result);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000929 if (do_conversions && !ConvertReturnValue(callsite_type, handle_type, result)) {
930 DCHECK(self->IsExceptionPending());
931 return false;
932 }
933 return true;
934 }
935 case mirror::MethodHandle::kInstancePut: {
Orion Hodson960d4f72017-11-10 15:32:38 +0000936 size_t obj_reg = operands->GetOperand(0);
937 size_t value_reg = operands->GetOperand(1);
Mathieu Chartier71b17082017-04-17 20:12:29 -0700938 const size_t kPTypeIndex = 1;
939 // Use ptypes instead of field type since we may be unboxing a reference for a primitive
940 // field. The field type is incorrect for this case.
941 JValue value = GetValueFromShadowFrame(
942 shadow_frame,
943 callsite_type->GetPTypes()->Get(kPTypeIndex)->GetPrimitiveType(),
944 value_reg);
945 if (do_conversions && !ConvertArgumentValue(callsite_type,
946 handle_type,
947 kPTypeIndex,
948 &value)) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000949 DCHECK(self->IsExceptionPending());
950 return false;
951 }
952 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(obj_reg);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100953 return MethodHandleFieldPut(self, shadow_frame, obj, field, field_type, value);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000954 }
955 case mirror::MethodHandle::kStaticPut: {
956 ObjPtr<mirror::Object> obj = GetAndInitializeDeclaringClass(self, field);
957 if (obj == nullptr) {
958 DCHECK(self->IsExceptionPending());
959 return false;
960 }
Orion Hodson960d4f72017-11-10 15:32:38 +0000961 size_t value_reg = operands->GetOperand(0);
Mathieu Chartier71b17082017-04-17 20:12:29 -0700962 const size_t kPTypeIndex = 0;
963 // Use ptypes instead of field type since we may be unboxing a reference for a primitive
964 // field. The field type is incorrect for this case.
965 JValue value = GetValueFromShadowFrame(
966 shadow_frame,
967 callsite_type->GetPTypes()->Get(kPTypeIndex)->GetPrimitiveType(),
968 value_reg);
969 if (do_conversions && !ConvertArgumentValue(callsite_type,
970 handle_type,
971 kPTypeIndex,
972 &value)) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000973 DCHECK(self->IsExceptionPending());
974 return false;
975 }
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100976 return MethodHandleFieldPut(self, shadow_frame, obj, field, field_type, value);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000977 }
978 default:
979 LOG(FATAL) << "Unreachable: " << handle_kind;
980 UNREACHABLE();
981 }
982}
983
Orion Hodsonb8b93872018-01-30 07:51:10 +0000984bool DoVarHandleInvokeTranslationUnchecked(Thread* self,
985 ShadowFrame& shadow_frame,
986 mirror::VarHandle::AccessMode access_mode,
987 Handle<mirror::VarHandle> vh,
988 Handle<mirror::MethodType> vh_type,
989 Handle<mirror::MethodType> callsite_type,
990 const InstructionOperands* const operands,
991 JValue* result)
992 REQUIRES_SHARED(Locks::mutator_lock_) {
993 DCHECK_EQ(operands->GetNumberOfOperands(), static_cast<uint32_t>(vh_type->GetNumberOfPTypes()));
994 DCHECK_EQ(operands->GetNumberOfOperands(),
995 static_cast<uint32_t>(callsite_type->GetNumberOfPTypes()));
996 const size_t vreg_count = vh_type->NumberOfVRegs();
997 ShadowFrameAllocaUniquePtr accessor_frame =
998 CREATE_SHADOW_FRAME(vreg_count, nullptr, shadow_frame.GetMethod(), shadow_frame.GetDexPC());
999 ShadowFrameGetter getter(shadow_frame, operands);
1000 static const uint32_t kFirstAccessorReg = 0;
1001 ShadowFrameSetter setter(accessor_frame.get(), kFirstAccessorReg);
1002 if (!PerformConversions(self, callsite_type, vh_type, &getter, &setter)) {
1003 return false;
1004 }
1005 RangeInstructionOperands accessor_operands(kFirstAccessorReg, kFirstAccessorReg + vreg_count);
1006 if (!vh->Access(access_mode, accessor_frame.get(), &accessor_operands, result)) {
1007 return false;
1008 }
1009 return ConvertReturnValue(callsite_type, vh_type, result);
1010}
1011
1012bool DoVarHandleInvokeTranslation(Thread* self,
1013 ShadowFrame& shadow_frame,
1014 bool invokeExact,
1015 Handle<mirror::MethodHandle> method_handle,
1016 Handle<mirror::MethodType> callsite_type,
1017 const InstructionOperands* const operands,
1018 JValue* result)
1019 REQUIRES_SHARED(Locks::mutator_lock_) {
1020 if (!invokeExact) {
1021 // Exact invokes are checked for compatability higher up. The
1022 // non-exact invoke path doesn't have a similar check due to
1023 // transformers which have EmulatedStack frame arguments with the
1024 // actual method type associated with the frame.
1025 if (UNLIKELY(!callsite_type->IsConvertible(method_handle->GetMethodType()))) {
1026 ThrowWrongMethodTypeException(method_handle->GetMethodType(), callsite_type.Get());
1027 return false;
1028 }
1029 }
1030
1031 //
1032 // Basic checks that apply in all cases.
1033 //
1034 StackHandleScope<6> hs(self);
1035 Handle<mirror::ObjectArray<mirror::Class>>
1036 callsite_ptypes(hs.NewHandle(callsite_type->GetPTypes()));
1037 Handle<mirror::ObjectArray<mirror::Class>>
1038 mh_ptypes(hs.NewHandle(method_handle->GetMethodType()->GetPTypes()));
1039
1040 // Check that the first parameter is a VarHandle
1041 if (callsite_ptypes->GetLength() < 1 ||
1042 !mh_ptypes->Get(0)->IsAssignableFrom(callsite_ptypes->Get(0)) ||
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001043 mh_ptypes->Get(0) != GetClassRoot<mirror::VarHandle>()) {
Orion Hodsonb8b93872018-01-30 07:51:10 +00001044 ThrowWrongMethodTypeException(method_handle->GetMethodType(), callsite_type.Get());
1045 return false;
1046 }
1047
1048 // Get the receiver
Vladimir Marko4bb2af52019-03-22 11:09:19 +00001049 ObjPtr<mirror::Object> receiver = shadow_frame.GetVRegReference(operands->GetOperand(0));
Orion Hodsonb8b93872018-01-30 07:51:10 +00001050 if (receiver == nullptr) {
1051 ThrowNullPointerException("Expected argument 1 to be a non-null VarHandle");
1052 return false;
1053 }
1054
1055 // Cast to VarHandle instance
Vladimir Marko4bb2af52019-03-22 11:09:19 +00001056 Handle<mirror::VarHandle> vh(hs.NewHandle(ObjPtr<mirror::VarHandle>::DownCast(receiver)));
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001057 DCHECK(GetClassRoot<mirror::VarHandle>()->IsAssignableFrom(vh->GetClass()));
Orion Hodsonb8b93872018-01-30 07:51:10 +00001058
1059 // Determine the accessor kind to dispatch
1060 ArtMethod* target_method = method_handle->GetTargetMethod();
1061 int intrinsic_index = target_method->GetIntrinsic();
1062 mirror::VarHandle::AccessMode access_mode =
1063 mirror::VarHandle::GetAccessModeByIntrinsic(static_cast<Intrinsics>(intrinsic_index));
1064 Handle<mirror::MethodType> vh_type =
1065 hs.NewHandle(vh->GetMethodTypeForAccessMode(self, access_mode));
1066 Handle<mirror::MethodType> mh_invoke_type = hs.NewHandle(
1067 mirror::MethodType::CloneWithoutLeadingParameter(self, method_handle->GetMethodType()));
1068 if (method_handle->GetHandleKind() == mirror::MethodHandle::Kind::kInvokeVarHandleExact) {
1069 if (!mh_invoke_type->IsExactMatch(vh_type.Get())) {
1070 ThrowWrongMethodTypeException(vh_type.Get(), mh_invoke_type.Get());
1071 return false;
1072 }
1073 } else {
1074 DCHECK_EQ(method_handle->GetHandleKind(), mirror::MethodHandle::Kind::kInvokeVarHandle);
1075 if (!mh_invoke_type->IsConvertible(vh_type.Get())) {
1076 ThrowWrongMethodTypeException(vh_type.Get(), mh_invoke_type.Get());
1077 return false;
1078 }
1079 }
1080
1081 Handle<mirror::MethodType> callsite_type_without_varhandle =
1082 hs.NewHandle(mirror::MethodType::CloneWithoutLeadingParameter(self, callsite_type.Get()));
1083 NoReceiverInstructionOperands varhandle_operands(operands);
1084 DCHECK_EQ(static_cast<int32_t>(varhandle_operands.GetNumberOfOperands()),
1085 callsite_type_without_varhandle->GetPTypes()->GetLength());
1086 return DoVarHandleInvokeTranslationUnchecked(self,
1087 shadow_frame,
1088 access_mode,
1089 vh,
1090 vh_type,
1091 callsite_type_without_varhandle,
1092 &varhandle_operands,
1093 result);
1094}
1095
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001096static inline bool MethodHandleInvokeInternal(Thread* self,
1097 ShadowFrame& shadow_frame,
1098 Handle<mirror::MethodHandle> method_handle,
1099 Handle<mirror::MethodType> callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +00001100 const InstructionOperands* const operands,
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001101 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +00001102 REQUIRES_SHARED(Locks::mutator_lock_) {
1103 const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind();
Orion Hodsona1be7132017-03-21 10:04:12 +00001104 if (IsFieldAccess(handle_kind)) {
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001105 ObjPtr<mirror::MethodType> handle_type(method_handle->GetMethodType());
Orion Hodsona1be7132017-03-21 10:04:12 +00001106 DCHECK(!callsite_type->IsExactMatch(handle_type.Ptr()));
1107 if (!callsite_type->IsConvertible(handle_type.Ptr())) {
Orion Hodson811bd5f2016-12-07 11:35:37 +00001108 ThrowWrongMethodTypeException(handle_type.Ptr(), callsite_type.Get());
1109 return false;
1110 }
Orion Hodsona1be7132017-03-21 10:04:12 +00001111 const bool do_convert = true;
Orion Hodson960d4f72017-11-10 15:32:38 +00001112 return MethodHandleFieldAccess<do_convert>(
Orion Hodsona1be7132017-03-21 10:04:12 +00001113 self,
1114 shadow_frame,
1115 method_handle,
1116 callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +00001117 operands,
Orion Hodsona1be7132017-03-21 10:04:12 +00001118 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +00001119 }
Orion Hodsonb8b93872018-01-30 07:51:10 +00001120 if (IsInvokeVarHandle(handle_kind)) {
1121 return DoVarHandleInvokeTranslation(self,
1122 shadow_frame,
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001123 /*invokeExact=*/ false,
Orion Hodsonb8b93872018-01-30 07:51:10 +00001124 method_handle,
1125 callsite_type,
1126 operands,
1127 result);
1128 }
Orion Hodson960d4f72017-11-10 15:32:38 +00001129 return DoInvokePolymorphicMethod(self,
1130 shadow_frame,
1131 method_handle,
1132 callsite_type,
1133 operands,
1134 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +00001135}
1136
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001137static inline bool MethodHandleInvokeExactInternal(
1138 Thread* self,
1139 ShadowFrame& shadow_frame,
1140 Handle<mirror::MethodHandle> method_handle,
1141 Handle<mirror::MethodType> callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +00001142 const InstructionOperands* const operands,
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001143 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +00001144 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodsona1be7132017-03-21 10:04:12 +00001145 StackHandleScope<1> hs(self);
Orion Hodsona1be7132017-03-21 10:04:12 +00001146 Handle<mirror::MethodType> method_handle_type(hs.NewHandle(method_handle->GetMethodType()));
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001147 if (!callsite_type->IsExactMatch(method_handle_type.Get())) {
1148 ThrowWrongMethodTypeException(method_handle_type.Get(), callsite_type.Get());
1149 return false;
1150 }
1151
1152 const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind();
Orion Hodson811bd5f2016-12-07 11:35:37 +00001153 if (IsFieldAccess(handle_kind)) {
1154 const bool do_convert = false;
Orion Hodson960d4f72017-11-10 15:32:38 +00001155 return MethodHandleFieldAccess<do_convert>(self,
1156 shadow_frame,
1157 method_handle,
1158 callsite_type,
1159 operands,
1160 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +00001161 }
1162
Orion Hodsona1be7132017-03-21 10:04:12 +00001163 // Slow-path check.
Orion Hodsonb8b93872018-01-30 07:51:10 +00001164 if (IsInvokeTransform(handle_kind) ||
1165 IsCallerTransformer(callsite_type)) {
Orion Hodson960d4f72017-11-10 15:32:38 +00001166 return DoInvokePolymorphicMethod(self,
1167 shadow_frame,
1168 method_handle,
1169 callsite_type,
1170 operands,
1171 result);
Orion Hodsonb8b93872018-01-30 07:51:10 +00001172 } else if (IsInvokeVarHandle(handle_kind)) {
1173 return DoVarHandleInvokeTranslation(self,
1174 shadow_frame,
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001175 /*invokeExact=*/ true,
Orion Hodsonb8b93872018-01-30 07:51:10 +00001176 method_handle,
1177 callsite_type,
1178 operands,
1179 result);
Orion Hodsona1be7132017-03-21 10:04:12 +00001180 }
1181
1182 // On the fast-path. This is equivalent to DoCallPolymoprhic without the conversion paths.
1183 ArtMethod* target_method = method_handle->GetTargetMethod();
Orion Hodson960d4f72017-11-10 15:32:38 +00001184 uint32_t receiver_reg = (operands->GetNumberOfOperands() > 0) ? operands->GetOperand(0) : 0u;
Orion Hodsona1be7132017-03-21 10:04:12 +00001185 ArtMethod* called_method = RefineTargetMethod(self,
Orion Hodson811bd5f2016-12-07 11:35:37 +00001186 shadow_frame,
Orion Hodsona1be7132017-03-21 10:04:12 +00001187 handle_kind,
1188 method_handle_type,
Orion Hodson811bd5f2016-12-07 11:35:37 +00001189 callsite_type,
Orion Hodsona1be7132017-03-21 10:04:12 +00001190 receiver_reg,
1191 target_method);
1192 if (called_method == nullptr) {
1193 DCHECK(self->IsExceptionPending());
1194 return false;
1195 }
1196
1197 // Compute method information.
David Sehr0225f8e2018-01-31 08:52:24 +00001198 CodeItemDataAccessor accessor(called_method->DexInstructionData());
Orion Hodsona1be7132017-03-21 10:04:12 +00001199 uint16_t num_regs;
1200 size_t num_input_regs;
1201 size_t first_dest_reg;
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001202 if (LIKELY(accessor.HasCodeItem())) {
1203 num_regs = accessor.RegistersSize();
1204 first_dest_reg = num_regs - accessor.InsSize();
1205 num_input_regs = accessor.InsSize();
Orion Hodsona1be7132017-03-21 10:04:12 +00001206 // Parameter registers go at the end of the shadow frame.
1207 DCHECK_NE(first_dest_reg, (size_t)-1);
1208 } else {
1209 // No local regs for proxy and native methods.
1210 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1211 num_regs = num_input_regs = GetInsForProxyOrNativeMethod(called_method);
1212 first_dest_reg = 0;
1213 }
1214
1215 // Allocate shadow frame on the stack.
1216 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
1217 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
1218 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
1219 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Orion Hodson960d4f72017-11-10 15:32:38 +00001220 CopyArgumentsFromCallerFrame(shadow_frame,
1221 new_shadow_frame,
1222 operands,
1223 first_dest_reg);
Orion Hodsona1be7132017-03-21 10:04:12 +00001224 self->EndAssertNoThreadSuspension(old_cause);
1225
Jeff Hao5ea84132017-05-05 16:59:29 -07001226 bool use_interpreter_entrypoint = ClassLinker::ShouldUseInterpreterEntrypoint(
1227 called_method, called_method->GetEntryPointFromQuickCompiledCode());
1228 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001229 accessor,
Jeff Hao5ea84132017-05-05 16:59:29 -07001230 shadow_frame.GetMethod(),
1231 first_dest_reg,
1232 new_shadow_frame,
1233 result,
1234 use_interpreter_entrypoint);
Orion Hodsona1be7132017-03-21 10:04:12 +00001235 if (self->IsExceptionPending()) {
1236 return false;
1237 }
1238 return true;
Orion Hodson811bd5f2016-12-07 11:35:37 +00001239}
1240
1241} // namespace
1242
Orion Hodson960d4f72017-11-10 15:32:38 +00001243bool MethodHandleInvoke(Thread* self,
1244 ShadowFrame& shadow_frame,
1245 Handle<mirror::MethodHandle> method_handle,
1246 Handle<mirror::MethodType> callsite_type,
1247 const InstructionOperands* const operands,
1248 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +00001249 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001250 if (UNLIKELY(callsite_type->IsExactMatch(method_handle->GetMethodType()))) {
1251 // A non-exact invoke that can be invoked exactly.
Orion Hodson960d4f72017-11-10 15:32:38 +00001252 return MethodHandleInvokeExactInternal(self,
1253 shadow_frame,
1254 method_handle,
1255 callsite_type,
1256 operands,
1257 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +00001258 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +00001259 return MethodHandleInvokeInternal(self,
1260 shadow_frame,
1261 method_handle,
1262 callsite_type,
1263 operands,
1264 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +00001265 }
1266}
1267
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001268bool MethodHandleInvokeExact(Thread* self,
Orion Hodson960d4f72017-11-10 15:32:38 +00001269 ShadowFrame& shadow_frame,
1270 Handle<mirror::MethodHandle> method_handle,
1271 Handle<mirror::MethodType> callsite_type,
1272 const InstructionOperands* const operands,
1273 JValue* result)
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001274 REQUIRES_SHARED(Locks::mutator_lock_) {
1275 // We need to check the nominal type of the handle in addition to the
1276 // real type. The "nominal" type is present when MethodHandle.asType is
1277 // called any handle, and results in the declared type of the handle
1278 // changing.
1279 ObjPtr<mirror::MethodType> nominal_type(method_handle->GetNominalType());
1280 if (UNLIKELY(nominal_type != nullptr)) {
1281 if (UNLIKELY(!callsite_type->IsExactMatch(nominal_type.Ptr()))) {
1282 ThrowWrongMethodTypeException(nominal_type.Ptr(), callsite_type.Get());
1283 return false;
1284 }
1285 if (LIKELY(!nominal_type->IsExactMatch(method_handle->GetMethodType()))) {
1286 // Different nominal type means we have to treat as non-exact.
Orion Hodson960d4f72017-11-10 15:32:38 +00001287 return MethodHandleInvokeInternal(self,
1288 shadow_frame,
1289 method_handle,
1290 callsite_type,
1291 operands,
1292 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001293 }
1294 }
Orion Hodson960d4f72017-11-10 15:32:38 +00001295 return MethodHandleInvokeExactInternal(self,
1296 shadow_frame,
1297 method_handle,
1298 callsite_type,
1299 operands,
1300 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001301}
1302
Orion Hodsonba28f9f2016-10-26 10:56:25 +01001303} // namespace art