blob: 068bc285e521579404c5b17b2c9eea1fd679a47b [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"
Andreas Gampec15a2f42017-04-21 12:09:39 -070026#include "java_vm_ext.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070027#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/class-inl.h"
Neil Fuller0e844392016-09-08 13:43:31 +010029#include "mirror/executable.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "mirror/object_array-inl.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070031#include "nativehelper/scoped_local_ref.h"
Jeff Hao11d5d8f2014-03-26 15:08:20 -070032#include "nth_caller_visitor.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070033#include "scoped_thread_state_change-inl.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010034#include "stack_reference.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070035#include "well_known_classes.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070036
Elliott Hughes418d20f2011-09-22 14:00:39 -070037namespace art {
Ian Rogers9e937be2018-02-15 17:06:58 -080038namespace {
Elliott Hughes418d20f2011-09-22 14:00:39 -070039
Andreas Gampe46ee31b2016-12-14 10:11:49 -080040using android::base::StringPrintf;
41
Ian Rogers53b8b092014-03-13 23:45:53 -070042class ArgArray {
43 public:
Roland Levillain3887c462015-08-12 18:15:42 +010044 ArgArray(const char* shorty, uint32_t shorty_len)
Ian Rogers53b8b092014-03-13 23:45:53 -070045 : shorty_(shorty), shorty_len_(shorty_len), num_bytes_(0) {
46 size_t num_slots = shorty_len + 1; // +1 in case of receiver.
47 if (LIKELY((num_slots * 2) < kSmallArgArraySize)) {
48 // We can trivially use the small arg array.
49 arg_array_ = small_arg_array_;
50 } else {
51 // Analyze shorty to see if we need the large arg array.
52 for (size_t i = 1; i < shorty_len; ++i) {
53 char c = shorty[i];
54 if (c == 'J' || c == 'D') {
55 num_slots++;
56 }
57 }
58 if (num_slots <= kSmallArgArraySize) {
59 arg_array_ = small_arg_array_;
60 } else {
61 large_arg_array_.reset(new uint32_t[num_slots]);
62 arg_array_ = large_arg_array_.get();
63 }
64 }
65 }
66
67 uint32_t* GetArray() {
68 return arg_array_;
69 }
70
71 uint32_t GetNumBytes() {
72 return num_bytes_;
73 }
74
75 void Append(uint32_t value) {
76 arg_array_[num_bytes_ / 4] = value;
77 num_bytes_ += 4;
78 }
79
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070080 void Append(ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070081 Append(StackReference<mirror::Object>::FromMirrorPtr(obj.Ptr()).AsVRegValue());
Ian Rogers53b8b092014-03-13 23:45:53 -070082 }
83
84 void AppendWide(uint64_t value) {
Ian Rogers53b8b092014-03-13 23:45:53 -070085 arg_array_[num_bytes_ / 4] = value;
86 arg_array_[(num_bytes_ / 4) + 1] = value >> 32;
87 num_bytes_ += 8;
88 }
89
90 void AppendFloat(float value) {
91 jvalue jv;
92 jv.f = value;
93 Append(jv.i);
94 }
95
96 void AppendDouble(double value) {
97 jvalue jv;
98 jv.d = value;
99 AppendWide(jv.j);
100 }
101
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700102 void BuildArgArrayFromVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700103 ObjPtr<mirror::Object> receiver,
104 va_list ap)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700105 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700106 // Set receiver if non-null (method is not static)
107 if (receiver != nullptr) {
108 Append(receiver);
109 }
110 for (size_t i = 1; i < shorty_len_; ++i) {
111 switch (shorty_[i]) {
112 case 'Z':
113 case 'B':
114 case 'C':
115 case 'S':
116 case 'I':
117 Append(va_arg(ap, jint));
118 break;
119 case 'F':
120 AppendFloat(va_arg(ap, jdouble));
121 break;
122 case 'L':
Mathieu Chartier0795f232016-09-27 18:43:30 -0700123 Append(soa.Decode<mirror::Object>(va_arg(ap, jobject)));
Ian Rogers53b8b092014-03-13 23:45:53 -0700124 break;
125 case 'D':
126 AppendDouble(va_arg(ap, jdouble));
127 break;
128 case 'J':
129 AppendWide(va_arg(ap, jlong));
130 break;
131#ifndef NDEBUG
132 default:
133 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
134#endif
135 }
136 }
137 }
138
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700139 void BuildArgArrayFromJValues(const ScopedObjectAccessAlreadyRunnable& soa,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700140 ObjPtr<mirror::Object> receiver, jvalue* args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700141 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700142 // Set receiver if non-null (method is not static)
143 if (receiver != nullptr) {
144 Append(receiver);
145 }
146 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
147 switch (shorty_[i]) {
148 case 'Z':
149 Append(args[args_offset].z);
150 break;
151 case 'B':
152 Append(args[args_offset].b);
153 break;
154 case 'C':
155 Append(args[args_offset].c);
156 break;
157 case 'S':
158 Append(args[args_offset].s);
159 break;
160 case 'I':
Ian Rogers9e937be2018-02-15 17:06:58 -0800161 FALLTHROUGH_INTENDED;
Ian Rogers53b8b092014-03-13 23:45:53 -0700162 case 'F':
163 Append(args[args_offset].i);
164 break;
165 case 'L':
Mathieu Chartier0795f232016-09-27 18:43:30 -0700166 Append(soa.Decode<mirror::Object>(args[args_offset].l));
Ian Rogers53b8b092014-03-13 23:45:53 -0700167 break;
168 case 'D':
Ian Rogers9e937be2018-02-15 17:06:58 -0800169 FALLTHROUGH_INTENDED;
Ian Rogers53b8b092014-03-13 23:45:53 -0700170 case 'J':
171 AppendWide(args[args_offset].j);
172 break;
173#ifndef NDEBUG
174 default:
175 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
176#endif
177 }
178 }
179 }
180
181 void BuildArgArrayFromFrame(ShadowFrame* shadow_frame, uint32_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700182 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700183 // Set receiver if non-null (method is not static)
184 size_t cur_arg = arg_offset;
185 if (!shadow_frame->GetMethod()->IsStatic()) {
186 Append(shadow_frame->GetVReg(cur_arg));
187 cur_arg++;
188 }
189 for (size_t i = 1; i < shorty_len_; ++i) {
190 switch (shorty_[i]) {
191 case 'Z':
192 case 'B':
193 case 'C':
194 case 'S':
195 case 'I':
196 case 'F':
197 case 'L':
198 Append(shadow_frame->GetVReg(cur_arg));
199 cur_arg++;
200 break;
201 case 'D':
202 case 'J':
203 AppendWide(shadow_frame->GetVRegLong(cur_arg));
204 cur_arg++;
205 cur_arg++;
206 break;
207#ifndef NDEBUG
208 default:
209 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
210#endif
211 }
212 }
213 }
214
215 static void ThrowIllegalPrimitiveArgumentException(const char* expected,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700216 const char* found_descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700217 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000218 ThrowIllegalArgumentException(
Ian Rogers53b8b092014-03-13 23:45:53 -0700219 StringPrintf("Invalid primitive conversion from %s to %s", expected,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700220 PrettyDescriptor(found_descriptor).c_str()).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700221 }
222
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700223 bool BuildArgArrayFromObjectArray(ObjPtr<mirror::Object> receiver,
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000224 ObjPtr<mirror::ObjectArray<mirror::Object>> raw_args,
225 ArtMethod* m,
226 Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700227 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700228 const DexFile::TypeList* classes = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700229 // Set receiver if non-null (method is not static)
230 if (receiver != nullptr) {
231 Append(receiver);
232 }
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000233 StackHandleScope<2> hs(self);
234 MutableHandle<mirror::Object> arg(hs.NewHandle<mirror::Object>(nullptr));
235 Handle<mirror::ObjectArray<mirror::Object>> args(
236 hs.NewHandle<mirror::ObjectArray<mirror::Object>>(raw_args));
Ian Rogers53b8b092014-03-13 23:45:53 -0700237 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000238 arg.Assign(args->Get(args_offset));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800239 if (((shorty_[i] == 'L') && (arg != nullptr)) ||
240 ((arg == nullptr && shorty_[i] != 'L'))) {
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000241 // TODO: The method's parameter's type must have been previously resolved, yet
242 // we've seen cases where it's not b/34440020.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700243 ObjPtr<mirror::Class> dst_class(
Vladimir Markob45528c2017-07-27 14:14:28 +0100244 m->ResolveClassFromTypeIndex(classes->GetTypeItem(args_offset).type_idx_));
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000245 if (dst_class.Ptr() == nullptr) {
246 CHECK(self->IsExceptionPending());
247 return false;
248 }
Andreas Gampefa4333d2017-02-14 11:10:34 -0800249 if (UNLIKELY(arg == nullptr || !arg->InstanceOf(dst_class))) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000250 ThrowIllegalArgumentException(
Ian Rogers11e4c032014-03-14 12:00:39 -0700251 StringPrintf("method %s argument %zd has type %s, got %s",
David Sehr709b0702016-10-13 09:12:37 -0700252 m->PrettyMethod(false).c_str(),
Ian Rogers53b8b092014-03-13 23:45:53 -0700253 args_offset + 1, // Humans don't count from 0.
David Sehr709b0702016-10-13 09:12:37 -0700254 mirror::Class::PrettyDescriptor(dst_class).c_str(),
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000255 mirror::Object::PrettyTypeOf(arg.Get()).c_str()).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700256 return false;
257 }
258 }
259
260#define DO_FIRST_ARG(match_descriptor, get_fn, append) { \
Andreas Gampefa4333d2017-02-14 11:10:34 -0800261 if (LIKELY(arg != nullptr && \
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000262 arg->GetClass()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700263 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000264 append(primitive_field-> get_fn(arg.Get()));
Ian Rogers53b8b092014-03-13 23:45:53 -0700265
266#define DO_ARG(match_descriptor, get_fn, append) \
Andreas Gampefa4333d2017-02-14 11:10:34 -0800267 } else if (LIKELY(arg != nullptr && \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700268 arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700269 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000270 append(primitive_field-> get_fn(arg.Get()));
Ian Rogers53b8b092014-03-13 23:45:53 -0700271
272#define DO_FAIL(expected) \
273 } else { \
274 if (arg->GetClass<>()->IsPrimitive()) { \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700275 std::string temp; \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700276 ThrowIllegalPrimitiveArgumentException(expected, \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700277 arg->GetClass<>()->GetDescriptor(&temp)); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700278 } else { \
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000279 ThrowIllegalArgumentException(\
Ian Rogers11e4c032014-03-14 12:00:39 -0700280 StringPrintf("method %s argument %zd has type %s, got %s", \
David Sehr709b0702016-10-13 09:12:37 -0700281 ArtMethod::PrettyMethod(m, false).c_str(), \
Ian Rogers53b8b092014-03-13 23:45:53 -0700282 args_offset + 1, \
283 expected, \
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000284 mirror::Object::PrettyTypeOf(arg.Get()).c_str()).c_str()); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700285 } \
286 return false; \
287 } }
288
289 switch (shorty_[i]) {
290 case 'L':
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000291 Append(arg.Get());
Ian Rogers53b8b092014-03-13 23:45:53 -0700292 break;
293 case 'Z':
294 DO_FIRST_ARG("Ljava/lang/Boolean;", GetBoolean, Append)
295 DO_FAIL("boolean")
296 break;
297 case 'B':
298 DO_FIRST_ARG("Ljava/lang/Byte;", GetByte, Append)
299 DO_FAIL("byte")
300 break;
301 case 'C':
302 DO_FIRST_ARG("Ljava/lang/Character;", GetChar, Append)
303 DO_FAIL("char")
304 break;
305 case 'S':
306 DO_FIRST_ARG("Ljava/lang/Short;", GetShort, Append)
307 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
308 DO_FAIL("short")
309 break;
310 case 'I':
311 DO_FIRST_ARG("Ljava/lang/Integer;", GetInt, Append)
312 DO_ARG("Ljava/lang/Character;", GetChar, Append)
313 DO_ARG("Ljava/lang/Short;", GetShort, Append)
314 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
315 DO_FAIL("int")
316 break;
317 case 'J':
318 DO_FIRST_ARG("Ljava/lang/Long;", GetLong, AppendWide)
319 DO_ARG("Ljava/lang/Integer;", GetInt, AppendWide)
320 DO_ARG("Ljava/lang/Character;", GetChar, AppendWide)
321 DO_ARG("Ljava/lang/Short;", GetShort, AppendWide)
322 DO_ARG("Ljava/lang/Byte;", GetByte, AppendWide)
323 DO_FAIL("long")
324 break;
325 case 'F':
326 DO_FIRST_ARG("Ljava/lang/Float;", GetFloat, AppendFloat)
327 DO_ARG("Ljava/lang/Long;", GetLong, AppendFloat)
328 DO_ARG("Ljava/lang/Integer;", GetInt, AppendFloat)
329 DO_ARG("Ljava/lang/Character;", GetChar, AppendFloat)
330 DO_ARG("Ljava/lang/Short;", GetShort, AppendFloat)
331 DO_ARG("Ljava/lang/Byte;", GetByte, AppendFloat)
332 DO_FAIL("float")
333 break;
334 case 'D':
335 DO_FIRST_ARG("Ljava/lang/Double;", GetDouble, AppendDouble)
336 DO_ARG("Ljava/lang/Float;", GetFloat, AppendDouble)
337 DO_ARG("Ljava/lang/Long;", GetLong, AppendDouble)
338 DO_ARG("Ljava/lang/Integer;", GetInt, AppendDouble)
339 DO_ARG("Ljava/lang/Character;", GetChar, AppendDouble)
340 DO_ARG("Ljava/lang/Short;", GetShort, AppendDouble)
341 DO_ARG("Ljava/lang/Byte;", GetByte, AppendDouble)
342 DO_FAIL("double")
343 break;
344#ifndef NDEBUG
345 default:
346 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
Ian Rogersa0485602014-12-02 15:48:04 -0800347 UNREACHABLE();
Ian Rogers53b8b092014-03-13 23:45:53 -0700348#endif
349 }
350#undef DO_FIRST_ARG
351#undef DO_ARG
352#undef DO_FAIL
353 }
354 return true;
355 }
356
357 private:
358 enum { kSmallArgArraySize = 16 };
359 const char* const shorty_;
360 const uint32_t shorty_len_;
361 uint32_t num_bytes_;
362 uint32_t* arg_array_;
363 uint32_t small_arg_array_[kSmallArgArraySize];
Ian Rogers700a4022014-05-19 16:49:03 -0700364 std::unique_ptr<uint32_t[]> large_arg_array_;
Ian Rogers53b8b092014-03-13 23:45:53 -0700365};
366
Ian Rogers9e937be2018-02-15 17:06:58 -0800367void CheckMethodArguments(JavaVMExt* vm, ArtMethod* m, uint32_t* args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700368 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700369 const DexFile::TypeList* params = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700370 if (params == nullptr) {
371 return; // No arguments so nothing to check.
372 }
373 uint32_t offset = 0;
374 uint32_t num_params = params->Size();
375 size_t error_count = 0;
376 if (!m->IsStatic()) {
377 offset = 1;
378 }
Ian Rogersa0485602014-12-02 15:48:04 -0800379 // TODO: If args contain object references, it may cause problems.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700380 Thread* const self = Thread::Current();
Ian Rogers53b8b092014-03-13 23:45:53 -0700381 for (uint32_t i = 0; i < num_params; i++) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800382 dex::TypeIndex type_idx = params->GetTypeItem(i).type_idx_;
Vladimir Markob45528c2017-07-27 14:14:28 +0100383 ObjPtr<mirror::Class> param_type(m->ResolveClassFromTypeIndex(type_idx));
Ian Rogers53b8b092014-03-13 23:45:53 -0700384 if (param_type == nullptr) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700385 CHECK(self->IsExceptionPending());
386 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700387 << m->GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000388 << self->GetException()->Dump();
Ian Rogers53b8b092014-03-13 23:45:53 -0700389 self->ClearException();
390 ++error_count;
391 } else if (!param_type->IsPrimitive()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700392 // TODO: There is a compaction bug here since GetClassFromTypeIdx can cause thread suspension,
393 // this is a hard to fix problem since the args can contain Object*, we need to save and
394 // restore them by using a visitor similar to the ones used in the trampoline entrypoints.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700395 ObjPtr<mirror::Object> argument =
Ian Rogers68d8b422014-07-17 11:09:10 -0700396 (reinterpret_cast<StackReference<mirror::Object>*>(&args[i + offset]))->AsMirrorPtr();
Ian Rogers53b8b092014-03-13 23:45:53 -0700397 if (argument != nullptr && !argument->InstanceOf(param_type)) {
398 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
David Sehr709b0702016-10-13 09:12:37 -0700399 << argument->PrettyTypeOf() << " as argument " << (i + 1)
400 << " to " << m->PrettyMethod();
Ian Rogers53b8b092014-03-13 23:45:53 -0700401 ++error_count;
402 }
403 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
404 offset++;
Ian Rogers68d8b422014-07-17 11:09:10 -0700405 } else {
406 int32_t arg = static_cast<int32_t>(args[i + offset]);
407 if (param_type->IsPrimitiveBoolean()) {
408 if (arg != JNI_TRUE && arg != JNI_FALSE) {
409 LOG(ERROR) << "JNI ERROR (app bug): expected jboolean (0/1) but got value of "
David Sehr709b0702016-10-13 09:12:37 -0700410 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700411 ++error_count;
412 }
413 } else if (param_type->IsPrimitiveByte()) {
414 if (arg < -128 || arg > 127) {
415 LOG(ERROR) << "JNI ERROR (app bug): expected jbyte but got value of "
David Sehr709b0702016-10-13 09:12:37 -0700416 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700417 ++error_count;
418 }
419 } else if (param_type->IsPrimitiveChar()) {
420 if (args[i + offset] > 0xFFFF) {
421 LOG(ERROR) << "JNI ERROR (app bug): expected jchar but got value of "
David Sehr709b0702016-10-13 09:12:37 -0700422 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700423 ++error_count;
424 }
425 } else if (param_type->IsPrimitiveShort()) {
426 if (arg < -32768 || arg > 0x7FFF) {
427 LOG(ERROR) << "JNI ERROR (app bug): expected jshort but got value of "
David Sehr709b0702016-10-13 09:12:37 -0700428 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700429 ++error_count;
430 }
431 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700432 }
433 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700434 if (UNLIKELY(error_count > 0)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700435 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
436 // with an argument.
Ian Rogers68d8b422014-07-17 11:09:10 -0700437 vm->JniAbortF(nullptr, "bad arguments passed to %s (see above for details)",
David Sehr709b0702016-10-13 09:12:37 -0700438 m->PrettyMethod().c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700439 }
440}
441
Ian Rogers9e937be2018-02-15 17:06:58 -0800442ArtMethod* FindVirtualMethod(ObjPtr<mirror::Object> receiver, ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700443 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700444 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method, kRuntimePointerSize);
Ian Rogers53b8b092014-03-13 23:45:53 -0700445}
446
447
Ian Rogers9e937be2018-02-15 17:06:58 -0800448void InvokeWithArgArray(const ScopedObjectAccessAlreadyRunnable& soa,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700449 ArtMethod* method, ArgArray* arg_array, JValue* result,
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700450 const char* shorty)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700451 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700452 uint32_t* args = arg_array->GetArray();
Ian Rogers55256cb2017-12-21 17:07:11 -0800453 if (UNLIKELY(soa.Env()->IsCheckJniEnabled())) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700454 CheckMethodArguments(soa.Vm(), method->GetInterfaceMethodIfProxy(kRuntimePointerSize), args);
Ian Rogers53b8b092014-03-13 23:45:53 -0700455 }
456 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, shorty);
457}
458
Ian Rogers9e937be2018-02-15 17:06:58 -0800459} // anonymous namespace
460
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700461JValue InvokeWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
462 va_list args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700463 REQUIRES_SHARED(Locks::mutator_lock_) {
Dave Allison648d7112014-07-25 16:15:27 -0700464 // We want to make sure that the stack is not within a small distance from the
465 // protected region in case we are calling into a leaf function whose stack
466 // check has been elided.
467 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
468 ThrowStackOverflowError(soa.Self());
469 return JValue();
470 }
471
Andreas Gampe13b27842016-11-07 16:48:23 -0800472 ArtMethod* method = jni::DecodeArtMethod(mid);
Jeff Hao39b6c242015-05-19 20:30:23 -0700473 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
474 if (is_string_init) {
475 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100476 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700477 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700478 ObjPtr<mirror::Object> receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700479 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700480 const char* shorty =
481 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700482 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700483 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700484 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700485 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700486 if (is_string_init) {
487 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700488 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700489 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700490 return result;
491}
492
Jeff Hao39b6c242015-05-19 20:30:23 -0700493JValue InvokeWithJValues(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
494 jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700495 // We want to make sure that the stack is not within a small distance from the
496 // protected region in case we are calling into a leaf function whose stack
497 // check has been elided.
498 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
499 ThrowStackOverflowError(soa.Self());
500 return JValue();
501 }
502
Andreas Gampe13b27842016-11-07 16:48:23 -0800503 ArtMethod* method = jni::DecodeArtMethod(mid);
Jeff Hao39b6c242015-05-19 20:30:23 -0700504 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
505 if (is_string_init) {
506 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100507 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700508 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700509 ObjPtr<mirror::Object> receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700510 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700511 const char* shorty =
512 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700513 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700514 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700515 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700516 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700517 if (is_string_init) {
518 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700519 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700520 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700521 return result;
522}
523
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700524JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
Jeff Hao39b6c242015-05-19 20:30:23 -0700525 jobject obj, jmethodID mid, jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700526 // We want to make sure that the stack is not within a small distance from the
527 // protected region in case we are calling into a leaf function whose stack
528 // check has been elided.
529 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
530 ThrowStackOverflowError(soa.Self());
531 return JValue();
532 }
533
Mathieu Chartier0795f232016-09-27 18:43:30 -0700534 ObjPtr<mirror::Object> receiver = soa.Decode<mirror::Object>(obj);
Andreas Gampe13b27842016-11-07 16:48:23 -0800535 ArtMethod* method = FindVirtualMethod(receiver, jni::DecodeArtMethod(mid));
Jeff Hao39b6c242015-05-19 20:30:23 -0700536 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
537 if (is_string_init) {
538 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100539 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700540 receiver = nullptr;
541 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700542 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700543 const char* shorty =
544 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700545 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700546 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700547 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700548 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700549 if (is_string_init) {
550 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700551 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700552 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700553 return result;
554}
555
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700556JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
Ian Rogers53b8b092014-03-13 23:45:53 -0700557 jobject obj, jmethodID mid, va_list args) {
Dave Allison648d7112014-07-25 16:15:27 -0700558 // We want to make sure that the stack is not within a small distance from the
559 // protected region in case we are calling into a leaf function whose stack
560 // check has been elided.
561 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
562 ThrowStackOverflowError(soa.Self());
563 return JValue();
564 }
565
Mathieu Chartier0795f232016-09-27 18:43:30 -0700566 ObjPtr<mirror::Object> receiver = soa.Decode<mirror::Object>(obj);
Andreas Gampe13b27842016-11-07 16:48:23 -0800567 ArtMethod* method = FindVirtualMethod(receiver, jni::DecodeArtMethod(mid));
Jeff Hao39b6c242015-05-19 20:30:23 -0700568 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
569 if (is_string_init) {
570 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100571 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700572 receiver = nullptr;
573 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700574 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700575 const char* shorty =
576 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700577 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700578 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700579 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700580 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700581 if (is_string_init) {
582 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700583 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700584 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700585 return result;
586}
587
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700588jobject InvokeMethod(const ScopedObjectAccessAlreadyRunnable& soa, jobject javaMethod,
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700589 jobject javaReceiver, jobject javaArgs, size_t num_frames) {
Dave Allison648d7112014-07-25 16:15:27 -0700590 // We want to make sure that the stack is not within a small distance from the
591 // protected region in case we are calling into a leaf function whose stack
592 // check has been elided.
593 if (UNLIKELY(__builtin_frame_address(0) <
594 soa.Self()->GetStackEndForInterpreter(true))) {
595 ThrowStackOverflowError(soa.Self());
596 return nullptr;
597 }
598
Mathieu Chartier0795f232016-09-27 18:43:30 -0700599 ObjPtr<mirror::Executable> executable = soa.Decode<mirror::Executable>(javaMethod);
Neil Fuller0e844392016-09-08 13:43:31 +0100600 const bool accessible = executable->IsAccessible();
601 ArtMethod* m = executable->GetArtMethod();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700602
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700603 ObjPtr<mirror::Class> declaring_class = m->GetDeclaringClass();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800604 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700605 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -0700606 HandleWrapperObjPtr<mirror::Class> h_class(hs.NewHandleWrapper(&declaring_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700607 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), h_class, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800608 return nullptr;
609 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700610 }
611
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700612 ObjPtr<mirror::Object> receiver;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700613 if (!m->IsStatic()) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800614 // Replace calls to String.<init> with equivalent StringFactory call.
615 if (declaring_class->IsStringClass() && m->IsConstructor()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100616 m = WellKnownClasses::StringInitToStringFactory(m);
Jeff Hao848f70a2014-01-15 13:49:50 -0800617 CHECK(javaReceiver == nullptr);
618 } else {
619 // Check that the receiver is non-null and an instance of the field's declaring class.
Mathieu Chartier0795f232016-09-27 18:43:30 -0700620 receiver = soa.Decode<mirror::Object>(javaReceiver);
Jeff Hao848f70a2014-01-15 13:49:50 -0800621 if (!VerifyObjectIsClass(receiver, declaring_class)) {
622 return nullptr;
623 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700624
Jeff Hao848f70a2014-01-15 13:49:50 -0800625 // Find the actual implementation of the virtual method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700626 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m, kRuntimePointerSize);
Jeff Hao848f70a2014-01-15 13:49:50 -0800627 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700628 }
629
630 // Get our arrays of arguments and their types, and check they're the same size.
Mathieu Chartier0795f232016-09-27 18:43:30 -0700631 ObjPtr<mirror::ObjectArray<mirror::Object>> objects =
632 soa.Decode<mirror::ObjectArray<mirror::Object>>(javaArgs);
Andreas Gampe542451c2016-07-26 09:02:02 -0700633 auto* np_method = m->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700634 const DexFile::TypeList* classes = np_method->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700635 uint32_t classes_size = (classes == nullptr) ? 0 : classes->Size();
636 uint32_t arg_count = (objects != nullptr) ? objects->GetLength() : 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800637 if (arg_count != classes_size) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000638 ThrowIllegalArgumentException(StringPrintf("Wrong number of arguments; expected %d, got %d",
Ian Rogers62d6c772013-02-27 08:32:07 -0800639 classes_size, arg_count).c_str());
Ian Rogersa0485602014-12-02 15:48:04 -0800640 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700641 }
642
Jeff Haocb4581a2014-03-28 15:43:37 -0700643 // If method is not set to be accessible, verify it can be accessed by the caller.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700644 ObjPtr<mirror::Class> calling_class;
Mathieu Chartier268764d2016-09-13 12:09:38 -0700645 if (!accessible && !VerifyAccess(soa.Self(),
646 receiver,
647 declaring_class,
648 m->GetAccessFlags(),
649 &calling_class,
650 num_frames)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000651 ThrowIllegalAccessException(
Andreas Gampec0d82292014-09-23 10:38:30 -0700652 StringPrintf("Class %s cannot access %s method %s of class %s",
David Sehr709b0702016-10-13 09:12:37 -0700653 calling_class == nullptr ? "null" : calling_class->PrettyClass().c_str(),
Andreas Gampec0d82292014-09-23 10:38:30 -0700654 PrettyJavaAccessFlags(m->GetAccessFlags()).c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700655 m->PrettyMethod().c_str(),
Andreas Gampec0d82292014-09-23 10:38:30 -0700656 m->GetDeclaringClass() == nullptr ? "null" :
David Sehr709b0702016-10-13 09:12:37 -0700657 m->GetDeclaringClass()->PrettyClass().c_str()).c_str());
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700658 return nullptr;
659 }
660
Ian Rogers53b8b092014-03-13 23:45:53 -0700661 // Invoke the method.
662 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700663 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700664 const char* shorty = np_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700665 ArgArray arg_array(shorty, shorty_len);
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000666 if (!arg_array.BuildArgArrayFromObjectArray(receiver, objects, np_method, soa.Self())) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700667 CHECK(soa.Self()->IsExceptionPending());
668 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700669 }
670
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700671 InvokeWithArgArray(soa, m, &arg_array, &result, shorty);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700672
673 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700674 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700675 // If we get another exception when we are trying to wrap, then just use that instead.
Chang Xing443f8622017-06-08 11:31:48 -0700676 ScopedLocalRef<jthrowable> th(soa.Env(), soa.Env()->ExceptionOccurred());
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700677 soa.Self()->ClearException();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700678 jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException");
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700679 if (exception_class == nullptr) {
Mathieu Chartiercc9d1cb2017-03-16 15:50:57 -0700680 soa.Self()->AssertPendingException();
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700681 return nullptr;
682 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700683 jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700684 CHECK(mid != nullptr);
Chang Xing443f8622017-06-08 11:31:48 -0700685 jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th.get());
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700686 if (exception_instance == nullptr) {
Mathieu Chartiercc9d1cb2017-03-16 15:50:57 -0700687 soa.Self()->AssertPendingException();
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700688 return nullptr;
689 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700690 soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance));
Ian Rogersa0485602014-12-02 15:48:04 -0800691 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700692 }
693
694 // Box if necessary and return.
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -0700695 return soa.AddLocalReference<jobject>(BoxPrimitive(Primitive::GetType(shorty[0]), result));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700696}
697
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700698ObjPtr<mirror::Object> BoxPrimitive(Primitive::Type src_class, const JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700699 if (src_class == Primitive::kPrimNot) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700700 return MakeObjPtr(value.GetL());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700701 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700702 if (src_class == Primitive::kPrimVoid) {
703 // There's no such thing as a void field, and void methods invoked via reflection return null.
704 return nullptr;
705 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700706
Ian Rogers84956ff2014-03-26 23:52:41 -0700707 jmethodID m = nullptr;
Ian Rogers0177e532014-02-11 16:30:46 -0800708 const char* shorty;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700709 switch (src_class) {
710 case Primitive::kPrimBoolean:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700711 m = WellKnownClasses::java_lang_Boolean_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800712 shorty = "LZ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700713 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700714 case Primitive::kPrimByte:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700715 m = WellKnownClasses::java_lang_Byte_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800716 shorty = "LB";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700717 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700718 case Primitive::kPrimChar:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700719 m = WellKnownClasses::java_lang_Character_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800720 shorty = "LC";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700721 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700722 case Primitive::kPrimDouble:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700723 m = WellKnownClasses::java_lang_Double_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800724 shorty = "LD";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700725 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700726 case Primitive::kPrimFloat:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700727 m = WellKnownClasses::java_lang_Float_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800728 shorty = "LF";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700729 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700730 case Primitive::kPrimInt:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700731 m = WellKnownClasses::java_lang_Integer_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800732 shorty = "LI";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700733 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700734 case Primitive::kPrimLong:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700735 m = WellKnownClasses::java_lang_Long_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800736 shorty = "LJ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700737 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700738 case Primitive::kPrimShort:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700739 m = WellKnownClasses::java_lang_Short_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800740 shorty = "LS";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700741 break;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700742 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700743 LOG(FATAL) << static_cast<int>(src_class);
Ian Rogers0177e532014-02-11 16:30:46 -0800744 shorty = nullptr;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700745 }
746
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700747 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers53b8b092014-03-13 23:45:53 -0700748 DCHECK_EQ(soa.Self()->GetState(), kRunnable);
Jeff Hao5d917302013-02-27 17:57:33 -0800749
Ian Rogers53b8b092014-03-13 23:45:53 -0700750 ArgArray arg_array(shorty, 2);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800751 JValue result;
Jeff Hao5d917302013-02-27 17:57:33 -0800752 if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) {
753 arg_array.AppendWide(value.GetJ());
754 } else {
755 arg_array.Append(value.GetI());
756 }
757
Andreas Gampe13b27842016-11-07 16:48:23 -0800758 jni::DecodeArtMethod(m)->Invoke(soa.Self(),
759 arg_array.GetArray(),
760 arg_array.GetNumBytes(),
761 &result,
762 shorty);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800763 return result.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700764}
765
Mathieu Chartierc7853442015-03-27 14:35:38 -0700766static std::string UnboxingFailureKind(ArtField* f)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700767 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700768 if (f != nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700769 return "field " + f->PrettyField(false);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700770 }
771 return "result";
772}
773
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700774static bool UnboxPrimitive(ObjPtr<mirror::Object> o,
775 ObjPtr<mirror::Class> dst_class,
776 ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -0700777 JValue* unboxed_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700778 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700779 bool unbox_for_result = (f == nullptr);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700780 if (!dst_class->IsPrimitive()) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700781 if (UNLIKELY(o != nullptr && !o->InstanceOf(dst_class))) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800782 if (!unbox_for_result) {
David Sehr709b0702016-10-13 09:12:37 -0700783 ThrowIllegalArgumentException(
784 StringPrintf("%s has type %s, got %s",
785 UnboxingFailureKind(f).c_str(),
786 dst_class->PrettyDescriptor().c_str(),
787 o->PrettyTypeOf().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800788 } else {
David Sehr709b0702016-10-13 09:12:37 -0700789 ThrowClassCastException(
790 StringPrintf("Couldn't convert result of type %s to %s",
791 o->PrettyTypeOf().c_str(),
792 dst_class->PrettyDescriptor().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800793 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700794 return false;
795 }
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700796 unboxed_value->SetL(o);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700797 return true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800798 }
799 if (UNLIKELY(dst_class->GetPrimitiveType() == Primitive::kPrimVoid)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000800 ThrowIllegalArgumentException(StringPrintf("Can't unbox %s to void",
Ian Rogers84956ff2014-03-26 23:52:41 -0700801 UnboxingFailureKind(f).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700802 return false;
803 }
Ian Rogers84956ff2014-03-26 23:52:41 -0700804 if (UNLIKELY(o == nullptr)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800805 if (!unbox_for_result) {
David Sehr709b0702016-10-13 09:12:37 -0700806 ThrowIllegalArgumentException(
807 StringPrintf("%s has type %s, got null",
808 UnboxingFailureKind(f).c_str(),
809 dst_class->PrettyDescriptor().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800810 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700811 ThrowNullPointerException(
812 StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
David Sehr709b0702016-10-13 09:12:37 -0700813 dst_class->PrettyDescriptor().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800814 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700815 return false;
816 }
817
Elliott Hughes1d878f32012-04-11 15:17:54 -0700818 JValue boxed_value;
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700819 ObjPtr<mirror::Class> klass = o->GetClass();
820 ObjPtr<mirror::Class> src_class = nullptr;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700821 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700822 ArtField* primitive_field = &klass->GetIFieldsPtr()->At(0);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700823 if (klass->DescriptorEquals("Ljava/lang/Boolean;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700824 src_class = class_linker->FindPrimitiveClass('Z');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700825 boxed_value.SetZ(primitive_field->GetBoolean(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700826 } else if (klass->DescriptorEquals("Ljava/lang/Byte;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700827 src_class = class_linker->FindPrimitiveClass('B');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700828 boxed_value.SetB(primitive_field->GetByte(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700829 } else if (klass->DescriptorEquals("Ljava/lang/Character;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700830 src_class = class_linker->FindPrimitiveClass('C');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700831 boxed_value.SetC(primitive_field->GetChar(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700832 } else if (klass->DescriptorEquals("Ljava/lang/Float;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700833 src_class = class_linker->FindPrimitiveClass('F');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700834 boxed_value.SetF(primitive_field->GetFloat(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700835 } else if (klass->DescriptorEquals("Ljava/lang/Double;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700836 src_class = class_linker->FindPrimitiveClass('D');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700837 boxed_value.SetD(primitive_field->GetDouble(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700838 } else if (klass->DescriptorEquals("Ljava/lang/Integer;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700839 src_class = class_linker->FindPrimitiveClass('I');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700840 boxed_value.SetI(primitive_field->GetInt(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700841 } else if (klass->DescriptorEquals("Ljava/lang/Long;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700842 src_class = class_linker->FindPrimitiveClass('J');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700843 boxed_value.SetJ(primitive_field->GetLong(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700844 } else if (klass->DescriptorEquals("Ljava/lang/Short;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700845 src_class = class_linker->FindPrimitiveClass('S');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700846 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700847 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700848 std::string temp;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000849 ThrowIllegalArgumentException(
Ian Rogers1ff3c982014-08-12 02:30:58 -0700850 StringPrintf("%s has type %s, got %s", UnboxingFailureKind(f).c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700851 dst_class->PrettyDescriptor().c_str(),
Ian Rogers1ff3c982014-08-12 02:30:58 -0700852 PrettyDescriptor(o->GetClass()->GetDescriptor(&temp)).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700853 return false;
854 }
855
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000856 return ConvertPrimitiveValue(unbox_for_result,
Ian Rogers62d6c772013-02-27 08:32:07 -0800857 src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700858 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700859}
860
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700861bool UnboxPrimitiveForField(ObjPtr<mirror::Object> o,
862 ObjPtr<mirror::Class> dst_class,
863 ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -0700864 JValue* unboxed_value) {
865 DCHECK(f != nullptr);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000866 return UnboxPrimitive(o, dst_class, f, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700867}
868
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700869bool UnboxPrimitiveForResult(ObjPtr<mirror::Object> o,
870 ObjPtr<mirror::Class> dst_class,
871 JValue* unboxed_value) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000872 return UnboxPrimitive(o, dst_class, nullptr, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700873}
874
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700875ObjPtr<mirror::Class> GetCallingClass(Thread* self, size_t num_frames) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700876 NthCallerVisitor visitor(self, num_frames);
877 visitor.WalkStack();
878 return visitor.caller != nullptr ? visitor.caller->GetDeclaringClass() : nullptr;
879}
880
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700881bool VerifyAccess(Thread* self,
882 ObjPtr<mirror::Object> obj,
883 ObjPtr<mirror::Class> declaring_class,
884 uint32_t access_flags,
885 ObjPtr<mirror::Class>* calling_class,
886 size_t num_frames) {
Mathieu Chartier76433272014-09-26 14:32:37 -0700887 if ((access_flags & kAccPublic) != 0) {
888 return true;
889 }
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700890 ObjPtr<mirror::Class> klass = GetCallingClass(self, num_frames);
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700891 if (UNLIKELY(klass == nullptr)) {
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100892 // The caller is an attached native thread.
Mathieu Chartier76433272014-09-26 14:32:37 -0700893 return false;
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100894 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700895 *calling_class = klass;
Mathieu Chartier268764d2016-09-13 12:09:38 -0700896 return VerifyAccess(obj, declaring_class, access_flags, klass);
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700897}
898
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700899bool VerifyAccess(ObjPtr<mirror::Object> obj,
900 ObjPtr<mirror::Class> declaring_class,
Mathieu Chartier268764d2016-09-13 12:09:38 -0700901 uint32_t access_flags,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700902 ObjPtr<mirror::Class> calling_class) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700903 if (calling_class == declaring_class) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700904 return true;
905 }
Mathieu Chartier268764d2016-09-13 12:09:38 -0700906 ScopedAssertNoThreadSuspension sants("verify-access");
Jeff Haocb4581a2014-03-28 15:43:37 -0700907 if ((access_flags & kAccPrivate) != 0) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700908 return false;
909 }
Jeff Haocb4581a2014-03-28 15:43:37 -0700910 if ((access_flags & kAccProtected) != 0) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700911 if (obj != nullptr && !obj->InstanceOf(calling_class) &&
Mathieu Chartier3398c782016-09-30 10:27:43 -0700912 !declaring_class->IsInSamePackage(calling_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700913 return false;
Mathieu Chartier3398c782016-09-30 10:27:43 -0700914 } else if (declaring_class->IsAssignableFrom(calling_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700915 return true;
916 }
917 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700918 return declaring_class->IsInSamePackage(calling_class);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700919}
920
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700921void InvalidReceiverError(ObjPtr<mirror::Object> o, ObjPtr<mirror::Class> c) {
David Sehr709b0702016-10-13 09:12:37 -0700922 std::string expected_class_name(mirror::Class::PrettyDescriptor(c));
923 std::string actual_class_name(mirror::Object::PrettyTypeOf(o));
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700924 ThrowIllegalArgumentException(StringPrintf("Expected receiver of type %s, but got %s",
925 expected_class_name.c_str(),
926 actual_class_name.c_str()).c_str());
927}
928
Jeff Hao83c81952015-05-27 19:29:29 -0700929// This only works if there's one reference which points to the object in obj.
930// Will need to be fixed if there's cases where it's not.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700931void UpdateReference(Thread* self, jobject obj, ObjPtr<mirror::Object> result) {
Jeff Hao83c81952015-05-27 19:29:29 -0700932 IndirectRef ref = reinterpret_cast<IndirectRef>(obj);
Andreas Gampedc061d02016-10-24 13:19:37 -0700933 IndirectRefKind kind = IndirectReferenceTable::GetIndirectRefKind(ref);
Jeff Hao83c81952015-05-27 19:29:29 -0700934 if (kind == kLocal) {
Ian Rogers55256cb2017-12-21 17:07:11 -0800935 self->GetJniEnv()->UpdateLocal(obj, result);
Jeff Hao83c81952015-05-27 19:29:29 -0700936 } else if (kind == kHandleScopeOrInvalid) {
937 LOG(FATAL) << "Unsupported UpdateReference for kind kHandleScopeOrInvalid";
938 } else if (kind == kGlobal) {
Ian Rogers55256cb2017-12-21 17:07:11 -0800939 self->GetJniEnv()->GetVm()->UpdateGlobal(self, ref, result);
Jeff Hao83c81952015-05-27 19:29:29 -0700940 } else {
941 DCHECK_EQ(kind, kWeakGlobal);
Ian Rogers55256cb2017-12-21 17:07:11 -0800942 self->GetJniEnv()->GetVm()->UpdateWeakGlobal(self, ref, result);
Jeff Hao83c81952015-05-27 19:29:29 -0700943 }
944}
945
Elliott Hughes418d20f2011-09-22 14:00:39 -0700946} // namespace art