blob: afa49d0ab51c25186d370f4c8dc56a10bf3aa8e2 [file] [log] [blame]
Elliott Hughes418d20f2011-09-22 14:00:39 -07001/*
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 Chartier76433272014-09-26 14:32:37 -070017#include "reflection-inl.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070018
Mathieu Chartierc7853442015-03-27 14:35:38 -070019#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070021#include "base/enums.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070022#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080023#include "common_throws.h"
David Sehr9e734c72018-01-04 17:56:19 -080024#include "dex/dex_file-inl.h"
Jeff Hao39b6c242015-05-19 20:30:23 -070025#include "indirect_reference_table-inl.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010026#include "jni/java_vm_ext.h"
27#include "jni/jni_internal.h"
Andreas Gampec5b75642018-05-16 15:12:11 -070028#include "jvalue-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/class-inl.h"
Neil Fuller0e844392016-09-08 13:43:31 +010030#include "mirror/executable.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "mirror/object_array-inl.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070032#include "nativehelper/scoped_local_ref.h"
Jeff Hao11d5d8f2014-03-26 15:08:20 -070033#include "nth_caller_visitor.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070034#include "scoped_thread_state_change-inl.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010035#include "stack_reference.h"
Andreas Gampe639b2b12019-01-08 10:32:50 -080036#include "thread-inl.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070037#include "well_known_classes.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070038
Elliott Hughes418d20f2011-09-22 14:00:39 -070039namespace art {
Ian Rogers9e937be2018-02-15 17:06:58 -080040namespace {
Elliott Hughes418d20f2011-09-22 14:00:39 -070041
Andreas Gampe46ee31b2016-12-14 10:11:49 -080042using android::base::StringPrintf;
43
Ian Rogers53b8b092014-03-13 23:45:53 -070044class ArgArray {
45 public:
Roland Levillain3887c462015-08-12 18:15:42 +010046 ArgArray(const char* shorty, uint32_t shorty_len)
Ian Rogers53b8b092014-03-13 23:45:53 -070047 : 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 Chartiera59d9b22016-09-26 18:13:17 -070082 void Append(ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070083 Append(StackReference<mirror::Object>::FromMirrorPtr(obj.Ptr()).AsVRegValue());
Ian Rogers53b8b092014-03-13 23:45:53 -070084 }
85
86 void AppendWide(uint64_t value) {
Ian Rogers53b8b092014-03-13 23:45:53 -070087 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 Chartier2b7c4d12014-05-19 10:52:16 -0700104 void BuildArgArrayFromVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700105 ObjPtr<mirror::Object> receiver,
106 va_list ap)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700107 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700108 // 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 Chartier0795f232016-09-27 18:43:30 -0700125 Append(soa.Decode<mirror::Object>(va_arg(ap, jobject)));
Ian Rogers53b8b092014-03-13 23:45:53 -0700126 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 Chartier2b7c4d12014-05-19 10:52:16 -0700141 void BuildArgArrayFromJValues(const ScopedObjectAccessAlreadyRunnable& soa,
Elliott Hughes22352f32018-06-15 17:33:58 -0700142 ObjPtr<mirror::Object> receiver, const jvalue* args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700143 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700144 // 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 Rogers9e937be2018-02-15 17:06:58 -0800163 FALLTHROUGH_INTENDED;
Ian Rogers53b8b092014-03-13 23:45:53 -0700164 case 'F':
165 Append(args[args_offset].i);
166 break;
167 case 'L':
Mathieu Chartier0795f232016-09-27 18:43:30 -0700168 Append(soa.Decode<mirror::Object>(args[args_offset].l));
Ian Rogers53b8b092014-03-13 23:45:53 -0700169 break;
170 case 'D':
Ian Rogers9e937be2018-02-15 17:06:58 -0800171 FALLTHROUGH_INTENDED;
Ian Rogers53b8b092014-03-13 23:45:53 -0700172 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 Gampebdf7f1c2016-08-30 16:38:47 -0700184 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700185 // 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 Rogers1ff3c982014-08-12 02:30:58 -0700218 const char* found_descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700219 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000220 ThrowIllegalArgumentException(
Ian Rogers53b8b092014-03-13 23:45:53 -0700221 StringPrintf("Invalid primitive conversion from %s to %s", expected,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700222 PrettyDescriptor(found_descriptor).c_str()).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700223 }
224
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700225 bool BuildArgArrayFromObjectArray(ObjPtr<mirror::Object> receiver,
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000226 ObjPtr<mirror::ObjectArray<mirror::Object>> raw_args,
227 ArtMethod* m,
228 Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700229 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800230 const dex::TypeList* classes = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700231 // Set receiver if non-null (method is not static)
232 if (receiver != nullptr) {
233 Append(receiver);
234 }
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000235 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 Rogers53b8b092014-03-13 23:45:53 -0700239 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000240 arg.Assign(args->Get(args_offset));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800241 if (((shorty_[i] == 'L') && (arg != nullptr)) ||
242 ((arg == nullptr && shorty_[i] != 'L'))) {
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000243 // 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 Chartiera59d9b22016-09-26 18:13:17 -0700245 ObjPtr<mirror::Class> dst_class(
Vladimir Markob45528c2017-07-27 14:14:28 +0100246 m->ResolveClassFromTypeIndex(classes->GetTypeItem(args_offset).type_idx_));
Vladimir Markobcf17522018-06-01 13:14:32 +0100247 if (dst_class == nullptr) {
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000248 CHECK(self->IsExceptionPending());
249 return false;
250 }
Andreas Gampefa4333d2017-02-14 11:10:34 -0800251 if (UNLIKELY(arg == nullptr || !arg->InstanceOf(dst_class))) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000252 ThrowIllegalArgumentException(
Ian Rogers11e4c032014-03-14 12:00:39 -0700253 StringPrintf("method %s argument %zd has type %s, got %s",
David Sehr709b0702016-10-13 09:12:37 -0700254 m->PrettyMethod(false).c_str(),
Ian Rogers53b8b092014-03-13 23:45:53 -0700255 args_offset + 1, // Humans don't count from 0.
David Sehr709b0702016-10-13 09:12:37 -0700256 mirror::Class::PrettyDescriptor(dst_class).c_str(),
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000257 mirror::Object::PrettyTypeOf(arg.Get()).c_str()).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700258 return false;
259 }
260 }
261
262#define DO_FIRST_ARG(match_descriptor, get_fn, append) { \
Andreas Gampefa4333d2017-02-14 11:10:34 -0800263 if (LIKELY(arg != nullptr && \
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000264 arg->GetClass()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700265 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000266 append(primitive_field-> get_fn(arg.Get()));
Ian Rogers53b8b092014-03-13 23:45:53 -0700267
268#define DO_ARG(match_descriptor, get_fn, append) \
Andreas Gampefa4333d2017-02-14 11:10:34 -0800269 } else if (LIKELY(arg != nullptr && \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700270 arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700271 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000272 append(primitive_field-> get_fn(arg.Get()));
Ian Rogers53b8b092014-03-13 23:45:53 -0700273
274#define DO_FAIL(expected) \
275 } else { \
276 if (arg->GetClass<>()->IsPrimitive()) { \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700277 std::string temp; \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700278 ThrowIllegalPrimitiveArgumentException(expected, \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700279 arg->GetClass<>()->GetDescriptor(&temp)); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700280 } else { \
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000281 ThrowIllegalArgumentException(\
Ian Rogers11e4c032014-03-14 12:00:39 -0700282 StringPrintf("method %s argument %zd has type %s, got %s", \
David Sehr709b0702016-10-13 09:12:37 -0700283 ArtMethod::PrettyMethod(m, false).c_str(), \
Ian Rogers53b8b092014-03-13 23:45:53 -0700284 args_offset + 1, \
285 expected, \
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000286 mirror::Object::PrettyTypeOf(arg.Get()).c_str()).c_str()); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700287 } \
288 return false; \
289 } }
290
291 switch (shorty_[i]) {
292 case 'L':
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000293 Append(arg.Get());
Ian Rogers53b8b092014-03-13 23:45:53 -0700294 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 Rogersa0485602014-12-02 15:48:04 -0800349 UNREACHABLE();
Ian Rogers53b8b092014-03-13 23:45:53 -0700350#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 Rogers700a4022014-05-19 16:49:03 -0700366 std::unique_ptr<uint32_t[]> large_arg_array_;
Ian Rogers53b8b092014-03-13 23:45:53 -0700367};
368
Ian Rogers9e937be2018-02-15 17:06:58 -0800369void CheckMethodArguments(JavaVMExt* vm, ArtMethod* m, uint32_t* args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700370 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800371 const dex::TypeList* params = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700372 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 Rogersa0485602014-12-02 15:48:04 -0800381 // TODO: If args contain object references, it may cause problems.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700382 Thread* const self = Thread::Current();
Ian Rogers53b8b092014-03-13 23:45:53 -0700383 for (uint32_t i = 0; i < num_params; i++) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800384 dex::TypeIndex type_idx = params->GetTypeItem(i).type_idx_;
Vladimir Markob45528c2017-07-27 14:14:28 +0100385 ObjPtr<mirror::Class> param_type(m->ResolveClassFromTypeIndex(type_idx));
Ian Rogers53b8b092014-03-13 23:45:53 -0700386 if (param_type == nullptr) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700387 CHECK(self->IsExceptionPending());
388 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700389 << m->GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000390 << self->GetException()->Dump();
Ian Rogers53b8b092014-03-13 23:45:53 -0700391 self->ClearException();
392 ++error_count;
393 } else if (!param_type->IsPrimitive()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700394 // 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 Chartiera59d9b22016-09-26 18:13:17 -0700397 ObjPtr<mirror::Object> argument =
Ian Rogers68d8b422014-07-17 11:09:10 -0700398 (reinterpret_cast<StackReference<mirror::Object>*>(&args[i + offset]))->AsMirrorPtr();
Ian Rogers53b8b092014-03-13 23:45:53 -0700399 if (argument != nullptr && !argument->InstanceOf(param_type)) {
400 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
David Sehr709b0702016-10-13 09:12:37 -0700401 << argument->PrettyTypeOf() << " as argument " << (i + 1)
402 << " to " << m->PrettyMethod();
Ian Rogers53b8b092014-03-13 23:45:53 -0700403 ++error_count;
404 }
405 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
406 offset++;
Ian Rogers68d8b422014-07-17 11:09:10 -0700407 } 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 Sehr709b0702016-10-13 09:12:37 -0700412 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700413 ++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 Sehr709b0702016-10-13 09:12:37 -0700418 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700419 ++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 Sehr709b0702016-10-13 09:12:37 -0700424 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700425 ++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 Sehr709b0702016-10-13 09:12:37 -0700430 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700431 ++error_count;
432 }
433 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700434 }
435 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700436 if (UNLIKELY(error_count > 0)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700437 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
438 // with an argument.
Ian Rogers68d8b422014-07-17 11:09:10 -0700439 vm->JniAbortF(nullptr, "bad arguments passed to %s (see above for details)",
David Sehr709b0702016-10-13 09:12:37 -0700440 m->PrettyMethod().c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700441 }
442}
443
Ian Rogers9e937be2018-02-15 17:06:58 -0800444ArtMethod* FindVirtualMethod(ObjPtr<mirror::Object> receiver, ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700445 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700446 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method, kRuntimePointerSize);
Ian Rogers53b8b092014-03-13 23:45:53 -0700447}
448
449
Ian Rogers9e937be2018-02-15 17:06:58 -0800450void InvokeWithArgArray(const ScopedObjectAccessAlreadyRunnable& soa,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700451 ArtMethod* method, ArgArray* arg_array, JValue* result,
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700452 const char* shorty)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700453 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700454 uint32_t* args = arg_array->GetArray();
Ian Rogers55256cb2017-12-21 17:07:11 -0800455 if (UNLIKELY(soa.Env()->IsCheckJniEnabled())) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700456 CheckMethodArguments(soa.Vm(), method->GetInterfaceMethodIfProxy(kRuntimePointerSize), args);
Ian Rogers53b8b092014-03-13 23:45:53 -0700457 }
458 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, shorty);
459}
460
Andreas Gampe8ad7a3b2017-05-22 16:08:52 -0700461ALWAYS_INLINE
462bool CheckArgsForInvokeMethod(ArtMethod* np_method,
463 ObjPtr<mirror::ObjectArray<mirror::Object>> objects)
464 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800465 const dex::TypeList* classes = np_method->GetParameterTypeList();
Andreas Gampe8ad7a3b2017-05-22 16:08:52 -0700466 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
476ALWAYS_INLINE
477bool 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()) {
liulvping7d4a71e2020-12-30 09:46:50 +0800497 // 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 Gampe8ad7a3b2017-05-22 16:08:52 -0700516 }
Andreas Gampe8ad7a3b2017-05-22 16:08:52 -0700517 return false;
518 }
519
520 return true;
521}
522
Ian Rogers9e937be2018-02-15 17:06:58 -0800523} // anonymous namespace
524
Alex Light01fbfbe2019-06-27 10:47:04 -0700525template <>
Chris Wailes9f61e0a2021-05-12 17:16:50 -0700526NO_STACK_PROTECTOR
Alex Light01fbfbe2019-06-27 10:47:04 -0700527JValue InvokeWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
528 jobject obj,
529 ArtMethod* method,
530 va_list args) REQUIRES_SHARED(Locks::mutator_lock_) {
Dave Allison648d7112014-07-25 16:15:27 -0700531 // We want to make sure that the stack is not within a small distance from the
532 // protected region in case we are calling into a leaf function whose stack
533 // check has been elided.
534 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
535 ThrowStackOverflowError(soa.Self());
536 return JValue();
537 }
Jeff Hao39b6c242015-05-19 20:30:23 -0700538 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
539 if (is_string_init) {
540 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100541 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700542 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700543 ObjPtr<mirror::Object> receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700544 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700545 const char* shorty =
546 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700547 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700548 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700549 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700550 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700551 if (is_string_init) {
552 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700553 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700554 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700555 return result;
556}
557
Alex Light01fbfbe2019-06-27 10:47:04 -0700558template <>
Chris Wailes9f61e0a2021-05-12 17:16:50 -0700559NO_STACK_PROTECTOR
Alex Light01fbfbe2019-06-27 10:47:04 -0700560JValue InvokeWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
561 jobject obj,
562 jmethodID mid,
563 va_list args) REQUIRES_SHARED(Locks::mutator_lock_) {
564 DCHECK(mid != nullptr) << "Called with null jmethodID";
565 return InvokeWithVarArgs(soa, obj, jni::DecodeArtMethod(mid), args);
566}
567
568template <>
569JValue InvokeWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
570 jobject obj,
571 ArtMethod* method,
Elliott Hughes22352f32018-06-15 17:33:58 -0700572 const jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700573 // We want to make sure that the stack is not within a small distance from the
574 // protected region in case we are calling into a leaf function whose stack
575 // check has been elided.
576 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
577 ThrowStackOverflowError(soa.Self());
578 return JValue();
579 }
Jeff Hao39b6c242015-05-19 20:30:23 -0700580 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
581 if (is_string_init) {
582 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100583 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700584 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700585 ObjPtr<mirror::Object> receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700586 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700587 const char* shorty =
588 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700589 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700590 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700591 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700592 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700593 if (is_string_init) {
594 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700595 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700596 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700597 return result;
598}
599
Alex Light01fbfbe2019-06-27 10:47:04 -0700600template <>
601JValue InvokeWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
602 jobject obj,
603 jmethodID mid,
604 const jvalue* args) {
605 DCHECK(mid != nullptr) << "Called with null jmethodID";
606 return InvokeWithJValues(soa, obj, jni::DecodeArtMethod(mid), args);
607}
608
609template <>
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700610JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
Alex Light01fbfbe2019-06-27 10:47:04 -0700611 jobject obj,
612 ArtMethod* interface_method,
613 const jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700614 // We want to make sure that the stack is not within a small distance from the
615 // protected region in case we are calling into a leaf function whose stack
616 // check has been elided.
617 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
618 ThrowStackOverflowError(soa.Self());
619 return JValue();
620 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700621 ObjPtr<mirror::Object> receiver = soa.Decode<mirror::Object>(obj);
Alex Light01fbfbe2019-06-27 10:47:04 -0700622 ArtMethod* method = FindVirtualMethod(receiver, interface_method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700623 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
624 if (is_string_init) {
625 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100626 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700627 receiver = nullptr;
628 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700629 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700630 const char* shorty =
631 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700632 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700633 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700634 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700635 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700636 if (is_string_init) {
637 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700638 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700639 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700640 return result;
641}
642
Alex Light01fbfbe2019-06-27 10:47:04 -0700643template <>
644JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
645 jobject obj,
646 jmethodID mid,
647 const jvalue* args) {
648 DCHECK(mid != nullptr) << "Called with null jmethodID";
649 return InvokeVirtualOrInterfaceWithJValues(soa, obj, jni::DecodeArtMethod(mid), args);
650}
651
652template <>
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700653JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
Alex Light01fbfbe2019-06-27 10:47:04 -0700654 jobject obj,
655 ArtMethod* interface_method,
656 va_list args) {
Dave Allison648d7112014-07-25 16:15:27 -0700657 // We want to make sure that the stack is not within a small distance from the
658 // protected region in case we are calling into a leaf function whose stack
659 // check has been elided.
660 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
661 ThrowStackOverflowError(soa.Self());
662 return JValue();
663 }
664
Mathieu Chartier0795f232016-09-27 18:43:30 -0700665 ObjPtr<mirror::Object> receiver = soa.Decode<mirror::Object>(obj);
Alex Light01fbfbe2019-06-27 10:47:04 -0700666 ArtMethod* method = FindVirtualMethod(receiver, interface_method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700667 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
668 if (is_string_init) {
669 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100670 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700671 receiver = nullptr;
672 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700673 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700674 const char* shorty =
675 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700676 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700677 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700678 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700679 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700680 if (is_string_init) {
681 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700682 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700683 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700684 return result;
685}
686
Alex Light01fbfbe2019-06-27 10:47:04 -0700687template <>
688JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
689 jobject obj,
690 jmethodID mid,
691 va_list args) {
692 DCHECK(mid != nullptr) << "Called with null jmethodID";
693 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, jni::DecodeArtMethod(mid), args);
694}
695
liulvpingfff1d8f2020-12-21 09:43:37 +0800696template <PointerSize kPointerSize>
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700697jobject InvokeMethod(const ScopedObjectAccessAlreadyRunnable& soa, jobject javaMethod,
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700698 jobject javaReceiver, jobject javaArgs, size_t num_frames) {
Dave Allison648d7112014-07-25 16:15:27 -0700699 // We want to make sure that the stack is not within a small distance from the
700 // protected region in case we are calling into a leaf function whose stack
701 // check has been elided.
702 if (UNLIKELY(__builtin_frame_address(0) <
703 soa.Self()->GetStackEndForInterpreter(true))) {
704 ThrowStackOverflowError(soa.Self());
705 return nullptr;
706 }
707
Mathieu Chartier0795f232016-09-27 18:43:30 -0700708 ObjPtr<mirror::Executable> executable = soa.Decode<mirror::Executable>(javaMethod);
Neil Fuller0e844392016-09-08 13:43:31 +0100709 const bool accessible = executable->IsAccessible();
710 ArtMethod* m = executable->GetArtMethod();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700711
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700712 ObjPtr<mirror::Class> declaring_class = m->GetDeclaringClass();
Vladimir Markode055502019-08-02 13:41:35 +0100713 if (UNLIKELY(!declaring_class->IsVisiblyInitialized())) {
714 Thread* self = soa.Self();
715 StackHandleScope<1> hs(self);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700716 HandleWrapperObjPtr<mirror::Class> h_class(hs.NewHandleWrapper(&declaring_class));
Vladimir Markode055502019-08-02 13:41:35 +0100717 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(
718 self, h_class, /*can_init_fields=*/ true, /*can_init_parents=*/ true))) {
719 DCHECK(self->IsExceptionPending());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800720 return nullptr;
721 }
Vladimir Markode055502019-08-02 13:41:35 +0100722 DCHECK(h_class->IsInitializing());
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700723 }
724
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700725 ObjPtr<mirror::Object> receiver;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700726 if (!m->IsStatic()) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800727 // Replace calls to String.<init> with equivalent StringFactory call.
728 if (declaring_class->IsStringClass() && m->IsConstructor()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100729 m = WellKnownClasses::StringInitToStringFactory(m);
Jeff Hao848f70a2014-01-15 13:49:50 -0800730 CHECK(javaReceiver == nullptr);
731 } else {
732 // Check that the receiver is non-null and an instance of the field's declaring class.
Mathieu Chartier0795f232016-09-27 18:43:30 -0700733 receiver = soa.Decode<mirror::Object>(javaReceiver);
Jeff Hao848f70a2014-01-15 13:49:50 -0800734 if (!VerifyObjectIsClass(receiver, declaring_class)) {
735 return nullptr;
736 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700737
Jeff Hao848f70a2014-01-15 13:49:50 -0800738 // Find the actual implementation of the virtual method.
liulvpingfff1d8f2020-12-21 09:43:37 +0800739 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m, kPointerSize);
Jeff Hao848f70a2014-01-15 13:49:50 -0800740 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700741 }
742
743 // Get our arrays of arguments and their types, and check they're the same size.
Mathieu Chartier0795f232016-09-27 18:43:30 -0700744 ObjPtr<mirror::ObjectArray<mirror::Object>> objects =
745 soa.Decode<mirror::ObjectArray<mirror::Object>>(javaArgs);
liulvpingfff1d8f2020-12-21 09:43:37 +0800746 auto* np_method = m->GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampe8ad7a3b2017-05-22 16:08:52 -0700747 if (!CheckArgsForInvokeMethod(np_method, objects)) {
Ian Rogersa0485602014-12-02 15:48:04 -0800748 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700749 }
750
Jeff Haocb4581a2014-03-28 15:43:37 -0700751 // If method is not set to be accessible, verify it can be accessed by the caller.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700752 ObjPtr<mirror::Class> calling_class;
Mathieu Chartier268764d2016-09-13 12:09:38 -0700753 if (!accessible && !VerifyAccess(soa.Self(),
754 receiver,
755 declaring_class,
756 m->GetAccessFlags(),
757 &calling_class,
758 num_frames)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000759 ThrowIllegalAccessException(
Andreas Gampec0d82292014-09-23 10:38:30 -0700760 StringPrintf("Class %s cannot access %s method %s of class %s",
David Sehr709b0702016-10-13 09:12:37 -0700761 calling_class == nullptr ? "null" : calling_class->PrettyClass().c_str(),
Andreas Gampec0d82292014-09-23 10:38:30 -0700762 PrettyJavaAccessFlags(m->GetAccessFlags()).c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700763 m->PrettyMethod().c_str(),
Andreas Gampec0d82292014-09-23 10:38:30 -0700764 m->GetDeclaringClass() == nullptr ? "null" :
David Sehr709b0702016-10-13 09:12:37 -0700765 m->GetDeclaringClass()->PrettyClass().c_str()).c_str());
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700766 return nullptr;
767 }
768
Ian Rogers53b8b092014-03-13 23:45:53 -0700769 // Invoke the method.
770 JValue result;
Andreas Gampe8ad7a3b2017-05-22 16:08:52 -0700771 const char* shorty;
772 if (!InvokeMethodImpl(soa, m, np_method, receiver, objects, &shorty, &result)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700773 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700774 }
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -0700775 return soa.AddLocalReference<jobject>(BoxPrimitive(Primitive::GetType(shorty[0]), result));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700776}
777
liulvpingfff1d8f2020-12-21 09:43:37 +0800778template
779jobject InvokeMethod<PointerSize::k32>(const ScopedObjectAccessAlreadyRunnable& soa,
780 jobject javaMethod,
781 jobject javaReceiver,
782 jobject javaArgs,
783 size_t num_frames);
784template
785jobject InvokeMethod<PointerSize::k64>(const ScopedObjectAccessAlreadyRunnable& soa,
786 jobject javaMethod,
787 jobject javaReceiver,
788 jobject javaArgs,
789 size_t num_frames);
790
Andreas Gampe8ad7a3b2017-05-22 16:08:52 -0700791void InvokeConstructor(const ScopedObjectAccessAlreadyRunnable& soa,
792 ArtMethod* constructor,
793 ObjPtr<mirror::Object> receiver,
794 jobject javaArgs) {
795 // We want to make sure that the stack is not within a small distance from the
796 // protected region in case we are calling into a leaf function whose stack
797 // check has been elided.
798 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEndForInterpreter(true))) {
799 ThrowStackOverflowError(soa.Self());
800 return;
801 }
802
803 if (kIsDebugBuild) {
804 CHECK(constructor->IsConstructor());
805
806 ObjPtr<mirror::Class> declaring_class = constructor->GetDeclaringClass();
Vladimir Marko38a062e2019-08-02 13:59:27 +0100807 CHECK(declaring_class->IsInitializing());
Andreas Gampe8ad7a3b2017-05-22 16:08:52 -0700808
809 // Calls to String.<init> should have been repplaced with with equivalent StringFactory calls.
810 CHECK(!declaring_class->IsStringClass());
811
812 // Check that the receiver is non-null and an instance of the field's declaring class.
813 CHECK(receiver != nullptr);
814 CHECK(VerifyObjectIsClass(receiver, declaring_class));
815 CHECK_EQ(constructor,
816 receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(constructor,
817 kRuntimePointerSize));
818 }
819
820 // Get our arrays of arguments and their types, and check they're the same size.
821 ObjPtr<mirror::ObjectArray<mirror::Object>> objects =
822 soa.Decode<mirror::ObjectArray<mirror::Object>>(javaArgs);
823 ArtMethod* np_method = constructor->GetInterfaceMethodIfProxy(kRuntimePointerSize);
824 if (!CheckArgsForInvokeMethod(np_method, objects)) {
825 return;
826 }
827
828 // Invoke the constructor.
829 JValue result;
830 const char* shorty;
831 InvokeMethodImpl(soa, constructor, np_method, receiver, objects, &shorty, &result);
832}
833
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700834ObjPtr<mirror::Object> BoxPrimitive(Primitive::Type src_class, const JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700835 if (src_class == Primitive::kPrimNot) {
Vladimir Markod7e9bbf2019-03-28 13:18:57 +0000836 return value.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700837 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700838 if (src_class == Primitive::kPrimVoid) {
839 // There's no such thing as a void field, and void methods invoked via reflection return null.
840 return nullptr;
841 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700842
Ian Rogers84956ff2014-03-26 23:52:41 -0700843 jmethodID m = nullptr;
Ian Rogers0177e532014-02-11 16:30:46 -0800844 const char* shorty;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700845 switch (src_class) {
846 case Primitive::kPrimBoolean:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700847 m = WellKnownClasses::java_lang_Boolean_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800848 shorty = "LZ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700849 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700850 case Primitive::kPrimByte:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700851 m = WellKnownClasses::java_lang_Byte_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800852 shorty = "LB";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700853 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700854 case Primitive::kPrimChar:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700855 m = WellKnownClasses::java_lang_Character_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800856 shorty = "LC";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700857 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700858 case Primitive::kPrimDouble:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700859 m = WellKnownClasses::java_lang_Double_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800860 shorty = "LD";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700861 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700862 case Primitive::kPrimFloat:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700863 m = WellKnownClasses::java_lang_Float_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800864 shorty = "LF";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700865 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700866 case Primitive::kPrimInt:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700867 m = WellKnownClasses::java_lang_Integer_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800868 shorty = "LI";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700869 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700870 case Primitive::kPrimLong:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700871 m = WellKnownClasses::java_lang_Long_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800872 shorty = "LJ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700873 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700874 case Primitive::kPrimShort:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700875 m = WellKnownClasses::java_lang_Short_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800876 shorty = "LS";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700877 break;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700878 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700879 LOG(FATAL) << static_cast<int>(src_class);
Ian Rogers0177e532014-02-11 16:30:46 -0800880 shorty = nullptr;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700881 }
882
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700883 ScopedObjectAccessUnchecked soa(Thread::Current());
Vladimir Markoddf4fd32021-11-22 16:31:57 +0000884 DCHECK_EQ(soa.Self()->GetState(), ThreadState::kRunnable);
Jeff Hao5d917302013-02-27 17:57:33 -0800885
Ian Rogers53b8b092014-03-13 23:45:53 -0700886 ArgArray arg_array(shorty, 2);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800887 JValue result;
Jeff Hao5d917302013-02-27 17:57:33 -0800888 if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) {
889 arg_array.AppendWide(value.GetJ());
890 } else {
891 arg_array.Append(value.GetI());
892 }
893
Andreas Gampe13b27842016-11-07 16:48:23 -0800894 jni::DecodeArtMethod(m)->Invoke(soa.Self(),
895 arg_array.GetArray(),
896 arg_array.GetNumBytes(),
897 &result,
898 shorty);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800899 return result.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700900}
901
Mathieu Chartierc7853442015-03-27 14:35:38 -0700902static std::string UnboxingFailureKind(ArtField* f)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700903 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700904 if (f != nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700905 return "field " + f->PrettyField(false);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700906 }
907 return "result";
908}
909
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700910static bool UnboxPrimitive(ObjPtr<mirror::Object> o,
911 ObjPtr<mirror::Class> dst_class,
912 ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -0700913 JValue* unboxed_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700914 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700915 bool unbox_for_result = (f == nullptr);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700916 if (!dst_class->IsPrimitive()) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700917 if (UNLIKELY(o != nullptr && !o->InstanceOf(dst_class))) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800918 if (!unbox_for_result) {
David Sehr709b0702016-10-13 09:12:37 -0700919 ThrowIllegalArgumentException(
920 StringPrintf("%s has type %s, got %s",
921 UnboxingFailureKind(f).c_str(),
922 dst_class->PrettyDescriptor().c_str(),
923 o->PrettyTypeOf().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800924 } else {
David Sehr709b0702016-10-13 09:12:37 -0700925 ThrowClassCastException(
926 StringPrintf("Couldn't convert result of type %s to %s",
927 o->PrettyTypeOf().c_str(),
928 dst_class->PrettyDescriptor().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800929 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700930 return false;
931 }
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700932 unboxed_value->SetL(o);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700933 return true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800934 }
935 if (UNLIKELY(dst_class->GetPrimitiveType() == Primitive::kPrimVoid)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000936 ThrowIllegalArgumentException(StringPrintf("Can't unbox %s to void",
Ian Rogers84956ff2014-03-26 23:52:41 -0700937 UnboxingFailureKind(f).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700938 return false;
939 }
Ian Rogers84956ff2014-03-26 23:52:41 -0700940 if (UNLIKELY(o == nullptr)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800941 if (!unbox_for_result) {
David Sehr709b0702016-10-13 09:12:37 -0700942 ThrowIllegalArgumentException(
943 StringPrintf("%s has type %s, got null",
944 UnboxingFailureKind(f).c_str(),
945 dst_class->PrettyDescriptor().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800946 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700947 ThrowNullPointerException(
948 StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
David Sehr709b0702016-10-13 09:12:37 -0700949 dst_class->PrettyDescriptor().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800950 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700951 return false;
952 }
953
Elliott Hughes1d878f32012-04-11 15:17:54 -0700954 JValue boxed_value;
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700955 ObjPtr<mirror::Class> klass = o->GetClass();
Vladimir Marko9186b182018-11-06 14:55:54 +0000956 Primitive::Type primitive_type;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700957 ArtField* primitive_field = &klass->GetIFieldsPtr()->At(0);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700958 if (klass->DescriptorEquals("Ljava/lang/Boolean;")) {
Vladimir Marko9186b182018-11-06 14:55:54 +0000959 primitive_type = Primitive::kPrimBoolean;
Mathieu Chartier3398c782016-09-30 10:27:43 -0700960 boxed_value.SetZ(primitive_field->GetBoolean(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700961 } else if (klass->DescriptorEquals("Ljava/lang/Byte;")) {
Vladimir Marko9186b182018-11-06 14:55:54 +0000962 primitive_type = Primitive::kPrimByte;
Mathieu Chartier3398c782016-09-30 10:27:43 -0700963 boxed_value.SetB(primitive_field->GetByte(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700964 } else if (klass->DescriptorEquals("Ljava/lang/Character;")) {
Vladimir Marko9186b182018-11-06 14:55:54 +0000965 primitive_type = Primitive::kPrimChar;
Mathieu Chartier3398c782016-09-30 10:27:43 -0700966 boxed_value.SetC(primitive_field->GetChar(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700967 } else if (klass->DescriptorEquals("Ljava/lang/Float;")) {
Vladimir Marko9186b182018-11-06 14:55:54 +0000968 primitive_type = Primitive::kPrimFloat;
Mathieu Chartier3398c782016-09-30 10:27:43 -0700969 boxed_value.SetF(primitive_field->GetFloat(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700970 } else if (klass->DescriptorEquals("Ljava/lang/Double;")) {
Vladimir Marko9186b182018-11-06 14:55:54 +0000971 primitive_type = Primitive::kPrimDouble;
Mathieu Chartier3398c782016-09-30 10:27:43 -0700972 boxed_value.SetD(primitive_field->GetDouble(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700973 } else if (klass->DescriptorEquals("Ljava/lang/Integer;")) {
Vladimir Marko9186b182018-11-06 14:55:54 +0000974 primitive_type = Primitive::kPrimInt;
Mathieu Chartier3398c782016-09-30 10:27:43 -0700975 boxed_value.SetI(primitive_field->GetInt(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700976 } else if (klass->DescriptorEquals("Ljava/lang/Long;")) {
Vladimir Marko9186b182018-11-06 14:55:54 +0000977 primitive_type = Primitive::kPrimLong;
Mathieu Chartier3398c782016-09-30 10:27:43 -0700978 boxed_value.SetJ(primitive_field->GetLong(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700979 } else if (klass->DescriptorEquals("Ljava/lang/Short;")) {
Vladimir Marko9186b182018-11-06 14:55:54 +0000980 primitive_type = Primitive::kPrimShort;
Mathieu Chartier3398c782016-09-30 10:27:43 -0700981 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700982 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700983 std::string temp;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000984 ThrowIllegalArgumentException(
Ian Rogers1ff3c982014-08-12 02:30:58 -0700985 StringPrintf("%s has type %s, got %s", UnboxingFailureKind(f).c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700986 dst_class->PrettyDescriptor().c_str(),
Ian Rogers1ff3c982014-08-12 02:30:58 -0700987 PrettyDescriptor(o->GetClass()->GetDescriptor(&temp)).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700988 return false;
989 }
990
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000991 return ConvertPrimitiveValue(unbox_for_result,
Vladimir Marko9186b182018-11-06 14:55:54 +0000992 primitive_type,
993 dst_class->GetPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700994 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700995}
996
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700997bool UnboxPrimitiveForField(ObjPtr<mirror::Object> o,
998 ObjPtr<mirror::Class> dst_class,
999 ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -07001000 JValue* unboxed_value) {
1001 DCHECK(f != nullptr);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001002 return UnboxPrimitive(o, dst_class, f, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -07001003}
1004
Mathieu Chartiera59d9b22016-09-26 18:13:17 -07001005bool UnboxPrimitiveForResult(ObjPtr<mirror::Object> o,
1006 ObjPtr<mirror::Class> dst_class,
1007 JValue* unboxed_value) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001008 return UnboxPrimitive(o, dst_class, nullptr, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -07001009}
1010
Mathieu Chartiera59d9b22016-09-26 18:13:17 -07001011ObjPtr<mirror::Class> GetCallingClass(Thread* self, size_t num_frames) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -07001012 NthCallerVisitor visitor(self, num_frames);
1013 visitor.WalkStack();
1014 return visitor.caller != nullptr ? visitor.caller->GetDeclaringClass() : nullptr;
1015}
1016
Mathieu Chartiera59d9b22016-09-26 18:13:17 -07001017bool VerifyAccess(Thread* self,
1018 ObjPtr<mirror::Object> obj,
1019 ObjPtr<mirror::Class> declaring_class,
1020 uint32_t access_flags,
1021 ObjPtr<mirror::Class>* calling_class,
1022 size_t num_frames) {
Mathieu Chartier76433272014-09-26 14:32:37 -07001023 if ((access_flags & kAccPublic) != 0) {
1024 return true;
1025 }
Mathieu Chartiera59d9b22016-09-26 18:13:17 -07001026 ObjPtr<mirror::Class> klass = GetCallingClass(self, num_frames);
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -07001027 if (UNLIKELY(klass == nullptr)) {
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +01001028 // The caller is an attached native thread.
Mathieu Chartier76433272014-09-26 14:32:37 -07001029 return false;
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +01001030 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -07001031 *calling_class = klass;
Mathieu Chartier268764d2016-09-13 12:09:38 -07001032 return VerifyAccess(obj, declaring_class, access_flags, klass);
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -07001033}
1034
Mathieu Chartiera59d9b22016-09-26 18:13:17 -07001035bool VerifyAccess(ObjPtr<mirror::Object> obj,
1036 ObjPtr<mirror::Class> declaring_class,
Mathieu Chartier268764d2016-09-13 12:09:38 -07001037 uint32_t access_flags,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -07001038 ObjPtr<mirror::Class> calling_class) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -07001039 if (calling_class == declaring_class) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -07001040 return true;
1041 }
Mathieu Chartier268764d2016-09-13 12:09:38 -07001042 ScopedAssertNoThreadSuspension sants("verify-access");
Jeff Haocb4581a2014-03-28 15:43:37 -07001043 if ((access_flags & kAccPrivate) != 0) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -07001044 return false;
1045 }
Jeff Haocb4581a2014-03-28 15:43:37 -07001046 if ((access_flags & kAccProtected) != 0) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -07001047 if (obj != nullptr && !obj->InstanceOf(calling_class) &&
Mathieu Chartier3398c782016-09-30 10:27:43 -07001048 !declaring_class->IsInSamePackage(calling_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -07001049 return false;
Mathieu Chartier3398c782016-09-30 10:27:43 -07001050 } else if (declaring_class->IsAssignableFrom(calling_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -07001051 return true;
1052 }
1053 }
Mathieu Chartier3398c782016-09-30 10:27:43 -07001054 return declaring_class->IsInSamePackage(calling_class);
Jeff Hao11d5d8f2014-03-26 15:08:20 -07001055}
1056
Mathieu Chartiera59d9b22016-09-26 18:13:17 -07001057void InvalidReceiverError(ObjPtr<mirror::Object> o, ObjPtr<mirror::Class> c) {
David Sehr709b0702016-10-13 09:12:37 -07001058 std::string expected_class_name(mirror::Class::PrettyDescriptor(c));
1059 std::string actual_class_name(mirror::Object::PrettyTypeOf(o));
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001060 ThrowIllegalArgumentException(StringPrintf("Expected receiver of type %s, but got %s",
1061 expected_class_name.c_str(),
1062 actual_class_name.c_str()).c_str());
1063}
1064
Jeff Hao83c81952015-05-27 19:29:29 -07001065// This only works if there's one reference which points to the object in obj.
1066// Will need to be fixed if there's cases where it's not.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -07001067void UpdateReference(Thread* self, jobject obj, ObjPtr<mirror::Object> result) {
Jeff Hao83c81952015-05-27 19:29:29 -07001068 IndirectRef ref = reinterpret_cast<IndirectRef>(obj);
Andreas Gampedc061d02016-10-24 13:19:37 -07001069 IndirectRefKind kind = IndirectReferenceTable::GetIndirectRefKind(ref);
Jeff Hao83c81952015-05-27 19:29:29 -07001070 if (kind == kLocal) {
Ian Rogers55256cb2017-12-21 17:07:11 -08001071 self->GetJniEnv()->UpdateLocal(obj, result);
Vladimir Markocedec9d2021-02-08 16:16:13 +00001072 } else if (kind == kJniTransitionOrInvalid) {
1073 LOG(FATAL) << "Unsupported UpdateReference for kind kJniTransitionOrInvalid";
Jeff Hao83c81952015-05-27 19:29:29 -07001074 } else if (kind == kGlobal) {
Ian Rogers55256cb2017-12-21 17:07:11 -08001075 self->GetJniEnv()->GetVm()->UpdateGlobal(self, ref, result);
Jeff Hao83c81952015-05-27 19:29:29 -07001076 } else {
1077 DCHECK_EQ(kind, kWeakGlobal);
Ian Rogers55256cb2017-12-21 17:07:11 -08001078 self->GetJniEnv()->GetVm()->UpdateWeakGlobal(self, ref, result);
Jeff Hao83c81952015-05-27 19:29:29 -07001079 }
1080}
1081
Elliott Hughes418d20f2011-09-22 14:00:39 -07001082} // namespace art