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