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" |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 21 | #include "class_linker.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 22 | #include "common_throws.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 23 | #include "dex_file-inl.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 24 | #include "entrypoints/entrypoint_utils.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" |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 27 | #include "mirror/abstract_method.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | #include "mirror/class-inl.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" |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 32 | #include "stack.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 33 | #include "well_known_classes.h" |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 34 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 35 | namespace art { |
| 36 | |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 37 | class ArgArray { |
| 38 | public: |
| 39 | explicit ArgArray(const char* shorty, uint32_t shorty_len) |
| 40 | : shorty_(shorty), shorty_len_(shorty_len), num_bytes_(0) { |
| 41 | size_t num_slots = shorty_len + 1; // +1 in case of receiver. |
| 42 | if (LIKELY((num_slots * 2) < kSmallArgArraySize)) { |
| 43 | // We can trivially use the small arg array. |
| 44 | arg_array_ = small_arg_array_; |
| 45 | } else { |
| 46 | // Analyze shorty to see if we need the large arg array. |
| 47 | for (size_t i = 1; i < shorty_len; ++i) { |
| 48 | char c = shorty[i]; |
| 49 | if (c == 'J' || c == 'D') { |
| 50 | num_slots++; |
| 51 | } |
| 52 | } |
| 53 | if (num_slots <= kSmallArgArraySize) { |
| 54 | arg_array_ = small_arg_array_; |
| 55 | } else { |
| 56 | large_arg_array_.reset(new uint32_t[num_slots]); |
| 57 | arg_array_ = large_arg_array_.get(); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | uint32_t* GetArray() { |
| 63 | return arg_array_; |
| 64 | } |
| 65 | |
| 66 | uint32_t GetNumBytes() { |
| 67 | return num_bytes_; |
| 68 | } |
| 69 | |
| 70 | void Append(uint32_t value) { |
| 71 | arg_array_[num_bytes_ / 4] = value; |
| 72 | num_bytes_ += 4; |
| 73 | } |
| 74 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 75 | void Append(mirror::Object* obj) SHARED_REQUIRES(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) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 99 | SHARED_REQUIRES(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) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 135 | SHARED_REQUIRES(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) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 174 | SHARED_REQUIRES(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) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 209 | SHARED_REQUIRES(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) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 217 | SHARED_REQUIRES(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'))) { |
| 226 | mirror::Class* dst_class = |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 227 | 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", |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 231 | PrettyMethod(m, 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", \ |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 259 | PrettyMethod(m, 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 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 345 | static void CheckMethodArguments(JavaVMExt* vm, ArtMethod* m, uint32_t* args) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 346 | SHARED_REQUIRES(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 | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 358 | Thread* const self = Thread::Current(); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 359 | for (uint32_t i = 0; i < num_params; i++) { |
| 360 | uint16_t type_idx = params->GetTypeItem(i).type_idx_; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 361 | mirror::Class* param_type = m->GetClassFromTypeIndex(type_idx, true); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 362 | if (param_type == nullptr) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 363 | CHECK(self->IsExceptionPending()); |
| 364 | LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: " |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 365 | << m->GetTypeDescriptorFromTypeIdx(type_idx) << "\n" |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 366 | << self->GetException()->Dump(); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 367 | self->ClearException(); |
| 368 | ++error_count; |
| 369 | } else if (!param_type->IsPrimitive()) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 370 | // TODO: There is a compaction bug here since GetClassFromTypeIdx can cause thread suspension, |
| 371 | // this is a hard to fix problem since the args can contain Object*, we need to save and |
| 372 | // 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] | 373 | mirror::Object* argument = |
| 374 | (reinterpret_cast<StackReference<mirror::Object>*>(&args[i + offset]))->AsMirrorPtr(); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 375 | if (argument != nullptr && !argument->InstanceOf(param_type)) { |
| 376 | LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of " |
| 377 | << PrettyTypeOf(argument) << " as argument " << (i + 1) |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 378 | << " to " << PrettyMethod(m); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 379 | ++error_count; |
| 380 | } |
| 381 | } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) { |
| 382 | offset++; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 383 | } else { |
| 384 | int32_t arg = static_cast<int32_t>(args[i + offset]); |
| 385 | if (param_type->IsPrimitiveBoolean()) { |
| 386 | if (arg != JNI_TRUE && arg != JNI_FALSE) { |
| 387 | 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] | 388 | << arg << " as argument " << (i + 1) << " to " << PrettyMethod(m); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 389 | ++error_count; |
| 390 | } |
| 391 | } else if (param_type->IsPrimitiveByte()) { |
| 392 | if (arg < -128 || arg > 127) { |
| 393 | LOG(ERROR) << "JNI ERROR (app bug): expected jbyte 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->IsPrimitiveChar()) { |
| 398 | if (args[i + offset] > 0xFFFF) { |
| 399 | LOG(ERROR) << "JNI ERROR (app bug): expected jchar 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->IsPrimitiveShort()) { |
| 404 | if (arg < -32768 || arg > 0x7FFF) { |
| 405 | LOG(ERROR) << "JNI ERROR (app bug): expected jshort 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 | } |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 410 | } |
| 411 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 412 | if (UNLIKELY(error_count > 0)) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 413 | // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort |
| 414 | // with an argument. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 415 | vm->JniAbortF(nullptr, "bad arguments passed to %s (see above for details)", |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 416 | PrettyMethod(m).c_str()); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 420 | static ArtMethod* FindVirtualMethod(mirror::Object* receiver, ArtMethod* method) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 421 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 422 | return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method, sizeof(void*)); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 426 | static void InvokeWithArgArray(const ScopedObjectAccessAlreadyRunnable& soa, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 427 | ArtMethod* method, ArgArray* arg_array, JValue* result, |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 428 | const char* shorty) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 429 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 430 | uint32_t* args = arg_array->GetArray(); |
| 431 | if (UNLIKELY(soa.Env()->check_jni)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 432 | CheckMethodArguments(soa.Vm(), method->GetInterfaceMethodIfProxy(sizeof(void*)), args); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 433 | } |
| 434 | method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, shorty); |
| 435 | } |
| 436 | |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 437 | JValue InvokeWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid, |
| 438 | va_list args) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 439 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 440 | // We want to make sure that the stack is not within a small distance from the |
| 441 | // protected region in case we are calling into a leaf function whose stack |
| 442 | // check has been elided. |
| 443 | if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) { |
| 444 | ThrowStackOverflowError(soa.Self()); |
| 445 | return JValue(); |
| 446 | } |
| 447 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 448 | ArtMethod* method = soa.DecodeMethod(mid); |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 449 | bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor(); |
| 450 | if (is_string_init) { |
| 451 | // Replace calls to String.<init> with equivalent StringFactory call. |
| 452 | method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid)); |
| 453 | } |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 454 | mirror::Object* receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object*>(obj); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 455 | uint32_t shorty_len = 0; |
| 456 | const char* shorty = method->GetShorty(&shorty_len); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 457 | JValue result; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 458 | ArgArray arg_array(shorty, shorty_len); |
Ian Rogers | e18fdd2 | 2014-03-14 13:29:43 -0700 | [diff] [blame] | 459 | arg_array.BuildArgArrayFromVarArgs(soa, receiver, args); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 460 | InvokeWithArgArray(soa, method, &arg_array, &result, shorty); |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 461 | if (is_string_init) { |
| 462 | // For string init, remap original receiver to StringFactory result. |
Jeff Hao | 83c8195 | 2015-05-27 19:29:29 -0700 | [diff] [blame] | 463 | UpdateReference(soa.Self(), obj, result.GetL()); |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 464 | } |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 465 | return result; |
| 466 | } |
| 467 | |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 468 | JValue InvokeWithJValues(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid, |
| 469 | jvalue* args) { |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 470 | // We want to make sure that the stack is not within a small distance from the |
| 471 | // protected region in case we are calling into a leaf function whose stack |
| 472 | // check has been elided. |
| 473 | if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) { |
| 474 | ThrowStackOverflowError(soa.Self()); |
| 475 | return JValue(); |
| 476 | } |
| 477 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 478 | ArtMethod* method = soa.DecodeMethod(mid); |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 479 | bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor(); |
| 480 | if (is_string_init) { |
| 481 | // Replace calls to String.<init> with equivalent StringFactory call. |
| 482 | method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid)); |
| 483 | } |
| 484 | mirror::Object* receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object*>(obj); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 485 | uint32_t shorty_len = 0; |
| 486 | const char* shorty = method->GetShorty(&shorty_len); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 487 | JValue result; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 488 | ArgArray arg_array(shorty, shorty_len); |
Ian Rogers | e18fdd2 | 2014-03-14 13:29:43 -0700 | [diff] [blame] | 489 | arg_array.BuildArgArrayFromJValues(soa, receiver, args); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 490 | InvokeWithArgArray(soa, method, &arg_array, &result, shorty); |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 491 | if (is_string_init) { |
| 492 | // For string init, remap original receiver to StringFactory result. |
Jeff Hao | 83c8195 | 2015-05-27 19:29:29 -0700 | [diff] [blame] | 493 | UpdateReference(soa.Self(), obj, result.GetL()); |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 494 | } |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 495 | return result; |
| 496 | } |
| 497 | |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 498 | JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccessAlreadyRunnable& soa, |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 499 | jobject obj, jmethodID mid, jvalue* args) { |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 500 | // We want to make sure that the stack is not within a small distance from the |
| 501 | // protected region in case we are calling into a leaf function whose stack |
| 502 | // check has been elided. |
| 503 | if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) { |
| 504 | ThrowStackOverflowError(soa.Self()); |
| 505 | return JValue(); |
| 506 | } |
| 507 | |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 508 | mirror::Object* receiver = soa.Decode<mirror::Object*>(obj); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 509 | ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid)); |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 510 | bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor(); |
| 511 | if (is_string_init) { |
| 512 | // Replace calls to String.<init> with equivalent StringFactory call. |
| 513 | method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid)); |
| 514 | receiver = nullptr; |
| 515 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 516 | uint32_t shorty_len = 0; |
| 517 | const char* shorty = method->GetShorty(&shorty_len); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 518 | JValue result; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 519 | ArgArray arg_array(shorty, shorty_len); |
Ian Rogers | e18fdd2 | 2014-03-14 13:29:43 -0700 | [diff] [blame] | 520 | arg_array.BuildArgArrayFromJValues(soa, receiver, args); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 521 | InvokeWithArgArray(soa, method, &arg_array, &result, shorty); |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 522 | if (is_string_init) { |
| 523 | // For string init, remap original receiver to StringFactory result. |
Jeff Hao | 83c8195 | 2015-05-27 19:29:29 -0700 | [diff] [blame] | 524 | UpdateReference(soa.Self(), obj, result.GetL()); |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 525 | } |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 526 | return result; |
| 527 | } |
| 528 | |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 529 | JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa, |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 530 | jobject obj, jmethodID mid, va_list args) { |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 531 | // We want to make sure that the stack is not within a small distance from the |
| 532 | // protected region in case we are calling into a leaf function whose stack |
| 533 | // check has been elided. |
| 534 | if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) { |
| 535 | ThrowStackOverflowError(soa.Self()); |
| 536 | return JValue(); |
| 537 | } |
| 538 | |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 539 | mirror::Object* receiver = soa.Decode<mirror::Object*>(obj); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 540 | ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid)); |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 541 | bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor(); |
| 542 | if (is_string_init) { |
| 543 | // Replace calls to String.<init> with equivalent StringFactory call. |
| 544 | method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid)); |
| 545 | receiver = nullptr; |
| 546 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 547 | uint32_t shorty_len = 0; |
| 548 | const char* shorty = method->GetShorty(&shorty_len); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 549 | JValue result; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 550 | ArgArray arg_array(shorty, shorty_len); |
Ian Rogers | e18fdd2 | 2014-03-14 13:29:43 -0700 | [diff] [blame] | 551 | arg_array.BuildArgArrayFromVarArgs(soa, receiver, args); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 552 | InvokeWithArgArray(soa, method, &arg_array, &result, shorty); |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 553 | if (is_string_init) { |
| 554 | // For string init, remap original receiver to StringFactory result. |
Jeff Hao | 83c8195 | 2015-05-27 19:29:29 -0700 | [diff] [blame] | 555 | UpdateReference(soa.Self(), obj, result.GetL()); |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 556 | } |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 557 | return result; |
| 558 | } |
| 559 | |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 560 | jobject InvokeMethod(const ScopedObjectAccessAlreadyRunnable& soa, jobject javaMethod, |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 561 | jobject javaReceiver, jobject javaArgs, size_t num_frames) { |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 562 | // We want to make sure that the stack is not within a small distance from the |
| 563 | // protected region in case we are calling into a leaf function whose stack |
| 564 | // check has been elided. |
| 565 | if (UNLIKELY(__builtin_frame_address(0) < |
| 566 | soa.Self()->GetStackEndForInterpreter(true))) { |
| 567 | ThrowStackOverflowError(soa.Self()); |
| 568 | return nullptr; |
| 569 | } |
| 570 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 571 | auto* abstract_method = soa.Decode<mirror::AbstractMethod*>(javaMethod); |
| 572 | const bool accessible = abstract_method->IsAccessible(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 573 | ArtMethod* m = abstract_method->GetArtMethod(); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 574 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 575 | mirror::Class* declaring_class = m->GetDeclaringClass(); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 576 | if (UNLIKELY(!declaring_class->IsInitialized())) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 577 | StackHandleScope<1> hs(soa.Self()); |
| 578 | Handle<mirror::Class> h_class(hs.NewHandle(declaring_class)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 579 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), h_class, true, true)) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 580 | return nullptr; |
| 581 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 582 | declaring_class = h_class.Get(); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 585 | mirror::Object* receiver = nullptr; |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 586 | if (!m->IsStatic()) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 587 | // Replace calls to String.<init> with equivalent StringFactory call. |
| 588 | if (declaring_class->IsStringClass() && m->IsConstructor()) { |
| 589 | jmethodID mid = soa.EncodeMethod(m); |
| 590 | m = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid)); |
| 591 | CHECK(javaReceiver == nullptr); |
| 592 | } else { |
| 593 | // Check that the receiver is non-null and an instance of the field's declaring class. |
| 594 | receiver = soa.Decode<mirror::Object*>(javaReceiver); |
| 595 | if (!VerifyObjectIsClass(receiver, declaring_class)) { |
| 596 | return nullptr; |
| 597 | } |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 598 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 599 | // Find the actual implementation of the virtual method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 600 | m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m, sizeof(void*)); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 601 | } |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | // 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] | 605 | auto* objects = soa.Decode<mirror::ObjectArray<mirror::Object>*>(javaArgs); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 606 | auto* np_method = m->GetInterfaceMethodIfProxy(sizeof(void*)); |
| 607 | const DexFile::TypeList* classes = np_method->GetParameterTypeList(); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 608 | uint32_t classes_size = (classes == nullptr) ? 0 : classes->Size(); |
| 609 | uint32_t arg_count = (objects != nullptr) ? objects->GetLength() : 0; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 610 | if (arg_count != classes_size) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 611 | ThrowIllegalArgumentException(StringPrintf("Wrong number of arguments; expected %d, got %d", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 612 | classes_size, arg_count).c_str()); |
Ian Rogers | a048560 | 2014-12-02 15:48:04 -0800 | [diff] [blame] | 613 | return nullptr; |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Jeff Hao | cb4581a | 2014-03-28 15:43:37 -0700 | [diff] [blame] | 616 | // 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] | 617 | mirror::Class* calling_class = nullptr; |
| 618 | if (!accessible && !VerifyAccess(soa.Self(), receiver, declaring_class, m->GetAccessFlags(), |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 619 | &calling_class, num_frames)) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 620 | ThrowIllegalAccessException( |
Andreas Gampe | c0d8229 | 2014-09-23 10:38:30 -0700 | [diff] [blame] | 621 | StringPrintf("Class %s cannot access %s method %s of class %s", |
| 622 | calling_class == nullptr ? "null" : PrettyClass(calling_class).c_str(), |
| 623 | PrettyJavaAccessFlags(m->GetAccessFlags()).c_str(), |
| 624 | PrettyMethod(m).c_str(), |
| 625 | m->GetDeclaringClass() == nullptr ? "null" : |
| 626 | PrettyClass(m->GetDeclaringClass()).c_str()).c_str()); |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 627 | return nullptr; |
| 628 | } |
| 629 | |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 630 | // Invoke the method. |
| 631 | JValue result; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 632 | uint32_t shorty_len = 0; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 633 | const char* shorty = np_method->GetShorty(&shorty_len); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 634 | ArgArray arg_array(shorty, shorty_len); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 635 | if (!arg_array.BuildArgArrayFromObjectArray(receiver, objects, np_method)) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 636 | CHECK(soa.Self()->IsExceptionPending()); |
| 637 | return nullptr; |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 640 | InvokeWithArgArray(soa, m, &arg_array, &result, shorty); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 641 | |
| 642 | // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 643 | if (soa.Self()->IsExceptionPending()) { |
Mathieu Chartier | a61894d | 2015-04-23 16:32:54 -0700 | [diff] [blame] | 644 | // 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] | 645 | jthrowable th = soa.Env()->ExceptionOccurred(); |
Mathieu Chartier | a61894d | 2015-04-23 16:32:54 -0700 | [diff] [blame] | 646 | soa.Self()->ClearException(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 647 | jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException"); |
Mathieu Chartier | a61894d | 2015-04-23 16:32:54 -0700 | [diff] [blame] | 648 | if (exception_class == nullptr) { |
| 649 | soa.Self()->AssertPendingOOMException(); |
| 650 | return nullptr; |
| 651 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 652 | jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V"); |
Mathieu Chartier | a61894d | 2015-04-23 16:32:54 -0700 | [diff] [blame] | 653 | CHECK(mid != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 654 | jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th); |
Mathieu Chartier | a61894d | 2015-04-23 16:32:54 -0700 | [diff] [blame] | 655 | if (exception_instance == nullptr) { |
| 656 | soa.Self()->AssertPendingOOMException(); |
| 657 | return nullptr; |
| 658 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 659 | soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance)); |
Ian Rogers | a048560 | 2014-12-02 15:48:04 -0800 | [diff] [blame] | 660 | return nullptr; |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | // Box if necessary and return. |
Ian Rogers | a048560 | 2014-12-02 15:48:04 -0800 | [diff] [blame] | 664 | return soa.AddLocalReference<jobject>(BoxPrimitive(Primitive::GetType(shorty[0]), result)); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 667 | mirror::Object* BoxPrimitive(Primitive::Type src_class, const JValue& value) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 668 | if (src_class == Primitive::kPrimNot) { |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 669 | return value.GetL(); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 670 | } |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 671 | if (src_class == Primitive::kPrimVoid) { |
| 672 | // There's no such thing as a void field, and void methods invoked via reflection return null. |
| 673 | return nullptr; |
| 674 | } |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 675 | |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 676 | jmethodID m = nullptr; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 677 | const char* shorty; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 678 | switch (src_class) { |
| 679 | case Primitive::kPrimBoolean: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 680 | m = WellKnownClasses::java_lang_Boolean_valueOf; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 681 | shorty = "LZ"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 682 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 683 | case Primitive::kPrimByte: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 684 | m = WellKnownClasses::java_lang_Byte_valueOf; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 685 | shorty = "LB"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 686 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 687 | case Primitive::kPrimChar: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 688 | m = WellKnownClasses::java_lang_Character_valueOf; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 689 | shorty = "LC"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 690 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 691 | case Primitive::kPrimDouble: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 692 | m = WellKnownClasses::java_lang_Double_valueOf; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 693 | shorty = "LD"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 694 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 695 | case Primitive::kPrimFloat: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 696 | m = WellKnownClasses::java_lang_Float_valueOf; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 697 | shorty = "LF"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 698 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 699 | case Primitive::kPrimInt: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 700 | m = WellKnownClasses::java_lang_Integer_valueOf; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 701 | shorty = "LI"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 702 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 703 | case Primitive::kPrimLong: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 704 | m = WellKnownClasses::java_lang_Long_valueOf; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 705 | shorty = "LJ"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 706 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 707 | case Primitive::kPrimShort: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 708 | m = WellKnownClasses::java_lang_Short_valueOf; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 709 | shorty = "LS"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 710 | break; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 711 | default: |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 712 | LOG(FATAL) << static_cast<int>(src_class); |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 713 | shorty = nullptr; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 716 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 717 | DCHECK_EQ(soa.Self()->GetState(), kRunnable); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 718 | |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 719 | ArgArray arg_array(shorty, 2); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 720 | JValue result; |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 721 | if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) { |
| 722 | arg_array.AppendWide(value.GetJ()); |
| 723 | } else { |
| 724 | arg_array.Append(value.GetI()); |
| 725 | } |
| 726 | |
| 727 | soa.DecodeMethod(m)->Invoke(soa.Self(), arg_array.GetArray(), arg_array.GetNumBytes(), |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 728 | &result, shorty); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 729 | return result.GetL(); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 730 | } |
| 731 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 732 | static std::string UnboxingFailureKind(ArtField* f) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 733 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 734 | if (f != nullptr) { |
Elliott Hughes | aaa5edc | 2012-05-16 15:54:30 -0700 | [diff] [blame] | 735 | return "field " + PrettyField(f, false); |
| 736 | } |
| 737 | return "result"; |
| 738 | } |
| 739 | |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 740 | static bool UnboxPrimitive(mirror::Object* o, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 741 | mirror::Class* dst_class, ArtField* f, |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 742 | JValue* unboxed_value) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 743 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 744 | bool unbox_for_result = (f == nullptr); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 745 | if (!dst_class->IsPrimitive()) { |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 746 | if (UNLIKELY(o != nullptr && !o->InstanceOf(dst_class))) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 747 | if (!unbox_for_result) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 748 | ThrowIllegalArgumentException(StringPrintf("%s has type %s, got %s", |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 749 | UnboxingFailureKind(f).c_str(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 750 | PrettyDescriptor(dst_class).c_str(), |
| 751 | PrettyTypeOf(o).c_str()).c_str()); |
| 752 | } else { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 753 | ThrowClassCastException(StringPrintf("Couldn't convert result of type %s to %s", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 754 | PrettyTypeOf(o).c_str(), |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 755 | PrettyDescriptor(dst_class).c_str()).c_str()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 756 | } |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 757 | return false; |
| 758 | } |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 759 | unboxed_value->SetL(o); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 760 | return true; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 761 | } |
| 762 | if (UNLIKELY(dst_class->GetPrimitiveType() == Primitive::kPrimVoid)) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 763 | ThrowIllegalArgumentException(StringPrintf("Can't unbox %s to void", |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 764 | UnboxingFailureKind(f).c_str()).c_str()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 765 | return false; |
| 766 | } |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 767 | if (UNLIKELY(o == nullptr)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 768 | if (!unbox_for_result) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 769 | ThrowIllegalArgumentException(StringPrintf("%s has type %s, got null", |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 770 | UnboxingFailureKind(f).c_str(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 771 | PrettyDescriptor(dst_class).c_str()).c_str()); |
| 772 | } else { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 773 | 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] | 774 | PrettyDescriptor(dst_class).c_str()).c_str()); |
| 775 | } |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 776 | return false; |
| 777 | } |
| 778 | |
Elliott Hughes | 1d878f3 | 2012-04-11 15:17:54 -0700 | [diff] [blame] | 779 | JValue boxed_value; |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 780 | mirror::Class* klass = o->GetClass(); |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 781 | mirror::Class* src_class = nullptr; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 782 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
| 783 | ArtField* primitive_field = &klass->GetIFields()[0]; |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 784 | if (klass->DescriptorEquals("Ljava/lang/Boolean;")) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 785 | src_class = class_linker->FindPrimitiveClass('Z'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 786 | boxed_value.SetZ(primitive_field->GetBoolean(o)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 787 | } else if (klass->DescriptorEquals("Ljava/lang/Byte;")) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 788 | src_class = class_linker->FindPrimitiveClass('B'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 789 | boxed_value.SetB(primitive_field->GetByte(o)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 790 | } else if (klass->DescriptorEquals("Ljava/lang/Character;")) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 791 | src_class = class_linker->FindPrimitiveClass('C'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 792 | boxed_value.SetC(primitive_field->GetChar(o)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 793 | } else if (klass->DescriptorEquals("Ljava/lang/Float;")) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 794 | src_class = class_linker->FindPrimitiveClass('F'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 795 | boxed_value.SetF(primitive_field->GetFloat(o)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 796 | } else if (klass->DescriptorEquals("Ljava/lang/Double;")) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 797 | src_class = class_linker->FindPrimitiveClass('D'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 798 | boxed_value.SetD(primitive_field->GetDouble(o)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 799 | } else if (klass->DescriptorEquals("Ljava/lang/Integer;")) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 800 | src_class = class_linker->FindPrimitiveClass('I'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 801 | boxed_value.SetI(primitive_field->GetInt(o)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 802 | } else if (klass->DescriptorEquals("Ljava/lang/Long;")) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 803 | src_class = class_linker->FindPrimitiveClass('J'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 804 | boxed_value.SetJ(primitive_field->GetLong(o)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 805 | } else if (klass->DescriptorEquals("Ljava/lang/Short;")) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 806 | src_class = class_linker->FindPrimitiveClass('S'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 807 | boxed_value.SetS(primitive_field->GetShort(o)); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 808 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 809 | std::string temp; |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 810 | ThrowIllegalArgumentException( |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 811 | StringPrintf("%s has type %s, got %s", UnboxingFailureKind(f).c_str(), |
| 812 | PrettyDescriptor(dst_class).c_str(), |
| 813 | PrettyDescriptor(o->GetClass()->GetDescriptor(&temp)).c_str()).c_str()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 814 | return false; |
| 815 | } |
| 816 | |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 817 | return ConvertPrimitiveValue(unbox_for_result, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 818 | src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(), |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 819 | boxed_value, unboxed_value); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 820 | } |
| 821 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 822 | bool UnboxPrimitiveForField(mirror::Object* o, mirror::Class* dst_class, ArtField* f, |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 823 | JValue* unboxed_value) { |
| 824 | DCHECK(f != nullptr); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 825 | return UnboxPrimitive(o, dst_class, f, unboxed_value); |
Elliott Hughes | aaa5edc | 2012-05-16 15:54:30 -0700 | [diff] [blame] | 826 | } |
| 827 | |
Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 828 | bool UnboxPrimitiveForResult(mirror::Object* o, mirror::Class* dst_class, JValue* unboxed_value) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 829 | return UnboxPrimitive(o, dst_class, nullptr, unboxed_value); |
Elliott Hughes | aaa5edc | 2012-05-16 15:54:30 -0700 | [diff] [blame] | 830 | } |
| 831 | |
Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 832 | mirror::Class* GetCallingClass(Thread* self, size_t num_frames) { |
| 833 | NthCallerVisitor visitor(self, num_frames); |
| 834 | visitor.WalkStack(); |
| 835 | return visitor.caller != nullptr ? visitor.caller->GetDeclaringClass() : nullptr; |
| 836 | } |
| 837 | |
Andreas Gampe | c0d8229 | 2014-09-23 10:38:30 -0700 | [diff] [blame] | 838 | bool VerifyAccess(Thread* self, mirror::Object* obj, mirror::Class* declaring_class, |
Mathieu Chartier | ca239af | 2015-03-29 18:27:50 -0700 | [diff] [blame] | 839 | uint32_t access_flags, mirror::Class** calling_class, size_t num_frames) { |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 840 | if ((access_flags & kAccPublic) != 0) { |
| 841 | return true; |
| 842 | } |
Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 843 | auto* klass = GetCallingClass(self, num_frames); |
| 844 | if (UNLIKELY(klass == nullptr)) { |
Vladimir Marko | 3bd7a6c | 2014-06-12 15:22:31 +0100 | [diff] [blame] | 845 | // The caller is an attached native thread. |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 846 | return false; |
Vladimir Marko | 3bd7a6c | 2014-06-12 15:22:31 +0100 | [diff] [blame] | 847 | } |
Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 848 | *calling_class = klass; |
| 849 | return VerifyAccess(self, obj, declaring_class, access_flags, klass); |
| 850 | } |
| 851 | |
| 852 | bool VerifyAccess(Thread* self, mirror::Object* obj, mirror::Class* declaring_class, |
| 853 | uint32_t access_flags, mirror::Class* calling_class) { |
| 854 | if (calling_class == declaring_class) { |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 855 | return true; |
| 856 | } |
Andreas Gampe | c0d8229 | 2014-09-23 10:38:30 -0700 | [diff] [blame] | 857 | ScopedAssertNoThreadSuspension sants(self, "verify-access"); |
Jeff Hao | cb4581a | 2014-03-28 15:43:37 -0700 | [diff] [blame] | 858 | if ((access_flags & kAccPrivate) != 0) { |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 859 | return false; |
| 860 | } |
Jeff Hao | cb4581a | 2014-03-28 15:43:37 -0700 | [diff] [blame] | 861 | if ((access_flags & kAccProtected) != 0) { |
Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 862 | if (obj != nullptr && !obj->InstanceOf(calling_class) && |
| 863 | !declaring_class->IsInSamePackage(calling_class)) { |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 864 | return false; |
Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 865 | } else if (declaring_class->IsAssignableFrom(calling_class)) { |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 866 | return true; |
| 867 | } |
| 868 | } |
Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 869 | return declaring_class->IsInSamePackage(calling_class); |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 870 | } |
| 871 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 872 | void InvalidReceiverError(mirror::Object* o, mirror::Class* c) { |
| 873 | std::string expected_class_name(PrettyDescriptor(c)); |
| 874 | std::string actual_class_name(PrettyTypeOf(o)); |
| 875 | ThrowIllegalArgumentException(StringPrintf("Expected receiver of type %s, but got %s", |
| 876 | expected_class_name.c_str(), |
| 877 | actual_class_name.c_str()).c_str()); |
| 878 | } |
| 879 | |
Jeff Hao | 83c8195 | 2015-05-27 19:29:29 -0700 | [diff] [blame] | 880 | // This only works if there's one reference which points to the object in obj. |
| 881 | // Will need to be fixed if there's cases where it's not. |
| 882 | void UpdateReference(Thread* self, jobject obj, mirror::Object* result) { |
| 883 | IndirectRef ref = reinterpret_cast<IndirectRef>(obj); |
| 884 | IndirectRefKind kind = GetIndirectRefKind(ref); |
| 885 | if (kind == kLocal) { |
| 886 | self->GetJniEnv()->locals.Update(obj, result); |
| 887 | } else if (kind == kHandleScopeOrInvalid) { |
| 888 | LOG(FATAL) << "Unsupported UpdateReference for kind kHandleScopeOrInvalid"; |
| 889 | } else if (kind == kGlobal) { |
| 890 | self->GetJniEnv()->vm->UpdateGlobal(self, ref, result); |
| 891 | } else { |
| 892 | DCHECK_EQ(kind, kWeakGlobal); |
| 893 | self->GetJniEnv()->vm->UpdateWeakGlobal(self, ref, result); |
| 894 | } |
| 895 | } |
| 896 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 897 | } // namespace art |