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