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