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