blob: 75176f938e05a8197a1d68bf4b851d5506985819 [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,
219 ObjPtr<mirror::ObjectArray<mirror::Object>> args,
220 ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700221 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700222 const DexFile::TypeList* classes = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700223 // Set receiver if non-null (method is not static)
224 if (receiver != nullptr) {
225 Append(receiver);
226 }
227 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700228 ObjPtr<mirror::Object> arg(args->Get(args_offset));
Ian Rogers53b8b092014-03-13 23:45:53 -0700229 if (((shorty_[i] == 'L') && (arg != nullptr)) || ((arg == nullptr && shorty_[i] != 'L'))) {
Vladimir Marko942fd312017-01-16 20:52:19 +0000230 // Note: The method's parameter's type must have been previously resolved.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700231 ObjPtr<mirror::Class> dst_class(
Vladimir Marko05792b92015-08-03 11:56:49 +0100232 m->GetClassFromTypeIndex(classes->GetTypeItem(args_offset).type_idx_,
Vladimir Marko942fd312017-01-16 20:52:19 +0000233 false /* resolve */));
234 DCHECK(dst_class != nullptr) << m->PrettyMethod() << " arg #" << i;
Ian Rogers53b8b092014-03-13 23:45:53 -0700235 if (UNLIKELY(arg == nullptr || !arg->InstanceOf(dst_class))) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000236 ThrowIllegalArgumentException(
Ian Rogers11e4c032014-03-14 12:00:39 -0700237 StringPrintf("method %s argument %zd has type %s, got %s",
David Sehr709b0702016-10-13 09:12:37 -0700238 m->PrettyMethod(false).c_str(),
Ian Rogers53b8b092014-03-13 23:45:53 -0700239 args_offset + 1, // Humans don't count from 0.
David Sehr709b0702016-10-13 09:12:37 -0700240 mirror::Class::PrettyDescriptor(dst_class).c_str(),
241 mirror::Object::PrettyTypeOf(arg).c_str()).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700242 return false;
243 }
244 }
245
246#define DO_FIRST_ARG(match_descriptor, get_fn, append) { \
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700247 if (LIKELY(arg != nullptr && arg->GetClass()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700248 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Mathieu Chartier3398c782016-09-30 10:27:43 -0700249 append(primitive_field-> get_fn(arg));
Ian Rogers53b8b092014-03-13 23:45:53 -0700250
251#define DO_ARG(match_descriptor, get_fn, append) \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700252 } else if (LIKELY(arg != nullptr && \
253 arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700254 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Mathieu Chartier3398c782016-09-30 10:27:43 -0700255 append(primitive_field-> get_fn(arg));
Ian Rogers53b8b092014-03-13 23:45:53 -0700256
257#define DO_FAIL(expected) \
258 } else { \
259 if (arg->GetClass<>()->IsPrimitive()) { \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700260 std::string temp; \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700261 ThrowIllegalPrimitiveArgumentException(expected, \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700262 arg->GetClass<>()->GetDescriptor(&temp)); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700263 } else { \
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000264 ThrowIllegalArgumentException(\
Ian Rogers11e4c032014-03-14 12:00:39 -0700265 StringPrintf("method %s argument %zd has type %s, got %s", \
David Sehr709b0702016-10-13 09:12:37 -0700266 ArtMethod::PrettyMethod(m, false).c_str(), \
Ian Rogers53b8b092014-03-13 23:45:53 -0700267 args_offset + 1, \
268 expected, \
David Sehr709b0702016-10-13 09:12:37 -0700269 mirror::Object::PrettyTypeOf(arg).c_str()).c_str()); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700270 } \
271 return false; \
272 } }
273
274 switch (shorty_[i]) {
275 case 'L':
276 Append(arg);
277 break;
278 case 'Z':
279 DO_FIRST_ARG("Ljava/lang/Boolean;", GetBoolean, Append)
280 DO_FAIL("boolean")
281 break;
282 case 'B':
283 DO_FIRST_ARG("Ljava/lang/Byte;", GetByte, Append)
284 DO_FAIL("byte")
285 break;
286 case 'C':
287 DO_FIRST_ARG("Ljava/lang/Character;", GetChar, Append)
288 DO_FAIL("char")
289 break;
290 case 'S':
291 DO_FIRST_ARG("Ljava/lang/Short;", GetShort, Append)
292 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
293 DO_FAIL("short")
294 break;
295 case 'I':
296 DO_FIRST_ARG("Ljava/lang/Integer;", GetInt, Append)
297 DO_ARG("Ljava/lang/Character;", GetChar, Append)
298 DO_ARG("Ljava/lang/Short;", GetShort, Append)
299 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
300 DO_FAIL("int")
301 break;
302 case 'J':
303 DO_FIRST_ARG("Ljava/lang/Long;", GetLong, AppendWide)
304 DO_ARG("Ljava/lang/Integer;", GetInt, AppendWide)
305 DO_ARG("Ljava/lang/Character;", GetChar, AppendWide)
306 DO_ARG("Ljava/lang/Short;", GetShort, AppendWide)
307 DO_ARG("Ljava/lang/Byte;", GetByte, AppendWide)
308 DO_FAIL("long")
309 break;
310 case 'F':
311 DO_FIRST_ARG("Ljava/lang/Float;", GetFloat, AppendFloat)
312 DO_ARG("Ljava/lang/Long;", GetLong, AppendFloat)
313 DO_ARG("Ljava/lang/Integer;", GetInt, AppendFloat)
314 DO_ARG("Ljava/lang/Character;", GetChar, AppendFloat)
315 DO_ARG("Ljava/lang/Short;", GetShort, AppendFloat)
316 DO_ARG("Ljava/lang/Byte;", GetByte, AppendFloat)
317 DO_FAIL("float")
318 break;
319 case 'D':
320 DO_FIRST_ARG("Ljava/lang/Double;", GetDouble, AppendDouble)
321 DO_ARG("Ljava/lang/Float;", GetFloat, AppendDouble)
322 DO_ARG("Ljava/lang/Long;", GetLong, AppendDouble)
323 DO_ARG("Ljava/lang/Integer;", GetInt, AppendDouble)
324 DO_ARG("Ljava/lang/Character;", GetChar, AppendDouble)
325 DO_ARG("Ljava/lang/Short;", GetShort, AppendDouble)
326 DO_ARG("Ljava/lang/Byte;", GetByte, AppendDouble)
327 DO_FAIL("double")
328 break;
329#ifndef NDEBUG
330 default:
331 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
Ian Rogersa0485602014-12-02 15:48:04 -0800332 UNREACHABLE();
Ian Rogers53b8b092014-03-13 23:45:53 -0700333#endif
334 }
335#undef DO_FIRST_ARG
336#undef DO_ARG
337#undef DO_FAIL
338 }
339 return true;
340 }
341
342 private:
343 enum { kSmallArgArraySize = 16 };
344 const char* const shorty_;
345 const uint32_t shorty_len_;
346 uint32_t num_bytes_;
347 uint32_t* arg_array_;
348 uint32_t small_arg_array_[kSmallArgArraySize];
Ian Rogers700a4022014-05-19 16:49:03 -0700349 std::unique_ptr<uint32_t[]> large_arg_array_;
Ian Rogers53b8b092014-03-13 23:45:53 -0700350};
351
Mathieu Chartiere401d142015-04-22 13:56:20 -0700352static void CheckMethodArguments(JavaVMExt* vm, ArtMethod* m, uint32_t* args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700353 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700354 const DexFile::TypeList* params = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700355 if (params == nullptr) {
356 return; // No arguments so nothing to check.
357 }
358 uint32_t offset = 0;
359 uint32_t num_params = params->Size();
360 size_t error_count = 0;
361 if (!m->IsStatic()) {
362 offset = 1;
363 }
Ian Rogersa0485602014-12-02 15:48:04 -0800364 // TODO: If args contain object references, it may cause problems.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700365 Thread* const self = Thread::Current();
Ian Rogers53b8b092014-03-13 23:45:53 -0700366 for (uint32_t i = 0; i < num_params; i++) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800367 dex::TypeIndex type_idx = params->GetTypeItem(i).type_idx_;
Vladimir Marko942fd312017-01-16 20:52:19 +0000368 ObjPtr<mirror::Class> param_type(m->GetClassFromTypeIndex(type_idx, true /* resolve */));
Ian Rogers53b8b092014-03-13 23:45:53 -0700369 if (param_type == nullptr) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700370 CHECK(self->IsExceptionPending());
371 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700372 << m->GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000373 << self->GetException()->Dump();
Ian Rogers53b8b092014-03-13 23:45:53 -0700374 self->ClearException();
375 ++error_count;
376 } else if (!param_type->IsPrimitive()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700377 // TODO: There is a compaction bug here since GetClassFromTypeIdx can cause thread suspension,
378 // this is a hard to fix problem since the args can contain Object*, we need to save and
379 // restore them by using a visitor similar to the ones used in the trampoline entrypoints.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700380 ObjPtr<mirror::Object> argument =
Ian Rogers68d8b422014-07-17 11:09:10 -0700381 (reinterpret_cast<StackReference<mirror::Object>*>(&args[i + offset]))->AsMirrorPtr();
Ian Rogers53b8b092014-03-13 23:45:53 -0700382 if (argument != nullptr && !argument->InstanceOf(param_type)) {
383 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
David Sehr709b0702016-10-13 09:12:37 -0700384 << argument->PrettyTypeOf() << " as argument " << (i + 1)
385 << " to " << m->PrettyMethod();
Ian Rogers53b8b092014-03-13 23:45:53 -0700386 ++error_count;
387 }
388 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
389 offset++;
Ian Rogers68d8b422014-07-17 11:09:10 -0700390 } else {
391 int32_t arg = static_cast<int32_t>(args[i + offset]);
392 if (param_type->IsPrimitiveBoolean()) {
393 if (arg != JNI_TRUE && arg != JNI_FALSE) {
394 LOG(ERROR) << "JNI ERROR (app bug): expected jboolean (0/1) but got value of "
David Sehr709b0702016-10-13 09:12:37 -0700395 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700396 ++error_count;
397 }
398 } else if (param_type->IsPrimitiveByte()) {
399 if (arg < -128 || arg > 127) {
400 LOG(ERROR) << "JNI ERROR (app bug): expected jbyte but got value of "
David Sehr709b0702016-10-13 09:12:37 -0700401 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700402 ++error_count;
403 }
404 } else if (param_type->IsPrimitiveChar()) {
405 if (args[i + offset] > 0xFFFF) {
406 LOG(ERROR) << "JNI ERROR (app bug): expected jchar but got value of "
David Sehr709b0702016-10-13 09:12:37 -0700407 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700408 ++error_count;
409 }
410 } else if (param_type->IsPrimitiveShort()) {
411 if (arg < -32768 || arg > 0x7FFF) {
412 LOG(ERROR) << "JNI ERROR (app bug): expected jshort but got value of "
David Sehr709b0702016-10-13 09:12:37 -0700413 << arg << " as argument " << (i + 1) << " to " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700414 ++error_count;
415 }
416 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700417 }
418 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700419 if (UNLIKELY(error_count > 0)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700420 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
421 // with an argument.
Ian Rogers68d8b422014-07-17 11:09:10 -0700422 vm->JniAbortF(nullptr, "bad arguments passed to %s (see above for details)",
David Sehr709b0702016-10-13 09:12:37 -0700423 m->PrettyMethod().c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700424 }
425}
426
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700427static ArtMethod* FindVirtualMethod(ObjPtr<mirror::Object> receiver, ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700428 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700429 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method, kRuntimePointerSize);
Ian Rogers53b8b092014-03-13 23:45:53 -0700430}
431
432
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700433static void InvokeWithArgArray(const ScopedObjectAccessAlreadyRunnable& soa,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700434 ArtMethod* method, ArgArray* arg_array, JValue* result,
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700435 const char* shorty)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700436 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700437 uint32_t* args = arg_array->GetArray();
438 if (UNLIKELY(soa.Env()->check_jni)) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700439 CheckMethodArguments(soa.Vm(), method->GetInterfaceMethodIfProxy(kRuntimePointerSize), args);
Ian Rogers53b8b092014-03-13 23:45:53 -0700440 }
441 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, shorty);
442}
443
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700444JValue InvokeWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
445 va_list args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700446 REQUIRES_SHARED(Locks::mutator_lock_) {
Dave Allison648d7112014-07-25 16:15:27 -0700447 // We want to make sure that the stack is not within a small distance from the
448 // protected region in case we are calling into a leaf function whose stack
449 // check has been elided.
450 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
451 ThrowStackOverflowError(soa.Self());
452 return JValue();
453 }
454
Andreas Gampe13b27842016-11-07 16:48:23 -0800455 ArtMethod* method = jni::DecodeArtMethod(mid);
Jeff Hao39b6c242015-05-19 20:30:23 -0700456 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
457 if (is_string_init) {
458 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100459 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700460 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700461 ObjPtr<mirror::Object> receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700462 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700463 const char* shorty =
464 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700465 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700466 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700467 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700468 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700469 if (is_string_init) {
470 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700471 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700472 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700473 return result;
474}
475
Jeff Hao39b6c242015-05-19 20:30:23 -0700476JValue InvokeWithJValues(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
477 jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700478 // We want to make sure that the stack is not within a small distance from the
479 // protected region in case we are calling into a leaf function whose stack
480 // check has been elided.
481 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
482 ThrowStackOverflowError(soa.Self());
483 return JValue();
484 }
485
Andreas Gampe13b27842016-11-07 16:48:23 -0800486 ArtMethod* method = jni::DecodeArtMethod(mid);
Jeff Hao39b6c242015-05-19 20:30:23 -0700487 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
488 if (is_string_init) {
489 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100490 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700491 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700492 ObjPtr<mirror::Object> receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700493 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700494 const char* shorty =
495 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700496 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700497 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700498 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700499 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700500 if (is_string_init) {
501 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700502 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700503 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700504 return result;
505}
506
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700507JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
Jeff Hao39b6c242015-05-19 20:30:23 -0700508 jobject obj, jmethodID mid, jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700509 // We want to make sure that the stack is not within a small distance from the
510 // protected region in case we are calling into a leaf function whose stack
511 // check has been elided.
512 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
513 ThrowStackOverflowError(soa.Self());
514 return JValue();
515 }
516
Mathieu Chartier0795f232016-09-27 18:43:30 -0700517 ObjPtr<mirror::Object> receiver = soa.Decode<mirror::Object>(obj);
Andreas Gampe13b27842016-11-07 16:48:23 -0800518 ArtMethod* method = FindVirtualMethod(receiver, jni::DecodeArtMethod(mid));
Jeff Hao39b6c242015-05-19 20:30:23 -0700519 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
520 if (is_string_init) {
521 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100522 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700523 receiver = nullptr;
524 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700525 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700526 const char* shorty =
527 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700528 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700529 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700530 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700531 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700532 if (is_string_init) {
533 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700534 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700535 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700536 return result;
537}
538
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700539JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
Ian Rogers53b8b092014-03-13 23:45:53 -0700540 jobject obj, jmethodID mid, va_list args) {
Dave Allison648d7112014-07-25 16:15:27 -0700541 // We want to make sure that the stack is not within a small distance from the
542 // protected region in case we are calling into a leaf function whose stack
543 // check has been elided.
544 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
545 ThrowStackOverflowError(soa.Self());
546 return JValue();
547 }
548
Mathieu Chartier0795f232016-09-27 18:43:30 -0700549 ObjPtr<mirror::Object> receiver = soa.Decode<mirror::Object>(obj);
Andreas Gampe13b27842016-11-07 16:48:23 -0800550 ArtMethod* method = FindVirtualMethod(receiver, jni::DecodeArtMethod(mid));
Jeff Hao39b6c242015-05-19 20:30:23 -0700551 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
552 if (is_string_init) {
553 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100554 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700555 receiver = nullptr;
556 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700557 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700558 const char* shorty =
559 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700560 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700561 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700562 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700563 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700564 if (is_string_init) {
565 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700566 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700567 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700568 return result;
569}
570
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700571jobject InvokeMethod(const ScopedObjectAccessAlreadyRunnable& soa, jobject javaMethod,
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700572 jobject javaReceiver, jobject javaArgs, size_t num_frames) {
Dave Allison648d7112014-07-25 16:15:27 -0700573 // We want to make sure that the stack is not within a small distance from the
574 // protected region in case we are calling into a leaf function whose stack
575 // check has been elided.
576 if (UNLIKELY(__builtin_frame_address(0) <
577 soa.Self()->GetStackEndForInterpreter(true))) {
578 ThrowStackOverflowError(soa.Self());
579 return nullptr;
580 }
581
Mathieu Chartier0795f232016-09-27 18:43:30 -0700582 ObjPtr<mirror::Executable> executable = soa.Decode<mirror::Executable>(javaMethod);
Neil Fuller0e844392016-09-08 13:43:31 +0100583 const bool accessible = executable->IsAccessible();
584 ArtMethod* m = executable->GetArtMethod();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700585
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700586 ObjPtr<mirror::Class> declaring_class = m->GetDeclaringClass();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800587 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700588 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -0700589 HandleWrapperObjPtr<mirror::Class> h_class(hs.NewHandleWrapper(&declaring_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700590 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), h_class, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800591 return nullptr;
592 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700593 }
594
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700595 ObjPtr<mirror::Object> receiver;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700596 if (!m->IsStatic()) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800597 // Replace calls to String.<init> with equivalent StringFactory call.
598 if (declaring_class->IsStringClass() && m->IsConstructor()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100599 m = WellKnownClasses::StringInitToStringFactory(m);
Jeff Hao848f70a2014-01-15 13:49:50 -0800600 CHECK(javaReceiver == nullptr);
601 } else {
602 // Check that the receiver is non-null and an instance of the field's declaring class.
Mathieu Chartier0795f232016-09-27 18:43:30 -0700603 receiver = soa.Decode<mirror::Object>(javaReceiver);
Jeff Hao848f70a2014-01-15 13:49:50 -0800604 if (!VerifyObjectIsClass(receiver, declaring_class)) {
605 return nullptr;
606 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700607
Jeff Hao848f70a2014-01-15 13:49:50 -0800608 // Find the actual implementation of the virtual method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700609 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m, kRuntimePointerSize);
Jeff Hao848f70a2014-01-15 13:49:50 -0800610 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700611 }
612
613 // Get our arrays of arguments and their types, and check they're the same size.
Mathieu Chartier0795f232016-09-27 18:43:30 -0700614 ObjPtr<mirror::ObjectArray<mirror::Object>> objects =
615 soa.Decode<mirror::ObjectArray<mirror::Object>>(javaArgs);
Andreas Gampe542451c2016-07-26 09:02:02 -0700616 auto* np_method = m->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700617 const DexFile::TypeList* classes = np_method->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700618 uint32_t classes_size = (classes == nullptr) ? 0 : classes->Size();
619 uint32_t arg_count = (objects != nullptr) ? objects->GetLength() : 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800620 if (arg_count != classes_size) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000621 ThrowIllegalArgumentException(StringPrintf("Wrong number of arguments; expected %d, got %d",
Ian Rogers62d6c772013-02-27 08:32:07 -0800622 classes_size, arg_count).c_str());
Ian Rogersa0485602014-12-02 15:48:04 -0800623 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700624 }
625
Jeff Haocb4581a2014-03-28 15:43:37 -0700626 // If method is not set to be accessible, verify it can be accessed by the caller.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700627 ObjPtr<mirror::Class> calling_class;
Mathieu Chartier268764d2016-09-13 12:09:38 -0700628 if (!accessible && !VerifyAccess(soa.Self(),
629 receiver,
630 declaring_class,
631 m->GetAccessFlags(),
632 &calling_class,
633 num_frames)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000634 ThrowIllegalAccessException(
Andreas Gampec0d82292014-09-23 10:38:30 -0700635 StringPrintf("Class %s cannot access %s method %s of class %s",
David Sehr709b0702016-10-13 09:12:37 -0700636 calling_class == nullptr ? "null" : calling_class->PrettyClass().c_str(),
Andreas Gampec0d82292014-09-23 10:38:30 -0700637 PrettyJavaAccessFlags(m->GetAccessFlags()).c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700638 m->PrettyMethod().c_str(),
Andreas Gampec0d82292014-09-23 10:38:30 -0700639 m->GetDeclaringClass() == nullptr ? "null" :
David Sehr709b0702016-10-13 09:12:37 -0700640 m->GetDeclaringClass()->PrettyClass().c_str()).c_str());
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700641 return nullptr;
642 }
643
Ian Rogers53b8b092014-03-13 23:45:53 -0700644 // Invoke the method.
645 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700646 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700647 const char* shorty = np_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700648 ArgArray arg_array(shorty, shorty_len);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700649 if (!arg_array.BuildArgArrayFromObjectArray(receiver, objects, np_method)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700650 CHECK(soa.Self()->IsExceptionPending());
651 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700652 }
653
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700654 InvokeWithArgArray(soa, m, &arg_array, &result, shorty);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700655
656 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700657 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700658 // If we get another exception when we are trying to wrap, then just use that instead.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700659 jthrowable th = soa.Env()->ExceptionOccurred();
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700660 soa.Self()->ClearException();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700661 jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException");
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700662 if (exception_class == nullptr) {
663 soa.Self()->AssertPendingOOMException();
664 return nullptr;
665 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700666 jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700667 CHECK(mid != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700668 jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th);
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700669 if (exception_instance == nullptr) {
670 soa.Self()->AssertPendingOOMException();
671 return nullptr;
672 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700673 soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance));
Ian Rogersa0485602014-12-02 15:48:04 -0800674 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700675 }
676
677 // Box if necessary and return.
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -0700678 return soa.AddLocalReference<jobject>(BoxPrimitive(Primitive::GetType(shorty[0]), result));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700679}
680
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700681ObjPtr<mirror::Object> BoxPrimitive(Primitive::Type src_class, const JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700682 if (src_class == Primitive::kPrimNot) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700683 return MakeObjPtr(value.GetL());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700684 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700685 if (src_class == Primitive::kPrimVoid) {
686 // There's no such thing as a void field, and void methods invoked via reflection return null.
687 return nullptr;
688 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700689
Ian Rogers84956ff2014-03-26 23:52:41 -0700690 jmethodID m = nullptr;
Ian Rogers0177e532014-02-11 16:30:46 -0800691 const char* shorty;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700692 switch (src_class) {
693 case Primitive::kPrimBoolean:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700694 m = WellKnownClasses::java_lang_Boolean_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800695 shorty = "LZ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700696 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700697 case Primitive::kPrimByte:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700698 m = WellKnownClasses::java_lang_Byte_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800699 shorty = "LB";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700700 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700701 case Primitive::kPrimChar:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700702 m = WellKnownClasses::java_lang_Character_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800703 shorty = "LC";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700704 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700705 case Primitive::kPrimDouble:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700706 m = WellKnownClasses::java_lang_Double_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800707 shorty = "LD";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700708 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700709 case Primitive::kPrimFloat:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700710 m = WellKnownClasses::java_lang_Float_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800711 shorty = "LF";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700712 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700713 case Primitive::kPrimInt:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700714 m = WellKnownClasses::java_lang_Integer_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800715 shorty = "LI";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700716 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700717 case Primitive::kPrimLong:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700718 m = WellKnownClasses::java_lang_Long_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800719 shorty = "LJ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700720 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700721 case Primitive::kPrimShort:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700722 m = WellKnownClasses::java_lang_Short_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800723 shorty = "LS";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700724 break;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700725 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700726 LOG(FATAL) << static_cast<int>(src_class);
Ian Rogers0177e532014-02-11 16:30:46 -0800727 shorty = nullptr;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700728 }
729
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700730 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers53b8b092014-03-13 23:45:53 -0700731 DCHECK_EQ(soa.Self()->GetState(), kRunnable);
Jeff Hao5d917302013-02-27 17:57:33 -0800732
Ian Rogers53b8b092014-03-13 23:45:53 -0700733 ArgArray arg_array(shorty, 2);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800734 JValue result;
Jeff Hao5d917302013-02-27 17:57:33 -0800735 if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) {
736 arg_array.AppendWide(value.GetJ());
737 } else {
738 arg_array.Append(value.GetI());
739 }
740
Andreas Gampe13b27842016-11-07 16:48:23 -0800741 jni::DecodeArtMethod(m)->Invoke(soa.Self(),
742 arg_array.GetArray(),
743 arg_array.GetNumBytes(),
744 &result,
745 shorty);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800746 return result.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700747}
748
Mathieu Chartierc7853442015-03-27 14:35:38 -0700749static std::string UnboxingFailureKind(ArtField* f)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700750 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700751 if (f != nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700752 return "field " + f->PrettyField(false);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700753 }
754 return "result";
755}
756
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700757static bool UnboxPrimitive(ObjPtr<mirror::Object> o,
758 ObjPtr<mirror::Class> dst_class,
759 ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -0700760 JValue* unboxed_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700761 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700762 bool unbox_for_result = (f == nullptr);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700763 if (!dst_class->IsPrimitive()) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700764 if (UNLIKELY(o != nullptr && !o->InstanceOf(dst_class))) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800765 if (!unbox_for_result) {
David Sehr709b0702016-10-13 09:12:37 -0700766 ThrowIllegalArgumentException(
767 StringPrintf("%s has type %s, got %s",
768 UnboxingFailureKind(f).c_str(),
769 dst_class->PrettyDescriptor().c_str(),
770 o->PrettyTypeOf().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800771 } else {
David Sehr709b0702016-10-13 09:12:37 -0700772 ThrowClassCastException(
773 StringPrintf("Couldn't convert result of type %s to %s",
774 o->PrettyTypeOf().c_str(),
775 dst_class->PrettyDescriptor().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800776 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700777 return false;
778 }
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700779 unboxed_value->SetL(o);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700780 return true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800781 }
782 if (UNLIKELY(dst_class->GetPrimitiveType() == Primitive::kPrimVoid)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000783 ThrowIllegalArgumentException(StringPrintf("Can't unbox %s to void",
Ian Rogers84956ff2014-03-26 23:52:41 -0700784 UnboxingFailureKind(f).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700785 return false;
786 }
Ian Rogers84956ff2014-03-26 23:52:41 -0700787 if (UNLIKELY(o == nullptr)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800788 if (!unbox_for_result) {
David Sehr709b0702016-10-13 09:12:37 -0700789 ThrowIllegalArgumentException(
790 StringPrintf("%s has type %s, got null",
791 UnboxingFailureKind(f).c_str(),
792 dst_class->PrettyDescriptor().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800793 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700794 ThrowNullPointerException(
795 StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
David Sehr709b0702016-10-13 09:12:37 -0700796 dst_class->PrettyDescriptor().c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800797 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700798 return false;
799 }
800
Elliott Hughes1d878f32012-04-11 15:17:54 -0700801 JValue boxed_value;
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700802 ObjPtr<mirror::Class> klass = o->GetClass();
803 ObjPtr<mirror::Class> src_class = nullptr;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700804 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700805 ArtField* primitive_field = &klass->GetIFieldsPtr()->At(0);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700806 if (klass->DescriptorEquals("Ljava/lang/Boolean;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700807 src_class = class_linker->FindPrimitiveClass('Z');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700808 boxed_value.SetZ(primitive_field->GetBoolean(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700809 } else if (klass->DescriptorEquals("Ljava/lang/Byte;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700810 src_class = class_linker->FindPrimitiveClass('B');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700811 boxed_value.SetB(primitive_field->GetByte(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700812 } else if (klass->DescriptorEquals("Ljava/lang/Character;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700813 src_class = class_linker->FindPrimitiveClass('C');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700814 boxed_value.SetC(primitive_field->GetChar(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700815 } else if (klass->DescriptorEquals("Ljava/lang/Float;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700816 src_class = class_linker->FindPrimitiveClass('F');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700817 boxed_value.SetF(primitive_field->GetFloat(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700818 } else if (klass->DescriptorEquals("Ljava/lang/Double;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700819 src_class = class_linker->FindPrimitiveClass('D');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700820 boxed_value.SetD(primitive_field->GetDouble(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700821 } else if (klass->DescriptorEquals("Ljava/lang/Integer;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700822 src_class = class_linker->FindPrimitiveClass('I');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700823 boxed_value.SetI(primitive_field->GetInt(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700824 } else if (klass->DescriptorEquals("Ljava/lang/Long;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700825 src_class = class_linker->FindPrimitiveClass('J');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700826 boxed_value.SetJ(primitive_field->GetLong(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700827 } else if (klass->DescriptorEquals("Ljava/lang/Short;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700828 src_class = class_linker->FindPrimitiveClass('S');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700829 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700830 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700831 std::string temp;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000832 ThrowIllegalArgumentException(
Ian Rogers1ff3c982014-08-12 02:30:58 -0700833 StringPrintf("%s has type %s, got %s", UnboxingFailureKind(f).c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700834 dst_class->PrettyDescriptor().c_str(),
Ian Rogers1ff3c982014-08-12 02:30:58 -0700835 PrettyDescriptor(o->GetClass()->GetDescriptor(&temp)).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700836 return false;
837 }
838
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000839 return ConvertPrimitiveValue(unbox_for_result,
Ian Rogers62d6c772013-02-27 08:32:07 -0800840 src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700841 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700842}
843
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700844bool UnboxPrimitiveForField(ObjPtr<mirror::Object> o,
845 ObjPtr<mirror::Class> dst_class,
846 ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -0700847 JValue* unboxed_value) {
848 DCHECK(f != nullptr);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000849 return UnboxPrimitive(o, dst_class, f, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700850}
851
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700852bool UnboxPrimitiveForResult(ObjPtr<mirror::Object> o,
853 ObjPtr<mirror::Class> dst_class,
854 JValue* unboxed_value) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000855 return UnboxPrimitive(o, dst_class, nullptr, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700856}
857
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700858ObjPtr<mirror::Class> GetCallingClass(Thread* self, size_t num_frames) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700859 NthCallerVisitor visitor(self, num_frames);
860 visitor.WalkStack();
861 return visitor.caller != nullptr ? visitor.caller->GetDeclaringClass() : nullptr;
862}
863
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700864bool VerifyAccess(Thread* self,
865 ObjPtr<mirror::Object> obj,
866 ObjPtr<mirror::Class> declaring_class,
867 uint32_t access_flags,
868 ObjPtr<mirror::Class>* calling_class,
869 size_t num_frames) {
Mathieu Chartier76433272014-09-26 14:32:37 -0700870 if ((access_flags & kAccPublic) != 0) {
871 return true;
872 }
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700873 ObjPtr<mirror::Class> klass = GetCallingClass(self, num_frames);
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700874 if (UNLIKELY(klass == nullptr)) {
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100875 // The caller is an attached native thread.
Mathieu Chartier76433272014-09-26 14:32:37 -0700876 return false;
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100877 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700878 *calling_class = klass;
Mathieu Chartier268764d2016-09-13 12:09:38 -0700879 return VerifyAccess(obj, declaring_class, access_flags, klass);
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700880}
881
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700882bool VerifyAccess(ObjPtr<mirror::Object> obj,
883 ObjPtr<mirror::Class> declaring_class,
Mathieu Chartier268764d2016-09-13 12:09:38 -0700884 uint32_t access_flags,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700885 ObjPtr<mirror::Class> calling_class) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700886 if (calling_class == declaring_class) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700887 return true;
888 }
Mathieu Chartier268764d2016-09-13 12:09:38 -0700889 ScopedAssertNoThreadSuspension sants("verify-access");
Jeff Haocb4581a2014-03-28 15:43:37 -0700890 if ((access_flags & kAccPrivate) != 0) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700891 return false;
892 }
Jeff Haocb4581a2014-03-28 15:43:37 -0700893 if ((access_flags & kAccProtected) != 0) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700894 if (obj != nullptr && !obj->InstanceOf(calling_class) &&
Mathieu Chartier3398c782016-09-30 10:27:43 -0700895 !declaring_class->IsInSamePackage(calling_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700896 return false;
Mathieu Chartier3398c782016-09-30 10:27:43 -0700897 } else if (declaring_class->IsAssignableFrom(calling_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700898 return true;
899 }
900 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700901 return declaring_class->IsInSamePackage(calling_class);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700902}
903
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700904void InvalidReceiverError(ObjPtr<mirror::Object> o, ObjPtr<mirror::Class> c) {
David Sehr709b0702016-10-13 09:12:37 -0700905 std::string expected_class_name(mirror::Class::PrettyDescriptor(c));
906 std::string actual_class_name(mirror::Object::PrettyTypeOf(o));
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700907 ThrowIllegalArgumentException(StringPrintf("Expected receiver of type %s, but got %s",
908 expected_class_name.c_str(),
909 actual_class_name.c_str()).c_str());
910}
911
Jeff Hao83c81952015-05-27 19:29:29 -0700912// This only works if there's one reference which points to the object in obj.
913// Will need to be fixed if there's cases where it's not.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700914void UpdateReference(Thread* self, jobject obj, ObjPtr<mirror::Object> result) {
Jeff Hao83c81952015-05-27 19:29:29 -0700915 IndirectRef ref = reinterpret_cast<IndirectRef>(obj);
Andreas Gampedc061d02016-10-24 13:19:37 -0700916 IndirectRefKind kind = IndirectReferenceTable::GetIndirectRefKind(ref);
Jeff Hao83c81952015-05-27 19:29:29 -0700917 if (kind == kLocal) {
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -0700918 self->GetJniEnv()->locals.Update(obj, result);
Jeff Hao83c81952015-05-27 19:29:29 -0700919 } else if (kind == kHandleScopeOrInvalid) {
920 LOG(FATAL) << "Unsupported UpdateReference for kind kHandleScopeOrInvalid";
921 } else if (kind == kGlobal) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700922 self->GetJniEnv()->vm->UpdateGlobal(self, ref, result);
Jeff Hao83c81952015-05-27 19:29:29 -0700923 } else {
924 DCHECK_EQ(kind, kWeakGlobal);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700925 self->GetJniEnv()->vm->UpdateWeakGlobal(self, ref, result);
Jeff Hao83c81952015-05-27 19:29:29 -0700926 }
927}
928
Elliott Hughes418d20f2011-09-22 14:00:39 -0700929} // namespace art