Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 "reflection.h" |
| 18 | |
| 19 | #include "class_linker.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 20 | #include "common_throws.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 21 | #include "dex_file-inl.h" |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 22 | #include "jni_internal.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 23 | #include "mirror/art_field-inl.h" |
| 24 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 25 | #include "mirror/class.h" |
| 26 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | #include "mirror/object_array.h" |
| 28 | #include "mirror/object_array-inl.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 29 | #include "object_utils.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 30 | #include "scoped_thread_state_change.h" |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 31 | #include "stack.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 32 | #include "well_known_classes.h" |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 33 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 34 | namespace art { |
| 35 | |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 36 | class ArgArray { |
| 37 | public: |
| 38 | explicit ArgArray(const char* shorty, uint32_t shorty_len) |
| 39 | : shorty_(shorty), shorty_len_(shorty_len), num_bytes_(0) { |
| 40 | size_t num_slots = shorty_len + 1; // +1 in case of receiver. |
| 41 | if (LIKELY((num_slots * 2) < kSmallArgArraySize)) { |
| 42 | // We can trivially use the small arg array. |
| 43 | arg_array_ = small_arg_array_; |
| 44 | } else { |
| 45 | // Analyze shorty to see if we need the large arg array. |
| 46 | for (size_t i = 1; i < shorty_len; ++i) { |
| 47 | char c = shorty[i]; |
| 48 | if (c == 'J' || c == 'D') { |
| 49 | num_slots++; |
| 50 | } |
| 51 | } |
| 52 | if (num_slots <= kSmallArgArraySize) { |
| 53 | arg_array_ = small_arg_array_; |
| 54 | } else { |
| 55 | large_arg_array_.reset(new uint32_t[num_slots]); |
| 56 | arg_array_ = large_arg_array_.get(); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | uint32_t* GetArray() { |
| 62 | return arg_array_; |
| 63 | } |
| 64 | |
| 65 | uint32_t GetNumBytes() { |
| 66 | return num_bytes_; |
| 67 | } |
| 68 | |
| 69 | void Append(uint32_t value) { |
| 70 | arg_array_[num_bytes_ / 4] = value; |
| 71 | num_bytes_ += 4; |
| 72 | } |
| 73 | |
| 74 | void Append(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 75 | Append(StackReference<mirror::Object>::FromMirrorPtr(obj).AsVRegValue()); |
| 76 | } |
| 77 | |
| 78 | void AppendWide(uint64_t value) { |
| 79 | // For ARM and MIPS portable, align wide values to 8 bytes (ArgArray starts at offset of 4). |
| 80 | #if defined(ART_USE_PORTABLE_COMPILER) && (defined(__arm__) || defined(__mips__)) |
| 81 | if (num_bytes_ % 8 == 0) { |
| 82 | num_bytes_ += 4; |
| 83 | } |
| 84 | #endif |
| 85 | arg_array_[num_bytes_ / 4] = value; |
| 86 | arg_array_[(num_bytes_ / 4) + 1] = value >> 32; |
| 87 | num_bytes_ += 8; |
| 88 | } |
| 89 | |
| 90 | void AppendFloat(float value) { |
| 91 | jvalue jv; |
| 92 | jv.f = value; |
| 93 | Append(jv.i); |
| 94 | } |
| 95 | |
| 96 | void AppendDouble(double value) { |
| 97 | jvalue jv; |
| 98 | jv.d = value; |
| 99 | AppendWide(jv.j); |
| 100 | } |
| 101 | |
Ian Rogers | e18fdd2 | 2014-03-14 13:29:43 -0700 | [diff] [blame^] | 102 | void BuildArgArrayFromVarArgs(const ScopedObjectAccess& soa, mirror::Object* receiver, va_list ap) |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 103 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 104 | // Set receiver if non-null (method is not static) |
| 105 | if (receiver != nullptr) { |
| 106 | Append(receiver); |
| 107 | } |
| 108 | for (size_t i = 1; i < shorty_len_; ++i) { |
| 109 | switch (shorty_[i]) { |
| 110 | case 'Z': |
| 111 | case 'B': |
| 112 | case 'C': |
| 113 | case 'S': |
| 114 | case 'I': |
| 115 | Append(va_arg(ap, jint)); |
| 116 | break; |
| 117 | case 'F': |
| 118 | AppendFloat(va_arg(ap, jdouble)); |
| 119 | break; |
| 120 | case 'L': |
| 121 | Append(soa.Decode<mirror::Object*>(va_arg(ap, jobject))); |
| 122 | break; |
| 123 | case 'D': |
| 124 | AppendDouble(va_arg(ap, jdouble)); |
| 125 | break; |
| 126 | case 'J': |
| 127 | AppendWide(va_arg(ap, jlong)); |
| 128 | break; |
| 129 | #ifndef NDEBUG |
| 130 | default: |
| 131 | LOG(FATAL) << "Unexpected shorty character: " << shorty_[i]; |
| 132 | #endif |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
Ian Rogers | e18fdd2 | 2014-03-14 13:29:43 -0700 | [diff] [blame^] | 137 | void BuildArgArrayFromJValues(const ScopedObjectAccessUnchecked& soa, mirror::Object* receiver, |
| 138 | jvalue* args) |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 139 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 140 | // Set receiver if non-null (method is not static) |
| 141 | if (receiver != nullptr) { |
| 142 | Append(receiver); |
| 143 | } |
| 144 | for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) { |
| 145 | switch (shorty_[i]) { |
| 146 | case 'Z': |
| 147 | Append(args[args_offset].z); |
| 148 | break; |
| 149 | case 'B': |
| 150 | Append(args[args_offset].b); |
| 151 | break; |
| 152 | case 'C': |
| 153 | Append(args[args_offset].c); |
| 154 | break; |
| 155 | case 'S': |
| 156 | Append(args[args_offset].s); |
| 157 | break; |
| 158 | case 'I': |
| 159 | case 'F': |
| 160 | Append(args[args_offset].i); |
| 161 | break; |
| 162 | case 'L': |
| 163 | Append(soa.Decode<mirror::Object*>(args[args_offset].l)); |
| 164 | break; |
| 165 | case 'D': |
| 166 | case 'J': |
| 167 | AppendWide(args[args_offset].j); |
| 168 | break; |
| 169 | #ifndef NDEBUG |
| 170 | default: |
| 171 | LOG(FATAL) << "Unexpected shorty character: " << shorty_[i]; |
| 172 | #endif |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | void BuildArgArrayFromFrame(ShadowFrame* shadow_frame, uint32_t arg_offset) |
| 178 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 179 | // Set receiver if non-null (method is not static) |
| 180 | size_t cur_arg = arg_offset; |
| 181 | if (!shadow_frame->GetMethod()->IsStatic()) { |
| 182 | Append(shadow_frame->GetVReg(cur_arg)); |
| 183 | cur_arg++; |
| 184 | } |
| 185 | for (size_t i = 1; i < shorty_len_; ++i) { |
| 186 | switch (shorty_[i]) { |
| 187 | case 'Z': |
| 188 | case 'B': |
| 189 | case 'C': |
| 190 | case 'S': |
| 191 | case 'I': |
| 192 | case 'F': |
| 193 | case 'L': |
| 194 | Append(shadow_frame->GetVReg(cur_arg)); |
| 195 | cur_arg++; |
| 196 | break; |
| 197 | case 'D': |
| 198 | case 'J': |
| 199 | AppendWide(shadow_frame->GetVRegLong(cur_arg)); |
| 200 | cur_arg++; |
| 201 | cur_arg++; |
| 202 | break; |
| 203 | #ifndef NDEBUG |
| 204 | default: |
| 205 | LOG(FATAL) << "Unexpected shorty character: " << shorty_[i]; |
| 206 | #endif |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | static void ThrowIllegalPrimitiveArgumentException(const char* expected, |
| 212 | const StringPiece& found_descriptor) |
| 213 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 214 | ThrowIllegalArgumentException(nullptr, |
| 215 | StringPrintf("Invalid primitive conversion from %s to %s", expected, |
| 216 | PrettyDescriptor(found_descriptor.as_string()).c_str()).c_str()); |
| 217 | } |
| 218 | |
Ian Rogers | e18fdd2 | 2014-03-14 13:29:43 -0700 | [diff] [blame^] | 219 | bool BuildArgArrayFromObjectArray(const ScopedObjectAccess& soa, mirror::Object* receiver, |
| 220 | mirror::ObjectArray<mirror::Object>* args, MethodHelper& mh) |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 221 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 222 | const DexFile::TypeList* classes = mh.GetParameterTypeList(); |
| 223 | // Set receiver if non-null (method is not static) |
| 224 | if (receiver != nullptr) { |
| 225 | Append(receiver); |
| 226 | } |
| 227 | for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) { |
| 228 | mirror::Object* arg = args->Get(args_offset); |
| 229 | if (((shorty_[i] == 'L') && (arg != nullptr)) || ((arg == nullptr && shorty_[i] != 'L'))) { |
| 230 | mirror::Class* dst_class = |
| 231 | mh.GetClassFromTypeIdx(classes->GetTypeItem(args_offset).type_idx_); |
| 232 | if (UNLIKELY(arg == nullptr || !arg->InstanceOf(dst_class))) { |
| 233 | ThrowIllegalArgumentException(nullptr, |
Ian Rogers | 11e4c03 | 2014-03-14 12:00:39 -0700 | [diff] [blame] | 234 | StringPrintf("method %s argument %zd has type %s, got %s", |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 235 | PrettyMethod(mh.GetMethod(), false).c_str(), |
| 236 | args_offset + 1, // Humans don't count from 0. |
| 237 | PrettyDescriptor(dst_class).c_str(), |
| 238 | PrettyTypeOf(arg).c_str()).c_str()); |
| 239 | return false; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | #define DO_FIRST_ARG(match_descriptor, get_fn, append) { \ |
| 244 | const StringPiece src_descriptor(arg != nullptr \ |
| 245 | ? ClassHelper(arg->GetClass<>()).GetDescriptor() \ |
| 246 | : "null"); \ |
| 247 | if (LIKELY(src_descriptor == match_descriptor)) { \ |
| 248 | mirror::ArtField* primitive_field = arg->GetClass()->GetIFields()->Get(0); \ |
| 249 | append(primitive_field-> get_fn(arg)); |
| 250 | |
| 251 | #define DO_ARG(match_descriptor, get_fn, append) \ |
| 252 | } else if (LIKELY(src_descriptor == match_descriptor)) { \ |
| 253 | mirror::ArtField* primitive_field = arg->GetClass()->GetIFields()->Get(0); \ |
| 254 | append(primitive_field-> get_fn(arg)); |
| 255 | |
| 256 | #define DO_FAIL(expected) \ |
| 257 | } else { \ |
| 258 | if (arg->GetClass<>()->IsPrimitive()) { \ |
| 259 | ThrowIllegalPrimitiveArgumentException(expected, src_descriptor); \ |
| 260 | } else { \ |
| 261 | ThrowIllegalArgumentException(nullptr, \ |
Ian Rogers | 11e4c03 | 2014-03-14 12:00:39 -0700 | [diff] [blame] | 262 | StringPrintf("method %s argument %zd has type %s, got %s", \ |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 263 | PrettyMethod(mh.GetMethod(), false).c_str(), \ |
| 264 | args_offset + 1, \ |
| 265 | expected, \ |
| 266 | PrettyTypeOf(arg).c_str()).c_str()); \ |
| 267 | } \ |
| 268 | return false; \ |
| 269 | } } |
| 270 | |
| 271 | switch (shorty_[i]) { |
| 272 | case 'L': |
| 273 | Append(arg); |
| 274 | break; |
| 275 | case 'Z': |
| 276 | DO_FIRST_ARG("Ljava/lang/Boolean;", GetBoolean, Append) |
| 277 | DO_FAIL("boolean") |
| 278 | break; |
| 279 | case 'B': |
| 280 | DO_FIRST_ARG("Ljava/lang/Byte;", GetByte, Append) |
| 281 | DO_FAIL("byte") |
| 282 | break; |
| 283 | case 'C': |
| 284 | DO_FIRST_ARG("Ljava/lang/Character;", GetChar, Append) |
| 285 | DO_FAIL("char") |
| 286 | break; |
| 287 | case 'S': |
| 288 | DO_FIRST_ARG("Ljava/lang/Short;", GetShort, Append) |
| 289 | DO_ARG("Ljava/lang/Byte;", GetByte, Append) |
| 290 | DO_FAIL("short") |
| 291 | break; |
| 292 | case 'I': |
| 293 | DO_FIRST_ARG("Ljava/lang/Integer;", GetInt, Append) |
| 294 | DO_ARG("Ljava/lang/Character;", GetChar, Append) |
| 295 | DO_ARG("Ljava/lang/Short;", GetShort, Append) |
| 296 | DO_ARG("Ljava/lang/Byte;", GetByte, Append) |
| 297 | DO_FAIL("int") |
| 298 | break; |
| 299 | case 'J': |
| 300 | DO_FIRST_ARG("Ljava/lang/Long;", GetLong, AppendWide) |
| 301 | DO_ARG("Ljava/lang/Integer;", GetInt, AppendWide) |
| 302 | DO_ARG("Ljava/lang/Character;", GetChar, AppendWide) |
| 303 | DO_ARG("Ljava/lang/Short;", GetShort, AppendWide) |
| 304 | DO_ARG("Ljava/lang/Byte;", GetByte, AppendWide) |
| 305 | DO_FAIL("long") |
| 306 | break; |
| 307 | case 'F': |
| 308 | DO_FIRST_ARG("Ljava/lang/Float;", GetFloat, AppendFloat) |
| 309 | DO_ARG("Ljava/lang/Long;", GetLong, AppendFloat) |
| 310 | DO_ARG("Ljava/lang/Integer;", GetInt, AppendFloat) |
| 311 | DO_ARG("Ljava/lang/Character;", GetChar, AppendFloat) |
| 312 | DO_ARG("Ljava/lang/Short;", GetShort, AppendFloat) |
| 313 | DO_ARG("Ljava/lang/Byte;", GetByte, AppendFloat) |
| 314 | DO_FAIL("float") |
| 315 | break; |
| 316 | case 'D': |
| 317 | DO_FIRST_ARG("Ljava/lang/Double;", GetDouble, AppendDouble) |
| 318 | DO_ARG("Ljava/lang/Float;", GetFloat, AppendDouble) |
| 319 | DO_ARG("Ljava/lang/Long;", GetLong, AppendDouble) |
| 320 | DO_ARG("Ljava/lang/Integer;", GetInt, AppendDouble) |
| 321 | DO_ARG("Ljava/lang/Character;", GetChar, AppendDouble) |
| 322 | DO_ARG("Ljava/lang/Short;", GetShort, AppendDouble) |
| 323 | DO_ARG("Ljava/lang/Byte;", GetByte, AppendDouble) |
| 324 | DO_FAIL("double") |
| 325 | break; |
| 326 | #ifndef NDEBUG |
| 327 | default: |
| 328 | LOG(FATAL) << "Unexpected shorty character: " << shorty_[i]; |
| 329 | #endif |
| 330 | } |
| 331 | #undef DO_FIRST_ARG |
| 332 | #undef DO_ARG |
| 333 | #undef DO_FAIL |
| 334 | } |
| 335 | return true; |
| 336 | } |
| 337 | |
| 338 | private: |
| 339 | enum { kSmallArgArraySize = 16 }; |
| 340 | const char* const shorty_; |
| 341 | const uint32_t shorty_len_; |
| 342 | uint32_t num_bytes_; |
| 343 | uint32_t* arg_array_; |
| 344 | uint32_t small_arg_array_[kSmallArgArraySize]; |
| 345 | UniquePtr<uint32_t[]> large_arg_array_; |
| 346 | }; |
| 347 | |
| 348 | static void CheckMethodArguments(mirror::ArtMethod* m, uint32_t* args) |
| 349 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 350 | const DexFile::TypeList* params = MethodHelper(m).GetParameterTypeList(); |
| 351 | if (params == nullptr) { |
| 352 | return; // No arguments so nothing to check. |
| 353 | } |
| 354 | uint32_t offset = 0; |
| 355 | uint32_t num_params = params->Size(); |
| 356 | size_t error_count = 0; |
| 357 | if (!m->IsStatic()) { |
| 358 | offset = 1; |
| 359 | } |
| 360 | for (uint32_t i = 0; i < num_params; i++) { |
| 361 | uint16_t type_idx = params->GetTypeItem(i).type_idx_; |
| 362 | mirror::Class* param_type = MethodHelper(m).GetClassFromTypeIdx(type_idx); |
| 363 | if (param_type == nullptr) { |
| 364 | Thread* self = Thread::Current(); |
| 365 | CHECK(self->IsExceptionPending()); |
| 366 | LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: " |
| 367 | << MethodHelper(m).GetTypeDescriptorFromTypeIdx(type_idx) << "\n" |
| 368 | << self->GetException(nullptr)->Dump(); |
| 369 | self->ClearException(); |
| 370 | ++error_count; |
| 371 | } else if (!param_type->IsPrimitive()) { |
| 372 | // TODO: check primitives are in range. |
| 373 | mirror::Object* argument = reinterpret_cast<mirror::Object*>(args[i + offset]); |
| 374 | if (argument != nullptr && !argument->InstanceOf(param_type)) { |
| 375 | LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of " |
| 376 | << PrettyTypeOf(argument) << " as argument " << (i + 1) |
| 377 | << " to " << PrettyMethod(m); |
| 378 | ++error_count; |
| 379 | } |
| 380 | } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) { |
| 381 | offset++; |
| 382 | } |
| 383 | } |
| 384 | if (error_count > 0) { |
| 385 | // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort |
| 386 | // with an argument. |
| 387 | JniAbortF(nullptr, "bad arguments passed to %s (see above for details)", |
| 388 | PrettyMethod(m).c_str()); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | static mirror::ArtMethod* FindVirtualMethod(mirror::Object* receiver, |
| 393 | mirror::ArtMethod* method) |
| 394 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 395 | return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method); |
| 396 | } |
| 397 | |
| 398 | |
| 399 | static void InvokeWithArgArray(const ScopedObjectAccessUnchecked& soa, mirror::ArtMethod* method, |
| 400 | ArgArray* arg_array, JValue* result, const char* shorty) |
| 401 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 402 | uint32_t* args = arg_array->GetArray(); |
| 403 | if (UNLIKELY(soa.Env()->check_jni)) { |
| 404 | CheckMethodArguments(method, args); |
| 405 | } |
| 406 | method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, shorty); |
| 407 | } |
| 408 | |
| 409 | JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj, jmethodID mid, va_list args) |
| 410 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 411 | mirror::ArtMethod* method = soa.DecodeMethod(mid); |
| 412 | mirror::Object* receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object*>(obj); |
| 413 | MethodHelper mh(method); |
| 414 | JValue result; |
| 415 | ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength()); |
Ian Rogers | e18fdd2 | 2014-03-14 13:29:43 -0700 | [diff] [blame^] | 416 | arg_array.BuildArgArrayFromVarArgs(soa, receiver, args); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 417 | InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()); |
| 418 | return result; |
| 419 | } |
| 420 | |
| 421 | JValue InvokeWithJValues(const ScopedObjectAccessUnchecked& soa, mirror::Object* receiver, |
| 422 | jmethodID mid, jvalue* args) { |
| 423 | mirror::ArtMethod* method = soa.DecodeMethod(mid); |
| 424 | MethodHelper mh(method); |
| 425 | JValue result; |
| 426 | ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength()); |
Ian Rogers | e18fdd2 | 2014-03-14 13:29:43 -0700 | [diff] [blame^] | 427 | arg_array.BuildArgArrayFromJValues(soa, receiver, args); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 428 | InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()); |
| 429 | return result; |
| 430 | } |
| 431 | |
| 432 | JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa, |
| 433 | mirror::Object* receiver, jmethodID mid, jvalue* args) { |
| 434 | mirror::ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid)); |
| 435 | MethodHelper mh(method); |
| 436 | JValue result; |
| 437 | ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength()); |
Ian Rogers | e18fdd2 | 2014-03-14 13:29:43 -0700 | [diff] [blame^] | 438 | arg_array.BuildArgArrayFromJValues(soa, receiver, args); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 439 | InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()); |
| 440 | return result; |
| 441 | } |
| 442 | |
| 443 | JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa, |
| 444 | jobject obj, jmethodID mid, va_list args) { |
| 445 | mirror::Object* receiver = soa.Decode<mirror::Object*>(obj); |
| 446 | mirror::ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid)); |
| 447 | MethodHelper mh(method); |
| 448 | JValue result; |
| 449 | ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength()); |
Ian Rogers | e18fdd2 | 2014-03-14 13:29:43 -0700 | [diff] [blame^] | 450 | arg_array.BuildArgArrayFromVarArgs(soa, receiver, args); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 451 | InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()); |
| 452 | return result; |
| 453 | } |
| 454 | |
| 455 | void InvokeWithShadowFrame(Thread* self, ShadowFrame* shadow_frame, uint16_t arg_offset, |
| 456 | MethodHelper& mh, JValue* result) { |
| 457 | ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength()); |
| 458 | arg_array.BuildArgArrayFromFrame(shadow_frame, arg_offset); |
| 459 | shadow_frame->GetMethod()->Invoke(self, arg_array.GetArray(), arg_array.GetNumBytes(), result, |
| 460 | mh.GetShorty()); |
| 461 | } |
| 462 | |
| 463 | jobject InvokeMethod(const ScopedObjectAccess& soa, jobject javaMethod, |
| 464 | jobject javaReceiver, jobject javaArgs) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 465 | jmethodID mid = soa.Env()->FromReflectedMethod(javaMethod); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 466 | mirror::ArtMethod* m = soa.DecodeMethod(mid); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 467 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 468 | mirror::Class* declaring_class = m->GetDeclaringClass(); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 469 | if (UNLIKELY(!declaring_class->IsInitialized())) { |
| 470 | SirtRef<mirror::Class> sirt_c(soa.Self(), declaring_class); |
| 471 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(sirt_c, true, true)) { |
| 472 | return nullptr; |
| 473 | } |
| 474 | declaring_class = sirt_c.get(); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 477 | mirror::Object* receiver = nullptr; |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 478 | if (!m->IsStatic()) { |
| 479 | // Check that the receiver is non-null and an instance of the field's declaring class. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 480 | receiver = soa.Decode<mirror::Object*>(javaReceiver); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 481 | if (!VerifyObjectIsClass(receiver, declaring_class)) { |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 482 | return NULL; |
| 483 | } |
| 484 | |
| 485 | // Find the actual implementation of the virtual method. |
| 486 | m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m); |
| 487 | } |
| 488 | |
| 489 | // Get our arrays of arguments and their types, and check they're the same size. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 490 | mirror::ObjectArray<mirror::Object>* objects = |
| 491 | soa.Decode<mirror::ObjectArray<mirror::Object>*>(javaArgs); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 492 | MethodHelper mh(m); |
| 493 | const DexFile::TypeList* classes = mh.GetParameterTypeList(); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 494 | uint32_t classes_size = (classes == nullptr) ? 0 : classes->Size(); |
| 495 | uint32_t arg_count = (objects != nullptr) ? objects->GetLength() : 0; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 496 | if (arg_count != classes_size) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 497 | ThrowIllegalArgumentException(NULL, |
| 498 | StringPrintf("Wrong number of arguments; expected %d, got %d", |
| 499 | classes_size, arg_count).c_str()); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 500 | return NULL; |
| 501 | } |
| 502 | |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 503 | // Invoke the method. |
| 504 | JValue result; |
| 505 | ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength()); |
Ian Rogers | e18fdd2 | 2014-03-14 13:29:43 -0700 | [diff] [blame^] | 506 | if (!arg_array.BuildArgArrayFromObjectArray(soa, receiver, objects, mh)) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 507 | CHECK(soa.Self()->IsExceptionPending()); |
| 508 | return nullptr; |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 511 | InvokeWithArgArray(soa, m, &arg_array, &result, mh.GetShorty()); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 512 | |
| 513 | // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 514 | if (soa.Self()->IsExceptionPending()) { |
| 515 | jthrowable th = soa.Env()->ExceptionOccurred(); |
| 516 | soa.Env()->ExceptionClear(); |
| 517 | jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException"); |
| 518 | jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V"); |
| 519 | jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th); |
| 520 | soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance)); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 521 | return NULL; |
| 522 | } |
| 523 | |
| 524 | // Box if necessary and return. |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 525 | return soa.AddLocalReference<jobject>(BoxPrimitive(mh.GetReturnType()->GetPrimitiveType(), |
| 526 | result)); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 529 | bool VerifyObjectIsClass(mirror::Object* o, mirror::Class* c) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 530 | if (o == NULL) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 531 | ThrowNullPointerException(NULL, "null receiver"); |
| 532 | return false; |
Elliott Hughes | b600b3f | 2012-03-14 13:57:24 -0700 | [diff] [blame] | 533 | } else if (!o->InstanceOf(c)) { |
Elliott Hughes | b600b3f | 2012-03-14 13:57:24 -0700 | [diff] [blame] | 534 | std::string expected_class_name(PrettyDescriptor(c)); |
| 535 | std::string actual_class_name(PrettyTypeOf(o)); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 536 | ThrowIllegalArgumentException(NULL, |
| 537 | StringPrintf("Expected receiver of type %s, but got %s", |
| 538 | expected_class_name.c_str(), |
| 539 | actual_class_name.c_str()).c_str()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 540 | return false; |
| 541 | } |
| 542 | return true; |
| 543 | } |
| 544 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 545 | bool ConvertPrimitiveValue(const ThrowLocation* throw_location, bool unbox_for_result, |
| 546 | Primitive::Type srcType, Primitive::Type dstType, |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 547 | const JValue& src, JValue& dst) { |
Jesse Wilson | c129a6b | 2011-11-24 14:47:46 -0500 | [diff] [blame] | 548 | CHECK(srcType != Primitive::kPrimNot && dstType != Primitive::kPrimNot); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 549 | switch (dstType) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 550 | case Primitive::kPrimBoolean: |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 551 | if (srcType == Primitive::kPrimBoolean) { |
| 552 | dst.SetZ(src.GetZ()); |
| 553 | return true; |
| 554 | } |
| 555 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 556 | case Primitive::kPrimChar: |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 557 | if (srcType == Primitive::kPrimChar) { |
| 558 | dst.SetC(src.GetC()); |
| 559 | return true; |
| 560 | } |
| 561 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 562 | case Primitive::kPrimByte: |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 563 | if (srcType == Primitive::kPrimByte) { |
| 564 | dst.SetB(src.GetB()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 565 | return true; |
| 566 | } |
| 567 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 568 | case Primitive::kPrimShort: |
| 569 | if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimShort) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 570 | dst.SetS(src.GetI()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 571 | return true; |
| 572 | } |
| 573 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 574 | case Primitive::kPrimInt: |
| 575 | if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar || |
| 576 | srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 577 | dst.SetI(src.GetI()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 578 | return true; |
| 579 | } |
| 580 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 581 | case Primitive::kPrimLong: |
| 582 | if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar || |
| 583 | srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 584 | dst.SetJ(src.GetI()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 585 | return true; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 586 | } else if (srcType == Primitive::kPrimLong) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 587 | dst.SetJ(src.GetJ()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 588 | return true; |
| 589 | } |
| 590 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 591 | case Primitive::kPrimFloat: |
| 592 | if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar || |
| 593 | srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 594 | dst.SetF(src.GetI()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 595 | return true; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 596 | } else if (srcType == Primitive::kPrimLong) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 597 | dst.SetF(src.GetJ()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 598 | return true; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 599 | } else if (srcType == Primitive::kPrimFloat) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 600 | dst.SetF(src.GetF()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 601 | return true; |
| 602 | } |
| 603 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 604 | case Primitive::kPrimDouble: |
| 605 | if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar || |
| 606 | srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 607 | dst.SetD(src.GetI()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 608 | return true; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 609 | } else if (srcType == Primitive::kPrimLong) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 610 | dst.SetD(src.GetJ()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 611 | return true; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 612 | } else if (srcType == Primitive::kPrimFloat) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 613 | dst.SetD(src.GetF()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 614 | return true; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 615 | } else if (srcType == Primitive::kPrimDouble) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 616 | dst.SetJ(src.GetJ()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 617 | return true; |
| 618 | } |
| 619 | break; |
| 620 | default: |
| 621 | break; |
| 622 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 623 | if (!unbox_for_result) { |
| 624 | ThrowIllegalArgumentException(throw_location, |
| 625 | StringPrintf("Invalid primitive conversion from %s to %s", |
| 626 | PrettyDescriptor(srcType).c_str(), |
| 627 | PrettyDescriptor(dstType).c_str()).c_str()); |
| 628 | } else { |
| 629 | ThrowClassCastException(throw_location, |
| 630 | StringPrintf("Couldn't convert result of type %s to %s", |
| 631 | PrettyDescriptor(srcType).c_str(), |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 632 | PrettyDescriptor(dstType).c_str()).c_str()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 633 | } |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 634 | return false; |
| 635 | } |
| 636 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 637 | mirror::Object* BoxPrimitive(Primitive::Type src_class, const JValue& value) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 638 | if (src_class == Primitive::kPrimNot) { |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 639 | return value.GetL(); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 640 | } |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 641 | if (src_class == Primitive::kPrimVoid) { |
| 642 | // There's no such thing as a void field, and void methods invoked via reflection return null. |
| 643 | return nullptr; |
| 644 | } |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 645 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 646 | jmethodID m = NULL; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 647 | const char* shorty; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 648 | switch (src_class) { |
| 649 | case Primitive::kPrimBoolean: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 650 | m = WellKnownClasses::java_lang_Boolean_valueOf; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 651 | shorty = "LZ"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 652 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 653 | case Primitive::kPrimByte: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 654 | m = WellKnownClasses::java_lang_Byte_valueOf; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 655 | shorty = "LB"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 656 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 657 | case Primitive::kPrimChar: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 658 | m = WellKnownClasses::java_lang_Character_valueOf; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 659 | shorty = "LC"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 660 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 661 | case Primitive::kPrimDouble: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 662 | m = WellKnownClasses::java_lang_Double_valueOf; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 663 | shorty = "LD"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 664 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 665 | case Primitive::kPrimFloat: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 666 | m = WellKnownClasses::java_lang_Float_valueOf; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 667 | shorty = "LF"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 668 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 669 | case Primitive::kPrimInt: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 670 | m = WellKnownClasses::java_lang_Integer_valueOf; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 671 | shorty = "LI"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 672 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 673 | case Primitive::kPrimLong: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 674 | m = WellKnownClasses::java_lang_Long_valueOf; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 675 | shorty = "LJ"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 676 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 677 | case Primitive::kPrimShort: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 678 | m = WellKnownClasses::java_lang_Short_valueOf; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 679 | shorty = "LS"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 680 | break; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 681 | default: |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 682 | LOG(FATAL) << static_cast<int>(src_class); |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 683 | shorty = nullptr; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 686 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 687 | DCHECK_EQ(soa.Self()->GetState(), kRunnable); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 688 | |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 689 | ArgArray arg_array(shorty, 2); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 690 | JValue result; |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 691 | if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) { |
| 692 | arg_array.AppendWide(value.GetJ()); |
| 693 | } else { |
| 694 | arg_array.Append(value.GetI()); |
| 695 | } |
| 696 | |
| 697 | soa.DecodeMethod(m)->Invoke(soa.Self(), arg_array.GetArray(), arg_array.GetNumBytes(), |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 698 | &result, shorty); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 699 | return result.GetL(); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 700 | } |
| 701 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 702 | static std::string UnboxingFailureKind(mirror::ArtMethod* m, int index, mirror::ArtField* f) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 703 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 84a5bb4 | 2012-05-16 17:52:15 -0700 | [diff] [blame] | 704 | if (m != NULL && index != -1) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 705 | ++index; // Humans count from 1. |
Elliott Hughes | 84a5bb4 | 2012-05-16 17:52:15 -0700 | [diff] [blame] | 706 | return StringPrintf("method %s argument %d", PrettyMethod(m, false).c_str(), index); |
Elliott Hughes | aaa5edc | 2012-05-16 15:54:30 -0700 | [diff] [blame] | 707 | } |
| 708 | if (f != NULL) { |
| 709 | return "field " + PrettyField(f, false); |
| 710 | } |
| 711 | return "result"; |
| 712 | } |
| 713 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 714 | static bool UnboxPrimitive(const ThrowLocation* throw_location, mirror::Object* o, |
| 715 | mirror::Class* dst_class, JValue& unboxed_value, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 716 | mirror::ArtMethod* m, int index, mirror::ArtField* f) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 717 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 718 | bool unbox_for_result = (f == NULL) && (index == -1); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 719 | if (!dst_class->IsPrimitive()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 720 | if (UNLIKELY(o != NULL && !o->InstanceOf(dst_class))) { |
| 721 | if (!unbox_for_result) { |
| 722 | ThrowIllegalArgumentException(throw_location, |
| 723 | StringPrintf("%s has type %s, got %s", |
| 724 | UnboxingFailureKind(m, index, f).c_str(), |
| 725 | PrettyDescriptor(dst_class).c_str(), |
| 726 | PrettyTypeOf(o).c_str()).c_str()); |
| 727 | } else { |
| 728 | ThrowClassCastException(throw_location, |
| 729 | StringPrintf("Couldn't convert result of type %s to %s", |
| 730 | PrettyTypeOf(o).c_str(), |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 731 | PrettyDescriptor(dst_class).c_str()).c_str()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 732 | } |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 733 | return false; |
| 734 | } |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 735 | unboxed_value.SetL(o); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 736 | return true; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 737 | } |
| 738 | if (UNLIKELY(dst_class->GetPrimitiveType() == Primitive::kPrimVoid)) { |
| 739 | ThrowIllegalArgumentException(throw_location, |
| 740 | StringPrintf("Can't unbox %s to void", |
| 741 | UnboxingFailureKind(m, index, f).c_str()).c_str()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 742 | return false; |
| 743 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 744 | if (UNLIKELY(o == NULL)) { |
| 745 | if (!unbox_for_result) { |
| 746 | ThrowIllegalArgumentException(throw_location, |
| 747 | StringPrintf("%s has type %s, got null", |
| 748 | UnboxingFailureKind(m, index, f).c_str(), |
| 749 | PrettyDescriptor(dst_class).c_str()).c_str()); |
| 750 | } else { |
| 751 | ThrowNullPointerException(throw_location, |
| 752 | StringPrintf("Expected to unbox a '%s' primitive type but was returned null", |
| 753 | PrettyDescriptor(dst_class).c_str()).c_str()); |
| 754 | } |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 755 | return false; |
| 756 | } |
| 757 | |
Elliott Hughes | 1d878f3 | 2012-04-11 15:17:54 -0700 | [diff] [blame] | 758 | JValue boxed_value; |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 759 | const StringPiece src_descriptor(ClassHelper(o->GetClass()).GetDescriptor()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 760 | mirror::Class* src_class = NULL; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 761 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 762 | mirror::ArtField* primitive_field = o->GetClass()->GetIFields()->Get(0); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 763 | if (src_descriptor == "Ljava/lang/Boolean;") { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 764 | src_class = class_linker->FindPrimitiveClass('Z'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 765 | boxed_value.SetZ(primitive_field->GetBoolean(o)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 766 | } else if (src_descriptor == "Ljava/lang/Byte;") { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 767 | src_class = class_linker->FindPrimitiveClass('B'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 768 | boxed_value.SetB(primitive_field->GetByte(o)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 769 | } else if (src_descriptor == "Ljava/lang/Character;") { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 770 | src_class = class_linker->FindPrimitiveClass('C'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 771 | boxed_value.SetC(primitive_field->GetChar(o)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 772 | } else if (src_descriptor == "Ljava/lang/Float;") { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 773 | src_class = class_linker->FindPrimitiveClass('F'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 774 | boxed_value.SetF(primitive_field->GetFloat(o)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 775 | } else if (src_descriptor == "Ljava/lang/Double;") { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 776 | src_class = class_linker->FindPrimitiveClass('D'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 777 | boxed_value.SetD(primitive_field->GetDouble(o)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 778 | } else if (src_descriptor == "Ljava/lang/Integer;") { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 779 | src_class = class_linker->FindPrimitiveClass('I'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 780 | boxed_value.SetI(primitive_field->GetInt(o)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 781 | } else if (src_descriptor == "Ljava/lang/Long;") { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 782 | src_class = class_linker->FindPrimitiveClass('J'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 783 | boxed_value.SetJ(primitive_field->GetLong(o)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 784 | } else if (src_descriptor == "Ljava/lang/Short;") { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 785 | src_class = class_linker->FindPrimitiveClass('S'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 786 | boxed_value.SetS(primitive_field->GetShort(o)); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 787 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 788 | ThrowIllegalArgumentException(throw_location, |
| 789 | StringPrintf("%s has type %s, got %s", |
| 790 | UnboxingFailureKind(m, index, f).c_str(), |
| 791 | PrettyDescriptor(dst_class).c_str(), |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 792 | PrettyDescriptor(src_descriptor.data()).c_str()).c_str()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 793 | return false; |
| 794 | } |
| 795 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 796 | return ConvertPrimitiveValue(throw_location, unbox_for_result, |
| 797 | src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(), |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 798 | boxed_value, unboxed_value); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 799 | } |
| 800 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 801 | bool UnboxPrimitiveForArgument(mirror::Object* o, mirror::Class* dst_class, JValue& unboxed_value, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 802 | mirror::ArtMethod* m, size_t index) { |
Elliott Hughes | 84a5bb4 | 2012-05-16 17:52:15 -0700 | [diff] [blame] | 803 | CHECK(m != NULL); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 804 | return UnboxPrimitive(NULL, o, dst_class, unboxed_value, m, index, NULL); |
Elliott Hughes | aaa5edc | 2012-05-16 15:54:30 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 807 | bool UnboxPrimitiveForField(mirror::Object* o, mirror::Class* dst_class, JValue& unboxed_value, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 808 | mirror::ArtField* f) { |
Elliott Hughes | aaa5edc | 2012-05-16 15:54:30 -0700 | [diff] [blame] | 809 | CHECK(f != NULL); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 810 | return UnboxPrimitive(NULL, o, dst_class, unboxed_value, NULL, -1, f); |
Elliott Hughes | aaa5edc | 2012-05-16 15:54:30 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 813 | bool UnboxPrimitiveForResult(const ThrowLocation& throw_location, mirror::Object* o, |
| 814 | mirror::Class* dst_class, JValue& unboxed_value) { |
| 815 | return UnboxPrimitive(&throw_location, o, dst_class, unboxed_value, NULL, -1, NULL); |
Elliott Hughes | aaa5edc | 2012-05-16 15:54:30 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 818 | } // namespace art |