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