blob: 066bc1264c869db724d33ad6d93f8b7e230a1471 [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
Ian Rogers53b8b092014-03-13 23:45:53 -070037class ArgArray {
38 public:
Roland Levillain3887c462015-08-12 18:15:42 +010039 ArgArray(const char* shorty, uint32_t shorty_len)
Ian Rogers53b8b092014-03-13 23:45:53 -070040 : shorty_(shorty), shorty_len_(shorty_len), num_bytes_(0) {
41 size_t num_slots = shorty_len + 1; // +1 in case of receiver.
42 if (LIKELY((num_slots * 2) < kSmallArgArraySize)) {
43 // We can trivially use the small arg array.
44 arg_array_ = small_arg_array_;
45 } else {
46 // Analyze shorty to see if we need the large arg array.
47 for (size_t i = 1; i < shorty_len; ++i) {
48 char c = shorty[i];
49 if (c == 'J' || c == 'D') {
50 num_slots++;
51 }
52 }
53 if (num_slots <= kSmallArgArraySize) {
54 arg_array_ = small_arg_array_;
55 } else {
56 large_arg_array_.reset(new uint32_t[num_slots]);
57 arg_array_ = large_arg_array_.get();
58 }
59 }
60 }
61
62 uint32_t* GetArray() {
63 return arg_array_;
64 }
65
66 uint32_t GetNumBytes() {
67 return num_bytes_;
68 }
69
70 void Append(uint32_t value) {
71 arg_array_[num_bytes_ / 4] = value;
72 num_bytes_ += 4;
73 }
74
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070075 void Append(ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_) {
76 Append(StackReference<mirror::Object>::FromMirrorPtr(obj.Decode()).AsVRegValue());
Ian Rogers53b8b092014-03-13 23:45:53 -070077 }
78
79 void AppendWide(uint64_t value) {
Ian Rogers53b8b092014-03-13 23:45:53 -070080 arg_array_[num_bytes_ / 4] = value;
81 arg_array_[(num_bytes_ / 4) + 1] = value >> 32;
82 num_bytes_ += 8;
83 }
84
85 void AppendFloat(float value) {
86 jvalue jv;
87 jv.f = value;
88 Append(jv.i);
89 }
90
91 void AppendDouble(double value) {
92 jvalue jv;
93 jv.d = value;
94 AppendWide(jv.j);
95 }
96
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070097 void BuildArgArrayFromVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070098 ObjPtr<mirror::Object> receiver,
99 va_list ap)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700100 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700101 // Set receiver if non-null (method is not static)
102 if (receiver != nullptr) {
103 Append(receiver);
104 }
105 for (size_t i = 1; i < shorty_len_; ++i) {
106 switch (shorty_[i]) {
107 case 'Z':
108 case 'B':
109 case 'C':
110 case 'S':
111 case 'I':
112 Append(va_arg(ap, jint));
113 break;
114 case 'F':
115 AppendFloat(va_arg(ap, jdouble));
116 break;
117 case 'L':
Mathieu Chartier0795f232016-09-27 18:43:30 -0700118 Append(soa.Decode<mirror::Object>(va_arg(ap, jobject)));
Ian Rogers53b8b092014-03-13 23:45:53 -0700119 break;
120 case 'D':
121 AppendDouble(va_arg(ap, jdouble));
122 break;
123 case 'J':
124 AppendWide(va_arg(ap, jlong));
125 break;
126#ifndef NDEBUG
127 default:
128 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
129#endif
130 }
131 }
132 }
133
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700134 void BuildArgArrayFromJValues(const ScopedObjectAccessAlreadyRunnable& soa,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700135 ObjPtr<mirror::Object> receiver, jvalue* args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700136 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700137 // Set receiver if non-null (method is not static)
138 if (receiver != nullptr) {
139 Append(receiver);
140 }
141 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
142 switch (shorty_[i]) {
143 case 'Z':
144 Append(args[args_offset].z);
145 break;
146 case 'B':
147 Append(args[args_offset].b);
148 break;
149 case 'C':
150 Append(args[args_offset].c);
151 break;
152 case 'S':
153 Append(args[args_offset].s);
154 break;
155 case 'I':
156 case 'F':
157 Append(args[args_offset].i);
158 break;
159 case 'L':
Mathieu Chartier0795f232016-09-27 18:43:30 -0700160 Append(soa.Decode<mirror::Object>(args[args_offset].l));
Ian Rogers53b8b092014-03-13 23:45:53 -0700161 break;
162 case 'D':
163 case 'J':
164 AppendWide(args[args_offset].j);
165 break;
166#ifndef NDEBUG
167 default:
168 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
169#endif
170 }
171 }
172 }
173
174 void BuildArgArrayFromFrame(ShadowFrame* shadow_frame, uint32_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700175 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700176 // Set receiver if non-null (method is not static)
177 size_t cur_arg = arg_offset;
178 if (!shadow_frame->GetMethod()->IsStatic()) {
179 Append(shadow_frame->GetVReg(cur_arg));
180 cur_arg++;
181 }
182 for (size_t i = 1; i < shorty_len_; ++i) {
183 switch (shorty_[i]) {
184 case 'Z':
185 case 'B':
186 case 'C':
187 case 'S':
188 case 'I':
189 case 'F':
190 case 'L':
191 Append(shadow_frame->GetVReg(cur_arg));
192 cur_arg++;
193 break;
194 case 'D':
195 case 'J':
196 AppendWide(shadow_frame->GetVRegLong(cur_arg));
197 cur_arg++;
198 cur_arg++;
199 break;
200#ifndef NDEBUG
201 default:
202 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
203#endif
204 }
205 }
206 }
207
208 static void ThrowIllegalPrimitiveArgumentException(const char* expected,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700209 const char* found_descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700210 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000211 ThrowIllegalArgumentException(
Ian Rogers53b8b092014-03-13 23:45:53 -0700212 StringPrintf("Invalid primitive conversion from %s to %s", expected,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700213 PrettyDescriptor(found_descriptor).c_str()).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700214 }
215
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700216 bool BuildArgArrayFromObjectArray(ObjPtr<mirror::Object> receiver,
217 ObjPtr<mirror::ObjectArray<mirror::Object>> args,
218 ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700219 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700220 const DexFile::TypeList* classes = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700221 // Set receiver if non-null (method is not static)
222 if (receiver != nullptr) {
223 Append(receiver);
224 }
225 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700226 ObjPtr<mirror::Object> arg(args->Get(args_offset));
Ian Rogers53b8b092014-03-13 23:45:53 -0700227 if (((shorty_[i] == 'L') && (arg != nullptr)) || ((arg == nullptr && shorty_[i] != 'L'))) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700228 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700229 ObjPtr<mirror::Class> dst_class(
Vladimir Marko05792b92015-08-03 11:56:49 +0100230 m->GetClassFromTypeIndex(classes->GetTypeItem(args_offset).type_idx_,
231 true /* resolve */,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700232 pointer_size));
Ian Rogers53b8b092014-03-13 23:45:53 -0700233 if (UNLIKELY(arg == nullptr || !arg->InstanceOf(dst_class))) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000234 ThrowIllegalArgumentException(
Ian Rogers11e4c032014-03-14 12:00:39 -0700235 StringPrintf("method %s argument %zd has type %s, got %s",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700236 PrettyMethod(m, false).c_str(),
Ian Rogers53b8b092014-03-13 23:45:53 -0700237 args_offset + 1, // Humans don't count from 0.
238 PrettyDescriptor(dst_class).c_str(),
239 PrettyTypeOf(arg).c_str()).c_str());
240 return false;
241 }
242 }
243
244#define DO_FIRST_ARG(match_descriptor, get_fn, append) { \
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700245 if (LIKELY(arg != nullptr && arg->GetClass()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700246 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Mathieu Chartier3398c782016-09-30 10:27:43 -0700247 append(primitive_field-> get_fn(arg));
Ian Rogers53b8b092014-03-13 23:45:53 -0700248
249#define DO_ARG(match_descriptor, get_fn, append) \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700250 } else if (LIKELY(arg != nullptr && \
251 arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700252 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Mathieu Chartier3398c782016-09-30 10:27:43 -0700253 append(primitive_field-> get_fn(arg));
Ian Rogers53b8b092014-03-13 23:45:53 -0700254
255#define DO_FAIL(expected) \
256 } else { \
257 if (arg->GetClass<>()->IsPrimitive()) { \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700258 std::string temp; \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700259 ThrowIllegalPrimitiveArgumentException(expected, \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700260 arg->GetClass<>()->GetDescriptor(&temp)); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700261 } else { \
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000262 ThrowIllegalArgumentException(\
Ian Rogers11e4c032014-03-14 12:00:39 -0700263 StringPrintf("method %s argument %zd has type %s, got %s", \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700264 PrettyMethod(m, false).c_str(), \
Ian Rogers53b8b092014-03-13 23:45:53 -0700265 args_offset + 1, \
266 expected, \
267 PrettyTypeOf(arg).c_str()).c_str()); \
268 } \
269 return false; \
270 } }
271
272 switch (shorty_[i]) {
273 case 'L':
274 Append(arg);
275 break;
276 case 'Z':
277 DO_FIRST_ARG("Ljava/lang/Boolean;", GetBoolean, Append)
278 DO_FAIL("boolean")
279 break;
280 case 'B':
281 DO_FIRST_ARG("Ljava/lang/Byte;", GetByte, Append)
282 DO_FAIL("byte")
283 break;
284 case 'C':
285 DO_FIRST_ARG("Ljava/lang/Character;", GetChar, Append)
286 DO_FAIL("char")
287 break;
288 case 'S':
289 DO_FIRST_ARG("Ljava/lang/Short;", GetShort, Append)
290 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
291 DO_FAIL("short")
292 break;
293 case 'I':
294 DO_FIRST_ARG("Ljava/lang/Integer;", GetInt, Append)
295 DO_ARG("Ljava/lang/Character;", GetChar, Append)
296 DO_ARG("Ljava/lang/Short;", GetShort, Append)
297 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
298 DO_FAIL("int")
299 break;
300 case 'J':
301 DO_FIRST_ARG("Ljava/lang/Long;", GetLong, AppendWide)
302 DO_ARG("Ljava/lang/Integer;", GetInt, AppendWide)
303 DO_ARG("Ljava/lang/Character;", GetChar, AppendWide)
304 DO_ARG("Ljava/lang/Short;", GetShort, AppendWide)
305 DO_ARG("Ljava/lang/Byte;", GetByte, AppendWide)
306 DO_FAIL("long")
307 break;
308 case 'F':
309 DO_FIRST_ARG("Ljava/lang/Float;", GetFloat, AppendFloat)
310 DO_ARG("Ljava/lang/Long;", GetLong, AppendFloat)
311 DO_ARG("Ljava/lang/Integer;", GetInt, AppendFloat)
312 DO_ARG("Ljava/lang/Character;", GetChar, AppendFloat)
313 DO_ARG("Ljava/lang/Short;", GetShort, AppendFloat)
314 DO_ARG("Ljava/lang/Byte;", GetByte, AppendFloat)
315 DO_FAIL("float")
316 break;
317 case 'D':
318 DO_FIRST_ARG("Ljava/lang/Double;", GetDouble, AppendDouble)
319 DO_ARG("Ljava/lang/Float;", GetFloat, AppendDouble)
320 DO_ARG("Ljava/lang/Long;", GetLong, AppendDouble)
321 DO_ARG("Ljava/lang/Integer;", GetInt, AppendDouble)
322 DO_ARG("Ljava/lang/Character;", GetChar, AppendDouble)
323 DO_ARG("Ljava/lang/Short;", GetShort, AppendDouble)
324 DO_ARG("Ljava/lang/Byte;", GetByte, AppendDouble)
325 DO_FAIL("double")
326 break;
327#ifndef NDEBUG
328 default:
329 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
Ian Rogersa0485602014-12-02 15:48:04 -0800330 UNREACHABLE();
Ian Rogers53b8b092014-03-13 23:45:53 -0700331#endif
332 }
333#undef DO_FIRST_ARG
334#undef DO_ARG
335#undef DO_FAIL
336 }
337 return true;
338 }
339
340 private:
341 enum { kSmallArgArraySize = 16 };
342 const char* const shorty_;
343 const uint32_t shorty_len_;
344 uint32_t num_bytes_;
345 uint32_t* arg_array_;
346 uint32_t small_arg_array_[kSmallArgArraySize];
Ian Rogers700a4022014-05-19 16:49:03 -0700347 std::unique_ptr<uint32_t[]> large_arg_array_;
Ian Rogers53b8b092014-03-13 23:45:53 -0700348};
349
Mathieu Chartiere401d142015-04-22 13:56:20 -0700350static void CheckMethodArguments(JavaVMExt* vm, ArtMethod* m, uint32_t* args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700351 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700352 const DexFile::TypeList* params = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700353 if (params == nullptr) {
354 return; // No arguments so nothing to check.
355 }
356 uint32_t offset = 0;
357 uint32_t num_params = params->Size();
358 size_t error_count = 0;
359 if (!m->IsStatic()) {
360 offset = 1;
361 }
Ian Rogersa0485602014-12-02 15:48:04 -0800362 // TODO: If args contain object references, it may cause problems.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700363 Thread* const self = Thread::Current();
Andreas Gampe542451c2016-07-26 09:02:02 -0700364 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Ian Rogers53b8b092014-03-13 23:45:53 -0700365 for (uint32_t i = 0; i < num_params; i++) {
366 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700367 ObjPtr<mirror::Class> param_type(m->GetClassFromTypeIndex(type_idx,
368 true /* resolve*/,
369 pointer_size));
Ian Rogers53b8b092014-03-13 23:45:53 -0700370 if (param_type == nullptr) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700371 CHECK(self->IsExceptionPending());
372 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700373 << m->GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000374 << self->GetException()->Dump();
Ian Rogers53b8b092014-03-13 23:45:53 -0700375 self->ClearException();
376 ++error_count;
377 } else if (!param_type->IsPrimitive()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700378 // TODO: There is a compaction bug here since GetClassFromTypeIdx can cause thread suspension,
379 // this is a hard to fix problem since the args can contain Object*, we need to save and
380 // restore them by using a visitor similar to the ones used in the trampoline entrypoints.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700381 ObjPtr<mirror::Object> argument =
Ian Rogers68d8b422014-07-17 11:09:10 -0700382 (reinterpret_cast<StackReference<mirror::Object>*>(&args[i + offset]))->AsMirrorPtr();
Ian Rogers53b8b092014-03-13 23:45:53 -0700383 if (argument != nullptr && !argument->InstanceOf(param_type)) {
384 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
385 << PrettyTypeOf(argument) << " as argument " << (i + 1)
Mathieu Chartiere401d142015-04-22 13:56:20 -0700386 << " to " << PrettyMethod(m);
Ian Rogers53b8b092014-03-13 23:45:53 -0700387 ++error_count;
388 }
389 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
390 offset++;
Ian Rogers68d8b422014-07-17 11:09:10 -0700391 } else {
392 int32_t arg = static_cast<int32_t>(args[i + offset]);
393 if (param_type->IsPrimitiveBoolean()) {
394 if (arg != JNI_TRUE && arg != JNI_FALSE) {
395 LOG(ERROR) << "JNI ERROR (app bug): expected jboolean (0/1) but got value of "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700396 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(m);
Ian Rogers68d8b422014-07-17 11:09:10 -0700397 ++error_count;
398 }
399 } else if (param_type->IsPrimitiveByte()) {
400 if (arg < -128 || arg > 127) {
401 LOG(ERROR) << "JNI ERROR (app bug): expected jbyte but got value of "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700402 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(m);
Ian Rogers68d8b422014-07-17 11:09:10 -0700403 ++error_count;
404 }
405 } else if (param_type->IsPrimitiveChar()) {
406 if (args[i + offset] > 0xFFFF) {
407 LOG(ERROR) << "JNI ERROR (app bug): expected jchar but got value of "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700408 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(m);
Ian Rogers68d8b422014-07-17 11:09:10 -0700409 ++error_count;
410 }
411 } else if (param_type->IsPrimitiveShort()) {
412 if (arg < -32768 || arg > 0x7FFF) {
413 LOG(ERROR) << "JNI ERROR (app bug): expected jshort but got value of "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700414 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(m);
Ian Rogers68d8b422014-07-17 11:09:10 -0700415 ++error_count;
416 }
417 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700418 }
419 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700420 if (UNLIKELY(error_count > 0)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700421 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
422 // with an argument.
Ian Rogers68d8b422014-07-17 11:09:10 -0700423 vm->JniAbortF(nullptr, "bad arguments passed to %s (see above for details)",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700424 PrettyMethod(m).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700425 }
426}
427
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700428static ArtMethod* FindVirtualMethod(ObjPtr<mirror::Object> receiver, ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700429 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700430 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method, kRuntimePointerSize);
Ian Rogers53b8b092014-03-13 23:45:53 -0700431}
432
433
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700434static void InvokeWithArgArray(const ScopedObjectAccessAlreadyRunnable& soa,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700435 ArtMethod* method, ArgArray* arg_array, JValue* result,
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700436 const char* shorty)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700437 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700438 uint32_t* args = arg_array->GetArray();
439 if (UNLIKELY(soa.Env()->check_jni)) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700440 CheckMethodArguments(soa.Vm(), method->GetInterfaceMethodIfProxy(kRuntimePointerSize), args);
Ian Rogers53b8b092014-03-13 23:45:53 -0700441 }
442 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, shorty);
443}
444
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700445JValue InvokeWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
446 va_list args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700447 REQUIRES_SHARED(Locks::mutator_lock_) {
Dave Allison648d7112014-07-25 16:15:27 -0700448 // We want to make sure that the stack is not within a small distance from the
449 // protected region in case we are calling into a leaf function whose stack
450 // check has been elided.
451 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
452 ThrowStackOverflowError(soa.Self());
453 return JValue();
454 }
455
Mathieu Chartiere401d142015-04-22 13:56:20 -0700456 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao39b6c242015-05-19 20:30:23 -0700457 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
458 if (is_string_init) {
459 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100460 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700461 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700462 ObjPtr<mirror::Object> receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700463 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700464 const char* shorty =
465 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700466 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700467 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700468 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700469 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700470 if (is_string_init) {
471 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700472 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700473 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700474 return result;
475}
476
Jeff Hao39b6c242015-05-19 20:30:23 -0700477JValue InvokeWithJValues(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
478 jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700479 // We want to make sure that the stack is not within a small distance from the
480 // protected region in case we are calling into a leaf function whose stack
481 // check has been elided.
482 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
483 ThrowStackOverflowError(soa.Self());
484 return JValue();
485 }
486
Mathieu Chartiere401d142015-04-22 13:56:20 -0700487 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao39b6c242015-05-19 20:30:23 -0700488 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
489 if (is_string_init) {
490 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100491 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700492 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700493 ObjPtr<mirror::Object> receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700494 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700495 const char* shorty =
496 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700497 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700498 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700499 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700500 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700501 if (is_string_init) {
502 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700503 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700504 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700505 return result;
506}
507
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700508JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
Jeff Hao39b6c242015-05-19 20:30:23 -0700509 jobject obj, jmethodID mid, jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700510 // We want to make sure that the stack is not within a small distance from the
511 // protected region in case we are calling into a leaf function whose stack
512 // check has been elided.
513 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
514 ThrowStackOverflowError(soa.Self());
515 return JValue();
516 }
517
Mathieu Chartier0795f232016-09-27 18:43:30 -0700518 ObjPtr<mirror::Object> receiver = soa.Decode<mirror::Object>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700519 ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Jeff Hao39b6c242015-05-19 20:30:23 -0700520 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
521 if (is_string_init) {
522 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100523 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700524 receiver = nullptr;
525 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700526 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700527 const char* shorty =
528 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700529 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700530 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700531 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700532 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700533 if (is_string_init) {
534 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700535 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700536 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700537 return result;
538}
539
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700540JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
Ian Rogers53b8b092014-03-13 23:45:53 -0700541 jobject obj, jmethodID mid, va_list args) {
Dave Allison648d7112014-07-25 16:15:27 -0700542 // We want to make sure that the stack is not within a small distance from the
543 // protected region in case we are calling into a leaf function whose stack
544 // check has been elided.
545 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
546 ThrowStackOverflowError(soa.Self());
547 return JValue();
548 }
549
Mathieu Chartier0795f232016-09-27 18:43:30 -0700550 ObjPtr<mirror::Object> receiver = soa.Decode<mirror::Object>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700551 ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Jeff Hao39b6c242015-05-19 20:30:23 -0700552 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
553 if (is_string_init) {
554 // Replace calls to String.<init> with equivalent StringFactory call.
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100555 method = WellKnownClasses::StringInitToStringFactory(method);
Jeff Hao39b6c242015-05-19 20:30:23 -0700556 receiver = nullptr;
557 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700558 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700559 const char* shorty =
560 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700561 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700562 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700563 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700564 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700565 if (is_string_init) {
566 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700567 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700568 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700569 return result;
570}
571
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700572jobject InvokeMethod(const ScopedObjectAccessAlreadyRunnable& soa, jobject javaMethod,
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700573 jobject javaReceiver, jobject javaArgs, size_t num_frames) {
Dave Allison648d7112014-07-25 16:15:27 -0700574 // We want to make sure that the stack is not within a small distance from the
575 // protected region in case we are calling into a leaf function whose stack
576 // check has been elided.
577 if (UNLIKELY(__builtin_frame_address(0) <
578 soa.Self()->GetStackEndForInterpreter(true))) {
579 ThrowStackOverflowError(soa.Self());
580 return nullptr;
581 }
582
Mathieu Chartier0795f232016-09-27 18:43:30 -0700583 ObjPtr<mirror::Executable> executable = soa.Decode<mirror::Executable>(javaMethod);
Neil Fuller0e844392016-09-08 13:43:31 +0100584 const bool accessible = executable->IsAccessible();
585 ArtMethod* m = executable->GetArtMethod();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700586
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700587 ObjPtr<mirror::Class> declaring_class = m->GetDeclaringClass();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800588 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700589 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -0700590 HandleWrapperObjPtr<mirror::Class> h_class(hs.NewHandleWrapper(&declaring_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700591 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), h_class, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800592 return nullptr;
593 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700594 }
595
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700596 ObjPtr<mirror::Object> receiver;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700597 if (!m->IsStatic()) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800598 // Replace calls to String.<init> with equivalent StringFactory call.
599 if (declaring_class->IsStringClass() && m->IsConstructor()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100600 m = WellKnownClasses::StringInitToStringFactory(m);
Jeff Hao848f70a2014-01-15 13:49:50 -0800601 CHECK(javaReceiver == nullptr);
602 } else {
603 // Check that the receiver is non-null and an instance of the field's declaring class.
Mathieu Chartier0795f232016-09-27 18:43:30 -0700604 receiver = soa.Decode<mirror::Object>(javaReceiver);
Jeff Hao848f70a2014-01-15 13:49:50 -0800605 if (!VerifyObjectIsClass(receiver, declaring_class)) {
606 return nullptr;
607 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700608
Jeff Hao848f70a2014-01-15 13:49:50 -0800609 // Find the actual implementation of the virtual method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700610 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m, kRuntimePointerSize);
Jeff Hao848f70a2014-01-15 13:49:50 -0800611 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700612 }
613
614 // Get our arrays of arguments and their types, and check they're the same size.
Mathieu Chartier0795f232016-09-27 18:43:30 -0700615 ObjPtr<mirror::ObjectArray<mirror::Object>> objects =
616 soa.Decode<mirror::ObjectArray<mirror::Object>>(javaArgs);
Andreas Gampe542451c2016-07-26 09:02:02 -0700617 auto* np_method = m->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700618 const DexFile::TypeList* classes = np_method->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700619 uint32_t classes_size = (classes == nullptr) ? 0 : classes->Size();
620 uint32_t arg_count = (objects != nullptr) ? objects->GetLength() : 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800621 if (arg_count != classes_size) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000622 ThrowIllegalArgumentException(StringPrintf("Wrong number of arguments; expected %d, got %d",
Ian Rogers62d6c772013-02-27 08:32:07 -0800623 classes_size, arg_count).c_str());
Ian Rogersa0485602014-12-02 15:48:04 -0800624 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700625 }
626
Jeff Haocb4581a2014-03-28 15:43:37 -0700627 // If method is not set to be accessible, verify it can be accessed by the caller.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700628 ObjPtr<mirror::Class> calling_class;
Mathieu Chartier268764d2016-09-13 12:09:38 -0700629 if (!accessible && !VerifyAccess(soa.Self(),
630 receiver,
631 declaring_class,
632 m->GetAccessFlags(),
633 &calling_class,
634 num_frames)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000635 ThrowIllegalAccessException(
Andreas Gampec0d82292014-09-23 10:38:30 -0700636 StringPrintf("Class %s cannot access %s method %s of class %s",
637 calling_class == nullptr ? "null" : PrettyClass(calling_class).c_str(),
638 PrettyJavaAccessFlags(m->GetAccessFlags()).c_str(),
639 PrettyMethod(m).c_str(),
640 m->GetDeclaringClass() == nullptr ? "null" :
641 PrettyClass(m->GetDeclaringClass()).c_str()).c_str());
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700642 return nullptr;
643 }
644
Ian Rogers53b8b092014-03-13 23:45:53 -0700645 // Invoke the method.
646 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700647 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700648 const char* shorty = np_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700649 ArgArray arg_array(shorty, shorty_len);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700650 if (!arg_array.BuildArgArrayFromObjectArray(receiver, objects, np_method)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700651 CHECK(soa.Self()->IsExceptionPending());
652 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700653 }
654
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700655 InvokeWithArgArray(soa, m, &arg_array, &result, shorty);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700656
657 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700658 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700659 // If we get another exception when we are trying to wrap, then just use that instead.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700660 jthrowable th = soa.Env()->ExceptionOccurred();
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700661 soa.Self()->ClearException();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700662 jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException");
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700663 if (exception_class == nullptr) {
664 soa.Self()->AssertPendingOOMException();
665 return nullptr;
666 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700667 jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700668 CHECK(mid != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700669 jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th);
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700670 if (exception_instance == nullptr) {
671 soa.Self()->AssertPendingOOMException();
672 return nullptr;
673 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700674 soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance));
Ian Rogersa0485602014-12-02 15:48:04 -0800675 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700676 }
677
678 // Box if necessary and return.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700679 return soa.AddLocalReference<jobject>(
680 BoxPrimitive(Primitive::GetType(shorty[0]), result).Decode());
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700681}
682
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700683ObjPtr<mirror::Object> BoxPrimitive(Primitive::Type src_class, const JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700684 if (src_class == Primitive::kPrimNot) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700685 return MakeObjPtr(value.GetL());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700686 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700687 if (src_class == Primitive::kPrimVoid) {
688 // There's no such thing as a void field, and void methods invoked via reflection return null.
689 return nullptr;
690 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700691
Ian Rogers84956ff2014-03-26 23:52:41 -0700692 jmethodID m = nullptr;
Ian Rogers0177e532014-02-11 16:30:46 -0800693 const char* shorty;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700694 switch (src_class) {
695 case Primitive::kPrimBoolean:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700696 m = WellKnownClasses::java_lang_Boolean_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800697 shorty = "LZ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700698 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700699 case Primitive::kPrimByte:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700700 m = WellKnownClasses::java_lang_Byte_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800701 shorty = "LB";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700702 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700703 case Primitive::kPrimChar:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700704 m = WellKnownClasses::java_lang_Character_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800705 shorty = "LC";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700706 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700707 case Primitive::kPrimDouble:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700708 m = WellKnownClasses::java_lang_Double_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800709 shorty = "LD";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700710 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700711 case Primitive::kPrimFloat:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700712 m = WellKnownClasses::java_lang_Float_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800713 shorty = "LF";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700714 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700715 case Primitive::kPrimInt:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700716 m = WellKnownClasses::java_lang_Integer_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800717 shorty = "LI";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700718 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700719 case Primitive::kPrimLong:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700720 m = WellKnownClasses::java_lang_Long_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800721 shorty = "LJ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700722 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700723 case Primitive::kPrimShort:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700724 m = WellKnownClasses::java_lang_Short_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800725 shorty = "LS";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700726 break;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700727 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700728 LOG(FATAL) << static_cast<int>(src_class);
Ian Rogers0177e532014-02-11 16:30:46 -0800729 shorty = nullptr;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700730 }
731
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700732 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers53b8b092014-03-13 23:45:53 -0700733 DCHECK_EQ(soa.Self()->GetState(), kRunnable);
Jeff Hao5d917302013-02-27 17:57:33 -0800734
Ian Rogers53b8b092014-03-13 23:45:53 -0700735 ArgArray arg_array(shorty, 2);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800736 JValue result;
Jeff Hao5d917302013-02-27 17:57:33 -0800737 if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) {
738 arg_array.AppendWide(value.GetJ());
739 } else {
740 arg_array.Append(value.GetI());
741 }
742
743 soa.DecodeMethod(m)->Invoke(soa.Self(), arg_array.GetArray(), arg_array.GetNumBytes(),
Ian Rogers0177e532014-02-11 16:30:46 -0800744 &result, shorty);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800745 return result.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700746}
747
Mathieu Chartierc7853442015-03-27 14:35:38 -0700748static std::string UnboxingFailureKind(ArtField* f)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700749 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700750 if (f != nullptr) {
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700751 return "field " + PrettyField(f, false);
752 }
753 return "result";
754}
755
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700756static bool UnboxPrimitive(ObjPtr<mirror::Object> o,
757 ObjPtr<mirror::Class> dst_class,
758 ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -0700759 JValue* unboxed_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700760 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700761 bool unbox_for_result = (f == nullptr);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700762 if (!dst_class->IsPrimitive()) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700763 if (UNLIKELY(o != nullptr && !o->InstanceOf(dst_class))) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800764 if (!unbox_for_result) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000765 ThrowIllegalArgumentException(StringPrintf("%s has type %s, got %s",
Ian Rogers84956ff2014-03-26 23:52:41 -0700766 UnboxingFailureKind(f).c_str(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800767 PrettyDescriptor(dst_class).c_str(),
768 PrettyTypeOf(o).c_str()).c_str());
769 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000770 ThrowClassCastException(StringPrintf("Couldn't convert result of type %s to %s",
Ian Rogers62d6c772013-02-27 08:32:07 -0800771 PrettyTypeOf(o).c_str(),
Brian Carlstromdf629502013-07-17 22:39:56 -0700772 PrettyDescriptor(dst_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800773 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700774 return false;
775 }
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700776 unboxed_value->SetL(o.Decode());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700777 return true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800778 }
779 if (UNLIKELY(dst_class->GetPrimitiveType() == Primitive::kPrimVoid)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000780 ThrowIllegalArgumentException(StringPrintf("Can't unbox %s to void",
Ian Rogers84956ff2014-03-26 23:52:41 -0700781 UnboxingFailureKind(f).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700782 return false;
783 }
Ian Rogers84956ff2014-03-26 23:52:41 -0700784 if (UNLIKELY(o == nullptr)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800785 if (!unbox_for_result) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000786 ThrowIllegalArgumentException(StringPrintf("%s has type %s, got null",
Ian Rogers84956ff2014-03-26 23:52:41 -0700787 UnboxingFailureKind(f).c_str(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800788 PrettyDescriptor(dst_class).c_str()).c_str());
789 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700790 ThrowNullPointerException(
791 StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
792 PrettyDescriptor(dst_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800793 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700794 return false;
795 }
796
Elliott Hughes1d878f32012-04-11 15:17:54 -0700797 JValue boxed_value;
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700798 ObjPtr<mirror::Class> klass = o->GetClass();
799 ObjPtr<mirror::Class> src_class = nullptr;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700800 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700801 ArtField* primitive_field = &klass->GetIFieldsPtr()->At(0);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700802 if (klass->DescriptorEquals("Ljava/lang/Boolean;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700803 src_class = class_linker->FindPrimitiveClass('Z');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700804 boxed_value.SetZ(primitive_field->GetBoolean(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700805 } else if (klass->DescriptorEquals("Ljava/lang/Byte;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700806 src_class = class_linker->FindPrimitiveClass('B');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700807 boxed_value.SetB(primitive_field->GetByte(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700808 } else if (klass->DescriptorEquals("Ljava/lang/Character;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700809 src_class = class_linker->FindPrimitiveClass('C');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700810 boxed_value.SetC(primitive_field->GetChar(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700811 } else if (klass->DescriptorEquals("Ljava/lang/Float;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700812 src_class = class_linker->FindPrimitiveClass('F');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700813 boxed_value.SetF(primitive_field->GetFloat(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700814 } else if (klass->DescriptorEquals("Ljava/lang/Double;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700815 src_class = class_linker->FindPrimitiveClass('D');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700816 boxed_value.SetD(primitive_field->GetDouble(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700817 } else if (klass->DescriptorEquals("Ljava/lang/Integer;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700818 src_class = class_linker->FindPrimitiveClass('I');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700819 boxed_value.SetI(primitive_field->GetInt(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700820 } else if (klass->DescriptorEquals("Ljava/lang/Long;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700821 src_class = class_linker->FindPrimitiveClass('J');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700822 boxed_value.SetJ(primitive_field->GetLong(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700823 } else if (klass->DescriptorEquals("Ljava/lang/Short;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700824 src_class = class_linker->FindPrimitiveClass('S');
Mathieu Chartier3398c782016-09-30 10:27:43 -0700825 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700826 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700827 std::string temp;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000828 ThrowIllegalArgumentException(
Ian Rogers1ff3c982014-08-12 02:30:58 -0700829 StringPrintf("%s has type %s, got %s", UnboxingFailureKind(f).c_str(),
830 PrettyDescriptor(dst_class).c_str(),
831 PrettyDescriptor(o->GetClass()->GetDescriptor(&temp)).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700832 return false;
833 }
834
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000835 return ConvertPrimitiveValue(unbox_for_result,
Ian Rogers62d6c772013-02-27 08:32:07 -0800836 src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700837 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700838}
839
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700840bool UnboxPrimitiveForField(ObjPtr<mirror::Object> o,
841 ObjPtr<mirror::Class> dst_class,
842 ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -0700843 JValue* unboxed_value) {
844 DCHECK(f != nullptr);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000845 return UnboxPrimitive(o, dst_class, f, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700846}
847
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700848bool UnboxPrimitiveForResult(ObjPtr<mirror::Object> o,
849 ObjPtr<mirror::Class> dst_class,
850 JValue* unboxed_value) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000851 return UnboxPrimitive(o, dst_class, nullptr, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700852}
853
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700854ObjPtr<mirror::Class> GetCallingClass(Thread* self, size_t num_frames) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700855 NthCallerVisitor visitor(self, num_frames);
856 visitor.WalkStack();
857 return visitor.caller != nullptr ? visitor.caller->GetDeclaringClass() : nullptr;
858}
859
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700860bool VerifyAccess(Thread* self,
861 ObjPtr<mirror::Object> obj,
862 ObjPtr<mirror::Class> declaring_class,
863 uint32_t access_flags,
864 ObjPtr<mirror::Class>* calling_class,
865 size_t num_frames) {
Mathieu Chartier76433272014-09-26 14:32:37 -0700866 if ((access_flags & kAccPublic) != 0) {
867 return true;
868 }
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700869 ObjPtr<mirror::Class> klass = GetCallingClass(self, num_frames);
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700870 if (UNLIKELY(klass == nullptr)) {
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100871 // The caller is an attached native thread.
Mathieu Chartier76433272014-09-26 14:32:37 -0700872 return false;
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100873 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700874 *calling_class = klass;
Mathieu Chartier268764d2016-09-13 12:09:38 -0700875 return VerifyAccess(obj, declaring_class, access_flags, klass);
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700876}
877
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700878bool VerifyAccess(ObjPtr<mirror::Object> obj,
879 ObjPtr<mirror::Class> declaring_class,
Mathieu Chartier268764d2016-09-13 12:09:38 -0700880 uint32_t access_flags,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700881 ObjPtr<mirror::Class> calling_class) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700882 if (calling_class == declaring_class) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700883 return true;
884 }
Mathieu Chartier268764d2016-09-13 12:09:38 -0700885 ScopedAssertNoThreadSuspension sants("verify-access");
Jeff Haocb4581a2014-03-28 15:43:37 -0700886 if ((access_flags & kAccPrivate) != 0) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700887 return false;
888 }
Jeff Haocb4581a2014-03-28 15:43:37 -0700889 if ((access_flags & kAccProtected) != 0) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700890 if (obj != nullptr && !obj->InstanceOf(calling_class) &&
Mathieu Chartier3398c782016-09-30 10:27:43 -0700891 !declaring_class->IsInSamePackage(calling_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700892 return false;
Mathieu Chartier3398c782016-09-30 10:27:43 -0700893 } else if (declaring_class->IsAssignableFrom(calling_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700894 return true;
895 }
896 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700897 return declaring_class->IsInSamePackage(calling_class);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700898}
899
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700900void InvalidReceiverError(ObjPtr<mirror::Object> o, ObjPtr<mirror::Class> c) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700901 std::string expected_class_name(PrettyDescriptor(c));
902 std::string actual_class_name(PrettyTypeOf(o));
903 ThrowIllegalArgumentException(StringPrintf("Expected receiver of type %s, but got %s",
904 expected_class_name.c_str(),
905 actual_class_name.c_str()).c_str());
906}
907
Jeff Hao83c81952015-05-27 19:29:29 -0700908// This only works if there's one reference which points to the object in obj.
909// Will need to be fixed if there's cases where it's not.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700910void UpdateReference(Thread* self, jobject obj, ObjPtr<mirror::Object> result) {
Jeff Hao83c81952015-05-27 19:29:29 -0700911 IndirectRef ref = reinterpret_cast<IndirectRef>(obj);
912 IndirectRefKind kind = GetIndirectRefKind(ref);
913 if (kind == kLocal) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700914 self->GetJniEnv()->locals.Update(obj, result.Decode());
Jeff Hao83c81952015-05-27 19:29:29 -0700915 } else if (kind == kHandleScopeOrInvalid) {
916 LOG(FATAL) << "Unsupported UpdateReference for kind kHandleScopeOrInvalid";
917 } else if (kind == kGlobal) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700918 self->GetJniEnv()->vm->UpdateGlobal(self, ref, result.Decode());
Jeff Hao83c81952015-05-27 19:29:29 -0700919 } else {
920 DCHECK_EQ(kind, kWeakGlobal);
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700921 self->GetJniEnv()->vm->UpdateWeakGlobal(self, ref, result.Decode());
Jeff Hao83c81952015-05-27 19:29:29 -0700922 }
923}
924
Elliott Hughes418d20f2011-09-22 14:00:39 -0700925} // namespace art