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