blob: 9683cedd4daa97ec014ab78e9ec796d07c33609b [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"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070024#include "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 {
38
Andreas Gampe46ee31b2016-12-14 10:11:49 -080039using android::base::StringPrintf;
40
Ian Rogers53b8b092014-03-13 23:45:53 -070041class ArgArray {
42 public:
Roland Levillain3887c462015-08-12 18:15:42 +010043 ArgArray(const char* shorty, uint32_t shorty_len)
Ian Rogers53b8b092014-03-13 23:45:53 -070044 : shorty_(shorty), shorty_len_(shorty_len), num_bytes_(0) {
45 size_t num_slots = shorty_len + 1; // +1 in case of receiver.
46 if (LIKELY((num_slots * 2) < kSmallArgArraySize)) {
47 // We can trivially use the small arg array.
48 arg_array_ = small_arg_array_;
49 } else {
50 // Analyze shorty to see if we need the large arg array.
51 for (size_t i = 1; i < shorty_len; ++i) {
52 char c = shorty[i];
53 if (c == 'J' || c == 'D') {
54 num_slots++;
55 }
56 }
57 if (num_slots <= kSmallArgArraySize) {
58 arg_array_ = small_arg_array_;
59 } else {
60 large_arg_array_.reset(new uint32_t[num_slots]);
61 arg_array_ = large_arg_array_.get();
62 }
63 }
64 }
65
66 uint32_t* GetArray() {
67 return arg_array_;
68 }
69
70 uint32_t GetNumBytes() {
71 return num_bytes_;
72 }
73
74 void Append(uint32_t value) {
75 arg_array_[num_bytes_ / 4] = value;
76 num_bytes_ += 4;
77 }
78
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070079 void Append(ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070080 Append(StackReference<mirror::Object>::FromMirrorPtr(obj.Ptr()).AsVRegValue());
Ian Rogers53b8b092014-03-13 23:45:53 -070081 }
82
83 void AppendWide(uint64_t value) {
Ian Rogers53b8b092014-03-13 23:45:53 -070084 arg_array_[num_bytes_ / 4] = value;
85 arg_array_[(num_bytes_ / 4) + 1] = value >> 32;
86 num_bytes_ += 8;
87 }
88
89 void AppendFloat(float value) {
90 jvalue jv;
91 jv.f = value;
92 Append(jv.i);
93 }
94
95 void AppendDouble(double value) {
96 jvalue jv;
97 jv.d = value;
98 AppendWide(jv.j);
99 }
100
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700101 void BuildArgArrayFromVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700102 ObjPtr<mirror::Object> receiver,
103 va_list ap)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700104 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700105 // Set receiver if non-null (method is not static)
106 if (receiver != nullptr) {
107 Append(receiver);
108 }
109 for (size_t i = 1; i < shorty_len_; ++i) {
110 switch (shorty_[i]) {
111 case 'Z':
112 case 'B':
113 case 'C':
114 case 'S':
115 case 'I':
116 Append(va_arg(ap, jint));
117 break;
118 case 'F':
119 AppendFloat(va_arg(ap, jdouble));
120 break;
121 case 'L':
Mathieu Chartier0795f232016-09-27 18:43:30 -0700122 Append(soa.Decode<mirror::Object>(va_arg(ap, jobject)));
Ian Rogers53b8b092014-03-13 23:45:53 -0700123 break;
124 case 'D':
125 AppendDouble(va_arg(ap, jdouble));
126 break;
127 case 'J':
128 AppendWide(va_arg(ap, jlong));
129 break;
130#ifndef NDEBUG
131 default:
132 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
133#endif
134 }
135 }
136 }
137
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700138 void BuildArgArrayFromJValues(const ScopedObjectAccessAlreadyRunnable& soa,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700139 ObjPtr<mirror::Object> receiver, jvalue* args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700140 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700141 // Set receiver if non-null (method is not static)
142 if (receiver != nullptr) {
143 Append(receiver);
144 }
145 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
146 switch (shorty_[i]) {
147 case 'Z':
148 Append(args[args_offset].z);
149 break;
150 case 'B':
151 Append(args[args_offset].b);
152 break;
153 case 'C':
154 Append(args[args_offset].c);
155 break;
156 case 'S':
157 Append(args[args_offset].s);
158 break;
159 case 'I':
160 case 'F':
161 Append(args[args_offset].i);
162 break;
163 case 'L':
Mathieu Chartier0795f232016-09-27 18:43:30 -0700164 Append(soa.Decode<mirror::Object>(args[args_offset].l));
Ian Rogers53b8b092014-03-13 23:45:53 -0700165 break;
166 case 'D':
167 case 'J':
168 AppendWide(args[args_offset].j);
169 break;
170#ifndef NDEBUG
171 default:
172 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
173#endif
174 }
175 }
176 }
177
178 void BuildArgArrayFromFrame(ShadowFrame* shadow_frame, uint32_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700179 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700180 // Set receiver if non-null (method is not static)
181 size_t cur_arg = arg_offset;
182 if (!shadow_frame->GetMethod()->IsStatic()) {
183 Append(shadow_frame->GetVReg(cur_arg));
184 cur_arg++;
185 }
186 for (size_t i = 1; i < shorty_len_; ++i) {
187 switch (shorty_[i]) {
188 case 'Z':
189 case 'B':
190 case 'C':
191 case 'S':
192 case 'I':
193 case 'F':
194 case 'L':
195 Append(shadow_frame->GetVReg(cur_arg));
196 cur_arg++;
197 break;
198 case 'D':
199 case 'J':
200 AppendWide(shadow_frame->GetVRegLong(cur_arg));
201 cur_arg++;
202 cur_arg++;
203 break;
204#ifndef NDEBUG
205 default:
206 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
207#endif
208 }
209 }
210 }
211
212 static void ThrowIllegalPrimitiveArgumentException(const char* expected,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700213 const char* found_descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700214 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000215 ThrowIllegalArgumentException(
Ian Rogers53b8b092014-03-13 23:45:53 -0700216 StringPrintf("Invalid primitive conversion from %s to %s", expected,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700217 PrettyDescriptor(found_descriptor).c_str()).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700218 }
219
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700220 bool BuildArgArrayFromObjectArray(ObjPtr<mirror::Object> receiver,
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000221 ObjPtr<mirror::ObjectArray<mirror::Object>> raw_args,
222 ArtMethod* m,
223 Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700224 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700225 const DexFile::TypeList* classes = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700226 // Set receiver if non-null (method is not static)
227 if (receiver != nullptr) {
228 Append(receiver);
229 }
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000230 StackHandleScope<2> hs(self);
231 MutableHandle<mirror::Object> arg(hs.NewHandle<mirror::Object>(nullptr));
232 Handle<mirror::ObjectArray<mirror::Object>> args(
233 hs.NewHandle<mirror::ObjectArray<mirror::Object>>(raw_args));
Ian Rogers53b8b092014-03-13 23:45:53 -0700234 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000235 arg.Assign(args->Get(args_offset));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800236 if (((shorty_[i] == 'L') && (arg != nullptr)) ||
237 ((arg == nullptr && shorty_[i] != 'L'))) {
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000238 // TODO: The method's parameter's type must have been previously resolved, yet
239 // we've seen cases where it's not b/34440020.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700240 ObjPtr<mirror::Class> dst_class(
Vladimir Markob45528c2017-07-27 14:14:28 +0100241 m->ResolveClassFromTypeIndex(classes->GetTypeItem(args_offset).type_idx_));
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000242 if (dst_class.Ptr() == nullptr) {
243 CHECK(self->IsExceptionPending());
244 return false;
245 }
Andreas Gampefa4333d2017-02-14 11:10:34 -0800246 if (UNLIKELY(arg == nullptr || !arg->InstanceOf(dst_class))) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000247 ThrowIllegalArgumentException(
Ian Rogers11e4c032014-03-14 12:00:39 -0700248 StringPrintf("method %s argument %zd has type %s, got %s",
David Sehr709b0702016-10-13 09:12:37 -0700249 m->PrettyMethod(false).c_str(),
Ian Rogers53b8b092014-03-13 23:45:53 -0700250 args_offset + 1, // Humans don't count from 0.
David Sehr709b0702016-10-13 09:12:37 -0700251 mirror::Class::PrettyDescriptor(dst_class).c_str(),
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000252 mirror::Object::PrettyTypeOf(arg.Get()).c_str()).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700253 return false;
254 }
255 }
256
257#define DO_FIRST_ARG(match_descriptor, get_fn, append) { \
Andreas Gampefa4333d2017-02-14 11:10:34 -0800258 if (LIKELY(arg != nullptr && \
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000259 arg->GetClass()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700260 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000261 append(primitive_field-> get_fn(arg.Get()));
Ian Rogers53b8b092014-03-13 23:45:53 -0700262
263#define DO_ARG(match_descriptor, get_fn, append) \
Andreas Gampefa4333d2017-02-14 11:10:34 -0800264 } else if (LIKELY(arg != nullptr && \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700265 arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700266 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000267 append(primitive_field-> get_fn(arg.Get()));
Ian Rogers53b8b092014-03-13 23:45:53 -0700268
269#define DO_FAIL(expected) \
270 } else { \
271 if (arg->GetClass<>()->IsPrimitive()) { \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700272 std::string temp; \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700273 ThrowIllegalPrimitiveArgumentException(expected, \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700274 arg->GetClass<>()->GetDescriptor(&temp)); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700275 } else { \
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000276 ThrowIllegalArgumentException(\
Ian Rogers11e4c032014-03-14 12:00:39 -0700277 StringPrintf("method %s argument %zd has type %s, got %s", \
David Sehr709b0702016-10-13 09:12:37 -0700278 ArtMethod::PrettyMethod(m, false).c_str(), \
Ian Rogers53b8b092014-03-13 23:45:53 -0700279 args_offset + 1, \
280 expected, \
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000281 mirror::Object::PrettyTypeOf(arg.Get()).c_str()).c_str()); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700282 } \
283 return false; \
284 } }
285
286 switch (shorty_[i]) {
287 case 'L':
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000288 Append(arg.Get());
Ian Rogers53b8b092014-03-13 23:45:53 -0700289 break;
290 case 'Z':
291 DO_FIRST_ARG("Ljava/lang/Boolean;", GetBoolean, Append)
292 DO_FAIL("boolean")
293 break;
294 case 'B':
295 DO_FIRST_ARG("Ljava/lang/Byte;", GetByte, Append)
296 DO_FAIL("byte")
297 break;
298 case 'C':
299 DO_FIRST_ARG("Ljava/lang/Character;", GetChar, Append)
300 DO_FAIL("char")
301 break;
302 case 'S':
303 DO_FIRST_ARG("Ljava/lang/Short;", GetShort, Append)
304 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
305 DO_FAIL("short")
306 break;
307 case 'I':
308 DO_FIRST_ARG("Ljava/lang/Integer;", GetInt, Append)
309 DO_ARG("Ljava/lang/Character;", GetChar, Append)
310 DO_ARG("Ljava/lang/Short;", GetShort, Append)
311 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
312 DO_FAIL("int")
313 break;
314 case 'J':
315 DO_FIRST_ARG("Ljava/lang/Long;", GetLong, AppendWide)
316 DO_ARG("Ljava/lang/Integer;", GetInt, AppendWide)
317 DO_ARG("Ljava/lang/Character;", GetChar, AppendWide)
318 DO_ARG("Ljava/lang/Short;", GetShort, AppendWide)
319 DO_ARG("Ljava/lang/Byte;", GetByte, AppendWide)
320 DO_FAIL("long")
321 break;
322 case 'F':
323 DO_FIRST_ARG("Ljava/lang/Float;", GetFloat, AppendFloat)
324 DO_ARG("Ljava/lang/Long;", GetLong, AppendFloat)
325 DO_ARG("Ljava/lang/Integer;", GetInt, AppendFloat)
326 DO_ARG("Ljava/lang/Character;", GetChar, AppendFloat)
327 DO_ARG("Ljava/lang/Short;", GetShort, AppendFloat)
328 DO_ARG("Ljava/lang/Byte;", GetByte, AppendFloat)
329 DO_FAIL("float")
330 break;
331 case 'D':
332 DO_FIRST_ARG("Ljava/lang/Double;", GetDouble, AppendDouble)
333 DO_ARG("Ljava/lang/Float;", GetFloat, AppendDouble)
334 DO_ARG("Ljava/lang/Long;", GetLong, AppendDouble)
335 DO_ARG("Ljava/lang/Integer;", GetInt, AppendDouble)
336 DO_ARG("Ljava/lang/Character;", GetChar, AppendDouble)
337 DO_ARG("Ljava/lang/Short;", GetShort, AppendDouble)
338 DO_ARG("Ljava/lang/Byte;", GetByte, AppendDouble)
339 DO_FAIL("double")
340 break;
341#ifndef NDEBUG
342 default:
343 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
Ian Rogersa0485602014-12-02 15:48:04 -0800344 UNREACHABLE();
Ian Rogers53b8b092014-03-13 23:45:53 -0700345#endif
346 }
347#undef DO_FIRST_ARG
348#undef DO_ARG
349#undef DO_FAIL
350 }
351 return true;
352 }
353
354 private:
355 enum { kSmallArgArraySize = 16 };
356 const char* const shorty_;
357 const uint32_t shorty_len_;
358 uint32_t num_bytes_;
359 uint32_t* arg_array_;
360 uint32_t small_arg_array_[kSmallArgArraySize];
Ian Rogers700a4022014-05-19 16:49:03 -0700361 std::unique_ptr<uint32_t[]> large_arg_array_;
Ian Rogers53b8b092014-03-13 23:45:53 -0700362};
363
Mathieu Chartiere401d142015-04-22 13:56:20 -0700364static void CheckMethodArguments(JavaVMExt* vm, ArtMethod* m, uint32_t* args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700365 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700366 const DexFile::TypeList* params = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700367 if (params == nullptr) {
368 return; // No arguments so nothing to check.
369 }
370 uint32_t offset = 0;
371 uint32_t num_params = params->Size();
372 size_t error_count = 0;
373 if (!m->IsStatic()) {
374 offset = 1;
375 }
Ian Rogersa0485602014-12-02 15:48:04 -0800376 // TODO: If args contain object references, it may cause problems.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700377 Thread* const self = Thread::Current();
Ian Rogers53b8b092014-03-13 23:45:53 -0700378 for (uint32_t i = 0; i < num_params; i++) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800379 dex::TypeIndex type_idx = params->GetTypeItem(i).type_idx_;
Vladimir Markob45528c2017-07-27 14:14:28 +0100380 ObjPtr<mirror::Class> param_type(m->ResolveClassFromTypeIndex(type_idx));
Ian Rogers53b8b092014-03-13 23:45:53 -0700381 if (param_type == nullptr) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700382 CHECK(self->IsExceptionPending());
383 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700384 << m->GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000385 << self->GetException()->Dump();
Ian Rogers53b8b092014-03-13 23:45:53 -0700386 self->ClearException();
387 ++error_count;
388 } else if (!param_type->IsPrimitive()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700389 // TODO: There is a compaction bug here since GetClassFromTypeIdx can cause thread suspension,
390 // this is a hard to fix problem since the args can contain Object*, we need to save and
391 // restore them by using a visitor similar to the ones used in the trampoline entrypoints.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700392 ObjPtr<mirror::Object> argument =
Ian Rogers68d8b422014-07-17 11:09:10 -0700393 (reinterpret_cast<StackReference<mirror::Object>*>(&args[i + offset]))->AsMirrorPtr();
Ian Rogers53b8b092014-03-13 23:45:53 -0700394 if (argument != nullptr && !argument->InstanceOf(param_type)) {
395 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
David Sehr709b0702016-10-13 09:12:37 -0700396 << argument->PrettyTypeOf() << " as argument " << (i + 1)
397 << " to " << m->PrettyMethod();
Ian Rogers53b8b092014-03-13 23:45:53 -0700398 ++error_count;
399 }
400 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
401 offset++;
Ian Rogers68d8b422014-07-17 11:09:10 -0700402 } else {
403 int32_t arg = static_cast<int32_t>(args[i + offset]);
404 if (param_type->IsPrimitiveBoolean()) {
405 if (arg != JNI_TRUE && arg != JNI_FALSE) {
406 LOG(ERROR) << "JNI ERROR (app bug): expected jboolean (0/1) but got value of "
David Sehr709b0702016-10-13 09:12:37 -0700407 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700408 ++error_count;
409 }
410 } else if (param_type->IsPrimitiveByte()) {
411 if (arg < -128 || arg > 127) {
412 LOG(ERROR) << "JNI ERROR (app bug): expected jbyte but got value of "
David Sehr709b0702016-10-13 09:12:37 -0700413 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700414 ++error_count;
415 }
416 } else if (param_type->IsPrimitiveChar()) {
417 if (args[i + offset] > 0xFFFF) {
418 LOG(ERROR) << "JNI ERROR (app bug): expected jchar but got value of "
David Sehr709b0702016-10-13 09:12:37 -0700419 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700420 ++error_count;
421 }
422 } else if (param_type->IsPrimitiveShort()) {
423 if (arg < -32768 || arg > 0x7FFF) {
424 LOG(ERROR) << "JNI ERROR (app bug): expected jshort but got value of "
David Sehr709b0702016-10-13 09:12:37 -0700425 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700426 ++error_count;
427 }
428 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700429 }
430 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700431 if (UNLIKELY(error_count > 0)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700432 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
433 // with an argument.
Ian Rogers68d8b422014-07-17 11:09:10 -0700434 vm->JniAbortF(nullptr, "bad arguments passed to %s (see above for details)",
David Sehr709b0702016-10-13 09:12:37 -0700435 m->PrettyMethod().c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700436 }
437}
438
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700439static ArtMethod* FindVirtualMethod(ObjPtr<mirror::Object> receiver, ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700440 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700441 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method, kRuntimePointerSize);
Ian Rogers53b8b092014-03-13 23:45:53 -0700442}
443
444
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700445static void InvokeWithArgArray(const ScopedObjectAccessAlreadyRunnable& soa,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700446 ArtMethod* method, ArgArray* arg_array, JValue* result,
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700447 const char* shorty)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700448 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700449 uint32_t* args = arg_array->GetArray();
450 if (UNLIKELY(soa.Env()->check_jni)) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700451 CheckMethodArguments(soa.Vm(), method->GetInterfaceMethodIfProxy(kRuntimePointerSize), args);
Ian Rogers53b8b092014-03-13 23:45:53 -0700452 }
453 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, shorty);
454}
455
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700456JValue InvokeWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
457 va_list args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700458 REQUIRES_SHARED(Locks::mutator_lock_) {
Dave Allison648d7112014-07-25 16:15:27 -0700459 // We want to make sure that the stack is not within a small distance from the
460 // protected region in case we are calling into a leaf function whose stack
461 // check has been elided.
462 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
463 ThrowStackOverflowError(soa.Self());
464 return JValue();
465 }
466
Andreas Gampe13b27842016-11-07 16:48:23 -0800467 ArtMethod* method = jni::DecodeArtMethod(mid);
Jeff Hao39b6c242015-05-19 20:30:23 -0700468 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
469 if (is_string_init) {
470 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100471 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700472 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700473 ObjPtr<mirror::Object> receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700474 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700475 const char* shorty =
476 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700477 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700478 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700479 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700480 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700481 if (is_string_init) {
482 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700483 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700484 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700485 return result;
486}
487
Jeff Hao39b6c242015-05-19 20:30:23 -0700488JValue InvokeWithJValues(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
489 jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700490 // We want to make sure that the stack is not within a small distance from the
491 // protected region in case we are calling into a leaf function whose stack
492 // check has been elided.
493 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
494 ThrowStackOverflowError(soa.Self());
495 return JValue();
496 }
497
Andreas Gampe13b27842016-11-07 16:48:23 -0800498 ArtMethod* method = jni::DecodeArtMethod(mid);
Jeff Hao39b6c242015-05-19 20:30:23 -0700499 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
500 if (is_string_init) {
501 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100502 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700503 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700504 ObjPtr<mirror::Object> receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700505 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700506 const char* shorty =
507 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700508 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700509 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700510 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700511 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700512 if (is_string_init) {
513 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700514 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700515 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700516 return result;
517}
518
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700519JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
Jeff Hao39b6c242015-05-19 20:30:23 -0700520 jobject obj, jmethodID mid, jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700521 // We want to make sure that the stack is not within a small distance from the
522 // protected region in case we are calling into a leaf function whose stack
523 // check has been elided.
524 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
525 ThrowStackOverflowError(soa.Self());
526 return JValue();
527 }
528
Mathieu Chartier0795f232016-09-27 18:43:30 -0700529 ObjPtr<mirror::Object> receiver = soa.Decode<mirror::Object>(obj);
Andreas Gampe13b27842016-11-07 16:48:23 -0800530 ArtMethod* method = FindVirtualMethod(receiver, jni::DecodeArtMethod(mid));
Jeff Hao39b6c242015-05-19 20:30:23 -0700531 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
532 if (is_string_init) {
533 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100534 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700535 receiver = nullptr;
536 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700537 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700538 const char* shorty =
539 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700540 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700541 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700542 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700543 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700544 if (is_string_init) {
545 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700546 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700547 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700548 return result;
549}
550
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700551JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
Ian Rogers53b8b092014-03-13 23:45:53 -0700552 jobject obj, jmethodID mid, va_list args) {
Dave Allison648d7112014-07-25 16:15:27 -0700553 // We want to make sure that the stack is not within a small distance from the
554 // protected region in case we are calling into a leaf function whose stack
555 // check has been elided.
556 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
557 ThrowStackOverflowError(soa.Self());
558 return JValue();
559 }
560
Mathieu Chartier0795f232016-09-27 18:43:30 -0700561 ObjPtr<mirror::Object> receiver = soa.Decode<mirror::Object>(obj);
Andreas Gampe13b27842016-11-07 16:48:23 -0800562 ArtMethod* method = FindVirtualMethod(receiver, 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 receiver = nullptr;
568 }
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.BuildArgArrayFromVarArgs(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 -0700583jobject InvokeMethod(const ScopedObjectAccessAlreadyRunnable& soa, jobject javaMethod,
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700584 jobject javaReceiver, jobject javaArgs, size_t num_frames) {
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) <
589 soa.Self()->GetStackEndForInterpreter(true))) {
590 ThrowStackOverflowError(soa.Self());
591 return nullptr;
592 }
593
Mathieu Chartier0795f232016-09-27 18:43:30 -0700594 ObjPtr<mirror::Executable> executable = soa.Decode<mirror::Executable>(javaMethod);
Neil Fuller0e844392016-09-08 13:43:31 +0100595 const bool accessible = executable->IsAccessible();
596 ArtMethod* m = executable->GetArtMethod();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700597
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700598 ObjPtr<mirror::Class> declaring_class = m->GetDeclaringClass();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800599 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700600 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -0700601 HandleWrapperObjPtr<mirror::Class> h_class(hs.NewHandleWrapper(&declaring_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700602 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), h_class, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800603 return nullptr;
604 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700605 }
606
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700607 ObjPtr<mirror::Object> receiver;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700608 if (!m->IsStatic()) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800609 // Replace calls to String.<init> with equivalent StringFactory call.
610 if (declaring_class->IsStringClass() && m->IsConstructor()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100611 m = WellKnownClasses::StringInitToStringFactory(m);
Jeff Hao848f70a2014-01-15 13:49:50 -0800612 CHECK(javaReceiver == nullptr);
613 } else {
614 // Check that the receiver is non-null and an instance of the field's declaring class.
Mathieu Chartier0795f232016-09-27 18:43:30 -0700615 receiver = soa.Decode<mirror::Object>(javaReceiver);
Jeff Hao848f70a2014-01-15 13:49:50 -0800616 if (!VerifyObjectIsClass(receiver, declaring_class)) {
617 return nullptr;
618 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700619
Jeff Hao848f70a2014-01-15 13:49:50 -0800620 // Find the actual implementation of the virtual method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700621 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m, kRuntimePointerSize);
Jeff Hao848f70a2014-01-15 13:49:50 -0800622 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700623 }
624
625 // Get our arrays of arguments and their types, and check they're the same size.
Mathieu Chartier0795f232016-09-27 18:43:30 -0700626 ObjPtr<mirror::ObjectArray<mirror::Object>> objects =
627 soa.Decode<mirror::ObjectArray<mirror::Object>>(javaArgs);
Andreas Gampe542451c2016-07-26 09:02:02 -0700628 auto* np_method = m->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700629 const DexFile::TypeList* classes = np_method->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700630 uint32_t classes_size = (classes == nullptr) ? 0 : classes->Size();
631 uint32_t arg_count = (objects != nullptr) ? objects->GetLength() : 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800632 if (arg_count != classes_size) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000633 ThrowIllegalArgumentException(StringPrintf("Wrong number of arguments; expected %d, got %d",
Ian Rogers62d6c772013-02-27 08:32:07 -0800634 classes_size, arg_count).c_str());
Ian Rogersa0485602014-12-02 15:48:04 -0800635 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700636 }
637
Jeff Haocb4581a2014-03-28 15:43:37 -0700638 // If method is not set to be accessible, verify it can be accessed by the caller.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700639 ObjPtr<mirror::Class> calling_class;
Mathieu Chartier268764d2016-09-13 12:09:38 -0700640 if (!accessible && !VerifyAccess(soa.Self(),
641 receiver,
642 declaring_class,
643 m->GetAccessFlags(),
644 &calling_class,
645 num_frames)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000646 ThrowIllegalAccessException(
Andreas Gampec0d82292014-09-23 10:38:30 -0700647 StringPrintf("Class %s cannot access %s method %s of class %s",
David Sehr709b0702016-10-13 09:12:37 -0700648 calling_class == nullptr ? "null" : calling_class->PrettyClass().c_str(),
Andreas Gampec0d82292014-09-23 10:38:30 -0700649 PrettyJavaAccessFlags(m->GetAccessFlags()).c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700650 m->PrettyMethod().c_str(),
Andreas Gampec0d82292014-09-23 10:38:30 -0700651 m->GetDeclaringClass() == nullptr ? "null" :
David Sehr709b0702016-10-13 09:12:37 -0700652 m->GetDeclaringClass()->PrettyClass().c_str()).c_str());
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700653 return nullptr;
654 }
655
Ian Rogers53b8b092014-03-13 23:45:53 -0700656 // Invoke the method.
657 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700658 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700659 const char* shorty = np_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700660 ArgArray arg_array(shorty, shorty_len);
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000661 if (!arg_array.BuildArgArrayFromObjectArray(receiver, objects, np_method, soa.Self())) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700662 CHECK(soa.Self()->IsExceptionPending());
663 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700664 }
665
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700666 InvokeWithArgArray(soa, m, &arg_array, &result, shorty);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700667
668 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700669 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700670 // If we get another exception when we are trying to wrap, then just use that instead.
Chang Xing443f8622017-06-08 11:31:48 -0700671 ScopedLocalRef<jthrowable> th(soa.Env(), soa.Env()->ExceptionOccurred());
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700672 soa.Self()->ClearException();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700673 jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException");
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700674 if (exception_class == nullptr) {
Mathieu Chartiercc9d1cb2017-03-16 15:50:57 -0700675 soa.Self()->AssertPendingException();
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700676 return nullptr;
677 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700678 jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700679 CHECK(mid != nullptr);
Chang Xing443f8622017-06-08 11:31:48 -0700680 jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th.get());
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700681 if (exception_instance == nullptr) {
Mathieu Chartiercc9d1cb2017-03-16 15:50:57 -0700682 soa.Self()->AssertPendingException();
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700683 return nullptr;
684 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700685 soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance));
Ian Rogersa0485602014-12-02 15:48:04 -0800686 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700687 }
688
689 // Box if necessary and return.
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -0700690 return soa.AddLocalReference<jobject>(BoxPrimitive(Primitive::GetType(shorty[0]), result));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700691}
692
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700693ObjPtr<mirror::Object> BoxPrimitive(Primitive::Type src_class, const JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700694 if (src_class == Primitive::kPrimNot) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700695 return MakeObjPtr(value.GetL());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700696 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700697 if (src_class == Primitive::kPrimVoid) {
698 // There's no such thing as a void field, and void methods invoked via reflection return null.
699 return nullptr;
700 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700701
Ian Rogers84956ff2014-03-26 23:52:41 -0700702 jmethodID m = nullptr;
Ian Rogers0177e532014-02-11 16:30:46 -0800703 const char* shorty;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700704 switch (src_class) {
705 case Primitive::kPrimBoolean:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700706 m = WellKnownClasses::java_lang_Boolean_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800707 shorty = "LZ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700708 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700709 case Primitive::kPrimByte:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700710 m = WellKnownClasses::java_lang_Byte_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800711 shorty = "LB";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700712 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700713 case Primitive::kPrimChar:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700714 m = WellKnownClasses::java_lang_Character_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800715 shorty = "LC";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700716 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700717 case Primitive::kPrimDouble:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700718 m = WellKnownClasses::java_lang_Double_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800719 shorty = "LD";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700720 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700721 case Primitive::kPrimFloat:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700722 m = WellKnownClasses::java_lang_Float_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800723 shorty = "LF";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700724 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700725 case Primitive::kPrimInt:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700726 m = WellKnownClasses::java_lang_Integer_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800727 shorty = "LI";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700728 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700729 case Primitive::kPrimLong:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700730 m = WellKnownClasses::java_lang_Long_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800731 shorty = "LJ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700732 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700733 case Primitive::kPrimShort:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700734 m = WellKnownClasses::java_lang_Short_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800735 shorty = "LS";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700736 break;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700737 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700738 LOG(FATAL) << static_cast<int>(src_class);
Ian Rogers0177e532014-02-11 16:30:46 -0800739 shorty = nullptr;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700740 }
741
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700742 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers53b8b092014-03-13 23:45:53 -0700743 DCHECK_EQ(soa.Self()->GetState(), kRunnable);
Jeff Hao5d917302013-02-27 17:57:33 -0800744
Ian Rogers53b8b092014-03-13 23:45:53 -0700745 ArgArray arg_array(shorty, 2);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800746 JValue result;
Jeff Hao5d917302013-02-27 17:57:33 -0800747 if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) {
748 arg_array.AppendWide(value.GetJ());
749 } else {
750 arg_array.Append(value.GetI());
751 }
752
Andreas Gampe13b27842016-11-07 16:48:23 -0800753 jni::DecodeArtMethod(m)->Invoke(soa.Self(),
754 arg_array.GetArray(),
755 arg_array.GetNumBytes(),
756 &result,
757 shorty);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800758 return result.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700759}
760
Mathieu Chartierc7853442015-03-27 14:35:38 -0700761static std::string UnboxingFailureKind(ArtField* f)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700762 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700763 if (f != nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700764 return "field " + f->PrettyField(false);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700765 }
766 return "result";
767}
768
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700769static bool UnboxPrimitive(ObjPtr<mirror::Object> o,
770 ObjPtr<mirror::Class> dst_class,
771 ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -0700772 JValue* unboxed_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700773 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700774 bool unbox_for_result = (f == nullptr);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700775 if (!dst_class->IsPrimitive()) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700776 if (UNLIKELY(o != nullptr && !o->InstanceOf(dst_class))) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800777 if (!unbox_for_result) {
David Sehr709b0702016-10-13 09:12:37 -0700778 ThrowIllegalArgumentException(
779 StringPrintf("%s has type %s, got %s",
780 UnboxingFailureKind(f).c_str(),
781 dst_class->PrettyDescriptor().c_str(),
782 o->PrettyTypeOf().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800783 } else {
David Sehr709b0702016-10-13 09:12:37 -0700784 ThrowClassCastException(
785 StringPrintf("Couldn't convert result of type %s to %s",
786 o->PrettyTypeOf().c_str(),
787 dst_class->PrettyDescriptor().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800788 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700789 return false;
790 }
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700791 unboxed_value->SetL(o);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700792 return true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800793 }
794 if (UNLIKELY(dst_class->GetPrimitiveType() == Primitive::kPrimVoid)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000795 ThrowIllegalArgumentException(StringPrintf("Can't unbox %s to void",
Ian Rogers84956ff2014-03-26 23:52:41 -0700796 UnboxingFailureKind(f).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700797 return false;
798 }
Ian Rogers84956ff2014-03-26 23:52:41 -0700799 if (UNLIKELY(o == nullptr)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800800 if (!unbox_for_result) {
David Sehr709b0702016-10-13 09:12:37 -0700801 ThrowIllegalArgumentException(
802 StringPrintf("%s has type %s, got null",
803 UnboxingFailureKind(f).c_str(),
804 dst_class->PrettyDescriptor().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800805 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700806 ThrowNullPointerException(
807 StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
David Sehr709b0702016-10-13 09:12:37 -0700808 dst_class->PrettyDescriptor().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800809 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700810 return false;
811 }
812
Elliott Hughes1d878f32012-04-11 15:17:54 -0700813 JValue boxed_value;
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700814 ObjPtr<mirror::Class> klass = o->GetClass();
815 ObjPtr<mirror::Class> src_class = nullptr;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700816 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700817 ArtField* primitive_field = &klass->GetIFieldsPtr()->At(0);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700818 if (klass->DescriptorEquals("Ljava/lang/Boolean;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700819 src_class = class_linker->FindPrimitiveClass('Z');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700820 boxed_value.SetZ(primitive_field->GetBoolean(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700821 } else if (klass->DescriptorEquals("Ljava/lang/Byte;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700822 src_class = class_linker->FindPrimitiveClass('B');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700823 boxed_value.SetB(primitive_field->GetByte(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700824 } else if (klass->DescriptorEquals("Ljava/lang/Character;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700825 src_class = class_linker->FindPrimitiveClass('C');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700826 boxed_value.SetC(primitive_field->GetChar(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700827 } else if (klass->DescriptorEquals("Ljava/lang/Float;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700828 src_class = class_linker->FindPrimitiveClass('F');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700829 boxed_value.SetF(primitive_field->GetFloat(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700830 } else if (klass->DescriptorEquals("Ljava/lang/Double;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700831 src_class = class_linker->FindPrimitiveClass('D');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700832 boxed_value.SetD(primitive_field->GetDouble(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700833 } else if (klass->DescriptorEquals("Ljava/lang/Integer;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700834 src_class = class_linker->FindPrimitiveClass('I');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700835 boxed_value.SetI(primitive_field->GetInt(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700836 } else if (klass->DescriptorEquals("Ljava/lang/Long;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700837 src_class = class_linker->FindPrimitiveClass('J');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700838 boxed_value.SetJ(primitive_field->GetLong(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700839 } else if (klass->DescriptorEquals("Ljava/lang/Short;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700840 src_class = class_linker->FindPrimitiveClass('S');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700841 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700842 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700843 std::string temp;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000844 ThrowIllegalArgumentException(
Ian Rogers1ff3c982014-08-12 02:30:58 -0700845 StringPrintf("%s has type %s, got %s", UnboxingFailureKind(f).c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700846 dst_class->PrettyDescriptor().c_str(),
Ian Rogers1ff3c982014-08-12 02:30:58 -0700847 PrettyDescriptor(o->GetClass()->GetDescriptor(&temp)).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700848 return false;
849 }
850
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000851 return ConvertPrimitiveValue(unbox_for_result,
Ian Rogers62d6c772013-02-27 08:32:07 -0800852 src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700853 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700854}
855
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700856bool UnboxPrimitiveForField(ObjPtr<mirror::Object> o,
857 ObjPtr<mirror::Class> dst_class,
858 ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -0700859 JValue* unboxed_value) {
860 DCHECK(f != nullptr);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000861 return UnboxPrimitive(o, dst_class, f, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700862}
863
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700864bool UnboxPrimitiveForResult(ObjPtr<mirror::Object> o,
865 ObjPtr<mirror::Class> dst_class,
866 JValue* unboxed_value) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000867 return UnboxPrimitive(o, dst_class, nullptr, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700868}
869
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700870ObjPtr<mirror::Class> GetCallingClass(Thread* self, size_t num_frames) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700871 NthCallerVisitor visitor(self, num_frames);
872 visitor.WalkStack();
873 return visitor.caller != nullptr ? visitor.caller->GetDeclaringClass() : nullptr;
874}
875
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700876bool VerifyAccess(Thread* self,
877 ObjPtr<mirror::Object> obj,
878 ObjPtr<mirror::Class> declaring_class,
879 uint32_t access_flags,
880 ObjPtr<mirror::Class>* calling_class,
881 size_t num_frames) {
Mathieu Chartier76433272014-09-26 14:32:37 -0700882 if ((access_flags & kAccPublic) != 0) {
883 return true;
884 }
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700885 ObjPtr<mirror::Class> klass = GetCallingClass(self, num_frames);
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700886 if (UNLIKELY(klass == nullptr)) {
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100887 // The caller is an attached native thread.
Mathieu Chartier76433272014-09-26 14:32:37 -0700888 return false;
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100889 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700890 *calling_class = klass;
Mathieu Chartier268764d2016-09-13 12:09:38 -0700891 return VerifyAccess(obj, declaring_class, access_flags, klass);
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700892}
893
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700894bool VerifyAccess(ObjPtr<mirror::Object> obj,
895 ObjPtr<mirror::Class> declaring_class,
Mathieu Chartier268764d2016-09-13 12:09:38 -0700896 uint32_t access_flags,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700897 ObjPtr<mirror::Class> calling_class) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700898 if (calling_class == declaring_class) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700899 return true;
900 }
Mathieu Chartier268764d2016-09-13 12:09:38 -0700901 ScopedAssertNoThreadSuspension sants("verify-access");
Jeff Haocb4581a2014-03-28 15:43:37 -0700902 if ((access_flags & kAccPrivate) != 0) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700903 return false;
904 }
Jeff Haocb4581a2014-03-28 15:43:37 -0700905 if ((access_flags & kAccProtected) != 0) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700906 if (obj != nullptr && !obj->InstanceOf(calling_class) &&
Mathieu Chartier3398c782016-09-30 10:27:43 -0700907 !declaring_class->IsInSamePackage(calling_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700908 return false;
Mathieu Chartier3398c782016-09-30 10:27:43 -0700909 } else if (declaring_class->IsAssignableFrom(calling_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700910 return true;
911 }
912 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700913 return declaring_class->IsInSamePackage(calling_class);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700914}
915
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700916void InvalidReceiverError(ObjPtr<mirror::Object> o, ObjPtr<mirror::Class> c) {
David Sehr709b0702016-10-13 09:12:37 -0700917 std::string expected_class_name(mirror::Class::PrettyDescriptor(c));
918 std::string actual_class_name(mirror::Object::PrettyTypeOf(o));
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700919 ThrowIllegalArgumentException(StringPrintf("Expected receiver of type %s, but got %s",
920 expected_class_name.c_str(),
921 actual_class_name.c_str()).c_str());
922}
923
Jeff Hao83c81952015-05-27 19:29:29 -0700924// This only works if there's one reference which points to the object in obj.
925// Will need to be fixed if there's cases where it's not.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700926void UpdateReference(Thread* self, jobject obj, ObjPtr<mirror::Object> result) {
Jeff Hao83c81952015-05-27 19:29:29 -0700927 IndirectRef ref = reinterpret_cast<IndirectRef>(obj);
Andreas Gampedc061d02016-10-24 13:19:37 -0700928 IndirectRefKind kind = IndirectReferenceTable::GetIndirectRefKind(ref);
Jeff Hao83c81952015-05-27 19:29:29 -0700929 if (kind == kLocal) {
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -0700930 self->GetJniEnv()->locals.Update(obj, result);
Jeff Hao83c81952015-05-27 19:29:29 -0700931 } else if (kind == kHandleScopeOrInvalid) {
932 LOG(FATAL) << "Unsupported UpdateReference for kind kHandleScopeOrInvalid";
933 } else if (kind == kGlobal) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700934 self->GetJniEnv()->vm->UpdateGlobal(self, ref, result);
Jeff Hao83c81952015-05-27 19:29:29 -0700935 } else {
936 DCHECK_EQ(kind, kWeakGlobal);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700937 self->GetJniEnv()->vm->UpdateWeakGlobal(self, ref, result);
Jeff Hao83c81952015-05-27 19:29:29 -0700938 }
939}
940
Elliott Hughes418d20f2011-09-22 14:00:39 -0700941} // namespace art