blob: 2fe1e64fe72013ada5248dff3ebe870cf17c1f5e [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"
Elliott Hughes418d20f2011-09-22 14:00:39 -070021#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080022#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070023#include "dex_file-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070024#include "entrypoints/entrypoint_utils.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
Mathieu Chartier90443472015-07-16 20:32:27 -070075 void Append(mirror::Object* obj) SHARED_REQUIRES(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)
Mathieu Chartier90443472015-07-16 20:32:27 -070099 SHARED_REQUIRES(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)
Mathieu Chartier90443472015-07-16 20:32:27 -0700135 SHARED_REQUIRES(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)
Mathieu Chartier90443472015-07-16 20:32:27 -0700174 SHARED_REQUIRES(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)
Mathieu Chartier90443472015-07-16 20:32:27 -0700209 SHARED_REQUIRES(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)
Mathieu Chartier90443472015-07-16 20:32:27 -0700217 SHARED_REQUIRES(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'))) {
226 mirror::Class* dst_class =
Mathieu Chartiere401d142015-04-22 13:56:20 -0700227 m->GetClassFromTypeIndex(classes->GetTypeItem(args_offset).type_idx_, true);
Ian Rogers53b8b092014-03-13 23:45:53 -0700228 if (UNLIKELY(arg == nullptr || !arg->InstanceOf(dst_class))) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000229 ThrowIllegalArgumentException(
Ian Rogers11e4c032014-03-14 12:00:39 -0700230 StringPrintf("method %s argument %zd has type %s, got %s",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700231 PrettyMethod(m, false).c_str(),
Ian Rogers53b8b092014-03-13 23:45:53 -0700232 args_offset + 1, // Humans don't count from 0.
233 PrettyDescriptor(dst_class).c_str(),
234 PrettyTypeOf(arg).c_str()).c_str());
235 return false;
236 }
237 }
238
239#define DO_FIRST_ARG(match_descriptor, get_fn, append) { \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700240 if (LIKELY(arg != nullptr && arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700241 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700242 append(primitive_field-> get_fn(arg));
243
244#define DO_ARG(match_descriptor, get_fn, append) \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700245 } else if (LIKELY(arg != nullptr && \
246 arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700247 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700248 append(primitive_field-> get_fn(arg));
249
250#define DO_FAIL(expected) \
251 } else { \
252 if (arg->GetClass<>()->IsPrimitive()) { \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700253 std::string temp; \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700254 ThrowIllegalPrimitiveArgumentException(expected, \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700255 arg->GetClass<>()->GetDescriptor(&temp)); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700256 } else { \
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000257 ThrowIllegalArgumentException(\
Ian Rogers11e4c032014-03-14 12:00:39 -0700258 StringPrintf("method %s argument %zd has type %s, got %s", \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700259 PrettyMethod(m, false).c_str(), \
Ian Rogers53b8b092014-03-13 23:45:53 -0700260 args_offset + 1, \
261 expected, \
262 PrettyTypeOf(arg).c_str()).c_str()); \
263 } \
264 return false; \
265 } }
266
267 switch (shorty_[i]) {
268 case 'L':
269 Append(arg);
270 break;
271 case 'Z':
272 DO_FIRST_ARG("Ljava/lang/Boolean;", GetBoolean, Append)
273 DO_FAIL("boolean")
274 break;
275 case 'B':
276 DO_FIRST_ARG("Ljava/lang/Byte;", GetByte, Append)
277 DO_FAIL("byte")
278 break;
279 case 'C':
280 DO_FIRST_ARG("Ljava/lang/Character;", GetChar, Append)
281 DO_FAIL("char")
282 break;
283 case 'S':
284 DO_FIRST_ARG("Ljava/lang/Short;", GetShort, Append)
285 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
286 DO_FAIL("short")
287 break;
288 case 'I':
289 DO_FIRST_ARG("Ljava/lang/Integer;", GetInt, Append)
290 DO_ARG("Ljava/lang/Character;", GetChar, Append)
291 DO_ARG("Ljava/lang/Short;", GetShort, Append)
292 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
293 DO_FAIL("int")
294 break;
295 case 'J':
296 DO_FIRST_ARG("Ljava/lang/Long;", GetLong, AppendWide)
297 DO_ARG("Ljava/lang/Integer;", GetInt, AppendWide)
298 DO_ARG("Ljava/lang/Character;", GetChar, AppendWide)
299 DO_ARG("Ljava/lang/Short;", GetShort, AppendWide)
300 DO_ARG("Ljava/lang/Byte;", GetByte, AppendWide)
301 DO_FAIL("long")
302 break;
303 case 'F':
304 DO_FIRST_ARG("Ljava/lang/Float;", GetFloat, AppendFloat)
305 DO_ARG("Ljava/lang/Long;", GetLong, AppendFloat)
306 DO_ARG("Ljava/lang/Integer;", GetInt, AppendFloat)
307 DO_ARG("Ljava/lang/Character;", GetChar, AppendFloat)
308 DO_ARG("Ljava/lang/Short;", GetShort, AppendFloat)
309 DO_ARG("Ljava/lang/Byte;", GetByte, AppendFloat)
310 DO_FAIL("float")
311 break;
312 case 'D':
313 DO_FIRST_ARG("Ljava/lang/Double;", GetDouble, AppendDouble)
314 DO_ARG("Ljava/lang/Float;", GetFloat, AppendDouble)
315 DO_ARG("Ljava/lang/Long;", GetLong, AppendDouble)
316 DO_ARG("Ljava/lang/Integer;", GetInt, AppendDouble)
317 DO_ARG("Ljava/lang/Character;", GetChar, AppendDouble)
318 DO_ARG("Ljava/lang/Short;", GetShort, AppendDouble)
319 DO_ARG("Ljava/lang/Byte;", GetByte, AppendDouble)
320 DO_FAIL("double")
321 break;
322#ifndef NDEBUG
323 default:
324 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
Ian Rogersa0485602014-12-02 15:48:04 -0800325 UNREACHABLE();
Ian Rogers53b8b092014-03-13 23:45:53 -0700326#endif
327 }
328#undef DO_FIRST_ARG
329#undef DO_ARG
330#undef DO_FAIL
331 }
332 return true;
333 }
334
335 private:
336 enum { kSmallArgArraySize = 16 };
337 const char* const shorty_;
338 const uint32_t shorty_len_;
339 uint32_t num_bytes_;
340 uint32_t* arg_array_;
341 uint32_t small_arg_array_[kSmallArgArraySize];
Ian Rogers700a4022014-05-19 16:49:03 -0700342 std::unique_ptr<uint32_t[]> large_arg_array_;
Ian Rogers53b8b092014-03-13 23:45:53 -0700343};
344
Mathieu Chartiere401d142015-04-22 13:56:20 -0700345static void CheckMethodArguments(JavaVMExt* vm, ArtMethod* m, uint32_t* args)
Mathieu Chartier90443472015-07-16 20:32:27 -0700346 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700347 const DexFile::TypeList* params = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700348 if (params == nullptr) {
349 return; // No arguments so nothing to check.
350 }
351 uint32_t offset = 0;
352 uint32_t num_params = params->Size();
353 size_t error_count = 0;
354 if (!m->IsStatic()) {
355 offset = 1;
356 }
Ian Rogersa0485602014-12-02 15:48:04 -0800357 // TODO: If args contain object references, it may cause problems.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700358 Thread* const self = Thread::Current();
Ian Rogers53b8b092014-03-13 23:45:53 -0700359 for (uint32_t i = 0; i < num_params; i++) {
360 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700361 mirror::Class* param_type = m->GetClassFromTypeIndex(type_idx, true);
Ian Rogers53b8b092014-03-13 23:45:53 -0700362 if (param_type == nullptr) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700363 CHECK(self->IsExceptionPending());
364 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700365 << m->GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000366 << self->GetException()->Dump();
Ian Rogers53b8b092014-03-13 23:45:53 -0700367 self->ClearException();
368 ++error_count;
369 } else if (!param_type->IsPrimitive()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700370 // TODO: There is a compaction bug here since GetClassFromTypeIdx can cause thread suspension,
371 // this is a hard to fix problem since the args can contain Object*, we need to save and
372 // restore them by using a visitor similar to the ones used in the trampoline entrypoints.
Ian Rogers68d8b422014-07-17 11:09:10 -0700373 mirror::Object* argument =
374 (reinterpret_cast<StackReference<mirror::Object>*>(&args[i + offset]))->AsMirrorPtr();
Ian Rogers53b8b092014-03-13 23:45:53 -0700375 if (argument != nullptr && !argument->InstanceOf(param_type)) {
376 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
377 << PrettyTypeOf(argument) << " as argument " << (i + 1)
Mathieu Chartiere401d142015-04-22 13:56:20 -0700378 << " to " << PrettyMethod(m);
Ian Rogers53b8b092014-03-13 23:45:53 -0700379 ++error_count;
380 }
381 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
382 offset++;
Ian Rogers68d8b422014-07-17 11:09:10 -0700383 } else {
384 int32_t arg = static_cast<int32_t>(args[i + offset]);
385 if (param_type->IsPrimitiveBoolean()) {
386 if (arg != JNI_TRUE && arg != JNI_FALSE) {
387 LOG(ERROR) << "JNI ERROR (app bug): expected jboolean (0/1) but got value of "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700388 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(m);
Ian Rogers68d8b422014-07-17 11:09:10 -0700389 ++error_count;
390 }
391 } else if (param_type->IsPrimitiveByte()) {
392 if (arg < -128 || arg > 127) {
393 LOG(ERROR) << "JNI ERROR (app bug): expected jbyte 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->IsPrimitiveChar()) {
398 if (args[i + offset] > 0xFFFF) {
399 LOG(ERROR) << "JNI ERROR (app bug): expected jchar 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->IsPrimitiveShort()) {
404 if (arg < -32768 || arg > 0x7FFF) {
405 LOG(ERROR) << "JNI ERROR (app bug): expected jshort 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 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700410 }
411 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700412 if (UNLIKELY(error_count > 0)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700413 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
414 // with an argument.
Ian Rogers68d8b422014-07-17 11:09:10 -0700415 vm->JniAbortF(nullptr, "bad arguments passed to %s (see above for details)",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700416 PrettyMethod(m).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700417 }
418}
419
Mathieu Chartiere401d142015-04-22 13:56:20 -0700420static ArtMethod* FindVirtualMethod(mirror::Object* receiver, ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700421 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700422 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method, sizeof(void*));
Ian Rogers53b8b092014-03-13 23:45:53 -0700423}
424
425
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700426static void InvokeWithArgArray(const ScopedObjectAccessAlreadyRunnable& soa,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700427 ArtMethod* method, ArgArray* arg_array, JValue* result,
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700428 const char* shorty)
Mathieu Chartier90443472015-07-16 20:32:27 -0700429 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700430 uint32_t* args = arg_array->GetArray();
431 if (UNLIKELY(soa.Env()->check_jni)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700432 CheckMethodArguments(soa.Vm(), method->GetInterfaceMethodIfProxy(sizeof(void*)), args);
Ian Rogers53b8b092014-03-13 23:45:53 -0700433 }
434 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, shorty);
435}
436
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700437JValue InvokeWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
438 va_list args)
Mathieu Chartier90443472015-07-16 20:32:27 -0700439 SHARED_REQUIRES(Locks::mutator_lock_) {
Dave Allison648d7112014-07-25 16:15:27 -0700440 // We want to make sure that the stack is not within a small distance from the
441 // protected region in case we are calling into a leaf function whose stack
442 // check has been elided.
443 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
444 ThrowStackOverflowError(soa.Self());
445 return JValue();
446 }
447
Mathieu Chartiere401d142015-04-22 13:56:20 -0700448 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao39b6c242015-05-19 20:30:23 -0700449 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
450 if (is_string_init) {
451 // Replace calls to String.<init> with equivalent StringFactory call.
452 method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
453 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700454 mirror::Object* receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object*>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700455 uint32_t shorty_len = 0;
456 const char* shorty = method->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700457 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700458 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700459 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700460 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700461 if (is_string_init) {
462 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700463 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700464 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700465 return result;
466}
467
Jeff Hao39b6c242015-05-19 20:30:23 -0700468JValue InvokeWithJValues(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
469 jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700470 // We want to make sure that the stack is not within a small distance from the
471 // protected region in case we are calling into a leaf function whose stack
472 // check has been elided.
473 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
474 ThrowStackOverflowError(soa.Self());
475 return JValue();
476 }
477
Mathieu Chartiere401d142015-04-22 13:56:20 -0700478 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao39b6c242015-05-19 20:30:23 -0700479 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
480 if (is_string_init) {
481 // Replace calls to String.<init> with equivalent StringFactory call.
482 method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
483 }
484 mirror::Object* receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object*>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700485 uint32_t shorty_len = 0;
486 const char* shorty = method->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700487 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700488 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700489 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700490 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700491 if (is_string_init) {
492 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700493 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700494 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700495 return result;
496}
497
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700498JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
Jeff Hao39b6c242015-05-19 20:30:23 -0700499 jobject obj, jmethodID mid, jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700500 // We want to make sure that the stack is not within a small distance from the
501 // protected region in case we are calling into a leaf function whose stack
502 // check has been elided.
503 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
504 ThrowStackOverflowError(soa.Self());
505 return JValue();
506 }
507
Jeff Hao39b6c242015-05-19 20:30:23 -0700508 mirror::Object* receiver = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700509 ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Jeff Hao39b6c242015-05-19 20:30:23 -0700510 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
511 if (is_string_init) {
512 // Replace calls to String.<init> with equivalent StringFactory call.
513 method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
514 receiver = nullptr;
515 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700516 uint32_t shorty_len = 0;
517 const char* shorty = method->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700518 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700519 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700520 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700521 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700522 if (is_string_init) {
523 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700524 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700525 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700526 return result;
527}
528
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700529JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
Ian Rogers53b8b092014-03-13 23:45:53 -0700530 jobject obj, jmethodID mid, va_list args) {
Dave Allison648d7112014-07-25 16:15:27 -0700531 // We want to make sure that the stack is not within a small distance from the
532 // protected region in case we are calling into a leaf function whose stack
533 // check has been elided.
534 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
535 ThrowStackOverflowError(soa.Self());
536 return JValue();
537 }
538
Ian Rogers53b8b092014-03-13 23:45:53 -0700539 mirror::Object* receiver = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700540 ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Jeff Hao39b6c242015-05-19 20:30:23 -0700541 bool is_string_init = method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
542 if (is_string_init) {
543 // Replace calls to String.<init> with equivalent StringFactory call.
544 method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
545 receiver = nullptr;
546 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700547 uint32_t shorty_len = 0;
548 const char* shorty = method->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700549 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700550 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700551 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700552 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Jeff Hao39b6c242015-05-19 20:30:23 -0700553 if (is_string_init) {
554 // For string init, remap original receiver to StringFactory result.
Jeff Hao83c81952015-05-27 19:29:29 -0700555 UpdateReference(soa.Self(), obj, result.GetL());
Jeff Hao39b6c242015-05-19 20:30:23 -0700556 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700557 return result;
558}
559
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700560jobject InvokeMethod(const ScopedObjectAccessAlreadyRunnable& soa, jobject javaMethod,
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700561 jobject javaReceiver, jobject javaArgs, size_t num_frames) {
Dave Allison648d7112014-07-25 16:15:27 -0700562 // We want to make sure that the stack is not within a small distance from the
563 // protected region in case we are calling into a leaf function whose stack
564 // check has been elided.
565 if (UNLIKELY(__builtin_frame_address(0) <
566 soa.Self()->GetStackEndForInterpreter(true))) {
567 ThrowStackOverflowError(soa.Self());
568 return nullptr;
569 }
570
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700571 auto* abstract_method = soa.Decode<mirror::AbstractMethod*>(javaMethod);
572 const bool accessible = abstract_method->IsAccessible();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700573 ArtMethod* m = abstract_method->GetArtMethod();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700574
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800575 mirror::Class* declaring_class = m->GetDeclaringClass();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800576 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700577 StackHandleScope<1> hs(soa.Self());
578 Handle<mirror::Class> h_class(hs.NewHandle(declaring_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700579 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), h_class, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800580 return nullptr;
581 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700582 declaring_class = h_class.Get();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700583 }
584
Ian Rogers53b8b092014-03-13 23:45:53 -0700585 mirror::Object* receiver = nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700586 if (!m->IsStatic()) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800587 // Replace calls to String.<init> with equivalent StringFactory call.
588 if (declaring_class->IsStringClass() && m->IsConstructor()) {
589 jmethodID mid = soa.EncodeMethod(m);
590 m = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
591 CHECK(javaReceiver == nullptr);
592 } else {
593 // Check that the receiver is non-null and an instance of the field's declaring class.
594 receiver = soa.Decode<mirror::Object*>(javaReceiver);
595 if (!VerifyObjectIsClass(receiver, declaring_class)) {
596 return nullptr;
597 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700598
Jeff Hao848f70a2014-01-15 13:49:50 -0800599 // Find the actual implementation of the virtual method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700600 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m, sizeof(void*));
Jeff Hao848f70a2014-01-15 13:49:50 -0800601 }
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700602 }
603
604 // Get our arrays of arguments and their types, and check they're the same size.
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700605 auto* objects = soa.Decode<mirror::ObjectArray<mirror::Object>*>(javaArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700606 auto* np_method = m->GetInterfaceMethodIfProxy(sizeof(void*));
607 const DexFile::TypeList* classes = np_method->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700608 uint32_t classes_size = (classes == nullptr) ? 0 : classes->Size();
609 uint32_t arg_count = (objects != nullptr) ? objects->GetLength() : 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800610 if (arg_count != classes_size) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000611 ThrowIllegalArgumentException(StringPrintf("Wrong number of arguments; expected %d, got %d",
Ian Rogers62d6c772013-02-27 08:32:07 -0800612 classes_size, arg_count).c_str());
Ian Rogersa0485602014-12-02 15:48:04 -0800613 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700614 }
615
Jeff Haocb4581a2014-03-28 15:43:37 -0700616 // If method is not set to be accessible, verify it can be accessed by the caller.
Andreas Gampec0d82292014-09-23 10:38:30 -0700617 mirror::Class* calling_class = nullptr;
618 if (!accessible && !VerifyAccess(soa.Self(), receiver, declaring_class, m->GetAccessFlags(),
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700619 &calling_class, num_frames)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000620 ThrowIllegalAccessException(
Andreas Gampec0d82292014-09-23 10:38:30 -0700621 StringPrintf("Class %s cannot access %s method %s of class %s",
622 calling_class == nullptr ? "null" : PrettyClass(calling_class).c_str(),
623 PrettyJavaAccessFlags(m->GetAccessFlags()).c_str(),
624 PrettyMethod(m).c_str(),
625 m->GetDeclaringClass() == nullptr ? "null" :
626 PrettyClass(m->GetDeclaringClass()).c_str()).c_str());
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700627 return nullptr;
628 }
629
Ian Rogers53b8b092014-03-13 23:45:53 -0700630 // Invoke the method.
631 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700632 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700633 const char* shorty = np_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700634 ArgArray arg_array(shorty, shorty_len);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700635 if (!arg_array.BuildArgArrayFromObjectArray(receiver, objects, np_method)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700636 CHECK(soa.Self()->IsExceptionPending());
637 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700638 }
639
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700640 InvokeWithArgArray(soa, m, &arg_array, &result, shorty);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700641
642 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700643 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700644 // If we get another exception when we are trying to wrap, then just use that instead.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700645 jthrowable th = soa.Env()->ExceptionOccurred();
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700646 soa.Self()->ClearException();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700647 jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException");
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700648 if (exception_class == nullptr) {
649 soa.Self()->AssertPendingOOMException();
650 return nullptr;
651 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700652 jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700653 CHECK(mid != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700654 jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th);
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700655 if (exception_instance == nullptr) {
656 soa.Self()->AssertPendingOOMException();
657 return nullptr;
658 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700659 soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance));
Ian Rogersa0485602014-12-02 15:48:04 -0800660 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700661 }
662
663 // Box if necessary and return.
Ian Rogersa0485602014-12-02 15:48:04 -0800664 return soa.AddLocalReference<jobject>(BoxPrimitive(Primitive::GetType(shorty[0]), result));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700665}
666
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800667mirror::Object* BoxPrimitive(Primitive::Type src_class, const JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700668 if (src_class == Primitive::kPrimNot) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800669 return value.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700670 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700671 if (src_class == Primitive::kPrimVoid) {
672 // There's no such thing as a void field, and void methods invoked via reflection return null.
673 return nullptr;
674 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700675
Ian Rogers84956ff2014-03-26 23:52:41 -0700676 jmethodID m = nullptr;
Ian Rogers0177e532014-02-11 16:30:46 -0800677 const char* shorty;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700678 switch (src_class) {
679 case Primitive::kPrimBoolean:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700680 m = WellKnownClasses::java_lang_Boolean_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800681 shorty = "LZ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700682 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700683 case Primitive::kPrimByte:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700684 m = WellKnownClasses::java_lang_Byte_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800685 shorty = "LB";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700686 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700687 case Primitive::kPrimChar:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700688 m = WellKnownClasses::java_lang_Character_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800689 shorty = "LC";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700690 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700691 case Primitive::kPrimDouble:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700692 m = WellKnownClasses::java_lang_Double_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800693 shorty = "LD";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700694 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700695 case Primitive::kPrimFloat:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700696 m = WellKnownClasses::java_lang_Float_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800697 shorty = "LF";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700698 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700699 case Primitive::kPrimInt:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700700 m = WellKnownClasses::java_lang_Integer_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800701 shorty = "LI";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700702 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700703 case Primitive::kPrimLong:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700704 m = WellKnownClasses::java_lang_Long_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800705 shorty = "LJ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700706 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700707 case Primitive::kPrimShort:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700708 m = WellKnownClasses::java_lang_Short_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800709 shorty = "LS";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700710 break;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700711 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700712 LOG(FATAL) << static_cast<int>(src_class);
Ian Rogers0177e532014-02-11 16:30:46 -0800713 shorty = nullptr;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700714 }
715
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700716 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers53b8b092014-03-13 23:45:53 -0700717 DCHECK_EQ(soa.Self()->GetState(), kRunnable);
Jeff Hao5d917302013-02-27 17:57:33 -0800718
Ian Rogers53b8b092014-03-13 23:45:53 -0700719 ArgArray arg_array(shorty, 2);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800720 JValue result;
Jeff Hao5d917302013-02-27 17:57:33 -0800721 if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) {
722 arg_array.AppendWide(value.GetJ());
723 } else {
724 arg_array.Append(value.GetI());
725 }
726
727 soa.DecodeMethod(m)->Invoke(soa.Self(), arg_array.GetArray(), arg_array.GetNumBytes(),
Ian Rogers0177e532014-02-11 16:30:46 -0800728 &result, shorty);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800729 return result.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700730}
731
Mathieu Chartierc7853442015-03-27 14:35:38 -0700732static std::string UnboxingFailureKind(ArtField* f)
Mathieu Chartier90443472015-07-16 20:32:27 -0700733 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700734 if (f != nullptr) {
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700735 return "field " + PrettyField(f, false);
736 }
737 return "result";
738}
739
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000740static bool UnboxPrimitive(mirror::Object* o,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700741 mirror::Class* dst_class, ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -0700742 JValue* unboxed_value)
Mathieu Chartier90443472015-07-16 20:32:27 -0700743 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700744 bool unbox_for_result = (f == nullptr);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700745 if (!dst_class->IsPrimitive()) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700746 if (UNLIKELY(o != nullptr && !o->InstanceOf(dst_class))) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800747 if (!unbox_for_result) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000748 ThrowIllegalArgumentException(StringPrintf("%s has type %s, got %s",
Ian Rogers84956ff2014-03-26 23:52:41 -0700749 UnboxingFailureKind(f).c_str(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800750 PrettyDescriptor(dst_class).c_str(),
751 PrettyTypeOf(o).c_str()).c_str());
752 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000753 ThrowClassCastException(StringPrintf("Couldn't convert result of type %s to %s",
Ian Rogers62d6c772013-02-27 08:32:07 -0800754 PrettyTypeOf(o).c_str(),
Brian Carlstromdf629502013-07-17 22:39:56 -0700755 PrettyDescriptor(dst_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800756 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700757 return false;
758 }
Ian Rogers84956ff2014-03-26 23:52:41 -0700759 unboxed_value->SetL(o);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700760 return true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800761 }
762 if (UNLIKELY(dst_class->GetPrimitiveType() == Primitive::kPrimVoid)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000763 ThrowIllegalArgumentException(StringPrintf("Can't unbox %s to void",
Ian Rogers84956ff2014-03-26 23:52:41 -0700764 UnboxingFailureKind(f).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700765 return false;
766 }
Ian Rogers84956ff2014-03-26 23:52:41 -0700767 if (UNLIKELY(o == nullptr)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800768 if (!unbox_for_result) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000769 ThrowIllegalArgumentException(StringPrintf("%s has type %s, got null",
Ian Rogers84956ff2014-03-26 23:52:41 -0700770 UnboxingFailureKind(f).c_str(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800771 PrettyDescriptor(dst_class).c_str()).c_str());
772 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000773 ThrowNullPointerException(StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
Ian Rogers62d6c772013-02-27 08:32:07 -0800774 PrettyDescriptor(dst_class).c_str()).c_str());
775 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700776 return false;
777 }
778
Elliott Hughes1d878f32012-04-11 15:17:54 -0700779 JValue boxed_value;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700780 mirror::Class* klass = o->GetClass();
Ian Rogers84956ff2014-03-26 23:52:41 -0700781 mirror::Class* src_class = nullptr;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700782 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700783 ArtField* primitive_field = &klass->GetIFieldsPtr()->At(0);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700784 if (klass->DescriptorEquals("Ljava/lang/Boolean;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700785 src_class = class_linker->FindPrimitiveClass('Z');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700786 boxed_value.SetZ(primitive_field->GetBoolean(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700787 } else if (klass->DescriptorEquals("Ljava/lang/Byte;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700788 src_class = class_linker->FindPrimitiveClass('B');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700789 boxed_value.SetB(primitive_field->GetByte(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700790 } else if (klass->DescriptorEquals("Ljava/lang/Character;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700791 src_class = class_linker->FindPrimitiveClass('C');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700792 boxed_value.SetC(primitive_field->GetChar(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700793 } else if (klass->DescriptorEquals("Ljava/lang/Float;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700794 src_class = class_linker->FindPrimitiveClass('F');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700795 boxed_value.SetF(primitive_field->GetFloat(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700796 } else if (klass->DescriptorEquals("Ljava/lang/Double;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700797 src_class = class_linker->FindPrimitiveClass('D');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700798 boxed_value.SetD(primitive_field->GetDouble(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700799 } else if (klass->DescriptorEquals("Ljava/lang/Integer;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700800 src_class = class_linker->FindPrimitiveClass('I');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700801 boxed_value.SetI(primitive_field->GetInt(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700802 } else if (klass->DescriptorEquals("Ljava/lang/Long;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700803 src_class = class_linker->FindPrimitiveClass('J');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700804 boxed_value.SetJ(primitive_field->GetLong(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700805 } else if (klass->DescriptorEquals("Ljava/lang/Short;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700806 src_class = class_linker->FindPrimitiveClass('S');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700807 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700808 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700809 std::string temp;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000810 ThrowIllegalArgumentException(
Ian Rogers1ff3c982014-08-12 02:30:58 -0700811 StringPrintf("%s has type %s, got %s", UnboxingFailureKind(f).c_str(),
812 PrettyDescriptor(dst_class).c_str(),
813 PrettyDescriptor(o->GetClass()->GetDescriptor(&temp)).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700814 return false;
815 }
816
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000817 return ConvertPrimitiveValue(unbox_for_result,
Ian Rogers62d6c772013-02-27 08:32:07 -0800818 src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700819 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700820}
821
Mathieu Chartierc7853442015-03-27 14:35:38 -0700822bool UnboxPrimitiveForField(mirror::Object* o, mirror::Class* dst_class, ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -0700823 JValue* unboxed_value) {
824 DCHECK(f != nullptr);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000825 return UnboxPrimitive(o, dst_class, f, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700826}
827
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700828bool UnboxPrimitiveForResult(mirror::Object* o, mirror::Class* dst_class, JValue* unboxed_value) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000829 return UnboxPrimitive(o, dst_class, nullptr, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700830}
831
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700832mirror::Class* GetCallingClass(Thread* self, size_t num_frames) {
833 NthCallerVisitor visitor(self, num_frames);
834 visitor.WalkStack();
835 return visitor.caller != nullptr ? visitor.caller->GetDeclaringClass() : nullptr;
836}
837
Andreas Gampec0d82292014-09-23 10:38:30 -0700838bool VerifyAccess(Thread* self, mirror::Object* obj, mirror::Class* declaring_class,
Mathieu Chartierca239af2015-03-29 18:27:50 -0700839 uint32_t access_flags, mirror::Class** calling_class, size_t num_frames) {
Mathieu Chartier76433272014-09-26 14:32:37 -0700840 if ((access_flags & kAccPublic) != 0) {
841 return true;
842 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700843 auto* klass = GetCallingClass(self, num_frames);
844 if (UNLIKELY(klass == nullptr)) {
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100845 // The caller is an attached native thread.
Mathieu Chartier76433272014-09-26 14:32:37 -0700846 return false;
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100847 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700848 *calling_class = klass;
849 return VerifyAccess(self, obj, declaring_class, access_flags, klass);
850}
851
852bool VerifyAccess(Thread* self, mirror::Object* obj, mirror::Class* declaring_class,
853 uint32_t access_flags, mirror::Class* calling_class) {
854 if (calling_class == declaring_class) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700855 return true;
856 }
Andreas Gampec0d82292014-09-23 10:38:30 -0700857 ScopedAssertNoThreadSuspension sants(self, "verify-access");
Jeff Haocb4581a2014-03-28 15:43:37 -0700858 if ((access_flags & kAccPrivate) != 0) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700859 return false;
860 }
Jeff Haocb4581a2014-03-28 15:43:37 -0700861 if ((access_flags & kAccProtected) != 0) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700862 if (obj != nullptr && !obj->InstanceOf(calling_class) &&
863 !declaring_class->IsInSamePackage(calling_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700864 return false;
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700865 } else if (declaring_class->IsAssignableFrom(calling_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700866 return true;
867 }
868 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700869 return declaring_class->IsInSamePackage(calling_class);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700870}
871
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700872void InvalidReceiverError(mirror::Object* o, mirror::Class* c) {
873 std::string expected_class_name(PrettyDescriptor(c));
874 std::string actual_class_name(PrettyTypeOf(o));
875 ThrowIllegalArgumentException(StringPrintf("Expected receiver of type %s, but got %s",
876 expected_class_name.c_str(),
877 actual_class_name.c_str()).c_str());
878}
879
Jeff Hao83c81952015-05-27 19:29:29 -0700880// This only works if there's one reference which points to the object in obj.
881// Will need to be fixed if there's cases where it's not.
882void UpdateReference(Thread* self, jobject obj, mirror::Object* result) {
883 IndirectRef ref = reinterpret_cast<IndirectRef>(obj);
884 IndirectRefKind kind = GetIndirectRefKind(ref);
885 if (kind == kLocal) {
886 self->GetJniEnv()->locals.Update(obj, result);
887 } else if (kind == kHandleScopeOrInvalid) {
888 LOG(FATAL) << "Unsupported UpdateReference for kind kHandleScopeOrInvalid";
889 } else if (kind == kGlobal) {
890 self->GetJniEnv()->vm->UpdateGlobal(self, ref, result);
891 } else {
892 DCHECK_EQ(kind, kWeakGlobal);
893 self->GetJniEnv()->vm->UpdateWeakGlobal(self, ref, result);
894 }
895}
896
Elliott Hughes418d20f2011-09-22 14:00:39 -0700897} // namespace art