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 | |
| 17 | #include "method_handles.h" |
| 18 | |
| 19 | #include "method_handles-inl.h" |
| 20 | #include "jvalue.h" |
| 21 | #include "jvalue-inl.h" |
| 22 | #include "reflection.h" |
| 23 | #include "reflection-inl.h" |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 24 | #include "well_known_classes.h" |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | namespace { |
| 29 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 30 | #define PRIMITIVES_LIST(V) \ |
| 31 | V(Primitive::kPrimBoolean, Boolean, Boolean, Z) \ |
| 32 | V(Primitive::kPrimByte, Byte, Byte, B) \ |
| 33 | V(Primitive::kPrimChar, Char, Character, C) \ |
| 34 | V(Primitive::kPrimShort, Short, Short, S) \ |
| 35 | V(Primitive::kPrimInt, Int, Integer, I) \ |
| 36 | V(Primitive::kPrimLong, Long, Long, J) \ |
| 37 | V(Primitive::kPrimFloat, Float, Float, F) \ |
| 38 | V(Primitive::kPrimDouble, Double, Double, D) |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 39 | |
| 40 | // Assigns |type| to the primitive type associated with |klass|. Returns |
| 41 | // true iff. |klass| was a boxed type (Integer, Long etc.), false otherwise. |
| 42 | bool GetUnboxedPrimitiveType(ObjPtr<mirror::Class> klass, Primitive::Type* type) |
| 43 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 44 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 45 | #define LOOKUP_PRIMITIVE(primitive, _, __, ___) \ |
| 46 | if (klass->DescriptorEquals(Primitive::BoxedDescriptor(primitive))) { \ |
| 47 | *type = primitive; \ |
| 48 | return true; \ |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 49 | } |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 50 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 51 | PRIMITIVES_LIST(LOOKUP_PRIMITIVE); |
| 52 | #undef LOOKUP_PRIMITIVE |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 53 | return false; |
| 54 | } |
| 55 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 56 | ObjPtr<mirror::Class> GetBoxedPrimitiveClass(Primitive::Type type) |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 57 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 58 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
| 59 | jmethodID m = nullptr; |
| 60 | switch (type) { |
| 61 | #define CASE_PRIMITIVE(primitive, _, java_name, __) \ |
| 62 | case primitive: \ |
| 63 | m = WellKnownClasses::java_lang_ ## java_name ## _valueOf; \ |
| 64 | break; |
| 65 | PRIMITIVES_LIST(CASE_PRIMITIVE); |
| 66 | #undef CASE_PRIMITIVE |
| 67 | case Primitive::Type::kPrimNot: |
| 68 | case Primitive::Type::kPrimVoid: |
| 69 | return nullptr; |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 70 | } |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 71 | return jni::DecodeArtMethod(m)->GetDeclaringClass(); |
| 72 | } |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 73 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 74 | bool GetUnboxedTypeAndValue(ObjPtr<mirror::Object> o, Primitive::Type* type, JValue* value) |
| 75 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 76 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 77 | ObjPtr<mirror::Class> klass = o->GetClass(); |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 78 | ArtField* primitive_field = &klass->GetIFieldsPtr()->At(0); |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 79 | #define CASE_PRIMITIVE(primitive, abbrev, _, shorthand) \ |
| 80 | if (klass == GetBoxedPrimitiveClass(primitive)) { \ |
| 81 | *type = primitive; \ |
| 82 | value->Set ## shorthand(primitive_field->Get ## abbrev(o)); \ |
| 83 | return true; \ |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 84 | } |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 85 | PRIMITIVES_LIST(CASE_PRIMITIVE) |
| 86 | #undef CASE_PRIMITIVE |
| 87 | return false; |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | inline bool IsReferenceType(Primitive::Type type) { |
| 91 | return type == Primitive::kPrimNot; |
| 92 | } |
| 93 | |
| 94 | inline bool IsPrimitiveType(Primitive::Type type) { |
| 95 | return !IsReferenceType(type); |
| 96 | } |
| 97 | |
| 98 | } // namespace |
| 99 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 100 | bool IsParameterTypeConvertible(ObjPtr<mirror::Class> from, ObjPtr<mirror::Class> to) |
| 101 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 102 | // This function returns true if there's any conceivable conversion |
| 103 | // between |from| and |to|. It's expected this method will be used |
| 104 | // to determine if a WrongMethodTypeException should be raised. The |
| 105 | // decision logic follows the documentation for MethodType.asType(). |
| 106 | if (from == to) { |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | Primitive::Type from_primitive = from->GetPrimitiveType(); |
| 111 | Primitive::Type to_primitive = to->GetPrimitiveType(); |
| 112 | DCHECK(from_primitive != Primitive::Type::kPrimVoid); |
| 113 | DCHECK(to_primitive != Primitive::Type::kPrimVoid); |
| 114 | |
| 115 | // If |to| and |from| are references. |
| 116 | if (IsReferenceType(from_primitive) && IsReferenceType(to_primitive)) { |
| 117 | // Assignability is determined during parameter conversion when |
| 118 | // invoking the associated method handle. |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | // If |to| and |from| are primitives and a widening conversion exists. |
| 123 | if (Primitive::IsWidenable(from_primitive, to_primitive)) { |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | // If |to| is a reference and |from| is a primitive, then boxing conversion. |
| 128 | if (IsReferenceType(to_primitive) && IsPrimitiveType(from_primitive)) { |
| 129 | return to->IsAssignableFrom(GetBoxedPrimitiveClass(from_primitive)); |
| 130 | } |
| 131 | |
| 132 | // If |from| is a reference and |to| is a primitive, then unboxing conversion. |
| 133 | if (IsPrimitiveType(to_primitive) && IsReferenceType(from_primitive)) { |
| 134 | if (from->DescriptorEquals("Ljava/lang/Object;")) { |
| 135 | // Object might be converted into a primitive during unboxing. |
| 136 | return true; |
| 137 | } else if (Primitive::IsNumericType(to_primitive) && |
| 138 | from->DescriptorEquals("Ljava/lang/Number;")) { |
| 139 | // Number might be unboxed into any of the number primitive types. |
| 140 | return true; |
| 141 | } |
| 142 | Primitive::Type unboxed_type; |
| 143 | if (GetUnboxedPrimitiveType(from, &unboxed_type)) { |
Orion Hodson | f1412b4 | 2016-11-11 12:03:29 +0000 | [diff] [blame] | 144 | if (unboxed_type == to_primitive) { |
| 145 | // Straightforward unboxing conversion such as Boolean => boolean. |
| 146 | return true; |
| 147 | } else { |
| 148 | // Check if widening operations for numeric primitives would work, |
| 149 | // such as Byte => byte => long. |
| 150 | return Primitive::IsWidenable(unboxed_type, to_primitive); |
| 151 | } |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | bool IsReturnTypeConvertible(ObjPtr<mirror::Class> from, ObjPtr<mirror::Class> to) |
| 159 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 160 | if (to->GetPrimitiveType() == Primitive::Type::kPrimVoid) { |
| 161 | // Result will be ignored. |
| 162 | return true; |
| 163 | } else if (from->GetPrimitiveType() == Primitive::Type::kPrimVoid) { |
| 164 | // Returned value will be 0 / null. |
| 165 | return true; |
| 166 | } else { |
| 167 | // Otherwise apply usual parameter conversion rules. |
| 168 | return IsParameterTypeConvertible(from, to); |
| 169 | } |
| 170 | } |
| 171 | |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 172 | bool ConvertJValueCommon( |
| 173 | Handle<mirror::MethodType> callsite_type, |
| 174 | Handle<mirror::MethodType> callee_type, |
| 175 | ObjPtr<mirror::Class> from, |
| 176 | ObjPtr<mirror::Class> to, |
| 177 | JValue* value) { |
| 178 | // The reader maybe concerned about the safety of the heap object |
| 179 | // that may be in |value|. There is only one case where allocation |
| 180 | // is obviously needed and that's for boxing. However, in the case |
| 181 | // of boxing |value| contains a non-reference type. |
| 182 | |
| 183 | const Primitive::Type from_type = from->GetPrimitiveType(); |
| 184 | const Primitive::Type to_type = to->GetPrimitiveType(); |
| 185 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 186 | // Put incoming value into |src_value| and set return value to 0. |
| 187 | // Errors and conversions from void require the return value to be 0. |
| 188 | const JValue src_value(*value); |
| 189 | value->SetJ(0); |
| 190 | |
| 191 | // Conversion from void set result to zero. |
| 192 | if (from_type == Primitive::kPrimVoid) { |
| 193 | return true; |
| 194 | } |
| 195 | |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 196 | // This method must be called only when the types don't match. |
| 197 | DCHECK(from != to); |
| 198 | |
| 199 | if (IsPrimitiveType(from_type) && IsPrimitiveType(to_type)) { |
| 200 | // The source and target types are both primitives. |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 201 | if (UNLIKELY(!ConvertPrimitiveValueNoThrow(from_type, to_type, src_value, value))) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 202 | ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get()); |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 203 | return false; |
| 204 | } |
| 205 | return true; |
| 206 | } else if (IsReferenceType(from_type) && IsReferenceType(to_type)) { |
| 207 | // They're both reference types. If "from" is null, we can pass it |
| 208 | // through unchanged. If not, we must generate a cast exception if |
| 209 | // |to| is not assignable from the dynamic type of |ref|. |
| 210 | // |
| 211 | // Playing it safe with StackHandleScope here, not expecting any allocation |
| 212 | // in mirror::Class::IsAssignable(). |
| 213 | StackHandleScope<2> hs(Thread::Current()); |
| 214 | Handle<mirror::Class> h_to(hs.NewHandle(to)); |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 215 | Handle<mirror::Object> h_obj(hs.NewHandle(src_value.GetL())); |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 216 | if (h_obj.Get() != nullptr && !to->IsAssignableFrom(h_obj->GetClass())) { |
| 217 | ThrowClassCastException(h_to.Get(), h_obj->GetClass()); |
| 218 | return false; |
| 219 | } |
| 220 | value->SetL(h_obj.Get()); |
| 221 | return true; |
| 222 | } else if (IsReferenceType(to_type)) { |
| 223 | DCHECK(IsPrimitiveType(from_type)); |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 224 | // The source type is a primitive and the target type is a reference, so we must box. |
| 225 | // The target type maybe a super class of the boxed source type, for example, |
| 226 | // if the source type is int, it's boxed type is java.lang.Integer, and the target |
| 227 | // type could be java.lang.Number. |
| 228 | Primitive::Type type; |
| 229 | if (!GetUnboxedPrimitiveType(to, &type)) { |
| 230 | ObjPtr<mirror::Class> boxed_from_class = GetBoxedPrimitiveClass(from_type); |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 231 | if (boxed_from_class->IsSubClass(to)) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 232 | type = from_type; |
| 233 | } else { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 234 | ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get()); |
| 235 | return false; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | if (UNLIKELY(from_type != type)) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 240 | ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get()); |
| 241 | return false; |
| 242 | } |
| 243 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 244 | if (!ConvertPrimitiveValueNoThrow(from_type, type, src_value, value)) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 245 | ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get()); |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | // Then perform the actual boxing, and then set the reference. |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 250 | ObjPtr<mirror::Object> boxed = BoxPrimitive(type, src_value); |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 251 | value->SetL(boxed.Ptr()); |
| 252 | return true; |
| 253 | } else { |
| 254 | // The source type is a reference and the target type is a primitive, so we must unbox. |
| 255 | DCHECK(IsReferenceType(from_type)); |
| 256 | DCHECK(IsPrimitiveType(to_type)); |
| 257 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 258 | ObjPtr<mirror::Object> from_obj(src_value.GetL()); |
| 259 | if (UNLIKELY(from_obj == nullptr)) { |
| 260 | ThrowNullPointerException( |
| 261 | StringPrintf("Expected to unbox a '%s' primitive type but was returned null", |
| 262 | from->PrettyDescriptor().c_str()).c_str()); |
| 263 | return false; |
| 264 | } |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 265 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 266 | Primitive::Type unboxed_type; |
| 267 | JValue unboxed_value; |
| 268 | if (UNLIKELY(!GetUnboxedTypeAndValue(from_obj, &unboxed_type, &unboxed_value))) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 269 | ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get()); |
| 270 | return false; |
| 271 | } |
| 272 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 273 | if (UNLIKELY(!ConvertPrimitiveValueNoThrow(unboxed_type, to_type, unboxed_value, value))) { |
| 274 | ThrowClassCastException(from, to); |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 275 | return false; |
| 276 | } |
| 277 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 278 | return true; |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | } // namespace art |