blob: 8bb3bec9ac3618b8ed77b7611b01e73eab693b4e [file] [log] [blame]
Orion Hodsonba28f9f2016-10-26 10:56:25 +01001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Orion Hodsonba28f9f2016-10-26 10:56:25 +010017#include "method_handles-inl.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080018
19#include "android-base/stringprintf.h"
20
Orion Hodson811bd5f2016-12-07 11:35:37 +000021#include "common_dex_operations.h"
Orion Hodsonba28f9f2016-10-26 10:56:25 +010022#include "jvalue-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070023#include "jvalue.h"
Orion Hodson811bd5f2016-12-07 11:35:37 +000024#include "mirror/emulated_stack_frame.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080025#include "mirror/method_handle_impl-inl.h"
Orion Hodson811bd5f2016-12-07 11:35:37 +000026#include "mirror/method_type.h"
Orion Hodsonba28f9f2016-10-26 10:56:25 +010027#include "reflection-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070028#include "reflection.h"
Orion Hodson1a06f9f2016-11-09 08:32:42 +000029#include "well_known_classes.h"
Orion Hodsonba28f9f2016-10-26 10:56:25 +010030
31namespace art {
32
Andreas Gampe46ee31b2016-12-14 10:11:49 -080033using android::base::StringPrintf;
34
Orion Hodsonba28f9f2016-10-26 10:56:25 +010035namespace {
36
Orion Hodson1a06f9f2016-11-09 08:32:42 +000037#define PRIMITIVES_LIST(V) \
38 V(Primitive::kPrimBoolean, Boolean, Boolean, Z) \
39 V(Primitive::kPrimByte, Byte, Byte, B) \
40 V(Primitive::kPrimChar, Char, Character, C) \
41 V(Primitive::kPrimShort, Short, Short, S) \
42 V(Primitive::kPrimInt, Int, Integer, I) \
43 V(Primitive::kPrimLong, Long, Long, J) \
44 V(Primitive::kPrimFloat, Float, Float, F) \
45 V(Primitive::kPrimDouble, Double, Double, D)
Orion Hodsonba28f9f2016-10-26 10:56:25 +010046
47// Assigns |type| to the primitive type associated with |klass|. Returns
48// true iff. |klass| was a boxed type (Integer, Long etc.), false otherwise.
49bool GetUnboxedPrimitiveType(ObjPtr<mirror::Class> klass, Primitive::Type* type)
50 REQUIRES_SHARED(Locks::mutator_lock_) {
51 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Orion Hodsona1be7132017-03-21 10:04:12 +000052 std::string storage;
53 const char* descriptor = klass->GetDescriptor(&storage);
54 static const char kJavaLangPrefix[] = "Ljava/lang/";
55 static const size_t kJavaLangPrefixSize = sizeof(kJavaLangPrefix) - 1;
56 if (strncmp(descriptor, kJavaLangPrefix, kJavaLangPrefixSize) != 0) {
57 return false;
58 }
59
60 descriptor += kJavaLangPrefixSize;
61#define LOOKUP_PRIMITIVE(primitive, _, java_name, ___) \
62 if (strcmp(descriptor, #java_name ";") == 0) { \
63 *type = primitive; \
64 return true; \
Orion Hodsonba28f9f2016-10-26 10:56:25 +010065 }
Orion Hodsonba28f9f2016-10-26 10:56:25 +010066
Orion Hodson1a06f9f2016-11-09 08:32:42 +000067 PRIMITIVES_LIST(LOOKUP_PRIMITIVE);
68#undef LOOKUP_PRIMITIVE
Orion Hodsonba28f9f2016-10-26 10:56:25 +010069 return false;
70}
71
Orion Hodson1a06f9f2016-11-09 08:32:42 +000072ObjPtr<mirror::Class> GetBoxedPrimitiveClass(Primitive::Type type)
Orion Hodsonba28f9f2016-10-26 10:56:25 +010073 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson1a06f9f2016-11-09 08:32:42 +000074 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
75 jmethodID m = nullptr;
76 switch (type) {
77#define CASE_PRIMITIVE(primitive, _, java_name, __) \
78 case primitive: \
79 m = WellKnownClasses::java_lang_ ## java_name ## _valueOf; \
80 break;
81 PRIMITIVES_LIST(CASE_PRIMITIVE);
82#undef CASE_PRIMITIVE
83 case Primitive::Type::kPrimNot:
84 case Primitive::Type::kPrimVoid:
85 return nullptr;
Orion Hodsonba28f9f2016-10-26 10:56:25 +010086 }
Orion Hodson1a06f9f2016-11-09 08:32:42 +000087 return jni::DecodeArtMethod(m)->GetDeclaringClass();
88}
Orion Hodsonba28f9f2016-10-26 10:56:25 +010089
Orion Hodson1a06f9f2016-11-09 08:32:42 +000090bool GetUnboxedTypeAndValue(ObjPtr<mirror::Object> o, Primitive::Type* type, JValue* value)
91 REQUIRES_SHARED(Locks::mutator_lock_) {
92 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Orion Hodsonba28f9f2016-10-26 10:56:25 +010093 ObjPtr<mirror::Class> klass = o->GetClass();
Orion Hodsonba28f9f2016-10-26 10:56:25 +010094 ArtField* primitive_field = &klass->GetIFieldsPtr()->At(0);
Orion Hodson1a06f9f2016-11-09 08:32:42 +000095#define CASE_PRIMITIVE(primitive, abbrev, _, shorthand) \
96 if (klass == GetBoxedPrimitiveClass(primitive)) { \
97 *type = primitive; \
98 value->Set ## shorthand(primitive_field->Get ## abbrev(o)); \
99 return true; \
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100100 }
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000101 PRIMITIVES_LIST(CASE_PRIMITIVE)
102#undef CASE_PRIMITIVE
103 return false;
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100104}
105
106inline bool IsReferenceType(Primitive::Type type) {
107 return type == Primitive::kPrimNot;
108}
109
110inline bool IsPrimitiveType(Primitive::Type type) {
111 return !IsReferenceType(type);
112}
113
114} // namespace
115
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000116bool IsParameterTypeConvertible(ObjPtr<mirror::Class> from, ObjPtr<mirror::Class> to)
117 REQUIRES_SHARED(Locks::mutator_lock_) {
118 // This function returns true if there's any conceivable conversion
119 // between |from| and |to|. It's expected this method will be used
120 // to determine if a WrongMethodTypeException should be raised. The
121 // decision logic follows the documentation for MethodType.asType().
122 if (from == to) {
123 return true;
124 }
125
126 Primitive::Type from_primitive = from->GetPrimitiveType();
127 Primitive::Type to_primitive = to->GetPrimitiveType();
128 DCHECK(from_primitive != Primitive::Type::kPrimVoid);
129 DCHECK(to_primitive != Primitive::Type::kPrimVoid);
130
131 // If |to| and |from| are references.
132 if (IsReferenceType(from_primitive) && IsReferenceType(to_primitive)) {
133 // Assignability is determined during parameter conversion when
134 // invoking the associated method handle.
135 return true;
136 }
137
138 // If |to| and |from| are primitives and a widening conversion exists.
139 if (Primitive::IsWidenable(from_primitive, to_primitive)) {
140 return true;
141 }
142
143 // If |to| is a reference and |from| is a primitive, then boxing conversion.
144 if (IsReferenceType(to_primitive) && IsPrimitiveType(from_primitive)) {
145 return to->IsAssignableFrom(GetBoxedPrimitiveClass(from_primitive));
146 }
147
148 // If |from| is a reference and |to| is a primitive, then unboxing conversion.
149 if (IsPrimitiveType(to_primitive) && IsReferenceType(from_primitive)) {
150 if (from->DescriptorEquals("Ljava/lang/Object;")) {
151 // Object might be converted into a primitive during unboxing.
152 return true;
Orion Hodsona1be7132017-03-21 10:04:12 +0000153 }
154
155 if (Primitive::IsNumericType(to_primitive) && from->DescriptorEquals("Ljava/lang/Number;")) {
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000156 // Number might be unboxed into any of the number primitive types.
157 return true;
158 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000159
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000160 Primitive::Type unboxed_type;
161 if (GetUnboxedPrimitiveType(from, &unboxed_type)) {
Orion Hodsonf1412b42016-11-11 12:03:29 +0000162 if (unboxed_type == to_primitive) {
163 // Straightforward unboxing conversion such as Boolean => boolean.
164 return true;
Orion Hodsonf1412b42016-11-11 12:03:29 +0000165 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000166
167 // Check if widening operations for numeric primitives would work,
168 // such as Byte => byte => long.
169 return Primitive::IsWidenable(unboxed_type, to_primitive);
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000170 }
171 }
172
173 return false;
174}
175
176bool IsReturnTypeConvertible(ObjPtr<mirror::Class> from, ObjPtr<mirror::Class> to)
177 REQUIRES_SHARED(Locks::mutator_lock_) {
178 if (to->GetPrimitiveType() == Primitive::Type::kPrimVoid) {
179 // Result will be ignored.
180 return true;
181 } else if (from->GetPrimitiveType() == Primitive::Type::kPrimVoid) {
182 // Returned value will be 0 / null.
183 return true;
184 } else {
185 // Otherwise apply usual parameter conversion rules.
186 return IsParameterTypeConvertible(from, to);
187 }
188}
189
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100190bool ConvertJValueCommon(
191 Handle<mirror::MethodType> callsite_type,
192 Handle<mirror::MethodType> callee_type,
193 ObjPtr<mirror::Class> from,
194 ObjPtr<mirror::Class> to,
195 JValue* value) {
196 // The reader maybe concerned about the safety of the heap object
197 // that may be in |value|. There is only one case where allocation
198 // is obviously needed and that's for boxing. However, in the case
199 // of boxing |value| contains a non-reference type.
200
201 const Primitive::Type from_type = from->GetPrimitiveType();
202 const Primitive::Type to_type = to->GetPrimitiveType();
203
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000204 // Put incoming value into |src_value| and set return value to 0.
205 // Errors and conversions from void require the return value to be 0.
206 const JValue src_value(*value);
207 value->SetJ(0);
208
209 // Conversion from void set result to zero.
210 if (from_type == Primitive::kPrimVoid) {
211 return true;
212 }
213
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100214 // This method must be called only when the types don't match.
215 DCHECK(from != to);
216
217 if (IsPrimitiveType(from_type) && IsPrimitiveType(to_type)) {
218 // The source and target types are both primitives.
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000219 if (UNLIKELY(!ConvertPrimitiveValueNoThrow(from_type, to_type, src_value, value))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100220 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100221 return false;
222 }
223 return true;
224 } else if (IsReferenceType(from_type) && IsReferenceType(to_type)) {
225 // They're both reference types. If "from" is null, we can pass it
226 // through unchanged. If not, we must generate a cast exception if
227 // |to| is not assignable from the dynamic type of |ref|.
228 //
229 // Playing it safe with StackHandleScope here, not expecting any allocation
230 // in mirror::Class::IsAssignable().
231 StackHandleScope<2> hs(Thread::Current());
232 Handle<mirror::Class> h_to(hs.NewHandle(to));
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000233 Handle<mirror::Object> h_obj(hs.NewHandle(src_value.GetL()));
Orion Hodsonc1d3bac2018-01-26 14:38:55 +0000234 if (UNLIKELY(!h_obj.IsNull() && !to->IsAssignableFrom(h_obj->GetClass()))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100235 ThrowClassCastException(h_to.Get(), h_obj->GetClass());
236 return false;
237 }
238 value->SetL(h_obj.Get());
239 return true;
240 } else if (IsReferenceType(to_type)) {
241 DCHECK(IsPrimitiveType(from_type));
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100242 // The source type is a primitive and the target type is a reference, so we must box.
243 // The target type maybe a super class of the boxed source type, for example,
244 // if the source type is int, it's boxed type is java.lang.Integer, and the target
245 // type could be java.lang.Number.
246 Primitive::Type type;
247 if (!GetUnboxedPrimitiveType(to, &type)) {
248 ObjPtr<mirror::Class> boxed_from_class = GetBoxedPrimitiveClass(from_type);
Orion Hodsonc1d3bac2018-01-26 14:38:55 +0000249 if (LIKELY(boxed_from_class->IsSubClass(to))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100250 type = from_type;
251 } else {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100252 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
253 return false;
254 }
255 }
256
257 if (UNLIKELY(from_type != type)) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100258 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
259 return false;
260 }
261
Orion Hodsonc1d3bac2018-01-26 14:38:55 +0000262 if (UNLIKELY(!ConvertPrimitiveValueNoThrow(from_type, type, src_value, value))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100263 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
264 return false;
265 }
266
267 // Then perform the actual boxing, and then set the reference.
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000268 ObjPtr<mirror::Object> boxed = BoxPrimitive(type, src_value);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100269 value->SetL(boxed.Ptr());
270 return true;
271 } else {
272 // The source type is a reference and the target type is a primitive, so we must unbox.
273 DCHECK(IsReferenceType(from_type));
274 DCHECK(IsPrimitiveType(to_type));
275
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000276 ObjPtr<mirror::Object> from_obj(src_value.GetL());
Orion Hodsonc1d3bac2018-01-26 14:38:55 +0000277 if (UNLIKELY(from_obj.IsNull())) {
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000278 ThrowNullPointerException(
279 StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
280 from->PrettyDescriptor().c_str()).c_str());
281 return false;
282 }
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100283
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000284 Primitive::Type unboxed_type;
285 JValue unboxed_value;
286 if (UNLIKELY(!GetUnboxedTypeAndValue(from_obj, &unboxed_type, &unboxed_value))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100287 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
288 return false;
289 }
290
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000291 if (UNLIKELY(!ConvertPrimitiveValueNoThrow(unboxed_type, to_type, unboxed_value, value))) {
Orion Hodsonc1d3bac2018-01-26 14:38:55 +0000292 if (from->IsAssignableFrom(GetBoxedPrimitiveClass(to_type))) {
293 // CallSite may be Number, but the Number object is
294 // incompatible, e.g. Number (Integer) for a short.
295 ThrowClassCastException(from, to);
296 } else {
297 // CallSite is incompatible, e.g. Integer for a short.
298 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
299 }
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100300 return false;
301 }
302
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000303 return true;
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100304 }
305}
306
Orion Hodson811bd5f2016-12-07 11:35:37 +0000307namespace {
308
Orion Hodson811bd5f2016-12-07 11:35:37 +0000309inline void CopyArgumentsFromCallerFrame(const ShadowFrame& caller_frame,
310 ShadowFrame* callee_frame,
Orion Hodson960d4f72017-11-10 15:32:38 +0000311 const InstructionOperands* const operands,
312 const size_t first_dst_reg)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000313 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000314 for (size_t i = 0; i < operands->GetNumberOfOperands(); ++i) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000315 size_t dst_reg = first_dst_reg + i;
Orion Hodson960d4f72017-11-10 15:32:38 +0000316 size_t src_reg = operands->GetOperand(i);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000317 // Uint required, so that sign extension does not make this wrong on 64-bit systems
318 uint32_t src_value = caller_frame.GetVReg(src_reg);
319 ObjPtr<mirror::Object> o = caller_frame.GetVRegReference<kVerifyNone>(src_reg);
320 // If both register locations contains the same value, the register probably holds a reference.
321 // Note: As an optimization, non-moving collectors leave a stale reference value
322 // in the references array even after the original vreg was overwritten to a non-reference.
323 if (src_value == reinterpret_cast<uintptr_t>(o.Ptr())) {
324 callee_frame->SetVRegReference(dst_reg, o.Ptr());
325 } else {
326 callee_frame->SetVReg(dst_reg, src_value);
327 }
328 }
329}
330
Orion Hodson811bd5f2016-12-07 11:35:37 +0000331inline bool ConvertAndCopyArgumentsFromCallerFrame(
332 Thread* self,
333 Handle<mirror::MethodType> callsite_type,
334 Handle<mirror::MethodType> callee_type,
335 const ShadowFrame& caller_frame,
Orion Hodson960d4f72017-11-10 15:32:38 +0000336 uint32_t first_dest_reg,
337 const InstructionOperands* const operands,
Orion Hodson811bd5f2016-12-07 11:35:37 +0000338 ShadowFrame* callee_frame)
339 REQUIRES_SHARED(Locks::mutator_lock_) {
340 ObjPtr<mirror::ObjectArray<mirror::Class>> from_types(callsite_type->GetPTypes());
341 ObjPtr<mirror::ObjectArray<mirror::Class>> to_types(callee_type->GetPTypes());
342
343 const int32_t num_method_params = from_types->GetLength();
344 if (to_types->GetLength() != num_method_params) {
345 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
346 return false;
347 }
348
Orion Hodson960d4f72017-11-10 15:32:38 +0000349 ShadowFrameGetter getter(operands, caller_frame);
350 ShadowFrameSetter setter(callee_frame, first_dest_reg);
351 return PerformConversions<ShadowFrameGetter, ShadowFrameSetter>(self,
352 callsite_type,
353 callee_type,
354 &getter,
355 &setter,
356 num_method_params);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000357}
358
Orion Hodson811bd5f2016-12-07 11:35:37 +0000359inline bool IsInvoke(const mirror::MethodHandle::Kind handle_kind) {
360 return handle_kind <= mirror::MethodHandle::Kind::kLastInvokeKind;
361}
362
363inline bool IsInvokeTransform(const mirror::MethodHandle::Kind handle_kind) {
364 return (handle_kind == mirror::MethodHandle::Kind::kInvokeTransform
365 || handle_kind == mirror::MethodHandle::Kind::kInvokeCallSiteTransform);
366}
367
368inline bool IsFieldAccess(mirror::MethodHandle::Kind handle_kind) {
369 return (handle_kind >= mirror::MethodHandle::Kind::kFirstAccessorKind
370 && handle_kind <= mirror::MethodHandle::Kind::kLastAccessorKind);
371}
372
373// Calculate the number of ins for a proxy or native method, where we
374// can't just look at the code item.
375static inline size_t GetInsForProxyOrNativeMethod(ArtMethod* method)
376 REQUIRES_SHARED(Locks::mutator_lock_) {
377 DCHECK(method->IsNative() || method->IsProxyMethod());
Orion Hodson811bd5f2016-12-07 11:35:37 +0000378 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Orion Hodsona1be7132017-03-21 10:04:12 +0000379 uint32_t shorty_length = 0;
380 const char* shorty = method->GetShorty(&shorty_length);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000381
Orion Hodsona1be7132017-03-21 10:04:12 +0000382 // Static methods do not include the receiver. The receiver isn't included
383 // in the shorty_length though the return value is.
384 size_t num_ins = method->IsStatic() ? shorty_length - 1 : shorty_length;
385 for (const char* c = shorty + 1; *c != '\0'; ++c) {
386 if (*c == 'J' || *c == 'D') {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000387 ++num_ins;
388 }
389 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000390 return num_ins;
391}
392
393// Returns true iff. the callsite type for a polymorphic invoke is transformer
394// like, i.e that it has a single input argument whose type is
395// dalvik.system.EmulatedStackFrame.
396static inline bool IsCallerTransformer(Handle<mirror::MethodType> callsite_type)
397 REQUIRES_SHARED(Locks::mutator_lock_) {
398 ObjPtr<mirror::ObjectArray<mirror::Class>> param_types(callsite_type->GetPTypes());
399 if (param_types->GetLength() == 1) {
400 ObjPtr<mirror::Class> param(param_types->GetWithoutChecks(0));
Orion Hodsona1be7132017-03-21 10:04:12 +0000401 // NB Comparing descriptor here as it appears faster in cycle simulation than using:
402 // param == WellKnownClasses::ToClass(WellKnownClasses::dalvik_system_EmulatedStackFrame)
403 // Costs are 98 vs 173 cycles per invocation.
404 return param->DescriptorEquals("Ldalvik/system/EmulatedStackFrame;");
Orion Hodson811bd5f2016-12-07 11:35:37 +0000405 }
406
407 return false;
408}
409
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100410static inline bool MethodHandleInvokeMethod(ArtMethod* called_method,
411 Handle<mirror::MethodType> callsite_type,
412 Handle<mirror::MethodType> target_type,
413 Thread* self,
414 ShadowFrame& shadow_frame,
Orion Hodson960d4f72017-11-10 15:32:38 +0000415 const InstructionOperands* const operands,
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100416 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000417 // Compute method information.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800418 CodeItemDataAccessor accessor(called_method);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000419
420 // Number of registers for the callee's call frame. Note that for non-exact
421 // invokes, we always derive this information from the callee method. We
422 // cannot guarantee during verification that the number of registers encoded
423 // in the invoke is equal to the number of ins for the callee. This is because
424 // some transformations (such as boxing a long -> Long or wideining an
425 // int -> long will change that number.
426 uint16_t num_regs;
427 size_t num_input_regs;
428 size_t first_dest_reg;
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800429 if (LIKELY(accessor.HasCodeItem())) {
430 num_regs = accessor.RegistersSize();
431 first_dest_reg = num_regs - accessor.InsSize();
432 num_input_regs = accessor.InsSize();
Orion Hodson811bd5f2016-12-07 11:35:37 +0000433 // Parameter registers go at the end of the shadow frame.
434 DCHECK_NE(first_dest_reg, (size_t)-1);
435 } else {
436 // No local regs for proxy and native methods.
437 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
438 num_regs = num_input_regs = GetInsForProxyOrNativeMethod(called_method);
439 first_dest_reg = 0;
440 }
441
442 // Allocate shadow frame on the stack.
443 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
444 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
445 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
446
447 // Whether this polymorphic invoke was issued by a transformer method.
448 bool is_caller_transformer = false;
449 // Thread might be suspended during PerformArgumentConversions due to the
450 // allocations performed during boxing.
451 {
452 ScopedStackedShadowFramePusher pusher(
453 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
454 if (callsite_type->IsExactMatch(target_type.Get())) {
455 // This is an exact invoke, we can take the fast path of just copying all
456 // registers without performing any argument conversions.
Orion Hodson960d4f72017-11-10 15:32:38 +0000457 CopyArgumentsFromCallerFrame(shadow_frame,
458 new_shadow_frame,
459 operands,
460 first_dest_reg);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000461 } else {
462 // This includes the case where we're entering this invoke-polymorphic
463 // from a transformer method. In that case, the callsite_type will contain
464 // a single argument of type dalvik.system.EmulatedStackFrame. In that
465 // case, we'll have to unmarshal the EmulatedStackFrame into the
466 // new_shadow_frame and perform argument conversions on it.
467 if (IsCallerTransformer(callsite_type)) {
468 is_caller_transformer = true;
469 // The emulated stack frame is the first and only argument when we're coming
470 // through from a transformer.
Orion Hodson960d4f72017-11-10 15:32:38 +0000471 size_t first_arg_register = operands->GetOperand(0);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000472 ObjPtr<mirror::EmulatedStackFrame> emulated_stack_frame(
473 reinterpret_cast<mirror::EmulatedStackFrame*>(
474 shadow_frame.GetVRegReference(first_arg_register)));
475 if (!emulated_stack_frame->WriteToShadowFrame(self,
476 target_type,
477 first_dest_reg,
478 new_shadow_frame)) {
479 DCHECK(self->IsExceptionPending());
480 result->SetL(0);
481 return false;
482 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000483 } else {
484 if (!callsite_type->IsConvertible(target_type.Get())) {
485 ThrowWrongMethodTypeException(target_type.Get(), callsite_type.Get());
486 return false;
487 }
Orion Hodson960d4f72017-11-10 15:32:38 +0000488 if (!ConvertAndCopyArgumentsFromCallerFrame(self,
489 callsite_type,
490 target_type,
491 shadow_frame,
492 first_dest_reg,
493 operands,
494 new_shadow_frame)) {
Orion Hodsona1be7132017-03-21 10:04:12 +0000495 DCHECK(self->IsExceptionPending());
496 result->SetL(0);
497 return false;
498 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000499 }
500 }
501 }
502
Jeff Hao5ea84132017-05-05 16:59:29 -0700503 bool use_interpreter_entrypoint = ClassLinker::ShouldUseInterpreterEntrypoint(
504 called_method, called_method->GetEntryPointFromQuickCompiledCode());
505 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800506 accessor,
Jeff Hao5ea84132017-05-05 16:59:29 -0700507 shadow_frame.GetMethod(),
508 first_dest_reg,
509 new_shadow_frame,
510 result,
511 use_interpreter_entrypoint);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000512 if (self->IsExceptionPending()) {
513 return false;
514 }
515
516 // If the caller of this signature polymorphic method was a transformer,
517 // we need to copy the result back out to the emulated stack frame.
518 if (is_caller_transformer) {
519 StackHandleScope<2> hs(self);
Orion Hodson960d4f72017-11-10 15:32:38 +0000520 size_t first_callee_register = operands->GetOperand(0);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000521 Handle<mirror::EmulatedStackFrame> emulated_stack_frame(
522 hs.NewHandle(reinterpret_cast<mirror::EmulatedStackFrame*>(
523 shadow_frame.GetVRegReference(first_callee_register))));
524 Handle<mirror::MethodType> emulated_stack_type(hs.NewHandle(emulated_stack_frame->GetType()));
525 JValue local_result;
526 local_result.SetJ(result->GetJ());
527
528 if (ConvertReturnValue(emulated_stack_type, target_type, &local_result)) {
529 emulated_stack_frame->SetReturnValue(self, local_result);
530 return true;
Orion Hodson811bd5f2016-12-07 11:35:37 +0000531 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000532
533 DCHECK(self->IsExceptionPending());
534 return false;
Orion Hodson811bd5f2016-12-07 11:35:37 +0000535 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000536
537 return ConvertReturnValue(callsite_type, target_type, result);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000538}
539
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100540static inline bool MethodHandleInvokeTransform(ArtMethod* called_method,
541 Handle<mirror::MethodType> callsite_type,
542 Handle<mirror::MethodType> callee_type,
543 Thread* self,
544 ShadowFrame& shadow_frame,
545 Handle<mirror::MethodHandle> receiver,
Orion Hodson960d4f72017-11-10 15:32:38 +0000546 const InstructionOperands* const operands,
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100547 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000548 REQUIRES_SHARED(Locks::mutator_lock_) {
549 // This can be fixed to two, because the method we're calling here
550 // (MethodHandle.transformInternal) doesn't have any locals and the signature
551 // is known :
552 //
553 // private MethodHandle.transformInternal(EmulatedStackFrame sf);
554 //
555 // This means we need only two vregs :
556 // - One for the receiver object.
557 // - One for the only method argument (an EmulatedStackFrame).
558 static constexpr size_t kNumRegsForTransform = 2;
559
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800560 CodeItemDataAccessor accessor(called_method);
561 DCHECK_EQ(kNumRegsForTransform, accessor.RegistersSize());
562 DCHECK_EQ(kNumRegsForTransform, accessor.InsSize());
Orion Hodson811bd5f2016-12-07 11:35:37 +0000563
564 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
565 CREATE_SHADOW_FRAME(kNumRegsForTransform, &shadow_frame, called_method, /* dex pc */ 0);
566 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
567
568 StackHandleScope<1> hs(self);
569 MutableHandle<mirror::EmulatedStackFrame> sf(hs.NewHandle<mirror::EmulatedStackFrame>(nullptr));
570 if (IsCallerTransformer(callsite_type)) {
571 // If we're entering this transformer from another transformer, we can pass
572 // through the handle directly to the callee, instead of having to
573 // instantiate a new stack frame based on the shadow frame.
Orion Hodson960d4f72017-11-10 15:32:38 +0000574 size_t first_callee_register = operands->GetOperand(0);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000575 sf.Assign(reinterpret_cast<mirror::EmulatedStackFrame*>(
576 shadow_frame.GetVRegReference(first_callee_register)));
577 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000578 sf.Assign(mirror::EmulatedStackFrame::CreateFromShadowFrameAndArgs(self,
579 callsite_type,
580 callee_type,
581 shadow_frame,
582 operands));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000583
584 // Something went wrong while creating the emulated stack frame, we should
585 // throw the pending exception.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800586 if (sf == nullptr) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000587 DCHECK(self->IsExceptionPending());
588 return false;
589 }
590 }
591
592 new_shadow_frame->SetVRegReference(0, receiver.Get());
593 new_shadow_frame->SetVRegReference(1, sf.Get());
594
Jeff Hao5ea84132017-05-05 16:59:29 -0700595 bool use_interpreter_entrypoint = ClassLinker::ShouldUseInterpreterEntrypoint(
596 called_method, called_method->GetEntryPointFromQuickCompiledCode());
Orion Hodson811bd5f2016-12-07 11:35:37 +0000597 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800598 accessor,
Orion Hodson811bd5f2016-12-07 11:35:37 +0000599 shadow_frame.GetMethod(),
600 0 /* first destination register */,
601 new_shadow_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -0700602 result,
603 use_interpreter_entrypoint);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000604 if (self->IsExceptionPending()) {
605 return false;
606 }
607
608 // If the called transformer method we called has returned a value, then we
609 // need to copy it back to |result|.
610 sf->GetReturnValue(self, result);
611 return ConvertReturnValue(callsite_type, callee_type, result);
612}
613
614inline static ObjPtr<mirror::Class> GetAndInitializeDeclaringClass(Thread* self, ArtField* field)
615 REQUIRES_SHARED(Locks::mutator_lock_) {
616 // Method handle invocations on static fields should ensure class is
617 // initialized. This usually happens when an instance is constructed
618 // or class members referenced, but this is not guaranteed when
619 // looking up method handles.
620 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
621 if (UNLIKELY(!klass->IsInitialized())) {
622 StackHandleScope<1> hs(self);
623 HandleWrapperObjPtr<mirror::Class> h(hs.NewHandleWrapper(&klass));
624 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h, true, true)) {
625 DCHECK(self->IsExceptionPending());
626 return nullptr;
627 }
628 }
629 return klass;
630}
631
Orion Hodsona1be7132017-03-21 10:04:12 +0000632ArtMethod* RefineTargetMethod(Thread* self,
633 ShadowFrame& shadow_frame,
634 const mirror::MethodHandle::Kind& handle_kind,
635 Handle<mirror::MethodType> handle_type,
636 Handle<mirror::MethodType> callsite_type,
637 const uint32_t receiver_reg,
638 ArtMethod* target_method)
639 REQUIRES_SHARED(Locks::mutator_lock_) {
640 if (handle_kind == mirror::MethodHandle::Kind::kInvokeVirtual ||
641 handle_kind == mirror::MethodHandle::Kind::kInvokeInterface) {
642 // For virtual and interface methods ensure target_method points to
643 // the actual method to invoke.
644 ObjPtr<mirror::Object> receiver(shadow_frame.GetVRegReference(receiver_reg));
645 if (IsCallerTransformer(callsite_type)) {
646 // The current receiver is an emulated stack frame, the method's
647 // receiver needs to be fetched from there as the emulated frame
648 // will be unpacked into a new frame.
649 receiver = ObjPtr<mirror::EmulatedStackFrame>::DownCast(receiver)->GetReceiver();
650 }
651
652 ObjPtr<mirror::Class> declaring_class(target_method->GetDeclaringClass());
653 if (receiver == nullptr || receiver->GetClass() != declaring_class) {
654 // Verify that _vRegC is an object reference and of the type expected by
655 // the receiver.
656 if (!VerifyObjectIsClass(receiver, declaring_class)) {
657 DCHECK(self->IsExceptionPending());
658 return nullptr;
659 }
660 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(
661 target_method, kRuntimePointerSize);
662 }
663 } else if (handle_kind == mirror::MethodHandle::Kind::kInvokeDirect) {
664 // String constructors are a special case, they are replaced with
665 // StringFactory methods.
666 if (target_method->IsConstructor() && target_method->GetDeclaringClass()->IsStringClass()) {
667 DCHECK(handle_type->GetRType()->IsStringClass());
668 return WellKnownClasses::StringInitToStringFactory(target_method);
669 }
670 } else if (handle_kind == mirror::MethodHandle::Kind::kInvokeSuper) {
671 ObjPtr<mirror::Class> declaring_class = target_method->GetDeclaringClass();
672
673 // Note that we're not dynamically dispatching on the type of the receiver
674 // here. We use the static type of the "receiver" object that we've
675 // recorded in the method handle's type, which will be the same as the
676 // special caller that was specified at the point of lookup.
677 ObjPtr<mirror::Class> referrer_class = handle_type->GetPTypes()->Get(0);
678 if (!declaring_class->IsInterface()) {
679 ObjPtr<mirror::Class> super_class = referrer_class->GetSuperClass();
680 uint16_t vtable_index = target_method->GetMethodIndex();
681 DCHECK(super_class != nullptr);
682 DCHECK(super_class->HasVTable());
683 // Note that super_class is a super of referrer_class and target_method
684 // will always be declared by super_class (or one of its super classes).
685 DCHECK_LT(vtable_index, super_class->GetVTableLength());
686 return super_class->GetVTableEntry(vtable_index, kRuntimePointerSize);
687 } else {
688 return referrer_class->FindVirtualMethodForInterfaceSuper(target_method, kRuntimePointerSize);
689 }
690 }
691 return target_method;
692}
693
Orion Hodsona1be7132017-03-21 10:04:12 +0000694bool DoInvokePolymorphicMethod(Thread* self,
695 ShadowFrame& shadow_frame,
696 Handle<mirror::MethodHandle> method_handle,
697 Handle<mirror::MethodType> callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +0000698 const InstructionOperands* const operands,
Orion Hodsona1be7132017-03-21 10:04:12 +0000699 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000700 REQUIRES_SHARED(Locks::mutator_lock_) {
701 StackHandleScope<1> hs(self);
702 Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType()));
703 const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind();
Orion Hodsona1be7132017-03-21 10:04:12 +0000704 DCHECK(IsInvoke(handle_kind));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000705
Orion Hodsona1be7132017-03-21 10:04:12 +0000706 // Get the method we're actually invoking along with the kind of
707 // invoke that is desired. We don't need to perform access checks at this
708 // point because they would have been performed on our behalf at the point
709 // of creation of the method handle.
710 ArtMethod* target_method = method_handle->GetTargetMethod();
Orion Hodson960d4f72017-11-10 15:32:38 +0000711 uint32_t receiver_reg = (operands->GetNumberOfOperands() > 0) ? operands->GetOperand(0) : 0u;
Orion Hodsona1be7132017-03-21 10:04:12 +0000712 ArtMethod* called_method = RefineTargetMethod(self,
713 shadow_frame,
714 handle_kind,
715 handle_type,
716 callsite_type,
717 receiver_reg,
718 target_method);
719 if (called_method == nullptr) {
720 DCHECK(self->IsExceptionPending());
721 return false;
722 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000723
Orion Hodsona1be7132017-03-21 10:04:12 +0000724 if (IsInvokeTransform(handle_kind)) {
725 // There are two cases here - method handles representing regular
726 // transforms and those representing call site transforms. Method
727 // handles for call site transforms adapt their MethodType to match
728 // the call site. For these, the |callee_type| is the same as the
729 // |callsite_type|. The VarargsCollector is such a tranform, its
730 // method type depends on the call site, ie. x(a) or x(a, b), or
731 // x(a, b, c). The VarargsCollector invokes a variable arity method
732 // with the arity arguments in an array.
733 Handle<mirror::MethodType> callee_type =
734 (handle_kind == mirror::MethodHandle::Kind::kInvokeCallSiteTransform) ? callsite_type
735 : handle_type;
Orion Hodson960d4f72017-11-10 15:32:38 +0000736 return MethodHandleInvokeTransform(called_method,
737 callsite_type,
738 callee_type,
739 self,
740 shadow_frame,
741 method_handle /* receiver */,
742 operands,
743 result);
Orion Hodsona1be7132017-03-21 10:04:12 +0000744 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000745 return MethodHandleInvokeMethod(called_method,
746 callsite_type,
747 handle_type,
748 self,
749 shadow_frame,
750 operands,
751 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000752 }
753}
754
755// Helper for getters in invoke-polymorphic.
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100756inline static void MethodHandleFieldGet(Thread* self,
757 const ShadowFrame& shadow_frame,
758 ObjPtr<mirror::Object>& obj,
759 ArtField* field,
760 Primitive::Type field_type,
761 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000762 REQUIRES_SHARED(Locks::mutator_lock_) {
763 switch (field_type) {
764 case Primitive::kPrimBoolean:
765 DoFieldGetCommon<Primitive::kPrimBoolean>(self, shadow_frame, obj, field, result);
766 break;
767 case Primitive::kPrimByte:
768 DoFieldGetCommon<Primitive::kPrimByte>(self, shadow_frame, obj, field, result);
769 break;
770 case Primitive::kPrimChar:
771 DoFieldGetCommon<Primitive::kPrimChar>(self, shadow_frame, obj, field, result);
772 break;
773 case Primitive::kPrimShort:
774 DoFieldGetCommon<Primitive::kPrimShort>(self, shadow_frame, obj, field, result);
775 break;
776 case Primitive::kPrimInt:
777 DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result);
778 break;
779 case Primitive::kPrimLong:
780 DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result);
781 break;
782 case Primitive::kPrimFloat:
783 DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result);
784 break;
785 case Primitive::kPrimDouble:
786 DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result);
787 break;
788 case Primitive::kPrimNot:
789 DoFieldGetCommon<Primitive::kPrimNot>(self, shadow_frame, obj, field, result);
790 break;
791 case Primitive::kPrimVoid:
792 LOG(FATAL) << "Unreachable: " << field_type;
793 UNREACHABLE();
794 }
795}
796
797// Helper for setters in invoke-polymorphic.
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100798inline bool MethodHandleFieldPut(Thread* self,
799 ShadowFrame& shadow_frame,
800 ObjPtr<mirror::Object>& obj,
801 ArtField* field,
802 Primitive::Type field_type,
803 JValue& value)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000804 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodsonc069a302017-01-18 09:23:12 +0000805 DCHECK(!Runtime::Current()->IsActiveTransaction());
806 static const bool kTransaction = false; // Not in a transaction.
807 static const bool kAssignabilityCheck = false; // No access check.
Orion Hodson811bd5f2016-12-07 11:35:37 +0000808 switch (field_type) {
809 case Primitive::kPrimBoolean:
Orion Hodsonc069a302017-01-18 09:23:12 +0000810 return
811 DoFieldPutCommon<Primitive::kPrimBoolean, kAssignabilityCheck, kTransaction>(
812 self, shadow_frame, obj, field, value);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000813 case Primitive::kPrimByte:
Orion Hodsonc069a302017-01-18 09:23:12 +0000814 return DoFieldPutCommon<Primitive::kPrimByte, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000815 self, shadow_frame, obj, field, value);
816 case Primitive::kPrimChar:
Orion Hodsonc069a302017-01-18 09:23:12 +0000817 return DoFieldPutCommon<Primitive::kPrimChar, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000818 self, shadow_frame, obj, field, value);
819 case Primitive::kPrimShort:
Orion Hodsonc069a302017-01-18 09:23:12 +0000820 return DoFieldPutCommon<Primitive::kPrimShort, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000821 self, shadow_frame, obj, field, value);
822 case Primitive::kPrimInt:
823 case Primitive::kPrimFloat:
Orion Hodsonc069a302017-01-18 09:23:12 +0000824 return DoFieldPutCommon<Primitive::kPrimInt, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000825 self, shadow_frame, obj, field, value);
826 case Primitive::kPrimLong:
827 case Primitive::kPrimDouble:
Orion Hodsonc069a302017-01-18 09:23:12 +0000828 return DoFieldPutCommon<Primitive::kPrimLong, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000829 self, shadow_frame, obj, field, value);
830 case Primitive::kPrimNot:
Orion Hodsonc069a302017-01-18 09:23:12 +0000831 return DoFieldPutCommon<Primitive::kPrimNot, kAssignabilityCheck, kTransaction>(
Orion Hodson811bd5f2016-12-07 11:35:37 +0000832 self, shadow_frame, obj, field, value);
833 case Primitive::kPrimVoid:
834 LOG(FATAL) << "Unreachable: " << field_type;
835 UNREACHABLE();
836 }
837}
838
839static JValue GetValueFromShadowFrame(const ShadowFrame& shadow_frame,
840 Primitive::Type field_type,
841 uint32_t vreg)
842 REQUIRES_SHARED(Locks::mutator_lock_) {
843 JValue field_value;
844 switch (field_type) {
845 case Primitive::kPrimBoolean:
846 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
847 break;
848 case Primitive::kPrimByte:
849 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
850 break;
851 case Primitive::kPrimChar:
852 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
853 break;
854 case Primitive::kPrimShort:
855 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
856 break;
857 case Primitive::kPrimInt:
858 case Primitive::kPrimFloat:
859 field_value.SetI(shadow_frame.GetVReg(vreg));
860 break;
861 case Primitive::kPrimLong:
862 case Primitive::kPrimDouble:
863 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
864 break;
865 case Primitive::kPrimNot:
866 field_value.SetL(shadow_frame.GetVRegReference(vreg));
867 break;
868 case Primitive::kPrimVoid:
869 LOG(FATAL) << "Unreachable: " << field_type;
870 UNREACHABLE();
871 }
872 return field_value;
873}
874
Orion Hodson960d4f72017-11-10 15:32:38 +0000875template <bool do_conversions>
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100876bool MethodHandleFieldAccess(Thread* self,
877 ShadowFrame& shadow_frame,
878 Handle<mirror::MethodHandle> method_handle,
879 Handle<mirror::MethodType> callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +0000880 const InstructionOperands* const operands,
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100881 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000882 StackHandleScope<1> hs(self);
883 Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType()));
884 const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind();
885 ArtField* field = method_handle->GetTargetField();
886 Primitive::Type field_type = field->GetTypeAsPrimitiveType();
Orion Hodson811bd5f2016-12-07 11:35:37 +0000887 switch (handle_kind) {
888 case mirror::MethodHandle::kInstanceGet: {
Orion Hodson960d4f72017-11-10 15:32:38 +0000889 size_t obj_reg = operands->GetOperand(0);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000890 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(obj_reg);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100891 MethodHandleFieldGet(self, shadow_frame, obj, field, field_type, result);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000892 if (do_conversions && !ConvertReturnValue(callsite_type, handle_type, result)) {
893 DCHECK(self->IsExceptionPending());
894 return false;
895 }
896 return true;
897 }
898 case mirror::MethodHandle::kStaticGet: {
899 ObjPtr<mirror::Object> obj = GetAndInitializeDeclaringClass(self, field);
900 if (obj == nullptr) {
901 DCHECK(self->IsExceptionPending());
902 return false;
903 }
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100904 MethodHandleFieldGet(self, shadow_frame, obj, field, field_type, result);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000905 if (do_conversions && !ConvertReturnValue(callsite_type, handle_type, result)) {
906 DCHECK(self->IsExceptionPending());
907 return false;
908 }
909 return true;
910 }
911 case mirror::MethodHandle::kInstancePut: {
Orion Hodson960d4f72017-11-10 15:32:38 +0000912 size_t obj_reg = operands->GetOperand(0);
913 size_t value_reg = operands->GetOperand(1);
Mathieu Chartier71b17082017-04-17 20:12:29 -0700914 const size_t kPTypeIndex = 1;
915 // Use ptypes instead of field type since we may be unboxing a reference for a primitive
916 // field. The field type is incorrect for this case.
917 JValue value = GetValueFromShadowFrame(
918 shadow_frame,
919 callsite_type->GetPTypes()->Get(kPTypeIndex)->GetPrimitiveType(),
920 value_reg);
921 if (do_conversions && !ConvertArgumentValue(callsite_type,
922 handle_type,
923 kPTypeIndex,
924 &value)) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000925 DCHECK(self->IsExceptionPending());
926 return false;
927 }
928 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(obj_reg);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100929 return MethodHandleFieldPut(self, shadow_frame, obj, field, field_type, value);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000930 }
931 case mirror::MethodHandle::kStaticPut: {
932 ObjPtr<mirror::Object> obj = GetAndInitializeDeclaringClass(self, field);
933 if (obj == nullptr) {
934 DCHECK(self->IsExceptionPending());
935 return false;
936 }
Orion Hodson960d4f72017-11-10 15:32:38 +0000937 size_t value_reg = operands->GetOperand(0);
Mathieu Chartier71b17082017-04-17 20:12:29 -0700938 const size_t kPTypeIndex = 0;
939 // Use ptypes instead of field type since we may be unboxing a reference for a primitive
940 // field. The field type is incorrect for this case.
941 JValue value = GetValueFromShadowFrame(
942 shadow_frame,
943 callsite_type->GetPTypes()->Get(kPTypeIndex)->GetPrimitiveType(),
944 value_reg);
945 if (do_conversions && !ConvertArgumentValue(callsite_type,
946 handle_type,
947 kPTypeIndex,
948 &value)) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000949 DCHECK(self->IsExceptionPending());
950 return false;
951 }
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100952 return MethodHandleFieldPut(self, shadow_frame, obj, field, field_type, value);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000953 }
954 default:
955 LOG(FATAL) << "Unreachable: " << handle_kind;
956 UNREACHABLE();
957 }
958}
959
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100960static inline bool MethodHandleInvokeInternal(Thread* self,
961 ShadowFrame& shadow_frame,
962 Handle<mirror::MethodHandle> method_handle,
963 Handle<mirror::MethodType> callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +0000964 const InstructionOperands* const operands,
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100965 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000966 REQUIRES_SHARED(Locks::mutator_lock_) {
967 const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind();
Orion Hodsona1be7132017-03-21 10:04:12 +0000968 if (IsFieldAccess(handle_kind)) {
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100969 ObjPtr<mirror::MethodType> handle_type(method_handle->GetMethodType());
Orion Hodsona1be7132017-03-21 10:04:12 +0000970 DCHECK(!callsite_type->IsExactMatch(handle_type.Ptr()));
971 if (!callsite_type->IsConvertible(handle_type.Ptr())) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000972 ThrowWrongMethodTypeException(handle_type.Ptr(), callsite_type.Get());
973 return false;
974 }
Orion Hodsona1be7132017-03-21 10:04:12 +0000975 const bool do_convert = true;
Orion Hodson960d4f72017-11-10 15:32:38 +0000976 return MethodHandleFieldAccess<do_convert>(
Orion Hodsona1be7132017-03-21 10:04:12 +0000977 self,
978 shadow_frame,
979 method_handle,
980 callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +0000981 operands,
Orion Hodsona1be7132017-03-21 10:04:12 +0000982 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000983 }
Orion Hodson960d4f72017-11-10 15:32:38 +0000984 return DoInvokePolymorphicMethod(self,
985 shadow_frame,
986 method_handle,
987 callsite_type,
988 operands,
989 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000990}
991
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100992static inline bool MethodHandleInvokeExactInternal(
993 Thread* self,
994 ShadowFrame& shadow_frame,
995 Handle<mirror::MethodHandle> method_handle,
996 Handle<mirror::MethodType> callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +0000997 const InstructionOperands* const operands,
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100998 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000999 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodsona1be7132017-03-21 10:04:12 +00001000 StackHandleScope<1> hs(self);
Orion Hodsona1be7132017-03-21 10:04:12 +00001001 Handle<mirror::MethodType> method_handle_type(hs.NewHandle(method_handle->GetMethodType()));
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001002 if (!callsite_type->IsExactMatch(method_handle_type.Get())) {
1003 ThrowWrongMethodTypeException(method_handle_type.Get(), callsite_type.Get());
1004 return false;
1005 }
1006
1007 const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind();
Orion Hodson811bd5f2016-12-07 11:35:37 +00001008 if (IsFieldAccess(handle_kind)) {
1009 const bool do_convert = false;
Orion Hodson960d4f72017-11-10 15:32:38 +00001010 return MethodHandleFieldAccess<do_convert>(self,
1011 shadow_frame,
1012 method_handle,
1013 callsite_type,
1014 operands,
1015 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +00001016 }
1017
Orion Hodsona1be7132017-03-21 10:04:12 +00001018 // Slow-path check.
1019 if (IsInvokeTransform(handle_kind) || IsCallerTransformer(callsite_type)) {
Orion Hodson960d4f72017-11-10 15:32:38 +00001020 return DoInvokePolymorphicMethod(self,
1021 shadow_frame,
1022 method_handle,
1023 callsite_type,
1024 operands,
1025 result);
Orion Hodsona1be7132017-03-21 10:04:12 +00001026 }
1027
1028 // On the fast-path. This is equivalent to DoCallPolymoprhic without the conversion paths.
1029 ArtMethod* target_method = method_handle->GetTargetMethod();
Orion Hodson960d4f72017-11-10 15:32:38 +00001030 uint32_t receiver_reg = (operands->GetNumberOfOperands() > 0) ? operands->GetOperand(0) : 0u;
Orion Hodsona1be7132017-03-21 10:04:12 +00001031 ArtMethod* called_method = RefineTargetMethod(self,
Orion Hodson811bd5f2016-12-07 11:35:37 +00001032 shadow_frame,
Orion Hodsona1be7132017-03-21 10:04:12 +00001033 handle_kind,
1034 method_handle_type,
Orion Hodson811bd5f2016-12-07 11:35:37 +00001035 callsite_type,
Orion Hodsona1be7132017-03-21 10:04:12 +00001036 receiver_reg,
1037 target_method);
1038 if (called_method == nullptr) {
1039 DCHECK(self->IsExceptionPending());
1040 return false;
1041 }
1042
1043 // Compute method information.
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001044 CodeItemDataAccessor accessor(called_method);
Orion Hodsona1be7132017-03-21 10:04:12 +00001045 uint16_t num_regs;
1046 size_t num_input_regs;
1047 size_t first_dest_reg;
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001048 if (LIKELY(accessor.HasCodeItem())) {
1049 num_regs = accessor.RegistersSize();
1050 first_dest_reg = num_regs - accessor.InsSize();
1051 num_input_regs = accessor.InsSize();
Orion Hodsona1be7132017-03-21 10:04:12 +00001052 // Parameter registers go at the end of the shadow frame.
1053 DCHECK_NE(first_dest_reg, (size_t)-1);
1054 } else {
1055 // No local regs for proxy and native methods.
1056 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1057 num_regs = num_input_regs = GetInsForProxyOrNativeMethod(called_method);
1058 first_dest_reg = 0;
1059 }
1060
1061 // Allocate shadow frame on the stack.
1062 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
1063 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
1064 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
1065 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Orion Hodson960d4f72017-11-10 15:32:38 +00001066 CopyArgumentsFromCallerFrame(shadow_frame,
1067 new_shadow_frame,
1068 operands,
1069 first_dest_reg);
Orion Hodsona1be7132017-03-21 10:04:12 +00001070 self->EndAssertNoThreadSuspension(old_cause);
1071
Jeff Hao5ea84132017-05-05 16:59:29 -07001072 bool use_interpreter_entrypoint = ClassLinker::ShouldUseInterpreterEntrypoint(
1073 called_method, called_method->GetEntryPointFromQuickCompiledCode());
1074 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001075 accessor,
Jeff Hao5ea84132017-05-05 16:59:29 -07001076 shadow_frame.GetMethod(),
1077 first_dest_reg,
1078 new_shadow_frame,
1079 result,
1080 use_interpreter_entrypoint);
Orion Hodsona1be7132017-03-21 10:04:12 +00001081 if (self->IsExceptionPending()) {
1082 return false;
1083 }
1084 return true;
Orion Hodson811bd5f2016-12-07 11:35:37 +00001085}
1086
1087} // namespace
1088
Orion Hodson960d4f72017-11-10 15:32:38 +00001089bool MethodHandleInvoke(Thread* self,
1090 ShadowFrame& shadow_frame,
1091 Handle<mirror::MethodHandle> method_handle,
1092 Handle<mirror::MethodType> callsite_type,
1093 const InstructionOperands* const operands,
1094 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +00001095 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001096 if (UNLIKELY(callsite_type->IsExactMatch(method_handle->GetMethodType()))) {
1097 // A non-exact invoke that can be invoked exactly.
Orion Hodson960d4f72017-11-10 15:32:38 +00001098 return MethodHandleInvokeExactInternal(self,
1099 shadow_frame,
1100 method_handle,
1101 callsite_type,
1102 operands,
1103 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +00001104 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +00001105 return MethodHandleInvokeInternal(self,
1106 shadow_frame,
1107 method_handle,
1108 callsite_type,
1109 operands,
1110 result);
Orion Hodson811bd5f2016-12-07 11:35:37 +00001111 }
1112}
1113
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001114bool MethodHandleInvokeExact(Thread* self,
Orion Hodson960d4f72017-11-10 15:32:38 +00001115 ShadowFrame& shadow_frame,
1116 Handle<mirror::MethodHandle> method_handle,
1117 Handle<mirror::MethodType> callsite_type,
1118 const InstructionOperands* const operands,
1119 JValue* result)
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001120 REQUIRES_SHARED(Locks::mutator_lock_) {
1121 // We need to check the nominal type of the handle in addition to the
1122 // real type. The "nominal" type is present when MethodHandle.asType is
1123 // called any handle, and results in the declared type of the handle
1124 // changing.
1125 ObjPtr<mirror::MethodType> nominal_type(method_handle->GetNominalType());
1126 if (UNLIKELY(nominal_type != nullptr)) {
1127 if (UNLIKELY(!callsite_type->IsExactMatch(nominal_type.Ptr()))) {
1128 ThrowWrongMethodTypeException(nominal_type.Ptr(), callsite_type.Get());
1129 return false;
1130 }
1131 if (LIKELY(!nominal_type->IsExactMatch(method_handle->GetMethodType()))) {
1132 // Different nominal type means we have to treat as non-exact.
Orion Hodson960d4f72017-11-10 15:32:38 +00001133 return MethodHandleInvokeInternal(self,
1134 shadow_frame,
1135 method_handle,
1136 callsite_type,
1137 operands,
1138 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001139 }
1140 }
Orion Hodson960d4f72017-11-10 15:32:38 +00001141 return MethodHandleInvokeExactInternal(self,
1142 shadow_frame,
1143 method_handle,
1144 callsite_type,
1145 operands,
1146 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001147}
1148
Orion Hodsonba28f9f2016-10-26 10:56:25 +01001149} // namespace art