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