blob: 6f1d15c767f0458db022ddb974031d1a85c01b35 [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"
Steven Morelande431e272017-07-18 16:53:49 -070031#include "nativehelper/ScopedLocalRef.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 Marko05792b92015-08-03 11:56:49 +0100241 m->GetClassFromTypeIndex(classes->GetTypeItem(args_offset).type_idx_,
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000242 true /* resolve */));
243 if (dst_class.Ptr() == nullptr) {
244 CHECK(self->IsExceptionPending());
245 return false;
246 }
Andreas Gampefa4333d2017-02-14 11:10:34 -0800247 if (UNLIKELY(arg == nullptr || !arg->InstanceOf(dst_class))) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000248 ThrowIllegalArgumentException(
Ian Rogers11e4c032014-03-14 12:00:39 -0700249 StringPrintf("method %s argument %zd has type %s, got %s",
David Sehr709b0702016-10-13 09:12:37 -0700250 m->PrettyMethod(false).c_str(),
Ian Rogers53b8b092014-03-13 23:45:53 -0700251 args_offset + 1, // Humans don't count from 0.
David Sehr709b0702016-10-13 09:12:37 -0700252 mirror::Class::PrettyDescriptor(dst_class).c_str(),
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000253 mirror::Object::PrettyTypeOf(arg.Get()).c_str()).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700254 return false;
255 }
256 }
257
258#define DO_FIRST_ARG(match_descriptor, get_fn, append) { \
Andreas Gampefa4333d2017-02-14 11:10:34 -0800259 if (LIKELY(arg != nullptr && \
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000260 arg->GetClass()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700261 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000262 append(primitive_field-> get_fn(arg.Get()));
Ian Rogers53b8b092014-03-13 23:45:53 -0700263
264#define DO_ARG(match_descriptor, get_fn, append) \
Andreas Gampefa4333d2017-02-14 11:10:34 -0800265 } else if (LIKELY(arg != nullptr && \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700266 arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700267 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000268 append(primitive_field-> get_fn(arg.Get()));
Ian Rogers53b8b092014-03-13 23:45:53 -0700269
270#define DO_FAIL(expected) \
271 } else { \
272 if (arg->GetClass<>()->IsPrimitive()) { \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700273 std::string temp; \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700274 ThrowIllegalPrimitiveArgumentException(expected, \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700275 arg->GetClass<>()->GetDescriptor(&temp)); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700276 } else { \
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000277 ThrowIllegalArgumentException(\
Ian Rogers11e4c032014-03-14 12:00:39 -0700278 StringPrintf("method %s argument %zd has type %s, got %s", \
David Sehr709b0702016-10-13 09:12:37 -0700279 ArtMethod::PrettyMethod(m, false).c_str(), \
Ian Rogers53b8b092014-03-13 23:45:53 -0700280 args_offset + 1, \
281 expected, \
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000282 mirror::Object::PrettyTypeOf(arg.Get()).c_str()).c_str()); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700283 } \
284 return false; \
285 } }
286
287 switch (shorty_[i]) {
288 case 'L':
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000289 Append(arg.Get());
Ian Rogers53b8b092014-03-13 23:45:53 -0700290 break;
291 case 'Z':
292 DO_FIRST_ARG("Ljava/lang/Boolean;", GetBoolean, Append)
293 DO_FAIL("boolean")
294 break;
295 case 'B':
296 DO_FIRST_ARG("Ljava/lang/Byte;", GetByte, Append)
297 DO_FAIL("byte")
298 break;
299 case 'C':
300 DO_FIRST_ARG("Ljava/lang/Character;", GetChar, Append)
301 DO_FAIL("char")
302 break;
303 case 'S':
304 DO_FIRST_ARG("Ljava/lang/Short;", GetShort, Append)
305 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
306 DO_FAIL("short")
307 break;
308 case 'I':
309 DO_FIRST_ARG("Ljava/lang/Integer;", GetInt, Append)
310 DO_ARG("Ljava/lang/Character;", GetChar, Append)
311 DO_ARG("Ljava/lang/Short;", GetShort, Append)
312 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
313 DO_FAIL("int")
314 break;
315 case 'J':
316 DO_FIRST_ARG("Ljava/lang/Long;", GetLong, AppendWide)
317 DO_ARG("Ljava/lang/Integer;", GetInt, AppendWide)
318 DO_ARG("Ljava/lang/Character;", GetChar, AppendWide)
319 DO_ARG("Ljava/lang/Short;", GetShort, AppendWide)
320 DO_ARG("Ljava/lang/Byte;", GetByte, AppendWide)
321 DO_FAIL("long")
322 break;
323 case 'F':
324 DO_FIRST_ARG("Ljava/lang/Float;", GetFloat, AppendFloat)
325 DO_ARG("Ljava/lang/Long;", GetLong, AppendFloat)
326 DO_ARG("Ljava/lang/Integer;", GetInt, AppendFloat)
327 DO_ARG("Ljava/lang/Character;", GetChar, AppendFloat)
328 DO_ARG("Ljava/lang/Short;", GetShort, AppendFloat)
329 DO_ARG("Ljava/lang/Byte;", GetByte, AppendFloat)
330 DO_FAIL("float")
331 break;
332 case 'D':
333 DO_FIRST_ARG("Ljava/lang/Double;", GetDouble, AppendDouble)
334 DO_ARG("Ljava/lang/Float;", GetFloat, AppendDouble)
335 DO_ARG("Ljava/lang/Long;", GetLong, AppendDouble)
336 DO_ARG("Ljava/lang/Integer;", GetInt, AppendDouble)
337 DO_ARG("Ljava/lang/Character;", GetChar, AppendDouble)
338 DO_ARG("Ljava/lang/Short;", GetShort, AppendDouble)
339 DO_ARG("Ljava/lang/Byte;", GetByte, AppendDouble)
340 DO_FAIL("double")
341 break;
342#ifndef NDEBUG
343 default:
344 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
Ian Rogersa0485602014-12-02 15:48:04 -0800345 UNREACHABLE();
Ian Rogers53b8b092014-03-13 23:45:53 -0700346#endif
347 }
348#undef DO_FIRST_ARG
349#undef DO_ARG
350#undef DO_FAIL
351 }
352 return true;
353 }
354
355 private:
356 enum { kSmallArgArraySize = 16 };
357 const char* const shorty_;
358 const uint32_t shorty_len_;
359 uint32_t num_bytes_;
360 uint32_t* arg_array_;
361 uint32_t small_arg_array_[kSmallArgArraySize];
Ian Rogers700a4022014-05-19 16:49:03 -0700362 std::unique_ptr<uint32_t[]> large_arg_array_;
Ian Rogers53b8b092014-03-13 23:45:53 -0700363};
364
Mathieu Chartiere401d142015-04-22 13:56:20 -0700365static void CheckMethodArguments(JavaVMExt* vm, ArtMethod* m, uint32_t* args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700366 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700367 const DexFile::TypeList* params = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700368 if (params == nullptr) {
369 return; // No arguments so nothing to check.
370 }
371 uint32_t offset = 0;
372 uint32_t num_params = params->Size();
373 size_t error_count = 0;
374 if (!m->IsStatic()) {
375 offset = 1;
376 }
Ian Rogersa0485602014-12-02 15:48:04 -0800377 // TODO: If args contain object references, it may cause problems.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700378 Thread* const self = Thread::Current();
Ian Rogers53b8b092014-03-13 23:45:53 -0700379 for (uint32_t i = 0; i < num_params; i++) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800380 dex::TypeIndex type_idx = params->GetTypeItem(i).type_idx_;
Vladimir Marko942fd312017-01-16 20:52:19 +0000381 ObjPtr<mirror::Class> param_type(m->GetClassFromTypeIndex(type_idx, true /* resolve */));
Ian Rogers53b8b092014-03-13 23:45:53 -0700382 if (param_type == nullptr) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700383 CHECK(self->IsExceptionPending());
384 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700385 << m->GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000386 << self->GetException()->Dump();
Ian Rogers53b8b092014-03-13 23:45:53 -0700387 self->ClearException();
388 ++error_count;
389 } else if (!param_type->IsPrimitive()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700390 // TODO: There is a compaction bug here since GetClassFromTypeIdx can cause thread suspension,
391 // this is a hard to fix problem since the args can contain Object*, we need to save and
392 // restore them by using a visitor similar to the ones used in the trampoline entrypoints.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700393 ObjPtr<mirror::Object> argument =
Ian Rogers68d8b422014-07-17 11:09:10 -0700394 (reinterpret_cast<StackReference<mirror::Object>*>(&args[i + offset]))->AsMirrorPtr();
Ian Rogers53b8b092014-03-13 23:45:53 -0700395 if (argument != nullptr && !argument->InstanceOf(param_type)) {
396 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
David Sehr709b0702016-10-13 09:12:37 -0700397 << argument->PrettyTypeOf() << " as argument " << (i + 1)
398 << " to " << m->PrettyMethod();
Ian Rogers53b8b092014-03-13 23:45:53 -0700399 ++error_count;
400 }
401 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
402 offset++;
Ian Rogers68d8b422014-07-17 11:09:10 -0700403 } else {
404 int32_t arg = static_cast<int32_t>(args[i + offset]);
405 if (param_type->IsPrimitiveBoolean()) {
406 if (arg != JNI_TRUE && arg != JNI_FALSE) {
407 LOG(ERROR) << "JNI ERROR (app bug): expected jboolean (0/1) but got value of "
David Sehr709b0702016-10-13 09:12:37 -0700408 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700409 ++error_count;
410 }
411 } else if (param_type->IsPrimitiveByte()) {
412 if (arg < -128 || arg > 127) {
413 LOG(ERROR) << "JNI ERROR (app bug): expected jbyte but got value of "
David Sehr709b0702016-10-13 09:12:37 -0700414 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700415 ++error_count;
416 }
417 } else if (param_type->IsPrimitiveChar()) {
418 if (args[i + offset] > 0xFFFF) {
419 LOG(ERROR) << "JNI ERROR (app bug): expected jchar but got value of "
David Sehr709b0702016-10-13 09:12:37 -0700420 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700421 ++error_count;
422 }
423 } else if (param_type->IsPrimitiveShort()) {
424 if (arg < -32768 || arg > 0x7FFF) {
425 LOG(ERROR) << "JNI ERROR (app bug): expected jshort but got value of "
David Sehr709b0702016-10-13 09:12:37 -0700426 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700427 ++error_count;
428 }
429 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700430 }
431 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700432 if (UNLIKELY(error_count > 0)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700433 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
434 // with an argument.
Ian Rogers68d8b422014-07-17 11:09:10 -0700435 vm->JniAbortF(nullptr, "bad arguments passed to %s (see above for details)",
David Sehr709b0702016-10-13 09:12:37 -0700436 m->PrettyMethod().c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700437 }
438}
439
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700440static ArtMethod* FindVirtualMethod(ObjPtr<mirror::Object> receiver, ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700441 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700442 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method, kRuntimePointerSize);
Ian Rogers53b8b092014-03-13 23:45:53 -0700443}
444
445
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700446static void InvokeWithArgArray(const ScopedObjectAccessAlreadyRunnable& soa,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700447 ArtMethod* method, ArgArray* arg_array, JValue* result,
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700448 const char* shorty)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700449 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700450 uint32_t* args = arg_array->GetArray();
451 if (UNLIKELY(soa.Env()->check_jni)) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700452 CheckMethodArguments(soa.Vm(), method->GetInterfaceMethodIfProxy(kRuntimePointerSize), args);
Ian Rogers53b8b092014-03-13 23:45:53 -0700453 }
454 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, shorty);
455}
456
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700457JValue InvokeWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
458 va_list args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700459 REQUIRES_SHARED(Locks::mutator_lock_) {
Dave Allison648d7112014-07-25 16:15:27 -0700460 // We want to make sure that the stack is not within a small distance from the
461 // protected region in case we are calling into a leaf function whose stack
462 // check has been elided.
463 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
464 ThrowStackOverflowError(soa.Self());
465 return JValue();
466 }
467
Andreas Gampe13b27842016-11-07 16:48:23 -0800468 ArtMethod* method = jni::DecodeArtMethod(mid);
Jeff Hao39b6c242015-05-19 20:30:23 -0700469 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
470 if (is_string_init) {
471 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100472 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700473 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700474 ObjPtr<mirror::Object> receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700475 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700476 const char* shorty =
477 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700478 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700479 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700480 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700481 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700482 if (is_string_init) {
483 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700484 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700485 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700486 return result;
487}
488
Jeff Hao39b6c242015-05-19 20:30:23 -0700489JValue InvokeWithJValues(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
490 jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700491 // We want to make sure that the stack is not within a small distance from the
492 // protected region in case we are calling into a leaf function whose stack
493 // check has been elided.
494 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
495 ThrowStackOverflowError(soa.Self());
496 return JValue();
497 }
498
Andreas Gampe13b27842016-11-07 16:48:23 -0800499 ArtMethod* method = jni::DecodeArtMethod(mid);
Jeff Hao39b6c242015-05-19 20:30:23 -0700500 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
501 if (is_string_init) {
502 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100503 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700504 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700505 ObjPtr<mirror::Object> receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700506 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700507 const char* shorty =
508 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700509 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700510 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700511 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700512 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700513 if (is_string_init) {
514 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700515 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700516 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700517 return result;
518}
519
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700520JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
Jeff Hao39b6c242015-05-19 20:30:23 -0700521 jobject obj, jmethodID mid, jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700522 // We want to make sure that the stack is not within a small distance from the
523 // protected region in case we are calling into a leaf function whose stack
524 // check has been elided.
525 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
526 ThrowStackOverflowError(soa.Self());
527 return JValue();
528 }
529
Mathieu Chartier0795f232016-09-27 18:43:30 -0700530 ObjPtr<mirror::Object> receiver = soa.Decode<mirror::Object>(obj);
Andreas Gampe13b27842016-11-07 16:48:23 -0800531 ArtMethod* method = FindVirtualMethod(receiver, jni::DecodeArtMethod(mid));
Jeff Hao39b6c242015-05-19 20:30:23 -0700532 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
533 if (is_string_init) {
534 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100535 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700536 receiver = nullptr;
537 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700538 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700539 const char* shorty =
540 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700541 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700542 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700543 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700544 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700545 if (is_string_init) {
546 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700547 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700548 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700549 return result;
550}
551
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700552JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
Ian Rogers53b8b092014-03-13 23:45:53 -0700553 jobject obj, jmethodID mid, va_list args) {
Dave Allison648d7112014-07-25 16:15:27 -0700554 // We want to make sure that the stack is not within a small distance from the
555 // protected region in case we are calling into a leaf function whose stack
556 // check has been elided.
557 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
558 ThrowStackOverflowError(soa.Self());
559 return JValue();
560 }
561
Mathieu Chartier0795f232016-09-27 18:43:30 -0700562 ObjPtr<mirror::Object> receiver = soa.Decode<mirror::Object>(obj);
Andreas Gampe13b27842016-11-07 16:48:23 -0800563 ArtMethod* method = FindVirtualMethod(receiver, jni::DecodeArtMethod(mid));
Jeff Hao39b6c242015-05-19 20:30:23 -0700564 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
565 if (is_string_init) {
566 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100567 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700568 receiver = nullptr;
569 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700570 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700571 const char* shorty =
572 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700573 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700574 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700575 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700576 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700577 if (is_string_init) {
578 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700579 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700580 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700581 return result;
582}
583
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700584jobject InvokeMethod(const ScopedObjectAccessAlreadyRunnable& soa, jobject javaMethod,
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700585 jobject javaReceiver, jobject javaArgs, size_t num_frames) {
Dave Allison648d7112014-07-25 16:15:27 -0700586 // We want to make sure that the stack is not within a small distance from the
587 // protected region in case we are calling into a leaf function whose stack
588 // check has been elided.
589 if (UNLIKELY(__builtin_frame_address(0) <
590 soa.Self()->GetStackEndForInterpreter(true))) {
591 ThrowStackOverflowError(soa.Self());
592 return nullptr;
593 }
594
Mathieu Chartier0795f232016-09-27 18:43:30 -0700595 ObjPtr<mirror::Executable> executable = soa.Decode<mirror::Executable>(javaMethod);
Neil Fuller0e844392016-09-08 13:43:31 +0100596 const bool accessible = executable->IsAccessible();
597 ArtMethod* m = executable->GetArtMethod();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700598
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700599 ObjPtr<mirror::Class> declaring_class = m->GetDeclaringClass();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800600 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700601 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -0700602 HandleWrapperObjPtr<mirror::Class> h_class(hs.NewHandleWrapper(&declaring_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700603 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), h_class, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800604 return nullptr;
605 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700606 }
607
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700608 ObjPtr<mirror::Object> receiver;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700609 if (!m->IsStatic()) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800610 // Replace calls to String.<init> with equivalent StringFactory call.
611 if (declaring_class->IsStringClass() && m->IsConstructor()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100612 m = WellKnownClasses::StringInitToStringFactory(m);
Jeff Hao848f70a2014-01-15 13:49:50 -0800613 CHECK(javaReceiver == nullptr);
614 } else {
615 // Check that the receiver is non-null and an instance of the field's declaring class.
Mathieu Chartier0795f232016-09-27 18:43:30 -0700616 receiver = soa.Decode<mirror::Object>(javaReceiver);
Jeff Hao848f70a2014-01-15 13:49:50 -0800617 if (!VerifyObjectIsClass(receiver, declaring_class)) {
618 return nullptr;
619 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700620
Jeff Hao848f70a2014-01-15 13:49:50 -0800621 // Find the actual implementation of the virtual method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700622 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m, kRuntimePointerSize);
Jeff Hao848f70a2014-01-15 13:49:50 -0800623 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700624 }
625
626 // Get our arrays of arguments and their types, and check they're the same size.
Mathieu Chartier0795f232016-09-27 18:43:30 -0700627 ObjPtr<mirror::ObjectArray<mirror::Object>> objects =
628 soa.Decode<mirror::ObjectArray<mirror::Object>>(javaArgs);
Andreas Gampe542451c2016-07-26 09:02:02 -0700629 auto* np_method = m->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700630 const DexFile::TypeList* classes = np_method->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700631 uint32_t classes_size = (classes == nullptr) ? 0 : classes->Size();
632 uint32_t arg_count = (objects != nullptr) ? objects->GetLength() : 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800633 if (arg_count != classes_size) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000634 ThrowIllegalArgumentException(StringPrintf("Wrong number of arguments; expected %d, got %d",
Ian Rogers62d6c772013-02-27 08:32:07 -0800635 classes_size, arg_count).c_str());
Ian Rogersa0485602014-12-02 15:48:04 -0800636 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700637 }
638
Jeff Haocb4581a2014-03-28 15:43:37 -0700639 // If method is not set to be accessible, verify it can be accessed by the caller.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700640 ObjPtr<mirror::Class> calling_class;
Mathieu Chartier268764d2016-09-13 12:09:38 -0700641 if (!accessible && !VerifyAccess(soa.Self(),
642 receiver,
643 declaring_class,
644 m->GetAccessFlags(),
645 &calling_class,
646 num_frames)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000647 ThrowIllegalAccessException(
Andreas Gampec0d82292014-09-23 10:38:30 -0700648 StringPrintf("Class %s cannot access %s method %s of class %s",
David Sehr709b0702016-10-13 09:12:37 -0700649 calling_class == nullptr ? "null" : calling_class->PrettyClass().c_str(),
Andreas Gampec0d82292014-09-23 10:38:30 -0700650 PrettyJavaAccessFlags(m->GetAccessFlags()).c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700651 m->PrettyMethod().c_str(),
Andreas Gampec0d82292014-09-23 10:38:30 -0700652 m->GetDeclaringClass() == nullptr ? "null" :
David Sehr709b0702016-10-13 09:12:37 -0700653 m->GetDeclaringClass()->PrettyClass().c_str()).c_str());
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700654 return nullptr;
655 }
656
Ian Rogers53b8b092014-03-13 23:45:53 -0700657 // Invoke the method.
658 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700659 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700660 const char* shorty = np_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700661 ArgArray arg_array(shorty, shorty_len);
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000662 if (!arg_array.BuildArgArrayFromObjectArray(receiver, objects, np_method, soa.Self())) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700663 CHECK(soa.Self()->IsExceptionPending());
664 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700665 }
666
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700667 InvokeWithArgArray(soa, m, &arg_array, &result, shorty);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700668
669 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700670 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700671 // If we get another exception when we are trying to wrap, then just use that instead.
Chang Xing443f8622017-06-08 11:31:48 -0700672 ScopedLocalRef<jthrowable> th(soa.Env(), soa.Env()->ExceptionOccurred());
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700673 soa.Self()->ClearException();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700674 jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException");
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700675 if (exception_class == nullptr) {
Mathieu Chartiercc9d1cb2017-03-16 15:50:57 -0700676 soa.Self()->AssertPendingException();
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700677 return nullptr;
678 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700679 jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700680 CHECK(mid != nullptr);
Chang Xing443f8622017-06-08 11:31:48 -0700681 jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th.get());
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700682 if (exception_instance == nullptr) {
Mathieu Chartiercc9d1cb2017-03-16 15:50:57 -0700683 soa.Self()->AssertPendingException();
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700684 return nullptr;
685 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700686 soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance));
Ian Rogersa0485602014-12-02 15:48:04 -0800687 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700688 }
689
690 // Box if necessary and return.
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -0700691 return soa.AddLocalReference<jobject>(BoxPrimitive(Primitive::GetType(shorty[0]), result));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700692}
693
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700694ObjPtr<mirror::Object> BoxPrimitive(Primitive::Type src_class, const JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700695 if (src_class == Primitive::kPrimNot) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700696 return MakeObjPtr(value.GetL());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700697 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700698 if (src_class == Primitive::kPrimVoid) {
699 // There's no such thing as a void field, and void methods invoked via reflection return null.
700 return nullptr;
701 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700702
Ian Rogers84956ff2014-03-26 23:52:41 -0700703 jmethodID m = nullptr;
Ian Rogers0177e532014-02-11 16:30:46 -0800704 const char* shorty;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700705 switch (src_class) {
706 case Primitive::kPrimBoolean:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700707 m = WellKnownClasses::java_lang_Boolean_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800708 shorty = "LZ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700709 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700710 case Primitive::kPrimByte:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700711 m = WellKnownClasses::java_lang_Byte_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800712 shorty = "LB";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700713 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700714 case Primitive::kPrimChar:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700715 m = WellKnownClasses::java_lang_Character_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800716 shorty = "LC";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700717 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700718 case Primitive::kPrimDouble:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700719 m = WellKnownClasses::java_lang_Double_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800720 shorty = "LD";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700721 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700722 case Primitive::kPrimFloat:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700723 m = WellKnownClasses::java_lang_Float_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800724 shorty = "LF";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700725 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700726 case Primitive::kPrimInt:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700727 m = WellKnownClasses::java_lang_Integer_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800728 shorty = "LI";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700729 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700730 case Primitive::kPrimLong:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700731 m = WellKnownClasses::java_lang_Long_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800732 shorty = "LJ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700733 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700734 case Primitive::kPrimShort:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700735 m = WellKnownClasses::java_lang_Short_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800736 shorty = "LS";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700737 break;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700738 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700739 LOG(FATAL) << static_cast<int>(src_class);
Ian Rogers0177e532014-02-11 16:30:46 -0800740 shorty = nullptr;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700741 }
742
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700743 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers53b8b092014-03-13 23:45:53 -0700744 DCHECK_EQ(soa.Self()->GetState(), kRunnable);
Jeff Hao5d917302013-02-27 17:57:33 -0800745
Ian Rogers53b8b092014-03-13 23:45:53 -0700746 ArgArray arg_array(shorty, 2);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800747 JValue result;
Jeff Hao5d917302013-02-27 17:57:33 -0800748 if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) {
749 arg_array.AppendWide(value.GetJ());
750 } else {
751 arg_array.Append(value.GetI());
752 }
753
Andreas Gampe13b27842016-11-07 16:48:23 -0800754 jni::DecodeArtMethod(m)->Invoke(soa.Self(),
755 arg_array.GetArray(),
756 arg_array.GetNumBytes(),
757 &result,
758 shorty);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800759 return result.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700760}
761
Mathieu Chartierc7853442015-03-27 14:35:38 -0700762static std::string UnboxingFailureKind(ArtField* f)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700763 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700764 if (f != nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700765 return "field " + f->PrettyField(false);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700766 }
767 return "result";
768}
769
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700770static bool UnboxPrimitive(ObjPtr<mirror::Object> o,
771 ObjPtr<mirror::Class> dst_class,
772 ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -0700773 JValue* unboxed_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700774 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700775 bool unbox_for_result = (f == nullptr);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700776 if (!dst_class->IsPrimitive()) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700777 if (UNLIKELY(o != nullptr && !o->InstanceOf(dst_class))) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800778 if (!unbox_for_result) {
David Sehr709b0702016-10-13 09:12:37 -0700779 ThrowIllegalArgumentException(
780 StringPrintf("%s has type %s, got %s",
781 UnboxingFailureKind(f).c_str(),
782 dst_class->PrettyDescriptor().c_str(),
783 o->PrettyTypeOf().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800784 } else {
David Sehr709b0702016-10-13 09:12:37 -0700785 ThrowClassCastException(
786 StringPrintf("Couldn't convert result of type %s to %s",
787 o->PrettyTypeOf().c_str(),
788 dst_class->PrettyDescriptor().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800789 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700790 return false;
791 }
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700792 unboxed_value->SetL(o);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700793 return true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800794 }
795 if (UNLIKELY(dst_class->GetPrimitiveType() == Primitive::kPrimVoid)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000796 ThrowIllegalArgumentException(StringPrintf("Can't unbox %s to void",
Ian Rogers84956ff2014-03-26 23:52:41 -0700797 UnboxingFailureKind(f).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700798 return false;
799 }
Ian Rogers84956ff2014-03-26 23:52:41 -0700800 if (UNLIKELY(o == nullptr)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800801 if (!unbox_for_result) {
David Sehr709b0702016-10-13 09:12:37 -0700802 ThrowIllegalArgumentException(
803 StringPrintf("%s has type %s, got null",
804 UnboxingFailureKind(f).c_str(),
805 dst_class->PrettyDescriptor().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800806 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700807 ThrowNullPointerException(
808 StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
David Sehr709b0702016-10-13 09:12:37 -0700809 dst_class->PrettyDescriptor().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800810 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700811 return false;
812 }
813
Elliott Hughes1d878f32012-04-11 15:17:54 -0700814 JValue boxed_value;
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700815 ObjPtr<mirror::Class> klass = o->GetClass();
816 ObjPtr<mirror::Class> src_class = nullptr;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700817 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700818 ArtField* primitive_field = &klass->GetIFieldsPtr()->At(0);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700819 if (klass->DescriptorEquals("Ljava/lang/Boolean;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700820 src_class = class_linker->FindPrimitiveClass('Z');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700821 boxed_value.SetZ(primitive_field->GetBoolean(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700822 } else if (klass->DescriptorEquals("Ljava/lang/Byte;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700823 src_class = class_linker->FindPrimitiveClass('B');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700824 boxed_value.SetB(primitive_field->GetByte(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700825 } else if (klass->DescriptorEquals("Ljava/lang/Character;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700826 src_class = class_linker->FindPrimitiveClass('C');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700827 boxed_value.SetC(primitive_field->GetChar(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700828 } else if (klass->DescriptorEquals("Ljava/lang/Float;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700829 src_class = class_linker->FindPrimitiveClass('F');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700830 boxed_value.SetF(primitive_field->GetFloat(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700831 } else if (klass->DescriptorEquals("Ljava/lang/Double;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700832 src_class = class_linker->FindPrimitiveClass('D');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700833 boxed_value.SetD(primitive_field->GetDouble(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700834 } else if (klass->DescriptorEquals("Ljava/lang/Integer;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700835 src_class = class_linker->FindPrimitiveClass('I');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700836 boxed_value.SetI(primitive_field->GetInt(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700837 } else if (klass->DescriptorEquals("Ljava/lang/Long;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700838 src_class = class_linker->FindPrimitiveClass('J');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700839 boxed_value.SetJ(primitive_field->GetLong(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700840 } else if (klass->DescriptorEquals("Ljava/lang/Short;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700841 src_class = class_linker->FindPrimitiveClass('S');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700842 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700843 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700844 std::string temp;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000845 ThrowIllegalArgumentException(
Ian Rogers1ff3c982014-08-12 02:30:58 -0700846 StringPrintf("%s has type %s, got %s", UnboxingFailureKind(f).c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700847 dst_class->PrettyDescriptor().c_str(),
Ian Rogers1ff3c982014-08-12 02:30:58 -0700848 PrettyDescriptor(o->GetClass()->GetDescriptor(&temp)).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700849 return false;
850 }
851
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000852 return ConvertPrimitiveValue(unbox_for_result,
Ian Rogers62d6c772013-02-27 08:32:07 -0800853 src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700854 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700855}
856
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700857bool UnboxPrimitiveForField(ObjPtr<mirror::Object> o,
858 ObjPtr<mirror::Class> dst_class,
859 ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -0700860 JValue* unboxed_value) {
861 DCHECK(f != nullptr);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000862 return UnboxPrimitive(o, dst_class, f, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700863}
864
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700865bool UnboxPrimitiveForResult(ObjPtr<mirror::Object> o,
866 ObjPtr<mirror::Class> dst_class,
867 JValue* unboxed_value) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000868 return UnboxPrimitive(o, dst_class, nullptr, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700869}
870
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700871ObjPtr<mirror::Class> GetCallingClass(Thread* self, size_t num_frames) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700872 NthCallerVisitor visitor(self, num_frames);
873 visitor.WalkStack();
874 return visitor.caller != nullptr ? visitor.caller->GetDeclaringClass() : nullptr;
875}
876
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700877bool VerifyAccess(Thread* self,
878 ObjPtr<mirror::Object> obj,
879 ObjPtr<mirror::Class> declaring_class,
880 uint32_t access_flags,
881 ObjPtr<mirror::Class>* calling_class,
882 size_t num_frames) {
Mathieu Chartier76433272014-09-26 14:32:37 -0700883 if ((access_flags & kAccPublic) != 0) {
884 return true;
885 }
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700886 ObjPtr<mirror::Class> klass = GetCallingClass(self, num_frames);
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700887 if (UNLIKELY(klass == nullptr)) {
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100888 // The caller is an attached native thread.
Mathieu Chartier76433272014-09-26 14:32:37 -0700889 return false;
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100890 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700891 *calling_class = klass;
Mathieu Chartier268764d2016-09-13 12:09:38 -0700892 return VerifyAccess(obj, declaring_class, access_flags, klass);
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700893}
894
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700895bool VerifyAccess(ObjPtr<mirror::Object> obj,
896 ObjPtr<mirror::Class> declaring_class,
Mathieu Chartier268764d2016-09-13 12:09:38 -0700897 uint32_t access_flags,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700898 ObjPtr<mirror::Class> calling_class) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700899 if (calling_class == declaring_class) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700900 return true;
901 }
Mathieu Chartier268764d2016-09-13 12:09:38 -0700902 ScopedAssertNoThreadSuspension sants("verify-access");
Jeff Haocb4581a2014-03-28 15:43:37 -0700903 if ((access_flags & kAccPrivate) != 0) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700904 return false;
905 }
Jeff Haocb4581a2014-03-28 15:43:37 -0700906 if ((access_flags & kAccProtected) != 0) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700907 if (obj != nullptr && !obj->InstanceOf(calling_class) &&
Mathieu Chartier3398c782016-09-30 10:27:43 -0700908 !declaring_class->IsInSamePackage(calling_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700909 return false;
Mathieu Chartier3398c782016-09-30 10:27:43 -0700910 } else if (declaring_class->IsAssignableFrom(calling_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700911 return true;
912 }
913 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700914 return declaring_class->IsInSamePackage(calling_class);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700915}
916
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700917void InvalidReceiverError(ObjPtr<mirror::Object> o, ObjPtr<mirror::Class> c) {
David Sehr709b0702016-10-13 09:12:37 -0700918 std::string expected_class_name(mirror::Class::PrettyDescriptor(c));
919 std::string actual_class_name(mirror::Object::PrettyTypeOf(o));
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700920 ThrowIllegalArgumentException(StringPrintf("Expected receiver of type %s, but got %s",
921 expected_class_name.c_str(),
922 actual_class_name.c_str()).c_str());
923}
924
Jeff Hao83c81952015-05-27 19:29:29 -0700925// This only works if there's one reference which points to the object in obj.
926// Will need to be fixed if there's cases where it's not.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700927void UpdateReference(Thread* self, jobject obj, ObjPtr<mirror::Object> result) {
Jeff Hao83c81952015-05-27 19:29:29 -0700928 IndirectRef ref = reinterpret_cast<IndirectRef>(obj);
Andreas Gampedc061d02016-10-24 13:19:37 -0700929 IndirectRefKind kind = IndirectReferenceTable::GetIndirectRefKind(ref);
Jeff Hao83c81952015-05-27 19:29:29 -0700930 if (kind == kLocal) {
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -0700931 self->GetJniEnv()->locals.Update(obj, result);
Jeff Hao83c81952015-05-27 19:29:29 -0700932 } else if (kind == kHandleScopeOrInvalid) {
933 LOG(FATAL) << "Unsupported UpdateReference for kind kHandleScopeOrInvalid";
934 } else if (kind == kGlobal) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700935 self->GetJniEnv()->vm->UpdateGlobal(self, ref, result);
Jeff Hao83c81952015-05-27 19:29:29 -0700936 } else {
937 DCHECK_EQ(kind, kWeakGlobal);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700938 self->GetJniEnv()->vm->UpdateWeakGlobal(self, ref, result);
Jeff Hao83c81952015-05-27 19:29:29 -0700939 }
940}
941
Elliott Hughes418d20f2011-09-22 14:00:39 -0700942} // namespace art