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 | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 37 | #include "var_handles.h" |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 38 | #include "well_known_classes.h" |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 39 | |
| 40 | namespace art { |
| 41 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 42 | using android::base::StringPrintf; |
| 43 | |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 44 | namespace { |
| 45 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 46 | #define PRIMITIVES_LIST(V) \ |
| 47 | V(Primitive::kPrimBoolean, Boolean, Boolean, Z) \ |
| 48 | V(Primitive::kPrimByte, Byte, Byte, B) \ |
| 49 | V(Primitive::kPrimChar, Char, Character, C) \ |
| 50 | V(Primitive::kPrimShort, Short, Short, S) \ |
| 51 | V(Primitive::kPrimInt, Int, Integer, I) \ |
| 52 | V(Primitive::kPrimLong, Long, Long, J) \ |
| 53 | V(Primitive::kPrimFloat, Float, Float, F) \ |
| 54 | V(Primitive::kPrimDouble, Double, Double, D) |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 55 | |
| 56 | // Assigns |type| to the primitive type associated with |klass|. Returns |
| 57 | // true iff. |klass| was a boxed type (Integer, Long etc.), false otherwise. |
| 58 | bool GetUnboxedPrimitiveType(ObjPtr<mirror::Class> klass, Primitive::Type* type) |
| 59 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 60 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 61 | std::string storage; |
| 62 | const char* descriptor = klass->GetDescriptor(&storage); |
| 63 | static const char kJavaLangPrefix[] = "Ljava/lang/"; |
| 64 | static const size_t kJavaLangPrefixSize = sizeof(kJavaLangPrefix) - 1; |
| 65 | if (strncmp(descriptor, kJavaLangPrefix, kJavaLangPrefixSize) != 0) { |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | descriptor += kJavaLangPrefixSize; |
| 70 | #define LOOKUP_PRIMITIVE(primitive, _, java_name, ___) \ |
| 71 | if (strcmp(descriptor, #java_name ";") == 0) { \ |
| 72 | *type = primitive; \ |
| 73 | return true; \ |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 74 | } |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 75 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 76 | PRIMITIVES_LIST(LOOKUP_PRIMITIVE); |
| 77 | #undef LOOKUP_PRIMITIVE |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 78 | return false; |
| 79 | } |
| 80 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 81 | ObjPtr<mirror::Class> GetBoxedPrimitiveClass(Primitive::Type type) |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 82 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 83 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
| 84 | jmethodID m = nullptr; |
| 85 | switch (type) { |
| 86 | #define CASE_PRIMITIVE(primitive, _, java_name, __) \ |
| 87 | case primitive: \ |
| 88 | m = WellKnownClasses::java_lang_ ## java_name ## _valueOf; \ |
| 89 | break; |
| 90 | PRIMITIVES_LIST(CASE_PRIMITIVE); |
| 91 | #undef CASE_PRIMITIVE |
| 92 | case Primitive::Type::kPrimNot: |
| 93 | case Primitive::Type::kPrimVoid: |
| 94 | return nullptr; |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 95 | } |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 96 | return jni::DecodeArtMethod(m)->GetDeclaringClass(); |
| 97 | } |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 98 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 99 | bool GetUnboxedTypeAndValue(ObjPtr<mirror::Object> o, Primitive::Type* type, JValue* value) |
| 100 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 101 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 102 | ObjPtr<mirror::Class> klass = o->GetClass(); |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 103 | ArtField* primitive_field = &klass->GetIFieldsPtr()->At(0); |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 104 | #define CASE_PRIMITIVE(primitive, abbrev, _, shorthand) \ |
| 105 | if (klass == GetBoxedPrimitiveClass(primitive)) { \ |
| 106 | *type = primitive; \ |
| 107 | value->Set ## shorthand(primitive_field->Get ## abbrev(o)); \ |
| 108 | return true; \ |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 109 | } |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 110 | PRIMITIVES_LIST(CASE_PRIMITIVE) |
| 111 | #undef CASE_PRIMITIVE |
| 112 | return false; |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | inline bool IsReferenceType(Primitive::Type type) { |
| 116 | return type == Primitive::kPrimNot; |
| 117 | } |
| 118 | |
| 119 | inline bool IsPrimitiveType(Primitive::Type type) { |
| 120 | return !IsReferenceType(type); |
| 121 | } |
| 122 | |
| 123 | } // namespace |
| 124 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 125 | bool IsParameterTypeConvertible(ObjPtr<mirror::Class> from, ObjPtr<mirror::Class> to) |
| 126 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 127 | // This function returns true if there's any conceivable conversion |
| 128 | // between |from| and |to|. It's expected this method will be used |
| 129 | // to determine if a WrongMethodTypeException should be raised. The |
| 130 | // decision logic follows the documentation for MethodType.asType(). |
| 131 | if (from == to) { |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | Primitive::Type from_primitive = from->GetPrimitiveType(); |
| 136 | Primitive::Type to_primitive = to->GetPrimitiveType(); |
| 137 | DCHECK(from_primitive != Primitive::Type::kPrimVoid); |
| 138 | DCHECK(to_primitive != Primitive::Type::kPrimVoid); |
| 139 | |
| 140 | // If |to| and |from| are references. |
| 141 | if (IsReferenceType(from_primitive) && IsReferenceType(to_primitive)) { |
| 142 | // Assignability is determined during parameter conversion when |
| 143 | // invoking the associated method handle. |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | // If |to| and |from| are primitives and a widening conversion exists. |
| 148 | if (Primitive::IsWidenable(from_primitive, to_primitive)) { |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | // If |to| is a reference and |from| is a primitive, then boxing conversion. |
| 153 | if (IsReferenceType(to_primitive) && IsPrimitiveType(from_primitive)) { |
| 154 | return to->IsAssignableFrom(GetBoxedPrimitiveClass(from_primitive)); |
| 155 | } |
| 156 | |
| 157 | // If |from| is a reference and |to| is a primitive, then unboxing conversion. |
| 158 | if (IsPrimitiveType(to_primitive) && IsReferenceType(from_primitive)) { |
| 159 | if (from->DescriptorEquals("Ljava/lang/Object;")) { |
| 160 | // Object might be converted into a primitive during unboxing. |
| 161 | return true; |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | if (Primitive::IsNumericType(to_primitive) && from->DescriptorEquals("Ljava/lang/Number;")) { |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 165 | // Number might be unboxed into any of the number primitive types. |
| 166 | return true; |
| 167 | } |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 168 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 169 | Primitive::Type unboxed_type; |
| 170 | if (GetUnboxedPrimitiveType(from, &unboxed_type)) { |
Orion Hodson | f1412b4 | 2016-11-11 12:03:29 +0000 | [diff] [blame] | 171 | if (unboxed_type == to_primitive) { |
| 172 | // Straightforward unboxing conversion such as Boolean => boolean. |
| 173 | return true; |
Orion Hodson | f1412b4 | 2016-11-11 12:03:29 +0000 | [diff] [blame] | 174 | } |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 175 | |
| 176 | // Check if widening operations for numeric primitives would work, |
| 177 | // such as Byte => byte => long. |
| 178 | return Primitive::IsWidenable(unboxed_type, to_primitive); |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | bool IsReturnTypeConvertible(ObjPtr<mirror::Class> from, ObjPtr<mirror::Class> to) |
| 186 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 187 | if (to->GetPrimitiveType() == Primitive::Type::kPrimVoid) { |
| 188 | // Result will be ignored. |
| 189 | return true; |
| 190 | } else if (from->GetPrimitiveType() == Primitive::Type::kPrimVoid) { |
| 191 | // Returned value will be 0 / null. |
| 192 | return true; |
| 193 | } else { |
| 194 | // Otherwise apply usual parameter conversion rules. |
| 195 | return IsParameterTypeConvertible(from, to); |
| 196 | } |
| 197 | } |
| 198 | |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 199 | bool ConvertJValueCommon( |
| 200 | Handle<mirror::MethodType> callsite_type, |
| 201 | Handle<mirror::MethodType> callee_type, |
| 202 | ObjPtr<mirror::Class> from, |
| 203 | ObjPtr<mirror::Class> to, |
| 204 | JValue* value) { |
| 205 | // The reader maybe concerned about the safety of the heap object |
| 206 | // that may be in |value|. There is only one case where allocation |
| 207 | // is obviously needed and that's for boxing. However, in the case |
| 208 | // of boxing |value| contains a non-reference type. |
| 209 | |
| 210 | const Primitive::Type from_type = from->GetPrimitiveType(); |
| 211 | const Primitive::Type to_type = to->GetPrimitiveType(); |
| 212 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 213 | // Put incoming value into |src_value| and set return value to 0. |
| 214 | // Errors and conversions from void require the return value to be 0. |
| 215 | const JValue src_value(*value); |
| 216 | value->SetJ(0); |
| 217 | |
| 218 | // Conversion from void set result to zero. |
| 219 | if (from_type == Primitive::kPrimVoid) { |
| 220 | return true; |
| 221 | } |
| 222 | |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 223 | // This method must be called only when the types don't match. |
| 224 | DCHECK(from != to); |
| 225 | |
| 226 | if (IsPrimitiveType(from_type) && IsPrimitiveType(to_type)) { |
| 227 | // The source and target types are both primitives. |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 228 | if (UNLIKELY(!ConvertPrimitiveValueNoThrow(from_type, to_type, src_value, value))) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 229 | ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get()); |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 230 | return false; |
| 231 | } |
| 232 | return true; |
| 233 | } else if (IsReferenceType(from_type) && IsReferenceType(to_type)) { |
| 234 | // They're both reference types. If "from" is null, we can pass it |
| 235 | // through unchanged. If not, we must generate a cast exception if |
| 236 | // |to| is not assignable from the dynamic type of |ref|. |
| 237 | // |
| 238 | // Playing it safe with StackHandleScope here, not expecting any allocation |
| 239 | // in mirror::Class::IsAssignable(). |
| 240 | StackHandleScope<2> hs(Thread::Current()); |
| 241 | Handle<mirror::Class> h_to(hs.NewHandle(to)); |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 242 | Handle<mirror::Object> h_obj(hs.NewHandle(src_value.GetL())); |
Orion Hodson | c1d3bac | 2018-01-26 14:38:55 +0000 | [diff] [blame] | 243 | if (UNLIKELY(!h_obj.IsNull() && !to->IsAssignableFrom(h_obj->GetClass()))) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 244 | ThrowClassCastException(h_to.Get(), h_obj->GetClass()); |
| 245 | return false; |
| 246 | } |
| 247 | value->SetL(h_obj.Get()); |
| 248 | return true; |
| 249 | } else if (IsReferenceType(to_type)) { |
| 250 | DCHECK(IsPrimitiveType(from_type)); |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 251 | // The source type is a primitive and the target type is a reference, so we must box. |
| 252 | // The target type maybe a super class of the boxed source type, for example, |
| 253 | // if the source type is int, it's boxed type is java.lang.Integer, and the target |
| 254 | // type could be java.lang.Number. |
| 255 | Primitive::Type type; |
| 256 | if (!GetUnboxedPrimitiveType(to, &type)) { |
| 257 | ObjPtr<mirror::Class> boxed_from_class = GetBoxedPrimitiveClass(from_type); |
Orion Hodson | c1d3bac | 2018-01-26 14:38:55 +0000 | [diff] [blame] | 258 | if (LIKELY(boxed_from_class->IsSubClass(to))) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 259 | type = from_type; |
| 260 | } else { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 261 | ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get()); |
| 262 | return false; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | if (UNLIKELY(from_type != type)) { |
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 | |
Orion Hodson | c1d3bac | 2018-01-26 14:38:55 +0000 | [diff] [blame] | 271 | if (UNLIKELY(!ConvertPrimitiveValueNoThrow(from_type, type, src_value, value))) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 272 | ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get()); |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | // Then perform the actual boxing, and then set the reference. |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 277 | ObjPtr<mirror::Object> boxed = BoxPrimitive(type, src_value); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 278 | value->SetL(boxed); |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 279 | return true; |
| 280 | } else { |
| 281 | // The source type is a reference and the target type is a primitive, so we must unbox. |
| 282 | DCHECK(IsReferenceType(from_type)); |
| 283 | DCHECK(IsPrimitiveType(to_type)); |
| 284 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 285 | ObjPtr<mirror::Object> from_obj(src_value.GetL()); |
Orion Hodson | c1d3bac | 2018-01-26 14:38:55 +0000 | [diff] [blame] | 286 | if (UNLIKELY(from_obj.IsNull())) { |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 287 | ThrowNullPointerException( |
| 288 | StringPrintf("Expected to unbox a '%s' primitive type but was returned null", |
| 289 | from->PrettyDescriptor().c_str()).c_str()); |
| 290 | return false; |
| 291 | } |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 292 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 293 | Primitive::Type unboxed_type; |
| 294 | JValue unboxed_value; |
| 295 | if (UNLIKELY(!GetUnboxedTypeAndValue(from_obj, &unboxed_type, &unboxed_value))) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 296 | ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get()); |
| 297 | return false; |
| 298 | } |
| 299 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 300 | if (UNLIKELY(!ConvertPrimitiveValueNoThrow(unboxed_type, to_type, unboxed_value, value))) { |
Orion Hodson | c1d3bac | 2018-01-26 14:38:55 +0000 | [diff] [blame] | 301 | if (from->IsAssignableFrom(GetBoxedPrimitiveClass(to_type))) { |
| 302 | // CallSite may be Number, but the Number object is |
| 303 | // incompatible, e.g. Number (Integer) for a short. |
| 304 | ThrowClassCastException(from, to); |
| 305 | } else { |
| 306 | // CallSite is incompatible, e.g. Integer for a short. |
| 307 | ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get()); |
| 308 | } |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 309 | return false; |
| 310 | } |
| 311 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 312 | return true; |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 316 | namespace { |
| 317 | |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 318 | inline void CopyArgumentsFromCallerFrame(const ShadowFrame& caller_frame, |
| 319 | ShadowFrame* callee_frame, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 320 | const InstructionOperands* const operands, |
| 321 | const size_t first_dst_reg) |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 322 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 323 | for (size_t i = 0; i < operands->GetNumberOfOperands(); ++i) { |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 324 | size_t dst_reg = first_dst_reg + i; |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 325 | size_t src_reg = operands->GetOperand(i); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 326 | // Uint required, so that sign extension does not make this wrong on 64-bit systems |
| 327 | uint32_t src_value = caller_frame.GetVReg(src_reg); |
| 328 | ObjPtr<mirror::Object> o = caller_frame.GetVRegReference<kVerifyNone>(src_reg); |
| 329 | // If both register locations contains the same value, the register probably holds a reference. |
| 330 | // Note: As an optimization, non-moving collectors leave a stale reference value |
| 331 | // in the references array even after the original vreg was overwritten to a non-reference. |
| 332 | if (src_value == reinterpret_cast<uintptr_t>(o.Ptr())) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 333 | callee_frame->SetVRegReference(dst_reg, o); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 334 | } else { |
| 335 | callee_frame->SetVReg(dst_reg, src_value); |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 340 | // Calculate the number of ins for a proxy or native method, where we |
| 341 | // can't just look at the code item. |
| 342 | static inline size_t GetInsForProxyOrNativeMethod(ArtMethod* method) |
| 343 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 344 | DCHECK(method->IsNative() || method->IsProxyMethod()); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 345 | method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 346 | uint32_t shorty_length = 0; |
| 347 | const char* shorty = method->GetShorty(&shorty_length); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 348 | |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 349 | // Static methods do not include the receiver. The receiver isn't included |
| 350 | // in the shorty_length though the return value is. |
| 351 | size_t num_ins = method->IsStatic() ? shorty_length - 1 : shorty_length; |
| 352 | for (const char* c = shorty + 1; *c != '\0'; ++c) { |
| 353 | if (*c == 'J' || *c == 'D') { |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 354 | ++num_ins; |
| 355 | } |
| 356 | } |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 357 | return num_ins; |
| 358 | } |
| 359 | |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 360 | static inline bool MethodHandleInvokeTransform(Thread* self, |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 361 | ShadowFrame& shadow_frame, |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 362 | Handle<mirror::MethodHandle> method_handle, |
| 363 | Handle<mirror::MethodType> callsite_type, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 364 | const InstructionOperands* const operands, |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 365 | JValue* result) |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 366 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 367 | // This can be fixed to two, because the method we're calling here |
| 368 | // (MethodHandle.transformInternal) doesn't have any locals and the signature |
| 369 | // is known : |
| 370 | // |
| 371 | // private MethodHandle.transformInternal(EmulatedStackFrame sf); |
| 372 | // |
| 373 | // This means we need only two vregs : |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 374 | // - One for the method_handle object. |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 375 | // - One for the only method argument (an EmulatedStackFrame). |
| 376 | static constexpr size_t kNumRegsForTransform = 2; |
| 377 | |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 378 | ArtMethod* called_method = method_handle->GetTargetMethod(); |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 379 | CodeItemDataAccessor accessor(called_method->DexInstructionData()); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 380 | DCHECK_EQ(kNumRegsForTransform, accessor.RegistersSize()); |
| 381 | DCHECK_EQ(kNumRegsForTransform, accessor.InsSize()); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 382 | |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 383 | StackHandleScope<2> hs(self); |
| 384 | Handle<mirror::MethodType> callee_type(hs.NewHandle(method_handle->GetMethodType())); |
| 385 | Handle<mirror::EmulatedStackFrame> sf( |
| 386 | hs.NewHandle<mirror::EmulatedStackFrame>( |
| 387 | mirror::EmulatedStackFrame::CreateFromShadowFrameAndArgs( |
| 388 | self, callsite_type, callee_type, shadow_frame, operands))); |
| 389 | if (sf == nullptr) { |
| 390 | DCHECK(self->IsExceptionPending()); |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | const char* old_cause = self->StartAssertNoThreadSuspension("MethodHandleInvokeTransform"); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 395 | ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr = |
| 396 | CREATE_SHADOW_FRAME(kNumRegsForTransform, &shadow_frame, called_method, /* dex pc */ 0); |
| 397 | ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get(); |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 398 | new_shadow_frame->SetVRegReference(0, method_handle.Get()); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 399 | new_shadow_frame->SetVRegReference(1, sf.Get()); |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 400 | self->EndAssertNoThreadSuspension(old_cause); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 401 | |
| 402 | PerformCall(self, |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 403 | accessor, |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 404 | shadow_frame.GetMethod(), |
| 405 | 0 /* first destination register */, |
| 406 | new_shadow_frame, |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 407 | result, |
Nicolas Geoffray | 854af03 | 2021-12-21 08:32:42 +0000 | [diff] [blame] | 408 | interpreter::ShouldStayInSwitchInterpreter(called_method)); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 409 | if (self->IsExceptionPending()) { |
| 410 | return false; |
| 411 | } |
| 412 | |
| 413 | // If the called transformer method we called has returned a value, then we |
| 414 | // need to copy it back to |result|. |
| 415 | sf->GetReturnValue(self, result); |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 416 | return true; |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | inline static ObjPtr<mirror::Class> GetAndInitializeDeclaringClass(Thread* self, ArtField* field) |
| 420 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 421 | // Method handle invocations on static fields should ensure class is |
| 422 | // initialized. This usually happens when an instance is constructed |
| 423 | // or class members referenced, but this is not guaranteed when |
| 424 | // looking up method handles. |
| 425 | ObjPtr<mirror::Class> klass = field->GetDeclaringClass(); |
| 426 | if (UNLIKELY(!klass->IsInitialized())) { |
| 427 | StackHandleScope<1> hs(self); |
| 428 | HandleWrapperObjPtr<mirror::Class> h(hs.NewHandleWrapper(&klass)); |
| 429 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h, true, true)) { |
| 430 | DCHECK(self->IsExceptionPending()); |
| 431 | return nullptr; |
| 432 | } |
| 433 | } |
| 434 | return klass; |
| 435 | } |
| 436 | |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 437 | ArtMethod* RefineTargetMethod(Thread* self, |
| 438 | ShadowFrame& shadow_frame, |
| 439 | const mirror::MethodHandle::Kind& handle_kind, |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 440 | ObjPtr<mirror::MethodType> handle_type, |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 441 | const uint32_t receiver_reg, |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 442 | ArtMethod* target_method) REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 443 | if (handle_kind == mirror::MethodHandle::Kind::kInvokeVirtual || |
| 444 | handle_kind == mirror::MethodHandle::Kind::kInvokeInterface) { |
| 445 | // For virtual and interface methods ensure target_method points to |
| 446 | // the actual method to invoke. |
| 447 | ObjPtr<mirror::Object> receiver(shadow_frame.GetVRegReference(receiver_reg)); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 448 | ObjPtr<mirror::Class> declaring_class(target_method->GetDeclaringClass()); |
| 449 | if (receiver == nullptr || receiver->GetClass() != declaring_class) { |
| 450 | // Verify that _vRegC is an object reference and of the type expected by |
| 451 | // the receiver. |
| 452 | if (!VerifyObjectIsClass(receiver, declaring_class)) { |
| 453 | DCHECK(self->IsExceptionPending()); |
| 454 | return nullptr; |
| 455 | } |
| 456 | return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface( |
| 457 | target_method, kRuntimePointerSize); |
| 458 | } |
| 459 | } else if (handle_kind == mirror::MethodHandle::Kind::kInvokeDirect) { |
| 460 | // String constructors are a special case, they are replaced with |
| 461 | // StringFactory methods. |
| 462 | if (target_method->IsConstructor() && target_method->GetDeclaringClass()->IsStringClass()) { |
| 463 | DCHECK(handle_type->GetRType()->IsStringClass()); |
| 464 | return WellKnownClasses::StringInitToStringFactory(target_method); |
| 465 | } |
| 466 | } else if (handle_kind == mirror::MethodHandle::Kind::kInvokeSuper) { |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 467 | // Note that we're not dynamically dispatching on the type of the receiver |
| 468 | // here. We use the static type of the "receiver" object that we've |
| 469 | // recorded in the method handle's type, which will be the same as the |
| 470 | // special caller that was specified at the point of lookup. |
| 471 | ObjPtr<mirror::Class> referrer_class = handle_type->GetPTypes()->Get(0); |
Orion Hodson | 0f59574 | 2018-04-10 14:49:28 +0100 | [diff] [blame] | 472 | ObjPtr<mirror::Class> declaring_class = target_method->GetDeclaringClass(); |
| 473 | if (referrer_class == declaring_class) { |
| 474 | return target_method; |
| 475 | } |
Alex Light | 49df715 | 2019-10-03 11:22:35 -0700 | [diff] [blame] | 476 | if (declaring_class->IsInterface()) { |
| 477 | if (target_method->IsAbstract()) { |
| 478 | std::string msg = |
| 479 | "Method " + target_method->PrettyMethod() + " is abstract interface method!"; |
| 480 | ThrowIllegalAccessException(msg.c_str()); |
| 481 | return nullptr; |
| 482 | } |
| 483 | } else { |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 484 | ObjPtr<mirror::Class> super_class = referrer_class->GetSuperClass(); |
| 485 | uint16_t vtable_index = target_method->GetMethodIndex(); |
| 486 | DCHECK(super_class != nullptr); |
| 487 | DCHECK(super_class->HasVTable()); |
| 488 | // Note that super_class is a super of referrer_class and target_method |
| 489 | // will always be declared by super_class (or one of its super classes). |
| 490 | DCHECK_LT(vtable_index, super_class->GetVTableLength()); |
| 491 | return super_class->GetVTableEntry(vtable_index, kRuntimePointerSize); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | return target_method; |
| 495 | } |
| 496 | |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 497 | // Helper for getters in invoke-polymorphic. |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 498 | inline static void MethodHandleFieldGet(Thread* self, |
| 499 | const ShadowFrame& shadow_frame, |
| 500 | ObjPtr<mirror::Object>& obj, |
| 501 | ArtField* field, |
| 502 | Primitive::Type field_type, |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 503 | JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 504 | switch (field_type) { |
| 505 | case Primitive::kPrimBoolean: |
| 506 | DoFieldGetCommon<Primitive::kPrimBoolean>(self, shadow_frame, obj, field, result); |
| 507 | break; |
| 508 | case Primitive::kPrimByte: |
| 509 | DoFieldGetCommon<Primitive::kPrimByte>(self, shadow_frame, obj, field, result); |
| 510 | break; |
| 511 | case Primitive::kPrimChar: |
| 512 | DoFieldGetCommon<Primitive::kPrimChar>(self, shadow_frame, obj, field, result); |
| 513 | break; |
| 514 | case Primitive::kPrimShort: |
| 515 | DoFieldGetCommon<Primitive::kPrimShort>(self, shadow_frame, obj, field, result); |
| 516 | break; |
| 517 | case Primitive::kPrimInt: |
| 518 | DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result); |
| 519 | break; |
| 520 | case Primitive::kPrimLong: |
| 521 | DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result); |
| 522 | break; |
| 523 | case Primitive::kPrimFloat: |
| 524 | DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result); |
| 525 | break; |
| 526 | case Primitive::kPrimDouble: |
| 527 | DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result); |
| 528 | break; |
| 529 | case Primitive::kPrimNot: |
| 530 | DoFieldGetCommon<Primitive::kPrimNot>(self, shadow_frame, obj, field, result); |
| 531 | break; |
| 532 | case Primitive::kPrimVoid: |
| 533 | LOG(FATAL) << "Unreachable: " << field_type; |
| 534 | UNREACHABLE(); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | // Helper for setters in invoke-polymorphic. |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 539 | inline bool MethodHandleFieldPut(Thread* self, |
| 540 | ShadowFrame& shadow_frame, |
| 541 | ObjPtr<mirror::Object>& obj, |
| 542 | ArtField* field, |
| 543 | Primitive::Type field_type, |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 544 | JValue& value) REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 545 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
| 546 | static const bool kTransaction = false; // Not in a transaction. |
| 547 | static const bool kAssignabilityCheck = false; // No access check. |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 548 | switch (field_type) { |
| 549 | case Primitive::kPrimBoolean: |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 550 | return |
| 551 | DoFieldPutCommon<Primitive::kPrimBoolean, kAssignabilityCheck, kTransaction>( |
| 552 | self, shadow_frame, obj, field, value); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 553 | case Primitive::kPrimByte: |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 554 | return DoFieldPutCommon<Primitive::kPrimByte, kAssignabilityCheck, kTransaction>( |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 555 | self, shadow_frame, obj, field, value); |
| 556 | case Primitive::kPrimChar: |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 557 | return DoFieldPutCommon<Primitive::kPrimChar, kAssignabilityCheck, kTransaction>( |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 558 | self, shadow_frame, obj, field, value); |
| 559 | case Primitive::kPrimShort: |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 560 | return DoFieldPutCommon<Primitive::kPrimShort, kAssignabilityCheck, kTransaction>( |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 561 | self, shadow_frame, obj, field, value); |
| 562 | case Primitive::kPrimInt: |
| 563 | case Primitive::kPrimFloat: |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 564 | return DoFieldPutCommon<Primitive::kPrimInt, kAssignabilityCheck, kTransaction>( |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 565 | self, shadow_frame, obj, field, value); |
| 566 | case Primitive::kPrimLong: |
| 567 | case Primitive::kPrimDouble: |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 568 | return DoFieldPutCommon<Primitive::kPrimLong, kAssignabilityCheck, kTransaction>( |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 569 | self, shadow_frame, obj, field, value); |
| 570 | case Primitive::kPrimNot: |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 571 | return DoFieldPutCommon<Primitive::kPrimNot, kAssignabilityCheck, kTransaction>( |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 572 | self, shadow_frame, obj, field, value); |
| 573 | case Primitive::kPrimVoid: |
| 574 | LOG(FATAL) << "Unreachable: " << field_type; |
| 575 | UNREACHABLE(); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | static JValue GetValueFromShadowFrame(const ShadowFrame& shadow_frame, |
| 580 | Primitive::Type field_type, |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 581 | uint32_t vreg) REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 582 | JValue field_value; |
| 583 | switch (field_type) { |
| 584 | case Primitive::kPrimBoolean: |
| 585 | field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg))); |
| 586 | break; |
| 587 | case Primitive::kPrimByte: |
| 588 | field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg))); |
| 589 | break; |
| 590 | case Primitive::kPrimChar: |
| 591 | field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg))); |
| 592 | break; |
| 593 | case Primitive::kPrimShort: |
| 594 | field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg))); |
| 595 | break; |
| 596 | case Primitive::kPrimInt: |
| 597 | case Primitive::kPrimFloat: |
| 598 | field_value.SetI(shadow_frame.GetVReg(vreg)); |
| 599 | break; |
| 600 | case Primitive::kPrimLong: |
| 601 | case Primitive::kPrimDouble: |
| 602 | field_value.SetJ(shadow_frame.GetVRegLong(vreg)); |
| 603 | break; |
| 604 | case Primitive::kPrimNot: |
| 605 | field_value.SetL(shadow_frame.GetVRegReference(vreg)); |
| 606 | break; |
| 607 | case Primitive::kPrimVoid: |
| 608 | LOG(FATAL) << "Unreachable: " << field_type; |
| 609 | UNREACHABLE(); |
| 610 | } |
| 611 | return field_value; |
| 612 | } |
| 613 | |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 614 | bool MethodHandleFieldAccess(Thread* self, |
| 615 | ShadowFrame& shadow_frame, |
| 616 | Handle<mirror::MethodHandle> method_handle, |
| 617 | Handle<mirror::MethodType> callsite_type, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 618 | const InstructionOperands* const operands, |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 619 | JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 620 | StackHandleScope<1> hs(self); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 621 | const mirror::MethodHandle::Kind handle_kind = method_handle->GetHandleKind(); |
| 622 | ArtField* field = method_handle->GetTargetField(); |
| 623 | Primitive::Type field_type = field->GetTypeAsPrimitiveType(); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 624 | switch (handle_kind) { |
| 625 | case mirror::MethodHandle::kInstanceGet: { |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 626 | size_t obj_reg = operands->GetOperand(0); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 627 | ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(obj_reg); |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 628 | MethodHandleFieldGet(self, shadow_frame, obj, field, field_type, result); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 629 | return true; |
| 630 | } |
| 631 | case mirror::MethodHandle::kStaticGet: { |
| 632 | ObjPtr<mirror::Object> obj = GetAndInitializeDeclaringClass(self, field); |
| 633 | if (obj == nullptr) { |
| 634 | DCHECK(self->IsExceptionPending()); |
| 635 | return false; |
| 636 | } |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 637 | MethodHandleFieldGet(self, shadow_frame, obj, field, field_type, result); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 638 | return true; |
| 639 | } |
| 640 | case mirror::MethodHandle::kInstancePut: { |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 641 | size_t obj_reg = operands->GetOperand(0); |
| 642 | size_t value_reg = operands->GetOperand(1); |
Mathieu Chartier | 71b1708 | 2017-04-17 20:12:29 -0700 | [diff] [blame] | 643 | const size_t kPTypeIndex = 1; |
| 644 | // Use ptypes instead of field type since we may be unboxing a reference for a primitive |
| 645 | // field. The field type is incorrect for this case. |
| 646 | JValue value = GetValueFromShadowFrame( |
| 647 | shadow_frame, |
| 648 | callsite_type->GetPTypes()->Get(kPTypeIndex)->GetPrimitiveType(), |
| 649 | value_reg); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 650 | ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(obj_reg); |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 651 | return MethodHandleFieldPut(self, shadow_frame, obj, field, field_type, value); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 652 | } |
| 653 | case mirror::MethodHandle::kStaticPut: { |
| 654 | ObjPtr<mirror::Object> obj = GetAndInitializeDeclaringClass(self, field); |
| 655 | if (obj == nullptr) { |
| 656 | DCHECK(self->IsExceptionPending()); |
| 657 | return false; |
| 658 | } |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 659 | size_t value_reg = operands->GetOperand(0); |
Mathieu Chartier | 71b1708 | 2017-04-17 20:12:29 -0700 | [diff] [blame] | 660 | const size_t kPTypeIndex = 0; |
| 661 | // Use ptypes instead of field type since we may be unboxing a reference for a primitive |
| 662 | // field. The field type is incorrect for this case. |
| 663 | JValue value = GetValueFromShadowFrame( |
| 664 | shadow_frame, |
| 665 | callsite_type->GetPTypes()->Get(kPTypeIndex)->GetPrimitiveType(), |
| 666 | value_reg); |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 667 | return MethodHandleFieldPut(self, shadow_frame, obj, field, field_type, value); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 668 | } |
| 669 | default: |
| 670 | LOG(FATAL) << "Unreachable: " << handle_kind; |
| 671 | UNREACHABLE(); |
| 672 | } |
| 673 | } |
| 674 | |
Orion Hodson | b8b9387 | 2018-01-30 07:51:10 +0000 | [diff] [blame] | 675 | bool DoVarHandleInvokeTranslation(Thread* self, |
| 676 | ShadowFrame& shadow_frame, |
Orion Hodson | b8b9387 | 2018-01-30 07:51:10 +0000 | [diff] [blame] | 677 | Handle<mirror::MethodHandle> method_handle, |
| 678 | Handle<mirror::MethodType> callsite_type, |
| 679 | const InstructionOperands* const operands, |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 680 | JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | b8b9387 | 2018-01-30 07:51:10 +0000 | [diff] [blame] | 681 | // |
| 682 | // Basic checks that apply in all cases. |
| 683 | // |
| 684 | StackHandleScope<6> hs(self); |
| 685 | Handle<mirror::ObjectArray<mirror::Class>> |
| 686 | callsite_ptypes(hs.NewHandle(callsite_type->GetPTypes())); |
| 687 | Handle<mirror::ObjectArray<mirror::Class>> |
| 688 | mh_ptypes(hs.NewHandle(method_handle->GetMethodType()->GetPTypes())); |
| 689 | |
| 690 | // Check that the first parameter is a VarHandle |
| 691 | if (callsite_ptypes->GetLength() < 1 || |
| 692 | !mh_ptypes->Get(0)->IsAssignableFrom(callsite_ptypes->Get(0)) || |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 693 | mh_ptypes->Get(0) != GetClassRoot<mirror::VarHandle>()) { |
Orion Hodson | b8b9387 | 2018-01-30 07:51:10 +0000 | [diff] [blame] | 694 | ThrowWrongMethodTypeException(method_handle->GetMethodType(), callsite_type.Get()); |
| 695 | return false; |
| 696 | } |
| 697 | |
| 698 | // Get the receiver |
Vladimir Marko | 4bb2af5 | 2019-03-22 11:09:19 +0000 | [diff] [blame] | 699 | ObjPtr<mirror::Object> receiver = shadow_frame.GetVRegReference(operands->GetOperand(0)); |
Orion Hodson | b8b9387 | 2018-01-30 07:51:10 +0000 | [diff] [blame] | 700 | if (receiver == nullptr) { |
| 701 | ThrowNullPointerException("Expected argument 1 to be a non-null VarHandle"); |
| 702 | return false; |
| 703 | } |
| 704 | |
| 705 | // Cast to VarHandle instance |
Vladimir Marko | 4bb2af5 | 2019-03-22 11:09:19 +0000 | [diff] [blame] | 706 | Handle<mirror::VarHandle> vh(hs.NewHandle(ObjPtr<mirror::VarHandle>::DownCast(receiver))); |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 707 | DCHECK(GetClassRoot<mirror::VarHandle>()->IsAssignableFrom(vh->GetClass())); |
Orion Hodson | b8b9387 | 2018-01-30 07:51:10 +0000 | [diff] [blame] | 708 | |
| 709 | // Determine the accessor kind to dispatch |
| 710 | ArtMethod* target_method = method_handle->GetTargetMethod(); |
| 711 | int intrinsic_index = target_method->GetIntrinsic(); |
| 712 | mirror::VarHandle::AccessMode access_mode = |
| 713 | mirror::VarHandle::GetAccessModeByIntrinsic(static_cast<Intrinsics>(intrinsic_index)); |
| 714 | Handle<mirror::MethodType> vh_type = |
| 715 | hs.NewHandle(vh->GetMethodTypeForAccessMode(self, access_mode)); |
| 716 | Handle<mirror::MethodType> mh_invoke_type = hs.NewHandle( |
| 717 | mirror::MethodType::CloneWithoutLeadingParameter(self, method_handle->GetMethodType())); |
| 718 | if (method_handle->GetHandleKind() == mirror::MethodHandle::Kind::kInvokeVarHandleExact) { |
| 719 | if (!mh_invoke_type->IsExactMatch(vh_type.Get())) { |
| 720 | ThrowWrongMethodTypeException(vh_type.Get(), mh_invoke_type.Get()); |
| 721 | return false; |
| 722 | } |
Orion Hodson | b8b9387 | 2018-01-30 07:51:10 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | Handle<mirror::MethodType> callsite_type_without_varhandle = |
| 726 | hs.NewHandle(mirror::MethodType::CloneWithoutLeadingParameter(self, callsite_type.Get())); |
| 727 | NoReceiverInstructionOperands varhandle_operands(operands); |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 728 | return VarHandleInvokeAccessor(self, |
| 729 | shadow_frame, |
| 730 | vh, |
| 731 | callsite_type_without_varhandle, |
| 732 | access_mode, |
| 733 | &varhandle_operands, |
| 734 | result); |
Orion Hodson | b8b9387 | 2018-01-30 07:51:10 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 737 | static bool DoMethodHandleInvokeMethod(Thread* self, |
| 738 | ShadowFrame& shadow_frame, |
| 739 | Handle<mirror::MethodHandle> method_handle, |
| 740 | const InstructionOperands* const operands, |
| 741 | JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 742 | ArtMethod* target_method = method_handle->GetTargetMethod(); |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 743 | uint32_t receiver_reg = (operands->GetNumberOfOperands() > 0) ? operands->GetOperand(0) : 0u; |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 744 | ArtMethod* called_method = RefineTargetMethod(self, |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 745 | shadow_frame, |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 746 | method_handle->GetHandleKind(), |
| 747 | method_handle->GetMethodType(), |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 748 | receiver_reg, |
| 749 | target_method); |
| 750 | if (called_method == nullptr) { |
| 751 | DCHECK(self->IsExceptionPending()); |
| 752 | return false; |
| 753 | } |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 754 | // Compute method information. |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 755 | CodeItemDataAccessor accessor(called_method->DexInstructionData()); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 756 | uint16_t num_regs; |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 757 | size_t first_dest_reg; |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 758 | if (LIKELY(accessor.HasCodeItem())) { |
| 759 | num_regs = accessor.RegistersSize(); |
| 760 | first_dest_reg = num_regs - accessor.InsSize(); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 761 | // Parameter registers go at the end of the shadow frame. |
| 762 | DCHECK_NE(first_dest_reg, (size_t)-1); |
| 763 | } else { |
| 764 | // No local regs for proxy and native methods. |
| 765 | DCHECK(called_method->IsNative() || called_method->IsProxyMethod()); |
Orion Hodson | c6270df | 2022-01-28 18:11:03 +0000 | [diff] [blame] | 766 | num_regs = GetInsForProxyOrNativeMethod(called_method); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 767 | first_dest_reg = 0; |
| 768 | } |
| 769 | |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 770 | const char* old_cause = self->StartAssertNoThreadSuspension("DoMethodHandleInvokeMethod"); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 771 | ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr = |
| 772 | CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0); |
| 773 | ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get(); |
Orion Hodson | b34c348 | 2022-02-05 17:18:45 +0000 | [diff] [blame] | 774 | CopyArgumentsFromCallerFrame(shadow_frame, new_shadow_frame, operands, first_dest_reg); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 775 | self->EndAssertNoThreadSuspension(old_cause); |
| 776 | |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 777 | PerformCall(self, |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 778 | accessor, |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 779 | shadow_frame.GetMethod(), |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 780 | first_dest_reg, |
| 781 | new_shadow_frame, |
| 782 | result, |
Nicolas Geoffray | 854af03 | 2021-12-21 08:32:42 +0000 | [diff] [blame] | 783 | interpreter::ShouldStayInSwitchInterpreter(called_method)); |
Orion Hodson | a1be713 | 2017-03-21 10:04:12 +0000 | [diff] [blame] | 784 | if (self->IsExceptionPending()) { |
| 785 | return false; |
| 786 | } |
| 787 | return true; |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 790 | static bool MethodHandleInvokeExactInternal(Thread* self, |
| 791 | ShadowFrame& shadow_frame, |
| 792 | Handle<mirror::MethodHandle> method_handle, |
| 793 | Handle<mirror::MethodType> callsite_type, |
| 794 | const InstructionOperands* const operands, |
| 795 | JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 796 | if (!callsite_type->IsExactMatch(method_handle->GetMethodType())) { |
| 797 | ThrowWrongMethodTypeException(method_handle->GetMethodType(), callsite_type.Get()); |
| 798 | return false; |
Orion Hodson | 3b1b216 | 2021-11-24 08:54:16 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 801 | switch (method_handle->GetHandleKind()) { |
| 802 | case mirror::MethodHandle::Kind::kInvokeDirect: |
| 803 | case mirror::MethodHandle::Kind::kInvokeInterface: |
| 804 | case mirror::MethodHandle::Kind::kInvokeStatic: |
| 805 | case mirror::MethodHandle::Kind::kInvokeSuper: |
| 806 | case mirror::MethodHandle::Kind::kInvokeVirtual: |
| 807 | return DoMethodHandleInvokeMethod(self, shadow_frame, method_handle, operands, result); |
| 808 | case mirror::MethodHandle::Kind::kInstanceGet: |
| 809 | case mirror::MethodHandle::Kind::kInstancePut: |
| 810 | case mirror::MethodHandle::Kind::kStaticGet: |
| 811 | case mirror::MethodHandle::Kind::kStaticPut: |
| 812 | return MethodHandleFieldAccess( |
| 813 | self, shadow_frame, method_handle, callsite_type, operands, result); |
| 814 | case mirror::MethodHandle::Kind::kInvokeTransform: |
| 815 | return MethodHandleInvokeTransform( |
| 816 | self, shadow_frame, method_handle, callsite_type, operands, result); |
| 817 | case mirror::MethodHandle::Kind::kInvokeVarHandle: |
| 818 | case mirror::MethodHandle::Kind::kInvokeVarHandleExact: |
| 819 | return DoVarHandleInvokeTranslation( |
| 820 | self, shadow_frame, method_handle, callsite_type, operands, result); |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | static bool MethodHandleInvokeInternal(Thread* self, |
| 825 | ShadowFrame& shadow_frame, |
| 826 | Handle<mirror::MethodHandle> method_handle, |
| 827 | Handle<mirror::MethodType> callsite_type, |
| 828 | const InstructionOperands* const operands, |
| 829 | JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 830 | StackHandleScope<2> hs(self); |
| 831 | Handle<mirror::MethodType> method_handle_type(hs.NewHandle(method_handle->GetMethodType())); |
| 832 | // Non-exact invoke behaves as calling mh.asType(newType). In ART, asType() is implemented |
| 833 | // as a transformer and it is expensive to call so check first if it's really necessary. |
| 834 | // |
| 835 | // There are two cases where the asType() transformation can be skipped: |
| 836 | // |
| 837 | // 1) the call site and type of the MethodHandle match, ie code is calling invoke() |
| 838 | // unnecessarily. |
| 839 | // |
| 840 | // 2) when the call site can be trivially converted to the MethodHandle type due to how |
| 841 | // values are represented in the ShadowFrame, ie all registers in the shadow frame are |
| 842 | // 32-bit, there is no byte, short, char, etc. So a call site with arguments of these |
| 843 | // kinds can be trivially converted to one with int arguments. Similarly if the reference |
| 844 | // types are assignable between the call site and MethodHandle type, then as asType() |
| 845 | // transformation isn't really doing any work. |
| 846 | // |
| 847 | // The following IsInPlaceConvertible check determines if either of these opportunities to |
| 848 | // skip asType() are true. |
| 849 | if (callsite_type->IsInPlaceConvertible(method_handle_type.Get())) { |
| 850 | return MethodHandleInvokeExact( |
| 851 | self, shadow_frame, method_handle, method_handle_type, operands, result); |
| 852 | } |
| 853 | |
| 854 | // Use asType() variant of this MethodHandle to adapt callsite to the target. |
| 855 | MutableHandle<mirror::MethodHandle> atc(hs.NewHandle(method_handle->GetAsTypeCache())); |
| 856 | if (atc == nullptr || !callsite_type->IsExactMatch(atc->GetMethodType())) { |
| 857 | // Cached asType adapter does not exist or is for another call site. Call |
| 858 | // MethodHandle::asType() to get an appropriate adapter. |
| 859 | ArtMethod* as_type = |
| 860 | jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_asType); |
| 861 | uint32_t as_type_args[] = { |
| 862 | static_cast<uint32_t>(reinterpret_cast<uintptr_t>(method_handle.Get())), |
| 863 | static_cast<uint32_t>(reinterpret_cast<uintptr_t>(callsite_type.Get()))}; |
| 864 | JValue atc_result; |
| 865 | as_type->Invoke(self, as_type_args, sizeof(as_type_args), &atc_result, "LL"); |
| 866 | if (atc_result.GetL() == nullptr) { |
| 867 | DCHECK(self->IsExceptionPending()); |
| 868 | return false; |
| 869 | } |
| 870 | ObjPtr<mirror::MethodHandle> atc_method_handle = |
| 871 | down_cast<mirror::MethodHandle*>(atc_result.GetL()); |
| 872 | atc.Assign(atc_method_handle); |
| 873 | DCHECK(!atc.IsNull()); |
| 874 | } |
| 875 | |
| 876 | return MethodHandleInvokeExact(self, shadow_frame, atc, callsite_type, operands, result); |
| 877 | } |
| 878 | |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 879 | } // namespace |
| 880 | |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 881 | bool MethodHandleInvoke(Thread* self, |
Orion Hodson | 3b1b216 | 2021-11-24 08:54:16 +0000 | [diff] [blame] | 882 | ShadowFrame& shadow_frame, |
| 883 | Handle<mirror::MethodHandle> method_handle, |
| 884 | Handle<mirror::MethodType> callsite_type, |
| 885 | const InstructionOperands* const operands, |
| 886 | JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 887 | return MethodHandleInvokeInternal( |
| 888 | self, shadow_frame, method_handle, callsite_type, operands, result); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 891 | bool MethodHandleInvokeExact(Thread* self, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 892 | ShadowFrame& shadow_frame, |
| 893 | Handle<mirror::MethodHandle> method_handle, |
| 894 | Handle<mirror::MethodType> callsite_type, |
| 895 | const InstructionOperands* const operands, |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 896 | JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | 3b1b216 | 2021-11-24 08:54:16 +0000 | [diff] [blame] | 897 | return MethodHandleInvokeExactInternal( |
| 898 | self, shadow_frame, method_handle, callsite_type, operands, result); |
Orion Hodson | 8671f58 | 2022-03-09 06:53:48 +0000 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | void MethodHandleInvokeExactWithFrame(Thread* self, |
| 902 | Handle<mirror::MethodHandle> method_handle, |
| 903 | Handle<mirror::EmulatedStackFrame> emulated_frame) |
| 904 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 905 | StackHandleScope<1> hs(self); |
| 906 | Handle<mirror::MethodType> callsite_type = hs.NewHandle(emulated_frame->GetType()); |
| 907 | |
| 908 | // Copy arguments from the EmalatedStackFrame to a ShadowFrame. |
| 909 | const uint16_t num_vregs = callsite_type->NumberOfVRegs(); |
| 910 | |
| 911 | const char* old_cause = self->StartAssertNoThreadSuspension("EmulatedStackFrame to ShadowFrame"); |
| 912 | ArtMethod* invoke_exact = |
| 913 | jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_invokeExact); |
| 914 | ShadowFrameAllocaUniquePtr shadow_frame = |
| 915 | CREATE_SHADOW_FRAME(num_vregs, /*link*/ nullptr, invoke_exact, /*dex_pc*/ 0); |
| 916 | emulated_frame->WriteToShadowFrame(self, callsite_type, 0, shadow_frame.get()); |
| 917 | self->EndAssertNoThreadSuspension(old_cause); |
| 918 | |
| 919 | ManagedStack fragment; |
| 920 | self->PushManagedStackFragment(&fragment); |
| 921 | self->PushShadowFrame(shadow_frame.get()); |
| 922 | |
| 923 | JValue result; |
| 924 | RangeInstructionOperands operands(0, num_vregs); |
| 925 | bool success = MethodHandleInvokeExact(self, |
| 926 | *shadow_frame.get(), |
| 927 | method_handle, |
| 928 | callsite_type, |
| 929 | &operands, |
| 930 | &result); |
| 931 | DCHECK_NE(success, self->IsExceptionPending()); |
| 932 | if (success) { |
| 933 | emulated_frame->SetReturnValue(self, result); |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 934 | } |
Orion Hodson | 8671f58 | 2022-03-09 06:53:48 +0000 | [diff] [blame] | 935 | |
| 936 | self->PopShadowFrame(); |
| 937 | self->PopManagedStackFragment(fragment); |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 938 | } |
| 939 | |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 940 | } // namespace art |