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