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