blob: 44e9bf542fe7b07658f792a33c46277cb7826a4a [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"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070027#include "mirror/abstract_method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/class-inl.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"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070031#include "scoped_thread_state_change.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070032#include "stack.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
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070075 void Append(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -070076 Append(StackReference<mirror::Object>::FromMirrorPtr(obj).AsVRegValue());
77 }
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,
98 mirror::Object* receiver, va_list ap)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070099 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700100 // Set receiver if non-null (method is not static)
101 if (receiver != nullptr) {
102 Append(receiver);
103 }
104 for (size_t i = 1; i < shorty_len_; ++i) {
105 switch (shorty_[i]) {
106 case 'Z':
107 case 'B':
108 case 'C':
109 case 'S':
110 case 'I':
111 Append(va_arg(ap, jint));
112 break;
113 case 'F':
114 AppendFloat(va_arg(ap, jdouble));
115 break;
116 case 'L':
117 Append(soa.Decode<mirror::Object*>(va_arg(ap, jobject)));
118 break;
119 case 'D':
120 AppendDouble(va_arg(ap, jdouble));
121 break;
122 case 'J':
123 AppendWide(va_arg(ap, jlong));
124 break;
125#ifndef NDEBUG
126 default:
127 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
128#endif
129 }
130 }
131 }
132
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700133 void BuildArgArrayFromJValues(const ScopedObjectAccessAlreadyRunnable& soa,
134 mirror::Object* receiver, jvalue* args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700135 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700136 // Set receiver if non-null (method is not static)
137 if (receiver != nullptr) {
138 Append(receiver);
139 }
140 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
141 switch (shorty_[i]) {
142 case 'Z':
143 Append(args[args_offset].z);
144 break;
145 case 'B':
146 Append(args[args_offset].b);
147 break;
148 case 'C':
149 Append(args[args_offset].c);
150 break;
151 case 'S':
152 Append(args[args_offset].s);
153 break;
154 case 'I':
155 case 'F':
156 Append(args[args_offset].i);
157 break;
158 case 'L':
159 Append(soa.Decode<mirror::Object*>(args[args_offset].l));
160 break;
161 case 'D':
162 case 'J':
163 AppendWide(args[args_offset].j);
164 break;
165#ifndef NDEBUG
166 default:
167 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
168#endif
169 }
170 }
171 }
172
173 void BuildArgArrayFromFrame(ShadowFrame* shadow_frame, uint32_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700174 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700175 // Set receiver if non-null (method is not static)
176 size_t cur_arg = arg_offset;
177 if (!shadow_frame->GetMethod()->IsStatic()) {
178 Append(shadow_frame->GetVReg(cur_arg));
179 cur_arg++;
180 }
181 for (size_t i = 1; i < shorty_len_; ++i) {
182 switch (shorty_[i]) {
183 case 'Z':
184 case 'B':
185 case 'C':
186 case 'S':
187 case 'I':
188 case 'F':
189 case 'L':
190 Append(shadow_frame->GetVReg(cur_arg));
191 cur_arg++;
192 break;
193 case 'D':
194 case 'J':
195 AppendWide(shadow_frame->GetVRegLong(cur_arg));
196 cur_arg++;
197 cur_arg++;
198 break;
199#ifndef NDEBUG
200 default:
201 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
202#endif
203 }
204 }
205 }
206
207 static void ThrowIllegalPrimitiveArgumentException(const char* expected,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700208 const char* found_descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700209 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000210 ThrowIllegalArgumentException(
Ian Rogers53b8b092014-03-13 23:45:53 -0700211 StringPrintf("Invalid primitive conversion from %s to %s", expected,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700212 PrettyDescriptor(found_descriptor).c_str()).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700213 }
214
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700215 bool BuildArgArrayFromObjectArray(mirror::Object* receiver,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700216 mirror::ObjectArray<mirror::Object>* args, ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700217 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700218 const DexFile::TypeList* classes = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700219 // Set receiver if non-null (method is not static)
220 if (receiver != nullptr) {
221 Append(receiver);
222 }
223 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
224 mirror::Object* arg = args->Get(args_offset);
225 if (((shorty_[i] == 'L') && (arg != nullptr)) || ((arg == nullptr && shorty_[i] != 'L'))) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700226 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Ian Rogers53b8b092014-03-13 23:45:53 -0700227 mirror::Class* dst_class =
Vladimir Marko05792b92015-08-03 11:56:49 +0100228 m->GetClassFromTypeIndex(classes->GetTypeItem(args_offset).type_idx_,
229 true /* resolve */,
230 pointer_size);
Ian Rogers53b8b092014-03-13 23:45:53 -0700231 if (UNLIKELY(arg == nullptr || !arg->InstanceOf(dst_class))) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000232 ThrowIllegalArgumentException(
Ian Rogers11e4c032014-03-14 12:00:39 -0700233 StringPrintf("method %s argument %zd has type %s, got %s",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700234 PrettyMethod(m, false).c_str(),
Ian Rogers53b8b092014-03-13 23:45:53 -0700235 args_offset + 1, // Humans don't count from 0.
236 PrettyDescriptor(dst_class).c_str(),
237 PrettyTypeOf(arg).c_str()).c_str());
238 return false;
239 }
240 }
241
242#define DO_FIRST_ARG(match_descriptor, get_fn, append) { \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700243 if (LIKELY(arg != nullptr && arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700244 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700245 append(primitive_field-> get_fn(arg));
246
247#define DO_ARG(match_descriptor, get_fn, append) \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700248 } else if (LIKELY(arg != nullptr && \
249 arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700250 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700251 append(primitive_field-> get_fn(arg));
252
253#define DO_FAIL(expected) \
254 } else { \
255 if (arg->GetClass<>()->IsPrimitive()) { \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700256 std::string temp; \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700257 ThrowIllegalPrimitiveArgumentException(expected, \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700258 arg->GetClass<>()->GetDescriptor(&temp)); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700259 } else { \
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000260 ThrowIllegalArgumentException(\
Ian Rogers11e4c032014-03-14 12:00:39 -0700261 StringPrintf("method %s argument %zd has type %s, got %s", \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700262 PrettyMethod(m, false).c_str(), \
Ian Rogers53b8b092014-03-13 23:45:53 -0700263 args_offset + 1, \
264 expected, \
265 PrettyTypeOf(arg).c_str()).c_str()); \
266 } \
267 return false; \
268 } }
269
270 switch (shorty_[i]) {
271 case 'L':
272 Append(arg);
273 break;
274 case 'Z':
275 DO_FIRST_ARG("Ljava/lang/Boolean;", GetBoolean, Append)
276 DO_FAIL("boolean")
277 break;
278 case 'B':
279 DO_FIRST_ARG("Ljava/lang/Byte;", GetByte, Append)
280 DO_FAIL("byte")
281 break;
282 case 'C':
283 DO_FIRST_ARG("Ljava/lang/Character;", GetChar, Append)
284 DO_FAIL("char")
285 break;
286 case 'S':
287 DO_FIRST_ARG("Ljava/lang/Short;", GetShort, Append)
288 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
289 DO_FAIL("short")
290 break;
291 case 'I':
292 DO_FIRST_ARG("Ljava/lang/Integer;", GetInt, Append)
293 DO_ARG("Ljava/lang/Character;", GetChar, Append)
294 DO_ARG("Ljava/lang/Short;", GetShort, Append)
295 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
296 DO_FAIL("int")
297 break;
298 case 'J':
299 DO_FIRST_ARG("Ljava/lang/Long;", GetLong, AppendWide)
300 DO_ARG("Ljava/lang/Integer;", GetInt, AppendWide)
301 DO_ARG("Ljava/lang/Character;", GetChar, AppendWide)
302 DO_ARG("Ljava/lang/Short;", GetShort, AppendWide)
303 DO_ARG("Ljava/lang/Byte;", GetByte, AppendWide)
304 DO_FAIL("long")
305 break;
306 case 'F':
307 DO_FIRST_ARG("Ljava/lang/Float;", GetFloat, AppendFloat)
308 DO_ARG("Ljava/lang/Long;", GetLong, AppendFloat)
309 DO_ARG("Ljava/lang/Integer;", GetInt, AppendFloat)
310 DO_ARG("Ljava/lang/Character;", GetChar, AppendFloat)
311 DO_ARG("Ljava/lang/Short;", GetShort, AppendFloat)
312 DO_ARG("Ljava/lang/Byte;", GetByte, AppendFloat)
313 DO_FAIL("float")
314 break;
315 case 'D':
316 DO_FIRST_ARG("Ljava/lang/Double;", GetDouble, AppendDouble)
317 DO_ARG("Ljava/lang/Float;", GetFloat, AppendDouble)
318 DO_ARG("Ljava/lang/Long;", GetLong, AppendDouble)
319 DO_ARG("Ljava/lang/Integer;", GetInt, AppendDouble)
320 DO_ARG("Ljava/lang/Character;", GetChar, AppendDouble)
321 DO_ARG("Ljava/lang/Short;", GetShort, AppendDouble)
322 DO_ARG("Ljava/lang/Byte;", GetByte, AppendDouble)
323 DO_FAIL("double")
324 break;
325#ifndef NDEBUG
326 default:
327 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
Ian Rogersa0485602014-12-02 15:48:04 -0800328 UNREACHABLE();
Ian Rogers53b8b092014-03-13 23:45:53 -0700329#endif
330 }
331#undef DO_FIRST_ARG
332#undef DO_ARG
333#undef DO_FAIL
334 }
335 return true;
336 }
337
338 private:
339 enum { kSmallArgArraySize = 16 };
340 const char* const shorty_;
341 const uint32_t shorty_len_;
342 uint32_t num_bytes_;
343 uint32_t* arg_array_;
344 uint32_t small_arg_array_[kSmallArgArraySize];
Ian Rogers700a4022014-05-19 16:49:03 -0700345 std::unique_ptr<uint32_t[]> large_arg_array_;
Ian Rogers53b8b092014-03-13 23:45:53 -0700346};
347
Mathieu Chartiere401d142015-04-22 13:56:20 -0700348static void CheckMethodArguments(JavaVMExt* vm, ArtMethod* m, uint32_t* args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700349 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700350 const DexFile::TypeList* params = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700351 if (params == nullptr) {
352 return; // No arguments so nothing to check.
353 }
354 uint32_t offset = 0;
355 uint32_t num_params = params->Size();
356 size_t error_count = 0;
357 if (!m->IsStatic()) {
358 offset = 1;
359 }
Ian Rogersa0485602014-12-02 15:48:04 -0800360 // TODO: If args contain object references, it may cause problems.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700361 Thread* const self = Thread::Current();
Andreas Gampe542451c2016-07-26 09:02:02 -0700362 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Ian Rogers53b8b092014-03-13 23:45:53 -0700363 for (uint32_t i = 0; i < num_params; i++) {
364 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100365 mirror::Class* param_type = m->GetClassFromTypeIndex(type_idx,
366 true /* resolve*/,
367 pointer_size);
Ian Rogers53b8b092014-03-13 23:45:53 -0700368 if (param_type == nullptr) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700369 CHECK(self->IsExceptionPending());
370 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700371 << m->GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000372 << self->GetException()->Dump();
Ian Rogers53b8b092014-03-13 23:45:53 -0700373 self->ClearException();
374 ++error_count;
375 } else if (!param_type->IsPrimitive()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700376 // TODO: There is a compaction bug here since GetClassFromTypeIdx can cause thread suspension,
377 // this is a hard to fix problem since the args can contain Object*, we need to save and
378 // restore them by using a visitor similar to the ones used in the trampoline entrypoints.
Ian Rogers68d8b422014-07-17 11:09:10 -0700379 mirror::Object* argument =
380 (reinterpret_cast<StackReference<mirror::Object>*>(&args[i + offset]))->AsMirrorPtr();
Ian Rogers53b8b092014-03-13 23:45:53 -0700381 if (argument != nullptr && !argument->InstanceOf(param_type)) {
382 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
383 << PrettyTypeOf(argument) << " as argument " << (i + 1)
Mathieu Chartiere401d142015-04-22 13:56:20 -0700384 << " to " << PrettyMethod(m);
Ian Rogers53b8b092014-03-13 23:45:53 -0700385 ++error_count;
386 }
387 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
388 offset++;
Ian Rogers68d8b422014-07-17 11:09:10 -0700389 } else {
390 int32_t arg = static_cast<int32_t>(args[i + offset]);
391 if (param_type->IsPrimitiveBoolean()) {
392 if (arg != JNI_TRUE && arg != JNI_FALSE) {
393 LOG(ERROR) << "JNI ERROR (app bug): expected jboolean (0/1) but got value of "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700394 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(m);
Ian Rogers68d8b422014-07-17 11:09:10 -0700395 ++error_count;
396 }
397 } else if (param_type->IsPrimitiveByte()) {
398 if (arg < -128 || arg > 127) {
399 LOG(ERROR) << "JNI ERROR (app bug): expected jbyte but got value of "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700400 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(m);
Ian Rogers68d8b422014-07-17 11:09:10 -0700401 ++error_count;
402 }
403 } else if (param_type->IsPrimitiveChar()) {
404 if (args[i + offset] > 0xFFFF) {
405 LOG(ERROR) << "JNI ERROR (app bug): expected jchar but got value of "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700406 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(m);
Ian Rogers68d8b422014-07-17 11:09:10 -0700407 ++error_count;
408 }
409 } else if (param_type->IsPrimitiveShort()) {
410 if (arg < -32768 || arg > 0x7FFF) {
411 LOG(ERROR) << "JNI ERROR (app bug): expected jshort but got value of "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700412 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(m);
Ian Rogers68d8b422014-07-17 11:09:10 -0700413 ++error_count;
414 }
415 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700416 }
417 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700418 if (UNLIKELY(error_count > 0)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700419 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
420 // with an argument.
Ian Rogers68d8b422014-07-17 11:09:10 -0700421 vm->JniAbortF(nullptr, "bad arguments passed to %s (see above for details)",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700422 PrettyMethod(m).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700423 }
424}
425
Mathieu Chartiere401d142015-04-22 13:56:20 -0700426static ArtMethod* FindVirtualMethod(mirror::Object* receiver, ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700427 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700428 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method, kRuntimePointerSize);
Ian Rogers53b8b092014-03-13 23:45:53 -0700429}
430
431
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700432static void InvokeWithArgArray(const ScopedObjectAccessAlreadyRunnable& soa,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700433 ArtMethod* method, ArgArray* arg_array, JValue* result,
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700434 const char* shorty)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700435 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700436 uint32_t* args = arg_array->GetArray();
437 if (UNLIKELY(soa.Env()->check_jni)) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700438 CheckMethodArguments(soa.Vm(), method->GetInterfaceMethodIfProxy(kRuntimePointerSize), args);
Ian Rogers53b8b092014-03-13 23:45:53 -0700439 }
440 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, shorty);
441}
442
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700443JValue InvokeWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
444 va_list args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700445 REQUIRES_SHARED(Locks::mutator_lock_) {
Dave Allison648d7112014-07-25 16:15:27 -0700446 // We want to make sure that the stack is not within a small distance from the
447 // protected region in case we are calling into a leaf function whose stack
448 // check has been elided.
449 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
450 ThrowStackOverflowError(soa.Self());
451 return JValue();
452 }
453
Mathieu Chartiere401d142015-04-22 13:56:20 -0700454 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao39b6c242015-05-19 20:30:23 -0700455 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
456 if (is_string_init) {
457 // Replace calls to String.<init> with equivalent StringFactory call.
458 method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
459 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700460 mirror::Object* receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object*>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700461 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700462 const char* shorty =
463 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700464 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700465 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700466 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700467 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700468 if (is_string_init) {
469 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700470 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700471 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700472 return result;
473}
474
Jeff Hao39b6c242015-05-19 20:30:23 -0700475JValue InvokeWithJValues(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
476 jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700477 // We want to make sure that the stack is not within a small distance from the
478 // protected region in case we are calling into a leaf function whose stack
479 // check has been elided.
480 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
481 ThrowStackOverflowError(soa.Self());
482 return JValue();
483 }
484
Mathieu Chartiere401d142015-04-22 13:56:20 -0700485 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao39b6c242015-05-19 20:30:23 -0700486 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
487 if (is_string_init) {
488 // Replace calls to String.<init> with equivalent StringFactory call.
489 method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
490 }
491 mirror::Object* receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object*>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700492 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700493 const char* shorty =
494 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700495 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700496 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700497 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700498 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700499 if (is_string_init) {
500 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700501 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700502 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700503 return result;
504}
505
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700506JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
Jeff Hao39b6c242015-05-19 20:30:23 -0700507 jobject obj, jmethodID mid, jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700508 // We want to make sure that the stack is not within a small distance from the
509 // protected region in case we are calling into a leaf function whose stack
510 // check has been elided.
511 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
512 ThrowStackOverflowError(soa.Self());
513 return JValue();
514 }
515
Jeff Hao39b6c242015-05-19 20:30:23 -0700516 mirror::Object* receiver = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700517 ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Jeff Hao39b6c242015-05-19 20:30:23 -0700518 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
519 if (is_string_init) {
520 // Replace calls to String.<init> with equivalent StringFactory call.
521 method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
522 receiver = nullptr;
523 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700524 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700525 const char* shorty =
526 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700527 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700528 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700529 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700530 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700531 if (is_string_init) {
532 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700533 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700534 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700535 return result;
536}
537
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700538JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
Ian Rogers53b8b092014-03-13 23:45:53 -0700539 jobject obj, jmethodID mid, va_list args) {
Dave Allison648d7112014-07-25 16:15:27 -0700540 // We want to make sure that the stack is not within a small distance from the
541 // protected region in case we are calling into a leaf function whose stack
542 // check has been elided.
543 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
544 ThrowStackOverflowError(soa.Self());
545 return JValue();
546 }
547
Ian Rogers53b8b092014-03-13 23:45:53 -0700548 mirror::Object* receiver = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700549 ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Jeff Hao39b6c242015-05-19 20:30:23 -0700550 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
551 if (is_string_init) {
552 // Replace calls to String.<init> with equivalent StringFactory call.
553 method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
554 receiver = nullptr;
555 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700556 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700557 const char* shorty =
558 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700559 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700560 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700561 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700562 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700563 if (is_string_init) {
564 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700565 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700566 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700567 return result;
568}
569
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700570jobject InvokeMethod(const ScopedObjectAccessAlreadyRunnable& soa, jobject javaMethod,
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700571 jobject javaReceiver, jobject javaArgs, size_t num_frames) {
Dave Allison648d7112014-07-25 16:15:27 -0700572 // We want to make sure that the stack is not within a small distance from the
573 // protected region in case we are calling into a leaf function whose stack
574 // check has been elided.
575 if (UNLIKELY(__builtin_frame_address(0) <
576 soa.Self()->GetStackEndForInterpreter(true))) {
577 ThrowStackOverflowError(soa.Self());
578 return nullptr;
579 }
580
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700581 auto* abstract_method = soa.Decode<mirror::AbstractMethod*>(javaMethod);
582 const bool accessible = abstract_method->IsAccessible();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700583 ArtMethod* m = abstract_method->GetArtMethod();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700584
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800585 mirror::Class* declaring_class = m->GetDeclaringClass();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800586 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700587 StackHandleScope<1> hs(soa.Self());
588 Handle<mirror::Class> h_class(hs.NewHandle(declaring_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700589 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), h_class, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800590 return nullptr;
591 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700592 declaring_class = h_class.Get();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700593 }
594
Ian Rogers53b8b092014-03-13 23:45:53 -0700595 mirror::Object* receiver = nullptr;
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()) {
599 jmethodID mid = soa.EncodeMethod(m);
600 m = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
601 CHECK(javaReceiver == nullptr);
602 } else {
603 // Check that the receiver is non-null and an instance of the field's declaring class.
604 receiver = soa.Decode<mirror::Object*>(javaReceiver);
605 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 Chartierfc58af42015-04-16 18:00:39 -0700615 auto* objects = 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.
Andreas Gampec0d82292014-09-23 10:38:30 -0700627 mirror::Class* calling_class = nullptr;
628 if (!accessible && !VerifyAccess(soa.Self(), receiver, declaring_class, m->GetAccessFlags(),
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700629 &calling_class, num_frames)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000630 ThrowIllegalAccessException(
Andreas Gampec0d82292014-09-23 10:38:30 -0700631 StringPrintf("Class %s cannot access %s method %s of class %s",
632 calling_class == nullptr ? "null" : PrettyClass(calling_class).c_str(),
633 PrettyJavaAccessFlags(m->GetAccessFlags()).c_str(),
634 PrettyMethod(m).c_str(),
635 m->GetDeclaringClass() == nullptr ? "null" :
636 PrettyClass(m->GetDeclaringClass()).c_str()).c_str());
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700637 return nullptr;
638 }
639
Ian Rogers53b8b092014-03-13 23:45:53 -0700640 // Invoke the method.
641 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700642 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700643 const char* shorty = np_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700644 ArgArray arg_array(shorty, shorty_len);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700645 if (!arg_array.BuildArgArrayFromObjectArray(receiver, objects, np_method)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700646 CHECK(soa.Self()->IsExceptionPending());
647 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700648 }
649
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700650 InvokeWithArgArray(soa, m, &arg_array, &result, shorty);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700651
652 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700653 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700654 // If we get another exception when we are trying to wrap, then just use that instead.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700655 jthrowable th = soa.Env()->ExceptionOccurred();
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700656 soa.Self()->ClearException();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700657 jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException");
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700658 if (exception_class == nullptr) {
659 soa.Self()->AssertPendingOOMException();
660 return nullptr;
661 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700662 jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700663 CHECK(mid != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700664 jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th);
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700665 if (exception_instance == nullptr) {
666 soa.Self()->AssertPendingOOMException();
667 return nullptr;
668 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700669 soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance));
Ian Rogersa0485602014-12-02 15:48:04 -0800670 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700671 }
672
673 // Box if necessary and return.
Ian Rogersa0485602014-12-02 15:48:04 -0800674 return soa.AddLocalReference<jobject>(BoxPrimitive(Primitive::GetType(shorty[0]), result));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700675}
676
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800677mirror::Object* BoxPrimitive(Primitive::Type src_class, const JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700678 if (src_class == Primitive::kPrimNot) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800679 return value.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700680 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700681 if (src_class == Primitive::kPrimVoid) {
682 // There's no such thing as a void field, and void methods invoked via reflection return null.
683 return nullptr;
684 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700685
Ian Rogers84956ff2014-03-26 23:52:41 -0700686 jmethodID m = nullptr;
Ian Rogers0177e532014-02-11 16:30:46 -0800687 const char* shorty;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700688 switch (src_class) {
689 case Primitive::kPrimBoolean:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700690 m = WellKnownClasses::java_lang_Boolean_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800691 shorty = "LZ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700692 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700693 case Primitive::kPrimByte:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700694 m = WellKnownClasses::java_lang_Byte_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800695 shorty = "LB";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700696 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700697 case Primitive::kPrimChar:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700698 m = WellKnownClasses::java_lang_Character_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800699 shorty = "LC";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700700 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700701 case Primitive::kPrimDouble:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700702 m = WellKnownClasses::java_lang_Double_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800703 shorty = "LD";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700704 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700705 case Primitive::kPrimFloat:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700706 m = WellKnownClasses::java_lang_Float_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800707 shorty = "LF";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700708 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700709 case Primitive::kPrimInt:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700710 m = WellKnownClasses::java_lang_Integer_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800711 shorty = "LI";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700712 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700713 case Primitive::kPrimLong:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700714 m = WellKnownClasses::java_lang_Long_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800715 shorty = "LJ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700716 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700717 case Primitive::kPrimShort:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700718 m = WellKnownClasses::java_lang_Short_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800719 shorty = "LS";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700720 break;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700721 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700722 LOG(FATAL) << static_cast<int>(src_class);
Ian Rogers0177e532014-02-11 16:30:46 -0800723 shorty = nullptr;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700724 }
725
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700726 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers53b8b092014-03-13 23:45:53 -0700727 DCHECK_EQ(soa.Self()->GetState(), kRunnable);
Jeff Hao5d917302013-02-27 17:57:33 -0800728
Ian Rogers53b8b092014-03-13 23:45:53 -0700729 ArgArray arg_array(shorty, 2);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800730 JValue result;
Jeff Hao5d917302013-02-27 17:57:33 -0800731 if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) {
732 arg_array.AppendWide(value.GetJ());
733 } else {
734 arg_array.Append(value.GetI());
735 }
736
737 soa.DecodeMethod(m)->Invoke(soa.Self(), arg_array.GetArray(), arg_array.GetNumBytes(),
Ian Rogers0177e532014-02-11 16:30:46 -0800738 &result, shorty);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800739 return result.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700740}
741
Mathieu Chartierc7853442015-03-27 14:35:38 -0700742static std::string UnboxingFailureKind(ArtField* f)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700743 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700744 if (f != nullptr) {
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700745 return "field " + PrettyField(f, false);
746 }
747 return "result";
748}
749
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000750static bool UnboxPrimitive(mirror::Object* o,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700751 mirror::Class* dst_class, ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -0700752 JValue* unboxed_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700753 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700754 bool unbox_for_result = (f == nullptr);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700755 if (!dst_class->IsPrimitive()) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700756 if (UNLIKELY(o != nullptr && !o->InstanceOf(dst_class))) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800757 if (!unbox_for_result) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000758 ThrowIllegalArgumentException(StringPrintf("%s has type %s, got %s",
Ian Rogers84956ff2014-03-26 23:52:41 -0700759 UnboxingFailureKind(f).c_str(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800760 PrettyDescriptor(dst_class).c_str(),
761 PrettyTypeOf(o).c_str()).c_str());
762 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000763 ThrowClassCastException(StringPrintf("Couldn't convert result of type %s to %s",
Ian Rogers62d6c772013-02-27 08:32:07 -0800764 PrettyTypeOf(o).c_str(),
Brian Carlstromdf629502013-07-17 22:39:56 -0700765 PrettyDescriptor(dst_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800766 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700767 return false;
768 }
Ian Rogers84956ff2014-03-26 23:52:41 -0700769 unboxed_value->SetL(o);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700770 return true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800771 }
772 if (UNLIKELY(dst_class->GetPrimitiveType() == Primitive::kPrimVoid)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000773 ThrowIllegalArgumentException(StringPrintf("Can't unbox %s to void",
Ian Rogers84956ff2014-03-26 23:52:41 -0700774 UnboxingFailureKind(f).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700775 return false;
776 }
Ian Rogers84956ff2014-03-26 23:52:41 -0700777 if (UNLIKELY(o == nullptr)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800778 if (!unbox_for_result) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000779 ThrowIllegalArgumentException(StringPrintf("%s has type %s, got null",
Ian Rogers84956ff2014-03-26 23:52:41 -0700780 UnboxingFailureKind(f).c_str(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800781 PrettyDescriptor(dst_class).c_str()).c_str());
782 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700783 ThrowNullPointerException(
784 StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
785 PrettyDescriptor(dst_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800786 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700787 return false;
788 }
789
Elliott Hughes1d878f32012-04-11 15:17:54 -0700790 JValue boxed_value;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700791 mirror::Class* klass = o->GetClass();
Ian Rogers84956ff2014-03-26 23:52:41 -0700792 mirror::Class* src_class = nullptr;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700793 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700794 ArtField* primitive_field = &klass->GetIFieldsPtr()->At(0);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700795 if (klass->DescriptorEquals("Ljava/lang/Boolean;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700796 src_class = class_linker->FindPrimitiveClass('Z');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700797 boxed_value.SetZ(primitive_field->GetBoolean(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700798 } else if (klass->DescriptorEquals("Ljava/lang/Byte;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700799 src_class = class_linker->FindPrimitiveClass('B');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700800 boxed_value.SetB(primitive_field->GetByte(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700801 } else if (klass->DescriptorEquals("Ljava/lang/Character;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700802 src_class = class_linker->FindPrimitiveClass('C');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700803 boxed_value.SetC(primitive_field->GetChar(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700804 } else if (klass->DescriptorEquals("Ljava/lang/Float;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700805 src_class = class_linker->FindPrimitiveClass('F');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700806 boxed_value.SetF(primitive_field->GetFloat(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700807 } else if (klass->DescriptorEquals("Ljava/lang/Double;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700808 src_class = class_linker->FindPrimitiveClass('D');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700809 boxed_value.SetD(primitive_field->GetDouble(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700810 } else if (klass->DescriptorEquals("Ljava/lang/Integer;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700811 src_class = class_linker->FindPrimitiveClass('I');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700812 boxed_value.SetI(primitive_field->GetInt(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700813 } else if (klass->DescriptorEquals("Ljava/lang/Long;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700814 src_class = class_linker->FindPrimitiveClass('J');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700815 boxed_value.SetJ(primitive_field->GetLong(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700816 } else if (klass->DescriptorEquals("Ljava/lang/Short;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700817 src_class = class_linker->FindPrimitiveClass('S');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700818 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700819 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700820 std::string temp;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000821 ThrowIllegalArgumentException(
Ian Rogers1ff3c982014-08-12 02:30:58 -0700822 StringPrintf("%s has type %s, got %s", UnboxingFailureKind(f).c_str(),
823 PrettyDescriptor(dst_class).c_str(),
824 PrettyDescriptor(o->GetClass()->GetDescriptor(&temp)).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700825 return false;
826 }
827
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000828 return ConvertPrimitiveValue(unbox_for_result,
Ian Rogers62d6c772013-02-27 08:32:07 -0800829 src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700830 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700831}
832
Mathieu Chartierc7853442015-03-27 14:35:38 -0700833bool UnboxPrimitiveForField(mirror::Object* o, mirror::Class* dst_class, ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -0700834 JValue* unboxed_value) {
835 DCHECK(f != nullptr);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000836 return UnboxPrimitive(o, dst_class, f, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700837}
838
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700839bool UnboxPrimitiveForResult(mirror::Object* o, mirror::Class* dst_class, JValue* unboxed_value) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000840 return UnboxPrimitive(o, dst_class, nullptr, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700841}
842
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700843mirror::Class* GetCallingClass(Thread* self, size_t num_frames) {
844 NthCallerVisitor visitor(self, num_frames);
845 visitor.WalkStack();
846 return visitor.caller != nullptr ? visitor.caller->GetDeclaringClass() : nullptr;
847}
848
Andreas Gampec0d82292014-09-23 10:38:30 -0700849bool VerifyAccess(Thread* self, mirror::Object* obj, mirror::Class* declaring_class,
Mathieu Chartierca239af2015-03-29 18:27:50 -0700850 uint32_t access_flags, mirror::Class** calling_class, size_t num_frames) {
Mathieu Chartier76433272014-09-26 14:32:37 -0700851 if ((access_flags & kAccPublic) != 0) {
852 return true;
853 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700854 auto* klass = GetCallingClass(self, num_frames);
855 if (UNLIKELY(klass == nullptr)) {
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100856 // The caller is an attached native thread.
Mathieu Chartier76433272014-09-26 14:32:37 -0700857 return false;
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100858 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700859 *calling_class = klass;
860 return VerifyAccess(self, obj, declaring_class, access_flags, klass);
861}
862
863bool VerifyAccess(Thread* self, mirror::Object* obj, mirror::Class* declaring_class,
864 uint32_t access_flags, mirror::Class* calling_class) {
865 if (calling_class == declaring_class) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700866 return true;
867 }
Andreas Gampec0d82292014-09-23 10:38:30 -0700868 ScopedAssertNoThreadSuspension sants(self, "verify-access");
Jeff Haocb4581a2014-03-28 15:43:37 -0700869 if ((access_flags & kAccPrivate) != 0) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700870 return false;
871 }
Jeff Haocb4581a2014-03-28 15:43:37 -0700872 if ((access_flags & kAccProtected) != 0) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700873 if (obj != nullptr && !obj->InstanceOf(calling_class) &&
874 !declaring_class->IsInSamePackage(calling_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700875 return false;
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700876 } else if (declaring_class->IsAssignableFrom(calling_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700877 return true;
878 }
879 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700880 return declaring_class->IsInSamePackage(calling_class);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700881}
882
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700883void InvalidReceiverError(mirror::Object* o, mirror::Class* c) {
884 std::string expected_class_name(PrettyDescriptor(c));
885 std::string actual_class_name(PrettyTypeOf(o));
886 ThrowIllegalArgumentException(StringPrintf("Expected receiver of type %s, but got %s",
887 expected_class_name.c_str(),
888 actual_class_name.c_str()).c_str());
889}
890
Jeff Hao83c81952015-05-27 19:29:29 -0700891// This only works if there's one reference which points to the object in obj.
892// Will need to be fixed if there's cases where it's not.
893void UpdateReference(Thread* self, jobject obj, mirror::Object* result) {
894 IndirectRef ref = reinterpret_cast<IndirectRef>(obj);
895 IndirectRefKind kind = GetIndirectRefKind(ref);
896 if (kind == kLocal) {
897 self->GetJniEnv()->locals.Update(obj, result);
898 } else if (kind == kHandleScopeOrInvalid) {
899 LOG(FATAL) << "Unsupported UpdateReference for kind kHandleScopeOrInvalid";
900 } else if (kind == kGlobal) {
901 self->GetJniEnv()->vm->UpdateGlobal(self, ref, result);
902 } else {
903 DCHECK_EQ(kind, kWeakGlobal);
904 self->GetJniEnv()->vm->UpdateWeakGlobal(self, ref, result);
905 }
906}
907
Elliott Hughes418d20f2011-09-22 14:00:39 -0700908} // namespace art