blob: 19f43f97a9864c4053e07b2c45dd5e1f5a40495a [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
Orion Hodson3b1b2162021-11-24 08:54:16 +000019#include "android-base/macros.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080020#include "android-base/stringprintf.h"
Vladimir Marko5868ada2020-05-12 11:50:34 +010021#include "class_root-inl.h"
Orion Hodson811bd5f2016-12-07 11:35:37 +000022#include "common_dex_operations.h"
Alex Light49df7152019-10-03 11:22:35 -070023#include "common_throws.h"
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +010024#include "interpreter/shadow_frame-inl.h"
Orion Hodson3b1b2162021-11-24 08:54:16 +000025#include "interpreter/shadow_frame.h"
Orion Hodsonba28f9f2016-10-26 10:56:25 +010026#include "jvalue-inl.h"
Andreas Gampe88dbad32018-06-26 19:54:12 -070027#include "mirror/class-inl.h"
Vladimir Marko423bebb2019-03-26 15:17:21 +000028#include "mirror/emulated_stack_frame-inl.h"
Orion Hodson3b1b2162021-11-24 08:54:16 +000029#include "mirror/emulated_stack_frame.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080030#include "mirror/method_handle_impl-inl.h"
Orion Hodson3b1b2162021-11-24 08:54:16 +000031#include "mirror/method_handle_impl.h"
Vladimir Marko5aead702019-03-27 11:00:36 +000032#include "mirror/method_type-inl.h"
Orion Hodsonb8b93872018-01-30 07:51:10 +000033#include "mirror/var_handle.h"
Orion Hodsonba28f9f2016-10-26 10:56:25 +010034#include "reflection-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "reflection.h"
Orion Hodson3b1b2162021-11-24 08:54:16 +000036#include "thread.h"
Orion Hodson1a06f9f2016-11-09 08:32:42 +000037#include "well_known_classes.h"
Orion Hodsonba28f9f2016-10-26 10:56:25 +010038
39namespace art {
40
Andreas Gampe46ee31b2016-12-14 10:11:49 -080041using android::base::StringPrintf;
42
Orion Hodsonba28f9f2016-10-26 10:56:25 +010043namespace {
44
Orion Hodson1a06f9f2016-11-09 08:32:42 +000045#define PRIMITIVES_LIST(V) \
46 V(Primitive::kPrimBoolean, Boolean, Boolean, Z) \
47 V(Primitive::kPrimByte, Byte, Byte, B) \
48 V(Primitive::kPrimChar, Char, Character, C) \
49 V(Primitive::kPrimShort, Short, Short, S) \
50 V(Primitive::kPrimInt, Int, Integer, I) \
51 V(Primitive::kPrimLong, Long, Long, J) \
52 V(Primitive::kPrimFloat, Float, Float, F) \
53 V(Primitive::kPrimDouble, Double, Double, D)
Orion Hodsonba28f9f2016-10-26 10:56:25 +010054
55// Assigns |type| to the primitive type associated with |klass|. Returns
56// true iff. |klass| was a boxed type (Integer, Long etc.), false otherwise.
57bool GetUnboxedPrimitiveType(ObjPtr<mirror::Class> klass, Primitive::Type* type)
58 REQUIRES_SHARED(Locks::mutator_lock_) {
59 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Orion Hodsona1be7132017-03-21 10:04:12 +000060 std::string storage;
61 const char* descriptor = klass->GetDescriptor(&storage);
62 static const char kJavaLangPrefix[] = "Ljava/lang/";
63 static const size_t kJavaLangPrefixSize = sizeof(kJavaLangPrefix) - 1;
64 if (strncmp(descriptor, kJavaLangPrefix, kJavaLangPrefixSize) != 0) {
65 return false;
66 }
67
68 descriptor += kJavaLangPrefixSize;
69#define LOOKUP_PRIMITIVE(primitive, _, java_name, ___) \
70 if (strcmp(descriptor, #java_name ";") == 0) { \
71 *type = primitive; \
72 return true; \
Orion Hodsonba28f9f2016-10-26 10:56:25 +010073 }
Orion Hodsonba28f9f2016-10-26 10:56:25 +010074
Orion Hodson1a06f9f2016-11-09 08:32:42 +000075 PRIMITIVES_LIST(LOOKUP_PRIMITIVE);
76#undef LOOKUP_PRIMITIVE
Orion Hodsonba28f9f2016-10-26 10:56:25 +010077 return false;
78}
79
Orion Hodson1a06f9f2016-11-09 08:32:42 +000080ObjPtr<mirror::Class> GetBoxedPrimitiveClass(Primitive::Type type)
Orion Hodsonba28f9f2016-10-26 10:56:25 +010081 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson1a06f9f2016-11-09 08:32:42 +000082 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
83 jmethodID m = nullptr;
84 switch (type) {
85#define CASE_PRIMITIVE(primitive, _, java_name, __) \
86 case primitive: \
87 m = WellKnownClasses::java_lang_ ## java_name ## _valueOf; \
88 break;
89 PRIMITIVES_LIST(CASE_PRIMITIVE);
90#undef CASE_PRIMITIVE
91 case Primitive::Type::kPrimNot:
92 case Primitive::Type::kPrimVoid:
93 return nullptr;
Orion Hodsonba28f9f2016-10-26 10:56:25 +010094 }
Orion Hodson1a06f9f2016-11-09 08:32:42 +000095 return jni::DecodeArtMethod(m)->GetDeclaringClass();
96}
Orion Hodsonba28f9f2016-10-26 10:56:25 +010097
Orion Hodson1a06f9f2016-11-09 08:32:42 +000098bool GetUnboxedTypeAndValue(ObjPtr<mirror::Object> o, Primitive::Type* type, JValue* value)
99 REQUIRES_SHARED(Locks::mutator_lock_) {
100 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100101 ObjPtr<mirror::Class> klass = o->GetClass();
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100102 ArtField* primitive_field = &klass->GetIFieldsPtr()->At(0);
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000103#define CASE_PRIMITIVE(primitive, abbrev, _, shorthand) \
104 if (klass == GetBoxedPrimitiveClass(primitive)) { \
105 *type = primitive; \
106 value->Set ## shorthand(primitive_field->Get ## abbrev(o)); \
107 return true; \
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100108 }
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000109 PRIMITIVES_LIST(CASE_PRIMITIVE)
110#undef CASE_PRIMITIVE
111 return false;
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100112}
113
114inline bool IsReferenceType(Primitive::Type type) {
115 return type == Primitive::kPrimNot;
116}
117
118inline bool IsPrimitiveType(Primitive::Type type) {
119 return !IsReferenceType(type);
120}
121
122} // namespace
123
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000124bool IsParameterTypeConvertible(ObjPtr<mirror::Class> from, ObjPtr<mirror::Class> to)
125 REQUIRES_SHARED(Locks::mutator_lock_) {
126 // This function returns true if there's any conceivable conversion
127 // between |from| and |to|. It's expected this method will be used
128 // to determine if a WrongMethodTypeException should be raised. The
129 // decision logic follows the documentation for MethodType.asType().
130 if (from == to) {
131 return true;
132 }
133
134 Primitive::Type from_primitive = from->GetPrimitiveType();
135 Primitive::Type to_primitive = to->GetPrimitiveType();
136 DCHECK(from_primitive != Primitive::Type::kPrimVoid);
137 DCHECK(to_primitive != Primitive::Type::kPrimVoid);
138
139 // If |to| and |from| are references.
140 if (IsReferenceType(from_primitive) && IsReferenceType(to_primitive)) {
141 // Assignability is determined during parameter conversion when
142 // invoking the associated method handle.
143 return true;
144 }
145
146 // If |to| and |from| are primitives and a widening conversion exists.
147 if (Primitive::IsWidenable(from_primitive, to_primitive)) {
148 return true;
149 }
150
151 // If |to| is a reference and |from| is a primitive, then boxing conversion.
152 if (IsReferenceType(to_primitive) && IsPrimitiveType(from_primitive)) {
153 return to->IsAssignableFrom(GetBoxedPrimitiveClass(from_primitive));
154 }
155
156 // If |from| is a reference and |to| is a primitive, then unboxing conversion.
157 if (IsPrimitiveType(to_primitive) && IsReferenceType(from_primitive)) {
158 if (from->DescriptorEquals("Ljava/lang/Object;")) {
159 // Object might be converted into a primitive during unboxing.
160 return true;
Orion Hodsona1be7132017-03-21 10:04:12 +0000161 }
162
163 if (Primitive::IsNumericType(to_primitive) && from->DescriptorEquals("Ljava/lang/Number;")) {
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000164 // Number might be unboxed into any of the number primitive types.
165 return true;
166 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000167
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000168 Primitive::Type unboxed_type;
169 if (GetUnboxedPrimitiveType(from, &unboxed_type)) {
Orion Hodsonf1412b42016-11-11 12:03:29 +0000170 if (unboxed_type == to_primitive) {
171 // Straightforward unboxing conversion such as Boolean => boolean.
172 return true;
Orion Hodsonf1412b42016-11-11 12:03:29 +0000173 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000174
175 // Check if widening operations for numeric primitives would work,
176 // such as Byte => byte => long.
177 return Primitive::IsWidenable(unboxed_type, to_primitive);
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000178 }
179 }
180
181 return false;
182}
183
184bool IsReturnTypeConvertible(ObjPtr<mirror::Class> from, ObjPtr<mirror::Class> to)
185 REQUIRES_SHARED(Locks::mutator_lock_) {
186 if (to->GetPrimitiveType() == Primitive::Type::kPrimVoid) {
187 // Result will be ignored.
188 return true;
189 } else if (from->GetPrimitiveType() == Primitive::Type::kPrimVoid) {
190 // Returned value will be 0 / null.
191 return true;
192 } else {
193 // Otherwise apply usual parameter conversion rules.
194 return IsParameterTypeConvertible(from, to);
195 }
196}
197
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100198bool ConvertJValueCommon(
199 Handle<mirror::MethodType> callsite_type,
200 Handle<mirror::MethodType> callee_type,
201 ObjPtr<mirror::Class> from,
202 ObjPtr<mirror::Class> to,
203 JValue* value) {
204 // The reader maybe concerned about the safety of the heap object
205 // that may be in |value|. There is only one case where allocation
206 // is obviously needed and that's for boxing. However, in the case
207 // of boxing |value| contains a non-reference type.
208
209 const Primitive::Type from_type = from->GetPrimitiveType();
210 const Primitive::Type to_type = to->GetPrimitiveType();
211
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000212 // Put incoming value into |src_value| and set return value to 0.
213 // Errors and conversions from void require the return value to be 0.
214 const JValue src_value(*value);
215 value->SetJ(0);
216
217 // Conversion from void set result to zero.
218 if (from_type == Primitive::kPrimVoid) {
219 return true;
220 }
221
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100222 // This method must be called only when the types don't match.
223 DCHECK(from != to);
224
225 if (IsPrimitiveType(from_type) && IsPrimitiveType(to_type)) {
226 // The source and target types are both primitives.
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000227 if (UNLIKELY(!ConvertPrimitiveValueNoThrow(from_type, to_type, src_value, value))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100228 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100229 return false;
230 }
231 return true;
232 } else if (IsReferenceType(from_type) && IsReferenceType(to_type)) {
233 // They're both reference types. If "from" is null, we can pass it
234 // through unchanged. If not, we must generate a cast exception if
235 // |to| is not assignable from the dynamic type of |ref|.
236 //
237 // Playing it safe with StackHandleScope here, not expecting any allocation
238 // in mirror::Class::IsAssignable().
239 StackHandleScope<2> hs(Thread::Current());
240 Handle<mirror::Class> h_to(hs.NewHandle(to));
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000241 Handle<mirror::Object> h_obj(hs.NewHandle(src_value.GetL()));
Orion Hodsonc1d3bac2018-01-26 14:38:55 +0000242 if (UNLIKELY(!h_obj.IsNull() && !to->IsAssignableFrom(h_obj->GetClass()))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100243 ThrowClassCastException(h_to.Get(), h_obj->GetClass());
244 return false;
245 }
246 value->SetL(h_obj.Get());
247 return true;
248 } else if (IsReferenceType(to_type)) {
249 DCHECK(IsPrimitiveType(from_type));
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100250 // The source type is a primitive and the target type is a reference, so we must box.
251 // The target type maybe a super class of the boxed source type, for example,
252 // if the source type is int, it's boxed type is java.lang.Integer, and the target
253 // type could be java.lang.Number.
254 Primitive::Type type;
255 if (!GetUnboxedPrimitiveType(to, &type)) {
256 ObjPtr<mirror::Class> boxed_from_class = GetBoxedPrimitiveClass(from_type);
Orion Hodsonc1d3bac2018-01-26 14:38:55 +0000257 if (LIKELY(boxed_from_class->IsSubClass(to))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100258 type = from_type;
259 } else {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100260 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
261 return false;
262 }
263 }
264
265 if (UNLIKELY(from_type != type)) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100266 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
267 return false;
268 }
269
Orion Hodsonc1d3bac2018-01-26 14:38:55 +0000270 if (UNLIKELY(!ConvertPrimitiveValueNoThrow(from_type, type, src_value, value))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100271 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
272 return false;
273 }
274
275 // Then perform the actual boxing, and then set the reference.
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000276 ObjPtr<mirror::Object> boxed = BoxPrimitive(type, src_value);
Vladimir Markobcf17522018-06-01 13:14:32 +0100277 value->SetL(boxed);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100278 return true;
279 } else {
280 // The source type is a reference and the target type is a primitive, so we must unbox.
281 DCHECK(IsReferenceType(from_type));
282 DCHECK(IsPrimitiveType(to_type));
283
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000284 ObjPtr<mirror::Object> from_obj(src_value.GetL());
Orion Hodsonc1d3bac2018-01-26 14:38:55 +0000285 if (UNLIKELY(from_obj.IsNull())) {
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000286 ThrowNullPointerException(
287 StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
288 from->PrettyDescriptor().c_str()).c_str());
289 return false;
290 }
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100291
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000292 Primitive::Type unboxed_type;
293 JValue unboxed_value;
294 if (UNLIKELY(!GetUnboxedTypeAndValue(from_obj, &unboxed_type, &unboxed_value))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100295 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
296 return false;
297 }
298
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000299 if (UNLIKELY(!ConvertPrimitiveValueNoThrow(unboxed_type, to_type, unboxed_value, value))) {
Orion Hodsonc1d3bac2018-01-26 14:38:55 +0000300 if (from->IsAssignableFrom(GetBoxedPrimitiveClass(to_type))) {
301 // CallSite may be Number, but the Number object is
302 // incompatible, e.g. Number (Integer) for a short.
303 ThrowClassCastException(from, to);
304 } else {
305 // CallSite is incompatible, e.g. Integer for a short.
306 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
307 }
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100308 return false;
309 }
310
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000311 return true;
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100312 }
313}
314
Orion Hodson811bd5f2016-12-07 11:35:37 +0000315namespace {
316
Orion Hodson811bd5f2016-12-07 11:35:37 +0000317inline void CopyArgumentsFromCallerFrame(const ShadowFrame& caller_frame,
318 ShadowFrame* callee_frame,
Orion Hodson960d4f72017-11-10 15:32:38 +0000319 const InstructionOperands* const operands,
320 const size_t first_dst_reg)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000321 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000322 for (size_t i = 0; i < operands->GetNumberOfOperands(); ++i) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000323 size_t dst_reg = first_dst_reg + i;
Orion Hodson960d4f72017-11-10 15:32:38 +0000324 size_t src_reg = operands->GetOperand(i);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000325 // Uint required, so that sign extension does not make this wrong on 64-bit systems
326 uint32_t src_value = caller_frame.GetVReg(src_reg);
327 ObjPtr<mirror::Object> o = caller_frame.GetVRegReference<kVerifyNone>(src_reg);
328 // If both register locations contains the same value, the register probably holds a reference.
329 // Note: As an optimization, non-moving collectors leave a stale reference value
330 // in the references array even after the original vreg was overwritten to a non-reference.
331 if (src_value == reinterpret_cast<uintptr_t>(o.Ptr())) {
Vladimir Markobcf17522018-06-01 13:14:32 +0100332 callee_frame->SetVRegReference(dst_reg, o);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000333 } else {
334 callee_frame->SetVReg(dst_reg, src_value);
335 }
336 }
337}
338
Orion Hodson811bd5f2016-12-07 11:35:37 +0000339inline bool ConvertAndCopyArgumentsFromCallerFrame(
340 Thread* self,
341 Handle<mirror::MethodType> callsite_type,
342 Handle<mirror::MethodType> callee_type,
343 const ShadowFrame& caller_frame,
Orion Hodson960d4f72017-11-10 15:32:38 +0000344 uint32_t first_dest_reg,
345 const InstructionOperands* const operands,
Orion Hodson811bd5f2016-12-07 11:35:37 +0000346 ShadowFrame* callee_frame)
347 REQUIRES_SHARED(Locks::mutator_lock_) {
348 ObjPtr<mirror::ObjectArray<mirror::Class>> from_types(callsite_type->GetPTypes());
349 ObjPtr<mirror::ObjectArray<mirror::Class>> to_types(callee_type->GetPTypes());
350
351 const int32_t num_method_params = from_types->GetLength();
352 if (to_types->GetLength() != num_method_params) {
353 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
354 return false;
355 }
356
Orion Hodson928033d2018-02-07 05:30:54 +0000357 ShadowFrameGetter getter(caller_frame, operands);
Orion Hodson960d4f72017-11-10 15:32:38 +0000358 ShadowFrameSetter setter(callee_frame, first_dest_reg);
359 return PerformConversions<ShadowFrameGetter, ShadowFrameSetter>(self,
360 callsite_type,
361 callee_type,
362 &getter,
363 &setter,
364 num_method_params);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000365}
366
Orion Hodson811bd5f2016-12-07 11:35:37 +0000367inline bool IsInvoke(const mirror::MethodHandle::Kind handle_kind) {
368 return handle_kind <= mirror::MethodHandle::Kind::kLastInvokeKind;
369}
370
371inline bool IsInvokeTransform(const mirror::MethodHandle::Kind handle_kind) {
372 return (handle_kind == mirror::MethodHandle::Kind::kInvokeTransform
373 || handle_kind == mirror::MethodHandle::Kind::kInvokeCallSiteTransform);
374}
375
Orion Hodsonb8b93872018-01-30 07:51:10 +0000376inline bool IsInvokeVarHandle(const mirror::MethodHandle::Kind handle_kind) {
377 return (handle_kind == mirror::MethodHandle::Kind::kInvokeVarHandle ||
378 handle_kind == mirror::MethodHandle::Kind::kInvokeVarHandleExact);
379}
380
Orion Hodson811bd5f2016-12-07 11:35:37 +0000381inline bool IsFieldAccess(mirror::MethodHandle::Kind handle_kind) {
382 return (handle_kind >= mirror::MethodHandle::Kind::kFirstAccessorKind
383 && handle_kind <= mirror::MethodHandle::Kind::kLastAccessorKind);
384}
385
386// Calculate the number of ins for a proxy or native method, where we
387// can't just look at the code item.
388static inline size_t GetInsForProxyOrNativeMethod(ArtMethod* method)
389 REQUIRES_SHARED(Locks::mutator_lock_) {
390 DCHECK(method->IsNative() || method->IsProxyMethod());
Orion Hodson811bd5f2016-12-07 11:35:37 +0000391 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Orion Hodsona1be7132017-03-21 10:04:12 +0000392 uint32_t shorty_length = 0;
393 const char* shorty = method->GetShorty(&shorty_length);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000394
Orion Hodsona1be7132017-03-21 10:04:12 +0000395 // Static methods do not include the receiver. The receiver isn't included
396 // in the shorty_length though the return value is.
397 size_t num_ins = method->IsStatic() ? shorty_length - 1 : shorty_length;
398 for (const char* c = shorty + 1; *c != '\0'; ++c) {
399 if (*c == 'J' || *c == 'D') {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000400 ++num_ins;
401 }
402 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000403 return num_ins;
404}
405
406// Returns true iff. the callsite type for a polymorphic invoke is transformer
407// like, i.e that it has a single input argument whose type is
408// dalvik.system.EmulatedStackFrame.
Orion Hodson3b1b2162021-11-24 08:54:16 +0000409static inline bool InvokedFromTransform(Handle<mirror::MethodType> callsite_type)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000410 REQUIRES_SHARED(Locks::mutator_lock_) {
411 ObjPtr<mirror::ObjectArray<mirror::Class>> param_types(callsite_type->GetPTypes());
412 if (param_types->GetLength() == 1) {
413 ObjPtr<mirror::Class> param(param_types->GetWithoutChecks(0));
Orion Hodsona1be7132017-03-21 10:04:12 +0000414 // NB Comparing descriptor here as it appears faster in cycle simulation than using:
415 // param == WellKnownClasses::ToClass(WellKnownClasses::dalvik_system_EmulatedStackFrame)
416 // Costs are 98 vs 173 cycles per invocation.
417 return param->DescriptorEquals("Ldalvik/system/EmulatedStackFrame;");
Orion Hodson811bd5f2016-12-07 11:35:37 +0000418 }
419
420 return false;
421}
422
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100423static inline bool MethodHandleInvokeMethod(ArtMethod* called_method,
424 Handle<mirror::MethodType> callsite_type,
425 Handle<mirror::MethodType> target_type,
Orion Hodson2764f612019-11-11 14:36:20 +0000426 Handle<mirror::MethodType> nominal_type,
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100427 Thread* self,
428 ShadowFrame& shadow_frame,
Orion Hodson960d4f72017-11-10 15:32:38 +0000429 const InstructionOperands* const operands,
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100430 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000431 // Compute method information.
David Sehr0225f8e2018-01-31 08:52:24 +0000432 CodeItemDataAccessor accessor(called_method->DexInstructionData());
Orion Hodson811bd5f2016-12-07 11:35:37 +0000433
434 // Number of registers for the callee's call frame. Note that for non-exact
435 // invokes, we always derive this information from the callee method. We
436 // cannot guarantee during verification that the number of registers encoded
437 // in the invoke is equal to the number of ins for the callee. This is because
438 // some transformations (such as boxing a long -> Long or wideining an
439 // int -> long will change that number.
440 uint16_t num_regs;
441 size_t num_input_regs;
442 size_t first_dest_reg;
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800443 if (LIKELY(accessor.HasCodeItem())) {
444 num_regs = accessor.RegistersSize();
445 first_dest_reg = num_regs - accessor.InsSize();
446 num_input_regs = accessor.InsSize();
Orion Hodson811bd5f2016-12-07 11:35:37 +0000447 // Parameter registers go at the end of the shadow frame.
448 DCHECK_NE(first_dest_reg, (size_t)-1);
449 } else {
450 // No local regs for proxy and native methods.
451 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
452 num_regs = num_input_regs = GetInsForProxyOrNativeMethod(called_method);
453 first_dest_reg = 0;
454 }
455
456 // Allocate shadow frame on the stack.
457 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
458 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
459 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
460
461 // Whether this polymorphic invoke was issued by a transformer method.
462 bool is_caller_transformer = false;
463 // Thread might be suspended during PerformArgumentConversions due to the
464 // allocations performed during boxing.
465 {
466 ScopedStackedShadowFramePusher pusher(
467 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
468 if (callsite_type->IsExactMatch(target_type.Get())) {
469 // This is an exact invoke, we can take the fast path of just copying all
470 // registers without performing any argument conversions.
Orion Hodson960d4f72017-11-10 15:32:38 +0000471 CopyArgumentsFromCallerFrame(shadow_frame,
472 new_shadow_frame,
473 operands,
474 first_dest_reg);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000475 } else {
476 // This includes the case where we're entering this invoke-polymorphic
477 // from a transformer method. In that case, the callsite_type will contain
478 // a single argument of type dalvik.system.EmulatedStackFrame. In that
479 // case, we'll have to unmarshal the EmulatedStackFrame into the
480 // new_shadow_frame and perform argument conversions on it.
Orion Hodson3b1b2162021-11-24 08:54:16 +0000481 if (InvokedFromTransform(callsite_type)) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000482 is_caller_transformer = true;
483 // The emulated stack frame is the first and only argument when we're coming
484 // through from a transformer.
Orion Hodson960d4f72017-11-10 15:32:38 +0000485 size_t first_arg_register = operands->GetOperand(0);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000486 ObjPtr<mirror::EmulatedStackFrame> emulated_stack_frame(
Vladimir Markod7e9bbf2019-03-28 13:18:57 +0000487 ObjPtr<mirror::EmulatedStackFrame>::DownCast(
488 shadow_frame.GetVRegReference(first_arg_register)));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000489 if (!emulated_stack_frame->WriteToShadowFrame(self,
490 target_type,
491 first_dest_reg,
492 new_shadow_frame)) {
493 DCHECK(self->IsExceptionPending());
Yi Kong4b22b342018-08-02 14:43:21 -0700494 result->SetL(nullptr);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000495 return false;
496 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000497 } else {
498 if (!callsite_type->IsConvertible(target_type.Get())) {
499 ThrowWrongMethodTypeException(target_type.Get(), callsite_type.Get());
500 return false;
501 }
Orion Hodson960d4f72017-11-10 15:32:38 +0000502 if (!ConvertAndCopyArgumentsFromCallerFrame(self,
503 callsite_type,
504 target_type,
505 shadow_frame,
506 first_dest_reg,
507 operands,
508 new_shadow_frame)) {
Orion Hodsona1be7132017-03-21 10:04:12 +0000509 DCHECK(self->IsExceptionPending());
Yi Kong4b22b342018-08-02 14:43:21 -0700510 result->SetL(nullptr);
Orion Hodsona1be7132017-03-21 10:04:12 +0000511 return false;
512 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000513 }
514 }
515 }
516
Nicolas Geoffray8cedd8b2021-12-20 16:49:14 +0000517 bool use_interpreter_entrypoint = ClassLinker::ShouldUseInterpreterEntrypoint(
518 called_method, called_method->GetEntryPointFromQuickCompiledCode());
Jeff Hao5ea84132017-05-05 16:59:29 -0700519 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800520 accessor,
Jeff Hao5ea84132017-05-05 16:59:29 -0700521 shadow_frame.GetMethod(),
522 first_dest_reg,
523 new_shadow_frame,
524 result,
Nicolas Geoffray8cedd8b2021-12-20 16:49:14 +0000525 use_interpreter_entrypoint);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000526 if (self->IsExceptionPending()) {
527 return false;
528 }
529
530 // If the caller of this signature polymorphic method was a transformer,
531 // we need to copy the result back out to the emulated stack frame.
532 if (is_caller_transformer) {
533 StackHandleScope<2> hs(self);
Orion Hodson960d4f72017-11-10 15:32:38 +0000534 size_t first_callee_register = operands->GetOperand(0);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000535 Handle<mirror::EmulatedStackFrame> emulated_stack_frame(
Vladimir Markod7e9bbf2019-03-28 13:18:57 +0000536 hs.NewHandle(ObjPtr<mirror::EmulatedStackFrame>::DownCast(
537 shadow_frame.GetVRegReference(first_callee_register))));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000538 Handle<mirror::MethodType> emulated_stack_type(hs.NewHandle(emulated_stack_frame->GetType()));
539 JValue local_result;
540 local_result.SetJ(result->GetJ());
541
542 if (ConvertReturnValue(emulated_stack_type, target_type, &local_result)) {
543 emulated_stack_frame->SetReturnValue(self, local_result);
544 return true;
Orion Hodson811bd5f2016-12-07 11:35:37 +0000545 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000546
547 DCHECK(self->IsExceptionPending());
548 return false;
Orion Hodson811bd5f2016-12-07 11:35:37 +0000549 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000550
Orion Hodson2764f612019-11-11 14:36:20 +0000551 if (nominal_type != nullptr) {
552 return ConvertReturnValue(nominal_type, target_type, result) &&
553 ConvertReturnValue(callsite_type, nominal_type, result);
554 }
555
Orion Hodsona1be7132017-03-21 10:04:12 +0000556 return ConvertReturnValue(callsite_type, target_type, result);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000557}
558
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100559static inline bool MethodHandleInvokeTransform(ArtMethod* called_method,
560 Handle<mirror::MethodType> callsite_type,
561 Handle<mirror::MethodType> callee_type,
562 Thread* self,
563 ShadowFrame& shadow_frame,
564 Handle<mirror::MethodHandle> receiver,
Orion Hodson960d4f72017-11-10 15:32:38 +0000565 const InstructionOperands* const operands,
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100566 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000567 REQUIRES_SHARED(Locks::mutator_lock_) {
568 // This can be fixed to two, because the method we're calling here
569 // (MethodHandle.transformInternal) doesn't have any locals and the signature
570 // is known :
571 //
572 // private MethodHandle.transformInternal(EmulatedStackFrame sf);
573 //
574 // This means we need only two vregs :
575 // - One for the receiver object.
576 // - One for the only method argument (an EmulatedStackFrame).
577 static constexpr size_t kNumRegsForTransform = 2;
578
David Sehr0225f8e2018-01-31 08:52:24 +0000579 CodeItemDataAccessor accessor(called_method->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800580 DCHECK_EQ(kNumRegsForTransform, accessor.RegistersSize());
581 DCHECK_EQ(kNumRegsForTransform, accessor.InsSize());
Orion Hodson811bd5f2016-12-07 11:35:37 +0000582
583 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
584 CREATE_SHADOW_FRAME(kNumRegsForTransform, &shadow_frame, called_method, /* dex pc */ 0);
585 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
586
587 StackHandleScope<1> hs(self);
588 MutableHandle<mirror::EmulatedStackFrame> sf(hs.NewHandle<mirror::EmulatedStackFrame>(nullptr));
Orion Hodson3b1b2162021-11-24 08:54:16 +0000589 if (InvokedFromTransform(callsite_type)) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000590 // If we're entering this transformer from another transformer, we can pass
591 // through the handle directly to the callee, instead of having to
592 // instantiate a new stack frame based on the shadow frame.
Orion Hodson960d4f72017-11-10 15:32:38 +0000593 size_t first_callee_register = operands->GetOperand(0);
Vladimir Markod7e9bbf2019-03-28 13:18:57 +0000594 sf.Assign(ObjPtr<mirror::EmulatedStackFrame>::DownCast(
595 shadow_frame.GetVRegReference(first_callee_register)));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000596 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000597 sf.Assign(mirror::EmulatedStackFrame::CreateFromShadowFrameAndArgs(self,
598 callsite_type,
599 callee_type,
600 shadow_frame,
601 operands));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000602
603 // Something went wrong while creating the emulated stack frame, we should
604 // throw the pending exception.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800605 if (sf == nullptr) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000606 DCHECK(self->IsExceptionPending());
607 return false;
608 }
609 }
610
611 new_shadow_frame->SetVRegReference(0, receiver.Get());
612 new_shadow_frame->SetVRegReference(1, sf.Get());
613
Nicolas Geoffray8cedd8b2021-12-20 16:49:14 +0000614 bool use_interpreter_entrypoint = ClassLinker::ShouldUseInterpreterEntrypoint(
615 called_method, called_method->GetEntryPointFromQuickCompiledCode());
Orion Hodson811bd5f2016-12-07 11:35:37 +0000616 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800617 accessor,
Orion Hodson811bd5f2016-12-07 11:35:37 +0000618 shadow_frame.GetMethod(),
619 0 /* first destination register */,
620 new_shadow_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -0700621 result,
Nicolas Geoffray8cedd8b2021-12-20 16:49:14 +0000622 use_interpreter_entrypoint);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000623 if (self->IsExceptionPending()) {
624 return false;
625 }
626
627 // If the called transformer method we called has returned a value, then we
628 // need to copy it back to |result|.
629 sf->GetReturnValue(self, result);
630 return ConvertReturnValue(callsite_type, callee_type, result);
631}
632
633inline static ObjPtr<mirror::Class> GetAndInitializeDeclaringClass(Thread* self, ArtField* field)
634 REQUIRES_SHARED(Locks::mutator_lock_) {
635 // Method handle invocations on static fields should ensure class is
636 // initialized. This usually happens when an instance is constructed
637 // or class members referenced, but this is not guaranteed when
638 // looking up method handles.
639 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
640 if (UNLIKELY(!klass->IsInitialized())) {
641 StackHandleScope<1> hs(self);
642 HandleWrapperObjPtr<mirror::Class> h(hs.NewHandleWrapper(&klass));
643 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h, true, true)) {
644 DCHECK(self->IsExceptionPending());
645 return nullptr;
646 }
647 }
648 return klass;
649}
650
Orion Hodsona1be7132017-03-21 10:04:12 +0000651ArtMethod* RefineTargetMethod(Thread* self,
652 ShadowFrame& shadow_frame,
653 const mirror::MethodHandle::Kind& handle_kind,
654 Handle<mirror::MethodType> handle_type,
655 Handle<mirror::MethodType> callsite_type,
656 const uint32_t receiver_reg,
657 ArtMethod* target_method)
658 REQUIRES_SHARED(Locks::mutator_lock_) {
659 if (handle_kind == mirror::MethodHandle::Kind::kInvokeVirtual ||
660 handle_kind == mirror::MethodHandle::Kind::kInvokeInterface) {
661 // For virtual and interface methods ensure target_method points to
662 // the actual method to invoke.
663 ObjPtr<mirror::Object> receiver(shadow_frame.GetVRegReference(receiver_reg));
Orion Hodson3b1b2162021-11-24 08:54:16 +0000664 if (InvokedFromTransform(callsite_type)) {
Orion Hodsona1be7132017-03-21 10:04:12 +0000665 // The current receiver is an emulated stack frame, the method's
666 // receiver needs to be fetched from there as the emulated frame
667 // will be unpacked into a new frame.
668 receiver = ObjPtr<mirror::EmulatedStackFrame>::DownCast(receiver)->GetReceiver();
669 }
670
671 ObjPtr<mirror::Class> declaring_class(target_method->GetDeclaringClass());
672 if (receiver == nullptr || receiver->GetClass() != declaring_class) {
673 // Verify that _vRegC is an object reference and of the type expected by
674 // the receiver.
675 if (!VerifyObjectIsClass(receiver, declaring_class)) {
676 DCHECK(self->IsExceptionPending());
677 return nullptr;
678 }
679 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(
680 target_method, kRuntimePointerSize);
681 }
682 } else if (handle_kind == mirror::MethodHandle::Kind::kInvokeDirect) {
683 // String constructors are a special case, they are replaced with
684 // StringFactory methods.
685 if (target_method->IsConstructor() && target_method->GetDeclaringClass()->IsStringClass()) {
686 DCHECK(handle_type->GetRType()->IsStringClass());
687 return WellKnownClasses::StringInitToStringFactory(target_method);
688 }
689 } else if (handle_kind == mirror::MethodHandle::Kind::kInvokeSuper) {
Orion Hodsona1be7132017-03-21 10:04:12 +0000690 // Note that we're not dynamically dispatching on the type of the receiver
691 // here. We use the static type of the "receiver" object that we've
692 // recorded in the method handle's type, which will be the same as the
693 // special caller that was specified at the point of lookup.
694 ObjPtr<mirror::Class> referrer_class = handle_type->GetPTypes()->Get(0);
Orion Hodson0f595742018-04-10 14:49:28 +0100695 ObjPtr<mirror::Class> declaring_class = target_method->GetDeclaringClass();
696 if (referrer_class == declaring_class) {
697 return target_method;
698 }
Alex Light49df7152019-10-03 11:22:35 -0700699 if (declaring_class->IsInterface()) {
700 if (target_method->IsAbstract()) {
701 std::string msg =
702 "Method " + target_method->PrettyMethod() + " is abstract interface method!";
703 ThrowIllegalAccessException(msg.c_str());
704 return nullptr;
705 }
706 } else {
Orion Hodsona1be7132017-03-21 10:04:12 +0000707 ObjPtr<mirror::Class> super_class = referrer_class->GetSuperClass();
708 uint16_t vtable_index = target_method->GetMethodIndex();
709 DCHECK(super_class != nullptr);
710 DCHECK(super_class->HasVTable());
711 // Note that super_class is a super of referrer_class and target_method
712 // will always be declared by super_class (or one of its super classes).
713 DCHECK_LT(vtable_index, super_class->GetVTableLength());
714 return super_class->GetVTableEntry(vtable_index, kRuntimePointerSize);
Orion Hodsona1be7132017-03-21 10:04:12 +0000715 }
716 }
717 return target_method;
718}
719
Orion Hodsona1be7132017-03-21 10:04:12 +0000720bool DoInvokePolymorphicMethod(Thread* self,
721 ShadowFrame& shadow_frame,
722 Handle<mirror::MethodHandle> method_handle,
723 Handle<mirror::MethodType> callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +0000724 const InstructionOperands* const operands,
Orion Hodsona1be7132017-03-21 10:04:12 +0000725 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000726 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson2764f612019-11-11 14:36:20 +0000727 StackHandleScope<2> hs(self);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000728 Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType()));
Orion Hodson2764f612019-11-11 14:36:20 +0000729 Handle<mirror::MethodType> nominal_handle_type(hs.NewHandle(method_handle->GetNominalType()));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000730 const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind();
Orion Hodsona1be7132017-03-21 10:04:12 +0000731 DCHECK(IsInvoke(handle_kind));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000732
Orion Hodsona1be7132017-03-21 10:04:12 +0000733 // Get the method we're actually invoking along with the kind of
734 // invoke that is desired. We don't need to perform access checks at this
735 // point because they would have been performed on our behalf at the point
736 // of creation of the method handle.
737 ArtMethod* target_method = method_handle->GetTargetMethod();
Orion Hodson960d4f72017-11-10 15:32:38 +0000738 uint32_t receiver_reg = (operands->GetNumberOfOperands() > 0) ? operands->GetOperand(0) : 0u;
Orion Hodsona1be7132017-03-21 10:04:12 +0000739 ArtMethod* called_method = RefineTargetMethod(self,
740 shadow_frame,
741 handle_kind,
742 handle_type,
743 callsite_type,
744 receiver_reg,
745 target_method);
746 if (called_method == nullptr) {
747 DCHECK(self->IsExceptionPending());
748 return false;
749 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000750
Orion Hodsona1be7132017-03-21 10:04:12 +0000751 if (IsInvokeTransform(handle_kind)) {
752 // There are two cases here - method handles representing regular
753 // transforms and those representing call site transforms. Method
754 // handles for call site transforms adapt their MethodType to match
755 // the call site. For these, the |callee_type| is the same as the
756 // |callsite_type|. The VarargsCollector is such a tranform, its
757 // method type depends on the call site, ie. x(a) or x(a, b), or
758 // x(a, b, c). The VarargsCollector invokes a variable arity method
759 // with the arity arguments in an array.
760 Handle<mirror::MethodType> callee_type =
761 (handle_kind == mirror::MethodHandle::Kind::kInvokeCallSiteTransform) ? callsite_type
762 : handle_type;
Orion Hodson960d4f72017-11-10 15:32:38 +0000763 return MethodHandleInvokeTransform(called_method,
764 callsite_type,
765 callee_type,
766 self,
767 shadow_frame,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700768 /* receiver= */ method_handle,
Orion Hodson960d4f72017-11-10 15:32:38 +0000769 operands,
770 result);
Orion Hodsona1be7132017-03-21 10:04:12 +0000771 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000772 return MethodHandleInvokeMethod(called_method,
773 callsite_type,
774 handle_type,
Orion Hodson2764f612019-11-11 14:36:20 +0000775 nominal_handle_type,
Orion Hodson960d4f72017-11-10 15:32:38 +0000776 self,
777 shadow_frame,
778 operands,
779 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000780 }
781}
782
783// Helper for getters in invoke-polymorphic.
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100784inline static void MethodHandleFieldGet(Thread* self,
785 const ShadowFrame& shadow_frame,
786 ObjPtr<mirror::Object>& obj,
787 ArtField* field,
788 Primitive::Type field_type,
789 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000790 REQUIRES_SHARED(Locks::mutator_lock_) {
791 switch (field_type) {
792 case Primitive::kPrimBoolean:
793 DoFieldGetCommon<Primitive::kPrimBoolean>(self, shadow_frame, obj, field, result);
794 break;
795 case Primitive::kPrimByte:
796 DoFieldGetCommon<Primitive::kPrimByte>(self, shadow_frame, obj, field, result);
797 break;
798 case Primitive::kPrimChar:
799 DoFieldGetCommon<Primitive::kPrimChar>(self, shadow_frame, obj, field, result);
800 break;
801 case Primitive::kPrimShort:
802 DoFieldGetCommon<Primitive::kPrimShort>(self, shadow_frame, obj, field, result);
803 break;
804 case Primitive::kPrimInt:
805 DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result);
806 break;
807 case Primitive::kPrimLong:
808 DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result);
809 break;
810 case Primitive::kPrimFloat:
811 DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result);
812 break;
813 case Primitive::kPrimDouble:
814 DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result);
815 break;
816 case Primitive::kPrimNot:
817 DoFieldGetCommon<Primitive::kPrimNot>(self, shadow_frame, obj, field, result);
818 break;
819 case Primitive::kPrimVoid:
820 LOG(FATAL) << "Unreachable: " << field_type;
821 UNREACHABLE();
822 }
823}
824
825// Helper for setters in invoke-polymorphic.
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100826inline bool MethodHandleFieldPut(Thread* self,
827 ShadowFrame& shadow_frame,
828 ObjPtr<mirror::Object>& obj,
829 ArtField* field,
830 Primitive::Type field_type,
831 JValue& value)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000832 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodsonc069a302017-01-18 09:23:12 +0000833 DCHECK(!Runtime::Current()->IsActiveTransaction());
834 static const bool kTransaction = false; // Not in a transaction.
835 static const bool kAssignabilityCheck = false; // No access check.
Orion Hodson811bd5f2016-12-07 11:35:37 +0000836 switch (field_type) {
837 case Primitive::kPrimBoolean:
Orion Hodsonc069a302017-01-18 09:23:12 +0000838 return
839 DoFieldPutCommon<Primitive::kPrimBoolean, kAssignabilityCheck, kTransaction>(
840 self, shadow_frame, obj, field, value);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000841 case Primitive::kPrimByte:
Orion Hodsonc069a302017-01-18 09:23:12 +0000842 return DoFieldPutCommon<Primitive::kPrimByte, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000843 self, shadow_frame, obj, field, value);
844 case Primitive::kPrimChar:
Orion Hodsonc069a302017-01-18 09:23:12 +0000845 return DoFieldPutCommon<Primitive::kPrimChar, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000846 self, shadow_frame, obj, field, value);
847 case Primitive::kPrimShort:
Orion Hodsonc069a302017-01-18 09:23:12 +0000848 return DoFieldPutCommon<Primitive::kPrimShort, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000849 self, shadow_frame, obj, field, value);
850 case Primitive::kPrimInt:
851 case Primitive::kPrimFloat:
Orion Hodsonc069a302017-01-18 09:23:12 +0000852 return DoFieldPutCommon<Primitive::kPrimInt, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000853 self, shadow_frame, obj, field, value);
854 case Primitive::kPrimLong:
855 case Primitive::kPrimDouble:
Orion Hodsonc069a302017-01-18 09:23:12 +0000856 return DoFieldPutCommon<Primitive::kPrimLong, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000857 self, shadow_frame, obj, field, value);
858 case Primitive::kPrimNot:
Orion Hodsonc069a302017-01-18 09:23:12 +0000859 return DoFieldPutCommon<Primitive::kPrimNot, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000860 self, shadow_frame, obj, field, value);
861 case Primitive::kPrimVoid:
862 LOG(FATAL) << "Unreachable: " << field_type;
863 UNREACHABLE();
864 }
865}
866
867static JValue GetValueFromShadowFrame(const ShadowFrame& shadow_frame,
868 Primitive::Type field_type,
869 uint32_t vreg)
870 REQUIRES_SHARED(Locks::mutator_lock_) {
871 JValue field_value;
872 switch (field_type) {
873 case Primitive::kPrimBoolean:
874 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
875 break;
876 case Primitive::kPrimByte:
877 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
878 break;
879 case Primitive::kPrimChar:
880 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
881 break;
882 case Primitive::kPrimShort:
883 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
884 break;
885 case Primitive::kPrimInt:
886 case Primitive::kPrimFloat:
887 field_value.SetI(shadow_frame.GetVReg(vreg));
888 break;
889 case Primitive::kPrimLong:
890 case Primitive::kPrimDouble:
891 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
892 break;
893 case Primitive::kPrimNot:
894 field_value.SetL(shadow_frame.GetVRegReference(vreg));
895 break;
896 case Primitive::kPrimVoid:
897 LOG(FATAL) << "Unreachable: " << field_type;
898 UNREACHABLE();
899 }
900 return field_value;
901}
902
Orion Hodson960d4f72017-11-10 15:32:38 +0000903template <bool do_conversions>
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100904bool MethodHandleFieldAccess(Thread* self,
905 ShadowFrame& shadow_frame,
906 Handle<mirror::MethodHandle> method_handle,
907 Handle<mirror::MethodType> callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +0000908 const InstructionOperands* const operands,
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100909 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000910 StackHandleScope<1> hs(self);
911 Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType()));
912 const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind();
913 ArtField* field = method_handle->GetTargetField();
914 Primitive::Type field_type = field->GetTypeAsPrimitiveType();
Orion Hodson811bd5f2016-12-07 11:35:37 +0000915 switch (handle_kind) {
916 case mirror::MethodHandle::kInstanceGet: {
Orion Hodson960d4f72017-11-10 15:32:38 +0000917 size_t obj_reg = operands->GetOperand(0);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000918 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(obj_reg);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100919 MethodHandleFieldGet(self, shadow_frame, obj, field, field_type, result);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000920 if (do_conversions && !ConvertReturnValue(callsite_type, handle_type, result)) {
921 DCHECK(self->IsExceptionPending());
922 return false;
923 }
924 return true;
925 }
926 case mirror::MethodHandle::kStaticGet: {
927 ObjPtr<mirror::Object> obj = GetAndInitializeDeclaringClass(self, field);
928 if (obj == nullptr) {
929 DCHECK(self->IsExceptionPending());
930 return false;
931 }
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100932 MethodHandleFieldGet(self, shadow_frame, obj, field, field_type, result);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000933 if (do_conversions && !ConvertReturnValue(callsite_type, handle_type, result)) {
934 DCHECK(self->IsExceptionPending());
935 return false;
936 }
937 return true;
938 }
939 case mirror::MethodHandle::kInstancePut: {
Orion Hodson960d4f72017-11-10 15:32:38 +0000940 size_t obj_reg = operands->GetOperand(0);
941 size_t value_reg = operands->GetOperand(1);
Mathieu Chartier71b17082017-04-17 20:12:29 -0700942 const size_t kPTypeIndex = 1;
943 // Use ptypes instead of field type since we may be unboxing a reference for a primitive
944 // field. The field type is incorrect for this case.
945 JValue value = GetValueFromShadowFrame(
946 shadow_frame,
947 callsite_type->GetPTypes()->Get(kPTypeIndex)->GetPrimitiveType(),
948 value_reg);
949 if (do_conversions && !ConvertArgumentValue(callsite_type,
950 handle_type,
951 kPTypeIndex,
952 &value)) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000953 DCHECK(self->IsExceptionPending());
954 return false;
955 }
956 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(obj_reg);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100957 return MethodHandleFieldPut(self, shadow_frame, obj, field, field_type, value);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000958 }
959 case mirror::MethodHandle::kStaticPut: {
960 ObjPtr<mirror::Object> obj = GetAndInitializeDeclaringClass(self, field);
961 if (obj == nullptr) {
962 DCHECK(self->IsExceptionPending());
963 return false;
964 }
Orion Hodson960d4f72017-11-10 15:32:38 +0000965 size_t value_reg = operands->GetOperand(0);
Mathieu Chartier71b17082017-04-17 20:12:29 -0700966 const size_t kPTypeIndex = 0;
967 // Use ptypes instead of field type since we may be unboxing a reference for a primitive
968 // field. The field type is incorrect for this case.
969 JValue value = GetValueFromShadowFrame(
970 shadow_frame,
971 callsite_type->GetPTypes()->Get(kPTypeIndex)->GetPrimitiveType(),
972 value_reg);
973 if (do_conversions && !ConvertArgumentValue(callsite_type,
974 handle_type,
975 kPTypeIndex,
976 &value)) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000977 DCHECK(self->IsExceptionPending());
978 return false;
979 }
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100980 return MethodHandleFieldPut(self, shadow_frame, obj, field, field_type, value);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000981 }
982 default:
983 LOG(FATAL) << "Unreachable: " << handle_kind;
984 UNREACHABLE();
985 }
986}
987
Orion Hodsonb8b93872018-01-30 07:51:10 +0000988bool DoVarHandleInvokeTranslationUnchecked(Thread* self,
989 ShadowFrame& shadow_frame,
990 mirror::VarHandle::AccessMode access_mode,
991 Handle<mirror::VarHandle> vh,
992 Handle<mirror::MethodType> vh_type,
993 Handle<mirror::MethodType> callsite_type,
994 const InstructionOperands* const operands,
995 JValue* result)
996 REQUIRES_SHARED(Locks::mutator_lock_) {
997 DCHECK_EQ(operands->GetNumberOfOperands(), static_cast<uint32_t>(vh_type->GetNumberOfPTypes()));
998 DCHECK_EQ(operands->GetNumberOfOperands(),
999 static_cast<uint32_t>(callsite_type->GetNumberOfPTypes()));
Orion Hodsond56e8442021-11-30 13:28:55 +00001000 if (!vh->IsAccessModeSupported(access_mode)) {
1001 ThrowUnsupportedOperationException();
1002 return false;
1003 }
1004
Orion Hodsonb8b93872018-01-30 07:51:10 +00001005 const size_t vreg_count = vh_type->NumberOfVRegs();
1006 ShadowFrameAllocaUniquePtr accessor_frame =
1007 CREATE_SHADOW_FRAME(vreg_count, nullptr, shadow_frame.GetMethod(), shadow_frame.GetDexPC());
1008 ShadowFrameGetter getter(shadow_frame, operands);
1009 static const uint32_t kFirstAccessorReg = 0;
1010 ShadowFrameSetter setter(accessor_frame.get(), kFirstAccessorReg);
1011 if (!PerformConversions(self, callsite_type, vh_type, &getter, &setter)) {
1012 return false;
1013 }
1014 RangeInstructionOperands accessor_operands(kFirstAccessorReg, kFirstAccessorReg + vreg_count);
1015 if (!vh->Access(access_mode, accessor_frame.get(), &accessor_operands, result)) {
1016 return false;
1017 }
1018 return ConvertReturnValue(callsite_type, vh_type, result);
1019}
1020
1021bool DoVarHandleInvokeTranslation(Thread* self,
1022 ShadowFrame& shadow_frame,
1023 bool invokeExact,
1024 Handle<mirror::MethodHandle> method_handle,
1025 Handle<mirror::MethodType> callsite_type,
1026 const InstructionOperands* const operands,
1027 JValue* result)
1028 REQUIRES_SHARED(Locks::mutator_lock_) {
1029 if (!invokeExact) {
1030 // Exact invokes are checked for compatability higher up. The
1031 // non-exact invoke path doesn't have a similar check due to
1032 // transformers which have EmulatedStack frame arguments with the
1033 // actual method type associated with the frame.
1034 if (UNLIKELY(!callsite_type->IsConvertible(method_handle->GetMethodType()))) {
1035 ThrowWrongMethodTypeException(method_handle->GetMethodType(), callsite_type.Get());
1036 return false;
1037 }
1038 }
1039
1040 //
1041 // Basic checks that apply in all cases.
1042 //
1043 StackHandleScope<6> hs(self);
1044 Handle<mirror::ObjectArray<mirror::Class>>
1045 callsite_ptypes(hs.NewHandle(callsite_type->GetPTypes()));
1046 Handle<mirror::ObjectArray<mirror::Class>>
1047 mh_ptypes(hs.NewHandle(method_handle->GetMethodType()->GetPTypes()));
1048
1049 // Check that the first parameter is a VarHandle
1050 if (callsite_ptypes->GetLength() < 1 ||
1051 !mh_ptypes->Get(0)->IsAssignableFrom(callsite_ptypes->Get(0)) ||
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001052 mh_ptypes->Get(0) != GetClassRoot<mirror::VarHandle>()) {
Orion Hodsonb8b93872018-01-30 07:51:10 +00001053 ThrowWrongMethodTypeException(method_handle->GetMethodType(), callsite_type.Get());
1054 return false;
1055 }
1056
1057 // Get the receiver
Vladimir Marko4bb2af52019-03-22 11:09:19 +00001058 ObjPtr<mirror::Object> receiver = shadow_frame.GetVRegReference(operands->GetOperand(0));
Orion Hodsonb8b93872018-01-30 07:51:10 +00001059 if (receiver == nullptr) {
1060 ThrowNullPointerException("Expected argument 1 to be a non-null VarHandle");
1061 return false;
1062 }
1063
1064 // Cast to VarHandle instance
Vladimir Marko4bb2af52019-03-22 11:09:19 +00001065 Handle<mirror::VarHandle> vh(hs.NewHandle(ObjPtr<mirror::VarHandle>::DownCast(receiver)));
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001066 DCHECK(GetClassRoot<mirror::VarHandle>()->IsAssignableFrom(vh->GetClass()));
Orion Hodsonb8b93872018-01-30 07:51:10 +00001067
1068 // Determine the accessor kind to dispatch
1069 ArtMethod* target_method = method_handle->GetTargetMethod();
1070 int intrinsic_index = target_method->GetIntrinsic();
1071 mirror::VarHandle::AccessMode access_mode =
1072 mirror::VarHandle::GetAccessModeByIntrinsic(static_cast<Intrinsics>(intrinsic_index));
1073 Handle<mirror::MethodType> vh_type =
1074 hs.NewHandle(vh->GetMethodTypeForAccessMode(self, access_mode));
1075 Handle<mirror::MethodType> mh_invoke_type = hs.NewHandle(
1076 mirror::MethodType::CloneWithoutLeadingParameter(self, method_handle->GetMethodType()));
1077 if (method_handle->GetHandleKind() == mirror::MethodHandle::Kind::kInvokeVarHandleExact) {
1078 if (!mh_invoke_type->IsExactMatch(vh_type.Get())) {
1079 ThrowWrongMethodTypeException(vh_type.Get(), mh_invoke_type.Get());
1080 return false;
1081 }
1082 } else {
1083 DCHECK_EQ(method_handle->GetHandleKind(), mirror::MethodHandle::Kind::kInvokeVarHandle);
1084 if (!mh_invoke_type->IsConvertible(vh_type.Get())) {
1085 ThrowWrongMethodTypeException(vh_type.Get(), mh_invoke_type.Get());
1086 return false;
1087 }
1088 }
1089
1090 Handle<mirror::MethodType> callsite_type_without_varhandle =
1091 hs.NewHandle(mirror::MethodType::CloneWithoutLeadingParameter(self, callsite_type.Get()));
1092 NoReceiverInstructionOperands varhandle_operands(operands);
1093 DCHECK_EQ(static_cast<int32_t>(varhandle_operands.GetNumberOfOperands()),
1094 callsite_type_without_varhandle->GetPTypes()->GetLength());
1095 return DoVarHandleInvokeTranslationUnchecked(self,
1096 shadow_frame,
1097 access_mode,
1098 vh,
1099 vh_type,
1100 callsite_type_without_varhandle,
1101 &varhandle_operands,
1102 result);
1103}
1104
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001105static inline bool MethodHandleInvokeInternal(Thread* self,
1106 ShadowFrame& shadow_frame,
1107 Handle<mirror::MethodHandle> method_handle,
1108 Handle<mirror::MethodType> callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +00001109 const InstructionOperands* const operands,
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001110 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +00001111 REQUIRES_SHARED(Locks::mutator_lock_) {
1112 const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind();
Orion Hodsona1be7132017-03-21 10:04:12 +00001113 if (IsFieldAccess(handle_kind)) {
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001114 ObjPtr<mirror::MethodType> handle_type(method_handle->GetMethodType());
Orion Hodsona1be7132017-03-21 10:04:12 +00001115 DCHECK(!callsite_type->IsExactMatch(handle_type.Ptr()));
1116 if (!callsite_type->IsConvertible(handle_type.Ptr())) {
Orion Hodson811bd5f2016-12-07 11:35:37 +00001117 ThrowWrongMethodTypeException(handle_type.Ptr(), callsite_type.Get());
1118 return false;
1119 }
Orion Hodsona1be7132017-03-21 10:04:12 +00001120 const bool do_convert = true;
Orion Hodson960d4f72017-11-10 15:32:38 +00001121 return MethodHandleFieldAccess<do_convert>(
Orion Hodsona1be7132017-03-21 10:04:12 +00001122 self,
1123 shadow_frame,
1124 method_handle,
1125 callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +00001126 operands,
Orion Hodsona1be7132017-03-21 10:04:12 +00001127 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +00001128 }
Orion Hodsonb8b93872018-01-30 07:51:10 +00001129 if (IsInvokeVarHandle(handle_kind)) {
1130 return DoVarHandleInvokeTranslation(self,
1131 shadow_frame,
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001132 /*invokeExact=*/ false,
Orion Hodsonb8b93872018-01-30 07:51:10 +00001133 method_handle,
1134 callsite_type,
1135 operands,
1136 result);
1137 }
Orion Hodson960d4f72017-11-10 15:32:38 +00001138 return DoInvokePolymorphicMethod(self,
1139 shadow_frame,
1140 method_handle,
1141 callsite_type,
1142 operands,
1143 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +00001144}
1145
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001146static inline bool MethodHandleInvokeExactInternal(
1147 Thread* self,
1148 ShadowFrame& shadow_frame,
1149 Handle<mirror::MethodHandle> method_handle,
1150 Handle<mirror::MethodType> callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +00001151 const InstructionOperands* const operands,
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001152 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +00001153 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodsona1be7132017-03-21 10:04:12 +00001154 StackHandleScope<1> hs(self);
Orion Hodsona1be7132017-03-21 10:04:12 +00001155 Handle<mirror::MethodType> method_handle_type(hs.NewHandle(method_handle->GetMethodType()));
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001156 if (!callsite_type->IsExactMatch(method_handle_type.Get())) {
1157 ThrowWrongMethodTypeException(method_handle_type.Get(), callsite_type.Get());
1158 return false;
1159 }
1160
1161 const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind();
Orion Hodson811bd5f2016-12-07 11:35:37 +00001162 if (IsFieldAccess(handle_kind)) {
1163 const bool do_convert = false;
Orion Hodson960d4f72017-11-10 15:32:38 +00001164 return MethodHandleFieldAccess<do_convert>(self,
1165 shadow_frame,
1166 method_handle,
1167 callsite_type,
1168 operands,
1169 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +00001170 }
1171
Orion Hodsona1be7132017-03-21 10:04:12 +00001172 // Slow-path check.
Orion Hodsonb8b93872018-01-30 07:51:10 +00001173 if (IsInvokeTransform(handle_kind) ||
Orion Hodson3b1b2162021-11-24 08:54:16 +00001174 InvokedFromTransform(callsite_type)) {
Orion Hodson960d4f72017-11-10 15:32:38 +00001175 return DoInvokePolymorphicMethod(self,
1176 shadow_frame,
1177 method_handle,
1178 callsite_type,
1179 operands,
1180 result);
Orion Hodsonb8b93872018-01-30 07:51:10 +00001181 } else if (IsInvokeVarHandle(handle_kind)) {
1182 return DoVarHandleInvokeTranslation(self,
1183 shadow_frame,
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001184 /*invokeExact=*/ true,
Orion Hodsonb8b93872018-01-30 07:51:10 +00001185 method_handle,
1186 callsite_type,
1187 operands,
1188 result);
Orion Hodsona1be7132017-03-21 10:04:12 +00001189 }
1190
Orion Hodson3b1b2162021-11-24 08:54:16 +00001191 // On the fast-path. This is equivalent to DoCallPolymorphic without the conversion paths.
Orion Hodsona1be7132017-03-21 10:04:12 +00001192 ArtMethod* target_method = method_handle->GetTargetMethod();
Orion Hodson960d4f72017-11-10 15:32:38 +00001193 uint32_t receiver_reg = (operands->GetNumberOfOperands() > 0) ? operands->GetOperand(0) : 0u;
Orion Hodsona1be7132017-03-21 10:04:12 +00001194 ArtMethod* called_method = RefineTargetMethod(self,
Orion Hodson811bd5f2016-12-07 11:35:37 +00001195 shadow_frame,
Orion Hodsona1be7132017-03-21 10:04:12 +00001196 handle_kind,
1197 method_handle_type,
Orion Hodson811bd5f2016-12-07 11:35:37 +00001198 callsite_type,
Orion Hodsona1be7132017-03-21 10:04:12 +00001199 receiver_reg,
1200 target_method);
1201 if (called_method == nullptr) {
1202 DCHECK(self->IsExceptionPending());
1203 return false;
1204 }
1205
1206 // Compute method information.
David Sehr0225f8e2018-01-31 08:52:24 +00001207 CodeItemDataAccessor accessor(called_method->DexInstructionData());
Orion Hodsona1be7132017-03-21 10:04:12 +00001208 uint16_t num_regs;
1209 size_t num_input_regs;
1210 size_t first_dest_reg;
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001211 if (LIKELY(accessor.HasCodeItem())) {
1212 num_regs = accessor.RegistersSize();
1213 first_dest_reg = num_regs - accessor.InsSize();
1214 num_input_regs = accessor.InsSize();
Orion Hodsona1be7132017-03-21 10:04:12 +00001215 // Parameter registers go at the end of the shadow frame.
1216 DCHECK_NE(first_dest_reg, (size_t)-1);
1217 } else {
1218 // No local regs for proxy and native methods.
1219 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1220 num_regs = num_input_regs = GetInsForProxyOrNativeMethod(called_method);
1221 first_dest_reg = 0;
1222 }
1223
1224 // Allocate shadow frame on the stack.
1225 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
1226 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
1227 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
1228 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Orion Hodson960d4f72017-11-10 15:32:38 +00001229 CopyArgumentsFromCallerFrame(shadow_frame,
1230 new_shadow_frame,
1231 operands,
1232 first_dest_reg);
Orion Hodsona1be7132017-03-21 10:04:12 +00001233 self->EndAssertNoThreadSuspension(old_cause);
1234
Nicolas Geoffray8cedd8b2021-12-20 16:49:14 +00001235 bool use_interpreter_entrypoint = ClassLinker::ShouldUseInterpreterEntrypoint(
1236 called_method, called_method->GetEntryPointFromQuickCompiledCode());
Jeff Hao5ea84132017-05-05 16:59:29 -07001237 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001238 accessor,
Orion Hodson3b1b2162021-11-24 08:54:16 +00001239 called_method,
Jeff Hao5ea84132017-05-05 16:59:29 -07001240 first_dest_reg,
1241 new_shadow_frame,
1242 result,
Nicolas Geoffray8cedd8b2021-12-20 16:49:14 +00001243 use_interpreter_entrypoint);
Orion Hodsona1be7132017-03-21 10:04:12 +00001244 if (self->IsExceptionPending()) {
1245 return false;
1246 }
1247 return true;
Orion Hodson811bd5f2016-12-07 11:35:37 +00001248}
1249
Orion Hodson3b1b2162021-11-24 08:54:16 +00001250template <typename T>
1251bool InvokeInContext(Thread* self,
1252 ShadowFrame& shadow_frame,
1253 Handle<mirror::MethodHandle> method_handle,
1254 Handle<mirror::MethodType> callsite_type,
1255 const InstructionOperands* const operands,
1256 JValue* result,
1257 T invoker) REQUIRES_SHARED(Locks::mutator_lock_) {
1258 if (!InvokedFromTransform(callsite_type) || IsInvokeTransform(method_handle->GetHandleKind())) {
1259 return invoker(self, shadow_frame, method_handle, callsite_type, operands, result);
1260 }
1261
1262 // The MethodHandle invocation has occurred within a Transformer that has created an emulated
1263 // stack frame as context with which to invoke the MethodHandle. We need to unpack this, then
1264 // invoke the MethodHandle in the provided context.
1265 StackHandleScope<2> hs(self);
1266 size_t first_argument_vreg = operands->GetOperand(0);
1267 Handle<mirror::EmulatedStackFrame> emulated_stack_frame =
1268 hs.NewHandle(ObjPtr<mirror::EmulatedStackFrame>::DownCast(
1269 shadow_frame.GetVRegReference(first_argument_vreg)));
1270 Handle<mirror::MethodType> effective_callsite_type =
1271 hs.NewHandle(emulated_stack_frame->GetType());
1272 uint16_t num_vregs = effective_callsite_type->NumberOfVRegs();
1273
1274 RangeInstructionOperands effective_operands(0, num_vregs);
1275 ArtMethod* called_method = method_handle->GetTargetMethod(); // invoke / invokeExact.
1276 // Create a shadow frame
1277 ShadowFrameAllocaUniquePtr effective_frame =
1278 CREATE_SHADOW_FRAME(num_vregs, &shadow_frame, called_method, shadow_frame.GetDexPC());
1279 if (UNLIKELY(effective_operands.GetNumberOfOperands() != 0 &&
1280 !emulated_stack_frame->WriteToShadowFrame(self,
1281 effective_callsite_type,
1282 effective_operands.GetOperand(0),
1283 effective_frame.get()))) {
1284 DCHECK(self->IsExceptionPending());
1285 result->SetL(nullptr);
1286 return false;
1287 }
1288
1289 ScopedStackedShadowFramePusher pusher(
1290 self, effective_frame.get(), StackedShadowFrameType::kShadowFrameUnderConstruction);
1291
1292 // Invoke MethodHandle in newly created context
1293 bool success = invoker(self,
1294 *effective_frame.get(),
1295 method_handle,
1296 effective_callsite_type,
1297 &effective_operands,
1298 result);
1299 if (success) {
1300 emulated_stack_frame->SetReturnValue(self, *result);
1301 }
1302 return success;
1303}
1304
Orion Hodson811bd5f2016-12-07 11:35:37 +00001305} // namespace
1306
Orion Hodson960d4f72017-11-10 15:32:38 +00001307bool MethodHandleInvoke(Thread* self,
Orion Hodson3b1b2162021-11-24 08:54:16 +00001308 ShadowFrame& shadow_frame,
1309 Handle<mirror::MethodHandle> method_handle,
1310 Handle<mirror::MethodType> callsite_type,
1311 const InstructionOperands* const operands,
1312 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
1313 auto invoke = [](Thread* self,
1314 ShadowFrame& shadow_frame,
1315 Handle<mirror::MethodHandle> method_handle,
1316 Handle<mirror::MethodType> callsite_type,
1317 const InstructionOperands* const operands,
1318 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
1319 if (UNLIKELY(callsite_type->IsExactMatch(method_handle->GetMethodType()))) {
1320 // A non-exact invoke that can be invoked exactly.
1321 return MethodHandleInvokeExactInternal(
1322 self, shadow_frame, method_handle, callsite_type, operands, result);
1323 } else {
1324 return MethodHandleInvokeInternal(
1325 self, shadow_frame, method_handle, callsite_type, operands, result);
1326 }
1327 };
1328 return InvokeInContext(
1329 self, shadow_frame, method_handle, callsite_type, operands, result, invoke);
Orion Hodson811bd5f2016-12-07 11:35:37 +00001330}
1331
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001332bool MethodHandleInvokeExact(Thread* self,
Orion Hodson960d4f72017-11-10 15:32:38 +00001333 ShadowFrame& shadow_frame,
1334 Handle<mirror::MethodHandle> method_handle,
1335 Handle<mirror::MethodType> callsite_type,
1336 const InstructionOperands* const operands,
1337 JValue* result)
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001338 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson3b1b2162021-11-24 08:54:16 +00001339 auto invoke = [](Thread* self,
1340 ShadowFrame& shadow_frame,
1341 Handle<mirror::MethodHandle> method_handle,
1342 Handle<mirror::MethodType> callsite_type,
1343 const InstructionOperands* const operands,
1344 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
1345 // We need to check the nominal type of the handle in addition to the
1346 // real type. The "nominal" type is present when MethodHandle.asType is
1347 // called any handle, and results in the declared type of the handle
1348 // changing.
1349 ObjPtr<mirror::MethodType> nominal_type(method_handle->GetNominalType());
1350 if (UNLIKELY(nominal_type != nullptr)) {
1351 if (UNLIKELY(!callsite_type->IsExactMatch(nominal_type.Ptr()))) {
1352 ThrowWrongMethodTypeException(nominal_type.Ptr(), callsite_type.Get());
1353 return false;
1354 }
1355 if (LIKELY(!nominal_type->IsExactMatch(method_handle->GetMethodType()))) {
1356 // Different nominal type means we have to treat as non-exact.
1357 return MethodHandleInvokeInternal(
1358 self, shadow_frame, method_handle, callsite_type, operands, result);
1359 }
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001360 }
Orion Hodson3b1b2162021-11-24 08:54:16 +00001361 return MethodHandleInvokeExactInternal(
1362 self, shadow_frame, method_handle, callsite_type, operands, result);
1363 };
1364 return InvokeInContext(
1365 self, shadow_frame, method_handle, callsite_type, operands, result, invoke);
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001366}
1367
Orion Hodsonba28f9f2016-10-26 10:56:25 +01001368} // namespace art