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 | |
Vladimir Marko | 5868ada | 2020-05-12 11:50:34 +0100 | [diff] [blame^] | 21 | #include "class_root-inl.h" |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 22 | #include "common_dex_operations.h" |
Alex Light | 49df715 | 2019-10-03 11:22:35 -0700 | [diff] [blame] | 23 | #include "common_throws.h" |
Vladimir Marko | 6ec2a1b | 2018-05-22 15:33:48 +0100 | [diff] [blame] | 24 | #include "interpreter/shadow_frame-inl.h" |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 25 | #include "jvalue-inl.h" |
Andreas Gampe | 88dbad3 | 2018-06-26 19:54:12 -0700 | [diff] [blame] | 26 | #include "mirror/class-inl.h" |
Vladimir Marko | 423bebb | 2019-03-26 15:17:21 +0000 | [diff] [blame] | 27 | #include "mirror/emulated_stack_frame-inl.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 28 | #include "mirror/method_handle_impl-inl.h" |
Vladimir Marko | 5aead70 | 2019-03-27 11:00:36 +0000 | [diff] [blame] | 29 | #include "mirror/method_type-inl.h" |
Orion Hodson | b8b9387 | 2018-01-30 07:51:10 +0000 | [diff] [blame] | 30 | #include "mirror/var_handle.h" |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 31 | #include "reflection-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 32 | #include "reflection.h" |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 33 | #include "well_known_classes.h" |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 34 | |
| 35 | namespace art { |
| 36 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 37 | using android::base::StringPrintf; |
| 38 | |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 39 | namespace { |
| 40 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 41 | #define PRIMITIVES_LIST(V) \ |
| 42 | V(Primitive::kPrimBoolean, Boolean, Boolean, Z) \ |
| 43 | V(Primitive::kPrimByte, Byte, Byte, B) \ |
| 44 | V(Primitive::kPrimChar, Char, Character, C) \ |
| 45 | V(Primitive::kPrimShort, Short, Short, S) \ |
| 46 | V(Primitive::kPrimInt, Int, Integer, I) \ |
| 47 | V(Primitive::kPrimLong, Long, Long, J) \ |
| 48 | V(Primitive::kPrimFloat, Float, Float, F) \ |
| 49 | V(Primitive::kPrimDouble, Double, Double, D) |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 50 | |
| 51 | // Assigns |type| to the primitive type associated with |klass|. Returns |
| 52 | // true iff. |klass| was a boxed type (Integer, Long etc.), false otherwise. |
| 53 | bool GetUnboxedPrimitiveType(ObjPtr<mirror::Class> klass, Primitive::Type* type) |
| 54 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 55 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 56 | std::string storage; |
| 57 | const char* descriptor = klass->GetDescriptor(&storage); |
| 58 | static const char kJavaLangPrefix[] = "Ljava/lang/"; |
| 59 | static const size_t kJavaLangPrefixSize = sizeof(kJavaLangPrefix) - 1; |
| 60 | if (strncmp(descriptor, kJavaLangPrefix, kJavaLangPrefixSize) != 0) { |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | descriptor += kJavaLangPrefixSize; |
| 65 | #define LOOKUP_PRIMITIVE(primitive, _, java_name, ___) \ |
| 66 | if (strcmp(descriptor, #java_name ";") == 0) { \ |
| 67 | *type = primitive; \ |
| 68 | return true; \ |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 69 | } |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 70 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 71 | PRIMITIVES_LIST(LOOKUP_PRIMITIVE); |
| 72 | #undef LOOKUP_PRIMITIVE |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 73 | return false; |
| 74 | } |
| 75 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 76 | ObjPtr<mirror::Class> GetBoxedPrimitiveClass(Primitive::Type type) |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 77 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 78 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
| 79 | jmethodID m = nullptr; |
| 80 | switch (type) { |
| 81 | #define CASE_PRIMITIVE(primitive, _, java_name, __) \ |
| 82 | case primitive: \ |
| 83 | m = WellKnownClasses::java_lang_ ## java_name ## _valueOf; \ |
| 84 | break; |
| 85 | PRIMITIVES_LIST(CASE_PRIMITIVE); |
| 86 | #undef CASE_PRIMITIVE |
| 87 | case Primitive::Type::kPrimNot: |
| 88 | case Primitive::Type::kPrimVoid: |
| 89 | return nullptr; |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 90 | } |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 91 | return jni::DecodeArtMethod(m)->GetDeclaringClass(); |
| 92 | } |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 93 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 94 | bool GetUnboxedTypeAndValue(ObjPtr<mirror::Object> o, Primitive::Type* type, JValue* value) |
| 95 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 96 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 97 | ObjPtr<mirror::Class> klass = o->GetClass(); |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 98 | ArtField* primitive_field = &klass->GetIFieldsPtr()->At(0); |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 99 | #define CASE_PRIMITIVE(primitive, abbrev, _, shorthand) \ |
| 100 | if (klass == GetBoxedPrimitiveClass(primitive)) { \ |
| 101 | *type = primitive; \ |
| 102 | value->Set ## shorthand(primitive_field->Get ## abbrev(o)); \ |
| 103 | return true; \ |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 104 | } |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 105 | PRIMITIVES_LIST(CASE_PRIMITIVE) |
| 106 | #undef CASE_PRIMITIVE |
| 107 | return false; |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | inline bool IsReferenceType(Primitive::Type type) { |
| 111 | return type == Primitive::kPrimNot; |
| 112 | } |
| 113 | |
| 114 | inline bool IsPrimitiveType(Primitive::Type type) { |
| 115 | return !IsReferenceType(type); |
| 116 | } |
| 117 | |
| 118 | } // namespace |
| 119 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 120 | bool IsParameterTypeConvertible(ObjPtr<mirror::Class> from, ObjPtr<mirror::Class> to) |
| 121 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 122 | // This function returns true if there's any conceivable conversion |
| 123 | // between |from| and |to|. It's expected this method will be used |
| 124 | // to determine if a WrongMethodTypeException should be raised. The |
| 125 | // decision logic follows the documentation for MethodType.asType(). |
| 126 | if (from == to) { |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | Primitive::Type from_primitive = from->GetPrimitiveType(); |
| 131 | Primitive::Type to_primitive = to->GetPrimitiveType(); |
| 132 | DCHECK(from_primitive != Primitive::Type::kPrimVoid); |
| 133 | DCHECK(to_primitive != Primitive::Type::kPrimVoid); |
| 134 | |
| 135 | // If |to| and |from| are references. |
| 136 | if (IsReferenceType(from_primitive) && IsReferenceType(to_primitive)) { |
| 137 | // Assignability is determined during parameter conversion when |
| 138 | // invoking the associated method handle. |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | // If |to| and |from| are primitives and a widening conversion exists. |
| 143 | if (Primitive::IsWidenable(from_primitive, to_primitive)) { |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | // If |to| is a reference and |from| is a primitive, then boxing conversion. |
| 148 | if (IsReferenceType(to_primitive) && IsPrimitiveType(from_primitive)) { |
| 149 | return to->IsAssignableFrom(GetBoxedPrimitiveClass(from_primitive)); |
| 150 | } |
| 151 | |
| 152 | // If |from| is a reference and |to| is a primitive, then unboxing conversion. |
| 153 | if (IsPrimitiveType(to_primitive) && IsReferenceType(from_primitive)) { |
| 154 | if (from->DescriptorEquals("Ljava/lang/Object;")) { |
| 155 | // Object might be converted into a primitive during unboxing. |
| 156 | return true; |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | if (Primitive::IsNumericType(to_primitive) && from->DescriptorEquals("Ljava/lang/Number;")) { |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 160 | // Number might be unboxed into any of the number primitive types. |
| 161 | return true; |
| 162 | } |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 163 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 164 | Primitive::Type unboxed_type; |
| 165 | if (GetUnboxedPrimitiveType(from, &unboxed_type)) { |
Orion Hodson | f1412b4 | 2016-11-11 12:03:29 +0000 | [diff] [blame] | 166 | if (unboxed_type == to_primitive) { |
| 167 | // Straightforward unboxing conversion such as Boolean => boolean. |
| 168 | return true; |
Orion Hodson | f1412b4 | 2016-11-11 12:03:29 +0000 | [diff] [blame] | 169 | } |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 170 | |
| 171 | // Check if widening operations for numeric primitives would work, |
| 172 | // such as Byte => byte => long. |
| 173 | return Primitive::IsWidenable(unboxed_type, to_primitive); |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | bool IsReturnTypeConvertible(ObjPtr<mirror::Class> from, ObjPtr<mirror::Class> to) |
| 181 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 182 | if (to->GetPrimitiveType() == Primitive::Type::kPrimVoid) { |
| 183 | // Result will be ignored. |
| 184 | return true; |
| 185 | } else if (from->GetPrimitiveType() == Primitive::Type::kPrimVoid) { |
| 186 | // Returned value will be 0 / null. |
| 187 | return true; |
| 188 | } else { |
| 189 | // Otherwise apply usual parameter conversion rules. |
| 190 | return IsParameterTypeConvertible(from, to); |
| 191 | } |
| 192 | } |
| 193 | |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 194 | bool ConvertJValueCommon( |
| 195 | Handle<mirror::MethodType> callsite_type, |
| 196 | Handle<mirror::MethodType> callee_type, |
| 197 | ObjPtr<mirror::Class> from, |
| 198 | ObjPtr<mirror::Class> to, |
| 199 | JValue* value) { |
| 200 | // The reader maybe concerned about the safety of the heap object |
| 201 | // that may be in |value|. There is only one case where allocation |
| 202 | // is obviously needed and that's for boxing. However, in the case |
| 203 | // of boxing |value| contains a non-reference type. |
| 204 | |
| 205 | const Primitive::Type from_type = from->GetPrimitiveType(); |
| 206 | const Primitive::Type to_type = to->GetPrimitiveType(); |
| 207 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 208 | // Put incoming value into |src_value| and set return value to 0. |
| 209 | // Errors and conversions from void require the return value to be 0. |
| 210 | const JValue src_value(*value); |
| 211 | value->SetJ(0); |
| 212 | |
| 213 | // Conversion from void set result to zero. |
| 214 | if (from_type == Primitive::kPrimVoid) { |
| 215 | return true; |
| 216 | } |
| 217 | |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 218 | // This method must be called only when the types don't match. |
| 219 | DCHECK(from != to); |
| 220 | |
| 221 | if (IsPrimitiveType(from_type) && IsPrimitiveType(to_type)) { |
| 222 | // The source and target types are both primitives. |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 223 | if (UNLIKELY(!ConvertPrimitiveValueNoThrow(from_type, to_type, src_value, value))) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 224 | ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get()); |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 225 | return false; |
| 226 | } |
| 227 | return true; |
| 228 | } else if (IsReferenceType(from_type) && IsReferenceType(to_type)) { |
| 229 | // They're both reference types. If "from" is null, we can pass it |
| 230 | // through unchanged. If not, we must generate a cast exception if |
| 231 | // |to| is not assignable from the dynamic type of |ref|. |
| 232 | // |
| 233 | // Playing it safe with StackHandleScope here, not expecting any allocation |
| 234 | // in mirror::Class::IsAssignable(). |
| 235 | StackHandleScope<2> hs(Thread::Current()); |
| 236 | Handle<mirror::Class> h_to(hs.NewHandle(to)); |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 237 | Handle<mirror::Object> h_obj(hs.NewHandle(src_value.GetL())); |
Orion Hodson | c1d3bac | 2018-01-26 14:38:55 +0000 | [diff] [blame] | 238 | if (UNLIKELY(!h_obj.IsNull() && !to->IsAssignableFrom(h_obj->GetClass()))) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 239 | ThrowClassCastException(h_to.Get(), h_obj->GetClass()); |
| 240 | return false; |
| 241 | } |
| 242 | value->SetL(h_obj.Get()); |
| 243 | return true; |
| 244 | } else if (IsReferenceType(to_type)) { |
| 245 | DCHECK(IsPrimitiveType(from_type)); |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 246 | // The source type is a primitive and the target type is a reference, so we must box. |
| 247 | // The target type maybe a super class of the boxed source type, for example, |
| 248 | // if the source type is int, it's boxed type is java.lang.Integer, and the target |
| 249 | // type could be java.lang.Number. |
| 250 | Primitive::Type type; |
| 251 | if (!GetUnboxedPrimitiveType(to, &type)) { |
| 252 | ObjPtr<mirror::Class> boxed_from_class = GetBoxedPrimitiveClass(from_type); |
Orion Hodson | c1d3bac | 2018-01-26 14:38:55 +0000 | [diff] [blame] | 253 | if (LIKELY(boxed_from_class->IsSubClass(to))) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 254 | type = from_type; |
| 255 | } else { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 256 | ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get()); |
| 257 | return false; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | if (UNLIKELY(from_type != type)) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 262 | ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get()); |
| 263 | return false; |
| 264 | } |
| 265 | |
Orion Hodson | c1d3bac | 2018-01-26 14:38:55 +0000 | [diff] [blame] | 266 | if (UNLIKELY(!ConvertPrimitiveValueNoThrow(from_type, type, src_value, value))) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 267 | ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get()); |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | // Then perform the actual boxing, and then set the reference. |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 272 | ObjPtr<mirror::Object> boxed = BoxPrimitive(type, src_value); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 273 | value->SetL(boxed); |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 274 | return true; |
| 275 | } else { |
| 276 | // The source type is a reference and the target type is a primitive, so we must unbox. |
| 277 | DCHECK(IsReferenceType(from_type)); |
| 278 | DCHECK(IsPrimitiveType(to_type)); |
| 279 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 280 | ObjPtr<mirror::Object> from_obj(src_value.GetL()); |
Orion Hodson | c1d3bac | 2018-01-26 14:38:55 +0000 | [diff] [blame] | 281 | if (UNLIKELY(from_obj.IsNull())) { |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 282 | ThrowNullPointerException( |
| 283 | StringPrintf("Expected to unbox a '%s' primitive type but was returned null", |
| 284 | from->PrettyDescriptor().c_str()).c_str()); |
| 285 | return false; |
| 286 | } |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 287 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 288 | Primitive::Type unboxed_type; |
| 289 | JValue unboxed_value; |
| 290 | if (UNLIKELY(!GetUnboxedTypeAndValue(from_obj, &unboxed_type, &unboxed_value))) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 291 | ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get()); |
| 292 | return false; |
| 293 | } |
| 294 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 295 | if (UNLIKELY(!ConvertPrimitiveValueNoThrow(unboxed_type, to_type, unboxed_value, value))) { |
Orion Hodson | c1d3bac | 2018-01-26 14:38:55 +0000 | [diff] [blame] | 296 | if (from->IsAssignableFrom(GetBoxedPrimitiveClass(to_type))) { |
| 297 | // CallSite may be Number, but the Number object is |
| 298 | // incompatible, e.g. Number (Integer) for a short. |
| 299 | ThrowClassCastException(from, to); |
| 300 | } else { |
| 301 | // CallSite is incompatible, e.g. Integer for a short. |
| 302 | ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get()); |
| 303 | } |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 304 | return false; |
| 305 | } |
| 306 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 307 | return true; |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 311 | namespace { |
| 312 | |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 313 | inline void CopyArgumentsFromCallerFrame(const ShadowFrame& caller_frame, |
| 314 | ShadowFrame* callee_frame, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 315 | const InstructionOperands* const operands, |
| 316 | const size_t first_dst_reg) |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 317 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 318 | for (size_t i = 0; i < operands->GetNumberOfOperands(); ++i) { |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 319 | size_t dst_reg = first_dst_reg + i; |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 320 | size_t src_reg = operands->GetOperand(i); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 321 | // Uint required, so that sign extension does not make this wrong on 64-bit systems |
| 322 | uint32_t src_value = caller_frame.GetVReg(src_reg); |
| 323 | ObjPtr<mirror::Object> o = caller_frame.GetVRegReference<kVerifyNone>(src_reg); |
| 324 | // If both register locations contains the same value, the register probably holds a reference. |
| 325 | // Note: As an optimization, non-moving collectors leave a stale reference value |
| 326 | // in the references array even after the original vreg was overwritten to a non-reference. |
| 327 | if (src_value == reinterpret_cast<uintptr_t>(o.Ptr())) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 328 | callee_frame->SetVRegReference(dst_reg, o); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 329 | } else { |
| 330 | callee_frame->SetVReg(dst_reg, src_value); |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 335 | inline bool ConvertAndCopyArgumentsFromCallerFrame( |
| 336 | Thread* self, |
| 337 | Handle<mirror::MethodType> callsite_type, |
| 338 | Handle<mirror::MethodType> callee_type, |
| 339 | const ShadowFrame& caller_frame, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 340 | uint32_t first_dest_reg, |
| 341 | const InstructionOperands* const operands, |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 342 | ShadowFrame* callee_frame) |
| 343 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 344 | ObjPtr<mirror::ObjectArray<mirror::Class>> from_types(callsite_type->GetPTypes()); |
| 345 | ObjPtr<mirror::ObjectArray<mirror::Class>> to_types(callee_type->GetPTypes()); |
| 346 | |
| 347 | const int32_t num_method_params = from_types->GetLength(); |
| 348 | if (to_types->GetLength() != num_method_params) { |
| 349 | ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get()); |
| 350 | return false; |
| 351 | } |
| 352 | |
Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 353 | ShadowFrameGetter getter(caller_frame, operands); |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 354 | ShadowFrameSetter setter(callee_frame, first_dest_reg); |
| 355 | return PerformConversions<ShadowFrameGetter, ShadowFrameSetter>(self, |
| 356 | callsite_type, |
| 357 | callee_type, |
| 358 | &getter, |
| 359 | &setter, |
| 360 | num_method_params); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 363 | inline bool IsInvoke(const mirror::MethodHandle::Kind handle_kind) { |
| 364 | return handle_kind <= mirror::MethodHandle::Kind::kLastInvokeKind; |
| 365 | } |
| 366 | |
| 367 | inline bool IsInvokeTransform(const mirror::MethodHandle::Kind handle_kind) { |
| 368 | return (handle_kind == mirror::MethodHandle::Kind::kInvokeTransform |
| 369 | || handle_kind == mirror::MethodHandle::Kind::kInvokeCallSiteTransform); |
| 370 | } |
| 371 | |
Orion Hodson | b8b9387 | 2018-01-30 07:51:10 +0000 | [diff] [blame] | 372 | inline bool IsInvokeVarHandle(const mirror::MethodHandle::Kind handle_kind) { |
| 373 | return (handle_kind == mirror::MethodHandle::Kind::kInvokeVarHandle || |
| 374 | handle_kind == mirror::MethodHandle::Kind::kInvokeVarHandleExact); |
| 375 | } |
| 376 | |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 377 | inline bool IsFieldAccess(mirror::MethodHandle::Kind handle_kind) { |
| 378 | return (handle_kind >= mirror::MethodHandle::Kind::kFirstAccessorKind |
| 379 | && handle_kind <= mirror::MethodHandle::Kind::kLastAccessorKind); |
| 380 | } |
| 381 | |
| 382 | // Calculate the number of ins for a proxy or native method, where we |
| 383 | // can't just look at the code item. |
| 384 | static inline size_t GetInsForProxyOrNativeMethod(ArtMethod* method) |
| 385 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 386 | DCHECK(method->IsNative() || method->IsProxyMethod()); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 387 | method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 388 | uint32_t shorty_length = 0; |
| 389 | const char* shorty = method->GetShorty(&shorty_length); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 390 | |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 391 | // Static methods do not include the receiver. The receiver isn't included |
| 392 | // in the shorty_length though the return value is. |
| 393 | size_t num_ins = method->IsStatic() ? shorty_length - 1 : shorty_length; |
| 394 | for (const char* c = shorty + 1; *c != '\0'; ++c) { |
| 395 | if (*c == 'J' || *c == 'D') { |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 396 | ++num_ins; |
| 397 | } |
| 398 | } |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 399 | return num_ins; |
| 400 | } |
| 401 | |
| 402 | // Returns true iff. the callsite type for a polymorphic invoke is transformer |
| 403 | // like, i.e that it has a single input argument whose type is |
| 404 | // dalvik.system.EmulatedStackFrame. |
| 405 | static inline bool IsCallerTransformer(Handle<mirror::MethodType> callsite_type) |
| 406 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 407 | ObjPtr<mirror::ObjectArray<mirror::Class>> param_types(callsite_type->GetPTypes()); |
| 408 | if (param_types->GetLength() == 1) { |
| 409 | ObjPtr<mirror::Class> param(param_types->GetWithoutChecks(0)); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 410 | // NB Comparing descriptor here as it appears faster in cycle simulation than using: |
| 411 | // param == WellKnownClasses::ToClass(WellKnownClasses::dalvik_system_EmulatedStackFrame) |
| 412 | // Costs are 98 vs 173 cycles per invocation. |
| 413 | return param->DescriptorEquals("Ldalvik/system/EmulatedStackFrame;"); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | return false; |
| 417 | } |
| 418 | |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 419 | static inline bool MethodHandleInvokeMethod(ArtMethod* called_method, |
| 420 | Handle<mirror::MethodType> callsite_type, |
| 421 | Handle<mirror::MethodType> target_type, |
Orion Hodson | 2764f61 | 2019-11-11 14:36:20 +0000 | [diff] [blame] | 422 | Handle<mirror::MethodType> nominal_type, |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 423 | Thread* self, |
| 424 | ShadowFrame& shadow_frame, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 425 | const InstructionOperands* const operands, |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 426 | JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 427 | // Compute method information. |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 428 | CodeItemDataAccessor accessor(called_method->DexInstructionData()); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 429 | |
| 430 | // Number of registers for the callee's call frame. Note that for non-exact |
| 431 | // invokes, we always derive this information from the callee method. We |
| 432 | // cannot guarantee during verification that the number of registers encoded |
| 433 | // in the invoke is equal to the number of ins for the callee. This is because |
| 434 | // some transformations (such as boxing a long -> Long or wideining an |
| 435 | // int -> long will change that number. |
| 436 | uint16_t num_regs; |
| 437 | size_t num_input_regs; |
| 438 | size_t first_dest_reg; |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 439 | if (LIKELY(accessor.HasCodeItem())) { |
| 440 | num_regs = accessor.RegistersSize(); |
| 441 | first_dest_reg = num_regs - accessor.InsSize(); |
| 442 | num_input_regs = accessor.InsSize(); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 443 | // Parameter registers go at the end of the shadow frame. |
| 444 | DCHECK_NE(first_dest_reg, (size_t)-1); |
| 445 | } else { |
| 446 | // No local regs for proxy and native methods. |
| 447 | DCHECK(called_method->IsNative() || called_method->IsProxyMethod()); |
| 448 | num_regs = num_input_regs = GetInsForProxyOrNativeMethod(called_method); |
| 449 | first_dest_reg = 0; |
| 450 | } |
| 451 | |
| 452 | // Allocate shadow frame on the stack. |
| 453 | ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr = |
| 454 | CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0); |
| 455 | ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get(); |
| 456 | |
| 457 | // Whether this polymorphic invoke was issued by a transformer method. |
| 458 | bool is_caller_transformer = false; |
| 459 | // Thread might be suspended during PerformArgumentConversions due to the |
| 460 | // allocations performed during boxing. |
| 461 | { |
| 462 | ScopedStackedShadowFramePusher pusher( |
| 463 | self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction); |
| 464 | if (callsite_type->IsExactMatch(target_type.Get())) { |
| 465 | // This is an exact invoke, we can take the fast path of just copying all |
| 466 | // registers without performing any argument conversions. |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 467 | CopyArgumentsFromCallerFrame(shadow_frame, |
| 468 | new_shadow_frame, |
| 469 | operands, |
| 470 | first_dest_reg); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 471 | } else { |
| 472 | // This includes the case where we're entering this invoke-polymorphic |
| 473 | // from a transformer method. In that case, the callsite_type will contain |
| 474 | // a single argument of type dalvik.system.EmulatedStackFrame. In that |
| 475 | // case, we'll have to unmarshal the EmulatedStackFrame into the |
| 476 | // new_shadow_frame and perform argument conversions on it. |
| 477 | if (IsCallerTransformer(callsite_type)) { |
| 478 | is_caller_transformer = true; |
| 479 | // The emulated stack frame is the first and only argument when we're coming |
| 480 | // through from a transformer. |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 481 | size_t first_arg_register = operands->GetOperand(0); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 482 | ObjPtr<mirror::EmulatedStackFrame> emulated_stack_frame( |
Vladimir Marko | d7e9bbf | 2019-03-28 13:18:57 +0000 | [diff] [blame] | 483 | ObjPtr<mirror::EmulatedStackFrame>::DownCast( |
| 484 | shadow_frame.GetVRegReference(first_arg_register))); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 485 | if (!emulated_stack_frame->WriteToShadowFrame(self, |
| 486 | target_type, |
| 487 | first_dest_reg, |
| 488 | new_shadow_frame)) { |
| 489 | DCHECK(self->IsExceptionPending()); |
Yi Kong | 4b22b34 | 2018-08-02 14:43:21 -0700 | [diff] [blame] | 490 | result->SetL(nullptr); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 491 | return false; |
| 492 | } |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 493 | } else { |
| 494 | if (!callsite_type->IsConvertible(target_type.Get())) { |
| 495 | ThrowWrongMethodTypeException(target_type.Get(), callsite_type.Get()); |
| 496 | return false; |
| 497 | } |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 498 | if (!ConvertAndCopyArgumentsFromCallerFrame(self, |
| 499 | callsite_type, |
| 500 | target_type, |
| 501 | shadow_frame, |
| 502 | first_dest_reg, |
| 503 | operands, |
| 504 | new_shadow_frame)) { |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 505 | DCHECK(self->IsExceptionPending()); |
Yi Kong | 4b22b34 | 2018-08-02 14:43:21 -0700 | [diff] [blame] | 506 | result->SetL(nullptr); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 507 | return false; |
| 508 | } |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 513 | bool use_interpreter_entrypoint = ClassLinker::ShouldUseInterpreterEntrypoint( |
| 514 | called_method, called_method->GetEntryPointFromQuickCompiledCode()); |
| 515 | PerformCall(self, |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 516 | accessor, |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 517 | shadow_frame.GetMethod(), |
| 518 | first_dest_reg, |
| 519 | new_shadow_frame, |
| 520 | result, |
| 521 | use_interpreter_entrypoint); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 522 | if (self->IsExceptionPending()) { |
| 523 | return false; |
| 524 | } |
| 525 | |
| 526 | // If the caller of this signature polymorphic method was a transformer, |
| 527 | // we need to copy the result back out to the emulated stack frame. |
| 528 | if (is_caller_transformer) { |
| 529 | StackHandleScope<2> hs(self); |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 530 | size_t first_callee_register = operands->GetOperand(0); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 531 | Handle<mirror::EmulatedStackFrame> emulated_stack_frame( |
Vladimir Marko | d7e9bbf | 2019-03-28 13:18:57 +0000 | [diff] [blame] | 532 | hs.NewHandle(ObjPtr<mirror::EmulatedStackFrame>::DownCast( |
| 533 | shadow_frame.GetVRegReference(first_callee_register)))); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 534 | Handle<mirror::MethodType> emulated_stack_type(hs.NewHandle(emulated_stack_frame->GetType())); |
| 535 | JValue local_result; |
| 536 | local_result.SetJ(result->GetJ()); |
| 537 | |
| 538 | if (ConvertReturnValue(emulated_stack_type, target_type, &local_result)) { |
| 539 | emulated_stack_frame->SetReturnValue(self, local_result); |
| 540 | return true; |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 541 | } |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 542 | |
| 543 | DCHECK(self->IsExceptionPending()); |
| 544 | return false; |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 545 | } |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 546 | |
Orion Hodson | 2764f61 | 2019-11-11 14:36:20 +0000 | [diff] [blame] | 547 | if (nominal_type != nullptr) { |
| 548 | return ConvertReturnValue(nominal_type, target_type, result) && |
| 549 | ConvertReturnValue(callsite_type, nominal_type, result); |
| 550 | } |
| 551 | |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 552 | return ConvertReturnValue(callsite_type, target_type, result); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 555 | static inline bool MethodHandleInvokeTransform(ArtMethod* called_method, |
| 556 | Handle<mirror::MethodType> callsite_type, |
| 557 | Handle<mirror::MethodType> callee_type, |
| 558 | Thread* self, |
| 559 | ShadowFrame& shadow_frame, |
| 560 | Handle<mirror::MethodHandle> receiver, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 561 | const InstructionOperands* const operands, |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 562 | JValue* result) |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 563 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 564 | // This can be fixed to two, because the method we're calling here |
| 565 | // (MethodHandle.transformInternal) doesn't have any locals and the signature |
| 566 | // is known : |
| 567 | // |
| 568 | // private MethodHandle.transformInternal(EmulatedStackFrame sf); |
| 569 | // |
| 570 | // This means we need only two vregs : |
| 571 | // - One for the receiver object. |
| 572 | // - One for the only method argument (an EmulatedStackFrame). |
| 573 | static constexpr size_t kNumRegsForTransform = 2; |
| 574 | |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 575 | CodeItemDataAccessor accessor(called_method->DexInstructionData()); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 576 | DCHECK_EQ(kNumRegsForTransform, accessor.RegistersSize()); |
| 577 | DCHECK_EQ(kNumRegsForTransform, accessor.InsSize()); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 578 | |
| 579 | ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr = |
| 580 | CREATE_SHADOW_FRAME(kNumRegsForTransform, &shadow_frame, called_method, /* dex pc */ 0); |
| 581 | ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get(); |
| 582 | |
| 583 | StackHandleScope<1> hs(self); |
| 584 | MutableHandle<mirror::EmulatedStackFrame> sf(hs.NewHandle<mirror::EmulatedStackFrame>(nullptr)); |
| 585 | if (IsCallerTransformer(callsite_type)) { |
| 586 | // If we're entering this transformer from another transformer, we can pass |
| 587 | // through the handle directly to the callee, instead of having to |
| 588 | // instantiate a new stack frame based on the shadow frame. |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 589 | size_t first_callee_register = operands->GetOperand(0); |
Vladimir Marko | d7e9bbf | 2019-03-28 13:18:57 +0000 | [diff] [blame] | 590 | sf.Assign(ObjPtr<mirror::EmulatedStackFrame>::DownCast( |
| 591 | shadow_frame.GetVRegReference(first_callee_register))); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 592 | } else { |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 593 | sf.Assign(mirror::EmulatedStackFrame::CreateFromShadowFrameAndArgs(self, |
| 594 | callsite_type, |
| 595 | callee_type, |
| 596 | shadow_frame, |
| 597 | operands)); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 598 | |
| 599 | // Something went wrong while creating the emulated stack frame, we should |
| 600 | // throw the pending exception. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 601 | if (sf == nullptr) { |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 602 | DCHECK(self->IsExceptionPending()); |
| 603 | return false; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | new_shadow_frame->SetVRegReference(0, receiver.Get()); |
| 608 | new_shadow_frame->SetVRegReference(1, sf.Get()); |
| 609 | |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 610 | bool use_interpreter_entrypoint = ClassLinker::ShouldUseInterpreterEntrypoint( |
| 611 | called_method, called_method->GetEntryPointFromQuickCompiledCode()); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 612 | PerformCall(self, |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 613 | accessor, |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 614 | shadow_frame.GetMethod(), |
| 615 | 0 /* first destination register */, |
| 616 | new_shadow_frame, |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 617 | result, |
| 618 | use_interpreter_entrypoint); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 619 | if (self->IsExceptionPending()) { |
| 620 | return false; |
| 621 | } |
| 622 | |
| 623 | // If the called transformer method we called has returned a value, then we |
| 624 | // need to copy it back to |result|. |
| 625 | sf->GetReturnValue(self, result); |
| 626 | return ConvertReturnValue(callsite_type, callee_type, result); |
| 627 | } |
| 628 | |
| 629 | inline static ObjPtr<mirror::Class> GetAndInitializeDeclaringClass(Thread* self, ArtField* field) |
| 630 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 631 | // Method handle invocations on static fields should ensure class is |
| 632 | // initialized. This usually happens when an instance is constructed |
| 633 | // or class members referenced, but this is not guaranteed when |
| 634 | // looking up method handles. |
| 635 | ObjPtr<mirror::Class> klass = field->GetDeclaringClass(); |
| 636 | if (UNLIKELY(!klass->IsInitialized())) { |
| 637 | StackHandleScope<1> hs(self); |
| 638 | HandleWrapperObjPtr<mirror::Class> h(hs.NewHandleWrapper(&klass)); |
| 639 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h, true, true)) { |
| 640 | DCHECK(self->IsExceptionPending()); |
| 641 | return nullptr; |
| 642 | } |
| 643 | } |
| 644 | return klass; |
| 645 | } |
| 646 | |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 647 | ArtMethod* RefineTargetMethod(Thread* self, |
| 648 | ShadowFrame& shadow_frame, |
| 649 | const mirror::MethodHandle::Kind& handle_kind, |
| 650 | Handle<mirror::MethodType> handle_type, |
| 651 | Handle<mirror::MethodType> callsite_type, |
| 652 | const uint32_t receiver_reg, |
| 653 | ArtMethod* target_method) |
| 654 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 655 | if (handle_kind == mirror::MethodHandle::Kind::kInvokeVirtual || |
| 656 | handle_kind == mirror::MethodHandle::Kind::kInvokeInterface) { |
| 657 | // For virtual and interface methods ensure target_method points to |
| 658 | // the actual method to invoke. |
| 659 | ObjPtr<mirror::Object> receiver(shadow_frame.GetVRegReference(receiver_reg)); |
| 660 | if (IsCallerTransformer(callsite_type)) { |
| 661 | // The current receiver is an emulated stack frame, the method's |
| 662 | // receiver needs to be fetched from there as the emulated frame |
| 663 | // will be unpacked into a new frame. |
| 664 | receiver = ObjPtr<mirror::EmulatedStackFrame>::DownCast(receiver)->GetReceiver(); |
| 665 | } |
| 666 | |
| 667 | ObjPtr<mirror::Class> declaring_class(target_method->GetDeclaringClass()); |
| 668 | if (receiver == nullptr || receiver->GetClass() != declaring_class) { |
| 669 | // Verify that _vRegC is an object reference and of the type expected by |
| 670 | // the receiver. |
| 671 | if (!VerifyObjectIsClass(receiver, declaring_class)) { |
| 672 | DCHECK(self->IsExceptionPending()); |
| 673 | return nullptr; |
| 674 | } |
| 675 | return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface( |
| 676 | target_method, kRuntimePointerSize); |
| 677 | } |
| 678 | } else if (handle_kind == mirror::MethodHandle::Kind::kInvokeDirect) { |
| 679 | // String constructors are a special case, they are replaced with |
| 680 | // StringFactory methods. |
| 681 | if (target_method->IsConstructor() && target_method->GetDeclaringClass()->IsStringClass()) { |
| 682 | DCHECK(handle_type->GetRType()->IsStringClass()); |
| 683 | return WellKnownClasses::StringInitToStringFactory(target_method); |
| 684 | } |
| 685 | } else if (handle_kind == mirror::MethodHandle::Kind::kInvokeSuper) { |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 686 | // Note that we're not dynamically dispatching on the type of the receiver |
| 687 | // here. We use the static type of the "receiver" object that we've |
| 688 | // recorded in the method handle's type, which will be the same as the |
| 689 | // special caller that was specified at the point of lookup. |
| 690 | ObjPtr<mirror::Class> referrer_class = handle_type->GetPTypes()->Get(0); |
Orion Hodson | 0f59574 | 2018-04-10 14:49:28 +0100 | [diff] [blame] | 691 | ObjPtr<mirror::Class> declaring_class = target_method->GetDeclaringClass(); |
| 692 | if (referrer_class == declaring_class) { |
| 693 | return target_method; |
| 694 | } |
Alex Light | 49df715 | 2019-10-03 11:22:35 -0700 | [diff] [blame] | 695 | if (declaring_class->IsInterface()) { |
| 696 | if (target_method->IsAbstract()) { |
| 697 | std::string msg = |
| 698 | "Method " + target_method->PrettyMethod() + " is abstract interface method!"; |
| 699 | ThrowIllegalAccessException(msg.c_str()); |
| 700 | return nullptr; |
| 701 | } |
| 702 | } else { |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 703 | ObjPtr<mirror::Class> super_class = referrer_class->GetSuperClass(); |
| 704 | uint16_t vtable_index = target_method->GetMethodIndex(); |
| 705 | DCHECK(super_class != nullptr); |
| 706 | DCHECK(super_class->HasVTable()); |
| 707 | // Note that super_class is a super of referrer_class and target_method |
| 708 | // will always be declared by super_class (or one of its super classes). |
| 709 | DCHECK_LT(vtable_index, super_class->GetVTableLength()); |
| 710 | return super_class->GetVTableEntry(vtable_index, kRuntimePointerSize); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | return target_method; |
| 714 | } |
| 715 | |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 716 | bool DoInvokePolymorphicMethod(Thread* self, |
| 717 | ShadowFrame& shadow_frame, |
| 718 | Handle<mirror::MethodHandle> method_handle, |
| 719 | Handle<mirror::MethodType> callsite_type, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 720 | const InstructionOperands* const operands, |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 721 | JValue* result) |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 722 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | 2764f61 | 2019-11-11 14:36:20 +0000 | [diff] [blame] | 723 | StackHandleScope<2> hs(self); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 724 | Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType())); |
Orion Hodson | 2764f61 | 2019-11-11 14:36:20 +0000 | [diff] [blame] | 725 | Handle<mirror::MethodType> nominal_handle_type(hs.NewHandle(method_handle->GetNominalType())); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 726 | const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind(); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 727 | DCHECK(IsInvoke(handle_kind)); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 728 | |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 729 | // Get the method we're actually invoking along with the kind of |
| 730 | // invoke that is desired. We don't need to perform access checks at this |
| 731 | // point because they would have been performed on our behalf at the point |
| 732 | // of creation of the method handle. |
| 733 | ArtMethod* target_method = method_handle->GetTargetMethod(); |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 734 | uint32_t receiver_reg = (operands->GetNumberOfOperands() > 0) ? operands->GetOperand(0) : 0u; |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 735 | ArtMethod* called_method = RefineTargetMethod(self, |
| 736 | shadow_frame, |
| 737 | handle_kind, |
| 738 | handle_type, |
| 739 | callsite_type, |
| 740 | receiver_reg, |
| 741 | target_method); |
| 742 | if (called_method == nullptr) { |
| 743 | DCHECK(self->IsExceptionPending()); |
| 744 | return false; |
| 745 | } |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 746 | |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 747 | if (IsInvokeTransform(handle_kind)) { |
| 748 | // There are two cases here - method handles representing regular |
| 749 | // transforms and those representing call site transforms. Method |
| 750 | // handles for call site transforms adapt their MethodType to match |
| 751 | // the call site. For these, the |callee_type| is the same as the |
| 752 | // |callsite_type|. The VarargsCollector is such a tranform, its |
| 753 | // method type depends on the call site, ie. x(a) or x(a, b), or |
| 754 | // x(a, b, c). The VarargsCollector invokes a variable arity method |
| 755 | // with the arity arguments in an array. |
| 756 | Handle<mirror::MethodType> callee_type = |
| 757 | (handle_kind == mirror::MethodHandle::Kind::kInvokeCallSiteTransform) ? callsite_type |
| 758 | : handle_type; |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 759 | return MethodHandleInvokeTransform(called_method, |
| 760 | callsite_type, |
| 761 | callee_type, |
| 762 | self, |
| 763 | shadow_frame, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 764 | /* receiver= */ method_handle, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 765 | operands, |
| 766 | result); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 767 | } else { |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 768 | return MethodHandleInvokeMethod(called_method, |
| 769 | callsite_type, |
| 770 | handle_type, |
Orion Hodson | 2764f61 | 2019-11-11 14:36:20 +0000 | [diff] [blame] | 771 | nominal_handle_type, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 772 | self, |
| 773 | shadow_frame, |
| 774 | operands, |
| 775 | result); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 776 | } |
| 777 | } |
| 778 | |
| 779 | // Helper for getters in invoke-polymorphic. |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 780 | inline static void MethodHandleFieldGet(Thread* self, |
| 781 | const ShadowFrame& shadow_frame, |
| 782 | ObjPtr<mirror::Object>& obj, |
| 783 | ArtField* field, |
| 784 | Primitive::Type field_type, |
| 785 | JValue* result) |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 786 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 787 | switch (field_type) { |
| 788 | case Primitive::kPrimBoolean: |
| 789 | DoFieldGetCommon<Primitive::kPrimBoolean>(self, shadow_frame, obj, field, result); |
| 790 | break; |
| 791 | case Primitive::kPrimByte: |
| 792 | DoFieldGetCommon<Primitive::kPrimByte>(self, shadow_frame, obj, field, result); |
| 793 | break; |
| 794 | case Primitive::kPrimChar: |
| 795 | DoFieldGetCommon<Primitive::kPrimChar>(self, shadow_frame, obj, field, result); |
| 796 | break; |
| 797 | case Primitive::kPrimShort: |
| 798 | DoFieldGetCommon<Primitive::kPrimShort>(self, shadow_frame, obj, field, result); |
| 799 | break; |
| 800 | case Primitive::kPrimInt: |
| 801 | DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result); |
| 802 | break; |
| 803 | case Primitive::kPrimLong: |
| 804 | DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result); |
| 805 | break; |
| 806 | case Primitive::kPrimFloat: |
| 807 | DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result); |
| 808 | break; |
| 809 | case Primitive::kPrimDouble: |
| 810 | DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result); |
| 811 | break; |
| 812 | case Primitive::kPrimNot: |
| 813 | DoFieldGetCommon<Primitive::kPrimNot>(self, shadow_frame, obj, field, result); |
| 814 | break; |
| 815 | case Primitive::kPrimVoid: |
| 816 | LOG(FATAL) << "Unreachable: " << field_type; |
| 817 | UNREACHABLE(); |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | // Helper for setters in invoke-polymorphic. |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 822 | inline bool MethodHandleFieldPut(Thread* self, |
| 823 | ShadowFrame& shadow_frame, |
| 824 | ObjPtr<mirror::Object>& obj, |
| 825 | ArtField* field, |
| 826 | Primitive::Type field_type, |
| 827 | JValue& value) |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 828 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 829 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
| 830 | static const bool kTransaction = false; // Not in a transaction. |
| 831 | static const bool kAssignabilityCheck = false; // No access check. |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 832 | switch (field_type) { |
| 833 | case Primitive::kPrimBoolean: |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 834 | return |
| 835 | DoFieldPutCommon<Primitive::kPrimBoolean, kAssignabilityCheck, kTransaction>( |
| 836 | self, shadow_frame, obj, field, value); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 837 | case Primitive::kPrimByte: |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 838 | return DoFieldPutCommon<Primitive::kPrimByte, kAssignabilityCheck, kTransaction>( |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 839 | self, shadow_frame, obj, field, value); |
| 840 | case Primitive::kPrimChar: |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 841 | return DoFieldPutCommon<Primitive::kPrimChar, kAssignabilityCheck, kTransaction>( |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 842 | self, shadow_frame, obj, field, value); |
| 843 | case Primitive::kPrimShort: |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 844 | return DoFieldPutCommon<Primitive::kPrimShort, kAssignabilityCheck, kTransaction>( |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 845 | self, shadow_frame, obj, field, value); |
| 846 | case Primitive::kPrimInt: |
| 847 | case Primitive::kPrimFloat: |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 848 | return DoFieldPutCommon<Primitive::kPrimInt, kAssignabilityCheck, kTransaction>( |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 849 | self, shadow_frame, obj, field, value); |
| 850 | case Primitive::kPrimLong: |
| 851 | case Primitive::kPrimDouble: |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 852 | return DoFieldPutCommon<Primitive::kPrimLong, kAssignabilityCheck, kTransaction>( |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 853 | self, shadow_frame, obj, field, value); |
| 854 | case Primitive::kPrimNot: |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 855 | return DoFieldPutCommon<Primitive::kPrimNot, kAssignabilityCheck, kTransaction>( |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 856 | self, shadow_frame, obj, field, value); |
| 857 | case Primitive::kPrimVoid: |
| 858 | LOG(FATAL) << "Unreachable: " << field_type; |
| 859 | UNREACHABLE(); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | static JValue GetValueFromShadowFrame(const ShadowFrame& shadow_frame, |
| 864 | Primitive::Type field_type, |
| 865 | uint32_t vreg) |
| 866 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 867 | JValue field_value; |
| 868 | switch (field_type) { |
| 869 | case Primitive::kPrimBoolean: |
| 870 | field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg))); |
| 871 | break; |
| 872 | case Primitive::kPrimByte: |
| 873 | field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg))); |
| 874 | break; |
| 875 | case Primitive::kPrimChar: |
| 876 | field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg))); |
| 877 | break; |
| 878 | case Primitive::kPrimShort: |
| 879 | field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg))); |
| 880 | break; |
| 881 | case Primitive::kPrimInt: |
| 882 | case Primitive::kPrimFloat: |
| 883 | field_value.SetI(shadow_frame.GetVReg(vreg)); |
| 884 | break; |
| 885 | case Primitive::kPrimLong: |
| 886 | case Primitive::kPrimDouble: |
| 887 | field_value.SetJ(shadow_frame.GetVRegLong(vreg)); |
| 888 | break; |
| 889 | case Primitive::kPrimNot: |
| 890 | field_value.SetL(shadow_frame.GetVRegReference(vreg)); |
| 891 | break; |
| 892 | case Primitive::kPrimVoid: |
| 893 | LOG(FATAL) << "Unreachable: " << field_type; |
| 894 | UNREACHABLE(); |
| 895 | } |
| 896 | return field_value; |
| 897 | } |
| 898 | |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 899 | template <bool do_conversions> |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 900 | bool MethodHandleFieldAccess(Thread* self, |
| 901 | ShadowFrame& shadow_frame, |
| 902 | Handle<mirror::MethodHandle> method_handle, |
| 903 | Handle<mirror::MethodType> callsite_type, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 904 | const InstructionOperands* const operands, |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 905 | JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 906 | StackHandleScope<1> hs(self); |
| 907 | Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType())); |
| 908 | const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind(); |
| 909 | ArtField* field = method_handle->GetTargetField(); |
| 910 | Primitive::Type field_type = field->GetTypeAsPrimitiveType(); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 911 | switch (handle_kind) { |
| 912 | case mirror::MethodHandle::kInstanceGet: { |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 913 | size_t obj_reg = operands->GetOperand(0); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 914 | ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(obj_reg); |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 915 | MethodHandleFieldGet(self, shadow_frame, obj, field, field_type, result); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 916 | if (do_conversions && !ConvertReturnValue(callsite_type, handle_type, result)) { |
| 917 | DCHECK(self->IsExceptionPending()); |
| 918 | return false; |
| 919 | } |
| 920 | return true; |
| 921 | } |
| 922 | case mirror::MethodHandle::kStaticGet: { |
| 923 | ObjPtr<mirror::Object> obj = GetAndInitializeDeclaringClass(self, field); |
| 924 | if (obj == nullptr) { |
| 925 | DCHECK(self->IsExceptionPending()); |
| 926 | return false; |
| 927 | } |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 928 | MethodHandleFieldGet(self, shadow_frame, obj, field, field_type, result); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 929 | if (do_conversions && !ConvertReturnValue(callsite_type, handle_type, result)) { |
| 930 | DCHECK(self->IsExceptionPending()); |
| 931 | return false; |
| 932 | } |
| 933 | return true; |
| 934 | } |
| 935 | case mirror::MethodHandle::kInstancePut: { |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 936 | size_t obj_reg = operands->GetOperand(0); |
| 937 | size_t value_reg = operands->GetOperand(1); |
Mathieu Chartier | 71b1708 | 2017-04-17 20:12:29 -0700 | [diff] [blame] | 938 | const size_t kPTypeIndex = 1; |
| 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 Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 949 | DCHECK(self->IsExceptionPending()); |
| 950 | return false; |
| 951 | } |
| 952 | ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(obj_reg); |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 953 | return MethodHandleFieldPut(self, shadow_frame, obj, field, field_type, value); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 954 | } |
| 955 | case mirror::MethodHandle::kStaticPut: { |
| 956 | ObjPtr<mirror::Object> obj = GetAndInitializeDeclaringClass(self, field); |
| 957 | if (obj == nullptr) { |
| 958 | DCHECK(self->IsExceptionPending()); |
| 959 | return false; |
| 960 | } |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 961 | size_t value_reg = operands->GetOperand(0); |
Mathieu Chartier | 71b1708 | 2017-04-17 20:12:29 -0700 | [diff] [blame] | 962 | const size_t kPTypeIndex = 0; |
| 963 | // Use ptypes instead of field type since we may be unboxing a reference for a primitive |
| 964 | // field. The field type is incorrect for this case. |
| 965 | JValue value = GetValueFromShadowFrame( |
| 966 | shadow_frame, |
| 967 | callsite_type->GetPTypes()->Get(kPTypeIndex)->GetPrimitiveType(), |
| 968 | value_reg); |
| 969 | if (do_conversions && !ConvertArgumentValue(callsite_type, |
| 970 | handle_type, |
| 971 | kPTypeIndex, |
| 972 | &value)) { |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 973 | DCHECK(self->IsExceptionPending()); |
| 974 | return false; |
| 975 | } |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 976 | return MethodHandleFieldPut(self, shadow_frame, obj, field, field_type, value); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 977 | } |
| 978 | default: |
| 979 | LOG(FATAL) << "Unreachable: " << handle_kind; |
| 980 | UNREACHABLE(); |
| 981 | } |
| 982 | } |
| 983 | |
Orion Hodson | b8b9387 | 2018-01-30 07:51:10 +0000 | [diff] [blame] | 984 | bool DoVarHandleInvokeTranslationUnchecked(Thread* self, |
| 985 | ShadowFrame& shadow_frame, |
| 986 | mirror::VarHandle::AccessMode access_mode, |
| 987 | Handle<mirror::VarHandle> vh, |
| 988 | Handle<mirror::MethodType> vh_type, |
| 989 | Handle<mirror::MethodType> callsite_type, |
| 990 | const InstructionOperands* const operands, |
| 991 | JValue* result) |
| 992 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 993 | DCHECK_EQ(operands->GetNumberOfOperands(), static_cast<uint32_t>(vh_type->GetNumberOfPTypes())); |
| 994 | DCHECK_EQ(operands->GetNumberOfOperands(), |
| 995 | static_cast<uint32_t>(callsite_type->GetNumberOfPTypes())); |
| 996 | const size_t vreg_count = vh_type->NumberOfVRegs(); |
| 997 | ShadowFrameAllocaUniquePtr accessor_frame = |
| 998 | CREATE_SHADOW_FRAME(vreg_count, nullptr, shadow_frame.GetMethod(), shadow_frame.GetDexPC()); |
| 999 | ShadowFrameGetter getter(shadow_frame, operands); |
| 1000 | static const uint32_t kFirstAccessorReg = 0; |
| 1001 | ShadowFrameSetter setter(accessor_frame.get(), kFirstAccessorReg); |
| 1002 | if (!PerformConversions(self, callsite_type, vh_type, &getter, &setter)) { |
| 1003 | return false; |
| 1004 | } |
| 1005 | RangeInstructionOperands accessor_operands(kFirstAccessorReg, kFirstAccessorReg + vreg_count); |
| 1006 | if (!vh->Access(access_mode, accessor_frame.get(), &accessor_operands, result)) { |
| 1007 | return false; |
| 1008 | } |
| 1009 | return ConvertReturnValue(callsite_type, vh_type, result); |
| 1010 | } |
| 1011 | |
| 1012 | bool DoVarHandleInvokeTranslation(Thread* self, |
| 1013 | ShadowFrame& shadow_frame, |
| 1014 | bool invokeExact, |
| 1015 | Handle<mirror::MethodHandle> method_handle, |
| 1016 | Handle<mirror::MethodType> callsite_type, |
| 1017 | const InstructionOperands* const operands, |
| 1018 | JValue* result) |
| 1019 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1020 | if (!invokeExact) { |
| 1021 | // Exact invokes are checked for compatability higher up. The |
| 1022 | // non-exact invoke path doesn't have a similar check due to |
| 1023 | // transformers which have EmulatedStack frame arguments with the |
| 1024 | // actual method type associated with the frame. |
| 1025 | if (UNLIKELY(!callsite_type->IsConvertible(method_handle->GetMethodType()))) { |
| 1026 | ThrowWrongMethodTypeException(method_handle->GetMethodType(), callsite_type.Get()); |
| 1027 | return false; |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | // |
| 1032 | // Basic checks that apply in all cases. |
| 1033 | // |
| 1034 | StackHandleScope<6> hs(self); |
| 1035 | Handle<mirror::ObjectArray<mirror::Class>> |
| 1036 | callsite_ptypes(hs.NewHandle(callsite_type->GetPTypes())); |
| 1037 | Handle<mirror::ObjectArray<mirror::Class>> |
| 1038 | mh_ptypes(hs.NewHandle(method_handle->GetMethodType()->GetPTypes())); |
| 1039 | |
| 1040 | // Check that the first parameter is a VarHandle |
| 1041 | if (callsite_ptypes->GetLength() < 1 || |
| 1042 | !mh_ptypes->Get(0)->IsAssignableFrom(callsite_ptypes->Get(0)) || |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 1043 | mh_ptypes->Get(0) != GetClassRoot<mirror::VarHandle>()) { |
Orion Hodson | b8b9387 | 2018-01-30 07:51:10 +0000 | [diff] [blame] | 1044 | ThrowWrongMethodTypeException(method_handle->GetMethodType(), callsite_type.Get()); |
| 1045 | return false; |
| 1046 | } |
| 1047 | |
| 1048 | // Get the receiver |
Vladimir Marko | 4bb2af5 | 2019-03-22 11:09:19 +0000 | [diff] [blame] | 1049 | ObjPtr<mirror::Object> receiver = shadow_frame.GetVRegReference(operands->GetOperand(0)); |
Orion Hodson | b8b9387 | 2018-01-30 07:51:10 +0000 | [diff] [blame] | 1050 | if (receiver == nullptr) { |
| 1051 | ThrowNullPointerException("Expected argument 1 to be a non-null VarHandle"); |
| 1052 | return false; |
| 1053 | } |
| 1054 | |
| 1055 | // Cast to VarHandle instance |
Vladimir Marko | 4bb2af5 | 2019-03-22 11:09:19 +0000 | [diff] [blame] | 1056 | Handle<mirror::VarHandle> vh(hs.NewHandle(ObjPtr<mirror::VarHandle>::DownCast(receiver))); |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 1057 | DCHECK(GetClassRoot<mirror::VarHandle>()->IsAssignableFrom(vh->GetClass())); |
Orion Hodson | b8b9387 | 2018-01-30 07:51:10 +0000 | [diff] [blame] | 1058 | |
| 1059 | // Determine the accessor kind to dispatch |
| 1060 | ArtMethod* target_method = method_handle->GetTargetMethod(); |
| 1061 | int intrinsic_index = target_method->GetIntrinsic(); |
| 1062 | mirror::VarHandle::AccessMode access_mode = |
| 1063 | mirror::VarHandle::GetAccessModeByIntrinsic(static_cast<Intrinsics>(intrinsic_index)); |
| 1064 | Handle<mirror::MethodType> vh_type = |
| 1065 | hs.NewHandle(vh->GetMethodTypeForAccessMode(self, access_mode)); |
| 1066 | Handle<mirror::MethodType> mh_invoke_type = hs.NewHandle( |
| 1067 | mirror::MethodType::CloneWithoutLeadingParameter(self, method_handle->GetMethodType())); |
| 1068 | if (method_handle->GetHandleKind() == mirror::MethodHandle::Kind::kInvokeVarHandleExact) { |
| 1069 | if (!mh_invoke_type->IsExactMatch(vh_type.Get())) { |
| 1070 | ThrowWrongMethodTypeException(vh_type.Get(), mh_invoke_type.Get()); |
| 1071 | return false; |
| 1072 | } |
| 1073 | } else { |
| 1074 | DCHECK_EQ(method_handle->GetHandleKind(), mirror::MethodHandle::Kind::kInvokeVarHandle); |
| 1075 | if (!mh_invoke_type->IsConvertible(vh_type.Get())) { |
| 1076 | ThrowWrongMethodTypeException(vh_type.Get(), mh_invoke_type.Get()); |
| 1077 | return false; |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | Handle<mirror::MethodType> callsite_type_without_varhandle = |
| 1082 | hs.NewHandle(mirror::MethodType::CloneWithoutLeadingParameter(self, callsite_type.Get())); |
| 1083 | NoReceiverInstructionOperands varhandle_operands(operands); |
| 1084 | DCHECK_EQ(static_cast<int32_t>(varhandle_operands.GetNumberOfOperands()), |
| 1085 | callsite_type_without_varhandle->GetPTypes()->GetLength()); |
| 1086 | return DoVarHandleInvokeTranslationUnchecked(self, |
| 1087 | shadow_frame, |
| 1088 | access_mode, |
| 1089 | vh, |
| 1090 | vh_type, |
| 1091 | callsite_type_without_varhandle, |
| 1092 | &varhandle_operands, |
| 1093 | result); |
| 1094 | } |
| 1095 | |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 1096 | static inline bool MethodHandleInvokeInternal(Thread* self, |
| 1097 | ShadowFrame& shadow_frame, |
| 1098 | Handle<mirror::MethodHandle> method_handle, |
| 1099 | Handle<mirror::MethodType> callsite_type, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1100 | const InstructionOperands* const operands, |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 1101 | JValue* result) |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 1102 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1103 | const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind(); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 1104 | if (IsFieldAccess(handle_kind)) { |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 1105 | ObjPtr<mirror::MethodType> handle_type(method_handle->GetMethodType()); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 1106 | DCHECK(!callsite_type->IsExactMatch(handle_type.Ptr())); |
| 1107 | if (!callsite_type->IsConvertible(handle_type.Ptr())) { |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 1108 | ThrowWrongMethodTypeException(handle_type.Ptr(), callsite_type.Get()); |
| 1109 | return false; |
| 1110 | } |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 1111 | const bool do_convert = true; |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1112 | return MethodHandleFieldAccess<do_convert>( |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 1113 | self, |
| 1114 | shadow_frame, |
| 1115 | method_handle, |
| 1116 | callsite_type, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1117 | operands, |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 1118 | result); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 1119 | } |
Orion Hodson | b8b9387 | 2018-01-30 07:51:10 +0000 | [diff] [blame] | 1120 | if (IsInvokeVarHandle(handle_kind)) { |
| 1121 | return DoVarHandleInvokeTranslation(self, |
| 1122 | shadow_frame, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1123 | /*invokeExact=*/ false, |
Orion Hodson | b8b9387 | 2018-01-30 07:51:10 +0000 | [diff] [blame] | 1124 | method_handle, |
| 1125 | callsite_type, |
| 1126 | operands, |
| 1127 | result); |
| 1128 | } |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1129 | return DoInvokePolymorphicMethod(self, |
| 1130 | shadow_frame, |
| 1131 | method_handle, |
| 1132 | callsite_type, |
| 1133 | operands, |
| 1134 | result); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 1137 | static inline bool MethodHandleInvokeExactInternal( |
| 1138 | Thread* self, |
| 1139 | ShadowFrame& shadow_frame, |
| 1140 | Handle<mirror::MethodHandle> method_handle, |
| 1141 | Handle<mirror::MethodType> callsite_type, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1142 | const InstructionOperands* const operands, |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 1143 | JValue* result) |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 1144 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 1145 | StackHandleScope<1> hs(self); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 1146 | Handle<mirror::MethodType> method_handle_type(hs.NewHandle(method_handle->GetMethodType())); |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 1147 | if (!callsite_type->IsExactMatch(method_handle_type.Get())) { |
| 1148 | ThrowWrongMethodTypeException(method_handle_type.Get(), callsite_type.Get()); |
| 1149 | return false; |
| 1150 | } |
| 1151 | |
| 1152 | const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind(); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 1153 | if (IsFieldAccess(handle_kind)) { |
| 1154 | const bool do_convert = false; |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1155 | return MethodHandleFieldAccess<do_convert>(self, |
| 1156 | shadow_frame, |
| 1157 | method_handle, |
| 1158 | callsite_type, |
| 1159 | operands, |
| 1160 | result); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 1163 | // Slow-path check. |
Orion Hodson | b8b9387 | 2018-01-30 07:51:10 +0000 | [diff] [blame] | 1164 | if (IsInvokeTransform(handle_kind) || |
| 1165 | IsCallerTransformer(callsite_type)) { |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1166 | return DoInvokePolymorphicMethod(self, |
| 1167 | shadow_frame, |
| 1168 | method_handle, |
| 1169 | callsite_type, |
| 1170 | operands, |
| 1171 | result); |
Orion Hodson | b8b9387 | 2018-01-30 07:51:10 +0000 | [diff] [blame] | 1172 | } else if (IsInvokeVarHandle(handle_kind)) { |
| 1173 | return DoVarHandleInvokeTranslation(self, |
| 1174 | shadow_frame, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1175 | /*invokeExact=*/ true, |
Orion Hodson | b8b9387 | 2018-01-30 07:51:10 +0000 | [diff] [blame] | 1176 | method_handle, |
| 1177 | callsite_type, |
| 1178 | operands, |
| 1179 | result); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | // On the fast-path. This is equivalent to DoCallPolymoprhic without the conversion paths. |
| 1183 | ArtMethod* target_method = method_handle->GetTargetMethod(); |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1184 | uint32_t receiver_reg = (operands->GetNumberOfOperands() > 0) ? operands->GetOperand(0) : 0u; |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 1185 | ArtMethod* called_method = RefineTargetMethod(self, |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 1186 | shadow_frame, |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 1187 | handle_kind, |
| 1188 | method_handle_type, |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 1189 | callsite_type, |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 1190 | receiver_reg, |
| 1191 | target_method); |
| 1192 | if (called_method == nullptr) { |
| 1193 | DCHECK(self->IsExceptionPending()); |
| 1194 | return false; |
| 1195 | } |
| 1196 | |
| 1197 | // Compute method information. |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 1198 | CodeItemDataAccessor accessor(called_method->DexInstructionData()); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 1199 | uint16_t num_regs; |
| 1200 | size_t num_input_regs; |
| 1201 | size_t first_dest_reg; |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 1202 | if (LIKELY(accessor.HasCodeItem())) { |
| 1203 | num_regs = accessor.RegistersSize(); |
| 1204 | first_dest_reg = num_regs - accessor.InsSize(); |
| 1205 | num_input_regs = accessor.InsSize(); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 1206 | // Parameter registers go at the end of the shadow frame. |
| 1207 | DCHECK_NE(first_dest_reg, (size_t)-1); |
| 1208 | } else { |
| 1209 | // No local regs for proxy and native methods. |
| 1210 | DCHECK(called_method->IsNative() || called_method->IsProxyMethod()); |
| 1211 | num_regs = num_input_regs = GetInsForProxyOrNativeMethod(called_method); |
| 1212 | first_dest_reg = 0; |
| 1213 | } |
| 1214 | |
| 1215 | // Allocate shadow frame on the stack. |
| 1216 | const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon"); |
| 1217 | ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr = |
| 1218 | CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0); |
| 1219 | ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get(); |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1220 | CopyArgumentsFromCallerFrame(shadow_frame, |
| 1221 | new_shadow_frame, |
| 1222 | operands, |
| 1223 | first_dest_reg); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 1224 | self->EndAssertNoThreadSuspension(old_cause); |
| 1225 | |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 1226 | bool use_interpreter_entrypoint = ClassLinker::ShouldUseInterpreterEntrypoint( |
| 1227 | called_method, called_method->GetEntryPointFromQuickCompiledCode()); |
| 1228 | PerformCall(self, |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 1229 | accessor, |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 1230 | shadow_frame.GetMethod(), |
| 1231 | first_dest_reg, |
| 1232 | new_shadow_frame, |
| 1233 | result, |
| 1234 | use_interpreter_entrypoint); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 1235 | if (self->IsExceptionPending()) { |
| 1236 | return false; |
| 1237 | } |
| 1238 | return true; |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | } // namespace |
| 1242 | |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1243 | bool MethodHandleInvoke(Thread* self, |
| 1244 | ShadowFrame& shadow_frame, |
| 1245 | Handle<mirror::MethodHandle> method_handle, |
| 1246 | Handle<mirror::MethodType> callsite_type, |
| 1247 | const InstructionOperands* const operands, |
| 1248 | JValue* result) |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 1249 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 1250 | if (UNLIKELY(callsite_type->IsExactMatch(method_handle->GetMethodType()))) { |
| 1251 | // A non-exact invoke that can be invoked exactly. |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1252 | return MethodHandleInvokeExactInternal(self, |
| 1253 | shadow_frame, |
| 1254 | method_handle, |
| 1255 | callsite_type, |
| 1256 | operands, |
| 1257 | result); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 1258 | } else { |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1259 | return MethodHandleInvokeInternal(self, |
| 1260 | shadow_frame, |
| 1261 | method_handle, |
| 1262 | callsite_type, |
| 1263 | operands, |
| 1264 | result); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 1265 | } |
| 1266 | } |
| 1267 | |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 1268 | bool MethodHandleInvokeExact(Thread* self, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1269 | ShadowFrame& shadow_frame, |
| 1270 | Handle<mirror::MethodHandle> method_handle, |
| 1271 | Handle<mirror::MethodType> callsite_type, |
| 1272 | const InstructionOperands* const operands, |
| 1273 | JValue* result) |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 1274 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1275 | // We need to check the nominal type of the handle in addition to the |
| 1276 | // real type. The "nominal" type is present when MethodHandle.asType is |
| 1277 | // called any handle, and results in the declared type of the handle |
| 1278 | // changing. |
| 1279 | ObjPtr<mirror::MethodType> nominal_type(method_handle->GetNominalType()); |
| 1280 | if (UNLIKELY(nominal_type != nullptr)) { |
| 1281 | if (UNLIKELY(!callsite_type->IsExactMatch(nominal_type.Ptr()))) { |
| 1282 | ThrowWrongMethodTypeException(nominal_type.Ptr(), callsite_type.Get()); |
| 1283 | return false; |
| 1284 | } |
| 1285 | if (LIKELY(!nominal_type->IsExactMatch(method_handle->GetMethodType()))) { |
| 1286 | // Different nominal type means we have to treat as non-exact. |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1287 | return MethodHandleInvokeInternal(self, |
| 1288 | shadow_frame, |
| 1289 | method_handle, |
| 1290 | callsite_type, |
| 1291 | operands, |
| 1292 | result); |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 1293 | } |
| 1294 | } |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1295 | return MethodHandleInvokeExactInternal(self, |
| 1296 | shadow_frame, |
| 1297 | method_handle, |
| 1298 | callsite_type, |
| 1299 | operands, |
| 1300 | result); |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 1301 | } |
| 1302 | |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 1303 | } // namespace art |