blob: 28df1749a4269626e6419d4a7992aa342e878783 [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 Markoc7aa87e2018-05-24 15:19:52 +010021#include "class_root.h"
Orion Hodson811bd5f2016-12-07 11:35:37 +000022#include "common_dex_operations.h"
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +010023#include "interpreter/shadow_frame-inl.h"
Orion Hodsonba28f9f2016-10-26 10:56:25 +010024#include "jvalue-inl.h"
Orion Hodson811bd5f2016-12-07 11:35:37 +000025#include "mirror/emulated_stack_frame.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080026#include "mirror/method_handle_impl-inl.h"
Orion Hodson811bd5f2016-12-07 11:35:37 +000027#include "mirror/method_type.h"
Orion Hodsonb8b93872018-01-30 07:51:10 +000028#include "mirror/var_handle.h"
Orion Hodsonba28f9f2016-10-26 10:56:25 +010029#include "reflection-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070030#include "reflection.h"
Orion Hodson1a06f9f2016-11-09 08:32:42 +000031#include "well_known_classes.h"
Orion Hodsonba28f9f2016-10-26 10:56:25 +010032
33namespace art {
34
Andreas Gampe46ee31b2016-12-14 10:11:49 -080035using android::base::StringPrintf;
36
Orion Hodsonba28f9f2016-10-26 10:56:25 +010037namespace {
38
Orion Hodson1a06f9f2016-11-09 08:32:42 +000039#define PRIMITIVES_LIST(V) \
40 V(Primitive::kPrimBoolean, Boolean, Boolean, Z) \
41 V(Primitive::kPrimByte, Byte, Byte, B) \
42 V(Primitive::kPrimChar, Char, Character, C) \
43 V(Primitive::kPrimShort, Short, Short, S) \
44 V(Primitive::kPrimInt, Int, Integer, I) \
45 V(Primitive::kPrimLong, Long, Long, J) \
46 V(Primitive::kPrimFloat, Float, Float, F) \
47 V(Primitive::kPrimDouble, Double, Double, D)
Orion Hodsonba28f9f2016-10-26 10:56:25 +010048
49// Assigns |type| to the primitive type associated with |klass|. Returns
50// true iff. |klass| was a boxed type (Integer, Long etc.), false otherwise.
51bool GetUnboxedPrimitiveType(ObjPtr<mirror::Class> klass, Primitive::Type* type)
52 REQUIRES_SHARED(Locks::mutator_lock_) {
53 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Orion Hodsona1be7132017-03-21 10:04:12 +000054 std::string storage;
55 const char* descriptor = klass->GetDescriptor(&storage);
56 static const char kJavaLangPrefix[] = "Ljava/lang/";
57 static const size_t kJavaLangPrefixSize = sizeof(kJavaLangPrefix) - 1;
58 if (strncmp(descriptor, kJavaLangPrefix, kJavaLangPrefixSize) != 0) {
59 return false;
60 }
61
62 descriptor += kJavaLangPrefixSize;
63#define LOOKUP_PRIMITIVE(primitive, _, java_name, ___) \
64 if (strcmp(descriptor, #java_name ";") == 0) { \
65 *type = primitive; \
66 return true; \
Orion Hodsonba28f9f2016-10-26 10:56:25 +010067 }
Orion Hodsonba28f9f2016-10-26 10:56:25 +010068
Orion Hodson1a06f9f2016-11-09 08:32:42 +000069 PRIMITIVES_LIST(LOOKUP_PRIMITIVE);
70#undef LOOKUP_PRIMITIVE
Orion Hodsonba28f9f2016-10-26 10:56:25 +010071 return false;
72}
73
Orion Hodson1a06f9f2016-11-09 08:32:42 +000074ObjPtr<mirror::Class> GetBoxedPrimitiveClass(Primitive::Type type)
Orion Hodsonba28f9f2016-10-26 10:56:25 +010075 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson1a06f9f2016-11-09 08:32:42 +000076 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
77 jmethodID m = nullptr;
78 switch (type) {
79#define CASE_PRIMITIVE(primitive, _, java_name, __) \
80 case primitive: \
81 m = WellKnownClasses::java_lang_ ## java_name ## _valueOf; \
82 break;
83 PRIMITIVES_LIST(CASE_PRIMITIVE);
84#undef CASE_PRIMITIVE
85 case Primitive::Type::kPrimNot:
86 case Primitive::Type::kPrimVoid:
87 return nullptr;
Orion Hodsonba28f9f2016-10-26 10:56:25 +010088 }
Orion Hodson1a06f9f2016-11-09 08:32:42 +000089 return jni::DecodeArtMethod(m)->GetDeclaringClass();
90}
Orion Hodsonba28f9f2016-10-26 10:56:25 +010091
Orion Hodson1a06f9f2016-11-09 08:32:42 +000092bool GetUnboxedTypeAndValue(ObjPtr<mirror::Object> o, Primitive::Type* type, JValue* value)
93 REQUIRES_SHARED(Locks::mutator_lock_) {
94 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Orion Hodsonba28f9f2016-10-26 10:56:25 +010095 ObjPtr<mirror::Class> klass = o->GetClass();
Orion Hodsonba28f9f2016-10-26 10:56:25 +010096 ArtField* primitive_field = &klass->GetIFieldsPtr()->At(0);
Orion Hodson1a06f9f2016-11-09 08:32:42 +000097#define CASE_PRIMITIVE(primitive, abbrev, _, shorthand) \
98 if (klass == GetBoxedPrimitiveClass(primitive)) { \
99 *type = primitive; \
100 value->Set ## shorthand(primitive_field->Get ## abbrev(o)); \
101 return true; \
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100102 }
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000103 PRIMITIVES_LIST(CASE_PRIMITIVE)
104#undef CASE_PRIMITIVE
105 return false;
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100106}
107
108inline bool IsReferenceType(Primitive::Type type) {
109 return type == Primitive::kPrimNot;
110}
111
112inline bool IsPrimitiveType(Primitive::Type type) {
113 return !IsReferenceType(type);
114}
115
116} // namespace
117
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000118bool IsParameterTypeConvertible(ObjPtr<mirror::Class> from, ObjPtr<mirror::Class> to)
119 REQUIRES_SHARED(Locks::mutator_lock_) {
120 // This function returns true if there's any conceivable conversion
121 // between |from| and |to|. It's expected this method will be used
122 // to determine if a WrongMethodTypeException should be raised. The
123 // decision logic follows the documentation for MethodType.asType().
124 if (from == to) {
125 return true;
126 }
127
128 Primitive::Type from_primitive = from->GetPrimitiveType();
129 Primitive::Type to_primitive = to->GetPrimitiveType();
130 DCHECK(from_primitive != Primitive::Type::kPrimVoid);
131 DCHECK(to_primitive != Primitive::Type::kPrimVoid);
132
133 // If |to| and |from| are references.
134 if (IsReferenceType(from_primitive) && IsReferenceType(to_primitive)) {
135 // Assignability is determined during parameter conversion when
136 // invoking the associated method handle.
137 return true;
138 }
139
140 // If |to| and |from| are primitives and a widening conversion exists.
141 if (Primitive::IsWidenable(from_primitive, to_primitive)) {
142 return true;
143 }
144
145 // If |to| is a reference and |from| is a primitive, then boxing conversion.
146 if (IsReferenceType(to_primitive) && IsPrimitiveType(from_primitive)) {
147 return to->IsAssignableFrom(GetBoxedPrimitiveClass(from_primitive));
148 }
149
150 // If |from| is a reference and |to| is a primitive, then unboxing conversion.
151 if (IsPrimitiveType(to_primitive) && IsReferenceType(from_primitive)) {
152 if (from->DescriptorEquals("Ljava/lang/Object;")) {
153 // Object might be converted into a primitive during unboxing.
154 return true;
Orion Hodsona1be7132017-03-21 10:04:12 +0000155 }
156
157 if (Primitive::IsNumericType(to_primitive) && from->DescriptorEquals("Ljava/lang/Number;")) {
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000158 // Number might be unboxed into any of the number primitive types.
159 return true;
160 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000161
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000162 Primitive::Type unboxed_type;
163 if (GetUnboxedPrimitiveType(from, &unboxed_type)) {
Orion Hodsonf1412b42016-11-11 12:03:29 +0000164 if (unboxed_type == to_primitive) {
165 // Straightforward unboxing conversion such as Boolean => boolean.
166 return true;
Orion Hodsonf1412b42016-11-11 12:03:29 +0000167 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000168
169 // Check if widening operations for numeric primitives would work,
170 // such as Byte => byte => long.
171 return Primitive::IsWidenable(unboxed_type, to_primitive);
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000172 }
173 }
174
175 return false;
176}
177
178bool IsReturnTypeConvertible(ObjPtr<mirror::Class> from, ObjPtr<mirror::Class> to)
179 REQUIRES_SHARED(Locks::mutator_lock_) {
180 if (to->GetPrimitiveType() == Primitive::Type::kPrimVoid) {
181 // Result will be ignored.
182 return true;
183 } else if (from->GetPrimitiveType() == Primitive::Type::kPrimVoid) {
184 // Returned value will be 0 / null.
185 return true;
186 } else {
187 // Otherwise apply usual parameter conversion rules.
188 return IsParameterTypeConvertible(from, to);
189 }
190}
191
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100192bool ConvertJValueCommon(
193 Handle<mirror::MethodType> callsite_type,
194 Handle<mirror::MethodType> callee_type,
195 ObjPtr<mirror::Class> from,
196 ObjPtr<mirror::Class> to,
197 JValue* value) {
198 // The reader maybe concerned about the safety of the heap object
199 // that may be in |value|. There is only one case where allocation
200 // is obviously needed and that's for boxing. However, in the case
201 // of boxing |value| contains a non-reference type.
202
203 const Primitive::Type from_type = from->GetPrimitiveType();
204 const Primitive::Type to_type = to->GetPrimitiveType();
205
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000206 // Put incoming value into |src_value| and set return value to 0.
207 // Errors and conversions from void require the return value to be 0.
208 const JValue src_value(*value);
209 value->SetJ(0);
210
211 // Conversion from void set result to zero.
212 if (from_type == Primitive::kPrimVoid) {
213 return true;
214 }
215
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100216 // This method must be called only when the types don't match.
217 DCHECK(from != to);
218
219 if (IsPrimitiveType(from_type) && IsPrimitiveType(to_type)) {
220 // The source and target types are both primitives.
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000221 if (UNLIKELY(!ConvertPrimitiveValueNoThrow(from_type, to_type, src_value, value))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100222 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100223 return false;
224 }
225 return true;
226 } else if (IsReferenceType(from_type) && IsReferenceType(to_type)) {
227 // They're both reference types. If "from" is null, we can pass it
228 // through unchanged. If not, we must generate a cast exception if
229 // |to| is not assignable from the dynamic type of |ref|.
230 //
231 // Playing it safe with StackHandleScope here, not expecting any allocation
232 // in mirror::Class::IsAssignable().
233 StackHandleScope<2> hs(Thread::Current());
234 Handle<mirror::Class> h_to(hs.NewHandle(to));
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000235 Handle<mirror::Object> h_obj(hs.NewHandle(src_value.GetL()));
Orion Hodsonc1d3bac2018-01-26 14:38:55 +0000236 if (UNLIKELY(!h_obj.IsNull() && !to->IsAssignableFrom(h_obj->GetClass()))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100237 ThrowClassCastException(h_to.Get(), h_obj->GetClass());
238 return false;
239 }
240 value->SetL(h_obj.Get());
241 return true;
242 } else if (IsReferenceType(to_type)) {
243 DCHECK(IsPrimitiveType(from_type));
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100244 // The source type is a primitive and the target type is a reference, so we must box.
245 // The target type maybe a super class of the boxed source type, for example,
246 // if the source type is int, it's boxed type is java.lang.Integer, and the target
247 // type could be java.lang.Number.
248 Primitive::Type type;
249 if (!GetUnboxedPrimitiveType(to, &type)) {
250 ObjPtr<mirror::Class> boxed_from_class = GetBoxedPrimitiveClass(from_type);
Orion Hodsonc1d3bac2018-01-26 14:38:55 +0000251 if (LIKELY(boxed_from_class->IsSubClass(to))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100252 type = from_type;
253 } else {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100254 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
255 return false;
256 }
257 }
258
259 if (UNLIKELY(from_type != type)) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100260 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
261 return false;
262 }
263
Orion Hodsonc1d3bac2018-01-26 14:38:55 +0000264 if (UNLIKELY(!ConvertPrimitiveValueNoThrow(from_type, type, src_value, value))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100265 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
266 return false;
267 }
268
269 // Then perform the actual boxing, and then set the reference.
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000270 ObjPtr<mirror::Object> boxed = BoxPrimitive(type, src_value);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100271 value->SetL(boxed.Ptr());
272 return true;
273 } else {
274 // The source type is a reference and the target type is a primitive, so we must unbox.
275 DCHECK(IsReferenceType(from_type));
276 DCHECK(IsPrimitiveType(to_type));
277
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000278 ObjPtr<mirror::Object> from_obj(src_value.GetL());
Orion Hodsonc1d3bac2018-01-26 14:38:55 +0000279 if (UNLIKELY(from_obj.IsNull())) {
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000280 ThrowNullPointerException(
281 StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
282 from->PrettyDescriptor().c_str()).c_str());
283 return false;
284 }
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100285
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000286 Primitive::Type unboxed_type;
287 JValue unboxed_value;
288 if (UNLIKELY(!GetUnboxedTypeAndValue(from_obj, &unboxed_type, &unboxed_value))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100289 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
290 return false;
291 }
292
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000293 if (UNLIKELY(!ConvertPrimitiveValueNoThrow(unboxed_type, to_type, unboxed_value, value))) {
Orion Hodsonc1d3bac2018-01-26 14:38:55 +0000294 if (from->IsAssignableFrom(GetBoxedPrimitiveClass(to_type))) {
295 // CallSite may be Number, but the Number object is
296 // incompatible, e.g. Number (Integer) for a short.
297 ThrowClassCastException(from, to);
298 } else {
299 // CallSite is incompatible, e.g. Integer for a short.
300 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
301 }
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100302 return false;
303 }
304
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000305 return true;
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100306 }
307}
308
Orion Hodson811bd5f2016-12-07 11:35:37 +0000309namespace {
310
Orion Hodson811bd5f2016-12-07 11:35:37 +0000311inline void CopyArgumentsFromCallerFrame(const ShadowFrame& caller_frame,
312 ShadowFrame* callee_frame,
Orion Hodson960d4f72017-11-10 15:32:38 +0000313 const InstructionOperands* const operands,
314 const size_t first_dst_reg)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000315 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000316 for (size_t i = 0; i < operands->GetNumberOfOperands(); ++i) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000317 size_t dst_reg = first_dst_reg + i;
Orion Hodson960d4f72017-11-10 15:32:38 +0000318 size_t src_reg = operands->GetOperand(i);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000319 // Uint required, so that sign extension does not make this wrong on 64-bit systems
320 uint32_t src_value = caller_frame.GetVReg(src_reg);
321 ObjPtr<mirror::Object> o = caller_frame.GetVRegReference<kVerifyNone>(src_reg);
322 // If both register locations contains the same value, the register probably holds a reference.
323 // Note: As an optimization, non-moving collectors leave a stale reference value
324 // in the references array even after the original vreg was overwritten to a non-reference.
325 if (src_value == reinterpret_cast<uintptr_t>(o.Ptr())) {
326 callee_frame->SetVRegReference(dst_reg, o.Ptr());
327 } else {
328 callee_frame->SetVReg(dst_reg, src_value);
329 }
330 }
331}
332
Orion Hodson811bd5f2016-12-07 11:35:37 +0000333inline bool ConvertAndCopyArgumentsFromCallerFrame(
334 Thread* self,
335 Handle<mirror::MethodType> callsite_type,
336 Handle<mirror::MethodType> callee_type,
337 const ShadowFrame& caller_frame,
Orion Hodson960d4f72017-11-10 15:32:38 +0000338 uint32_t first_dest_reg,
339 const InstructionOperands* const operands,
Orion Hodson811bd5f2016-12-07 11:35:37 +0000340 ShadowFrame* callee_frame)
341 REQUIRES_SHARED(Locks::mutator_lock_) {
342 ObjPtr<mirror::ObjectArray<mirror::Class>> from_types(callsite_type->GetPTypes());
343 ObjPtr<mirror::ObjectArray<mirror::Class>> to_types(callee_type->GetPTypes());
344
345 const int32_t num_method_params = from_types->GetLength();
346 if (to_types->GetLength() != num_method_params) {
347 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
348 return false;
349 }
350
Orion Hodson928033d2018-02-07 05:30:54 +0000351 ShadowFrameGetter getter(caller_frame, operands);
Orion Hodson960d4f72017-11-10 15:32:38 +0000352 ShadowFrameSetter setter(callee_frame, first_dest_reg);
353 return PerformConversions<ShadowFrameGetter, ShadowFrameSetter>(self,
354 callsite_type,
355 callee_type,
356 &getter,
357 &setter,
358 num_method_params);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000359}
360
Orion Hodson811bd5f2016-12-07 11:35:37 +0000361inline bool IsInvoke(const mirror::MethodHandle::Kind handle_kind) {
362 return handle_kind <= mirror::MethodHandle::Kind::kLastInvokeKind;
363}
364
365inline bool IsInvokeTransform(const mirror::MethodHandle::Kind handle_kind) {
366 return (handle_kind == mirror::MethodHandle::Kind::kInvokeTransform
367 || handle_kind == mirror::MethodHandle::Kind::kInvokeCallSiteTransform);
368}
369
Orion Hodsonb8b93872018-01-30 07:51:10 +0000370inline bool IsInvokeVarHandle(const mirror::MethodHandle::Kind handle_kind) {
371 return (handle_kind == mirror::MethodHandle::Kind::kInvokeVarHandle ||
372 handle_kind == mirror::MethodHandle::Kind::kInvokeVarHandleExact);
373}
374
Orion Hodson811bd5f2016-12-07 11:35:37 +0000375inline bool IsFieldAccess(mirror::MethodHandle::Kind handle_kind) {
376 return (handle_kind >= mirror::MethodHandle::Kind::kFirstAccessorKind
377 && handle_kind <= mirror::MethodHandle::Kind::kLastAccessorKind);
378}
379
380// Calculate the number of ins for a proxy or native method, where we
381// can't just look at the code item.
382static inline size_t GetInsForProxyOrNativeMethod(ArtMethod* method)
383 REQUIRES_SHARED(Locks::mutator_lock_) {
384 DCHECK(method->IsNative() || method->IsProxyMethod());
Orion Hodson811bd5f2016-12-07 11:35:37 +0000385 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Orion Hodsona1be7132017-03-21 10:04:12 +0000386 uint32_t shorty_length = 0;
387 const char* shorty = method->GetShorty(&shorty_length);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000388
Orion Hodsona1be7132017-03-21 10:04:12 +0000389 // Static methods do not include the receiver. The receiver isn't included
390 // in the shorty_length though the return value is.
391 size_t num_ins = method->IsStatic() ? shorty_length - 1 : shorty_length;
392 for (const char* c = shorty + 1; *c != '\0'; ++c) {
393 if (*c == 'J' || *c == 'D') {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000394 ++num_ins;
395 }
396 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000397 return num_ins;
398}
399
400// Returns true iff. the callsite type for a polymorphic invoke is transformer
401// like, i.e that it has a single input argument whose type is
402// dalvik.system.EmulatedStackFrame.
403static inline bool IsCallerTransformer(Handle<mirror::MethodType> callsite_type)
404 REQUIRES_SHARED(Locks::mutator_lock_) {
405 ObjPtr<mirror::ObjectArray<mirror::Class>> param_types(callsite_type->GetPTypes());
406 if (param_types->GetLength() == 1) {
407 ObjPtr<mirror::Class> param(param_types->GetWithoutChecks(0));
Orion Hodsona1be7132017-03-21 10:04:12 +0000408 // NB Comparing descriptor here as it appears faster in cycle simulation than using:
409 // param == WellKnownClasses::ToClass(WellKnownClasses::dalvik_system_EmulatedStackFrame)
410 // Costs are 98 vs 173 cycles per invocation.
411 return param->DescriptorEquals("Ldalvik/system/EmulatedStackFrame;");
Orion Hodson811bd5f2016-12-07 11:35:37 +0000412 }
413
414 return false;
415}
416
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100417static inline bool MethodHandleInvokeMethod(ArtMethod* called_method,
418 Handle<mirror::MethodType> callsite_type,
419 Handle<mirror::MethodType> target_type,
420 Thread* self,
421 ShadowFrame& shadow_frame,
Orion Hodson960d4f72017-11-10 15:32:38 +0000422 const InstructionOperands* const operands,
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100423 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000424 // Compute method information.
David Sehr0225f8e2018-01-31 08:52:24 +0000425 CodeItemDataAccessor accessor(called_method->DexInstructionData());
Orion Hodson811bd5f2016-12-07 11:35:37 +0000426
427 // Number of registers for the callee's call frame. Note that for non-exact
428 // invokes, we always derive this information from the callee method. We
429 // cannot guarantee during verification that the number of registers encoded
430 // in the invoke is equal to the number of ins for the callee. This is because
431 // some transformations (such as boxing a long -> Long or wideining an
432 // int -> long will change that number.
433 uint16_t num_regs;
434 size_t num_input_regs;
435 size_t first_dest_reg;
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800436 if (LIKELY(accessor.HasCodeItem())) {
437 num_regs = accessor.RegistersSize();
438 first_dest_reg = num_regs - accessor.InsSize();
439 num_input_regs = accessor.InsSize();
Orion Hodson811bd5f2016-12-07 11:35:37 +0000440 // Parameter registers go at the end of the shadow frame.
441 DCHECK_NE(first_dest_reg, (size_t)-1);
442 } else {
443 // No local regs for proxy and native methods.
444 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
445 num_regs = num_input_regs = GetInsForProxyOrNativeMethod(called_method);
446 first_dest_reg = 0;
447 }
448
449 // Allocate shadow frame on the stack.
450 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
451 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
452 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
453
454 // Whether this polymorphic invoke was issued by a transformer method.
455 bool is_caller_transformer = false;
456 // Thread might be suspended during PerformArgumentConversions due to the
457 // allocations performed during boxing.
458 {
459 ScopedStackedShadowFramePusher pusher(
460 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
461 if (callsite_type->IsExactMatch(target_type.Get())) {
462 // This is an exact invoke, we can take the fast path of just copying all
463 // registers without performing any argument conversions.
Orion Hodson960d4f72017-11-10 15:32:38 +0000464 CopyArgumentsFromCallerFrame(shadow_frame,
465 new_shadow_frame,
466 operands,
467 first_dest_reg);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000468 } else {
469 // This includes the case where we're entering this invoke-polymorphic
470 // from a transformer method. In that case, the callsite_type will contain
471 // a single argument of type dalvik.system.EmulatedStackFrame. In that
472 // case, we'll have to unmarshal the EmulatedStackFrame into the
473 // new_shadow_frame and perform argument conversions on it.
474 if (IsCallerTransformer(callsite_type)) {
475 is_caller_transformer = true;
476 // The emulated stack frame is the first and only argument when we're coming
477 // through from a transformer.
Orion Hodson960d4f72017-11-10 15:32:38 +0000478 size_t first_arg_register = operands->GetOperand(0);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000479 ObjPtr<mirror::EmulatedStackFrame> emulated_stack_frame(
480 reinterpret_cast<mirror::EmulatedStackFrame*>(
481 shadow_frame.GetVRegReference(first_arg_register)));
482 if (!emulated_stack_frame->WriteToShadowFrame(self,
483 target_type,
484 first_dest_reg,
485 new_shadow_frame)) {
486 DCHECK(self->IsExceptionPending());
487 result->SetL(0);
488 return false;
489 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000490 } else {
491 if (!callsite_type->IsConvertible(target_type.Get())) {
492 ThrowWrongMethodTypeException(target_type.Get(), callsite_type.Get());
493 return false;
494 }
Orion Hodson960d4f72017-11-10 15:32:38 +0000495 if (!ConvertAndCopyArgumentsFromCallerFrame(self,
496 callsite_type,
497 target_type,
498 shadow_frame,
499 first_dest_reg,
500 operands,
501 new_shadow_frame)) {
Orion Hodsona1be7132017-03-21 10:04:12 +0000502 DCHECK(self->IsExceptionPending());
503 result->SetL(0);
504 return false;
505 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000506 }
507 }
508 }
509
Jeff Hao5ea84132017-05-05 16:59:29 -0700510 bool use_interpreter_entrypoint = ClassLinker::ShouldUseInterpreterEntrypoint(
511 called_method, called_method->GetEntryPointFromQuickCompiledCode());
512 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800513 accessor,
Jeff Hao5ea84132017-05-05 16:59:29 -0700514 shadow_frame.GetMethod(),
515 first_dest_reg,
516 new_shadow_frame,
517 result,
518 use_interpreter_entrypoint);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000519 if (self->IsExceptionPending()) {
520 return false;
521 }
522
523 // If the caller of this signature polymorphic method was a transformer,
524 // we need to copy the result back out to the emulated stack frame.
525 if (is_caller_transformer) {
526 StackHandleScope<2> hs(self);
Orion Hodson960d4f72017-11-10 15:32:38 +0000527 size_t first_callee_register = operands->GetOperand(0);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000528 Handle<mirror::EmulatedStackFrame> emulated_stack_frame(
529 hs.NewHandle(reinterpret_cast<mirror::EmulatedStackFrame*>(
530 shadow_frame.GetVRegReference(first_callee_register))));
531 Handle<mirror::MethodType> emulated_stack_type(hs.NewHandle(emulated_stack_frame->GetType()));
532 JValue local_result;
533 local_result.SetJ(result->GetJ());
534
535 if (ConvertReturnValue(emulated_stack_type, target_type, &local_result)) {
536 emulated_stack_frame->SetReturnValue(self, local_result);
537 return true;
Orion Hodson811bd5f2016-12-07 11:35:37 +0000538 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000539
540 DCHECK(self->IsExceptionPending());
541 return false;
Orion Hodson811bd5f2016-12-07 11:35:37 +0000542 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000543
544 return ConvertReturnValue(callsite_type, target_type, result);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000545}
546
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100547static inline bool MethodHandleInvokeTransform(ArtMethod* called_method,
548 Handle<mirror::MethodType> callsite_type,
549 Handle<mirror::MethodType> callee_type,
550 Thread* self,
551 ShadowFrame& shadow_frame,
552 Handle<mirror::MethodHandle> receiver,
Orion Hodson960d4f72017-11-10 15:32:38 +0000553 const InstructionOperands* const operands,
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100554 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000555 REQUIRES_SHARED(Locks::mutator_lock_) {
556 // This can be fixed to two, because the method we're calling here
557 // (MethodHandle.transformInternal) doesn't have any locals and the signature
558 // is known :
559 //
560 // private MethodHandle.transformInternal(EmulatedStackFrame sf);
561 //
562 // This means we need only two vregs :
563 // - One for the receiver object.
564 // - One for the only method argument (an EmulatedStackFrame).
565 static constexpr size_t kNumRegsForTransform = 2;
566
David Sehr0225f8e2018-01-31 08:52:24 +0000567 CodeItemDataAccessor accessor(called_method->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800568 DCHECK_EQ(kNumRegsForTransform, accessor.RegistersSize());
569 DCHECK_EQ(kNumRegsForTransform, accessor.InsSize());
Orion Hodson811bd5f2016-12-07 11:35:37 +0000570
571 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
572 CREATE_SHADOW_FRAME(kNumRegsForTransform, &shadow_frame, called_method, /* dex pc */ 0);
573 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
574
575 StackHandleScope<1> hs(self);
576 MutableHandle<mirror::EmulatedStackFrame> sf(hs.NewHandle<mirror::EmulatedStackFrame>(nullptr));
577 if (IsCallerTransformer(callsite_type)) {
578 // If we're entering this transformer from another transformer, we can pass
579 // through the handle directly to the callee, instead of having to
580 // instantiate a new stack frame based on the shadow frame.
Orion Hodson960d4f72017-11-10 15:32:38 +0000581 size_t first_callee_register = operands->GetOperand(0);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000582 sf.Assign(reinterpret_cast<mirror::EmulatedStackFrame*>(
583 shadow_frame.GetVRegReference(first_callee_register)));
584 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000585 sf.Assign(mirror::EmulatedStackFrame::CreateFromShadowFrameAndArgs(self,
586 callsite_type,
587 callee_type,
588 shadow_frame,
589 operands));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000590
591 // Something went wrong while creating the emulated stack frame, we should
592 // throw the pending exception.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800593 if (sf == nullptr) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000594 DCHECK(self->IsExceptionPending());
595 return false;
596 }
597 }
598
599 new_shadow_frame->SetVRegReference(0, receiver.Get());
600 new_shadow_frame->SetVRegReference(1, sf.Get());
601
Jeff Hao5ea84132017-05-05 16:59:29 -0700602 bool use_interpreter_entrypoint = ClassLinker::ShouldUseInterpreterEntrypoint(
603 called_method, called_method->GetEntryPointFromQuickCompiledCode());
Orion Hodson811bd5f2016-12-07 11:35:37 +0000604 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800605 accessor,
Orion Hodson811bd5f2016-12-07 11:35:37 +0000606 shadow_frame.GetMethod(),
607 0 /* first destination register */,
608 new_shadow_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -0700609 result,
610 use_interpreter_entrypoint);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000611 if (self->IsExceptionPending()) {
612 return false;
613 }
614
615 // If the called transformer method we called has returned a value, then we
616 // need to copy it back to |result|.
617 sf->GetReturnValue(self, result);
618 return ConvertReturnValue(callsite_type, callee_type, result);
619}
620
621inline static ObjPtr<mirror::Class> GetAndInitializeDeclaringClass(Thread* self, ArtField* field)
622 REQUIRES_SHARED(Locks::mutator_lock_) {
623 // Method handle invocations on static fields should ensure class is
624 // initialized. This usually happens when an instance is constructed
625 // or class members referenced, but this is not guaranteed when
626 // looking up method handles.
627 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
628 if (UNLIKELY(!klass->IsInitialized())) {
629 StackHandleScope<1> hs(self);
630 HandleWrapperObjPtr<mirror::Class> h(hs.NewHandleWrapper(&klass));
631 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h, true, true)) {
632 DCHECK(self->IsExceptionPending());
633 return nullptr;
634 }
635 }
636 return klass;
637}
638
Orion Hodsona1be7132017-03-21 10:04:12 +0000639ArtMethod* RefineTargetMethod(Thread* self,
640 ShadowFrame& shadow_frame,
641 const mirror::MethodHandle::Kind& handle_kind,
642 Handle<mirror::MethodType> handle_type,
643 Handle<mirror::MethodType> callsite_type,
644 const uint32_t receiver_reg,
645 ArtMethod* target_method)
646 REQUIRES_SHARED(Locks::mutator_lock_) {
647 if (handle_kind == mirror::MethodHandle::Kind::kInvokeVirtual ||
648 handle_kind == mirror::MethodHandle::Kind::kInvokeInterface) {
649 // For virtual and interface methods ensure target_method points to
650 // the actual method to invoke.
651 ObjPtr<mirror::Object> receiver(shadow_frame.GetVRegReference(receiver_reg));
652 if (IsCallerTransformer(callsite_type)) {
653 // The current receiver is an emulated stack frame, the method's
654 // receiver needs to be fetched from there as the emulated frame
655 // will be unpacked into a new frame.
656 receiver = ObjPtr<mirror::EmulatedStackFrame>::DownCast(receiver)->GetReceiver();
657 }
658
659 ObjPtr<mirror::Class> declaring_class(target_method->GetDeclaringClass());
660 if (receiver == nullptr || receiver->GetClass() != declaring_class) {
661 // Verify that _vRegC is an object reference and of the type expected by
662 // the receiver.
663 if (!VerifyObjectIsClass(receiver, declaring_class)) {
664 DCHECK(self->IsExceptionPending());
665 return nullptr;
666 }
667 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(
668 target_method, kRuntimePointerSize);
669 }
670 } else if (handle_kind == mirror::MethodHandle::Kind::kInvokeDirect) {
671 // String constructors are a special case, they are replaced with
672 // StringFactory methods.
673 if (target_method->IsConstructor() && target_method->GetDeclaringClass()->IsStringClass()) {
674 DCHECK(handle_type->GetRType()->IsStringClass());
675 return WellKnownClasses::StringInitToStringFactory(target_method);
676 }
677 } else if (handle_kind == mirror::MethodHandle::Kind::kInvokeSuper) {
Orion Hodsona1be7132017-03-21 10:04:12 +0000678 // Note that we're not dynamically dispatching on the type of the receiver
679 // here. We use the static type of the "receiver" object that we've
680 // recorded in the method handle's type, which will be the same as the
681 // special caller that was specified at the point of lookup.
682 ObjPtr<mirror::Class> referrer_class = handle_type->GetPTypes()->Get(0);
Orion Hodson0f595742018-04-10 14:49:28 +0100683 ObjPtr<mirror::Class> declaring_class = target_method->GetDeclaringClass();
684 if (referrer_class == declaring_class) {
685 return target_method;
686 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000687 if (!declaring_class->IsInterface()) {
688 ObjPtr<mirror::Class> super_class = referrer_class->GetSuperClass();
689 uint16_t vtable_index = target_method->GetMethodIndex();
690 DCHECK(super_class != nullptr);
691 DCHECK(super_class->HasVTable());
692 // Note that super_class is a super of referrer_class and target_method
693 // will always be declared by super_class (or one of its super classes).
694 DCHECK_LT(vtable_index, super_class->GetVTableLength());
695 return super_class->GetVTableEntry(vtable_index, kRuntimePointerSize);
Orion Hodsona1be7132017-03-21 10:04:12 +0000696 }
697 }
698 return target_method;
699}
700
Orion Hodsona1be7132017-03-21 10:04:12 +0000701bool DoInvokePolymorphicMethod(Thread* self,
702 ShadowFrame& shadow_frame,
703 Handle<mirror::MethodHandle> method_handle,
704 Handle<mirror::MethodType> callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +0000705 const InstructionOperands* const operands,
Orion Hodsona1be7132017-03-21 10:04:12 +0000706 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000707 REQUIRES_SHARED(Locks::mutator_lock_) {
708 StackHandleScope<1> hs(self);
709 Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType()));
710 const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind();
Orion Hodsona1be7132017-03-21 10:04:12 +0000711 DCHECK(IsInvoke(handle_kind));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000712
Orion Hodsona1be7132017-03-21 10:04:12 +0000713 // Get the method we're actually invoking along with the kind of
714 // invoke that is desired. We don't need to perform access checks at this
715 // point because they would have been performed on our behalf at the point
716 // of creation of the method handle.
717 ArtMethod* target_method = method_handle->GetTargetMethod();
Orion Hodson960d4f72017-11-10 15:32:38 +0000718 uint32_t receiver_reg = (operands->GetNumberOfOperands() > 0) ? operands->GetOperand(0) : 0u;
Orion Hodsona1be7132017-03-21 10:04:12 +0000719 ArtMethod* called_method = RefineTargetMethod(self,
720 shadow_frame,
721 handle_kind,
722 handle_type,
723 callsite_type,
724 receiver_reg,
725 target_method);
726 if (called_method == nullptr) {
727 DCHECK(self->IsExceptionPending());
728 return false;
729 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000730
Orion Hodsona1be7132017-03-21 10:04:12 +0000731 if (IsInvokeTransform(handle_kind)) {
732 // There are two cases here - method handles representing regular
733 // transforms and those representing call site transforms. Method
734 // handles for call site transforms adapt their MethodType to match
735 // the call site. For these, the |callee_type| is the same as the
736 // |callsite_type|. The VarargsCollector is such a tranform, its
737 // method type depends on the call site, ie. x(a) or x(a, b), or
738 // x(a, b, c). The VarargsCollector invokes a variable arity method
739 // with the arity arguments in an array.
740 Handle<mirror::MethodType> callee_type =
741 (handle_kind == mirror::MethodHandle::Kind::kInvokeCallSiteTransform) ? callsite_type
742 : handle_type;
Orion Hodson960d4f72017-11-10 15:32:38 +0000743 return MethodHandleInvokeTransform(called_method,
744 callsite_type,
745 callee_type,
746 self,
747 shadow_frame,
748 method_handle /* receiver */,
749 operands,
750 result);
Orion Hodsona1be7132017-03-21 10:04:12 +0000751 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000752 return MethodHandleInvokeMethod(called_method,
753 callsite_type,
754 handle_type,
755 self,
756 shadow_frame,
757 operands,
758 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000759 }
760}
761
762// Helper for getters in invoke-polymorphic.
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100763inline static void MethodHandleFieldGet(Thread* self,
764 const ShadowFrame& shadow_frame,
765 ObjPtr<mirror::Object>& obj,
766 ArtField* field,
767 Primitive::Type field_type,
768 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000769 REQUIRES_SHARED(Locks::mutator_lock_) {
770 switch (field_type) {
771 case Primitive::kPrimBoolean:
772 DoFieldGetCommon<Primitive::kPrimBoolean>(self, shadow_frame, obj, field, result);
773 break;
774 case Primitive::kPrimByte:
775 DoFieldGetCommon<Primitive::kPrimByte>(self, shadow_frame, obj, field, result);
776 break;
777 case Primitive::kPrimChar:
778 DoFieldGetCommon<Primitive::kPrimChar>(self, shadow_frame, obj, field, result);
779 break;
780 case Primitive::kPrimShort:
781 DoFieldGetCommon<Primitive::kPrimShort>(self, shadow_frame, obj, field, result);
782 break;
783 case Primitive::kPrimInt:
784 DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result);
785 break;
786 case Primitive::kPrimLong:
787 DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result);
788 break;
789 case Primitive::kPrimFloat:
790 DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result);
791 break;
792 case Primitive::kPrimDouble:
793 DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result);
794 break;
795 case Primitive::kPrimNot:
796 DoFieldGetCommon<Primitive::kPrimNot>(self, shadow_frame, obj, field, result);
797 break;
798 case Primitive::kPrimVoid:
799 LOG(FATAL) << "Unreachable: " << field_type;
800 UNREACHABLE();
801 }
802}
803
804// Helper for setters in invoke-polymorphic.
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100805inline bool MethodHandleFieldPut(Thread* self,
806 ShadowFrame& shadow_frame,
807 ObjPtr<mirror::Object>& obj,
808 ArtField* field,
809 Primitive::Type field_type,
810 JValue& value)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000811 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodsonc069a302017-01-18 09:23:12 +0000812 DCHECK(!Runtime::Current()->IsActiveTransaction());
813 static const bool kTransaction = false; // Not in a transaction.
814 static const bool kAssignabilityCheck = false; // No access check.
Orion Hodson811bd5f2016-12-07 11:35:37 +0000815 switch (field_type) {
816 case Primitive::kPrimBoolean:
Orion Hodsonc069a302017-01-18 09:23:12 +0000817 return
818 DoFieldPutCommon<Primitive::kPrimBoolean, kAssignabilityCheck, kTransaction>(
819 self, shadow_frame, obj, field, value);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000820 case Primitive::kPrimByte:
Orion Hodsonc069a302017-01-18 09:23:12 +0000821 return DoFieldPutCommon<Primitive::kPrimByte, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000822 self, shadow_frame, obj, field, value);
823 case Primitive::kPrimChar:
Orion Hodsonc069a302017-01-18 09:23:12 +0000824 return DoFieldPutCommon<Primitive::kPrimChar, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000825 self, shadow_frame, obj, field, value);
826 case Primitive::kPrimShort:
Orion Hodsonc069a302017-01-18 09:23:12 +0000827 return DoFieldPutCommon<Primitive::kPrimShort, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000828 self, shadow_frame, obj, field, value);
829 case Primitive::kPrimInt:
830 case Primitive::kPrimFloat:
Orion Hodsonc069a302017-01-18 09:23:12 +0000831 return DoFieldPutCommon<Primitive::kPrimInt, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000832 self, shadow_frame, obj, field, value);
833 case Primitive::kPrimLong:
834 case Primitive::kPrimDouble:
Orion Hodsonc069a302017-01-18 09:23:12 +0000835 return DoFieldPutCommon<Primitive::kPrimLong, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000836 self, shadow_frame, obj, field, value);
837 case Primitive::kPrimNot:
Orion Hodsonc069a302017-01-18 09:23:12 +0000838 return DoFieldPutCommon<Primitive::kPrimNot, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000839 self, shadow_frame, obj, field, value);
840 case Primitive::kPrimVoid:
841 LOG(FATAL) << "Unreachable: " << field_type;
842 UNREACHABLE();
843 }
844}
845
846static JValue GetValueFromShadowFrame(const ShadowFrame& shadow_frame,
847 Primitive::Type field_type,
848 uint32_t vreg)
849 REQUIRES_SHARED(Locks::mutator_lock_) {
850 JValue field_value;
851 switch (field_type) {
852 case Primitive::kPrimBoolean:
853 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
854 break;
855 case Primitive::kPrimByte:
856 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
857 break;
858 case Primitive::kPrimChar:
859 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
860 break;
861 case Primitive::kPrimShort:
862 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
863 break;
864 case Primitive::kPrimInt:
865 case Primitive::kPrimFloat:
866 field_value.SetI(shadow_frame.GetVReg(vreg));
867 break;
868 case Primitive::kPrimLong:
869 case Primitive::kPrimDouble:
870 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
871 break;
872 case Primitive::kPrimNot:
873 field_value.SetL(shadow_frame.GetVRegReference(vreg));
874 break;
875 case Primitive::kPrimVoid:
876 LOG(FATAL) << "Unreachable: " << field_type;
877 UNREACHABLE();
878 }
879 return field_value;
880}
881
Orion Hodson960d4f72017-11-10 15:32:38 +0000882template <bool do_conversions>
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100883bool MethodHandleFieldAccess(Thread* self,
884 ShadowFrame& shadow_frame,
885 Handle<mirror::MethodHandle> method_handle,
886 Handle<mirror::MethodType> callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +0000887 const InstructionOperands* const operands,
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100888 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000889 StackHandleScope<1> hs(self);
890 Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType()));
891 const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind();
892 ArtField* field = method_handle->GetTargetField();
893 Primitive::Type field_type = field->GetTypeAsPrimitiveType();
Orion Hodson811bd5f2016-12-07 11:35:37 +0000894 switch (handle_kind) {
895 case mirror::MethodHandle::kInstanceGet: {
Orion Hodson960d4f72017-11-10 15:32:38 +0000896 size_t obj_reg = operands->GetOperand(0);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000897 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(obj_reg);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100898 MethodHandleFieldGet(self, shadow_frame, obj, field, field_type, result);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000899 if (do_conversions && !ConvertReturnValue(callsite_type, handle_type, result)) {
900 DCHECK(self->IsExceptionPending());
901 return false;
902 }
903 return true;
904 }
905 case mirror::MethodHandle::kStaticGet: {
906 ObjPtr<mirror::Object> obj = GetAndInitializeDeclaringClass(self, field);
907 if (obj == nullptr) {
908 DCHECK(self->IsExceptionPending());
909 return false;
910 }
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100911 MethodHandleFieldGet(self, shadow_frame, obj, field, field_type, result);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000912 if (do_conversions && !ConvertReturnValue(callsite_type, handle_type, result)) {
913 DCHECK(self->IsExceptionPending());
914 return false;
915 }
916 return true;
917 }
918 case mirror::MethodHandle::kInstancePut: {
Orion Hodson960d4f72017-11-10 15:32:38 +0000919 size_t obj_reg = operands->GetOperand(0);
920 size_t value_reg = operands->GetOperand(1);
Mathieu Chartier71b17082017-04-17 20:12:29 -0700921 const size_t kPTypeIndex = 1;
922 // Use ptypes instead of field type since we may be unboxing a reference for a primitive
923 // field. The field type is incorrect for this case.
924 JValue value = GetValueFromShadowFrame(
925 shadow_frame,
926 callsite_type->GetPTypes()->Get(kPTypeIndex)->GetPrimitiveType(),
927 value_reg);
928 if (do_conversions && !ConvertArgumentValue(callsite_type,
929 handle_type,
930 kPTypeIndex,
931 &value)) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000932 DCHECK(self->IsExceptionPending());
933 return false;
934 }
935 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(obj_reg);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100936 return MethodHandleFieldPut(self, shadow_frame, obj, field, field_type, value);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000937 }
938 case mirror::MethodHandle::kStaticPut: {
939 ObjPtr<mirror::Object> obj = GetAndInitializeDeclaringClass(self, field);
940 if (obj == nullptr) {
941 DCHECK(self->IsExceptionPending());
942 return false;
943 }
Orion Hodson960d4f72017-11-10 15:32:38 +0000944 size_t value_reg = operands->GetOperand(0);
Mathieu Chartier71b17082017-04-17 20:12:29 -0700945 const size_t kPTypeIndex = 0;
946 // Use ptypes instead of field type since we may be unboxing a reference for a primitive
947 // field. The field type is incorrect for this case.
948 JValue value = GetValueFromShadowFrame(
949 shadow_frame,
950 callsite_type->GetPTypes()->Get(kPTypeIndex)->GetPrimitiveType(),
951 value_reg);
952 if (do_conversions && !ConvertArgumentValue(callsite_type,
953 handle_type,
954 kPTypeIndex,
955 &value)) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000956 DCHECK(self->IsExceptionPending());
957 return false;
958 }
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100959 return MethodHandleFieldPut(self, shadow_frame, obj, field, field_type, value);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000960 }
961 default:
962 LOG(FATAL) << "Unreachable: " << handle_kind;
963 UNREACHABLE();
964 }
965}
966
Orion Hodsonb8b93872018-01-30 07:51:10 +0000967bool DoVarHandleInvokeTranslationUnchecked(Thread* self,
968 ShadowFrame& shadow_frame,
969 mirror::VarHandle::AccessMode access_mode,
970 Handle<mirror::VarHandle> vh,
971 Handle<mirror::MethodType> vh_type,
972 Handle<mirror::MethodType> callsite_type,
973 const InstructionOperands* const operands,
974 JValue* result)
975 REQUIRES_SHARED(Locks::mutator_lock_) {
976 DCHECK_EQ(operands->GetNumberOfOperands(), static_cast<uint32_t>(vh_type->GetNumberOfPTypes()));
977 DCHECK_EQ(operands->GetNumberOfOperands(),
978 static_cast<uint32_t>(callsite_type->GetNumberOfPTypes()));
979 const size_t vreg_count = vh_type->NumberOfVRegs();
980 ShadowFrameAllocaUniquePtr accessor_frame =
981 CREATE_SHADOW_FRAME(vreg_count, nullptr, shadow_frame.GetMethod(), shadow_frame.GetDexPC());
982 ShadowFrameGetter getter(shadow_frame, operands);
983 static const uint32_t kFirstAccessorReg = 0;
984 ShadowFrameSetter setter(accessor_frame.get(), kFirstAccessorReg);
985 if (!PerformConversions(self, callsite_type, vh_type, &getter, &setter)) {
986 return false;
987 }
988 RangeInstructionOperands accessor_operands(kFirstAccessorReg, kFirstAccessorReg + vreg_count);
989 if (!vh->Access(access_mode, accessor_frame.get(), &accessor_operands, result)) {
990 return false;
991 }
992 return ConvertReturnValue(callsite_type, vh_type, result);
993}
994
995bool DoVarHandleInvokeTranslation(Thread* self,
996 ShadowFrame& shadow_frame,
997 bool invokeExact,
998 Handle<mirror::MethodHandle> method_handle,
999 Handle<mirror::MethodType> callsite_type,
1000 const InstructionOperands* const operands,
1001 JValue* result)
1002 REQUIRES_SHARED(Locks::mutator_lock_) {
1003 if (!invokeExact) {
1004 // Exact invokes are checked for compatability higher up. The
1005 // non-exact invoke path doesn't have a similar check due to
1006 // transformers which have EmulatedStack frame arguments with the
1007 // actual method type associated with the frame.
1008 if (UNLIKELY(!callsite_type->IsConvertible(method_handle->GetMethodType()))) {
1009 ThrowWrongMethodTypeException(method_handle->GetMethodType(), callsite_type.Get());
1010 return false;
1011 }
1012 }
1013
1014 //
1015 // Basic checks that apply in all cases.
1016 //
1017 StackHandleScope<6> hs(self);
1018 Handle<mirror::ObjectArray<mirror::Class>>
1019 callsite_ptypes(hs.NewHandle(callsite_type->GetPTypes()));
1020 Handle<mirror::ObjectArray<mirror::Class>>
1021 mh_ptypes(hs.NewHandle(method_handle->GetMethodType()->GetPTypes()));
1022
1023 // Check that the first parameter is a VarHandle
1024 if (callsite_ptypes->GetLength() < 1 ||
1025 !mh_ptypes->Get(0)->IsAssignableFrom(callsite_ptypes->Get(0)) ||
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001026 mh_ptypes->Get(0) != GetClassRoot<mirror::VarHandle>()) {
Orion Hodsonb8b93872018-01-30 07:51:10 +00001027 ThrowWrongMethodTypeException(method_handle->GetMethodType(), callsite_type.Get());
1028 return false;
1029 }
1030
1031 // Get the receiver
1032 mirror::Object* receiver = shadow_frame.GetVRegReference(operands->GetOperand(0));
1033 if (receiver == nullptr) {
1034 ThrowNullPointerException("Expected argument 1 to be a non-null VarHandle");
1035 return false;
1036 }
1037
1038 // Cast to VarHandle instance
1039 Handle<mirror::VarHandle> vh(hs.NewHandle(down_cast<mirror::VarHandle*>(receiver)));
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001040 DCHECK(GetClassRoot<mirror::VarHandle>()->IsAssignableFrom(vh->GetClass()));
Orion Hodsonb8b93872018-01-30 07:51:10 +00001041
1042 // Determine the accessor kind to dispatch
1043 ArtMethod* target_method = method_handle->GetTargetMethod();
1044 int intrinsic_index = target_method->GetIntrinsic();
1045 mirror::VarHandle::AccessMode access_mode =
1046 mirror::VarHandle::GetAccessModeByIntrinsic(static_cast<Intrinsics>(intrinsic_index));
1047 Handle<mirror::MethodType> vh_type =
1048 hs.NewHandle(vh->GetMethodTypeForAccessMode(self, access_mode));
1049 Handle<mirror::MethodType> mh_invoke_type = hs.NewHandle(
1050 mirror::MethodType::CloneWithoutLeadingParameter(self, method_handle->GetMethodType()));
1051 if (method_handle->GetHandleKind() == mirror::MethodHandle::Kind::kInvokeVarHandleExact) {
1052 if (!mh_invoke_type->IsExactMatch(vh_type.Get())) {
1053 ThrowWrongMethodTypeException(vh_type.Get(), mh_invoke_type.Get());
1054 return false;
1055 }
1056 } else {
1057 DCHECK_EQ(method_handle->GetHandleKind(), mirror::MethodHandle::Kind::kInvokeVarHandle);
1058 if (!mh_invoke_type->IsConvertible(vh_type.Get())) {
1059 ThrowWrongMethodTypeException(vh_type.Get(), mh_invoke_type.Get());
1060 return false;
1061 }
1062 }
1063
1064 Handle<mirror::MethodType> callsite_type_without_varhandle =
1065 hs.NewHandle(mirror::MethodType::CloneWithoutLeadingParameter(self, callsite_type.Get()));
1066 NoReceiverInstructionOperands varhandle_operands(operands);
1067 DCHECK_EQ(static_cast<int32_t>(varhandle_operands.GetNumberOfOperands()),
1068 callsite_type_without_varhandle->GetPTypes()->GetLength());
1069 return DoVarHandleInvokeTranslationUnchecked(self,
1070 shadow_frame,
1071 access_mode,
1072 vh,
1073 vh_type,
1074 callsite_type_without_varhandle,
1075 &varhandle_operands,
1076 result);
1077}
1078
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001079static inline bool MethodHandleInvokeInternal(Thread* self,
1080 ShadowFrame& shadow_frame,
1081 Handle<mirror::MethodHandle> method_handle,
1082 Handle<mirror::MethodType> callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +00001083 const InstructionOperands* const operands,
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001084 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +00001085 REQUIRES_SHARED(Locks::mutator_lock_) {
1086 const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind();
Orion Hodsona1be7132017-03-21 10:04:12 +00001087 if (IsFieldAccess(handle_kind)) {
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001088 ObjPtr<mirror::MethodType> handle_type(method_handle->GetMethodType());
Orion Hodsona1be7132017-03-21 10:04:12 +00001089 DCHECK(!callsite_type->IsExactMatch(handle_type.Ptr()));
1090 if (!callsite_type->IsConvertible(handle_type.Ptr())) {
Orion Hodson811bd5f2016-12-07 11:35:37 +00001091 ThrowWrongMethodTypeException(handle_type.Ptr(), callsite_type.Get());
1092 return false;
1093 }
Orion Hodsona1be7132017-03-21 10:04:12 +00001094 const bool do_convert = true;
Orion Hodson960d4f72017-11-10 15:32:38 +00001095 return MethodHandleFieldAccess<do_convert>(
Orion Hodsona1be7132017-03-21 10:04:12 +00001096 self,
1097 shadow_frame,
1098 method_handle,
1099 callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +00001100 operands,
Orion Hodsona1be7132017-03-21 10:04:12 +00001101 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +00001102 }
Orion Hodsonb8b93872018-01-30 07:51:10 +00001103 if (IsInvokeVarHandle(handle_kind)) {
1104 return DoVarHandleInvokeTranslation(self,
1105 shadow_frame,
1106 /*invokeExact*/ false,
1107 method_handle,
1108 callsite_type,
1109 operands,
1110 result);
1111 }
Orion Hodson960d4f72017-11-10 15:32:38 +00001112 return DoInvokePolymorphicMethod(self,
1113 shadow_frame,
1114 method_handle,
1115 callsite_type,
1116 operands,
1117 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +00001118}
1119
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001120static inline bool MethodHandleInvokeExactInternal(
1121 Thread* self,
1122 ShadowFrame& shadow_frame,
1123 Handle<mirror::MethodHandle> method_handle,
1124 Handle<mirror::MethodType> callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +00001125 const InstructionOperands* const operands,
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001126 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +00001127 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodsona1be7132017-03-21 10:04:12 +00001128 StackHandleScope<1> hs(self);
Orion Hodsona1be7132017-03-21 10:04:12 +00001129 Handle<mirror::MethodType> method_handle_type(hs.NewHandle(method_handle->GetMethodType()));
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001130 if (!callsite_type->IsExactMatch(method_handle_type.Get())) {
1131 ThrowWrongMethodTypeException(method_handle_type.Get(), callsite_type.Get());
1132 return false;
1133 }
1134
1135 const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind();
Orion Hodson811bd5f2016-12-07 11:35:37 +00001136 if (IsFieldAccess(handle_kind)) {
1137 const bool do_convert = false;
Orion Hodson960d4f72017-11-10 15:32:38 +00001138 return MethodHandleFieldAccess<do_convert>(self,
1139 shadow_frame,
1140 method_handle,
1141 callsite_type,
1142 operands,
1143 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +00001144 }
1145
Orion Hodsona1be7132017-03-21 10:04:12 +00001146 // Slow-path check.
Orion Hodsonb8b93872018-01-30 07:51:10 +00001147 if (IsInvokeTransform(handle_kind) ||
1148 IsCallerTransformer(callsite_type)) {
Orion Hodson960d4f72017-11-10 15:32:38 +00001149 return DoInvokePolymorphicMethod(self,
1150 shadow_frame,
1151 method_handle,
1152 callsite_type,
1153 operands,
1154 result);
Orion Hodsonb8b93872018-01-30 07:51:10 +00001155 } else if (IsInvokeVarHandle(handle_kind)) {
1156 return DoVarHandleInvokeTranslation(self,
1157 shadow_frame,
1158 /*invokeExact*/ true,
1159 method_handle,
1160 callsite_type,
1161 operands,
1162 result);
Orion Hodsona1be7132017-03-21 10:04:12 +00001163 }
1164
1165 // On the fast-path. This is equivalent to DoCallPolymoprhic without the conversion paths.
1166 ArtMethod* target_method = method_handle->GetTargetMethod();
Orion Hodson960d4f72017-11-10 15:32:38 +00001167 uint32_t receiver_reg = (operands->GetNumberOfOperands() > 0) ? operands->GetOperand(0) : 0u;
Orion Hodsona1be7132017-03-21 10:04:12 +00001168 ArtMethod* called_method = RefineTargetMethod(self,
Orion Hodson811bd5f2016-12-07 11:35:37 +00001169 shadow_frame,
Orion Hodsona1be7132017-03-21 10:04:12 +00001170 handle_kind,
1171 method_handle_type,
Orion Hodson811bd5f2016-12-07 11:35:37 +00001172 callsite_type,
Orion Hodsona1be7132017-03-21 10:04:12 +00001173 receiver_reg,
1174 target_method);
1175 if (called_method == nullptr) {
1176 DCHECK(self->IsExceptionPending());
1177 return false;
1178 }
1179
1180 // Compute method information.
David Sehr0225f8e2018-01-31 08:52:24 +00001181 CodeItemDataAccessor accessor(called_method->DexInstructionData());
Orion Hodsona1be7132017-03-21 10:04:12 +00001182 uint16_t num_regs;
1183 size_t num_input_regs;
1184 size_t first_dest_reg;
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001185 if (LIKELY(accessor.HasCodeItem())) {
1186 num_regs = accessor.RegistersSize();
1187 first_dest_reg = num_regs - accessor.InsSize();
1188 num_input_regs = accessor.InsSize();
Orion Hodsona1be7132017-03-21 10:04:12 +00001189 // Parameter registers go at the end of the shadow frame.
1190 DCHECK_NE(first_dest_reg, (size_t)-1);
1191 } else {
1192 // No local regs for proxy and native methods.
1193 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1194 num_regs = num_input_regs = GetInsForProxyOrNativeMethod(called_method);
1195 first_dest_reg = 0;
1196 }
1197
1198 // Allocate shadow frame on the stack.
1199 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
1200 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
1201 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
1202 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Orion Hodson960d4f72017-11-10 15:32:38 +00001203 CopyArgumentsFromCallerFrame(shadow_frame,
1204 new_shadow_frame,
1205 operands,
1206 first_dest_reg);
Orion Hodsona1be7132017-03-21 10:04:12 +00001207 self->EndAssertNoThreadSuspension(old_cause);
1208
Jeff Hao5ea84132017-05-05 16:59:29 -07001209 bool use_interpreter_entrypoint = ClassLinker::ShouldUseInterpreterEntrypoint(
1210 called_method, called_method->GetEntryPointFromQuickCompiledCode());
1211 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001212 accessor,
Jeff Hao5ea84132017-05-05 16:59:29 -07001213 shadow_frame.GetMethod(),
1214 first_dest_reg,
1215 new_shadow_frame,
1216 result,
1217 use_interpreter_entrypoint);
Orion Hodsona1be7132017-03-21 10:04:12 +00001218 if (self->IsExceptionPending()) {
1219 return false;
1220 }
1221 return true;
Orion Hodson811bd5f2016-12-07 11:35:37 +00001222}
1223
1224} // namespace
1225
Orion Hodson960d4f72017-11-10 15:32:38 +00001226bool MethodHandleInvoke(Thread* self,
1227 ShadowFrame& shadow_frame,
1228 Handle<mirror::MethodHandle> method_handle,
1229 Handle<mirror::MethodType> callsite_type,
1230 const InstructionOperands* const operands,
1231 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +00001232 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001233 if (UNLIKELY(callsite_type->IsExactMatch(method_handle->GetMethodType()))) {
1234 // A non-exact invoke that can be invoked exactly.
Orion Hodson960d4f72017-11-10 15:32:38 +00001235 return MethodHandleInvokeExactInternal(self,
1236 shadow_frame,
1237 method_handle,
1238 callsite_type,
1239 operands,
1240 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +00001241 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +00001242 return MethodHandleInvokeInternal(self,
1243 shadow_frame,
1244 method_handle,
1245 callsite_type,
1246 operands,
1247 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +00001248 }
1249}
1250
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001251bool MethodHandleInvokeExact(Thread* self,
Orion Hodson960d4f72017-11-10 15:32:38 +00001252 ShadowFrame& shadow_frame,
1253 Handle<mirror::MethodHandle> method_handle,
1254 Handle<mirror::MethodType> callsite_type,
1255 const InstructionOperands* const operands,
1256 JValue* result)
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001257 REQUIRES_SHARED(Locks::mutator_lock_) {
1258 // We need to check the nominal type of the handle in addition to the
1259 // real type. The "nominal" type is present when MethodHandle.asType is
1260 // called any handle, and results in the declared type of the handle
1261 // changing.
1262 ObjPtr<mirror::MethodType> nominal_type(method_handle->GetNominalType());
1263 if (UNLIKELY(nominal_type != nullptr)) {
1264 if (UNLIKELY(!callsite_type->IsExactMatch(nominal_type.Ptr()))) {
1265 ThrowWrongMethodTypeException(nominal_type.Ptr(), callsite_type.Get());
1266 return false;
1267 }
1268 if (LIKELY(!nominal_type->IsExactMatch(method_handle->GetMethodType()))) {
1269 // Different nominal type means we have to treat as non-exact.
Orion Hodson960d4f72017-11-10 15:32:38 +00001270 return MethodHandleInvokeInternal(self,
1271 shadow_frame,
1272 method_handle,
1273 callsite_type,
1274 operands,
1275 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001276 }
1277 }
Orion Hodson960d4f72017-11-10 15:32:38 +00001278 return MethodHandleInvokeExactInternal(self,
1279 shadow_frame,
1280 method_handle,
1281 callsite_type,
1282 operands,
1283 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001284}
1285
Orion Hodsonba28f9f2016-10-26 10:56:25 +01001286} // namespace art