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" |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 29 | #include "nth_caller_visitor.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 30 | #include "object_utils.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 31 | #include "scoped_thread_state_change.h" |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 32 | #include "stack.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 33 | #include "well_known_classes.h" |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 34 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 35 | namespace art { |
| 36 | |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 37 | class ArgArray { |
| 38 | public: |
| 39 | explicit ArgArray(const char* shorty, uint32_t shorty_len) |
| 40 | : shorty_(shorty), shorty_len_(shorty_len), num_bytes_(0) { |
| 41 | size_t num_slots = shorty_len + 1; // +1 in case of receiver. |
| 42 | if (LIKELY((num_slots * 2) < kSmallArgArraySize)) { |
| 43 | // We can trivially use the small arg array. |
| 44 | arg_array_ = small_arg_array_; |
| 45 | } else { |
| 46 | // Analyze shorty to see if we need the large arg array. |
| 47 | for (size_t i = 1; i < shorty_len; ++i) { |
| 48 | char c = shorty[i]; |
| 49 | if (c == 'J' || c == 'D') { |
| 50 | num_slots++; |
| 51 | } |
| 52 | } |
| 53 | if (num_slots <= kSmallArgArraySize) { |
| 54 | arg_array_ = small_arg_array_; |
| 55 | } else { |
| 56 | large_arg_array_.reset(new uint32_t[num_slots]); |
| 57 | arg_array_ = large_arg_array_.get(); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | uint32_t* GetArray() { |
| 63 | return arg_array_; |
| 64 | } |
| 65 | |
| 66 | uint32_t GetNumBytes() { |
| 67 | return num_bytes_; |
| 68 | } |
| 69 | |
| 70 | void Append(uint32_t value) { |
| 71 | arg_array_[num_bytes_ / 4] = value; |
| 72 | num_bytes_ += 4; |
| 73 | } |
| 74 | |
| 75 | void Append(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 76 | Append(StackReference<mirror::Object>::FromMirrorPtr(obj).AsVRegValue()); |
| 77 | } |
| 78 | |
| 79 | void AppendWide(uint64_t value) { |
| 80 | // For ARM and MIPS portable, align wide values to 8 bytes (ArgArray starts at offset of 4). |
| 81 | #if defined(ART_USE_PORTABLE_COMPILER) && (defined(__arm__) || defined(__mips__)) |
| 82 | if (num_bytes_ % 8 == 0) { |
| 83 | num_bytes_ += 4; |
| 84 | } |
| 85 | #endif |
| 86 | arg_array_[num_bytes_ / 4] = value; |
| 87 | arg_array_[(num_bytes_ / 4) + 1] = value >> 32; |
| 88 | num_bytes_ += 8; |
| 89 | } |
| 90 | |
| 91 | void AppendFloat(float value) { |
| 92 | jvalue jv; |
| 93 | jv.f = value; |
| 94 | Append(jv.i); |
| 95 | } |
| 96 | |
| 97 | void AppendDouble(double value) { |
| 98 | jvalue jv; |
| 99 | jv.d = value; |
| 100 | AppendWide(jv.j); |
| 101 | } |
| 102 | |
Ian Rogers | e18fdd2 | 2014-03-14 13:29:43 -0700 | [diff] [blame] | 103 | void BuildArgArrayFromVarArgs(const ScopedObjectAccess& soa, mirror::Object* receiver, va_list ap) |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 104 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 105 | // Set receiver if non-null (method is not static) |
| 106 | if (receiver != nullptr) { |
| 107 | Append(receiver); |
| 108 | } |
| 109 | for (size_t i = 1; i < shorty_len_; ++i) { |
| 110 | switch (shorty_[i]) { |
| 111 | case 'Z': |
| 112 | case 'B': |
| 113 | case 'C': |
| 114 | case 'S': |
| 115 | case 'I': |
| 116 | Append(va_arg(ap, jint)); |
| 117 | break; |
| 118 | case 'F': |
| 119 | AppendFloat(va_arg(ap, jdouble)); |
| 120 | break; |
| 121 | case 'L': |
| 122 | Append(soa.Decode<mirror::Object*>(va_arg(ap, jobject))); |
| 123 | break; |
| 124 | case 'D': |
| 125 | AppendDouble(va_arg(ap, jdouble)); |
| 126 | break; |
| 127 | case 'J': |
| 128 | AppendWide(va_arg(ap, jlong)); |
| 129 | break; |
| 130 | #ifndef NDEBUG |
| 131 | default: |
| 132 | LOG(FATAL) << "Unexpected shorty character: " << shorty_[i]; |
| 133 | #endif |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
Ian Rogers | e18fdd2 | 2014-03-14 13:29:43 -0700 | [diff] [blame] | 138 | void BuildArgArrayFromJValues(const ScopedObjectAccessUnchecked& soa, mirror::Object* receiver, |
| 139 | jvalue* args) |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 140 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 141 | // Set receiver if non-null (method is not static) |
| 142 | if (receiver != nullptr) { |
| 143 | Append(receiver); |
| 144 | } |
| 145 | for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) { |
| 146 | switch (shorty_[i]) { |
| 147 | case 'Z': |
| 148 | Append(args[args_offset].z); |
| 149 | break; |
| 150 | case 'B': |
| 151 | Append(args[args_offset].b); |
| 152 | break; |
| 153 | case 'C': |
| 154 | Append(args[args_offset].c); |
| 155 | break; |
| 156 | case 'S': |
| 157 | Append(args[args_offset].s); |
| 158 | break; |
| 159 | case 'I': |
| 160 | case 'F': |
| 161 | Append(args[args_offset].i); |
| 162 | break; |
| 163 | case 'L': |
| 164 | Append(soa.Decode<mirror::Object*>(args[args_offset].l)); |
| 165 | break; |
| 166 | case 'D': |
| 167 | case 'J': |
| 168 | AppendWide(args[args_offset].j); |
| 169 | break; |
| 170 | #ifndef NDEBUG |
| 171 | default: |
| 172 | LOG(FATAL) << "Unexpected shorty character: " << shorty_[i]; |
| 173 | #endif |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void BuildArgArrayFromFrame(ShadowFrame* shadow_frame, uint32_t arg_offset) |
| 179 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 180 | // Set receiver if non-null (method is not static) |
| 181 | size_t cur_arg = arg_offset; |
| 182 | if (!shadow_frame->GetMethod()->IsStatic()) { |
| 183 | Append(shadow_frame->GetVReg(cur_arg)); |
| 184 | cur_arg++; |
| 185 | } |
| 186 | for (size_t i = 1; i < shorty_len_; ++i) { |
| 187 | switch (shorty_[i]) { |
| 188 | case 'Z': |
| 189 | case 'B': |
| 190 | case 'C': |
| 191 | case 'S': |
| 192 | case 'I': |
| 193 | case 'F': |
| 194 | case 'L': |
| 195 | Append(shadow_frame->GetVReg(cur_arg)); |
| 196 | cur_arg++; |
| 197 | break; |
| 198 | case 'D': |
| 199 | case 'J': |
| 200 | AppendWide(shadow_frame->GetVRegLong(cur_arg)); |
| 201 | cur_arg++; |
| 202 | cur_arg++; |
| 203 | break; |
| 204 | #ifndef NDEBUG |
| 205 | default: |
| 206 | LOG(FATAL) << "Unexpected shorty character: " << shorty_[i]; |
| 207 | #endif |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | static void ThrowIllegalPrimitiveArgumentException(const char* expected, |
| 213 | const StringPiece& found_descriptor) |
| 214 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 215 | ThrowIllegalArgumentException(nullptr, |
| 216 | StringPrintf("Invalid primitive conversion from %s to %s", expected, |
| 217 | PrettyDescriptor(found_descriptor.as_string()).c_str()).c_str()); |
| 218 | } |
| 219 | |
Ian Rogers | e18fdd2 | 2014-03-14 13:29:43 -0700 | [diff] [blame] | 220 | bool BuildArgArrayFromObjectArray(const ScopedObjectAccess& soa, mirror::Object* receiver, |
| 221 | mirror::ObjectArray<mirror::Object>* args, MethodHelper& mh) |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 222 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 223 | const DexFile::TypeList* classes = mh.GetParameterTypeList(); |
| 224 | // Set receiver if non-null (method is not static) |
| 225 | if (receiver != nullptr) { |
| 226 | Append(receiver); |
| 227 | } |
| 228 | for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) { |
| 229 | mirror::Object* arg = args->Get(args_offset); |
| 230 | if (((shorty_[i] == 'L') && (arg != nullptr)) || ((arg == nullptr && shorty_[i] != 'L'))) { |
| 231 | mirror::Class* dst_class = |
| 232 | mh.GetClassFromTypeIdx(classes->GetTypeItem(args_offset).type_idx_); |
| 233 | if (UNLIKELY(arg == nullptr || !arg->InstanceOf(dst_class))) { |
| 234 | ThrowIllegalArgumentException(nullptr, |
Ian Rogers | 11e4c03 | 2014-03-14 12:00:39 -0700 | [diff] [blame] | 235 | StringPrintf("method %s argument %zd has type %s, got %s", |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 236 | PrettyMethod(mh.GetMethod(), false).c_str(), |
| 237 | args_offset + 1, // Humans don't count from 0. |
| 238 | PrettyDescriptor(dst_class).c_str(), |
| 239 | PrettyTypeOf(arg).c_str()).c_str()); |
| 240 | return false; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | #define DO_FIRST_ARG(match_descriptor, get_fn, append) { \ |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 245 | if (LIKELY(arg != nullptr && arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \ |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 246 | mirror::ArtField* primitive_field = arg->GetClass()->GetIFields()->Get(0); \ |
| 247 | append(primitive_field-> get_fn(arg)); |
| 248 | |
| 249 | #define DO_ARG(match_descriptor, get_fn, append) \ |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 250 | } else if (LIKELY(arg != nullptr && \ |
| 251 | arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \ |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 252 | mirror::ArtField* primitive_field = arg->GetClass()->GetIFields()->Get(0); \ |
| 253 | append(primitive_field-> get_fn(arg)); |
| 254 | |
| 255 | #define DO_FAIL(expected) \ |
| 256 | } else { \ |
| 257 | if (arg->GetClass<>()->IsPrimitive()) { \ |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 258 | ThrowIllegalPrimitiveArgumentException(expected, \ |
| 259 | arg->GetClass<>()->GetDescriptor().c_str()); \ |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 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]; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame^] | 345 | std::unique_ptr<uint32_t[]> large_arg_array_; |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 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, |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 464 | jobject javaReceiver, jobject javaArgs, bool accessible) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 465 | mirror::ArtMethod* m = mirror::ArtMethod::FromReflectedMethod(soa, javaMethod); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 466 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 467 | mirror::Class* declaring_class = m->GetDeclaringClass(); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 468 | if (UNLIKELY(!declaring_class->IsInitialized())) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 469 | StackHandleScope<1> hs(soa.Self()); |
| 470 | Handle<mirror::Class> h_class(hs.NewHandle(declaring_class)); |
| 471 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(h_class, true, true)) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 472 | return nullptr; |
| 473 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 474 | declaring_class = h_class.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 | |
Jeff Hao | cb4581a | 2014-03-28 15:43:37 -0700 | [diff] [blame] | 503 | // If method is not set to be accessible, verify it can be accessed by the caller. |
| 504 | if (!accessible && !VerifyAccess(receiver, declaring_class, m->GetAccessFlags())) { |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 505 | ThrowIllegalAccessException(nullptr, StringPrintf("Cannot access method: %s", |
| 506 | PrettyMethod(m).c_str()).c_str()); |
| 507 | return nullptr; |
| 508 | } |
| 509 | |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 510 | // Invoke the method. |
| 511 | JValue result; |
| 512 | ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength()); |
Ian Rogers | e18fdd2 | 2014-03-14 13:29:43 -0700 | [diff] [blame] | 513 | if (!arg_array.BuildArgArrayFromObjectArray(soa, receiver, objects, mh)) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 514 | CHECK(soa.Self()->IsExceptionPending()); |
| 515 | return nullptr; |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 518 | InvokeWithArgArray(soa, m, &arg_array, &result, mh.GetShorty()); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 519 | |
| 520 | // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 521 | if (soa.Self()->IsExceptionPending()) { |
| 522 | jthrowable th = soa.Env()->ExceptionOccurred(); |
| 523 | soa.Env()->ExceptionClear(); |
| 524 | jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException"); |
| 525 | jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V"); |
| 526 | jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th); |
| 527 | soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance)); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 528 | return NULL; |
| 529 | } |
| 530 | |
| 531 | // Box if necessary and return. |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 532 | return soa.AddLocalReference<jobject>(BoxPrimitive(mh.GetReturnType()->GetPrimitiveType(), |
| 533 | result)); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 536 | bool VerifyObjectIsClass(mirror::Object* o, mirror::Class* c) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 537 | if (o == NULL) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 538 | ThrowNullPointerException(NULL, "null receiver"); |
| 539 | return false; |
Elliott Hughes | b600b3f | 2012-03-14 13:57:24 -0700 | [diff] [blame] | 540 | } else if (!o->InstanceOf(c)) { |
Elliott Hughes | b600b3f | 2012-03-14 13:57:24 -0700 | [diff] [blame] | 541 | std::string expected_class_name(PrettyDescriptor(c)); |
| 542 | std::string actual_class_name(PrettyTypeOf(o)); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 543 | ThrowIllegalArgumentException(NULL, |
| 544 | StringPrintf("Expected receiver of type %s, but got %s", |
| 545 | expected_class_name.c_str(), |
| 546 | actual_class_name.c_str()).c_str()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 547 | return false; |
| 548 | } |
| 549 | return true; |
| 550 | } |
| 551 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 552 | bool ConvertPrimitiveValue(const ThrowLocation* throw_location, bool unbox_for_result, |
| 553 | Primitive::Type srcType, Primitive::Type dstType, |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 554 | const JValue& src, JValue* dst) { |
| 555 | DCHECK(srcType != Primitive::kPrimNot && dstType != Primitive::kPrimNot); |
| 556 | if (LIKELY(srcType == dstType)) { |
| 557 | dst->SetJ(src.GetJ()); |
| 558 | return true; |
| 559 | } |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 560 | switch (dstType) { |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 561 | case Primitive::kPrimBoolean: // Fall-through. |
| 562 | case Primitive::kPrimChar: // Fall-through. |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 563 | case Primitive::kPrimByte: |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 564 | // Only expect assignment with source and destination of identical type. |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 565 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 566 | case Primitive::kPrimShort: |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 567 | if (srcType == Primitive::kPrimByte) { |
| 568 | dst->SetS(src.GetI()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 569 | return true; |
| 570 | } |
| 571 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 572 | case Primitive::kPrimInt: |
| 573 | if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar || |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 574 | srcType == Primitive::kPrimShort) { |
| 575 | dst->SetI(src.GetI()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 576 | return true; |
| 577 | } |
| 578 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 579 | case Primitive::kPrimLong: |
| 580 | if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar || |
| 581 | srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) { |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 582 | dst->SetJ(src.GetI()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 583 | return true; |
| 584 | } |
| 585 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 586 | case Primitive::kPrimFloat: |
| 587 | if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar || |
| 588 | srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) { |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 589 | dst->SetF(src.GetI()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 590 | return true; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 591 | } else if (srcType == Primitive::kPrimLong) { |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 592 | dst->SetF(src.GetJ()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 593 | return true; |
| 594 | } |
| 595 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 596 | case Primitive::kPrimDouble: |
| 597 | if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar || |
| 598 | srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) { |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 599 | dst->SetD(src.GetI()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 600 | return true; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 601 | } else if (srcType == Primitive::kPrimLong) { |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 602 | dst->SetD(src.GetJ()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 603 | return true; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 604 | } else if (srcType == Primitive::kPrimFloat) { |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 605 | dst->SetD(src.GetF()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 606 | return true; |
| 607 | } |
| 608 | break; |
| 609 | default: |
| 610 | break; |
| 611 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 612 | if (!unbox_for_result) { |
| 613 | ThrowIllegalArgumentException(throw_location, |
| 614 | StringPrintf("Invalid primitive conversion from %s to %s", |
| 615 | PrettyDescriptor(srcType).c_str(), |
| 616 | PrettyDescriptor(dstType).c_str()).c_str()); |
| 617 | } else { |
| 618 | ThrowClassCastException(throw_location, |
| 619 | StringPrintf("Couldn't convert result of type %s to %s", |
| 620 | PrettyDescriptor(srcType).c_str(), |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 621 | PrettyDescriptor(dstType).c_str()).c_str()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 622 | } |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 623 | return false; |
| 624 | } |
| 625 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 626 | mirror::Object* BoxPrimitive(Primitive::Type src_class, const JValue& value) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 627 | if (src_class == Primitive::kPrimNot) { |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 628 | return value.GetL(); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 629 | } |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 630 | if (src_class == Primitive::kPrimVoid) { |
| 631 | // There's no such thing as a void field, and void methods invoked via reflection return null. |
| 632 | return nullptr; |
| 633 | } |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 634 | |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 635 | jmethodID m = nullptr; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 636 | const char* shorty; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 637 | switch (src_class) { |
| 638 | case Primitive::kPrimBoolean: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 639 | m = WellKnownClasses::java_lang_Boolean_valueOf; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 640 | shorty = "LZ"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 641 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 642 | case Primitive::kPrimByte: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 643 | m = WellKnownClasses::java_lang_Byte_valueOf; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 644 | shorty = "LB"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 645 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 646 | case Primitive::kPrimChar: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 647 | m = WellKnownClasses::java_lang_Character_valueOf; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 648 | shorty = "LC"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 649 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 650 | case Primitive::kPrimDouble: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 651 | m = WellKnownClasses::java_lang_Double_valueOf; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 652 | shorty = "LD"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 653 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 654 | case Primitive::kPrimFloat: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 655 | m = WellKnownClasses::java_lang_Float_valueOf; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 656 | shorty = "LF"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 657 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 658 | case Primitive::kPrimInt: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 659 | m = WellKnownClasses::java_lang_Integer_valueOf; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 660 | shorty = "LI"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 661 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 662 | case Primitive::kPrimLong: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 663 | m = WellKnownClasses::java_lang_Long_valueOf; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 664 | shorty = "LJ"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 665 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 666 | case Primitive::kPrimShort: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 667 | m = WellKnownClasses::java_lang_Short_valueOf; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 668 | shorty = "LS"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 669 | break; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 670 | default: |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 671 | LOG(FATAL) << static_cast<int>(src_class); |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 672 | shorty = nullptr; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 675 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 676 | DCHECK_EQ(soa.Self()->GetState(), kRunnable); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 677 | |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 678 | ArgArray arg_array(shorty, 2); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 679 | JValue result; |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 680 | if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) { |
| 681 | arg_array.AppendWide(value.GetJ()); |
| 682 | } else { |
| 683 | arg_array.Append(value.GetI()); |
| 684 | } |
| 685 | |
| 686 | soa.DecodeMethod(m)->Invoke(soa.Self(), arg_array.GetArray(), arg_array.GetNumBytes(), |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 687 | &result, shorty); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 688 | return result.GetL(); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 691 | static std::string UnboxingFailureKind(mirror::ArtField* f) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 692 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 693 | if (f != nullptr) { |
Elliott Hughes | aaa5edc | 2012-05-16 15:54:30 -0700 | [diff] [blame] | 694 | return "field " + PrettyField(f, false); |
| 695 | } |
| 696 | return "result"; |
| 697 | } |
| 698 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 699 | static bool UnboxPrimitive(const ThrowLocation* throw_location, mirror::Object* o, |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 700 | mirror::Class* dst_class, mirror::ArtField* f, |
| 701 | JValue* unboxed_value) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 702 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 703 | bool unbox_for_result = (f == nullptr); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 704 | if (!dst_class->IsPrimitive()) { |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 705 | if (UNLIKELY(o != nullptr && !o->InstanceOf(dst_class))) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 706 | if (!unbox_for_result) { |
| 707 | ThrowIllegalArgumentException(throw_location, |
| 708 | StringPrintf("%s has type %s, got %s", |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 709 | UnboxingFailureKind(f).c_str(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 710 | PrettyDescriptor(dst_class).c_str(), |
| 711 | PrettyTypeOf(o).c_str()).c_str()); |
| 712 | } else { |
| 713 | ThrowClassCastException(throw_location, |
| 714 | StringPrintf("Couldn't convert result of type %s to %s", |
| 715 | PrettyTypeOf(o).c_str(), |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 716 | PrettyDescriptor(dst_class).c_str()).c_str()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 717 | } |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 718 | return false; |
| 719 | } |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 720 | unboxed_value->SetL(o); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 721 | return true; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 722 | } |
| 723 | if (UNLIKELY(dst_class->GetPrimitiveType() == Primitive::kPrimVoid)) { |
| 724 | ThrowIllegalArgumentException(throw_location, |
| 725 | StringPrintf("Can't unbox %s to void", |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 726 | UnboxingFailureKind(f).c_str()).c_str()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 727 | return false; |
| 728 | } |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 729 | if (UNLIKELY(o == nullptr)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 730 | if (!unbox_for_result) { |
| 731 | ThrowIllegalArgumentException(throw_location, |
| 732 | StringPrintf("%s has type %s, got null", |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 733 | UnboxingFailureKind(f).c_str(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 734 | PrettyDescriptor(dst_class).c_str()).c_str()); |
| 735 | } else { |
| 736 | ThrowNullPointerException(throw_location, |
| 737 | StringPrintf("Expected to unbox a '%s' primitive type but was returned null", |
| 738 | PrettyDescriptor(dst_class).c_str()).c_str()); |
| 739 | } |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 740 | return false; |
| 741 | } |
| 742 | |
Elliott Hughes | 1d878f3 | 2012-04-11 15:17:54 -0700 | [diff] [blame] | 743 | JValue boxed_value; |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 744 | mirror::Class* klass = o->GetClass(); |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 745 | mirror::Class* src_class = nullptr; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 746 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 747 | mirror::ArtField* primitive_field = o->GetClass()->GetIFields()->Get(0); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 748 | if (klass->DescriptorEquals("Ljava/lang/Boolean;")) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 749 | src_class = class_linker->FindPrimitiveClass('Z'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 750 | boxed_value.SetZ(primitive_field->GetBoolean(o)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 751 | } else if (klass->DescriptorEquals("Ljava/lang/Byte;")) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 752 | src_class = class_linker->FindPrimitiveClass('B'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 753 | boxed_value.SetB(primitive_field->GetByte(o)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 754 | } else if (klass->DescriptorEquals("Ljava/lang/Character;")) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 755 | src_class = class_linker->FindPrimitiveClass('C'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 756 | boxed_value.SetC(primitive_field->GetChar(o)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 757 | } else if (klass->DescriptorEquals("Ljava/lang/Float;")) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 758 | src_class = class_linker->FindPrimitiveClass('F'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 759 | boxed_value.SetF(primitive_field->GetFloat(o)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 760 | } else if (klass->DescriptorEquals("Ljava/lang/Double;")) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 761 | src_class = class_linker->FindPrimitiveClass('D'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 762 | boxed_value.SetD(primitive_field->GetDouble(o)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 763 | } else if (klass->DescriptorEquals("Ljava/lang/Integer;")) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 764 | src_class = class_linker->FindPrimitiveClass('I'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 765 | boxed_value.SetI(primitive_field->GetInt(o)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 766 | } else if (klass->DescriptorEquals("Ljava/lang/Long;")) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 767 | src_class = class_linker->FindPrimitiveClass('J'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 768 | boxed_value.SetJ(primitive_field->GetLong(o)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 769 | } else if (klass->DescriptorEquals("Ljava/lang/Short;")) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 770 | src_class = class_linker->FindPrimitiveClass('S'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 771 | boxed_value.SetS(primitive_field->GetShort(o)); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 772 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 773 | ThrowIllegalArgumentException(throw_location, |
| 774 | StringPrintf("%s has type %s, got %s", |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 775 | UnboxingFailureKind(f).c_str(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 776 | PrettyDescriptor(dst_class).c_str(), |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 777 | PrettyDescriptor(o->GetClass()->GetDescriptor()).c_str()).c_str()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 778 | return false; |
| 779 | } |
| 780 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 781 | return ConvertPrimitiveValue(throw_location, unbox_for_result, |
| 782 | src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(), |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 783 | boxed_value, unboxed_value); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 786 | bool UnboxPrimitiveForField(mirror::Object* o, mirror::Class* dst_class, mirror::ArtField* f, |
| 787 | JValue* unboxed_value) { |
| 788 | DCHECK(f != nullptr); |
| 789 | return UnboxPrimitive(nullptr, o, dst_class, f, unboxed_value); |
Elliott Hughes | aaa5edc | 2012-05-16 15:54:30 -0700 | [diff] [blame] | 790 | } |
| 791 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 792 | bool UnboxPrimitiveForResult(const ThrowLocation& throw_location, mirror::Object* o, |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 793 | mirror::Class* dst_class, JValue* unboxed_value) { |
| 794 | return UnboxPrimitive(&throw_location, o, dst_class, nullptr, unboxed_value); |
Elliott Hughes | aaa5edc | 2012-05-16 15:54:30 -0700 | [diff] [blame] | 795 | } |
| 796 | |
Jeff Hao | cb4581a | 2014-03-28 15:43:37 -0700 | [diff] [blame] | 797 | bool VerifyAccess(mirror::Object* obj, mirror::Class* declaring_class, uint32_t access_flags) { |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 798 | NthCallerVisitor visitor(Thread::Current(), 2); |
| 799 | visitor.WalkStack(); |
| 800 | mirror::Class* caller_class = visitor.caller->GetDeclaringClass(); |
| 801 | |
Jeff Hao | 925b687 | 2014-04-01 11:21:30 -0700 | [diff] [blame] | 802 | if (((access_flags & kAccPublic) != 0) || (caller_class == declaring_class)) { |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 803 | return true; |
| 804 | } |
Jeff Hao | cb4581a | 2014-03-28 15:43:37 -0700 | [diff] [blame] | 805 | if ((access_flags & kAccPrivate) != 0) { |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 806 | return false; |
| 807 | } |
Jeff Hao | cb4581a | 2014-03-28 15:43:37 -0700 | [diff] [blame] | 808 | if ((access_flags & kAccProtected) != 0) { |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 809 | if (obj != nullptr && !obj->InstanceOf(caller_class) && |
| 810 | !declaring_class->IsInSamePackage(caller_class)) { |
| 811 | return false; |
| 812 | } else if (declaring_class->IsAssignableFrom(caller_class)) { |
| 813 | return true; |
| 814 | } |
| 815 | } |
| 816 | if (!declaring_class->IsInSamePackage(caller_class)) { |
| 817 | return false; |
| 818 | } |
| 819 | return true; |
| 820 | } |
| 821 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 822 | } // namespace art |