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