blob: 3c64d4072035e78c3415c9e42984b46c8aa1515e [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"
Elliott Hughes418d20f2011-09-22 14:00:39 -070026#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/class-inl.h"
Neil Fuller0e844392016-09-08 13:43:31 +010028#include "mirror/executable.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/object_array-inl.h"
Jeff Hao11d5d8f2014-03-26 15:08:20 -070030#include "nth_caller_visitor.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070031#include "scoped_thread_state_change-inl.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010032#include "stack_reference.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070033#include "well_known_classes.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070034
Elliott Hughes418d20f2011-09-22 14:00:39 -070035namespace art {
36
Andreas Gampe46ee31b2016-12-14 10:11:49 -080037using android::base::StringPrintf;
38
Ian Rogers53b8b092014-03-13 23:45:53 -070039class ArgArray {
40 public:
Roland Levillain3887c462015-08-12 18:15:42 +010041 ArgArray(const char* shorty, uint32_t shorty_len)
Ian Rogers53b8b092014-03-13 23:45:53 -070042 : shorty_(shorty), shorty_len_(shorty_len), num_bytes_(0) {
43 size_t num_slots = shorty_len + 1; // +1 in case of receiver.
44 if (LIKELY((num_slots * 2) < kSmallArgArraySize)) {
45 // We can trivially use the small arg array.
46 arg_array_ = small_arg_array_;
47 } else {
48 // Analyze shorty to see if we need the large arg array.
49 for (size_t i = 1; i < shorty_len; ++i) {
50 char c = shorty[i];
51 if (c == 'J' || c == 'D') {
52 num_slots++;
53 }
54 }
55 if (num_slots <= kSmallArgArraySize) {
56 arg_array_ = small_arg_array_;
57 } else {
58 large_arg_array_.reset(new uint32_t[num_slots]);
59 arg_array_ = large_arg_array_.get();
60 }
61 }
62 }
63
64 uint32_t* GetArray() {
65 return arg_array_;
66 }
67
68 uint32_t GetNumBytes() {
69 return num_bytes_;
70 }
71
72 void Append(uint32_t value) {
73 arg_array_[num_bytes_ / 4] = value;
74 num_bytes_ += 4;
75 }
76
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070077 void Append(ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070078 Append(StackReference<mirror::Object>::FromMirrorPtr(obj.Ptr()).AsVRegValue());
Ian Rogers53b8b092014-03-13 23:45:53 -070079 }
80
81 void AppendWide(uint64_t value) {
Ian Rogers53b8b092014-03-13 23:45:53 -070082 arg_array_[num_bytes_ / 4] = value;
83 arg_array_[(num_bytes_ / 4) + 1] = value >> 32;
84 num_bytes_ += 8;
85 }
86
87 void AppendFloat(float value) {
88 jvalue jv;
89 jv.f = value;
90 Append(jv.i);
91 }
92
93 void AppendDouble(double value) {
94 jvalue jv;
95 jv.d = value;
96 AppendWide(jv.j);
97 }
98
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070099 void BuildArgArrayFromVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700100 ObjPtr<mirror::Object> receiver,
101 va_list ap)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700102 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700103 // Set receiver if non-null (method is not static)
104 if (receiver != nullptr) {
105 Append(receiver);
106 }
107 for (size_t i = 1; i < shorty_len_; ++i) {
108 switch (shorty_[i]) {
109 case 'Z':
110 case 'B':
111 case 'C':
112 case 'S':
113 case 'I':
114 Append(va_arg(ap, jint));
115 break;
116 case 'F':
117 AppendFloat(va_arg(ap, jdouble));
118 break;
119 case 'L':
Mathieu Chartier0795f232016-09-27 18:43:30 -0700120 Append(soa.Decode<mirror::Object>(va_arg(ap, jobject)));
Ian Rogers53b8b092014-03-13 23:45:53 -0700121 break;
122 case 'D':
123 AppendDouble(va_arg(ap, jdouble));
124 break;
125 case 'J':
126 AppendWide(va_arg(ap, jlong));
127 break;
128#ifndef NDEBUG
129 default:
130 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
131#endif
132 }
133 }
134 }
135
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700136 void BuildArgArrayFromJValues(const ScopedObjectAccessAlreadyRunnable& soa,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700137 ObjPtr<mirror::Object> receiver, jvalue* args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700138 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700139 // Set receiver if non-null (method is not static)
140 if (receiver != nullptr) {
141 Append(receiver);
142 }
143 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
144 switch (shorty_[i]) {
145 case 'Z':
146 Append(args[args_offset].z);
147 break;
148 case 'B':
149 Append(args[args_offset].b);
150 break;
151 case 'C':
152 Append(args[args_offset].c);
153 break;
154 case 'S':
155 Append(args[args_offset].s);
156 break;
157 case 'I':
158 case 'F':
159 Append(args[args_offset].i);
160 break;
161 case 'L':
Mathieu Chartier0795f232016-09-27 18:43:30 -0700162 Append(soa.Decode<mirror::Object>(args[args_offset].l));
Ian Rogers53b8b092014-03-13 23:45:53 -0700163 break;
164 case 'D':
165 case 'J':
166 AppendWide(args[args_offset].j);
167 break;
168#ifndef NDEBUG
169 default:
170 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
171#endif
172 }
173 }
174 }
175
176 void BuildArgArrayFromFrame(ShadowFrame* shadow_frame, uint32_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700177 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700178 // Set receiver if non-null (method is not static)
179 size_t cur_arg = arg_offset;
180 if (!shadow_frame->GetMethod()->IsStatic()) {
181 Append(shadow_frame->GetVReg(cur_arg));
182 cur_arg++;
183 }
184 for (size_t i = 1; i < shorty_len_; ++i) {
185 switch (shorty_[i]) {
186 case 'Z':
187 case 'B':
188 case 'C':
189 case 'S':
190 case 'I':
191 case 'F':
192 case 'L':
193 Append(shadow_frame->GetVReg(cur_arg));
194 cur_arg++;
195 break;
196 case 'D':
197 case 'J':
198 AppendWide(shadow_frame->GetVRegLong(cur_arg));
199 cur_arg++;
200 cur_arg++;
201 break;
202#ifndef NDEBUG
203 default:
204 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
205#endif
206 }
207 }
208 }
209
210 static void ThrowIllegalPrimitiveArgumentException(const char* expected,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700211 const char* found_descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700212 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000213 ThrowIllegalArgumentException(
Ian Rogers53b8b092014-03-13 23:45:53 -0700214 StringPrintf("Invalid primitive conversion from %s to %s", expected,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700215 PrettyDescriptor(found_descriptor).c_str()).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700216 }
217
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700218 bool BuildArgArrayFromObjectArray(ObjPtr<mirror::Object> receiver,
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000219 ObjPtr<mirror::ObjectArray<mirror::Object>> raw_args,
220 ArtMethod* m,
221 Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700222 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700223 const DexFile::TypeList* classes = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700224 // Set receiver if non-null (method is not static)
225 if (receiver != nullptr) {
226 Append(receiver);
227 }
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000228 StackHandleScope<2> hs(self);
229 MutableHandle<mirror::Object> arg(hs.NewHandle<mirror::Object>(nullptr));
230 Handle<mirror::ObjectArray<mirror::Object>> args(
231 hs.NewHandle<mirror::ObjectArray<mirror::Object>>(raw_args));
Ian Rogers53b8b092014-03-13 23:45:53 -0700232 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000233 arg.Assign(args->Get(args_offset));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800234 if (((shorty_[i] == 'L') && (arg != nullptr)) ||
235 ((arg == nullptr && shorty_[i] != 'L'))) {
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000236 // TODO: The method's parameter's type must have been previously resolved, yet
237 // we've seen cases where it's not b/34440020.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700238 ObjPtr<mirror::Class> dst_class(
Vladimir Marko05792b92015-08-03 11:56:49 +0100239 m->GetClassFromTypeIndex(classes->GetTypeItem(args_offset).type_idx_,
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000240 true /* resolve */));
241 if (dst_class.Ptr() == nullptr) {
242 CHECK(self->IsExceptionPending());
243 return false;
244 }
Andreas Gampefa4333d2017-02-14 11:10:34 -0800245 if (UNLIKELY(arg == nullptr || !arg->InstanceOf(dst_class))) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000246 ThrowIllegalArgumentException(
Ian Rogers11e4c032014-03-14 12:00:39 -0700247 StringPrintf("method %s argument %zd has type %s, got %s",
David Sehr709b0702016-10-13 09:12:37 -0700248 m->PrettyMethod(false).c_str(),
Ian Rogers53b8b092014-03-13 23:45:53 -0700249 args_offset + 1, // Humans don't count from 0.
David Sehr709b0702016-10-13 09:12:37 -0700250 mirror::Class::PrettyDescriptor(dst_class).c_str(),
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000251 mirror::Object::PrettyTypeOf(arg.Get()).c_str()).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700252 return false;
253 }
254 }
255
256#define DO_FIRST_ARG(match_descriptor, get_fn, append) { \
Andreas Gampefa4333d2017-02-14 11:10:34 -0800257 if (LIKELY(arg != nullptr && \
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000258 arg->GetClass()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700259 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000260 append(primitive_field-> get_fn(arg.Get()));
Ian Rogers53b8b092014-03-13 23:45:53 -0700261
262#define DO_ARG(match_descriptor, get_fn, append) \
Andreas Gampefa4333d2017-02-14 11:10:34 -0800263 } else if (LIKELY(arg != nullptr && \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700264 arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700265 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000266 append(primitive_field-> get_fn(arg.Get()));
Ian Rogers53b8b092014-03-13 23:45:53 -0700267
268#define DO_FAIL(expected) \
269 } else { \
270 if (arg->GetClass<>()->IsPrimitive()) { \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700271 std::string temp; \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700272 ThrowIllegalPrimitiveArgumentException(expected, \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700273 arg->GetClass<>()->GetDescriptor(&temp)); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700274 } else { \
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000275 ThrowIllegalArgumentException(\
Ian Rogers11e4c032014-03-14 12:00:39 -0700276 StringPrintf("method %s argument %zd has type %s, got %s", \
David Sehr709b0702016-10-13 09:12:37 -0700277 ArtMethod::PrettyMethod(m, false).c_str(), \
Ian Rogers53b8b092014-03-13 23:45:53 -0700278 args_offset + 1, \
279 expected, \
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000280 mirror::Object::PrettyTypeOf(arg.Get()).c_str()).c_str()); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700281 } \
282 return false; \
283 } }
284
285 switch (shorty_[i]) {
286 case 'L':
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000287 Append(arg.Get());
Ian Rogers53b8b092014-03-13 23:45:53 -0700288 break;
289 case 'Z':
290 DO_FIRST_ARG("Ljava/lang/Boolean;", GetBoolean, Append)
291 DO_FAIL("boolean")
292 break;
293 case 'B':
294 DO_FIRST_ARG("Ljava/lang/Byte;", GetByte, Append)
295 DO_FAIL("byte")
296 break;
297 case 'C':
298 DO_FIRST_ARG("Ljava/lang/Character;", GetChar, Append)
299 DO_FAIL("char")
300 break;
301 case 'S':
302 DO_FIRST_ARG("Ljava/lang/Short;", GetShort, Append)
303 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
304 DO_FAIL("short")
305 break;
306 case 'I':
307 DO_FIRST_ARG("Ljava/lang/Integer;", GetInt, Append)
308 DO_ARG("Ljava/lang/Character;", GetChar, Append)
309 DO_ARG("Ljava/lang/Short;", GetShort, Append)
310 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
311 DO_FAIL("int")
312 break;
313 case 'J':
314 DO_FIRST_ARG("Ljava/lang/Long;", GetLong, AppendWide)
315 DO_ARG("Ljava/lang/Integer;", GetInt, AppendWide)
316 DO_ARG("Ljava/lang/Character;", GetChar, AppendWide)
317 DO_ARG("Ljava/lang/Short;", GetShort, AppendWide)
318 DO_ARG("Ljava/lang/Byte;", GetByte, AppendWide)
319 DO_FAIL("long")
320 break;
321 case 'F':
322 DO_FIRST_ARG("Ljava/lang/Float;", GetFloat, AppendFloat)
323 DO_ARG("Ljava/lang/Long;", GetLong, AppendFloat)
324 DO_ARG("Ljava/lang/Integer;", GetInt, AppendFloat)
325 DO_ARG("Ljava/lang/Character;", GetChar, AppendFloat)
326 DO_ARG("Ljava/lang/Short;", GetShort, AppendFloat)
327 DO_ARG("Ljava/lang/Byte;", GetByte, AppendFloat)
328 DO_FAIL("float")
329 break;
330 case 'D':
331 DO_FIRST_ARG("Ljava/lang/Double;", GetDouble, AppendDouble)
332 DO_ARG("Ljava/lang/Float;", GetFloat, AppendDouble)
333 DO_ARG("Ljava/lang/Long;", GetLong, AppendDouble)
334 DO_ARG("Ljava/lang/Integer;", GetInt, AppendDouble)
335 DO_ARG("Ljava/lang/Character;", GetChar, AppendDouble)
336 DO_ARG("Ljava/lang/Short;", GetShort, AppendDouble)
337 DO_ARG("Ljava/lang/Byte;", GetByte, AppendDouble)
338 DO_FAIL("double")
339 break;
340#ifndef NDEBUG
341 default:
342 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
Ian Rogersa0485602014-12-02 15:48:04 -0800343 UNREACHABLE();
Ian Rogers53b8b092014-03-13 23:45:53 -0700344#endif
345 }
346#undef DO_FIRST_ARG
347#undef DO_ARG
348#undef DO_FAIL
349 }
350 return true;
351 }
352
353 private:
354 enum { kSmallArgArraySize = 16 };
355 const char* const shorty_;
356 const uint32_t shorty_len_;
357 uint32_t num_bytes_;
358 uint32_t* arg_array_;
359 uint32_t small_arg_array_[kSmallArgArraySize];
Ian Rogers700a4022014-05-19 16:49:03 -0700360 std::unique_ptr<uint32_t[]> large_arg_array_;
Ian Rogers53b8b092014-03-13 23:45:53 -0700361};
362
Mathieu Chartiere401d142015-04-22 13:56:20 -0700363static void CheckMethodArguments(JavaVMExt* vm, ArtMethod* m, uint32_t* args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700364 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700365 const DexFile::TypeList* params = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700366 if (params == nullptr) {
367 return; // No arguments so nothing to check.
368 }
369 uint32_t offset = 0;
370 uint32_t num_params = params->Size();
371 size_t error_count = 0;
372 if (!m->IsStatic()) {
373 offset = 1;
374 }
Ian Rogersa0485602014-12-02 15:48:04 -0800375 // TODO: If args contain object references, it may cause problems.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700376 Thread* const self = Thread::Current();
Ian Rogers53b8b092014-03-13 23:45:53 -0700377 for (uint32_t i = 0; i < num_params; i++) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800378 dex::TypeIndex type_idx = params->GetTypeItem(i).type_idx_;
Vladimir Marko942fd312017-01-16 20:52:19 +0000379 ObjPtr<mirror::Class> param_type(m->GetClassFromTypeIndex(type_idx, true /* resolve */));
Ian Rogers53b8b092014-03-13 23:45:53 -0700380 if (param_type == nullptr) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700381 CHECK(self->IsExceptionPending());
382 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700383 << m->GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000384 << self->GetException()->Dump();
Ian Rogers53b8b092014-03-13 23:45:53 -0700385 self->ClearException();
386 ++error_count;
387 } else if (!param_type->IsPrimitive()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700388 // TODO: There is a compaction bug here since GetClassFromTypeIdx can cause thread suspension,
389 // this is a hard to fix problem since the args can contain Object*, we need to save and
390 // restore them by using a visitor similar to the ones used in the trampoline entrypoints.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700391 ObjPtr<mirror::Object> argument =
Ian Rogers68d8b422014-07-17 11:09:10 -0700392 (reinterpret_cast<StackReference<mirror::Object>*>(&args[i + offset]))->AsMirrorPtr();
Ian Rogers53b8b092014-03-13 23:45:53 -0700393 if (argument != nullptr && !argument->InstanceOf(param_type)) {
394 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
David Sehr709b0702016-10-13 09:12:37 -0700395 << argument->PrettyTypeOf() << " as argument " << (i + 1)
396 << " to " << m->PrettyMethod();
Ian Rogers53b8b092014-03-13 23:45:53 -0700397 ++error_count;
398 }
399 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
400 offset++;
Ian Rogers68d8b422014-07-17 11:09:10 -0700401 } else {
402 int32_t arg = static_cast<int32_t>(args[i + offset]);
403 if (param_type->IsPrimitiveBoolean()) {
404 if (arg != JNI_TRUE && arg != JNI_FALSE) {
405 LOG(ERROR) << "JNI ERROR (app bug): expected jboolean (0/1) but got value of "
David Sehr709b0702016-10-13 09:12:37 -0700406 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700407 ++error_count;
408 }
409 } else if (param_type->IsPrimitiveByte()) {
410 if (arg < -128 || arg > 127) {
411 LOG(ERROR) << "JNI ERROR (app bug): expected jbyte but got value of "
David Sehr709b0702016-10-13 09:12:37 -0700412 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700413 ++error_count;
414 }
415 } else if (param_type->IsPrimitiveChar()) {
416 if (args[i + offset] > 0xFFFF) {
417 LOG(ERROR) << "JNI ERROR (app bug): expected jchar but got value of "
David Sehr709b0702016-10-13 09:12:37 -0700418 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700419 ++error_count;
420 }
421 } else if (param_type->IsPrimitiveShort()) {
422 if (arg < -32768 || arg > 0x7FFF) {
423 LOG(ERROR) << "JNI ERROR (app bug): expected jshort but got value of "
David Sehr709b0702016-10-13 09:12:37 -0700424 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700425 ++error_count;
426 }
427 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700428 }
429 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700430 if (UNLIKELY(error_count > 0)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700431 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
432 // with an argument.
Ian Rogers68d8b422014-07-17 11:09:10 -0700433 vm->JniAbortF(nullptr, "bad arguments passed to %s (see above for details)",
David Sehr709b0702016-10-13 09:12:37 -0700434 m->PrettyMethod().c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700435 }
436}
437
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700438static ArtMethod* FindVirtualMethod(ObjPtr<mirror::Object> receiver, ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700439 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700440 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method, kRuntimePointerSize);
Ian Rogers53b8b092014-03-13 23:45:53 -0700441}
442
443
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700444static void InvokeWithArgArray(const ScopedObjectAccessAlreadyRunnable& soa,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700445 ArtMethod* method, ArgArray* arg_array, JValue* result,
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700446 const char* shorty)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700447 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700448 uint32_t* args = arg_array->GetArray();
449 if (UNLIKELY(soa.Env()->check_jni)) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700450 CheckMethodArguments(soa.Vm(), method->GetInterfaceMethodIfProxy(kRuntimePointerSize), args);
Ian Rogers53b8b092014-03-13 23:45:53 -0700451 }
452 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, shorty);
453}
454
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700455JValue InvokeWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
456 va_list args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700457 REQUIRES_SHARED(Locks::mutator_lock_) {
Dave Allison648d7112014-07-25 16:15:27 -0700458 // We want to make sure that the stack is not within a small distance from the
459 // protected region in case we are calling into a leaf function whose stack
460 // check has been elided.
461 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
462 ThrowStackOverflowError(soa.Self());
463 return JValue();
464 }
465
Andreas Gampe13b27842016-11-07 16:48:23 -0800466 ArtMethod* method = jni::DecodeArtMethod(mid);
Jeff Hao39b6c242015-05-19 20:30:23 -0700467 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
468 if (is_string_init) {
469 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100470 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700471 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700472 ObjPtr<mirror::Object> receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700473 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700474 const char* shorty =
475 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700476 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700477 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700478 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700479 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700480 if (is_string_init) {
481 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700482 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700483 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700484 return result;
485}
486
Jeff Hao39b6c242015-05-19 20:30:23 -0700487JValue InvokeWithJValues(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
488 jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700489 // We want to make sure that the stack is not within a small distance from the
490 // protected region in case we are calling into a leaf function whose stack
491 // check has been elided.
492 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
493 ThrowStackOverflowError(soa.Self());
494 return JValue();
495 }
496
Andreas Gampe13b27842016-11-07 16:48:23 -0800497 ArtMethod* method = jni::DecodeArtMethod(mid);
Jeff Hao39b6c242015-05-19 20:30:23 -0700498 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
499 if (is_string_init) {
500 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100501 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700502 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700503 ObjPtr<mirror::Object> receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700504 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700505 const char* shorty =
506 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700507 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700508 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700509 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700510 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700511 if (is_string_init) {
512 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700513 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700514 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700515 return result;
516}
517
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700518JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
Jeff Hao39b6c242015-05-19 20:30:23 -0700519 jobject obj, jmethodID mid, jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700520 // We want to make sure that the stack is not within a small distance from the
521 // protected region in case we are calling into a leaf function whose stack
522 // check has been elided.
523 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
524 ThrowStackOverflowError(soa.Self());
525 return JValue();
526 }
527
Mathieu Chartier0795f232016-09-27 18:43:30 -0700528 ObjPtr<mirror::Object> receiver = soa.Decode<mirror::Object>(obj);
Andreas Gampe13b27842016-11-07 16:48:23 -0800529 ArtMethod* method = FindVirtualMethod(receiver, jni::DecodeArtMethod(mid));
Jeff Hao39b6c242015-05-19 20:30:23 -0700530 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
531 if (is_string_init) {
532 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100533 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700534 receiver = nullptr;
535 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700536 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700537 const char* shorty =
538 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700539 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700540 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700541 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700542 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700543 if (is_string_init) {
544 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700545 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700546 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700547 return result;
548}
549
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700550JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
Ian Rogers53b8b092014-03-13 23:45:53 -0700551 jobject obj, jmethodID mid, va_list args) {
Dave Allison648d7112014-07-25 16:15:27 -0700552 // We want to make sure that the stack is not within a small distance from the
553 // protected region in case we are calling into a leaf function whose stack
554 // check has been elided.
555 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
556 ThrowStackOverflowError(soa.Self());
557 return JValue();
558 }
559
Mathieu Chartier0795f232016-09-27 18:43:30 -0700560 ObjPtr<mirror::Object> receiver = soa.Decode<mirror::Object>(obj);
Andreas Gampe13b27842016-11-07 16:48:23 -0800561 ArtMethod* method = FindVirtualMethod(receiver, jni::DecodeArtMethod(mid));
Jeff Hao39b6c242015-05-19 20:30:23 -0700562 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
563 if (is_string_init) {
564 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100565 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700566 receiver = nullptr;
567 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700568 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700569 const char* shorty =
570 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700571 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700572 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700573 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700574 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700575 if (is_string_init) {
576 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700577 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700578 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700579 return result;
580}
581
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700582jobject InvokeMethod(const ScopedObjectAccessAlreadyRunnable& soa, jobject javaMethod,
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700583 jobject javaReceiver, jobject javaArgs, size_t num_frames) {
Dave Allison648d7112014-07-25 16:15:27 -0700584 // We want to make sure that the stack is not within a small distance from the
585 // protected region in case we are calling into a leaf function whose stack
586 // check has been elided.
587 if (UNLIKELY(__builtin_frame_address(0) <
588 soa.Self()->GetStackEndForInterpreter(true))) {
589 ThrowStackOverflowError(soa.Self());
590 return nullptr;
591 }
592
Mathieu Chartier0795f232016-09-27 18:43:30 -0700593 ObjPtr<mirror::Executable> executable = soa.Decode<mirror::Executable>(javaMethod);
Neil Fuller0e844392016-09-08 13:43:31 +0100594 const bool accessible = executable->IsAccessible();
595 ArtMethod* m = executable->GetArtMethod();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700596
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700597 ObjPtr<mirror::Class> declaring_class = m->GetDeclaringClass();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800598 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700599 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -0700600 HandleWrapperObjPtr<mirror::Class> h_class(hs.NewHandleWrapper(&declaring_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700601 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), h_class, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800602 return nullptr;
603 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700604 }
605
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700606 ObjPtr<mirror::Object> receiver;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700607 if (!m->IsStatic()) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800608 // Replace calls to String.<init> with equivalent StringFactory call.
609 if (declaring_class->IsStringClass() && m->IsConstructor()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100610 m = WellKnownClasses::StringInitToStringFactory(m);
Jeff Hao848f70a2014-01-15 13:49:50 -0800611 CHECK(javaReceiver == nullptr);
612 } else {
613 // Check that the receiver is non-null and an instance of the field's declaring class.
Mathieu Chartier0795f232016-09-27 18:43:30 -0700614 receiver = soa.Decode<mirror::Object>(javaReceiver);
Jeff Hao848f70a2014-01-15 13:49:50 -0800615 if (!VerifyObjectIsClass(receiver, declaring_class)) {
616 return nullptr;
617 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700618
Jeff Hao848f70a2014-01-15 13:49:50 -0800619 // Find the actual implementation of the virtual method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700620 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m, kRuntimePointerSize);
Jeff Hao848f70a2014-01-15 13:49:50 -0800621 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700622 }
623
624 // Get our arrays of arguments and their types, and check they're the same size.
Mathieu Chartier0795f232016-09-27 18:43:30 -0700625 ObjPtr<mirror::ObjectArray<mirror::Object>> objects =
626 soa.Decode<mirror::ObjectArray<mirror::Object>>(javaArgs);
Andreas Gampe542451c2016-07-26 09:02:02 -0700627 auto* np_method = m->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700628 const DexFile::TypeList* classes = np_method->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700629 uint32_t classes_size = (classes == nullptr) ? 0 : classes->Size();
630 uint32_t arg_count = (objects != nullptr) ? objects->GetLength() : 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800631 if (arg_count != classes_size) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000632 ThrowIllegalArgumentException(StringPrintf("Wrong number of arguments; expected %d, got %d",
Ian Rogers62d6c772013-02-27 08:32:07 -0800633 classes_size, arg_count).c_str());
Ian Rogersa0485602014-12-02 15:48:04 -0800634 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700635 }
636
Jeff Haocb4581a2014-03-28 15:43:37 -0700637 // If method is not set to be accessible, verify it can be accessed by the caller.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700638 ObjPtr<mirror::Class> calling_class;
Mathieu Chartier268764d2016-09-13 12:09:38 -0700639 if (!accessible && !VerifyAccess(soa.Self(),
640 receiver,
641 declaring_class,
642 m->GetAccessFlags(),
643 &calling_class,
644 num_frames)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000645 ThrowIllegalAccessException(
Andreas Gampec0d82292014-09-23 10:38:30 -0700646 StringPrintf("Class %s cannot access %s method %s of class %s",
David Sehr709b0702016-10-13 09:12:37 -0700647 calling_class == nullptr ? "null" : calling_class->PrettyClass().c_str(),
Andreas Gampec0d82292014-09-23 10:38:30 -0700648 PrettyJavaAccessFlags(m->GetAccessFlags()).c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700649 m->PrettyMethod().c_str(),
Andreas Gampec0d82292014-09-23 10:38:30 -0700650 m->GetDeclaringClass() == nullptr ? "null" :
David Sehr709b0702016-10-13 09:12:37 -0700651 m->GetDeclaringClass()->PrettyClass().c_str()).c_str());
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700652 return nullptr;
653 }
654
Ian Rogers53b8b092014-03-13 23:45:53 -0700655 // Invoke the method.
656 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700657 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700658 const char* shorty = np_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700659 ArgArray arg_array(shorty, shorty_len);
Nicolas Geoffray4c041172017-01-19 16:25:06 +0000660 if (!arg_array.BuildArgArrayFromObjectArray(receiver, objects, np_method, soa.Self())) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700661 CHECK(soa.Self()->IsExceptionPending());
662 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700663 }
664
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700665 InvokeWithArgArray(soa, m, &arg_array, &result, shorty);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700666
667 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700668 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700669 // If we get another exception when we are trying to wrap, then just use that instead.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700670 jthrowable th = soa.Env()->ExceptionOccurred();
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700671 soa.Self()->ClearException();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700672 jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException");
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700673 if (exception_class == nullptr) {
674 soa.Self()->AssertPendingOOMException();
675 return nullptr;
676 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700677 jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700678 CHECK(mid != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700679 jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th);
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700680 if (exception_instance == nullptr) {
681 soa.Self()->AssertPendingOOMException();
682 return nullptr;
683 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700684 soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance));
Ian Rogersa0485602014-12-02 15:48:04 -0800685 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700686 }
687
688 // Box if necessary and return.
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -0700689 return soa.AddLocalReference<jobject>(BoxPrimitive(Primitive::GetType(shorty[0]), result));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700690}
691
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700692ObjPtr<mirror::Object> BoxPrimitive(Primitive::Type src_class, const JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700693 if (src_class == Primitive::kPrimNot) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700694 return MakeObjPtr(value.GetL());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700695 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700696 if (src_class == Primitive::kPrimVoid) {
697 // There's no such thing as a void field, and void methods invoked via reflection return null.
698 return nullptr;
699 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700700
Ian Rogers84956ff2014-03-26 23:52:41 -0700701 jmethodID m = nullptr;
Ian Rogers0177e532014-02-11 16:30:46 -0800702 const char* shorty;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700703 switch (src_class) {
704 case Primitive::kPrimBoolean:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700705 m = WellKnownClasses::java_lang_Boolean_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800706 shorty = "LZ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700707 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700708 case Primitive::kPrimByte:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700709 m = WellKnownClasses::java_lang_Byte_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800710 shorty = "LB";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700711 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700712 case Primitive::kPrimChar:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700713 m = WellKnownClasses::java_lang_Character_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800714 shorty = "LC";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700715 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700716 case Primitive::kPrimDouble:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700717 m = WellKnownClasses::java_lang_Double_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800718 shorty = "LD";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700719 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700720 case Primitive::kPrimFloat:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700721 m = WellKnownClasses::java_lang_Float_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800722 shorty = "LF";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700723 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700724 case Primitive::kPrimInt:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700725 m = WellKnownClasses::java_lang_Integer_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800726 shorty = "LI";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700727 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700728 case Primitive::kPrimLong:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700729 m = WellKnownClasses::java_lang_Long_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800730 shorty = "LJ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700731 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700732 case Primitive::kPrimShort:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700733 m = WellKnownClasses::java_lang_Short_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800734 shorty = "LS";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700735 break;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700736 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700737 LOG(FATAL) << static_cast<int>(src_class);
Ian Rogers0177e532014-02-11 16:30:46 -0800738 shorty = nullptr;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700739 }
740
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700741 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers53b8b092014-03-13 23:45:53 -0700742 DCHECK_EQ(soa.Self()->GetState(), kRunnable);
Jeff Hao5d917302013-02-27 17:57:33 -0800743
Ian Rogers53b8b092014-03-13 23:45:53 -0700744 ArgArray arg_array(shorty, 2);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800745 JValue result;
Jeff Hao5d917302013-02-27 17:57:33 -0800746 if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) {
747 arg_array.AppendWide(value.GetJ());
748 } else {
749 arg_array.Append(value.GetI());
750 }
751
Andreas Gampe13b27842016-11-07 16:48:23 -0800752 jni::DecodeArtMethod(m)->Invoke(soa.Self(),
753 arg_array.GetArray(),
754 arg_array.GetNumBytes(),
755 &result,
756 shorty);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800757 return result.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700758}
759
Mathieu Chartierc7853442015-03-27 14:35:38 -0700760static std::string UnboxingFailureKind(ArtField* f)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700761 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700762 if (f != nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700763 return "field " + f->PrettyField(false);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700764 }
765 return "result";
766}
767
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700768static bool UnboxPrimitive(ObjPtr<mirror::Object> o,
769 ObjPtr<mirror::Class> dst_class,
770 ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -0700771 JValue* unboxed_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700772 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700773 bool unbox_for_result = (f == nullptr);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700774 if (!dst_class->IsPrimitive()) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700775 if (UNLIKELY(o != nullptr && !o->InstanceOf(dst_class))) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800776 if (!unbox_for_result) {
David Sehr709b0702016-10-13 09:12:37 -0700777 ThrowIllegalArgumentException(
778 StringPrintf("%s has type %s, got %s",
779 UnboxingFailureKind(f).c_str(),
780 dst_class->PrettyDescriptor().c_str(),
781 o->PrettyTypeOf().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800782 } else {
David Sehr709b0702016-10-13 09:12:37 -0700783 ThrowClassCastException(
784 StringPrintf("Couldn't convert result of type %s to %s",
785 o->PrettyTypeOf().c_str(),
786 dst_class->PrettyDescriptor().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800787 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700788 return false;
789 }
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700790 unboxed_value->SetL(o);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700791 return true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800792 }
793 if (UNLIKELY(dst_class->GetPrimitiveType() == Primitive::kPrimVoid)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000794 ThrowIllegalArgumentException(StringPrintf("Can't unbox %s to void",
Ian Rogers84956ff2014-03-26 23:52:41 -0700795 UnboxingFailureKind(f).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700796 return false;
797 }
Ian Rogers84956ff2014-03-26 23:52:41 -0700798 if (UNLIKELY(o == nullptr)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800799 if (!unbox_for_result) {
David Sehr709b0702016-10-13 09:12:37 -0700800 ThrowIllegalArgumentException(
801 StringPrintf("%s has type %s, got null",
802 UnboxingFailureKind(f).c_str(),
803 dst_class->PrettyDescriptor().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800804 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700805 ThrowNullPointerException(
806 StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
David Sehr709b0702016-10-13 09:12:37 -0700807 dst_class->PrettyDescriptor().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800808 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700809 return false;
810 }
811
Elliott Hughes1d878f32012-04-11 15:17:54 -0700812 JValue boxed_value;
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700813 ObjPtr<mirror::Class> klass = o->GetClass();
814 ObjPtr<mirror::Class> src_class = nullptr;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700815 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700816 ArtField* primitive_field = &klass->GetIFieldsPtr()->At(0);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700817 if (klass->DescriptorEquals("Ljava/lang/Boolean;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700818 src_class = class_linker->FindPrimitiveClass('Z');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700819 boxed_value.SetZ(primitive_field->GetBoolean(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700820 } else if (klass->DescriptorEquals("Ljava/lang/Byte;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700821 src_class = class_linker->FindPrimitiveClass('B');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700822 boxed_value.SetB(primitive_field->GetByte(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700823 } else if (klass->DescriptorEquals("Ljava/lang/Character;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700824 src_class = class_linker->FindPrimitiveClass('C');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700825 boxed_value.SetC(primitive_field->GetChar(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700826 } else if (klass->DescriptorEquals("Ljava/lang/Float;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700827 src_class = class_linker->FindPrimitiveClass('F');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700828 boxed_value.SetF(primitive_field->GetFloat(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700829 } else if (klass->DescriptorEquals("Ljava/lang/Double;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700830 src_class = class_linker->FindPrimitiveClass('D');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700831 boxed_value.SetD(primitive_field->GetDouble(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700832 } else if (klass->DescriptorEquals("Ljava/lang/Integer;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700833 src_class = class_linker->FindPrimitiveClass('I');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700834 boxed_value.SetI(primitive_field->GetInt(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700835 } else if (klass->DescriptorEquals("Ljava/lang/Long;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700836 src_class = class_linker->FindPrimitiveClass('J');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700837 boxed_value.SetJ(primitive_field->GetLong(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700838 } else if (klass->DescriptorEquals("Ljava/lang/Short;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700839 src_class = class_linker->FindPrimitiveClass('S');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700840 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700841 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700842 std::string temp;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000843 ThrowIllegalArgumentException(
Ian Rogers1ff3c982014-08-12 02:30:58 -0700844 StringPrintf("%s has type %s, got %s", UnboxingFailureKind(f).c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700845 dst_class->PrettyDescriptor().c_str(),
Ian Rogers1ff3c982014-08-12 02:30:58 -0700846 PrettyDescriptor(o->GetClass()->GetDescriptor(&temp)).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700847 return false;
848 }
849
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000850 return ConvertPrimitiveValue(unbox_for_result,
Ian Rogers62d6c772013-02-27 08:32:07 -0800851 src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700852 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700853}
854
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700855bool UnboxPrimitiveForField(ObjPtr<mirror::Object> o,
856 ObjPtr<mirror::Class> dst_class,
857 ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -0700858 JValue* unboxed_value) {
859 DCHECK(f != nullptr);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000860 return UnboxPrimitive(o, dst_class, f, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700861}
862
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700863bool UnboxPrimitiveForResult(ObjPtr<mirror::Object> o,
864 ObjPtr<mirror::Class> dst_class,
865 JValue* unboxed_value) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000866 return UnboxPrimitive(o, dst_class, nullptr, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700867}
868
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700869ObjPtr<mirror::Class> GetCallingClass(Thread* self, size_t num_frames) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700870 NthCallerVisitor visitor(self, num_frames);
871 visitor.WalkStack();
872 return visitor.caller != nullptr ? visitor.caller->GetDeclaringClass() : nullptr;
873}
874
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700875bool VerifyAccess(Thread* self,
876 ObjPtr<mirror::Object> obj,
877 ObjPtr<mirror::Class> declaring_class,
878 uint32_t access_flags,
879 ObjPtr<mirror::Class>* calling_class,
880 size_t num_frames) {
Mathieu Chartier76433272014-09-26 14:32:37 -0700881 if ((access_flags & kAccPublic) != 0) {
882 return true;
883 }
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700884 ObjPtr<mirror::Class> klass = GetCallingClass(self, num_frames);
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700885 if (UNLIKELY(klass == nullptr)) {
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100886 // The caller is an attached native thread.
Mathieu Chartier76433272014-09-26 14:32:37 -0700887 return false;
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100888 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700889 *calling_class = klass;
Mathieu Chartier268764d2016-09-13 12:09:38 -0700890 return VerifyAccess(obj, declaring_class, access_flags, klass);
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700891}
892
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700893bool VerifyAccess(ObjPtr<mirror::Object> obj,
894 ObjPtr<mirror::Class> declaring_class,
Mathieu Chartier268764d2016-09-13 12:09:38 -0700895 uint32_t access_flags,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700896 ObjPtr<mirror::Class> calling_class) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700897 if (calling_class == declaring_class) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700898 return true;
899 }
Mathieu Chartier268764d2016-09-13 12:09:38 -0700900 ScopedAssertNoThreadSuspension sants("verify-access");
Jeff Haocb4581a2014-03-28 15:43:37 -0700901 if ((access_flags & kAccPrivate) != 0) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700902 return false;
903 }
Jeff Haocb4581a2014-03-28 15:43:37 -0700904 if ((access_flags & kAccProtected) != 0) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700905 if (obj != nullptr && !obj->InstanceOf(calling_class) &&
Mathieu Chartier3398c782016-09-30 10:27:43 -0700906 !declaring_class->IsInSamePackage(calling_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700907 return false;
Mathieu Chartier3398c782016-09-30 10:27:43 -0700908 } else if (declaring_class->IsAssignableFrom(calling_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700909 return true;
910 }
911 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700912 return declaring_class->IsInSamePackage(calling_class);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700913}
914
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700915void InvalidReceiverError(ObjPtr<mirror::Object> o, ObjPtr<mirror::Class> c) {
David Sehr709b0702016-10-13 09:12:37 -0700916 std::string expected_class_name(mirror::Class::PrettyDescriptor(c));
917 std::string actual_class_name(mirror::Object::PrettyTypeOf(o));
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700918 ThrowIllegalArgumentException(StringPrintf("Expected receiver of type %s, but got %s",
919 expected_class_name.c_str(),
920 actual_class_name.c_str()).c_str());
921}
922
Jeff Hao83c81952015-05-27 19:29:29 -0700923// This only works if there's one reference which points to the object in obj.
924// Will need to be fixed if there's cases where it's not.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700925void UpdateReference(Thread* self, jobject obj, ObjPtr<mirror::Object> result) {
Jeff Hao83c81952015-05-27 19:29:29 -0700926 IndirectRef ref = reinterpret_cast<IndirectRef>(obj);
Andreas Gampedc061d02016-10-24 13:19:37 -0700927 IndirectRefKind kind = IndirectReferenceTable::GetIndirectRefKind(ref);
Jeff Hao83c81952015-05-27 19:29:29 -0700928 if (kind == kLocal) {
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -0700929 self->GetJniEnv()->locals.Update(obj, result);
Jeff Hao83c81952015-05-27 19:29:29 -0700930 } else if (kind == kHandleScopeOrInvalid) {
931 LOG(FATAL) << "Unsupported UpdateReference for kind kHandleScopeOrInvalid";
932 } else if (kind == kGlobal) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700933 self->GetJniEnv()->vm->UpdateGlobal(self, ref, result);
Jeff Hao83c81952015-05-27 19:29:29 -0700934 } else {
935 DCHECK_EQ(kind, kWeakGlobal);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700936 self->GetJniEnv()->vm->UpdateWeakGlobal(self, ref, result);
Jeff Hao83c81952015-05-27 19:29:29 -0700937 }
938}
939
Elliott Hughes418d20f2011-09-22 14:00:39 -0700940} // namespace art