blob: 99886e5c2f05072c0b9e53074c20cad3182ce2d0 [file] [log] [blame]
Orion Hodsonba28f9f2016-10-26 10:56:25 +01001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Orion Hodsonba28f9f2016-10-26 10:56:25 +010017#include "method_handles-inl.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080018
19#include "android-base/stringprintf.h"
20
Orion Hodson811bd5f2016-12-07 11:35:37 +000021#include "common_dex_operations.h"
Orion Hodsonba28f9f2016-10-26 10:56:25 +010022#include "jvalue.h"
23#include "jvalue-inl.h"
Orion Hodson811bd5f2016-12-07 11:35:37 +000024#include "mirror/emulated_stack_frame.h"
25#include "mirror/method_handle_impl.h"
26#include "mirror/method_type.h"
Orion Hodsonba28f9f2016-10-26 10:56:25 +010027#include "reflection.h"
28#include "reflection-inl.h"
Orion Hodson1a06f9f2016-11-09 08:32:42 +000029#include "well_known_classes.h"
Orion Hodsonba28f9f2016-10-26 10:56:25 +010030
31namespace art {
32
Andreas Gampe46ee31b2016-12-14 10:11:49 -080033using android::base::StringPrintf;
34
Orion Hodsonba28f9f2016-10-26 10:56:25 +010035namespace {
36
Orion Hodson1a06f9f2016-11-09 08:32:42 +000037#define PRIMITIVES_LIST(V) \
38 V(Primitive::kPrimBoolean, Boolean, Boolean, Z) \
39 V(Primitive::kPrimByte, Byte, Byte, B) \
40 V(Primitive::kPrimChar, Char, Character, C) \
41 V(Primitive::kPrimShort, Short, Short, S) \
42 V(Primitive::kPrimInt, Int, Integer, I) \
43 V(Primitive::kPrimLong, Long, Long, J) \
44 V(Primitive::kPrimFloat, Float, Float, F) \
45 V(Primitive::kPrimDouble, Double, Double, D)
Orion Hodsonba28f9f2016-10-26 10:56:25 +010046
47// Assigns |type| to the primitive type associated with |klass|. Returns
48// true iff. |klass| was a boxed type (Integer, Long etc.), false otherwise.
49bool GetUnboxedPrimitiveType(ObjPtr<mirror::Class> klass, Primitive::Type* type)
50 REQUIRES_SHARED(Locks::mutator_lock_) {
51 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Orion Hodson1a06f9f2016-11-09 08:32:42 +000052#define LOOKUP_PRIMITIVE(primitive, _, __, ___) \
53 if (klass->DescriptorEquals(Primitive::BoxedDescriptor(primitive))) { \
54 *type = primitive; \
55 return true; \
Orion Hodsonba28f9f2016-10-26 10:56:25 +010056 }
Orion Hodsonba28f9f2016-10-26 10:56:25 +010057
Orion Hodson1a06f9f2016-11-09 08:32:42 +000058 PRIMITIVES_LIST(LOOKUP_PRIMITIVE);
59#undef LOOKUP_PRIMITIVE
Orion Hodsonba28f9f2016-10-26 10:56:25 +010060 return false;
61}
62
Orion Hodson1a06f9f2016-11-09 08:32:42 +000063ObjPtr<mirror::Class> GetBoxedPrimitiveClass(Primitive::Type type)
Orion Hodsonba28f9f2016-10-26 10:56:25 +010064 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson1a06f9f2016-11-09 08:32:42 +000065 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
66 jmethodID m = nullptr;
67 switch (type) {
68#define CASE_PRIMITIVE(primitive, _, java_name, __) \
69 case primitive: \
70 m = WellKnownClasses::java_lang_ ## java_name ## _valueOf; \
71 break;
72 PRIMITIVES_LIST(CASE_PRIMITIVE);
73#undef CASE_PRIMITIVE
74 case Primitive::Type::kPrimNot:
75 case Primitive::Type::kPrimVoid:
76 return nullptr;
Orion Hodsonba28f9f2016-10-26 10:56:25 +010077 }
Orion Hodson1a06f9f2016-11-09 08:32:42 +000078 return jni::DecodeArtMethod(m)->GetDeclaringClass();
79}
Orion Hodsonba28f9f2016-10-26 10:56:25 +010080
Orion Hodson1a06f9f2016-11-09 08:32:42 +000081bool GetUnboxedTypeAndValue(ObjPtr<mirror::Object> o, Primitive::Type* type, JValue* value)
82 REQUIRES_SHARED(Locks::mutator_lock_) {
83 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Orion Hodsonba28f9f2016-10-26 10:56:25 +010084 ObjPtr<mirror::Class> klass = o->GetClass();
Orion Hodsonba28f9f2016-10-26 10:56:25 +010085 ArtField* primitive_field = &klass->GetIFieldsPtr()->At(0);
Orion Hodson1a06f9f2016-11-09 08:32:42 +000086#define CASE_PRIMITIVE(primitive, abbrev, _, shorthand) \
87 if (klass == GetBoxedPrimitiveClass(primitive)) { \
88 *type = primitive; \
89 value->Set ## shorthand(primitive_field->Get ## abbrev(o)); \
90 return true; \
Orion Hodsonba28f9f2016-10-26 10:56:25 +010091 }
Orion Hodson1a06f9f2016-11-09 08:32:42 +000092 PRIMITIVES_LIST(CASE_PRIMITIVE)
93#undef CASE_PRIMITIVE
94 return false;
Orion Hodsonba28f9f2016-10-26 10:56:25 +010095}
96
97inline bool IsReferenceType(Primitive::Type type) {
98 return type == Primitive::kPrimNot;
99}
100
101inline bool IsPrimitiveType(Primitive::Type type) {
102 return !IsReferenceType(type);
103}
104
105} // namespace
106
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000107bool IsParameterTypeConvertible(ObjPtr<mirror::Class> from, ObjPtr<mirror::Class> to)
108 REQUIRES_SHARED(Locks::mutator_lock_) {
109 // This function returns true if there's any conceivable conversion
110 // between |from| and |to|. It's expected this method will be used
111 // to determine if a WrongMethodTypeException should be raised. The
112 // decision logic follows the documentation for MethodType.asType().
113 if (from == to) {
114 return true;
115 }
116
117 Primitive::Type from_primitive = from->GetPrimitiveType();
118 Primitive::Type to_primitive = to->GetPrimitiveType();
119 DCHECK(from_primitive != Primitive::Type::kPrimVoid);
120 DCHECK(to_primitive != Primitive::Type::kPrimVoid);
121
122 // If |to| and |from| are references.
123 if (IsReferenceType(from_primitive) && IsReferenceType(to_primitive)) {
124 // Assignability is determined during parameter conversion when
125 // invoking the associated method handle.
126 return true;
127 }
128
129 // If |to| and |from| are primitives and a widening conversion exists.
130 if (Primitive::IsWidenable(from_primitive, to_primitive)) {
131 return true;
132 }
133
134 // If |to| is a reference and |from| is a primitive, then boxing conversion.
135 if (IsReferenceType(to_primitive) && IsPrimitiveType(from_primitive)) {
136 return to->IsAssignableFrom(GetBoxedPrimitiveClass(from_primitive));
137 }
138
139 // If |from| is a reference and |to| is a primitive, then unboxing conversion.
140 if (IsPrimitiveType(to_primitive) && IsReferenceType(from_primitive)) {
141 if (from->DescriptorEquals("Ljava/lang/Object;")) {
142 // Object might be converted into a primitive during unboxing.
143 return true;
144 } else if (Primitive::IsNumericType(to_primitive) &&
145 from->DescriptorEquals("Ljava/lang/Number;")) {
146 // Number might be unboxed into any of the number primitive types.
147 return true;
148 }
149 Primitive::Type unboxed_type;
150 if (GetUnboxedPrimitiveType(from, &unboxed_type)) {
Orion Hodsonf1412b42016-11-11 12:03:29 +0000151 if (unboxed_type == to_primitive) {
152 // Straightforward unboxing conversion such as Boolean => boolean.
153 return true;
154 } else {
155 // Check if widening operations for numeric primitives would work,
156 // such as Byte => byte => long.
157 return Primitive::IsWidenable(unboxed_type, to_primitive);
158 }
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000159 }
160 }
161
162 return false;
163}
164
165bool IsReturnTypeConvertible(ObjPtr<mirror::Class> from, ObjPtr<mirror::Class> to)
166 REQUIRES_SHARED(Locks::mutator_lock_) {
167 if (to->GetPrimitiveType() == Primitive::Type::kPrimVoid) {
168 // Result will be ignored.
169 return true;
170 } else if (from->GetPrimitiveType() == Primitive::Type::kPrimVoid) {
171 // Returned value will be 0 / null.
172 return true;
173 } else {
174 // Otherwise apply usual parameter conversion rules.
175 return IsParameterTypeConvertible(from, to);
176 }
177}
178
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100179bool ConvertJValueCommon(
180 Handle<mirror::MethodType> callsite_type,
181 Handle<mirror::MethodType> callee_type,
182 ObjPtr<mirror::Class> from,
183 ObjPtr<mirror::Class> to,
184 JValue* value) {
185 // The reader maybe concerned about the safety of the heap object
186 // that may be in |value|. There is only one case where allocation
187 // is obviously needed and that's for boxing. However, in the case
188 // of boxing |value| contains a non-reference type.
189
190 const Primitive::Type from_type = from->GetPrimitiveType();
191 const Primitive::Type to_type = to->GetPrimitiveType();
192
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000193 // Put incoming value into |src_value| and set return value to 0.
194 // Errors and conversions from void require the return value to be 0.
195 const JValue src_value(*value);
196 value->SetJ(0);
197
198 // Conversion from void set result to zero.
199 if (from_type == Primitive::kPrimVoid) {
200 return true;
201 }
202
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100203 // This method must be called only when the types don't match.
204 DCHECK(from != to);
205
206 if (IsPrimitiveType(from_type) && IsPrimitiveType(to_type)) {
207 // The source and target types are both primitives.
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000208 if (UNLIKELY(!ConvertPrimitiveValueNoThrow(from_type, to_type, src_value, value))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100209 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100210 return false;
211 }
212 return true;
213 } else if (IsReferenceType(from_type) && IsReferenceType(to_type)) {
214 // They're both reference types. If "from" is null, we can pass it
215 // through unchanged. If not, we must generate a cast exception if
216 // |to| is not assignable from the dynamic type of |ref|.
217 //
218 // Playing it safe with StackHandleScope here, not expecting any allocation
219 // in mirror::Class::IsAssignable().
220 StackHandleScope<2> hs(Thread::Current());
221 Handle<mirror::Class> h_to(hs.NewHandle(to));
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000222 Handle<mirror::Object> h_obj(hs.NewHandle(src_value.GetL()));
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100223 if (h_obj.Get() != nullptr && !to->IsAssignableFrom(h_obj->GetClass())) {
224 ThrowClassCastException(h_to.Get(), h_obj->GetClass());
225 return false;
226 }
227 value->SetL(h_obj.Get());
228 return true;
229 } else if (IsReferenceType(to_type)) {
230 DCHECK(IsPrimitiveType(from_type));
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100231 // The source type is a primitive and the target type is a reference, so we must box.
232 // The target type maybe a super class of the boxed source type, for example,
233 // if the source type is int, it's boxed type is java.lang.Integer, and the target
234 // type could be java.lang.Number.
235 Primitive::Type type;
236 if (!GetUnboxedPrimitiveType(to, &type)) {
237 ObjPtr<mirror::Class> boxed_from_class = GetBoxedPrimitiveClass(from_type);
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000238 if (boxed_from_class->IsSubClass(to)) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100239 type = from_type;
240 } else {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100241 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
242 return false;
243 }
244 }
245
246 if (UNLIKELY(from_type != type)) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100247 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
248 return false;
249 }
250
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000251 if (!ConvertPrimitiveValueNoThrow(from_type, type, src_value, value)) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100252 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
253 return false;
254 }
255
256 // Then perform the actual boxing, and then set the reference.
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000257 ObjPtr<mirror::Object> boxed = BoxPrimitive(type, src_value);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100258 value->SetL(boxed.Ptr());
259 return true;
260 } else {
261 // The source type is a reference and the target type is a primitive, so we must unbox.
262 DCHECK(IsReferenceType(from_type));
263 DCHECK(IsPrimitiveType(to_type));
264
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000265 ObjPtr<mirror::Object> from_obj(src_value.GetL());
266 if (UNLIKELY(from_obj == nullptr)) {
267 ThrowNullPointerException(
268 StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
269 from->PrettyDescriptor().c_str()).c_str());
270 return false;
271 }
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100272
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000273 Primitive::Type unboxed_type;
274 JValue unboxed_value;
275 if (UNLIKELY(!GetUnboxedTypeAndValue(from_obj, &unboxed_type, &unboxed_value))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100276 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
277 return false;
278 }
279
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000280 if (UNLIKELY(!ConvertPrimitiveValueNoThrow(unboxed_type, to_type, unboxed_value, value))) {
281 ThrowClassCastException(from, to);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100282 return false;
283 }
284
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000285 return true;
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100286 }
287}
288
Orion Hodson811bd5f2016-12-07 11:35:37 +0000289namespace {
290
291template <bool is_range>
292inline void CopyArgumentsFromCallerFrame(const ShadowFrame& caller_frame,
293 ShadowFrame* callee_frame,
294 const uint32_t (&args)[Instruction::kMaxVarArgRegs],
295 uint32_t first_arg,
296 const size_t first_dst_reg,
297 const size_t num_regs)
298 REQUIRES_SHARED(Locks::mutator_lock_) {
299 for (size_t i = 0; i < num_regs; ++i) {
300 size_t dst_reg = first_dst_reg + i;
301 size_t src_reg = is_range ? (first_arg + i) : args[i];
302 // Uint required, so that sign extension does not make this wrong on 64-bit systems
303 uint32_t src_value = caller_frame.GetVReg(src_reg);
304 ObjPtr<mirror::Object> o = caller_frame.GetVRegReference<kVerifyNone>(src_reg);
305 // If both register locations contains the same value, the register probably holds a reference.
306 // Note: As an optimization, non-moving collectors leave a stale reference value
307 // in the references array even after the original vreg was overwritten to a non-reference.
308 if (src_value == reinterpret_cast<uintptr_t>(o.Ptr())) {
309 callee_frame->SetVRegReference(dst_reg, o.Ptr());
310 } else {
311 callee_frame->SetVReg(dst_reg, src_value);
312 }
313 }
314}
315
316template <bool is_range>
317inline bool ConvertAndCopyArgumentsFromCallerFrame(
318 Thread* self,
319 Handle<mirror::MethodType> callsite_type,
320 Handle<mirror::MethodType> callee_type,
321 const ShadowFrame& caller_frame,
322 const uint32_t (&args)[Instruction::kMaxVarArgRegs],
323 uint32_t first_arg,
324 uint32_t first_dst_reg,
325 ShadowFrame* callee_frame)
326 REQUIRES_SHARED(Locks::mutator_lock_) {
327 ObjPtr<mirror::ObjectArray<mirror::Class>> from_types(callsite_type->GetPTypes());
328 ObjPtr<mirror::ObjectArray<mirror::Class>> to_types(callee_type->GetPTypes());
329
330 const int32_t num_method_params = from_types->GetLength();
331 if (to_types->GetLength() != num_method_params) {
332 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
333 return false;
334 }
335
336 ShadowFrameGetter<is_range> getter(first_arg, args, caller_frame);
337 ShadowFrameSetter setter(callee_frame, first_dst_reg);
338
339 return PerformConversions<ShadowFrameGetter<is_range>, ShadowFrameSetter>(self,
340 callsite_type,
341 callee_type,
342 &getter,
343 &setter,
344 num_method_params);
345}
346
347inline bool IsMethodHandleInvokeExact(const ArtMethod* const method) {
348 if (method == jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_invokeExact)) {
349 return true;
350 } else {
351 DCHECK_EQ(method, jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_invoke));
352 return false;
353 }
354}
355
356inline bool IsInvoke(const mirror::MethodHandle::Kind handle_kind) {
357 return handle_kind <= mirror::MethodHandle::Kind::kLastInvokeKind;
358}
359
360inline bool IsInvokeTransform(const mirror::MethodHandle::Kind handle_kind) {
361 return (handle_kind == mirror::MethodHandle::Kind::kInvokeTransform
362 || handle_kind == mirror::MethodHandle::Kind::kInvokeCallSiteTransform);
363}
364
365inline bool IsFieldAccess(mirror::MethodHandle::Kind handle_kind) {
366 return (handle_kind >= mirror::MethodHandle::Kind::kFirstAccessorKind
367 && handle_kind <= mirror::MethodHandle::Kind::kLastAccessorKind);
368}
369
370// Calculate the number of ins for a proxy or native method, where we
371// can't just look at the code item.
372static inline size_t GetInsForProxyOrNativeMethod(ArtMethod* method)
373 REQUIRES_SHARED(Locks::mutator_lock_) {
374 DCHECK(method->IsNative() || method->IsProxyMethod());
375
376 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
377 size_t num_ins = 0;
378 // Separate accounting for the receiver, which isn't a part of the
379 // shorty.
380 if (!method->IsStatic()) {
381 ++num_ins;
382 }
383
384 uint32_t shorty_len = 0;
385 const char* shorty = method->GetShorty(&shorty_len);
386 for (size_t i = 1; i < shorty_len; ++i) {
387 const char c = shorty[i];
388 ++num_ins;
389 if (c == 'J' || c == 'D') {
390 ++num_ins;
391 }
392 }
393
394 return num_ins;
395}
396
397// Returns true iff. the callsite type for a polymorphic invoke is transformer
398// like, i.e that it has a single input argument whose type is
399// dalvik.system.EmulatedStackFrame.
400static inline bool IsCallerTransformer(Handle<mirror::MethodType> callsite_type)
401 REQUIRES_SHARED(Locks::mutator_lock_) {
402 ObjPtr<mirror::ObjectArray<mirror::Class>> param_types(callsite_type->GetPTypes());
403 if (param_types->GetLength() == 1) {
404 ObjPtr<mirror::Class> param(param_types->GetWithoutChecks(0));
405 return param == WellKnownClasses::ToClass(WellKnownClasses::dalvik_system_EmulatedStackFrame);
406 }
407
408 return false;
409}
410
411template <bool is_range>
412static inline bool DoCallPolymorphic(ArtMethod* called_method,
413 Handle<mirror::MethodType> callsite_type,
414 Handle<mirror::MethodType> target_type,
415 Thread* self,
416 ShadowFrame& shadow_frame,
417 const uint32_t (&args)[Instruction::kMaxVarArgRegs],
418 uint32_t first_arg,
419 JValue* result,
420 const mirror::MethodHandle::Kind handle_kind)
421 REQUIRES_SHARED(Locks::mutator_lock_) {
422 // Compute method information.
423 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
424
425 // Number of registers for the callee's call frame. Note that for non-exact
426 // invokes, we always derive this information from the callee method. We
427 // cannot guarantee during verification that the number of registers encoded
428 // in the invoke is equal to the number of ins for the callee. This is because
429 // some transformations (such as boxing a long -> Long or wideining an
430 // int -> long will change that number.
431 uint16_t num_regs;
432 size_t num_input_regs;
433 size_t first_dest_reg;
434 if (LIKELY(code_item != nullptr)) {
435 num_regs = code_item->registers_size_;
436 first_dest_reg = num_regs - code_item->ins_size_;
437 num_input_regs = code_item->ins_size_;
438 // Parameter registers go at the end of the shadow frame.
439 DCHECK_NE(first_dest_reg, (size_t)-1);
440 } else {
441 // No local regs for proxy and native methods.
442 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
443 num_regs = num_input_regs = GetInsForProxyOrNativeMethod(called_method);
444 first_dest_reg = 0;
445 }
446
447 // Allocate shadow frame on the stack.
448 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
449 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
450 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
451
452 // Whether this polymorphic invoke was issued by a transformer method.
453 bool is_caller_transformer = false;
454 // Thread might be suspended during PerformArgumentConversions due to the
455 // allocations performed during boxing.
456 {
457 ScopedStackedShadowFramePusher pusher(
458 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
459 if (callsite_type->IsExactMatch(target_type.Get())) {
460 // This is an exact invoke, we can take the fast path of just copying all
461 // registers without performing any argument conversions.
462 CopyArgumentsFromCallerFrame<is_range>(shadow_frame,
463 new_shadow_frame,
464 args,
465 first_arg,
466 first_dest_reg,
467 num_input_regs);
468 } 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.
478 size_t first_arg_register = (is_range) ? first_arg : args[0];
479 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 }
490 } else if (!ConvertAndCopyArgumentsFromCallerFrame<is_range>(self,
491 callsite_type,
492 target_type,
493 shadow_frame,
494 args,
495 first_arg,
496 first_dest_reg,
497 new_shadow_frame)) {
498 DCHECK(self->IsExceptionPending());
499 result->SetL(0);
500 return false;
501 }
502 }
503 }
504
505 // See TODO in DoInvokePolymorphic : We need to perform this dynamic, receiver
506 // based dispatch right before we perform the actual call, because the
507 // receiver isn't known very early.
508 if (handle_kind == mirror::MethodHandle::Kind::kInvokeVirtual ||
509 handle_kind == mirror::MethodHandle::Kind::kInvokeInterface) {
510 ObjPtr<mirror::Object> receiver(new_shadow_frame->GetVRegReference(first_dest_reg));
511 ObjPtr<mirror::Class> declaring_class(called_method->GetDeclaringClass());
512 // Verify that _vRegC is an object reference and of the type expected by
513 // the receiver.
514 if (!VerifyObjectIsClass(receiver, declaring_class)) {
515 DCHECK(self->IsExceptionPending());
516 return false;
517 }
518
519 called_method = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(
520 called_method, kRuntimePointerSize);
521 }
522
523 PerformCall(self, code_item, shadow_frame.GetMethod(), first_dest_reg, new_shadow_frame, result);
524 if (self->IsExceptionPending()) {
525 return false;
526 }
527
528 // If the caller of this signature polymorphic method was a transformer,
529 // we need to copy the result back out to the emulated stack frame.
530 if (is_caller_transformer) {
531 StackHandleScope<2> hs(self);
532 size_t first_callee_register = is_range ? (first_arg) : args[0];
533 Handle<mirror::EmulatedStackFrame> emulated_stack_frame(
534 hs.NewHandle(reinterpret_cast<mirror::EmulatedStackFrame*>(
535 shadow_frame.GetVRegReference(first_callee_register))));
536 Handle<mirror::MethodType> emulated_stack_type(hs.NewHandle(emulated_stack_frame->GetType()));
537 JValue local_result;
538 local_result.SetJ(result->GetJ());
539
540 if (ConvertReturnValue(emulated_stack_type, target_type, &local_result)) {
541 emulated_stack_frame->SetReturnValue(self, local_result);
542 return true;
543 } else {
544 DCHECK(self->IsExceptionPending());
545 return false;
546 }
547 } else {
548 return ConvertReturnValue(callsite_type, target_type, result);
549 }
550}
551
552template <bool is_range>
553static inline bool DoCallTransform(ArtMethod* called_method,
554 Handle<mirror::MethodType> callsite_type,
555 Handle<mirror::MethodType> callee_type,
556 Thread* self,
557 ShadowFrame& shadow_frame,
558 Handle<mirror::MethodHandleImpl> receiver,
559 const uint32_t (&args)[Instruction::kMaxVarArgRegs],
560 uint32_t first_arg,
561 JValue* result)
562 REQUIRES_SHARED(Locks::mutator_lock_) {
563 // This can be fixed to two, because the method we're calling here
564 // (MethodHandle.transformInternal) doesn't have any locals and the signature
565 // is known :
566 //
567 // private MethodHandle.transformInternal(EmulatedStackFrame sf);
568 //
569 // This means we need only two vregs :
570 // - One for the receiver object.
571 // - One for the only method argument (an EmulatedStackFrame).
572 static constexpr size_t kNumRegsForTransform = 2;
573
574 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
575 DCHECK(code_item != nullptr);
576 DCHECK_EQ(kNumRegsForTransform, code_item->registers_size_);
577 DCHECK_EQ(kNumRegsForTransform, code_item->ins_size_);
578
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.
589 size_t first_callee_register = is_range ? first_arg : args[0];
590 sf.Assign(reinterpret_cast<mirror::EmulatedStackFrame*>(
591 shadow_frame.GetVRegReference(first_callee_register)));
592 } else {
593 sf.Assign(mirror::EmulatedStackFrame::CreateFromShadowFrameAndArgs<is_range>(self,
594 callsite_type,
595 callee_type,
596 shadow_frame,
597 first_arg,
598 args));
599
600 // Something went wrong while creating the emulated stack frame, we should
601 // throw the pending exception.
602 if (sf.Get() == nullptr) {
603 DCHECK(self->IsExceptionPending());
604 return false;
605 }
606 }
607
608 new_shadow_frame->SetVRegReference(0, receiver.Get());
609 new_shadow_frame->SetVRegReference(1, sf.Get());
610
611 PerformCall(self,
612 code_item,
613 shadow_frame.GetMethod(),
614 0 /* first destination register */,
615 new_shadow_frame,
616 result);
617 if (self->IsExceptionPending()) {
618 return false;
619 }
620
621 // If the called transformer method we called has returned a value, then we
622 // need to copy it back to |result|.
623 sf->GetReturnValue(self, result);
624 return ConvertReturnValue(callsite_type, callee_type, result);
625}
626
627inline static ObjPtr<mirror::Class> GetAndInitializeDeclaringClass(Thread* self, ArtField* field)
628 REQUIRES_SHARED(Locks::mutator_lock_) {
629 // Method handle invocations on static fields should ensure class is
630 // initialized. This usually happens when an instance is constructed
631 // or class members referenced, but this is not guaranteed when
632 // looking up method handles.
633 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
634 if (UNLIKELY(!klass->IsInitialized())) {
635 StackHandleScope<1> hs(self);
636 HandleWrapperObjPtr<mirror::Class> h(hs.NewHandleWrapper(&klass));
637 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h, true, true)) {
638 DCHECK(self->IsExceptionPending());
639 return nullptr;
640 }
641 }
642 return klass;
643}
644
645template <bool is_range>
646bool DoInvokePolymorphicUnchecked(Thread* self,
647 ShadowFrame& shadow_frame,
648 Handle<mirror::MethodHandleImpl> method_handle,
649 Handle<mirror::MethodType> callsite_type,
650 const uint32_t (&args)[Instruction::kMaxVarArgRegs],
651 uint32_t first_arg,
652 JValue* result)
653 REQUIRES_SHARED(Locks::mutator_lock_) {
654 StackHandleScope<1> hs(self);
655 Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType()));
656 const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind();
657 if (IsInvoke(handle_kind)) {
658 // Get the method we're actually invoking along with the kind of
659 // invoke that is desired. We don't need to perform access checks at this
660 // point because they would have been performed on our behalf at the point
661 // of creation of the method handle.
662 ArtMethod* called_method = method_handle->GetTargetMethod();
663 CHECK(called_method != nullptr);
664
665 if (handle_kind == mirror::MethodHandle::Kind::kInvokeVirtual ||
666 handle_kind == mirror::MethodHandle::Kind::kInvokeInterface) {
667 // TODO: Unfortunately, we have to postpone dynamic receiver based checks
668 // because the receiver might be cast or might come from an emulated stack
669 // frame, which means that it is unknown at this point. We perform these
670 // checks inside DoCallPolymorphic right before we do the actual invoke.
671 } else if (handle_kind == mirror::MethodHandle::Kind::kInvokeDirect) {
672 // String constructors are a special case, they are replaced with StringFactory
673 // methods.
674 if (called_method->IsConstructor() && called_method->GetDeclaringClass()->IsStringClass()) {
675 DCHECK(handle_type->GetRType()->IsStringClass());
676 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
677 }
678 } else if (handle_kind == mirror::MethodHandle::Kind::kInvokeSuper) {
679 ObjPtr<mirror::Class> declaring_class = called_method->GetDeclaringClass();
680
681 // Note that we're not dynamically dispatching on the type of the receiver
682 // here. We use the static type of the "receiver" object that we've
683 // recorded in the method handle's type, which will be the same as the
684 // special caller that was specified at the point of lookup.
685 ObjPtr<mirror::Class> referrer_class = handle_type->GetPTypes()->Get(0);
686 if (!declaring_class->IsInterface()) {
687 ObjPtr<mirror::Class> super_class = referrer_class->GetSuperClass();
688 uint16_t vtable_index = called_method->GetMethodIndex();
689 DCHECK(super_class != nullptr);
690 DCHECK(super_class->HasVTable());
691 // Note that super_class is a super of referrer_class and called_method
692 // will always be declared by super_class (or one of its super classes).
693 DCHECK_LT(vtable_index, super_class->GetVTableLength());
694 called_method = super_class->GetVTableEntry(vtable_index, kRuntimePointerSize);
695 } else {
696 called_method = referrer_class->FindVirtualMethodForInterfaceSuper(
697 called_method, kRuntimePointerSize);
698 }
699 CHECK(called_method != nullptr);
700 }
701 if (IsInvokeTransform(handle_kind)) {
702 // There are two cases here - method handles representing regular
703 // transforms and those representing call site transforms. Method
704 // handles for call site transforms adapt their MethodType to match
705 // the call site. For these, the |callee_type| is the same as the
706 // |callsite_type|. The VarargsCollector is such a tranform, its
707 // method type depends on the call site, ie. x(a) or x(a, b), or
708 // x(a, b, c). The VarargsCollector invokes a variable arity method
709 // with the arity arguments in an array.
710 Handle<mirror::MethodType> callee_type =
711 (handle_kind == mirror::MethodHandle::Kind::kInvokeCallSiteTransform) ? callsite_type
712 : handle_type;
713 return DoCallTransform<is_range>(called_method,
714 callsite_type,
715 callee_type,
716 self,
717 shadow_frame,
718 method_handle /* receiver */,
719 args,
720 first_arg,
721 result);
722
723 } else {
724 return DoCallPolymorphic<is_range>(called_method,
725 callsite_type,
726 handle_type,
727 self,
728 shadow_frame,
729 args,
730 first_arg,
731 result,
732 handle_kind);
733 }
734 } else {
735 LOG(FATAL) << "Unreachable: " << handle_kind;
736 UNREACHABLE();
737 }
738}
739
740// Helper for getters in invoke-polymorphic.
741inline static void DoFieldGetForInvokePolymorphic(Thread* self,
742 const ShadowFrame& shadow_frame,
743 ObjPtr<mirror::Object>& obj,
744 ArtField* field,
745 Primitive::Type field_type,
746 JValue* result)
747 REQUIRES_SHARED(Locks::mutator_lock_) {
748 switch (field_type) {
749 case Primitive::kPrimBoolean:
750 DoFieldGetCommon<Primitive::kPrimBoolean>(self, shadow_frame, obj, field, result);
751 break;
752 case Primitive::kPrimByte:
753 DoFieldGetCommon<Primitive::kPrimByte>(self, shadow_frame, obj, field, result);
754 break;
755 case Primitive::kPrimChar:
756 DoFieldGetCommon<Primitive::kPrimChar>(self, shadow_frame, obj, field, result);
757 break;
758 case Primitive::kPrimShort:
759 DoFieldGetCommon<Primitive::kPrimShort>(self, shadow_frame, obj, field, result);
760 break;
761 case Primitive::kPrimInt:
762 DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result);
763 break;
764 case Primitive::kPrimLong:
765 DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result);
766 break;
767 case Primitive::kPrimFloat:
768 DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result);
769 break;
770 case Primitive::kPrimDouble:
771 DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result);
772 break;
773 case Primitive::kPrimNot:
774 DoFieldGetCommon<Primitive::kPrimNot>(self, shadow_frame, obj, field, result);
775 break;
776 case Primitive::kPrimVoid:
777 LOG(FATAL) << "Unreachable: " << field_type;
778 UNREACHABLE();
779 }
780}
781
782// Helper for setters in invoke-polymorphic.
783template <bool do_assignability_check>
784inline bool DoFieldPutForInvokePolymorphic(Thread* self,
785 ShadowFrame& shadow_frame,
786 ObjPtr<mirror::Object>& obj,
787 ArtField* field,
788 Primitive::Type field_type,
789 const JValue& value)
790 REQUIRES_SHARED(Locks::mutator_lock_) {
791 static const bool kTransaction = false;
792 switch (field_type) {
793 case Primitive::kPrimBoolean:
794 return DoFieldPutCommon<Primitive::kPrimBoolean, do_assignability_check, kTransaction>(
795 self, shadow_frame, obj, field, value);
796 case Primitive::kPrimByte:
797 return DoFieldPutCommon<Primitive::kPrimByte, do_assignability_check, kTransaction>(
798 self, shadow_frame, obj, field, value);
799 case Primitive::kPrimChar:
800 return DoFieldPutCommon<Primitive::kPrimChar, do_assignability_check, kTransaction>(
801 self, shadow_frame, obj, field, value);
802 case Primitive::kPrimShort:
803 return DoFieldPutCommon<Primitive::kPrimShort, do_assignability_check, kTransaction>(
804 self, shadow_frame, obj, field, value);
805 case Primitive::kPrimInt:
806 case Primitive::kPrimFloat:
807 return DoFieldPutCommon<Primitive::kPrimInt, do_assignability_check, kTransaction>(
808 self, shadow_frame, obj, field, value);
809 case Primitive::kPrimLong:
810 case Primitive::kPrimDouble:
811 return DoFieldPutCommon<Primitive::kPrimLong, do_assignability_check, kTransaction>(
812 self, shadow_frame, obj, field, value);
813 case Primitive::kPrimNot:
814 return DoFieldPutCommon<Primitive::kPrimNot, do_assignability_check, kTransaction>(
815 self, shadow_frame, obj, field, value);
816 case Primitive::kPrimVoid:
817 LOG(FATAL) << "Unreachable: " << field_type;
818 UNREACHABLE();
819 }
820}
821
822static JValue GetValueFromShadowFrame(const ShadowFrame& shadow_frame,
823 Primitive::Type field_type,
824 uint32_t vreg)
825 REQUIRES_SHARED(Locks::mutator_lock_) {
826 JValue field_value;
827 switch (field_type) {
828 case Primitive::kPrimBoolean:
829 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
830 break;
831 case Primitive::kPrimByte:
832 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
833 break;
834 case Primitive::kPrimChar:
835 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
836 break;
837 case Primitive::kPrimShort:
838 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
839 break;
840 case Primitive::kPrimInt:
841 case Primitive::kPrimFloat:
842 field_value.SetI(shadow_frame.GetVReg(vreg));
843 break;
844 case Primitive::kPrimLong:
845 case Primitive::kPrimDouble:
846 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
847 break;
848 case Primitive::kPrimNot:
849 field_value.SetL(shadow_frame.GetVRegReference(vreg));
850 break;
851 case Primitive::kPrimVoid:
852 LOG(FATAL) << "Unreachable: " << field_type;
853 UNREACHABLE();
854 }
855 return field_value;
856}
857
858template <bool is_range, bool do_conversions, bool do_assignability_check>
859bool DoInvokePolymorphicFieldAccess(Thread* self,
860 ShadowFrame& shadow_frame,
861 Handle<mirror::MethodHandleImpl> method_handle,
862 Handle<mirror::MethodType> callsite_type,
863 const uint32_t (&args)[Instruction::kMaxVarArgRegs],
864 uint32_t first_arg,
865 JValue* result)
866 REQUIRES_SHARED(Locks::mutator_lock_) {
867 StackHandleScope<1> hs(self);
868 Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType()));
869 const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind();
870 ArtField* field = method_handle->GetTargetField();
871 Primitive::Type field_type = field->GetTypeAsPrimitiveType();
872
873 switch (handle_kind) {
874 case mirror::MethodHandle::kInstanceGet: {
875 size_t obj_reg = is_range ? first_arg : args[0];
876 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(obj_reg);
877 DoFieldGetForInvokePolymorphic(self, shadow_frame, obj, field, field_type, result);
878 if (do_conversions && !ConvertReturnValue(callsite_type, handle_type, result)) {
879 DCHECK(self->IsExceptionPending());
880 return false;
881 }
882 return true;
883 }
884 case mirror::MethodHandle::kStaticGet: {
885 ObjPtr<mirror::Object> obj = GetAndInitializeDeclaringClass(self, field);
886 if (obj == nullptr) {
887 DCHECK(self->IsExceptionPending());
888 return false;
889 }
890 DoFieldGetForInvokePolymorphic(self, shadow_frame, obj, field, field_type, result);
891 if (do_conversions && !ConvertReturnValue(callsite_type, handle_type, result)) {
892 DCHECK(self->IsExceptionPending());
893 return false;
894 }
895 return true;
896 }
897 case mirror::MethodHandle::kInstancePut: {
898 size_t obj_reg = is_range ? first_arg : args[0];
899 size_t value_reg = is_range ? (first_arg + 1) : args[1];
900 JValue value = GetValueFromShadowFrame(shadow_frame, field_type, value_reg);
901 if (do_conversions && !ConvertArgumentValue(callsite_type, handle_type, 1, &value)) {
902 DCHECK(self->IsExceptionPending());
903 return false;
904 }
905 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(obj_reg);
906 return DoFieldPutForInvokePolymorphic<do_assignability_check>(self,
907 shadow_frame,
908 obj,
909 field,
910 field_type,
911 value);
912 }
913 case mirror::MethodHandle::kStaticPut: {
914 ObjPtr<mirror::Object> obj = GetAndInitializeDeclaringClass(self, field);
915 if (obj == nullptr) {
916 DCHECK(self->IsExceptionPending());
917 return false;
918 }
919 size_t value_reg = is_range ? first_arg : args[0];
920 JValue value = GetValueFromShadowFrame(shadow_frame, field_type, value_reg);
921 if (do_conversions && !ConvertArgumentValue(callsite_type, handle_type, 0, &value)) {
922 DCHECK(self->IsExceptionPending());
923 return false;
924 }
925 return DoFieldPutForInvokePolymorphic<do_assignability_check>(self,
926 shadow_frame,
927 obj,
928 field,
929 field_type,
930 value);
931 }
932 default:
933 LOG(FATAL) << "Unreachable: " << handle_kind;
934 UNREACHABLE();
935 }
936}
937
938template <bool is_range, bool do_assignability_check>
939static inline bool DoInvokePolymorphicNonExact(Thread* self,
940 ShadowFrame& shadow_frame,
941 Handle<mirror::MethodHandleImpl> method_handle,
942 Handle<mirror::MethodType> callsite_type,
943 const uint32_t (&args)[Instruction::kMaxVarArgRegs],
944 uint32_t first_arg,
945 JValue* result)
946 REQUIRES_SHARED(Locks::mutator_lock_) {
947 const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind();
948 ObjPtr<mirror::MethodType> handle_type(method_handle->GetMethodType());
949 CHECK(handle_type != nullptr);
950
951 if (!IsInvokeTransform(handle_kind)) {
952 if (UNLIKELY(!IsCallerTransformer(callsite_type) &&
953 !callsite_type->IsConvertible(handle_type.Ptr()))) {
954 ThrowWrongMethodTypeException(handle_type.Ptr(), callsite_type.Get());
955 return false;
956 }
957 }
958
959 if (IsFieldAccess(handle_kind)) {
960 if (UNLIKELY(callsite_type->IsExactMatch(handle_type.Ptr()))) {
961 const bool do_convert = false;
962 return DoInvokePolymorphicFieldAccess<is_range, do_convert, do_assignability_check>(
963 self,
964 shadow_frame,
965 method_handle,
966 callsite_type,
967 args,
968 first_arg,
969 result);
970 } else {
971 const bool do_convert = true;
972 return DoInvokePolymorphicFieldAccess<is_range, do_convert, do_assignability_check>(
973 self,
974 shadow_frame,
975 method_handle,
976 callsite_type,
977 args,
978 first_arg,
979 result);
980 }
981 }
982
983 if (UNLIKELY(callsite_type->IsExactMatch(handle_type.Ptr()))) {
984 return DoInvokePolymorphicUnchecked<is_range>(self,
985 shadow_frame,
986 method_handle,
987 callsite_type,
988 args,
989 first_arg,
990 result);
991 } else {
992 return DoInvokePolymorphicUnchecked<is_range>(self,
993 shadow_frame,
994 method_handle,
995 callsite_type,
996 args,
997 first_arg,
998 result);
999 }
1000}
1001
1002template <bool is_range, bool do_assignability_check>
1003bool DoInvokePolymorphicExact(Thread* self,
1004 ShadowFrame& shadow_frame,
1005 Handle<mirror::MethodHandleImpl> method_handle,
1006 Handle<mirror::MethodType> callsite_type,
1007 const uint32_t (&args)[Instruction::kMaxVarArgRegs],
1008 uint32_t first_arg,
1009 JValue* result)
1010 REQUIRES_SHARED(Locks::mutator_lock_) {
1011 // We need to check the nominal type of the handle in addition to the
1012 // real type. The "nominal" type is present when MethodHandle.asType is
1013 // called any handle, and results in the declared type of the handle
1014 // changing.
1015 ObjPtr<mirror::MethodType> nominal_type(method_handle->GetNominalType());
1016 if (UNLIKELY(nominal_type != nullptr)) {
1017 if (UNLIKELY(!callsite_type->IsExactMatch(nominal_type.Ptr()))) {
1018 ThrowWrongMethodTypeException(nominal_type.Ptr(), callsite_type.Get());
1019 return false;
1020 }
1021 return DoInvokePolymorphicNonExact<is_range, do_assignability_check>(self,
1022 shadow_frame,
1023 method_handle,
1024 callsite_type,
1025 args,
1026 first_arg,
1027 result);
1028 }
1029
1030 ObjPtr<mirror::MethodType> handle_type(method_handle->GetMethodType());
1031 if (UNLIKELY(!callsite_type->IsExactMatch(handle_type.Ptr()))) {
1032 ThrowWrongMethodTypeException(handle_type.Ptr(), callsite_type.Get());
1033 return false;
1034 }
1035
1036 const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind();
1037 if (IsFieldAccess(handle_kind)) {
1038 const bool do_convert = false;
1039 return DoInvokePolymorphicFieldAccess<is_range, do_convert, do_assignability_check>(
1040 self,
1041 shadow_frame,
1042 method_handle,
1043 callsite_type,
1044 args,
1045 first_arg,
1046 result);
1047 }
1048
1049 return DoInvokePolymorphicUnchecked<is_range>(self,
1050 shadow_frame,
1051 method_handle,
1052 callsite_type,
1053 args,
1054 first_arg,
1055 result);
1056}
1057
1058} // namespace
1059
1060template <bool is_range, bool do_assignability_check>
1061bool DoInvokePolymorphic(Thread* self,
1062 ArtMethod* invoke_method,
1063 ShadowFrame& shadow_frame,
1064 Handle<mirror::MethodHandleImpl> method_handle,
1065 Handle<mirror::MethodType> callsite_type,
1066 const uint32_t (&args)[Instruction::kMaxVarArgRegs],
1067 uint32_t first_arg,
1068 JValue* result)
1069 REQUIRES_SHARED(Locks::mutator_lock_) {
1070 if (IsMethodHandleInvokeExact(invoke_method)) {
1071 return DoInvokePolymorphicExact<is_range, do_assignability_check>(self,
1072 shadow_frame,
1073 method_handle,
1074 callsite_type,
1075 args,
1076 first_arg,
1077 result);
1078 } else {
1079 return DoInvokePolymorphicNonExact<is_range, do_assignability_check>(self,
1080 shadow_frame,
1081 method_handle,
1082 callsite_type,
1083 args,
1084 first_arg,
1085 result);
1086 }
1087}
1088
1089#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range, _do_assignability_check) \
1090template REQUIRES_SHARED(Locks::mutator_lock_) \
1091bool DoInvokePolymorphic<_is_range, _do_assignability_check>( \
1092 Thread* self, \
1093 ArtMethod* invoke_method, \
1094 ShadowFrame& shadow_frame, \
1095 Handle<mirror::MethodHandleImpl> method_handle, \
1096 Handle<mirror::MethodType> callsite_type, \
1097 const uint32_t (&args)[Instruction::kMaxVarArgRegs], \
1098 uint32_t first_arg, \
1099 JValue* result)
1100
1101EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, true);
1102EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, false);
1103EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, true);
1104EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, false);
1105#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1106
Orion Hodsonba28f9f2016-10-26 10:56:25 +01001107} // namespace art