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