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