Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 1 | /* |
| 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 Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 17 | #include "method_handles-inl.h" |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 18 | |
| 19 | #include "android-base/stringprintf.h" |
| 20 | |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 21 | #include "common_dex_operations.h" |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 22 | #include "jvalue-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 23 | #include "jvalue.h" |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 24 | #include "mirror/emulated_stack_frame.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 25 | #include "mirror/method_handle_impl-inl.h" |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 26 | #include "mirror/method_type.h" |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 27 | #include "reflection-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 28 | #include "reflection.h" |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 29 | #include "well_known_classes.h" |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 33 | using android::base::StringPrintf; |
| 34 | |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 35 | namespace { |
| 36 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 37 | #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 Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 46 | |
| 47 | // Assigns |type| to the primitive type associated with |klass|. Returns |
| 48 | // true iff. |klass| was a boxed type (Integer, Long etc.), false otherwise. |
| 49 | bool GetUnboxedPrimitiveType(ObjPtr<mirror::Class> klass, Primitive::Type* type) |
| 50 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 51 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 52 | 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 Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 65 | } |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 66 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 67 | PRIMITIVES_LIST(LOOKUP_PRIMITIVE); |
| 68 | #undef LOOKUP_PRIMITIVE |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 69 | return false; |
| 70 | } |
| 71 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 72 | ObjPtr<mirror::Class> GetBoxedPrimitiveClass(Primitive::Type type) |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 73 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 74 | 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 Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 86 | } |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 87 | return jni::DecodeArtMethod(m)->GetDeclaringClass(); |
| 88 | } |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 89 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 90 | bool GetUnboxedTypeAndValue(ObjPtr<mirror::Object> o, Primitive::Type* type, JValue* value) |
| 91 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 92 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 93 | ObjPtr<mirror::Class> klass = o->GetClass(); |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 94 | ArtField* primitive_field = &klass->GetIFieldsPtr()->At(0); |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 95 | #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 Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 100 | } |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 101 | PRIMITIVES_LIST(CASE_PRIMITIVE) |
| 102 | #undef CASE_PRIMITIVE |
| 103 | return false; |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | inline bool IsReferenceType(Primitive::Type type) { |
| 107 | return type == Primitive::kPrimNot; |
| 108 | } |
| 109 | |
| 110 | inline bool IsPrimitiveType(Primitive::Type type) { |
| 111 | return !IsReferenceType(type); |
| 112 | } |
| 113 | |
| 114 | } // namespace |
| 115 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 116 | bool 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 Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | if (Primitive::IsNumericType(to_primitive) && from->DescriptorEquals("Ljava/lang/Number;")) { |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 156 | // Number might be unboxed into any of the number primitive types. |
| 157 | return true; |
| 158 | } |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 159 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 160 | Primitive::Type unboxed_type; |
| 161 | if (GetUnboxedPrimitiveType(from, &unboxed_type)) { |
Orion Hodson | f1412b4 | 2016-11-11 12:03:29 +0000 | [diff] [blame] | 162 | if (unboxed_type == to_primitive) { |
| 163 | // Straightforward unboxing conversion such as Boolean => boolean. |
| 164 | return true; |
Orion Hodson | f1412b4 | 2016-11-11 12:03:29 +0000 | [diff] [blame] | 165 | } |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 166 | |
| 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 Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | bool 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 Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 190 | bool 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 Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 204 | // 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 Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 214 | // 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 Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 219 | if (UNLIKELY(!ConvertPrimitiveValueNoThrow(from_type, to_type, src_value, value))) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 220 | ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get()); |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 221 | 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 Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 233 | Handle<mirror::Object> h_obj(hs.NewHandle(src_value.GetL())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 234 | if (h_obj != nullptr && !to->IsAssignableFrom(h_obj->GetClass())) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 235 | 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 Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 242 | // 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 Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 249 | if (boxed_from_class->IsSubClass(to)) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 250 | type = from_type; |
| 251 | } else { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 252 | ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get()); |
| 253 | return false; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if (UNLIKELY(from_type != type)) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 258 | ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get()); |
| 259 | return false; |
| 260 | } |
| 261 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 262 | if (!ConvertPrimitiveValueNoThrow(from_type, type, src_value, value)) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 263 | ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get()); |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | // Then perform the actual boxing, and then set the reference. |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 268 | ObjPtr<mirror::Object> boxed = BoxPrimitive(type, src_value); |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 269 | 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 Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 276 | ObjPtr<mirror::Object> from_obj(src_value.GetL()); |
| 277 | if (UNLIKELY(from_obj == nullptr)) { |
| 278 | 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 Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 283 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 284 | Primitive::Type unboxed_type; |
| 285 | JValue unboxed_value; |
| 286 | if (UNLIKELY(!GetUnboxedTypeAndValue(from_obj, &unboxed_type, &unboxed_value))) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 287 | ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get()); |
| 288 | return false; |
| 289 | } |
| 290 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 291 | if (UNLIKELY(!ConvertPrimitiveValueNoThrow(unboxed_type, to_type, unboxed_value, value))) { |
| 292 | ThrowClassCastException(from, to); |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 293 | return false; |
| 294 | } |
| 295 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 296 | return true; |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 300 | namespace { |
| 301 | |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 302 | inline void CopyArgumentsFromCallerFrame(const ShadowFrame& caller_frame, |
| 303 | ShadowFrame* callee_frame, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 304 | const InstructionOperands* const operands, |
| 305 | const size_t first_dst_reg) |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 306 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 307 | for (size_t i = 0; i < operands->GetNumberOfOperands(); ++i) { |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 308 | size_t dst_reg = first_dst_reg + i; |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 309 | size_t src_reg = operands->GetOperand(i); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 310 | // Uint required, so that sign extension does not make this wrong on 64-bit systems |
| 311 | uint32_t src_value = caller_frame.GetVReg(src_reg); |
| 312 | ObjPtr<mirror::Object> o = caller_frame.GetVRegReference<kVerifyNone>(src_reg); |
| 313 | // If both register locations contains the same value, the register probably holds a reference. |
| 314 | // Note: As an optimization, non-moving collectors leave a stale reference value |
| 315 | // in the references array even after the original vreg was overwritten to a non-reference. |
| 316 | if (src_value == reinterpret_cast<uintptr_t>(o.Ptr())) { |
| 317 | callee_frame->SetVRegReference(dst_reg, o.Ptr()); |
| 318 | } else { |
| 319 | callee_frame->SetVReg(dst_reg, src_value); |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 324 | inline bool ConvertAndCopyArgumentsFromCallerFrame( |
| 325 | Thread* self, |
| 326 | Handle<mirror::MethodType> callsite_type, |
| 327 | Handle<mirror::MethodType> callee_type, |
| 328 | const ShadowFrame& caller_frame, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 329 | uint32_t first_dest_reg, |
| 330 | const InstructionOperands* const operands, |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 331 | ShadowFrame* callee_frame) |
| 332 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 333 | ObjPtr<mirror::ObjectArray<mirror::Class>> from_types(callsite_type->GetPTypes()); |
| 334 | ObjPtr<mirror::ObjectArray<mirror::Class>> to_types(callee_type->GetPTypes()); |
| 335 | |
| 336 | const int32_t num_method_params = from_types->GetLength(); |
| 337 | if (to_types->GetLength() != num_method_params) { |
| 338 | ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get()); |
| 339 | return false; |
| 340 | } |
| 341 | |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 342 | ShadowFrameGetter getter(operands, caller_frame); |
| 343 | ShadowFrameSetter setter(callee_frame, first_dest_reg); |
| 344 | return PerformConversions<ShadowFrameGetter, ShadowFrameSetter>(self, |
| 345 | callsite_type, |
| 346 | callee_type, |
| 347 | &getter, |
| 348 | &setter, |
| 349 | num_method_params); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 352 | inline bool IsInvoke(const mirror::MethodHandle::Kind handle_kind) { |
| 353 | return handle_kind <= mirror::MethodHandle::Kind::kLastInvokeKind; |
| 354 | } |
| 355 | |
| 356 | inline bool IsInvokeTransform(const mirror::MethodHandle::Kind handle_kind) { |
| 357 | return (handle_kind == mirror::MethodHandle::Kind::kInvokeTransform |
| 358 | || handle_kind == mirror::MethodHandle::Kind::kInvokeCallSiteTransform); |
| 359 | } |
| 360 | |
| 361 | inline bool IsFieldAccess(mirror::MethodHandle::Kind handle_kind) { |
| 362 | return (handle_kind >= mirror::MethodHandle::Kind::kFirstAccessorKind |
| 363 | && handle_kind <= mirror::MethodHandle::Kind::kLastAccessorKind); |
| 364 | } |
| 365 | |
| 366 | // Calculate the number of ins for a proxy or native method, where we |
| 367 | // can't just look at the code item. |
| 368 | static inline size_t GetInsForProxyOrNativeMethod(ArtMethod* method) |
| 369 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 370 | DCHECK(method->IsNative() || method->IsProxyMethod()); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 371 | method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 372 | uint32_t shorty_length = 0; |
| 373 | const char* shorty = method->GetShorty(&shorty_length); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 374 | |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 375 | // Static methods do not include the receiver. The receiver isn't included |
| 376 | // in the shorty_length though the return value is. |
| 377 | size_t num_ins = method->IsStatic() ? shorty_length - 1 : shorty_length; |
| 378 | for (const char* c = shorty + 1; *c != '\0'; ++c) { |
| 379 | if (*c == 'J' || *c == 'D') { |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 380 | ++num_ins; |
| 381 | } |
| 382 | } |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 383 | return num_ins; |
| 384 | } |
| 385 | |
| 386 | // Returns true iff. the callsite type for a polymorphic invoke is transformer |
| 387 | // like, i.e that it has a single input argument whose type is |
| 388 | // dalvik.system.EmulatedStackFrame. |
| 389 | static inline bool IsCallerTransformer(Handle<mirror::MethodType> callsite_type) |
| 390 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 391 | ObjPtr<mirror::ObjectArray<mirror::Class>> param_types(callsite_type->GetPTypes()); |
| 392 | if (param_types->GetLength() == 1) { |
| 393 | ObjPtr<mirror::Class> param(param_types->GetWithoutChecks(0)); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 394 | // NB Comparing descriptor here as it appears faster in cycle simulation than using: |
| 395 | // param == WellKnownClasses::ToClass(WellKnownClasses::dalvik_system_EmulatedStackFrame) |
| 396 | // Costs are 98 vs 173 cycles per invocation. |
| 397 | return param->DescriptorEquals("Ldalvik/system/EmulatedStackFrame;"); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | return false; |
| 401 | } |
| 402 | |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 403 | static inline bool MethodHandleInvokeMethod(ArtMethod* called_method, |
| 404 | Handle<mirror::MethodType> callsite_type, |
| 405 | Handle<mirror::MethodType> target_type, |
| 406 | Thread* self, |
| 407 | ShadowFrame& shadow_frame, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 408 | const InstructionOperands* const operands, |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 409 | JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 410 | // Compute method information. |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame^] | 411 | CodeItemDataAccessor accessor(called_method); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 412 | |
| 413 | // Number of registers for the callee's call frame. Note that for non-exact |
| 414 | // invokes, we always derive this information from the callee method. We |
| 415 | // cannot guarantee during verification that the number of registers encoded |
| 416 | // in the invoke is equal to the number of ins for the callee. This is because |
| 417 | // some transformations (such as boxing a long -> Long or wideining an |
| 418 | // int -> long will change that number. |
| 419 | uint16_t num_regs; |
| 420 | size_t num_input_regs; |
| 421 | size_t first_dest_reg; |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame^] | 422 | if (LIKELY(accessor.HasCodeItem())) { |
| 423 | num_regs = accessor.RegistersSize(); |
| 424 | first_dest_reg = num_regs - accessor.InsSize(); |
| 425 | num_input_regs = accessor.InsSize(); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 426 | // Parameter registers go at the end of the shadow frame. |
| 427 | DCHECK_NE(first_dest_reg, (size_t)-1); |
| 428 | } else { |
| 429 | // No local regs for proxy and native methods. |
| 430 | DCHECK(called_method->IsNative() || called_method->IsProxyMethod()); |
| 431 | num_regs = num_input_regs = GetInsForProxyOrNativeMethod(called_method); |
| 432 | first_dest_reg = 0; |
| 433 | } |
| 434 | |
| 435 | // Allocate shadow frame on the stack. |
| 436 | ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr = |
| 437 | CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0); |
| 438 | ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get(); |
| 439 | |
| 440 | // Whether this polymorphic invoke was issued by a transformer method. |
| 441 | bool is_caller_transformer = false; |
| 442 | // Thread might be suspended during PerformArgumentConversions due to the |
| 443 | // allocations performed during boxing. |
| 444 | { |
| 445 | ScopedStackedShadowFramePusher pusher( |
| 446 | self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction); |
| 447 | if (callsite_type->IsExactMatch(target_type.Get())) { |
| 448 | // This is an exact invoke, we can take the fast path of just copying all |
| 449 | // registers without performing any argument conversions. |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 450 | CopyArgumentsFromCallerFrame(shadow_frame, |
| 451 | new_shadow_frame, |
| 452 | operands, |
| 453 | first_dest_reg); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 454 | } else { |
| 455 | // This includes the case where we're entering this invoke-polymorphic |
| 456 | // from a transformer method. In that case, the callsite_type will contain |
| 457 | // a single argument of type dalvik.system.EmulatedStackFrame. In that |
| 458 | // case, we'll have to unmarshal the EmulatedStackFrame into the |
| 459 | // new_shadow_frame and perform argument conversions on it. |
| 460 | if (IsCallerTransformer(callsite_type)) { |
| 461 | is_caller_transformer = true; |
| 462 | // The emulated stack frame is the first and only argument when we're coming |
| 463 | // through from a transformer. |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 464 | size_t first_arg_register = operands->GetOperand(0); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 465 | ObjPtr<mirror::EmulatedStackFrame> emulated_stack_frame( |
| 466 | reinterpret_cast<mirror::EmulatedStackFrame*>( |
| 467 | shadow_frame.GetVRegReference(first_arg_register))); |
| 468 | if (!emulated_stack_frame->WriteToShadowFrame(self, |
| 469 | target_type, |
| 470 | first_dest_reg, |
| 471 | new_shadow_frame)) { |
| 472 | DCHECK(self->IsExceptionPending()); |
| 473 | result->SetL(0); |
| 474 | return false; |
| 475 | } |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 476 | } else { |
| 477 | if (!callsite_type->IsConvertible(target_type.Get())) { |
| 478 | ThrowWrongMethodTypeException(target_type.Get(), callsite_type.Get()); |
| 479 | return false; |
| 480 | } |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 481 | if (!ConvertAndCopyArgumentsFromCallerFrame(self, |
| 482 | callsite_type, |
| 483 | target_type, |
| 484 | shadow_frame, |
| 485 | first_dest_reg, |
| 486 | operands, |
| 487 | new_shadow_frame)) { |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 488 | DCHECK(self->IsExceptionPending()); |
| 489 | result->SetL(0); |
| 490 | return false; |
| 491 | } |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 496 | bool use_interpreter_entrypoint = ClassLinker::ShouldUseInterpreterEntrypoint( |
| 497 | called_method, called_method->GetEntryPointFromQuickCompiledCode()); |
| 498 | PerformCall(self, |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame^] | 499 | accessor, |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 500 | shadow_frame.GetMethod(), |
| 501 | first_dest_reg, |
| 502 | new_shadow_frame, |
| 503 | result, |
| 504 | use_interpreter_entrypoint); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 505 | if (self->IsExceptionPending()) { |
| 506 | return false; |
| 507 | } |
| 508 | |
| 509 | // If the caller of this signature polymorphic method was a transformer, |
| 510 | // we need to copy the result back out to the emulated stack frame. |
| 511 | if (is_caller_transformer) { |
| 512 | StackHandleScope<2> hs(self); |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 513 | size_t first_callee_register = operands->GetOperand(0); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 514 | Handle<mirror::EmulatedStackFrame> emulated_stack_frame( |
| 515 | hs.NewHandle(reinterpret_cast<mirror::EmulatedStackFrame*>( |
| 516 | shadow_frame.GetVRegReference(first_callee_register)))); |
| 517 | Handle<mirror::MethodType> emulated_stack_type(hs.NewHandle(emulated_stack_frame->GetType())); |
| 518 | JValue local_result; |
| 519 | local_result.SetJ(result->GetJ()); |
| 520 | |
| 521 | if (ConvertReturnValue(emulated_stack_type, target_type, &local_result)) { |
| 522 | emulated_stack_frame->SetReturnValue(self, local_result); |
| 523 | return true; |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 524 | } |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 525 | |
| 526 | DCHECK(self->IsExceptionPending()); |
| 527 | return false; |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 528 | } |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 529 | |
| 530 | return ConvertReturnValue(callsite_type, target_type, result); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 533 | static inline bool MethodHandleInvokeTransform(ArtMethod* called_method, |
| 534 | Handle<mirror::MethodType> callsite_type, |
| 535 | Handle<mirror::MethodType> callee_type, |
| 536 | Thread* self, |
| 537 | ShadowFrame& shadow_frame, |
| 538 | Handle<mirror::MethodHandle> receiver, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 539 | const InstructionOperands* const operands, |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 540 | JValue* result) |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 541 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 542 | // This can be fixed to two, because the method we're calling here |
| 543 | // (MethodHandle.transformInternal) doesn't have any locals and the signature |
| 544 | // is known : |
| 545 | // |
| 546 | // private MethodHandle.transformInternal(EmulatedStackFrame sf); |
| 547 | // |
| 548 | // This means we need only two vregs : |
| 549 | // - One for the receiver object. |
| 550 | // - One for the only method argument (an EmulatedStackFrame). |
| 551 | static constexpr size_t kNumRegsForTransform = 2; |
| 552 | |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame^] | 553 | CodeItemDataAccessor accessor(called_method); |
| 554 | DCHECK_EQ(kNumRegsForTransform, accessor.RegistersSize()); |
| 555 | DCHECK_EQ(kNumRegsForTransform, accessor.InsSize()); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 556 | |
| 557 | ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr = |
| 558 | CREATE_SHADOW_FRAME(kNumRegsForTransform, &shadow_frame, called_method, /* dex pc */ 0); |
| 559 | ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get(); |
| 560 | |
| 561 | StackHandleScope<1> hs(self); |
| 562 | MutableHandle<mirror::EmulatedStackFrame> sf(hs.NewHandle<mirror::EmulatedStackFrame>(nullptr)); |
| 563 | if (IsCallerTransformer(callsite_type)) { |
| 564 | // If we're entering this transformer from another transformer, we can pass |
| 565 | // through the handle directly to the callee, instead of having to |
| 566 | // instantiate a new stack frame based on the shadow frame. |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 567 | size_t first_callee_register = operands->GetOperand(0); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 568 | sf.Assign(reinterpret_cast<mirror::EmulatedStackFrame*>( |
| 569 | shadow_frame.GetVRegReference(first_callee_register))); |
| 570 | } else { |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 571 | sf.Assign(mirror::EmulatedStackFrame::CreateFromShadowFrameAndArgs(self, |
| 572 | callsite_type, |
| 573 | callee_type, |
| 574 | shadow_frame, |
| 575 | operands)); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 576 | |
| 577 | // Something went wrong while creating the emulated stack frame, we should |
| 578 | // throw the pending exception. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 579 | if (sf == nullptr) { |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 580 | DCHECK(self->IsExceptionPending()); |
| 581 | return false; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | new_shadow_frame->SetVRegReference(0, receiver.Get()); |
| 586 | new_shadow_frame->SetVRegReference(1, sf.Get()); |
| 587 | |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 588 | bool use_interpreter_entrypoint = ClassLinker::ShouldUseInterpreterEntrypoint( |
| 589 | called_method, called_method->GetEntryPointFromQuickCompiledCode()); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 590 | PerformCall(self, |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame^] | 591 | accessor, |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 592 | shadow_frame.GetMethod(), |
| 593 | 0 /* first destination register */, |
| 594 | new_shadow_frame, |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 595 | result, |
| 596 | use_interpreter_entrypoint); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 597 | if (self->IsExceptionPending()) { |
| 598 | return false; |
| 599 | } |
| 600 | |
| 601 | // If the called transformer method we called has returned a value, then we |
| 602 | // need to copy it back to |result|. |
| 603 | sf->GetReturnValue(self, result); |
| 604 | return ConvertReturnValue(callsite_type, callee_type, result); |
| 605 | } |
| 606 | |
| 607 | inline static ObjPtr<mirror::Class> GetAndInitializeDeclaringClass(Thread* self, ArtField* field) |
| 608 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 609 | // Method handle invocations on static fields should ensure class is |
| 610 | // initialized. This usually happens when an instance is constructed |
| 611 | // or class members referenced, but this is not guaranteed when |
| 612 | // looking up method handles. |
| 613 | ObjPtr<mirror::Class> klass = field->GetDeclaringClass(); |
| 614 | if (UNLIKELY(!klass->IsInitialized())) { |
| 615 | StackHandleScope<1> hs(self); |
| 616 | HandleWrapperObjPtr<mirror::Class> h(hs.NewHandleWrapper(&klass)); |
| 617 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h, true, true)) { |
| 618 | DCHECK(self->IsExceptionPending()); |
| 619 | return nullptr; |
| 620 | } |
| 621 | } |
| 622 | return klass; |
| 623 | } |
| 624 | |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 625 | ArtMethod* RefineTargetMethod(Thread* self, |
| 626 | ShadowFrame& shadow_frame, |
| 627 | const mirror::MethodHandle::Kind& handle_kind, |
| 628 | Handle<mirror::MethodType> handle_type, |
| 629 | Handle<mirror::MethodType> callsite_type, |
| 630 | const uint32_t receiver_reg, |
| 631 | ArtMethod* target_method) |
| 632 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 633 | if (handle_kind == mirror::MethodHandle::Kind::kInvokeVirtual || |
| 634 | handle_kind == mirror::MethodHandle::Kind::kInvokeInterface) { |
| 635 | // For virtual and interface methods ensure target_method points to |
| 636 | // the actual method to invoke. |
| 637 | ObjPtr<mirror::Object> receiver(shadow_frame.GetVRegReference(receiver_reg)); |
| 638 | if (IsCallerTransformer(callsite_type)) { |
| 639 | // The current receiver is an emulated stack frame, the method's |
| 640 | // receiver needs to be fetched from there as the emulated frame |
| 641 | // will be unpacked into a new frame. |
| 642 | receiver = ObjPtr<mirror::EmulatedStackFrame>::DownCast(receiver)->GetReceiver(); |
| 643 | } |
| 644 | |
| 645 | ObjPtr<mirror::Class> declaring_class(target_method->GetDeclaringClass()); |
| 646 | if (receiver == nullptr || receiver->GetClass() != declaring_class) { |
| 647 | // Verify that _vRegC is an object reference and of the type expected by |
| 648 | // the receiver. |
| 649 | if (!VerifyObjectIsClass(receiver, declaring_class)) { |
| 650 | DCHECK(self->IsExceptionPending()); |
| 651 | return nullptr; |
| 652 | } |
| 653 | return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface( |
| 654 | target_method, kRuntimePointerSize); |
| 655 | } |
| 656 | } else if (handle_kind == mirror::MethodHandle::Kind::kInvokeDirect) { |
| 657 | // String constructors are a special case, they are replaced with |
| 658 | // StringFactory methods. |
| 659 | if (target_method->IsConstructor() && target_method->GetDeclaringClass()->IsStringClass()) { |
| 660 | DCHECK(handle_type->GetRType()->IsStringClass()); |
| 661 | return WellKnownClasses::StringInitToStringFactory(target_method); |
| 662 | } |
| 663 | } else if (handle_kind == mirror::MethodHandle::Kind::kInvokeSuper) { |
| 664 | ObjPtr<mirror::Class> declaring_class = target_method->GetDeclaringClass(); |
| 665 | |
| 666 | // Note that we're not dynamically dispatching on the type of the receiver |
| 667 | // here. We use the static type of the "receiver" object that we've |
| 668 | // recorded in the method handle's type, which will be the same as the |
| 669 | // special caller that was specified at the point of lookup. |
| 670 | ObjPtr<mirror::Class> referrer_class = handle_type->GetPTypes()->Get(0); |
| 671 | if (!declaring_class->IsInterface()) { |
| 672 | ObjPtr<mirror::Class> super_class = referrer_class->GetSuperClass(); |
| 673 | uint16_t vtable_index = target_method->GetMethodIndex(); |
| 674 | DCHECK(super_class != nullptr); |
| 675 | DCHECK(super_class->HasVTable()); |
| 676 | // Note that super_class is a super of referrer_class and target_method |
| 677 | // will always be declared by super_class (or one of its super classes). |
| 678 | DCHECK_LT(vtable_index, super_class->GetVTableLength()); |
| 679 | return super_class->GetVTableEntry(vtable_index, kRuntimePointerSize); |
| 680 | } else { |
| 681 | return referrer_class->FindVirtualMethodForInterfaceSuper(target_method, kRuntimePointerSize); |
| 682 | } |
| 683 | } |
| 684 | return target_method; |
| 685 | } |
| 686 | |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 687 | bool DoInvokePolymorphicMethod(Thread* self, |
| 688 | ShadowFrame& shadow_frame, |
| 689 | Handle<mirror::MethodHandle> method_handle, |
| 690 | Handle<mirror::MethodType> callsite_type, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 691 | const InstructionOperands* const operands, |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 692 | JValue* result) |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 693 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 694 | StackHandleScope<1> hs(self); |
| 695 | Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType())); |
| 696 | const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind(); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 697 | DCHECK(IsInvoke(handle_kind)); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 698 | |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 699 | // Get the method we're actually invoking along with the kind of |
| 700 | // invoke that is desired. We don't need to perform access checks at this |
| 701 | // point because they would have been performed on our behalf at the point |
| 702 | // of creation of the method handle. |
| 703 | ArtMethod* target_method = method_handle->GetTargetMethod(); |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 704 | uint32_t receiver_reg = (operands->GetNumberOfOperands() > 0) ? operands->GetOperand(0) : 0u; |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 705 | ArtMethod* called_method = RefineTargetMethod(self, |
| 706 | shadow_frame, |
| 707 | handle_kind, |
| 708 | handle_type, |
| 709 | callsite_type, |
| 710 | receiver_reg, |
| 711 | target_method); |
| 712 | if (called_method == nullptr) { |
| 713 | DCHECK(self->IsExceptionPending()); |
| 714 | return false; |
| 715 | } |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 716 | |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 717 | if (IsInvokeTransform(handle_kind)) { |
| 718 | // There are two cases here - method handles representing regular |
| 719 | // transforms and those representing call site transforms. Method |
| 720 | // handles for call site transforms adapt their MethodType to match |
| 721 | // the call site. For these, the |callee_type| is the same as the |
| 722 | // |callsite_type|. The VarargsCollector is such a tranform, its |
| 723 | // method type depends on the call site, ie. x(a) or x(a, b), or |
| 724 | // x(a, b, c). The VarargsCollector invokes a variable arity method |
| 725 | // with the arity arguments in an array. |
| 726 | Handle<mirror::MethodType> callee_type = |
| 727 | (handle_kind == mirror::MethodHandle::Kind::kInvokeCallSiteTransform) ? callsite_type |
| 728 | : handle_type; |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 729 | return MethodHandleInvokeTransform(called_method, |
| 730 | callsite_type, |
| 731 | callee_type, |
| 732 | self, |
| 733 | shadow_frame, |
| 734 | method_handle /* receiver */, |
| 735 | operands, |
| 736 | result); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 737 | } else { |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 738 | return MethodHandleInvokeMethod(called_method, |
| 739 | callsite_type, |
| 740 | handle_type, |
| 741 | self, |
| 742 | shadow_frame, |
| 743 | operands, |
| 744 | result); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | |
| 748 | // Helper for getters in invoke-polymorphic. |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 749 | inline static void MethodHandleFieldGet(Thread* self, |
| 750 | const ShadowFrame& shadow_frame, |
| 751 | ObjPtr<mirror::Object>& obj, |
| 752 | ArtField* field, |
| 753 | Primitive::Type field_type, |
| 754 | JValue* result) |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 755 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 756 | switch (field_type) { |
| 757 | case Primitive::kPrimBoolean: |
| 758 | DoFieldGetCommon<Primitive::kPrimBoolean>(self, shadow_frame, obj, field, result); |
| 759 | break; |
| 760 | case Primitive::kPrimByte: |
| 761 | DoFieldGetCommon<Primitive::kPrimByte>(self, shadow_frame, obj, field, result); |
| 762 | break; |
| 763 | case Primitive::kPrimChar: |
| 764 | DoFieldGetCommon<Primitive::kPrimChar>(self, shadow_frame, obj, field, result); |
| 765 | break; |
| 766 | case Primitive::kPrimShort: |
| 767 | DoFieldGetCommon<Primitive::kPrimShort>(self, shadow_frame, obj, field, result); |
| 768 | break; |
| 769 | case Primitive::kPrimInt: |
| 770 | DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result); |
| 771 | break; |
| 772 | case Primitive::kPrimLong: |
| 773 | DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result); |
| 774 | break; |
| 775 | case Primitive::kPrimFloat: |
| 776 | DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result); |
| 777 | break; |
| 778 | case Primitive::kPrimDouble: |
| 779 | DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result); |
| 780 | break; |
| 781 | case Primitive::kPrimNot: |
| 782 | DoFieldGetCommon<Primitive::kPrimNot>(self, shadow_frame, obj, field, result); |
| 783 | break; |
| 784 | case Primitive::kPrimVoid: |
| 785 | LOG(FATAL) << "Unreachable: " << field_type; |
| 786 | UNREACHABLE(); |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | // Helper for setters in invoke-polymorphic. |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 791 | inline bool MethodHandleFieldPut(Thread* self, |
| 792 | ShadowFrame& shadow_frame, |
| 793 | ObjPtr<mirror::Object>& obj, |
| 794 | ArtField* field, |
| 795 | Primitive::Type field_type, |
| 796 | JValue& value) |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 797 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 798 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
| 799 | static const bool kTransaction = false; // Not in a transaction. |
| 800 | static const bool kAssignabilityCheck = false; // No access check. |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 801 | switch (field_type) { |
| 802 | case Primitive::kPrimBoolean: |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 803 | return |
| 804 | DoFieldPutCommon<Primitive::kPrimBoolean, kAssignabilityCheck, kTransaction>( |
| 805 | self, shadow_frame, obj, field, value); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 806 | case Primitive::kPrimByte: |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 807 | return DoFieldPutCommon<Primitive::kPrimByte, kAssignabilityCheck, kTransaction>( |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 808 | self, shadow_frame, obj, field, value); |
| 809 | case Primitive::kPrimChar: |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 810 | return DoFieldPutCommon<Primitive::kPrimChar, kAssignabilityCheck, kTransaction>( |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 811 | self, shadow_frame, obj, field, value); |
| 812 | case Primitive::kPrimShort: |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 813 | return DoFieldPutCommon<Primitive::kPrimShort, kAssignabilityCheck, kTransaction>( |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 814 | self, shadow_frame, obj, field, value); |
| 815 | case Primitive::kPrimInt: |
| 816 | case Primitive::kPrimFloat: |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 817 | return DoFieldPutCommon<Primitive::kPrimInt, kAssignabilityCheck, kTransaction>( |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 818 | self, shadow_frame, obj, field, value); |
| 819 | case Primitive::kPrimLong: |
| 820 | case Primitive::kPrimDouble: |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 821 | return DoFieldPutCommon<Primitive::kPrimLong, kAssignabilityCheck, kTransaction>( |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 822 | self, shadow_frame, obj, field, value); |
| 823 | case Primitive::kPrimNot: |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 824 | return DoFieldPutCommon<Primitive::kPrimNot, kAssignabilityCheck, kTransaction>( |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 825 | self, shadow_frame, obj, field, value); |
| 826 | case Primitive::kPrimVoid: |
| 827 | LOG(FATAL) << "Unreachable: " << field_type; |
| 828 | UNREACHABLE(); |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | static JValue GetValueFromShadowFrame(const ShadowFrame& shadow_frame, |
| 833 | Primitive::Type field_type, |
| 834 | uint32_t vreg) |
| 835 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 836 | JValue field_value; |
| 837 | switch (field_type) { |
| 838 | case Primitive::kPrimBoolean: |
| 839 | field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg))); |
| 840 | break; |
| 841 | case Primitive::kPrimByte: |
| 842 | field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg))); |
| 843 | break; |
| 844 | case Primitive::kPrimChar: |
| 845 | field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg))); |
| 846 | break; |
| 847 | case Primitive::kPrimShort: |
| 848 | field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg))); |
| 849 | break; |
| 850 | case Primitive::kPrimInt: |
| 851 | case Primitive::kPrimFloat: |
| 852 | field_value.SetI(shadow_frame.GetVReg(vreg)); |
| 853 | break; |
| 854 | case Primitive::kPrimLong: |
| 855 | case Primitive::kPrimDouble: |
| 856 | field_value.SetJ(shadow_frame.GetVRegLong(vreg)); |
| 857 | break; |
| 858 | case Primitive::kPrimNot: |
| 859 | field_value.SetL(shadow_frame.GetVRegReference(vreg)); |
| 860 | break; |
| 861 | case Primitive::kPrimVoid: |
| 862 | LOG(FATAL) << "Unreachable: " << field_type; |
| 863 | UNREACHABLE(); |
| 864 | } |
| 865 | return field_value; |
| 866 | } |
| 867 | |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 868 | template <bool do_conversions> |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 869 | bool MethodHandleFieldAccess(Thread* self, |
| 870 | ShadowFrame& shadow_frame, |
| 871 | Handle<mirror::MethodHandle> method_handle, |
| 872 | Handle<mirror::MethodType> callsite_type, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 873 | const InstructionOperands* const operands, |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 874 | JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 875 | StackHandleScope<1> hs(self); |
| 876 | Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType())); |
| 877 | const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind(); |
| 878 | ArtField* field = method_handle->GetTargetField(); |
| 879 | Primitive::Type field_type = field->GetTypeAsPrimitiveType(); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 880 | switch (handle_kind) { |
| 881 | case mirror::MethodHandle::kInstanceGet: { |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 882 | size_t obj_reg = operands->GetOperand(0); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 883 | ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(obj_reg); |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 884 | MethodHandleFieldGet(self, shadow_frame, obj, field, field_type, result); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 885 | if (do_conversions && !ConvertReturnValue(callsite_type, handle_type, result)) { |
| 886 | DCHECK(self->IsExceptionPending()); |
| 887 | return false; |
| 888 | } |
| 889 | return true; |
| 890 | } |
| 891 | case mirror::MethodHandle::kStaticGet: { |
| 892 | ObjPtr<mirror::Object> obj = GetAndInitializeDeclaringClass(self, field); |
| 893 | if (obj == nullptr) { |
| 894 | DCHECK(self->IsExceptionPending()); |
| 895 | return false; |
| 896 | } |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 897 | MethodHandleFieldGet(self, shadow_frame, obj, field, field_type, result); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 898 | if (do_conversions && !ConvertReturnValue(callsite_type, handle_type, result)) { |
| 899 | DCHECK(self->IsExceptionPending()); |
| 900 | return false; |
| 901 | } |
| 902 | return true; |
| 903 | } |
| 904 | case mirror::MethodHandle::kInstancePut: { |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 905 | size_t obj_reg = operands->GetOperand(0); |
| 906 | size_t value_reg = operands->GetOperand(1); |
Mathieu Chartier | 71b1708 | 2017-04-17 20:12:29 -0700 | [diff] [blame] | 907 | const size_t kPTypeIndex = 1; |
| 908 | // Use ptypes instead of field type since we may be unboxing a reference for a primitive |
| 909 | // field. The field type is incorrect for this case. |
| 910 | JValue value = GetValueFromShadowFrame( |
| 911 | shadow_frame, |
| 912 | callsite_type->GetPTypes()->Get(kPTypeIndex)->GetPrimitiveType(), |
| 913 | value_reg); |
| 914 | if (do_conversions && !ConvertArgumentValue(callsite_type, |
| 915 | handle_type, |
| 916 | kPTypeIndex, |
| 917 | &value)) { |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 918 | DCHECK(self->IsExceptionPending()); |
| 919 | return false; |
| 920 | } |
| 921 | ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(obj_reg); |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 922 | return MethodHandleFieldPut(self, shadow_frame, obj, field, field_type, value); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 923 | } |
| 924 | case mirror::MethodHandle::kStaticPut: { |
| 925 | ObjPtr<mirror::Object> obj = GetAndInitializeDeclaringClass(self, field); |
| 926 | if (obj == nullptr) { |
| 927 | DCHECK(self->IsExceptionPending()); |
| 928 | return false; |
| 929 | } |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 930 | size_t value_reg = operands->GetOperand(0); |
Mathieu Chartier | 71b1708 | 2017-04-17 20:12:29 -0700 | [diff] [blame] | 931 | const size_t kPTypeIndex = 0; |
| 932 | // Use ptypes instead of field type since we may be unboxing a reference for a primitive |
| 933 | // field. The field type is incorrect for this case. |
| 934 | JValue value = GetValueFromShadowFrame( |
| 935 | shadow_frame, |
| 936 | callsite_type->GetPTypes()->Get(kPTypeIndex)->GetPrimitiveType(), |
| 937 | value_reg); |
| 938 | if (do_conversions && !ConvertArgumentValue(callsite_type, |
| 939 | handle_type, |
| 940 | kPTypeIndex, |
| 941 | &value)) { |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 942 | DCHECK(self->IsExceptionPending()); |
| 943 | return false; |
| 944 | } |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 945 | return MethodHandleFieldPut(self, shadow_frame, obj, field, field_type, value); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 946 | } |
| 947 | default: |
| 948 | LOG(FATAL) << "Unreachable: " << handle_kind; |
| 949 | UNREACHABLE(); |
| 950 | } |
| 951 | } |
| 952 | |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 953 | static inline bool MethodHandleInvokeInternal(Thread* self, |
| 954 | ShadowFrame& shadow_frame, |
| 955 | Handle<mirror::MethodHandle> method_handle, |
| 956 | Handle<mirror::MethodType> callsite_type, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 957 | const InstructionOperands* const operands, |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 958 | JValue* result) |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 959 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 960 | const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind(); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 961 | if (IsFieldAccess(handle_kind)) { |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 962 | ObjPtr<mirror::MethodType> handle_type(method_handle->GetMethodType()); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 963 | DCHECK(!callsite_type->IsExactMatch(handle_type.Ptr())); |
| 964 | if (!callsite_type->IsConvertible(handle_type.Ptr())) { |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 965 | ThrowWrongMethodTypeException(handle_type.Ptr(), callsite_type.Get()); |
| 966 | return false; |
| 967 | } |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 968 | const bool do_convert = true; |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 969 | return MethodHandleFieldAccess<do_convert>( |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 970 | self, |
| 971 | shadow_frame, |
| 972 | method_handle, |
| 973 | callsite_type, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 974 | operands, |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 975 | result); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 976 | } |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 977 | return DoInvokePolymorphicMethod(self, |
| 978 | shadow_frame, |
| 979 | method_handle, |
| 980 | callsite_type, |
| 981 | operands, |
| 982 | result); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 985 | static inline bool MethodHandleInvokeExactInternal( |
| 986 | Thread* self, |
| 987 | ShadowFrame& shadow_frame, |
| 988 | Handle<mirror::MethodHandle> method_handle, |
| 989 | Handle<mirror::MethodType> callsite_type, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 990 | const InstructionOperands* const operands, |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 991 | JValue* result) |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 992 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 993 | StackHandleScope<1> hs(self); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 994 | Handle<mirror::MethodType> method_handle_type(hs.NewHandle(method_handle->GetMethodType())); |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 995 | if (!callsite_type->IsExactMatch(method_handle_type.Get())) { |
| 996 | ThrowWrongMethodTypeException(method_handle_type.Get(), callsite_type.Get()); |
| 997 | return false; |
| 998 | } |
| 999 | |
| 1000 | const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind(); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 1001 | if (IsFieldAccess(handle_kind)) { |
| 1002 | const bool do_convert = false; |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1003 | return MethodHandleFieldAccess<do_convert>(self, |
| 1004 | shadow_frame, |
| 1005 | method_handle, |
| 1006 | callsite_type, |
| 1007 | operands, |
| 1008 | result); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 1011 | // Slow-path check. |
| 1012 | if (IsInvokeTransform(handle_kind) || IsCallerTransformer(callsite_type)) { |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1013 | return DoInvokePolymorphicMethod(self, |
| 1014 | shadow_frame, |
| 1015 | method_handle, |
| 1016 | callsite_type, |
| 1017 | operands, |
| 1018 | result); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | // On the fast-path. This is equivalent to DoCallPolymoprhic without the conversion paths. |
| 1022 | ArtMethod* target_method = method_handle->GetTargetMethod(); |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1023 | uint32_t receiver_reg = (operands->GetNumberOfOperands() > 0) ? operands->GetOperand(0) : 0u; |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 1024 | ArtMethod* called_method = RefineTargetMethod(self, |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 1025 | shadow_frame, |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 1026 | handle_kind, |
| 1027 | method_handle_type, |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 1028 | callsite_type, |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 1029 | receiver_reg, |
| 1030 | target_method); |
| 1031 | if (called_method == nullptr) { |
| 1032 | DCHECK(self->IsExceptionPending()); |
| 1033 | return false; |
| 1034 | } |
| 1035 | |
| 1036 | // Compute method information. |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame^] | 1037 | CodeItemDataAccessor accessor(called_method); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 1038 | uint16_t num_regs; |
| 1039 | size_t num_input_regs; |
| 1040 | size_t first_dest_reg; |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame^] | 1041 | if (LIKELY(accessor.HasCodeItem())) { |
| 1042 | num_regs = accessor.RegistersSize(); |
| 1043 | first_dest_reg = num_regs - accessor.InsSize(); |
| 1044 | num_input_regs = accessor.InsSize(); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 1045 | // Parameter registers go at the end of the shadow frame. |
| 1046 | DCHECK_NE(first_dest_reg, (size_t)-1); |
| 1047 | } else { |
| 1048 | // No local regs for proxy and native methods. |
| 1049 | DCHECK(called_method->IsNative() || called_method->IsProxyMethod()); |
| 1050 | num_regs = num_input_regs = GetInsForProxyOrNativeMethod(called_method); |
| 1051 | first_dest_reg = 0; |
| 1052 | } |
| 1053 | |
| 1054 | // Allocate shadow frame on the stack. |
| 1055 | const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon"); |
| 1056 | ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr = |
| 1057 | CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0); |
| 1058 | ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get(); |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1059 | CopyArgumentsFromCallerFrame(shadow_frame, |
| 1060 | new_shadow_frame, |
| 1061 | operands, |
| 1062 | first_dest_reg); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 1063 | self->EndAssertNoThreadSuspension(old_cause); |
| 1064 | |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 1065 | bool use_interpreter_entrypoint = ClassLinker::ShouldUseInterpreterEntrypoint( |
| 1066 | called_method, called_method->GetEntryPointFromQuickCompiledCode()); |
| 1067 | PerformCall(self, |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame^] | 1068 | accessor, |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 1069 | shadow_frame.GetMethod(), |
| 1070 | first_dest_reg, |
| 1071 | new_shadow_frame, |
| 1072 | result, |
| 1073 | use_interpreter_entrypoint); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 1074 | if (self->IsExceptionPending()) { |
| 1075 | return false; |
| 1076 | } |
| 1077 | return true; |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | } // namespace |
| 1081 | |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1082 | bool MethodHandleInvoke(Thread* self, |
| 1083 | ShadowFrame& shadow_frame, |
| 1084 | Handle<mirror::MethodHandle> method_handle, |
| 1085 | Handle<mirror::MethodType> callsite_type, |
| 1086 | const InstructionOperands* const operands, |
| 1087 | JValue* result) |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 1088 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 1089 | if (UNLIKELY(callsite_type->IsExactMatch(method_handle->GetMethodType()))) { |
| 1090 | // A non-exact invoke that can be invoked exactly. |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1091 | return MethodHandleInvokeExactInternal(self, |
| 1092 | shadow_frame, |
| 1093 | method_handle, |
| 1094 | callsite_type, |
| 1095 | operands, |
| 1096 | result); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 1097 | } else { |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1098 | return MethodHandleInvokeInternal(self, |
| 1099 | shadow_frame, |
| 1100 | method_handle, |
| 1101 | callsite_type, |
| 1102 | operands, |
| 1103 | result); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 1107 | bool MethodHandleInvokeExact(Thread* self, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1108 | ShadowFrame& shadow_frame, |
| 1109 | Handle<mirror::MethodHandle> method_handle, |
| 1110 | Handle<mirror::MethodType> callsite_type, |
| 1111 | const InstructionOperands* const operands, |
| 1112 | JValue* result) |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 1113 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1114 | // We need to check the nominal type of the handle in addition to the |
| 1115 | // real type. The "nominal" type is present when MethodHandle.asType is |
| 1116 | // called any handle, and results in the declared type of the handle |
| 1117 | // changing. |
| 1118 | ObjPtr<mirror::MethodType> nominal_type(method_handle->GetNominalType()); |
| 1119 | if (UNLIKELY(nominal_type != nullptr)) { |
| 1120 | if (UNLIKELY(!callsite_type->IsExactMatch(nominal_type.Ptr()))) { |
| 1121 | ThrowWrongMethodTypeException(nominal_type.Ptr(), callsite_type.Get()); |
| 1122 | return false; |
| 1123 | } |
| 1124 | if (LIKELY(!nominal_type->IsExactMatch(method_handle->GetMethodType()))) { |
| 1125 | // Different nominal type means we have to treat as non-exact. |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1126 | return MethodHandleInvokeInternal(self, |
| 1127 | shadow_frame, |
| 1128 | method_handle, |
| 1129 | callsite_type, |
| 1130 | operands, |
| 1131 | result); |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 1132 | } |
| 1133 | } |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1134 | return MethodHandleInvokeExactInternal(self, |
| 1135 | shadow_frame, |
| 1136 | method_handle, |
| 1137 | callsite_type, |
| 1138 | operands, |
| 1139 | result); |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 1140 | } |
| 1141 | |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 1142 | } // namespace art |