blob: 80118365f05d9477b90977e725dc10ecdce657ca [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"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070036#include "well_known_classes.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070037
Elliott Hughes418d20f2011-09-22 14:00:39 -070038namespace art {
Ian Rogers9e937be2018-02-15 17:06:58 -080039namespace {
Elliott Hughes418d20f2011-09-22 14:00:39 -070040
Andreas Gampe46ee31b2016-12-14 10:11:49 -080041using android::base::StringPrintf;
42
Ian Rogers53b8b092014-03-13 23:45:53 -070043class ArgArray {
44 public:
Roland Levillain3887c462015-08-12 18:15:42 +010045 ArgArray(const char* shorty, uint32_t shorty_len)
Ian Rogers53b8b092014-03-13 23:45:53 -070046 : shorty_(shorty), shorty_len_(shorty_len), num_bytes_(0) {
47 size_t num_slots = shorty_len + 1; // +1 in case of receiver.
48 if (LIKELY((num_slots * 2) < kSmallArgArraySize)) {
49 // We can trivially use the small arg array.
50 arg_array_ = small_arg_array_;
51 } else {
52 // Analyze shorty to see if we need the large arg array.
53 for (size_t i = 1; i < shorty_len; ++i) {
54 char c = shorty[i];
55 if (c == 'J' || c == 'D') {
56 num_slots++;
57 }
58 }
59 if (num_slots <= kSmallArgArraySize) {
60 arg_array_ = small_arg_array_;
61 } else {
62 large_arg_array_.reset(new uint32_t[num_slots]);
63 arg_array_ = large_arg_array_.get();
64 }
65 }
66 }
67
68 uint32_t* GetArray() {
69 return arg_array_;
70 }
71
72 uint32_t GetNumBytes() {
73 return num_bytes_;
74 }
75
76 void Append(uint32_t value) {
77 arg_array_[num_bytes_ / 4] = value;
78 num_bytes_ += 4;
79 }
80
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070081 void Append(ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070082 Append(StackReference<mirror::Object>::FromMirrorPtr(obj.Ptr()).AsVRegValue());
Ian Rogers53b8b092014-03-13 23:45:53 -070083 }
84
85 void AppendWide(uint64_t value) {
Ian Rogers53b8b092014-03-13 23:45:53 -070086 arg_array_[num_bytes_ / 4] = value;
87 arg_array_[(num_bytes_ / 4) + 1] = value >> 32;
88 num_bytes_ += 8;
89 }
90
91 void AppendFloat(float value) {
92 jvalue jv;
93 jv.f = value;
94 Append(jv.i);
95 }
96
97 void AppendDouble(double value) {
98 jvalue jv;
99 jv.d = value;
100 AppendWide(jv.j);
101 }
102
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700103 void BuildArgArrayFromVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700104 ObjPtr<mirror::Object> receiver,
105 va_list ap)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700106 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700107 // Set receiver if non-null (method is not static)
108 if (receiver != nullptr) {
109 Append(receiver);
110 }
111 for (size_t i = 1; i < shorty_len_; ++i) {
112 switch (shorty_[i]) {
113 case 'Z':
114 case 'B':
115 case 'C':
116 case 'S':
117 case 'I':
118 Append(va_arg(ap, jint));
119 break;
120 case 'F':
121 AppendFloat(va_arg(ap, jdouble));
122 break;
123 case 'L':
Mathieu Chartier0795f232016-09-27 18:43:30 -0700124 Append(soa.Decode<mirror::Object>(va_arg(ap, jobject)));
Ian Rogers53b8b092014-03-13 23:45:53 -0700125 break;
126 case 'D':
127 AppendDouble(va_arg(ap, jdouble));
128 break;
129 case 'J':
130 AppendWide(va_arg(ap, jlong));
131 break;
132#ifndef NDEBUG
133 default:
134 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
135#endif
136 }
137 }
138 }
139
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700140 void BuildArgArrayFromJValues(const ScopedObjectAccessAlreadyRunnable& soa,
Elliott Hughes22352f32018-06-15 17:33:58 -0700141 ObjPtr<mirror::Object> receiver, const jvalue* args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700142 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700143 // Set receiver if non-null (method is not static)
144 if (receiver != nullptr) {
145 Append(receiver);
146 }
147 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
148 switch (shorty_[i]) {
149 case 'Z':
150 Append(args[args_offset].z);
151 break;
152 case 'B':
153 Append(args[args_offset].b);
154 break;
155 case 'C':
156 Append(args[args_offset].c);
157 break;
158 case 'S':
159 Append(args[args_offset].s);
160 break;
161 case 'I':
Ian Rogers9e937be2018-02-15 17:06:58 -0800162 FALLTHROUGH_INTENDED;
Ian Rogers53b8b092014-03-13 23:45:53 -0700163 case 'F':
164 Append(args[args_offset].i);
165 break;
166 case 'L':
Mathieu Chartier0795f232016-09-27 18:43:30 -0700167 Append(soa.Decode<mirror::Object>(args[args_offset].l));
Ian Rogers53b8b092014-03-13 23:45:53 -0700168 break;
169 case 'D':
Ian Rogers9e937be2018-02-15 17:06:58 -0800170 FALLTHROUGH_INTENDED;
Ian Rogers53b8b092014-03-13 23:45:53 -0700171 case 'J':
172 AppendWide(args[args_offset].j);
173 break;
174#ifndef NDEBUG
175 default:
176 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
177#endif
178 }
179 }
180 }
181
182 void BuildArgArrayFromFrame(ShadowFrame* shadow_frame, uint32_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700183 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700184 // Set receiver if non-null (method is not static)
185 size_t cur_arg = arg_offset;
186 if (!shadow_frame->GetMethod()->IsStatic()) {
187 Append(shadow_frame->GetVReg(cur_arg));
188 cur_arg++;
189 }
190 for (size_t i = 1; i < shorty_len_; ++i) {
191 switch (shorty_[i]) {
192 case 'Z':
193 case 'B':
194 case 'C':
195 case 'S':
196 case 'I':
197 case 'F':
198 case 'L':
199 Append(shadow_frame->GetVReg(cur_arg));
200 cur_arg++;
201 break;
202 case 'D':
203 case 'J':
204 AppendWide(shadow_frame->GetVRegLong(cur_arg));
205 cur_arg++;
206 cur_arg++;
207 break;
208#ifndef NDEBUG
209 default:
210 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
211#endif
212 }
213 }
214 }
215
216 static void ThrowIllegalPrimitiveArgumentException(const char* expected,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700217 const char* found_descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700218 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000219 ThrowIllegalArgumentException(
Ian Rogers53b8b092014-03-13 23:45:53 -0700220 StringPrintf("Invalid primitive conversion from %s to %s", expected,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700221 PrettyDescriptor(found_descriptor).c_str()).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700222 }
223
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700224 bool BuildArgArrayFromObjectArray(ObjPtr<mirror::Object> receiver,
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000225 ObjPtr<mirror::ObjectArray<mirror::Object>> raw_args,
226 ArtMethod* m,
227 Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700228 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700229 const DexFile::TypeList* classes = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700230 // Set receiver if non-null (method is not static)
231 if (receiver != nullptr) {
232 Append(receiver);
233 }
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000234 StackHandleScope<2> hs(self);
235 MutableHandle<mirror::Object> arg(hs.NewHandle<mirror::Object>(nullptr));
236 Handle<mirror::ObjectArray<mirror::Object>> args(
237 hs.NewHandle<mirror::ObjectArray<mirror::Object>>(raw_args));
Ian Rogers53b8b092014-03-13 23:45:53 -0700238 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000239 arg.Assign(args->Get(args_offset));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800240 if (((shorty_[i] == 'L') && (arg != nullptr)) ||
241 ((arg == nullptr && shorty_[i] != 'L'))) {
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000242 // TODO: The method's parameter's type must have been previously resolved, yet
243 // we've seen cases where it's not b/34440020.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700244 ObjPtr<mirror::Class> dst_class(
Vladimir Markob45528c2017-07-27 14:14:28 +0100245 m->ResolveClassFromTypeIndex(classes->GetTypeItem(args_offset).type_idx_));
Vladimir Markobcf17522018-06-01 13:14:32 +0100246 if (dst_class == nullptr) {
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000247 CHECK(self->IsExceptionPending());
248 return false;
249 }
Andreas Gampefa4333d2017-02-14 11:10:34 -0800250 if (UNLIKELY(arg == nullptr || !arg->InstanceOf(dst_class))) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000251 ThrowIllegalArgumentException(
Ian Rogers11e4c032014-03-14 12:00:39 -0700252 StringPrintf("method %s argument %zd has type %s, got %s",
David Sehr709b0702016-10-13 09:12:37 -0700253 m->PrettyMethod(false).c_str(),
Ian Rogers53b8b092014-03-13 23:45:53 -0700254 args_offset + 1, // Humans don't count from 0.
David Sehr709b0702016-10-13 09:12:37 -0700255 mirror::Class::PrettyDescriptor(dst_class).c_str(),
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000256 mirror::Object::PrettyTypeOf(arg.Get()).c_str()).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700257 return false;
258 }
259 }
260
261#define DO_FIRST_ARG(match_descriptor, get_fn, append) { \
Andreas Gampefa4333d2017-02-14 11:10:34 -0800262 if (LIKELY(arg != nullptr && \
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000263 arg->GetClass()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700264 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000265 append(primitive_field-> get_fn(arg.Get()));
Ian Rogers53b8b092014-03-13 23:45:53 -0700266
267#define DO_ARG(match_descriptor, get_fn, append) \
Andreas Gampefa4333d2017-02-14 11:10:34 -0800268 } else if (LIKELY(arg != nullptr && \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700269 arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700270 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000271 append(primitive_field-> get_fn(arg.Get()));
Ian Rogers53b8b092014-03-13 23:45:53 -0700272
273#define DO_FAIL(expected) \
274 } else { \
275 if (arg->GetClass<>()->IsPrimitive()) { \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700276 std::string temp; \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700277 ThrowIllegalPrimitiveArgumentException(expected, \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700278 arg->GetClass<>()->GetDescriptor(&temp)); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700279 } else { \
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000280 ThrowIllegalArgumentException(\
Ian Rogers11e4c032014-03-14 12:00:39 -0700281 StringPrintf("method %s argument %zd has type %s, got %s", \
David Sehr709b0702016-10-13 09:12:37 -0700282 ArtMethod::PrettyMethod(m, false).c_str(), \
Ian Rogers53b8b092014-03-13 23:45:53 -0700283 args_offset + 1, \
284 expected, \
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000285 mirror::Object::PrettyTypeOf(arg.Get()).c_str()).c_str()); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700286 } \
287 return false; \
288 } }
289
290 switch (shorty_[i]) {
291 case 'L':
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000292 Append(arg.Get());
Ian Rogers53b8b092014-03-13 23:45:53 -0700293 break;
294 case 'Z':
295 DO_FIRST_ARG("Ljava/lang/Boolean;", GetBoolean, Append)
296 DO_FAIL("boolean")
297 break;
298 case 'B':
299 DO_FIRST_ARG("Ljava/lang/Byte;", GetByte, Append)
300 DO_FAIL("byte")
301 break;
302 case 'C':
303 DO_FIRST_ARG("Ljava/lang/Character;", GetChar, Append)
304 DO_FAIL("char")
305 break;
306 case 'S':
307 DO_FIRST_ARG("Ljava/lang/Short;", GetShort, Append)
308 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
309 DO_FAIL("short")
310 break;
311 case 'I':
312 DO_FIRST_ARG("Ljava/lang/Integer;", GetInt, Append)
313 DO_ARG("Ljava/lang/Character;", GetChar, Append)
314 DO_ARG("Ljava/lang/Short;", GetShort, Append)
315 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
316 DO_FAIL("int")
317 break;
318 case 'J':
319 DO_FIRST_ARG("Ljava/lang/Long;", GetLong, AppendWide)
320 DO_ARG("Ljava/lang/Integer;", GetInt, AppendWide)
321 DO_ARG("Ljava/lang/Character;", GetChar, AppendWide)
322 DO_ARG("Ljava/lang/Short;", GetShort, AppendWide)
323 DO_ARG("Ljava/lang/Byte;", GetByte, AppendWide)
324 DO_FAIL("long")
325 break;
326 case 'F':
327 DO_FIRST_ARG("Ljava/lang/Float;", GetFloat, AppendFloat)
328 DO_ARG("Ljava/lang/Long;", GetLong, AppendFloat)
329 DO_ARG("Ljava/lang/Integer;", GetInt, AppendFloat)
330 DO_ARG("Ljava/lang/Character;", GetChar, AppendFloat)
331 DO_ARG("Ljava/lang/Short;", GetShort, AppendFloat)
332 DO_ARG("Ljava/lang/Byte;", GetByte, AppendFloat)
333 DO_FAIL("float")
334 break;
335 case 'D':
336 DO_FIRST_ARG("Ljava/lang/Double;", GetDouble, AppendDouble)
337 DO_ARG("Ljava/lang/Float;", GetFloat, AppendDouble)
338 DO_ARG("Ljava/lang/Long;", GetLong, AppendDouble)
339 DO_ARG("Ljava/lang/Integer;", GetInt, AppendDouble)
340 DO_ARG("Ljava/lang/Character;", GetChar, AppendDouble)
341 DO_ARG("Ljava/lang/Short;", GetShort, AppendDouble)
342 DO_ARG("Ljava/lang/Byte;", GetByte, AppendDouble)
343 DO_FAIL("double")
344 break;
345#ifndef NDEBUG
346 default:
347 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
Ian Rogersa0485602014-12-02 15:48:04 -0800348 UNREACHABLE();
Ian Rogers53b8b092014-03-13 23:45:53 -0700349#endif
350 }
351#undef DO_FIRST_ARG
352#undef DO_ARG
353#undef DO_FAIL
354 }
355 return true;
356 }
357
358 private:
359 enum { kSmallArgArraySize = 16 };
360 const char* const shorty_;
361 const uint32_t shorty_len_;
362 uint32_t num_bytes_;
363 uint32_t* arg_array_;
364 uint32_t small_arg_array_[kSmallArgArraySize];
Ian Rogers700a4022014-05-19 16:49:03 -0700365 std::unique_ptr<uint32_t[]> large_arg_array_;
Ian Rogers53b8b092014-03-13 23:45:53 -0700366};
367
Ian Rogers9e937be2018-02-15 17:06:58 -0800368void CheckMethodArguments(JavaVMExt* vm, ArtMethod* m, uint32_t* args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700369 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700370 const DexFile::TypeList* params = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700371 if (params == nullptr) {
372 return; // No arguments so nothing to check.
373 }
374 uint32_t offset = 0;
375 uint32_t num_params = params->Size();
376 size_t error_count = 0;
377 if (!m->IsStatic()) {
378 offset = 1;
379 }
Ian Rogersa0485602014-12-02 15:48:04 -0800380 // TODO: If args contain object references, it may cause problems.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700381 Thread* const self = Thread::Current();
Ian Rogers53b8b092014-03-13 23:45:53 -0700382 for (uint32_t i = 0; i < num_params; i++) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800383 dex::TypeIndex type_idx = params->GetTypeItem(i).type_idx_;
Vladimir Markob45528c2017-07-27 14:14:28 +0100384 ObjPtr<mirror::Class> param_type(m->ResolveClassFromTypeIndex(type_idx));
Ian Rogers53b8b092014-03-13 23:45:53 -0700385 if (param_type == nullptr) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700386 CHECK(self->IsExceptionPending());
387 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700388 << m->GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000389 << self->GetException()->Dump();
Ian Rogers53b8b092014-03-13 23:45:53 -0700390 self->ClearException();
391 ++error_count;
392 } else if (!param_type->IsPrimitive()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700393 // TODO: There is a compaction bug here since GetClassFromTypeIdx can cause thread suspension,
394 // this is a hard to fix problem since the args can contain Object*, we need to save and
395 // restore them by using a visitor similar to the ones used in the trampoline entrypoints.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700396 ObjPtr<mirror::Object> argument =
Ian Rogers68d8b422014-07-17 11:09:10 -0700397 (reinterpret_cast<StackReference<mirror::Object>*>(&args[i + offset]))->AsMirrorPtr();
Ian Rogers53b8b092014-03-13 23:45:53 -0700398 if (argument != nullptr && !argument->InstanceOf(param_type)) {
399 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
David Sehr709b0702016-10-13 09:12:37 -0700400 << argument->PrettyTypeOf() << " as argument " << (i + 1)
401 << " to " << m->PrettyMethod();
Ian Rogers53b8b092014-03-13 23:45:53 -0700402 ++error_count;
403 }
404 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
405 offset++;
Ian Rogers68d8b422014-07-17 11:09:10 -0700406 } else {
407 int32_t arg = static_cast<int32_t>(args[i + offset]);
408 if (param_type->IsPrimitiveBoolean()) {
409 if (arg != JNI_TRUE && arg != JNI_FALSE) {
410 LOG(ERROR) << "JNI ERROR (app bug): expected jboolean (0/1) but got value of "
David Sehr709b0702016-10-13 09:12:37 -0700411 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700412 ++error_count;
413 }
414 } else if (param_type->IsPrimitiveByte()) {
415 if (arg < -128 || arg > 127) {
416 LOG(ERROR) << "JNI ERROR (app bug): expected jbyte but got value of "
David Sehr709b0702016-10-13 09:12:37 -0700417 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700418 ++error_count;
419 }
420 } else if (param_type->IsPrimitiveChar()) {
421 if (args[i + offset] > 0xFFFF) {
422 LOG(ERROR) << "JNI ERROR (app bug): expected jchar but got value of "
David Sehr709b0702016-10-13 09:12:37 -0700423 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700424 ++error_count;
425 }
426 } else if (param_type->IsPrimitiveShort()) {
427 if (arg < -32768 || arg > 0x7FFF) {
428 LOG(ERROR) << "JNI ERROR (app bug): expected jshort but got value of "
David Sehr709b0702016-10-13 09:12:37 -0700429 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700430 ++error_count;
431 }
432 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700433 }
434 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700435 if (UNLIKELY(error_count > 0)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700436 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
437 // with an argument.
Ian Rogers68d8b422014-07-17 11:09:10 -0700438 vm->JniAbortF(nullptr, "bad arguments passed to %s (see above for details)",
David Sehr709b0702016-10-13 09:12:37 -0700439 m->PrettyMethod().c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700440 }
441}
442
Ian Rogers9e937be2018-02-15 17:06:58 -0800443ArtMethod* FindVirtualMethod(ObjPtr<mirror::Object> receiver, ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700444 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700445 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method, kRuntimePointerSize);
Ian Rogers53b8b092014-03-13 23:45:53 -0700446}
447
448
Ian Rogers9e937be2018-02-15 17:06:58 -0800449void InvokeWithArgArray(const ScopedObjectAccessAlreadyRunnable& soa,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700450 ArtMethod* method, ArgArray* arg_array, JValue* result,
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700451 const char* shorty)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700452 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700453 uint32_t* args = arg_array->GetArray();
Ian Rogers55256cb2017-12-21 17:07:11 -0800454 if (UNLIKELY(soa.Env()->IsCheckJniEnabled())) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700455 CheckMethodArguments(soa.Vm(), method->GetInterfaceMethodIfProxy(kRuntimePointerSize), args);
Ian Rogers53b8b092014-03-13 23:45:53 -0700456 }
457 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, shorty);
458}
459
Andreas Gampe8ad7a3b2017-05-22 16:08:52 -0700460ALWAYS_INLINE
461bool CheckArgsForInvokeMethod(ArtMethod* np_method,
462 ObjPtr<mirror::ObjectArray<mirror::Object>> objects)
463 REQUIRES_SHARED(Locks::mutator_lock_) {
464 const DexFile::TypeList* classes = np_method->GetParameterTypeList();
465 uint32_t classes_size = (classes == nullptr) ? 0 : classes->Size();
466 uint32_t arg_count = (objects == nullptr) ? 0 : objects->GetLength();
467 if (UNLIKELY(arg_count != classes_size)) {
468 ThrowIllegalArgumentException(StringPrintf("Wrong number of arguments; expected %d, got %d",
469 classes_size, arg_count).c_str());
470 return false;
471 }
472 return true;
473}
474
475ALWAYS_INLINE
476bool InvokeMethodImpl(const ScopedObjectAccessAlreadyRunnable& soa,
477 ArtMethod* m,
478 ArtMethod* np_method,
479 ObjPtr<mirror::Object> receiver,
480 ObjPtr<mirror::ObjectArray<mirror::Object>> objects,
481 const char** shorty,
482 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
483 // Invoke the method.
484 uint32_t shorty_len = 0;
485 *shorty = np_method->GetShorty(&shorty_len);
486 ArgArray arg_array(*shorty, shorty_len);
487 if (!arg_array.BuildArgArrayFromObjectArray(receiver, objects, np_method, soa.Self())) {
488 CHECK(soa.Self()->IsExceptionPending());
489 return false;
490 }
491
492 InvokeWithArgArray(soa, m, &arg_array, result, *shorty);
493
494 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
495 if (soa.Self()->IsExceptionPending()) {
496 // If we get another exception when we are trying to wrap, then just use that instead.
497 ScopedLocalRef<jthrowable> th(soa.Env(), soa.Env()->ExceptionOccurred());
498 soa.Self()->ClearException();
499 jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException");
500 if (exception_class == nullptr) {
501 soa.Self()->AssertPendingException();
502 return false;
503 }
504 jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
505 CHECK(mid != nullptr);
506 jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th.get());
507 if (exception_instance == nullptr) {
508 soa.Self()->AssertPendingException();
509 return false;
510 }
511 soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance));
512 return false;
513 }
514
515 return true;
516}
517
Ian Rogers9e937be2018-02-15 17:06:58 -0800518} // anonymous namespace
519
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700520JValue InvokeWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
521 va_list args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700522 REQUIRES_SHARED(Locks::mutator_lock_) {
Dave Allison648d7112014-07-25 16:15:27 -0700523 // We want to make sure that the stack is not within a small distance from the
524 // protected region in case we are calling into a leaf function whose stack
525 // check has been elided.
526 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
527 ThrowStackOverflowError(soa.Self());
528 return JValue();
529 }
530
Andreas Gampe13b27842016-11-07 16:48:23 -0800531 ArtMethod* method = jni::DecodeArtMethod(mid);
Jeff Hao39b6c242015-05-19 20:30:23 -0700532 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
533 if (is_string_init) {
534 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100535 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700536 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700537 ObjPtr<mirror::Object> receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700538 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700539 const char* shorty =
540 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700541 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700542 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700543 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700544 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700545 if (is_string_init) {
546 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700547 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700548 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700549 return result;
550}
551
Jeff Hao39b6c242015-05-19 20:30:23 -0700552JValue InvokeWithJValues(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
Elliott Hughes22352f32018-06-15 17:33:58 -0700553 const jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700554 // We want to make sure that the stack is not within a small distance from the
555 // protected region in case we are calling into a leaf function whose stack
556 // check has been elided.
557 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
558 ThrowStackOverflowError(soa.Self());
559 return JValue();
560 }
561
Andreas Gampe13b27842016-11-07 16:48:23 -0800562 ArtMethod* method = jni::DecodeArtMethod(mid);
Jeff Hao39b6c242015-05-19 20:30:23 -0700563 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
564 if (is_string_init) {
565 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100566 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700567 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700568 ObjPtr<mirror::Object> receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700569 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700570 const char* shorty =
571 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700572 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700573 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700574 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700575 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700576 if (is_string_init) {
577 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700578 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700579 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700580 return result;
581}
582
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700583JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
Elliott Hughes22352f32018-06-15 17:33:58 -0700584 jobject obj, jmethodID mid, const jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700585 // We want to make sure that the stack is not within a small distance from the
586 // protected region in case we are calling into a leaf function whose stack
587 // check has been elided.
588 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
589 ThrowStackOverflowError(soa.Self());
590 return JValue();
591 }
592
Mathieu Chartier0795f232016-09-27 18:43:30 -0700593 ObjPtr<mirror::Object> receiver = soa.Decode<mirror::Object>(obj);
Andreas Gampe13b27842016-11-07 16:48:23 -0800594 ArtMethod* method = FindVirtualMethod(receiver, jni::DecodeArtMethod(mid));
Jeff Hao39b6c242015-05-19 20:30:23 -0700595 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
596 if (is_string_init) {
597 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100598 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700599 receiver = nullptr;
600 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700601 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700602 const char* shorty =
603 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700604 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700605 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700606 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700607 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700608 if (is_string_init) {
609 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700610 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700611 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700612 return result;
613}
614
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700615JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
Ian Rogers53b8b092014-03-13 23:45:53 -0700616 jobject obj, jmethodID mid, va_list args) {
Dave Allison648d7112014-07-25 16:15:27 -0700617 // We want to make sure that the stack is not within a small distance from the
618 // protected region in case we are calling into a leaf function whose stack
619 // check has been elided.
620 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
621 ThrowStackOverflowError(soa.Self());
622 return JValue();
623 }
624
Mathieu Chartier0795f232016-09-27 18:43:30 -0700625 ObjPtr<mirror::Object> receiver = soa.Decode<mirror::Object>(obj);
Andreas Gampe13b27842016-11-07 16:48:23 -0800626 ArtMethod* method = FindVirtualMethod(receiver, jni::DecodeArtMethod(mid));
Jeff Hao39b6c242015-05-19 20:30:23 -0700627 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
628 if (is_string_init) {
629 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100630 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700631 receiver = nullptr;
632 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700633 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700634 const char* shorty =
635 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700636 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700637 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700638 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700639 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700640 if (is_string_init) {
641 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700642 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700643 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700644 return result;
645}
646
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700647jobject InvokeMethod(const ScopedObjectAccessAlreadyRunnable& soa, jobject javaMethod,
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700648 jobject javaReceiver, jobject javaArgs, size_t num_frames) {
Dave Allison648d7112014-07-25 16:15:27 -0700649 // We want to make sure that the stack is not within a small distance from the
650 // protected region in case we are calling into a leaf function whose stack
651 // check has been elided.
652 if (UNLIKELY(__builtin_frame_address(0) <
653 soa.Self()->GetStackEndForInterpreter(true))) {
654 ThrowStackOverflowError(soa.Self());
655 return nullptr;
656 }
657
Mathieu Chartier0795f232016-09-27 18:43:30 -0700658 ObjPtr<mirror::Executable> executable = soa.Decode<mirror::Executable>(javaMethod);
Neil Fuller0e844392016-09-08 13:43:31 +0100659 const bool accessible = executable->IsAccessible();
660 ArtMethod* m = executable->GetArtMethod();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700661
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700662 ObjPtr<mirror::Class> declaring_class = m->GetDeclaringClass();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800663 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700664 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -0700665 HandleWrapperObjPtr<mirror::Class> h_class(hs.NewHandleWrapper(&declaring_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700666 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), h_class, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800667 return nullptr;
668 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700669 }
670
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700671 ObjPtr<mirror::Object> receiver;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700672 if (!m->IsStatic()) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800673 // Replace calls to String.<init> with equivalent StringFactory call.
674 if (declaring_class->IsStringClass() && m->IsConstructor()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100675 m = WellKnownClasses::StringInitToStringFactory(m);
Jeff Hao848f70a2014-01-15 13:49:50 -0800676 CHECK(javaReceiver == nullptr);
677 } else {
678 // Check that the receiver is non-null and an instance of the field's declaring class.
Mathieu Chartier0795f232016-09-27 18:43:30 -0700679 receiver = soa.Decode<mirror::Object>(javaReceiver);
Jeff Hao848f70a2014-01-15 13:49:50 -0800680 if (!VerifyObjectIsClass(receiver, declaring_class)) {
681 return nullptr;
682 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700683
Jeff Hao848f70a2014-01-15 13:49:50 -0800684 // Find the actual implementation of the virtual method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700685 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m, kRuntimePointerSize);
Jeff Hao848f70a2014-01-15 13:49:50 -0800686 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700687 }
688
689 // Get our arrays of arguments and their types, and check they're the same size.
Mathieu Chartier0795f232016-09-27 18:43:30 -0700690 ObjPtr<mirror::ObjectArray<mirror::Object>> objects =
691 soa.Decode<mirror::ObjectArray<mirror::Object>>(javaArgs);
Andreas Gampe542451c2016-07-26 09:02:02 -0700692 auto* np_method = m->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Andreas Gampe8ad7a3b2017-05-22 16:08:52 -0700693 if (!CheckArgsForInvokeMethod(np_method, objects)) {
Ian Rogersa0485602014-12-02 15:48:04 -0800694 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700695 }
696
Jeff Haocb4581a2014-03-28 15:43:37 -0700697 // If method is not set to be accessible, verify it can be accessed by the caller.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700698 ObjPtr<mirror::Class> calling_class;
Mathieu Chartier268764d2016-09-13 12:09:38 -0700699 if (!accessible && !VerifyAccess(soa.Self(),
700 receiver,
701 declaring_class,
702 m->GetAccessFlags(),
703 &calling_class,
704 num_frames)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000705 ThrowIllegalAccessException(
Andreas Gampec0d82292014-09-23 10:38:30 -0700706 StringPrintf("Class %s cannot access %s method %s of class %s",
David Sehr709b0702016-10-13 09:12:37 -0700707 calling_class == nullptr ? "null" : calling_class->PrettyClass().c_str(),
Andreas Gampec0d82292014-09-23 10:38:30 -0700708 PrettyJavaAccessFlags(m->GetAccessFlags()).c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700709 m->PrettyMethod().c_str(),
Andreas Gampec0d82292014-09-23 10:38:30 -0700710 m->GetDeclaringClass() == nullptr ? "null" :
David Sehr709b0702016-10-13 09:12:37 -0700711 m->GetDeclaringClass()->PrettyClass().c_str()).c_str());
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700712 return nullptr;
713 }
714
Ian Rogers53b8b092014-03-13 23:45:53 -0700715 // Invoke the method.
716 JValue result;
Andreas Gampe8ad7a3b2017-05-22 16:08:52 -0700717 const char* shorty;
718 if (!InvokeMethodImpl(soa, m, np_method, receiver, objects, &shorty, &result)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700719 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700720 }
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -0700721 return soa.AddLocalReference<jobject>(BoxPrimitive(Primitive::GetType(shorty[0]), result));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700722}
723
Andreas Gampe8ad7a3b2017-05-22 16:08:52 -0700724void InvokeConstructor(const ScopedObjectAccessAlreadyRunnable& soa,
725 ArtMethod* constructor,
726 ObjPtr<mirror::Object> receiver,
727 jobject javaArgs) {
728 // We want to make sure that the stack is not within a small distance from the
729 // protected region in case we are calling into a leaf function whose stack
730 // check has been elided.
731 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEndForInterpreter(true))) {
732 ThrowStackOverflowError(soa.Self());
733 return;
734 }
735
736 if (kIsDebugBuild) {
737 CHECK(constructor->IsConstructor());
738
739 ObjPtr<mirror::Class> declaring_class = constructor->GetDeclaringClass();
740 CHECK(declaring_class->IsInitialized());
741
742 // Calls to String.<init> should have been repplaced with with equivalent StringFactory calls.
743 CHECK(!declaring_class->IsStringClass());
744
745 // Check that the receiver is non-null and an instance of the field's declaring class.
746 CHECK(receiver != nullptr);
747 CHECK(VerifyObjectIsClass(receiver, declaring_class));
748 CHECK_EQ(constructor,
749 receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(constructor,
750 kRuntimePointerSize));
751 }
752
753 // Get our arrays of arguments and their types, and check they're the same size.
754 ObjPtr<mirror::ObjectArray<mirror::Object>> objects =
755 soa.Decode<mirror::ObjectArray<mirror::Object>>(javaArgs);
756 ArtMethod* np_method = constructor->GetInterfaceMethodIfProxy(kRuntimePointerSize);
757 if (!CheckArgsForInvokeMethod(np_method, objects)) {
758 return;
759 }
760
761 // Invoke the constructor.
762 JValue result;
763 const char* shorty;
764 InvokeMethodImpl(soa, constructor, np_method, receiver, objects, &shorty, &result);
765}
766
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700767ObjPtr<mirror::Object> BoxPrimitive(Primitive::Type src_class, const JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700768 if (src_class == Primitive::kPrimNot) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700769 return MakeObjPtr(value.GetL());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700770 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700771 if (src_class == Primitive::kPrimVoid) {
772 // There's no such thing as a void field, and void methods invoked via reflection return null.
773 return nullptr;
774 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700775
Ian Rogers84956ff2014-03-26 23:52:41 -0700776 jmethodID m = nullptr;
Ian Rogers0177e532014-02-11 16:30:46 -0800777 const char* shorty;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700778 switch (src_class) {
779 case Primitive::kPrimBoolean:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700780 m = WellKnownClasses::java_lang_Boolean_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800781 shorty = "LZ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700782 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700783 case Primitive::kPrimByte:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700784 m = WellKnownClasses::java_lang_Byte_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800785 shorty = "LB";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700786 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700787 case Primitive::kPrimChar:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700788 m = WellKnownClasses::java_lang_Character_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800789 shorty = "LC";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700790 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700791 case Primitive::kPrimDouble:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700792 m = WellKnownClasses::java_lang_Double_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800793 shorty = "LD";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700794 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700795 case Primitive::kPrimFloat:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700796 m = WellKnownClasses::java_lang_Float_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800797 shorty = "LF";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700798 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700799 case Primitive::kPrimInt:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700800 m = WellKnownClasses::java_lang_Integer_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800801 shorty = "LI";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700802 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700803 case Primitive::kPrimLong:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700804 m = WellKnownClasses::java_lang_Long_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800805 shorty = "LJ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700806 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700807 case Primitive::kPrimShort:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700808 m = WellKnownClasses::java_lang_Short_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800809 shorty = "LS";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700810 break;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700811 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700812 LOG(FATAL) << static_cast<int>(src_class);
Ian Rogers0177e532014-02-11 16:30:46 -0800813 shorty = nullptr;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700814 }
815
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700816 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers53b8b092014-03-13 23:45:53 -0700817 DCHECK_EQ(soa.Self()->GetState(), kRunnable);
Jeff Hao5d917302013-02-27 17:57:33 -0800818
Ian Rogers53b8b092014-03-13 23:45:53 -0700819 ArgArray arg_array(shorty, 2);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800820 JValue result;
Jeff Hao5d917302013-02-27 17:57:33 -0800821 if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) {
822 arg_array.AppendWide(value.GetJ());
823 } else {
824 arg_array.Append(value.GetI());
825 }
826
Andreas Gampe13b27842016-11-07 16:48:23 -0800827 jni::DecodeArtMethod(m)->Invoke(soa.Self(),
828 arg_array.GetArray(),
829 arg_array.GetNumBytes(),
830 &result,
831 shorty);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800832 return result.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700833}
834
Mathieu Chartierc7853442015-03-27 14:35:38 -0700835static std::string UnboxingFailureKind(ArtField* f)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700836 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700837 if (f != nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700838 return "field " + f->PrettyField(false);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700839 }
840 return "result";
841}
842
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700843static bool UnboxPrimitive(ObjPtr<mirror::Object> o,
844 ObjPtr<mirror::Class> dst_class,
845 ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -0700846 JValue* unboxed_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700847 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700848 bool unbox_for_result = (f == nullptr);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700849 if (!dst_class->IsPrimitive()) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700850 if (UNLIKELY(o != nullptr && !o->InstanceOf(dst_class))) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800851 if (!unbox_for_result) {
David Sehr709b0702016-10-13 09:12:37 -0700852 ThrowIllegalArgumentException(
853 StringPrintf("%s has type %s, got %s",
854 UnboxingFailureKind(f).c_str(),
855 dst_class->PrettyDescriptor().c_str(),
856 o->PrettyTypeOf().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800857 } else {
David Sehr709b0702016-10-13 09:12:37 -0700858 ThrowClassCastException(
859 StringPrintf("Couldn't convert result of type %s to %s",
860 o->PrettyTypeOf().c_str(),
861 dst_class->PrettyDescriptor().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800862 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700863 return false;
864 }
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700865 unboxed_value->SetL(o);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700866 return true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800867 }
868 if (UNLIKELY(dst_class->GetPrimitiveType() == Primitive::kPrimVoid)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000869 ThrowIllegalArgumentException(StringPrintf("Can't unbox %s to void",
Ian Rogers84956ff2014-03-26 23:52:41 -0700870 UnboxingFailureKind(f).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700871 return false;
872 }
Ian Rogers84956ff2014-03-26 23:52:41 -0700873 if (UNLIKELY(o == nullptr)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800874 if (!unbox_for_result) {
David Sehr709b0702016-10-13 09:12:37 -0700875 ThrowIllegalArgumentException(
876 StringPrintf("%s has type %s, got null",
877 UnboxingFailureKind(f).c_str(),
878 dst_class->PrettyDescriptor().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800879 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700880 ThrowNullPointerException(
881 StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
David Sehr709b0702016-10-13 09:12:37 -0700882 dst_class->PrettyDescriptor().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800883 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700884 return false;
885 }
886
Elliott Hughes1d878f32012-04-11 15:17:54 -0700887 JValue boxed_value;
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700888 ObjPtr<mirror::Class> klass = o->GetClass();
Vladimir Marko9186b182018-11-06 14:55:54 +0000889 Primitive::Type primitive_type;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700890 ArtField* primitive_field = &klass->GetIFieldsPtr()->At(0);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700891 if (klass->DescriptorEquals("Ljava/lang/Boolean;")) {
Vladimir Marko9186b182018-11-06 14:55:54 +0000892 primitive_type = Primitive::kPrimBoolean;
Mathieu Chartier3398c782016-09-30 10:27:43 -0700893 boxed_value.SetZ(primitive_field->GetBoolean(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700894 } else if (klass->DescriptorEquals("Ljava/lang/Byte;")) {
Vladimir Marko9186b182018-11-06 14:55:54 +0000895 primitive_type = Primitive::kPrimByte;
Mathieu Chartier3398c782016-09-30 10:27:43 -0700896 boxed_value.SetB(primitive_field->GetByte(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700897 } else if (klass->DescriptorEquals("Ljava/lang/Character;")) {
Vladimir Marko9186b182018-11-06 14:55:54 +0000898 primitive_type = Primitive::kPrimChar;
Mathieu Chartier3398c782016-09-30 10:27:43 -0700899 boxed_value.SetC(primitive_field->GetChar(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700900 } else if (klass->DescriptorEquals("Ljava/lang/Float;")) {
Vladimir Marko9186b182018-11-06 14:55:54 +0000901 primitive_type = Primitive::kPrimFloat;
Mathieu Chartier3398c782016-09-30 10:27:43 -0700902 boxed_value.SetF(primitive_field->GetFloat(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700903 } else if (klass->DescriptorEquals("Ljava/lang/Double;")) {
Vladimir Marko9186b182018-11-06 14:55:54 +0000904 primitive_type = Primitive::kPrimDouble;
Mathieu Chartier3398c782016-09-30 10:27:43 -0700905 boxed_value.SetD(primitive_field->GetDouble(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700906 } else if (klass->DescriptorEquals("Ljava/lang/Integer;")) {
Vladimir Marko9186b182018-11-06 14:55:54 +0000907 primitive_type = Primitive::kPrimInt;
Mathieu Chartier3398c782016-09-30 10:27:43 -0700908 boxed_value.SetI(primitive_field->GetInt(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700909 } else if (klass->DescriptorEquals("Ljava/lang/Long;")) {
Vladimir Marko9186b182018-11-06 14:55:54 +0000910 primitive_type = Primitive::kPrimLong;
Mathieu Chartier3398c782016-09-30 10:27:43 -0700911 boxed_value.SetJ(primitive_field->GetLong(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700912 } else if (klass->DescriptorEquals("Ljava/lang/Short;")) {
Vladimir Marko9186b182018-11-06 14:55:54 +0000913 primitive_type = Primitive::kPrimShort;
Mathieu Chartier3398c782016-09-30 10:27:43 -0700914 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700915 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700916 std::string temp;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000917 ThrowIllegalArgumentException(
Ian Rogers1ff3c982014-08-12 02:30:58 -0700918 StringPrintf("%s has type %s, got %s", UnboxingFailureKind(f).c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700919 dst_class->PrettyDescriptor().c_str(),
Ian Rogers1ff3c982014-08-12 02:30:58 -0700920 PrettyDescriptor(o->GetClass()->GetDescriptor(&temp)).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700921 return false;
922 }
923
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000924 return ConvertPrimitiveValue(unbox_for_result,
Vladimir Marko9186b182018-11-06 14:55:54 +0000925 primitive_type,
926 dst_class->GetPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700927 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700928}
929
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700930bool UnboxPrimitiveForField(ObjPtr<mirror::Object> o,
931 ObjPtr<mirror::Class> dst_class,
932 ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -0700933 JValue* unboxed_value) {
934 DCHECK(f != nullptr);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000935 return UnboxPrimitive(o, dst_class, f, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700936}
937
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700938bool UnboxPrimitiveForResult(ObjPtr<mirror::Object> o,
939 ObjPtr<mirror::Class> dst_class,
940 JValue* unboxed_value) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000941 return UnboxPrimitive(o, dst_class, nullptr, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700942}
943
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700944ObjPtr<mirror::Class> GetCallingClass(Thread* self, size_t num_frames) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700945 NthCallerVisitor visitor(self, num_frames);
946 visitor.WalkStack();
947 return visitor.caller != nullptr ? visitor.caller->GetDeclaringClass() : nullptr;
948}
949
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700950bool VerifyAccess(Thread* self,
951 ObjPtr<mirror::Object> obj,
952 ObjPtr<mirror::Class> declaring_class,
953 uint32_t access_flags,
954 ObjPtr<mirror::Class>* calling_class,
955 size_t num_frames) {
Mathieu Chartier76433272014-09-26 14:32:37 -0700956 if ((access_flags & kAccPublic) != 0) {
957 return true;
958 }
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700959 ObjPtr<mirror::Class> klass = GetCallingClass(self, num_frames);
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700960 if (UNLIKELY(klass == nullptr)) {
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100961 // The caller is an attached native thread.
Mathieu Chartier76433272014-09-26 14:32:37 -0700962 return false;
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100963 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700964 *calling_class = klass;
Mathieu Chartier268764d2016-09-13 12:09:38 -0700965 return VerifyAccess(obj, declaring_class, access_flags, klass);
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700966}
967
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700968bool VerifyAccess(ObjPtr<mirror::Object> obj,
969 ObjPtr<mirror::Class> declaring_class,
Mathieu Chartier268764d2016-09-13 12:09:38 -0700970 uint32_t access_flags,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700971 ObjPtr<mirror::Class> calling_class) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700972 if (calling_class == declaring_class) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700973 return true;
974 }
Mathieu Chartier268764d2016-09-13 12:09:38 -0700975 ScopedAssertNoThreadSuspension sants("verify-access");
Jeff Haocb4581a2014-03-28 15:43:37 -0700976 if ((access_flags & kAccPrivate) != 0) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700977 return false;
978 }
Jeff Haocb4581a2014-03-28 15:43:37 -0700979 if ((access_flags & kAccProtected) != 0) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700980 if (obj != nullptr && !obj->InstanceOf(calling_class) &&
Mathieu Chartier3398c782016-09-30 10:27:43 -0700981 !declaring_class->IsInSamePackage(calling_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700982 return false;
Mathieu Chartier3398c782016-09-30 10:27:43 -0700983 } else if (declaring_class->IsAssignableFrom(calling_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700984 return true;
985 }
986 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700987 return declaring_class->IsInSamePackage(calling_class);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700988}
989
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700990void InvalidReceiverError(ObjPtr<mirror::Object> o, ObjPtr<mirror::Class> c) {
David Sehr709b0702016-10-13 09:12:37 -0700991 std::string expected_class_name(mirror::Class::PrettyDescriptor(c));
992 std::string actual_class_name(mirror::Object::PrettyTypeOf(o));
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700993 ThrowIllegalArgumentException(StringPrintf("Expected receiver of type %s, but got %s",
994 expected_class_name.c_str(),
995 actual_class_name.c_str()).c_str());
996}
997
Jeff Hao83c81952015-05-27 19:29:29 -0700998// This only works if there's one reference which points to the object in obj.
999// Will need to be fixed if there's cases where it's not.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -07001000void UpdateReference(Thread* self, jobject obj, ObjPtr<mirror::Object> result) {
Jeff Hao83c81952015-05-27 19:29:29 -07001001 IndirectRef ref = reinterpret_cast<IndirectRef>(obj);
Andreas Gampedc061d02016-10-24 13:19:37 -07001002 IndirectRefKind kind = IndirectReferenceTable::GetIndirectRefKind(ref);
Jeff Hao83c81952015-05-27 19:29:29 -07001003 if (kind == kLocal) {
Ian Rogers55256cb2017-12-21 17:07:11 -08001004 self->GetJniEnv()->UpdateLocal(obj, result);
Jeff Hao83c81952015-05-27 19:29:29 -07001005 } else if (kind == kHandleScopeOrInvalid) {
1006 LOG(FATAL) << "Unsupported UpdateReference for kind kHandleScopeOrInvalid";
1007 } else if (kind == kGlobal) {
Ian Rogers55256cb2017-12-21 17:07:11 -08001008 self->GetJniEnv()->GetVm()->UpdateGlobal(self, ref, result);
Jeff Hao83c81952015-05-27 19:29:29 -07001009 } else {
1010 DCHECK_EQ(kind, kWeakGlobal);
Ian Rogers55256cb2017-12-21 17:07:11 -08001011 self->GetJniEnv()->GetVm()->UpdateWeakGlobal(self, ref, result);
Jeff Hao83c81952015-05-27 19:29:29 -07001012 }
1013}
1014
Elliott Hughes418d20f2011-09-22 14:00:39 -07001015} // namespace art