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