blob: 3e1315c73e1713630b0871b867a99b12781b7700 [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"
Elliott Hughes418d20f2011-09-22 14:00:39 -070020#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080021#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070022#include "dex_file-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070023#include "entrypoints/entrypoint_utils.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070024#include "jni_internal.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070025#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/object_array-inl.h"
Jeff Hao11d5d8f2014-03-26 15:08:20 -070028#include "nth_caller_visitor.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070029#include "scoped_thread_state_change.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070030#include "stack.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070031#include "well_known_classes.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070032
Elliott Hughes418d20f2011-09-22 14:00:39 -070033namespace art {
34
Ian Rogers53b8b092014-03-13 23:45:53 -070035class ArgArray {
36 public:
37 explicit ArgArray(const char* shorty, uint32_t shorty_len)
38 : shorty_(shorty), shorty_len_(shorty_len), num_bytes_(0) {
39 size_t num_slots = shorty_len + 1; // +1 in case of receiver.
40 if (LIKELY((num_slots * 2) < kSmallArgArraySize)) {
41 // We can trivially use the small arg array.
42 arg_array_ = small_arg_array_;
43 } else {
44 // Analyze shorty to see if we need the large arg array.
45 for (size_t i = 1; i < shorty_len; ++i) {
46 char c = shorty[i];
47 if (c == 'J' || c == 'D') {
48 num_slots++;
49 }
50 }
51 if (num_slots <= kSmallArgArraySize) {
52 arg_array_ = small_arg_array_;
53 } else {
54 large_arg_array_.reset(new uint32_t[num_slots]);
55 arg_array_ = large_arg_array_.get();
56 }
57 }
58 }
59
60 uint32_t* GetArray() {
61 return arg_array_;
62 }
63
64 uint32_t GetNumBytes() {
65 return num_bytes_;
66 }
67
68 void Append(uint32_t value) {
69 arg_array_[num_bytes_ / 4] = value;
70 num_bytes_ += 4;
71 }
72
73 void Append(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
74 Append(StackReference<mirror::Object>::FromMirrorPtr(obj).AsVRegValue());
75 }
76
77 void AppendWide(uint64_t value) {
Ian Rogers53b8b092014-03-13 23:45:53 -070078 arg_array_[num_bytes_ / 4] = value;
79 arg_array_[(num_bytes_ / 4) + 1] = value >> 32;
80 num_bytes_ += 8;
81 }
82
83 void AppendFloat(float value) {
84 jvalue jv;
85 jv.f = value;
86 Append(jv.i);
87 }
88
89 void AppendDouble(double value) {
90 jvalue jv;
91 jv.d = value;
92 AppendWide(jv.j);
93 }
94
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070095 void BuildArgArrayFromVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
96 mirror::Object* receiver, va_list ap)
Ian Rogers53b8b092014-03-13 23:45:53 -070097 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
98 // Set receiver if non-null (method is not static)
99 if (receiver != nullptr) {
100 Append(receiver);
101 }
102 for (size_t i = 1; i < shorty_len_; ++i) {
103 switch (shorty_[i]) {
104 case 'Z':
105 case 'B':
106 case 'C':
107 case 'S':
108 case 'I':
109 Append(va_arg(ap, jint));
110 break;
111 case 'F':
112 AppendFloat(va_arg(ap, jdouble));
113 break;
114 case 'L':
115 Append(soa.Decode<mirror::Object*>(va_arg(ap, jobject)));
116 break;
117 case 'D':
118 AppendDouble(va_arg(ap, jdouble));
119 break;
120 case 'J':
121 AppendWide(va_arg(ap, jlong));
122 break;
123#ifndef NDEBUG
124 default:
125 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
126#endif
127 }
128 }
129 }
130
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700131 void BuildArgArrayFromJValues(const ScopedObjectAccessAlreadyRunnable& soa,
132 mirror::Object* receiver, jvalue* args)
Ian Rogers53b8b092014-03-13 23:45:53 -0700133 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
134 // Set receiver if non-null (method is not static)
135 if (receiver != nullptr) {
136 Append(receiver);
137 }
138 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
139 switch (shorty_[i]) {
140 case 'Z':
141 Append(args[args_offset].z);
142 break;
143 case 'B':
144 Append(args[args_offset].b);
145 break;
146 case 'C':
147 Append(args[args_offset].c);
148 break;
149 case 'S':
150 Append(args[args_offset].s);
151 break;
152 case 'I':
153 case 'F':
154 Append(args[args_offset].i);
155 break;
156 case 'L':
157 Append(soa.Decode<mirror::Object*>(args[args_offset].l));
158 break;
159 case 'D':
160 case 'J':
161 AppendWide(args[args_offset].j);
162 break;
163#ifndef NDEBUG
164 default:
165 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
166#endif
167 }
168 }
169 }
170
171 void BuildArgArrayFromFrame(ShadowFrame* shadow_frame, uint32_t arg_offset)
172 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
173 // Set receiver if non-null (method is not static)
174 size_t cur_arg = arg_offset;
175 if (!shadow_frame->GetMethod()->IsStatic()) {
176 Append(shadow_frame->GetVReg(cur_arg));
177 cur_arg++;
178 }
179 for (size_t i = 1; i < shorty_len_; ++i) {
180 switch (shorty_[i]) {
181 case 'Z':
182 case 'B':
183 case 'C':
184 case 'S':
185 case 'I':
186 case 'F':
187 case 'L':
188 Append(shadow_frame->GetVReg(cur_arg));
189 cur_arg++;
190 break;
191 case 'D':
192 case 'J':
193 AppendWide(shadow_frame->GetVRegLong(cur_arg));
194 cur_arg++;
195 cur_arg++;
196 break;
197#ifndef NDEBUG
198 default:
199 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
200#endif
201 }
202 }
203 }
204
205 static void ThrowIllegalPrimitiveArgumentException(const char* expected,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700206 const char* found_descriptor)
Ian Rogers53b8b092014-03-13 23:45:53 -0700207 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000208 ThrowIllegalArgumentException(
Ian Rogers53b8b092014-03-13 23:45:53 -0700209 StringPrintf("Invalid primitive conversion from %s to %s", expected,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700210 PrettyDescriptor(found_descriptor).c_str()).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700211 }
212
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700213 bool BuildArgArrayFromObjectArray(mirror::Object* receiver,
Ian Rogersa0485602014-12-02 15:48:04 -0800214 mirror::ObjectArray<mirror::Object>* args,
215 Handle<mirror::ArtMethod> h_m)
Ian Rogers53b8b092014-03-13 23:45:53 -0700216 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersa0485602014-12-02 15:48:04 -0800217 const DexFile::TypeList* classes = h_m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700218 // Set receiver if non-null (method is not static)
219 if (receiver != nullptr) {
220 Append(receiver);
221 }
222 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
223 mirror::Object* arg = args->Get(args_offset);
224 if (((shorty_[i] == 'L') && (arg != nullptr)) || ((arg == nullptr && shorty_[i] != 'L'))) {
225 mirror::Class* dst_class =
Ian Rogersa0485602014-12-02 15:48:04 -0800226 h_m->GetClassFromTypeIndex(classes->GetTypeItem(args_offset).type_idx_, true);
Ian Rogers53b8b092014-03-13 23:45:53 -0700227 if (UNLIKELY(arg == nullptr || !arg->InstanceOf(dst_class))) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000228 ThrowIllegalArgumentException(
Ian Rogers11e4c032014-03-14 12:00:39 -0700229 StringPrintf("method %s argument %zd has type %s, got %s",
Ian Rogersa0485602014-12-02 15:48:04 -0800230 PrettyMethod(h_m.Get(), false).c_str(),
Ian Rogers53b8b092014-03-13 23:45:53 -0700231 args_offset + 1, // Humans don't count from 0.
232 PrettyDescriptor(dst_class).c_str(),
233 PrettyTypeOf(arg).c_str()).c_str());
234 return false;
235 }
236 }
237
238#define DO_FIRST_ARG(match_descriptor, get_fn, append) { \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700239 if (LIKELY(arg != nullptr && arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700240 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700241 append(primitive_field-> get_fn(arg));
242
243#define DO_ARG(match_descriptor, get_fn, append) \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700244 } else if (LIKELY(arg != nullptr && \
245 arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700246 ArtField* primitive_field = arg->GetClass()->GetInstanceField(0); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700247 append(primitive_field-> get_fn(arg));
248
249#define DO_FAIL(expected) \
250 } else { \
251 if (arg->GetClass<>()->IsPrimitive()) { \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700252 std::string temp; \
Mathieu Chartierf8322842014-05-16 10:59:25 -0700253 ThrowIllegalPrimitiveArgumentException(expected, \
Ian Rogers1ff3c982014-08-12 02:30:58 -0700254 arg->GetClass<>()->GetDescriptor(&temp)); \
Ian Rogers53b8b092014-03-13 23:45:53 -0700255 } else { \
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000256 ThrowIllegalArgumentException(\
Ian Rogers11e4c032014-03-14 12:00:39 -0700257 StringPrintf("method %s argument %zd has type %s, got %s", \
Ian Rogersa0485602014-12-02 15:48:04 -0800258 PrettyMethod(h_m.Get(), false).c_str(), \
Ian Rogers53b8b092014-03-13 23:45:53 -0700259 args_offset + 1, \
260 expected, \
261 PrettyTypeOf(arg).c_str()).c_str()); \
262 } \
263 return false; \
264 } }
265
266 switch (shorty_[i]) {
267 case 'L':
268 Append(arg);
269 break;
270 case 'Z':
271 DO_FIRST_ARG("Ljava/lang/Boolean;", GetBoolean, Append)
272 DO_FAIL("boolean")
273 break;
274 case 'B':
275 DO_FIRST_ARG("Ljava/lang/Byte;", GetByte, Append)
276 DO_FAIL("byte")
277 break;
278 case 'C':
279 DO_FIRST_ARG("Ljava/lang/Character;", GetChar, Append)
280 DO_FAIL("char")
281 break;
282 case 'S':
283 DO_FIRST_ARG("Ljava/lang/Short;", GetShort, Append)
284 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
285 DO_FAIL("short")
286 break;
287 case 'I':
288 DO_FIRST_ARG("Ljava/lang/Integer;", GetInt, Append)
289 DO_ARG("Ljava/lang/Character;", GetChar, Append)
290 DO_ARG("Ljava/lang/Short;", GetShort, Append)
291 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
292 DO_FAIL("int")
293 break;
294 case 'J':
295 DO_FIRST_ARG("Ljava/lang/Long;", GetLong, AppendWide)
296 DO_ARG("Ljava/lang/Integer;", GetInt, AppendWide)
297 DO_ARG("Ljava/lang/Character;", GetChar, AppendWide)
298 DO_ARG("Ljava/lang/Short;", GetShort, AppendWide)
299 DO_ARG("Ljava/lang/Byte;", GetByte, AppendWide)
300 DO_FAIL("long")
301 break;
302 case 'F':
303 DO_FIRST_ARG("Ljava/lang/Float;", GetFloat, AppendFloat)
304 DO_ARG("Ljava/lang/Long;", GetLong, AppendFloat)
305 DO_ARG("Ljava/lang/Integer;", GetInt, AppendFloat)
306 DO_ARG("Ljava/lang/Character;", GetChar, AppendFloat)
307 DO_ARG("Ljava/lang/Short;", GetShort, AppendFloat)
308 DO_ARG("Ljava/lang/Byte;", GetByte, AppendFloat)
309 DO_FAIL("float")
310 break;
311 case 'D':
312 DO_FIRST_ARG("Ljava/lang/Double;", GetDouble, AppendDouble)
313 DO_ARG("Ljava/lang/Float;", GetFloat, AppendDouble)
314 DO_ARG("Ljava/lang/Long;", GetLong, AppendDouble)
315 DO_ARG("Ljava/lang/Integer;", GetInt, AppendDouble)
316 DO_ARG("Ljava/lang/Character;", GetChar, AppendDouble)
317 DO_ARG("Ljava/lang/Short;", GetShort, AppendDouble)
318 DO_ARG("Ljava/lang/Byte;", GetByte, AppendDouble)
319 DO_FAIL("double")
320 break;
321#ifndef NDEBUG
322 default:
323 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
Ian Rogersa0485602014-12-02 15:48:04 -0800324 UNREACHABLE();
Ian Rogers53b8b092014-03-13 23:45:53 -0700325#endif
326 }
327#undef DO_FIRST_ARG
328#undef DO_ARG
329#undef DO_FAIL
330 }
331 return true;
332 }
333
334 private:
335 enum { kSmallArgArraySize = 16 };
336 const char* const shorty_;
337 const uint32_t shorty_len_;
338 uint32_t num_bytes_;
339 uint32_t* arg_array_;
340 uint32_t small_arg_array_[kSmallArgArraySize];
Ian Rogers700a4022014-05-19 16:49:03 -0700341 std::unique_ptr<uint32_t[]> large_arg_array_;
Ian Rogers53b8b092014-03-13 23:45:53 -0700342};
343
Ian Rogers68d8b422014-07-17 11:09:10 -0700344static void CheckMethodArguments(JavaVMExt* vm, mirror::ArtMethod* m, uint32_t* args)
Ian Rogers53b8b092014-03-13 23:45:53 -0700345 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700346 const DexFile::TypeList* params = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700347 if (params == nullptr) {
348 return; // No arguments so nothing to check.
349 }
350 uint32_t offset = 0;
351 uint32_t num_params = params->Size();
352 size_t error_count = 0;
353 if (!m->IsStatic()) {
354 offset = 1;
355 }
Ian Rogersa0485602014-12-02 15:48:04 -0800356 // TODO: If args contain object references, it may cause problems.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700357 Thread* self = Thread::Current();
358 StackHandleScope<1> hs(self);
359 Handle<mirror::ArtMethod> h_m(hs.NewHandle(m));
Ian Rogers53b8b092014-03-13 23:45:53 -0700360 for (uint32_t i = 0; i < num_params; i++) {
361 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
Ian Rogersa0485602014-12-02 15:48:04 -0800362 mirror::Class* param_type = h_m->GetClassFromTypeIndex(type_idx, true);
Ian Rogers53b8b092014-03-13 23:45:53 -0700363 if (param_type == nullptr) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700364 CHECK(self->IsExceptionPending());
365 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700366 << h_m->GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000367 << self->GetException()->Dump();
Ian Rogers53b8b092014-03-13 23:45:53 -0700368 self->ClearException();
369 ++error_count;
370 } else if (!param_type->IsPrimitive()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700371 // TODO: There is a compaction bug here since GetClassFromTypeIdx can cause thread suspension,
372 // this is a hard to fix problem since the args can contain Object*, we need to save and
373 // restore them by using a visitor similar to the ones used in the trampoline entrypoints.
Ian Rogers68d8b422014-07-17 11:09:10 -0700374 mirror::Object* argument =
375 (reinterpret_cast<StackReference<mirror::Object>*>(&args[i + offset]))->AsMirrorPtr();
Ian Rogers53b8b092014-03-13 23:45:53 -0700376 if (argument != nullptr && !argument->InstanceOf(param_type)) {
377 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
378 << PrettyTypeOf(argument) << " as argument " << (i + 1)
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700379 << " to " << PrettyMethod(h_m.Get());
Ian Rogers53b8b092014-03-13 23:45:53 -0700380 ++error_count;
381 }
382 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
383 offset++;
Ian Rogers68d8b422014-07-17 11:09:10 -0700384 } else {
385 int32_t arg = static_cast<int32_t>(args[i + offset]);
386 if (param_type->IsPrimitiveBoolean()) {
387 if (arg != JNI_TRUE && arg != JNI_FALSE) {
388 LOG(ERROR) << "JNI ERROR (app bug): expected jboolean (0/1) but got value of "
389 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(h_m.Get());
390 ++error_count;
391 }
392 } else if (param_type->IsPrimitiveByte()) {
393 if (arg < -128 || arg > 127) {
394 LOG(ERROR) << "JNI ERROR (app bug): expected jbyte but got value of "
395 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(h_m.Get());
396 ++error_count;
397 }
398 } else if (param_type->IsPrimitiveChar()) {
399 if (args[i + offset] > 0xFFFF) {
400 LOG(ERROR) << "JNI ERROR (app bug): expected jchar but got value of "
401 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(h_m.Get());
402 ++error_count;
403 }
404 } else if (param_type->IsPrimitiveShort()) {
405 if (arg < -32768 || arg > 0x7FFF) {
406 LOG(ERROR) << "JNI ERROR (app bug): expected jshort but got value of "
407 << arg << " as argument " << (i + 1) << " to " << PrettyMethod(h_m.Get());
408 ++error_count;
409 }
410 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700411 }
412 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700413 if (UNLIKELY(error_count > 0)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700414 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
415 // with an argument.
Ian Rogers68d8b422014-07-17 11:09:10 -0700416 vm->JniAbortF(nullptr, "bad arguments passed to %s (see above for details)",
417 PrettyMethod(h_m.Get()).c_str());
Ian Rogers53b8b092014-03-13 23:45:53 -0700418 }
419}
420
421static mirror::ArtMethod* FindVirtualMethod(mirror::Object* receiver,
422 mirror::ArtMethod* method)
423 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
424 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
425}
426
427
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700428static void InvokeWithArgArray(const ScopedObjectAccessAlreadyRunnable& soa,
429 mirror::ArtMethod* method, ArgArray* arg_array, JValue* result,
430 const char* shorty)
Ian Rogers53b8b092014-03-13 23:45:53 -0700431 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
432 uint32_t* args = arg_array->GetArray();
433 if (UNLIKELY(soa.Env()->check_jni)) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700434 CheckMethodArguments(soa.Vm(), method, args);
Ian Rogers53b8b092014-03-13 23:45:53 -0700435 }
436 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, shorty);
437}
438
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700439JValue InvokeWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
440 va_list args)
Ian Rogers53b8b092014-03-13 23:45:53 -0700441 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Dave Allison648d7112014-07-25 16:15:27 -0700442 // We want to make sure that the stack is not within a small distance from the
443 // protected region in case we are calling into a leaf function whose stack
444 // check has been elided.
445 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
446 ThrowStackOverflowError(soa.Self());
447 return JValue();
448 }
449
Ian Rogers53b8b092014-03-13 23:45:53 -0700450 mirror::ArtMethod* method = soa.DecodeMethod(mid);
451 mirror::Object* receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object*>(obj);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700452 uint32_t shorty_len = 0;
453 const char* shorty = method->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700454 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700455 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700456 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700457 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Ian Rogers53b8b092014-03-13 23:45:53 -0700458 return result;
459}
460
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700461JValue InvokeWithJValues(const ScopedObjectAccessAlreadyRunnable& soa, mirror::Object* receiver,
Ian Rogers53b8b092014-03-13 23:45:53 -0700462 jmethodID mid, jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700463 // We want to make sure that the stack is not within a small distance from the
464 // protected region in case we are calling into a leaf function whose stack
465 // check has been elided.
466 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
467 ThrowStackOverflowError(soa.Self());
468 return JValue();
469 }
470
Ian Rogers53b8b092014-03-13 23:45:53 -0700471 mirror::ArtMethod* method = soa.DecodeMethod(mid);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700472 uint32_t shorty_len = 0;
473 const char* shorty = method->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700474 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700475 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700476 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700477 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Ian Rogers53b8b092014-03-13 23:45:53 -0700478 return result;
479}
480
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700481JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
Ian Rogers53b8b092014-03-13 23:45:53 -0700482 mirror::Object* receiver, jmethodID mid, jvalue* args) {
Dave Allison648d7112014-07-25 16:15:27 -0700483 // We want to make sure that the stack is not within a small distance from the
484 // protected region in case we are calling into a leaf function whose stack
485 // check has been elided.
486 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
487 ThrowStackOverflowError(soa.Self());
488 return JValue();
489 }
490
Ian Rogers53b8b092014-03-13 23:45:53 -0700491 mirror::ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700492 uint32_t shorty_len = 0;
493 const char* shorty = method->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700494 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700495 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700496 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700497 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Ian Rogers53b8b092014-03-13 23:45:53 -0700498 return result;
499}
500
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700501JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
Ian Rogers53b8b092014-03-13 23:45:53 -0700502 jobject obj, jmethodID mid, va_list args) {
Dave Allison648d7112014-07-25 16:15:27 -0700503 // We want to make sure that the stack is not within a small distance from the
504 // protected region in case we are calling into a leaf function whose stack
505 // check has been elided.
506 if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
507 ThrowStackOverflowError(soa.Self());
508 return JValue();
509 }
510
Ian Rogers53b8b092014-03-13 23:45:53 -0700511 mirror::Object* receiver = soa.Decode<mirror::Object*>(obj);
512 mirror::ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700513 uint32_t shorty_len = 0;
514 const char* shorty = method->GetShorty(&shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700515 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700516 ArgArray arg_array(shorty, shorty_len);
Ian Rogerse18fdd22014-03-14 13:29:43 -0700517 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700518 InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
Ian Rogers53b8b092014-03-13 23:45:53 -0700519 return result;
520}
521
522void InvokeWithShadowFrame(Thread* self, ShadowFrame* shadow_frame, uint16_t arg_offset,
Ian Rogerse94652f2014-12-02 11:13:19 -0800523 JValue* result) {
Dave Allison648d7112014-07-25 16:15:27 -0700524 // We want to make sure that the stack is not within a small distance from the
525 // protected region in case we are calling into a leaf function whose stack
526 // check has been elided.
527 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
528 ThrowStackOverflowError(self);
529 return;
530 }
Ian Rogerse94652f2014-12-02 11:13:19 -0800531 uint32_t shorty_len;
532 const char* shorty = shadow_frame->GetMethod()->GetShorty(&shorty_len);
533 ArgArray arg_array(shorty, shorty_len);
Ian Rogers53b8b092014-03-13 23:45:53 -0700534 arg_array.BuildArgArrayFromFrame(shadow_frame, arg_offset);
535 shadow_frame->GetMethod()->Invoke(self, arg_array.GetArray(), arg_array.GetNumBytes(), result,
Ian Rogerse94652f2014-12-02 11:13:19 -0800536 shorty);
Ian Rogers53b8b092014-03-13 23:45:53 -0700537}
538
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700539jobject InvokeMethod(const ScopedObjectAccessAlreadyRunnable& soa, jobject javaMethod,
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700540 jobject javaReceiver, jobject javaArgs, bool accessible) {
Dave Allison648d7112014-07-25 16:15:27 -0700541 // We want to make sure that the stack is not within a small distance from the
542 // protected region in case we are calling into a leaf function whose stack
543 // check has been elided.
544 if (UNLIKELY(__builtin_frame_address(0) <
545 soa.Self()->GetStackEndForInterpreter(true))) {
546 ThrowStackOverflowError(soa.Self());
547 return nullptr;
548 }
549
Ian Rogers62f05122014-03-21 11:21:29 -0700550 mirror::ArtMethod* m = mirror::ArtMethod::FromReflectedMethod(soa, javaMethod);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700551
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800552 mirror::Class* declaring_class = m->GetDeclaringClass();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800553 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700554 StackHandleScope<1> hs(soa.Self());
555 Handle<mirror::Class> h_class(hs.NewHandle(declaring_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700556 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), h_class, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800557 return nullptr;
558 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700559 declaring_class = h_class.Get();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700560 }
561
Ian Rogers53b8b092014-03-13 23:45:53 -0700562 mirror::Object* receiver = nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700563 if (!m->IsStatic()) {
564 // Check that the receiver is non-null and an instance of the field's declaring class.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800565 receiver = soa.Decode<mirror::Object*>(javaReceiver);
Ian Rogers53b8b092014-03-13 23:45:53 -0700566 if (!VerifyObjectIsClass(receiver, declaring_class)) {
Ian Rogersa0485602014-12-02 15:48:04 -0800567 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700568 }
569
570 // Find the actual implementation of the virtual method.
571 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m);
572 }
573
574 // Get our arrays of arguments and their types, and check they're the same size.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800575 mirror::ObjectArray<mirror::Object>* objects =
576 soa.Decode<mirror::ObjectArray<mirror::Object>*>(javaArgs);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700577 const DexFile::TypeList* classes = m->GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700578 uint32_t classes_size = (classes == nullptr) ? 0 : classes->Size();
579 uint32_t arg_count = (objects != nullptr) ? objects->GetLength() : 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800580 if (arg_count != classes_size) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000581 ThrowIllegalArgumentException(StringPrintf("Wrong number of arguments; expected %d, got %d",
Ian Rogers62d6c772013-02-27 08:32:07 -0800582 classes_size, arg_count).c_str());
Ian Rogersa0485602014-12-02 15:48:04 -0800583 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700584 }
585
Jeff Haocb4581a2014-03-28 15:43:37 -0700586 // If method is not set to be accessible, verify it can be accessed by the caller.
Andreas Gampec0d82292014-09-23 10:38:30 -0700587 mirror::Class* calling_class = nullptr;
588 if (!accessible && !VerifyAccess(soa.Self(), receiver, declaring_class, m->GetAccessFlags(),
Mathieu Chartierca239af2015-03-29 18:27:50 -0700589 &calling_class, 2)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000590 ThrowIllegalAccessException(
Andreas Gampec0d82292014-09-23 10:38:30 -0700591 StringPrintf("Class %s cannot access %s method %s of class %s",
592 calling_class == nullptr ? "null" : PrettyClass(calling_class).c_str(),
593 PrettyJavaAccessFlags(m->GetAccessFlags()).c_str(),
594 PrettyMethod(m).c_str(),
595 m->GetDeclaringClass() == nullptr ? "null" :
596 PrettyClass(m->GetDeclaringClass()).c_str()).c_str());
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700597 return nullptr;
598 }
599
Ian Rogers53b8b092014-03-13 23:45:53 -0700600 // Invoke the method.
601 JValue result;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700602 uint32_t shorty_len = 0;
603 const char* shorty = m->GetShorty(&shorty_len);
604 ArgArray arg_array(shorty, shorty_len);
605 StackHandleScope<1> hs(soa.Self());
Ian Rogersa0485602014-12-02 15:48:04 -0800606 Handle<mirror::ArtMethod> h_m(hs.NewHandle(m));
607 if (!arg_array.BuildArgArrayFromObjectArray(receiver, objects, h_m)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700608 CHECK(soa.Self()->IsExceptionPending());
609 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700610 }
611
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700612 InvokeWithArgArray(soa, m, &arg_array, &result, shorty);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700613
614 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700615 if (soa.Self()->IsExceptionPending()) {
616 jthrowable th = soa.Env()->ExceptionOccurred();
617 soa.Env()->ExceptionClear();
618 jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException");
619 jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
620 jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th);
621 soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance));
Ian Rogersa0485602014-12-02 15:48:04 -0800622 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700623 }
624
625 // Box if necessary and return.
Ian Rogersa0485602014-12-02 15:48:04 -0800626 return soa.AddLocalReference<jobject>(BoxPrimitive(Primitive::GetType(shorty[0]), result));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700627}
628
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800629mirror::Object* BoxPrimitive(Primitive::Type src_class, const JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700630 if (src_class == Primitive::kPrimNot) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800631 return value.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700632 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700633 if (src_class == Primitive::kPrimVoid) {
634 // There's no such thing as a void field, and void methods invoked via reflection return null.
635 return nullptr;
636 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700637
Ian Rogers84956ff2014-03-26 23:52:41 -0700638 jmethodID m = nullptr;
Ian Rogers0177e532014-02-11 16:30:46 -0800639 const char* shorty;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700640 switch (src_class) {
641 case Primitive::kPrimBoolean:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700642 m = WellKnownClasses::java_lang_Boolean_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800643 shorty = "LZ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700644 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700645 case Primitive::kPrimByte:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700646 m = WellKnownClasses::java_lang_Byte_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800647 shorty = "LB";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700648 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700649 case Primitive::kPrimChar:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700650 m = WellKnownClasses::java_lang_Character_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800651 shorty = "LC";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700652 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700653 case Primitive::kPrimDouble:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700654 m = WellKnownClasses::java_lang_Double_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800655 shorty = "LD";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700656 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700657 case Primitive::kPrimFloat:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700658 m = WellKnownClasses::java_lang_Float_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800659 shorty = "LF";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700660 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700661 case Primitive::kPrimInt:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700662 m = WellKnownClasses::java_lang_Integer_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800663 shorty = "LI";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700664 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700665 case Primitive::kPrimLong:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700666 m = WellKnownClasses::java_lang_Long_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800667 shorty = "LJ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700668 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700669 case Primitive::kPrimShort:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700670 m = WellKnownClasses::java_lang_Short_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800671 shorty = "LS";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700672 break;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700673 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700674 LOG(FATAL) << static_cast<int>(src_class);
Ian Rogers0177e532014-02-11 16:30:46 -0800675 shorty = nullptr;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700676 }
677
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700678 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers53b8b092014-03-13 23:45:53 -0700679 DCHECK_EQ(soa.Self()->GetState(), kRunnable);
Jeff Hao5d917302013-02-27 17:57:33 -0800680
Ian Rogers53b8b092014-03-13 23:45:53 -0700681 ArgArray arg_array(shorty, 2);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800682 JValue result;
Jeff Hao5d917302013-02-27 17:57:33 -0800683 if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) {
684 arg_array.AppendWide(value.GetJ());
685 } else {
686 arg_array.Append(value.GetI());
687 }
688
689 soa.DecodeMethod(m)->Invoke(soa.Self(), arg_array.GetArray(), arg_array.GetNumBytes(),
Ian Rogers0177e532014-02-11 16:30:46 -0800690 &result, shorty);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800691 return result.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700692}
693
Mathieu Chartierc7853442015-03-27 14:35:38 -0700694static std::string UnboxingFailureKind(ArtField* f)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700695 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700696 if (f != nullptr) {
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700697 return "field " + PrettyField(f, false);
698 }
699 return "result";
700}
701
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000702static bool UnboxPrimitive(mirror::Object* o,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700703 mirror::Class* dst_class, ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -0700704 JValue* unboxed_value)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700705 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700706 bool unbox_for_result = (f == nullptr);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700707 if (!dst_class->IsPrimitive()) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700708 if (UNLIKELY(o != nullptr && !o->InstanceOf(dst_class))) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800709 if (!unbox_for_result) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000710 ThrowIllegalArgumentException(StringPrintf("%s has type %s, got %s",
Ian Rogers84956ff2014-03-26 23:52:41 -0700711 UnboxingFailureKind(f).c_str(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800712 PrettyDescriptor(dst_class).c_str(),
713 PrettyTypeOf(o).c_str()).c_str());
714 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000715 ThrowClassCastException(StringPrintf("Couldn't convert result of type %s to %s",
Ian Rogers62d6c772013-02-27 08:32:07 -0800716 PrettyTypeOf(o).c_str(),
Brian Carlstromdf629502013-07-17 22:39:56 -0700717 PrettyDescriptor(dst_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800718 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700719 return false;
720 }
Ian Rogers84956ff2014-03-26 23:52:41 -0700721 unboxed_value->SetL(o);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700722 return true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800723 }
724 if (UNLIKELY(dst_class->GetPrimitiveType() == Primitive::kPrimVoid)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000725 ThrowIllegalArgumentException(StringPrintf("Can't unbox %s to void",
Ian Rogers84956ff2014-03-26 23:52:41 -0700726 UnboxingFailureKind(f).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700727 return false;
728 }
Ian Rogers84956ff2014-03-26 23:52:41 -0700729 if (UNLIKELY(o == nullptr)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800730 if (!unbox_for_result) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000731 ThrowIllegalArgumentException(StringPrintf("%s has type %s, got null",
Ian Rogers84956ff2014-03-26 23:52:41 -0700732 UnboxingFailureKind(f).c_str(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800733 PrettyDescriptor(dst_class).c_str()).c_str());
734 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000735 ThrowNullPointerException(StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
Ian Rogers62d6c772013-02-27 08:32:07 -0800736 PrettyDescriptor(dst_class).c_str()).c_str());
737 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700738 return false;
739 }
740
Elliott Hughes1d878f32012-04-11 15:17:54 -0700741 JValue boxed_value;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700742 mirror::Class* klass = o->GetClass();
Ian Rogers84956ff2014-03-26 23:52:41 -0700743 mirror::Class* src_class = nullptr;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700744 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
745 ArtField* primitive_field = &klass->GetIFields()[0];
Mathieu Chartierf8322842014-05-16 10:59:25 -0700746 if (klass->DescriptorEquals("Ljava/lang/Boolean;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700747 src_class = class_linker->FindPrimitiveClass('Z');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700748 boxed_value.SetZ(primitive_field->GetBoolean(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700749 } else if (klass->DescriptorEquals("Ljava/lang/Byte;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700750 src_class = class_linker->FindPrimitiveClass('B');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700751 boxed_value.SetB(primitive_field->GetByte(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700752 } else if (klass->DescriptorEquals("Ljava/lang/Character;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700753 src_class = class_linker->FindPrimitiveClass('C');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700754 boxed_value.SetC(primitive_field->GetChar(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700755 } else if (klass->DescriptorEquals("Ljava/lang/Float;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700756 src_class = class_linker->FindPrimitiveClass('F');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700757 boxed_value.SetF(primitive_field->GetFloat(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700758 } else if (klass->DescriptorEquals("Ljava/lang/Double;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700759 src_class = class_linker->FindPrimitiveClass('D');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700760 boxed_value.SetD(primitive_field->GetDouble(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700761 } else if (klass->DescriptorEquals("Ljava/lang/Integer;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700762 src_class = class_linker->FindPrimitiveClass('I');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700763 boxed_value.SetI(primitive_field->GetInt(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700764 } else if (klass->DescriptorEquals("Ljava/lang/Long;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700765 src_class = class_linker->FindPrimitiveClass('J');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700766 boxed_value.SetJ(primitive_field->GetLong(o));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700767 } else if (klass->DescriptorEquals("Ljava/lang/Short;")) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700768 src_class = class_linker->FindPrimitiveClass('S');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700769 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700770 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700771 std::string temp;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000772 ThrowIllegalArgumentException(
Ian Rogers1ff3c982014-08-12 02:30:58 -0700773 StringPrintf("%s has type %s, got %s", UnboxingFailureKind(f).c_str(),
774 PrettyDescriptor(dst_class).c_str(),
775 PrettyDescriptor(o->GetClass()->GetDescriptor(&temp)).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700776 return false;
777 }
778
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000779 return ConvertPrimitiveValue(unbox_for_result,
Ian Rogers62d6c772013-02-27 08:32:07 -0800780 src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700781 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700782}
783
Mathieu Chartierc7853442015-03-27 14:35:38 -0700784bool UnboxPrimitiveForField(mirror::Object* o, mirror::Class* dst_class, ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -0700785 JValue* unboxed_value) {
786 DCHECK(f != nullptr);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000787 return UnboxPrimitive(o, dst_class, f, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700788}
789
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000790bool UnboxPrimitiveForResult(mirror::Object* o,
Ian Rogers84956ff2014-03-26 23:52:41 -0700791 mirror::Class* dst_class, JValue* unboxed_value) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000792 return UnboxPrimitive(o, dst_class, nullptr, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700793}
794
Andreas Gampec0d82292014-09-23 10:38:30 -0700795bool VerifyAccess(Thread* self, mirror::Object* obj, mirror::Class* declaring_class,
Mathieu Chartierca239af2015-03-29 18:27:50 -0700796 uint32_t access_flags, mirror::Class** calling_class, size_t num_frames) {
Mathieu Chartier76433272014-09-26 14:32:37 -0700797 if ((access_flags & kAccPublic) != 0) {
798 return true;
799 }
Mathieu Chartierca239af2015-03-29 18:27:50 -0700800 NthCallerVisitor visitor(self, num_frames);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700801 visitor.WalkStack();
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100802 if (UNLIKELY(visitor.caller == nullptr)) {
803 // The caller is an attached native thread.
Mathieu Chartier76433272014-09-26 14:32:37 -0700804 return false;
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100805 }
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700806 mirror::Class* caller_class = visitor.caller->GetDeclaringClass();
Mathieu Chartier76433272014-09-26 14:32:37 -0700807 if (caller_class == declaring_class) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700808 return true;
809 }
Andreas Gampec0d82292014-09-23 10:38:30 -0700810 ScopedAssertNoThreadSuspension sants(self, "verify-access");
811 *calling_class = caller_class;
Jeff Haocb4581a2014-03-28 15:43:37 -0700812 if ((access_flags & kAccPrivate) != 0) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700813 return false;
814 }
Jeff Haocb4581a2014-03-28 15:43:37 -0700815 if ((access_flags & kAccProtected) != 0) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700816 if (obj != nullptr && !obj->InstanceOf(caller_class) &&
817 !declaring_class->IsInSamePackage(caller_class)) {
818 return false;
819 } else if (declaring_class->IsAssignableFrom(caller_class)) {
820 return true;
821 }
822 }
Mathieu Chartier76433272014-09-26 14:32:37 -0700823 return declaring_class->IsInSamePackage(caller_class);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700824}
825
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700826void InvalidReceiverError(mirror::Object* o, mirror::Class* c) {
827 std::string expected_class_name(PrettyDescriptor(c));
828 std::string actual_class_name(PrettyTypeOf(o));
829 ThrowIllegalArgumentException(StringPrintf("Expected receiver of type %s, but got %s",
830 expected_class_name.c_str(),
831 actual_class_name.c_str()).c_str());
832}
833
Elliott Hughes418d20f2011-09-22 14:00:39 -0700834} // namespace art